Part 10 of 13 · 4 min read
Interview answers and mistakes
I would start from the threat model, not the tooling. There are three ways running code goes wrong. One is a mistake (a runaway loop, a huge allocation). Another is an instruction hidden in data the agent read. The third is a trusted helper on the host side that gets tricked into using its own privilege. Only the first needs no attacker at all. That is why limits on time, memory, processes and output come before anything else.
Then I would build in layers, because each one is narrow by design. First come resource limits and a polling monitor for anything the kernel will not enforce for the privilege level I run at. Next come a scrubbed environment and a private working directory. Then come process, network and filesystem namespaces. I would test, not assume, that each layer bites, the way RLIMIT_NPROC turned out not to for root. I would insist on both filesystem and network isolation, because either alone leaves an attack open. No network still lets a secret be read and printed to the model. Filesystem isolation alone does not stop a program that already has what it needs from sending it out. I would not rely on a filter over the code's text. It only catches phrasings someone thought to ban, and it blocks ordinary work in the process.
Then the part that separates answers: how do I know my sandbox's checks would catch a real regression? Plant defects and predict which checks fail before running them. In my own run, 27 of 29 defects were caught. The two escapes turned out to be checks that were already covered by a different guard. That is itself a useful thing to learn about your own suite.
Follow-ups worth preparing for: "What does a filter on the code actually protect against?" (almost nothing against a determined attacker; it is a cheap hint to the model, not a defence). "Where does the confused-deputy risk live?" (the host-side file API, which runs with the host's privileges on names the sandboxed program chose). "What would you still need for hostile, multi-tenant code?" (a stronger boundary than a shared kernel — gVisor or a virtual machine, not just namespaces).
Mistakes that undercut a sandbox
- Trusting a resource limit without checking who enforces it. Symptom: a process-count cap is set, and a program still starts far more processes than the cap allows. Cause:
RLIMIT_NPROCis not enforced for root, and the sandbox runs as root inside its namespace. Fix: a polling monitor that counts processes itself and kills the tree when the count is exceeded. - Checking a file name for
..and absolute paths but never resolving symbolic links. Symptom: the file API refuses the obvious traversal attempts but still leaks a secret file. Cause: a program inside the sandbox created a link whose name looks harmless but whose target is not. Fix: resolve the name all the way through any link and compare the resolved path against the resolved root before opening anything. - Isolating the network but not the filesystem, or the reverse. Symptom: a security review checks the box for "sandboxed" after adding one of the two. Cause: each layer only stops the attack it was built for; neither alone stops both exfiltration and tampering. Fix: test both configurations against both kinds of attack before calling either one sufficient.
- Letting the sandbox run unisolated when the host cannot create the needed namespaces. Symptom: code that should have been contained runs with no limits at all, on a machine nobody checked in advance. Cause: the setup silently fell back instead of refusing. Fix: fail closed — raise an error and refuse to run rather than execute with weaker isolation than the policy asked for.
- Trusting a passing test suite without ever planting a defect. Symptom: a real regression in a limit or a check ships unnoticed. Cause: the suite was never run against a known-broken sandbox. Fix: plant realistic defects, predict which checks should fail, and treat any defect that escapes as a documented gap, not a surprise found later.
Check yourself
A program in the sandbox opens a socket to your internal metadata service. Which layer stops it, and which layers do not?
Show answer
Network isolation, usually a network namespace or no route. Time, memory and process limits do not. Filesystem isolation does not either, which is why both are needed.
For the curious Why a filter is not a sandbox
A filter reads the code before it runs, and code can be written so the filter does not recognise it. The experiment in this module runs sixteen programs at six levels and shows which attacks only the last level stops. The suite runs real processes under real time limits: a full run here took 219 tests in about 2 minutes 40 seconds, so it is slow, not stuck.
What you will use daily at work
| Practice | What it looks like |
|---|---|
| Limits on time, memory and processes | Set before the code starts; killed as a group. |
| A scrubbed environment | No inherited secrets or tokens. |
| A tool result that guides | "Timed out after 10 s; split the work or use less data." |