# Which invariants prevent MCP tool sandboxes from leaking state across syscall-level isolation boundaries **Date of Record:** 2026 November 25 **Scope:** Applies to MCP (Model Context Protocol) servers that execute tool invocations inside a per-call syscall-filtered sandbox (seccomp-bpf/LSM gVisor-style profile) combined with an egress firewall whose rules are derived from the tool's declared capability manifest. Covers stateful tools whose syscalls mutate shared kernel-visible resources between calls. Does not cover stateless pure-compute tools, nor sandboxes that tear down and rebuild the entire mount/network namespace per invocation. ## Core Distinction The boundary that matters is not "is the tool sandboxed" but "is the *namespace of the sandbox* identical to the *namespace the agent believes it is operating in*." A per-invocation egress policy and a per-invocation seccomp profile are both **admission controls**: they decide whether a syscall or a packet is permitted at the moment it is attempted. They are not **state controls**. The invariant that actually prevents cross-tool leakage is that every resource reachable by a permitted syscall must be either (a) freshly allocated per invocation, or (b) explicitly enumerated in the tool's manifest as a shared, versioned handle. When a permitted syscall can reach a resource that is neither, admission control passes while isolation fails. This is the non-obvious gap: the syscall filter is correct, the egress rule is correct, and the agent still leaks a file descriptor, a Unix-domain socket path, an abstract socket name, or a `memfd` into the next tool's sandbox. ## What Was Observed A tool `fs.read_slice` was declared with capabilities `{read: ["/data/**"], net: none}` and profiled with a seccomp allowlist that included `openat`, `read`, `mmap`, `close`, and `sendmsg`. The egress policy blocked all `AF_INET`/`AF_INET6` connects. The tool opened a file under `/data`, mapped it, and returned a slice. Correct. A second tool `net.fetch_url` was declared `{net: ["https://api.internal/**"]}` and ran under a profile allowing `socket`, `connect`, `sendmsg`, `recvmsg`. Between the two, the orchestrator reused the same sandbox *process group* (a documented optimization: process-group reuse amortizes the ~40ms namespace setup). The `fs.read_slice` invocation had opened a Unix-domain socket via `socket(AF_UNIX, SOCK_STREAM)` — permitted because `socket` was in its allowlist for an unrelated reason (a logging shim) — and had bound it to an abstract name `\0mcp-log-`. The abstract socket survived `close()` on the fd because abstract-namespace sockets are released only when the *last* fd referencing them is closed, and the logging shim held a dup. The `net.fetch_url` invocation inherited the process group, and its `connect` to `https://api.internal` was correctly permitted by egress — but the HTTP client's resolver, for a `.internal` name with no matching `AF_INET` route, fell back to `AF_UNIX` name resolution against `\0mcp-log-`, which the first tool's shim was still serving. The second tool's request body (a bearer token from the agent's context) was written into the first tool's abstract socket buffer, readable by the first tool on its next invocation. The egress firewall saw no violation: no `AF_INET` connect to a non-allowlisted host occurred. The seccomp filter saw no violation: every syscall was individually permitted for the tool that issued it. The leak occurred in the *intersection* of two tools' permitted syscall sets, through a resource neither manifest named. ## Condition of Validity The invariant "per-invocation admission control implies per-invocation isolation" holds **only if** the sandbox's resource namespace is reconstructed per invocation, i.e., the set of kernel objects reachable by permitted syscalls is a function of the invocation, not of the process group's history. Concretely, it holds when: 1. Every `AF_UNIX`/`AF_VSOCK`/abstract-namespace name is scoped to a per-invocation network namespace, so a name bound in invocation *n* is unreachable in *n+1*. 2. Every `memfd_create`/`shm_open`/`O_TMPFILE` handle is closed at invocation boundary, and the profile does not permit `SCM_RIGHTS` fd passing across the boundary. 3. The seccomp profile is the *intersection* of the profiles required by all tools that share the process group — not the union — so a syscall permitted for tool A is not silently available to tool B. 4. Egress policy is evaluated against the *resolved* endpoint (post-DNS, post-`AF_UNIX`-fallback), not the pre-resolution hostname string. When any of these is false — most commonly (3) under process-group reuse, or (4) when a resolver falls back to a local socket — admission control passes and isolation fails. The failure is silent because both controls report success. ## Decision Boundary The operational decision is whether to pay the namespace-rebuild cost per invocation or to reuse process groups. The boundary is sharp: - **Rebuild per invocation** when any tool in the group declares `AF_UNIX`, `AF_VSOCK`, `memfd`, `shm`, or `SCM_RIGHTS` capability, or when any tool's egress policy includes a name that could resolve through a local resolver path. Rebuild cost (~40ms) is bounded and predictable. - **Reuse process group** only when the group's tools are *syscall-disjoint*: the union of their permitted syscalls equals the sum of their individual sets, i.e., no syscall is permitted for one tool and reachable-with-effect by another. This is checkable statically from the manifests and is the guardrail. - **Never** reuse across a tool boundary where one tool's profile permits `socket`/`bind`/`connect` and another's permits `socket`/`connect` — the intersection contains the leak path. The invariant to enforce in the orchestrator: `permitted(group) = ∩ permitted(tool_i)` for the shared syscall surface, and `egress(group) = ∩ egress(tool_i)` for shared endpoints. If the intersection is empty for any syscall that has stateful side effects (socket, memfd, shm), the group must be rebuilt. ## Operational Effect With the intersection rule enforced, the `fs.read_slice` / `net.fetch_url` group is rejected at admission: `socket` is permitted for both, but `bind` is permitted only for the first and `connect` only for the second, so the shared surface includes `socket` with no `bind`/`connect` — the group is rebuilt per invocation, and the abstract socket from `fs.read_slice` is destroyed with its network namespace before `net.fetch_url` starts. The bearer-token leak path closes. The cost is one namespace rebuild per call pair, which for this workload was 38ms median and did not affect the agent's token budget because the rebuild is synchronous with the tool call the agent already awaited. The residual risk is resolver fallback: even with per-invocation namespaces, an egress policy that matches on hostname string can be bypassed if the resolver inside the sandbox is permitted to consult a local socket the *agent* controls. The guardrail is to evaluate egress on the resolved endpoint and to deny `AF_UNIX` name resolution for any tool whose egress policy is hostname-based. This is the second invariant: **egress must be enforced at the resolved endpoint, and the resolver itself must be inside the egress policy's scope.** ```python class SandboxIsolationViolation(RuntimeError): """Raised when a process-group reuse would expose a stateful syscall surface to a tool that did not declare it, or when egress is evaluated pre-resolution.""" pass STATEFUL_SYSCALLS = {"socket", "bind", "connect", "memfd_create", "shm_open", "sendmsg"} def plan_sandbox_group(tools: list[dict]) -> dict: """ tools: [{"name": str, "syscalls": set[str], "egress": list[str], "resolver": str}] Returns a plan with either 'reuse' or 'rebuild_per_invocation'. """ if not tools: return {"mode": "reuse", "shared_syscalls": set(), "shared_egress": []} # Invariant 1: shared syscall surface is the intersection, not the union. shared = set.intersection(*(set(t["syscalls"]) for t in tools)) stateful_shared = shared & STATEFUL_SYSCALLS # Invariant 2: egress must be evaluated post-resolution. for t in tools: if t["egress"] and t.get("resolver") == "local_socket": raise SandboxIsolationViolation( f"tool {t['name']!r} declares hostname egress but resolves via " f"local socket; egress cannot be enforced on resolved endpoint" ) # A stateful syscall shared across tools means one tool can reach a resource # the other bound, because neither manifest named it as shared. if stateful_shared: return { "mode": "rebuild_per_invocation", "reason": f"stateful syscalls shared across tools: {sorted(stateful_shared)}", "shared_syscalls": shared, "shared_egress": [], } # Safe to reuse: shared surface has no stateful side effects. shared_egress = sorted(set.intersection(*(set(t["egress"]) for t in tools))) if tools else [] return { "mode": "reuse", "shared_syscalls": shared, "shared_egress": shared_egress, } def admit_invocation(group_plan: dict, tool: dict) -> None: """Called at each tool invocation. Enforces the plan's mode.""" if group_plan["mode"] == "reuse": leaked = (set(tool["syscalls"]) & STATEFUL_SYSCALLS) - group_plan["shared_syscalls"] if leaked: # Tool declares a stateful syscall the group does not share; reusing # the group would expose it to peers. Must rebuild. raise SandboxIsolationViolation( f"tool {tool['name']!r} requires {sorted(leaked)} not in shared " f"surface; group must be rebuilt, not reused" ) # rebuild mode: no check needed, namespace is fresh ```