aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/erc20/contracts/test/ReentrantERC20Token.sol
blob: 0629754c08fdb192d55b702ce1aa0db99dbfb463 (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*

  Copyright 2018 ZeroEx Intl.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

*/

pragma solidity 0.4.24;
pragma experimental ABIEncoderV2;

import "@0x/contracts-utils/contracts/src/LibBytes.sol";
import "../src/ERC20Token.sol";
import "@0x/contracts-exchange/contracts/src/interfaces/IExchange.sol";
import "@0x/contracts-exchange-libs/contracts/src/LibOrder.sol";


// solhint-disable no-unused-vars
contract ReentrantERC20Token is
    ERC20Token
{
    using LibBytes for bytes;

    // solhint-disable-next-line var-name-mixedcase
    IExchange internal EXCHANGE;

    bytes internal constant REENTRANCY_ILLEGAL_REVERT_REASON = abi.encodeWithSelector(
        bytes4(keccak256("Error(string)")),
        "REENTRANCY_ILLEGAL"
    );

    // All of these functions are potentially vulnerable to reentrancy
    // We do not test any "noThrow" functions because `fillOrderNoThrow` makes a delegatecall to `fillOrder`
    enum ExchangeFunction {
        FILL_ORDER,
        FILL_OR_KILL_ORDER,
        BATCH_FILL_ORDERS,
        BATCH_FILL_OR_KILL_ORDERS,
        MARKET_BUY_ORDERS,
        MARKET_SELL_ORDERS,
        MATCH_ORDERS,
        CANCEL_ORDER,
        BATCH_CANCEL_ORDERS,
        CANCEL_ORDERS_UP_TO,
        SET_SIGNATURE_VALIDATOR_APPROVAL
    }

    uint8 internal currentFunctionId = 0;

    constructor (address _exchange)
        public
    {
        EXCHANGE = IExchange(_exchange);
    }

    /// @dev Set the current function that will be called when `transferFrom` is called.
    /// @param _currentFunctionId Id that corresponds to function name.
    function setCurrentFunction(uint8 _currentFunctionId)
        external
    {
        currentFunctionId = _currentFunctionId;
    }

    /// @dev A version of `transferFrom` that attempts to reenter the Exchange contract.
    /// @param _from The address of the sender
    /// @param _to The address of the recipient
    /// @param _value The amount of token to be transferred
    function transferFrom(
        address _from,
        address _to,
        uint256 _value
    )
        external
        returns (bool)
    {
        // This order would normally be invalid, but it will be used strictly for testing reentrnacy.
        // Any reentrancy checks will happen before any other checks that invalidate the order.
        LibOrder.Order memory order;

        // Initialize remaining null parameters
        bytes memory signature;
        LibOrder.Order[] memory orders;
        uint256[] memory takerAssetFillAmounts;
        bytes[] memory signatures;
        bytes memory callData;

        // Create callData for function that corresponds to currentFunctionId
        if (currentFunctionId == uint8(ExchangeFunction.FILL_ORDER)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.fillOrder.selector,
                order,
                0,
                signature
            );
        } else if (currentFunctionId == uint8(ExchangeFunction.FILL_OR_KILL_ORDER)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.fillOrKillOrder.selector,
                order,
                0,
                signature
            );
        } else if (currentFunctionId == uint8(ExchangeFunction.BATCH_FILL_ORDERS)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.batchFillOrders.selector,
                orders,
                takerAssetFillAmounts,
                signatures
            );
        } else if (currentFunctionId == uint8(ExchangeFunction.BATCH_FILL_OR_KILL_ORDERS)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.batchFillOrKillOrders.selector,
                orders,
                takerAssetFillAmounts,
                signatures
            );
        } else if (currentFunctionId == uint8(ExchangeFunction.MARKET_BUY_ORDERS)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.marketBuyOrders.selector,
                orders,
                0,
                signatures
            );
        } else if (currentFunctionId == uint8(ExchangeFunction.MARKET_SELL_ORDERS)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.marketSellOrders.selector,
                orders,
                0,
                signatures
            );
        } else if (currentFunctionId == uint8(ExchangeFunction.MATCH_ORDERS)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.matchOrders.selector,
                order,
                order,
                signature,
                signature
            );
        } else if (currentFunctionId == uint8(ExchangeFunction.CANCEL_ORDER)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.cancelOrder.selector,
                order
            );
        } else if (currentFunctionId == uint8(ExchangeFunction.BATCH_CANCEL_ORDERS)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.batchCancelOrders.selector,
                orders
            );
        } else if (currentFunctionId == uint8(ExchangeFunction.CANCEL_ORDERS_UP_TO)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.cancelOrdersUpTo.selector,
                0
            );
        } else if (currentFunctionId == uint8(ExchangeFunction.SET_SIGNATURE_VALIDATOR_APPROVAL)) {
            callData = abi.encodeWithSelector(
                EXCHANGE.setSignatureValidatorApproval.selector,
                address(0),
                false
            );
        }

        // Call Exchange function, swallow error
        address(EXCHANGE).call(callData);

        // Revert reason is 100 bytes
        bytes memory returnData = new bytes(100);

        // Copy return data
        assembly {
            returndatacopy(add(returnData, 32), 0, 100)
        }

        // Revert if function reverted with REENTRANCY_ILLEGAL error
        require(!REENTRANCY_ILLEGAL_REVERT_REASON.equals(returnData));

        // Transfer will return true if function failed for any other reason
        return true;
    }
}