Sub topic 13
Writing a Minimal Custom Policy Module as a Last Resort
Assume you can read Linux permissions, inspect SELinux contexts, and find AVC records with ausearch. You should also know how to restart a service and reproduce its failure. A custom policy module changes the system's security policy, so create one only after checking labels, booleans, and port types.
When a custom module is appropriate
SELinux commonly blocks a service because a file has the wrong label, a required boolean is disabled, or a port has the wrong type. Fix those causes first. A custom module is appropriate when:
- The service needs a special access pattern that the standard policy does not provide.
- No existing boolean enables the required behavior.
- The file and port labels are correct.
- The denied operation is expected and narrowly scoped.
audit2allow converts AVC denials into candidate policy rules. It does not understand your application's security design. If you feed it unrelated denials, it may create rules that grant far more access than the service needs.
Generate and review the candidate policy
The -c httpd filter selects denials associated with the httpd command. The --raw option preserves the format expected by audit2allow. The -M myhttpd option creates a policy source file named myhttpd.te and a compiled package named myhttpd.pp.
sudo ausearch -m avc -c httpd --raw | audit2allow -M myhttpd
cat myhttpd.teRead every generated rule. Look for unexpected paths, commands, users, or operations. A rule allowing httpd_t to read a file type may be reasonable. A rule allowing broad access to an administrative directory may indicate a labeling error or a compromised process.
Compare each rule with the service's intended behavior:
| Question | Why it matters |
|---|---|
| Is the denied action required by the application? | Removes accidental permissions. |
| Is the target type correct? | Prevents fixing a mislabeled file with a broad rule. |
| Is the access limited to the service domain? | Keeps the exception narrow. |
| Can a boolean or standard label solve it? | Avoids unnecessary local policy. |
Install and test the module
Install the compiled package only after reviewing the source:
sudo semodule -i myhttpd.pp
sudo systemctl restart httpd
sudo ausearch -m avc -ts recentA successful restart does not prove the policy is correct. Test the application path that previously failed. Also test that the service cannot access unrelated sensitive files. Watch for new AVC records. If the module grants access that you no longer need, remove it and regenerate a smaller policy.
sudo semodule -r myhttpdThe module name is the name passed to audit2allow -M. Removing it returns policy behavior to its previous state, but it does not undo unrelated file-label or boolean changes.
A disciplined decision path
Use this order for a denial:
- Confirm that the denial belongs to the expected service.
- Check the file, process, and port contexts.
- Apply a persistent label, boolean, or port-label fix when one exists.
- Reproduce the failure and collect only relevant AVC records.
- Generate a candidate module and remove unjustified rules.
- Install, test, and monitor the module.
- Document why the exception exists and how to remove it.
Exercise
An HTTP service is denied access to a custom document directory. Should you immediately run audit2allow?
Show answer
No. First assign the directory the appropriate persistent file context with semanage fcontext and restorecon. Use a custom module only if the remaining denial represents intentional behavior with no standard policy control.