TExcelTAD
Home › Cloud & Container Security › Container Isolation: Namespaces, cgroups and Capabilities

Container Isolation: Namespaces, cgroups and Capabilities

A container is not a small virtual machine. It is an ordinary Linux process that the kernel shows a restricted view of the system and allows a restricted set of actions. There is one kernel, shared by the host and every container on it, and the isolation is only as strong as the set of kernel features the runtime turned on and the settings that didn't turn them off. That is the root of every container escape class in a later lesson: each one is a missing or disabled layer. This lesson names the layers, inspects them from inside a real container, and decodes the capability sets that decide what root in a container can do. Under the Hood explains the runtime stack (dockerd, containerd, runc) that sets them up.

The layers

Layer Kernel feature What it isolates or limits Turned off by
View namespaces: pid, mnt, net, uts, ipc, user, cgroup, time which processes, mounts, network interfaces, hostname, IPC objects and IDs the process sees --pid=host, --network=host, --ipc=host, hostPID/hostNetwork/hostIPC in Kubernetes
Resources cgroups (v2 on current distributions) memory, CPU, number of processes, I/O no limits set (the default in Docker; a fork bomb takes the host's process table)
Privilege capabilities: root's power split into about 40 units which privileged operations root in the container may perform --privileged, --cap-add, capabilities.add
System calls seccomp filter which syscalls (and arguments) are allowed --security-opt seccomp=unconfined, --privileged
Access control AppArmor or SELinux profile which files, mounts and operations are allowed apparmor=unconfined, label=disable, --privileged
Identity user namespace / rootless runtime whether UID 0 in the container is UID 0 on the host (off by default in Docker and Kubernetes)
Filesystem overlay root, mount options what the container can write and which host paths it sees host bind mounts, hostPath volumes

The next lesson covers seccomp, AppArmor, SELinux and rootless operation; this one covers the first three rows.

Looking from inside

Your sandbox sessions run in a Docker container, which makes it a convenient specimen. The session below prints what the process can see and do. It runs only inside your sandbox and changes nothing.

bash · try it yourself
id
echo "--- capabilities (hex masks)"
grep -E '^Cap(Prm|Eff|Bnd)' /proc/self/status
echo "--- the bounding set, decoded"
setpriv -d | grep 'bounding set'
echo "--- PID namespace: PID 1 is the session shell, not the host's init"
ps -o pid,comm -p 1
echo "--- cgroup: the container's own root, memory not limited"
cat /proc/self/cgroup /sys/fs/cgroup/memory.max
echo "--- seccomp mode (2 = filter) and AppArmor profile"
grep -E '^Seccomp:' /proc/self/status
cat /proc/self/attr/current
uid=1000(learner) gid=1000(learner) groups=1000(learner)
--- capabilities (hex masks)
CapPrm:	0000000000000000
CapEff:	0000000000000000
CapBnd:	00000000a80425fb
--- the bounding set, decoded
Capability bounding set: chown,dac_override,fowner,fsetid,kill,setgid,setuid,setpcap,net_bind_service,net_raw,sys_chroot,mknod,audit_write,setfcap
--- PID namespace: PID 1 is the session shell, not the host's init
    PID COMMAND
      1 bash
--- cgroup: the container's own root, memory not limited
0::/
max
--- seccomp mode (2 = filter) and AppArmor profile
Seccomp:	2
docker-default (enforce)

Read it as a checklist. The process runs as an unprivileged user, so its effective capabilities (CapEff) are zero. The bounding set (CapBnd) is Docker's default of 14 capabilities: the most any process in this container could ever gain, even by running a setuid-root binary. The PID namespace hides the host's processes (PID 1 is the session's own shell). The container has its own cgroup but no memory limit (max). Seccomp is in filter mode, and AppArmor enforces Docker's default profile. That is a reasonably hardened default container; the next section shows what changes when someone adds capabilities or runs it privileged.

Decoding capability sets

Capability masks are bit fields: bit n set means capability number n. The program decodes the sandbox's bounding set and those of three other containers, and marks the capabilities that are known to break isolation (each of these appears in published container escapes or makes host compromise trivial).

python · try it yourself
CAPS = ["chown", "dac_override", "dac_read_search", "fowner", "fsetid", "kill", "setgid", "setuid", "setpcap",
        "linux_immutable", "net_bind_service", "net_broadcast", "net_admin", "net_raw", "ipc_lock", "ipc_owner",
        "sys_module", "sys_rawio", "sys_chroot", "sys_ptrace", "sys_pacct", "sys_admin", "sys_boot", "sys_nice",
        "sys_resource", "sys_time", "sys_tty_config", "mknod", "lease", "audit_write", "audit_control", "setfcap",
        "mac_override", "mac_admin", "syslog", "wake_alarm", "block_suspend", "audit_read", "perfmon", "bpf",
        "checkpoint_restore"]
DANGEROUS = {
    "sys_admin": "mount filesystems, many namespace and device operations: the 'new root'",
    "sys_module": "load kernel modules: arbitrary kernel code",
    "sys_ptrace": "trace other processes (host processes if the PID namespace is shared)",
    "dac_read_search": "open files by handle, bypassing path checks (a known escape class)",
    "sys_rawio": "raw I/O ports and device memory",
    "net_admin": "reconfigure network interfaces, routes and firewall rules",
    "bpf": "load eBPF programs into the kernel",
    "sys_boot": "reboot the host",
}

def decode(mask_hex):
    m = int(mask_hex, 16)
    return [name for bit, name in enumerate(CAPS) if m >> bit & 1]

containers = {
    "sandbox (Docker default)": "00000000a80425fb",
    "hardened (cap-drop ALL, add NET_BIND_SERVICE)": "0000000000000400",
    "app with --cap-add SYS_PTRACE,NET_ADMIN": "00000000a80c35fb",
    "--privileged": "000001ffffffffff",
}
for name, mask in containers.items():
    caps = decode(mask)
    bad = [c for c in caps if c in DANGEROUS]
    print(f"{name}: {len(caps)} capabilities")
    if len(caps) <= 16:
        print("   " + ", ".join(caps))
    for c in bad:
        print(f"   !! {c}: {DANGEROUS[c]}")
sandbox (Docker default): 14 capabilities
   chown, dac_override, fowner, fsetid, kill, setgid, setuid, setpcap, net_bind_service, net_raw, sys_chroot, mknod, audit_write, setfcap
hardened (cap-drop ALL, add NET_BIND_SERVICE): 1 capabilities
   net_bind_service
app with --cap-add SYS_PTRACE,NET_ADMIN: 16 capabilities
   chown, dac_override, fowner, fsetid, kill, setgid, setuid, setpcap, net_bind_service, net_admin, net_raw, sys_chroot, sys_ptrace, mknod, audit_write, setfcap
   !! net_admin: reconfigure network interfaces, routes and firewall rules
   !! sys_ptrace: trace other processes (host processes if the PID namespace is shared)
--privileged: 41 capabilities
   !! dac_read_search: open files by handle, bypassing path checks (a known escape class)
   !! net_admin: reconfigure network interfaces, routes and firewall rules
   !! sys_module: load kernel modules: arbitrary kernel code
   !! sys_rawio: raw I/O ports and device memory
   !! sys_ptrace: trace other processes (host processes if the PID namespace is shared)
   !! sys_admin: mount filesystems, many namespace and device operations: the 'new root'
   !! sys_boot: reboot the host
   !! bpf: load eBPF programs into the kernel

Two readings matter in reviews. First, the Docker default isn't minimal: net_raw allows crafting packets (ARP spoofing on the container network) and setuid/setgid/chown are only needed by images that switch users at start. Dropping everything and adding back what the application needs is the hardened pattern. Second, --privileged gives all 41 capabilities, including sys_admin and sys_module, and also disables seccomp and AppArmor and exposes host devices: it is not "a few more permissions" but the removal of the container boundary.

How weak isolation is detected

  • Configuration review: docker inspect (HostConfig.Privileged, CapAdd, PidMode, NetworkMode, SecurityOpt, Memory, PidsLimit), Kubernetes pod specs (securityContext.privileged, capabilities.add, hostPID, hostNetwork, resource limits), and IaC or manifest scanning (Pod Security, Image Hardening).
  • From inside (incident response or an assessment in scope): the /proc/self/status fields above, /proc/1/cgroup, visible host processes or devices.
  • At runtime: rules that flag privileged containers starting, capability-using syscalls (mount, init_module, ptrace on foreign PIDs) from containers (Runtime Detection).

How it is fixed

  • --cap-drop=ALL (Kubernetes capabilities: {drop: ["ALL"]}) and add back only what the process needs, typically nothing or NET_BIND_SERVICE.
  • Never --privileged or host namespaces for application workloads; isolate the few system agents that need them on dedicated nodes.
  • Set memory, CPU and PID limits so one container can't starve the host.
  • Run as a non-root user and set no-new-privileges (allowPrivilegeEscalation: false), so setuid binaries can't raise privileges even within the bounding set.

Pitfalls

  • Reading CapEff of a non-root shell (zero) and concluding the container is safe: the bounding set and the user the application runs as are what count.
  • Adding SYS_ADMIN to make one mount work: it is the broadest capability and the core of several escape classes.
  • Forgetting that the kernel is shared: no namespace protects against a kernel bug reachable through an allowed syscall.

Practice

1. Decode 00000000a80c35fb by hand from the default mask. Which two bits were added?

0xa80c35fb − 0xa80425fb = 0x81000, which is bits 12 (net_admin, 0x1000) and 19 (sys_ptrace, 0x80000).

2. A pod has hostPID: true and SYS_PTRACE. Why is that combination worse than either alone?

hostPID makes the host's processes visible; SYS_PTRACE allows attaching to them (subject to other checks). Together the container can inspect and manipulate processes outside itself, including reading their memory and secrets.

3. What would the session print for memory.max if the container were started with --memory 256m?

268435456, the limit in bytes (256 × 1024 × 1024); the kernel's memory cgroup then kills the container's processes when it exceeds that, instead of letting it exhaust the host.

Test yourself

What does a container share with every other container on the same host?

  1. Nothing
  2. The kernel
  3. The root filesystem
  4. The PID 1 process

What is the capability bounding set?

  1. The capabilities the process uses now
  2. The maximum set any process in the container can ever obtain
  3. The capabilities of the host
  4. A seccomp profile

How many capabilities does --privileged give in the program?

  1. 14
  2. 21
  3. 41
  4. 0

Which capability is the most common ingredient of container escapes?

  1. net_bind_service
  2. chown
  3. sys_admin
  4. kill