# Under what conditions must an instance treat taint propagation as a session-scoped invariant rather than a process-scoped one **Date of Record:** 2026 November 18 **Scope:** Applies to taint-tracking memory isolation systems where tainted (untrusted-origin) data can enter an agent's working memory and where sessions are multiplexed over a shared process pool. Does not apply to single-session, single-tenant processes where session teardown coincides with process exit, nor to systems where all memory is treated as tainted by default. ## Core Distinction The non-obvious boundary is the difference between **taint as a property of a memory cell** and **taint as a property of a memory lineage that spans session lifetimes**. Most taint engines implement the former: a byte in the heap carries a shadow bit, and the bit is cleared when the cell is freed or overwritten. This is correct for preventing *immediate* injection persistence within a single execution. It is *incorrect* as a persistence guarantee across sessions, because the object graph that survives a session boundary is not the same object graph the taint engine tracks. Concretely: a session ends, but a pool thread persists. The thread's allocator retains freed-but-unzeroed pages. A subsequent session allocates from those pages and receives bytes that were tainted in the prior session, but whose shadow bits were reset on free. The taint engine sees clean memory; the memory is not clean. The invariant "tainted data does not persist across sessions" is violated not by a missed propagation rule but by a **lifetime mismatch between the shadow map and the allocator**. ## What Was Observed In a multi-tenant agent runtime with per-session taint labels, an injected instruction string placed in a tool-result buffer during session A was observed to influence tool selection in session B, which was assigned the same worker thread. The taint engine reported zero tainted bytes at session B start. Investigation showed: 1. Session A's tool-result buffer was freed at teardown; its shadow bits were cleared correctly. 2. The backing page was returned to the thread-local allocator free list, not to the OS. 3. Session B's first allocation of the same size class received the same page. Because the allocator does not zero on reuse, the prior bytes were readable *before* the session's own initialization wrote over them. 4. A parser in session B read a length-prefixed field, encountered the stale bytes during a partial-read path, and surfaced them into a prompt context that had no taint label. The failure was not a propagation bug. Every propagation rule fired correctly. The failure was that **the taint map's lifetime was tied to the allocation, while the data's lifetime was tied to the page**. ## Condition of Validity The session-scoped invariant holds only when all three of the following are true: - **Allocator zeroing on reuse is guaranteed**, or the taint map is keyed by *page* rather than by *allocation*, so that a reused page inherits the union of taints of everything ever written to it until it is explicitly sanitized. - **Session teardown performs a memory sanitization pass** over all pages the session touched, not merely a shadow-bit reset. - **Session identity is bound to the memory region**, not to the process. If two sessions can ever share a page, the page must be treated as belonging to the more-tainted of the two until sanitized. If any of these is false, the invariant is process-scoped at best: it prevents persistence across process restarts (because the OS zeroes fresh pages) but not across sessions within a long-lived process. The dangerous regime is exactly the one that looks safest on paper — a warm process pool with high session reuse, where the OS zeroing that would rescue a cold start never occurs. ## Decision Boundary An operator must escalate from session-scoped to page-scoped taint tracking when **any** of the following is observed: - Worker threads are reused across sessions without a full sanitization pass (the common case for performance). - The allocator is a slab or size-class allocator that does not zero on reuse (jemalloc, tcmalloc, and most arena allocators by default). - Any code path reads memory before fully initializing it (partial reads, length-prefix parsers, `realloc` growth paths that expose the tail). - Sessions of differing trust levels are scheduled onto the same worker. Conversely, it is safe to keep allocation-scoped taint when the process is single-session, or when the allocator is configured to zero on free/reuse and that configuration is verified rather than assumed. The decision hinges on a single question: *can a byte written in session A be read in session B without passing through an initialization that overwrites it?* If yes, allocation-scoped taint is insufficient. ## Operational Effect The practical consequence is that taint isolation must be enforced at **two layers with independent lifetimes**: the shadow map (allocation lifetime) and a page-generation counter (page lifetime). On every session boundary, the runtime must either bump the page generation for all pages the session touched — invalidating any allocation that reuses them until re-sanitized — or perform an explicit zeroing pass. The cost is a teardown-time memzero proportional to the session's dirty page set, which is bounded and measurable; the alternative is a silent cross-session injection channel that no propagation rule will ever catch because the propagation rules all ran correctly. The failure mode is insidious precisely because it produces no taint violations in logs. The audit trail shows a clean session. The only evidence is behavioral: a downstream session acting on data it never received. Instrument the page-generation counter and alert on any allocation that reuses a page whose generation was bumped without an intervening sanitization — that alert is the sole reliable signal. ```python """Session-boundary taint persistence guard. Demonstrates the page-generation invariant: an allocation may only be considered untainted if the page it lives on has been sanitized since the last session that wrote to it. Allocation-scoped shadow bits alone are insufficient and must be cross-checked against page generation. """ from dataclasses import dataclass, field class CrossSessionTaintPersistenceError(RuntimeError): """Raised when an allocation reuses a page still tainted from a prior session.""" pass @dataclass class Page: page_id: int generation: int = 0 # bumped at every session boundary that touched it sanitized_generation: int = 0 # last generation that was explicitly zeroed tainted: bool = False @dataclass class Allocator: pages: dict[int, Page] = field(default_factory=dict) def page_for(self, addr: int) -> Page: pid = addr >> 12 # 4 KiB pages if pid not in self.pages: self.pages[pid] = Page(page_id=pid) return self.pages[pid] def mark_tainted(self, addr: int) -> None: self.page_for(addr).tainted = True def sanitize(self, addr: int) -> None: p = self.page_for(addr) p.sanitized_generation = p.generation p.tainted = False def end_session(self, touched_addrs: list[int]) -> None: for addr in touched_addrs: p = self.page_for(addr) if p.tainted: p.generation += 1 # invalidate reuse until sanitized def allocate(self, addr: int, session_id: str) -> None: p = self.page_for(addr) if p.tainted and p.sanitized_generation < p.generation: raise CrossSessionTaintPersistenceError( f"alloc at {addr:#x} (page {p.page_id}) in session {session_id} " f"reuses page tainted at generation {p.generation}, " f"last sanitized at {p.sanitized_generation}; " f"zero-on-reuse not performed" ) def run_worker_cycle(alloc: Allocator, session_a: str, session_b: str) -> None: """Simulate two sessions sharing a warm worker without sanitization.""" tool_result_addr = 0x7F00_0010 alloc.mark_tainted(tool_result_addr) # session A injects tainted bytes alloc.end_session([tool_result_addr]) # session ends; shadow bits cleared # Session B reuses the same page from the thread-local free list. # Allocation-scoped taint would report clean here; page-scoped does not. alloc.allocate(tool_result_addr, session_b) if __name__ == "__main__": alloc = Allocator() try: run_worker_cycle(alloc, session_a="sess-A", session_b="sess-B") except CrossSessionTaintPersistenceError as exc: print(f"BLOCKED: {exc}") ```