-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtriforceafl_for_afl_2.52b.diff
1443 lines (1195 loc) · 41.4 KB
/
triforceafl_for_afl_2.52b.diff
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/Makefile b/Makefile
index 0b2c92b..54fe545 100644
--- a/Makefile
+++ b/Makefile
@@ -44,7 +44,10 @@ endif
COMM_HDR = alloc-inl.h config.h debug.h types.h
-all: test_x86 $(PROGS) afl-as test_build all_done
+all: test_x86 $(PROGS) afl-as test_build all_done #afl-qemu-system-trace afl-qemu-trace
+# The two commented entries in the line above are part of the triforceAFL patchset,
+# but not required for the FIRMAFL reproducability study.
+# All we need is an updated afl-fuzz, the qemu variants are provided by FirmAFL
ifndef AFL_NO_X86
@@ -89,8 +92,8 @@ ifndef AFL_NO_X86
test_build: afl-gcc afl-as afl-showmap
@echo "[*] Testing the CC wrapper and instrumentation output..."
unset AFL_USE_ASAN AFL_USE_MSAN; AFL_QUIET=1 AFL_INST_RATIO=100 AFL_PATH=. ./$(TEST_CC) $(CFLAGS) test-instr.c -o test-instr $(LDFLAGS)
- echo 0 | ./afl-showmap -m none -q -o .test-instr0 ./test-instr
- echo 1 | ./afl-showmap -m none -q -o .test-instr1 ./test-instr
+ echo 0 | ./afl-showmap -m none -q -o .test-instr0 -- ./test-instr
+ echo 1 | ./afl-showmap -m none -q -o .test-instr1 -- ./test-instr
@rm -f test-instr
@cmp -s .test-instr0 .test-instr1; DR="$$?"; rm -f .test-instr0 .test-instr1; if [ "$$DR" = "0" ]; then echo; echo "Oops, the instrumentation does not seem to be behaving correctly!"; echo; echo "Please ping <[email protected]> to troubleshoot the issue."; echo; exit 1; fi
@echo "[+] All right, the instrumentation seems to be working!"
@@ -108,6 +111,9 @@ all_done: test_build
@if [ "`uname`" = "Darwin" ]; then printf "\nWARNING: Fuzzing on MacOS X is slow because of the unusually high overhead of\nfork() on this OS. Consider using Linux or *BSD. You can also use VirtualBox\n(virtualbox.org) to put AFL inside a Linux or *BSD VM.\n\n"; fi
@! tty <&1 >/dev/null || printf "\033[0;30mNOTE: If you can read this, your terminal probably uses white background.\nThis will make the UI hard to read. See docs/status_screen.txt for advice.\033[0m\n" 2>/dev/null
+afl-qemu-system-trace afl-qemu-trace :
+ cd qemu_mode && ./build_qemu_support.sh
+
.NOTPARALLEL: clean
clean:
diff --git a/afl-analyze.c b/afl-analyze.c
index 813e79b..dfd5654 100644
--- a/afl-analyze.c
+++ b/afl-analyze.c
@@ -877,7 +877,7 @@ static void find_binary(u8* fname) {
/* Fix up argv for QEMU. */
-static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
+static char** get_qemu_argv(int qemu_mode, u8* own_loc, char** argv, int argc) {
char** new_argv = ck_alloc(sizeof(char*) * (argc + 4));
u8 *tmp, *cp, *rsl, *own_copy;
@@ -886,18 +886,24 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
setenv("QEMU_LOG", "nochain", 1);
- memcpy(new_argv + 3, argv + 1, sizeof(char*) * argc);
-
+ if(qemu_mode == 1) {
+ memcpy(new_argv + 3, argv + 1, sizeof(char*) * argc);
+
+ new_argv[2] = target_path;
+ new_argv[1] = "--";
+ } else {
+ memcpy(new_argv + 1, argv + 1, sizeof(char*) * argc);
+ }
+
/* Now we need to actually find qemu for argv[0]. */
-
- new_argv[2] = target_path;
- new_argv[1] = "--";
-
tmp = getenv("AFL_PATH");
if (tmp) {
- cp = alloc_printf("%s/afl-qemu-trace", tmp);
+ if(qemu_mode == 1)
+ cp = alloc_printf("%s/afl-qemu-trace", tmp);
+ else
+ cp = alloc_printf("%s/afl-qemu-system-trace", tmp);
if (access(cp, X_OK))
FATAL("Unable to find '%s'", tmp);
@@ -914,7 +920,10 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
*rsl = 0;
- cp = alloc_printf("%s/afl-qemu-trace", own_copy);
+ if(qemu_mode == 1)
+ cp = alloc_printf("%s/afl-qemu-trace", own_copy);
+ else
+ cp = alloc_printf("%s/afl-qemu-system-trace", own_copy);
ck_free(own_copy);
if (!access(cp, X_OK)) {
@@ -926,12 +935,19 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
} else ck_free(own_copy);
- if (!access(BIN_PATH "/afl-qemu-trace", X_OK)) {
+ if (qemu_mode == 1 && !access(BIN_PATH "/afl-qemu-trace", X_OK)) {
+
target_path = new_argv[0] = BIN_PATH "/afl-qemu-trace";
return new_argv;
}
+ if (qemu_mode > 1 && !access(BIN_PATH "/afl-qemu-system-trace", X_OK)) {
+
+ target_path = new_argv[0] = BIN_PATH "/afl-qemu-system-trace";
+ return new_argv;
+ }
+
FATAL("Unable to find 'afl-qemu-trace'.");
@@ -1024,10 +1040,10 @@ int main(int argc, char** argv) {
case 'Q':
- if (qemu_mode) FATAL("Multiple -Q options not supported");
+ //if (qemu_mode) FATAL("Multiple -Q options not supported");
if (!mem_limit_given) mem_limit = MEM_LIMIT_QEMU;
- qemu_mode = 1;
+ qemu_mode += 1;
break;
default:
@@ -1049,7 +1065,7 @@ int main(int argc, char** argv) {
detect_file_args(argv + optind);
if (qemu_mode)
- use_argv = get_qemu_argv(argv[0], argv + optind, argc - optind);
+ use_argv = get_qemu_argv(qemu_mode, argv[0], argv + optind, argc - optind);
else
use_argv = argv + optind;
diff --git a/afl-cmin b/afl-cmin
index bc3494e..8fa8f28 100755
--- a/afl-cmin
+++ b/afl-cmin
@@ -79,9 +79,7 @@ while getopts "+i:o:f:m:t:eQC" opt; do
export AFL_CMIN_CRASHES_ONLY=1
;;
"Q")
- EXTRA_PAR="$EXTRA_PAR -Q"
- test "$MEM_LIMIT_GIVEN" = "" && MEM_LIMIT=250
- QEMU_MODE=1
+ QEMU_MODE="${QEMU_MODE}Q"
;;
"?")
exit 1
@@ -91,6 +89,11 @@ while getopts "+i:o:f:m:t:eQC" opt; do
done
+if [ ! -z $QEMU_MODE ] ; then
+ # XXX set MEM_LIMIT?
+ EXTRA_PAR="$EXTRA_PAR -$QEMU_MODE"
+fi
+
shift $((OPTIND-1))
TARGET_BIN="$1"
@@ -155,13 +158,16 @@ fi
TRACE_DIR="$OUT_DIR/.traces"
-if [ "$STDIN_FILE" = "" ]; then
-
- if echo "$*" | grep -qF '@@'; then
- STDIN_FILE="$TRACE_DIR/.cur_input"
- fi
+# XXX we're using @@ in the afl-fuzz sense in afl-showmap now.
+# so get rid of this stdin stuff, which we dont support anymore. -TimN
-fi
+#if [ "$STDIN_FILE" = "" ]; then
+#
+# if echo "$*" | grep -qF '@@'; then
+# STDIN_FILE="$TRACE_DIR/.cur_input"
+# fi
+#
+#fi
# Check for obvious errors.
@@ -270,32 +276,35 @@ fi
# Make sure that we can actually get anything out of afl-showmap before we
# waste too much time.
-echo "[*] Testing the target binary..."
-
-if [ "$STDIN_FILE" = "" ]; then
-
- AFL_CMIN_ALLOW_ANY=1 "$SHOWMAP" -m "$MEM_LIMIT" -t "$TIMEOUT" -o "$TRACE_DIR/.run_test" -Z $EXTRA_PAR -- "$@" <"$IN_DIR/$FIRST_FILE"
-
-else
-
- cp "$IN_DIR/$FIRST_FILE" "$STDIN_FILE"
- AFL_CMIN_ALLOW_ANY=1 "$SHOWMAP" -m "$MEM_LIMIT" -t "$TIMEOUT" -o "$TRACE_DIR/.run_test" -Z $EXTRA_PAR -A "$STDIN_FILE" -- "$@" </dev/null
-
-fi
-
-FIRST_COUNT=$((`grep -c . "$TRACE_DIR/.run_test"`))
-
-if [ "$FIRST_COUNT" -gt "0" ]; then
-
- echo "[+] OK, $FIRST_COUNT tuples recorded."
-
-else
-
- echo "[-] Error: no instrumentation output detected (perhaps crash or timeout)." 1>&2
- test "$AFL_KEEP_TRACES" = "" && rm -rf "$TRACE_DIR"
- exit 1
+# XXX removed this for now -- we cant afford another showmap startup
+# when showmap might take several minutes to launch forkserver -TimN
-fi
+#echo "[*] Testing the target binary..."
+#
+#if [ "$STDIN_FILE" = "" ]; then
+#
+# AFL_CMIN_ALLOW_ANY=1 "$SHOWMAP" -m "$MEM_LIMIT" -t "$TIMEOUT" -o "$TRACE_DIR/.run_test" -Z $EXTRA_PAR -- "$@" <"$IN_DIR/$FIRST_FILE"
+#
+#else
+#
+# cp "$IN_DIR/$FIRST_FILE" "$STDIN_FILE"
+# AFL_CMIN_ALLOW_ANY=1 "$SHOWMAP" -m "$MEM_LIMIT" -t "$TIMEOUT" -o "$TRACE_DIR/.run_test" -Z $EXTRA_PAR -A "$STDIN_FILE" -- "$@" </dev/null
+#
+#fi
+#
+#FIRST_COUNT=$((`grep -c . "$TRACE_DIR/.run_test"`))
+#
+#if [ "$FIRST_COUNT" -gt "0" ]; then
+#
+# echo "[+] OK, $FIRST_COUNT tuples recorded."
+#
+#else
+#
+# echo "[-] Error: no instrumentation output detected (perhaps crash or timeout)." 1>&2
+# test "$AFL_KEEP_TRACES" = "" && rm -rf "$TRACE_DIR"
+# exit 1
+#
+#fi
# Let's roll!
@@ -311,28 +320,13 @@ echo "[*] Obtaining traces for input files in '$IN_DIR'..."
if [ "$STDIN_FILE" = "" ]; then
- while read -r fn; do
-
- CUR=$((CUR+1))
- printf "\\r Processing file $CUR/$IN_COUNT... "
-
- "$SHOWMAP" -m "$MEM_LIMIT" -t "$TIMEOUT" -o "$TRACE_DIR/$fn" -Z $EXTRA_PAR -- "$@" <"$IN_DIR/$fn"
-
- done < <(ls "$IN_DIR")
+ "$SHOWMAP" -m "$MEM_LIMIT" -t "$TIMEOUT" -o "$TRACE_DIR" -Z $EXTRA_PAR "$IN_DIR"/* -- "$@"
else
- while read -r fn; do
-
- CUR=$((CUR+1))
- printf "\\r Processing file $CUR/$IN_COUNT... "
-
- cp "$IN_DIR/$fn" "$STDIN_FILE"
-
- "$SHOWMAP" -m "$MEM_LIMIT" -t "$TIMEOUT" -o "$TRACE_DIR/$fn" -Z $EXTRA_PAR -A "$STDIN_FILE" -- "$@" </dev/null
-
- done < <(ls "$IN_DIR")
-
+ cp "$IN_DIR/$fn" "$STDIN_FILE"
+
+ "$SHOWMAP" -m "$MEM_LIMIT" -t "$TIMEOUT" -o "$TRACE_DIR" -Z $EXTRA_PAR -A "$STDIN_FILE" "$IN_DIR"/* -- "$@" </dev/null
fi
diff --git a/afl-fuzz.c b/afl-fuzz.c
index 01b4afe..b26fa7d 100644
--- a/afl-fuzz.c
+++ b/afl-fuzz.c
@@ -1998,7 +1998,7 @@ EXP_ST void init_forkserver(char** argv) {
if (!getrlimit(RLIMIT_NOFILE, &r) && r.rlim_cur < FORKSRV_FD + 2) {
r.rlim_cur = FORKSRV_FD + 2;
- setrlimit(RLIMIT_NOFILE, &r); /* Ignore errors */
+ //XXX setrlimit(RLIMIT_NOFILE, &r); /* Ignore errors */
}
@@ -2008,7 +2008,7 @@ EXP_ST void init_forkserver(char** argv) {
#ifdef RLIMIT_AS
- setrlimit(RLIMIT_AS, &r); /* Ignore errors */
+ //XXX setrlimit(RLIMIT_AS, &r); /* Ignore errors */
#else
@@ -2452,6 +2452,11 @@ static u8 run_target(char** argv, u32 timeout) {
kill_signal = 0;
return FAULT_CRASH;
}
+
+ /* treat all non-zero return values from qemu system test as a crash */
+ if(qemu_mode > 1 && WEXITSTATUS(status) != 0) {
+ return FAULT_CRASH;
+ }
if ((dumb_mode == 1 || no_forkserver) && tb4 == EXEC_FAIL_SIG)
return FAULT_ERROR;
@@ -6855,7 +6860,7 @@ EXP_ST void check_binary(u8* fname) {
}
- if (getenv("AFL_SKIP_BIN_CHECK")) return;
+ if (qemu_mode > 1 || getenv("AFL_SKIP_BIN_CHECK")) return;
/* Check for blatant user errors. */
@@ -7600,7 +7605,7 @@ EXP_ST void setup_signal_handlers(void) {
/* Rewrite argv for QEMU. */
-static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
+static char** get_qemu_argv(int qemu_mode, u8* own_loc, char** argv, int argc) {
char** new_argv = ck_alloc(sizeof(char*) * (argc + 4));
u8 *tmp, *cp, *rsl, *own_copy;
@@ -7609,10 +7614,14 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
setenv("QEMU_LOG", "nochain", 1);
- memcpy(new_argv + 3, argv + 1, sizeof(char*) * argc);
+ if(qemu_mode == 1) {
+ memcpy(new_argv + 3, argv + 1, sizeof(char*) * argc);
+ new_argv[2] = target_path;
+ new_argv[1] = "--";
+ } else {
+ memcpy(new_argv + 1, argv + 1, sizeof(char*) * argc);
+ }
- new_argv[2] = target_path;
- new_argv[1] = "--";
/* Now we need to actually find the QEMU binary to put in argv[0]. */
@@ -7620,7 +7629,10 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
if (tmp) {
- cp = alloc_printf("%s/afl-qemu-trace", tmp);
+ if(qemu_mode == 1)
+ cp = alloc_printf("%s/afl-qemu-trace", tmp);
+ else
+ cp = alloc_printf("%s/%s", tmp, argv[0]);
if (access(cp, X_OK))
FATAL("Unable to find '%s'", tmp);
@@ -7637,7 +7649,10 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
*rsl = 0;
- cp = alloc_printf("%s/afl-qemu-trace", own_copy);
+ if(qemu_mode == 1)
+ cp = alloc_printf("%s/afl-qemu-trace", own_copy);
+ else
+ cp = alloc_printf("%s/%s", own_copy, argv[0]);
ck_free(own_copy);
if (!access(cp, X_OK)) {
@@ -7649,11 +7664,18 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
} else ck_free(own_copy);
- if (!access(BIN_PATH "/afl-qemu-trace", X_OK)) {
+ if (qemu_mode == 1 && !access(BIN_PATH "/afl-qemu-trace", X_OK)) {
target_path = new_argv[0] = ck_strdup(BIN_PATH "/afl-qemu-trace");
return new_argv;
+ }
+ cp = alloc_printf("%s/%s", BIN_PATH, argv[0]);
+ if (qemu_mode > 1 && !access(cp, X_OK)) {
+
+ target_path = new_argv[0] = cp;
+ return new_argv;
+
}
SAYF("\n" cLRD "[-] " cRST
@@ -7884,8 +7906,8 @@ int main(int argc, char** argv) {
case 'Q': /* QEMU mode */
- if (qemu_mode) FATAL("Multiple -Q options not supported");
- qemu_mode = 1;
+ //if (qemu_mode) FATAL("Multiple -Q options not supported");
+ qemu_mode += 1;
if (!mem_limit_given) mem_limit = MEM_LIMIT_QEMU;
@@ -7974,7 +7996,7 @@ int main(int argc, char** argv) {
start_time = get_cur_time();
if (qemu_mode)
- use_argv = get_qemu_argv(argv[0], argv + optind, argc - optind);
+ use_argv = get_qemu_argv(qemu_mode, argv[0], argv + optind, argc - optind);
else
use_argv = argv + optind;
diff --git a/afl-showmap.c b/afl-showmap.c
index df3f7cd..f2c5395 100644
--- a/afl-showmap.c
+++ b/afl-showmap.c
@@ -38,6 +38,7 @@
#include <signal.h>
#include <dirent.h>
#include <fcntl.h>
+#include <libgen.h>
#include <sys/wait.h>
#include <sys/time.h>
@@ -46,7 +47,11 @@
#include <sys/types.h>
#include <sys/resource.h>
-static s32 child_pid; /* PID of the tested program */
+static s32 forksrv_pid, /* PID of the fork server */
+ child_pid; /* PID of the tested program */
+
+static s32 fsrv_ctl_fd, /* Fork server control pipe (write) */
+ fsrv_st_fd; /* Fork server status pipe (read) */
static u8* trace_bits; /* SHM with instrumentation bitmap */
@@ -231,29 +236,41 @@ static u32 write_results(void) {
static void handle_timeout(int sig) {
- child_timed_out = 1;
- if (child_pid > 0) kill(child_pid, SIGKILL);
+ if (child_pid > 0) {
-}
+ child_timed_out = 1;
+ kill(child_pid, SIGKILL);
+ } else if (child_pid == -1 && forksrv_pid > 0) {
-/* Execute target application. */
+ child_timed_out = 1;
+ kill(forksrv_pid, SIGKILL);
-static void run_target(char** argv) {
+ }
+
+}
+
+/* start the app and it's forkserver */
+static void init_forkserver(char **argv) {
static struct itimerval it;
+ int st_pipe[2], ctl_pipe[2];
int status = 0;
+ s32 rlen;
+
+ if (!quiet_mode)
+ SAYF("Spinning up the fork server...\n");
+ if (pipe(st_pipe) || pipe(ctl_pipe)) PFATAL("pipe() failed");
if (!quiet_mode)
SAYF("-- Program output begins --\n" cRST);
- MEM_BARRIER();
- child_pid = fork();
+ forksrv_pid = fork();
- if (child_pid < 0) PFATAL("fork() failed");
+ if (forksrv_pid < 0) PFATAL("fork() failed");
- if (!child_pid) {
+ if (!forksrv_pid) {
struct rlimit r;
@@ -291,6 +308,16 @@ static void run_target(char** argv) {
setrlimit(RLIMIT_CORE, &r); /* Ignore errors */
+ /* Set up control and status pipes, close the unneeded original fds. */
+
+ if (dup2(ctl_pipe[0], FORKSRV_FD) < 0) PFATAL("dup2() failed");
+ if (dup2(st_pipe[1], FORKSRV_FD + 1) < 0) PFATAL("dup2() failed");
+
+ close(ctl_pipe[0]);
+ close(ctl_pipe[1]);
+ close(st_pipe[0]);
+ close(st_pipe[1]);
+
if (!getenv("LD_BIND_LAZY")) setenv("LD_BIND_NOW", "1", 0);
setsid();
@@ -302,6 +329,105 @@ static void run_target(char** argv) {
}
+ /* Close the unneeded endpoints. */
+
+ close(ctl_pipe[0]);
+ close(st_pipe[1]);
+
+ fsrv_ctl_fd = ctl_pipe[1];
+ fsrv_st_fd = st_pipe[0];
+
+ /* Configure timeout, wait for child, cancel timeout. */
+
+ if (exec_tmout) {
+
+ child_timed_out = 0;
+ it.it_value.tv_sec = (exec_tmout * FORK_WAIT_MULT / 1000);
+ it.it_value.tv_usec = ((exec_tmout * FORK_WAIT_MULT) % 1000) * 1000;
+
+ }
+
+ setitimer(ITIMER_REAL, &it, NULL);
+
+ rlen = read(fsrv_st_fd, &status, 4);
+
+ it.it_value.tv_sec = 0;
+ it.it_value.tv_usec = 0;
+ setitimer(ITIMER_REAL, &it, NULL);
+
+ if(!quiet_mode)
+ SAYF(cRST "-- Program output ends --\n");
+
+ /* If we have a four-byte "hello" message from the server, we're all set.
+ Otherwise, try to figure out what went wrong. */
+
+ if (rlen == 4) {
+ if(!quiet_mode)
+ SAYF("All right - fork server is up.");
+ return;
+ }
+
+ if (waitpid(forksrv_pid, &status, 0) <= 0)
+ PFATAL("waitpid() failed");
+
+ if (WIFSIGNALED(status))
+ child_crashed = 1;
+
+ if (!quiet_mode) {
+
+ if (child_timed_out)
+ SAYF(cLRD "\n+++ Program timed off +++\n" cRST);
+ else if (stop_soon)
+ SAYF(cLRD "\n+++ Program aborted by user +++\n" cRST);
+ else if (child_crashed)
+ SAYF(cLRD "\n+++ Program killed by signal %u +++\n" cRST, WTERMSIG(status));
+
+ }
+
+}
+
+
+/* Execute target application. */
+
+static u8 run_target(char** argv) {
+
+ static struct itimerval it;
+ static u32 prev_timed_out = 0;
+
+ int status = 0;
+
+ if(!quiet_mode)
+ SAYF("-- Program output begins --\n" cRST);
+
+ /* After this memset, trace_bits[] are effectively volatile, so we
+ must prevent any earlier operations from venturing into that
+ territory. */
+
+ memset(trace_bits, 0, MAP_SIZE);
+ MEM_BARRIER();
+
+ s32 res;
+
+ /* we have the fork server up and running, so simply
+ tell it to have at it, and then read back PID. */
+
+ if ((res = write(fsrv_ctl_fd, &prev_timed_out, 4)) != 4) {
+
+ if (stop_soon) return 0;
+ RPFATAL(res, "Unable to request new process from fork server (OOM?)");
+
+ }
+
+ if ((res = read(fsrv_st_fd, &child_pid, 4)) != 4) {
+
+ if (stop_soon) return 0;
+ RPFATAL(res, "Unable to request new process from fork server (OOM?)");
+
+ }
+
+ if (child_pid <= 0) FATAL("Fork server is misbehaving (OOM?)");
+
+
/* Configure timeout, wait for child, cancel timeout. */
if (exec_tmout) {
@@ -314,13 +440,21 @@ static void run_target(char** argv) {
setitimer(ITIMER_REAL, &it, NULL);
- if (waitpid(child_pid, &status, 0) <= 0) FATAL("waitpid() failed");
+ if ((res = read(fsrv_st_fd, &status, 4)) != 4) {
+
+ if (stop_soon) return 0;
+ RPFATAL(res, "Unable to communicate with fork server (OOM?)");
+
+ }
child_pid = 0;
it.it_value.tv_sec = 0;
it.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &it, NULL);
+ if(!quiet_mode)
+ SAYF(cRST "-- Program output ends --\n");
+
MEM_BARRIER();
/* Clean up bitmap, analyze exit condition, etc. */
@@ -331,8 +465,7 @@ static void run_target(char** argv) {
classify_counts(trace_bits, binary_mode ?
count_class_binary : count_class_human);
- if (!quiet_mode)
- SAYF(cRST "-- Program output ends --\n");
+ prev_timed_out = child_timed_out;
if (!child_timed_out && !stop_soon && WIFSIGNALED(status))
child_crashed = 1;
@@ -348,7 +481,7 @@ static void run_target(char** argv) {
}
-
+ return 0;
}
@@ -410,6 +543,9 @@ static void setup_signal_handlers(void) {
sa.sa_handler = handle_timeout;
sigaction(SIGALRM, &sa, NULL);
+ /* ignore */
+ sigaction(SIGPIPE, &sa, NULL);
+
}
@@ -471,11 +607,12 @@ static void usage(u8* argv0) {
show_banner();
- SAYF("\n%s [ options ] -- /path/to/target_app [ ... ]\n\n"
+ SAYF("\n%s [ options ] -- /path/to/target_app [ ... ]\n"
+ "%s [ options ] files.. -- /path/to/target_app [ ... ]\n\n"
"Required parameters:\n\n"
- " -o file - file to write the trace data to\n\n"
+ " -o file - file (or dir) to write the trace data to\n\n"
"Execution control settings:\n\n"
@@ -492,7 +629,7 @@ static void usage(u8* argv0) {
"This tool displays raw tuple data captured by AFL instrumentation.\n"
"For additional help, consult %s/README.\n\n" cRST,
- argv0, MEM_LIMIT, doc_path);
+ argv0, argv0, MEM_LIMIT, doc_path);
exit(1);
@@ -554,7 +691,8 @@ static void find_binary(u8* fname) {
/* Fix up argv for QEMU. */
-static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
+static char** get_qemu_argv(int qemu_mode, u8* own_loc, char** argv, int argc) {
+
char** new_argv = ck_alloc(sizeof(char*) * (argc + 4));
u8 *tmp, *cp, *rsl, *own_copy;
@@ -563,10 +701,13 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
setenv("QEMU_LOG", "nochain", 1);
- memcpy(new_argv + 3, argv + 1, sizeof(char*) * argc);
-
- new_argv[2] = target_path;
- new_argv[1] = "--";
+ if(qemu_mode == 1) {
+ memcpy(new_argv + 3, argv + 1, sizeof(char*) * argc);
+ new_argv[2] = target_path;
+ new_argv[1] = "--";
+ } else {
+ memcpy(new_argv + 1, argv + 1, sizeof(char*) * argc);
+ }
/* Now we need to actually find qemu for argv[0]. */
@@ -574,7 +715,10 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
if (tmp) {
- cp = alloc_printf("%s/afl-qemu-trace", tmp);
+ if(qemu_mode == 1)
+ cp = alloc_printf("%s/afl-qemu-trace", tmp);
+ else
+ cp = alloc_printf("%s/afl-qemu-system-trace", tmp);
if (access(cp, X_OK))
FATAL("Unable to find '%s'", tmp);
@@ -591,7 +735,10 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
*rsl = 0;
- cp = alloc_printf("%s/afl-qemu-trace", own_copy);
+ if(qemu_mode == 1)
+ cp = alloc_printf("%s/afl-qemu-trace", own_copy);
+ else
+ cp = alloc_printf("%s/afl-qemu-system-trace", own_copy);
ck_free(own_copy);
if (!access(cp, X_OK)) {
@@ -603,10 +750,15 @@ static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
} else ck_free(own_copy);
- if (!access(BIN_PATH "/afl-qemu-trace", X_OK)) {
+ if (qemu_mode == 1 && !access(BIN_PATH "/afl-qemu-trace", X_OK)) {
target_path = new_argv[0] = BIN_PATH "/afl-qemu-trace";
return new_argv;
+ }
+ if (qemu_mode > 1 && !access(BIN_PATH "/afl-qemu-system-trace", X_OK)) {
+
+ target_path = new_argv[0] = BIN_PATH "/afl-qemu-system-trace";
+ return new_argv;
}
@@ -622,9 +774,23 @@ int main(int argc, char** argv) {
s32 opt;
u8 mem_limit_given = 0, timeout_given = 0, qemu_mode = 0;
u32 tcnt;
- char** use_argv;
+ char** use_argv, **argv2, **files;
+ int argc2, i, nfiles;
+
doc_path = access(DOC_PATH, F_OK) ? "docs" : DOC_PATH;
+
+ /* split command line */
+ for(i = 1; i < argc; i++) {
+ if(strcmp(argv[i], "--") == 0)
+ break;
+ }
+ if(!argv[i])
+ usage(argv[0]);
+ argv[i] = 0;
+ argv2 = argv + (i + 1);
+ argc2 = argc - (i + 1);
+ argc = i;
while ((opt = getopt(argc,argv,"+o:m:t:A:eqZQbc")) > 0)
@@ -717,10 +883,10 @@ int main(int argc, char** argv) {
case 'Q':
- if (qemu_mode) FATAL("Multiple -Q options not supported");
+ //if (qemu_mode) FATAL("Multiple -Q options not supported");
if (!mem_limit_given) mem_limit = MEM_LIMIT_QEMU;
- qemu_mode = 1;
+ qemu_mode += 1;
break;
case 'b':
@@ -743,36 +909,62 @@ int main(int argc, char** argv) {
}
- if (optind == argc || !out_file) usage(argv[0]);
+ files = argv + optind;
+ nfiles = argc - optind;
+
+ if (!out_file || !argc2) usage(argv[0]);
+ if (files[0] && at_file) FATAL("Multi-file and -A option are incompatible");
setup_shm();
setup_signal_handlers();
set_up_environment();
- find_binary(argv[optind]);
+ find_binary(argv2[0]);
+
if (!quiet_mode) {
show_banner();
ACTF("Executing '%s'...\n", target_path);
}
- detect_file_args(argv + optind);
+ if(files[0])
+ at_file = ".curinput"; // XXX shared by everyone in cwd!
+ detect_file_args(argv2);
if (qemu_mode)
- use_argv = get_qemu_argv(argv[0], argv + optind, argc - optind);
+ use_argv = get_qemu_argv(qemu_mode, argv[0], argv2, argc2);
else
- use_argv = argv + optind;
+ use_argv = argv2;
- run_target(use_argv);
+ init_forkserver(use_argv);
+ if(!files[0]) { /* just run cmd, output to file */
+ run_target(use_argv);
+ tcnt = write_results();
- tcnt = write_results();
-
- if (!quiet_mode) {
-
- if (!tcnt) FATAL("No instrumentation detected" cRST);
- OKF("Captured %u tuples in '%s'." cRST, tcnt, out_file);
+ if (!quiet_mode) {
+ if (!tcnt) FATAL("No instrumentation detected" cRST);
+ OKF("Captured %u tuples in '%s'." cRST, tcnt, out_file);
+ }
+ } else { /* run all files, output to dir */
+ char *out_dir = out_file;
+ mkdir(out_dir, 0755);
+ for(i = 0; i < nfiles; i++) {
+ SAYF("file %d/%d - %s\n", i+1, nfiles, files[i]);
+ unlink(at_file);
+ if(symlink(files[i], at_file) == -1)
+ FATAL("symlink %s to %s failed", files[i], at_file);
+
+ run_target(use_argv);
+
+ out_file = alloc_printf("%s/%s", out_dir, basename(files[i]));
+ tcnt = write_results();
+ if (!quiet_mode) {
+ if (!tcnt) FATAL("No instrumentation detected" cRST);
+ OKF("Captured %u tuples in '%s'." cRST, tcnt, out_file);
+ }
+ }
}
exit(child_crashed * 2 + child_timed_out);
diff --git a/afl-tmin.c b/afl-tmin.c
index a58c520..96cbe31 100644
--- a/afl-tmin.c
+++ b/afl-tmin.c
@@ -44,7 +44,11 @@
#include <sys/types.h>
#include <sys/resource.h>
-static s32 child_pid; /* PID of the tested program */
+static s32 forksrv_pid, /* PID of the fork server */
+ child_pid; /* PID of the tested program */
+
+static s32 fsrv_ctl_fd, /* Fork server control pipe (write) */
+ fsrv_st_fd; /* Fork server status pipe (read) */
static u8 *trace_bits, /* SHM with instrumentation bitmap */
*mask_bitmap; /* Mask for trace bits (-B) */
@@ -55,8 +59,12 @@ static u8 *in_file, /* Minimizer input test case */
*target_path, /* Path to target binary */
*doc_path; /* Path to docs */
+static s32 prog_in_fd; /* Persistent fd for prog_in */
+
static u8* in_data; /* Input data for trimming */
+static u8 qemu_mode = 0; /* Running in QEMU mode? */
+
static u32 in_len, /* Input data length */
orig_cksum, /* Original checksum */
total_execs, /* Total number of execs */
@@ -236,38 +244,71 @@ static s32 write_to_file(u8* path, u8* mem, u32 len) {
}
+/* Write modified data to file for testing. If use_stdin is clear, the old file
+ is unlinked and a new one is created. Otherwise, prog_in_fd is rewound and
+ truncated. */
+
+static void write_to_testcase(void* mem, u32 len) {
+
+ s32 fd = prog_in_fd;
+
+ if (!use_stdin) {
+
+ unlink(prog_in); /* Ignore errors. */
+
+ fd = open(prog_in, O_WRONLY | O_CREAT | O_EXCL, 0600);
+
+ if (fd < 0) PFATAL("Unable to create '%s'", prog_in);
+
+ } else lseek(fd, 0, SEEK_SET);
+
+ ck_write(fd, mem, len, prog_in);
+
+ if (use_stdin) {
+
+ if (ftruncate(fd, len)) PFATAL("ftruncate() failed");
+ lseek(fd, 0, SEEK_SET);
+
+ } else close(fd);
+
+}
+
+
/* Handle timeout signal. */
static void handle_timeout(int sig) {
- child_timed_out = 1;
- if (child_pid > 0) kill(child_pid, SIGKILL);
+ if (child_pid > 0) {
-}
+ child_timed_out = 1;
+ kill(child_pid, SIGKILL);
+ } else if (child_pid == -1 && forksrv_pid > 0) {
-/* Execute target application. Returns 0 if the changes are a dud, or
- 1 if they should be kept. */
+ child_timed_out = 1;
+ kill(forksrv_pid, SIGKILL);
-static u8 run_target(char** argv, u8* mem, u32 len, u8 first_run) {
+ }
- static struct itimerval it;
- int status = 0;
+}
- s32 prog_in_fd;
- u32 cksum;
- memset(trace_bits, 0, MAP_SIZE);
- MEM_BARRIER();
+/* start the app and it's forkserver */
+static void init_forkserver(char **argv) {
+ static struct itimerval it;
+ int st_pipe[2], ctl_pipe[2];
+ int status = 0;
+ s32 rlen;
- prog_in_fd = write_to_file(prog_in, mem, len);
+ ACTF("Spinning up the fork server...");
+ if (pipe(st_pipe) || pipe(ctl_pipe)) PFATAL("pipe() failed");
- child_pid = fork();
+ forksrv_pid = fork();
- if (child_pid < 0) PFATAL("fork() failed");
+ if (forksrv_pid< 0) PFATAL("fork() failed");
- if (!child_pid) {
+ if (!forksrv_pid) {
struct rlimit r;
@@ -303,6 +344,16 @@ static u8 run_target(char** argv, u8* mem, u32 len, u8 first_run) {
r.rlim_max = r.rlim_cur = 0;
setrlimit(RLIMIT_CORE, &r); /* Ignore errors */
+
+ /* Set up control and status pipes, close the unneeded original fds. */
+
+ if (dup2(ctl_pipe[0], FORKSRV_FD) < 0) PFATAL("dup2() failed");
+ if (dup2(st_pipe[1], FORKSRV_FD + 1) < 0) PFATAL("dup2() failed");
+