Sub topic 15
A Repeatable Workflow for Confining a Service
This chapter assumes you have read the earlier chapters on contexts, type enforcement, file labels, booleans, port labels, AVC denials and permissive domains. Here you combine those tools into one fixed procedure. You can then apply it to a new or misbehaving service without guessing.
The examples use Apache httpd on a RHEL-family system. The same steps apply to nginx, a database or any other confined daemon.
Why a fixed order matters
When a service fails under SELinux, the usual temptation is to run setenforce 0 and move on. That removes protection from every process on the host. The opposite temptation is to paste denials into audit2allow and install whatever comes out. That often grants more access than the service needs.
A fixed order avoids both. You try the narrowest, most standard fix first, and you move to a broader one only when the narrower one does not apply. The order is:
- Confirm the service is confined and SELinux is enforcing.
- Reproduce the failure and read the denials.
- Fix labels on files and directories.
- Fix the port label.
- Look for a boolean.
- Collect remaining denials in a permissive domain.
- Write a custom module only if nothing above fits.
- Verify, clean up and record what you changed.
Step 1: Confirm the starting state
Check three facts before you change anything: the mode, the domain of the running process, and the label on the data.
getenforce
ps -eZ | grep httpd
ls -Zd /srv/webYou want Enforcing, a domain such as httpd_t on the processes, and a type on the data that the domain may read, such as httpd_sys_content_t.
If the process runs as unconfined_service_t or init_t, no service-specific policy applies to it. This typically happens when you start a binary from a non-standard path, for example /opt/app/bin/server. The fix is then to label the binary (step 3), not to change booleans.
Step 2: Reproduce and read the denials
Reproduce the failure once, then query the audit log for recent denials.
sudo ausearch -m avc -ts recent
sudo ausearch -m avc -ts recent | audit2whyRead the scontext (the acting domain), tcontext (the target's context), tclass (file, tcp_socket and so on) and the denied permission. These four fields tell you which step to take next:
| What the denial shows | Likely cause | Next step |
|---|---|---|
| `tclass=file` or `dir`, and `tcontext` has an unexpected type such as `default_t` or `user_home_t` | Wrong file label | Step 3 |
| `tclass=tcp_socket`, `name_bind`, and a port number | Unlabeled port | Step 4 |
| `tclass=tcp_socket`, `name_connect` | Outbound connection, often a boolean | Step 5 |
| `audit2why` mentions a boolean | Boolean | Step 5 |
| Several unrelated denials, or none matching the above | Needs wider review | Step 6 |
If you see no denials but the service still fails, SELinux may not be the cause. Check standard permissions, the firewall and the service's own log first. Also note that policy contains dontaudit rules that suppress some denials. To rule that out temporarily, run sudo semodule -DB to disable them, reproduce the problem, then run sudo semodule -B to restore them.
Step 3: Fix file labels
Label the path persistently with semanage fcontext, then apply the rule to the existing files with restorecon.
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
sudo restorecon -Rv /srv/web
ls -Zd /srv/webPreview first with sudo restorecon -Rvn /srv/web. The -n flag reports what would change without changing it.
Three edge cases cause most repeat failures:
chconchanges a label immediately but writes no rule. A laterrestoreconor full relabel reverts it. Usesemanage fcontextfor anything that must survive.mvkeeps the file's old label, whilecpcreates a new file that inherits the label of the destination directory. After moving content into a web root, runrestorecon.- Writable content needs a different type. Serving files needs
httpd_sys_content_t. Letting httpd write to a directory needshttpd_sys_rw_content_torhttpd_sys_content_rw_t, depending on policy version. Check the exact name withsemanage fcontext -l | grep httpd_sys.
Step 4: Fix port labels
If the service must bind to a port that policy does not assign to its type, list the current labels and add the port.
sudo semanage port -l | grep '^http_port_t'
sudo semanage port -a -t http_port_t -p tcp 8888If the port already has a label from a different type, -a fails with an error. In that case use -m (modify) only if you are sure no other service depends on that label.
Step 5: Look for a boolean
Booleans are switches that the policy authors built in for common needs. Search for ones that match the service, read the current value and enable the one you need.
sudo semanage boolean -l | grep httpd_can_network
getsebool httpd_can_network_connect
sudo setsebool -P httpd_can_network_connect onThe -P flag makes the change persistent across reboots. Without it, the change is lost at the next boot. Change one boolean at a time and retest. Some booleans are broad. For example, httpd_unified and httpd_execmem weaken the confinement noticeably. If a narrower boolean such as httpd_can_network_connect_db fits, choose it over the general one.
Step 6: Collect the rest in a permissive domain
If steps 3 to 5 do not clear every denial, put only the service's domain into permissive mode. Denials are then logged but not enforced, so one test run reveals all the remaining denials instead of just the first one.
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_tEnforcing mode stops at the first denied operation, which hides later ones. That is why a single permissive run is more informative. Permissive records show permissive=1 in the AVC message. Remove the exception as soon as you have the data, because the domain is unprotected while it exists.
Step 7: A custom module as a last resort
Only when no label, port or boolean explains the remaining denials should you generate a module. Restrict the input to the service you are fixing, and read the result before installing it.
sudo ausearch -m avc -c httpd --raw | audit2allow -M myhttpd
cat myhttpd.te
sudo semodule -i myhttpd.ppIn myhttpd.te, look for allow rules that grant broad access, such as write access to etc_t or shadow_t, or execmem and execmod permissions. These suggest the application, not the policy, is the problem. Remove any rule you cannot justify. Keep the .te file in version control, since the compiled .pp file is hard to review later.
Step 8: Verify and record
Verification means testing in enforcing mode with no permissive exceptions left.
getenforce
sudo semanage permissive -l
sudo ausearch -m avc -ts recentThe permissive list should show no leftover entries for your domain. Then restart the service and run the real workload, not just a single request. Cron jobs, log rotation and reloads touch files the main request path never reaches. Finally, write down each persistent change: the fcontext rules, port labels, booleans and modules. You can list local customizations at any time with sudo semanage fcontext -l -C, sudo semanage port -l -C and sudo semanage boolean -l -C.
A triage script
This script gathers the facts from steps 1 and 2 in one run. It prints the mode, the process domain, recent denials for the command name, and, if you pass a path, the files that restorecon would relabel.
#!/usr/bin/env bash
set -uo pipefail
comm=${1:?usage: selinux-triage.sh COMMAND_NAME [PATH]}
path=${2:-}
if [ "$(id -u)" -ne 0 ]; then
echo "Run as root (ausearch needs access to the audit log)." >&2
exit 1
fi
echo "== Mode"
getenforce
echo "== Process domains"
ps -eZ | grep -w "$comm" || echo "no running process named $comm"
echo "== Recent AVC denials for $comm"
ausearch -m avc -c "$comm" -ts recent | audit2why || echo "none found"
if [ -n "$path" ]; then
echo "== Labels that restorecon would change under $path"
restorecon -Rvn "$path"
fiRun it as sudo bash selinux-triage.sh httpd /srv/web. It changes nothing, so it is safe to run on a production host.
Exercise
Apache httpd cannot read files you copied into /srv/web, and ausearch -m avc -ts recent shows denials with tcontext=...:default_t:s0. Which step of the workflow applies, and which two commands fix it persistently?
Show answer
The file label step. The files carry default_t, so httpdt is denied. Run `sudo semanage fcontext -a -t httpdsyscontentt "/srv/web(/.*)?" to record the rule, then sudo restorecon -Rv /srv/web to apply it to existing files. Do not use chcon` for this, because its change is lost on relabel, and do not disable SELinux.
Exercise
After fixing labels, httpd as a reverse proxy is still denied with name_connect on a backend port, and no boolean you tried fixes it. What should you do before writing a custom module?
Show answer
Check whether the backend port has the wrong label (semanage port -l). If it does not, put httpdt into permissive mode with `semanage permissive -a httpdt, reproduce the problem to collect all denials, then remove the exception with semanage permissive -d httpd_t. Only if the collected denials are legitimate and no label or boolean covers them, generate a module with audit2allow -M, read the .te file and install it with semodule -i`.