Skip to content
DoS Audit finding Intermediate

LiquidityPool can be DoS when a complete withdrawal is performed

The LiquidityPool's getTokenPrice() function divides by totalSupply, which can become zero after a complete withdrawal while totalFunds remains non-zero due to rounding. This causes a division-by-zero revert, blocking further deposits and requiring redeployment.

peakbolt Follow with auditor0517

Originally disclosed on Code4rena

Submitted by peakbolt, also found by auditor0517

LiquidityPool can be DoS when all funds are withdrawn from the pool, causing getTokenPrice() to revert due to zero totalSupply().

Impact

The LiquidityPool will not be able to proceed and requires new deployment of the contracts.

Detailed Explanation

The LiquidityPool.getTokenPrice() will encounter a division by zero error when the totalSupply is zero but the totalFunds is non-zero. This could occur when there are partial withdrawals, causing totalFunds to be non-zero after a complete withdrawal, due to rounding from the withdrawals.

function getTokenPrice() public view override returns (uint256) {
    if (totalFunds == 0) {
        return 1e18;
    }

    uint256 totalSupply = liquidityToken.totalSupply() + totalQueuedWithdrawals;
    int256 skew = _getSkew();

    if (skew == 0) {
        return totalFunds.divWadDown(totalSupply);
    }

    (uint256 markPrice, bool isInvalid) = getMarkPrice();
    require(!isInvalid);

    uint256 totalValue = totalFunds;

    uint256 amountOwed = markPrice.mulWadDown(powerPerp.totalSupply());
    uint256 amountToCollect = markPrice.mulWadDown(shortToken.totalShorts());
    uint256 totalMargin = _getTotalMargin();

    totalValue += totalMargin + amountToCollect;
    totalValue -= uint256((int256(amountOwed) + usedFunds));

    return totalValue.divWadDown(totalSupply);
}

Proof of Concept

Add the following test case to test/LiquidityPool.Trades.t.sol

function testLiquidityPoolTokenPriceError() public {
    uint256 longAmount = 100e18;
    int256 skew = pool.getSkew();
    assertEq(skew, 0);

    // make some trades for pool to earn fees
    susd.mint(user_1, 100e18);
    openLong(longAmount, longAmount * 1000, user_1);
    setAssetPrice(initialPrice * 99 / 100);
    closeLong(longAmount, 0, user_1);

    // Queue withdrawal. This was deposited in TestSystem.preparePool()
    pool.queueWithdraw(1_000_000e18, address(this));
    skip(14500);

    // This will perform a partial withdrawals due to margin in perpMarket
    // we use this just to trigger some rounding so that
    // totalFunds wont be zero during we fully withdraw from the pool
    pool.processWithdraws(1);

    // Transfer back margins from perpMarket so that
    // we can continue to withdraw the balance.
    pool.rebalanceMargin(-70567200000000000000000);

    // This will do a complete withdrawals, reducing totalSupply to zero.
    // However, getTokenPrice() will encounter division by zero error.
    // That is because there will be small amount of totalFund left due to rounding.
    pool.processWithdraws(1);

    // approve funds for deposit again
    susd.approve(address(pool), 1_000_000e18);

    // This deposit will revert as LiquidityPool.getTokenPrice()
    // will encounter division by zero error.
    vm.expectRevert();
    pool.deposit(1_000_000e18, address(this));

}

Recommended Mitigation Steps

Add in validation to check and handle when totalSupply is zero.

mubaris (Polynomial) confirmed


The original report as published on Code4rena, by peakbolt. 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 getTokenPrice() function does not handle the case where totalSupply is zero while totalFunds is non-zero, leading to a division by zero.

Impact

The LiquidityPool cannot proceed and requires new deployment of the contracts.

What the attacker needed

Pool has funds and users can withdraw; rounding leaves small totalFunds after full withdrawal

The fix

Add validation to check and handle the case when totalSupply is zero.

How it was found

  1. Make trades so the pool earns fees.
  2. Queue a withdrawal and advance time.
  3. Process a partial withdrawal to trigger rounding.
  4. Rebalance margin back from the perpetual market.
  5. Process a complete withdrawal, reducing totalSupply to zero.
  6. Attempt a new deposit, which reverts due to the division-by-zero error.

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 of money that users deposit into and withdraw from. The pool calculates a price per share by dividing the total money by the number of shares; if all shares are withdrawn but a tiny amount of money remains due to rounding, the division breaks and the pool stops working.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by peakbolt

View all

16 more disclosed reports, best first.

More from Polynomial Protocol contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.