Basket becomes unusable if everybody burns their shares
The contract calculates a fee ratio by dividing by the total supply of shares, which can be zero if all shares are burned. An attacker could burn all shares, causing future minting to revert and making the basket unusable.
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:
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
Alex the Entreprenerd (judge) commented:
I feel like this can also happen right after the contract was created
mintTowill callhandleFeeswhich will revert due to division by 0Finding is valid
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
The contract divides by totalSupply without checking if it is zero.
Impact
The basket becomes unusable because minting reverts, preventing any further deposits.
What the attacker needed
All shares are burned
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 the basket.
- Attempt to mint a small amount, which 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 basket lets people deposit tokens and get shares. If everyone sells their shares, the total number of shares becomes zero. Later, when someone tries to deposit again, the contract tries to divide by zero, which is not allowed, so the deposit fails and the basket is stuck.
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.