Puppy Raffle

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

totalFees uint64 downcast silently overflows above ~18.44 ETH in fees, permanently bricking withdrawFees()

Summary

totalFees is stored as a uint64, and the codebase targets Solidity ^0.7.6, which performs no automatic overflow/underflow checking (this predates Solidity 0.8's built-in checks). Once a single round's 20% fee exceeds type(uint64).max wei (~18.446744073709551615 ETH), the uint64(fee) downcast in selectWinner silently wraps, permanently desynchronizing totalFees from the real ETH sitting in the contract -- which bricks withdrawFees forever, since it requires an exact balance match.

Description

uint64 public totalFees = 0;
...
uint256 fee = (totalAmountCollected * 20) / 100;
totalFees = totalFees + uint64(fee); // @> unchecked downcast, wraps silently on 0.7.x
...
function withdrawFees() external {
require(address(this).balance == uint256(totalFees), "PuppyRaffle: There are currently players active!");
...
}

fee is a full uint256, but it's narrowed to uint64 before being added to totalFees. On Solidity 0.7.x this narrowing is unchecked, so once fee (or the running total) exceeds 2^64 - 1 wei, the high bits are silently dropped. The contract's actual ETH balance is unaffected (it really did receive all of that fee's ETH) -- but the bookkeeping variable totalFees no longer matches it, and withdrawFees's exact-equality check can then never pass again.

Risk

Likelihood:

  • Requires a single round to collect more than ~92.2 ETH total (20% of which is ~18.44 ETH) -- plausible for a raffle with a meaningful entrance fee and a healthy number of players, and only gets easier to trigger as totalFees accumulates fee from multiple rounds without ever being withdrawn in between.

  • No attacker cooperation is needed beyond normal popular usage of the raffle; it can also be hit intentionally by anyone willing to fund enough entries in one round.

Impact:

  • Once triggered, withdrawFees reverts unconditionally and permanently (the exact-balance check can never be satisfied again), locking the fee owner's share of every player's payment in the contract forever with no recovery path.

Proof of Concept

function test_H4_totalFeesUint64Truncation_bricksFeeWithdrawal() public {
uint256 bigEntranceFee = 10 ether;
PuppyRaffle bigRaffle = new PuppyRaffle(bigEntranceFee, feeAddress, duration);
uint256 n = 10; // 10 * 10 ETH = 100 ETH collected -> 20% fee = 20 ETH > uint64.max wei
address[] memory players = new address[](n);
for (uint256 i = 0; i < n; i++) players[i] = address(uint160(0x2000 + i));
vm.deal(address(this), bigEntranceFee * n);
bigRaffle.enterRaffle{value: bigEntranceFee * n}(players);
vm.warp(block.timestamp + duration);
bigRaffle.selectWinner();
uint256 realFee = (bigEntranceFee * n * 20) / 100; // 20 ether
assertTrue(uint256(bigRaffle.totalFees()) != realFee, "totalFees silently truncated vs. the real fee owed");
vm.expectRevert("PuppyRaffle: There are currently players active!");
bigRaffle.withdrawFees(); // permanently reverts from here on
}

Run with forge test --match-test test_H4_totalFeesUint64Truncation_bricksFeeWithdrawal -vv.

Recommended Mitigation

Store totalFees as a uint256 instead of uint64 (there is no meaningful gas benefit to packing it tightly here), and/or explicitly bound fee accumulation with a checked-arithmetic library so any real overflow reverts loudly instead of silently wrapping.

Updates

Lead Judging Commences

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

[H-05] Typecasting from uint256 to uint64 in PuppyRaffle.selectWinner() May Lead to Overflow and Incorrect Fee Calculation

## Description ## Vulnerability Details The type conversion from uint256 to uint64 in the expression 'totalFees = totalFees + uint64(fee)' may potentially cause overflow problems if the 'fee' exceeds the maximum value that a uint64 can accommodate (2^64 - 1). ```javascript totalFees = totalFees + uint64(fee); ``` ## POC <details> <summary>Code</summary> ```javascript function testOverflow() public { uint256 initialBalance = address(puppyRaffle).balance; // This value is greater than the maximum value a uint64 can hold uint256 fee = 2**64; // Send ether to the contract (bool success, ) = address(puppyRaffle).call{value: fee}(""); assertTrue(success); uint256 finalBalance = address(puppyRaffle).balance; // Check if the contract's balance increased by the expected amount assertEq(finalBalance, initialBalance + fee); } ``` </details> In this test, assertTrue(success) checks if the ether was successfully sent to the contract, and assertEq(finalBalance, initialBalance + fee) checks if the contract's balance increased by the expected amount. If the balance didn't increase as expected, it could indicate an overflow. ## Impact This could consequently lead to inaccuracies in the computation of 'totalFees'. ## Recommendations To resolve this issue, you should change the data type of `totalFees` from `uint64` to `uint256`. This will prevent any potential overflow issues, as `uint256` can accommodate much larger numbers than `uint64`. Here's how you can do it: Change the declaration of `totalFees` from: ```javascript uint64 public totalFees = 0; ``` to: ```jasvascript uint256 public totalFees = 0; ``` And update the line where `totalFees` is updated from: ```diff - totalFees = totalFees + uint64(fee); + totalFees = totalFees + fee; ``` This way, you ensure that the data types are consistent and can handle the range of values that your contract may encounter.

Support

FAQs

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

Give us feedback!