BalancerStrategy.sol : _withdraw withdraws insufficient tokens
The BalancerStrategy contract's _withdraw function scales the requested withdrawal amount by the pool's price per share before calling _vaultWithdraw, which then withdraws only that scaled-down amount. This can leave the contract with insufficient WETH, causing the subsequent balance check to revert and blocking withdrawals.
carrotsmuggler Follow with kaden, n1punp and 1 other
Originally disclosed on Code4rena
Submitted by carrotsmuggler, also found by kaden, n1punp, and chaduke
The funciton _withdraw in the balancer strategy contract is called during withdraw operations to withdraw WETH from the balancer pool. The function calculats the amount to withdraw, and then calls _vaultWithdraw function.
if (amount > queued) {
uint256 pricePerShare = pool.getRate();
uint256 decimals = IStrictERC20(address(pool)).decimals();
uint256 toWithdraw = (((amount - queued) * (10 ** decimals)) /
pricePerShare);
_vaultWithdraw(toWithdraw);
}
The function _vaultWithdraw submits an exit request with the following userData.
exitRequest.userData = abi.encode(
2,
exitRequest.minAmountsOut,
pool.balanceOf(address(this))
);
A value of 2 here corresponds to specifying the exact number of tokens coming out of the contract. Thus the function _vaultWithdraw will withdraw the exact number of tokens passed to it in its parameter.
The issue however is that the function _vaultWithdraw is not called with the amount of tokens needed to be withdrawn, it is called by the amount scaled down by pricePerShare. Thus if the actual withdrawn amount is less the amounts the user actually wanted. This causes a revert in the next step.
require(
amount <= wrappedNative.balanceOf(address(this)),
"BalancerStrategy: not enough"
);
Since an insuffucient amount of tokens are withdrawn, this step will revert if there arent enough spare tokens in the contract. Since the contract incorrectly scales doen the withdraw amount and causes a revert, this is classified as a high severity issue.
Proof of Concept
The following exercise shows that passing the same exitRequest data to the balancerPool actually extracts the exact number of tokens as specified in minamountsOut.
A position is created on optimism’s weth-reth pool. The userData is generated using the following code.
```solidity
function temp() external pure returns(bytes memory){
uint256[] memory amts = new uint256[](2);
amts[0] = 500;
amts[1] = 0;
uint256 max = 20170422329691;
return(abi.encode(2,amts,max));
}
Min amount out of WETH is set to 500 wei. The exitRequestis then constructed as follows with the userData from above.
{
"assets": [
"0x4200000000000000000000000000000000000006",
"0x9bcef72be871e61ed4fbbc7630889bee758eb81d"
],
"minAmountsOut": ["500", "0"],
"toInternalBalance": false,
"userData": "0x00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000012584adba15b000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001f40000000000000000000000000000000000000000000000000000000000000000"
}
This is an exit request of type 2, which specifies the exact amount of tokens to be withdrawn. This transaction was then run on tenderly to check how many tokens are withdrawn. From the screenshot here from tenderly we can see only 500 wei of WETH is withdrawn.
This proves that the _vaultWithdraw function withdraws the exact amount of tokens passed to it as a parameter. Since the passed parameter is scaled down by pricePerShare, this leads to an insufficient amount withdrawn, and eventually a revert.
Tools Used
Tenderly
Recommended Mitigation Steps
Pass the amount to be withdrawn without scaling it down by pricePerShare.
0xRektora (Tapioca) confirmed via duplicate issue 51
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 withdrawal amount is incorrectly scaled down by the pool's price per share before being passed to _vaultWithdraw, which expects the exact token amount to withdraw.
What the attacker needed
Contract holds insufficient spare WETH; user initiates a withdrawal
The fix
The amount passed to _vaultWithdraw should not be scaled down by pricePerShare.
How it was found
- Call the withdraw function on the BalancerStrategy contract.
- The _withdraw function calculates the amount to withdraw and scales it down by pricePerShare.
- It calls _vaultWithdraw with the scaled-down amount, which withdraws only that many tokens.
- The contract then checks its WETH balance against the requested amount and reverts if insufficient.
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 smart contract lets users withdraw their funds from a pool. The code mistakenly divides the amount to withdraw by a price ratio, so it asks the pool for less money than it should. When the contract then checks it has enough to pay the user, it fails and the withdrawal stops.
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.