SECURITY ENGINEERING · PART 1 · LINUX AND WINDOWS, FROM THE INSIDE · LINUX · 14 OF 229

SELinux vs AppArmor: Mandatory Access Control

Mandatory access control, the layer that contains a process even after it is compromised: why normal Linux permissions are not enough, how SELinux (labels and policy) and AppArmor (per-program profiles) confine what a service may do, enforcing versus permissive modes, reading denials instead of just disabling it, and why these systems blunt real exploits.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • MAC confines a process beyond its user
  • SELinux labels, AppArmor profiles
  • keep it enforcing
  • read denials, do not disable

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.

Discretionary access control
  • 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.

MAC in one line
  • 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.

SELinux (RHEL, Fedora, Android)
  • 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.

AppArmor (Ubuntu, SUSE)
  • 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.

What it stops
  • 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.

Work with it, not around it
  • 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.

In practice
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What is mandatory access control (MAC)?

A system-wide policy, set by the administrator, that defines what each program may do regardless of its user, and which the program cannot override. It confines a compromised process to its legitimate actions, unlike discretionary permissions that a process inherits from its user.

Q2Why are normal Linux permissions not enough?

They are discretionary: a process runs as a user and gets everything that user can do. When a service is compromised the attacker inherits all of that access. Nothing limits the program itself, which is the gap mandatory access control fills.

Q3How does SELinux work?

It labels every file, process and port with a type, and a policy defines which process types may access which resource types, default-deny. It runs in enforcing (block), permissive (log only) or disabled mode, and is the default on Red Hat, Fedora and Android.

Q4How does AppArmor differ from SELinux?

AppArmor uses per-program profiles written in terms of file paths rather than labels, with enforce and complain modes. It is generally considered easier to read and write, while SELinux is more fine-grained. Both provide the same core confinement; AppArmor is the Ubuntu/SUSE default.

Q5What kinds of attack does MAC blunt?

It limits what a compromised service can read or run, so it blocks many web-shell and reverse-shell attempts, contains path-traversal reads to the service’s allowed files, and confines containers. It does not fix the initial bug but turns full compromise into limited compromise.

Q6What should you do when an app breaks after enabling SELinux?

Read the denial rather than disabling SELinux. Use ausearch to see the AVC denial, audit2allow to craft a correct rule, and restorecon to fix mislabeled files (the usual cause). Start in permissive mode, resolve the denials, then switch to enforcing.

Q7Why is "setenforce 0" a bad habit?

It disables enforcement to make an app work and is often left off, discarding an entire layer of defense. The correct approach is to diagnose the specific denial and fix the policy or file labels so protection stays on.

Q8How can SELinux or AppArmor denials aid detection?

A sudden spike of a service being denied unusual actions, such as trying to spawn a shell or read files outside its set, can indicate the service is being actively exploited, so denials are worth monitoring as a security signal, not just noise.