Skip to content
Smart contracts Audit finding Intermediate

First depositor can break minting of shares

The pair contract mints LP shares using a formula that rounds down, so an attacker who seeds the pool with a tiny amount and then donates a large amount directly to the pair can make each share extremely valuable. Later depositors adding less than the inflated reserve then receive zero LP tokens for their deposit.

minhquanym Follow with Apocalypto, 0xDecorativePineapple and 40 others

Originally disclosed on Code4rena

Submitted by minhquanym, also found by Apocalypto, 0xDecorativePineapple, Franfran, dipp, rjs, ak1, Tricko, Jeiwan, unforgiven, hansfriese, BAHOZ, unforgiven, bytehat, UNCHAIN, immeas, SamGMK, fs0c, Tointer, haku, Koolex, 141345, ElKu, rajatbeladiya, hihen, izhelyazkov, KingNFT, koxuan, 0x52, carrotsmuggler, yixxas, HE1M, supernova, cozzetti, rvierdiiev, SamGMK, aviggiano, seyni, lumoswiz, ladboy233, chaduke, cccz, and eyexploit

The attack vector and impact is the same as TOB-YEARN-003, where users may not receive shares in exchange for their deposits if the total asset amount has been manipulated through a large “donation”.

Proof of Concept

In Pair.add(), the amount of LP token minted is calculated as

function addQuote(uint256 baseTokenAmount, uint256 fractionalTokenAmount) public view returns (uint256) {
    uint256 lpTokenSupply = lpToken.totalSupply();
    if (lpTokenSupply > 0) {
        // calculate amount of lp tokens as a fraction of existing reserves
        uint256 baseTokenShare = (baseTokenAmount * lpTokenSupply) / baseTokenReserves();
        uint256 fractionalTokenShare = (fractionalTokenAmount * lpTokenSupply) / fractionalTokenReserves();
        return Math.min(baseTokenShare, fractionalTokenShare);
    } else {
        // if there is no liquidity then init
        return Math.sqrt(baseTokenAmount * fractionalTokenAmount);
    }
}

An attacker can exploit using these steps

  1. Create and add 1 wei baseToken - 1 wei quoteToken to the pair. At this moment, attacker is minted 1 wei LP token because sqrt(1 * 1) = 1
  2. Transfer large amount of baseToken and quoteToken directly to the pair, such as 1e9 baseToken - 1e9 quoteToken. Since no new LP token is minted, 1 wei LP token worths 1e9 baseToken - 1e9 quoteToken.
  3. Normal users add liquidity to pool will receive 0 LP token if they add less than 1e9 token because of rounding division.
baseTokenShare = (X * 1) / 1e9;
fractionalTokenShare = (Y * 1) / 1e9;

Recommended Mitigation Steps

require(lpTokenAmount != 0, "No LP minted");

outdoteth (Caviar) confirmed and commented:

Fixed in: https://github.com/outdoteth/caviar/pull/3


The original report as published on Code4rena, by minhquanym. 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 share calculation divides by reserves that can be inflated by direct transfers without minting new shares, and the result is not checked to be non-zero.

Impact

Users could deposit tokens and receive no shares in return, losing the value of their deposit to the attacker's inflated share.

What the attacker needed

Attacker acts first on a new pair; attacker can transfer tokens directly to pair

The fix

It was fixed by sending the first minimum liquidity LP tokens to the zero address when supply is zero and requiring that the minted LP amount is non-zero.

How it was found

  1. Create a pair and add a minimal amount of each token, receiving one wei of LP token.
  2. Transfer a large amount of both tokens directly to the pair so the existing single share becomes very valuable.
  3. Observe that a normal user depositing less than the inflated reserve receives 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.

A liquidity pool is a shared pot where people deposit two tokens and get shares (LP tokens) representing their portion. Shares are calculated by dividing the deposit by the pool's total reserves, and whole-number maths rounds down. If someone makes the pool's reserves huge while the number of shares stays tiny, a normal deposit divided by that huge reserve rounds to zero, so the depositor gets nothing back for their tokens.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by minhquanym

View all

28 more disclosed reports, best first.

More from Caviar Private Pools

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.