Sub topic 7
Mandatory Access Control with SELinux or AppArmor
Assume you can use sudo, edit configuration files, and read systemd logs. This chapter uses a test Linux server. Mandatory access control (MAC) adds policy rules that restrict what processes may do, even when normal Unix permissions allow the action.
Discretionary access control (DAC) is the usual Linux model: file owners and administrators choose permissions. MAC adds centrally managed rules that the kernel enforces. A compromised service therefore cannot automatically read every file available to its Unix user.
SELinux and AppArmor
SELinux and AppArmor provide the same security goal but use different policy models.
| System | Policy model | Common distributions | Typical investigation command |
|---|---|---|---|
| SELinux | Labels and security contexts | RHEL, Fedora, Rocky Linux | `ausearch` and `sealert` |
| AppArmor | Program paths and profiles | Ubuntu, Debian, SUSE | `aa-status` and `journalctl` |
SELinux labels files, processes, and ports. A rule might allow the web server domain to read files labeled httpd_sys_content_t but deny access to a user's home directory. AppArmor profiles identify programs by path and list permitted capabilities, files, and network operations.
Check which system is active before changing policy:
if command -v getenforce >/dev/null 2>&1; then
printf 'SELinux: '
getenforce
elif command -v aa-status >/dev/null 2>&1; then
sudo aa-status
else
echo "No SELinux or AppArmor management command found"
fiEnforcing mode and policy states
SELinux normally has three states:
Enforcingblocks actions that violate policy and records them.Permissiveallows the actions but records violations. It is useful for diagnosis, not as a final control.Disableddoes not load SELinux policy and requires a reboot to enable again.
AppArmor profiles commonly use:
enforce, which blocks violations.complain, which records violations without blocking them.unconfined, which means no profile restricts the program.
Check the current state:
getenforce 2>/dev/null || true
sudo aa-status 2>/dev/null || trueDo not disable MAC because an application fails. First identify the denied operation, decide whether the operation is legitimate, and adjust the narrowest policy possible.
A controlled troubleshooting workflow
When SELinux blocks an action, search recent audit events:
sudo ausearch -m AVC -ts recentAn AVC, or Access Vector Cache message, describes a denied SELinux operation. On systems with the analysis tools installed, this can produce a human-readable explanation:
sudo sealert -a /var/log/audit/audit.logFor AppArmor, inspect kernel and service messages:
sudo journalctl -k -g 'apparmor="DENIED"' --since "15 minutes ago"
sudo aa-statusBefore changing policy, verify file ownership, labels, and paths. For SELinux:
ls -Z /srv/www
sudo restorecon -Rv /srv/wwwrestorecon restores the default labels defined for the path. If a custom location should permanently use a web-content label, define that mapping with semanage fcontext, then run restorecon. Avoid chcon for permanent changes because a relabeling operation can remove them.
For AppArmor, place local profile changes in the distribution's local include area when supported, then reload the profile and test the application. Keep local rules narrow and document why each rule exists.
Include MAC checks in acceptance tests after deployments. Confirm the service runs in enforcing mode, review new denials, and verify that policy changes are version-controlled and reversible.
Exercise
A web service cannot read files under /srv/www after a deployment. What should you check before creating an allow rule?
Show answer
Check the service identity, the file permissions, SELinux labels or the AppArmor profile, recent denial logs, and whether the path has the expected default policy mapping.