Skip to content
Business logic Audit finding Advanced

zeroswap/UniswapV2Pair.sol Token reserves per lp token can be manipulated due to lack of MINIMUM_LIQUIDITY when minting the first liquidity with migrator

The UniswapV2Pair contract's first mint logic allows a migrator to set an arbitrary desired liquidity, bypassing the MINIMUM_LIQUIDITY lock. An attacker could mint 1 wei of LP tokens and then donate large amounts of tokens to inflate reserves, causing future depositors to lose funds due to precision loss.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

if (_totalSupply == 0) {
    address migrator = IUniswapV2Factory(factory).migrator();
    if (msg.sender == migrator) {
        liquidity = IMigrator(migrator).desiredLiquidity();
        require(liquidity > 0 && liquidity != uint256(-1), "Bad desired liquidity");
    } else {
        require(migrator == address(0), "Must not have migrator");
        liquidity = Math.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);
        _mint(address(0), MINIMUM_LIQUIDITY); // permanently lock the first MINIMUM_LIQUIDITY tokens
    }
} else {
    liquidity = Math.min(amount0.mul(_totalSupply) / _reserve0, amount1.mul(_totalSupply) / _reserve1);
}

https://github.com/Plex-Engineer/zeroswap/blob/03507a80322112f4f3c723fc68bed0f138702836/contracts/Migrator.sol#L28-L46

function migrate(IUniswapV2Pair orig) public returns (IUniswapV2Pair) {
    require(msg.sender == chef, "not from master chef");
    require(block.number >= notBeforeBlock, "too early to migrate");
    require(orig.factory() == oldFactory, "not from old factory");
    address token0 = orig.token0();
    address token1 = orig.token1();
    IUniswapV2Pair pair = IUniswapV2Pair(factory.getPair(token0, token1));
    if (pair == IUniswapV2Pair(address(0))) {
        pair = IUniswapV2Pair(factory.createPair(token0, token1));
    }
    uint256 lp = orig.balanceOf(msg.sender);
    if (lp == 0) return pair;
    desiredLiquidity = lp;
    orig.transferFrom(msg.sender, address(orig), lp);
    orig.burn(address(pair));
    pair.mint(msg.sender);
    desiredLiquidity = uint256(-1);
    return pair;
}

When minting LP tokens (addLiquidity), the amount of lp tokens you are getting is calculated based on liquidity = Math.min(amount0.mul(_totalSupply) / _reserve0, amount1.mul(_totalSupply) / _reserve1);, if the _totalSupply is small enough, and 1 wei of the lp token worth large amounts of token0 and token1, the user who adds small amounts of liquidity will receive less amount of lp tokens due to precision loss.

A sophisticated attacker can artificially create that scenario by mint only 1 wei of lp token and add 1e24 or even larger amounts of token0 and token1 by sending the tokens to the contract and then call sync() to update the reserves.

Then all the new depositors will lose up to 1e24, let’s say they deposited 1.99e24, they will only receive 1 wei of lp token, therefore, losing 0.99e24 of token0 and token1.

This attack vector was mitigated the original version of UniswapV2Pair by introcuing the MINIMUM_LIQUIDITY minted and permanently lock in address(0) upon the first mint.

However, this can now be bypassed with the migrator, and this attacker vector is open again.

Recommended Mitigation Steps

Given the fact that zeroswap will be a DEX that does not need a feature to migrate liquidity from other DEXs, consider removing the migrator.

tkkwon1998 (Canto) acknowledged and commented:

Issue acknowledged, we will not be migrating liquidity from zeroswap.

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

The warden has shown how, due to legacy code, an LP pair can be permissionlessly minted and setup to cause loss to future depositors.

Deployment of pairs is permissionless, however, the setup of Factory is Admin Dependent.

While the Migrator file was out of scope, I believe the sponsor acknowledging, and the code being on the Pair file allows the finding to be valid.

However, because it is ultimately contingent on setup, I think Medium Severity to be more appropriate.


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 migrator path in the first mint bypasses the MINIMUM_LIQUIDITY lock, allowing an attacker to set an arbitrarily small total supply and manipulate the reserves.

Impact

New depositors could lose up to the full amount they deposit due to receiving disproportionately few LP tokens, but the attack requires the migrator to be set and the factory setup to be admin-dependent.

What the attacker needed

Migrator role set in factory; attacker has some LP tokens to migrate; victim deposits liquidity

The fix

The migrator should be removed, as the DEX does not need to migrate liquidity from other DEXs.

How it was found

  1. Attacker calls migrate with a small amount of LP tokens to create a new pair with 1 wei of liquidity.
  2. Attacker sends large amounts of token0 and token1 directly to the pair contract.
  3. Attacker calls sync() to update the reserves to the inflated amounts.
  4. Future depositors add liquidity and receive very few LP tokens due to precision loss, losing most of their deposit.

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.

In a liquidity pool, the first person to add liquidity normally gets a small number of pool tokens permanently locked to prevent manipulation. Here, a special migrator role can skip that lock and create a tiny initial supply. An attacker can then inflate the pool's reserves, making later depositors receive far fewer pool tokens than they should, effectively stealing their funds.

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 Canto

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.