Skip to content
Business logic Audit finding Intermediate

Lack of sanity check on _initialTokenSupply and _initialTokenPrice can lead to a seller losing his NFT

The contract lacks a sanity check on the initial token supply and price, so if their product is too small, the primary reserve balance becomes zero due to rounding. This prevents buying and selling, and the NFT owner may lose the NFT in a gas war when trying to recover it via initiateBuyout.

itsmeSTYJ Follow

Originally disclosed on Code4rena

Submitted by itsmeSTYJ

There is no sanity check to ensure that (primaryReserveRatio * _initialTokenSupply * _initialTokenPrice) is ≥ (SCALE * 1e18). As a result, _primaryReserveBalance is given a value of 0 since divisions in solidity are rounded down. This also means that primaryReserveBalance and fictitiousPrimaryReserveBalance have a value of 0.

When this happens, the buy function doesn’t work because _chargeFee will revert on line 222. You can’t sell either since totalSupply == initialSupply. The only way to recover the NFT is for the owner of the NFT to call initiateBuyout but there is always the possibility that someone else also spotted this mistake and will attempt to also call initiateBuyout once the minBuyoutTime is reached. If the owner loses this gas war, the owner has effectively lost his NFT.

Recommended Mitigation Steps

Add some sanity checks to ensure a sane expected value for _initialTokenSupply and _initialTokenPrice. There were multiple instances when a user tried to interact with a contract but entered a wrong value because they are not aware they needed to include decimals. A recent example of this is https://cointelegraph.com/news/1-million-rock-nft-sells-for-a-penny-in-all-ore-nothing-error.


Note: Normally, I would categorise issues like this as medium severity since it is a loss predicated on having met certain conditions but because there is also a lack of sanity check on _minBuyoutTime, it is entirely possible for a seller to lose his NFT immediately once the vault is created. There are many monsters waiting in the dark forest, all it takes is one mistake. That said, I will defer the final judgement to the judges & sponsors.

mundhrakeshav (Nibbl) disputed, disagreed with severity and commented:

Case when that would happen is _initialTokenSupply or _initialTokenPrice is 0. But then it would revert here https://github.com/code-423n4/2022-06-nibbl/blob/8c3dbd6adf350f35c58b31723d42117765644110/contracts/NibblVault.sol#L183

HardlyDifficult (judge) commented:

Case when that would happen is _initialTokenSupply or _initialTokenPrice is 0.

primaryReserveRatio = 200000 and SCALE = 1000000 — so it seems this applies anytime _initialTokenSupply * _initialTokenPrice < 1000000 * 1e18 / 200000, not just when one of those values is 0. Please correct me if I got that wrong.

HardlyDifficult (judge) decreased severity to Medium and commented:

This is a good report. I agree this seems like something that should be addressed. Assuming my math is right, if the initialTokenPrice was 1 wei then the initialTokenSupply must be >= 5e18. They are using the default of 18 decimals (which is also industry standard) so that’s 5 tokens. Given this is not very large window and these values impact the curve — without a clear POC High would not be warranted, downgrading to Medium risk.


The original report as published on Code4rena, by itsmeSTYJ. 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 contract does not validate that the product of the primary reserve ratio, initial token supply, and initial token price is large enough to avoid rounding down to zero in the reserve balance calculation.

Impact

The buy function reverts and sell is impossible, so the NFT owner must call initiateBuyout; if someone else calls it first, the owner could lose the NFT.

What the attacker needed

InitialTokenSupply and initialTokenPrice set such that…; vault created with these values

The fix

Add sanity checks to ensure sane expected values for _initialTokenSupply and _initialTokenPrice.

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.

This is a smart contract for buying and selling NFTs. If the initial supply and price are set too low, a calculation rounds down to zero, which breaks the buy and sell functions. The NFT owner then has to use a special function to recover the NFT, but someone else might beat them to it, causing the owner to lose the NFT.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by itsmeSTYJ

View all

9 more disclosed reports, best first.

More from Nibbl contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.