Protego field desk
Advanced Hardening and Threat Hunting · Advanced

L21. SELinux and AppArmor: Mandatory Access Control

Video generating

Check back soon for the video lesson on SELinux and AppArmor: Mandatory Access Control

Course outlineLesson 21 of 23

File permissions stop users from touching each other's files, but they do nothing once an attacker controls a process running as root. SELinux and AppArmor add a second, mandatory layer that confines what each program can do. Learn how both work, how to check they are enforcing, and how to fix a denial without switching protection off.

Why Permissions Are Not Enough

Everything you have configured so far (file permissions, sudo, PAM) is discretionary access control (DAC): the owner of a file decides who can use it, and root can override all of it. If an attacker exploits a web server running as root, or escalates to root, DAC no longer protects anything. Mandatory access control (MAC) adds a policy that the kernel enforces on every process, including root. A confined web server can read its web content and listen on its port, and nothing else, even if it is fully compromised. Linux has two main MAC systems:

SELinuxAppArmor
Default onRHEL, Fedora, Rocky, AlmaLinux, CentOS StreamUbuntu, Debian, SUSE
How it identifies filesLabels (security contexts) stored with each fileFile paths written in each profile
Policy modelEvery process and file has a type; rules allow type to type accessEach program has a profile listing what it may access
Learning curveSteeper, but very fine-grainedEasier to read and write
Check statusgetenforce or sestatusaa-status
Use whichever your distribution ships. The CIS benchmarks require it to be installed and enforcing, not swapped for the other one.

SELinux: Modes and Contexts

SELinux runs in one of three modes:

  • Enforcing: policy is enforced and violations are blocked and logged
  • Permissive: nothing is blocked, but violations are logged (useful for troubleshooting)
  • Disabled: SELinux is off entirely
# Current mode
getenforce

# Full status, including the loaded policy
sestatus

# Switch to permissive until the next reboot (for troubleshooting only)
sudo setenforce 0

# Back to enforcing
sudo setenforce 1

The persistent setting lives in /etc/selinux/config as SELINUX=enforcing.

Every file and process carries a security context in the form user:role:type:level. In practice you only need the type:

# Context of files
ls -Z /var/www/html
# system_u:object_r:httpd_sys_content_t:s0 index.html

# Context of running processes
ps -eZ | grep httpd
# system_u:system_r:httpd_t:s0  1234 ?  00:00:01 httpd

The policy says a process of type httpd_t may read files of type httpd_sys_content_t. If the web server is compromised, it still cannot read /etc/shadow (type shadow_t) or users' home directories.

Fixing an SELinux Denial the Right Way

The most common SELinux problem is not an attack: it is a file with the wrong label after it was moved or created in an unusual place. When something breaks, find the denial first:

# Recent denials from the audit log
sudo ausearch -m AVC -ts recent

# Plain-English explanation and suggested fix (setroubleshoot package)
sudo sealert -a /var/log/audit/audit.log

Then use the smallest fix that matches the cause:

CauseFix
File has the wrong labelsudo restorecon -Rv /var/www/html resets labels to the policy default
Content lives in a custom directorysudo semanage fcontext -a -t httpd_sys_content_t "/srv/site(/.*)?" then sudo restorecon -Rv /srv/site
Feature is off by defaultBooleans, for example sudo setsebool -P httpd_can_network_connect on (list them with getsebool -a)
Service uses a non-standard portsudo semanage port -a -t http_port_t -p tcp 8081
audit2allow can generate a custom policy module from denials, but treat it as a last resort and read what it produces: it allows exactly what was denied, which may include what an attacker tried. If you need time to investigate, put only the one domain in permissive mode (sudo semanage permissive -a httpd_t) instead of the whole system.

AppArmor: Profiles and Modes

AppArmor confines programs with profiles stored in /etc/apparmor.d/. Each profile lists the files, capabilities and network access a program may use, by path. Profiles run in one of two modes:

  • Enforce: violations are blocked and logged
  • Complain: violations are only logged
# Which profiles are loaded, and in which mode
sudo aa-status

# Tools for switching modes and building profiles
sudo apt install apparmor-utils

# Put one profile in complain mode while you troubleshoot
sudo aa-complain /etc/apparmor.d/usr.sbin.cupsd

# Back to enforce
sudo aa-enforce /etc/apparmor.d/usr.sbin.cupsd

# Reload a profile after editing it
sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.cupsd

Denials appear in the kernel log with apparmor="DENIED":

sudo journalctl -k | grep -i 'apparmor="DENIED"'

To confine a program that has no profile yet, aa-genprof watches it run and proposes rules; aa-logprof updates an existing profile from logged events. As with audit2allow, read each suggestion before accepting it.

Containers Use MAC Too

MAC is one of the layers that keeps a compromised container from reaching the host. Docker on Ubuntu applies a default AppArmor profile (docker-default) to containers, and Podman and Docker on RHEL-family hosts label containers with SELinux (container_t). Running containers with --privileged or disabling these profiles removes that layer.

What to Check on Every Server

# SELinux systems: should print Enforcing
getenforce

# AppArmor systems: profiles should be in enforce mode, with none unconfined that should be confined
sudo aa-status

If you find SELinux disabled "because it broke something", the fix is almost always a label, boolean or port from the table above, not turning protection off.

References

Continue Learning

Exam Focus Points
  • ✓Discretionary access control can be overridden by root; mandatory access control is enforced by the kernel on every process, including root
  • ✓SELinux labels files and processes with types and allows access type to type; AppArmor confines programs with path-based profiles
  • ✓SELinux modes are enforcing, permissive and disabled; AppArmor profiles run in enforce or complain mode
  • ✓Fix SELinux denials with restorecon, semanage fcontext, booleans or port labels before considering audit2allow
  • ✓Container runtimes use AppArmor or SELinux to confine containers; --privileged removes that layer
Knowledge Check

1. A web server on a RHEL system cannot read files after they were moved from a home directory into /var/www/html. What is the most likely fix?

2. What does AppArmor complain mode do?

3. Why does mandatory access control still help after an attacker gains root inside a confined service?