Notes
Why DAC is not enough
Normal Linux permissions are discretionary access control, DAC: the owner of a file decides who can use it, and a process inherits whatever its user can do. That is fine until a service is compromised. Now the attacker runs as that service's user and can touch everything that user can, read every readable file, connect out to anywhere. Nothing limits the program itself to its legitimate job. That gap is what mandatory access control fills.
- normal Linux perms = DAC
- the file owner decides access
- a process runs as a user and gets everything that user can do
- compromise the web service → the attacker can read every file www-data can read
- nothing limits the program itself
Mandatory access control
Mandatory access control is a system-wide policy, set by the administrator, that defines what each program may do, and that the program cannot override even if it is running as root. The policy for a web server says it may read its web root, bind its port and write its logs, and nothing else. So even a fully compromised web server cannot read the shadow file, cannot spawn a shell, cannot connect wherever it likes. The exploit still runs, but it runs inside a cage.
- a policy, set by the admin, defines what each program may do
- the program cannot grant itself more web server may: read its web root, bind its port, write its logs
- web server may NOT: read /etc/shadow, run a shell, connect anywhere it likes
SELinux
SELinux, the default on Red Hat, Fedora and Android, works with labels. Every file, process and port gets a label called a type, and the policy says which process types may touch which file types. The web server type may read files labelled as web content, and that is it. It runs in three modes: enforcing, which blocks; permissive, which only logs what it would block; and disabled. SELinux is default-deny, so anything the policy does not explicitly allow is denied, which is exactly the property you want.
- everything gets a label (type): files, processes, ports
- policy says which process type may touch which file type
- e.g. httpd_t may read httpd_sys_content_t
- modes: enforcing, permissive (log only), disabled
- default-deny: not allowed = denied
AppArmor
AppArmor, the default on Ubuntu and SUSE, reaches a similar goal differently. Instead of labels, it uses per-program profiles written in terms of file paths. The profile for nginx allows it to read the web directory, write its logs and bind its port, and denies everything else. It has an enforce mode and a complain mode that only logs. Many people find AppArmor easier to read and write than SELinux policy, while SELinux is more fine-grained. Both give you the same core protection.
- per-program profiles, by file path, not labels
- e.g. /usr/sbin/nginx profile: allow read /var/www/** allow its logs, its port deny the rest
- modes: enforce, complain (log only)
- easier to read and write than SELinux policy
Why it matters
This is not theoretical. Mandatory access control blunts real exploit chains. A web shell that tries to spawn a bash process is blocked if the policy does not allow the web server to run shells. A path-traversal bug that tries to read the shadow file is denied because the service may only read its own content. It confines containers too, which is why SELinux is important under Docker and Kubernetes. It does not stop the initial bug, but it turns a full compromise into a contained one.
- limits what a compromised service can read and run
- blocks many web-shell and reverse-shell attempts
- contains path traversal: the service cannot read files outside its allowed set
- confines containers (SELinux + Docker/Kubernetes)
- turns full compromise into limited compromise
The wrong fix
The most common mistake in the field is this: an app breaks after MAC is enabled, so someone disables it and never turns it back on. Do not. A denial means the policy is blocking something; read it. On SELinux, ausearch shows the denial, audit2allow helps craft a rule, and restorecon fixes mislabeled files, the usual cause. On AppArmor, update the profile with aa-logprof. Start in permissive or complain mode, watch what it would block, fix the policy, then enforce.
- app broke after enabling MAC? it is denying something
- SELinux: ausearch -m AVC; audit2allow to craft a rule; restorecon to fix labels
- AppArmor: check the profile, aa-logprof to update it
- start permissive/complain, watch logs, then enforce
- NEVER setenforce 0 and forget
Using it well
To use it well: keep SELinux or AppArmor in enforcing mode, which hardening benchmarks check for. Rely on the profiles that ship with the distribution, since they already confine common services like web servers and databases. For your own application, write a profile or policy that grants least privilege, and test it in permissive mode first. And watch the denials as a detection signal: a sudden spike of a service being denied unusual actions can mean it is being exploited right now.
- keep SELinux/AppArmor enforcing (CIS checks this)
- use the shipped profiles; they cover common services
- write a profile/policy for your own app: least privilege
- test in permissive first
- monitor denials as a signal: a spike can mean an attack
Common mistakes
Mistakes. Running setenforce zero to make an app work and forgetting it, which throws away a whole layer of defense. Disabling it permanently in the config. Blaming SELinux when the real problem is a mislabeled file that restorecon would fix in a second. Leaving your own application with no profile, so only the shipped services are protected. Never reading denials, so you miss both misconfigurations and attacks. And ignoring MAC in containers, where it is one of the few things isolating workloads.
- setenforce 0 to "fix" an app
- disabling it in the config permanently
- mislabeled files, then blaming SELinux
- no profile for your own app
- never reading denials
- ignoring MAC in containers
Say it like this
In the interview: SELinux and AppArmor provide mandatory access control, a policy that limits what each program may do regardless of its user, so a compromised service is confined and cannot, for example, read the shadow file or spawn a shell. I keep it in enforcing mode, use the shipped profiles, write least-privilege profiles for my own applications, and when something breaks I read the denial and fix the policy rather than disabling the whole protection.
- "Mandatory access control: a policy limiting what each program may do, so a compromised service is confined
- and cannot read /etc/shadow or spawn a shell. I keep it
- enforcing, profile my own apps
- least-privilege, and read
- denials instead of disabling
- it."