What Containers Isolate and What They Do Not
A container is a Linux process with namespaces, cgroups, capabilities and a seccomp filter around it, sharing one kernel with every other container on the node. Run small Docker demos to see each layer, decode a capability bitmask, and learn which pod settings remove the isolation and the traces that escape attempts leave.
Contentsand lesson path
On this page
- Why it matters
- Namespaces and cgroups: a restricted view, not a separate machine
- Capabilities, seccomp and no-new-privileges
- Read-only roots, and what isolation does not cover
- Check your understanding
- Apply it
- Review prompt
Lessons of Container & Kubernetes Security
- What Containers Isolate and What They Do Not Concept · 15 min
- Hardening and Scanning Images Lab · 25 min
- Cluster Hardening: RBAC, Pod Security and Network Policy Lab · 30 min
- Admission Control for Trusted Workloads Worked example · 20 min
- Runtime Detection and Cluster Audit Logs Lab · 25 min
- Container & Kubernetes Security Check Knowledge check · 6 questions
What you will learn
- Build minimal, non-root container images, scan them for vulnerabilities and misconfigurations, and enforce signed or allow-listed images at admission.
- Harden a Kubernetes cluster with least-privilege RBAC, Pod Security Standards, network policies and sound secret handling, verified against the CIS Kubernetes Benchmark.
Prerequisites: Kubernetes Fundamentals
01. Why it matters
Many teams treat a container as a small virtual machine. It is not one. A container is an ordinary Linux process, and the kernel gives that process a restricted view of the system. Every container on a node shares the same kernel. The boundary is therefore only as strong as the kernel features that draw it and the settings you choose. One setting, such as privileged: true in a pod spec, removes most of it.
As a defender you need two skills. First, know which settings weaken isolation, so that you can harden images and pod specs; the rest of this skill does that. Second, recognise the traces that an attempt to break out leaves behind: a field in a pod spec, an event in the Kubernetes audit log, an alert from a runtime sensor. MITRE ATT&CK, a public knowledge base of attacker behaviour, lists these techniques in its Containers matrix. This lesson names the techniques you will meet and the evidence each one leaves. It does not show how to carry them out.
You need: Docker and the alpine:3.20 image (docker pull alpine:3.20), plus Python 3.10 or later. The outputs below come from Docker Engine 29.8.0 on Linux 6.8. Container names start with seclab- so that you can find and remove them easily.
02. Namespaces and cgroups: a restricted view, not a separate machine
Two kernel features make a container. Namespaces give a process its own view of one kind of resource. There are namespaces for process IDs (PID), the file system tree (mount), network interfaces and ports (network), the host name (UTS), inter-process communication (IPC), user and group IDs (user) and the cgroup tree. cgroups (control groups) limit and count what a group of processes may use, such as CPU time, memory and the number of processes. A container runtime such as containerd or Docker creates these for each container and then starts your program inside them.
Start a container that sleeps, then look at it from inside and from the host.
docker run -d --rm --name seclab-sleeper alpine:3.20 sleep 300 > /dev/null
echo "inside the container:"
docker exec seclab-sleeper ps -o user,pid,comm
echo "seen from the host:"
docker top seclab-sleeper -o user,pid,comm
echo "kernel inside: $(docker exec seclab-sleeper uname -r), on the host: $(uname -r)"
docker rm -f seclab-sleeper > /dev/nullinside the container:
USER PID COMMAND
root 1 sleep
root 7 ps
seen from the host:
USER PID COMMAND
root 2549927 sleep
kernel inside: 6.8.0-138-generic, on the host: 6.8.0-138-genericInside, sleep is PID 1 and the container sees only its own two processes. That is the PID namespace at work. The host sees the same process with an ordinary host PID, owned by root. The kernel version is the same in both places, because there is only one kernel.
So namespaces change what a process can see. They do not give it a kernel of its own. This has two consequences. A bug in the kernel can be reached from any container on the node. And UID 0 inside the container is UID 0 on the host, unless the pod runs in a user namespace. In Kubernetes you request one with hostUsers: false in the pod spec (stable since v1.36); container root is then mapped to an unprivileged UID on the node.
03. Capabilities, seccomp and no-new-privileges
Linux splits the power of root into about forty capabilities. For example, CAP_NET_ADMIN lets a process change network settings and CAP_SYS_ADMIN allows many administrative operations, including mounting file systems. The kernel keeps several capability sets for each process as hexadecimal bitmasks in /proc/<pid>/status. CapEff is the effective set, the capabilities in force now. CapBnd is the bounding set, a ceiling that no program started later in the container can rise above.
Two more lines in the same file matter. Seccomp: 2 means a seccomp filter is active: a filter that decides which system calls (requests from a program to the kernel) the kernel accepts from this process. NoNewPrivs: 1 means the process and its children cannot gain privileges when they run a set-user-ID program such as su. Compare a default container, a non-root user and a container with every capability dropped.
show='grep -E "^(CapEff|CapBnd|NoNewPrivs|Seccomp):" /proc/self/status'
echo "== default"
docker run --rm --name seclab-caps alpine:3.20 sh -c "$show"
echo "== --user 65532:65532"
docker run --rm --name seclab-caps --user 65532:65532 alpine:3.20 sh -c "$show"
echo "== --cap-drop ALL --security-opt no-new-privileges"
docker run --rm --name seclab-caps --cap-drop ALL \
--security-opt no-new-privileges alpine:3.20 sh -c "$show"== default
CapEff: 00000000a80425fb
CapBnd: 00000000a80425fb
NoNewPrivs: 0
Seccomp: 2
== --user 65532:65532
CapEff: 0000000000000000
CapBnd: 00000000a80425fb
NoNewPrivs: 0
Seccomp: 2
== --cap-drop ALL --security-opt no-new-privileges
CapEff: 0000000000000000
CapBnd: 0000000000000000
NoNewPrivs: 1
Seccomp: 2A bitmask is hard to read, so decode it. Each bit number is a capability defined in the kernel header capability.h.
"""Decode a Linux capability bitmask from /proc/<pid>/status."""
import sys
# Bit numbers from include/uapi/linux/capability.h (Linux 5.9 and later).
NAMES = [
"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",
]
for mask_hex in sys.argv[1:]:
mask = int(mask_hex, 16)
held = [name for bit, name in enumerate(NAMES) if mask >> bit & 1]
print(f"{mask_hex}: {len(held)} capabilities")
for name in held:
print(f" CAP_{name}")python3 decode_caps.py 00000000a80425fb00000000a80425fb: 14 capabilities
CAP_CHOWN
CAP_DAC_OVERRIDE
CAP_FOWNER
CAP_FSETID
CAP_KILL
CAP_SETGID
CAP_SETUID
CAP_SETPCAP
CAP_NET_BIND_SERVICE
CAP_NET_RAW
CAP_SYS_CHROOT
CAP_MKNOD
CAP_AUDIT_WRITE
CAP_SETFCAPRead the results together. The default container runs as root with 14 capabilities, among them CAP_NET_RAW (send raw packets, which can be used to forge traffic on the pod network) and CAP_SETUID. A non-root user has an empty effective set, but the bounding set is unchanged, so a set-user-ID binary in the image could still give a process those capabilities back. Dropping ALL empties the bounding set as well, and no-new-privileges closes the set-user-ID path. In a Kubernetes pod spec the same controls are capabilities.drop: ["ALL"], allowPrivilegeEscalation: false, and runAsNonRoot: true with a runAsUser.
Note one difference between the tools. Docker applies its default seccomp profile unless you switch it off, which is why every run above shows Seccomp: 2. Kubernetes runs containers with seccomp Unconfined unless the pod sets seccompProfile: {type: RuntimeDefault} or the kubelet runs with --seccomp-default. The same image can therefore be filtered on a laptop and unfiltered in a cluster. That is one reason the Restricted Pod Security Standard requires the field to be set.
04. Read-only roots, and what isolation does not cover
A read-only root file system stops a process from changing the files of the image: it cannot replace a binary or drop a tool into /usr/bin. Most programs still need a writable scratch folder, so you mount a small temporary file system (tmpfs) on /tmp. In Kubernetes that is readOnlyRootFilesystem: true plus an emptyDir volume.
docker run --rm --name seclab-ro --read-only --tmpfs /tmp alpine:3.20 \
sh -c 'touch /etc/marker; touch /tmp/marker && echo "write to /tmp: ok"'touch: /etc/marker: Read-only file system
write to /tmp: okThese controls reduce what a compromised process can do. They do not make a container a security boundary like a virtual machine, and some pod settings remove them. Learn the settings below as traces: when you see them in a pod spec, in an audit log create event or in an admission rejection, ask who needs them and why.
- Escape to Host (T1611).
privileged: true,hostPID: true, an addedCAP_SYS_ADMIN, or ahostPathvolume that mounts/or a runtime socket such as/run/containerd/containerd.sock. Each one gives a process in the container a handle on the node. A mounted runtime socket, for example, lets the process ask the runtime for a new container with none of the restrictions. - Deploy Container (T1610). A new pod from an identity that does not normally create pods, often in an unusual namespace and with one of the fields above. The trace is a
createevent onpodsin the audit log. - Exploitation for Privilege Escalation (T1068). A kernel bug reached from inside a container. The shared kernel is the reason this matters. Seccomp, dropped capabilities and prompt kernel patching on the nodes reduce the exposure; none of them removes it.
None of these techniques needs a clever exploit when the pod spec already grants the access. That is why the next lessons enforce settings at admission instead of trusting each team to set them.
05. Check your understanding
06. Apply it
Prepare for the skill project. Take the manifests you deployed in Kubernetes Fundamentals and, for each container, write down four facts: the user it runs as, its effective and bounding capabilities, its seccomp mode and whether its root file system is writable. On a running kind cluster, kubectl exec <pod> -- grep -E "^(Cap|Seccomp)" /proc/1/status gives you the raw values, and decode_caps.py turns them into names. Distroless images have no shell or grep, so that command fails there; instead run kubectl debug -it <pod> --image=busybox:1.37 --target=<container> -- grep -E "^(Cap|Seccomp)" /proc/1/status, which reads the target's status from an ephemeral container that shares its process namespace. Mark every container that runs as root, keeps the default capabilities or shows Seccomp: 0; these are the first things you will fix in the next labs.
07. Review prompt
Without looking back:
- Why does a container see PID 1 while the host sees a large PID for the same process?
- What is the difference between
CapEffandCapBnd, and why does droppingALLmatter for a non-root user? - Why can the same image run with a seccomp filter under Docker and without one in a Kubernetes pod?
- Name two pod spec fields that are traces of Escape to Host (T1611), and say what each gives the process.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
6 questions · about 5 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.