Skip to content
Race condition Audit finding Intermediate

UniV3Vault.sol#collectEarnings() can be front run

The vault's total value locked (TVL) calculation excludes unharvested trading fees, so an attacker can deposit just before fees are collected, take a large share of the pool, and then withdraw to capture a portion of those pending fees. This allows the attacker to steal half of the pending yields in about one block.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

For UniV3Vault, it seems that lp fees are collected through collectEarnings() callable by the strategy and reinvested (rebalanced).

However, in the current implementation, unharvested yields are not included in tvl(), making it vulnerable to front-run attacks that steal pending yields.

Proof Of Concept

Given:

  • Current tvl() is 10 ETH and 40,000 USDC;
  • Current unclaimed yields (trading fees) is 1 ETH and 4,000 USDC;
  • strategy calls collectEarnings() to collect fees and reinvest;
  • The attacker sends a deposit tx with a higher gas price to deposit 10 ETH and 40,000 USDC, take 50% share of the pool;
  • After the transaction in step 1 is packed, the attacker calls withdraw() and retrieves 10.5 ETH and 42,000 USDC.

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

Recommendation

Consider including fees in tvl().

For the code to calculate fees earned, please reference _computeFeesEarned() in G-UNI project:

MihanixA (Mellow Protocol) confirmed:

Thanks! Added tokensOwed to UniV3Vault’s tvl

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 vault's TVL calculation omits uncollected trading fees, so share price does not reflect pending earnings.

Impact

The attacker could steal half of the pending yields in about one block of time.

What the attacker needed

Attacker can submit a deposit transaction; attacker can pay higher gas to front-run; strategy calls collectEarnings()

The fix

The fix is to include uncollected fees in the TVL calculation, for example by adding tokensOwed to the vault's TVL.

How it was found

  1. The strategy calls collectEarnings() to collect fees and reinvest.
  2. The attacker sends a deposit transaction with a higher gas price to deposit 10 ETH and 40,000 USDC, taking 50% share of the pool.
  3. After the collectEarnings transaction is packed, the attacker calls withdraw() and retrieves 10.5 ETH and 42,000 USDC.

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 from many users and shares profits based on how much each person deposited. The bug is that the vault's reported total value does not include fees that have been earned but not yet collected. An attacker can quickly deposit a large amount just before those fees are added, then withdraw and take a share of the fees that they did not earn.

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 Mellow Protocol contest

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.