Stage 3 · Build
Namespaces, cgroups & Security
Container Escape Analysis
Privileged pods, hostPath mounts, /proc exposure, and namespace breakout mitigations.
Container Escape Vectors
Container escapes break out of the container's isolation boundary to access the host or other containers. Understanding escape vectors is essential for securing containerized workloads.
- Privileged containers — full host access via capabilities
- /proc and /sys exposure — host filesystem access
- hostPath mounts — direct host filesystem access
- Kernel vulnerabilities — exploiting kernel bugs from inside
- Misconfigured capabilities — CAP_SYS_ADMIN, CAP_SYS_PTRACE
- Docker socket exposure — container runtime control
Privileged Containers
# A privileged container has ALL capabilities
# This is equivalent to root on the host
# Check if running privileged
cat /proc/1/status | grep Cap
# CapPrm: 0000003fffffffff (all capabilities)
# Classic escape: mount host filesystem
mkdir /host
mount /dev/sda1 /host
chroot /host
# Or mount the host's root namespace
mount --bind /proc/1/ns/mnt /mnt
# In Kubernetes, check for privileged pods
kubectl get pod <pod> -o yaml | grep privileged
# privileged: true <- DANGEROUSPrivileged containers are the most common escape vector. They have full access to host devices, can mount filesystems, and load kernel modules.
/proc Exposure
# /proc/self/root gives access to the container's root
# /proc/1/root gives access to PID 1's root (host if PID 1 is host)
# Read host files via /proc
cat /proc/1/root/etc/shadow # May work in misconfigured setups
# /proc/sys gives access to kernel parameters
cat /proc/sys/kernel/hostname # Shows host hostname
# Access host network namespace
ls -la /proc/1/ns/net
# Docker/Kubernetes should mask /proc
# Check what's visible
ls /proc/sysrq-trigger # Should be inaccessible
cat /proc/1/cgroup # Shows cgroup infoMask sensitive /proc paths in containers. Kubernetes does this by default. Docker allows read/write access to some /proc paths.
hostPath Mounts
# Kubernetes hostPath mounts can expose the host filesystem
# DANGEROUS hostPath configurations:
# Mount / (root filesystem)
# Mount /etc (configuration files)
# Mount /var/run/docker.sock (Docker API access)
# Mount /proc (host process information)
# Check existing hostPath mounts
kubectl get pod <pod> -o json | jq '.spec.volumes[] | select(.hostPath)'
# Docker equivalent
docker inspect <container> | jq '.Mounts[] | select(.Type=="bind")'
# Docker socket exposure allows full host control
docker run -v /var/run/docker.sock:/var/run/docker.sock docker docker pshostPath mounts break container filesystem isolation. Only mount specific, required directories. Never mount / or Docker socket.
Kernel Exploits
Containers share the host kernel. A kernel vulnerability can be exploited from inside a container to gain host access. This is why kernel updates are critical for container hosts.
# Check kernel version
uname -r
# Container-specific kernel features that may be exploitable:
# - user namespace clone (unprivileged user namespace)
# - cgroup release_agent
# - /proc/sys/kernel/core_pattern
# - CAP_SYS_MODULE (kernel module loading)
# - CAP_SYS_PTRACE (process tracing)
# Mitigate with:
# - Keep kernel updated
# - Disable unprivileged user namespaces if not needed
# - Use seccomp to block dangerous syscalls
# - Use AppArmor/SELinux to restrict file access
# Check for known container escape CVEs
# CVE-2022-0185 (heap overflow in fsconfig)
# CVE-2022-0492 (cgroup escape)
# CVE-2022-25636 (nf_tables bypass)Kernel exploits are the hardest to defend against. Regular kernel updates and restrictive seccomp profiles are the best mitigations.
Mitigation Strategies
# Drop ALL capabilities, add only what's needed
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE myapp
# Use read-only root filesystem
docker run --read-only --tmpfs /tmp myapp
# Disable privileged mode
# NEVER use --privileged in production
# Use seccomp profiles
docker run --security-opt seccomp=my-profile.json myapp
# Use AppArmor profiles
docker run --security-opt apparmor=my-profile myapp
# In Kubernetes, use PodSecurityPolicy or PodSecurityAdmission
apiVersion: v1
kind: Pod
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 1000
containers:
- name: myapp
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
add: ["NET_BIND_SERVICE"]Layer defenses: drop capabilities, use read-only filesystems, apply seccomp/AppArmor profiles, and run as non-root.
Enable Pod Security Admission with restricted profile to enforce security standards. This prevents privileged containers, hostPath mounts, and other dangerous configurations.
Containers share the host kernel. A kernel vulnerability affects all containers. Keep kernels updated and use seccomp to reduce the attack surface.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.