CSE Roadmap

Core tier · about 4 hours · 13 parts

Part 5 of 13 · 2 min read

Limits are not isolation, and the gap between them is where TOCTOU lives

Capping how much damage a process can do in a given direction says nothing about whether it can still read or connect to things it should never have touched.

Think of putting a speed limiter on a car without taking away the keys to the neighbor's garage. The car can only go so fast. But it can still drive straight into places it was never supposed to go.

a sandbox is an environment that restricts what running code can read, write, execute and connect to, enforced by an isolation boundary. A separate process user, container, or VM -- and resource limits alone are not that boundary: a CPU- and memory-limited process can still read every file its user can read and open any network connection. That means TOCTOU (time of check to time of use), where the situation changes between a check and the action that follows it, remains a real gap even after limits are in place.

A worked example. A child process started with a scrubbed environment (only PATH set, nothing inherited) prints "not visible" when asked to read SECRET_TOKEN from its environment. The identical probe code, run with the full parent environment passed through instead, prints "hunter2" -- the actual secret value -- even though the same CPU, memory, and file-size limits are in effect in both runs. The resource limits never touched this at all; what changed was a completely separate decision about what to hand the child in the first place.

The key lines. From code/mini-sandbox/sandbox.py:

# from code/mini-sandbox/sandbox.py
r = subprocess.run([sys.executable, "-I", "-c", code], capture_output=True, text=True, timeout=wall_s, preexec_fn=limits,
                   env=env if env is not None else {"PATH": "/usr/bin:/bin"})

Read it as: the default env argument is a hardcoded dictionary with just one key, PATH. This single line is doing two unrelated jobs at once. preexec_fn=limits sets the resource caps, and the separate env= argument decides what the child can see in its environment. The module's own test deliberately runs the same probe both ways to show these are two independent controls, not one.

Sandbox, isolation boundary, TOCTOU

A sandbox is an environment that restricts what running code can read, write, execute and connect to. An isolation boundary is the mechanism that enforces it (a separate process user, container, VM). TOCTOU is the situation changing between the time something is checked and the time it is used.

Example: a resource-limited process that still prints "hunter2" when handed the parent's environment is the clearest possible demonstration that limits and isolation are different controls -- the process obeyed every CPU and memory cap perfectly while reading a secret it should never have been able to see, because nothing about a CPU limit touches what's in an environment variable.