Part 5 of 13 · 2 min read
Namespaces, environment and directory
Namespaces: seeing less
A namespace gives a process its own private view of one kind of system resource. They are the foundation of containers, and the sandbox uses four of them through the unshare command.
- Process-id namespace. The program sees only its own processes, and when the first one ends, the kernel kills all the rest. A program that starts a detached background process and exits cannot leave it running. In the experiment the detached "orphan" survived at L0, L1 and L2 and was gone at L3.
- Network namespace. The program gets a network with no interfaces up, not even this machine's own loopback address. A connection attempt fails at once with error 101 ("Network is unreachable"). The honeypot web server on the host's loopback is not reachable from inside, which is how the experiment proves that a program could not send anything out.
- Mount namespace. Paths can be replaced for the program only: a directory by an empty one, a file by
/dev/null. The real files are untouched on the host. The experiment hides a "home directory" that holds a fake SSH key. Writing into the hidden directory also goes nowhere. - User namespace. Needed to do the other three without being the real root. The program is "root" inside it. The last step,
setpriv --no-new-privs --bounding-set=-all, removes every capability and prevents gaining any, so that "root" cannot even mount a filesystem (a test tries and fails).
Fail closed
If the machine cannot create a namespace (a kernel with user namespaces turned off, a locked-down container, a different operating system), the example refuses to run and raises SandboxUnavailable. The alternative, quietly running without the isolation the policy asked for, is a defect planted (fail_open), and a check catches it. A sandbox that stops sandboxing without saying so is worse than none, because everyone believes it is there.
The environment and the working directory
Two cheap layers close two real holes.
- A scrubbed environment. A child process inherits the parent's environment variables, and that is where API keys live. At L2 the program starts with only
PATH,LANG,HOMEandTMPDIR(both pointing into its own directory). Claude Code's own documentation makes the same point: its sandboxed commands inherit the parent's environment by default, credentials included, and it offers a setting to strip them. - A private working directory. At L0 and L1 the program runs in the application's own directory, and one of the attacks simply overwrites a file there. From L2 each sandbox session gets a fresh directory with owner-only permissions. It lives across several runs (so a program can leave a file for the next one) and is deleted when the session ends.
Neither layer helps against a program that reads a file by its full path. That takes the filesystem layer.