Distillate

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:

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.

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:

RuntimeIsolation ModelOverheadBest For
Docker / containerdNamespace + 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.
FirecrackerKVM 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 lowLocal 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.

TierFilesystemNetworkApprovalExample Actions
T0: ReadRead-only root, no writesNoneNot requiredread_file, grep, list_dir
T1: SandboxedWritable /workspace onlyRegistry mirror onlyNot requiredwrite_file, bun test, npm install
T2: ExternalWritable workspaceFull egressHuman modalgit push, API calls, email
T3: IrreversibleN/AN/AHard vetorm -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:

# 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.

Sandbox Escapes: Understanding the Adversary

Sandboxing is an arms race. Understanding how sandboxes are escaped is essential for building effective defenses:

Escape VectorMechanismMitigation
Kernel ExploitA 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 RaceAgent 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 ExhaustionAgent 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 SocketAgent 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 AttacksAgent 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