User can trade away an asset using Balancer batchSwap without removing it from account
The BalancerController contract only records the first token in a batchSwap chain as tokensOut, ignoring tokens that are actually spent from the user's account. An attacker can set a later swap step's amount higher than the previous step's output, causing extra tokens to be traded away without the account's asset tracking being updated.
Source: https://github.com/sherlock-audit/2022-11-sentiment-judging/issues/7
Found by
obront
Summary
Balancer's batchSwap() function takes in an amount of tokens to trade at each step of the swap. This amount can actually exceed the amount returned from the previous step. Because BalancerController.sol uses only the first token in the chain as tokensOut, a user can abuse this functionality to empty their account of a token while bypassing the expected removeAsset calls.
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).
The expectation is that users will either leave amount = 0 (which will autofill the amount returned from the last step) or will set amount manually to some smaller value.
However, it is also possible to input an amount that is greater than the value returned by the previous step, if you own those tokens and you have already approved the Balancer contract to trade them.
Using this technique, a user could send their tokens to Balancer without having them flagged by Sentiment as tokensOut. In an extreme case, this would allow them to get rid of all their tokens, without their account being updated to reflect this.
Here is an example:
- A user has 400 BAL and 100 WETH
- They call
batchSwap()with the path BAL => WETH => USDC - For the first step, they set
amount = 400 - For the second step, they set
amount = [amount returned from step 1] + 100 - The result is that they will have traded all of their BAL and WETH into USDC, but only BAL will appear in
tokensOut
Impact
Users have the ability to use the batchSwap() functionality to throw off the accounting on their account, removing a token but keeping the token in their assets array and keeping hasAsset[token] = true.
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 STATICCALL Balancer's canBatchSwap() to return the asset deltas
- Any positive number in assetDeltas means the token belongs in tokensOut
- Any negative number in assetDeltas means the token belongs in tokensIn
Discussion
r0ohafza
PR: https://github.com/sentimentxyz/controller/pull/49
zobront
Confirmed fix. 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 contract only inspects the first token in the batchSwap path when determining tokensOut, rather than using Balancer's returned asset deltas to identify all tokens actually spent.
Impact
A user could remove a token from their account while the system still records it as an asset, throwing off the account's accounting.
What the attacker needed
A user account with multiple tokens; approval for Balancer to spend those tokens
The fix
The issue was fixed by blocking all trades where an intermediate amount is greater than zero, rather than adopting the suggested asset-delta approach.
How it was found
- A user has a balance of two tokens, for example 400 BAL and 100 WETH.
- They call batchSwap with a path that goes through both tokens, such as BAL to WETH to USDC.
- For the first step they set the amount to their full BAL balance.
- For the second step they set the amount to the output of step one plus their full WETH balance.
- The swap executes, trading away both BAL and WETH, but only BAL is recorded in tokensOut.
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 in a system that tracks which tokens a user has. When a user swaps tokens, the system is supposed to update its records to show which tokens left the account. The code only checked the first token in a multi-step swap, so a user could secretly trade away other tokens and the system would still think they owned them.
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.