Skip to content
Business logic Audit finding Intermediate

Transferring quoteToken to the exchange pool contract will cause future liquidity providers to lose funds

The LP token minting calculation uses the contract's actual quoteToken balance instead of the internal reserve, allowing an attacker to donate quoteToken and inflate the balance so future liquidity providers receive fewer LP tokens and lose funds.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

In the current implementation, the amount of LP tokens to be minted when addLiquidity() is calculated based on the ratio between the amount of newly added quoteToken and the current wallet balance of quoteToken in the Exchange contract.

However, since anyone can transfer quoteToken to the contract, and make the balance of quoteToken to be larger than _internalBalances.quoteTokenReserveQty, existing liquidity providers can take advantage of this by donating quoteToken and make future liquidity providers receive fewer LP tokens than expected and lose funds.

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

liquidityTokenQty = calculateLiquidityTokenQtyForDoubleAssetEntry(
    _totalSupplyOfLiquidityTokens,
    quoteTokenQty,
    _quoteTokenReserveQty // IERC20(quoteToken).balanceOf(address(this))
);

PoC

Given:

  • The Exchange pool is new;
  • Alice addLiquidity() with 1e18 baseToken and 1e18 quoteToken, recived 1e18 LP token;
  • Alice transfer 99e18 quoteToken to the Exchange pool contract;
  • Bob addLiquidity() with 1e18 baseToken and 1e18 quoteToken;
  • Bob removeLiquidity() with all the LP token in balance.

Expected Results: Bob recived 1e18 baseToken and >= 1e18 quoteToken.

Actual Results: Bob recived ~0.02e18 baseToken and ~1e18 quoteToken.

Alice can now removeLiquidity() and recive ~1.98e18 baseToken and ~100e18 quoteToken.

As a result, Bob suffers a fund loss of 0.98e18 baseToken.

Recommendation

Change to:

liquidityTokenQty = calculateLiquidityTokenQtyForDoubleAssetEntry(
    _totalSupplyOfLiquidityTokens,
    quoteTokenQty,
    _internalBalances.quoteTokenReserveQty
);

0xean (ElasticSwap) confirmed and commented:

This does appear to be correct after attempting a POC. Thank you WatchPug!

Alex the Entreprenerd (judge) commented:

The warden identified a way to exploit the protocol math to devalue future liquidity provision at the advantage of early liquidity providers.

The exploit is extractive in nature, however, because this is reliably performable and effectively breaks the protocol’s goals and mechanics, I believe High Severity to be appropriate.

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

The LP token minting calculation uses the contract's actual quoteToken balance instead of the internal reserve balance.

Impact

Future liquidity providers could lose funds when adding liquidity after an attacker donates quoteToken to the pool.

What the attacker needed

Attacker is an existing liquidity provider; Attacker can transfer quoteToken to the pool; Victim adds liquidity after the donation

How it was found

  1. Alice adds liquidity with 1e18 baseToken and 1e18 quoteToken, receiving 1e18 LP tokens.
  2. Alice transfers 99e18 quoteToken directly to the Exchange pool contract.
  3. Bob adds liquidity with 1e18 baseToken and 1e18 quoteToken.
  4. Bob removes all his LP tokens.
  5. Bob receives only ~0.02e18 baseToken and ~1e18 quoteToken, losing ~0.98e18 baseToken.
  6. Alice removes her liquidity and receives ~1.98e18 baseToken and ~100e18 quoteToken.

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 shares (LP tokens) based on how much you put in. The bug is that the pool counts all the money sitting in its account, not just the money it officially tracks. So someone can send extra money directly to the pool, making it look bigger, and the next person who adds money gets fewer shares than they should, losing out.

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.