Oracle.sol: observe function has overflow risk and should cast to uint256 like Uniswap V3 does
The Oracle.observe function in Aloe II lacks a uint256 cast when computing [redacted], risking intermediate overflow when liquidity is extremely low and observations are far apart. This could corrupt implied volatility calculations, leading to incorrect LTV ratios and potential bad debt.
roguereddwarf
The Oracle.observe function basically uses the same math from the Uniswap V3 code to search for observations.
In comparison to Uniswap V3, the Oracle.observe function takes a seed such that the runtime of the function can be decreased by calculating the seed off-chain to act as a hint for finding the observation.
In the process of copying the Uniswap V3 code, a uint256 cast has been forgotten which introduces a risk of intermediate overflow in the Oracle.observe function.
Thereby the secondsPerLiquidityCumulativeX128 return value can be wrong which can corrupt the implied volatility (ÌV) calculation.
Vulnerability Detail
Looking at the Oracle.observe function, the secondsPerLiquidityCumulativeX128 return value is calculated as follows:
If liquidity is very low and the time difference between observations is very big (hours to days), this can lead to the intermediate overflow in the Oracle library, such that the secondsPerLiquidityCumulative is much smaller than it should be.
The lowest value for the above division is 1. In that case the accumulator grows by 2^128 (~3.4e38) every second.
If observations are apart 24 hours (86400 seconds), this can lead to an overflow:
Assume for simplicity target - timeL = timeR - timeL
The corrupted return value affects the Volatility library. Specifically, the IV calculation.
This can lead to wrong IV updates and LTV ratios that do not reflect the true IV, making the application more prone to bad debt or reducing capital efficiency.
2 comment(s) were left on this issue during the judging contest.
tsvetanovv commented:
I don't think this is problem because liqCumR and liqCumL are uint160
MohammedRizwan commented:
valid
haydenshively
Valid high, will fix
panprog
Escalate
This should be a valid medium, not high, because while the situation is possible, it can only happen when liquidity is extremely low (basically no liquidity). The example calculation assumes minimum liquidity value (0 or 1):
Even with such a low liquidity value the overflow happens just barely. Even a value of 2 for liquidity already won't overflow. If the pool has 0 liquidity (or 1, 2 or even 100 - even for token with 6 decimals that's basically empty pool) for over a day - nobody should/will really use it. Any semi-active pool which will actually be used will have much higher liquidity at all times.
So the issue describes a very edge case which most probably won't ever happen in real life.
The reporter probably also understands it and set his reported issue severity to medium, not high.
sherlock-admin2
Escalate
This should be a valid medium, not high, because while the situation is possible, it can only happen when liquidity is extremely low (basically no liquidity). The example calculation assumes minimum liquidity value (0 or 1):
Even with such a low liquidity value the overflow happens just barely. Even a value of 2 for liquidity already won't overflow. If the pool has 0 liquidity (or 1, 2 or even 100 - even for token with 6 decimals that's basically empty pool) for over a day - nobody should/will really use it. Any semi-active pool which will actually be used will have much higher liquidity at all times.
So the issue describes a very edge case which most probably won't ever happen in real life.
The reporter probably also understands it and set his reported issue severity to medium, not high.
You've created a valid escalation!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
roguereddwarf
@panprog
I agree with the escalation. I intentionally reported this as Medium due to the Low likelihood.
Trumpero
Planning to accept escalation and downgrade this issue to medium because of the low likelihood.
The original report as published on Sherlock, by roguereddwarf. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the multiplication of a uint160 difference by a uint56 delta is performed without casting to uint256, causing an overflow when the product exceeds the maximum uint160 value.
Impact
The corrupted return value can lead to wrong implied volatility updates and LTV ratios that do not reflect the true IV, making the application more prone to bad debt or reducing capital efficiency; however, this only occurs when liquidity is extremely low (e.g., 0 or 1) and observations are far apart, so the likelihood is low.
What the attacker needed
Liquidity extremely low (0 or 1); observations far apart (hours to days)
The fix
The code was fixed by adding the missing uint256 cast, as confirmed in the pull request.
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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
This report is about a smart contract bug where a calculation can overflow because a number is stored in a type that is too small. When liquidity is very low and time gaps are large, the result becomes wrong, which can mess up risk calculations and potentially cause financial losses.