Submitted by Egis_Security
This is one of the edge cases in getRatiosFromPriceLiquidity:
The range where the price can be is very large, between 0.27702e6 (highPrice) and 0.001083e6 (lowPrice). If we are closer to the lowPrice, then pd.currentPrice is set to pd.lutData.lowPrice:
In this case, the current price is much smaller than the target price and because of this, updateReserve will have to do a very large correction of the reserve in order to converge on the target price.
Because of these large corrections, the reserve will become so small, that the next time the reserve has to be updated, the amount that it has to be reduced by will be larger than the reserve itself, resulting in a panic underflow and bricking the function.
This revert happens because of two reasons:
Its important to note, that even if lowPriceJ is significantly reduced, the function wont revert, but it will never converge on a price and this can only be fixed by reducing the gap between highPrice and lowPrice effectively reducing the range for that specific price.
We wanted to showcase both issues and that even if the underflow (lowPriceJ issue) is resolved that the estimated ranges still are too large, thus making the price impossible to converge.
The below tests showcase both the original code (underflow) and how reducing lowPriceJ doesnt fix the real problem.
Test case for underflow: paste the following inside BeanstalkStable2LiquidityTest and run forge test --match-contract BeanstalkStable2LiquidityTest --mt test_calcReserveAtRatioLiquiditExtreme -vvvv:
Logs:
As you can see, the code attempts massive corrections to the reserve in order to achieve convergence, but the amount to decrease the reserve is too large and the function underflows.
Test case when lowering lowPriceJ: run the same test as before, but change lowPriceJ to 10e18, for example purposes:
Logs:
The logs are compacted as they are very large, but the idea is this:
Foundry
To completely fix both issues, lowPriceJ must be lowered and the estimated range must be narrowed down.
DoS
Brean0 (Basin) confirmed
0xsomeone (judge) decreased severity to Low and commented:
deth (warden) commented:
nickkatsios (Basin) commented:
0xsomeone (judge) increased severity to Medium and commented:
deth (warden) commented:
0xsomeone (judge) commented:
For this audit, 3 reports were submitted by wardens detailing low risk and non-critical issues. The report highlighted below by Rhaydden received the top score from the judge.
The following wardens also submitted reports: ZanyBonzy and 0xAadi.
