forked from pessimistic-io/slitherin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arbitrary_call_test.sol
177 lines (154 loc) · 4.82 KB
/
arbitrary_call_test.sol
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
interface IERC20 {
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool);
}
contract Test {
address toAddress;
bytes callData;
function _makeCall(address to, bytes memory data) private {
//This is not vulnarable by itself
to.call(data);
}
function _makeCall2(address to) private {
to.call(callData);
}
function _makeCall3(bytes memory data) private {
toAddress.call(data);
}
function _makeCall4(bytes memory data) private {
toAddress.call(abi.encodePacked(callData, data));
}
function _makeCallToThis(bytes memory data) private {
address(this).call(data);
}
function _makeCallNoData(address to) private {
to.call("");
}
function publicCallArbitraty(address to, bytes memory data) public {
//This is vulnerable
_makeCall(to, data);
_makeCall2(to);
_makeCall3(data);
_makeCall4(data);
_makeCallToThis(data);
_makeCallNoData(to);
}
function calldataHalfManipulated(bytes memory data) public {
bytes4 selector = 0xffffffff;
bytes memory _calldata = abi.encodeWithSelector(selector, data);
//this will also be considered as fully vulnerable, even though user cannot fully manipulate the calldata
_makeCall3(data);
}
function publicNonArbitraryCall() public {
//This is not vulnerable
_makeCall(toAddress, callData);
}
function semiArbitraryCall(address to) external {
_makeCall(to, callData);
}
function semiArbitraryCall2(bytes calldata data) external {
_makeCall(toAddress, data);
}
function assembly_delegateCall_full_tainted(
address target,
address data
) public returns (bytes memory response) {
// call contract in current context
bytes memory _data = abi.encodeWithSignature("relay(address)", data);
assembly {
let succeeded := delegatecall(
sub(gas(), 5000),
target,
add(_data, 0x20),
mload(_data),
0,
0
)
let size := returndatasize()
response := mload(0x40)
mstore(
0x40,
add(response, and(add(add(size, 0x20), 0x1f), not(0x1f)))
)
mstore(response, size)
returndatacopy(add(response, 0x20), 0, size)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
revert(add(response, 0x20), size)
}
}
}
function assembly_delegateCall_only_data_tainted(
address data
) public returns (bytes memory response) {
// call contract in current context
bytes memory _data = abi.encodeWithSignature("relay(address)", data);
address target = toAddress;
assembly {
let succeeded := delegatecall(
sub(gas(), 5000),
target,
add(_data, 0x20),
mload(_data),
0,
0
)
let size := returndatasize()
response := mload(0x40)
mstore(
0x40,
add(response, and(add(add(size, 0x20), 0x1f), not(0x1f)))
)
mstore(response, size)
returndatacopy(add(response, 0x20), 0, size)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
revert(add(response, 0x20), size)
}
}
}
function assembly_call_full_tainted(
address target,
address data
) public returns (bytes memory response) {
// call contract in current context
bytes memory _data = abi.encodeWithSignature("dosmth(address)", data);
assembly {
let succeeded := call(
sub(gas(), 5000),
target,
0,
add(_data, 0x20),
mload(_data),
0,
0
)
let size := returndatasize()
response := mload(0x40)
mstore(
0x40,
add(response, and(add(add(size, 0x20), 0x1f), not(0x1f)))
)
mstore(response, size)
returndatacopy(add(response, 0x20), 0, size)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
revert(add(response, 0x20), size)
}
}
}
}
contract TestWithApprove {
function swap(IERC20 token) external {
token.transferFrom(msg.sender, address(this), 1);
}
function call(address to, bytes memory data) public {
to.call(data);
}
}