Preserve NonZero and Odd invariants on zeroize#1287
Open
tob-joe wants to merge 1 commit into
Open
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
| impl<T: zeroize::Zeroize + One> zeroize::Zeroize for NonZero<T> { | ||
| fn zeroize(&mut self) { | ||
| self.0.zeroize(); | ||
| self.0 = T::one_like(&self.0); |
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Preserve
NonZeroandOddwrapper invariants afterzeroize().The current
Zeroizeimpls zeroize the wrapped value directly. ForNonZero<T>andOdd<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
zeroizecrate's behavior for coreNonZero*integer types, and preserves boxed integer precision throughOne::one_like.The tests cover stack and boxed
NonZero/Oddvalues, 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.