Container escape
Table of Contents
Container Escape
Description
Containerization is a technology that isolates applications by running them in lightweight, virtualized environments called containers. These containers rely on the host's operating system kernel (e.g., Linux kernel) but use features like namespaces and cgroups to provide strong, but not absolute, isolation.
A container escape is a successful Privilege Escalation attack that moves the attacker from the restricted, sandboxed environment of the container to the privileged, shared environment of the host machine.
Attack Vectors
Container escapes typically succeed due to one of three main categories of security misconfiguration or flaw:
- Kernel Vulnerabilities: The most severe type. The container and the host share the same kernel. If the kernel version is unpatched and contains a known local exploit, the attacker can leverage the flaw from within the container to gain full
rootaccess on the host. - Container Misconfigurations (Weak Isolation):
- Privileged Mode: The container is run in
privilegedmode, which essentially removes all container-specific security restrictions and grants the container near-full capabilities, often including direct access to host devices. - Insecure Volume Mounts: Sensitive host directories (e.g.,
/proc,/sys, or the host's root directory/) are mounted inside the container, giving the container access to critical host files, configuration, or control over host processes. - Insecure Capabilities: The container is granted unnecessary Linux capabilities (e.g.,
CAP_SYS_ADMINorCAP_DAC_READ_SEARCH) which allow it to perform actions normally reserved for the host root user.
- Privileged Mode: The container is run in
- Vulnerable Container Runtime: Exploiting flaws in the container software itself (e.g., Docker, runC, or Kubernetes components) that handle the creation and management of the container processes.
Impact
A successful container escape grants the attacker control over the host machine, leading to the compromise of all other containers running on that host, data theft, and the installation of persistence mechanisms.
Examples
. Exploiting Privileged Containers
If a Docker container is started with the --privileged flag, an attacker can exploit this by mounting the host's disk.
Vulnerable Setup Command:
docker run -it --privileged ubuntu bash
Attack Commands (Inside the Container):
# 1. Access the host's disk device (e.g., /dev/sda1) # Attacker can now create a malicious file or modify system configuration on the host mkdir /host_root mount /dev/sda1 /host_root # 2. Modify the host's cron job (a persistence mechanism) echo '* * * * * root bash /tmp/backdoor.sh' >> /host_root/etc/crontab
2. Exploiting Insecure Volume Mounts
If the host's /proc filesystem is mounted, an attacker can use it to potentially write to arbitrary files on the host using its process IDs (PIDs).
Vulnerable Setup Command (Mounting Host Root):
docker run -it -v /:/host ubuntu bash
Attack Commands (Inside the Container):
# 1. Change root directory to the mounted host filesystem chroot /host # 2. The attacker now has a root shell on the host machine. # They can read /etc/shadow or execute commands. cat /etc/shadow
3. Leveraging Kernel Vulnerabilities
An attacker identifies the host kernel version using uname -a inside the container and finds a corresponding Local Privilege Escalation (LPE) exploit from Exploit-DB (e.g., a buffer overflow in a kernel module).
Attack Steps (Conceptual): 1. Compile the LPE exploit C code inside the container. 2. Execute the compiled exploit binary. 3. The exploit code runs with kernel privileges, achieving a kernel panic or, ideally for the attacker, replacing the current low-privileged process with a root process on the host OS, thus achieving the container escape.