The claimAllRewards and claimSingleReward functions are vulnerable to a reentrancy attack because they transfer Ether to the user before updating the contract’s state. This allows an attacker to repeatedly call the function and drain funds from the contract by re-entering the function before the contract's state is properly updated, leading to a depletion of the contract's balance and loss of funds.
Both the claimAllRewards and claimSingleReward functions perform an Ether transfer to the user before updating the contract's state (deleting rewards). This exposes the contract to a reentrancy attack, where an attacker can re-enter the function during the external call (the transfer of Ether) and repeatedly claim rewards or deplete the contract's balance.
Here’s the problematic part of the code in claimAllRewards
Similarly, in claimSingleReward:
Both functions first send Ether using the call method, and only after the transfer is complete, they update the state of the contract by deleting the rewards. This opens the door for a reentrancy attack, where a malicious contract can repeatedly call these functions during the Ether transfer (before the rewards are deleted), draining the contract of its funds.
The impact of this vulnerability is severe:
Full contract drain: An attacker could repeatedly call claimAllRewards or claimSingleReward and drain the contract's balance before the rewards are properly deleted, causing significant financial loss.
Loss of trust: Users may lose trust in the protocol if the contract’s funds are drained due to such an exploit.
Manual code review
To prevent reentrancy attacks, follow the checks-effects-interactions pattern, which ensures that the contract's state is updated before any external calls (like transferring Ether). Here’s how the functions can be rewritten:
Fix in claimAllRewards:
Fix in claimSingleReward:
Reentrancy Guard: Consider using OpenZeppelin’s ReentrancyGuard to provide additional protection against reentrancy attacks by ensuring that a function cannot be re-entered before its execution completes.
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.