Sub topic 12
Isolation Strength and Security Boundaries
This chapter assumes you know that containers share the host kernel and that namespaces and cgroups restrict what a process sees and uses. Here we look at what that means for security: where the boundary between workloads sits, and how strong it is.
Where the boundary sits
A security boundary is the line an attacker must cross to reach something they should not. The two technologies put that line in different places.
- A VM's boundary is the hypervisor. A guest talks to virtualized hardware, and the hypervisor mediates every access to the real machine. The attack surface is the hypervisor and its device emulation, which is small compared with a full kernel.
- A container's boundary is the host kernel. Every container makes system calls directly to the same kernel. The attack surface is the whole system call interface plus the kernel code behind it.
Why a kernel bug matters more for containers
If a process inside a container exploits a kernel vulnerability, it runs code inside the host kernel. That kernel serves every other container on the machine, so a successful exploit can mean a full escape. In a VM, the same exploit only compromises the guest kernel. The attacker would then need a second, separate bug in the hypervisor to get out.
Namespaces are not a security feature in themselves. They hide things from a process, but they do not stop it from calling kernel code that has a flaw.
Hardening a container
Because the kernel is the boundary, you reduce what a container can ask of it. Common controls are:
- Run as a non-root user, so a compromise starts with fewer privileges.
- Drop Linux capabilities, which are the pieces that make up root's powers.
- Apply a seccomp profile, which blocks system calls the workload never needs.
- Use a mandatory access control system such as AppArmor or SELinux.
- Mount the root filesystem read-only.
This example applies several of them. It runs on a Linux host with Docker installed.
docker run --rm \
--user 1000:1000 \
--cap-drop=ALL \
--read-only \
--security-opt no-new-privileges \
alpine idThe command prints a user ID of 1000 with no root access. The no-new-privileges option stops a process from gaining privileges through setuid binaries.
Strengthening the boundary
When shared-kernel isolation is not enough, add a layer between the workload and the host kernel. Chapter 13 covers these approaches in detail. In short:
- Rootless mode and user namespaces map root inside the container to an unprivileged user on the host.
- Sandboxed runtimes such as gVisor intercept system calls in a user-space kernel.
- MicroVMs such as Firecracker and Kata Containers give each container its own small VM and kernel.
Comparing the boundaries
| Aspect | Container | VM |
|---|---|---|
| Boundary | Host kernel | Hypervisor |
| Attack surface | Full system call interface | Hypervisor and emulated devices |
| Kernel flaw in workload | Can affect all containers | Contained to the guest |
| Typical use for untrusted code | Needs extra hardening | Common default |
No boundary is perfect. Hypervisors have had escape bugs too. The practical difference is that the container boundary is larger and changes with every kernel release.
Common mistakes
- Assuming a container is a sandbox by default. It is process isolation, not a hardened boundary.
- Running workloads from different trust levels, such as customers' code, on one shared kernel.
- Leaving containers as root with default capabilities.
- Forgetting that all containers on a host depend on the same kernel patch level.
Exercise
A service runs code uploaded by anonymous users. Why is a plain container a weaker choice than a VM for this, and name two ways to strengthen the isolation.
Show answer
A plain container shares the host kernel, so a kernel vulnerability exploited by the uploaded code could compromise the host and every other container. A VM adds a hypervisor boundary, which requires a second bug to escape. You can strengthen isolation by running the code in a microVM such as Firecracker or Kata Containers, or in a sandboxed runtime such as gVisor. Dropping capabilities and applying a seccomp profile also help.
Exercise
Why is mounting /var/run/docker.sock into a container dangerous?
Show answer
The socket gives full control of the Docker daemon, which runs as root on the host. A process with access can start a privileged container that mounts the host filesystem, so it is effectively host root.