Dynamic modification of maxPrizeCount affects prize claims
The contract owner can reduce maxPrizeCount after prizes are set, causing winners of higher-indexed prizes to be unable to claim them. Claiming also clears entries, so even restoring the count does not allow recovery.
EV_om Follow with 0xDING99YA and rvierdiiev
Originally disclosed on Code4rena
Submitted by EV_om, also found by 0xDING99YA and rvierdiiev
The ThrusterTreasure contract is designed to manage rounds of a lottery game, where participants can enter tickets and claim prizes based on random draws. The contract includes a variable maxPrizeCount which dictates the maximum number of prizes that can be set for any given round. This variable can be modified by the contract owner at any time through the setMaxPrizeCount(uint256 _maxPrizeCount) function:
ThrusterTreasure.sol#L139-L142
function setMaxPrizeCount(uint256 _maxPrizeCount) external onlyOwner {
maxPrizeCount = _maxPrizeCount;
emit SetMaxPrizeCount(_maxPrizeCount);
}
The issue arises when maxPrizeCount is decreased after prizes for a round have been set but before they have been claimed. Since the claimPrizesForRound(uint256 roundToClaim) function iterates over prize indices up to maxPrizeCount, reducing this count means that winners of prizes with indices higher than the new maxPrizeCount will be unable to claim their winnings:
ThrusterTreasure.sol#L102-L120
function claimPrizesForRound(uint256 roundToClaim) external {
...
uint256 maxPrizeCount_ = maxPrizeCount;
for (uint256 i = 0; i < maxPrizeCount_; i++) {
[claim prize]
}
entered[msg.sender][roundToClaim] = Round(0, 0, roundToClaim); // Clear user's tickets for the round
emit CheckedPrizesForRound(msg.sender, roundToClaim);
}
This could lead to a scenario where legitimate winners are denied their prizes due to a change in contract state that is unrelated to the rules of the game or their actions. Moreover, since calling claimPrizesForRound() clears the user’s entries for the round, reverting maxPrizeCount to its previous state does not allow them to claim the remaining tickets. This means they will effectively never be able to claim their prize.
Proof of Concept
- The contract owner sets
maxPrizeCountto 5 and configures five prizes for a given round. - Users participate in the round, and the round concludes with winners determined for all five prizes.
- The contract owner reduces
maxPrizeCountto 3 for the next round. - Winners of prizes 4 and 5 attempt to claim their prizes but are unable to do so because the
claimPrizesForRound(uint256 roundToClaim)function now iterates only up to the newmaxPrizeCountof 3.
Recommended Mitigation Steps
To address this issue, implementing a checkpoint pattern for the maxPrizeCount variable is suggested. This method involves tracking changes to maxPrizeCount with checkpoints that record the value and the round number when the change occurs.
A possible implementation could look like this:
// Add a struct to store checkpoints for maxPrizeCount changes
struct MaxPrizeCountCheckpoint {
uint256 round;
uint256 maxPrizeCount;
}
// Use an array to keep track of all checkpoints
MaxPrizeCountCheckpoint[] public maxPrizeCountCheckpoints;
constructor(
...
) Ownable(msg.sender) {
maxPrizeCountCheckpoints.push(
MaxPrizeCountCheckpoint(0, _maxPrizeCount)
);
...
}
// Modify setMaxPrizeCount to push a new checkpoint to the array
function setMaxPrizeCount(uint256 _maxPrizeCount) external onlyOwner {
require(_maxPrizeCount != getMaxPrizeCountForRound(currentRound), "same value")
maxPrizeCountCheckpoints.push(
MaxPrizeCountCheckpoint(currentRound, _maxPrizeCount)
);
emit SetMaxPrizeCount(_maxPrizeCount);
}
// Helper function to get the maxPrizeCount for a given round
// Assumes more recent rounds will be queried more often
function getMaxPrizeCountForRound(uint256 _round) public view returns (uint256) {
uint256 length = maxPrizeCountCheckpoints.length;
for (uint256 i = length; i > 0; i--) {
MaxPrizeCountCheckpoint storage checkpoint = maxPrizeCountCheckpoints[i - 1];
if (checkpoint.round <= _round) {
return checkpoint.maxPrizeCount;
}
}
return 0;
}
// Disallow setting prizes for future rounds since the maxPrizeCount could change
function setPrize(uint64 _prizeIndex, uint256 _amountWETH, uint256 _amountUSDB, uint64 _numWinners) external onlyOwner {
uint256 maxPrizeCount = getMaxPrizeCountForRound(currentRound);
require(_prizeIndex < maxPrizeCount, "IPC");
...
}
function claimPrizesForRound(uint256 roundToClaim) external {
uint256 maxPrizeCount = getMaxPrizeCountForRound(roundToClaim);
...
}
This change ensures that each round’s prize structure is fixed upon the round’s creation, preventing post-hoc alterations that could negatively impact participants. Note that this implementation still requires attention is paid to not calling setMaxPrizeCount() for a given round if prizes have already been set for higher indices.
jooleseth (Thruster) commented:
I would consider this QA to ensure to add a require check that
maxPrizeCountcannot be decreased as that is the intention.
That’s indeed a much simpler fix!
I would still say Medium severity is appropriate seeing as that could not have been inferred from the audit scope and given the potential impact, but either way I’ll accept the judge’s decision.
jooleseth (Thruster) confirmed
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 claim function reads the current global maxPrizeCount instead of the value that was in effect when the round's prizes were set.
Impact
Legitimate winners of higher-indexed prizes could be permanently denied their prizes, even if the owner later restores the original count.
What the attacker needed
Contract owner reduces maxPrizeCount; prizes already set for a round; winners have not yet claimed
The fix
The contract should prevent decreasing maxPrizeCount, or use a checkpoint pattern to record the value per round so claims use the count that was in effect when prizes were set.
How it was found
- The owner sets maxPrizeCount to 5 and configures five prizes for a round.
- Users participate and winners are determined for all five prizes.
- The owner reduces maxPrizeCount to 3 for the next round.
- Winners of prizes 4 and 5 try to claim but cannot because the claim function now iterates only up to 3.
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 lets the owner change a setting that controls how many prizes are checked when someone claims. If the owner lowers that number after winners are chosen, people who won later prizes cannot claim them, and their entries are wiped when they try.
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.