Sandboxed Execution for Coding & Computer Agents
Isolate untrusted LLM-generated code at scale: Firecracker microVMs vs gVisor vs containers, warm snapshot pools, CoW overlays, and secret-injecting proxies.
In this lesson (3 bite-sized sections)
Why coding agents require hardware-enforced isolation
Cloud coding and computer-use agents such as Claude Code, Codex, Devin, Modal, and E2B execute LLM-generated shell commands, package installs, and untrusted repository test suites on every turn. Even well-meaning users trigger indirect prompt injection hidden inside a cloned README or postinstall hook that tries to read environment secrets, probe cloud instance metadata (169.254.169.254), or escape to neighboring tenants.
Isolation choices hinge on kernel boundaries. Standard Docker containers (runc) share the host Linux kernel; a single kernel syscall bug grants full host root escape. gVisor (runsc) interposes a user-space Go kernel between guest syscalls and the host (strong defense-in-depth, with measurable syscall and filesystem overhead on heavy builds). WASM / Pyodide starts in sub-milliseconds with tight memory safety, but cannot run arbitrary native Linux compilers, Dockerized services, or C extensions. Firecracker microVMs pair KVM hardware virtualization with a minimal Rust VMM, giving each session its own dedicated guest Linux kernel in ~125ms cold boot (<50ms from memory snapshots) at under 5 MiB VMM overhead.
| Runtime | Isolation boundary | Startup time | POSIX / native builds | Primary trade-off |
|---|---|---|---|---|
| Docker (runc) | Shared host kernel (namespaces + seccomp) | 50-200 ms | Full native speed | Single kernel bug exposes host and peer tenants |
| gVisor (runsc) | User-space Go kernel intercepting syscalls | 100-300 ms | High compatibility | Syscall and heavy I/O overhead on builds |
| Firecracker microVM | KVM hardware virtualization + dedicated kernel | ~125 ms cold / <50 ms snapshot | Full Linux userland | Requires KVM bare-metal or nested virt hosts |
| WASM / Pyodide | Memory-safe linear sandbox, capability imports | 1-15 ms | No native ELF/POSIX shells | Cannot run arbitrary apt, gcc, or bash toolchains |
Sub-second warm snapshot pools and copy-on-write overlays
Cold-booting a VM, cloning a 4 GB repo, and installing toolchains takes 20 to 45 seconds, ruining interactive agent turns. Platforms achieve sub-second assignment using pre-warmed microVM snapshot pools paired with copy-on-write (CoW) overlays (OverlayFS, dm-thin, or lazy FUSE block paging).
Base images with OS and language runtimes are booted once, warmed, and snapshotted (CPU state, memory pages, rootfs). When an agent turn arrives, the scheduler claims a warm slot or restores a snapshot with demand-paged memory and mounts the tenant's repository diff as an ephemeral upper CoW layer. After the session finishes, the microVM is destroyed completely rather than scrubbed and reused across tenants. Run the demo below to see how warm pool sizing absorbs bursty agent arrivals versus cold-start fallbacks.
Egress allowlisting, secret-injecting proxies, and kill switches
A perfect hypervisor still fails if guest environment variables hold raw GITHUB_TOKEN, LLM API keys, or cloud IAM credentials: one injected shell command exfiltrates env over HTTPS. Production sandboxes enforce default-deny egress on the host TAP device (blocking RFC 1918 subnets and 169.254.169.254) and route permitted hosts through a host-side credential-injection proxy. The guest sees only an opaque placeholder token; the host proxy validates the destination against a per-session allowlist and injects the real short-lived credential outside the VM.
Finally, hostile or buggy code creates fork bombs, infinite loops, and runaway disk writes. Enforce hard ceilings outside the guest: vCPU quotas, strict non-swappable RAM caps, max PID counts, rootfs write limits, and an external wall-clock kill switch that terminates the VMM cleanly while returning partial stdout/stderr so the agent can recover.
Match each coding-agent sandbox control to the layer that enforces it.
Sort 4 more items to check
Recap: isolate untrusted coding agents behind hardware-virtualized microVMs, hit sub-second starts with single-tenant warm snapshot pools and CoW overlays, keep raw secrets out of guests via egress-allowlisted credential proxies, and enforce resource and wall-clock kill switches from the host.
Sources: Firecracker: lightweight virtualization for serverless · gVisor security architecture · E2B sandbox documentation · Modal sandboxes guide
import heapq
import statistics
# 40 turns: steady (1/40ms) then burst (1/15ms); peak = 420/15 = 28 warm slots
ARRIVALS_MS = [i * 40 for i in range(15)] + [600 + i * 15 for i in range(25)]
COLD_BOOT_MS, WARM_ATTACH_MS = 420, 25
def simulate_pool(pool_size: int) -> None:
ready = [0] * pool_size
heapq.heapify(ready)
starts, waits, hits = [], [], 0
for t in ARRIVALS_MS:
if ready and ready[0] <= t:
heapq.heapreplace(ready, t + COLD_BOOT_MS)
hits += 1
starts.append(WARM_ATTACH_MS)
waits.append(0)
elif ready and (ready[0] - t) + WARM_ATTACH_MS < COLD_BOOT_MS:
wait = heapq.heapreplace(ready, ready[0] + COLD_BOOT_MS) - t
starts.append(wait + WARM_ATTACH_MS)
waits.append(wait)
else:
starts.append(COLD_BOOT_MS)
waits.append(0)
p95 = sorted(starts)[int(len(starts) * 0.95) - 1]
hit_pct = 100.0 * hits / len(ARRIVALS_MS)
print(f"pool={pool_size:>2} | hit={hit_pct:5.1f}% | avg={statistics.mean(starts):5.1f}ms | p95={p95:3d}ms | max_wait={max(waits):3d}ms")
for size in (0, 8, 14, 20, 28):
simulate_pool(size)Apply
Your turn
The task this lesson builds to.
Architect a multi-tenant cloud sandbox service that executes untrusted LLM-generated Python, Node, and shell commands for 1,000 concurrent coding-agent sessions, targeting sub-300ms warm startup while preventing host escape and secret exfiltration.
Think about
- Which isolation runtime gives full Linux compiler/toolchain compatibility without sharing a kernel across tenants?
- How do warm snapshot pools and copy-on-write overlays hit sub-300ms startup without reusing live VMs between customers?
- How can a coding agent push to GitHub or install private packages if raw API credentials never enter the sandbox?
- Which host-enforced resource caps stop fork bombs, infinite loops, and disk fills?
Draft your design answer here Save your draft, compare against the model answer, and track progress.
Practice
Make it stick
A second problem on the same idea, so it survives past today.
Evaluate and redesign an existing coding-agent runner that clones user repositories into privileged Docker-in-Docker Kubernetes pods with the user's GitHub Personal Access Token stored in an environment variable, detailing the isolation boundary, egress controls, and snapshot lifecycle needed to secure it.
Think about
- Which concrete attack paths open immediately when a pod runs privileged Docker-in-Docker with a raw GitHub PAT in
env? - How do you support repositories whose test suites need Postgres or Redis without giving untrusted agent code privileged Docker access on the host?
- What changes in the secret flow and workspace lifecycle so a malicious
Makefileorpackage.jsonpostinstall hook cannot steal tokens or pivot laterally?
Draft your design answer here Save your draft, compare against the model answer, and track progress.