Skip to content

zeroize: avoid double-zeroizing Vec's initialized elements - #1525

Open
Johnsonajibi wants to merge 1 commit into
RustCrypto:masterfrom
Johnsonajibi:fix/vec-zeroize-double-write
Open

zeroize: avoid double-zeroizing Vec's initialized elements#1525
Johnsonajibi wants to merge 1 commit into
RustCrypto:masterfrom
Johnsonajibi:fix/vec-zeroize-double-write

Conversation

@Johnsonajibi

Copy link
Copy Markdown

Fixes #1524.

Vec<Z>::zeroize() currently:

  1. Zeroizes the initialized elements via iter_mut()
  2. Calls clear(), which resets len to 0
  3. Zeroizes spare_capacity_mut()

Since spare_capacity_mut() covers everything beyond len, and len is already 0 by step 3, it covers the entire allocation - re-zeroizing the elements that were already zeroized in step 1.

This PR reorders the operations so the spare (truly uninitialized) capacity is zeroed first, while len still reflects the real element count. The two zeroing passes then cover disjoint ranges instead of overlapping.

Behavior is unchanged - the full allocation is still zeroed - only the redundant work is removed. Verified locally with cargo test --features alloc: all existing tests pass, including zeroize_vec_entire_capacity, which specifically checks that no partially-zeroized or uninitialized data survives (this test's ordering-sensitive assertions still hold under the new order - traced through manually and confirmed via the test run).

Vec<Z>::zeroize() zeroized the initialized elements via iter_mut(),
then called clear() (which resets len to 0), then zeroized
spare_capacity_mut(). Since spare_capacity_mut() covers everything
beyond len, and len was already 0 at that point, it covered the
entire allocation - re-zeroizing the elements that were already
zeroized in the first step.

Reorder so the spare (truly uninitialized) capacity is zeroed first,
while len still reflects the real element count, so the two zeroing
passes cover disjoint ranges. Behavior is unchanged (the full
allocation is still zeroed) other than removing the redundant work;
existing tests (including zeroize_vec_entire_capacity, which checks
no partially-zeroized or uninitialized data survives) still pass.

Fixes RustCrypto#1524
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zeroize: Vec<Z>::zeroize() zeroizes the initialized elements twice

1 participant