Checks / Docker and containers
A container has dangerous capabilities
- Check
docker.dangerous_caps- Severity
- medium
- Runs
- on the server, in the daily run of the agent
- Looks at
- CapAdd contains SYS_ADMIN, NET_ADMIN, SYS_PTRACE, SYS_MODULE or ALL
What the check looks at
The agent reads HostConfig.CapAdd of every running container from docker inspect and reports the
container when the list contains SYS_ADMIN, NET_ADMIN, SYS_PTRACE, SYS_MODULE or ALL (Docker
stores them with a CAP_ prefix, such as CAP_NET_ADMIN). The evidence line names the container and the
capabilities. Privileged containers are reported by docker.privileged.
Why it matters
Docker starts containers with 14 capabilities. The ones this check looks for are not among them, and each gives a root process in the container more power. From the capabilities(7) man page:
SYS_ADMINcovers "a range of system administration operations", including mounts. The man page calls it "the new root".SYS_MODULEcan "load and unload kernel modules". Code in a kernel module runs in the kernel of the host, outside any container.SYS_PTRACEcan trace other processes. With--pid=hostthat includes the processes of the host.NET_ADMINconfigures interfaces, firewall rules and routes. With--network=hostthese are the ones of the host.ALLgives every capability.
How to fix it
The commands are the same on every distribution.
-
Find out which operation needs the capability. VPN containers usually need
NET_ADMINand the device/dev/net/tun; FUSE needsSYS_ADMINand/dev/fuse; debuggers needSYS_PTRACE. A web application or database needs none of them. -
Drop all capabilities and add back only the needed ones:
services: app: image: example/app:1.4.2 cap_drop: [ALL] cap_add: [NET_BIND_SERVICE] security_opt: - no-new-privileges:trueWith
docker run:--cap-drop ALL --cap-add NET_BIND_SERVICE --security-opt no-new-privileges. -
Recreate the container (
docker compose up -d --force-recreate <service>) and test the program. If it fails with "Operation not permitted", add one capability at a time.
SYS_MODULE in a container is rarely needed. Load the kernel module on the host instead, for WireGuard with
sudo modprobe wireguard and, on systemd hosts, echo wireguard | sudo tee /etc/modules-load.d/wireguard.conf
for every boot. Then drop the capability.
How to check the fix
docker ps -q | xargs -r docker inspect --format '{{.Name}} capadd={{.HostConfig.CapAdd}}'
The output should list none of the capabilities above, or only those you decided to keep.
When a warning is fine
NET_ADMIN for a VPN or router container on its own bridge network affects only the network of that
container. SYS_PTRACE in a debugging container you start for an hour is fine if you remove it afterwards.
Accept such results on the dashboard, with a note on why the container needs the capability. SYS_MODULE and ALL deserve a closer look every time.
An accepted result is left out of the digest and the counts until it changes; then it shows again.
Sources
- capabilities(7)
- Docker docs: Running containers, runtime privilege and Linux capabilities
— default list,
--cap-add,--cap-drop,CAP_prefix.