Skip to content
Smart contracts Audit finding Intermediate

randomIndex is not truly random - possibility of predictably minting a specific token Id

The contract generates token IDs using a pseudo-random value derived from miner-controllable inputs, allowing miners to predict and mint a specific token ID. Non-miner attackers could also repeatedly test the minting condition until they obtain the desired ID.

Larva Labs Meebits

Originally disclosed on Code4rena

`randomIndex’ is not random. Any miner has access to these values:

uint index = uint(keccak256(abi.encodePacked(nonce, msg.sender, block.difficulty, block.timestamp))) % totalSize;

Non-miner attackers could also test the minting random condition until they get the ID they are looking to access.

The internal variable indices seems to be used to avoid this type of collision.

While this makes it less straightforward, there is still the possibility of minting a token with a specific ID.

That said, _addNFToken is checking if the token is already owned by an address, ensuring a token can’t be stolen.

Refactoring as suggested below will save gas, make code easier to read and prevent reverts in rare unfortunate occasions of clashes.

Recommend not generating random IDs and instead using counters. It makes the code more predictable and easier to read, avoids clashing of IDs, and reduces the need to track minted tokens.

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

The random index is derived from miner-controllable values (block.difficulty, block.timestamp) and the sender's address, making it predictable.

Impact

A miner could predictably mint a specific token ID, and non-miners could repeatedly attempt to mint until they obtain a desired ID, potentially undermining fairness in token distribution.

What the attacker needed

Ability to mine blocks or repeatedly call mint

The fix

The code should be refactored to use a counter instead of generating random IDs, which would save gas, improve readability, and prevent ID clashes.

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.

The contract tries to pick a random number to decide which token ID to mint, but it uses information that miners can see and control, like the block timestamp. This means a miner can predict the result and choose to mint a specific token ID. Even regular users could keep trying until they get the ID they want.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More from Larva Labs Meebits

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.