Skip to content
Race condition Audit finding Intermediate

BathPair.sol#rebalancePair() can be front run to steal the pending rebalancing amount

The BathToken contract's share price drops as pending trades fill, then jumps when a strategist rebalances by transferring underlying tokens. An attacker can front-run the rebalance transaction with a large deposit to capture a share of the pending yield, then withdraw immediately after for a profit.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathToken.sol#L756-L759

function underlyingBalance() public view returns (uint256) {
    uint256 _pool = IERC20(underlyingToken).balanceOf(address(this));
    return _pool.add(outstandingAmount);
}

https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathToken.sol#L294-L303

function removeFilledTradeAmount(uint256 amt) external onlyPair {
    outstandingAmount = outstandingAmount.sub(amt);
    emit LogRemoveFilledTradeAmount(
        IERC20(underlyingToken),
        amt,
        underlyingBalance(),
        outstandingAmount,
        totalSupply
    );
}

For BathToken, there will be non-underlyingToken assets sitting on the contract that have filled to the contract and are awaiting rebalancing by strategists.

We assume the rebalance will happen periodically, between one rebalance to the next rebalance, underlyingBalance() will decrease over time as the orders get filled, so that the price per share will get lower while the actual equity remain relatively stable. This kind of price deviation will later be corrected by rebalancing.

Every time a BathPair.sol#rebalancePair() get called, there will be a surge of price per share for the BathToken, as a certain amount of underlyingToken will be transferred into the contract.

This enables a well known attack vector, which allows the pending yields to be stolen by front run the strategist’s BathPair.sol#rebalancePair() transaction, deposit and take a large share of the vault, and withdraw() right after the rebalancePair() transaction for instant profit.

Proof of Concept

Given:

  • Current underlyingBalance() is 100,000 USDC;
  • Pending rebalancing amount is 1000 USDC;
  • strategist calls rebalancePair();
  • The attacker sends a deposit tx with a higher gas price to deposit 100,000 USDC, take 50% share of the pool;
  • After the transaction in step 1 is mined, the attacker calls withdraw() and retireve 100,500 USDC.

As a result, the attacker has stolen half of the pending yields in about 1 block of time.

Recommendation

Consider adding a new variable to track rebalancingAmount on BathToken.

BathToken should be notified for any pending rebalancing amount changes via BathPair in order to avoid sudden surge of pricePerShare over rebalancePair().

rebalancingAmount should be considered as part of underlyingBalance().

bghughes (Rubicon) disputed and marked as duplicate:

Bad issue due to #344 #43 #74

HickupHH3 (judge) commented:

It’s kinda like the flip side to #341, where an incoming deposit benefits by frontrunning.

#221 briefly mentions it: “Similar problem also affect the deposit function since it relies on the proper accounting of the underlying balance or outstanding amount too. The amount of BathToken (e.g. BathWETH) that depositer received might affected.”

In this case, a depositor can execute the frontrun attack vector exists even if the strategist is actively rebalancing. Hence, the high severity rating is justified.


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

Likely the share price is based only on the current underlying token balance, which temporarily dips as trades fill and then jumps when rebalancing transfers tokens in, without accounting for pending rebalancing amounts.

Impact

An attacker could steal a portion of the pending rebalancing yield in about one block, and this attack is possible even if the strategist is actively rebalancing.

What the attacker needed

Attacker can submit a transaction with higher gas price; strategist calls rebalancePair()

The fix

The contract should track a rebalancingAmount variable and include it in underlyingBalance() to prevent sudden share price surges.

How it was found

  1. The strategist calls rebalancePair(), which will transfer underlying tokens into the BathToken contract.
  2. The attacker sends a deposit transaction with a higher gas price to deposit a large amount and take a significant share of the pool.
  3. After the rebalance transaction is mined, the attacker calls withdraw() to retrieve their deposit plus a portion of the pending yield.

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 vault holds money for many users, and each user's share is worth a portion of the total. When new money is about to be added to the vault, someone can quickly put in a large deposit just before that happens, making their share temporarily bigger, and then take their money out right after to grab part of the new money. This is unfair because they did not earn that profit.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Rubicon v2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.