Skip to content
DoS Audit finding Intermediate

Division by zero error causes KangarooVault to be DoS with funds locked inside

The KangarooVault contract's getTokenPrice() function divides by totalSupply without checking if it is zero, causing a division-by-zero error when all vault tokens are withdrawn but funds remain. This locks the remaining funds permanently because deposits and withdrawals are blocked.

peakbolt Follow

Originally disclosed on Code4rena

Submitted by peakbolt

KangarooVault can be DoS with funds locked in the contract due to a division by zero error in getTokenPrice() as it does not handle the scenario where getTotalSupply() is zero.

Impact

Funds will be locked within the KangarooVault (as shown in the PoC below) and it is not able to recover from the DoS.

That is because, to recover from the DoS, it requires increasing total supply through minting of new tokens via deposits. However, that is not possible as initiateDeposit() relies on getTokenPrice().

Also, we cannot withdraw the remaining funds as there are no more VaultTokens left to burn.

Detailed Explanation

getTokenPrice() will attempt to perform a division by getTotalSupply() when totalFunds != 0 and positionId == 0. This scenario is possible when there are remaining funds in KangarooVault when all positions are closed and all vault token holders withdrawn their funds.

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

    uint256 totalSupply = getTotalSupply();
    if (positionData.positionId == 0) {
        return totalFunds.divWadDown(totalSupply);
    }

Proof of Concept

Add the following imports and test case to test/Kangaroo.Vault.t.sol

function testKangarooDivisionByZero() public {
    uint256 amt = 231e18;

    // Open position to decrease availableFunds for withdrawals.
    kangaroo.openPosition(amt, 0);
    skip(100);
    kangaroo.executePerpOrders(emptyData);
    kangaroo.clearPendingOpenOrders(0);

    // initiate user withdrawal
    // this will be a partial withdrawal due to the open position.
    vm.prank(user_1);
    kangaroo.initiateWithdrawal(user_1, 5e23);
    kangaroo.processWithdrawalQueue(1);

    // close all position
    kangaroo.closePosition(amt, 1000000e18);
    skip(100);
    kangaroo.executePerpOrders(emptyData);
    kangaroo.clearPendingCloseOrders(0);

    // Complete remaining withdrawals of funds.
    // this will reduce totalSupply to zero and later cause a division by zero error.
    kangaroo.processWithdrawalQueue(1);

    /// prepare for new deposit
    vm.startPrank(user_1);
    susd.approve(address(kangaroo), 5e23);

    // This deposit will revert due to division by zero.
    vm.expectRevert();
    kangaroo.initiateDeposit(user_1, 5e23);
    vm.stopPrank();

    // KangarooVault is now DoS and some funds are locked in it
    assertEq(susd.balanceOf(address(kangaroo)), 168969);
}

Recommended Mitigation Steps

Fix getTokenPrice() to handle the scenario when totalSupply() is zero.

Dravee (judge) commented:

Feels extremely similar to https://github.com/code-423n4/2023-03-polynomial-findings/issues/157 by the same warden, but the impact is on a different contract and requires a different POC. Won’t flag as a duplicate for now.

mubaris (Polynomial) confirmed

Dravee (judge) commented:

Will keep as high due to the warden showing in this case a direct impact on assets.


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, leading to a division by zero.

Impact

Funds are locked in the KangarooVault and the contract cannot recover from the denial of service.

What the attacker needed

All vault tokens withdrawn; remaining funds in contract; no open positions

The fix

Fix getTokenPrice() to handle the scenario when totalSupply() is zero.

How it was found

  1. Open a position to reduce available funds for withdrawals.
  2. Initiate a partial withdrawal and process the withdrawal queue.
  3. Close all positions and process the close orders.
  4. Process the remaining withdrawals, reducing total supply to zero.
  5. Attempt a new deposit, which reverts due to division by zero.

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 smart contract had a maths bug: it tried to divide by zero when no tokens were left, which made the contract stop working. Because of this, the money already in the contract got stuck and nobody could deposit or withdraw to fix it.

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.