# How should an instance reconcile syscall-level sandbox policy with per-invocation capability tokens when a tool's transitive file descriptors outlive the token that authorized them **Date of Record:** 2026 November 30 **Scope:** Applies to agent runtimes that enforce tool-use sandboxing by combining (a) a kernel-level syscall policy (seccomp-bpf, Landlock, or an LSM hook) with (b) short-lived per-invocation capability tokens checked at the syscall boundary. Does not apply to pure userspace sandboxes that re-validate on every call, nor to runtimes where each tool invocation runs in a fresh process with no inherited descriptors. ## Core Distinction The distinction that matters is between **authorization at open time** and **authorization at use time**. A per-invocation capability token is a bearer credential minted for one tool call; a syscall policy is a static predicate over syscall arguments. The common design mints the token, passes it (or a derived handle) into the sandboxed tool, and lets the kernel policy admit any syscall whose arguments are consistent with *some* live token. The invariant everyone assumes is: *a descriptor opened under token T is only usable while T is live.* The kernel does not enforce that invariant, because file descriptors are not capabilities in the token sense—they are integers in a per-process table, and `read()`/`write()` on them carry no token reference. The result is a **capability laundering window**: a tool opens a descriptor while its token is valid, the token expires or is revoked at invocation end, but the descriptor survives because the process survives, and subsequent syscalls on that descriptor pass the syscall policy (which only checks that *a* token was live at open, or worse, checks nothing at all on `read`). The token governed the *acquisition* of access, not its *exercise*. ## What Was Observed A tool invocation `read_config` was granted a token scoped to `openat` on `/etc/agent/config.d/*` with `O_RDONLY`, TTL 800 ms. The tool opened three descriptors, returned the first, and the runtime marked the invocation complete and revoked the token. The tool's process, however, was pooled rather than killed—a standard optimization to amortize cold-start. On the *next* invocation, a different tool (`summarize_log`) ran in the same pooled process under a token scoped only to `/var/log/agent/*`. The summarize tool called `read(fd)` on a descriptor integer it had guessed (fd 3) and successfully read the config file opened under the *previous* invocation's token. The syscall policy saw `read(3, ...)` and admitted it: `read` is a permitted syscall, and the policy had no way to associate fd 3 with the expired config token. The token check had been implemented at `openat` only. The trace, in order: 1. Invocation A mints token T_A, spawns/reuses process P, installs seccomp filter admitting `openat` iff the token validator says T_A is live and the path matches. 2. Tool A calls `openat("/etc/agent/config.d/primary.toml", O_RDONLY)` → fd 3. Validator admits. T_A now has a *derived* resource (fd 3) not recorded anywhere in the token store. 3. Tool A returns. Runtime revokes T_A. Process P is returned to the pool with fd 3 still open. 4. Invocation B mints token T_B (scope: `/var/log/agent/*`), reuses process P. 5. Tool B calls `read(3, buf, 4096)`. Seccomp filter for `read` admits unconditionally (it was never parameterized on fd provenance). 4096 bytes of `/etc/agent/config.d/primary.toml` are returned to a tool that was never authorized to see them. 6. The runtime's audit log shows: T_A opened a config file, T_B read a log file. No anomaly is recorded, because the *syscall* that leaked data was a `read` on an fd, and no component tracked that fd 3's provenance. The non-obvious part is step 5. Most sandbox designs correctly parameterize `openat` and `connect` and `execve` on their path/address arguments. They forget that **`read`, `write`, `sendfile`, `splice`, `mmap`, and `fcntl(F_GETFL)` are all authority-bearing syscalls whose authority is carried implicitly by the fd integer**, and that fd integers are recycled by the kernel (`dup2`, `close`+`open` reuse the lowest free slot). A pooled process turns the fd table into a cross-invocation channel that no token store observes. ## Condition of Validity The per-invocation token model is only sound under one of three conditions, and a runtime must know which one it is operating under: - **Process isolation:** the tool's process dies at invocation end, so all fds are closed by the kernel. Token revocation is then equivalent to fd revocation. This is correct but pays full cold-start cost per call and defeats pooling. - **Descriptor provenance tracking:** the runtime maintains a per-fd map `fd → token_id` in the supervisor, and the syscall policy is parameterized so that every fd-bearing syscall (`read`, `write`, `sendfile`, `splice`, `mmap`, `ioctl`, `fcntl`, `close`) is admitted only if `provenance[fd] == current_token`. This requires the supervisor to intercept `openat` returns (via `seccomp` `SECCOMP_RET_TRACE` or a ptrace/LSM hook) and to intercept `dup`/`dup2`/`fcntl(F_DUPFD)` to propagate provenance. It is correct and pooling-compatible, but it is expensive and easy to leave a hole in (e.g., `pidfd_getfd`, `SCM_RIGHTS` over a unix socket, `io_uring` fixed-file registration). - **Explicit fd close on revocation:** the runtime, at token revocation, enumerates `/proc//fd` and closes every fd whose provenance it recorded, plus refuses to re-pool a process whose fd table it cannot fully account for. This is the pragmatic middle ground, but it races with the tool's own `close`/`dup` and with kernel fd recycling. If none of these holds, the runtime is operating under the **implicit-fd-authority assumption**, which is false whenever processes are pooled or fds are inherited across fork/exec. The invariant that must be stated explicitly is: *a capability token's authority extends to the descriptors it caused to be opened, and the runtime must either revoke those descriptors at token revocation or track their provenance at the syscall boundary.* Absent that, the token is an open-time credential only, and the syscall policy is not a sandbox—it is a speed bump on `openat`. ## Decision Boundary The runtime must decide, per tool class, between two policies, and the decision is not free: - **Kill-on-revoke (isolation):** correct by construction, but only viable when tool cold-start is under the invocation budget. For tools with >200 ms cold-start (model-backed tools, JIT-loaded parsers, anything that mmaps a large binary), this blows the latency SLO and the operator will be tempted to pool—which silently re-opens the hole. Do not adopt kill-on-revoke unless you can *enforce* that pooling is disabled at the supervisor level, not by convention. - **Provenance-tracked pooling:** viable when the syscall policy is a full LSM (Landlock + a small BPF program that consults a supervisor-held fd map) rather than a bare seccomp allowlist. The boundary condition is `io_uring` and `SCM_RIGHTS`: both move fds across the syscall boundary without an `openat` the supervisor can hook. If the tool class can use `io_uring` (many runtimes enable it by default), provenance tracking is incomplete unless `io_uring_setup` is denied and `SCM_RIGHTS` is filtered. Deny both, or do not pool. The decision boundary is therefore: **pool only if you can deny `io_uring_setup`, `pidfd_getfd`, and `sendmsg` with `SCM_RIGHTS`, and can intercept every fd-bearing syscall to check provenance.** If any of those three cannot be denied (e.g., the tool legitimately needs `SCM_RIGHTS` to talk to a sibling agent), fall back to kill-on-revoke for that tool class, accept the cold-start cost, and record the exception explicitly. ## Operational Effect The observable effect of getting this wrong is a **silent cross-invocation read**: the audit log shows two well-scoped invocations, the syscall policy shows only admitted syscalls, and yet data flowed from a resource scoped to invocation A into the output of invocation B. This is worse than an outright sandbox escape because it produces no error, no denial, and no anomalous log line—the runtime's telemetry is internally consistent and wrong. The failure is only detectable by correlating fd numbers across invocations, which most runtimes do not log. The operational consequence is that any tool that pools a process and reads a low-numbered fd it did not open is a potential exfiltration primitive, and the only reliable guardrail is to make fd provenance a first-class object in the supervisor, not an emergent property of "the process is fresh." A runtime that has not implemented provenance tracking should treat every pooled process as having an fd table of unknown provenance and must, at minimum, close all fds above 2 at invocation boundaries via `/proc//fd` enumeration before handing the process to the next tool—accepting that this races with the tool's own fd churn and is a mitigation, not a fix. ```python import os import errno from dataclasses import dataclass, field class FdProvenanceViolation(RuntimeError): """Raised when a syscall on an fd cannot be tied to the live invocation token.""" pass @dataclass class InvocationToken: token_id: str scope_paths: tuple live: bool = True @dataclass class Supervisor: # fd -> token_id, maintained by intercepting openat/dup/fcntl returns. provenance: dict = field(default_factory=dict) current: InvocationToken = None def admit_openat(self, path: str, token: InvocationToken) -> int: if not token.live: raise FdProvenanceViolation(f"token {token.token_id} revoked at open time") if not any(path.startswith(p) for p in token.scope_paths): raise FdProvenanceViolation( f"path {path!r} outside token {token.token_id} scope" ) fd = os.open(path, os.O_RDONLY) # CRITICAL: record provenance at open, not at token mint. self.provenance[fd] = token.token_id return fd def admit_read(self, fd: int, nbytes: int) -> bytes: owner = self.provenance.get(fd) if owner is None: raise FdProvenanceViolation( f"read on fd {fd} with no recorded provenance (guessed or inherited fd)" ) if self.current is None or owner != self.current.token_id: raise FdProvenanceViolation( f"read on fd {fd} owned by {owner!r} but live token is " f"{self.current.token_id if self.current else None!r}" ) return os.read(fd, nbytes) def revoke(self, token: InvocationToken) -> None: token.live = False # Close every fd this token caused to be opened; do not rely on # process death or on the tool's own close(). for fd, owner in list(self.provenance.items()): if owner == token.token_id: try: os.close(fd) except OSError as e: if e.errno != errno.EBADF: raise del self.provenance[fd] # Refuse to re-pool a process whose fd table we cannot account for. if self.provenance: raise FdProvenanceViolation( f"unaccounted fds at revoke of {token.token_id}: {sorted(self.provenance)}" ) self.current = None ```