Skip to content

Instantly share code, notes, and snippets.

@wadealexc
Created June 29, 2018 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wadealexc/b270d51f6db70ebf8f0661c24f6d379f to your computer and use it in GitHub Desktop.
Save wadealexc/b270d51f6db70ebf8f0661c24f6d379f to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.23;
contract SafeExecution {
address public owner;
modifier noOwner() {
require(owner == 0, 'level already completed');
_;
}
bytes4 internal constant SET = bytes4(keccak256('Set(uint256)'));
function execute(address _target) public noOwner {
require(_target.delegatecall(abi.encodeWithSelector(this.execute.selector)) == false, 'unsafe execution');
(bytes4 sel, uint val) = getRet();
require(sel == SET);
function () func;
assembly { func := val }
func();
}
function getRet() internal pure returns (bytes4 sel, uint val) {
assembly {
if iszero(eq(returndatasize, 0x24)) { revert(0, 0) }
let ptr := mload(0x40)
returndatacopy(ptr, 0, 0x24)
sel := and(mload(ptr), 0xffffffff00000000000000000000000000000000000000000000000000000000)
val := mload(add(0x04, ptr))
}
}
function setOwnerExt() external { if (false) setOwner(); }
function setOwner() private { owner = msg.sender; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment