SwapAction::getSwapToken will return wrong swap token for balancer EXACT_OUT swaps
The getSwapToken helper in SwapAction always returns the last asset in the Balancer asset array, which is only correct for EXACT_IN swaps. For EXACT_OUT swaps the output token is the first asset, so the function reports the wrong token.
Submitted by 0xAlix2
As known, when doing a Balancer EXACT_OUT batch swap, assets should be passed in reverse order, this is thoroughly documented here.
Swapping in USDC for an exact amount out of BAL
swapType = `EXACT_OUT` and `assets` = [BAL, WETH, DAI, USDC]:
However, in SwapAction::getSwapToken, for Balancer swaps it always returns the last asset in the assets array, which is correct for EXACT_IN but wrong for EXACT_OUT, where it should be the first asset in the assets array.
Proof of Concept
function test_wrongSwapTokenReturned() public {
uint256 amount = 200 ether;
deal(address(token), user, amount);
deal(address(underlyingToken), user, amount);
address[] memory assets = new address[](2);
assets[0] = address(underlyingToken);
assets[1] = address(token);
SwapParams memory swapParams = SwapParams({
swapProtocol: SwapProtocol.BALANCER,
swapType: SwapType.EXACT_OUT,
assetIn: address(token),
amount: 1 ether,
limit: 2 ether,
recipient: user,
deadline: block.timestamp,
args: abi.encode(weightedPoolIdArray, assets)
});
vm.startPrank(user);
token.approve(address(swapAction), amount);
underlyingToken.approve(address(swapAction), amount);
uint256 tokenBalanceBefore = token.balanceOf(address(user));
uint256 underlyingTokenBalanceBefore = underlyingToken.balanceOf(address(user));
// Swap token for underlying token, using EXACT_OUT
swapAction.transferAndSwap(user, emptyPermitParams, swapParams);
uint256 tokenBalanceAfter = token.balanceOf(address(user));
uint256 underlyingTokenBalanceAfter = underlyingToken.balanceOf(address(user));
// Verify that the out token is the underlying token
assertLt(tokenBalanceAfter, tokenBalanceBefore);
assertGt(underlyingTokenBalanceAfter, underlyingTokenBalanceBefore);
// `getSwapToken` returns the out token as `token` which is wrong
assertEq(swapAction.getSwapToken(swapParams), address(token));
vm.stopPrank();
}
Recommended Mitigation Steps
Add the following in SwapAction::getSwapToken:
if (swapParams.swapType == SwapType.EXACT_OUT) token = primarySwapPath[0];
else token = primarySwapPath[primarySwapPath.length - 1];
Assessed type
Error
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 helper assumes a single asset ordering for all Balancer swap types and does not branch on EXACT_OUT, where assets are supplied in reverse.
Impact
Any caller relying on getSwapToken for a Balancer EXACT_OUT swap receives the wrong token address, which could mislead downstream logic that uses that value.
What the attacker needed
Balancer EXACT_OUT swap used
The fix
The helper should return the first asset for EXACT_OUT swaps and the last asset otherwise; the report was confirmed by the project.
How it was found
- Set up a Balancer EXACT_OUT swap with the assets array in reverse order.
- Call transferAndSwap with those swap parameters.
- Observe the balances change as expected for the swap.
- Call getSwapToken and see it returns the input token rather than the output token.
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 swap helper needs to know which token comes out of a trade. Balancer lists the tokens in reverse order for one kind of swap (exact output), so picking the last entry gives the wrong answer. The code should pick the first entry for that swap type instead.
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.