Sub topic 13
Writing a Minimal Custom Policy Module as a Last Resort
This chapter assumes you can read a security context, relabel files with semanage fcontext and restorecon, set booleans, label ports, and find AVC denials with ausearch and audit2why. You should also know how to put one domain into permissive mode. If you can't yet, review those chapters first.
A custom policy module is a small piece of loadable policy that adds allow rules to the policy already running in the kernel. It works, but it permanently widens what a confined domain may do. Use it only when a label, a boolean, or a port assignment cannot fix the denial.
When a module is the right tool
Work through the cheaper fixes first. Each one is narrower, maintained by the distribution, and survives policy updates.
| Symptom in the denial | Preferred fix | Module needed? |
|---|---|---|
| Target file has the wrong type (`default_t`, `user_home_t`, `tmp_t`) | `semanage fcontext` plus `restorecon` | No |
| `name_bind` denied on a TCP or UDP port | `semanage port -a` | No |
| `audit2why` names a boolean | `setsebool -P` | No |
| Service reads or writes a path the policy never anticipated, with correct labels | Custom module | Yes |
| Third-party daemon with no shipped policy runs as `unconfined_service_t` | Write a real policy, or accept the unconfined state | Usually |
| Denial caused by a bug, such as a service writing to `/etc` | Fix the service or its configuration | No |
A module is appropriate when the access is legitimate, repeatable, and not covered by anything the policy authors provided. A one-off denial from a misconfigured path is not a reason to write one.
How audit2allow turns denials into rules
audit2allow reads AVC records and prints the allow rules that would have permitted them. Each rule has this shape:
allow source_domain target_type:object_class { permissions };For example, allow httpd_t var_lib_t:file { read open getattr }; lets processes in httpd_t read, open and stat files labeled var_lib_t. The tool does not judge whether the access is wise. It permits whatever was denied, so you must review the output.
With -M name, audit2allow also writes a type enforcement source file (name.te) and compiles it into a policy package (name.pp). The .pp file is what semodule installs.
The pipeline looks like this:
Building a module step by step
1. Reproduce the failure with a clean log
Restart the service and trigger the failing action once. A tight window keeps unrelated denials out of the module. Confirm that no label, boolean or port fix applies:
sudo ausearch -m avc -ts recent | audit2whyIf audit2why suggests a boolean or a type change, stop and apply that instead.
2. Generate the module from only the relevant denials
Filter by command name with -c so denials from other programs are excluded. Use --raw so audit2allow receives the original records:
sudo ausearch -m avc -c httpd --raw | audit2allow -M myhttpdThis creates myhttpd.te and myhttpd.pp in the current directory. Run it in a scratch directory you own, not in /etc or /var/www.
3. Read the generated rules
cat myhttpd.teA typical result:
module myhttpd 1.0;
require {
type httpd_t;
type var_lib_t;
class file { getattr open read };
}
#============= httpd_t ==============
allow httpd_t var_lib_t:file { getattr open read };Ask these questions about every allow line:
- Is the target type specific, or a broad one such as
var_lib_t,etc_t,tmp_t,default_t, orshadow_t? - Are the permissions the minimum, or do they include
write,append,execute,execmemorsetattr? - Is the source domain the service you intend, and not a shared one?
Broad rules are often a sign that the file has the wrong label. Here, giving the application's data directory its own type, or a type the policy already allows for httpd, is usually better than letting httpd_t read every var_lib_t file on the system.
4. Install the module
Only after the review:
sudo semodule -i myhttpd.pp5. Retest and confirm
sudo semodule -l | grep myhttpd
sudo ausearch -m avc -ts recentRepeat the failing action. If a new denial appears, the application needed more than one access. Remove the old module, regenerate it from all the relevant denials, and review it again.
Dontaudit rules can hide the real problem
The targeted policy contains dontaudit rules. These suppress the log entry for certain known-harmless denials. If a service fails but the audit log is empty, the denial may be silenced. Disable the suppression temporarily and reproduce the failure:
sudo semodule -DB
# reproduce the problem and read the denials
sudo semodule -B-D disables dontaudit rules and -B rebuilds the policy. Run semodule -B afterward to restore normal behavior. Leaving dontaudit off fills the audit log with noise.
Managing and removing modules
Local modules are stored in the policy store and persist across reboots. List and remove them like this:
sudo semodule -l
sudo semodule -r myhttpdKeep the .te file in version control together with a note explaining why the module exists. Without that record, nobody can tell later whether the rule is still needed.
A policy package built on one release may not load on another if types or classes were renamed. After a major upgrade, rerun the service's tests and check semodule -l for load errors in the journal.
Common mistakes
- Piping every denial into
audit2allow. This captures stray denials, including ones that indicate real attacks or misconfiguration, and turns them into permanent permissions. - Skipping the review. The tool never refuses a request. A generated rule that grants
httpd_twrite access toshadow_twould be installed without complaint. - Reaching for a module before a label. Many modules exist only because a directory kept the type
default_t. - Putting the module in the wrong place. The
.ppfile is only an input tosemodule. Policy is stored in the policy store, not in the working directory. - Forgetting that a module is global. It applies to every process in that domain on the host, not only the one instance that failed.
Summary of decision criteria
Write a module when the access is legitimate, the labels are already correct, no boolean covers it, and the rule is narrow enough that you can explain it in one sentence. In every other case, fix the cause instead.
Exercise
A confined httpd service cannot read /srv/reports, and the audit log shows a denial for httpd_t on default_t:file { read open }. Should you build a custom module with audit2allow? What do you do instead, and which commands do you run?
Show answer
No. The file has the wrong label (default_t), so the fix is a label, not a module. Record a persistent rule and apply it: sudo semanage fcontext -a -t httpd_sys_content_t "/srv/reports(/.*)?" followed by sudo restorecon -Rv /srv/reports. A module allowing httpd_t to read default_t would let httpd read every unlabeled file on the system.
Exercise
You run ausearch -m avc -c httpd --raw | audit2allow -M myhttpd and the generated .te file contains allow httpd_t shadow_t:file { read open };. What should you do, and what is the command that would install the module if you decided it was safe?
Show answer
Do not install it. shadow_t is the type of the password hash file, so the rule would let a compromised httpd read password hashes. Find out why httpd tried to read it, which is probably a bug or misconfiguration, and fix that. The install command would be sudo semodule -i myhttpd.pp, but only after a review that finds every rule narrow and justified.
Exercise
A service fails with a permission error, yet ausearch -m avc -ts recent shows nothing. What can explain this, and how do you check it?
Show answer
A dontaudit rule may be suppressing the log entry. Disable those rules with sudo semodule -DB, reproduce the failure, read the denials with ausearch, then restore normal behavior with sudo semodule -B.