Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: high
Valid

`MathMasters::mulWad` and `MathMasters::mulWadUp` will revert even when division result is within `uint256` range

Description

MathMasters::mulWad and MathMasters::mulWadUp revert when the multiplication overflows without considering the subsequent division. However, the result of the multiplication and division can be within the uint256 range in this case: $ x \cdot y > \text{type(uint256).max} > \frac{x \cdot y}{WAD} $

Impact

Likelihood: Medium

  • Occurs whenever : $ x \cdot y > \text{type(uint256).max} > \frac{x \cdot y}{WAD}$ which is equivalent to :

Impact: Medium/Low

  • If users are unaware of this behavior, the program will revert, leading to unexpected behavior in contracts using this library.

  • Other libraries, such as Math by OpenZeppelin, manage this case, potentially causing confusion for users.

Proof of Concept

Foundry PoC added in MathMasters.t.sol
function testMulWadRevertEvenIfTheResultOfTheDivisionIsNotOverflowed()
public
{
assertEq(
// Will fail even if the result is 1e60 which is lower than type(uint256).max
MathMasters.mulWad(1e60, 1e18),
Math.mulDiv(1e60, 1e18, 1e18, Math.Rounding.Floor)
);
assertEq(
// Will fail even if the result is 1e60 which is lower than type(uint256).max
MathMasters.mulWadUp(1e60, 1e18),
Math.mulDiv(1e60, 1e18, 1e18, Math.Rounding.Ceil)
);
}

Recommended Mitigation

  • Use a well-known and tested library like Math by OpenZeppelin.

  • Alternatively, implement a mechanism inspired by existing libraries to handle large numbers when the result can be a valid number after division.

Updates

Lead Judging Commences

inallhonesty Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

`mulWadUp` has a bad overflow check

n0kto Submitter
over 1 year ago
inallhonesty Lead Judge
over 1 year ago
inallhonesty Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

`mulWadUp` has a bad overflow check

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.