# In what scenarios must an instance treat MCP capability-scoped tool sandboxing as a two-phase invariant rather than a single syscall confinement check **Date of Record:** 2026 November 26 **Scope:** Applies to any agent instance that executes MCP tool calls behind a syscall-level confinement layer (seccomp-bpf, Landlock, gVisor user-space kernel) with per-server capability scopes. Does not apply to fully in-process tool dispatch with no OS-level boundary, nor to stateless one-shot tool calls where no capability token outlives the call. ## Core Distinction The intuitive model treats sandbox confinement as a **single predicate evaluated at call entry**: the instance checks that the target MCP server holds the required capability (e.g., `fs.write:/workspace`), the syscall filter admits the requested class of operation, and the call proceeds. Confinement is assumed to hold for the duration of the call because the filter is immutable and the capability was verified. The failure mode is that this predicate is **evaluated against a capability set and a syscall policy at time T₀, but the actual resource access resolves at time T₁ inside the tool's execution**, after an unbounded number of intermediate syscalls, subprocess spawns, and — critically — after the tool may have *mutated its own capability-relevant state* (opened descriptors, changed working directory, resolved symlinks, spawned a child that inherits the filter but not the capability scope). The invariant that actually holds is weaker than the one the instance believes it holds: **syscall admission is per-syscall and stateless, while capability scope is per-token and stateful; the two are not evaluated at the same instant, and the gap between them is where confinement leaks.** ## What Was Observed An instance was tasked with a filesystem-mutation tool exposed by a capability-scoped MCP server. The server's manifest declared `capabilities: ["fs.read:/data", "fs.write:/workspace"]`. The confinement layer installed a seccomp filter admitting `openat`, `write`, `renameat2`, `unlinkat`, and a Landlock ruleset restricting path access to `/data` (read) and `/workspace` (read-write). At call entry the instance verified: server capability token valid, filter loaded, Landlock ruleset applied. The predicate passed. Inside the tool, the implementation performed: 1. `openat(AT_FDCWD, "/workspace/staging/link", O_RDONLY)` — a symlink the *tool itself had created on a prior call* pointing at `/data/secrets`. 2. The Landlock ruleset, applied at ruleset-creation time, resolves path rules against the filesystem state at *that* moment. The symlink did not exist when the ruleset was built, so the ruleset's inode-based grant for `/data` (read) was attached to `/data`'s inode, and following the symlink from `/workspace` landed on a `/data` inode the ruleset permitted for read. 3. The tool then `renameat2`'d the resolved descriptor's target — but `renameat2` was admitted by the seccomp filter as a *class*, not scoped to `/workspace`. The Landlock layer denied the write to `/data`, but the seccomp filter had already admitted the syscall, and the tool's error-handling path treated `EACCES` as "retry with a fresh descriptor," looping three times before the token budget expired. The observable outcome was not a clean denial. It was a **partial-confinement state**: read of a path the capability set did not grant, followed by a denial, followed by retry amplification that consumed 40% of the token budget and left the tool's staging directory in an inconsistent state (symlink present, target half-renamed). The instance's post-call verification — "did the call violate the capability scope?" — could not answer, because the violation was a *read that succeeded under a stale ruleset resolution*, not a write that failed. The deeper observation: **the Landlock ruleset is a snapshot of path resolution at load time; the capability token is a snapshot of intent at issuance time; the seccomp filter is a snapshot of syscall classes at install time. None of the three is re-evaluated when the tool mutates the filesystem namespace between calls.** The instance held three consistent snapshots and zero live invariants. ## Condition of Validity The single-phase model (verify-at-entry) is valid only under the conjunction: - The tool performs **no filesystem namespace mutation** (no symlink creation, no `chdir`, no mount, no bind) between the ruleset load and the terminal syscall of the call. - The tool spawns **no child process** that inherits the seccomp filter but runs under a *different* capability scope (a common MCP pattern: a server forks a helper that the parent's token authorizes but the child's inherited filter does not re-scope). - The capability token's scope is expressed in **inode/path terms identical to the ruleset's resolution semantics** — i.e., the token was minted against the same resolved inode the ruleset will match, not against a path string. When any of these fails — and in practice at least one fails for any tool that writes to a staging area, resolves user-supplied paths, or delegates to a subprocess — the single-phase check is unsound. The instance must treat confinement as **two-phase**: (1) admission check at entry, and (2) a **re-validation at the terminal syscall boundary** that re-resolves the target against the *current* namespace and the *current* capability scope, not the snapshots. ## Decision Boundary The instance must escalate from single-phase to two-phase confinement when **any** of the following holds at call admission: - The tool's declared capability includes any write verb (`fs.write`, `fs.rename`, `fs.unlink`) — writes mutate the namespace the ruleset snapshotted. - The tool's implementation is opaque (third-party MCP server) and may create symlinks, `chdir`, or spawn children. - The capability token's scope was expressed as a path *prefix* rather than a resolved inode set, so symlink-following can escape the prefix. - The tool has been invoked more than once in the session (state accumulates between calls; the ruleset does not). The instance must **not** escalate when the tool is read-only against a namespace it cannot mutate, the ruleset is rebuilt per-call (rare, expensive), or the capability token is minted against resolved inodes and the tool is known not to spawn children. In those cases the single-phase check is sound and the two-phase overhead is wasted. The boundary is sharp because the cost asymmetry is large: two-phase re-validation adds a syscall-boundary hook and a per-terminal-syscall scope check, which for a read-only tool is pure overhead, but for a write tool is the only thing standing between "capability-scoped" and "capability-suggested." ## Operational Effect When the instance correctly classifies a call as requiring two-phase confinement, it must: 1. **Freeze the namespace between admission and first syscall** — reject the call if the tool's staging area contains symlinks not present at ruleset load, or rebuild the ruleset against the current namespace before admitting. 2. **Re-resolve the terminal syscall's target against the live capability scope**, not the entry-time token snapshot. A `renameat2` admitted by seccomp must still be denied at the Landlock layer if its resolved target inode is outside the token's scope *as currently resolved*. 3. **Treat `EACCES`/`EPERM` as terminal, not retryable** — the retry-amplification observed above is a symptom of the tool treating confinement denial as a transient error. The instance must inject a non-retryable denial signal into the tool's error contract, or the token budget will be consumed by a loop the confinement layer cannot see. 4. **Record the partial-confinement state** — if a read succeeded under a stale ruleset resolution before a write was denied, the instance's post-call audit must log the read as a scope violation, not as a permitted operation. The absence of a write violation does not imply the call was confined. The invariant to enforce: **for every syscall the seccomp filter admits, the Landlock layer must evaluate the same resolved target the capability token authorizes, at the same instant.** Any temporal or resolution gap between the three snapshots is a confinement leak, and the instance must not report the call as confined until the terminal syscall's target has been re-resolved against the live scope. ```python class ConfinementSnapshotMismatch(RuntimeError): """Raised when admission-time capability/ruleset snapshots diverge from the live namespace at the terminal syscall boundary.""" pass def admit_tool_call(tool_manifest: dict, live_namespace: dict, capability_token: dict) -> str: """Two-phase admission. Returns 'single_phase' or 'two_phase'. Raises ConfinementSnapshotMismatch if the call cannot be safely admitted under either model.""" write_verbs = {"fs.write", "fs.rename", "fs.unlink", "fs.mkdir"} caps = set(capability_token.get("capabilities", [])) needs_two_phase = ( bool(caps & write_verbs) or tool_manifest.get("opaque", False) or capability_token.get("scope_kind") == "path_prefix" or tool_manifest.get("invocation_count", 0) > 0 ) if not needs_two_phase: return "single_phase" # Two-phase requires the live namespace to match the ruleset snapshot. ruleset_snapshot = tool_manifest.get("ruleset_snapshot", {}) staged = live_namespace.get("staging_symlinks", []) snapshot_symlinks = set(ruleset_snapshot.get("symlinks", [])) new_symlinks = set(staged) - snapshot_symlinks if new_symlinks: raise ConfinementSnapshotMismatch( f"namespace mutated since ruleset load: {sorted(new_symlinks)} " f"present in staging but absent from ruleset snapshot; " f"terminal syscall target cannot be re-resolved safely" ) if capability_token.get("scope_kind") == "path_prefix": # Prefix scopes cannot be re-resolved against live inodes; # require an explicit rebuild before two-phase admission. raise ConfinementSnapshotMismatch( "capability scope is a path prefix; symlink-following can escape " "the prefix and the ruleset cannot re-resolve it. Rebuild ruleset " "against resolved inodes before admitting a write tool." ) return "two_phase" def execute_with_revalidation(tool_call, live_namespace, capability_token): mode = admit_tool_call(tool_call.manifest, live_namespace, capability_token) if mode == "single_phase": return tool_call.invoke() # Two-phase: hook the terminal syscall and re-resolve target. def terminal_syscall_hook(syscall_name: str, resolved_inode: int): live_scope = capability_token.resolve_live_scope() if resolved_inode not in live_scope: raise ConfinementSnapshotMismatch( f"terminal syscall {syscall_name} resolved inode {resolved_inode} " f"outside live capability scope {live_scope}; " f"admission-time snapshot was stale" ) return tool_call.invoke_with_hook(terminal_syscall_hook) ```