Skip to main content

scrub_stack

Function scrub_stack 

Source
pub fn scrub_stack()
Expand description

Overwrites the stack below the caller’s frame (32 KiB of it in an optimised build, 128 KiB in an unoptimised one).

Building a keyed object leaves copies of the key and its schedule in the stack frames of the functions that built it: a value moved out of a frame is not dropped there, so its zeroize-on-drop never runs on the copy left behind, and a provider’s own temporaries are not erased at all. Those dead frames are reused by later calls, and a value later built there and moved into the heap (into a Box, a Vec) carries whatever was in its padding or in the unused part of an enum or union along: the key ends up in memory the heap frees without erasure. Calling this right after a key schedule is built and moved to where it lives, from the frame that called the provider, overwrites what the deeper frames left.

What it does: one bulk zero fill (ptr::write_bytes, a memset) of a buffer of that size in its own frame, which is never inlined, so the buffer lies in the stack just below the caller’s frame, where the frames of the calls the caller made before lay. The buffer is then handed to std::hint::black_box and followed by a compiler fence, so the compiler cannot treat the fill as a dead store and drop it. The calling thread needs that much stack free below the caller.

What it does not do: it cannot reach registers, the caller’s own frame or any frame above it, other threads’ stacks, or anything deeper than that size below the caller (a build profile with opt-level 0 and debug-assertions = false gets the optimised depth); and copies the compiler or a provider leaves that no test can see are not observed, so their absence is not guaranteed. The tests show only that copies carried from dead frames into heap padding are gone (tests/dtls_secret_erasure.rs; docs/key-erasure.md).