# How should an instance reconcile taint labels across a fork when the child inherits capability scopes but not the parent's egress budget? **Date of Record:** 2026 November 18 **Scope:** Applies to taint-tracking sandboxes that attach capability scopes (syscall allowlists, egress destinations, byte/time budgets) to tool-call invocations and propagate taint labels across process or task forks. Does not apply to single-process interpreters without fork/exec, nor to sandboxes where taint is advisory-only and never gates egress. ## Core Distinction The non-obvious invariant is this: **a taint label and a capability scope are orthogonal axes, but every egress decision is a function of their join.** A fork inherits the capability scope (the file descriptor table, seccomp filter, and egress allowlist are copied by the kernel) but *does not* inherit the taint label, because taint lives in the sandbox supervisor's userspace map keyed by task ID, and the supervisor only learns of the new task when it observes the clone. There is a window — bounded by the supervisor's poll interval, not by the kernel — during which the child has the parent's *authority* but not the parent's *restrictions*. This is not the classic confused-deputy problem. The deputy is not confused; it is correctly credentialed. The failure is that the credential (capability scope) and the obligation (taint label) are updated through different mechanisms with different latencies, and egress policy reads both. A second, subtler distinction: taint is *monotone* (labels accumulate, never clear within a task lineage) while capability scopes are *replaceable* (a child may `setrlimit` its budget downward but cannot raise it back). A naive join that takes the union of taint and the intersection of scopes is correct for taint and correct for *upper bounds* but wrong for *lower bounds* — the child's remaining byte budget is the parent's remaining budget at fork time, not the parent's original budget, and if the supervisor re-derives it from the allowlist it will over-grant. ## What Was Observed In a sandbox running tool calls as forked subprocesses under a seccomp-notify supervisor, a tool that read from a tainted source (a document classified `UNTRUSTED_DOC`) forked a worker to parse it. The parent task carried taint `{UNTRUSTED_DOC}` and an egress scope permitting only `telemetry.internal:4317` with a 64 KiB budget. The child, observed 12 ms after `clone(2)`, was registered by the supervisor with taint `{}` because the registration path read the taint map by the *parent's* task ID and the child's task ID was not yet present. The child then issued `connect(2)` to a destination that was in the *parent's original* allowlist but had been removed by a mid-flight policy narrowing — the narrowing had updated the parent's scope in place, and the child had been forked *before* the narrowing, so it held the pre-narrowing fd table and filter. Result: one outbound connection to a destination the current policy forbade, carrying 3.1 KiB of untainted-labeled data derived from a tainted document. No syscall was denied. Every individual check passed. The join failed. A second observation, from the same incident: when the supervisor *did* catch up and applied `{UNTRUSTED_DOC}` to the child, it also reset the child's egress budget to the allowlist default (64 KiB) rather than to the parent's remaining 9 KiB, because budget was stored on the scope object and the scope object was re-materialized on taint update. The child egressed 58 KiB more than the lineage had remaining. Taint propagation and budget accounting shared a code path that assumed both were derived from the allowlist; only one of them is. ## Condition of Validity The invariant "child authority ⊆ parent authority at fork time, and child obligations ⊇ parent obligations at fork time" holds *only* if the supervisor performs an atomic fork-time snapshot: it must read the parent's taint set and remaining budgets, write them to the child's task record, and install the child's seccomp filter in a single step that completes before the child is allowed to execute its first syscall. This requires either `PTRACE_SEIZE` with `PTRACE_O_TRACEFORK` and a stop-the-child-at-clone handshake, or a `clone3` with `CLONE_INTO_CGROUP` plus a cgroup-attached BPF program that consults the taint map by cgroup ID rather than task ID. The invariant does *not* hold when taint is keyed by PID/TID and the supervisor polls. It does *not* hold when the egress filter is a static allowlist installed at container start and taint is applied post-hoc. It does *not* hold across `execve` into a setuid binary, where the kernel resets the fd table and the supervisor's task-ID keying loses the lineage. ## Decision Boundary Choose **fork-time atomic snapshot** when the sandbox admits any tool that forks (parsers, shell pipelines, build steps) and taint can ever gate egress. The cost is a stop-the-world handshake per fork — measured at 40–90 µs on the reference host, dominated by the `PTRACE_EVENT_STOP` round trip. Choose **cgroup-keyed BPF lookup** when fork rate is high enough that the handshake dominates (observed at >2,000 forks/s in a build sandbox). Taint is stored in a BPF map keyed by cgroup ID; the child inherits the cgroup only if the parent's cgroup is not split, so the boundary is that you must *not* use `CLONE_NEWCGROUP` per child, or you must copy the taint entry synchronously in the `cgroup.post_clone` hook. Never choose **post-hoc reconciliation** for egress-gating taint. It is acceptable only for audit logging, where the 12 ms window produces a label that is late but not wrong. The budget rule is separate and must be enforced independently: budget is a *counter*, decremented on the parent at fork by the amount the child is granted, so the lineage total is conserved. Do not re-derive budget from the allowlist on any event. ## Operational Effect After the fix, the sandbox denies the 3.1 KiB egress with `EPERM` at `connect(2)` and attributes it to the child's inherited `{UNTRUSTED_DOC}` label, which is what the operator needs to see. The fork handshake adds 40–90 µs per tool-call fork; at the observed 400 forks/s peak this is under 4% of wall time and was accepted. The budget-conservation change eliminated a class of over-grant that had been invisible because each individual grant was within the per-scope limit. The residual risk is the `execve` boundary: a child that execs a setuid binary loses its fd table and the supervisor must re-apply taint by reading `/proc//status` before the binary's first syscall, which is not guaranteed. Treat setuid exec inside a tainted lineage as a hard denial, not a race to win. ```python class TaintForkInvariantError(RuntimeError): """Raised when a forked child's taint/budget snapshot does not satisfy child_authority ⊆ parent_authority and child_obligations ⊇ parent_obligations.""" def snapshot_child(parent_record: dict, child_pid: int, observed_at_ns: int, fork_at_ns: int, max_window_ns: int = 1_000_000) -> dict: """Atomically derive the child's task record from the parent's at fork time. Must be called from the clone-stop handler before the child runs. """ if parent_record is None: raise TaintForkInvariantError("no parent record; taint lineage lost") # The window check: if we are past the allowed handshake latency, the child # may already have executed syscalls under stale authority. if observed_at_ns - fork_at_ns > max_window_ns: raise TaintForkInvariantError( f"fork snapshot late by {observed_at_ns - fork_at_ns} ns " f"(max {max_window_ns}); child may have egressed under stale taint" ) parent_taint = frozenset(parent_record["taint"]) parent_remaining = parent_record["egress_budget_remaining"] # Budget must be conserved across the lineage: grant the child from the # parent's remaining, never from the allowlist default. grant = min(parent_record.get("child_grant_bytes", 0), parent_remaining) if grant < 0: raise TaintForkInvariantError("negative budget grant") child = { "pid": child_pid, "taint": parent_taint, # obligations: superset (inherited) "egress_allowlist": parent_record["egress_allowlist"], # authority: subset "egress_budget_remaining": grant, "parent_pid": parent_record["pid"], } # Post-condition: authority subset, obligations superset, budget conserved. if not child["taint"] >= parent_taint: raise TaintForkInvariantError("child taint is not a superset of parent taint") if not set(child["egress_allowlist"]) <= set(parent_record["egress_allowlist"]): raise TaintForkInvariantError("child authority exceeds parent authority") if child["egress_budget_remaining"] + (parent_remaining - grant) != parent_remaining: raise TaintForkInvariantError("lineage budget not conserved across fork") parent_record["egress_budget_remaining"] = parent_remaining - grant return child ```