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.
Submitted by WatchPug
function underlyingBalance() public view returns (uint256) {
uint256 _pool = IERC20(underlyingToken).balanceOf(address(this));
return _pool.add(outstandingAmount);
}
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()is100,000 USDC; - Pending rebalancing amount is
1000 USDC; strategistcallsrebalancePair();- 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 retireve100,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:
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.
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
- The strategist calls rebalancePair(), which will transfer underlying tokens into the BathToken contract.
- The attacker sends a deposit transaction with a higher gas price to deposit a large amount and take a significant share of the pool.
- 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.