Skip to content
Smart contracts Audit finding Advanced

_getReferencePoolPriceX96() will show incorrect price for negative tick deltas in current implementation cause it doesn’t round up for them

The oracle's TWAP calculation rounded a negative tick delta toward zero instead of down, so the returned tick could be higher than the true time-weighted average. This could produce an incorrect reference price, potentially letting unhealthy loans appear healthy or affecting swap enablement in the automator.

Bauchibred Follow with Giorgio and kodyvim

Originally disclosed on Code4rena

Submitted by Bauchibred, also found by grearlake (1, 2), Giorgio, and kodyvim

Take a look here.

    function _getReferencePoolPriceX96(IUniswapV3Pool pool, uint32 twapSeconds) internal view returns (uint256) {
        uint160 sqrtPriceX96;
        // if twap seconds set to 0 just use pool price
        if (twapSeconds == 0) {
            (sqrtPriceX96,,,,,,) = pool.slot0();
        } else {
            uint32[] memory secondsAgos = new uint32[](2);
            secondsAgos[0] = 0; // from (before)
            secondsAgos[1] = twapSeconds; // from (before)
            (int56[] memory tickCumulatives,) = pool.observe(secondsAgos); // pool observe may fail when there is not enough history available (only use pool with enough history!)
            //@audit
            int24 tick = int24((tickCumulatives[0] - tickCumulatives[1]) / int56(uint56(twapSeconds)));
            sqrtPriceX96 = TickMath.getSqrtRatioAtTick(tick);
        }

        return FullMath.mulDiv(sqrtPriceX96, sqrtPriceX96, Q96);
    }

This function is used to calculate the reference pool price. It uses either the latest slot price or TWAP based on twapSeconds.

Now note that unlike the original uniswap implementation, here the delta of the tick cumulative is being calculated in a different manner, i.e protocol implements (tickCumulatives(0) - tickCumulatives(1) instead of tickCumulatives[1] - (tickCumulatives[0] which is because here, secondsAgos[0] = 0; and secondsAgos[1] = twapSeconds;; unlike in Uniswap OracleLibrary where secondsAgos[0] = secondsAgo; and secondsAgos[1] = 0;, so everything checks out and the tick deltas are calculated accurately, i.e in our case tickCumulativesDelta = tickCumulatives[0] - tickCumulatives[1].

The problem now is that in the case if our tickCumulativesDelta is negative, i.e int24(tickCumulatives[0] - tickCumulatives[1] < 0) , then the tick should be rounded down, as it’s done in theuniswap library.

But this is not being done and as a result, in the case if int24(tickCumulatives[0] - tickCumulatives[1]) is negative and (tickCumulatives[0] - tickCumulatives[1]) % secondsAgo != 0, then the returned tick will be bigger then it should be; which opens possibility for some price manipulations and arbitrage opportunities.

Impact

  1. In this case, if int24(tickCumulatives[0] - tickCumulatives[1]) is negative and ((tickCumulatives[0] - tickCumulatives[1]) % secondsAgo != 0, then returned tick will be bigger than it should be which places protocol wanting prices to be right not be able to achieve this goal. Note that whereas protocol in some cases relies on multiple sources of price, they still come down and end on weighing the differences between the prices and reverting if a certain limit is passed (MIN_PRICE_DIFFERENCE) between both the Chainlink price and Uniswap twap price.
  2. Now in the case where the implemented pricing mode is only TWAP, then the protocol would work with a flawed price since the returned price would be different than it really is; potentially leading to say, for example, some positions that should be liquidatable not being liquidated. Before liquidation, there is a check to see if the loan is healthy. Now this check queries the value of this asset via getValue() and if returned price is wrong then unhealthy loans could be pronounced as healthy and vice versa.
  3. Also, this indirectly curbs the access to functions like borrow(), transform() and decreaseLiquidityAndCollect(), since they all make a call to _requireLoanIsHealthy(), which would be unavailable due to it’s dependence on _checkLoanIsHealthy().
  4. This bug case causes the Automator’s _getTWAPTick() function to also return a wrong tick, which then leads to _hasMaxTWAPTickDifference() returning false data, since the difference would now be bigger eventually leading to wrongly disabling/enabling of swaps in AutoCompound.sol, whereas, it should be otherwise.

Note that for the second/third case, the call route to get to _getReferencePoolPriceX96() is: "_checkLoanIsHealthy() -> getValue() -> _getReferenceTokenPriceX96 -> _getTWAPPriceX96 -> _getReferencePoolPriceX96() " as can be seen here.

Tools Used

Recommended Mitigation Steps

Add this line: if (tickCumulatives[0] - tickCumulatives[1] < 0 && (tickCumulatives[0] - tickCumulatives[1]) % secondsAgo != 0) timeWeightedTick --;.

Assessed type

Uniswap

kalinbas (Revert) confirmed

Revert mitigated:

PR here - fixed calculation.

Status: Mitigation confirmed. Full details in reports from thank_you, b0g0 and ktg.


The original report as published on Code4rena, by Bauchibred. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 integer division of a negative tick cumulative delta truncates toward zero instead of rounding down, unlike Uniswap's OracleLibrary.

Impact

The incorrect tick could make unhealthy loans appear healthy and could wrongly enable or disable swaps in the automator, though the protocol sometimes cross-checks against Chainlink and reverts if the difference exceeds a limit.

What the attacker needed

TWAP pricing mode used; negative tick delta not evenly divisible

The fix

The calculation was fixed by rounding the tick down when the cumulative delta is negative and not evenly divisible by the TWAP period.

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.

A price oracle is a contract that reports an asset's price. This one averages prices over time by dividing a change in a tick value, but when that change is negative and does not divide evenly, the code rounds the wrong way. The result is a slightly too-high price, which can make a risky loan look safer than it is.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by Bauchibred

View all

22 more disclosed reports, best first.

More from Revert Lend

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.