OLD | NEW |
(Empty) | |
| 1 /* |
| 2 american fuzzy lop - test case minimizer |
| 3 ---------------------------------------- |
| 4 |
| 5 Written and maintained by Michal Zalewski <lcamtuf@google.com> |
| 6 |
| 7 Copyright 2015, 2016 Google Inc. All rights reserved. |
| 8 |
| 9 Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 you may not use this file except in compliance with the License. |
| 11 You may obtain a copy of the License at: |
| 12 |
| 13 http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
| 15 A simple test case minimizer that takes an input file and tries to remove |
| 16 as much data as possible while keeping the binary in a crashing state |
| 17 *or* producing consistent instrumentation output (the mode is auto-selected |
| 18 based on the initially observed behavior). |
| 19 |
| 20 */ |
| 21 |
| 22 #define AFL_MAIN |
| 23 |
| 24 #include "config.h" |
| 25 #include "types.h" |
| 26 #include "debug.h" |
| 27 #include "alloc-inl.h" |
| 28 #include "hash.h" |
| 29 |
| 30 #include <stdio.h> |
| 31 #include <unistd.h> |
| 32 #include <stdlib.h> |
| 33 #include <string.h> |
| 34 #include <time.h> |
| 35 #include <errno.h> |
| 36 #include <signal.h> |
| 37 #include <dirent.h> |
| 38 #include <fcntl.h> |
| 39 |
| 40 #include <sys/wait.h> |
| 41 #include <sys/time.h> |
| 42 #include <sys/shm.h> |
| 43 #include <sys/stat.h> |
| 44 #include <sys/types.h> |
| 45 #include <sys/resource.h> |
| 46 |
| 47 static s32 child_pid; /* PID of the tested program */ |
| 48 |
| 49 static u8* trace_bits; /* SHM with instrumentation bitmap */ |
| 50 |
| 51 static u8 *in_file, /* Minimizer input test case */ |
| 52 *out_file, /* Minimizer output file */ |
| 53 *prog_in, /* Targeted program input file */ |
| 54 *target_path, /* Path to target binary */ |
| 55 *doc_path; /* Path to docs */ |
| 56 |
| 57 static u8* in_data; /* Input data for trimming */ |
| 58 |
| 59 static u32 in_len, /* Input data length */ |
| 60 orig_cksum, /* Original checksum */ |
| 61 total_execs, /* Total number of execs */ |
| 62 missed_hangs, /* Misses due to hangs */ |
| 63 missed_crashes, /* Misses due to crashes */ |
| 64 missed_paths, /* Misses due to exec path diffs */ |
| 65 exec_tmout = EXEC_TIMEOUT; /* Exec timeout (ms) */ |
| 66 |
| 67 static u64 mem_limit = MEM_LIMIT; /* Memory limit (MB) */ |
| 68 |
| 69 static s32 shm_id, /* ID of the SHM region */ |
| 70 dev_null_fd = -1; /* FD to /dev/null */ |
| 71 |
| 72 static u8 crash_mode, /* Crash-centric mode? */ |
| 73 exit_crash, /* Treat non-zero exit as crash? */ |
| 74 edges_only, /* Ignore hit counts? */ |
| 75 use_stdin = 1; /* Use stdin for program input? */ |
| 76 |
| 77 static volatile u8 |
| 78 stop_soon, /* Ctrl-C pressed? */ |
| 79 child_timed_out; /* Child timed out? */ |
| 80 |
| 81 |
| 82 /* Classify tuple counts. This is a slow & naive version, but good enough here.
*/ |
| 83 |
| 84 #define AREP4(_sym) (_sym), (_sym), (_sym), (_sym) |
| 85 #define AREP8(_sym) AREP4(_sym), AREP4(_sym) |
| 86 #define AREP16(_sym) AREP8(_sym), AREP8(_sym) |
| 87 #define AREP32(_sym) AREP16(_sym), AREP16(_sym) |
| 88 #define AREP64(_sym) AREP32(_sym), AREP32(_sym) |
| 89 #define AREP128(_sym) AREP64(_sym), AREP64(_sym) |
| 90 |
| 91 static u8 count_class_lookup[256] = { |
| 92 |
| 93 /* 0 - 3: 4 */ 0, 1, 2, 4, |
| 94 /* 4 - 7: +4 */ AREP4(8), |
| 95 /* 8 - 15: +8 */ AREP8(16), |
| 96 /* 16 - 31: +16 */ AREP16(32), |
| 97 /* 32 - 127: +96 */ AREP64(64), AREP32(64), |
| 98 /* 128+: +128 */ AREP128(128) |
| 99 |
| 100 }; |
| 101 |
| 102 static void classify_counts(u8* mem) { |
| 103 |
| 104 u32 i = MAP_SIZE; |
| 105 |
| 106 if (edges_only) { |
| 107 |
| 108 while (i--) { |
| 109 if (*mem) *mem = 1; |
| 110 mem++; |
| 111 } |
| 112 |
| 113 } else { |
| 114 |
| 115 while (i--) { |
| 116 *mem = count_class_lookup[*mem]; |
| 117 mem++; |
| 118 } |
| 119 |
| 120 } |
| 121 |
| 122 } |
| 123 |
| 124 |
| 125 /* See if any bytes are set in the bitmap. */ |
| 126 |
| 127 static inline u8 anything_set(void) { |
| 128 |
| 129 u32* ptr = (u32*)trace_bits; |
| 130 u32 i = (MAP_SIZE >> 2); |
| 131 |
| 132 while (i--) if (*(ptr++)) return 1; |
| 133 |
| 134 return 0; |
| 135 |
| 136 } |
| 137 |
| 138 |
| 139 |
| 140 /* Get rid of shared memory and temp files (atexit handler). */ |
| 141 |
| 142 static void remove_shm(void) { |
| 143 |
| 144 unlink(prog_in); /* Ignore errors */ |
| 145 shmctl(shm_id, IPC_RMID, NULL); |
| 146 |
| 147 } |
| 148 |
| 149 |
| 150 /* Configure shared memory. */ |
| 151 |
| 152 static void setup_shm(void) { |
| 153 |
| 154 u8* shm_str; |
| 155 |
| 156 shm_id = shmget(IPC_PRIVATE, MAP_SIZE, IPC_CREAT | IPC_EXCL | 0600); |
| 157 |
| 158 if (shm_id < 0) PFATAL("shmget() failed"); |
| 159 |
| 160 atexit(remove_shm); |
| 161 |
| 162 shm_str = alloc_printf("%d", shm_id); |
| 163 |
| 164 setenv(SHM_ENV_VAR, shm_str, 1); |
| 165 |
| 166 ck_free(shm_str); |
| 167 |
| 168 trace_bits = shmat(shm_id, NULL, 0); |
| 169 |
| 170 if (!trace_bits) PFATAL("shmat() failed"); |
| 171 |
| 172 } |
| 173 |
| 174 |
| 175 /* Read initial file. */ |
| 176 |
| 177 static void read_initial_file(void) { |
| 178 |
| 179 struct stat st; |
| 180 s32 fd = open(in_file, O_RDONLY); |
| 181 |
| 182 if (fd < 0) PFATAL("Unable to open '%s'", in_file); |
| 183 |
| 184 if (fstat(fd, &st) || !st.st_size) |
| 185 FATAL("Zero-sized input file."); |
| 186 |
| 187 if (st.st_size >= TMIN_MAX_FILE) |
| 188 FATAL("Input file is too large (%u MB max)", TMIN_MAX_FILE / 1024 / 1024); |
| 189 |
| 190 in_len = st.st_size; |
| 191 in_data = ck_alloc_nozero(in_len); |
| 192 |
| 193 ck_read(fd, in_data, in_len, in_file); |
| 194 |
| 195 close(fd); |
| 196 |
| 197 OKF("Read %u byte%s from '%s'.", in_len, in_len == 1 ? "" : "s", in_file); |
| 198 |
| 199 } |
| 200 |
| 201 |
| 202 /* Write output file. */ |
| 203 |
| 204 static s32 write_to_file(u8* path, u8* mem, u32 len) { |
| 205 |
| 206 s32 ret; |
| 207 |
| 208 unlink(path); /* Ignore errors */ |
| 209 |
| 210 ret = open(path, O_RDWR | O_CREAT | O_EXCL, 0600); |
| 211 |
| 212 if (ret < 0) PFATAL("Unable to create '%s'", path); |
| 213 |
| 214 ck_write(ret, mem, len, path); |
| 215 |
| 216 lseek(ret, 0, SEEK_SET); |
| 217 |
| 218 return ret; |
| 219 |
| 220 } |
| 221 |
| 222 |
| 223 /* Handle timeout signal. */ |
| 224 |
| 225 static void handle_timeout(int sig) { |
| 226 |
| 227 child_timed_out = 1; |
| 228 if (child_pid > 0) kill(child_pid, SIGKILL); |
| 229 |
| 230 } |
| 231 |
| 232 |
| 233 /* Execute target application. Returns 0 if the changes are a dud, or |
| 234 1 if they should be kept. */ |
| 235 |
| 236 static u8 run_target(char** argv, u8* mem, u32 len, u8 first_run) { |
| 237 |
| 238 static struct itimerval it; |
| 239 int status = 0; |
| 240 |
| 241 s32 prog_in_fd; |
| 242 u32 cksum; |
| 243 |
| 244 memset(trace_bits, 0, MAP_SIZE); |
| 245 MEM_BARRIER(); |
| 246 |
| 247 prog_in_fd = write_to_file(prog_in, mem, len); |
| 248 |
| 249 child_pid = fork(); |
| 250 |
| 251 if (child_pid < 0) PFATAL("fork() failed"); |
| 252 |
| 253 if (!child_pid) { |
| 254 |
| 255 struct rlimit r; |
| 256 |
| 257 if (dup2(use_stdin ? prog_in_fd : dev_null_fd, 0) < 0 || |
| 258 dup2(dev_null_fd, 1) < 0 || |
| 259 dup2(dev_null_fd, 2) < 0) { |
| 260 |
| 261 *(u32*)trace_bits = EXEC_FAIL_SIG; |
| 262 PFATAL("dup2() failed"); |
| 263 |
| 264 } |
| 265 |
| 266 close(dev_null_fd); |
| 267 close(prog_in_fd); |
| 268 |
| 269 if (mem_limit) { |
| 270 |
| 271 r.rlim_max = r.rlim_cur = ((rlim_t)mem_limit) << 20; |
| 272 |
| 273 #ifdef RLIMIT_AS |
| 274 |
| 275 setrlimit(RLIMIT_AS, &r); /* Ignore errors */ |
| 276 |
| 277 #else |
| 278 |
| 279 setrlimit(RLIMIT_DATA, &r); /* Ignore errors */ |
| 280 |
| 281 #endif /* ^RLIMIT_AS */ |
| 282 |
| 283 } |
| 284 |
| 285 r.rlim_max = r.rlim_cur = 0; |
| 286 setrlimit(RLIMIT_CORE, &r); /* Ignore errors */ |
| 287 |
| 288 execv(target_path, argv); |
| 289 |
| 290 *(u32*)trace_bits = EXEC_FAIL_SIG; |
| 291 exit(0); |
| 292 |
| 293 } |
| 294 |
| 295 close(prog_in_fd); |
| 296 |
| 297 /* Configure timeout, wait for child, cancel timeout. */ |
| 298 |
| 299 child_timed_out = 0; |
| 300 it.it_value.tv_sec = (exec_tmout / 1000); |
| 301 it.it_value.tv_usec = (exec_tmout % 1000) * 1000; |
| 302 |
| 303 setitimer(ITIMER_REAL, &it, NULL); |
| 304 |
| 305 if (waitpid(child_pid, &status, 0) <= 0) FATAL("waitpid() failed"); |
| 306 |
| 307 child_pid = 0; |
| 308 it.it_value.tv_sec = 0; |
| 309 it.it_value.tv_usec = 0; |
| 310 |
| 311 setitimer(ITIMER_REAL, &it, NULL); |
| 312 |
| 313 MEM_BARRIER(); |
| 314 |
| 315 /* Clean up bitmap, analyze exit condition, etc. */ |
| 316 |
| 317 if (*(u32*)trace_bits == EXEC_FAIL_SIG) |
| 318 FATAL("Unable to execute '%s'", argv[0]); |
| 319 |
| 320 classify_counts(trace_bits); |
| 321 total_execs++; |
| 322 |
| 323 if (stop_soon) { |
| 324 SAYF(cRST cLRD "\n+++ Minimization aborted by user +++\n" cRST); |
| 325 exit(1); |
| 326 } |
| 327 |
| 328 /* Always discard inputs that time out. */ |
| 329 |
| 330 if (child_timed_out) { |
| 331 |
| 332 missed_hangs++; |
| 333 return 0; |
| 334 |
| 335 } |
| 336 |
| 337 /* Handle crashing inputs depending on current mode. */ |
| 338 |
| 339 if (WIFSIGNALED(status) || |
| 340 (WIFEXITED(status) && WEXITSTATUS(status) == MSAN_ERROR) || |
| 341 (WIFEXITED(status) && WEXITSTATUS(status) && exit_crash)) { |
| 342 |
| 343 if (first_run) crash_mode = 1; |
| 344 |
| 345 if (crash_mode) { |
| 346 |
| 347 return 1; |
| 348 |
| 349 } else { |
| 350 |
| 351 missed_crashes++; |
| 352 return 0; |
| 353 |
| 354 } |
| 355 |
| 356 } |
| 357 |
| 358 /* Handle non-crashing inputs appropriately. */ |
| 359 |
| 360 if (crash_mode) { |
| 361 |
| 362 missed_paths++; |
| 363 return 0; |
| 364 |
| 365 } |
| 366 |
| 367 cksum = hash32(trace_bits, MAP_SIZE, HASH_CONST); |
| 368 |
| 369 if (first_run) orig_cksum = cksum; |
| 370 |
| 371 if (orig_cksum == cksum) return 1; |
| 372 |
| 373 missed_paths++; |
| 374 return 0; |
| 375 |
| 376 } |
| 377 |
| 378 |
| 379 /* Find first power of two greater or equal to val. */ |
| 380 |
| 381 static u32 next_p2(u32 val) { |
| 382 |
| 383 u32 ret = 1; |
| 384 while (val > ret) ret <<= 1; |
| 385 return ret; |
| 386 |
| 387 } |
| 388 |
| 389 |
| 390 /* Actually minimize! */ |
| 391 |
| 392 static void minimize(char** argv) { |
| 393 |
| 394 static u32 alpha_map[256]; |
| 395 |
| 396 u8* tmp_buf = ck_alloc_nozero(in_len); |
| 397 u32 orig_len = in_len, stage_o_len; |
| 398 |
| 399 u32 del_len, set_len, del_pos, set_pos, i, alpha_size, cur_pass = 0; |
| 400 u32 syms_removed, alpha_del0 = 0, alpha_del1, alpha_del2, alpha_d_total = 0; |
| 401 u8 changed_any, prev_del; |
| 402 |
| 403 /*********************** |
| 404 * BLOCK NORMALIZATION * |
| 405 ***********************/ |
| 406 |
| 407 set_len = next_p2(in_len / TMIN_SET_STEPS); |
| 408 set_pos = 0; |
| 409 |
| 410 if (set_len < TMIN_SET_MIN_SIZE) set_len = TMIN_SET_MIN_SIZE; |
| 411 |
| 412 ACTF(cBRI "Stage #0: " cRST "One-time block normalization..."); |
| 413 |
| 414 while (set_pos < in_len) { |
| 415 |
| 416 u8 res; |
| 417 u32 use_len = MIN(set_len, in_len - set_pos); |
| 418 |
| 419 for (i = 0; i < use_len; i++) |
| 420 if (in_data[set_pos + i] != '0') break; |
| 421 |
| 422 if (i != use_len) { |
| 423 |
| 424 memcpy(tmp_buf, in_data, in_len); |
| 425 memset(tmp_buf + set_pos, '0', use_len); |
| 426 |
| 427 res = run_target(argv, tmp_buf, in_len, 0); |
| 428 |
| 429 if (res) { |
| 430 |
| 431 memset(in_data + set_pos, '0', use_len); |
| 432 changed_any = 1; |
| 433 alpha_del0 += use_len; |
| 434 |
| 435 } |
| 436 |
| 437 } |
| 438 |
| 439 set_pos += set_len; |
| 440 |
| 441 } |
| 442 |
| 443 alpha_d_total += alpha_del0; |
| 444 |
| 445 OKF("Block normalization complete, %u byte%s replaced.", alpha_del0, |
| 446 alpha_del0 == 1 ? "" : "s"); |
| 447 |
| 448 next_pass: |
| 449 |
| 450 ACTF(cYEL "--- " cBRI "Pass #%u " cYEL "---", ++cur_pass); |
| 451 changed_any = 0; |
| 452 |
| 453 /****************** |
| 454 * BLOCK DELETION * |
| 455 ******************/ |
| 456 |
| 457 del_len = next_p2(in_len / TRIM_START_STEPS); |
| 458 stage_o_len = in_len; |
| 459 |
| 460 ACTF(cBRI "Stage #1: " cRST "Removing blocks of data..."); |
| 461 |
| 462 next_del_blksize: |
| 463 |
| 464 if (!del_len) del_len = 1; |
| 465 del_pos = 0; |
| 466 prev_del = 1; |
| 467 |
| 468 SAYF(cGRA " Block length = %u, remaining size = %u\n" cRST, |
| 469 del_len, in_len); |
| 470 |
| 471 while (del_pos < in_len) { |
| 472 |
| 473 u8 res; |
| 474 s32 tail_len; |
| 475 |
| 476 tail_len = in_len - del_pos - del_len; |
| 477 if (tail_len < 0) tail_len = 0; |
| 478 |
| 479 /* If we have processed at least one full block (initially, prev_del == 1), |
| 480 and we did so without deleting the previous one, and we aren't at the |
| 481 very end of the buffer (tail_len > 0), and the current block is the same |
| 482 as the previous one... skip this step as a no-op. */ |
| 483 |
| 484 if (!prev_del && tail_len && !memcmp(in_data + del_pos - del_len, |
| 485 in_data + del_pos, del_len)) { |
| 486 |
| 487 del_pos += del_len; |
| 488 continue; |
| 489 |
| 490 } |
| 491 |
| 492 prev_del = 0; |
| 493 |
| 494 /* Head */ |
| 495 memcpy(tmp_buf, in_data, del_pos); |
| 496 |
| 497 /* Tail */ |
| 498 memcpy(tmp_buf + del_pos, in_data + del_pos + del_len, tail_len); |
| 499 |
| 500 res = run_target(argv, tmp_buf, del_pos + tail_len, 0); |
| 501 |
| 502 if (res) { |
| 503 |
| 504 memcpy(in_data, tmp_buf, del_pos + tail_len); |
| 505 prev_del = 1; |
| 506 in_len = del_pos + tail_len; |
| 507 |
| 508 changed_any = 1; |
| 509 |
| 510 } else del_pos += del_len; |
| 511 |
| 512 } |
| 513 |
| 514 if (del_len > 1 && in_len >= 1) { |
| 515 |
| 516 del_len /= 2; |
| 517 goto next_del_blksize; |
| 518 |
| 519 } |
| 520 |
| 521 OKF("Block removal complete, %u bytes deleted.", stage_o_len - in_len); |
| 522 |
| 523 if (!in_len && changed_any) |
| 524 WARNF(cLRD "Down to zero bytes - check the command line and mem limit!" cRST
); |
| 525 |
| 526 if (cur_pass > 1 && !changed_any) goto finalize_all; |
| 527 |
| 528 /************************* |
| 529 * ALPHABET MINIMIZATION * |
| 530 *************************/ |
| 531 |
| 532 alpha_size = 0; |
| 533 alpha_del1 = 0; |
| 534 syms_removed = 0; |
| 535 |
| 536 memset(alpha_map, 0, 256 * sizeof(u32)); |
| 537 |
| 538 for (i = 0; i < in_len; i++) { |
| 539 if (!alpha_map[in_data[i]]) alpha_size++; |
| 540 alpha_map[in_data[i]]++; |
| 541 } |
| 542 |
| 543 ACTF(cBRI "Stage #2: " cRST "Minimizing symbols (%u code point%s)...", |
| 544 alpha_size, alpha_size == 1 ? "" : "s"); |
| 545 |
| 546 for (i = 0; i < 256; i++) { |
| 547 |
| 548 u32 r; |
| 549 u8 res; |
| 550 |
| 551 if (i == '0' || !alpha_map[i]) continue; |
| 552 |
| 553 memcpy(tmp_buf, in_data, in_len); |
| 554 |
| 555 for (r = 0; r < in_len; r++) |
| 556 if (tmp_buf[r] == i) tmp_buf[r] = '0'; |
| 557 |
| 558 res = run_target(argv, tmp_buf, in_len, 0); |
| 559 |
| 560 if (res) { |
| 561 |
| 562 memcpy(in_data, tmp_buf, in_len); |
| 563 syms_removed++; |
| 564 alpha_del1 += alpha_map[i]; |
| 565 changed_any = 1; |
| 566 |
| 567 } |
| 568 |
| 569 } |
| 570 |
| 571 alpha_d_total += alpha_del1; |
| 572 |
| 573 OKF("Symbol minimization finished, %u symbol%s (%u byte%s) replaced.", |
| 574 syms_removed, syms_removed == 1 ? "" : "s", |
| 575 alpha_del1, alpha_del1 == 1 ? "" : "s"); |
| 576 |
| 577 /************************** |
| 578 * CHARACTER MINIMIZATION * |
| 579 **************************/ |
| 580 |
| 581 alpha_del2 = 0; |
| 582 |
| 583 ACTF(cBRI "Stage #3: " cRST "Character minimization..."); |
| 584 |
| 585 memcpy(tmp_buf, in_data, in_len); |
| 586 |
| 587 for (i = 0; i < in_len; i++) { |
| 588 |
| 589 u8 res, orig = tmp_buf[i]; |
| 590 |
| 591 if (orig == '0') continue; |
| 592 tmp_buf[i] = '0'; |
| 593 |
| 594 res = run_target(argv, tmp_buf, in_len, 0); |
| 595 |
| 596 if (res) { |
| 597 |
| 598 in_data[i] = '0'; |
| 599 alpha_del2++; |
| 600 changed_any = 1; |
| 601 |
| 602 } else tmp_buf[i] = orig; |
| 603 |
| 604 } |
| 605 |
| 606 alpha_d_total += alpha_del2; |
| 607 |
| 608 OKF("Character minimization done, %u byte%s replaced.", |
| 609 alpha_del2, alpha_del2 == 1 ? "" : "s"); |
| 610 |
| 611 if (changed_any) goto next_pass; |
| 612 |
| 613 finalize_all: |
| 614 |
| 615 SAYF("\n" |
| 616 cGRA " File size reduced by : " cRST "%0.02f%% (to %u byte%s)\n" |
| 617 cGRA " Characters simplified : " cRST "%0.02f%%\n" |
| 618 cGRA " Number of execs done : " cRST "%u\n" |
| 619 cGRA " Fruitless execs : " cRST "path=%u crash=%u hang=%s%u\n\n"
, |
| 620 100 - ((double)in_len) * 100 / orig_len, in_len, in_len == 1 ? "" : "s", |
| 621 ((double)(alpha_d_total)) * 100 / (in_len ? in_len : 1), |
| 622 total_execs, missed_paths, missed_crashes, missed_hangs ? cLRD : "", |
| 623 missed_hangs); |
| 624 |
| 625 if (total_execs > 50 && missed_hangs * 10 > total_execs) |
| 626 WARNF(cLRD "Frequent timeouts - results may be skewed." cRST); |
| 627 |
| 628 } |
| 629 |
| 630 |
| 631 |
| 632 /* Handle Ctrl-C and the like. */ |
| 633 |
| 634 static void handle_stop_sig(int sig) { |
| 635 |
| 636 stop_soon = 1; |
| 637 |
| 638 if (child_pid > 0) kill(child_pid, SIGKILL); |
| 639 |
| 640 } |
| 641 |
| 642 |
| 643 /* Do basic preparations - persistent fds, filenames, etc. */ |
| 644 |
| 645 static void set_up_environment(void) { |
| 646 |
| 647 u8* x; |
| 648 |
| 649 dev_null_fd = open("/dev/null", O_RDWR); |
| 650 if (dev_null_fd < 0) PFATAL("Unable to open /dev/null"); |
| 651 |
| 652 if (!prog_in) { |
| 653 |
| 654 u8* use_dir = "."; |
| 655 |
| 656 if (!access(use_dir, R_OK | W_OK | X_OK)) { |
| 657 |
| 658 use_dir = getenv("TMPDIR"); |
| 659 if (!use_dir) use_dir = "/tmp"; |
| 660 |
| 661 prog_in = alloc_printf("%s/.afl-tmin-temp-%u", use_dir, getpid()); |
| 662 |
| 663 } |
| 664 |
| 665 } |
| 666 |
| 667 /* Set sane defaults... */ |
| 668 |
| 669 x = getenv("ASAN_OPTIONS"); |
| 670 |
| 671 if (x) { |
| 672 |
| 673 if (!strstr(x, "abort_on_error=1")) |
| 674 FATAL("Custom ASAN_OPTIONS set without abort_on_error=1 - please fix!"); |
| 675 |
| 676 if (!strstr(x, "symbolize=0")) |
| 677 FATAL("Custom ASAN_OPTIONS set without symbolize=0 - please fix!"); |
| 678 |
| 679 } |
| 680 |
| 681 x = getenv("MSAN_OPTIONS"); |
| 682 |
| 683 if (x) { |
| 684 |
| 685 if (!strstr(x, "exit_code=" STRINGIFY(MSAN_ERROR))) |
| 686 FATAL("Custom MSAN_OPTIONS set without exit_code=" |
| 687 STRINGIFY(MSAN_ERROR) " - please fix!"); |
| 688 |
| 689 if (!strstr(x, "symbolize=0")) |
| 690 FATAL("Custom MSAN_OPTIONS set without symbolize=0 - please fix!"); |
| 691 |
| 692 } |
| 693 |
| 694 setenv("ASAN_OPTIONS", "abort_on_error=1:" |
| 695 "detect_leaks=0:" |
| 696 "symbolize=0:" |
| 697 "allocator_may_return_null=1", 0); |
| 698 |
| 699 setenv("MSAN_OPTIONS", "exit_code=" STRINGIFY(MSAN_ERROR) ":" |
| 700 "symbolize=0:" |
| 701 "abort_on_error=1:" |
| 702 "allocator_may_return_null=1:" |
| 703 "msan_track_origins=0", 0); |
| 704 |
| 705 if (getenv("AFL_LD_PRELOAD")) |
| 706 setenv("LD_PRELOAD", getenv("AFL_LD_PRELOAD"), 1); |
| 707 |
| 708 } |
| 709 |
| 710 |
| 711 /* Setup signal handlers, duh. */ |
| 712 |
| 713 static void setup_signal_handlers(void) { |
| 714 |
| 715 struct sigaction sa; |
| 716 |
| 717 sa.sa_handler = NULL; |
| 718 sa.sa_flags = SA_RESTART; |
| 719 sa.sa_sigaction = NULL; |
| 720 |
| 721 sigemptyset(&sa.sa_mask); |
| 722 |
| 723 /* Various ways of saying "stop". */ |
| 724 |
| 725 sa.sa_handler = handle_stop_sig; |
| 726 sigaction(SIGHUP, &sa, NULL); |
| 727 sigaction(SIGINT, &sa, NULL); |
| 728 sigaction(SIGTERM, &sa, NULL); |
| 729 |
| 730 /* Exec timeout notifications. */ |
| 731 |
| 732 sa.sa_handler = handle_timeout; |
| 733 sigaction(SIGALRM, &sa, NULL); |
| 734 |
| 735 } |
| 736 |
| 737 |
| 738 /* Detect @@ in args. */ |
| 739 |
| 740 static void detect_file_args(char** argv) { |
| 741 |
| 742 u32 i = 0; |
| 743 u8* cwd = getcwd(NULL, 0); |
| 744 |
| 745 if (!cwd) PFATAL("getcwd() failed"); |
| 746 |
| 747 while (argv[i]) { |
| 748 |
| 749 u8* aa_loc = strstr(argv[i], "@@"); |
| 750 |
| 751 if (aa_loc) { |
| 752 |
| 753 u8 *aa_subst, *n_arg; |
| 754 |
| 755 /* Be sure that we're always using fully-qualified paths. */ |
| 756 |
| 757 if (prog_in[0] == '/') aa_subst = prog_in; |
| 758 else aa_subst = alloc_printf("%s/%s", cwd, prog_in); |
| 759 |
| 760 /* Construct a replacement argv value. */ |
| 761 |
| 762 *aa_loc = 0; |
| 763 n_arg = alloc_printf("%s%s%s", argv[i], aa_subst, aa_loc + 2); |
| 764 argv[i] = n_arg; |
| 765 *aa_loc = '@'; |
| 766 |
| 767 if (prog_in[0] != '/') ck_free(aa_subst); |
| 768 |
| 769 } |
| 770 |
| 771 i++; |
| 772 |
| 773 } |
| 774 |
| 775 free(cwd); /* not tracked */ |
| 776 |
| 777 } |
| 778 |
| 779 |
| 780 /* Display usage hints. */ |
| 781 |
| 782 static void usage(u8* argv0) { |
| 783 |
| 784 SAYF("\n%s [ options ] -- /path/to/target_app [ ... ]\n\n" |
| 785 |
| 786 "Required parameters:\n\n" |
| 787 |
| 788 " -i file - input test case to be shrunk by the tool\n" |
| 789 " -o file - final output location for the minimized data\n\n" |
| 790 |
| 791 "Execution control settings:\n\n" |
| 792 |
| 793 " -f file - input file read by the tested program (stdin)\n" |
| 794 " -t msec - timeout for each run (%u ms)\n" |
| 795 " -m megs - memory limit for child process (%u MB)\n" |
| 796 " -Q - use binary-only instrumentation (QEMU mode)\n\n" |
| 797 |
| 798 "Minimization settings:\n\n" |
| 799 |
| 800 " -e - solve for edge coverage only, ignore hit counts\n" |
| 801 " -x - treat non-zero exit codes as crashes\n\n" |
| 802 |
| 803 "For additional tips, please consult %s/README.\n\n", |
| 804 |
| 805 argv0, EXEC_TIMEOUT, MEM_LIMIT, doc_path); |
| 806 |
| 807 exit(1); |
| 808 |
| 809 } |
| 810 |
| 811 |
| 812 /* Find binary. */ |
| 813 |
| 814 static void find_binary(u8* fname) { |
| 815 |
| 816 u8* env_path = 0; |
| 817 struct stat st; |
| 818 |
| 819 if (strchr(fname, '/') || !(env_path = getenv("PATH"))) { |
| 820 |
| 821 target_path = ck_strdup(fname); |
| 822 |
| 823 if (stat(target_path, &st) || !S_ISREG(st.st_mode) || |
| 824 !(st.st_mode & 0111) || st.st_size < 4) |
| 825 FATAL("Program '%s' not found or not executable", fname); |
| 826 |
| 827 } else { |
| 828 |
| 829 while (env_path) { |
| 830 |
| 831 u8 *cur_elem, *delim = strchr(env_path, ':'); |
| 832 |
| 833 if (delim) { |
| 834 |
| 835 cur_elem = ck_alloc(delim - env_path + 1); |
| 836 memcpy(cur_elem, env_path, delim - env_path); |
| 837 delim++; |
| 838 |
| 839 } else cur_elem = ck_strdup(env_path); |
| 840 |
| 841 env_path = delim; |
| 842 |
| 843 if (cur_elem[0]) |
| 844 target_path = alloc_printf("%s/%s", cur_elem, fname); |
| 845 else |
| 846 target_path = ck_strdup(fname); |
| 847 |
| 848 ck_free(cur_elem); |
| 849 |
| 850 if (!stat(target_path, &st) && S_ISREG(st.st_mode) && |
| 851 (st.st_mode & 0111) && st.st_size >= 4) break; |
| 852 |
| 853 ck_free(target_path); |
| 854 target_path = 0; |
| 855 |
| 856 } |
| 857 |
| 858 if (!target_path) FATAL("Program '%s' not found or not executable", fname); |
| 859 |
| 860 } |
| 861 |
| 862 } |
| 863 |
| 864 |
| 865 /* Fix up argv for QEMU. */ |
| 866 |
| 867 static char** get_qemu_argv(u8* own_loc, char** argv, int argc) { |
| 868 |
| 869 char** new_argv = ck_alloc(sizeof(char*) * (argc + 4)); |
| 870 u8 *tmp, *cp, *rsl, *own_copy; |
| 871 |
| 872 memcpy(new_argv + 3, argv + 1, sizeof(char*) * argc); |
| 873 |
| 874 /* Now we need to actually find qemu for argv[0]. */ |
| 875 |
| 876 new_argv[2] = target_path; |
| 877 new_argv[1] = "--"; |
| 878 |
| 879 tmp = getenv("AFL_PATH"); |
| 880 |
| 881 if (tmp) { |
| 882 |
| 883 cp = alloc_printf("%s/afl-qemu-trace", tmp); |
| 884 |
| 885 if (access(cp, X_OK)) |
| 886 FATAL("Unable to find '%s'", tmp); |
| 887 |
| 888 target_path = new_argv[0] = cp; |
| 889 return new_argv; |
| 890 |
| 891 } |
| 892 |
| 893 own_copy = ck_strdup(own_loc); |
| 894 rsl = strrchr(own_copy, '/'); |
| 895 |
| 896 if (rsl) { |
| 897 |
| 898 *rsl = 0; |
| 899 |
| 900 cp = alloc_printf("%s/afl-qemu-trace", own_copy); |
| 901 ck_free(own_copy); |
| 902 |
| 903 if (!access(cp, X_OK)) { |
| 904 |
| 905 target_path = new_argv[0] = cp; |
| 906 return new_argv; |
| 907 |
| 908 } |
| 909 |
| 910 } else ck_free(own_copy); |
| 911 |
| 912 if (!access(BIN_PATH "/afl-qemu-trace", X_OK)) { |
| 913 |
| 914 target_path = new_argv[0] = BIN_PATH "/afl-qemu-trace"; |
| 915 return new_argv; |
| 916 |
| 917 } |
| 918 |
| 919 FATAL("Unable to find 'afl-qemu-trace'."); |
| 920 |
| 921 } |
| 922 |
| 923 |
| 924 /* Main entry point */ |
| 925 |
| 926 int main(int argc, char** argv) { |
| 927 |
| 928 s32 opt; |
| 929 u8 mem_limit_given = 0, timeout_given = 0, qemu_mode = 0; |
| 930 char** use_argv; |
| 931 |
| 932 doc_path = access(DOC_PATH, F_OK) ? "docs" : DOC_PATH; |
| 933 |
| 934 SAYF(cCYA "afl-tmin " cBRI VERSION cRST " by <lcamtuf@google.com>\n"); |
| 935 |
| 936 while ((opt = getopt(argc,argv,"+i:o:f:m:t:xeQ")) > 0) |
| 937 |
| 938 switch (opt) { |
| 939 |
| 940 case 'i': |
| 941 |
| 942 if (in_file) FATAL("Multiple -i options not supported"); |
| 943 in_file = optarg; |
| 944 break; |
| 945 |
| 946 case 'o': |
| 947 |
| 948 if (out_file) FATAL("Multiple -o options not supported"); |
| 949 out_file = optarg; |
| 950 break; |
| 951 |
| 952 case 'f': |
| 953 |
| 954 if (prog_in) FATAL("Multiple -f options not supported"); |
| 955 use_stdin = 0; |
| 956 prog_in = optarg; |
| 957 break; |
| 958 |
| 959 case 'e': |
| 960 |
| 961 if (edges_only) FATAL("Multiple -e options not supported"); |
| 962 edges_only = 1; |
| 963 break; |
| 964 |
| 965 case 'x': |
| 966 |
| 967 if (exit_crash) FATAL("Multiple -x options not supported"); |
| 968 exit_crash = 1; |
| 969 break; |
| 970 |
| 971 case 'm': { |
| 972 |
| 973 u8 suffix = 'M'; |
| 974 |
| 975 if (mem_limit_given) FATAL("Multiple -m options not supported"); |
| 976 mem_limit_given = 1; |
| 977 |
| 978 if (!strcmp(optarg, "none")) { |
| 979 |
| 980 mem_limit = 0; |
| 981 break; |
| 982 |
| 983 } |
| 984 |
| 985 if (sscanf(optarg, "%llu%c", &mem_limit, &suffix) < 1 || |
| 986 optarg[0] == '-') FATAL("Bad syntax used for -m"); |
| 987 |
| 988 switch (suffix) { |
| 989 |
| 990 case 'T': mem_limit *= 1024 * 1024; break; |
| 991 case 'G': mem_limit *= 1024; break; |
| 992 case 'k': mem_limit /= 1024; break; |
| 993 case 'M': break; |
| 994 |
| 995 default: FATAL("Unsupported suffix or bad syntax for -m"); |
| 996 |
| 997 } |
| 998 |
| 999 if (mem_limit < 5) FATAL("Dangerously low value of -m"); |
| 1000 |
| 1001 if (sizeof(rlim_t) == 4 && mem_limit > 2000) |
| 1002 FATAL("Value of -m out of range on 32-bit systems"); |
| 1003 |
| 1004 } |
| 1005 |
| 1006 break; |
| 1007 |
| 1008 case 't': |
| 1009 |
| 1010 if (timeout_given) FATAL("Multiple -t options not supported"); |
| 1011 timeout_given = 1; |
| 1012 |
| 1013 exec_tmout = atoi(optarg); |
| 1014 |
| 1015 if (exec_tmout < 10 || optarg[0] == '-') |
| 1016 FATAL("Dangerously low value of -t"); |
| 1017 |
| 1018 break; |
| 1019 |
| 1020 case 'Q': |
| 1021 |
| 1022 if (qemu_mode) FATAL("Multiple -Q options not supported"); |
| 1023 if (!mem_limit_given) mem_limit = MEM_LIMIT_QEMU; |
| 1024 |
| 1025 qemu_mode = 1; |
| 1026 break; |
| 1027 |
| 1028 default: |
| 1029 |
| 1030 usage(argv[0]); |
| 1031 |
| 1032 } |
| 1033 |
| 1034 if (optind == argc || !in_file || !out_file) usage(argv[0]); |
| 1035 |
| 1036 setup_shm(); |
| 1037 setup_signal_handlers(); |
| 1038 |
| 1039 set_up_environment(); |
| 1040 |
| 1041 find_binary(argv[optind]); |
| 1042 detect_file_args(argv + optind); |
| 1043 |
| 1044 if (qemu_mode) |
| 1045 use_argv = get_qemu_argv(argv[0], argv + optind, argc - optind); |
| 1046 else |
| 1047 use_argv = argv + optind; |
| 1048 |
| 1049 SAYF("\n"); |
| 1050 |
| 1051 read_initial_file(); |
| 1052 |
| 1053 ACTF("Performing dry run (mem limit = %llu MB, timeout = %u ms%s)...", |
| 1054 mem_limit, exec_tmout, edges_only ? ", edges only" : ""); |
| 1055 |
| 1056 run_target(use_argv, in_data, in_len, 1); |
| 1057 |
| 1058 if (child_timed_out) |
| 1059 FATAL("Target binary times out (adjusting -t may help)."); |
| 1060 |
| 1061 if (!crash_mode) { |
| 1062 |
| 1063 OKF("Program terminates normally, minimizing in " |
| 1064 cCYA "instrumented" cRST " mode."); |
| 1065 |
| 1066 if (!anything_set()) FATAL("No instrumentation detected."); |
| 1067 |
| 1068 } else { |
| 1069 |
| 1070 OKF("Program exits with a signal, minimizing in " cMGN "crash" cRST |
| 1071 " mode."); |
| 1072 |
| 1073 } |
| 1074 |
| 1075 minimize(use_argv); |
| 1076 |
| 1077 ACTF("Writing output to '%s'...", out_file); |
| 1078 |
| 1079 close(write_to_file(out_file, in_data, in_len)); |
| 1080 |
| 1081 OKF("We're done here. Have a nice day!\n"); |
| 1082 |
| 1083 exit(0); |
| 1084 |
| 1085 } |
| 1086 |
OLD | NEW |