forked from Metalnem/libfuzzer-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibfuzzer-dotnet.cc
267 lines (211 loc) · 5.76 KB
/
libfuzzer-dotnet.cc
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "errno.h"
#include "stddef.h"
#include "stdint.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include <sys/shm.h>
#define _STR(x) #x
#define STR(x) _STR(x)
#define MAP_SIZE (1 << 16)
#define DATA_SIZE (1 << 20)
#define CTL_FD 198
#define ST_FD 199
#define LEN_FLD_SIZE 4
#define SHM_ID_VAR "__LIBFUZZER_SHM_ID"
#define CTL_FD_VAR "__LIBFUZZER_CONTROL_PIPE_ID"
#define ST_FD_VAR "__LIBFUZZER_STATUS_PIPE_ID"
__attribute__((weak, section("__libfuzzer_extra_counters")))
uint8_t extra_counters[MAP_SIZE];
static const char *target_path_name = "--target_path";
static const char *target_arg_name = "--target_arg";
static const char *target_path;
static const char *target_arg;
static int ctl_fd;
static int st_fd;
static int shm_id;
static uint8_t *trace_bits;
static pid_t child_pid;
static void die(const char *msg)
{
printf("%s\n", msg);
exit(1);
}
static void die_sys(const char *msg)
{
printf("%s: %s\n", msg, strerror(errno));
exit(1);
}
static void remove_shm()
{
shmctl(shm_id, IPC_RMID, NULL);
}
// Read the flag value from the single command line parameter. For example,
// read_flag_value("--target_path=binary", "--target-path") will return "binary".
static const char *read_flag_value(const char *param, const char *name)
{
size_t len = strlen(name);
if (strstr(param, name) == param && param[len] == '=' && param[len + 1])
{
return ¶m[len + 1];
}
return NULL;
}
// Read target_path (the path to .NET executable) and target_arg (optional command
// line argument that can be passed to .NET executable) from the command line parameters.
static void parse_flags(int argc, char **argv)
{
for (int i = 0; i < argc; ++i)
{
char *param = argv[i];
if (!target_path)
{
target_path = read_flag_value(param, target_path_name);
}
if (!target_arg)
{
target_arg = read_flag_value(param, target_arg_name);
}
}
}
// Start the .NET child process and initialize two pipes and one shared
// memory segment for the communication between the parent and the child.
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
{
parse_flags(*argc, *argv);
if (!target_path)
{
die("You must specify the target path by using the --target_path command line flag.");
}
int ctl_pipe[2];
int st_pipe[2];
if (pipe(ctl_pipe) || pipe(st_pipe))
{
die_sys("pipe() failed");
}
shm_id = shmget(IPC_PRIVATE, MAP_SIZE + DATA_SIZE, IPC_CREAT | IPC_EXCL | 0600);
if (shm_id < 0)
{
die_sys("shmget() failed");
}
atexit(remove_shm);
trace_bits = static_cast<uint8_t *>(shmat(shm_id, NULL, 0));
if (trace_bits == (void *)-1)
{
die_sys("shmat() failed");
}
child_pid = fork();
if (child_pid < 0)
{
die_sys("fork() failed");
}
if (!child_pid)
{
if (dup2(ctl_pipe[0], CTL_FD) < 0 || dup2(st_pipe[1], ST_FD) < 0)
{
die_sys("dup() failed");
}
close(ctl_pipe[0]);
close(ctl_pipe[1]);
close(st_pipe[0]);
close(st_pipe[1]);
char shm_str[12];
sprintf(shm_str, "%d", shm_id);
if (setenv(SHM_ID_VAR, shm_str, 1))
{
die_sys("setenv() failed setting shared memory ID");
}
if (setenv(CTL_FD_VAR, STR(CTL_FD), 1))
{
die_sys("setenv() failed setting control pipe ID");
}
if (setenv(ST_FD_VAR, STR(ST_FD), 1))
{
die_sys("setenv() failed setting status pipe ID");
}
if (target_arg)
{
execlp(target_path, "", target_arg, NULL);
}
else
{
execlp(target_path, "", NULL);
}
die_sys("execlp() failed");
}
else
{
close(ctl_pipe[0]);
close(st_pipe[1]);
ctl_fd = ctl_pipe[1];
st_fd = st_pipe[0];
ssize_t result;
int32_t status;
while ((result = read(st_fd, &status, LEN_FLD_SIZE)) == -1 && errno == EINTR)
{
continue;
}
if (result == -1)
{
die_sys("read() failed");
}
if (result != LEN_FLD_SIZE)
{
printf("short read: expected %d bytes, got %zd bytes\n", LEN_FLD_SIZE, result);
exit(1);
}
}
return 0;
}
// Fuzz the data by writing it to the shared memory segment, sending
// the size of the data to the .NET process (which will then run
// its own fuzzing function on the shared memory data), and receiving
// the status of the executed operation.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
if (size > DATA_SIZE)
{
die("Size of the input data must not exceed 1 MiB.");
}
memset(trace_bits, 0, MAP_SIZE);
memcpy(trace_bits + MAP_SIZE, data, size);
ssize_t result;
while ((result = write(ctl_fd, &size, LEN_FLD_SIZE)) == -1 && errno == EINTR)
{
continue;
}
if (result == -1)
{
die_sys("write() failed");
}
if (result != LEN_FLD_SIZE)
{
printf("short write: expected %d bytes, got %zd bytes\n", LEN_FLD_SIZE, result);
exit(1);
}
int32_t status;
while ((result = read(st_fd, &status, LEN_FLD_SIZE)) == -1 && errno == EINTR)
{
continue;
}
memcpy(extra_counters, trace_bits, MAP_SIZE);
if (result == -1)
{
die_sys("read() failed");
}
if (result == 0)
{
die("The child process terminated unexpectedly.");
}
if (result != LEN_FLD_SIZE)
{
printf("short read: expected %d bytes, got %zd bytes\n", LEN_FLD_SIZE, result);
exit(1);
}
if (status)
{
__builtin_trap();
}
return 0;
}