Skip to content
Business logic Audit finding Intermediate

cumulativePrice is corrupted when price updates are rejected

The BalancerPriceOracle contract updates cumulativePrice before checking if the price deviation exceeds maxSlippage. When the slippage check fails, the function returns false but cumulativePrice has already been permanently modified, corrupting the TWAP calculation.

santipu_ Follow with coinsspor, rox_k and 4 others

Originally disclosed on Code4rena

Submitted by santipu_, also found by coinsspor, rox_k, Silvermist, StrangerMontana, sunnyshrma, and taronsung

autonolas-tokenomics/../contracts/oracles/BalancerPriceOracle.sol#L109

The BalancerPriceOracle contract updates cumulativePrice before checking whether the price deviation exceeds maxSlippage. When the slippage check fails and the update is rejected, the function returns false but the cumulativePrice has already been permanently modified. This corrupts the TWAP calculation, causing it to diverge from the expected value.

The root cause is the ordering of operations in updatePrice():

    function updatePrice() public returns (bool) {
        // ...

        // Update cumulative price with the previous average over the elapsed time
>>      snapshot.cumulativePrice += snapshot.averagePrice * elapsedTime;

        // Update the average price to reflect the current price
        uint256 averagePrice = (snapshot.cumulativePrice + (currentPrice * elapsedTime)) /
            ((snapshot.cumulativePrice / snapshot.averagePrice) + elapsedTime);

        // Check if price deviation is too high
        if (currentPrice < averagePrice - (averagePrice * maxSlippage / 100) ||
            currentPrice > averagePrice + (averagePrice * maxSlippage / 100))
        {
>>          return false;
        }

        // ...
    }

When the function returns false:

  • snapshot.cumulativePrice has been increased
  • snapshot.averagePrice remains unchanged (old value)
  • snapshot.lastUpdated remains unchanged (old timestamp)

Example Scenario

  1. Initial state:

    • cumulativePrice = 10,000
    • averagePrice = 100
    • lastUpdated = T₀
  2. After 100 seconds, someone calls updatePrice() with a price that exceeds slippage:

    • elapsedTime = 100
    • currentPrice = 200 (exceeds maxSlippage, let’s say 10%)
    • cumulativePrice = 10,000 + (100 × 100) = 20,000 ✗ (modified!)
    • Slippage check fails → returns false
    • averagePrice stays at 100
    • lastUpdated stays at T₀
  3. After another 100 seconds, legitimate update at price 105:

    • elapsedTime = 200 (time since T₀, not 100!)
    • cumulativePrice = 20,000 + (100 × 200) = 40,000

The problem: The cumulativePrice is now 40,000, but it should only be 30,000 if the rejected update hadn’t corrupted it:

  • Correct: 10,000 + (100 × 200) = 30,000
  • Actual: 20,000 + (100 × 200) = 40,000

The cumulative price has an extra 10,000 from the rejected update. This error persists and affects all future TWAP calculations.

Impact

The corrupted cumulativePrice causes validatePrice() to compute an incorrect TWAP:

  1. Denial of Service: The skewed TWAP may fall outside the acceptable slippage range from the current spot price, causing validatePrice() to incorrectly return false for legitimate operations.
  2. Sandwich Attacks: When the corrupted TWAP happens to align with a manipulated spot price, attackers can exploit BuyBackBurner and LiquidityManagerOptimism by sandwiching transactions that should have been rejected.

The severity compounds over time as multiple rejected updates each add erroneous values to cumulativePrice.

Recommended mitigation steps

Move the slippage check before updating cumulativePrice, or use a local variable for the calculation and only persist to storage after validation passes.


The original report as published on Code4rena, by santipu_. 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 function updates cumulativePrice before performing the slippage check, so a rejected update still permanently modifies state.

Impact

The corrupted cumulativePrice causes validatePrice() to compute an incorrect TWAP, potentially leading to denial of service for legitimate operations and enabling sandwich attacks when the corrupted TWAP aligns with a manipulated spot price.

What the attacker needed

Ability to call updatePrice(); price deviation exceeds maxSlippage

The fix

Move the slippage check before updating cumulativePrice, or use a local variable for the calculation and only persist to storage after validation passes.

How it was found

  1. Initial state: cumulativePrice = 10,000, averagePrice = 100, lastUpdated = T₀.
  2. After 100 seconds, call updatePrice() with a price that exceeds slippage (e.g., 200).
  3. cumulativePrice becomes 20,000, but slippage check fails and function returns false.
  4. averagePrice and lastUpdated remain unchanged.
  5. After another 100 seconds, a legitimate update at price 105 occurs.
  6. elapsedTime is 200 seconds, cumulativePrice becomes 40,000 instead of the correct 30,000.

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 smart contract that provides price data to other contracts. This oracle keeps a running total (cumulativePrice) to calculate an average price over time. The bug is that it adds to this total before checking if the new price is too different from the average. If the check fails, the total is still changed, so future averages are wrong.

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 by santipu_

View all

13 more disclosed reports, best first.

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.