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.
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
totalSupplyto 0, which will causehandleFeesto revert. The finding is valid and I agree with the severity as this can happen if every share holder decides to burnPersonally I think moving
handleFeesto 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 ensuretotalSupplynever reaches 0
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
- Burn all shares from two accounts
- Approve tokens for minting
- 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.