Sandboxing untrusted code
Running code you didn't write, from user scripts to plugins to generated code, so an attack stays inside a boundary: which boundary to pick, what it costs to start and run, and what still leaks through it.
What it is
Sandboxing is running code you don't trust (a submission to an online judge, a customer's plugin, a tenant's serverless function, a script an LLM just wrote) inside a boundary that limits what it can see, touch and consume. The first design question is who you're protecting: the host machine, the other tenants sharing it, or both. Firecracker's design document is a good model of the mindset: it treats each guest vCPU thread as malicious from the moment it starts and nests trust zones from the guest outward to the host, with a barrier at each step.
The host kernel's own tools. A seccomp filter is a small BPF program a process installs on itself; the kernel runs it on each system call and the program can allow or reject the call based on its number and arguments. Children created with fork or clone inherit the filter and it survives execve, and an unprivileged process must set no_new_privs first, so a setuid program it launches under the filter gains no extra privileges. The seccomp() system call arrived in Linux 3.17. A namespace wraps one global resource (process IDs, mount points, network devices and ports, user and group IDs) so the processes inside see their own copy of it and not the host's; the manual page names containers as one use of them. Control groups organize processes into a hierarchy whose CPU time, memory and block-device I/O can be capped and measured, so one misbehaving tenant doesn't starve the rest. Whatever a container built from such primitives sets as limits, the kernel it shares with the host is what enforces them.
Why that isn't enough for hostile code. All of those rules are enforced by the one host kernel the sandboxed program can still call. The gVisor project's security introduction puts it plainly: the workload is one system call away from the host, filters have to be tailored to each workload, and a program that needs a broad call such as ioctl or io_uring is hard to filter tightly. gVisor's answer is an application kernel, the Sentry, written in Go and running in user space. It intercepts the guest's system calls and page faults and handles them itself, passing none through; a feature it hasn't reimplemented is unavailable to the guest. Breaking out means finding a bug in the Sentry and a second one in the host kernel, which share no code. The price is reduced compatibility and extra cost per system call.
MicroVMs. Firecracker puts a KVM virtualization boundary first and still runs its own process under seccomp, cgroups and namespaces, started by a jailer that sets up the cgroup and chroot, drops privileges and then executes the VMM. The guest sees a deliberately small device model: virtio network and block devices, a serial console and a partial keyboard controller used to request a reboot. With a minimal kernel, one vCPU and 128 MiB of RAM, the project reports creating about 5 microVMs per host core per second. Firecracker filters no network traffic itself, so everything a guest sends out should be filtered on the host.
Language runtimes. Cloudflare Workers runs many V8 isolates in one process; an isolate keeps its code from reaching memory outside itself, which lets a machine host thousands of tenants when each guest has to fit in a couple of megabytes. The platform freezes the value of Date.now() while code runs, offers no other timer and no threads, because a precise clock is what a Spectre-style attack needs. Outside V8 there is a second layer: namespaces and seccomp applied after the runtime starts but before any isolate loads, which allows an empty filesystem and a block on all filesystem-related calls, something a container engine normally can't do because it still has to exec the guest binary from disk. Access to the outside world comes through APIs designed as capabilities: with no filesystem API, there is no filesystem access. WebAssembly takes the same shape at the module level: each module runs fault-isolated in a linear memory of fixed maximum size, out-of-bounds reads and bad indirect calls trap, the call stack is kept out of reach of heap overflows, and the module reaches the host through the functions it imports and nothing else.
| What to limit | Mechanism | Why |
|---|---|---|
CPU time and memory | cgroups on the sandbox's process or VM | Keeps one tenant from exhausting the machine; a denial-of-service defense, not a confidentiality one |
Wall-clock time | A supervisor that kills the sandbox at a deadline | An infinite loop under a CPU quota still holds its slot |
System calls | seccomp filter, or an inner kernel that handles them | Each allowed call is attack surface on the host kernel |
Filesystem | Mount namespace, not chroot alone; an empty root where possible | Hides the host's files and other tenants' data |
Network | Host-side filtering of all guest traffic; an HTTP API instead of raw sockets | Blocks reaching internal services and the metadata endpoint |
Disk and network I/O rate | Rate limiting at the device barrier | A noisy tenant otherwise saturates shared devices |
When to use it
Reach for a sandbox whenever code crosses your trust boundary rather than data: user programs in a judge or a notebook, plugins and transforms customers upload, functions from many tenants on a shared fleet, agents that run model-written code, and parsers for file formats with a history of exploits. Then choose the boundary by two numbers, how hostile the author may be and how many tenants a machine must hold. Workers needs thousands of resident tenants per machine, many of which spend less than a fraction of a millisecond on an event, so a per-tenant process was ruled out and isolates won. gVisor fits most Linux containers (it does not implement every system call) that need a process-like footprint and fast start, and its default Systrap platform needs no hardware virtualization, so it runs inside a VM you rent. Firecracker is built to run workloads from different customers on one machine, with the strongest line between them.
Stack layers rather than trusting one. Each of the three systems above does this on its own account: Firecracker says it should run constrained at the process level even behind KVM, gVisor uses seccomp and namespaces as a second line that needs no per-workload tuning, and Workers wraps V8 in a process-level sandbox and spreads tenants across separate runtime instances by trust level, so a free-tier script doesn't share a process with an enterprise one. Patch speed is a layer too: Workers reports a patch gap under 24 hours between a V8 fix being published and reaching production, which matters because fixes for open-source engines are visible to attackers the moment they land.
When not to
If the code is written by your own team and shipped through your own pipeline, a dedicated sandbox mostly buys overhead; ordinary least privilege (a non-root user, a reduced capability set, the resource limits you'd set anyway) is the right amount of isolation. If what arrives from outside is data rather than code, validate it: a schema check or a parser with size and depth limits is cheaper and easier to reason about than a VM. And if users need a small, fixed set of operations, give them a configuration format or a narrow expression language instead of a general-purpose runtime; nothing executable means nothing to contain.
A lighter boundary is also the wrong one when the workload doesn't fit it. Code that leans on broad kernel interfaces such as ioctl or io_uring is hard to filter tightly, and gVisor's own docs warn that system-call-heavy programs can perform poorly on it and that compatibility is reduced. There a full VM, or no multi-tenancy on that host, is the honest choice. Finally, a sandbox covers what happens inside it and nothing earlier: gVisor lists an exploit in containerd that starts a container without it as outside its protection, and both gVisor and Workers say Spectre-class side channels have to be handled at the host, the hardware or the API design rather than by the boundary itself.
Compared
| Boundary | What enforces it | Density and start | What still gets through | Pick it when |
|---|---|---|---|---|
Namespaces, seccomp, cgroups (a plain container) | The host kernel | Process-cheap; the highest density of the native options | A bug in any system call the filter allows | Authors you mostly trust and a workload you can write a tight filter for; the threat is misbehavior, not malice |
User-space kernel (gVisor) | The Sentry in Go, with seccomp behind it | Process-like footprint, flexible memory, fast start | Needs a Sentry bug plus a host kernel bug; side channels pass | Most Linux containers from tenants you partly trust, or a host without hardware virtualization |
MicroVM (Firecracker) | KVM, then a jailed VMM process | About 5 created per host core per second at 128 MiB each; a kernel per guest | A hypervisor or KVM bug; guest egress until you filter it | Mutually distrusting customers running any binary; the strongest line is worth the memory |
Engine isolate (V8) | The JavaScript engine's memory isolation | Many per process, thousands per machine, a few megabytes each | A V8 bug; Spectre-style side channels, which need extra layers such as removed timers and process separation | Many tiny, latency-sensitive tenants writing JavaScript or Wasm |
WebAssembly module | Bounds-checked linear memory and typed imports | Runs inside a host runtime, in a browser or outside one | A runtime bug or a host function that does too much | Plugins compiled to Wasm that need a narrow, explicit API |
In a picture
Step 1 of 4
Start with the host kernel's limits
The program's system calls pass through a seccomp filter before the host kernel sees them, and cgroups meter what the process may consume.