In this raffle protocol, each player contributes an entrance fee of 1e18 wei. The protocol's share is calculated based on the totalFees
variable.
The vulnerability lies in the usage of the uint64 data type for the totalFees
variable, which is utilized in the selectWinner()
function, as illustrated by the formula:
uint64 is used for totalFees
as a variable and also same is used in selectWinner()
function. Depending on the number of players who enter the raffle there is a risk of overflow.
Due to the use of uint64 for the totalFees
variable, there is a risk of overflow depending on the number of players participating in the raffle. If the fee value surpasses the maximum value of uint64 (18446744073709551615), the calculation will yield an incorrect result.
Let's calculate what the maximum totalAmountCollected
can be:
uint256 fee = (totalAmountCollected * 20) / 100;
18446744073709551615 = (totalAmountCollected
* 20) / 100
totalAmountCollected
is in wei. Let's divide both side of the equation by 1e18
18.44 = (totalAmountCollected
in ether) * 20 /100
If the number of players exceeds 92 the protocol fee will be miscalculated.
Given that the maximum integer value of uint64 is 18446744073709551615, any situation where the fee value in the formula exceeds this maximum value will result in miscalculations. As calculated above, if the number of players surpasses 92, the protocol fee will be incorrectly calculated.
To prove this vulnerability, the provided Proof of Concept (PoC) demonstrates a scenario with 93 players, showcasing how the totalFees
calculation would exceed the maximum value of uint64. The resulting miscalculation of the totalFees
is evident from the PoC
Below is the PoC used to prove:
Manual code review
Foundry
To mitigate this issue, it is recommended to use the uint256 data type instead of uint64 for variables such as totalFees
. This adjustment would prevent potential overflow problems and ensure the accurate calculation of fees, even for larger numbers of participants in the raffle.
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.