Questo contenuto non è disponibile nella lingua selezionata.
Chapter 8. Writing a custom SELinux policy
To run your applications confined by SELinux, you must write and use a custom policy.
8.2. Creating and enforcing an SELinux policy for a custom application Copia collegamentoCollegamento copiato negli appunti!
You can confine applications by SELinux to 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.
Prerequisites
-
The
selinux-policy-devel
package and its dependencies are installed on your system.
Procedure
For this example procedure, prepare a simple daemon that opens the
/var/log/messages
file for writing:Create a new file, and open it in a text editor of your choice:
vi mydaemon.c
$ vi mydaemon.c
Copy 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.c
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
systemd
unit 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
$ 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
Copy 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 using the
restorecon
command:restorecon -v /usr/local/bin/mydaemon /usr/lib/systemd/system
restorecon -v /usr/local/bin/mydaemon /usr/lib/systemd/system
Copy 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
# 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
Copy 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
# 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
sealert
tool:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
audit2allow
tool to suggest changes:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Because rules suggested by
audit2allow
can 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-expander
tool, 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.te
Copy 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.te
Copy 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
$ ps -efZ | grep mydaemon system_u:system_r:mydaemon_t:s0 root 8150 1 0 17:18 ? 00:00:00 /usr/local/bin/mydaemon
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that your custom application does not cause any SELinux denials:
ausearch -m AVC -ts recent
# ausearch -m AVC -ts recent <no matches>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow