Sub topic 15
What Sandboxing Does Not Protect Against
This chapter assumes you have read the earlier chapters on container hardening, filesystem scoping, network egress, and choosing an isolation level. You should be comfortable reading docker inspect output and thinking about threats as paths from an attacker to an asset.
A sandbox limits what a process can reach on the host. It does not decide whether the process behaves well, and it does not protect anything you deliberately placed inside the boundary. This chapter lists the risks that remain after you have applied every control so far, and what to do about each.
What the Boundary Covers
A hardened sandbox answers one question: if the agent runs hostile commands, what can they touch? With the earlier controls in place, the answer is the mounted workspace, whatever the network policy allows, and any secrets you injected. Everything else on the host is out of reach, as long as the isolation layer holds.
That answer leaves out four things. The agent can misuse what it was given. Output that leaves the sandbox can cause harm later. The isolation layer itself can have bugs. Your own configuration can be wrong.
Damage Inside the Workspace
The workspace mount is writable, so the agent can delete files, corrupt them, or plant code. Planted code is the dangerous case, because it runs later on the host, outside the sandbox. Common places for it:
- Git hooks in
.git/hooks, which run on your nextgit commit. package.jsonscripts such aspostinstall, andMakefiletargets.- Editor task files such as
.vscode/tasks.json. - CI configuration, which runs with your pipeline's credentials.
- Shell startup files, if you ever mounted a home directory.
The sandbox cannot stop this, because writing files into the project is the agent's job. The defenses are procedural:
- Keep the project in version control and let the agent work on a branch or a copy.
- Review the full diff, including hidden directories, before running anything on the host.
- Mount
.gitread-only if the agent does not need to commit, which removes the hook path. - Build, test, and run the agent's output inside a sandbox as well.
# List changed files outside ordinary source paths before you trust the result
git status --porcelain --untracked-files=all | grep -E '\.git/|\.github/|\.vscode/|package\.json|Makefile' || echo "no sensitive paths changed"Note that git status does not list changes inside .git/hooks, so hooks need a direct check, for example ls -la .git/hooks.
Data Leaving Through Allowed Channels
Egress control reduces exfiltration, but any allowed channel is a possible exit. If the agent can reach a package registry, it can encode data in a package name or request path. If it can reach a model API, the files it reads are sent to that API by design. DNS is a classic gap. A container with --network none has no DNS, but a container on a restricted network that still resolves arbitrary names can leak data through query names.
An allowlist of domains shrinks the problem without removing it. Anything the agent can read, it can send to any destination you permitted. Keep sensitive files out of the mount, and treat each allowed destination as a place the workspace contents might end up.
Prompt Injection and Manipulated Intent
A sandbox constrains capability, not intent. If a README, issue comment, dependency, or web page contains instructions, the agent may follow them, and every action will still be inside the sandbox. The injected instructions might tell the agent to add a backdoor to the code, include a malicious dependency, or send the workspace to an allowed endpoint.
You cannot fix this inside the container. Combine these measures:
- Give the agent only the capabilities the task needs, so injected instructions have little to use.
- Require human approval for network changes, dependency additions, and changes to build or CI files.
- Treat all agent output as untrusted code from a stranger and review it that way.
Secrets Placed Inside the Boundary
Anything you inject is exposed to everything the agent runs. That includes environment variables, mounted credential files, and tokens in a .env file inside the workspace. A --read-only root filesystem does not hide them, and neither do dropped capabilities. The agent, and any injected instruction, can read them and use them through whatever egress exists.
Use short-lived, narrowly scoped tokens, for example a read-only token limited to one repository. Prefer a proxy that adds credentials to outgoing requests outside the sandbox, so the secret never enters it. Revoke tokens after the session. A sandbox does not shrink the damage from a leaked token. A narrowly scoped token does.
Isolation Escapes
Containers share the host kernel. A kernel vulnerability, or a flaw in the container runtime, can let code in the container reach the host. Real examples include the runc file-descriptor leak CVE-2019-5736 and the runc working-directory flaw CVE-2024-21626. Hardening flags reduce the attack surface, for instance by removing capabilities and applying the default seccomp profile, but they do not make the shared kernel safe.
Stronger layers shift the risk instead of removing it:
| Layer | What the agent's code attacks | Typical residual risk |
|---|---|---|
| Hardened container | Host kernel through system calls | Kernel and runtime bugs |
| gVisor (`runsc`) | gVisor's user-space kernel, then a small set of host calls | Bugs in gVisor itself, plus lower compatibility and speed |
| MicroVM or VM | Hypervisor and virtual devices | Hypervisor and device-emulation bugs |
Keep the host kernel, container runtime, and hypervisor patched. Choose a stronger layer when the code you run is more hostile, as discussed in the previous chapter.
Misconfiguration and Drift
Most real sandbox failures come from configuration, not from exploits. These settings undo the isolation:
- Mounting
/var/run/docker.sockgives the agent control of the Docker daemon, which is effectively root on the host. --privilegedrestores nearly all capabilities and device access.--network hostremoves network isolation.- Mounting
$HOMEor/exposes SSH keys and cloud credentials. - Adding capabilities back with
--cap-addfor a one-off fix, then never removing it.
The following script inspects running containers and flags these problems. It needs Python 3.9 or later and access to the Docker CLI.
import json
import subprocess
import sys
def inspect(name: str) -> dict:
result = subprocess.run(
["docker", "inspect", name],
capture_output=True, text=True, check=True,
)
return json.loads(result.stdout)[0]
def audit(info: dict) -> list[str]:
host = info["HostConfig"]
problems = []
if host.get("Privileged"):
problems.append("runs with --privileged")
if host.get("NetworkMode") == "host":
problems.append("uses the host network")
if host.get("CapAdd"):
problems.append(f"adds capabilities: {host['CapAdd']}")
if not host.get("ReadonlyRootfs"):
problems.append("root filesystem is writable")
if not any("no-new-privileges" in o for o in host.get("SecurityOpt") or []):
problems.append("no-new-privileges is not set")
if not host.get("Memory"):
problems.append("no memory limit")
pids = host.get("PidsLimit")
if not pids or pids < 0:
problems.append("no process limit")
if (info["Config"].get("User") or "0").split(":")[0] in ("0", "root"):
problems.append("runs as root")
for mount in info.get("Mounts", []):
if "docker.sock" in mount.get("Source", ""):
problems.append("mounts the Docker socket")
return problems
if __name__ == "__main__":
for container in sys.argv[1:]:
issues = audit(inspect(container))
print(f"{container}: {'OK' if not issues else ''}")
for issue in issues:
print(f" - {issue}")Run it as python audit_sandbox.py my-agent-container. A check like this catches drift, but it only tests settings you thought to list.
Limits That Are Not Security Boundaries
CPU, memory, and process limits protect the host from exhaustion. They do not limit:
- Disk use inside the writable workspace, unless you set a quota.
- Spending on a paid model API or cloud resources the agent can call.
- Wall-clock time, unless you add a timeout, as the wrapper in the earlier chapter does.
- Noisy-neighbor effects such as cache contention or timing side channels, which sit outside the scope of ordinary container limits.
Human Factors
Approval prompts only work if people read them. Frequent prompts lead to approval fatigue, where a reviewer approves everything. Large diffs get skimmed. Reduce the problem by making safe actions automatic, keeping risky actions rare enough to be read, and showing the reviewer exactly what will run. Keep an audit log of commands and file changes so you can reconstruct events afterwards. A log records an incident but does not prevent it.
Summary of Residual Risks
| Risk | Does the sandbox help? | What reduces it |
|---|---|---|
| Malicious code planted in the workspace | No | Diff review, read-only `.git`, run results in a sandbox |
| Exfiltration over allowed channels | Partly | Domain allowlists, smaller mounts |
| Prompt injection | No | Least capability, approvals |
| Leaked secrets | No | Scoped, short-lived tokens, credential proxy |
| Kernel or hypervisor escape | Depends on the layer | Patching, gVisor or microVM |
| Misconfiguration | No | Audits, infrastructure as code |
| API cost and disk growth | No | Quotas, budgets, timeouts |
| Approval fatigue | No | Fewer, clearer prompts |
Exercise
An agent runs in a container with --cap-drop=ALL, --read-only, and --network none, with the project mounted writable. After the session you run git commit on the host and a script you never wrote executes. Which risk is this, why did the sandbox not stop it, and what two steps would have prevented it?
Show answer
The agent wrote a Git hook (for example .git/hooks/pre-commit) into the writable workspace. The sandbox only constrained what the agent could reach, and writing into the mounted project is allowed. The hook ran later on the host, outside the boundary. Two preventions: review the full diff and the contents of hidden directories such as .git/hooks before using the project on the host, and mount .git read-only (or give the agent a copy of the repository) so it cannot create hooks.
Exercise
Your audit script reports that a container mounts /var/run/docker.sock. Explain why this defeats the other hardening options and name one safer alternative for letting the agent build images.
Show answer
Access to the Docker socket lets the agent ask the host's Docker daemon to start any container, including a privileged one with the host filesystem mounted. This gives root-equivalent control of the host, so flags like --cap-drop=ALL on the agent's own container no longer matter. A safer alternative is a rootless or daemonless builder (such as rootless BuildKit) running inside a separate sandbox, or a nested VM or microVM dedicated to builds.