Capabilities, seccomp and read-only containers

Advanced · 7 min

Shrink what a container can do: drop capabilities, block privilege gain, keep or tighten the seccomp profile, and make the root filesystem read-only.

Before thisContainer isolation and its limitsRunning as non-root and rootless Docker

Datadog · 7 min

Capabilities, seccomp and read-only containers

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by DatadogReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Docker grants 14 capabilities by default, the bitmask 00000000a80425fb in /proc/1/status. Many services need none of them once they run as a non-root user.
  • Start from `--cap-drop ALL` and add back only what fails without it. Images whose entrypoint starts as root and switches user usually need CHOWN, SETUID and SETGID.
  • NET_RAW is in the default set but rarely needed: Docker enables unprivileged ICMP sockets for ping, and raw sockets let a container forge packets on the bridge.
  • `--security-opt no-new-privileges` stops setuid binaries and file capabilities from raising privileges inside the container.
  • The default seccomp profile blocks dangerous system calls and widens when you add capabilities. Keep it on. Never use `seccomp=unconfined` as a fix; write or load a custom profile instead.
  • `--read-only` makes the container's root filesystem immutable, so an attacker can't drop tools or edit binaries. Give writable paths back explicitly with `--tmpfs` or volumes.
  • In Compose the same controls are `cap_drop`, `cap_add`, `security_opt`, `read_only` and `tmpfs`.

Least privilege, one layer at a time

Running as a non-root user takes away the biggest key. This lesson takes away the smaller ones. The idea is the same as giving a contractor access to one room instead of the whole building: if their badge is stolen, the thief gets one room.

Docker offers four controls, each covering what the others miss:

Control Limits Flag
Capabilities which root privileges exist at all --cap-drop, --cap-add
No new privileges gaining privileges later through setuid or file capabilities --security-opt no-new-privileges
Seccomp which system calls reach the kernel --security-opt seccomp=...
Read-only root filesystem writing to the image’s files --read-only

Capabilities

The Linux capabilities lesson explains the mechanism. Docker’s default set, read from Moby’s caps/defaults.go, is 14 capabilities: CHOWN, DAC_OVERRIDE, FSETID, FOWNER, MKNOD, NET_RAW, SETGID, SETUID, SETFCAP, SETPCAP, NET_BIND_SERVICE, SYS_CHROOT, KILL and AUDIT_WRITE. Turned into the bitmask the kernel shows, with Python, that is 00000000a80425fb, the value grep CapEff /proc/1/status prints in a default root container.

Most services need few or none. The reliable method is subtractive: drop everything, run your tests, add back what breaks.

docker run --cap-drop ALL --security-opt no-new-privileges \
  --user 10001:10001 registry.example.com/api:1.4.2

Two capabilities deserve a note.

  • NET_BIND_SERVICE is usually unnecessary. Moby sets net.ipv4.ip_unprivileged_port_start=0 in containers with their own network namespace, so any user can bind port 80.
  • NET_RAW lets a process craft raw packets, which on a shared bridge means spoofing other containers’ traffic. Its legitimate use was ping, and Moby also sets net.ipv4.ping_group_range so ping works through ICMP sockets without it. Drop it.

Images whose entrypoint starts as root and then switches user, such as the official postgres and nginx, need CHOWN, SETUID and SETGID at startup. Either add those three back, or use an image built to start unprivileged.

No new privileges

A process with no capabilities can still gain them by executing a setuid-root binary or one with file capabilities. no-new-privileges sets the kernel’s PR_SET_NO_NEW_PRIVS flag, after which execve never raises privileges. It costs nothing for normal services. Set it on everything, or for every container at once with "no-new-privileges": true in daemon.json.

Seccomp

The isolation lesson described the default profile. I parsed it from Moby’s source: the default action is to return EPERM, 361 calls are always allowed, and others are allowed only when a matching capability is present. That last part means the profile follows your capabilities. Add CAP_SYS_ADMIN and mount, unshare and setns become available. Add CAP_SYS_PTRACE and ptrace becomes available on any kernel. Adding a capability widens two layers at once.

When software fails with operation not permitted and the cause is seccomp, the fix is a custom profile, not removing the filter:

curl -fsSL -o seccomp.json \
  https://raw.githubusercontent.com/moby/profiles/main/seccomp/default.json
# edit: add the one system call you need to an allow rule
docker run --security-opt seccomp=./seccomp.json image

Docker’s own release notes for 29.4.2 make the point. When a seccomp change for a kernel bug broke some 32-bit programs, the notes warned users not to fall back to seccomp=unconfined and instead pointed them at a specific alternative profile.

AppArmor, on Debian and Ubuntu hosts, is the other kernel filter. Docker applies docker-default automatically, and the Linux AppArmor lesson explains profiles.

Read-only root filesystem

An attacker who gets code execution usually wants to write something: a downloaded tool, a modified binary, a web shell. --read-only mounts the container’s root filesystem read-only, so the image’s files can’t change. Writable data goes where you put it:

docker run --read-only \
  --tmpfs /tmp --tmpfs /run --tmpfs /var/cache/nginx \
  -v site-data:/usr/share/nginx/html:ro \
  nginx:1.31

Finding those paths is the work. I extracted /etc/nginx/nginx.conf from the current nginx image: it writes its PID to /run/nginx.pid and uses /var/cache/nginx for temporary files, so those get tmpfs. A read-only container also makes drift impossible: every instance of the image is the same, which is a benefit beyond security.

All of it in Compose

services:
  api:
    image: registry.example.com/api:1.4.2
    user: "10001:10001"
    read_only: true
    tmpfs:
      - /tmp
    cap_drop: [ALL]
    security_opt:
      - no-new-privileges:true

To check the result on a running container: docker inspect shows HostConfig.CapDrop, SecurityOpt and ReadonlyRootfs, and inside, grep Cap /proc/1/status piped to capsh --decode shows the effective set.

About the video

The embed is all of Datadog’s “Container Security Fundamentals - Linux Capabilities (Part 2)”, October 2023, 7 minutes. The description says it hardens Docker containers by removing some of the default capabilities, and the chapters go through an example, dropping all capabilities, NET_RAW, and binding ports. That is the capabilities half of this lesson. It doesn’t cover no-new-privileges or read-only filesystems, and seccomp is in the same series’ next episode, linked in deeper. YouTube reports no audio language, so English was judged from the channel and its English captions. The series predates the 2026 AF_ALG change to the seccomp profile.

What was run: Moby’s default capability list and seccomp profile were read from its source and analysed with Python, including the bitmask calculation. The nginx configuration was extracted from the current nginx image’s layers. No containers were run, since no daemon was available. Flag behaviour is from the Docker CLI reference and Engine release notes, checked 2026-11-28.

Where this fits

The container is now locked down at run time. What it was given at build time can still leak: secrets at build time and run time.

Check yourself

  1. 01A service runs as USER 10001 and listens on port 8080. Which capabilities does it need?

    Usually none. A non-root process doesn't use root's capabilities for ordinary work, so --cap-drop ALL should work. Test it and add back only what actually fails.

  2. 02Why does --cap-drop ALL often break the stock postgres or nginx image?

    Their entrypoints start as root, change ownership of directories and then switch to an unprivileged user, which needs CHOWN, SETUID and SETGID. Either add those back or use a variant that starts unprivileged.

  3. 03An app fails with operation not permitted on a system call under Docker. Why is --security-opt seccomp=unconfined the wrong fix?

    It removes the whole filter, re-exposing hundreds of calls and the kernel bugs behind them. Find the one call that's blocked and allow it in a custom profile based on the default.

  4. 04Your container runs with --read-only and nginx fails to start. What is the usual fix?

    Give it writable scratch space for the paths it writes at runtime, for example --tmpfs /var/cache/nginx and --tmpfs /run, and keep the rest of the filesystem read-only.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark