Reentrancy attack in cancelRegistration()
from ThePredictor.sol
: Risk = High
https://github.com/Cyfrin/2024-07-the-predicter/blob/main/src/ThePredicter.sol#L64
If msg.sender is a contract, a user can use cancelRegistration()
to drain all eth in the contract with a reentrancy attack.
Let's create a contract that is going to be a user that will pay the entrance fee to be registered as a user but right after is going to cancel the registration.
If this contract has a fallback
function that recalls the cancelRegistration()
function in a loop, then it can drain/steal eth from the game's contract.
The attacker contract is as follow :
First Deploy the game's contract setting entranceFee
to 10
. Give the contract 1 eth for this example.
Deploy the attackerContract
passing the predictorAddress
in the parameter.
execute registerUser()
with entranceFee
Then execute the attackPredictor()
function.
Check the balance of our attacker contract to verify that we indeed stole 10 times entranceFee
.
Stealing ETH from the contract. High impact.
Brain, VisualCode & Foundry.
Mitigation
2 possibilities :
- Add a nonReentrant
modifier to avoid reentering the cancelRegistration()
function
- Or cancel the status of the user BEFORE sending eth, by replacing these lines of code (L64-66):
https://github.com/Cyfrin/2024-07-the-predicter/blob/main/src/ThePredicter.sol#L64-L66
-> by these lines of code :
Reentrancy of ThePredicter::cancelRegistration allows a maliciour user to drain all funds.
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.