Sandbox
A sandbox is an isolated environment that runs code on the assumption that it may be buggy or hostile, and limits the damage that code can do. If the code crashes or tries to break out, the sandbox is built to keep the effect inside, though a flaw in its boundary can still let code out. The limits are set from outside, so the code inside cannot lift them. Web browsers use the same idea, running the code of each site in a process that cannot reach the rest of the computer.
A sandbox can be thrown away once its work is done, along with anything the code left behind. That makes it a safe place for untrusted code, such as programs submitted by users or written by AI agents. Starting again from a known state also makes runs repeatable, since nothing from an earlier run can change the result. It also lets code be tested in a setting that looks real without touching anything real.
The term covers many mechanisms, from a filter on one process to a whole virtual machine. They differ mainly in where they draw the isolation boundary, and in what each sandbox costs to start and run. A stronger boundary leaves less shared code between the sandbox and the host, so fewer bugs can lead out. The right choice depends on who wrote the code and what an escape would cost.
Sandbox mechanisms
Sandbox mechanisms are the ways a system draws the line around the code it runs. The weakest restrict one program on a shared kernel, and the strongest give each sandbox a kernel of its own. Every sandbox still shares something with the host, at the very least the processor. The question is how much shared code the program inside can reach.
Process sandboxes and containers share the host kernel, so a bug in that kernel can reach every one of them. User-space kernels such as gVisor narrow that path by handling most system calls in a separate program. The kernel interface is large, and each part of it is code an attacker can probe. The cost of the user-space approach is speed, since each system call handled there takes longer.
MicroVMs run each sandbox on a minimal hypervisor, as Firecracker does. Isolation grows stronger from the first kind to the last, and so does the cost of starting and running each sandbox. A bug in the guest kernel then reaches only the guest, and an escape needs a second bug in the hypervisor. Hardware support for virtualization keeps the cost of this low enough to run microVMs in large numbers.
| Mechanism | Description |
|---|---|
| Process sandbox | A filter such as seccomp limits which system calls one low-privilege process may make. |
| Container | A container is a group of processes that the host kernel isolates and runs from a packaged image. |
| User-space kernel | A separate program handles most system calls of a container, so only a narrow interface reaches the host kernel. |
| MicroVM | A microVM is a virtual machine with a minimal device model, built to start fast and run in large numbers. |
Ephemeral and persistent sandboxes
Ephemeral and persistent sandboxes differ in how long they keep what the code wrote. An ephemeral sandbox exists for one task, starting from a clean image and deleted with everything it wrote. Ephemeral sandboxes suit single tasks, such as running one piece of code an agent wrote. Persistent ones suit longer work, such as a development environment that an agent returns to over days.
Nothing leaks from one run of an ephemeral sandbox to the next, which makes it simple to reason about. The price is repeating any setup, such as installing packages, on every run. A prepared image can absorb that setup, by building the packages into the image once. Errors are also easier to reproduce, since every run starts from the same state.
A persistent sandbox keeps its disk, and sometimes its memory, across stops and restarts. It moves through a lifecycle of states instead of a single run, and storing it costs something even while it is idle. Restarting it brings back installed tools and files, so work continues where it stopped. Its state can also drift, so an old sandbox may behave differently from a fresh copy of the same image.
Start time and idle cost
Start time and idle cost are the two measures that decide whether a sandbox design works in practice. Strong isolation alone is not enough when a platform creates sandboxes in large numbers. A design that takes seconds to start may work for one long session but fail for thousands of short tasks. The cost of each sandbox matters as much, because it multiplies by every sandbox kept around.
Every request waits for its sandbox to start, so a slow start adds to each task. For short tasks, the start can take longer than the work itself. Lighter mechanisms start faster, since a process or container skips booting a kernel. Machines started ahead of time and snapshots hide much of the start for heavier ones.
Idle sandboxes hold memory and disk while no code runs in them. Platforms keep that cost down by stopping or archiving them, then pay for it in time when the sandbox starts again. Stopping frees memory but keeps the disk, while archiving moves the disk to cheaper storage. The longer a sandbox sleeps, the further its state moves from fast storage, and the longer it takes to wake.