BalancerPairOracle#getPrice will revert due to division by zero in some cases
The BalancerPairOracle's getPrice function calls computeFairReserves, which performs integer division resA / resB. When resA is smaller than resB, this division yields zero, and a subsequent division by zero causes a revert. An attacker could trigger this condition by ensuring the pool's token balances or decimals cause resA < resB, making the oracle and any dependent functions unusable.
0x52 Follow with nobody2018
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-05-blueberry-judging/issues/25
Found by
0x52, nobody2018
Summary
BalancerPairOracle#getPrice internally calls computeFairReserves, which returns fair reserve amounts given spot reserves, weights, and fair prices. When the parameter resA passed to computeFairReserves is smaller than resB, division by 0 will occur.
Vulnerability Detail
In BalancerPairOracle#getPrice, resA and resB passed to computeFairReserves are the balance of TokenA and TokenB of the pool respectively. It is common for the balance of TokenB to be greater than the balance of TokenA.
function computeFairReserves(
uint256 resA,
uint256 resB,
uint256 wA,
uint256 wB,
uint256 pxA,
uint256 pxB
) internal pure returns (uint256 fairResA, uint256 fairResB) {
...
//@audit r0 = 0 when resA < resB.
-> uint256 r0 = resA / resB;
uint256 r1 = (wA * pxB) / (wB * pxA);
// fairResA = resA * (r1 / r0) ^ wB
// fairResB = resB * (r0 / r1) ^ wA
if (r0 > r1) {
uint256 ratio = r1 / r0;
fairResA = resA * (ratio ** wB);
fairResB = resB / (ratio ** wA);
} else {
-> uint256 ratio = r0 / r1; // radio = 0 when r0 = 0
-> fairResA = resA / (ratio ** wB); // revert divided by 0
fairResB = resB * (ratio ** wA);
}
}
Another case is when the decimals of tokenA is smaller than the decimals of tokenB, such as usdc(e6)-weth(e18).
Impact
All functions that subcall BalancerPairOracle#getPrice will be affected.
Code Snippet
Tool used
Manual Review
Recommendation
--- a/blueberry-core/contracts/oracle/BalancerPairOracle.sol
+++ b/blueberry-core/contracts/oracle/BalancerPairOracle.sol
@@ -50,7 +50,7 @@ contract BalancerPairOracle is UsingBaseOracle, IBaseOracle {
// --> fairResA / r1^wB = constant product
// --> fairResA = resA^wA * resB^wB * r1^wB
// --> fairResA = resA * (resB/resA)^wB * r1^wB = resA * (r1/r0)^wB
- uint256 r0 = resA / resB;
+ uint256 r0 = resA * 10**(decimalsB) / resB;
uint256 r1 = (wA * pxB) / (wB * pxA);
// fairResA = resA * (r1 / r0) ^ wB
// fairResB = resB * (r0 / r1) ^ wA
Discussion
securitygrid
Escalate for 10 USDC This is a valid M/H. Please review Vulnerability Detail of this report, it describes 2 cases:
- resA and resB passed to computeFairReserves are the balance of TokenA and TokenB of the pool respectively. It is common for the balance of TokenB to be greater than the balance of TokenA.
- when the decimals of tokenA is smaller than the decimals of tokenB, such as usdc(e6)-weth(e18).
This issue is same root as #28. The impact described in #28 is only possible if the dp of the two tokens are different. But it is not completely correct, because when the dp of token0 is less than the dp of token1, a division by zero error occurs, such as token0(e6)-token1(e18). Merging the two reports is the best description.
sherlock-admin
Escalate for 10 USDC This is a valid M/H. Please review Vulnerability Detail of this report, it describes 2 cases:
- resA and resB passed to computeFairReserves are the balance of TokenA and TokenB of the pool respectively. It is common for the balance of TokenB to be greater than the balance of TokenA.
- when the decimals of tokenA is smaller than the decimals of tokenB, such as usdc(e6)-weth(e18).
This issue is same root as #28. The impact described in #28 is only possible if the dp of the two tokens are different. But it is not completely correct, because when the dp of token0 is less than the dp of token1, a division by zero error occurs, such as token0(e6)-token1(e18). Merging the two reports is the best description.
You've created a valid escalation for 10 USDC!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
IAm0x52
Agreed with escalation. This and #28 are dupes and this does a better job of describing the issue so it should be the main issue. Additionally given that the contract would become nonfunctional rather than return an incorrect price, I agree with the watson's original severity of medium.
hrishibhat
Escalation accepted
Valid medium Making this issue the main one and #28 a duplicate of this issue.
sherlock-admin
Escalation accepted
Valid medium Making this issue the main one and #28 a duplicate of this issue.
This issue's escalations have been accepted!
Contestants' payouts and scores will be updated according to the changes made on this issue.
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 integer division resA / resB truncates to zero when resA < resB, and the code does not handle this case before using the result as a divisor.
Impact
All functions that call BalancerPairOracle#getPrice will be affected, and the contract becomes nonfunctional rather than returning an incorrect price.
What the attacker needed
ResA < resB in the pool; or tokenA decimals < tokenB decimals
The fix
The division should be scaled by the token decimals to avoid truncation to zero, for example by multiplying resA by 10 raised to the power of decimalsB before dividing by resB.
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.
This is a smart contract bug where a calculation divides one number by another. If the first number is smaller, the result is zero, and later the code divides by that zero, which is not allowed and makes the function fail. So the price oracle stops working, and anything that relies on it also breaks.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.