`BalancerPairOracle` can be manipulated using read-only reentrancy
The BalancerPairOracle contract calculates prices using Balancer pool token balances and total supply without checking the Balancer Vault's reentrancy guard. An attacker can exploit a read-only reentrancy during a pool join to make the oracle return a manipulated, lower price, causing premature liquidation of user positions.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-04-blueberry-judging/issues/141
Found by
cuthalion0x
Summary
BalancerPairOracle.getPrice makes an external call to BalancerVault.getPoolTokens without checking the Balancer Vault's reentrancy guard. As a result, the oracle can be trivially manipulated to liquidate user positions prematurely.
Vulnerability Detail
In February, the Balancer team disclosed a read-only reentrancy vulnerability in the Balancer Vault. The detailed disclosure can be found here. In short, all Balancer pools are susceptible to manipulation of their external queries, and all integrations must now take an extra step of precaution when consuming data. Via reentrancy, an attacker can force token balances and BPT supply to be out of sync, creating very inaccurate BPT prices.
Some protocols, such as Sentiment, remained unaware of this issue for a few months and were later hacked as a result.
BalancerPairOracle.getPrice makes a price calculation of the form f(balances) / pool.totalSupply(), so it is clearly vulnerable to synchronization issues between the two data points. A rough outline of the attack might look like this:
AttackerContract.flashLoan() ->
// Borrow lots of tokens and trigger a callback.
SomeProtocol.flashLoan() ->
AttackerContract.exploit()
AttackerContract.exploit() ->
// Join a Balancer Pool using the borrowed tokens and send some ETH along with the call.
BalancerVault.joinPool() ->
// The Vault will return the excess ETH to the sender, which will reenter this contract.
// At this point in the execution, the BPT supply has been updated but the token balances have not.
AttackerContract.receive()
AttackerContract.receive() ->
// Liquidate a position using the same Balancer Pool as collateral.
BlueBerryBank.liquidate() ->
// Call to the oracle to check the price.
BalancerPairOracle.getPrice() ->
// Query the token balances. At this point in the execution, these have not been updated (see above).
// So, the balances are still the same as before the start of the large pool join.
BalancerVaul.getPoolTokens()
// Query the BPT supply. At this point in the execution, the supply has already been updated (see above).
// So, it includes the latest large pool join, and as such the BPT supply has grown by a large amount.
BalancerPool.getTotalSupply()
// Now the price is computed using both balances and supply, and the result is much smaller than it should be.
price = f(balances) / pool.totalSupply()
// The position is liquidated under false pretenses.
Impact
Users choosing Balancer pool positions (such as Aura vaults) as collateral can be prematurely liquidated due to unreliable price data.
Code Snippet
Tool used
Manual Review
Recommendation
The Balancer team recommends utilizing their official library to safeguard queries such as Vault.getPoolTokens. However, the library makes a state-modifying call to the Balancer Vault, so it is not suitable for view functions such as BalancerPairOracle.getPrice. There are then two options:
- Invoke the library somewhere else. Perhaps insert a hook into critical system functions like
BlueBerryBank.liquidate. - Adapt a slightly different read-only solution that checks the Balancer Vault's reentrancy guard without actually entering.
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 oracle reads Balancer pool token balances and total supply without verifying that the Balancer Vault is not in the middle of an operation, allowing a read-only reentrancy to desynchronise the two values.
Impact
Users with Balancer pool positions as collateral could be prematurely liquidated due to unreliable price data.
What the attacker needed
Attacker can flash loan tokens; victim has a position using Balancer pool collateral; attacker can trigger a pool join with ETH
How it was found
- Attacker takes a flash loan to obtain a large amount of tokens.
- Attacker joins a Balancer pool with the borrowed tokens and sends ETH along with the call.
- The Balancer Vault returns excess ETH, triggering a callback that reenters the attacker contract.
- During the callback, the attacker calls BlueBerryBank.liquidate() on a position using the same Balancer pool as collateral.
- The liquidation calls BalancerPairOracle.getPrice(), which reads token balances (not yet updated) and total supply (already updated), producing a manipulated low price.
- The position is liquidated under false pretenses.
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 reentrancy attack happens when a contract calls another contract, and that call allows the attacker to run code before the first operation finishes. Here, the oracle reads two values (token balances and total supply) at different moments during a transaction, so they can be out of sync. This lets an attacker make the price look artificially low and trigger liquidations.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.