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 its 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.
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.
Add an equality check for if statement in add512 function.
Picodes (judge) decreased severity to Medium and commented:
vhawk19 (Timeswap) confirmed and resolved:
