The enterRaffle
function of the given smart contract is vulnerable to a Denial of Service (DoS) attack due to the potential to exceed the block gas limit. This vulnerability arises from the nested loop utilized for checking duplicate addresses, which can result in excessive gas consumption and consequently, transaction failure.
The enterRaffle
function allows a user to enter a raffle by providing an array of player addresses. The function requires the sent value to match the product of the entrance fee and the number of new players. It then iterates through the newPlayers
array, adding each player to the players
array. Subsequently, it employs a nested loop to check for duplicate player addresses among all players. The nested loop has a time complexity of O(n^2), making the gas cost of this operation grow quadratically with the number of players.
An attacker can exploit this vulnerability by sending a large array of unique addresses to the enterRaffle
function, causing the transaction to fail due to exceeding the block gas limit. This attack can be repeated, blocking legitimate users from entering the raffle and thus denying service.
Add this POC to the foundry test
Solidity ^0.8.0
Truffle for testing
Limit the number of players that can enter the raffle in a single transaction to a reasonable amount that will not exceed the block gas limit.
Optimize the duplicate check: Instead of using a nested loop, consider using a mapping to check for duplicate addresses, which would reduce the time complexity from O(n^2) to O(n).
Implement gas checks within the function to ensure that sufficient gas remains for the function to complete execution, and fail gracefully if not.
Example:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.