AI agentsLesson 7openfrom distillate
Sandboxing: Isolation Boundaries for Agent Execution
How to technically enforce an agent's boundaries with layered filesystem, network, resource and syscall isolation.
In Lesson 2, Section 5, we introduced the Autonomy Ladder—a tiered model for classifying actions by risk (Tier 0: Pure Read through Tier 3: Irreversible). That model answers what an agent is allowed to do. This lesson answers the harder question: how do we technically enforce those boundaries so that even a prompt-injected, hallucinating, or malicious model cannot cause real-world harm?
Sandboxing is the engineering discipline of building execution cages—constrained environments where agent tool calls run with minimized privileges, isolated resources, and no path of escape to the host system.
Why Sandboxing Is Non-Negotiable
Without sandboxing, an agent's tool calls execute with the full privileges of the user who launched the agent. This creates an extreme attack surface:
- Prompt Injection: A malicious web page or document instructs the agent to
rm -rf /or exfiltrate SSH keys. Without a sandbox, the agent happily complies. - Hallucinated Commands: The model generates a plausible-but-destructive command (e.g.,
DROP TABLE users) that was never intended by the user. - Dependency Exploits: A compromised npm package or PyPI dependency executes arbitrary code during
npm installorpip install. - Infinite Resource Consumption: A buggy agent loop spawns thousands of processes, exhausting CPU, memory, or disk.
The Sandboxing Stack: Four Layers of Isolation
Production-grade sandboxing is not a single mechanism—it is a defense-in-depth stack where each layer provides independent containment. If one layer fails, the others still protect the host.
Sandboxing Defense Stack: Agent Process → Layer 1 (Filesystem Isolation) → Layer 2 (Network Restrictions) → Layer 3 (Resource Limits via cgroups) → Layer 4 (Syscall Filtering via seccomp-bpf) → Host OS. Each layer provides independent containment.
- L1 — Filesystem Isolation: The agent sees a read-only root filesystem with a writable
tmpfsworkspace mounted at/workspace. Source code is bind-mounted read-only. Sensitive paths (~/.ssh,/etc/passwd, browser profiles) are either hidden or mounted as empty directories. - L2 — Network Restrictions: Egress is denied by default. The agent can only reach pre-approved endpoints (e.g., a package registry mirror) via an HTTP proxy that enforces DNS allowlisting. Raw sockets,
curlto arbitrary IPs, andncare blocked at the firewall layer. - L3 — Resource Limits: Linux
cgroupscap CPU (e.g., 1 core), memory (e.g., 512 MB), process count (e.g., 64 PIDs), and disk I/O bandwidth. This prevents fork bombs, memory exhaustion, and disk-filling attacks from impacting the host or other tenants. - L4 — Syscall Filtering: A
seccomp-bpfprofile whitelists only the ~50 syscalls needed for a development agent (read, write, open, close, mmap, etc.). Dangerous syscalls likemount,ptrace,kexec_load, andrebootare blocked at the kernel level.
Container Runtimes: From Docker to Micro-VMs
Not all sandboxing runtimes are created equal. The choice of container runtime determines the strength of the isolation boundary:
| Runtime | Isolation Model | Overhead | Best For |
|---|---|---|---|
| Docker / containerd | Namespace + cgroup isolation. Shared host kernel. | Low (~50ms startup) | Trusted workloads, CI pipelines, development sandboxes. |
| gVisor (runsc) | User-space kernel (Sentry) intercepts all syscalls. Guest kernel is a Go reimplementation. | Medium (~150ms startup, 2-5x syscall overhead) | Multi-tenant SaaS, running untrusted code. |
| Firecracker | KVM micro-VM. Dedicated guest kernel per sandbox. Minimal device model. | Low for a VM (~125ms startup) | Serverless (AWS Lambda), high-security isolation. |
| macOS Sandbox (sandbox-exec) | Seatbelt profile-based sandbox. Path-based file and network restrictions. | Very low | Local macOS agent development. |
Mapping the Autonomy Ladder to Sandbox Configurations
The four-tier autonomy model from Lesson 2 maps directly to concrete sandbox configurations. Each tier gets a progressively more restrictive execution environment:
Autonomy Ladder Sandbox Mapping: Tier 0 (Pure Read) gets a read-only filesystem with no network. Tier 1 (Sandboxed Mutation) gets a writable workspace with registry-only egress and cgroup limits. Tier 2 (External Action) gets full network but requires human approval and audit logging. Tier 3 (Irreversible) is hard-blocked by the policy engine with no execution path.
| Tier | Filesystem | Network | Approval | Example Actions |
|---|---|---|---|---|
| T0: Read | Read-only root, no writes | None | Not required | read_file, grep, list_dir |
| T1: Sandboxed | Writable /workspace only | Registry mirror only | Not required | write_file, bun test, npm install |
| T2: External | Writable workspace | Full egress | Human modal | git push, API calls, email |
| T3: Irreversible | N/A | N/A | Hard veto | rm -rf /, DROP TABLE, force-push |
Filesystem Sandboxing: The First Line of Defense
The filesystem is the most common attack vector for agent escapes. A robust filesystem sandbox must address three independent risks:
- Path Traversal: The agent writes to
../../etc/cron.d/backdooror/root/.ssh/authorized_keys. - Symlink Attacks: The agent creates a symlink from
/workspace/config.jsonto/etc/shadowand reads it through an allowed path. - Device Access: The agent opens
/dev/sdadirectly to bypass filesystem permissions.
# Example: Setting up a sandboxed filesystem namespace
# Create an isolated mount namespace
unshare --mount --pid --fork --mount-proc /bin/bash
# Remount root as read-only
mount -o remount,ro /
# Create a writable tmpfs workspace
mount -t tmpfs -o size=1g tmpfs /workspace
# Bind-mount source code read-only
mount --bind /home/user/project /workspace/project
mount -o remount,ro,bind /workspace/project
# Hide sensitive paths
mount -t tmpfs tmpfs /root
mount -t tmpfs tmpfs /home/user/.ssh
Network Sandboxing: Controlling Egress
Network egress is the exfiltration channel. An unsandboxed agent can curl sensitive files to an attacker-controlled server, participate in a DDoS botnet, or scan internal network services.
- 1 — Default Deny: The sandbox starts with no network interfaces. The agent cannot make any outbound connection unless explicitly permitted.
- 2 — Egress Proxy: All outbound HTTP/HTTPS traffic is forced through a local proxy (e.g., a sidecar container running
mitmproxyor a custom allowlist proxy). The proxy enforces URL allowlisting and blocks non-HTTP protocols. - 3 — DNS Allowlisting: The sandbox's DNS resolver only resolves pre-approved domains. Requests to any other domain return
NXDOMAIN, preventing DNS-based exfiltration and tunneling. - 4 — Raw Socket Blocking: The
CAP_NET_RAWcapability is dropped. The agent cannot create raw IP packets, perform ARP spoofing, or useping/traceroutefor network reconnaissance.
Sandbox Escapes: Understanding the Adversary
Sandboxing is an arms race. Understanding how sandboxes are escaped is essential for building effective defenses:
| Escape Vector | Mechanism | Mitigation |
|---|---|---|
| Kernel Exploit | A vulnerability in the host kernel (e.g., Dirty COW, Dirty Pipe) allows code execution outside the namespace. | Use gVisor or Firecracker (dedicated kernel). Patch host kernels aggressively. |
| Symlink Race | Agent creates a symlink during a TOCTOU window between path validation and file access. | Use O_NOFOLLOW flags. Validate paths inside the sandbox, not in the harness. |
| Resource Exhaustion | Agent spawns a fork bomb or fills the disk, causing denial of service to the host or other sandboxes. | Enforce cgroup PID limits, disk quotas, and CPU time limits. |
| Container Breakout via Docker Socket | Agent mounts /var/run/docker.sock and uses the Docker API to spawn a privileged container. | Never mount the Docker socket into a sandbox. Use rootless Docker or a remote builder API. |
| Side-Channel Attacks | Agent infers host state through timing, cache behavior, or shared resources. | Use Firecracker (no shared resources). Disable hyperthreading for high-security workloads. |
Practical Patterns for Agent Sandboxing
Here are the most common architectural patterns for sandboxing agents in production systems:
Agent Sandbox Execution Patterns: The harness routes tool calls to different executors based on risk tier. Tier 0 (Read) uses an in-process read-only executor. Tier 1 (Sandboxed) uses a Docker/gVisor container. Tier 2 (External) requires human approval before executing in a privileged, audited executor. Tier 3 is denied outright.
Quick Check
Why is running an agent inside a standard Docker container insufficient for executing untrusted code from a prompt-injected model?
References & Deeper Reading
- gVisor Documentation — Google's user-space kernel sandbox for containers.
- Firecracker — AWS's KVM micro-VM for serverless and sandboxing.
- seccomp(2) — Linux syscall filtering — Kernel-level syscall allowlisting.
- cgroups v2 — Linux resource control — CPU, memory, PID, and I/O limits.
- OWASP: Path Traversal — Filesystem escape patterns and defenses.
- Anthropic: Building Effective Agents — Autonomy tiers and sandbox architecture for production agents.