BalancerOracle::update() can return a stale price
The BalancerOracle contract's update function sets the safe price to the current price before recalculating a new price, so when the update window has passed, it returns a stale price from the last update. An attacker could potentially exploit this stale price to manipulate the oracle's output.
4B Follow with Bauchibred and Evo
Originally disclosed on Code4rena
Submitted by 4B, also found by Bauchibred and Evo
Whenever block.timestamp - lastUpdate > updateWaitWindow and needs to update the price, it will return a stale price because it will fetch the price from the lastUpdate not the currentUpdate.
Proof of Concept
In the update() function these are the lines we’ll find:
//@audit let's say price is out of date and its getting updated won't it get the price from one hour ago?
//problem is it uses a current timestamp but with an hour old price
if (block.timestamp - lastUpdate < updateWaitWindow) revert BalancerOracle__update_InUpdateWaitWindow();
// update the safe price first
safePrice = safePrice_ = currentPrice;
lastUpdate = block.timestamp;
uint256[] memory weights = IWeightedPool(pool).getNormalizedWeights();
uint256 totalSupply = IWeightedPool(pool).totalSupply();
uint256 totalPi = WAD;
uint256[] memory prices = new uint256[](weights.length);
// update balances in 18 decimals
for (uint256 i = 0; i < weights.length; i++) {
// reverts if the price is invalid or stale
prices[i] = _getTokenPrice(i);
uint256 val = wdiv(prices[i], weights[i]);
uint256 indivPi = uint256(wpow(int256(val), int256(weights[i])));
totalPi = wmul(totalPi, indivPi);
}
currentPrice = wdiv(wmul(totalPi, IWeightedPool(pool).getInvariant()), totalSupply);
From the code, we can see that the currentPrice is the last thing updated.
Whenever the updateWindow reaches or passes for us to fetch a new price, the safePrice is updated first, which is the value from the lastUpdate which is “stale”.
It can be argued that its a design decision meaning the updateWindow is just time it needs to fetch a new price, but it doesn’t mean the price is old. However, the updateWindow can be passed and not updated right after meaning the price is two times back because it wasn’t updated right away.
Recommended Mitigation Steps
Revisit the logic to be able to fetch fresh price whenever there need to be a new price fetched.
Assessed type
Oracle
0xtj24 (LoopFi) acknowledged and commented:
That logic updates the price only after a certain
updateWaitWindowhas passed, storing the oldest safe price. If not updated with the keeper it will return stale price depending on thestalePeriod. This is an expected behaviour.
Since
updateWaitWindowis immutable, it can’t be changed. Therefore, keeping the severity to Medium.
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 update function assigns the safe price from the current price before recalculating the new price, so the safe price remains stale until the next update.
Impact
The oracle may return a stale price, which could be exploited if the price is used in financial logic, though the team considers it expected behaviour.
What the attacker needed
None
The fix
The logic should be revisited to fetch a fresh price whenever a new price needs to be fetched.
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.
An oracle is a service that provides external data, like prices, to a smart contract. This report says the oracle sometimes gives an old price instead of a fresh one, which could be used to trick the contract.