forked from aflnet/aflnet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
afl-fuzz.c
9696 lines (6394 loc) · 259 KB
/
afl-fuzz.c
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
/*
Copyright 2013 Google LLC All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
american fuzzy lop - fuzzer code
--------------------------------
Written and maintained by Michal Zalewski <lcamtuf@google.com>
Forkserver design by Jann Horn <jannhorn@googlemail.com>
This is the real deal: the program takes an instrumented binary and
attempts a variety of basic fuzzing tricks, paying close attention to
how they affect the execution path.
*/
#define AFL_MAIN
#include "android-ashmem.h"
#define MESSAGES_TO_STDOUT
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#define _FILE_OFFSET_BITS 64
#include "config.h"
#include "types.h"
#include "debug.h"
#include "alloc-inl.h"
#include "hash.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <dirent.h>
#include <ctype.h>
#include <fcntl.h>
#include <termios.h>
#include <dlfcn.h>
#include <sched.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include "aflnet.h"
#include <graphviz/gvc.h>
#include <math.h>
#if defined(__APPLE__) || defined(__FreeBSD__) || defined (__OpenBSD__)
# include <sys/sysctl.h>
#endif /* __APPLE__ || __FreeBSD__ || __OpenBSD__ */
/* For systems that have sched_setaffinity; right now just Linux, but one
can hope... */
#ifdef __linux__
# define HAVE_AFFINITY 1
#endif /* __linux__ */
/* A toggle to export some variables when building as a library. Not very
useful for the general public. */
#ifdef AFL_LIB
# define EXP_ST
#else
# define EXP_ST static
#endif /* ^AFL_LIB */
/* Lots of globals, but mostly for the status UI and other things where it
really makes no sense to haul them around as function parameters. */
EXP_ST u8 *in_dir, /* Input directory with test cases */
*out_file, /* File to fuzz, if any */
*out_dir, /* Working & output directory */
*sync_dir, /* Synchronization directory */
*sync_id, /* Fuzzer ID */
*use_banner, /* Display banner */
*in_bitmap, /* Input bitmap */
*doc_path, /* Path to documentation dir */
*target_path, /* Path to target binary */
*orig_cmdline, /* Original command line */
*sbr_plugin_path; /* Path to SaBRe's plugin to load */
EXP_ST u32 exec_tmout = EXEC_TIMEOUT; /* Configurable exec timeout (ms) */
static u32 hang_tmout = EXEC_TIMEOUT; /* Timeout used for hang det (ms) */
EXP_ST u64 mem_limit = MEM_LIMIT; /* Memory cap for child (MB) */
static u32 stats_update_freq = 1; /* Stats update frequency (execs) */
EXP_ST u8 skip_deterministic, /* Skip deterministic stages? */
force_deterministic, /* Force deterministic stages? */
use_splicing, /* Recombine input files? */
dumb_mode, /* Run in non-instrumented mode? */
score_changed, /* Scoring for favorites changed? */
kill_signal, /* Signal that killed the child */
resuming_fuzz, /* Resuming an older fuzzing job? */
timeout_given, /* Specific timeout given? */
not_on_tty, /* stdout is not a tty */
term_too_small, /* terminal dimensions too small */
uses_asan, /* Target uses ASAN? */
no_forkserver, /* Disable forkserver? */
crash_mode, /* Crash mode! Yeah! */
in_place_resume, /* Attempt in-place resume? */
auto_changed, /* Auto-generated tokens changed? */
no_cpu_meter_red, /* Feng shui on the status screen */
no_arith, /* Skip most arithmetic ops */
shuffle_queue, /* Shuffle input queue? */
bitmap_changed = 1, /* Time to update bitmap? */
qemu_mode, /* Running in QEMU mode? */
sbr_mode, /* Running in SaBRe mode? */
skip_requested, /* Skip request, via SIGUSR1 */
run_over10m, /* Run time over 10 minutes? */
persistent_mode, /* Running in persistent mode? */
deferred_mode, /* Deferred forkserver mode? */
fast_cal; /* Try to calibrate faster? */
static s32 out_fd, /* Persistent fd for out_file */
dev_urandom_fd = -1, /* Persistent fd for /dev/urandom */
dev_null_fd = -1, /* Persistent fd for /dev/null */
fsrv_ctl_fd, /* Fork server control pipe (write) */
fsrv_st_fd, /* Fork server status pipe (read) */
sbr_data_fd, /* Transfere data to/from SaBRe */
sbr_ctl_fd, /* Understand state of SaBRe client */
sbr_data_fd_target,
sbr_ctl_fd_target;
static s32 forksrv_pid, /* PID of the fork server */
child_pid = -1, /* PID of the fuzzed program */
out_dir_fd = -1; /* FD of the lock file */
EXP_ST u8* trace_bits; /* SHM with instrumentation bitmap */
EXP_ST u8 virgin_bits[MAP_SIZE], /* Regions yet untouched by fuzzing */
virgin_tmout[MAP_SIZE], /* Bits we haven't seen in tmouts */
virgin_crash[MAP_SIZE]; /* Bits we haven't seen in crashes */
static u8 var_bytes[MAP_SIZE]; /* Bytes that appear to be variable */
static s32 shm_id; /* ID of the SHM region */
static volatile u8 stop_soon, /* Ctrl-C pressed? */
clear_screen = 1, /* Window resized? */
child_timed_out; /* Traced process timed out? */
EXP_ST u32 queued_paths, /* Total number of queued testcases */
queued_variable, /* Testcases with variable behavior */
queued_at_start, /* Total number of initial inputs */
queued_discovered, /* Items discovered during this run */
queued_imported, /* Items imported via -S */
queued_favored, /* Paths deemed favorable */
queued_with_cov, /* Paths with new coverage bytes */
pending_not_fuzzed, /* Queued but not done yet */
pending_favored, /* Pending favored paths */
cur_skipped_paths, /* Abandoned inputs in cur cycle */
cur_depth, /* Current path depth */
max_depth, /* Max path depth */
useless_at_start, /* Number of useless starting paths */
var_byte_count, /* Bitmap bytes with var behavior */
current_entry, /* Current queue entry ID */
havoc_div = 1; /* Cycle count divisor for havoc */
EXP_ST u64 total_crashes, /* Total number of crashes */
unique_crashes, /* Crashes with unique signatures */
total_tmouts, /* Total number of timeouts */
unique_tmouts, /* Timeouts with unique signatures */
unique_hangs, /* Hangs with unique signatures */
total_execs, /* Total execve() calls */
slowest_exec_ms, /* Slowest testcase non hang in ms */
start_time, /* Unix start time (ms) */
last_path_time, /* Time for most recent path (ms) */
last_crash_time, /* Time for most recent crash (ms) */
last_hang_time, /* Time for most recent hang (ms) */
last_crash_execs, /* Exec counter at last crash */
queue_cycle, /* Queue round counter */
cycles_wo_finds, /* Cycles without any new paths */
trim_execs, /* Execs done to trim input files */
bytes_trim_in, /* Bytes coming into the trimmer */
bytes_trim_out, /* Bytes coming outa the trimmer */
blocks_eff_total, /* Blocks subject to effector maps */
blocks_eff_select; /* Blocks selected as fuzzable */
static u32 subseq_tmouts; /* Number of timeouts in a row */
static u8 *stage_name = "init", /* Name of the current fuzz stage */
*stage_short, /* Short stage name */
*syncing_party; /* Currently syncing with... */
static s32 stage_cur, stage_max; /* Stage progression */
static s32 splicing_with = -1; /* Splicing with which test case? */
static u32 master_id, master_max; /* Master instance job splitting */
static u32 syncing_case; /* Syncing with case #... */
static s32 stage_cur_byte, /* Byte offset of current stage op */
stage_cur_val; /* Value used for stage op */
static u8 stage_val_type; /* Value type (STAGE_VAL_*) */
static u64 stage_finds[32], /* Patterns found per fuzz stage */
stage_cycles[32]; /* Execs per fuzz stage */
static u32 rand_cnt; /* Random number counter */
static u64 total_cal_us, /* Total calibration time (us) */
total_cal_cycles; /* Total calibration cycles */
static u64 total_bitmap_size, /* Total bit count for all bitmaps */
total_bitmap_entries; /* Number of bitmaps counted */
static s32 cpu_core_count; /* CPU core count */
#ifdef HAVE_AFFINITY
static s32 cpu_aff = -1; /* Selected CPU core */
#endif /* HAVE_AFFINITY */
static FILE* plot_file; /* Gnuplot output file */
struct queue_entry {
u8* fname; /* File name for the test case */
u32 len; /* Input length */
u8 cal_failed, /* Calibration failed? */
trim_done, /* Trimmed? */
was_fuzzed, /* Had any fuzzing done yet? */
passed_det, /* Deterministic stages passed? */
has_new_cov, /* Triggers new coverage? */
var_behavior, /* Variable behavior? */
favored, /* Currently favored? */
fs_redundant; /* Marked as redundant in the fs? */
u32 bitmap_size, /* Number of bits set in bitmap */
exec_cksum; /* Checksum of the execution trace */
u64 exec_us, /* Execution time (us) */
handicap, /* Number of queue cycles behind */
depth; /* Path depth */
u8* trace_mini; /* Trace bytes, if kept */
u32 tc_ref; /* Trace bytes ref count */
struct queue_entry *next, /* Next element, if any */
*next_100; /* 100 elements ahead */
region_t *regions; /* Regions keeping information of message(s) sent to the server under test */
u32 region_count; /* Total number of regions in this seed */
u32 index; /* Index of this queue entry in the whole queue */
u32 generating_state_id; /* ID of the start at which the new seed was generated */
u8 is_initial_seed; /* Is this an initial seed */
u32 unique_state_count; /* Unique number of states traversed by this queue entry */
};
static struct queue_entry *queue, /* Fuzzing queue (linked list) */
*queue_cur, /* Current offset within the queue */
*queue_top, /* Top of the list */
*q_prev100; /* Previous 100 marker */
static struct queue_entry*
top_rated[MAP_SIZE]; /* Top entries for bitmap bytes */
struct extra_data {
u8* data; /* Dictionary token data */
u32 len; /* Dictionary token length */
u32 hit_cnt; /* Use count in the corpus */
};
static struct extra_data* extras; /* Extra tokens to fuzz with */
static u32 extras_cnt; /* Total number of tokens read */
static struct extra_data* a_extras; /* Automatically selected extras */
static u32 a_extras_cnt; /* Total number of tokens available */
static u8* (*post_handler)(u8* buf, u32* len);
/* Interesting values, as per config.h */
static s8 interesting_8[] = { INTERESTING_8 };
static s16 interesting_16[] = { INTERESTING_8, INTERESTING_16 };
static s32 interesting_32[] = { INTERESTING_8, INTERESTING_16, INTERESTING_32 };
/* Fuzzing stages */
enum {
/* 00 */ STAGE_FLIP1,
/* 01 */ STAGE_FLIP2,
/* 02 */ STAGE_FLIP4,
/* 03 */ STAGE_FLIP8,
/* 04 */ STAGE_FLIP16,
/* 05 */ STAGE_FLIP32,
/* 06 */ STAGE_ARITH8,
/* 07 */ STAGE_ARITH16,
/* 08 */ STAGE_ARITH32,
/* 09 */ STAGE_INTEREST8,
/* 10 */ STAGE_INTEREST16,
/* 11 */ STAGE_INTEREST32,
/* 12 */ STAGE_EXTRAS_UO,
/* 13 */ STAGE_EXTRAS_UI,
/* 14 */ STAGE_EXTRAS_AO,
/* 15 */ STAGE_HAVOC,
/* 16 */ STAGE_SPLICE
};
/* Stage value types */
enum {
/* 00 */ STAGE_VAL_NONE,
/* 01 */ STAGE_VAL_LE,
/* 02 */ STAGE_VAL_BE
};
/* Execution status fault codes */
enum {
/* 00 */ FAULT_NONE,
/* 01 */ FAULT_TMOUT,
/* 02 */ FAULT_CRASH,
/* 03 */ FAULT_ERROR,
/* 04 */ FAULT_NOINST,
/* 05 */ FAULT_NOBITS
};
char** use_argv; /* argument to run the target program. In vanilla AFL, this is a local variable in main. */
/* add these declarations here so we can call these functions earlier */
static u8 run_target(char** argv, u32 timeout);
static inline u32 UR(u32 limit);
static inline u8 has_new_bits(u8* virgin_map);
/* AFLNet-specific variables & functions */
u32 server_wait_usecs = 10000;
u32 poll_wait_msecs = 1;
u32 socket_timeout_usecs = 1000;
u8 net_protocol;
u8* net_ip;
u32 net_port;
char *response_buf = NULL;
int response_buf_size = 0; //the size of the whole response buffer
u32 *response_bytes = NULL; //an array keeping accumulated response buffer size
//e.g., response_bytes[i] keeps the response buffer size
//once messages 0->i have been received and processed by the SUT
u32 max_annotated_regions = 0;
u32 target_state_id = 0;
u32 *state_ids = NULL;
u32 state_ids_count = 0;
u32 selected_state_index = 0;
u32 state_cycles = 0;
u32 messages_sent = 0;
EXP_ST u8 session_virgin_bits[MAP_SIZE]; /* Regions yet untouched while the SUT is still running */
EXP_ST u8 *cleanup_script; /* script to clean up the environment of the SUT -- make fuzzing more deterministic */
char **was_fuzzed_map = NULL; /* A 2D array keeping state-specific was_fuzzed information */
u32 fuzzed_map_states = 0;
u32 fuzzed_map_qentries = 0;
u32 max_seed_region_count = 0;
u32 local_port; /* TCP/UDP port number to use as source */
/* flags */
u8 use_net = 0;
u8 poll_wait = 0;
u8 server_wait = 0;
u8 socket_timeout = 0;
u8 protocol_selected = 0;
u8 terminate_child = 0;
u8 corpus_read_or_sync = 0;
u8 state_aware_mode = 0;
u8 region_level_mutation = 0;
u8 state_selection_algo = ROUND_ROBIN, seed_selection_algo = RANDOM_SELECTION;
u8 false_negative_reduction = 0;
/* Implemented state machine */
Agraph_t *ipsm;
static FILE* ipsm_dot_file;
/* Hash table/map and list */
klist_t(lms) *kl_messages;
khash_t(hs32) *khs_ipsm_paths;
khash_t(hms) *khms_states;
//M2_prev points to the last message of M1 (i.e., prefix)
//If M1 is empty, M2_prev == NULL
//M2_next points to the first message of M3 (i.e., suffix)
//If M3 is empty, M2_next point to the end of the kl_messages linked list
kliter_t(lms) *M2_prev, *M2_next;
//Function pointers pointing to Protocol-specific functions
unsigned int* (*extract_response_codes)(unsigned char* buf, unsigned int buf_size, unsigned int* state_count_ref) = NULL;
region_t* (*extract_requests)(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref) = NULL;
/* Initialize the implemented state machine as a graphviz graph */
void setup_ipsm()
{
ipsm = agopen("g", Agdirected, 0);
agattr(ipsm, AGNODE, "color", "black"); //Default node colr is black
agattr(ipsm, AGEDGE, "color", "black"); //Default edge color is black
khs_ipsm_paths = kh_init(hs32);
khms_states = kh_init(hms);
}
/* Free memory allocated to state-machine variables */
void destroy_ipsm()
{
agclose(ipsm);
kh_destroy(hs32, khs_ipsm_paths);
state_info_t *state;
kh_foreach_value(khms_states, state, {ck_free(state->seeds); ck_free(state);});
kh_destroy(hms, khms_states);
ck_free(state_ids);
}
/* Get state index in the state IDs list, given a state ID */
u32 get_state_index(u32 state_id) {
u32 index = 0;
for (index = 0; index < state_ids_count; index++) {
if (state_ids[index] == state_id) break;
}
return index;
}
/* Expand the size of the map when a new seed or a new state has been discovered */
void expand_was_fuzzed_map(u32 new_states, u32 new_qentries) {
int i, j;
//Realloc the memory
was_fuzzed_map = (char **)ck_realloc(was_fuzzed_map, (fuzzed_map_states + new_states) * sizeof(char *));
for (i = 0; i < fuzzed_map_states + new_states; i++)
was_fuzzed_map[i] = (char *)ck_realloc(was_fuzzed_map[i], (fuzzed_map_qentries + new_qentries) * sizeof(char));
//All new cells are marked as -1 -- meaning UNREACHABLE
//Keep other cells untouched
for (i = 0; i < fuzzed_map_states + new_states; i++)
for (j = 0; j < fuzzed_map_qentries + new_qentries; j++)
if ((i >= fuzzed_map_states) || (j >= fuzzed_map_qentries)) was_fuzzed_map[i][j] = -1;
//Update total number of states (rows) and total number of queue entries (columns) in the was_fuzzed_map
fuzzed_map_states += new_states;
fuzzed_map_qentries += new_qentries;
}
/* Get unique state count, given a state sequence */
u32 get_unique_state_count(unsigned int *state_sequence, unsigned int state_count) {
//A hash set is used so that no state is counted twice
khash_t(hs32) *khs_state_ids;
khs_state_ids = kh_init(hs32);
unsigned int discard, state_id, i;
u32 result = 0;
for (i = 0; i < state_count; i++) {
state_id = state_sequence[i];
if (kh_get(hs32, khs_state_ids, state_id) != kh_end(khs_state_ids)) {
continue;
} else {
kh_put(hs32, khs_state_ids, state_id, &discard);
result++;
}
}
kh_destroy(hs32, khs_state_ids);
return result;
}
/* Check if a state sequence is interesting (e.g., new state is discovered). Loop is taken into account */
u8 is_state_sequence_interesting(unsigned int *state_sequence, unsigned int state_count) {
//limit the loop count to only 1
u32 *trimmed_state_sequence = NULL;
u32 i, count = 0;
for (i=0; i < state_count; i++) {
if ((i >= 2) && (state_sequence[i] == state_sequence[i - 1]) && (state_sequence[i] == state_sequence[i - 2])) continue;
count++;
trimmed_state_sequence = (u32 *)realloc(trimmed_state_sequence, count * sizeof(unsigned int));
trimmed_state_sequence[count - 1] = state_sequence[i];
}
//Calculate the hash based on the shortened state sequence
u32 hashKey = hash32(trimmed_state_sequence, count * sizeof(unsigned int), 0);
if (trimmed_state_sequence) free(trimmed_state_sequence);
if (kh_get(hs32, khs_ipsm_paths, hashKey) != kh_end(khs_ipsm_paths)) {
return 0;
} else {
int dummy;
kh_put(hs32, khs_ipsm_paths, hashKey, &dummy);
return 1;
}
}
/* Update the annotations of regions (i.e., state sequence received from the server) */
void update_region_annotations(struct queue_entry* q)
{
u32 i = 0;
for (i = 0; i < messages_sent; i++) {
if ((response_bytes[i] == 0) || ( i > 0 && (response_bytes[i] - response_bytes[i - 1] == 0))) {
q->regions[i].state_sequence = NULL;
q->regions[i].state_count = 0;
} else {
unsigned int state_count;
q->regions[i].state_sequence = (*extract_response_codes)(response_buf, response_bytes[i], &state_count);
q->regions[i].state_count = state_count;
}
}
}
/* Choose a region data for region-level mutations */
u8* choose_source_region(u32 *out_len) {
u8 *out = NULL;
*out_len = 0;
struct queue_entry *q = queue;
//randomly select a seed
u32 index = UR(queued_paths);
while (index != 0) {
q = q->next;
index--;
}
//randomly select a region in the selected seed
if (q->region_count) {
u32 reg_index = UR(q->region_count);
u32 len = q->regions[reg_index].end_byte - q->regions[reg_index].start_byte + 1;
if (len <= MAX_FILE) {
out = (u8 *)ck_alloc(len);
if (out == NULL) PFATAL("Unable allocate a memory region to store a region");
*out_len = len;
//Read region data into memory. */
FILE *fp = fopen(q->fname, "rb");
fseek(fp, q->regions[reg_index].start_byte, SEEK_CUR);
fread(out, 1, len, fp);
fclose(fp);
}
}
return out;
}
/* Update #fuzzs visiting a specific state */
void update_fuzzs() {
unsigned int state_count, i, discard;
unsigned int *state_sequence = (*extract_response_codes)(response_buf, response_buf_size, &state_count);
//A hash set is used so that the #paths is not updated more than once for one specific state
khash_t(hs32) *khs_state_ids;
khint_t k;
khs_state_ids = kh_init(hs32);
for(i = 0; i < state_count; i++) {
unsigned int state_id = state_sequence[i];
if (kh_get(hs32, khs_state_ids, state_id) != kh_end(khs_state_ids)) {
continue;
} else {
kh_put(hs32, khs_state_ids, state_id, &discard);
k = kh_get(hms, khms_states, state_id);
if (k != kh_end(khms_states)) {
kh_val(khms_states, k)->fuzzs++;
}
}
}
ck_free(state_sequence);
kh_destroy(hs32, khs_state_ids);
}
/* Return the index of the "region" containing a given value */
u32 index_search(u32 *A, u32 n, u32 val) {
u32 index = 0;
for(index = 0; index < n; index++) {
if (val <= A[index]) break;
}
return index;
}
/* Calculate state scores and select the next state */
u32 update_scores_and_select_next_state(u8 mode) {
u32 result = 0, i;
if (state_ids_count == 0) return 0;
u32 *state_scores = NULL;
state_scores = (u32 *)ck_alloc(state_ids_count * sizeof(u32));
if (!state_scores) PFATAL("Cannot allocate memory for state_scores");
khint_t k;
state_info_t *state;
//Update the states' score
for(i = 0; i < state_ids_count; i++) {
u32 state_id = state_ids[i];
k = kh_get(hms, khms_states, state_id);
if (k != kh_end(khms_states)) {
state = kh_val(khms_states, k);
switch(mode) {
case FAVOR:
state->score = ceil(1000 * pow(2, -log10(log10(state->fuzzs + 1) * state->selected_times + 1)) * pow(2, log(state->paths_discovered + 1)));
break;
//other cases are reserved
}
if (i == 0) {
state_scores[i] = state->score;
} else {
state_scores[i] = state_scores[i-1] + state->score;
}
}
}
u32 randV = UR(state_scores[state_ids_count - 1]);
u32 idx = index_search(state_scores, state_ids_count, randV);
result = state_ids[idx];
if (state_scores) ck_free(state_scores);
return result;
}
/* Select a target state at which we do state-aware fuzzing */
unsigned int choose_target_state(u8 mode) {
u32 result = 0;
switch (mode) {
case RANDOM_SELECTION: //Random state selection
selected_state_index = UR(state_ids_count);
result = state_ids[selected_state_index];
break;
case ROUND_ROBIN: //Round-robin state selection
result = state_ids[selected_state_index];
selected_state_index++;
if (selected_state_index == state_ids_count) selected_state_index = 0;
break;
case FAVOR:
/* Do ROUND_ROBIN for a few cycles to get enough statistical information*/
if (state_cycles < 5) {
result = state_ids[selected_state_index];
selected_state_index++;
if (selected_state_index == state_ids_count) {
selected_state_index = 0;
state_cycles++;
}
break;
}
result = update_scores_and_select_next_state(FAVOR);
break;
default:
break;
}
return result;
}
/* Select a seed to exercise the target state */
struct queue_entry *choose_seed(u32 target_state_id, u8 mode)
{
khint_t k;
state_info_t *state;
struct queue_entry *result = NULL;
k = kh_get(hms, khms_states, target_state_id);
if (k != kh_end(khms_states)) {
state = kh_val(khms_states, k);
if (state->seeds_count == 0) return NULL;
switch (mode) {
case RANDOM_SELECTION: //Random seed selection
state->selected_seed_index = UR(state->seeds_count);
result = state->seeds[state->selected_seed_index];
break;
case ROUND_ROBIN: //Round-robin seed selection
result = state->seeds[state->selected_seed_index];
state->selected_seed_index++;
if (state->selected_seed_index == state->seeds_count) state->selected_seed_index = 0;
break;
case FAVOR:
if (state->seeds_count > 10) {
//Do seed selection similar to AFL + take into account state-aware information
//e.g., was_fuzzed information becomes state-aware
u32 passed_cycles = 0;
while (passed_cycles < 5) {
result = state->seeds[state->selected_seed_index];
if (state->selected_seed_index + 1 == state->seeds_count) {
state->selected_seed_index = 0;
passed_cycles++;
} else state->selected_seed_index++;
//Skip this seed with high probability if it is neither an initial seed nor a seed generated while the
//current target_state_id was targeted
if (result->generating_state_id != target_state_id && !result->is_initial_seed && UR(100) < 90) continue;
u32 target_state_index = get_state_index(target_state_id);
if (pending_favored) {
/* If we have any favored, non-fuzzed new arrivals in the queue,
possibly skip to them at the expense of already-fuzzed or non-favored
cases. */
if (((was_fuzzed_map[target_state_index][result->index] == 1) || !result->favored) && UR(100) < SKIP_TO_NEW_PROB) continue;
/* Otherwise, this seed is selected */
break;
} else if (!result->favored && queued_paths > 10) {
/* Otherwise, still possibly skip non-favored cases, albeit less often.
The odds of skipping stuff are higher for already-fuzzed inputs and
lower for never-fuzzed entries. */
if (queue_cycle > 1 && (was_fuzzed_map[target_state_index][result->index] == 0)) {
if (UR(100) < SKIP_NFAV_NEW_PROB) continue;
} else {
if (UR(100) < SKIP_NFAV_OLD_PROB) continue;
}
/* Otherwise, this seed is selected */
break;
}
}
} else {
//Do Round-robin if seeds_count of the selected state is small
result = state->seeds[state->selected_seed_index];
state->selected_seed_index++;
if (state->selected_seed_index == state->seeds_count) state->selected_seed_index = 0;
}
break;
default:
break;
}
} else {
PFATAL("AFLNet - the states hashtable has no entries for state %d", target_state_id);
}
return result;
}
/* Update state-aware variables */
void update_state_aware_variables(struct queue_entry *q, u8 dry_run)
{
khint_t k;
int discard, i;
state_info_t *state;
unsigned int state_count;
if (!response_buf_size || !response_bytes) return;
unsigned int *state_sequence = (*extract_response_codes)(response_buf, response_buf_size, &state_count);
q->unique_state_count = get_unique_state_count(state_sequence, state_count);
if (is_state_sequence_interesting(state_sequence, state_count)) {
//Save the current kl_messages to a file which can be used to replay the newly discovered paths on the ipsm
u8 *temp_str = state_sequence_to_string(state_sequence, state_count);
u8 *fname = alloc_printf("%s/replayable-new-ipsm-paths/id:%s:%s", out_dir, temp_str, dry_run ? basename(q->fname) : "new");
save_kl_messages_to_file(kl_messages, fname, 1, messages_sent);
ck_free(temp_str);
ck_free(fname);
//Update the IPSM graph
if (state_count > 1) {
unsigned int prevStateID = state_sequence[0];
for(i=1; i < state_count; i++) {
unsigned int curStateID = state_sequence[i];
char fromState[STATE_STR_LEN], toState[STATE_STR_LEN];
snprintf(fromState, STATE_STR_LEN, "%d", prevStateID);
snprintf(toState, STATE_STR_LEN, "%d", curStateID);
//Check if the prevStateID and curStateID have been added to the state machine as vertices
//Check also if the edge prevStateID->curStateID has been added
Agnode_t *from, *to;
Agedge_t *edge;
from = agnode(ipsm, fromState, FALSE);
if (!from) {
//Add a node to the graph
from = agnode(ipsm, fromState, TRUE);
if (dry_run) agset(from,"color","blue");
else agset(from,"color","red");
//Insert this newly discovered state into the states hashtable
state_info_t *newState_From = (state_info_t *) ck_alloc (sizeof(state_info_t));
newState_From->id = prevStateID;
newState_From->is_covered = 1;
newState_From->paths = 0;
newState_From->paths_discovered = 0;
newState_From->selected_times = 0;
newState_From->fuzzs = 0;
newState_From->score = 1;
newState_From->selected_seed_index = 0;
newState_From->seeds = NULL;
newState_From->seeds_count = 0;
k = kh_put(hms, khms_states, prevStateID, &discard);
kh_value(khms_states, k) = newState_From;
//Insert this into the state_ids array too
state_ids = (u32 *) ck_realloc(state_ids, (state_ids_count + 1) * sizeof(u32));
state_ids[state_ids_count++] = prevStateID;
if (prevStateID != 0) expand_was_fuzzed_map(1, 0);
}
to = agnode(ipsm, toState, FALSE);
if (!to) {
//Add a node to the graph
to = agnode(ipsm, toState, TRUE);
if (dry_run) agset(to,"color","blue");
else agset(to,"color","red");
//Insert this newly discovered state into the states hashtable
state_info_t *newState_To = (state_info_t *) ck_alloc (sizeof(state_info_t));
newState_To->id = curStateID;
newState_To->is_covered = 1;
newState_To->paths = 0;
newState_To->paths_discovered = 0;
newState_To->selected_times = 0;
newState_To->fuzzs = 0;
newState_To->score = 1;
newState_To->selected_seed_index = 0;
newState_To->seeds = NULL;
newState_To->seeds_count = 0;
k = kh_put(hms, khms_states, curStateID, &discard);
kh_value(khms_states, k) = newState_To;
//Insert this into the state_ids array too
state_ids = (u32 *) ck_realloc(state_ids, (state_ids_count + 1) * sizeof(u32));
state_ids[state_ids_count++] = curStateID;
if (curStateID != 0) expand_was_fuzzed_map(1, 0);
}
//Check if an edge from->to exists
edge = agedge(ipsm, from, to, NULL, FALSE);
if (!edge) {
//Add an edge to the graph
edge = agedge(ipsm, from, to, "new_edge", TRUE);
if (dry_run) agset(edge, "color", "blue");
else agset(edge, "color", "red");
}
//Update prevStateID
prevStateID = curStateID;
}
}
//Update the dot file
s32 fd;
u8* tmp;
tmp = alloc_printf("%s/ipsm.dot", out_dir);
fd = open(tmp, O_WRONLY | O_CREAT, 0600);
if (fd < 0) {
PFATAL("Unable to create %s", tmp);
} else {
ipsm_dot_file = fdopen(fd, "w");
agwrite(ipsm, ipsm_dot_file);
close(fileno(ipsm_dot_file));
ck_free(tmp);
}
}
//Update others no matter the new seed leads to interesting state sequence or not
//Annotate the regions
update_region_annotations(q);
//Update the states hashtable to keep the list of seeds which help us to reach a specific state
//Iterate over the regions & their annotated state (sub)sequences and update the hashtable accordingly
//All seed should "reach" state 0 (initial state) so we add this one to the map first
k = kh_get(hms, khms_states, 0);
if (k != kh_end(khms_states)) {
state = kh_val(khms_states, k);
state->seeds = (void **) ck_realloc (state->seeds, (state->seeds_count + 1) * sizeof(void *));
state->seeds[state->seeds_count] = (void *)q;
state->seeds_count++;
was_fuzzed_map[0][q->index] = 0; //Mark it as reachable but not fuzzed
} else {
PFATAL("AFLNet - the states hashtable should always contain an entry of the initial state");
}
//Now update other states
for(i = 0; i < q->region_count; i++) {
unsigned int regional_state_count = q->regions[i].state_count;
if (regional_state_count > 0) {
//reachable_state_id is the last ID in the state_sequence
unsigned int reachable_state_id = q->regions[i].state_sequence[regional_state_count - 1];
k = kh_get(hms, khms_states, reachable_state_id);
if (k != kh_end(khms_states)) {
state = kh_val(khms_states, k);
state->seeds = (void **) ck_realloc (state->seeds, (state->seeds_count + 1) * sizeof(void *));
state->seeds[state->seeds_count] = (void *)q;
state->seeds_count++;
} else {
//XXX. This branch is supposed to be not reachable
//However, due to some undeterminism, new state could be seen during regions' annotating process
//even though the state was not observed before
//To completely fix this, we should fix all causes leading to potential undeterminism
//For now, we just add the state into the hashtable
state_info_t *newState = (state_info_t *) ck_alloc (sizeof(state_info_t));
newState->id = reachable_state_id;
newState->is_covered = 1;
newState->paths = 0;
newState->paths_discovered = 0;
newState->selected_times = 0;
newState->fuzzs = 0;
newState->score = 1;
newState->selected_seed_index = 0;
newState->seeds = NULL;
newState->seeds = (void **) ck_realloc (newState->seeds, sizeof(void *));
newState->seeds[0] = (void *)q;
newState->seeds_count = 1;
k = kh_put(hms, khms_states, reachable_state_id, &discard);
kh_value(khms_states, k) = newState;
//Insert this into the state_ids array too
state_ids = (u32 *) ck_realloc(state_ids, (state_ids_count + 1) * sizeof(u32));
state_ids[state_ids_count++] = reachable_state_id;
if (reachable_state_id != 0) expand_was_fuzzed_map(1, 0);
}
was_fuzzed_map[get_state_index(reachable_state_id)][q->index] = 0; //Mark it as reachable but not fuzzed
}
}
//Update the number of paths which have traversed a specific state
//It can be used for calculating fuzzing energy
//A hash set is used so that the #paths is not updated more than once for one specific state
khash_t(hs32) *khs_state_ids;
khs_state_ids = kh_init(hs32);
for(i = 0; i < state_count; i++) {
unsigned int state_id = state_sequence[i];
if (kh_get(hs32, khs_state_ids, state_id) != kh_end(khs_state_ids)) {
continue;
} else {
kh_put(hs32, khs_state_ids, state_id, &discard);
k = kh_get(hms, khms_states, state_id);
if (k != kh_end(khms_states)) {
kh_val(khms_states, k)->paths++;
}
}
}
kh_destroy(hs32, khs_state_ids);
//Update paths_discovered
if (!dry_run) {
k = kh_get(hms, khms_states, target_state_id);
if (k != kh_end(khms_states)) {
kh_val(khms_states, k)->paths_discovered++;
}
}
//Free state sequence
if (state_sequence) ck_free(state_sequence);
}
#ifdef PRINT_BENCH
static double get_current_time() {
struct timeval t;
gettimeofday(&t, 0);
return t.tv_sec + t.tv_usec*1e-6;
}
#else
static double get_current_time() {
return 0.0;
}
#endif