Non terminal tokens received via Balancer batchSwap are not accounted for
The BalancerController contract only recorded the final token from a Balancer batchSwap, ignoring intermediate tokens returned to the user. An attacker could exploit this to have intermediate tokens not counted in their account balance, potentially leading to premature liquidation.
Source: https://github.com/sherlock-audit/2022-11-sentiment-judging/issues/6
Found by
Bahurum, obront
Summary
Balancer's batchSwap() function is able to return multiple tokens along the execution path, but BalancerController.sol only accounts for receipt of the final token in the swap. The result is that other tokens received from a user's trade will not be registered with the protocol or included in their account.
Vulnerability Detail
Balancer's batchSwap() functionality takes in an array of BatchSwapSteps, each of which dictates a pool to use for the swap, assets to swap to and from, an amount, and some additional user data (which is currently unused by Balancer).
For users who would like all their funds to move from step to step (swapping all of their input token from their first step, for the maximum amount of the output token from the final step), Balancer provides the helpful option to set amount = 0 for all steps after the first, and it will automatically use the maximum amount.
Alternatively, users are able to provide their own amounts in each step. Any amount returned from a step that is not used for the next step is sent to the user.
Here is a simple example:
- Step 1: Trade 400 BAL for max WETH (returns ~1.7)
- Step 2: Trade 1 WETH for max USDC (returns ~1575)
- Result: 400 BAL is inputted, and both 0.7 WETH and 1575 USDC are returned
Here is a gist with a standalone Foundry test that can be run to show this behavior.
As the canBatchSwap() function is currently implemented, only the final asset (in this case, USDC) would be returned in the tokensIn[] array. While the user's WETH would be transferred to their account, it would not be registered with the protocol to count towards their account balance or interact with.
Impact
If a user uses Balancer's batchSwap() functionality with returned intermediate tokens that are not in the account's asset list already, the user may get liquidated sooner than expected as RiskEngine.sol:_getBalance() only counts in the assets in the assets list.
Code Snippet
Tool used
Manual Review, Foundry
Recommendation
Balancer's batchSwap() function returns int256[] memory assetDeltas, which provide details on the final amounts of each asset that are inputted and outputted from the function.
Rather than try to manually calculate these values, the best option is likely the following:
- Use an in-line assembly block to
STATICCALLBalancer'scanBatchSwap()to return the asset deltas - Any positive number in
assetDeltasmeans the token belongs intokensOut - Any negative number in
assetDeltasmeans the token belongs intokensIn
Discussion
r0ohafza
PR: https://github.com/sentimentxyz/controller/pull/49
zobront
Confirmed. Didn't follow recommendation above, but blocked all trades with intermediate amounts > 0, so problem is solved.
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 controller only accounted for the final token in a batch swap, ignoring intermediate tokens returned to the user.
Impact
A user could be liquidated sooner than expected because intermediate tokens are not counted in their balance.
What the attacker needed
User uses batchSwap with intermediate tokens
The fix
The issue was fixed by blocking all trades with intermediate amounts greater than zero.
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.
When you swap tokens using a service like Balancer, you might get back more than one type of token. The protocol only kept track of the last token, so the others were invisible to its balance checks. This could cause the system to think you have less than you do, possibly triggering a liquidation.
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.