-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathparams.c
More file actions
867 lines (747 loc) · 25.1 KB
/
params.c
File metadata and controls
867 lines (747 loc) · 25.1 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <sys/resource.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 "strategy.h"
#include "syscall.h"
#include "tables.h"
#include "taint.h"
#include "trinity.h" // progname, max_files_rlimit
#include "utils.h"
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;
unsigned int alt_op_children = 0;
unsigned int explorer_children = 0;
bool user_specified_explorer_children = false;
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;
bool show_disabled_syscalls = false;
unsigned char verbosity = 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 stats_json = false;
bool quiet = false;
bool group_bias = false;
unsigned long epoch_iterations = 0;
unsigned int epoch_timeout = 0;
/*
* Parse a duration string with optional suffix:
* s = seconds (default if no suffix)
* m = minutes
* h = hours
* d = days
* On success, writes the value (in seconds) to *out and returns true.
* Returns false for empty input, garbage, multi-char suffix, unknown
* suffix, negative values, or anything that overflows unsigned int.
*/
static bool parse_duration(const char *s, unsigned int *out)
{
char *end;
unsigned long val;
unsigned long mult = 1;
if (s == NULL || *s == '\0')
return false;
errno = 0;
val = strtoul(s, &end, 10);
if (end == s || errno == ERANGE)
return false;
if (*end != '\0') {
if (end[1] != '\0')
return false;
switch (*end) {
case 's': mult = 1; break;
case 'm': mult = 60UL; break;
case 'h': mult = 60UL * 60; break;
case 'd': mult = 60UL * 60 * 24; break;
default: return false;
}
}
if (mult != 0 && val > ULONG_MAX / mult)
return false;
val *= mult;
if (val > UINT_MAX)
return false;
*out = (unsigned int)val;
return true;
}
/*
* Parse a non-negative decimal integer from optarg. Requires the entire
* string be consumed (no trailing junk), rejects empty input and overflow,
* and optionally rejects zero. On success writes the value to *out and
* returns true; on failure prints a diagnostic and returns false.
*/
static bool parse_unsigned(const char *s, const char *name,
bool allow_zero, unsigned long *out)
{
char *end;
unsigned long val;
if (s == NULL || *s == '\0') {
outputerr("--%s: missing value\n", name);
return false;
}
errno = 0;
val = strtoul(s, &end, 10);
if (end == s || *end != '\0') {
outputerr("--%s: can't parse '%s' as a number\n", name, s);
return false;
}
if (errno == ERANGE) {
outputerr("--%s: value '%s' out of range\n", name, s);
return false;
}
if (!allow_zero && val == 0) {
outputerr("--%s: zero is not a meaningful value\n", name);
return false;
}
*out = val;
return true;
}
/* ------------------------------------------------------------------ *
* max_children cap derivation
*
* Ceilings on max_children to keep a typo (-C 999999999) from turning
* into a host-level fork/allocation storm. Three independent budgets:
*
* (a) shared_regions[] capacity: each child consumes
* SHARED_REGIONS_PER_CHILD slots in alloc_shared()'s static
* tracker, with SHARED_REGIONS_GLOBAL_RESERVE held back for
* fixed allocations (shm, syscall table, kcov, image segments,
* etc).
* (b) RLIMIT_NPROC - HOST_NPROC_RESERVE: leave headroom for the
* parent and the operator's surrounding processes.
* (c) RLIMIT_NOFILE - PARENT_NOFILE_RESERVE: parent opens one
* /proc/<pid>/stat fd per child plus its own ancillary fds.
*
* The derived cap is the smallest of those plus PROJECT_MAX_CHILDREN.
* derive_max_children_cap() also reports which budget is binding so
* the operator-facing error/warning can name the source.
* ------------------------------------------------------------------ */
#define HOST_NPROC_RESERVE 32
#define PARENT_NOFILE_RESERVE 64
#define PROJECT_MAX_CHILDREN 16384
enum max_children_binding {
BINDING_PROJECT_MAX,
BINDING_SHARED_REGIONS,
BINDING_NPROC,
BINDING_NOFILE,
};
static const char *binding_name(enum max_children_binding b)
{
switch (b) {
case BINDING_PROJECT_MAX: return "project sanity limit";
case BINDING_SHARED_REGIONS: return "shared_regions[] capacity";
case BINDING_NPROC: return "RLIMIT_NPROC";
case BINDING_NOFILE: return "RLIMIT_NOFILE";
}
return "?";
}
static unsigned long derive_max_children_cap(enum max_children_binding *out_binding)
{
unsigned long cap = PROJECT_MAX_CHILDREN;
enum max_children_binding b = BINDING_PROJECT_MAX;
unsigned long shared_cap;
struct rlimit nproc;
shared_cap = (MAX_SHARED_ALLOCS - SHARED_REGIONS_GLOBAL_RESERVE) /
SHARED_REGIONS_PER_CHILD;
if (shared_cap < cap) {
cap = shared_cap;
b = BINDING_SHARED_REGIONS;
}
if (getrlimit(RLIMIT_NPROC, &nproc) == 0 &&
nproc.rlim_cur != RLIM_INFINITY) {
unsigned long nproc_cap;
if (nproc.rlim_cur > HOST_NPROC_RESERVE)
nproc_cap = nproc.rlim_cur - HOST_NPROC_RESERVE;
else
nproc_cap = 0;
if (nproc_cap < cap) {
cap = nproc_cap;
b = BINDING_NPROC;
}
}
if (max_files_rlimit.rlim_cur != RLIM_INFINITY) {
unsigned long nofile_cap;
if (max_files_rlimit.rlim_cur > PARENT_NOFILE_RESERVE)
nofile_cap = max_files_rlimit.rlim_cur - PARENT_NOFILE_RESERVE;
else
nofile_cap = 0;
if (nofile_cap < cap) {
cap = nofile_cap;
b = BINDING_NOFILE;
}
}
if (out_binding != NULL)
*out_binding = b;
return cap;
}
/*
* Compute the default explorer-pool size when --explorer-children was not
* passed. Default is max_children/8 (12.5%): for -C64 → 8 explorers, -C16
* → 2, -C8 → 1, -C4 → 0. Small fleets get zero explorers because the
* bandit's per-arm pull count needs the full slot budget to converge.
*
* Validates the explicit operator value (post-parse) against the ceiling
* of max_children/2: more than half being explorers leaves the bandit
* pool too small for UCB1 to differentiate arms.
*
* Called from main() after clamp_default_max_children() so max_children
* is final. Mirrors the alt_op_children clamp pattern in trinity.c.
*/
void clamp_default_explorer_children(void)
{
unsigned int ceiling = max_children / 2;
if (!user_specified_explorer_children) {
explorer_children = max_children / 8;
return;
}
if (explorer_children > ceiling) {
outputerr("warning: --explorer-children=%u exceeds max_children/2 (%u); clamping to %u\n",
explorer_children, ceiling, ceiling);
explorer_children = ceiling;
}
}
void clamp_default_max_children(void)
{
enum max_children_binding b;
unsigned long cap;
/* -C path validates against the cap inside parse_args; nothing to do. */
if (user_specified_children != 0)
return;
cap = derive_max_children_cap(&b);
if (cap == 0) {
outputerr("cannot run trinity: %s leaves no budget for children\n",
binding_name(b));
exit(EXIT_FAILURE);
}
if ((unsigned long)max_children > cap) {
outputerr("warning: default max_children=%u (num_online_cpus*4) "
"exceeds %s cap of %lu; clamping\n",
max_children, binding_name(b), cap);
max_children = (unsigned int)cap;
}
}
bool no_warm_start = false;
char *warm_start_path = NULL;
bool no_healer_warm_start = false;
bool no_healer_snapshot = false;
char *memory_max_arg = NULL;
char *memory_high_arg = NULL;
char *memory_swap_max_arg = NULL;
bool no_cgroup = false;
char *stats_log_path = NULL;
bool do_effector_map = false;
bool user_set_seed = false;
unsigned char desired_group = GROUP_NONE;
static const struct {
const char *name;
unsigned char id;
} group_names[] = {
{ "vm", GROUP_VM },
{ "vfs", GROUP_VFS },
{ "net", GROUP_NET },
{ "ipc", GROUP_IPC },
{ "process", GROUP_PROCESS },
{ "signal", GROUP_SIGNAL },
{ "io_uring", GROUP_IO_URING },
{ "bpf", GROUP_BPF },
{ "sched", GROUP_SCHED },
{ "time", GROUP_TIME },
};
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[] = {
{ "alt-op-children", 0, "reserve N children to run dedicated alt ops (mmap_lifecycle, mprotect_split, ...) round-robin instead of mixing them at 1% in every child" },
{ "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" },
{ "effector-map", 0, "calibrate per-bit input significance under KCOV and exit (one-shot)" },
{ "enable-fds", 0, NULL }, /* handled separately */
{ "epoch-iterations", 0, "syscalls per epoch before restarting (must be > 0; omit to disable)" },
{ "epoch-timeout", 0, "seconds per epoch before restarting (must be > 0; omit to disable)" },
{ "exclude", 'x', "don't call a specific syscall" },
{ "explorer-children", 0, "reserve N children to always run STRATEGY_RANDOM as a bandit-independent explorer pool (default: max_children/8; max: max_children/2)" },
{ "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" },
{ "max-runtime", 0, "maximum runtime before exit, with optional suffix s/m/h/d (e.g., 30s, 10m, 2h, 1d). Overrides --epoch-timeout." },
{ "memory-high", 0, "children/memory.high back-pressure threshold (workers cgroup). Accepts \"max\", N% of MemTotal, or N[KMG] bytes. Default: 50%." },
{ "memory-max", 0, "total trinity memory budget. Split into children/memory.max=<this>-parent_high and a small parent reservation parent_high=min(200M,this/16) so worker OOM doesn't take the parent. Accepts \"max\", N% of MemTotal, or N[KMG] bytes. Default: 60%." },
{ "memory-swap-max", 0, "children/memory.swap.max cap (workers cgroup). Accepts \"max\", N% of MemTotal, or N[KMG] bytes. Default: 20%." },
{ "no-cgroup", 0, "skip self-cgroup creation entirely (no in-binary memory containment)" },
{ "domain", 'P', "specify specific network domain for sockets" },
{ "quiet", 'q', "suppress the per-second progress line (other output unchanged)" },
{ "no_domain", 'E', "specify network domains to be excluded from testing" },
{ "print-disabled-syscalls", 0, "print syscalls disabled via AVOID_SYSCALL or NEED_ALARM and exit" },
{ "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" },
{ "stats-json", 0, "emit dump_stats output as a single JSON object on stdout (machine-readable)" },
{ "stats-log-file", 0, "path to append periodic stats dumps to (in addition to stdout)" },
{ "strategy", 0, "arm-selection policy for the multi-strategy rotation: round-robin (default) or bandit (UCB1)" },
{ "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)" },
{ "no-warm-start", 0, "skip loading and saving the persisted minicorpus" },
{ "warm-start-path", 0, "override the on-disk minicorpus path (default: $XDG_CACHE_HOME/trinity/corpus/<arch>)" },
{ "no-healer-warm-start", 0, "skip loading the persisted HEALER relation table on startup" },
{ "no-healer-snapshot", 0, "skip periodic and end-of-run HEALER relation-table snapshots" },
{ 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:qr:s:ST:V:vx:X";
static const struct option longopts[] = {
{ "alt-op-children", required_argument, NULL, 0 },
{ "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 },
{ "effector-map", no_argument, NULL, 0 },
{ "enable-fds", required_argument, NULL, 0 },
{ "epoch-iterations", required_argument, NULL, 0 },
{ "epoch-timeout", required_argument, NULL, 0 },
{ "exclude", required_argument, NULL, 'x' },
{ "explorer-children", required_argument, NULL, 0 },
{ "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' },
{ "max-runtime", required_argument, NULL, 0 },
{ "memory-high", required_argument, NULL, 0 },
{ "memory-max", required_argument, NULL, 0 },
{ "memory-swap-max", required_argument, NULL, 0 },
{ "no-cgroup", no_argument, NULL, 0 },
{ "ioctls", no_argument, NULL, 'I' },
{ "no_domain", required_argument, NULL, 'E' },
{ "domain", required_argument, NULL, 'P' },
{ "print-disabled-syscalls", no_argument, NULL, 0 },
{ "quiet", no_argument, NULL, 'q' },
{ "random", required_argument, NULL, 'r' },
{ "stats", no_argument, NULL, 0 },
{ "stats-json", no_argument, NULL, 0 },
{ "stats-log-file", required_argument, NULL, 0 },
{ "strategy", required_argument, NULL, 0 },
{ "show-unannotated", no_argument, NULL, 0 },
{ "syslog", no_argument, NULL, 'S' },
{ "verbose", no_argument, NULL, 'v' },
{ "victims", required_argument, NULL, 'V' },
{ "no-warm-start", no_argument, NULL, 0 },
{ "warm-start-path", required_argument, NULL, 0 },
{ "no-healer-warm-start", no_argument, NULL, 0 },
{ "no-healer-snapshot", no_argument, NULL, 0 },
{ NULL, 0, NULL, 0 } };
void parse_args(int argc, char *argv[])
{
int opt;
int opt_index = 0;
bool max_runtime_set = false;
bool epoch_timeout_set = false;
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;
enum max_children_binding b;
unsigned long cap;
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 == 0) {
outputerr("zero children ? WAT?\n");
exit(EXIT_FAILURE);
}
cap = derive_max_children_cap(&b);
if (val > cap) {
outputerr("--children=%lu exceeds %s cap of %lu\n",
val, binding_name(b), cap);
exit(EXIT_FAILURE);
}
user_specified_children = (unsigned int)val;
max_children = user_specified_children;
break;
}
case 'd':
dangerous = true;
break;
case 'D':
set_debug = true;
break;
case 'E':
parse_exclude_domains(optarg);
break;
case 'g': {
unsigned int i;
bool matched = false;
for (i = 0; i < ARRAY_SIZE(group_names); i++) {
if (!strcmp(optarg, group_names[i].name)) {
desired_group = group_names[i].id;
matched = true;
break;
}
}
if (!matched) {
outputerr("unknown group '%s'. Valid groups are:", optarg);
for (i = 0; i < ARRAY_SIZE(group_names); i++)
outputerr(" %s", group_names[i].name);
outputerr("\n");
exit(EXIT_FAILURE);
}
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 'q':
quiet = true;
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':
verbosity++;
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("alt-op-children", longopts[opt_index].name) == 0) {
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("--alt-op-children value %lu exceeds UINT_MAX\n", val);
exit(EXIT_FAILURE);
}
alt_op_children = (unsigned int)val;
}
if (strcmp("clowntown", longopts[opt_index].name) == 0)
clowntown = true;
if (strcmp("explorer-children", longopts[opt_index].name) == 0) {
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("--explorer-children value %lu exceeds UINT_MAX\n", val);
exit(EXIT_FAILURE);
}
explorer_children = (unsigned int)val;
user_specified_explorer_children = 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("effector-map", longopts[opt_index].name) == 0)
do_effector_map = true;
if (strcmp("enable-fds", longopts[opt_index].name) == 0)
process_fds_param(optarg, true);
if (strcmp("epoch-iterations", longopts[opt_index].name) == 0) {
if (!parse_unsigned(optarg, "epoch-iterations", false, &epoch_iterations))
exit(EXIT_FAILURE);
}
if (strcmp("epoch-timeout", longopts[opt_index].name) == 0) {
if (max_runtime_set) {
outputerr("warning: --max-runtime takes precedence; ignoring --epoch-timeout\n");
} else {
unsigned long val;
if (!parse_unsigned(optarg, "epoch-timeout", false, &val))
exit(EXIT_FAILURE);
if (val > UINT_MAX) {
outputerr("--epoch-timeout: value %lu exceeds UINT_MAX\n", val);
exit(EXIT_FAILURE);
}
epoch_timeout = (unsigned int)val;
epoch_timeout_set = true;
}
}
if (strcmp("memory-max", longopts[opt_index].name) == 0) {
free(memory_max_arg);
memory_max_arg = strdup(optarg);
if (memory_max_arg == NULL) {
outputerr("strdup failed\n");
exit(EXIT_FAILURE);
}
}
if (strcmp("memory-high", longopts[opt_index].name) == 0) {
free(memory_high_arg);
memory_high_arg = strdup(optarg);
if (memory_high_arg == NULL) {
outputerr("strdup failed\n");
exit(EXIT_FAILURE);
}
}
if (strcmp("memory-swap-max", longopts[opt_index].name) == 0) {
free(memory_swap_max_arg);
memory_swap_max_arg = strdup(optarg);
if (memory_swap_max_arg == NULL) {
outputerr("strdup failed\n");
exit(EXIT_FAILURE);
}
}
if (strcmp("no-cgroup", longopts[opt_index].name) == 0)
no_cgroup = true;
if (strcmp("max-runtime", longopts[opt_index].name) == 0) {
unsigned int seconds;
if (!parse_duration(optarg, &seconds)) {
outputerr("can't parse '%s' as a duration (use number with optional s/m/h/d suffix)\n", optarg);
exit(EXIT_FAILURE);
}
if (epoch_timeout_set)
outputerr("warning: --max-runtime overrides previously set --epoch-timeout\n");
epoch_timeout = seconds;
max_runtime_set = 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;
if (strcmp("stats-json", longopts[opt_index].name) == 0) {
stats_json = true;
show_stats = true;
}
if (strcmp("stats-log-file", longopts[opt_index].name) == 0) {
free(stats_log_path);
stats_log_path = strdup(optarg);
if (!stats_log_path) {
outputerr("strdup failed\n");
exit(EXIT_FAILURE);
}
}
if (strcmp("strategy", longopts[opt_index].name) == 0) {
if (!parse_picker_mode(optarg, &picker_mode_arg)) {
outputerr("--strategy: unknown picker '%s' (try round-robin or bandit)\n",
optarg);
exit(EXIT_FAILURE);
}
}
if (strcmp("no-warm-start", longopts[opt_index].name) == 0)
no_warm_start = true;
if (strcmp("print-disabled-syscalls", longopts[opt_index].name) == 0)
show_disabled_syscalls = true;
if (strcmp("warm-start-path", longopts[opt_index].name) == 0) {
free(warm_start_path);
warm_start_path = strdup(optarg);
if (!warm_start_path) {
outputerr("strdup failed\n");
exit(EXIT_FAILURE);
}
}
if (strcmp("no-healer-warm-start",
longopts[opt_index].name) == 0)
no_healer_warm_start = true;
if (strcmp("no-healer-snapshot",
longopts[opt_index].name) == 0)
no_healer_snapshot = true;
break;
}
}
if (verbosity > MAX_LOGLEVEL)
verbosity = MAX_LOGLEVEL;
output(1, "Done parsing arguments.\n");
}