From 26ef0d5507856730136863788db3fe3107f53d3a Mon Sep 17 00:00:00 2001 From: Gonzalo Balabasquer Date: Mon, 21 Jan 2019 11:16:35 -0300 Subject: [PATCH 1/2] Add check for invalid target. Closes #24 --- src/proxy.sol | 7 ++++++- src/proxy.t.sol | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/proxy.sol b/src/proxy.sol index 4f475bc..0bd545a 100644 --- a/src/proxy.sol +++ b/src/proxy.sol @@ -58,11 +58,16 @@ contract DSProxy is DSAuth, DSNote { returns (bytes memory response) { require(_target != address(0), "ds-proxy-target-address-required"); + uint size; + assembly { + size := extcodesize(_target) + } + require(size > 0, "ds-proxy-target-invalid-address"); // call contract in current context assembly { let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0) - let size := returndatasize + size := returndatasize response := mload(0x40) mstore(0x40, add(response, and(add(add(size, 0x20), 0x1f), not(0x1f)))) diff --git a/src/proxy.t.sol b/src/proxy.t.sol index ddf6cbf..a359514 100644 --- a/src/proxy.t.sol +++ b/src/proxy.t.sol @@ -319,6 +319,19 @@ contract DSProxyTest is DSTest { assertEq(address(this).balance, myBalance + 5); } + ///test 14 - check failure when an address without code is used as target + function testFail_executeNonContract() public { + bytes memory data = abi.encodeWithSignature("test()"); + proxy.execute(address(0x1), data); + } + + ///test 15 - check failure when a non existing function is called + function testFail_executeNonFunction() public { + address testContract = address(new TestContract()); + bytes memory data = abi.encodeWithSignature("nonExistingFunction()"); + proxy.execute(testContract, data); + } + function() external payable { } } From 5c91a66c0536755d31b44b35a70b9a8d014a3cee Mon Sep 17 00:00:00 2001 From: Gonzalo Balabasquer Date: Tue, 22 Jan 2019 09:53:18 -0300 Subject: [PATCH 2/2] Use different vairables for sizes --- src/proxy.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/proxy.sol b/src/proxy.sol index 0bd545a..1cc8768 100644 --- a/src/proxy.sol +++ b/src/proxy.sol @@ -58,16 +58,16 @@ contract DSProxy is DSAuth, DSNote { returns (bytes memory response) { require(_target != address(0), "ds-proxy-target-address-required"); - uint size; + uint csize; assembly { - size := extcodesize(_target) + csize := extcodesize(_target) } - require(size > 0, "ds-proxy-target-invalid-address"); + require(csize > 0, "ds-proxy-target-invalid-address"); // call contract in current context assembly { let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0) - size := returndatasize + let size := returndatasize response := mload(0x40) mstore(0x40, add(response, and(add(add(size, 0x20), 0x1f), not(0x1f))))