Skip to content
Business logic Audit finding Advanced

The value of LP token can be manipulated by the first minister, which allows the attacker to dilute future liquidity providers’ shares

The first liquidity provider of a pool can manipulate the ratio of reserves and the total supply of LP tokens so that later providers receive fewer LP tokens than they should due to rounding. An attacker can profit by taking a larger share of the pool when others add liquidity.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by camden, danb, and hyh

For the first minter of an Exchange pool, the ratio of X/Y and the totalSupply of the LP token can be manipulated.

A sophisticated attacker can mint and burn all of the LP tokens but 1 Wei, and then artificially create a situation of rebasing up by transferring baseToken to the pool contract. Then addLiquidity() in singleAssetEntry mode.

Due to the special design of singleAssetEntry mode, the value of LP token can be inflated very quickly.

As a result, 1 Wei of LP token can be worthing a significate amount of baseToken and quoteToken.

Combine this with the precision loss when calculating the amount of LP tokens to be minted to the new liquidity provider, the attacker can turn the pool into a trap which will take a certain amount of cut for all future liquidity providers by minting fewer LP tokens to them.

https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L493-L512

} else {
    // this user will set the initial pricing curve
    require(
        _baseTokenQtyDesired > 0,
        "MathLib: INSUFFICIENT_BASE_QTY_DESIRED"
    );
    require(
        _quoteTokenQtyDesired > 0,
        "MathLib: INSUFFICIENT_QUOTE_QTY_DESIRED"
    );

    tokenQtys.baseTokenQty = _baseTokenQtyDesired;
    tokenQtys.quoteTokenQty = _quoteTokenQtyDesired;
    tokenQtys.liquidityTokenQty = sqrt(
        _baseTokenQtyDesired * _quoteTokenQtyDesired
    );

    _internalBalances.baseTokenReserveQty += tokenQtys.baseTokenQty;
    _internalBalances.quoteTokenReserveQty += tokenQtys.quoteTokenQty;
}

https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L204-L212

function calculateLiquidityTokenQtyForDoubleAssetEntry(
    uint256 _totalSupplyOfLiquidityTokens,
    uint256 _quoteTokenQty,
    uint256 _quoteTokenReserveBalance
) public pure returns (uint256 liquidityTokenQty) {
    liquidityTokenQty =
        (_quoteTokenQty * _totalSupplyOfLiquidityTokens) /
        _quoteTokenReserveBalance;
}

PoC

Given:

  • The Pool is newly created;
  • The market price of baseToken in terms of quoteToken is 1.

The attacker can do the following steps in one tx:

  1. addLiquidity() with 2 Wei of baseToken and 100e18 quoteToken, received 14142135623 LP tokens;
  2. removeLiquidity() with 14142135622 LP tokens, the Pool state becomes:
  3. totalSupply of LP tokens: 1 Wei
  4. baseTokenReserveQty: 1 Wei
  5. quoteTokenReserveQty: 7071067813 Wei
  6. baseToken.transfer() 7071067812 Wei to the Pool contract;
  7. addLiquidity() with no baseToken and 50e18 quoteToken;
  8. swapBaseTokenForQuoteToken() with 600000000000000 baseToken, the Pool state becomes:
  9. totalSupply of LP tokens: 1 Wei
  10. quoteTokenReserveQty 591021750159032
  11. baseTokenReserveQty 600007071067801
  12. baseToken.transfer() 999399992928932200 Wei to the Pool contract;
  13. addLiquidity() with no baseToken and 1e18 quoteToken, the Pool state becomes:
  14. totalSupply of LP tokens: 1 Wei
  15. quoteTokenReserveQty: 1000000000000000013
  16. quoteTokenReserveQty: 985024641638342212
  17. baseTokenDecay: 0

From now on, addLiquidity() with less than 1e18 of baseToken and quoteToken will receive 0 LP token due to precision loss.

The amounts can be manipulated to higher numbers and cause most future liquidity providers to receive fewer LP tokens than expected, and the attacker will be able to profit from it as the attacker will take a larger share of the pool than expected.

Recommendation

Consider requiring a certain amount of minimal LP token amount (eg, 1e8) for the first minter and lock some of the first minter’s LP tokens by minting ~1% of the initial amount to the factory address.

0xean (ElasticSwap) confirmed, but disagreed with High severity and commented:

Thanks for the report, I don’t agree with the severity based on this

From now on, addLiquidity() with less than 1e18 of baseToken and quoteToken will receive 0 LP token due to precision loss.

which in your example represents a user trying to add dust to the contract after the attack.

I think we will implement the minimum locked liquidity to avoid rounding errors, but this attack assumes users are adding dust to the contract and that they are totally unaware of the contract state which is incorrect. Users specific a min and a max token qty’s when adding liquidity.

Would recommend med. risk on this one if not low risk given the attack is on “dust” amounts of tokens.

Alex the Entreprenerd (judge) changed severity to Medium and commented:

I agree with the finding and remember coming across it when reading the Yearn V0.4.2 Audit by Trails of Bits.

Ultimately this is contingent on a donation, that will make each share more valuable, so it’s effectively a way to use rounding against making dust donations.

Technically this same idea can be extended to huge donations, however there are very dubious economic reasons as to why you’d do that (perhaps frontrunning a moderate deposit with the goal of using this method to earn that MEV).

Ultimately this is something that can happen anytime you have X shares and Y totalSupply If the total supply reaches greater units than the shares, then integer division will inevitably eat out some of those shares.

Have yet to see a long term solution to this rounding problem, however, a simple initial addition that mints 1e18 shares will require some economic commitment by the potential exploiters.

Agree with medium severity.

0xean (ElasticSwap) resolved


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 lack of a minimum initial liquidity and locked tokens allows the first minter to set an extreme ratio and total supply, causing rounding errors for subsequent deposits.

Impact

Future liquidity providers may receive fewer LP tokens than expected, and the attacker could profit by taking a larger share of the pool; however, the attack requires users to add small amounts without checking the pool state, and the judge noted it is contingent on donations and rounding.

What the attacker needed

Attacker is first liquidity provider; Victims add liquidity without checking state

How it was found

  1. Add liquidity with a tiny amount of base token and a large amount of quote token to receive LP tokens.
  2. Remove almost all LP tokens, leaving only 1 wei of LP tokens and small reserves.
  3. Transfer base token directly to the pool to inflate the value of the remaining LP token.
  4. Add liquidity with only quote token to further inflate the LP token value.
  5. Perform a swap to adjust reserves and then transfer more base token to the pool.
  6. Add liquidity again with only quote token, resulting in a state where future small deposits receive zero LP tokens due to rounding.

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 liquidity pool, you get pool tokens (LP tokens) representing your share. The first person to add money sets the initial exchange rate and total number of tokens. If they make the numbers very lopsided, later people who add money may get almost no tokens because of how the maths rounds down. This means the first person can end up owning most of the pool and profit when others join.

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 WatchPug

View all

197 more disclosed reports, best first.

More from ElasticSwap 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.