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.
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.cumulativePricehas been increasedsnapshot.averagePriceremains unchanged (old value)snapshot.lastUpdatedremains unchanged (old timestamp)
Example Scenario
-
Initial state:
cumulativePrice = 10,000averagePrice = 100lastUpdated = T₀
-
After 100 seconds, someone calls
updatePrice()with a price that exceeds slippage:elapsedTime = 100currentPrice = 200(exceedsmaxSlippage, let’s say 10%)cumulativePrice = 10,000 + (100 × 100) = 20,000✗ (modified!)- Slippage check fails → returns
false averagePricestays at 100lastUpdatedstays at T₀
-
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:
- 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. - Sandwich Attacks: When the corrupted TWAP happens to align with a manipulated spot price, attackers can exploit
BuyBackBurnerandLiquidityManagerOptimismby 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.
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
- Initial state: cumulativePrice = 10,000, averagePrice = 100, lastUpdated = T₀.
- After 100 seconds, call updatePrice() with a price that exceeds slippage (e.g., 200).
- cumulativePrice becomes 20,000, but slippage check fails and function returns false.
- averagePrice and lastUpdated remain unchanged.
- After another 100 seconds, a legitimate update at price 105 occurs.
- 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.