diff options
author | Andrea Fioraldi <andreafioraldi@gmail.com> | 2020-01-17 16:41:30 +0100 |
---|---|---|
committer | Andrea Fioraldi <andreafioraldi@gmail.com> | 2020-01-17 16:41:30 +0100 |
commit | b6c5974b3781449996f2791b80e22c9fa6c9ba18 (patch) | |
tree | 4140474ae9397d21b9a27154162bf8cb125fca02 /src/afl-fuzz-cmplog.c | |
parent | f24135f1ed86443ead13c3b580a5d831d69662d3 (diff) | |
download | afl++-b6c5974b3781449996f2791b80e22c9fa6c9ba18.tar.gz |
format
Diffstat (limited to 'src/afl-fuzz-cmplog.c')
-rw-r--r-- | src/afl-fuzz-cmplog.c | 248 |
1 files changed, 139 insertions, 109 deletions
diff --git a/src/afl-fuzz-cmplog.c b/src/afl-fuzz-cmplog.c index 350cb105..924784cc 100644 --- a/src/afl-fuzz-cmplog.c +++ b/src/afl-fuzz-cmplog.c @@ -17,17 +17,19 @@ u8 common_fuzz_cmplog_stuff(char** argv, u8* out_buf, u32 len); -extern struct cmp_map* cmp_map; // defined in afl-sharedmem.c +extern struct cmp_map* cmp_map; // defined in afl-sharedmem.c -u8* cmplog_binary; +u8* cmplog_binary; char** its_argv; ///// Colorization struct range { - u32 start; - u32 end; - struct range * next; + + u32 start; + u32 end; + struct range* next; + }; struct range* add_range(struct range* ranges, u32 start, u32 end) { @@ -46,36 +48,42 @@ struct range* pop_biggest_range(struct range** ranges) { struct range* prev = NULL; struct range* rmax = NULL; struct range* prev_rmax = NULL; - u32 max_size = 0; - + u32 max_size = 0; + while (r) { + u32 s = r->end - r->start; if (s >= max_size) { + max_size = s; prev_rmax = prev; rmax = r; + } + prev = r; r = r->next; + } - + if (rmax) { + if (prev_rmax) prev_rmax->next = rmax->next; else *ranges = rmax->next; + } - + return rmax; } u8 get_exec_checksum(u8* buf, u32 len, u32* cksum) { - if (unlikely(common_fuzz_stuff(its_argv, buf, len))) - return 1; - - *cksum = hash32(trace_bits, MAP_SIZE, HASH_CONST); + if (unlikely(common_fuzz_stuff(its_argv, buf, len))) return 1; + + *cksum = hash32(trace_bits, MAP_SIZE, HASH_CONST); return 0; } @@ -91,50 +99,51 @@ static void rand_replace(u8* buf, u32 len) { u8 colorization(u8* buf, u32 len, u32 exec_cksum) { struct range* ranges = add_range(NULL, 0, len); - u8* backup = ck_alloc_nozero(len); - + u8* backup = ck_alloc_nozero(len); + u64 orig_hit_cnt, new_hit_cnt; orig_hit_cnt = queued_paths + unique_crashes; - + stage_name = "colorization"; stage_short = "colorization"; stage_max = 1000; - + struct range* rng; stage_cur = stage_max; while ((rng = pop_biggest_range(&ranges)) != NULL && stage_cur) { - + u32 s = rng->end - rng->start; memcpy(backup, buf + rng->start, s); rand_replace(buf + rng->start, s); - + u32 cksum; - if (unlikely(get_exec_checksum(buf, len, &cksum))) - return 1; - + if (unlikely(get_exec_checksum(buf, len, &cksum))) return 1; + if (cksum != exec_cksum) { - - ranges = add_range(ranges, rng->start, rng->start + s/2); - ranges = add_range(ranges, rng->start + s/2 +1, rng->end); + + ranges = add_range(ranges, rng->start, rng->start + s / 2); + ranges = add_range(ranges, rng->start + s / 2 + 1, rng->end); memcpy(buf + rng->start, backup, s); - + } - + ck_free(rng); --stage_cur; - + } - + new_hit_cnt = queued_paths + unique_crashes; stage_finds[STAGE_COLORIZATION] += new_hit_cnt - orig_hit_cnt; stage_cycles[STAGE_COLORIZATION] += stage_max - stage_cur; - + while (ranges) { + rng = ranges; ranges = ranges->next; ck_free(rng); + } - + return 0; } @@ -147,21 +156,20 @@ u8 its_fuzz(u32 idx, u32 size, u8* buf, u32 len, u8* status) { orig_hit_cnt = queued_paths + unique_crashes; - if (unlikely(common_fuzz_stuff(its_argv, buf, len))) - return 1; + if (unlikely(common_fuzz_stuff(its_argv, buf, len))) return 1; new_hit_cnt = queued_paths + unique_crashes; if (unlikely(new_hit_cnt != orig_hit_cnt)) { - + *status = 1; - + } else { - + if (size >= MIN_AUTO_EXTRA && size <= MAX_AUTO_EXTRA) maybe_add_auto(&buf[idx], size); *status = 2; - + } return 0; @@ -169,8 +177,9 @@ u8 its_fuzz(u32 idx, u32 size, u8* buf, u32 len, u8* status) { } u8 cmp_extend_encoding(struct cmp_header* h, u64 pattern, u64 repl, u32 idx, - u8* orig_buf, u8* buf, u32 len, u8 do_reverse, u8* status) { - + u8* orig_buf, u8* buf, u32 len, u8 do_reverse, + u8* status) { + u64* buf_64 = (u64*)&buf[idx]; u32* buf_32 = (u32*)&buf[idx]; u16* buf_16 = (u16*)&buf[idx]; @@ -179,59 +188,75 @@ u8 cmp_extend_encoding(struct cmp_header* h, u64 pattern, u64 repl, u32 idx, u32* o_buf_32 = (u32*)&orig_buf[idx]; u16* o_buf_16 = (u16*)&orig_buf[idx]; // u8* o_buf_8 = &orig_buf[idx]; - + u32 its_len = len - idx; *status = 0; if (SHAPE_BYTES(h->shape) == 8) { + if (its_len >= 8 && *buf_64 == pattern && *o_buf_64 == pattern) { + *buf_64 = repl; - if (unlikely(its_fuzz(idx, 8, buf, len, status))) - return 1; + if (unlikely(its_fuzz(idx, 8, buf, len, status))) return 1; *buf_64 = pattern; + } + // reverse encoding if (do_reverse) if (unlikely(cmp_extend_encoding(h, SWAP64(pattern), SWAP64(repl), idx, - orig_buf, buf, len, 0, status))) - return 1; + orig_buf, buf, len, 0, status))) + return 1; + } - + if (SHAPE_BYTES(h->shape) == 4 || *status == 2) { + if (its_len >= 4 && *buf_32 == (u32)pattern && *o_buf_32 == (u32)pattern) { + *buf_32 = (u32)repl; - if (unlikely(its_fuzz(idx, 4, buf, len, status))) - return 1; + if (unlikely(its_fuzz(idx, 4, buf, len, status))) return 1; *buf_32 = pattern; + } + // reverse encoding if (do_reverse) if (unlikely(cmp_extend_encoding(h, SWAP32(pattern), SWAP32(repl), idx, - orig_buf, buf, len, 0, status))) - return 1; + orig_buf, buf, len, 0, status))) + return 1; + } if (SHAPE_BYTES(h->shape) == 2 || *status == 2) { + if (its_len >= 2 && *buf_16 == (u16)pattern && *o_buf_16 == (u16)pattern) { + *buf_16 = (u16)repl; - if (unlikely(its_fuzz(idx, 2, buf, len, status))) - return 1; + if (unlikely(its_fuzz(idx, 2, buf, len, status))) return 1; *buf_16 = (u16)pattern; + } + // reverse encoding if (do_reverse) if (unlikely(cmp_extend_encoding(h, SWAP16(pattern), SWAP16(repl), idx, - orig_buf, buf, len, 0, status))) - return 1; + orig_buf, buf, len, 0, status))) + return 1; + } - + /*if (SHAPE_BYTES(h->shape) == 1 || *status == 2) { + if (its_len >= 2 && *buf_8 == (u8)pattern && *o_buf_8 == (u8)pattern) { + *buf_8 = (u8)repl; if (unlikely(its_fuzz(idx, 1, buf, len, status))) return 1; *buf_16 = (u16)pattern; + } + }*/ return 0; @@ -241,44 +266,49 @@ u8 cmp_extend_encoding(struct cmp_header* h, u64 pattern, u64 repl, u32 idx, u8 cmp_fuzz(u32 key, u8* orig_buf, u8* buf, u32 len) { struct cmp_header* h = &cmp_map->headers[key]; - u32 i, j, idx; - + u32 i, j, idx; + u32 loggeds = h->hits; - if (h->hits > CMP_MAP_H) - loggeds = CMP_MAP_H; - + if (h->hits > CMP_MAP_H) loggeds = CMP_MAP_H; + u8 status; // opt not in the paper u32 fails = 0; - + for (i = 0; i < loggeds; ++i) { - + struct cmp_operands* o = &cmp_map->log[key][i]; - + // opt not in the paper for (j = 0; j < i; ++j) if (cmp_map->log[key][j].v0 == o->v0 && cmp_map->log[key][i].v1 == o->v1) goto cmp_fuzz_next_iter; - + for (idx = 0; idx < len && fails < 8; ++idx) { - - if (unlikely(cmp_extend_encoding(h, o->v0, o->v1, idx, orig_buf, buf, len, 1, &status))) + + if (unlikely(cmp_extend_encoding(h, o->v0, o->v1, idx, orig_buf, buf, len, + 1, &status))) return 1; - if (status == 2) ++fails; - else if (status == 1) break; - - if (unlikely(cmp_extend_encoding(h, o->v1, o->v0, idx, orig_buf, buf, len, 1, &status))) + if (status == 2) + ++fails; + else if (status == 1) + break; + + if (unlikely(cmp_extend_encoding(h, o->v1, o->v0, idx, orig_buf, buf, len, + 1, &status))) return 1; - if (status == 2) ++fails; - else if (status == 1) break; - + if (status == 2) + ++fails; + else if (status == 1) + break; + } -cmp_fuzz_next_iter: + cmp_fuzz_next_iter: stage_cur++; - + } - + return 0; } @@ -286,67 +316,60 @@ cmp_fuzz_next_iter: ///// Input to State stage // queue_cur->exec_cksum -u8 input_to_state_stage(char** argv, u8* orig_buf, u8* buf, u32 len, u32 exec_cksum) { +u8 input_to_state_stage(char** argv, u8* orig_buf, u8* buf, u32 len, + u32 exec_cksum) { its_argv = argv; - if (unlikely(colorization(buf, len, exec_cksum))) - return 1; - + if (unlikely(colorization(buf, len, exec_cksum))) return 1; + // do it manually, forkserver clear only trace_bits memset(cmp_map->headers, 0, sizeof(cmp_map->headers)); - - if (unlikely(common_fuzz_cmplog_stuff(argv, buf, len))) - return 1; - + + if (unlikely(common_fuzz_cmplog_stuff(argv, buf, len))) return 1; + u64 orig_hit_cnt, new_hit_cnt; u64 orig_execs = total_execs; orig_hit_cnt = queued_paths + unique_crashes; - + stage_name = "input-to-state"; stage_short = "its"; stage_max = 0; stage_cur = 0; - + u32 k; for (k = 0; k < CMP_MAP_W; ++k) { - if (!cmp_map->headers[k].hits) - continue; + if (!cmp_map->headers[k].hits) continue; if (cmp_map->headers[k].hits > CMP_MAP_H) stage_max += CMP_MAP_H; else stage_max += cmp_map->headers[k].hits; } - + for (k = 0; k < CMP_MAP_W; ++k) { - if (!cmp_map->headers[k].hits) - continue; + if (!cmp_map->headers[k].hits) continue; cmp_fuzz(k, orig_buf, buf, len); - + } - + memcpy(buf, orig_buf, len); - + new_hit_cnt = queued_paths + unique_crashes; stage_finds[STAGE_ITS] += new_hit_cnt - orig_hit_cnt; stage_cycles[STAGE_ITS] += total_execs - orig_execs; - + return 0; } - //// CmpLog forkserver -s32 cmplog_forksrv_pid, - cmplog_child_pid, - cmplog_fsrv_ctl_fd, - cmplog_fsrv_st_fd; +s32 cmplog_forksrv_pid, cmplog_child_pid, cmplog_fsrv_ctl_fd, cmplog_fsrv_st_fd; -void init_cmplog_forkserver(char **argv) { +void init_cmplog_forkserver(char** argv) { static struct itimerval it; int st_pipe[2], ctl_pipe[2]; @@ -475,7 +498,7 @@ void init_cmplog_forkserver(char **argv) { /* Use a distinctive bitmap signature to tell the parent about execv() falling through. */ - *(u32 *)trace_bits = EXEC_FAIL_SIG; + *(u32*)trace_bits = EXEC_FAIL_SIG; exit(0); } @@ -519,7 +542,9 @@ void init_cmplog_forkserver(char **argv) { } if (child_timed_out) - FATAL("Timeout while initializing cmplog fork server (adjusting -t may help)"); + FATAL( + "Timeout while initializing cmplog fork server (adjusting -t may " + "help)"); if (waitpid(cmplog_forksrv_pid, &status, 0) <= 0) PFATAL("waitpid() failed"); @@ -598,7 +623,7 @@ void init_cmplog_forkserver(char **argv) { } - if (*(u32 *)trace_bits == EXEC_FAIL_SIG) + if (*(u32*)trace_bits == EXEC_FAIL_SIG) FATAL("Unable to execute target application ('%s')", argv[0]); if (mem_limit && mem_limit < 500 && uses_asan) { @@ -757,7 +782,7 @@ u8 run_cmplog_target(char** argv, u32 timeout) { setenv("MSAN_OPTIONS", "exit_code=" STRINGIFY(MSAN_ERROR) ":" "symbolize=0:" "msan_track_origins=0", 0); - + setenv("__AFL_CMPLOG_MODE__", "1", 1); argv[0] = cmplog_binary; @@ -781,18 +806,21 @@ u8 run_cmplog_target(char** argv, u32 timeout) { if ((res = write(cmplog_fsrv_ctl_fd, &prev_timed_out, 4)) != 4) { if (stop_soon) return 0; - RPFATAL(res, "Unable to request new process from cmplog fork server (OOM?)"); + RPFATAL(res, + "Unable to request new process from cmplog fork server (OOM?)"); } if ((res = read(cmplog_fsrv_st_fd, &cmplog_child_pid, 4)) != 4) { if (stop_soon) return 0; - RPFATAL(res, "Unable to request new process from cmplog fork server (OOM?)"); + RPFATAL(res, + "Unable to request new process from cmplog fork server (OOM?)"); } - if (cmplog_child_pid <= 0) FATAL("Cmplog fork server is misbehaving (OOM?)"); + if (cmplog_child_pid <= 0) + FATAL("Cmplog fork server is misbehaving (OOM?)"); } @@ -804,7 +832,8 @@ u8 run_cmplog_target(char** argv, u32 timeout) { setitimer(ITIMER_REAL, &it, NULL); - /* The SIGALRM handler simply kills the cmplog_child_pid and sets child_timed_out. */ + /* The SIGALRM handler simply kills the cmplog_child_pid and sets + * child_timed_out. */ if (dumb_mode == 1 || no_forkserver) { @@ -900,7 +929,7 @@ u8 run_cmplog_target(char** argv, u32 timeout) { u8 common_fuzz_cmplog_stuff(char** argv, u8* out_buf, u32 len) { u8 fault; - + if (post_handler) { out_buf = post_handler(out_buf, &len); @@ -948,3 +977,4 @@ u8 common_fuzz_cmplog_stuff(char** argv, u8* out_buf, u32 len) { return 0; } + |