Skip to content
Business logic Audit finding Advanced

Insolvency: The Lottery may incorrectly consider a year old jackpot ticket as unclaimed and increase currentNetProfit by its prize while it was actually claimed

The lottery contract could treat a one-year-old jackpot ticket as unclaimed and add its prize to the current net profit, while the ticket owner could still claim the prize in the same transaction, leaving the contract insolvent. An attacker could exploit this edge case to receive their prize while the contract incorrectly counts it as unclaimed.

NoamYakov Follow

Originally disclosed on Code4rena

Submitted by NoamYakov

https://github.com/code-423n4/2023-03-wenwin/blob/main/src/Lottery.sol#L135-L137

https://github.com/code-423n4/2023-03-wenwin/blob/main/src/Lottery.sol#L164-L166

Impact

According to the documentation:

“Winning tickets have up to 1 year to claim their prize. If a prize is not claimed before this period, the unclaimed prize money will go back to the Prize Pot.”

As part of this mechanism, the Lottery.executeDraw() function (which internally calls Lottery.returnUnclaimedJackpotToThePot()) increases Lottery.currentNetProfit by the prize of any 1 year old unclaimed jackpot ticket. At this point, the contract thinks it still holds that prize and it’s going to include it in the prize pot of the next draw. This function can only be called when block.timestamp >= drawScheduledAt(currentDraw).

On the other side, the Lottery.claimWinningTickets() function (which internally calls Lottery.claimWinningTicket() and Lottery.claimable()) sends the prize to the owner of a jackpot ticket only if it isn’t 1 year old (if block.timestamp <= ticketRegistrationDeadline(ticketInfo.drawId + LotteryMath.DRAWS_PER_YEAR)).

However, if Lottery.drawCoolDownPeriod is zero, both of these conditions can pass on the same time - when the block.timestamp is exactly the scheduled draw date of the draw that takes place exactly 1 year after the draw in which the jackpot ticket has won.

In this edge case, the owner of the jackpot ticket can call Lottery.executeDraw(), letting the Lottery think the prize wasn’t claimed, followed by Lottery.claimWinningTickets(), claiming the prize, all in the same transaction.

Proof of Concept

Let’s say Eve buys a ticket to draw #1 in a Lottery contract where Lottery.drawCoolDownPeriod equals zero, and wins the jackpot. Now, Eve can wait 1 year and then, when block.timestamp equals the scheduled draw date of draw #53 (which is also the registration deadline of draw #53), run a transaction that will do the following:

lottery.executeDraw()
lottery.claimWinningTickets([eveJackpotTicketId])

This will send Eve her prize, but will also leave the contract insolvent.

Recommended Mitigation Steps

Fix Lottery.claimable() to set claimableAmount to winAmount[ticketInfo.drawId][winTier] only if block.timestamp < ticketRegistrationDeadline(ticketInfo.drawId + LotteryMath.DRAWS_PER_YEAR) (strictly less than).

rand0c0des (Wenwin) disagreed with severity and commented:

I think this is Medium, it requires coolDownPeriod of 0 + needs the block executed at the actual time scheduled which might not happen that often.

cccz (judge) decreased severity to Medium and commented:

Due to the requirement drawCoolDownPeriod == 0, consider Medium.


The original report as published on Code4rena, by NoamYakov. 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 claimable check uses a non-strict comparison (<=) for the deadline, allowing a ticket to be both claimable and considered unclaimed at the exact deadline timestamp.

Impact

The contract could become insolvent because it pays out a prize while also counting it as unclaimed and adding it to the prize pot, but this requires drawCoolDownPeriod to be zero and the transaction to occur at the exact scheduled time.

What the attacker needed

DrawCoolDownPeriod equals zero; attacker owns a winning jackpot ticket; transaction at exact scheduled draw time

The fix

The claimable function should use a strict less-than comparison for the deadline to prevent the overlap.

How it was found

  1. Eve buys a ticket for draw #1 and wins the jackpot.
  2. Eve waits one year until the scheduled draw date of draw #53.
  3. Eve calls executeDraw() to make the contract consider the jackpot unclaimed.
  4. Eve calls claimWinningTickets() for her jackpot ticket in the same transaction.
  5. Eve receives her prize and the contract becomes insolvent.

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 lottery contract tracks unclaimed prizes and adds them back to the prize pool after a year. If a winner claims exactly when the contract checks for unclaimed prizes, the contract might think the prize is unclaimed and add it to the pool, but the winner still gets paid. This means the contract pays out more than it holds, becoming insolvent.

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 Wenwin 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.