Skip to content
Smart contracts Audit finding Advanced

unexpected overflow for FullMath.add512() which can result in irregular behavior

The add512 function in FullMath has an insufficient overflow check that can be bypassed when the high words sum to a value that overflows but the check only reverts if addendA1 > sum1. An attacker could manipulate leverage and deleverage functions to receive more tokens than expected.

codeislight Follow

Originally disclosed on Code4rena

Submitted by codeislight

The vulnerability originates from insufficient checking in add512 function, where the AddOverflow revert gets bypassed, essentially the function assumes that an overflow only happens if (addendA1 > sum1), where in the case that it’s possible for it to overflow in the case that addendA1 == sum1, which can be resulted through assigning a value that makes ( lt(sum0, addendA0) == 1 ) <=> sum0 < addendA0, which can only be achieved normally by overflowing the least significant addition. Then we can simply break the overflow check by assigning overflowing values which results in add(addendA1, addendB1) > type(256).max && addendA1 <= sum1, then we will manage to bypass the revert check and overflow the most significant part of add512 values.

The previous attack vector can lead to a manipulation in leverage and deleverage functions, in a way that it would result in more tokens for the user.

Proof of Concept

Inputting the following values results in an overflow:

uint256 addendA0 = 1

uint256 addendA1 = 100

uint256 addendB0 = 115792089237316195423570985008687907853269984665640564039457584007913129639935 (uint256 max value)

uint256 addendB1 = 115792089237316195423570985008687907853269984665640564039457584007913129639935 (uint256 max value)

results in:

sum0 = 0

sum1 = 100

The expected behavior is to revert since, A1 + B1 result in a value that overflows, but instead consider it as a valid behavior due to the insufficient checking.

Abstraction:

A1 - A0

B1 - B0

=

S1 - S0

S0 = A0 + B0

S1 = A1 + B1 + ( if S0 overflows [+ 1])

ensure A1 <= S1

revert only on A1 > S1

in the case of S0 overflows:

S1 = A1 + B1 + 1

require(A1 <= S1) is not most suited check, due to the fact that in the case of A1 == S1 check, it can still overflow if S1 = A1 + B1 + 1 overflows. which would bypass A1 > S1 revert check.

The major impact affects the leverage() and deleverage() results in values which are not expected.

Recommended Mitigation Steps

Add an equality check for if statement in add512 function.

   function add512(uint256 addendA0, uint256 addendA1, uint256 addendB0, uint256 addendB1) public pure returns (uint256 sum0, uint256 sum1) {
        assembly {
            sum0 := add(addendA0, addendB0)
            carry  := lt(sum0, addendA0)
            sum1 := add(add(addendA1, addendB1), carry)
        }
        if (addendA1 > sum1 ||      ((sum1 == addendA1 || sum1 == addendB1) && (carry < addendA0 || carry < addendB0))
) revert AddOverflow(addendA0, addendA1, addendB0, addendB1);
//
    }

Picodes (judge) decreased severity to Medium and commented:

No explanation related to how this could lead to errors in leverage or deleverage.

vhawk19 (Timeswap) confirmed and resolved:

Fixed in PR.


The original report as published on Code4rena, by codeislight. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the overflow check only compares the high word of the first addend with the high word of the sum, missing the case where the high words overflow but the first addend's high word is less than or equal to the sum's high word.

Impact

This could lead to manipulation in leverage and deleverage functions, potentially resulting in more tokens for the user.

What the attacker needed

Ability to call add512 with chosen inputs; leverage or deleverage uses add512

How it was found

  1. Call add512 with addendA0 = 1, addendA1 = 100, addendB0 = uint256 max, addendB1 = uint256 max.
  2. Observe that sum0 becomes 0 and sum1 becomes 100, and the function does not revert.
  3. The expected behaviour is to revert because A1 + B1 overflows, but the check is bypassed.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

This is about a maths function that adds two large numbers split into two parts. The check that should stop the result from being too big has a gap, so certain inputs slip through and produce a wrong answer. That wrong answer could then be used in other functions to get more tokens than you should.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More from Timeswap contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.