Skip to content
Business logic Audit finding Intermediate

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.

Bahurum Follow with obront

Originally disclosed on Sherlock

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

https://github.com/sherlock-audit/2022-11-sentiment/blob/main/controller-merged/src/balancer/BalancerController.sol#L193-L197

https://github.com/sherlock-audit/2022-11-sentiment/blob/main/controller-merged/src/balancer/BalancerController.sol#L219-L228

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. Didn't follow recommendation above, but blocked all trades with intermediate amounts > 0, so problem is solved.

The original report as published on Sherlock, by Bahurum. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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.

More by Bahurum

View all

10 more disclosed reports, best first.

More from Sentiment Update #3

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.