-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathparams.c
More file actions
409 lines (345 loc) · 10.6 KB
/
params.c
File metadata and controls
409 lines (345 loc) · 10.6 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <sys/types.h>
#include "bdevs.h"
#include "child.h"
#include "fd.h"
#include "net.h"
#include "params.h"
#include "domains.h"
#include "random.h"
#include "syscall.h"
#include "tables.h"
#include "taint.h"
#include "trinity.h" // progname
bool set_debug = false;
bool do_specific_syscall = false;
bool do_exclude_syscall = false;
bool do_32_arch = true;
bool do_64_arch = true;
unsigned int specific_domain = 0;
unsigned int user_specified_children = 0;
bool do_specific_domain = false;
bool no_domains[TRINITY_PF_MAX];
bool dry_run = false;
bool show_unannotated = false;
bool show_syscall_list = false;
bool show_ioctl_list = false;
unsigned char quiet_level = 1;
bool dangerous = false;
bool dropprivs = false;
bool do_syslog = false;
bool random_selection = false;
unsigned int random_selection_num;
bool clowntown = false;
bool show_stats = false;
bool group_bias = false;
bool user_set_seed = false;
unsigned char desired_group = GROUP_NONE;
char *specific_domain_optarg = NULL;
char *victim_paths[MAX_VICTIM_PATHS];
unsigned int nr_victim_paths;
unsigned int kernel_taint_mask = 0xFFFFFFFF;
bool kernel_taint_param_occured = false;
void enable_disable_fd_usage(void)
{
dump_fd_provider_names();
}
struct option_help {
const char *name; /* long option name (NULL = end of table) */
char shortopt; /* short option char, or 0 if none */
const char *desc; /* help text */
};
static const struct option_help option_descs[] = {
{ "arch", 'a', "selects syscalls for the specified architecture (32 or 64). Both by default." },
{ "bdev", 'b', "Add /dev node to list of block devices to use for destructive tests." },
{ "children", 'C', "specify number of child processes" },
{ "clowntown", 0, "enable clowntown mode" },
{ "dangerous", 'd', "enable dangerous mode" },
{ "debug", 'D', "enable debug" },
{ "disable-fds", 0, NULL }, /* handled separately */
{ "dropprivs", 'X', "if run as root, switch to nobody [EXPERIMENTAL]" },
{ "dry-run", 0, "parse args and exit without fuzzing" },
{ "enable-fds", 0, NULL }, /* handled separately */
{ "exclude", 'x', "don't call a specific syscall" },
{ "group", 'g', "only run syscalls from a certain group (vfs,vm,net,ipc,process,signal,io_uring,bpf,sched,time)" },
{ "group-bias", 0, "bias syscall selection toward the same group as the previous call" },
{ "help", 'h', "show this help" },
{ "ioctls", 'I', "list all ioctls" },
{ "kernel_taint", 'T', "controls which kernel taint flags should be considered (see README)" },
{ "list", 'L', "list all syscalls known on this architecture" },
{ "domain", 'P', "specify specific network domain for sockets" },
{ "no_domain", 'E', "specify network domains to be excluded from testing" },
{ "random", 'r', "pick N syscalls at random and just fuzz those" },
{ "show-unannotated", 0, "show unannotated syscalls" },
{ "stats", 0, "show errno distribution per syscall before exiting" },
{ "syslog", 'S', "log important info to syslog (useful if syslog is remote)" },
{ "verbose", 'v', "increase output verbosity. Repeat for more detail (-vv)" },
{ "victims", 'V', "path to victim files (may be repeated)" },
{ NULL, 0, NULL },
};
/* Short-only options that don't appear in longopts. */
static const struct option_help shortonly_descs[] = {
{ NULL, 'c', "target specific syscall (name, optionally @32 or @64)" },
{ NULL, 'N', "do N syscalls then exit" },
{ NULL, 's', "use N as random seed" },
{ NULL, 0, NULL },
};
static void usage(void)
{
const struct option_help *h;
outputerr("%s\n", progname);
for (h = option_descs; h->name != NULL; h++) {
if (h->desc == NULL)
continue;
if (h->shortopt)
outputerr(" --%s, -%c: %s\n", h->name, h->shortopt, h->desc);
else
outputerr(" --%s: %s\n", h->name, h->desc);
}
enable_disable_fd_usage();
for (h = shortonly_descs; h->shortopt != 0; h++)
outputerr(" -%c: %s\n", h->shortopt, h->desc);
outputerr("\n");
exit(EXIT_SUCCESS);
}
static const char paramstr[] = "a:b:c:C:dDE:g:hILN:P:r:s:ST:V:vx:X";
static const struct option longopts[] = {
{ "arch", required_argument, NULL, 'a' },
{ "bdev", required_argument, NULL, 'b' },
{ "children", required_argument, NULL, 'C' },
{ "clowntown", no_argument, NULL, 0 },
{ "dangerous", no_argument, NULL, 'd' },
{ "dropprivs", no_argument, NULL, 'X'},
{ "debug", no_argument, NULL, 'D' },
{ "disable-fds", required_argument, NULL, 0 },
{ "dry-run", no_argument, NULL, 0 },
{ "enable-fds", required_argument, NULL, 0 },
{ "exclude", required_argument, NULL, 'x' },
{ "group", required_argument, NULL, 'g' },
{ "group-bias", no_argument, NULL, 0 },
{ "kernel_taint", required_argument, NULL, 'T' },
{ "help", no_argument, NULL, 'h' },
{ "list", no_argument, NULL, 'L' },
{ "ioctls", no_argument, NULL, 'I' },
{ "no_domain", required_argument, NULL, 'E' },
{ "domain", required_argument, NULL, 'P' },
{ "random", required_argument, NULL, 'r' },
{ "stats", no_argument, NULL, 0 },
{ "show-unannotated", no_argument, NULL, 0 },
{ "syslog", no_argument, NULL, 'S' },
{ "verbose", no_argument, NULL, 'v' },
{ "victims", required_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 } };
void parse_args(int argc, char *argv[])
{
int opt;
int opt_index = 0;
while ((opt = getopt_long(argc, argv, paramstr, longopts, &opt_index)) != -1) {
switch (opt) {
default:
if (opt == '?')
exit(EXIT_FAILURE);
else
outputstd("opt:%c\n", opt);
return;
case 'a':
/* One of the architectures selected*/
do_32_arch = false;
do_64_arch = false;
if (strcmp(optarg, "64") == 0) {
do_32_arch = false;
do_64_arch = true;
} else if (strcmp(optarg, "32") == 0) {
do_32_arch = true;
do_64_arch = false;
} else {
outputstd("can't parse %s\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'b':
init_bdev_list();
process_bdev_param(optarg);
dump_bdev_list();
outputstd("--bdev doesn't do anything useful yet.\n");
exit(EXIT_SUCCESS);
case 'c':
/* syscalls are all disabled at this point. enable the syscall we care about. */
do_specific_syscall = true;
toggle_syscall(optarg, true);
break;
case 'C': {
char *end;
unsigned long val;
errno = 0;
val = strtoul(optarg, &end, 10);
if (end == optarg || *end != '\0' || errno == ERANGE) {
outputerr("can't parse '%s' as a number\n", optarg);
exit(EXIT_FAILURE);
}
if (val > UINT_MAX) {
outputerr("children value %lu exceeds UINT_MAX\n", val);
exit(EXIT_FAILURE);
}
user_specified_children = (unsigned int)val;
max_children = user_specified_children;
if (max_children == 0) {
outputerr("zero children ? WAT?\n");
exit(EXIT_FAILURE);
}
break;
}
case 'd':
dangerous = true;
break;
case 'D':
set_debug = true;
break;
case 'E':
parse_exclude_domains(optarg);
break;
case 'g':
if (!strcmp(optarg, "vm"))
desired_group = GROUP_VM;
else if (!strcmp(optarg, "vfs"))
desired_group = GROUP_VFS;
else if (!strcmp(optarg, "net"))
desired_group = GROUP_NET;
else if (!strcmp(optarg, "ipc"))
desired_group = GROUP_IPC;
else if (!strcmp(optarg, "process"))
desired_group = GROUP_PROCESS;
else if (!strcmp(optarg, "signal"))
desired_group = GROUP_SIGNAL;
else if (!strcmp(optarg, "io_uring"))
desired_group = GROUP_IO_URING;
else if (!strcmp(optarg, "bpf"))
desired_group = GROUP_BPF;
else if (!strcmp(optarg, "sched"))
desired_group = GROUP_SCHED;
else if (!strcmp(optarg, "time"))
desired_group = GROUP_TIME;
break;
/* Show help */
case 'h':
usage();
exit(EXIT_SUCCESS);
case 'I':
show_ioctl_list = true;
break;
case 'L':
show_syscall_list = true;
break;
/* Set number of syscalls to do */
case 'N': {
char *end;
errno = 0;
syscalls_todo = strtoul(optarg, &end, 10);
if (end == optarg || *end != '\0' || errno == ERANGE) {
outputerr("can't parse '%s' as a number\n", optarg);
exit(EXIT_FAILURE);
}
break;
}
case 'P': {
char *end;
errno = 0;
specific_domain = strtoul(optarg, &end, 10);
if (end == optarg || *end != '\0' || errno == ERANGE) {
outputerr("can't parse '%s' as a number\n", optarg);
exit(EXIT_FAILURE);
}
do_specific_domain = true;
specific_domain_optarg = optarg;
break;
}
case 'r': {
char *end;
if (do_exclude_syscall == true) {
outputerr("-r needs to be before any -x options.\n");
exit(EXIT_FAILURE);
}
errno = 0;
random_selection_num = strtoul(optarg, &end, 10);
if (end == optarg || *end != '\0' || errno == ERANGE) {
outputerr("can't parse '%s' as a number\n", optarg);
exit(EXIT_FAILURE);
}
random_selection = true;
break;
}
/* Set seed */
case 's': {
char *end;
errno = 0;
seed = strtoul(optarg, &end, 10);
if (end == optarg || *end != '\0' || errno == ERANGE) {
outputerr("can't parse '%s' as a number\n", optarg);
exit(EXIT_FAILURE);
}
user_set_seed = true;
break;
}
case 'S':
do_syslog = true;
break;
case 'T':
//Load mask for kernel taint flags.
process_taint_arg(optarg);
if (kernel_taint_mask != 0xFFFFFFFF)
outputstd("Custom kernel taint mask has been specified: 0x%08x (%d).\n",
kernel_taint_mask, kernel_taint_mask);
break;
case 'v':
quiet_level++;
break;
case 'V':
if (nr_victim_paths >= MAX_VICTIM_PATHS) {
outputerr("Too many victim paths (max %d).\n", MAX_VICTIM_PATHS);
exit(EXIT_FAILURE);
}
victim_paths[nr_victim_paths] = strdup(optarg);
if (!victim_paths[nr_victim_paths]) {
outputerr("strdup failed\n");
exit(EXIT_FAILURE);
}
nr_victim_paths++;
break;
case 'x':
do_exclude_syscall = true;
toggle_syscall(optarg, false);
break;
case 'X':
if (getuid() == 0)
dropprivs = true;
else
outputstd("Already running unprivileged, can't drop privs\n");
break;
case 0:
if (strcmp("clowntown", longopts[opt_index].name) == 0)
clowntown = true;
if (strcmp("disable-fds", longopts[opt_index].name) == 0)
process_fds_param(optarg, false);
if (strcmp("dry-run", longopts[opt_index].name) == 0)
dry_run = true;
if (strcmp("enable-fds", longopts[opt_index].name) == 0)
process_fds_param(optarg, true);
if (strcmp("group-bias", longopts[opt_index].name) == 0)
group_bias = true;
if (strcmp("show-unannotated", longopts[opt_index].name) == 0)
show_unannotated = true;
if (strcmp("stats", longopts[opt_index].name) == 0)
show_stats = true;
break;
}
}
if (quiet_level > MAX_LOGLEVEL)
quiet_level = MAX_LOGLEVEL;
output(1, "Done parsing arguments.\n");
}