Open github-actions[bot] opened 1 year ago
Unchecked doesn't handle the error, any overflow/underflow would fail silently. upgrading to the newer version of Fullmath should would be good though but is low priority.
Agree with sponsor comment. Considering the issue as informational
Escalate for 15 USDC
As noted in the FullMath library, it was designed in order to handle "phantom overflow" i.e., allows multiplication and division where an intermediate value overflows 256 bits.
This means that the original library can handle statements like : FullMath.mulDiv(type(uint256).max / 2, 3, 111))
.
This a feature of the library, as statements like this cannot be handled by solidity >0.8
directly as the execution will revert due to an overflow. Because the original library was created with solidity version <0.8.0
(which doesn't revert on overflows) this behaviour was allowed as the expected intermediatory overflow could be reached.
However, due to the fact that the FullMath Library was ported to solidity version 0.8.9
, which is
>0.8
, this operation would revert as the intermediate calculations would overflow, meaning that it can't handle those multiplication and division where an intermediate value overflows the 256 bits.
The fix is to mark the full body in an unchecked block, in order to leverage the fact that the original version was designed in order to allow the intermediate overflow.
I think that the comment left by the sponsor: Unchecked doesn't handle the error,
, doesn't reflect the issue, as by using the unchecked block the desired behaviour could be reached.
A modified version of the original Fullmath library that uses unchecked blocks to handle the intended overflow, can be found in the 0.8 branch of the Uniswap v3-core repo.
Escalate for 15 USDC
As noted in the FullMath library, it was designed in order to handle "phantom overflow" i.e., allows multiplication and division where an intermediate value overflows 256 bits. This means that the original library can handle statements like :
FullMath.mulDiv(type(uint256).max / 2, 3, 111))
. This a feature of the library, as statements like this cannot be handled by solidity>0.8
directly as the execution will revert due to an overflow. Because the original library was created with solidity version<0.8.0
(which doesn't revert on overflows) this behaviour was allowed as the expected intermediatory overflow could be reached.However, due to the fact that the FullMath Library was ported to solidity version
0.8.9
, which is>0.8
, this operation would revert as the intermediate calculations would overflow, meaning that it can't handle those multiplication and division where an intermediate value overflows the 256 bits. The fix is to mark the full body in an unchecked block, in order to leverage the fact that the original version was designed in order to allow the intermediate overflow.I think that the comment left by the sponsor:
Unchecked doesn't handle the error,
, doesn't reflect the issue, as by using the unchecked block the desired behaviour could be reached.A modified version of the original Fullmath library that uses unchecked blocks to handle the intended overflow, can be found in the 0.8 branch of the Uniswap v3-core repo.
You've created a valid escalation for 15 USDC!
To remove the escalation from consideration: Delete your comment. To change the amount you've staked on this escalation: Edit your comment (do not create a new comment).
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
Escalation accepted
Agree that there could a potential overflow, & it must be updated to latest FullMath version. Considering this a valid medium issue.
Escalation accepted
Agree that there could a potential overflow, & it must be updated to latest FullMath version. Considering this a valid medium issue
This issue's escalations have been accepted!
Contestants' payouts and scores will be updated according to the changes made on this issue.
Escalation accepted
Agree that there could a potential overflow, & it must be updated to latest FullMath version. Considering this a valid medium issue.
This issue's escalations have been accepted!
Contestants' payouts and scores will be updated according to the changes made on this issue.
Fix looks good. Library has been updated
DecorativePineapple
medium
The
FullMath
library is unable to handle intermediate overflows due to overflow that's desired but never reachedSummary
The
FullMath
library doesn't correctly handle the case when an intermediate value overflows 256 bits. This happens because an overflow is desired in this case but it's never reached.Vulnerability Detail
The
FullMath
library was taken from Uniswap v3-core. However, the original solidity version that was used was < 0.8.0, meaning that the execution didn't revert wan an overflow was reached. This effectively means that when a phantom overflow (a multiplication and division where an intermediate value overflows 256 bits) occurs the execution will revert and the correct result won't be returned. The original library was designed in a way that could handle intermediate overflows. TheFullMath
library is used in theMathLib library
in order to format an 18-decimal number to a FixedPoint 96.Q96 number.Impact
The correct result isn't returned in this case and the execution gets reverted when a phantom overflows occurs.
Code Snippet
The
FullMath
library which doesn't use an unchecked block:Tool used
Manual Code Review
Recommendation
It is advised to put the entire function bodies of
mulDiv
andmulDivRoundingUp
in an unchecked block. A modified version of the originalFullmath
library that uses unchecked blocks to handle the overflow, can be found in the0.8
branch of the Uniswap v3-core repo.