PoolAction::_balancerExit returns wrong token out amount
The `_balancerExit` function returns the recipient's entire token balance instead of the amount received from the exit, causing an exaggerated return value. This can lead to reverts in `PositionAction4626::_onDecreaseLever` when the contract tries to send more tokens than it holds.
Users use PoolAction::exit to exit a Balancer pool position, it calls _balancerExit to do the job. It is expected to exit the pool and return the amount out of the token; however, it returns the whole recipient’s balance, without considering the case where the recipient is holding an amount of the same token from different sources. It’ll return an exaggerated amount rather than the amount out.
This will cause PositionAction4626::_onDecreaseLever to revert sometimes. In this case, the user is leveraging down, exiting a Balancer pool, and holds some tokenOut amount. In PositionAction::onCreditFlashLoan, withdrawnCollateral would be an unreal exaggerated amount, and the contract will try to send back residualAmount which will be greater than its balance.
Proof of Concept
Add the following test in src/test/integration/PoolAction.t.sol:
function test_wrongBalancerExitAmount() public {
uint256 amount = 10 ether;
deal(wstETH, user, amount * 2);
(uint8 v, bytes32 r, bytes32 s) = PermitMaker.getPermit2TransferFromSignature(
address(wstETH),
address(poolAction),
amount,
NONCE,
block.timestamp,
userPk
);
PermitParams[] memory permitParamsArray = new PermitParams[](3);
permitParamsArray[1] = PermitParams(ApprovalType.PERMIT2, amount, NONCE, block.timestamp, v, r, s);
address[] memory tokens = new address[](3);
tokens[0] = wstETH_bb_a_WETH_BPTl;
tokens[1] = wstETH;
tokens[2] = bbaweth;
uint256[] memory maxAmountsIn = new uint256[](3);
maxAmountsIn[0] = 0;
maxAmountsIn[1] = amount;
maxAmountsIn[2] = 0;
uint256[] memory tokensIn = new uint256[](2);
tokensIn[0] = amount;
tokensIn[1] = 0;
uint256[] memory minAmountsOut = new uint256[](3);
minAmountsOut[0] = 0;
minAmountsOut[1] = 0;
minAmountsOut[2] = 0;
vm.startPrank(user);
// Joins Balancer pool with 10 WSTETH
poolAction.transferAndJoin(
user,
permitParamsArray,
PoolActionParams(Protocol.BALANCER, 0, user, abi.encode(poolId, tokens, tokensIn, maxAmountsIn))
);
uint256 balancerLP = ERC20(wstETH_bb_a_WETH_BPTl).balanceOf(user);
// Receives 11 Balancer LP tokens
assertEq(balancerLP / 1 ether, 11);
// Balance is 10 WSTETH
assertEq(ERC20(wstETH).balanceOf(user), amount);
// Transfer Balancer LP tokens to PoolAction
ERC20(wstETH_bb_a_WETH_BPTl).transfer(address(poolAction), balancerLP);
// Exits the Balancer pool
uint256 amountOut = poolAction.exit(
PoolActionParams(
Protocol.BALANCER,
0,
user,
abi.encode(poolId, wstETH_bb_a_WETH_BPTl, balancerLP, 0, tokens, minAmountsOut)
)
);
// Amount returned by the function is 19 WSTETH instead of 9 WSTETH
assertEq(amountOut / 1 ether, 19);
// Balance is 19 WSTETH
assertEq(ERC20(wstETH).balanceOf(user) / 1 ether, 19);
}
Recommended Mitigation Steps
Instead of returning the whole recipient’s balance, return the difference between his balances before and after the exit.
Could you please adjust the PoC to support this claim in PJQA please?
This will cause PositionAction4626::_onDecreaseLever to revert sometimes. In this case, the user is leveraging down, exiting a Balancer pool, and holds some tokenOut amount. In PositionAction::onCreditFlashLoan, withdrawnCollateral would be an unreal exaggerated amount, and the contract will try to send back residualAmount which will be greater than it’s balance
@Koolex - The third point refers to another issue that is reported, Issue 241, it just bypasses that issue and makes sure it doesn’t revert because of that issue. Issue #241 just shows a more sophisticated mitigation.
The original report as published on Code4rena, by 0xAlix2. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The function returns the recipient's total balance of the output token instead of the difference between the balance before and after the exit.
Impact
This can cause `PositionAction4626::_onDecreaseLever` to revert sometimes, as the contract may try to send more tokens than it holds.
What the attacker needed
User holds tokens from other sources; User exits a Balancer pool
The fix
The fix is to return the difference between the recipient's balance before and after the exit, rather than the total balance.
How it was found
User joins a Balancer pool with 10 WSTETH, receiving LP tokens.
User transfers the LP tokens to the PoolAction contract.
User calls `poolAction.exit` to exit the Balancer pool.
The function returns 19 WSTETH instead of the expected 9 WSTETH, as shown by the test assertion.
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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When you exit a Balancer pool, the contract should tell you how many tokens you just received. Instead, it tells you your total balance of that token, which includes tokens you already had. This wrong number can cause later steps to fail because the contract thinks it has more tokens than it actually does.
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.