Skip to content

Preserve NonZero and Odd invariants on zeroize#1287

Open
tob-joe wants to merge 1 commit into
RustCrypto:masterfrom
tob-joe:fix-wrapper-zeroize-invariants
Open

Preserve NonZero and Odd invariants on zeroize#1287
tob-joe wants to merge 1 commit into
RustCrypto:masterfrom
tob-joe:fix-wrapper-zeroize-invariants

Conversation

@tob-joe

@tob-joe tob-joe commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Preserve NonZero and Odd wrapper invariants after zeroize().

The current Zeroize impls zeroize the wrapped value directly. For NonZero<T> and Odd<T>, that can leave a live wrapper containing zero or an even value after a safe method call, violating the wrapper's type invariant.

This changes the impls to zeroize the inner storage and then restore a one-like sentinel value. That keeps the wrapper valid after zeroization, mirrors the zeroize crate's behavior for core NonZero* integer types, and preserves boxed integer precision through One::one_like.

The tests cover stack and boxed NonZero/Odd values, including boxed precision preservation after zeroization.


This work was completed by Trail of Bits as part of the Patch The Planet project in collaboration with OpenAI. The vulnerability was identified primarily by the Codex coding agent, and manually reviewed before submission.

NonZero and Odd wrappers implemented Zeroize by zeroizing their inner values directly. That can leave a live wrapper containing zero or an even value, violating the wrapper invariant after a safe method call.

After zeroizing the inner storage, restore a one-like sentinel value so the wrapper remains valid. This mirrors zeroize's behavior for core NonZero integer types, and preserves boxed integer precision via One::one_like.

Co-authored-by: GPT 5.5 <gpt-5.5@openai.com>
@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.05%. Comparing base (3f7dede) to head (3a0f89c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1287      +/-   ##
==========================================
+ Coverage   91.01%   91.05%   +0.03%     
==========================================
  Files         189      189              
  Lines       22160    22185      +25     
==========================================
+ Hits        20169    20200      +31     
+ Misses       1991     1985       -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/non_zero.rs
impl<T: zeroize::Zeroize + One> zeroize::Zeroize for NonZero<T> {
fn zeroize(&mut self) {
self.0.zeroize();
self.0 = T::one_like(&self.0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of a NonZero<BoxedUint>, this actually leaves the original value in memory and creates a new allocation. It might be best to zeroize the internal value, then use One::set_one to restore an acceptable state. Either way I think the modified bounds on the implementation make this a breaking change that would need to go into a future minor version.

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.

2 participants