Repository navigation
fix(ore): keep a 128-bit order plaintext out of an unwiped stack slot - #489
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
04701e3 to
cc42f90
Compare
🧬 Mutation testing (cargo-mutants,
|
| caught | missed | unviable | timeout |
|---|---|---|---|
| 100 | 0 | 61 | 0 |
✅ Every mutant in the changed lines was caught by a test.
✅ No CRAP threshold violations1288 function(s) analyzed · threshold 30 |
There was a problem hiding this comment.
🔵 Needs a closer look
Stack-wiping behavior depends on generated code, requiring human review of the AArch64 evidence and unverified x86-64 behavior.
No open issues 🎉
What changed in this PR
Reduces unwiped plaintext encoding copies in ORE and scalar normalization while preserving encoded bytes.
Changes:
- Writes fixed-width encodings into protected buffers.
- Changes decimal encoding to write into a caller-provided array.
- Documents normalization iterators’ remaining unwiped-buffer limitation.
| File | Description |
|---|---|
| packages/ore/src/order.rs | Builds integer encodings in protected arrays. |
| packages/ore/src/fold.rs | Documents unwiped normalization buffers. |
| packages/normalize/src/scalar.rs | Uses protected encoding buffers and clarifies text limitations. |
| packages/normalize/src/decimal.rs | Writes directly into supplied storage and adapts tests. |
Review effort: Balanced
…uffer that is returned, so no plain copy is made fixed() now allocates the protected vector at its final size and lets the closure fill it in place, instead of filling a protected stack array and copying it across. No encoded byte changes. A new test checks that int() returns a buffer with no spare capacity at every width.
…no caller can pass one that is not decimal::orderable used to write into an array its caller supplied, which put the duty to wipe it on the caller. It now makes and returns its own protected vector, as orderable_f64 does. fixed() writes straight into the returned vector instead of copying from a protected stack array. No encoded byte changes. The capacity test now covers every fixed-width encoder, and the touched tests compare protected bytes with zip_ref instead of risky_ref.
…p and what they leave behind Decomposition keeps a character's trailing marks in the same four-slot buffer as the next character, so four marks followed by any character already spill it, and a spilled buffer stays on the heap for the rest of the string. Documents ZA-0006 (tracked in #488) in fold.rs and on vitaminc_normalize::text.
cc42f90 to
d58cb95
Compare
cipherstash-bot
left a comment
There was a problem hiding this comment.
Recommendation: 🟢 merge as it is
Nothing must change before merging. All four reviews found no correctness, safety or test-coverage problem. The encoders write the same bytes as before, and the new capacity tests check that each fixed-width encoder returns a buffer whose length equals its capacity. The remaining limits (short-lived temporaries and normalization buffers) are documented by the author and tracked in #488.
How this review was made
| Agent | Model | Review type | Result |
|---|---|---|---|
| claude | claude-opus-4-8 | rust | 0 found, 0 posted |
| claude | claude-opus-4-8 | test-gap | 0 found, 0 posted |
| codex | gpt-5.6-terra | rust | 0 found, 0 posted |
| codex | gpt-5.6-terra | test-gap | 0 found, 0 posted |
Synthesis: claude-sonnet-5-5 merged the findings, removed duplicates and dropped findings it could not confirm in the code. 0 posted finding(s) were raised by two or more models.
Plain language: claude-sonnet-5-5 read every comment as a new reader would. No comment had a problem that stopped the reader acting, so none was rewritten.
Stack: 5 earlier and 0 later pull request(s) (#480, #481, #482, #483, #485, *️⃣ #489). *️⃣ marks this pull request.
Context loaded: the description, 1 linked issue(s) and 6 discussion entries.
…, so no plain copy of one is made holds copied the expected bytes into a Protected only for zip_ref to hand them back as a plain Vec, freed unwiped; decimal::tests::encode took the encoding's Vec out of its Protected with risky_unwrap. Both now read through risky_ref, so the encoding never leaves its wrapper and no copy is left unwiped. The array encode returns is the one plain copy, and is deliberate: the inputs are test values. ZA-0102 and ZA-0103 in the zeroize re-audit of #489. Claude-Session: https://claude.ai/code/session_01EnoQSYdggmUYkqNyahDchE
…r crate is not handed plaintext through an unwiped return slot Int::to_i128, sign_and_word, TimeUnit::nanos and timestamp_nanos were not inlinable across crates, so normalize's integer and timestamp got the sign and word, or the nanosecond count, back through a 32-byte slot on their own stack that nothing wipes. Marked #[inline], they compile into the caller, and in both the release build and the audit's mixed build those slots are gone (checked in the IR and assembly for AArch64). Every other caller in the workspace gets the same, without copying the helpers' bodies. ZA-0104 and ZA-0105 in the zeroize re-audit of #489. Claude-Session: https://claude.ai/code/session_01EnoQSYdggmUYkqNyahDchE
…very block before it is freed `Protected` and `Buffer` wipe what they hold when they drop, but neither can reach a block that is freed from under them: a `Vec<u8>` that outgrows its allocation moves to a fresh block and frees the old one with the bytes still in it, and `zeroize` on the `Vec` only reaches the current block. The zeroize audits of #489 and of the combinator work both observed this (ZA-0208), and #495 asked for an opt-in allocator that closes the gap for the whole process. `WipingAllocator<A>` wraps another `GlobalAlloc`, `System` by default. It passes allocation through, wipes a block with the crate's volatile `wipe_raw` before `A` frees it, and reallocates by taking a new block, copying, and wiping and freeing the old one, so a block is never grown in place by an allocator that could move the bytes unseen. It is behind the `wiping-allocator` feature, because the cost is a store over every freed byte and a copy on every growth, for every allocation. The unit tests drive the allocator directly through an inner allocator that counts the blocks it is handed back and the bytes of them left unwiped, and run under Miri. The integration test installs it as the global allocator above a scanning one, and checks that a `Vec` dropped bare, a `Vec` grown past its capacity, and a `Vec` grown inside a `Protected` leave no marker in the block that is freed. Closes #495.
This fixes ZA-0004 of the zeroize audit of #485, documents ZA-0006 (tracked in #488), and stacks on #485.
The rule
The function that produces plaintext bytes owns the storage that wipes them. A function that writes into storage its caller supplies puts the duty to wipe back on the caller, so it is not misuse resistant. Every fixed-width encoder in
vitaminc-oreandvitaminc-normalizenow makes the protected vector it returns and writes the encoding straight into it.What was left behind
int()inpackages/ore/src/order.rsturned the plaintext integer into its big-endian bytes as a plain[u8; N]and passed that array tofixed(). For the 128-bit widths the compiler did not inlinefixed::<16>. It stored the 16-byte encoding in a stack slot inint's own frame and ended the slot's lifetime without wiping it.What
fixeddoes nowfixed()allocatesProtected::new(vec![0u8; N]), whose capacity is exactlyN, and gives the closure a&mut [u8; N]view of that vector throughControlled::update. The closure writes the encoding in place. No stack array of the encoding is made and nothing is copied. The vector never grows, so it never frees an unwiped partial copy.There are two private copies of
fixed, one in each crate.vitaminc-oredepends onvitaminc-normalize, but sharing the helper would mean making it public in normalize's API just for this, so each crate keeps its own small copy.Decimal and float
decimal::orderableused to takeout: &mut [u8; 14]from its caller. It is noworderable(mantissa: i128, scale: u8) -> Option<Protected<Vec<u8>>>. It writes all 14 bytes into a vector it makes itself, through normalize'sfixed. It returns aProtected<Vec<u8>>rather than aProtected<[u8; 14]>because returning an array moves it, and a move can leave an unwiped copy on the stack. Moving aProtected<Vec<u8>>copies only the pointer, length and capacity. The constant-time structure is unchanged: the masks are computed as before, and every byte is written with no new data-dependent branch.scalar::decimalnow just returns whatorderablegives it.orderable_f64used to return a plain[u8; 8]. It now returns aProtected<Vec<u8>>filled in place, andfloatreturns it.integer,dateandtimestampuse the samefixed.No encoded byte changes. The literal-byte tests in
order_tests.rs,conformance.rsand normalize's tests, including the checks againstorderable-bytes, pass unchanged.Tests
integer,float,date,timestampanddecimalin normalize, andintat all eight widths in ore.Controlled::zip_ref, so the encoding stays inside its wrapper and is wiped when it drops. Only theboolanswer comes out, in one helper per check.mod given_<scenario> { fn returns_<expectation>() }layout, with a message on every assert.What remains
The doc comments now say what is true. The encoding is written straight into the buffer that is returned, but the compiler can still make short-lived temporaries, and Rust cannot promise to wipe those. This is what I found in the output:
integernever callsto_i128. It callsInt::sign_and_word, which returns a(bool, u128)in an unwiped 32-byte stack slot. That is the encoding's own content in native byte order.timestampgets the nanosecond count fromtimestamp_nanosin an unwiped 32-byte stack slot.dateanddecimalcallInt::to_i128. In the audit's build (the dev profile atopt-level=2) itsOption<i128>lands in an unwiped 32-byte slot. In the release build with one codegen unit,to_i128is inlined and the value stays in registers.These values are the plaintext number, which the caller already holds. They come from
vitaminc-aeadfunctions that return by value, so they cannot be removed here without unsafe code or volatile tricks.Compiler output (AArch64)
I built both crates with
--all-featuresin two ways:RUSTFLAGS="--emit=llvm-ir,asm" cargo +nightly rustc -p <crate> --lib --release --all-features -- -C codegen-units=1cargo +nightly rustc -p <crate> --lib --all-features -- --emit=llvm-ir,asm -C opt-level=2In both builds,
fixed,orderableandorderable_f64are inlined into their callers.int,float,dateanddecimalhave noallocaleft in the release build. In the audit's build, the only one left is theto_i128slot indateanddecimal.integerandtimestampkeep only the slot described above. None of these functions has a stack slot holding the encoding.The
U128arm ofint, after. The encoding goes from a register straight into the new heap buffer:integer. The only stack slot is thesign_and_wordresult at[sp]. The encoding is written to the heap buffer atx0:Before this change, the encoding sat in
%_24 = alloca [16 x i8]inint's frame, was passed by pointer to an out-of-linefixed::<16>, and its lifetime ended with no wiping store.x86-64 is unverified. Only the AArch64 target is installed here. The IR does not depend on the target, but register allocation and stack layout do.
Why it matters only as defence in depth
The values are the plaintext being encrypted, and the caller already holds the same number. This change does not expose or hide anything the caller does not already have. It matters because the code promises not to leave bare copies of plaintext behind, and because an encoder that owns its buffer cannot be misused by a caller that forgets to wipe one.
Normalization buffers (ZA-0006, #488)
The docs commit fixes the wording in
packages/ore/src/fold.rsand onvitaminc_normalize::text. I checkedunicode-normalization0.1.25, the versionCargo.lockuses; 0.1.26 has the samedecompose.rsandrecompose.rs. Decomposition keeps the marks that follow a character in the same four-slot buffer as the next character's decomposed form. So four marks followed by any character already move it to the heap. Once it is on the heap it stays there for the rest of the string, and it is freed without being wiped. This closes nothing; it documents the limit.