Fix BoxedUint GCD with mixed-precision zero operands#1291
Open
tob-joe wants to merge 1 commit into
Open
Conversation
Handle zero operands symmetrically before calling the nonzero GCD helper. The helper assumes the left operand is nonzero and is not valid for a mixed-precision zero right operand, because zero's trailing-zero count is bounded by its allocated precision. Replace either zero operand with one for the constant-time path, compute the dummy GCD, then conditionally correct the result with the original operand. For the variable-time path, return the nonzero operand directly, and return zero for gcd(0, 0). 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 #1291 +/- ##
==========================================
+ Coverage 91.26% 91.29% +0.02%
==========================================
Files 189 189
Lines 22390 22466 +76
==========================================
+ Hits 20434 20510 +76
Misses 1956 1956 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Summary
BoxedUint::gcddoes not correctly handle mixed-precision zero operands.The constant-time path only substitutes a nonzero placeholder for the left operand before calling the nonzero GCD helper. That causes two problems:
gcd(0_wide, x_narrow)can panic when the final constant-time correction assigns a narrow value into a wider result.gcd(x_wide, 0_narrow)can return the wrong value whenxhas more trailing zero bits than the zero right operand has precision.The second case occurs because
gcd_nzcomputes a common power-of-two factor usingmin(lhs.trailing_zeros(), rhs.trailing_zeros()). For a zeroBoxedUint,trailing_zeros()is capped by that value's precision, so a narrow zero right operand can cause the result to lose part of the power-of-two factor.Fix
This changes the
BoxedUintGCD wrapper to handle zero operands symmetrically.For the variable-time path, the wrapper now returns the nonzero operand directly when either side is zero, and returns zero for
gcd(0, 0).For the constant-time path, the wrapper now substitutes one for both zero operands before calling
gcd_nz, then conditionally corrects the result with the original operands. This avoids passing a zero right operand intogcd_nzwhile keeping the output precision a public function of the input precisions.Regression Tests
The new tests cover:
gcd(0_wide, x_narrow)gcd(x_narrow, 0_wide)gcd(x_wide, 0_narrow)wherexhas more trailing zero bits than the zero operand has precisiongcd_vartimegcd(0, 0)Verification
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.