Sub topic 13
Defense in depth and what these controls do not cover
This chapter assumes you have run containers with docker run, know basic Linux users and permissions, and can read Kubernetes pod manifests. It ties together the earlier controls and states what they leave open.
Layers, not a wall
Every control in this tutorial reduces what a compromised process can do. None of them stops the compromise itself. Defense in depth means stacking controls so that when one fails, another still limits the damage.
The controls you have applied cover different steps of an attack:
| Control | What it limits |
|---|---|
| Non-root user | Files and resources the process can touch by default |
| Dropped capabilities | Privileged kernel operations such as raw sockets or mounting |
| no-new-privileges | Gaining rights through setuid binaries |
| Seccomp profile | Which system calls reach the kernel at all |
| Read-only root filesystem | Persistence and tampering inside the container |
Because the layers are independent, a mistake in one is not fatal. If a profile allows a risky system call, the missing capability may still make that call fail. If a process writes to /tmp, the noexec mount option can still stop it from running what it wrote.
What these controls do not cover
The controls shrink the kernel attack surface. They do not remove it, and they do not address several other risks.
- Kernel vulnerabilities. Seccomp allows most system calls in the default profile. A bug in an allowed call, such as a flaw in a filesystem or networking path, can still give an attacker kernel code execution. All containers on the host share that kernel.
- Application bugs. Injection, broken authentication, and logic flaws happen inside the application. A hardened container still serves the vulnerable code, and an attacker can still read any data the app can read.
- Network access. None of these flags restrict traffic. A compromised process can still call internal services, the cloud metadata endpoint, or the internet unless you add network policies or firewall rules.
- Secrets and mounted data. Anything you mount or pass as an environment variable is available to the process. A read-only root filesystem does not make a mounted volume read-only.
- Image contents. Vulnerable packages, embedded credentials, and untrusted base images are supply chain problems. Scan images and pin versions separately.
- Resource abuse. A process can still consume CPU, memory, and process slots unless you set limits.
- Privileged settings.
--privileged, mounting the Docker socket, or sharing the host PID or network namespace bypass much of what you configured.
Adding the missing layers
Choose extra controls based on the gaps above.
- Limit resources with
--memory,--cpus, and--pids-limitin Docker, orresources.limitsin Kubernetes. - Restrict the network with Kubernetes NetworkPolicy, or user-defined Docker networks, and block access to the metadata endpoint.
- Add mandatory access control with AppArmor or SELinux, which restrict file and process access in ways seccomp cannot.
- Run rootless or user-namespaced so that root inside the container maps to an unprivileged user on the host.
- Enforce settings by policy. In Kubernetes, the Pod Security Standards
restrictedprofile requires several of these settings, so a pod without them is rejected at admission. - Keep the host patched and monitor runtime behavior for unexpected system calls or file writes.
The following Docker command adds resource limits to the hardened setup from earlier chapters:
docker run --rm \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid,size=64m \
--cap-drop ALL \
--security-opt no-new-privileges:true \
--user 10001:10001 \
--memory 256m \
--pids-limit 100 \
python:3.12-slim \
python -c "print('hardened and limited')"Deciding how far to go
Match the effort to the risk. An internal batch job that processes trusted input may need only the baseline settings. A service that parses files from the internet deserves a custom seccomp profile, network policy, and policy enforcement. Code you do not trust at all needs stronger isolation than a shared kernel can provide.
Exercise
A service runs with all capabilities dropped, the default seccomp profile, and a read-only root filesystem. An attacker exploits an SQL injection flaw in the app. Which of the controls would stop the attacker from reading the database contents, and why?
Show answer
None of them. SQL injection abuses the application's own legitimate access to the database, using only permitted system calls and no privileged operations or filesystem writes. Fixing it requires parameterized queries, least-privilege database accounts, and network restrictions, which are outside what capabilities, seccomp, and a read-only filesystem cover.
Exercise
Name two controls from this chapter that would reduce the harm if an attacker used a compromised container to reach the cloud metadata endpoint or exhaust host memory.
Show answer
A network policy (or firewall rule) blocking the metadata endpoint addresses the first. Memory and PID limits such as --memory and --pids-limit address the second.