Sub topic 11
Debugging with a Permissive Domain Instead of Disabling SELinux
This chapter assumes you can read security contexts and AVC denials, and that you know what a domain such as httpd_t is. It also assumes you are comfortable with sudo, systemctl and journalctl. The examples use Apache httpd on a RHEL-family system.
The problem with the big switch
When a service stops working and SELinux is the suspect, the quickest test is setenforce 0. That puts the whole system into permissive mode. It works as a test, but it has costs:
- Every confined service stops being enforced, not only the one you are debugging.
- Any denials from unrelated services are mixed into your log, which makes the useful ones harder to find.
- You may forget to switch back. A service that was tested only in permissive mode can fail the day enforcing mode returns.
- Editing
/etc/selinux/configtoSELINUX=disabledis worse. It removes labels from new files, and re-enabling later needs a full relabel and a reboot.
SELinux has a narrower tool. A single domain can be made permissive while the rest of the system stays in enforcing mode. Processes running in that domain are allowed to do everything, but each action the policy would have blocked is still written to the audit log.
How a permissive domain works
In the targeted policy, each confined service runs in a domain, a type attached to its processes. httpd_t is the domain for Apache. A permissive domain is a domain with a "permissive" flag set in the loaded policy.
For that domain only, the kernel does two things:
- It allows the access.
- It logs an AVC record saying the access would have been denied. The record ends with
permissive=1instead ofpermissive=0.
Other domains, such as sshd_t or mysqld_t, are still enforced. A compromised SSH daemon is still confined while you debug Apache.
Making one domain permissive
First confirm the system is enforcing and find the domain of the running service:
getenforce
ps -eZ | grep httpdThe second command should show system_u:system_r:httpd_t:s0 for the Apache processes. If you see unconfined_service_t instead, the service is not confined. That usually means its executable has the wrong label, and making a domain permissive will not help.
Now make httpd_t permissive, reproduce the problem, read the denials and remove the exception:
sudo semanage permissive -a httpd_t
# reproduce the problem, then review the logged denials
sudo ausearch -m avc -ts recent
sudo semanage permissive -d httpd_tThe -a option adds the exception and -d deletes it. You can list current exceptions with sudo semanage permissive -l.
The change is stored as a small policy module named permissive_httpd_t. It is persistent, so it survives a reboot. This differs from setenforce 0, which does not survive a reboot. If you forget to remove the exception, it stays in place. Check semanage permissive -l or semodule -l | grep permissive at the end of every debugging session.
The semanage permissive command needs the policycoreutils-python-utils package on RHEL 8 and later. Installing it is a one-time step.
Why this finds problems enforcing mode hides
In enforcing mode, a process usually stops at the first denied operation. You fix that one, restart, and hit the next. A service that needs five permissions takes five rounds.
In a permissive domain, the process continues past each would-be denial, so the log contains all of them from one run. This is the main reason to use it. You can then sort the full list into the right fixes:
| Denial in the log | Typical fix |
|---|---|
| File has the wrong type, such as `default_t` or `user_home_t` | `semanage fcontext` and `restorecon` |
| Service wants to connect out or use a network feature | A boolean such as `httpd_can_network_connect` |
| Bind to a port not labeled for the service | `semanage port -a` |
| None of the above fits | A custom module, reviewed first |
A worked session
Suppose Apache serves content from /srv/web, which you created by hand, and it returns 403 errors even though the Unix permissions are correct.
First, check the log in enforcing mode to confirm SELinux is involved:
sudo ausearch -m avc -ts recent | audit2whyIf that shows httpd_t denied read on files of type var_t or default_t, you already have the answer. Sometimes the output is longer or unclear. In that case, make the domain permissive and run the whole workload:
sudo semanage permissive -a httpd_t
sudo systemctl restart httpd
curl -i http://localhost/
sudo ausearch -m avc -ts recent | audit2whyBecause the domain is permissive, the page loads, and audit2why lists every access the policy would have blocked. Look for each distinct scontext, tcontext and tclass combination. Group them by cause as in the table above.
Apply the fixes one category at a time. For the labeling problem:
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
sudo restorecon -Rv /srv/webThen remove the permissive exception and test again in enforcing mode:
sudo semanage permissive -d httpd_t
sudo systemctl restart httpd
curl -i http://localhost/The final test in enforcing mode is not optional. Without it, you do not know that the fixes you applied cover everything the service needs.
Reading the permissive flag in a record
Each AVC record shows which mode produced it. A trimmed example:
type=AVC msg=audit(1764305000.123:412): avc: denied { read } for pid=2201 comm="httpd" name="index.html" dev="dm-0" ino=1048601 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:default_t:s0 tclass=file permissive=1The record still says denied, but permissive=1 means the access was allowed anyway. Use this field to separate records from a debugging run from records produced by genuine enforcement. When you search later, ausearch -m avc -ts today may return both kinds, so check the field before treating a record as a real failure.
Limits and edge cases
- Dontaudit rules hide some denials. The policy contains
dontauditrules for access that is known to be harmless. These are not logged even in a permissive domain. If you suspect a hidden denial, disable them temporarily withsudo semodule -DB, reproduce the problem, then restore them withsudo semodule -B. Use this sparingly, because the log becomes noisy. - Permissive does not cover everything. The
httpd_texception covers processes running inhttpd_t. Child processes that transition into another domain, such as a CGI script running inhttpd_sys_script_t, are separate domains. Make those permissive too if you see denials involving them. - Only the domain's own actions are relaxed. If another confined process, for example
mysqld_t, is denied while talking to Apache, that denial still happens. - Some denials are not about the domain. Problems caused by a wrong file label on the executable, by Unix permissions or by a firewall will not change when you switch the domain to permissive. If the failure persists with
httpd_tpermissive, SELinux is probably not the cause. - A permissive domain is a debugging state, not a fix. While it is active, a compromise of that service is not contained by SELinux.
Summary of the procedure
- Confirm the failure in enforcing mode with
ausearch -m avc -ts recent. - Find the domain with
ps -eZor from thescontextfield of the denial. - Run
semanage permissive -a <domain>. - Reproduce the full workload, not only the first step that failed.
- Review all denials with
ausearchandaudit2why, and group them by cause. - Fix them with labels, ports, booleans or, last, a custom module.
- Run
semanage permissive -d <domain>. - Repeat the workload in enforcing mode and check for new denials.
Exercise
Apache returns 403 for files in /srv/web. You ran sudo semanage permissive -a httpd_t, and the page now loads. List the steps you must take before you can consider the problem solved, and name two reasons you should not simply leave httpd_t permissive.
Show answer
Read the logged denials with sudo ausearch -m avc -ts recent | audit2why and apply the correct fixes, here probably semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?" followed by restorecon -Rv /srv/web. Then remove the exception with sudo semanage permissive -d httpd_t, restart httpd, and test again in enforcing mode to confirm no new denials appear. Two reasons not to leave it permissive: the domain is no longer confined, so a compromised httpd is not contained by SELinux, and the exception is stored as a persistent policy module, so it survives reboots and is easy to forget.
Exercise
An AVC record ends with permissive=1, but the application worked. What does the field tell you, and why does a permissive domain give a more complete list of needed permissions than enforcing mode?
Show answer
permissive=1 means the access would have been denied by policy but was allowed because the domain (or the whole system) was permissive. In enforcing mode a process normally stops at its first denied operation, so each run reveals only one problem. In a permissive domain the process continues, so one run logs every access the policy would block.