On a Linux host, file permissions and root decide almost everything, so one exploited service or one escaped container process inherits far more than it should. Mandatory access control (MAC) adds a second, system-enforced decision that the process cannot switch off: the kernel's Linux Security Module (LSM) framework acts as a reference monitor and asks SELinux, AppArmor or another module whether each access is allowed. This guide explains the reference monitor idea in practical terms, how the LSM framework works, how SELinux and AppArmor differ, how to choose between them, how MAC applies to Docker and Kubernetes workloads, and how to roll it out and measure it. It is for Linux platform engineers, container platform teams and security architects. For SELinux contexts, booleans and policy modules in depth, use the SELinux Guide; this page sits above it.
Key takeaways
- MAC is enforced by the kernel, not the application. LSM hooks run inside the kernel on security-relevant operations, so even a process running as root is held to the loaded policy.
- SELinux labels objects, AppArmor confines programs. SELinux decides on security contexts attached to every process and file; AppArmor applies per-program profiles, and programs without a profile run unconfined.
- Pick by distribution and team, not by benchmark. Use the module your distribution ships and supports by default; running against the grain costs more than any theoretical strength difference.
- Containers depend on MAC more than hosts do. Docker's docker-default AppArmor profile, SELinux category labels and the Kubernetes Pod Security Standards all assume MAC is on; privileged or unconfined pods remove that layer.
- Roll out through learning modes. AppArmor complain mode and SELinux permissive mode log what would be denied, which makes MAC deployable without outages if you actually read the logs.
The reference monitor in practice
James Anderson's 1972 Computer Security Technology Planning Study defined a reference monitor as the mechanism that validates every reference a program makes against what the subject is authorised to do, and set three principles for its implementation: it must be tamper-proof, always invoked, and small enough to be subject to analysis and tests. NIST SP 800-53 Rev. 5 still carries the idea as AC-25 Reference Monitor, alongside AC-3(3) Mandatory Access Control. See the Classic Security Models Guide for the models these mechanisms enforce.
| Principle | How Linux MAC meets it | Where it breaks in practice |
|---|---|---|
| Always invoked | LSM hooks sit on kernel paths for file, process, socket and IPC operations | Module disabled at boot, profile missing, or a process running unconfined |
| Tamper-proof | Policy is loaded by privileged tooling and enforced in kernel space | Root can switch to permissive or unload profiles unless that is itself restricted and monitored |
| Small enough to verify | Enforcement code is in the kernel; policy is declarative and reviewable | Policies grow large, broad allow rules and unconfined domains make review impractical |
How the Linux Security Module framework works
The kernel documentation describes LSM as a framework that lets security checks be hooked by kernel extensions. Despite the name, these are not loadable kernel modules: they are built into the kernel and selected at build time (CONFIG_DEFAULT_SECURITY, with CONFIG_LSM controlling load order) or at boot with the security= argument. The capability module always runs first, followed by any minor modules, then the major MAC module. The active list, in check order, is in /sys/kernel/security/lsm.
| Module | Type | Model | Typical use |
|---|---|---|---|
| SELinux | Major | Labels (security contexts) on every subject and object; type enforcement, optional MLS/MCS | Red Hat family, Fedora, Android |
| AppArmor | Major | Task-centred profiles loaded from user space; unprofiled tasks unconfined | Ubuntu, Debian, SUSE |
| Smack | Major | Simplified label-based MAC | Embedded systems |
| TOMOYO | Major | Path and process-history based | Niche deployments |
| Landlock | Minor, stackable | Unprivileged processes restrict themselves: filesystem rules, and TCP port rules from ABI v4 | Application self-sandboxing |
| Yama, LoadPin, SafeSetID, IPE | Minor | Targeted restrictions (ptrace scope, module loading source, UID transitions, integrity policy) | Host hardening layers |
The kernel's LSM index states that at most one major module is active alongside the minor ones, so a host is effectively either an SELinux host or an AppArmor host. Landlock is different in kind: it lets any process, including an unprivileged one, add sandbox layers that can only be tightened, never removed, up to 16 layers.
SELinux and AppArmor compared
| Aspect | SELinux | AppArmor |
|---|---|---|
| Decision basis | Security context labels stored on files (extended attributes) and assigned to processes | Profiles attached to programs; rules mostly expressed as paths |
| Default coverage | Targeted policy confines many system services; everything has a label | Only programs with a loaded profile are confined |
| Learning mode | Permissive (global or per domain) | Complain (per profile) |
| Key tools | getenforce, semanage, audit2why | aa-status, aa-complain, aa-enforce, aa-genprof, aa-logprof, apparmor_parser -r |
| Denial evidence | AVC records in the audit log | Kernel log entries with apparmor="DENIED", operation, profile, name, pid and comm |
| Containers | Container type plus unique MCS categories per container | docker-default or containerd default profile, or custom Localhost profiles |
| Learning curve | Steeper; labelling must stay correct after restores and moves | Gentler; profiles read like allow lists |
AppArmor in more depth
On Ubuntu, AppArmor is installed and loaded by default and aa-status shows which profiles are in enforce or complain mode. Profiles live in /etc/apparmor.d/, named after the executable path with slashes replaced by dots (/bin/ping becomes bin.ping), and shared rules come from abstractions such as abstractions/base. Disabling a profile is done by linking it into /etc/apparmor.d/disable/ and unloading it, which makes it easy to audit: list that directory. Ubuntu's documentation notes that explicit deny rules are not logged, so a quiet log does not prove nothing is being blocked.
The kernel documentation is blunt about the default state: AppArmor enforces nothing beyond normal Linux permissions until policy is loaded from user space, and tasks without a profile run unconfined. Coverage, not strength, is therefore the main AppArmor metric.
What a good profile or policy looks like
- Least privilege by path or type: the service can read its own configuration and libraries, write only its data and log directories, and nothing in home directories,
/rootor credential stores. - No shell or interpreter execution unless the service genuinely spawns one; an unexpected
execof/bin/shis the most common sign of exploitation. - Network limited to what the service needs: a database client does not need raw sockets or to listen on ports.
- Capabilities trimmed: profiles can deny Linux capabilities such as
sys_adminorsys_ptraceeven if the process holds them. - Abstractions reviewed, not trusted blindly: shared includes are convenient but can grant more than one service needs.
- Policy as code: profiles and SELinux modules live in version control, pass review, and deploy through the same pipeline as the service, so a change in behaviour and its policy change ship together.
Test each new or changed profile against the service's full lifecycle, not just normal requests: start-up, log rotation, certificate renewal, backups and upgrades are where denials appear first.
Choosing a MAC approach
- Follow the distribution: run SELinux on RHEL, Fedora and derivatives and AppArmor on Ubuntu, Debian and SUSE. Vendor policies, package scripts and support cases assume the default.
- Need multi-level or category separation: SELinux MLS/MCS gives label-based separation between tenants and containers that AppArmor's program-centred profiles do not model.
- Small team, mostly third-party services: AppArmor profiles shipped by packages plus complain-mode learning for your own daemons is the lower-effort path.
- Your own application wants to drop privileges: use Landlock (and seccomp) inside the application regardless of the host MAC; it needs no root and stacks on top.
- Mixed estate: standardise on metrics (coverage, unconfined count, denial review) rather than forcing one module everywhere.
Small versus large organisations: a small team gets most of the value by keeping the distribution default enforcing, never disabling it, and confining its two or three internet-facing services. A large estate needs a policy owner, a profile pipeline, an exception register with expiry dates, and confinement coverage reported per platform alongside patch compliance.
MAC for containers and Kubernetes
Containers share the host kernel, so MAC is one of the few controls that still applies after a process breaks out of its namespace view (MITRE ATT&CK T1611 Escape to Host).
- Docker: Docker generates a profile called
docker-defaultfrom a built-in template, loads it into the kernel and applies it to containers unless--security-opt apparmor=names another. Docker describes it as moderately protective with wide compatibility, so high-risk workloads deserve a custom profile. - Kubernetes AppArmor: stable since Kubernetes 1.31 and set with
securityContext.appArmorProfile.type:RuntimeDefault,Localhost(withlocalhostProfile) orUnconfined. Before v1.30 annotations were used. The kubelet rejects a pod whose profile is not loaded, and Kubernetes has no built-in mechanism to load profiles onto nodes, so use node configuration management or the Kubernetes Security Profiles Operator. - Kubernetes SELinux:
seLinuxOptionssets the user, role, type and level (for examplelevel: "s0:c123,c456"). Volume relabelling now uses a mount option by default where eligible;seLinuxChangePolicy: Recursiveopts out for volumes shared by pods with different labels, such as a privilegedspc_tpod and an unprivileged one. The Kubernetes documentation recordsSELinuxChangePolicyas GA in 1.36 andSELinuxMountas GA and enabled by default in 1.37, and recommends enabling the SELinuxWarningController and checking theselinux_warning_controller_selinux_volume_conflictmetric before upgrading. - Pod Security Standards: the Baseline profile expects the
RuntimeDefaultAppArmor profile to apply on supported hosts and forbids overriding or disabling it (or limits overrides to an allowed set). It restricts the SELinux type and forbids custom SELinux user or role. Enforce Baseline or Restricted with Pod Security Admission so developers cannot simply setUnconfined.
See the Kubernetes Security Guide and the Container Security Guide.
Rolling out mandatory access control
- Confirm the module is active: read
/sys/kernel/security/lsmon every image, and for AppArmor/sys/module/apparmor/parameters/enabled. Fail the build if MAC is missing. - Inventory confinement: list unconfined processes (AppArmor
aa-status, SELinux processes in unconfined domains) and rank by exposure: internet-facing first, then services holding credentials. - Learn before enforcing: put new profiles in complain mode, or a single SELinux domain in permissive mode, on a pilot ring. Exercise the service with real traffic and its maintenance jobs, then refine with
aa-logprofor the SELinux tooling. - Enforce by ring: move pilot hosts to enforce, then wider rings. Keep the change in configuration management so it is reviewable.
- Rollback plan: switch one profile to complain or one domain to permissive; never disable MAC host-wide as a fix. Record every rollback as an exception with an expiry.
- Bring workloads in: require
RuntimeDefaultat minimum for every pod through Pod Security Admission, then build Localhost profiles for high-risk workloads.
Detection and measurement
MAC is also a detection source. A denial on a production service is either a policy bug or someone trying something the service never does, and both need a ticket. Forward AVC records and AppArmor DENIED entries to the SIEM and alert on:
- Mode changes:
setenforce 0, profiles moved to complain or into the disable directory, or MAC disabled on the kernel command line. Map these to ATT&CK T1685 Disable or Modify Tools. - Denials from internet-facing services: especially attempts to execute shells, read credential files or open unexpected sockets.
- Pods admitted as Unconfined or privileged: from Kubernetes audit logs.
| Metric | Target | Source |
|---|---|---|
| Hosts with MAC enforcing | 100% of production images | Configuration scan of /sys/kernel/security/lsm and mode |
| Internet-facing services confined | 100% | aa-status or SELinux domain inventory |
| Pods running Unconfined or privileged | Zero outside approved exceptions | Admission and audit logs |
| Open complain or permissive exceptions past expiry | Zero | Exception register |
| Median time to triage a production denial | Under one working day | SIEM tickets |
Where identity fits
Many of the files MAC should protect are machine credentials: service account tokens mounted into pods, cloud credentials in home directories, SSH host keys, TLS private keys and secrets agent sockets. Writing profiles that deny a web server read access to those paths limits what a code execution bug can steal. In Kubernetes, MAC does not stop a compromised container using its own projected service account token, so pair it with minimal service account permissions. See the Kubernetes Secrets Security Guide.
Framework mapping
| Framework | Reference | Relevance |
|---|---|---|
| NIST SP 800-53 Rev. 5 | AC-3(3) Mandatory Access Control; AC-25 Reference Monitor | The control itself and its enforcement mechanism |
| NIST SP 800-53 Rev. 5 | SC-39 Process Isolation; CM-7 Least Functionality | Confining services and containers |
| NIST SP 800-53 Rev. 5 | AU-6, SI-4 | Reviewing and monitoring denials |
| NIST CSF 2.0 | PR.PS-01; DE.CM-09 | Configuration management and runtime monitoring |
| MITRE ATT&CK | T1611 Escape to Host; T1068 Exploitation for Privilege Escalation; T1685 Disable or Modify Tools | Techniques MAC constrains or that target MAC |
Common failure patterns
- Disabling MAC to fix an outage: one host goes permissive "temporarily" and the image is cloned. Fix the profile or label instead.
- Coverage assumed, not measured: AppArmor is "on" but the service that matters has no profile and runs unconfined.
- Privileged and Unconfined pods: a single privileged DaemonSet removes MAC from the most exposed workloads; Docker notes privileged containers without an explicit profile run unconfined under its custom-template feature.
- Complain mode forever: profiles stay in learning mode, logs fill up and nobody reviews them.
- Treating confinement as full isolation: MAC limits a process, not its legitimate network routes or credentials. The OpenAI and Hugging Face AI agent breach, as reported, involved no kernel escape at all: agents used reachable internal services and harvested service account tokens and instance credentials.
- Relabel surprises: restored files or shared volumes carry wrong SELinux labels, and teams respond by turning SELinux off rather than relabelling.
Practitioner checklist
- Verify MAC is enabled and enforcing in every production image and fail builds that lack it.
- Publish a confinement inventory and close gaps on internet-facing and credential-holding services first.
- Use complain or permissive mode only on pilot rings, with an expiry for every exception.
- Enforce Pod Security Admission at Baseline or Restricted so pods cannot opt out of RuntimeDefault.
- Load custom AppArmor profiles onto nodes through configuration management or the Security Profiles Operator.
- Write deny rules for credential paths and token mounts in service profiles.
- Send AVC and AppArmor denials to the SIEM and alert on any mode change.
- Before Kubernetes upgrades, check the SELinux volume conflict metric.
- Use Landlock or seccomp inside your own applications as an additional layer.
Standards and references
- Linux kernel documentation: Linux Security Module Development and Usage
- Linux kernel documentation: AppArmor
- Linux kernel documentation: Landlock unprivileged access control
- Kubernetes: Restrict a Container's Access to Resources with AppArmor
- Kubernetes: Configure a Security Context for a Pod or Container
- Kubernetes: Pod Security Standards
- Ubuntu Server documentation: AppArmor
- Docker documentation: AppArmor security profiles for Docker
Related NHI Mgmt Group resources: SELinux Guide · Linux Server Hardening Guide · Container Security Guide · Classic Security Models Guide