Container escape
| Class | Access control |
|---|---|
| Also known as | Container breakout, escape to host |
| MITRE ATT&CK | T1611 Escape to Host |
| Typical severity | Critical |
| Affects | Linux servers and clusters that run Docker or Kubernetes, and container platforms run by cloud providers |
A container on Linux is a group of ordinary processes running on the host's kernel. When Docker starts one, it creates a set of namespaces, which limit what the processes can see, and control groups, which limit what they can use.1 A container escape is any way for code inside that boundary to act on the host itself: to read and write the host's files, run programs outside the container or reach the other containers on the same machine.2 The attacker first needs code running in the container, for example through a hole in the application, so an escape is the second step of an attack.
What the boundary is made of
On top of namespaces and control groups, Docker removes most of the powers of root. Each power, such as mounting a disk or loading a kernel module, is a separate linux capability, and Docker keeps only an allowlist of them, so a process that becomes root inside a container still cannot do those things.1 A seccomp profile blocks around 44 of the more than 300 system calls, among them mount, unshare and keyctl; the last one reaches the kernel keyring, which has no namespace of its own.3
All containers on a host share one kernel. A kernel flaw that lets a normal user become root, such as Dirty COW, works the same from inside a well-isolated container and gives root on the host.4
Routes out
Settings that remove the boundary
The simplest escapes need no bug. The flag --privileged, or privileged: true in Compose, gives every capability, opens every device on the host and relaxes AppArmor or SELinux, the Linux security modules that confine a process, until the container has nearly the same access as a process outside it.5 A bind mount, which makes a host folder appear inside the container, hands over the whole disk when the folder is the host's root directory:
services:
app:
image: example/app
volumes:
- /:/host # the host's whole filesystemA process in this container that writes a file under /host/etc/cron.d has planted a job that the host's cron job runs. Writing payloads and running them through cron on the host is one of the documented escape routes, and the TeamTNT group deployed privileged containers that mount the victim's filesystem.2 A mounted /var/run/docker.sock is equivalent to root on the host, because it lets the container ask the Docker daemon to start such a container; Docker socket exposure covers this case.4 Smaller grants count too. The Kubernetes Baseline policy, written to prevent known privilege escalations, forbids pods that share the host's network, process list or inter-process communication (IPC) namespaces,6 and SYS_ADMIN is the capability the kernel checks before it allows mount or unshare.3
Bugs in the container runtime
runc is the standard low-level container runtime, the program that higher-level tools such as Docker and Kubernetes use to create containers.78 In CVE-2019-5736, a process running as root inside a container could overwrite the runc binary on the host and so get root code execution there. It needed someone to start a container from the attacker's image, or to attach with docker exec to a container the attacker had been able to write to. The default AppArmor policy did not stop it; a user namespace that keeps host root out of the container did.9
CVE-2024-21626, published on 31 January 2024, came from file descriptors, handles to open files, that runc leaked by mistake into the container's first process, one of them pointing at the host's /sys/fs/cgroup. A malicious image could make its working directory a symbolic link to /proc/self/fd/7/, and the container then started with access to the host's whole filesystem, and variants overwrote host programs such as /bin/bash. Versions from 1.0.0-rc93 to 1.1.11 are affected; runc 1.1.12 fixes it.7
Azurescape
In 2021, researchers at Unit 42 deployed a container to Azure Container Instances that reports which runtime is running it. The service was using runC v1.0.0-rc2, released in October 2016. Their CVE-2019-5736 exploit gave them a root shell on the underlying kubernetes node. Each customer container ran on a node of its own, so the escape alone reached nothing else, but exec requests arriving at that node carried a privileged service account token. With it they ran a shell on the cluster's API server, the program that controls the whole cluster, and took over the cluster and every customer container in it. Microsoft patched the service soon after the report, and Unit 42 knew of no use in the wild; customers were advised to revoke privileged credentials deployed before 31 August 2021.8
Reducing the risk
Keep the host kernel, Docker Engine and runc up to date.47 Then start containers with as little as they need:
docker run -u 4000 --cap-drop all --security-opt=no-new-privileges --read-only example/appThe -u flag, or a USER line in the Dockerfile, runs the app as a normal user. --cap-drop all removes every capability, and others can be added back one by one. no-new-privileges stops a process from gaining rights through setuid programs, which run with their owner's rights instead of the caller's, and --read-only makes the container's filesystem read-only.4 Compose has the matching keys user, cap_drop, security_opt and read_only.10
Where a container must run as root, the Docker daemon option --userns-remap=default maps its root to a high, unused user ID on the host, so a process that gets out holds no rights there.11 The docker group grants root-level rights and belongs only to accounts meant to have them.12 In Kubernetes, the Baseline Pod Security Standard rejects privileged pods.6 These are cases of Principle of least privilege; Securing a Docker application on a self-managed server applies them to a whole server.
On a running system, the signs are volume mounts of / or /proc, privileged containers nobody expected, unusual mount, unshare or keyctl calls from a container, and new processes on the host right after them.2
Prompt for a coding agent
Check every Dockerfile, docker-compose file, docker run command and Kubernetes manifest in this project for settings that weaken container isolation: privileged mode, bind mounts of / or other host system paths, a mounted /var/run/docker.sock, added capabilities, pid: host or network_mode: host, and containers that run as root. For each service, add a non-root user, drop all capabilities and add back only those it needs, set no-new-privileges and a read-only filesystem where the app allows it. Do not change what the services do. List every change and every setting you left in place because a service needs it, with the reason.
In SecFlash
On the Server page, SecFlash reports containers that run privileged as SF-SRV-401, can reach the Docker socket as SF-SRV-402, have the host's whole disk mounted as SF-SRV-403, hold powerful capabilities such as SYS_ADMIN as SF-SRV-404, can see the host's processes as SF-SRV-408 and run as root as SF-SRV-409. Accounts in the docker group are SF-SRV-304. On the Cloud page, it reads the repository and reports a Dockerfile that never leaves root as SF-CLD-902, a privileged Compose service as SF-CLD-906 and a Compose service that mounts the Docker socket as SF-CLD-907.
See also
- Docker socket exposure
- Securing a Docker application on a self-managed server
- Principle of least privilege
- Docker published ports and firewalls
- Cryptojacking
- Common Vulnerabilities and Exposures
- Category: Self-managed servers
References
- a b Docker (n.d.). "Docker Engine security". Docker Docs.
- a b c "Escape to Host, Technique T1611". MITRE ATT&CK.
- a b Docker (n.d.). "Seccomp security profiles for Docker". Docker Docs.
- a b c d OWASP (n.d.). "Docker Security Cheat Sheet". OWASP Cheat Sheet Series.
- back Docker (n.d.). "Running containers", section Runtime privilege and Linux capabilities. Docker Docs.
- a b Kubernetes (n.d.). "Pod Security Standards". Kubernetes Documentation.
- a b c runc maintainers (31 January 2024). "Several container breakouts due to internally leaked fds", GHSA-xr7r-f8xq-vfvv (CVE-2024-21626). GitHub.
- a b Unit 42, Palo Alto Networks (September 2021). "Finding Azurescape: Cross-Account Container Takeover in Azure Container Instances". Unit 42.
- back runc maintainers (February 2019). "CVE-2019-5736: runc container breakout". oss-security mailing list.
- back Docker (n.d.). "Services". Compose file reference, Docker Docs.
- back Docker (n.d.). "Isolate containers with a user namespace". Docker Docs.
- back Docker (n.d.). "Linux post-installation steps for Docker Engine". Docker Docs.
External links
- Docker Engine security, Docker Docs
- Docker Security Cheat Sheet, OWASP Cheat Sheet Series