Skip to content
Business logic Audit finding Intermediate

Mint function does not update LiquidityPosition state of caller before minting LP tokens. This

The mint function in the pool contract fails to update the caller's liquidity position state before minting new LP tokens, leaving fee growth and fee levels inconsistent with global values. This can cause liquidity providers to receive incorrect (lower) fees when they later burn their tokens.

0Kage Follow

Originally disclosed on Code4rena

Submitted by 0Kage

https://github.com/code-423n4/2023-01-timeswap/blob/ef4c84fb8535aad8abd6b67cc45d994337ec4514/packages/v2-pool/src/structs/Pool.sol#L302

https://github.com/code-423n4/2023-01-timeswap/blob/ef4c84fb8535aad8abd6b67cc45d994337ec4514/packages/v2-pool/src/structs/LiquidityPosition.sol#L60

Impact

When a LP mints V2 Pool tokens, mint function in PoolLibrary gets called. Inside this function updateDurationWeightBeforeMaturity updates global short, long0 and long1 fee growth.

Change in global fee growth necessitates an update to LiquidityPosition state of caller (specifically updating fees & fee growth rates) when there are state changes made to that position (in this case, increasing liquidity). This principle is followed in functions such as burn, transferLiquidity, transferFees. However when calling mint, this update is missing. As a result, growth & fee levels in liquidity position of caller are inconsistent with global fee growth rates.

Inconsistent state leads to incorrect calculations of long0/long1 and short fees of LP holders which in turn can lead to loss of fees. Since this impacts actual rewards for users, I’ve marked it as MEDIUM risk.

Proof of Concept

Let’s say, Bob has following sequence of events

  • MINT at T0: Bob is a LP who mints N pool tokens at T0
  • MINT at T1: Bob mints another M pool tokens at T1. At this point, had the protocol correctly updated fees before minting new pool tokens, Bob’s fees & growth rate would be a function of current liquidity (N), global updated short fee growth rate at t1 (s*t1) and Bob’s previous growth rate at t*0 (b_t0)
  • BURN at T2: Bob burns N + M tokens at T2. At this point, Bob’s fees should be a function of previous liquidity (N+M), global short fee growth rate (st2) and Bob’s previous growth rate at t*1(bt1) -> since this update never happened, Bob’s previous growth rate is wrongly referenced b*t0 instead of b_t1.

Bob could collect a lower fees because of this state inconsistency.

Recommended Mitigation Steps

Update the liquidity position state right before minting.

After line 302 of Pool.sol, update the LiquidityPosition by adding

  liquidityPosition.update(pool.long0FeeGrowth, pool.long1FeeGrowth, pool.shortFeeGrowth);

vhawk19 (Timeswap) confirmed


The original report as published on Code4rena, by 0Kage. 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 mint function does not call the liquidity position update routine before increasing liquidity, unlike other functions such as burn and transferLiquidity.

Impact

Bob could collect lower fees because of this state inconsistency.

What the attacker needed

User is a liquidity provider; Multiple mint operations over time

The fix

The liquidity position state should be updated right before minting, by calling the update function with the current global fee growth values.

How it was found

  1. Bob mints N pool tokens at time T0.
  2. Bob mints another M pool tokens at time T1 without his liquidity position being updated.
  3. Bob burns N + M tokens at time T2, but his previous growth rate is still from T0 instead of T1.

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.

When you add money to a pool, the system should update your personal record of fees earned. Here, it forgets to do that, so later when you take your money out, your fee calculation is based on old data and you might get less than you should.

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 0Kage

View all

7 more disclosed reports, best first.

More from Timeswap contest

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.