Sub topic 11
Syscall and mandatory access control profiles
This chapter assumes you can run containers with docker run, you know what Linux capabilities are (covered in the chapters on isolation and on dropping capabilities), and you can read a short JSON file. You do not need prior experience with seccomp or AppArmor.
Why capabilities are not enough
Dropping capabilities removes privileged powers from a process. It does not stop the process from asking the kernel to do things. A container still shares the host kernel, so every system call (syscall) the process can make is attack surface. A kernel bug reachable through a rarely used syscall can give a container process control of the host, even if the process has no capabilities.
Two kernel features narrow this surface further:
- Seccomp (secure computing mode) filters which syscalls a process may make, and optionally which arguments they may have.
- Mandatory access control (MAC), here AppArmor or SELinux, enforces a policy on files, network use, and other resources. Neither the process nor its owner can change that policy.
Both run in addition to the other controls. A syscall must pass the seccomp filter, the normal permission and capability checks, and the MAC policy.
Seccomp in Docker
The default profile
Docker applies a default seccomp profile to every container unless you change it. It is an allowlist: the default action is to return an error, and a long list of syscalls is explicitly allowed. Linux has more than 300 syscalls, and the default profile blocks roughly 40 to 50 of them. These include reboot, kexec_load, init_module, mount and unshare (when the container lacks the matching capability), and keyctl. The profile is deliberately broad so that most applications run unchanged.
You can confirm the default is active by checking the process status inside a container:
docker run --rm alpine grep Seccomp /proc/self/statusSeccomp: 2 means a filter is installed. Seccomp: 0 means none is.
The profile also adapts to the capabilities you grant. Adding --cap-add SYS_ADMIN unlocks mount and related syscalls, because the profile allows them only when the capability is present. This is one reason to avoid broad capability grants.
Writing a custom profile
A profile is a JSON file passed with --security-opt seccomp=. The key fields are:
defaultAction: what happens to any syscall not matched by a rule.architectures: the CPU architectures the rules apply to.syscalls: a list of rules, each withnames, anaction, and optionallyargsanderrnoRet.
Common actions are SCMP_ACT_ALLOW, SCMP_ACT_ERRNO (fail the call with an error code), SCMP_ACT_LOG (allow it and log it), and SCMP_ACT_KILL_PROCESS (terminate the process).
The following profile allows everything except uname, which fails with EPERM (error number 1). Blocking uname is not a useful hardening step. It is chosen because the effect is easy to see.
{
"defaultAction": "SCMP_ACT_ALLOW",
"syscalls": [
{
"names": ["uname"],
"action": "SCMP_ACT_ERRNO",
"errnoRet": 1
}
]
}Save it as deny-uname.json and run a container with it:
docker run --rm --security-opt seccomp=deny-uname.json alpine uname -aThe command fails with Operation not permitted. Running the same command without the option prints kernel information.
Denylist versus allowlist
The example above is a denylist: allow by default, block specific calls. It is easy to write, but it only blocks what you thought of. An allowlist sets defaultAction to SCMP_ACT_ERRNO and names every syscall the application needs. It is far stronger, but it breaks as soon as the application, its libraries, or the C library version uses a syscall you did not list.
A practical workflow for an allowlist:
- Start from Docker's default profile (
default.jsonin the Moby repository), not from an empty file. - Remove syscall groups your application never uses, or add rules that deny specific risky calls.
- Test with
SCMP_ACT_LOGas the default action first. Blocked calls are then logged, not enforced, so you can find missing ones. - Switch to
SCMP_ACT_ERRNOonce the logs are clean under realistic load, including startup, shutdown, and error paths.
Turning seccomp off
--security-opt seccomp=unconfined removes the filter. So does --privileged. Some debugging tools, such as strace and perf, need ptrace or perf_event_open, which the default profile restricts. When you need them, prefer a custom profile that allows only the extra syscalls over turning the filter off.
AppArmor in Docker
AppArmor is a Linux security module that confines programs by path-based rules. It is the default MAC system on Ubuntu, Debian, and SUSE hosts. Docker generates and loads a profile named docker-default and attaches it to containers when AppArmor is enabled on the host. That profile denies writes to sensitive paths such as /proc/sys, /sys/firmware, and /proc/sysrq-trigger, and denies mounting filesystems.
Check that a container is confined:
docker run --rm alpine cat /proc/self/attr/currentOutput such as docker-default (enforce) means the profile is active. The value unconfined means it is not.
Writing a custom profile
An AppArmor profile lists rules. A deny rule always wins over an allow rule, so a common pattern is to allow broadly and then deny specific paths. The profile below blocks all writes under /etc and all mounts:
#include <tunables/global>
profile docker-deny-etc flags=(attach_disconnected,mediate_deleted) {
#include <abstractions/base>
file,
network,
capability,
deny /etc/** w,
deny /proc/sysrq-trigger rwklx,
deny /sys/firmware/** rwklx,
deny mount,
}Save it as docker-deny-etc, load it into the kernel, and run a container with it:
sudo apparmor_parser -r -W docker-deny-etc
sudo aa-status | grep docker-deny-etc
docker run --rm --security-opt apparmor=docker-deny-etc alpine \
sh -c 'echo test > /etc/motd'The write fails with Permission denied, even though the process is root inside the container. The profile must be loaded on every host that runs the container. Docker does not copy it for you. A missing profile makes docker run fail, which is better than silently running unconfined.
SELinux on other distributions
Red Hat, Fedora, and CentOS family hosts use SELinux instead of AppArmor. SELinux labels every process and file, and the policy controls which labels may interact. To enable it for containers, set "selinux-enabled": true in /etc/docker/daemon.json (or start the daemon with --selinux-enabled). The :z and :Z suffixes on bind mounts relabel host files so the container may access them. Docker Desktop on macOS and Windows runs containers inside a VM, so host-level AppArmor and SELinux settings do not apply the same way.
Comparing the two
| Aspect | Seccomp | AppArmor |
|---|---|---|
| What it controls | Which syscalls a process can make, and optionally their arguments | Access to files, network operations, and capabilities by path-based rules |
| Docker default | Built-in allowlist profile | `docker-default` profile (on hosts with AppArmor) |
| Option | `--security-opt seccomp=file.json` | `--security-opt apparmor=name` |
| Where the policy lives | A file read when the container starts | A profile loaded into the host kernel |
| Typical failure | `Operation not permitted` or a killed process | `Permission denied` on a path |
The two are complementary. Seccomp cannot say "this process may write to /data but not /etc", because both are the same syscall. AppArmor cannot easily say "this process may never call keyctl".
Common mistakes
- Using
--privilegedand expecting profiles to apply. Privileged mode runs the container without the default seccomp and AppArmor confinement. - Disabling a profile to fix one error. Identify the specific syscall or path and allow only that.
- Not testing a custom allowlist under load. Missing syscalls often show up only in error handling or shutdown paths.
- Assuming a profile exists on every host. AppArmor profiles must be loaded on each machine, or containers will fail to start there.
- Treating profiles as a replacement for other controls. They reduce the impact of a compromise. Non-root users, dropped capabilities, and patched images remain necessary.
Exercise
A team's container needs only a handful of syscalls. They write a seccomp profile with defaultAction set to SCMP_ACT_ERRNO and list the syscalls seen in one strace run. After deployment, the service crashes during a rare error-handling path. What went wrong, and how should they build the profile instead?
Show answer
The strace run only recorded syscalls from the code paths it exercised, so the error-handling path used a syscall that was not on the allowlist, and the filter blocked it. They should start from Docker's default profile, or set defaultAction to SCMP_ACT_LOG first, run realistic and failure-case workloads (startup, shutdown, errors), add the logged syscalls to the allowlist, and only then switch the default action to SCMP_ACT_ERRNO.
Exercise
A container runs with --privileged, and the team says AppArmor protects it because the host uses AppArmor. Is that correct? What is a safer way to give the container the one extra power it needs?
Show answer
No. --privileged runs the container without the default seccomp and AppArmor confinement, so the host's AppArmor setup does not restrict it. A safer approach is to remove --privileged, identify the single capability or device the workload needs, and grant only that (for example with --cap-add or --device). The default seccomp and AppArmor profiles then stay in force, or you can adjust them with a narrow custom profile.