Skip to content
Business logic Audit finding Intermediate

Price cumulative last is used inverted in Uniswap Oracle

The Uniswap price oracle used the wrong cumulative price feed for each token direction, so the computed time-weighted average price was the inverse of the intended OLAS price. This could let the slippage check pass when the real price had moved beyond the allowed limit, causing buybacks to trade at an incorrect price.

holtzzx Follow with 4lifemen, Aamir and 4 others

Originally disclosed on Code4rena

Submitted by holtzzx, also found by 4lifemen, Aamir, Cryptor, santipu_, testnate, and Varun_05

autonolas-tokenomics/../contracts/oracles/UniswapPriceOracle.sol #L62

Currently, in UniswapPriceOracle the function validatePrice() is:

    function validatePrice(uint256 slippage) external view returns (bool) {
        require(slippage <= maxSlippage, "Slippage overflow");

        // Compute time-weighted average price
        // Fetch the cumulative prices from the pair
        uint256 cumulativePriceLast;
        if (direction == 0) {
            cumulativePriceLast = IUniswapV2(pair).price1CumulativeLast(); // @audit appears to be inverted, ts should be price0cumulative if direction is 0
        } else {
            cumulativePriceLast = IUniswapV2(pair).price0CumulativeLast();
        }

        // Fetch the reserves and the last block timestamp
        (, , uint256 blockTimestampLast) = IUniswapV2(pair).getReserves();

        // Require at least one block since last update
        if (block.timestamp == blockTimestampLast) {
            return false;
        }
        uint256 elapsedTime = block.timestamp - blockTimestampLast;

        uint256 tradePrice = getPrice();

        // Calculate cumulative prices
        uint256 cumulativePrice = cumulativePriceLast + (tradePrice * elapsedTime);

        // Calculate the TWAP for OLAS in terms of native token
        uint256 timeWeightedAverage = (cumulativePrice - cumulativePriceLast) / elapsedTime;

        // Get the final derivation to compare with slippage
        // Final derivation value must be
        uint256 derivation = (tradePrice > timeWeightedAverage)
            ? ((tradePrice - timeWeightedAverage) * 1e16) / timeWeightedAverage
            : ((timeWeightedAverage - tradePrice) * 1e16) / timeWeightedAverage;

        return derivation <= slippage;
    }

The meanings of priceCumulativeLast is that, if:

price0CumulativeLast -> will return the price of token1 denominated in token0 price1CumulativeLast -> will return the price of token0 denominated in token1

In constructor, we build out the direction that will denominate who is token0 and who is token1 as follows:

    constructor(address _secondToken, uint256 _maxSlippage, address _pair) {
        pair = _pair;
        maxSlippage = _maxSlippage;

        // Get token direction
        address token0 =  IUniswapV2(pair).token0();
        if (token0 != _secondToken) {
            direction = 1;
        }
    }

The whole goal of the oracle is to give the price of OLAS in units of second token (This is because it will be used in BuyBack since we swap a given second token to OLAS).

If the direction is 0, which means the secondToken is the token0, then we will fetch price1CumulativeLast

  if (direction == 0) {
            cumulativePriceLast = IUniswapV2(pair).price1CumulativeLast(); // @audit appears to be inverted, ts should be price0cumulative if direction is 0

Which will return the price of token0 denominated in token1 (Price of second token denominated in olas, which is exactly the inverse of what we want).

When direction is 1, which means the second token is the token1, we will fetch price0CumulativeLast, which returns the price of token1 denominated in token0 (Price of second token denominated in olas again).

You can see in BuyBackBurner how we used the validatePrice function for instance:

 function _buyOLAS(address secondToken, uint256 secondTokenAmount) internal virtual returns (uint256 olasAmount) {
        // Get oracle address
        address poolOracle = mapV2Oracles[secondToken];

        // Check for zero address
        require(poolOracle != address(0), "Zero oracle address");

        // Apply slippage protection
        require(IOracle(poolOracle).validatePrice(maxSlippage), "Before swap slippage limit is breached");

        // Get current pool price
        uint256 previousPrice = IOracle(poolOracle).getPrice();

The pool oracle is of the second token (Not OLAS, so we need to get the price of OLAS denominated in the second token).

Impact

If the price moves from 2 -> 1 for instance, we will compute as it was 0.5 -> 1 which is a gain of 100%, given it is the inverse.

And therefore the slippage check may pass when it shouldn’t.

This will allow trading at a wrong price, so users could be charged more, or gain more depending on how the price diverges. (Buybacks in Uniswap V2 will trade at a price that is not quite the market price, it is the inverse).

Mitigation

  if (direction == 0) {
            cumulativePriceLast = IUniswapV2(pair).price0CumulativeLast(); // @audit appears to be inverted, ts should be price0cumulative if direction is 0
        } else {
            cumulativePriceLast = IUniswapV2(pair).price1CumulativeLast();
        }

Proof of Concept

View detailed Proof of Concept


The original report as published on Code4rena, by holtzzx. 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

The oracle selected price1CumulativeLast when direction was 0 and price0CumulativeLast when direction was 1, which is the opposite of the correct mapping for the desired OLAS-denominated price.

Impact

The slippage check may pass when it should not, so buybacks could trade at a price that is not the market price, potentially charging users more or giving them more depending on how the price diverges.

What the attacker needed

A Uniswap V2 pair with OLAS; a configured oracle for the second token

The fix

The oracle should read price0CumulativeLast when direction is 0 and price1CumulativeLast when direction is 1.

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 how much one token is worth in another. This one read the wrong price series from the Uniswap pair, so it reported the inverse of the real price. That meant a safety check on price movement could approve a trade that should have been blocked.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More from Olas

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.