Este contenido no está disponible en el idioma seleccionado.
Chapter 8. Writing a custom SELinux policy
Write and use a custom SELinux policy to confine your applications and increase the security of your host system. The custom policy defines precise access rules tailored to your application requirements.
8.2. Creating and enforcing an SELinux policy for a custom application Copiar enlaceEnlace copiado en el portapapeles!
You can create and enforce a tailored SELinux policy to confine custom applications and increase the security of host systems and users' data.
Because each application has specific requirements, modify this example procedure for creating an SELinux policy that confines a simple daemon according to your use case. See the sepolgen(8), ausearch(8), audit2allow(1), audit2why(1), sealert(8), and restorecon(8) man pages on your system for details about commands used in the procedure.
Prerequisites
-
The
selinux-policy-develpackage and its dependencies are installed on your system.
Procedure
For this example procedure, prepare a simple daemon that opens the
/var/log/messagesfile for writing:Create a new file, and open it in a text editor of your choice, for example:
vi mydaemon.c
$ vi mydaemon.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow Insert the following code:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile the file:
gcc -o mydaemon mydaemon.c
$ gcc -o mydaemon mydaemon.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
systemdunit file for your daemon:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Install and start the daemon:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check that the new daemon is not confined by SELinux:
ps -efZ | grep mydaemon system_u:system_r:unconfined_service_t:s0 root 4117 1 0 16:56 ? 00:00:00 /usr/local/bin/mydaemon
$ ps -efZ | grep mydaemon system_u:system_r:unconfined_service_t:s0 root 4117 1 0 16:56 ? 00:00:00 /usr/local/bin/mydaemonCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Generate a custom policy for the daemon:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Rebuild the system policy with the new policy module using the setup script created by the previous command:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note that the setup script relabels the corresponding part of the file system by using the
restoreconcommand:restorecon -v /usr/local/bin/mydaemon /usr/lib/systemd/system
restorecon -v /usr/local/bin/mydaemon /usr/lib/systemd/systemCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the daemon, and check that it now runs confined by SELinux:
systemctl restart mydaemon ps -efZ | grep mydaemon system_u:system_r:mydaemon_t:s0 root 8150 1 0 17:18 ? 00:00:00 /usr/local/bin/mydaemon
# systemctl restart mydaemon $ ps -efZ | grep mydaemon system_u:system_r:mydaemon_t:s0 root 8150 1 0 17:18 ? 00:00:00 /usr/local/bin/mydaemonCopy to Clipboard Copied! Toggle word wrap Toggle overflow Because the daemon is now confined by SELinux, SELinux also prevents it from accessing
/var/log/messages. Display the corresponding denial message:ausearch -m AVC -ts recent ... type=AVC msg=audit(1590247112.719:5935): avc: denied { open } for pid=8150 comm="mydaemon" path="/var/log/messages" dev="dm-0" ino=2430831 scontext=system_u:system_r:mydaemon_t:s0 tcontext=unconfined_u:object_r:var_log_t:s0 tclass=file permissive=1 ...# ausearch -m AVC -ts recent ... type=AVC msg=audit(1590247112.719:5935): avc: denied { open } for pid=8150 comm="mydaemon" path="/var/log/messages" dev="dm-0" ino=2430831 scontext=system_u:system_r:mydaemon_t:s0 tcontext=unconfined_u:object_r:var_log_t:s0 tclass=file permissive=1 ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can get additional information also using the
sealerttool:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
audit2allowtool to suggest changes:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Because rules suggested by
audit2allowcan be incorrect for certain cases, use only a part of its output to find the corresponding policy interface. Inspect thelogging_write_generic_logs(mydaemon_t)macro with themacro-expandertool, to see all allow rules the macro provides:Copy to Clipboard Copied! Toggle word wrap Toggle overflow In this case, you can use the suggested interface, because it only provides read and write access to log files and their parent directories. Add the corresponding rule to your type enforcement file:
echo "logging_write_generic_logs(mydaemon_t)" >> mydaemon.te
$ echo "logging_write_generic_logs(mydaemon_t)" >> mydaemon.teCopy to Clipboard Copied! Toggle word wrap Toggle overflow Alternatively, you can add this rule instead of using the interface:
echo "allow mydaemon_t var_log_t:file { open write getattr };" >> mydaemon.te$ echo "allow mydaemon_t var_log_t:file { open write getattr };" >> mydaemon.teCopy to Clipboard Copied! Toggle word wrap Toggle overflow Reinstall the policy:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Check that your application runs confined by SELinux, for example:
ps -efZ | grep mydaemon system_u:system_r:mydaemon_t:s0 root 8150 1 0 17:18 ? 00:00:00 /usr/local/bin/mydaemon
$ ps -efZ | grep mydaemon system_u:system_r:mydaemon_t:s0 root 8150 1 0 17:18 ? 00:00:00 /usr/local/bin/mydaemonCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that your custom application does not cause any SELinux denials:
ausearch -m AVC -ts recent <no matches>
# ausearch -m AVC -ts recent <no matches>Copy to Clipboard Copied! Toggle word wrap Toggle overflow