Sub topic 15
A Repeatable Workflow for Confining a Service
Assume that you can use the Linux command line, sudo, systemctl, and journalctl. You should also understand normal file permissions and have access to an SELinux-enabled RHEL-family system.
SELinux troubleshooting works best as a controlled workflow. Start with the service's intended files, ports, and actions. Then inspect labels and policy decisions. Change the narrowest setting that solves the problem. Finally, test the service again and record the change.
1. Establish a baseline
Record the service status, its configuration paths, and its runtime identity. The runtime identity includes the SELinux domain of the process. For Apache, the domain is commonly httpd_t.
sudo systemctl status httpd
ps -eZ | grep httpd
getenforce
sudo semanage port -l | grep '^http_port_t'List the files that the service must read or write. Check both Unix permissions and SELinux labels:
namei -l /srv/web/index.html
ls -lZ /srv/web/index.htmlThe Unix permission bits may allow access while the SELinux type still denies it. For web content, a read-only document commonly uses the httpd_sys_content_t type. Writable application data needs a more specific type, such as httpd_sys_rw_content_t, when the policy supports that use.
2. Apply standard fixes first
Use persistent file-labeling rules for custom paths. restorecon applies those rules to existing files.
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
sudo restorecon -Rv /srv/web
ls -Zd /srv/webUse a boolean when the policy already supports the required behavior. A boolean is a policy switch that enables a defined feature without creating a new policy module.
getsebool httpd_can_network_connect
sudo setsebool -P httpd_can_network_connect onThe -P option makes the boolean persistent across reboots. Without it, the change affects the current running policy only.
For a non-standard listening port, assign the port an allowed SELinux type:
sudo semanage port -a -t http_port_t -p tcp 8888
sudo systemctl restart httpd3. Read denials instead of guessing
An AVC denial records an SELinux decision that blocked an operation. Reproduce the failure, then inspect recent records:
sudo ausearch -m avc -ts recent
sudo ausearch -m avc -ts recent | audit2whyPay attention to the source domain, target type, requested permission, and target path. These fields tell you whether the problem is a mislabeled file, a missing port label, a disabled boolean, or an operation that the policy does not support.
| Finding | Usual first action |
|---|---|
| Wrong file type | Add an `fcontext` rule and run `restorecon` |
| Unsupported service behavior | Check relevant booleans |
| Unlabeled or wrong port type | Add a `semanage port` rule |
| No label or boolean explains it | Investigate policy and application design |
4. Debug narrowly and finish with validation
If you need more evidence, make only the affected domain permissive:
sudo semanage permissive -a httpd_t
# Reproduce the failure and inspect the AVC records.
sudo ausearch -m avc -ts recent
sudo semanage permissive -d httpd_tThis logs denials for httpd_t without allowing every domain to bypass SELinux. Remove the exception immediately after testing.
Use audit2allow only after checking labels, booleans, ports, and application configuration. A generated rule may allow more access than intended. If a module is necessary, review its source before installation and test it under normal enforcing mode.
Document the final labels, booleans, ports, module name, and validation steps. This makes the configuration repeatable on another server.
Exercise
Apache serves files from /srv/web, but SELinux denies access. What should you check and change first?
Show answer
Check the file context with ls -lZ, create a persistent semanage fcontext rule for /srv/web, run restorecon, and retest before considering a custom policy module.