Sub topic 2
The targeted policy and what else exists
This chapter assumes you can use sudo, read logs with journalctl, and run a web server such as httpd on a RHEL-family system with SELinux enabled.
The targeted policy and what else exists
SELinux enforces mandatory access control. The kernel checks the SELinux policy in addition to ordinary file permissions, so a process can be blocked even when the file owner allows access. Run sestatus to see the mode and the loaded policy.
Most RHEL-family systems use the targeted policy. It confines selected daemons such as httpd and leaves regular user sessions mostly unconfined. The MLS (multi-level security) policy adds sensitivity levels for high-assurance sites and is uncommon on general servers.
Labeling files persistently with semanage fcontext and restorecon
Each file carries a security context, for example system_u:object_r:httpd_sys_content_t:s0. The type field decides what a confined service may read. The chcon command changes a label temporarily, and a relabel removes the change. Store a rule with semanage fcontext, then apply it with restorecon.
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
sudo restorecon -Rv /srv/web
ls -Zd /srv/webTuning service behavior with booleans
A boolean is an on or off switch in the policy. Check it with getsebool, and add -P to make a change survive a reboot.
getsebool httpd_can_network_connect
sudo setsebool -P httpd_can_network_connect onLetting a service use a non-standard port
Each network port has a type. Web ports carry http_port_t. To let httpd bind to TCP 8888, add a label:
sudo semanage port -a -t http_port_t -p tcp 8888If the port already has a label, use -m to modify it instead of -a.
Finding AVC denials in the audit log
When SELinux blocks an action, it writes an AVC denial to the audit log. Search for recent denials, then ask audit2why to explain them.
sudo ausearch -m avc -ts recent
sudo ausearch -m avc -ts recent | audit2whyExercise
A web server running from /srv/web returns "Permission denied" and the audit log shows an AVC denial for httpdsyscontent_t. You ran chcon earlier and the problem returned after a relabel. What command makes the fix persistent?
Show answer
Store a rule with sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?", then run sudo restorecon -Rv /srv/web.