Puppy Raffle

AI First Flight #1
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Severity: high
Valid

totalFees Overflow Causes Permanent Fund Lock

Root + Impact

Description

Normal Behaviour
The contract tracks protocol fees collected from each raffle round in the totalFees state variable.
These accumulated fees are later withdrawn by the owner via withdrawFees().

Issue
totalFees is declared as a uint64, but fees are accumulated over time without any upper bound or overflow protection.
When enough raffles are completed, totalFees silently overflows, resetting to a smaller value. This causes a mismatch between:

  • the actual ETH balance held by the contract, and

  • the recorded totalFees value

As a result, withdrawFees() becomes permanently unusable, rendering the fee withdrawal mechanism unreliable and permanently corrupting the accounting invariant

// @> totalFees uses uint64
uint64 public totalFees;
function selectWinner() external {
// ...
uint256 totalAmountCollected = players.length * entranceFee;
uint256 fee = (totalAmountCollected * 20) / 100;
// @> Root Cause: uint256 fee is narrowing casted to uint64 without overflow checks
// uint64 max is ~18.44 ETH. If totalFees + fee > 18.44 ETH, it wraps around.
totalFees = totalFees + uint64(fee);
// ...
}

Solidity 0.7.x does not revert on arithmetic overflow, resulting in silent wraparound and permanent accounting corruption.

Risk

Likelihood:

  • Reason 1: Fees accumulate every time a raffle is completed.

  • Reason 2: There is no cap, no reset, and no overflow check.

Impact:

  • Impact 1: totalFees no longer reflects the real ETH owed to the owner.

  • Impact 2: withdrawFees() reverts due to insufficient balance or incorrect accounting.

Proof of Concept

This bug is time-based, not attacker-based.

  1. Each raffle adds fees to totalFees

  2. uint64 max value ≈ 1.84e19

  3. After enough raffles, totalFees wraps around

  4. Internal accounting breaks permanently

// Pseudocode illustration
totalFees = type(uint64).max - 1;
totalFees += 10; // wraps to a small value

After overflow:

  • Contract balance is large

  • totalFees is small

  • Owner cannot withdraw correct amount

This is irreversible.

Recommended Mitigation

Option 1 (Best)

- uint64 public totalFees;
+ uint256 public totalFees;
- totalFees += uint64(fee);
+ totalFees += fee;

Option 2

Use SafeMath or checked arithmetic (less ideal in 0.7.x).

Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge 1 day ago
Submission Judgement Published
Validated
Assigned finding tags:

[H-06] Overflow/Underflow vulnerabilty for any version before 0.8.0

## Description The PuppyRaffle.sol uses Solidity compiler version 0.7.6. Any Solidity version before 0.8.0 is prone to Overflow/Underflow vulnerability. Short example - a `uint8 x;` can hold 256 values (from 0 - 255). If the calculation results in `x` variable to get 260 as value, the extra part will overflow and we will end up with 5 as a result instead of the expected 260 (because 260-255 = 5). ## Vulnerability Details I have two example below to demonstrate the problem of overflow and underflow with versions before 0.8.0, and how to fix it using safemath: Without `SafeMath`: ``` function withoutSafeMath() external pure returns (uint256 fee){ uint8 totalAmountCollected = 20; fee = (totalAmountCollected * 20) / 100; return fee; } // fee: 1 // WRONG!!! ``` In the above code,`without safeMath`, 20x20 (totalAmountCollected \* 20) was 400, but 400 is beyond the limit of uint8, so after going to 255, it went back to 0 and started counting from there. So, 400-255 = 145. 145 was the result of 20x20 in this code. And after dividing it by 100, we got 1.45, which the code showed as 1. With `SafeMath`: ``` function withSafeMath() external pure returns (uint256 fee){ uint8 totalAmountCollected = 20; fee = totalAmountCollected.mul(20).div(100); return fee; } // fee: 4 // CORRECT!!!! ``` This code didnt suffer from Overflow problem. Because of the safeMath, it was able to calculate 20x20 as 400, and then divided it by 100, to get 4 as result. ## Impact Depending on the bits assigned to a variable, and depending on whether the value assigned goes above or below a certain threshold, the code could end up giving unexpected results. This unexpected OVERFLOW and UNDERFLOW will result in unexpected and wrong calculations, which in turn will result in wrong data being used and presented to the users. ## Recommendations Modify the code to include SafeMath: 1. First import SafeMath from openzeppelin: ``` import "@openzeppelin/contracts/math/SafeMath.sol"; ``` 2. then add the following line, inside PuppyRaffle Contract: ``` using SafeMath for uint256; ``` (can also add safemath for uint8, uint16, etc as per need) 3. Then modify the `require` inside `enterRaffle() function`: ```diff - require(msg.value == entranceFee * newPlayers.length, "PuppyRaffle: Must send enough to enter raffle"); + uint256 totalEntranceFee = newPlayers.length.mul(entranceFee); + require(msg.value == totalEntranceFee, "PuppyRaffle: Must send enough to enter raffle"); ``` 3. Then modify variables (`totalAmountCollected`, `prizePool`, `fee`, and `totalFees`) inside `selectWinner()` function: ```diff - uint256 totalAmountCollected = players.length * entranceFee; + uint256 totalAmountCollected = players.length.mul(entranceFee); - uint256 prizePool = (totalAmountCollected * 80) / 100; + uint256 prizePool = totalAmountCollected.mul(80).div(100); - uint256 fee = (totalAmountCollected * 20) / 100; + uint256 fee = totalAmountCollected.mul(20).div(100); - totalFees = totalFees + uint64(fee); + totalFees = totalFees.add(fee); ``` This way, the code is now safe from Overflow/Underflow vulnerabilities.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!