BalancerPriceOracle::validatePrice uses stale TWAP
The validatePrice function in BalancerPriceOracle calculates a time-weighted average price from snapshot data that is only refreshed when updatePrice is called, and it never triggers that refresh itself. An attacker could exploit stale price data to either block legitimate buyback or liquidity operations or to pass validation with a manipulated spot price, potentially enabling sandwich attacks.
Submitted by santipu_, also found by LinKenji, M4v3r1ck, and PlayboiEvokid
autonolas-tokenomics/../contracts/oracles/BalancerPriceOracle.sol #L132
The BalancerPriceOracle contract’s validatePrice() function calculates the time-weighted average price using potentially outdated snapshot data because it never calls updatePrice() to refresh the oracle state before validation.
The root cause is that validatePrice() is a view function that only reads from storage without ensuring the underlying TWAP data has been recently updated current:
function validatePrice(uint256 slippage) external view returns (bool) {
// ...
// Compute time-weighted average price
>> uint256 timeWeightedAverage = (snapshot.cumulativePrice + (snapshot.averagePrice * elapsedTime)) /
((snapshot.cumulativePrice / snapshot.averagePrice) + elapsedTime);
uint256 tradePrice = getPrice();
// ...
}
The TWAP calculation uses snapshot.averagePrice and snapshot.cumulativePrice, which are only updated when updatePrice() is explicitly called. If no one has called updatePrice() for an extended period, the formula extrapolates from stale data:
snapshot.averagePricereflects the price from the last successful update, not the current market.snapshot.cumulativePricehasn’t accumulated price data for the intervening period.- The
elapsedTimegrows unboundedly, causing the staleaveragePriceto dominate the TWAP calculation.
For example, if the oracle was last updated 24 hours ago at price X, and the current spot price is Y (significantly different from X), the TWAP calculation will be heavily weighted toward the outdated price X. This produces a TWAP that doesn’t accurately reflect recent market activity.
The problem is exacerbated because updatePrice() is a public function with no incentive mechanism—there’s no guarantee anyone will call it regularly. The contracts that depend on validatePrice() (BuyBackBurner and LiquidityManagerOptimism) don’t call updatePrice() before validation either:
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");
// ...
}
function _checkTokensAndRemoveLiquidityV2(address[] memory tokens, bytes32 v2Pool)
internal
virtual
override
returns (uint256[] memory amounts)
{
// ...
// Apply slippage protection via V2 oracle: transform BPS into % as required by the function
>> if (!IOracle(oracleV2).validatePrice(maxSlippage / 100)) {
revert SlippageLimitBreached();
}
// ...
}
This leads to two failure modes:
- False negatives: Legitimate operations are blocked because the stale TWAP doesn’t reflect the current market price, causing
validatePrice()to return false even when the spot price is fair. - False positives: If the current spot price happens to align with the stale TWAP (potentially through manipulation),
validatePrice()returns true even though the TWAP is based on outdated data and doesn’t represent actual recent market activity.
Given that validatePrice() is the core protection mechanism for both BuyBackBurner and LiquidityManagerOptimism:
- BuyBackBurner: Buyback operations may be incorrectly blocked or incorrectly allowed based on stale price data, enabling either DoS or sandwich attacks.
- LiquidityManagerOptimism: Liquidity withdrawals face the same risk—legitimate withdrawals blocked, or manipulated withdrawals allowed through.
So the impact on both contracts are either a DoS under normal conditions, or allowed price manipulation that causes a loss of funds for the protocol due to sandwich attacks.
Recommended mitigation steps
There are different approaches to mitigate this issue:
- Modify
validatePrice()to callupdatePrice()internally before performing the TWAP calculation. This requires changingvalidatePrice()from a view function to a state-modifying function. - Update the functions calling
validatePrice()so they always callupdatePrice(). - Update
validatePrice()so that it returns false if the TWAP hasn’t been updated in a long time.
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 validatePrice view function reads stored TWAP snapshot values without ensuring they have been refreshed by updatePrice, so elapsed time grows and stale averagePrice dominates the calculation.
Impact
Legitimate buyback or liquidity operations could be blocked (DoS), or manipulated operations could pass validation and suffer sandwich attacks causing loss of funds.
What the attacker needed
UpdatePrice not called for an extended period; attacker can influence spot price
The fix
validatePrice should refresh the TWAP before validating, or callers should call updatePrice first, or validatePrice should return false when the TWAP is stale.
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 tells other contracts what an asset is worth. This oracle uses an average price that only updates when someone explicitly calls a refresh function, but the check that uses the average never calls refresh first. So the check can rely on an old average that no longer matches the market, which can wrongly block fair trades or wrongly allow unfair ones.