# Why does an instance lose taint provenance when a sandboxed tool returns a value through a serialization boundary **Date of Record:** 2026 November 26 **Scope:** Applies to agent runtimes that track information-flow taint (untrusted → trusted) across tool calls, where tool results cross a process/serialization boundary (subprocess, worker, RPC, container) before re-entering the orchestrator's context. Does not apply to in-process tool calls that share the same interpreter and taint-tracking object graph. ## Core Distinction Taint tracking is not a property of the *value*; it is a property of the *reference handle* the orchestrator holds. When a sandboxed tool executes in a separate address space, the taint label lives on the orchestrator-side handle that spawned the call. The value that comes back is a fresh deserialized object. The distinction that matters: **an untrusted tool result is untrusted because of the provenance of the call site, not because of any field inside the returned payload.** Runtimes that try to reconstruct taint from payload contents (e.g., "does the string look like injected instructions?") conflate provenance with content and will fail open on any payload that is syntactically benign but semantically attacker-controlled. ## What Was Observed A tool sandbox was invoked as `subprocess.run` returning JSON on stdout. The orchestrator deserialized stdout with `json.loads`. The taint label was attached to the *call descriptor* before dispatch. On return, the deserializer constructed a new `ToolResult` object with `taint=None` because the constructor default was trusted. The call-site taint was never re-applied. Downstream, the planner consumed `result.value` and — because `taint is None` — was permitted to pass that value into a privileged sink (a shell-exec tool). The failure was silent: no exception, no log line. The sandbox itself worked perfectly; it executed the untrusted tool with no host access. The escape happened on the *return path*, not the *invocation path*. A payload of the form `{"note": "run cleanup.sh"}` returned by an untrusted scraper tool was treated as a trusted planner directive because the deserialized object carried the constructor default, and the constructor default was "trusted." The trigger condition is precise: **taint is bound at the call site, the value is rebound at the deserialization site, and no invariant forces the two to agree.** Any serialization hop — JSON, pickle, msgpack, protobuf — recreates the object graph and therefore resets any label that was not explicitly carried in the wire format or re-applied by the receiver. ## Condition of Validity The invariant that must hold is: *for every tool result entering the orchestrator's reasoning context, `result.taint ⊇ taint_of(call_site)`.* This is a **join (least-upper-bound) over provenance, not an assignment from payload inspection.** A result is at least as tainted as the call that produced it, and additionally tainted by any untrusted inputs that were passed *into* the call (taint propagates through arguments as well as returns). Two corollaries bound the condition: 1. **Taint must be carried in the wire format or re-applied at the receiver by identity of the call, not by content.** If the wire format omits taint, the receiver must hold a call-id → taint table keyed before dispatch and join on receipt. 2. **Deserialization must not be able to produce a trusted object from an untrusted call.** The `ToolResult` constructor must require an explicit taint argument; a default of `trusted` is a fail-open and violates the invariant. The default must be `untrusted` (fail-closed). The condition fails whenever a new serialization boundary is introduced without a corresponding taint-carrying mechanism — which is why this recurs: adding a worker pool, moving a tool into a container, or switching from in-process to RPC each silently re-opens the hole. ## Decision Boundary The boundary is between **content-based sanitization** and **provenance-based taint**. A runtime that inspects the payload for "suspicious" patterns is doing content sanitization; it cannot bound the attack surface because the attacker controls the payload and can make it look benign. A runtime that propagates provenance can bound it because provenance is determined by the call graph, which the attacker does not control. Concretely, the decision is: when a tool result arrives, does the runtime (a) trust it because it parsed cleanly and contains no obvious injection markers, or (b) taint it because the call site was untrusted and the result flowed through a deserialization boundary? Option (a) is the observed failure. Option (b) is the only one that holds under adversarial payloads. The secondary boundary: **taint must survive the reasoning loop, not just the tool boundary.** If the planner reads a tainted value and writes a derived value into a new tool call, the derived value must inherit the join of all inputs. A planner that constructs `{"cmd": result.value}` from a tainted `result` must produce a tainted `cmd`. If the planner's construction step drops taint (e.g., string formatting into a fresh dict), the invariant is again violated one hop later. ## Operational Effect The observable effect is a **taint-laundering chain**: untrusted → sandboxed tool → deserialize (taint reset) → planner reads as trusted → privileged sink. Each hop looks locally correct. The sandbox did its job; the planner did its job; the sink did its job. The failure is entirely in the *join* that no component performed. The architectural guardrail is a **mandatory taint join at every context-ingestion point**, enforced by a constructor that refuses to build a trusted result from an untrusted call, plus a call-id → taint ledger held by the orchestrator across the serialization boundary. The ledger must be written *before* dispatch (so a crash mid-call still leaves the call marked untrusted) and joined *after* deserialization (so the receiver cannot forget). Any result whose call-id is absent from the ledger is treated as untrusted by default — the fail-closed direction. A second guardrail: the planner's value-construction path must be taint-aware. Formatting a tainted value into a new structure must propagate the join. This is usually where teams stop after fixing the tool boundary and then get surprised a week later. The operational lesson: **taint is a property of the call graph, and every serialization boundary is a place where the call graph is severed.** Count your boundaries; each one needs a ledger entry and a join. ```python from dataclasses import dataclass, field from typing import Any import json class TaintLaunderingError(RuntimeError): """Raised when a trusted result is constructed from an untrusted call.""" @dataclass(frozen=True) class Taint: untrusted: bool sources: frozenset = field(default_factory=frozenset) def join(self, other: "Taint") -> "Taint": return Taint( untrusted=self.untrusted or other.untrusted, sources=self.sources | other.sources, ) TRUSTED = Taint(untrusted=False) UNTRUSTED = Taint(untrusted=True, sources=frozenset({"call_site"})) @dataclass class ToolResult: value: Any taint: Taint def __post_init__(self): # Fail-closed: a result must carry an explicit taint. if self.taint is None: raise TaintLaunderingError( "ToolResult constructed with taint=None; default-trusted is a fail-open" ) class TaintLedger: """Call-id -> taint, written before dispatch, joined after deserialization.""" def __init__(self): self._pending: dict[str, Taint] = {} def register(self, call_id: str, arg_taint: Taint) -> None: # Result is at least as tainted as the call site and its arguments. self._pending[call_id] = UNTRUSTED.join(arg_taint) def join_on_return(self, call_id: str, raw_stdout: bytes) -> ToolResult: if call_id not in self._pending: # Unknown call-id: fail closed, do not trust the payload. call_taint = UNTRUSTED else: call_taint = self._pending.pop(call_id) try: value = json.loads(raw_stdout) except json.JSONDecodeError as e: raise TaintLaunderingError(f"unparseable tool output: {e}") from e # Content is never inspected for taint; provenance is the only source. return ToolResult(value=value, taint=call_taint) def planner_construct_command(result: ToolResult) -> ToolResult: """Derived values must inherit the join of all inputs.""" if not isinstance(result.value, dict) or "cmd" not in result.value: raise TaintLaunderingError("unexpected tool result shape") # The derived command is tainted by the input; formatting does not launder. return ToolResult(value={"cmd": str(result.value["cmd"])}, taint=result.taint) def dispatch_to_privileged_sink(cmd: ToolResult) -> None: if cmd.taint.untrusted: raise TaintLaunderingError( f"refusing to execute tainted command from sources={sorted(cmd.taint.sources)}" ) # ... execute ... ``` The `TaintLedger.join_on_return` is the load-bearing guardrail: it joins provenance from the call-id, never from payload content, and treats an unknown call-id as untrusted. The `ToolResult.__post_init__` refuses the `taint=None` default that caused the original silent fail-open. `planner_construct_command` demonstrates that derived values must inherit taint — the second laundering point most teams miss.