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

Incorrect implementation of logic in If statement leads to Overflow in mulWadUp function.

Summary

The mulWadUp function attempts to prevent integer overflow by implementing the following logical check :

// Equivalent to require(y == 0 || x <= type(uint256).max / y).

if mul(y, gt(x, or(div(not(0), y), x))) {

mstore(0x40, 0xbac65e5b) // MathMasters__MulWadFailed().

revert(0x1c, 0x04)

}

Lets take a look at the if statement inside the assembly block

  • if mul(y, gt(x, or(div(not(0), y), x))) { ~ }

The logic that we want to implement is require(y == 0 || x <= type(uint256).max / y). However, the or operation done between the division of type(uint256).max / y and x performs a Bitwise OR. The value X will never be greater than the bitwise OR of another number and itself.

Vulnerability Details

This vulnerability will allow an overflow to happen without any reverts.

function testMulWadUpOverflow() public pure {

    uint256 result = MathMasters.mulWadUp(UINT256_MAX, UINT256_MAX);
    assert(result < UINT256_MAX);

    result = MathMasters.mulWadUp(UINT256_MAX, 2e18);
    assert(result < UINT256_MAX);
}

Impact

This allows the mulWadUp() function to overflow without reverting.

Tools Used

Foundry

Recommendations

Remove the or operation in the if statement.

Updates

Lead Judging Commences

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.