Skip to content
Audit finding Advanced

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 Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-10-aloe-judging/issues/85

Found by

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:

https://github.com/aloelabs/aloe-ii/blob/c71e7b0cfdec830b1f054486dfe9d58ce407c7a4/core/src/libraries/Oracle.sol#L196

Solidity
liqCumL + uint160(((liqCumR - liqCumL) * delta) / denom)

The calculation is done in an unchecked block. liqCumR and liqCumL have type uint160.
delta and denom have type uint56.

Let's compare this to the Uniswap V3 code.

https://github.com/Uniswap/v3-core/blob/d8b1c635c275d2a9450bd6a78f3fa2484fef73eb/contracts/libraries/Oracle.sol#L279-L284

Solidity
beforeOrAt.secondsPerLiquidityCumulativeX128 +
    uint160(
        (uint256(
            atOrAfter.secondsPerLiquidityCumulativeX128 - beforeOrAt.secondsPerLiquidityCumulativeX128
        ) * targetDelta) / observationTimeDelta
    )

The result of atOrAfter.secondsPerLiquidityCumulativeX128 - beforeOrAt.secondsPerLiquidityCumulativeX128 is cast to uint256.

That's because multiplying the result by targetDelta can overflow the uint160 type.

The maximum value of uint160 is roughly 1.5e48.

delta is simply the time difference between timeL and target in seconds.

The secondsPerLiquidityCumulative values are accumulators that are calculated as follows: https://github.com/Uniswap/v3-core/blob/d8b1c635c275d2a9450bd6a78f3fa2484fef73eb/contracts/libraries/Oracle.sol#L41-L42

Solidity
secondsPerLiquidityCumulativeX128: last.secondsPerLiquidityCumulativeX128 +
    ((uint160(delta) << 128) / (liquidity > 0 ? liquidity : 1)),

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

Text
(liqCumR - liqCumL) * delta = 3.4e38 * 86400 * 86400 > 1.5e48`

Impact

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.

Code Snippet

https://github.com/aloelabs/aloe-ii/blob/c71e7b0cfdec830b1f054486dfe9d58ce407c7a4/core/src/libraries/Oracle.sol#L196

https://github.com/Uniswap/v3-core/blob/d8b1c635c275d2a9450bd6a78f3fa2484fef73eb/contracts/libraries/Oracle.sol#L279-L284

Tool used

Manual Review

Recommendation

Perform the same cast to uint256 that Uniswap V3 performs:

Solidity
liqCumL + uint160((uint256(liqCumR - liqCumL) * delta) / denom)

Discussion

sherlock-admin2

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):

Solidity
secondsPerLiquidityCumulativeX128: last.secondsPerLiquidityCumulativeX128 +
    ((uint160(delta) << 128) / (liquidity > 0 ? liquidity : 1)),
...
(liqCumR - liqCumL) * delta = 3.4e38 * 86400 * 86400 ~= 2.5e48 > 1.5e48`

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):

Solidity
secondsPerLiquidityCumulativeX128: last.secondsPerLiquidityCumulativeX128 +
    ((uint160(delta) << 128) / (liquidity > 0 ? liquidity : 1)),
...
(liqCumR - liqCumL) * delta = 3.4e38 * 86400 * 86400 ~= 2.5e48 > 1.5e48`

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.

Czar102

Result: Medium Unique

All parties agree on the above result.

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

haydenshively

Fixed in https://github.com/aloelabs/aloe-ii/pull/208

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.

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.

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.

More by roguereddwarf

View all

12 more disclosed reports, best first.

More from Aloe

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.