Skip to content
Business logic Audit finding Advanced

BalancerStrategy _withdraw uses BPT_IN_FOR_EXACT_TOKENS_OUT which can be attack to cause loss to all depositors

The BalancerStrategy contract uses a withdrawal method that allows burning all BPT tokens to receive a specified amount of tokens, which can be manipulated by imbalancing the pool to cause the strategy to burn all its BPT and suffer a total loss. An attacker can exploit this to steal value from the strategy, causing losses to all depositors.

GalloDaSballo Follow

Originally disclosed on Code4rena

Submitted by GalloDaSballo

Withdrawals can be manipulated to cause complete loss of all tokens.

The BalancerStrategy accounts for user deposits in terms of the BPT shares they contributed, however, for withdrawals, it estimates the amount of BPT to burn based on the amount of ETH to withdraw, which can be manipulated to cause a total loss to the Strategy.

Deposits of weth are done via userData.joinKind set to 1, which is extracted here in the generic Pool Logic: https://etherscan.io/address/0x5c6ee304399dbdb9c8ef030ab642b10820db8f56#code#F24#L49

The interpretation (by convention is shown here): https://etherscan.io/address/0x5c6ee304399dbdb9c8ef030ab642b10820db8f56#code#F24#L49

    enum JoinKind { INIT, EXACT_TOKENS_IN_FOR_BPT_OUT, TOKEN_IN_FOR_EXACT_BPT_OUT }

Which means that the deposit is using EXACT_TOKENS_IN_FOR_BPT_OUT which is safe in most circumstances (Pool Properly Balanced, with minimum liquidity).

BPT_IN_FOR_EXACT_TOKENS_OUT is vulnerable to manipulation

_vaultWithdraw uses the following logic to determine how many BPT to burn:

https://github.com/Tapioca-DAO/tapioca-yieldbox-strategies-audit/blob/05ba7108a83c66dada98bc5bc75cf18004f2a49b/contracts/balancer/BalancerStrategy.sol#L224-L242

uint256[] memory minAmountsOut = new uint256[](poolTokens.length);
        for (uint256 i = 0; i < poolTokens.length; i++) {
            if (poolTokens[i] == address(wrappedNative)) {
                minAmountsOut[i] = amount;
                index = int256(i);
            } else {
                minAmountsOut[i] = 0;
            }
        }

        IBalancerVault.ExitPoolRequest memory exitRequest;
        exitRequest.assets = poolTokens;
        exitRequest.minAmountsOut = minAmountsOut;
        exitRequest.toInternalBalance = false;
        exitRequest.userData = abi.encode(
            2,
            exitRequest.minAmountsOut,
            pool.balanceOf(address(this))
        );

This query logic is using 2, which Maps out to BPT_IN_FOR_EXACT_TOKENS_OUT which means Exact Out, with any (all) BPT IN, this means that the swapper is willing to burn all tokens: https://etherscan.io/address/0x5c6ee304399dbdb9c8ef030ab642b10820db8f56#code#F24#L51

        enum ExitKind { EXACT_BPT_IN_FOR_ONE_TOKEN_OUT, EXACT_BPT_IN_FOR_TOKENS_OUT, BPT_IN_FOR_EXACT_TOKENS_OUT }

This meets the 2 prerequisite for stealing value from the vault by socializing loss due to single sided exposure:

    1. The request is for at least amount WETH
    1. The request is using BPT_IN_FOR_EXACT_TOKENS_OUT

Which means the strategy will accept any slippage, in this case 100%, causing it to take a total loss for the goal of allowing a withdrawal, at the advantage of the attacker and the detriment of all other depositors.

POC

The requirement to trigger the loss are as follows:

  • Deposit to have some amount of BPTs deposited into the strategy
  • Imbalance the Pool to cause pro-rata amount of single token to require burning a lot more BPTs
  • Withdraw from the strategy, the strategy will burn all of the BPTs it owns (more than the shares)
  • Rebalance the pool with the excess value burned from the strategy

Further Details

Specifically, in withdrawing one Depositor Shares, the request would end up burning EVERYONEs shares, causing massive loss to everyone.

This has already been exploited and explained in Yearns Disclosure:

https://github.com/yearn/yearn-security/blob/master/disclosures/2022-01-30.md

More specifically this finding can cause a total loss, while trying to withdraw tokens for a single user, meaning that an attacker can setup the pool to cause a complete loss to all other stakers.

Mitigation Step

Use EXACT_BPT_IN_FOR_TOKENS_OUT and denominate the Strategy in LP tokens to avoid being attacked via single sided exposure.

cryptotechmaker (Tapioca) confirmed


The original report as published on Code4rena, by GalloDaSballo. 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.

How it worked

Root cause

The strategy uses a withdrawal request that allows burning an unlimited amount of BPT to receive a fixed amount of tokens, without slippage protection.

Impact

An attacker could cause a total loss of all tokens held by the strategy, affecting all depositors.

What the attacker needed

Strategy holds BPT tokens; Attacker can imbalance the pool; Attacker can trigger a withdrawal

The fix

Use `EXACT_BPT_IN_FOR_TOKENS_OUT` and denominate the Strategy in LP tokens to avoid being attacked via single sided exposure.

How it was found

  1. Deposit to have some amount of BPTs deposited into the strategy
  2. Imbalance the Pool to cause pro-rata amount of single token to require burning a lot more BPTs
  3. Withdraw from the strategy, the strategy will burn all of the BPTs it owns (more than the shares)
  4. Rebalance the pool with the excess value burned from the strategy

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.

The strategy holds pool tokens (BPT) that represent a share of a liquidity pool. When someone withdraws, the strategy asks the pool for a specific amount of tokens and is willing to burn all its BPT to get them. An attacker can make the pool give very few tokens for a lot of BPT, so the strategy burns all its BPT and loses everyone's money.

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 GalloDaSballo

View all

30 more disclosed reports, best first.

More from Tapioca Invitational

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.