| |

Docker 53 🐳 Enforcing Linux Security Modules: AppArmor Profiles and SELinux Labels for Containers

⏱️ Estimated read: 21 min | |

Capabilities restrict which operations a process may perform. Seccomp restricts which system calls a process may make. Linux Security Modules go one layer deeper: they restrict which resources a process may touch, even when the operation is permitted and the syscall is allowed. AppArmor does this by matching file paths and network rules. SELinux does it by matching security labels attached to every process and every file. A system typically has one or the other enabled, not both. Ubuntu and Debian ship AppArmor by default. RHEL, Fedora, and CentOS ship SELinux. Containers run under whichever module the host has active, and Docker supplies a default profile for each.

Key point: AppArmor and SELinux are complementary to capabilities and seccomp, not replacements for them. Capabilities answer “may this process perform the operation?” Seccomp answers “may this process make the syscall?” AppArmor and SELinux answer “may this process touch this specific resource?” All three layers are independent, and a container is hardened only when all three are considered.


Why Linux Security Modules exist

The discretionary access control problem. Standard UNIX permissions are discretionary. The owner of a file decides who may read or write it. A root process bypasses these checks entirely through CAP_DAC_OVERRIDE. If a container’s process is compromised and escalated to root, the file permission model provides no further protection. A mandatory access control system does, because its rules are set by the administrator and cannot be overridden by the process, regardless of its user ID.

The path versus label problem. Two models exist for describing what a process may access. The path-based model, used by AppArmor, says “this program may read /etc/nginx/** and nothing else.” The label-based model, used by SELinux, says “this process has label container_t and may read files labeled container_file_t.” Path-based rules are easier to read and write. Label-based rules survive file moves and hard links, because the label travels with the inode.

The container escape problem. A container escape requires the attacker to interact with host resources outside the container’s namespaces. AppArmor and SELinux restrict what those interactions can be. An AppArmor profile that denies execution of /bin/sh blocks a common post-exploitation step. An SELinux label that confines the container to container_t prevents it from reading the host’s /etc/shadow even if the process runs as root inside its namespace.

The Docker default problem. Docker ships a default AppArmor profile called docker-default and, when SELinux is enabled, applies container_t labels to container processes. The defaults are deliberately permissive, chosen to keep most workloads working out of the box. They block the most dangerous operations but allow a great deal that a specific application does not need. Tightening them is the work.

The custom profile problem. Writing a custom AppArmor profile or a custom SELinux policy requires knowing exactly what the application accesses. The empirical approach is to run the application under a permissive profile, observe what it accesses, and then write a profile that allows only those accesses. This is the same approach used for capabilities and seccomp.

The maintenance problem. A profile is a contract. When the application changes, the profile may need to change. A profile that is too tight breaks the application on update. A profile that is too loose provides no protection. Maintaining the balance is ongoing work, and it is why many teams rely on the defaults and accept their limitations.


a. AppArmor profiles

AppArmor attaches a profile to a program by path. When the program executes, the kernel loads its profile and enforces the rules for the lifetime of the process. Every file access, every network operation, and every capability use is checked against the profile.

The default Docker profile

Docker loads docker-default for every container unless a different profile is specified. The profile is stored at /etc/apparmor.d/docker on the host and is generated from a template in the Docker source tree.

# Check which profiles are loaded
sudo aa-status

# Inspect the default Docker profile
cat /etc/apparmor.d/docker

The default profile denies writes to /proc/sys, denies mounting filesystems, and denies access to several kernel interfaces. It allows the application to read and write files inside the container freely.

The basic syntax

An AppArmor profile lists allowed operations and denies everything else by default. The deny keyword makes an explicit denial, which takes precedence over any allow rule.

#include <tunables/global>

profile myapp flags=(attach_disconnected) {
  #include <abstractions/base>

  # Allow reading and writing the application directory.
  /app/** rw,

  # Allow reading configuration.
  /etc/myapp/** r,

  # Deny shell execution.
  deny /bin/sh ix,
  deny /bin/bash ix,

  # Deny writing to the entire filesystem by default.
  deny /** w,
}

The r flag allows reading, w allows writing, and ix allows execution and inherits the current profile. The deny /** w, at the end denies writing anywhere, which combined with the earlier rw on /app/** means the process can write only under /app.

Loading and applying a profile

The profile is written to a file in /etc/apparmor.d/, loaded with apparmor_parser, and applied to a container with --security-opt apparmor=.

# Write the profile
sudo vim /etc/apparmor.d/myapp

# Load it
sudo apparmor_parser -r -W /etc/apparmor.d/myapp

# Verify it is loaded
sudo aa-status | grep myapp

# Run the container with the profile
docker run --security-opt apparmor=myapp myapp:latest

The -r flag replaces an existing profile with the same name, and -W writes the compiled profile to disk. Once loaded, the profile applies to any process that matches its attachment rule.

The Nginx example

Docker’s documentation includes a profile that denies Nginx the ability to execute a shell. The rule is simple, and the protection is substantial: an attacker who compromises Nginx cannot run /bin/sh to escalate further.

profile docker-nginx flags=(attach_disconnected,mediate_deleted) {
  #include <abstractions/base>
  #include <abstractions/nameservice>

  deny /bin/sh ix,
  deny /bin/dash ix,
  deny /usr/bin/top ix,

  /usr/sbin/nginx ix,
  /etc/nginx/** r,
  /var/log/nginx/** rw,
  /var/cache/nginx/** rw,
  /run/nginx.pid rw,

  network inet tcp,
  network inet udp,
}

The profile allows Nginx to run, read its configuration, write its logs and cache, and use TCP and UDP. It denies the three binaries that an attacker would most likely try to execute.

The complain mode

A profile can be set to complain mode, which logs violations without enforcing them. This is the standard way to develop a profile: run in complain mode, observe what the application accesses, then write the enforcing profile based on the observations.

# Set a profile to complain mode
sudo aa-complain /etc/apparmor.d/myapp

# Set it back to enforce mode
sudo aa-enforce /etc/apparmor.d/myapp

# Check the audit log
sudo journalctl -k | grep apparmor

The audit log records each access that the profile would have denied. The application continues to run, and the log becomes the specification for the enforcing profile.

The limits of AppArmor

AppArmor is path-based. A process that accesses a file through a different path with the same inode can bypass path rules. A process that creates a hard link can present a file under a different path. A process that uses openat with a file descriptor passed over a socket can bypass path checks entirely. AppArmor mitigates these with attach_disconnected and mediate_deleted flags, but the model has inherent limits. This is why SELinux exists.


b. SELinux labels

SELinux attaches a security context to every process and every file. The context has four fields: user, role, type, and level. For containers, the type field is the one that matters, and the level field provides per-container isolation through MCS (Multi-Category Security) categories.

The container context

When Docker runs with --selinux-enabled, every container process is labeled system_u:system_r:container_t:s0:c1,c2, where c1,c2 are MCS categories unique to the container. Files inside the container’s writable layer are labeled container_file_t. The policy rule is: container_t processes may read and write container_file_t files, and nothing else.

# Check the host's SELinux status
getenforce

# Inspect a container process label
docker run --rm --security-opt label=type:container_t alpine \
  ps -efZ

# Inspect the labels on files
ls -Z /var/lib/docker/containers/

The -Z flag on ls and ps shows the SELinux context. On a system with SELinux in enforcing mode, the labels are visible and the policy is active.

Enabling SELinux for Docker

Docker must be started with the --selinux-enabled flag. On RHEL and Fedora, this is the default. On other distributions, it must be configured in the daemon configuration.

// /etc/docker/daemon.json
{
  "selinux-enabled": true
}

Red Hat’s container documentation notes that SELinux support is disabled by default in some Docker configurations and describes this as a significant gap, because the mandatory access control is what prevents many container escapes.

The volume label problem

A bind-mounted volume carries its host labels. If the host file is labeled user_home_t and the container process is container_t, the container cannot read it. The :z and :Z flags on the volume mount relabel the files.

# Relabel to allow all containers to access the volume
docker run -v /host/data:/data:z myapp

# Relabel to allow only this container to access the volume
docker run -v /host/data:/data:Z myapp

The :z flag applies the container_file_t label with a shared MCS category, which allows any container to access the files. The :Z flag applies the container_file_t label with the container’s private MCS category, which restricts access to that container alone.

The four context fields

The full SELinux context has four fields, each with a purpose.

system_u:system_r:container_t:s0:c124,c967
│        │        │           │  │
│        │        │           │  └── MCS categories
│        │        │           └───── Sensitivity level
│        │        └───────────────── Type (the key field)
│        └────────────────────────── Role
└─────────────────────────────────── User

The type field is what SELinux policy rules match against. The role and user fields are largely vestigial for containers. The level field carries the MCS categories, which provide per-container isolation even when all containers share the same type.

The MCS isolation

Two containers with the same type but different MCS categories cannot access each other’s files. A process labeled s0:c1,c2 cannot read a file labeled s0:c3,c4. This is what prevents one compromised container from reading another container’s data on the same host.

# Container A labels
docker run --rm alpine ls -Z /data
# system_u:object_r:container_file_t:s0:c1,c2

# Container B labels
docker run --rm alpine ls -Z /data
# system_u:object_r:container_file_t:s0:c3,c4

# Container A cannot read Container B's files even if they are on the same volume.

The policy modules

A custom SELinux policy is written as a module and loaded with semodule. The module defines new types, new rules, or both. For containers, the most common customizations are new types for specific applications, with rules that confine the application more tightly than container_t.

# Compile and install a policy module
checkmodule -M -m -o myapp.mod myapp.te
semodule_package -o myapp.pp -m myapp.mod
sudo semodule -i myapp.pp

Writing a custom policy module is more involved than writing an AppArmor profile. The audit log records denials, and audit2allow generates policy rules from them.

# Generate policy rules from recent denials
sudo ausearch -m avc -ts recent | audit2allow -M myapp

The limits of SELinux

SELinux’s label-based model is more robust than AppArmor’s path-based model against aliasing tricks, because the label travels with the inode and cannot be bypassed by using a different path. The cost is complexity. Writing policy requires understanding types, roles, levels, and the policy language. Many teams rely on the default container policy and the MCS isolation, which are provided by the distribution, rather than writing custom policy.


c. Choosing and combining

AppArmor and SELinux are alternatives, not complements. A host enables one or the other. The choice is determined by the distribution and the team’s familiarity.

The default posture

DistributionDefault LSMDocker default profile
Ubuntu / DebianAppArmordocker-default
RHEL / Fedora / CentOSSELinuxcontainer_t
openSUSEAppArmordocker-default
AlpineNoneNone

The docker-default profile and the container_t type are loaded automatically and provide a baseline. The baseline blocks the most dangerous operations. It does not tailor the rules to the specific application.

The custom profile decision

A custom profile is warranted when the application has a narrow and well-understood set of accesses, and when the cost of a compromise is high. A public-facing service that handles sensitive data justifies the effort. An internal batch job that runs in a trusted network may not.

The decision follows the same logic as capabilities and seccomp: start with the default, tighten it to the application’s actual needs, and document the result. The three layers are reviewed together.

The combination with capabilities and seccomp

The three LSM-adjacent layers address different questions, and all three are relevant.

LayerQuestionExample Rule
CapabilitiesMay the process perform the operation?--cap-drop=ALL
SeccompMay the process make the syscall?Deny ptrace, mount
AppArmorMay the process touch this path?Deny /bin/sh ix
SELinuxMay the process touch this label?container_t may not read shadow_t

A compromise must defeat all four to achieve a container escape. Each layer is independent, so a bug in one does not automatically expose the others.

The audit loop

All three systems write denials to the audit log. The loop is: run in a permissive or complain mode, collect denials, write rules that allow what the application actually needs, and enforce. The commands differ, but the process is the same.

# AppArmor: complain mode, then read the log
sudo aa-complain /etc/apparmor.d/myapp
sudo journalctl -k | grep apparmor

# SELinux: permissive mode, then read the log
sudo setenforce 0
sudo ausearch -m avc -ts recent

# Capabilities: run with everything dropped, observe failures
docker run --cap-drop=ALL myapp

The Kubernetes equivalent

Kubernetes exposes the same controls through the security context of a pod. The appArmorProfile and seLinuxOptions fields set the profile and the context.

securityContext:
  appArmorProfile:
    type: Localhost
    localhostProfile: myapp
  seLinuxOptions:
    type: container_t
    level: "s0:c1,c2"

The manifest encodes the policy in version control, which is the same principle as encoding capabilities and seccomp in the deployment.


Complete Example Session

# ============================================
# PART 1: CHECK THE HOST LSM
# ============================================

# AppArmor
sudo aa-status

# SELinux
getenforce

# Only one is typically active.
# Ubuntu: AppArmor. RHEL: SELinux.

# ============================================
# PART 2: THE DEFAULT DOCKER PROFILE
# ============================================

docker run -d --name default-nginx nginx

# The container runs under docker-default (AppArmor)
# or container_t (SELinux), depending on the host.

# Inspect the AppArmor profile:
sudo aa-status | grep docker-default

# Inspect the SELinux label:
docker exec default-nginx ps -efZ

# ============================================
# PART 3: WRITE A CUSTOM APPARMOR PROFILE
# ============================================

sudo tee /etc/apparmor.d/myapp <<'EOF'
#include <tunables/global>

profile myapp flags=(attach_disconnected,mediate_deleted) {
  #include <abstractions/base>

  # Allow the application binary.
  /app/myapp ix,

  # Allow reading configuration and writing logs.
  /etc/myapp/** r,
  /var/log/myapp/** rw,

  # Deny shell execution.
  deny /bin/sh ix,
  deny /bin/bash ix,
  deny /usr/bin/env ix,

  # Deny everything else by default.
  deny /** w,
}
EOF

# ============================================
# PART 4: LOAD AND APPLY THE PROFILE
# ============================================

sudo apparmor_parser -r -W /etc/apparmor.d/myapp

docker run -d --name hardened-app \
  --security-opt apparmor=myapp \
  myapp:latest

# The container is confined by the custom profile.
# Executing /bin/sh fails.

# ============================================
# PART 5: COMPLAIN MODE FOR DEVELOPMENT
# ============================================

sudo aa-complain /etc/apparmor.d/myapp

docker run -d --name dev-app \
  --security-opt apparmor=myapp \
  myapp:latest

# Exercise the application.
# Then read the log:
sudo journalctl -k | grep apparmor

# Each denial becomes a candidate rule.

# ============================================
# PART 6: ENABLE SELINUX FOR DOCKER
# ============================================

# /etc/docker/daemon.json
{
  "selinux-enabled": true
}

sudo systemctl restart docker

# Verify:
docker info | grep -i selinux

# ============================================
# PART 7: INSPECT CONTAINER LABELS
# ============================================

docker run --rm alpine ls -Z /etc
# system_u:object_r:container_file_t:s0:c1,c2

docker run --rm alpine ps -efZ
# system_u:system_r:container_t:s0:c3,c4

# ============================================
# PART 8: VOLUME LABELING
# ============================================

# Shared volume, accessible by any container
docker run -v /host/data:/data:z myapp

# Private volume, accessible by this container only
docker run -v /host/data:/data:Z myapp

# Without a label flag, the container cannot read the volume
# if the host label is incompatible.

# ============================================
# PART 9: MCS ISOLATION BETWEEN CONTAINERS
# ============================================

docker run -d --name app1 -v /shared:/data:z myapp
docker run -d --name app2 -v /shared:/data:z myapp

# Both have container_file_t with different MCS categories.
# They can both read the shared volume (label :z).
# They cannot read each other's private data.

# ============================================
# PART 10: THE HARDENED BASELINE
# ============================================

docker run -d --name hardened \
  --cap-drop=ALL \
  --cap-add=NET_BIND_SERVICE \
  --security-opt=no-new-privileges \
  --security-opt apparmor=myapp \
  --read-only \
  --tmpfs /tmp \
  --user 1000:1000 \
  myapp:latest

# Capabilities dropped.
# Privilege escalation blocked.
# AppArmor profile applied.
# Filesystem read-only.
# Non-root user.
# Each layer is independent.

The ten parts progressed from checking the host LSM through the default profile, writing and loading a custom AppArmor profile, complain mode, enabling SELinux, inspecting labels, volume labeling, MCS isolation, and the hardened baseline. The same nginx and myapp examples ran through every part, showing how the two modules enforce resource access.


Quick Reference

AppArmor vs SELinux

PropertyAppArmorSELinux
ModelPath-basedLabel-based
Default onUbuntu, Debian, openSUSERHEL, Fedora, CentOS
Docker defaultdocker-defaultcontainer_t
Custom policyProfile per programPolicy module
Audit tooljournalctl -kausearch, audit2allow
Modeenforce / complainenforcing / permissive
Bypass riskPath aliasingLabel evasion (harder)

AppArmor Commands

CommandPurpose
aa-statusList loaded profiles
apparmor_parser -r -W fileLoad or replace a profile
aa-complain profileSet to complain mode
aa-enforce profileSet to enforce mode
journalctl -k | grep apparmorRead denials

SELinux Commands

CommandPurpose
getenforceShow enforcing / permissive
setenforce 0Set permissive
ls -Z / ps -efZShow labels
ausearch -m avcRead denials
audit2allow -M nameGenerate policy from denials
semodule -i module.ppInstall a policy module

Volume Labels

FlagEffect
:zShared container_file_t label
:ZPrivate container_file_t label
noneHost label unchanged; may be inaccessible

The Four-Layer Model

LayerQuestionDocker Flag
CapabilitiesMay the operation run?--cap-drop, --cap-add
SeccompMay the syscall run?--security-opt seccomp=
AppArmorMay the path be touched?--security-opt apparmor=
SELinuxMay the label be touched?--security-opt label=

Best Practices

✅ Do This:

# Verify which LSM is active on the host.
sudo aa-status  # or getenforce  # ✅

# Use the Docker default profile as a baseline.
docker run nginx  # ✅

# Write a custom AppArmor profile for high-value services.
sudo apparmor_parser -r -W /etc/apparmor.d/myapp  # ✅

# Develop the profile in complain mode.
sudo aa-complain /etc/apparmor.d/myapp  # ✅

# Enable SELinux for Docker on SELinux distributions.
# /etc/docker/daemon.json: {"selinux-enabled": true}  # ✅

# Use :Z for volumes that belong to one container.
docker run -v /data:/data:Z myapp  # ✅

# Use :z for volumes shared between containers.
docker run -v /shared:/shared:z myapp  # ✅

# Encode the policy in Kubernetes manifests.
# securityContext.appArmorProfile, securityContext.seLinuxOptions  # ✅

❌ Don’t Do This:

# Don't disable SELinux to make a volume mount work.
sudo setenforce 0  # ❌ Use :z or :Z instead

# Don't write a profile that allows everything.
/profile flags=(complain) { /** rw, }  # ❌ No protection

# Don't skip the audit log when writing a profile.
# Denials are the specification.  # ❌

# Don't assume the default profile is sufficient.
# It blocks the worst operations, not the application's needs.  # ❌

# Don't mix AppArmor and SELinux on the same host.
# The kernel enforces the active LSM only.  # ❌

# Don't forget that the container's own root can be confined.
# AppArmor and SELinux apply regardless of the container's UID.  # ❌

Common Pitfalls

PitfallWhy It HappensFix
Volume inaccessibleHost label incompatibleUse :z or :Z
Profile not appliedNot loaded or wrong nameRun apparmor_parser, check aa-status
Application breaks on updateProfile too tightRe-run in complain mode, update rules
SELinux denials silentAudit not checkedausearch -m avc
Custom policy ignoredModule not installedsemodule -i
MCS conflictShared volume labelsUse :z for shared access
Profile bypassed by pathAppArmor path aliasingUse SELinux or accept the limit
Default profile too looseBroad compatibilityWrite a custom profile

Real-World Examples

1. Check the Host LSM

sudo aa-status  # AppArmor
getenforce      # SELinux

2. Run Under the Default Profile

docker run nginx

3. Apply a Custom AppArmor Profile

docker run --security-opt apparmor=myapp myapp:latest

4. Load an AppArmor Profile

sudo apparmor_parser -r -W /etc/apparmor.d/myapp

5. Complain Mode

sudo aa-complain /etc/apparmor.d/myapp

6. Enable SELinux for Docker

{ "selinux-enabled": true }

7. Inspect Labels

docker exec myapp ps -efZ

8. Shared Volume Label

docker run -v /shared:/data:z myapp

9. Private Volume Label

docker run -v /data:/data:Z myapp

10. Kubernetes Security Context

securityContext:
  appArmorProfile:
    type: Localhost
    localhostProfile: myapp
  seLinuxOptions:
    type: container_t

Visual

The Four-Layer Defense

┌────────────────────────────────────────────────────────────┐
│  DEFENSE IN DEPTH                                          │
│                                                            │
│  ┌──────────────────────────────────────────────────┐     │
│  │  CAPABILITIES                                     │     │
│  │    May the operation run?                         │     │
│  │    Example: CAP_NET_BIND_SERVICE for port 80      │     │
│  └──────────────────────────────────────────────────┘     │
│                        │                                   │
│                        v                                   │
│  ┌──────────────────────────────────────────────────┐     │
│  │  SECCOMP                                          │     │
│  │    May the syscall run?                           │     │
│  │    Example: deny mount, deny ptrace               │     │
│  └──────────────────────────────────────────────────┘     │
│                        │                                   │
│                        v                                   │
│  ┌──────────────────────────────────────────────────┐     │
│  │  APPARMOR (path-based)                            │     │
│  │    May this path be touched?                      │     │
│  │    Example: deny /bin/sh ix                       │     │
│  └──────────────────────────────────────────────────┘     │
│                        │                                   │
│                        v                                   │
│  ┌──────────────────────────────────────────────────┐     │
│  │  SELINUX (label-based)                            │     │
│  │    May this label be touched?                     │     │
│  │    Example: container_t may not read shadow_t     │     │
│  └──────────────────────────────────────────────────┘     │
│                                                            │
│  A compromise must defeat all four to escape.              │
│                                                            │
└────────────────────────────────────────────────────────────┘

The AppArmor Profile

┌────────────────────────────────────────────────────────────┐
│  APPARMOR PROFILE STRUCTURE                                │
│                                                            │
│  profile myapp {                                           │
│    #include <abstractions/base>    ← shared rules          │
│                                                            │
│    /app/myapp ix,                  ← allow execute         │
│    /etc/myapp/** r,                ← allow read            │
│    /var/log/myapp/** rw,           ← allow read/write      │
│                                                            │
│    deny /bin/sh ix,                ← deny shell            │
│    deny /bin/bash ix,              ← deny shell            │
│                                                            │
│    deny /** w,                     ← deny all writes       │
│  }                                                         │
│                                                            │
│  Default: deny.                                            │
│  Explicit allow for what the application needs.            │
│  Explicit deny for what the application must not do.       │
│                                                            │
└────────────────────────────────────────────────────────────┘

SELinux Context

┌────────────────────────────────────────────────────────────┐
│  SELINUX CONTEXT FOR A CONTAINER PROCESS                   │
│                                                            │
│  system_u : system_r : container_t : s0 : c1,c2            │
│  ────┬───   ────┬───   ─────┬─────   ─┬─   ──┬──           │
│      │          │           │         │      │             │
│      │          │           │         │      └─ MCS         │
│      │          │           │         │         categories  │
│      │          │           │         │                     │
│      │          │           │         └─ Sensitivity level  │
│      │          │           │                               │
│      │          │           └─ Type (the key field)         │
│      │          │                                           │
│      │          └─ Role (mostly vestigial)                  │
│      │                                                      │
│      └─ User (mostly vestigial)                             │
│                                                            │
│  Two containers share container_t but have different        │
│  MCS categories, so they cannot read each other's files.   │
│                                                            │
└────────────────────────────────────────────────────────────┘

The Audit Loop

┌────────────────────────────────────────────────────────────┐
│  DEVELOPING A PROFILE                                      │
│                                                            │
│  ┌──────────────────────────────────────────────────┐     │
│  │  1. START IN PERMISSIVE MODE                      │     │
│  │     AppArmor: aa-complain                         │     │
│  │     SELinux:  setenforce 0                        │     │
│  └──────────────────────────────────────────────────┘     │
│                        │                                   │
│                        v                                   │
│  ┌──────────────────────────────────────────────────┐     │
│  │  2. RUN THE APPLICATION                           │     │
│  │     Exercise every code path.                     │     │
│  └──────────────────────────────────────────────────┘     │
│                        │                                   │
│                        v                                   │
│  ┌──────────────────────────────────────────────────┐     │
│  │  3. COLLECT DENIALS                               │     │
│  │     AppArmor: journalctl -k | grep apparmor       │     │
│  │     SELinux:  ausearch -m avc                     │     │
│  └──────────────────────────────────────────────────┘     │
│                        │                                   │
│                        v                                   │
│  ┌──────────────────────────────────────────────────┐     │
│  │  4. WRITE RULES FOR EACH DENIAL                   │     │
│  │     Allow what the application actually does.     │     │
│  └──────────────────────────────────────────────────┘     │
│                        │                                   │
│                        v                                   │
│  ┌──────────────────────────────────────────────────┐     │
│  │  5. SWITCH TO ENFORCING MODE                      │     │
│  │     AppArmor: aa-enforce                          │     │
│  │     SELinux:  setenforce 1                        │     │
│  └──────────────────────────────────────────────────┘     │
│                                                            │
│  The log is the specification.                             │
│                                                            │
└────────────────────────────────────────────────────────────┘

Summary

ItemValue
AppArmorPath-based mandatory access control
SELinuxLabel-based mandatory access control
docker-defaultDefault AppArmor profile
container_tDefault SELinux type for containers
MCSPer-container categories for isolation
:zShared SELinux label for volumes
:ZPrivate SELinux label for volumes
aa-complainAppArmor permissive mode
setenforce 0SELinux permissive mode
ausearchRead SELinux denials
journalctl -kRead AppArmor denials

Key takeaways:

  • AppArmor and SELinux are not alternatives to capabilities and seccomp. They are a different layer. Capabilities govern operations, seccomp governs syscalls, and LSMs govern resource access. A container is hardened only when all four layers are considered.
  • AppArmor matches paths. A profile lists the paths the program may read, write, or execute. The docker-default profile is permissive; a custom profile narrows it to the application’s actual needs.
  • SELinux matches labels. Every process and file has a security context. Container processes are container_t, container files are container_file_t, and the policy allows the first to access the second and nothing else.
  • MCS categories isolate containers from each other. Two containers share the container_t type but have different MCS categories, so they cannot read each other’s files even when they share a host.
  • Volume labels must be explicit. A bind mount carries the host’s label, which may be incompatible with the container. The :z and :Z flags relabel the volume for shared or private access.
  • The docker-default profile and container_t type are baselines. They block the most dangerous operations but allow far more than a specific application needs. Tightening them is the work.
  • Profiles are developed in permissive mode. Run the application, collect denials, write rules, then enforce. The audit log is the specification.
  • Only one LSM is active. Ubuntu and Debian use AppArmor. RHEL, Fedora, and CentOS use SELinux. The choice is determined by the distribution, not by preference.
  • Encoding the policy in version control matters. The Kubernetes securityContext carries the AppArmor profile and SELinux options, so the policy ships with the application rather than living in an operator’s head.

Remember: Linux Security Modules are the layer that restricts which resources a container can touch. AppArmor does this by matching paths; SELinux does this by matching labels. Both are complementary to capabilities and seccomp, and both are enforced regardless of the container’s user ID. The Docker defaults for each are deliberately permissive, chosen for compatibility rather than security. Hardening a container means writing a custom profile or policy that allows only what the application actually needs, developing it in permissive mode against the audit log, and encoding it in the deployment manifest. The result is a container that runs the same workload with the same performance but a far smaller blast radius if it is ever compromised.


Was this helpful?


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!