eBPF for Container Security: Detect Escapes at Runtime
Your containers run in production. One gets compromised. The attacker breaks out of the container and lands on the host. How do you know it happened? Traditional tools—auditd, sysdig, falco with default rules—miss container-specific escape techniques or drown you in noise.
This post shows you how to write eBPF programs that detect container escapes at runtime with precision: namespace manipulation, host filesystem access, capability abuse, and kernel exploit attempts. We use Tracee (Aqua Security) for ready-to-deploy detection and bpftrace for custom rules. All examples tested on Ubuntu 24.04, kernel 6.8+, Docker 27.x, containerd 1.7+.
Why Container Escapes Need Special Detection
A container escape is not a single event—it is a sequence of privileged operations that, individually, look normal:
- Process in container calls
unshare(CLONE_NEWUSER)— “creating user namespace” - Process mounts host
/into container — “mounting volume” - Process uses
CAP_SYS_ADMINto pivot_root — “changing root filesystem” - Process executes host binary — “running command”
Each step alone is legitimate container behavior. The combination in sequence, from a non-init process, with specific capability usage—that is the escape.
eBPF lets you correlate these events in-kernel, with container context (container ID, pod name, image), and alert only on the suspicious sequence.
Tracee: Ready-to-Deploy Container Escape Detection
Tracee is an eBPF-based runtime security tool with built-in signatures for container escapes. Deploy in minutes:
# Quick start (Docker)
docker run --name tracee --rm --pid=host --cgroupns=host --privileged -v /etc/os-release:/etc/os-release:ro -v /var/run/docker.sock:/var/run/docker.sock:ro -v /run/containerd:/run/containerd:ro aquasec/tracee:latest
# Or with specific signatures
docker run --name tracee --rm --pid=host --cgroupns=host --privileged -v /etc/os-release:/etc/os-release:ro -v /var/run/docker.sock:/var/run/docker.sock:ro aquasec/tracee:latest --trace container=escape
Tracee signatures for container escapes:
| Signature ID | Name | Description |
|---|---|---|
| TRC-101 | Container Escape via Mount | Mounting host filesystem into container |
| TRC-102 | Container Escape via Privileged | Running privileged container with host access |
| TRC-103 | Container Escape via Namespace | Manipulating host namespaces from container |
| TRC-104 | Container Escape via Capabilities | Abusing CAP_SYS_ADMIN, CAP_DAC_OVERRIDE |
| TRC-105 | Container Escape via Proc | Accessing host /proc from container |
| TRC-106 | Container Escape via Cgroup | Escaping via cgroup release_agent |
| TRC-107 | Container Escape via Device | Accessing host devices (/dev/kmem, /dev/mem) |
# List all signatures
tracee --list-signatures
# Run only container escape signatures
tracee --trace container=escape --output json
Tracee Output Example: Mount Escape
{
"timestamp": "2024-11-15T10:30:45Z",
"signature": "Container Escape via Mount",
"signature_id": "TRC-101",
"process": {
"pid": 12345,
"ppid": 12340,
"comm": "mount",
"cwd": "/",
"exe": "/bin/mount",
"args": ["mount", "/dev/sda1", "/host"],
"uid": 0,
"gid": 0,
"container_id": "abc123def456",
"container_name": "web-app",
"pod_name": "web-app-7b9c8d",
"namespace": "production"
},
"event": {
"event_name": "mount",
"source": "/dev/sda1",
"target": "/host",
"filesystem": "ext4",
"flags": 0
},
"context": {
"host_pid": 54321,
"container_pid": 12345,
"thread_start_time": 1731666645000000000
}
}
Custom bpftrace Rules for Container Escapes
{
"timestamp": "2024-11-15T10:30:45Z",
"signature": "Container Escape via Mount",
"signature_id": "TRC-101",
"process": {
"pid": 12345,
"ppid": 12340,
"comm": "mount",
"cwd": "/",
"exe": "/bin/mount",
"args": ["mount", "/dev/sda1", "/host"],
"uid": 0,
"gid": 0,
"container_id": "abc123def456",
"container_name": "web-app",
"pod_name": "web-app-7b9c8d",
"namespace": "production"
},
"event": {
"event_name": "mount",
"source": "/dev/sda1",
"target": "/host",
"filesystem": "ext4",
"flags": 0
},
"context": {
"host_pid": 54321,
"container_pid": 12345,
"thread_start_time": 1731666645000000000
}
}
Tracee covers known patterns. For your specific environment, write custom bpftrace rules that understand your containers.
Rule 1: Detect Host Filesystem Mount from Container
# host_mount_escape.bt
# Detect mount syscall where source or target contains host paths
tracepoint:syscalls:sys_enter_mount
/str(args->source) ~ /^\/host/ || str(args->target) ~ /^\/host/ ||
str(args->source) ~ /^\/var\/lib\/docker/ || str(args->target) ~ /^\/var\/lib\/docker/ ||
str(args->source) ~ /^\/var\/lib\/containerd/ || str(args->target) ~ /^\/var\/lib\/containerd/
{
printf("HOST_MOUNT_ESCAPE: pid=%d comm=%s uid=%d src=%s tgt=%s fs=%s flags=0x%x\n",
pid, comm, uid, str(args->source), str(args->target), str(args->filesystemtype), args->flags);
}
Rule 2: Detect User Namespace Creation from Non-Init Container Process
# user_ns_escape.bt
# CLONE_NEWUSER = 0x10000000
# Only alert if NOT the container init (PID 1 in container)
tracepoint:syscalls:sys_enter_unshare
/args->flags & 0x10000000 && pid != 1/
{
printf("USER_NS_FROM_CONTAINER: pid=%d comm=%s uid=%d flags=0x%x\n",
pid, comm, uid, args->flags);
}
tracepoint:syscalls:sys_enter_clone
/args->flags & 0x10000000 && pid != 1/
{
printf("CLONE_NEWUSER_FROM_CONTAINER: pid=%d comm=%s uid=%d flags=0x%x\n",
pid, comm, uid, args->flags);
}
Rule 3: Detect Capability Abuse (CAP_SYS_ADMIN Usage)
# cap_sys_admin_abuse.bt
# Track processes that USE CAP_SYS_ADMIN (not just have it)
# We check for operations that require CAP_SYS_ADMIN
tracepoint:syscalls:sys_enter_mount,
tracepoint:syscalls:sys_enter_pivot_root,
tracepoint:syscalls:sys_enter_swapon,
tracepoint:syscalls:sys_enter_swapoff,
tracepoint:syscalls:sys_enter_sysfs,
tracepoint:syscalls:sys_enter_init_module,
tracepoint:syscalls:sys_enter_finit_module,
tracepoint:syscalls:sys_enter_delete_module
/uid == 0/ # Root in container (mapped to host uid via user ns)
{
printf("CAP_SYS_ADMIN_USE: pid=%d comm=%s op=%s\n", pid, comm, probe);
}
Rule 4: Detect /proc and /sys Access from Container
# proc_sys_access.bt
# Containers should not read host /proc or /sys
tracepoint:syscalls:sys_enter_openat
/str(args->filename) ~ /^\/proc\/[0-9]+/ &&
str(args->filename) !~ /^\/proc\/self/ &&
str(args->filename) !~ /^\/proc\/thread-self/
{
printf("HOST_PROC_ACCESS: pid=%d comm=%s uid=%d file=%s\n",
pid, comm, uid, str(args->filename));
}
tracepoint:syscalls:sys_enter_openat
/str(args->filename) ~ /^\/sys\/kernel/ || str(args->filename) ~ /^\/sys\/module/
{
printf("HOST_SYS_ACCESS: pid=%d comm=%s uid=%d file=%s\n",
pid, comm, uid, str(args->filename));
}
Rule 5: Detect Cgroup Escape (release_agent)
# cgroup_escape.bt
# Writing to cgroup release_agent or notify_on_release
tracepoint:syscalls:sys_enter_write
/str(args->buf) ~ /release_agent/ || str(args->buf) ~ /notify_on_release/
{
printf("CGROUP_ESCAPE_ATTEMPT: pid=%d comm=%s uid=%d buf=%s\n",
pid, comm, uid, str(args->buf));
}
tracepoint:syscalls:sys_enter_openat
/str(args->filename) ~ /cgroup.*release_agent/ || str(args->filename) ~ /cgroup.*notify_on_release/
{
printf("CGROUP_FILE_ACCESS: pid=%d comm=%s uid=%d file=%s\n",
pid, comm, uid, str(args->filename));
}
Rule 6: Detect Device Access (/dev/kmem, /dev/mem, /dev/port)
# device_access.bt
tracepoint:syscalls:sys_enter_openat
/str(args->filename) == "/dev/kmem" ||
str(args->filename) == "/dev/mem" ||
str(args->filename) == "/dev/port" ||
str(args->filename) == "/dev/kmsg" ||
str(args->filename) ~ /^\/dev\/loop/ ||
str(args->filename) ~ /^\/dev\/sd/
{
printf("SENSITIVE_DEVICE_ACCESS: pid=%d comm=%s uid=%d device=%s\n",
pid, comm, uid, str(args->filename));
}
Rule 7: Detect Pivot Root / Chroot Escape
# pivot_chroot_escape.bt
tracepoint:syscalls:sys_enter_pivot_root,
tracepoint:syscalls:sys_enter_chroot
/pid != 1/ # Not container init
{
printf("PIVOT_CHROOT_ESCAPE: pid=%d comm=%s uid=%d op=%s\n",
pid, comm, uid, probe);
}
Rule 8: Detect Kernel Exploit Primitives (commit_creds, prepare_kernel_cred)
# kernel_exploit_primitives.bt
# These kprobes catch the core kernel credential manipulation functions
# Used by almost all local privilege escalation exploits
kprobe:commit_creds
{
printf("COMMIT_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
kprobe:prepare_kernel_cred
{
printf("PREPARE_KERNEL_CRED: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
# Also catch override_creds / revert_creds (used in some exploits)
kprobe:override_creds
{
printf("OVERRIDE_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
kprobe:revert_creds
{
printf("REVERT_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
Container-Aware Detection: Correlating with Container Context
# host_mount_escape.bt
# Detect mount syscall where source or target contains host paths
tracepoint:syscalls:sys_enter_mount
/str(args->source) ~ /^\/host/ || str(args->target) ~ /^\/host/ ||
str(args->source) ~ /^\/var\/lib\/docker/ || str(args->target) ~ /^\/var\/lib\/docker/ ||
str(args->source) ~ /^\/var\/lib\/containerd/ || str(args->target) ~ /^\/var\/lib\/containerd/
{
printf("HOST_MOUNT_ESCAPE: pid=%d comm=%s uid=%d src=%s tgt=%s fs=%s flags=0x%x\n",
pid, comm, uid, str(args->source), str(args->target), str(args->filesystemtype), args->flags);
}
# user_ns_escape.bt
# CLONE_NEWUSER = 0x10000000
# Only alert if NOT the container init (PID 1 in container)
tracepoint:syscalls:sys_enter_unshare
/args->flags & 0x10000000 && pid != 1/
{
printf("USER_NS_FROM_CONTAINER: pid=%d comm=%s uid=%d flags=0x%x\n",
pid, comm, uid, args->flags);
}
tracepoint:syscalls:sys_enter_clone
/args->flags & 0x10000000 && pid != 1/
{
printf("CLONE_NEWUSER_FROM_CONTAINER: pid=%d comm=%s uid=%d flags=0x%x\n",
pid, comm, uid, args->flags);
}
Rule 3: Detect Capability Abuse (CAP_SYS_ADMIN Usage)
# cap_sys_admin_abuse.bt
# Track processes that USE CAP_SYS_ADMIN (not just have it)
# We check for operations that require CAP_SYS_ADMIN
tracepoint:syscalls:sys_enter_mount,
tracepoint:syscalls:sys_enter_pivot_root,
tracepoint:syscalls:sys_enter_swapon,
tracepoint:syscalls:sys_enter_swapoff,
tracepoint:syscalls:sys_enter_sysfs,
tracepoint:syscalls:sys_enter_init_module,
tracepoint:syscalls:sys_enter_finit_module,
tracepoint:syscalls:sys_enter_delete_module
/uid == 0/ # Root in container (mapped to host uid via user ns)
{
printf("CAP_SYS_ADMIN_USE: pid=%d comm=%s op=%s\n", pid, comm, probe);
}
Rule 4: Detect /proc and /sys Access from Container
# proc_sys_access.bt
# Containers should not read host /proc or /sys
tracepoint:syscalls:sys_enter_openat
/str(args->filename) ~ /^\/proc\/[0-9]+/ &&
str(args->filename) !~ /^\/proc\/self/ &&
str(args->filename) !~ /^\/proc\/thread-self/
{
printf("HOST_PROC_ACCESS: pid=%d comm=%s uid=%d file=%s\n",
pid, comm, uid, str(args->filename));
}
tracepoint:syscalls:sys_enter_openat
/str(args->filename) ~ /^\/sys\/kernel/ || str(args->filename) ~ /^\/sys\/module/
{
printf("HOST_SYS_ACCESS: pid=%d comm=%s uid=%d file=%s\n",
pid, comm, uid, str(args->filename));
}
Rule 5: Detect Cgroup Escape (release_agent)
# cgroup_escape.bt
# Writing to cgroup release_agent or notify_on_release
tracepoint:syscalls:sys_enter_write
/str(args->buf) ~ /release_agent/ || str(args->buf) ~ /notify_on_release/
{
printf("CGROUP_ESCAPE_ATTEMPT: pid=%d comm=%s uid=%d buf=%s\n",
pid, comm, uid, str(args->buf));
}
tracepoint:syscalls:sys_enter_openat
/str(args->filename) ~ /cgroup.*release_agent/ || str(args->filename) ~ /cgroup.*notify_on_release/
{
printf("CGROUP_FILE_ACCESS: pid=%d comm=%s uid=%d file=%s\n",
pid, comm, uid, str(args->filename));
}
Rule 6: Detect Device Access (/dev/kmem, /dev/mem, /dev/port)
# device_access.bt
tracepoint:syscalls:sys_enter_openat
/str(args->filename) == "/dev/kmem" ||
str(args->filename) == "/dev/mem" ||
str(args->filename) == "/dev/port" ||
str(args->filename) == "/dev/kmsg" ||
str(args->filename) ~ /^\/dev\/loop/ ||
str(args->filename) ~ /^\/dev\/sd/
{
printf("SENSITIVE_DEVICE_ACCESS: pid=%d comm=%s uid=%d device=%s\n",
pid, comm, uid, str(args->filename));
}
Rule 7: Detect Pivot Root / Chroot Escape
# pivot_chroot_escape.bt
tracepoint:syscalls:sys_enter_pivot_root,
tracepoint:syscalls:sys_enter_chroot
/pid != 1/ # Not container init
{
printf("PIVOT_CHROOT_ESCAPE: pid=%d comm=%s uid=%d op=%s\n",
pid, comm, uid, probe);
}
Rule 8: Detect Kernel Exploit Primitives (commit_creds, prepare_kernel_cred)
# kernel_exploit_primitives.bt
# These kprobes catch the core kernel credential manipulation functions
# Used by almost all local privilege escalation exploits
kprobe:commit_creds
{
printf("COMMIT_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
kprobe:prepare_kernel_cred
{
printf("PREPARE_KERNEL_CRED: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
# Also catch override_creds / revert_creds (used in some exploits)
kprobe:override_creds
{
printf("OVERRIDE_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
kprobe:revert_creds
{
printf("REVERT_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
Container-Aware Detection: Correlating with Container Context
# cap_sys_admin_abuse.bt
# Track processes that USE CAP_SYS_ADMIN (not just have it)
# We check for operations that require CAP_SYS_ADMIN
tracepoint:syscalls:sys_enter_mount,
tracepoint:syscalls:sys_enter_pivot_root,
tracepoint:syscalls:sys_enter_swapon,
tracepoint:syscalls:sys_enter_swapoff,
tracepoint:syscalls:sys_enter_sysfs,
tracepoint:syscalls:sys_enter_init_module,
tracepoint:syscalls:sys_enter_finit_module,
tracepoint:syscalls:sys_enter_delete_module
/uid == 0/ # Root in container (mapped to host uid via user ns)
{
printf("CAP_SYS_ADMIN_USE: pid=%d comm=%s op=%s\n", pid, comm, probe);
}
# proc_sys_access.bt
# Containers should not read host /proc or /sys
tracepoint:syscalls:sys_enter_openat
/str(args->filename) ~ /^\/proc\/[0-9]+/ &&
str(args->filename) !~ /^\/proc\/self/ &&
str(args->filename) !~ /^\/proc\/thread-self/
{
printf("HOST_PROC_ACCESS: pid=%d comm=%s uid=%d file=%s\n",
pid, comm, uid, str(args->filename));
}
tracepoint:syscalls:sys_enter_openat
/str(args->filename) ~ /^\/sys\/kernel/ || str(args->filename) ~ /^\/sys\/module/
{
printf("HOST_SYS_ACCESS: pid=%d comm=%s uid=%d file=%s\n",
pid, comm, uid, str(args->filename));
}
Rule 5: Detect Cgroup Escape (release_agent)
# cgroup_escape.bt
# Writing to cgroup release_agent or notify_on_release
tracepoint:syscalls:sys_enter_write
/str(args->buf) ~ /release_agent/ || str(args->buf) ~ /notify_on_release/
{
printf("CGROUP_ESCAPE_ATTEMPT: pid=%d comm=%s uid=%d buf=%s\n",
pid, comm, uid, str(args->buf));
}
tracepoint:syscalls:sys_enter_openat
/str(args->filename) ~ /cgroup.*release_agent/ || str(args->filename) ~ /cgroup.*notify_on_release/
{
printf("CGROUP_FILE_ACCESS: pid=%d comm=%s uid=%d file=%s\n",
pid, comm, uid, str(args->filename));
}
Rule 6: Detect Device Access (/dev/kmem, /dev/mem, /dev/port)
# device_access.bt
tracepoint:syscalls:sys_enter_openat
/str(args->filename) == "/dev/kmem" ||
str(args->filename) == "/dev/mem" ||
str(args->filename) == "/dev/port" ||
str(args->filename) == "/dev/kmsg" ||
str(args->filename) ~ /^\/dev\/loop/ ||
str(args->filename) ~ /^\/dev\/sd/
{
printf("SENSITIVE_DEVICE_ACCESS: pid=%d comm=%s uid=%d device=%s\n",
pid, comm, uid, str(args->filename));
}
Rule 7: Detect Pivot Root / Chroot Escape
# pivot_chroot_escape.bt
tracepoint:syscalls:sys_enter_pivot_root,
tracepoint:syscalls:sys_enter_chroot
/pid != 1/ # Not container init
{
printf("PIVOT_CHROOT_ESCAPE: pid=%d comm=%s uid=%d op=%s\n",
pid, comm, uid, probe);
}
Rule 8: Detect Kernel Exploit Primitives (commit_creds, prepare_kernel_cred)
# kernel_exploit_primitives.bt
# These kprobes catch the core kernel credential manipulation functions
# Used by almost all local privilege escalation exploits
kprobe:commit_creds
{
printf("COMMIT_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
kprobe:prepare_kernel_cred
{
printf("PREPARE_KERNEL_CRED: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
# Also catch override_creds / revert_creds (used in some exploits)
kprobe:override_creds
{
printf("OVERRIDE_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
kprobe:revert_creds
{
printf("REVERT_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
Container-Aware Detection: Correlating with Container Context
# cgroup_escape.bt
# Writing to cgroup release_agent or notify_on_release
tracepoint:syscalls:sys_enter_write
/str(args->buf) ~ /release_agent/ || str(args->buf) ~ /notify_on_release/
{
printf("CGROUP_ESCAPE_ATTEMPT: pid=%d comm=%s uid=%d buf=%s\n",
pid, comm, uid, str(args->buf));
}
tracepoint:syscalls:sys_enter_openat
/str(args->filename) ~ /cgroup.*release_agent/ || str(args->filename) ~ /cgroup.*notify_on_release/
{
printf("CGROUP_FILE_ACCESS: pid=%d comm=%s uid=%d file=%s\n",
pid, comm, uid, str(args->filename));
}
# device_access.bt
tracepoint:syscalls:sys_enter_openat
/str(args->filename) == "/dev/kmem" ||
str(args->filename) == "/dev/mem" ||
str(args->filename) == "/dev/port" ||
str(args->filename) == "/dev/kmsg" ||
str(args->filename) ~ /^\/dev\/loop/ ||
str(args->filename) ~ /^\/dev\/sd/
{
printf("SENSITIVE_DEVICE_ACCESS: pid=%d comm=%s uid=%d device=%s\n",
pid, comm, uid, str(args->filename));
}
Rule 7: Detect Pivot Root / Chroot Escape
# pivot_chroot_escape.bt
tracepoint:syscalls:sys_enter_pivot_root,
tracepoint:syscalls:sys_enter_chroot
/pid != 1/ # Not container init
{
printf("PIVOT_CHROOT_ESCAPE: pid=%d comm=%s uid=%d op=%s\n",
pid, comm, uid, probe);
}
Rule 8: Detect Kernel Exploit Primitives (commit_creds, prepare_kernel_cred)
# kernel_exploit_primitives.bt
# These kprobes catch the core kernel credential manipulation functions
# Used by almost all local privilege escalation exploits
kprobe:commit_creds
{
printf("COMMIT_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
kprobe:prepare_kernel_cred
{
printf("PREPARE_KERNEL_CRED: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
# Also catch override_creds / revert_creds (used in some exploits)
kprobe:override_creds
{
printf("OVERRIDE_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
kprobe:revert_creds
{
printf("REVERT_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
Container-Aware Detection: Correlating with Container Context
# pivot_chroot_escape.bt
tracepoint:syscalls:sys_enter_pivot_root,
tracepoint:syscalls:sys_enter_chroot
/pid != 1/ # Not container init
{
printf("PIVOT_CHROOT_ESCAPE: pid=%d comm=%s uid=%d op=%s\n",
pid, comm, uid, probe);
}
# kernel_exploit_primitives.bt
# These kprobes catch the core kernel credential manipulation functions
# Used by almost all local privilege escalation exploits
kprobe:commit_creds
{
printf("COMMIT_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
kprobe:prepare_kernel_cred
{
printf("PREPARE_KERNEL_CRED: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
# Also catch override_creds / revert_creds (used in some exploits)
kprobe:override_creds
{
printf("OVERRIDE_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
kprobe:revert_creds
{
printf("REVERT_CREDS: pid=%d comm=%s uid=%d\n", pid, comm, uid);
}
Container-Aware Detection: Correlating with Container Context
The rules above lack container context. To get container ID, image, pod name, you need to correlate with container runtime events. Tracee does this automatically. For custom bpftrace, use Tracee’s library or correlate via cgroup:
# container_context.bt
# Extract container ID from cgroup path
tracepoint:syscalls:sys_enter_mount
/str(args->source) ~ /^\/host/ || str(args->target) ~ /^\/host/
{
= cgrouppath();
= "";
if ( ~ /docker-([a-f0-9]{12})/) {
= ; // Simplified - extract actual ID
} else if ( ~ /containerd-([a-f0-9]{64})/) {
= ;
}
printf("HOST_MOUNT: pid=%d comm=%s uid=%d src=%s tgt=%s cgroup=%s\n",
pid, comm, uid, str(args->source), str(args->target), );
}
For production, use Tracee’s Go library which provides full container context:
// Go program using Tracee library
package main
import (
"github.com/aquasecurity/tracee/pkg/events"
"github.com/aquasecurity/tracee/types/detect"
)
func main() {
// Subscribe to container escape events
detector := detect.NewDetector()
detector.OnEvent(func(event detect.Event) {
if event.SignatureID == "TRC-101" { // Container Escape via Mount
// event.Container has: ID, Name, Image, PodName, Namespace
alert(event)
}
})
detector.Run()
}
Production Deployment Architecture
Option 1: DaemonSet on Kubernetes (Recommended)
# tracee-daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: tracee
namespace: kube-system
spec:
selector:
matchLabels:
app: tracee
template:
metadata:
labels:
app: tracee
spec:
hostPID: true
hostNetwork: true
serviceAccountName: tracee
containers:
- name: tracee
image: aquasec/tracee:latest
securityContext:
privileged: true
volumeMounts:
- name: tracee-socket
mountPath: /tmp/tracee
- name: docker-sock
mountPath: /var/run/docker.sock
- name: containerd-sock
mountPath: /run/containerd
- name: os-release
mountPath: /etc/os-release
readOnly: true
env:
- name: TRACEE_OUTPUT_FORMAT
value: "json"
- name: TRACEE_TRACE
value: "container=escape"
volumes:
- name: tracee-socket
hostPath:
path: /tmp/tracee
- name: docker-sock
hostPath:
path: /var/run/docker.sock
- name: containerd-sock
hostPath:
path: /run/containerd
- name: os-release
hostPath:
path: /etc/os-release
Option 2: Systemd Service on VMs
# /etc/systemd/system/tracee-container-escape.service
[Unit]
Description=Tracee Container Escape Detection
After=docker.service containerd.service
Requires=docker.service
[Service]
Type=simple
ExecStart=/usr/local/bin/tracee --trace container=escape --output json
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
AmbientCapabilities=CAP_BPF CAP_PERFMON CAP_SYS_ADMIN CAP_SYS_RESOURCE
CapabilityBoundingSet=CAP_BPF CAP_PERFMON CAP_SYS_ADMIN CAP_SYS_RESOURCE
[Install]
WantedBy=multi-user.target
Option 3: bpftrace Custom Rules as Systemd Services
# /etc/systemd/system/bpftrace-container-escape.service
[Unit]
Description=bpftrace Custom Container Escape Rules
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/bpftrace /opt/bpftrace/container_escape_rules.bt
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
AmbientCapabilities=CAP_BPF CAP_PERFMON CAP_SYS_ADMIN
CapabilityBoundingSet=CAP_BPF CAP_PERFMON CAP_SYS_ADMIN
[Install]
WantedBy=multi-user.target
Alerting and Integration
JSON Output to SIEM (Elastic, Splunk, Loki)
# Tracee JSON output to file (for Filebeat/Fluentd)
tracee --trace container=escape --output json --output-path /var/log/tracee/alerts.json
# bpftrace JSON (periodic flush)
# container_escape_json.bt
BEGIN { @alerts = 0; }
# ... your rules with JSON formatting ...
interval:s:30
{
// Flush accumulated alerts
// In practice, use a sidecar to read ring buffer
}
Webhook Alerting
# Simple webhook sender (run as sidecar)
#!/bin/bash
# /opt/tracee/webhook-alert.sh
tail -f /var/log/tracee/alerts.json | while read line; do
curl -X POST "https://your-siem/webhook" -H "Content-Type: application/json" -d ""
done
Performance and Tuning
| Configuration | CPU Overhead | Memory | Detection Coverage |
|---|---|---|---|
| Tracee: all signatures | ~3-5% | ~100-200MB | Full (300+ signatures) |
| Tracee: container=escape only | ~1-2% | ~50-100MB | Container escapes (7 sigs) |
| bpftrace: 8 custom rules | ~0.5-1% | ~10-20MB | Custom escape patterns |
| bpftrace: security_dashboard (12 rules) | ~1-2% | ~20-30MB | General security |
Tuning tips:
- Run Tracee with
--trace container=escapeinstead of all signatures - Filter by container label:
--container-label-selector "env=production" - Use bpftrace predicates (
/condition/) to drop events in-kernel - Set
--buffer-sizefor high-traffic systems - Exclude known-good processes: backup agents, monitoring daemons
Testing Your Detection
Simulate escapes safely to verify detection works:
# Test 1: Host mount escape (run in container)
docker run --rm -it --privileged -v /:/host ubuntu:24.04 mount /dev/sda1 /host/rootfs
# Test 2: User namespace escape
docker run --rm -it --security-opt seccomp=unconfined ubuntu:24.04 unshare -U /bin/bash
# Test 3: Cgroup escape (requires specific setup)
# mkdir -p /sys/fs/cgroup/test
# echo 1 > /sys/fs/cgroup/test/notify_on_release
# echo "/tmp/escape.sh" > /sys/fs/cgroup/test/release_agent
# Test 4: Proc access
docker run --rm -it ubuntu:24.04 cat /proc/1/environ
# Test 5: Device access
docker run --rm -it --device /dev/kmem ubuntu:24.04 ls -la /dev/kmem
Expected: Each test should generate an alert in Tracee/bpftrace output.
Tools Cross-References
- JSON Formatter — Format Tracee JSON alerts, bpftrace JSON output
- YAML Formatter — Format Kubernetes DaemonSet, systemd service files
- Cron Expression Generator — Schedule periodic detection rule updates
Related Posts
- bpftrace Tutorial: Write Your First Security Detection Rules — bpftrace fundamentals
- eBPF for Security Monitoring: Beginner’s Guide — eBPF basics
- Build a Sandboxed AI Agent on Ubuntu Using Docker/gVisor — Runtime isolation that prevents these escapes
- systemd Service Hardening: Complete Directive Reference — Secure the detection services
Conclusion
Container escapes follow patterns. They manipulate namespaces, mount host filesystems, abuse capabilities, access host /proc and /sys, and exploit kernel credential functions. eBPF lets you detect each pattern at the syscall level, in-kernel, with container context.
Start with Tracee’s built-in container escape signatures (--trace container=escape). They catch the known patterns with zero configuration. Add custom bpftrace rules for your environment-specific escape paths. Deploy as a DaemonSet on Kubernetes or systemd service on VMs. Pipe JSON alerts to your SIEM.
The key insight: a container escape is not one event—it is a sequence. eBPF gives you the granularity to see the sequence, the context to know which container, and the performance to run it on every node in production.
Next steps:
- Deploy Tracee DaemonSet on your Kubernetes cluster
- Tune signatures: disable noisy ones, add custom bpftrace rules
- Integrate alerts with your incident response workflow
- Test escapes monthly in staging
- Combine with gVisor (previous post) for prevention + detection
Prevention (gVisor) stops the escape. Detection (Tracee/bpftrace) tells you when someone tries. You need both.