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.
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.
- https://github.com/code-423n4/2021-12-mellow/blob/6679e2dd118b33481ee81ad013ece4ea723327b5/mellow-vaults/contracts/UniV3Vault.sol#L100-L122
- https://github.com/code-423n4/2021-12-mellow/blob/6679e2dd118b33481ee81ad013ece4ea723327b5/mellow-vaults/contracts/UniV3Vault.sol#L80-L97
Proof Of Concept
Given:
- Current
tvl()is10 ETHand40,000 USDC; - Current unclaimed yields (trading fees) is
1 ETHand4,000 USDC; strategycallscollectEarnings()to collect fees and reinvest;- The attacker sends a deposit tx with a higher gas price to deposit
10 ETHand40,000 USDC, take 50% share of the pool; - After the transaction in step 1 is packed, the attacker calls
withdraw()and retrieves10.5 ETHand42,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
tokensOwedtoUniV3Vault’stvl
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
- The strategy calls collectEarnings() to collect fees and reinvest.
- 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.
- 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.