Skip to content
DoS Audit finding Intermediate

Basket becomes unusable if everybody burns their shares

The contract calculates a fee ratio by dividing by totalSupply, which can be zero if all shares are burned. This causes a division-by-zero revert, making the basket unusable for future mints.

kenzo Follow

Originally disclosed on Code4rena

Submitted by kenzo.

While handling the fees, the contract calculates the new ibRatio by dividing by totalSupply. This can be 0 leading to a division by 0.

Impact

If everybody burns their shares, in the next mint, totalSupply will be 0, handleFees will revert, and so nobody will be able to use the basket anymore.

Proof of Concept

Vulnerable line: https://github.com/code-423n4/2021-09-defiProtocol/blob/52b74824c42acbcd64248f68c40128fe3a82caf6/contracts/contracts/Basket.sol#L124 You can add the following test to Basket.test.js and see that it reverts (..after you remove “nonReentrant” from “mint”, see other issue):

it("should divide by 0", async () => {
await basket.connect(addr1).burn(await basket.balanceOf(addr1.address));
await basket.connect(addr2).burn(await basket.balanceOf(addr2.address));
await UNI.connect(addr1).approve(basket.address, ethers.BigNumber.from(1));
await COMP.connect(addr1).approve(basket.address, ethers.BigNumber.from(1));
await AAVE.connect(addr1).approve(basket.address, ethers.BigNumber.from(1));
await basket.connect(addr1).mint(ethers.BigNumber.from(1));
});

Tools Used

Manual analysis, hardhat.

Recommended Mitigation Steps

Add a check to handleFees: if totalSupply= 0, you can just return, no need to calculate new ibRatio / fees. You might want to reset ibRatio to BASE at this point.

frank-beard (Kuiper) confirmed

itsmetechjay (organizer) commented:

Warden apologizes for linking the code of the previous Kuiper contest, however these lines are not changed in the new contest.

Alex the Entreprenerd (judge) commented:

Burning all shares will bring totalSupply to 0, which will cause handleFees to revert. The finding is valid and I agree with the severity as this can happen if every share holder decides to burn

Personally I think moving handleFees to a separate external call would be a simple mitigation (which also reduces gas cost for all users) Alternatively, the sponsor could always mint a few shares for each basket, to ensure totalSupply never reaches 0

The original report as published on Code4rena, by kenzo. 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

Impact

If all shares are burned, the basket becomes unusable because minting reverts, preventing any further use.

What the attacker needed

All share holders burn their shares

The fix

Add a check in handleFees to return early if totalSupply is zero, and optionally reset ibRatio to BASE.

How it was found

  1. Burn all shares from two accounts
  2. Approve tokens for minting
  3. Attempt to mint a small amount, which triggers handleFees and reverts

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 keeps track of shares owned by users. If every user sells or burns all their shares, the total number of shares becomes zero. The contract then tries to divide by that zero when calculating fees, which is not allowed in programming and causes an error, so no one can use the contract afterwards.

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 kenzo

View all

53 more disclosed reports, best first.

More from Kuiper 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.