Sub topic 9
Dropping Capabilities and Avoiding --privileged
This chapter assumes you can build and run containers, know how Linux users and processes work, and have read the earlier chapter on namespaces, cgroups, and capabilities. Here you will apply that model: remove the privileges your container does not need, and learn why --privileged undoes most of Docker's isolation.
Why capabilities matter
Traditional Unix splits the world into root (UID 0, can do anything) and everyone else. Linux capabilities break root's power into about 40 separate units. Examples are CAP_NET_BIND_SERVICE (bind ports below 1024), CAP_CHOWN (change file ownership), and CAP_SYS_ADMIN (a very broad set of administrative operations).
Docker starts every container with a reduced default set of roughly 14 capabilities, even when the process runs as root. Most applications need none of them. Every capability you leave in place gives an attacker who compromises the application one more tool to use against the kernel or the host.
The default set includes:
| Capability | What it allows | Commonly needed? |
|---|---|---|
| `CHOWN` | Change file owner and group | Rarely, at runtime |
| `DAC_OVERRIDE` | Bypass file permission checks | Rarely |
| `FOWNER` | Bypass owner checks on files | Rarely |
| `SETUID`, `SETGID` | Change process UID and GID | Only for programs that drop privileges themselves |
| `KILL` | Signal processes owned by other users | Rarely |
| `NET_BIND_SERVICE` | Bind to ports below 1024 | Only for low ports |
| `NET_RAW` | Craft raw packets (ping, spoofing) | Almost never |
| `SYS_CHROOT` | Use `chroot` | Rarely |
| `MKNOD` | Create device files | Almost never |
| `AUDIT_WRITE` | Write to the kernel audit log | Almost never |
The exact default list can vary between Docker versions, so check the documentation for your version.
The drop-all-then-add-back approach
The safest pattern is to start from nothing and add back only what the application proves it needs.
docker run -d --name web \
--user 10001:10001 \
--cap-drop ALL \
--security-opt no-new-privileges \
--read-only --tmpfs /tmp \
--memory 256m --cpus 0.5 --pids-limit 100 \
myapp:1.0Three flags in this command matter for this chapter:
--user 10001:10001runs the process as an unprivileged user. This was covered in the non-root chapter. Capabilities and a non-root user work together: a non-root process has no effective capabilities by default.--cap-drop ALLremoves every capability from the container's bounding set, including the Docker defaults.--security-opt no-new-privilegessets the kernel'sno_new_privsflag. A process can then no longer gain privileges through setuid binaries or file capabilities.
The read-only filesystem and resource limits in the same command are covered in the next chapter.
Adding back what you need
If the application fails after you drop everything, add back one capability at a time with --cap-add. A web server that must listen on port 80 as a non-root user is a typical case:
docker run -d --name proxy \
--user 10001:10001 \
--cap-drop ALL \
--cap-add NET_BIND_SERVICE \
--security-opt no-new-privileges \
nginx-unprivileged-custom:1.0Often you can avoid even that. Listen on a high port such as 8080 inside the container, then publish it as port 80 on the host with -p 80:8080. The Docker daemon handles the host side, so the container needs no capability.
Finding which capabilities you need
Do not guess. Work through these steps:
- Run the container with
--cap-drop ALL. - Exercise the application's normal paths: startup, requests, logging, shutdown.
- Read the logs for
Operation not permittedorEPERMerrors. - Add back the single capability that matches the failing operation.
- Repeat until the application works, then stop.
You can check what a running container actually holds by reading its process status:
docker run --rm --cap-drop ALL alpine grep Cap /proc/self/statusThe output shows hexadecimal bitmasks. A value of 0000000000000000 for CapEff and CapBnd means no capabilities. The capsh --decode=<mask> tool, available on most hosts, translates a mask into names.
The same restrictions in Compose look like this:
services:
web:
image: myapp:1.0
user: "10001:10001"
cap_drop:
- ALL
cap_add:
- NET_BIND_SERVICE
security_opt:
- no-new-privileges:trueWhat no-new-privileges does
Some programs gain privileges when executed. A setuid-root binary such as su or sudo runs as root no matter who launches it. A binary with file capabilities gains those capabilities on execution. With no-new-privileges, the kernel ignores both mechanisms for the container's processes and their children.
This closes a common escalation path: an attacker who gets a shell as a non-root user, then looks for a setuid binary with a known flaw. The flag costs nothing for most applications. It breaks only programs that intentionally rely on setuid, such as sudo inside the container.
The daemon-wide default, shown later in the daemon chapter, is "no-new-privileges": true in /etc/docker/daemon.json.
Why --privileged is dangerous
The --privileged flag is the opposite of everything above. It does several things at once:
- Grants all capabilities, including
CAP_SYS_ADMIN,CAP_SYS_MODULE, andCAP_SYS_PTRACE. - Gives the container access to all host devices under
/dev, including disks. - Disables the default seccomp profile and AppArmor confinement.
- Removes the read-only protection from parts of
/procand/sys.
A privileged container running as root is nearly equivalent to root on the host. A short demonstration shows how easily an attacker could reach the host disk:
docker run --rm -it --privileged alpine sh -c \
'mkdir /host && mount /dev/sda1 /host && ls /host'Your device name will differ, but the point holds. The container can mount the host's root filesystem and read or modify any file on it. Other known escapes from privileged containers include loading kernel modules and abusing the cgroup release_agent file to run commands on the host.
Narrower alternatives to --privileged
Most workloads that seem to need --privileged actually need one specific thing. Pick the smallest grant that works.
| Need | Instead of `--privileged` use |
|---|---|
| Access one device, such as a serial port | `--device /dev/ttyUSB0` |
| Change network settings, such as an interface or routes | `--cap-add NET_ADMIN` |
| Use `ping` or raw sockets | `--cap-add NET_RAW`, or a sysctl such as `net.ipv4.ping_group_range` |
| Attach a debugger to another process | `--cap-add SYS_PTRACE` for that container only |
| Run containers inside a container | Rootless Docker-in-Docker, Sysbox, or a dedicated build service such as BuildKit |
| Mount filesystems | A volume mount from the host, not `CAP_SYS_ADMIN` |
Even --cap-add SYS_ADMIN is close to privileged mode in practice, because that one capability covers a very large set of operations. If you see it in a configuration, ask what it is for.
Related risky flags
Several other options give a container similar reach without the word "privileged".
| Flag | Risk |
|---|---|
| `-v /var/run/docker.sock:/var/run/docker.sock` | Full control of the Docker daemon, which means root on the host |
| `--pid=host` | The container sees and can signal host processes |
| `--net=host` | The container shares the host network stack and can reach services bound to localhost |
| `--ipc=host` | Shared memory access with host processes |
| `--userns=host` | Disables user namespace remapping for this container |
| `-v /:/host` | Read and write access to the whole host filesystem |
Each of these deserves a written justification in code review.
Enforcing the policy
Rules that depend on every developer remembering flags will eventually fail. Some ways to enforce them:
- Set
"no-new-privileges": truein the daemon configuration, so the safe setting is the default. - Review Compose files and CI configuration for
privileged: true,cap_add, and host namespace options. A simple search finds them:
grep -rnE 'privileged: *true|--privileged|SYS_ADMIN|docker.sock' .- Run
docker inspecton existing containers to find privileged ones:
docker ps -q | xargs docker inspect \
--format '{{.Name}} privileged={{.HostConfig.Privileged}} capadd={{.HostConfig.CapAdd}}'- In Kubernetes, use Pod Security Standards at the "restricted" level, which forbids privileged containers and requires dropping all capabilities.
Common mistakes
- Dropping only the capabilities you think are dangerous, instead of dropping
ALL. The allow-list approach is safer because new capabilities do not appear unnoticed. - Adding
SYS_ADMINto get past a single error without checking what the operation was. - Assuming a non-root user makes capabilities irrelevant. Files with file capabilities and setuid binaries can still raise privileges unless
no-new-privilegesis set. - Running with
--privilegedonce to "see if it works", then leaving it in the final configuration. - Believing a privileged container is isolated because it has its own namespaces. With full capabilities and device access, namespaces are easy to step out of.
Summary of the working model
Think of container privilege as layered. The user ID decides who the process is. Capabilities decide what that identity may do to the kernel. no-new-privileges stops the process from gaining more later. Seccomp and AppArmor, covered in a later chapter, filter the system calls themselves. --privileged removes several of those layers together, which is why it should never be a convenience setting.
Exercise
A container runs as UID 10001 with --cap-drop ALL. The application must listen on port 443 inside the container, and it fails with "permission denied". Name two ways to fix this without using --privileged, and say which you prefer.
Show answer
Option one: add back only NET_BIND_SERVICE with --cap-add NET_BIND_SERVICE. Option two: change the application to listen on a port above 1023, such as 8443, and publish it with -p 443:8443. The second is preferred because the container then needs no capabilities at all.
Exercise
You find a Compose file with privileged: true for a service that reads from one USB serial adapter at /dev/ttyUSB0. What do you change?
Show answer
Remove privileged: true, and add devices: ["/dev/ttyUSB0:/dev/ttyUSB0"]. Also add cap_drop: [ALL] and no-new-privileges:true, then add back any capability only if the logs show a specific failure. Make sure the container user can access the device, for example by using group_add with the device's group.
Exercise
What two kinds of privilege gain does --security-opt no-new-privileges prevent, and why does it matter for a container running as a non-root user?
Show answer
It prevents gaining privileges by executing setuid or setgid binaries and by executing binaries that carry file capabilities. It matters for non-root users because an attacker with a shell as that user would otherwise look for such a binary to become root inside the container.