Skip to content
Business logic Audit finding Advanced

LiquidityProviders.sol The share price of the LP can be manipulated and making future liquidityProviders unable to removeLiquidity()

The share price of the liquidity pool can be manipulated to an extremely low value, causing an overflow in the calculation of token amounts when removing liquidity. This prevents future liquidity providers from withdrawing their funds.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

LiquidityProviders.sol#L345-L362

function removeLiquidity(uint256 _nftId, uint256 _amount)
    external
    nonReentrant
    onlyValidLpToken(_nftId, _msgSender())
    whenNotPaused
{
    (address _tokenAddress, uint256 nftSuppliedLiquidity, uint256 totalNFTShares) = lpToken.tokenMetadata(_nftId);
    require(_isSupportedToken(_tokenAddress), "ERR__TOKEN_NOT_SUPPORTED");

    require(_amount != 0, "ERR__INVALID_AMOUNT");
    require(nftSuppliedLiquidity >= _amount, "ERR__INSUFFICIENT_LIQUIDITY");
    whiteListPeriodManager.beforeLiquidityRemoval(_msgSender(), _tokenAddress, _amount);
    // Claculate how much shares represent input amount
    uint256 lpSharesForInputAmount = _amount * getTokenPriceInLPShares(_tokenAddress);

    // Calculate rewards accumulated
    uint256 eligibleLiquidity = sharesToTokenAmount(totalNFTShares, _tokenAddress);

LiquidityProviders.sol#L192-L194

function sharesToTokenAmount(uint256 _shares, address _tokenAddress) public view returns (uint256) {
    return (_shares * totalReserve[_tokenAddress]) / totalSharesMinted[_tokenAddress];
}

The share price of the liquidity can be manipulated to an extremely low value (1 underlying token worth a huge amount of shares), making it possible for sharesToTokenAmount(totalNFTShares, _tokenAddress) to overflow in removeLiquidity() and therefore freeze users’ funds.

Proof of Concept

  1. Alice addTokenLiquidity() with 1e8 * 1e18 XYZ on B-Chain, totalSharesMinted == 1e44;
  2. Alice sendFundsToUser() and bridge 1e8 * 1e18 XYZ from B-Chain to A-Chain;
  3. Alice depositErc20() and bridge 1e8 * 1e18 XYZ from A-Chain to B-Chain;
  4. Alice removeLiquidity() and withdraw 1e8 * 1e18 - 1 XYZ, then: totalReserve == 1 wei XYZ, and totalSharesMinted == 1e26;
  5. Bob addTokenLiquidity() with 3.4e7 * 1e18 XYZ;
  6. Bob tries to removeLiquidity().

Expected Results: Bob to get back the deposits;

Actual Results: The tx reverted due to overflow at sharesToTokenAmount().

Recommended Mitigation Steps

LiquidityProviders.sol#L280-L292

function _increaseLiquidity(uint256 _nftId, uint256 _amount) internal onlyValidLpToken(_nftId, _msgSender()) {
    (address token, uint256 totalSuppliedLiquidity, uint256 totalShares) = lpToken.tokenMetadata(_nftId);

    require(_amount > 0, "ERR__AMOUNT_IS_0");
    whiteListPeriodManager.beforeLiquidityAddition(_msgSender(), token, _amount);

    uint256 mintedSharesAmount;
    // Adding liquidity in the pool for the first time
    if (totalReserve[token] == 0) {
        mintedSharesAmount = BASE_DIVISOR * _amount;
    } else {
        mintedSharesAmount = (_amount * totalSharesMinted[token]) / totalReserve[token];
    }
    ...

Consider locking part of the first mint’s liquidity to maintain a minimum amount of totalReserve[token], so that the share price can not be easily manipulated.

ankurdubey521 (Biconomy) confirmed

pauliax (judge) commented:

Great find, with a PoC, deserves a severity of high because it is a valid attack path that does not have hand-wavy hypotheticals.


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 share price can be manipulated because the first mint does not lock a minimum amount of liquidity, allowing an attacker to make totalReserve very small relative to totalSharesMinted, so that later share-to-token calculations overflow.

Impact

Future liquidity providers could be unable to remove their liquidity, effectively freezing their funds.

What the attacker needed

Attacker has enough tokens to manipulate pool; victim adds liquidity after manipulation

How it was found

  1. Alice adds a large amount of token liquidity on B-Chain, setting totalSharesMinted to a huge number.
  2. Alice bridges tokens from B-Chain to A-Chain and back, manipulating the pool's reserves.
  3. Alice removes almost all her liquidity, leaving totalReserve at 1 wei and totalSharesMinted still very large.
  4. Bob adds liquidity with a large amount of tokens, receiving an enormous number of shares due to the low share price.
  5. Bob tries to remove his liquidity, but the transaction reverts due to an overflow in the share-to-token calculation.

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, users deposit tokens and receive shares representing their portion. The value of one share depends on the total tokens divided by total shares. An attacker can make each share worth very little by adding a tiny amount of tokens after almost emptying the pool. Then, when a new user deposits, they get a huge number of shares. Later, when they try to withdraw, a calculation multiplies that huge number by the token amount and overflows, causing the transaction to fail and locking 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 Biconomy - Smart Contract Wallet 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.