# Memory hygiene - Status: SHIPPED in C++ port, hardening in M2 - Spec: SEC-02, NFR-03, PIN-02 - Sources: `tosv.cpp` wipe_mem plus derive, `archive/tosv.py` legacy, `uploads/paste.txt` vision, elite report section 6 ## What it is Key bytes live as briefly as possible: derived per request, used chunk by chunk, wiped after use, and never assembled into one long-lived master value. Logs, dumps, and error paths carry no key material by construction, not by filter. ## How TOSV does it `derive_v2` returns the 32-byte key in an RAII `VKey` (mlock on birth, wipe plus munlock on death), secretstream consumes it during the chunk walk, and the buffers are zeroed when the request ends. v1 keeps the legacy layer vector with the same wipe discipline. M9 adds a bounded key cache (128 entries, 300s TTL, `sodium_malloc` guarded keys, wipe-on-evict, flush on duress wipe): the documented cryptoperiod for derived keys, see [ADR 0007](adr/0007-key-cache.md). The vision text demands more (never the whole key as one value, mlock against swap, wipe on idle timeout), and the elite report specifies the v2 shape: RAII locked buffers, `MADV_DONTDUMP`, guard pages, 5s integrity canaries. The M2 `wipe_mem` wiring has landed: key buffers route through the volatile helper (build shows zero warnings); the compiler-fragility note below still applies. ## What it protects against Co-tenant software on the same box: other processes, crash dumps, swap forensics, and `/proc` snoopers. It shrinks key lifetime from "vault uptime" to "request milliseconds". ## Attack realism Medium. Local unprivileged malware that scrapes RAM is real on shared VPS hosts; wiping plus short lifetimes measurably cut its harvest. Against local root, nothing in-process helps, and this page claims nothing there. Swap encryption and mlock (v2) close the hibernation-image hole. ## Pros Tiny exposure window. Zero log scrubbing needed. Composable: every later hardening (lock, guard, canary) stacks on the same discipline. ## Cons and how the vault resolves them - Raw `memset` can be optimized out by compilers. Resolved in M2 by routing all wipes through `volatile wipe_mem`, and in v2 by RAII locked buffers with `sodium_memzero`, see [future platform](sec-future-platform.md). - Keys exist whole during the chunk walk. Resolved by scope: one 32-byte v2 key per instant beats any whole-vault master key, and lifetimes are milliseconds. Per-byte streaming KDF is tracked as research, not promised. - Swap and core dumps can capture live keys. M5 attempts mlock on every derived key plus a boot probe line (`memlock=`); M7 lands RAII locked buffers (VKey) for v2; canaries plus guard pages stay future. Swap policy is a host-hardening step in the stunnel runbook. - `mlock` needs privilege and limits. Resolved by attempt-and-log: try the lock, audit failure, continue degraded. Never fail closed on hardening. ## Residual risk Compiler-elided memset until M2 lands, and swap images until v2. Both tracked with owners in the spec assumptions. M6: LKeys RAII wipes plus munlocks every key buffer and wipes KDF scratch; mlock can no longer leak across requests. ## See also - [Security index](security.md), [layers](sec-layers.md), [streaming](sec-streaming.md), [future platform](sec-future-platform.md)