Skip to content
Business logic Audit finding Intermediate

Raffle creator might not start raffle draw

The raffle contract does not require the creator to transfer the prize NFT until the draw is started, so a creator can collect participation fees and then refuse to start the draw, keeping the NFT. A creator can also pass the ownership check using a flash-loaned NFT and never actually own the prize.

gasperpre Follow with evan, hansfriese and 2 others

Originally disclosed on Code4rena

Submitted by gasperpre, also found by evan, hansfriese, SmartSek, and orion

https://github.com/code-423n4/2022-12-forgeries/blob/fc271cf20c05ce857d967728edfb368c58881d85/src/VRFNFTRandomDraw.sol#L173

https://github.com/code-423n4/2022-12-forgeries/blob/fc271cf20c05ce857d967728edfb368c58881d85/src/VRFNFTRandomDraw.sol#L304

https://github.com/code-423n4/2022-12-forgeries/blob/fc271cf20c05ce857d967728edfb368c58881d85/src/VRFNFTRandomDraw.sol#L127

Impact

The raffle creator is not required to actually give the NFT away. The NFT that is used for the raffle is transferred to the contract when startDraw is executed. Before that, the NFT is in the hands of the creator. This means that he might create a raffle to make users buy NFTs required to participate and then refuse to draw a winner and keep the NFT to himself. Furthermore, he might not even be the owner of the NFT in the first place, which he can achieve by flash loaning the NFT in order to pass the ownerOf check in initialize function.

Proof of Concept

Example 1

  1. User U creates an NFT collection C
  2. He buys a BAYC NFT
  3. He creates a raffle with it, and requires drawingToken to be from collection C
  4. Users buy tokens from his collection C
  5. He then refuses to execute startDraw function and rather sells the BAYC NFT

Example 2

  1. User U creates an NFT collection C
  2. User U uses an NFT flash loan to borrow a very expensive NFT
  3. In the same transaction he creates a raffle with this NFT, and requires drawingToken to be from collection C
  4. The check that he is the owner will pass, because for the duration of the transaction he in fact is
  5. Users see that there is a raffle for a very expensive NFT, so they buy tokens C
  6. The winner is never drawn, because the creator does not even own the NFT

Example 3

  1. User U has an NFT X
  2. He puts X on a sale on some NFT marketplace (which does not require him to lock it in contract)
  3. He forgets about it and creates a raffle with it
  4. Users buy the tokens necessary for the raffle
  5. User U wants to execute the startDraw function, but just before it the NFT X is bought from him through the marketplace
  6. The winner cannot be drawn

Recommended Mitigation Steps

Transfer the NFT to the contract at the time of creation of the raffle. You can do that by approving the factory contract to transfer the token and do the transfer in makeNewDraw function between cloning and initialization.

address newDrawing = ClonesUpgradeable.clone(implementation);
IERC721(settings.token).transferFrom(msg.sender, newDrawing, settings.tokenId);
        // Setup the new drawing
IVRFNFTRandomDraw(newDrawing).initialize(admin, settings);

Remember to remove token transfer from startDraw function.

Notice that the creator can still claim NFT after a week, without drawing, by executing lastResortTimelockOwnerClaimNFT. To prevent that, I would recommend adding a check in lastResortTimelockOwnerClaimNFT, if a winner was drawn.

 if (!request.hasChosenRandomNumber) {
            revert NEEDS_TO_HAVE_CHOSEN_A_NUMBER();
}

So now a user can trust that the NFT is locked in the contract, and it will be claimable only by a winner (or creator if the winner does not claim it). However, there is still no guarantee that the winner will actually be drawn, because the creator has to manually execute startDraw function. To fix this, I would recommend allowing anyone to execute startDraw function, so there is no need to rely on the creator. But we would need to limit the time window of when startDraw can be executed, so users have the time to get tokens before the drawing. That can be done by introducing a new state variable firstDrawTime, that acts as a timestamp after which drawing can happen.

if(block.timestamp < firstDrawTime) revert CANNOT_DRAW_YET();

Notice that now the NFT can only be claimed after the winner has been drawn. This means that we are depending on ChainLink VRF to be successful. For that reason I would recommend adding a role that has the power to change the VRF subscription or restore the NFT in cases where the winner is not picked in reasonable time. This role would be given to protocol owner (owner of the factory) / DAO / someone who would be considered as most reliable.

gzeon (judge) decreased severity to Medium and commented:

#359 (comment)

iainnash (Forgeries) confirmed


The original report as published on Code4rena, by gasperpre. 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 prize NFT is only transferred into the contract at draw time rather than at raffle creation, and the ownership check at initialisation can be satisfied by a temporary holder.

Impact

Participants can pay for entries and never have a winner drawn, while the creator keeps the prize; the creator may also never have owned the prize, having used a flash loan to pass the ownership check.

What the attacker needed

Creator deploys a token collection; participants buy entry tokens; creator controls draw timing

The fix

The prize NFT should be transferred into the contract when the raffle is created, the creator should not be able to claim it back before a winner is drawn, and anyone should be able to trigger the draw after a set time.

How it was found

  1. Create an NFT collection and obtain a valuable NFT.
  2. Create a raffle using that NFT and require participants to buy tokens from the creator's collection.
  3. Users buy the required tokens to enter.
  4. The creator refuses to execute the startDraw function and keeps or sells the NFT.

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 raffle lets people buy tokens for a chance to win a prize, here an NFT (a unique digital item). The contract only takes the prize from the creator when the draw begins, so the creator can take the entry money and simply never start the draw, keeping the prize. They can also pretend to own a prize by borrowing one for a single transaction.

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 from Forgeries 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.