|
|
/*
|
|
|
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>
|
|
|
|
|
|
/*检查编译环境是否定义了__APPLE__、__FreeBSD__或__OpenBSD__宏,这些宏分别代表苹果的macOS系统、FreeBSD操作系统和OpenBSD操作系统*/
|
|
|
#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... */
|
|
|
|
|
|
/*检查是否定义了__linux__宏,这个宏通常在编译Linux系统下的代码时被编译器定义*/
|
|
|
#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. */
|
|
|
|
|
|
/*如果代码中定义了AFL_LIB宏,那么EXP_ST宏被定义为一个空的宏,可以作为布尔标志使用。
|
|
|
如果代码中没有定义AFL_LIB宏,那么EXP_ST宏被定义为static,用于限制作用域。*/
|
|
|
#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, /* 包含测试用例的输入目录 */
|
|
|
*out_file, /* 要模糊测试的文件(如果有的话) */
|
|
|
*out_dir, /* 工作和输出目录 */
|
|
|
*sync_dir, /* 同步目录 */
|
|
|
*sync_id, /* 模糊测试器标识符 */
|
|
|
*use_banner, /* 显示横幅 */
|
|
|
*in_bitmap, /* 输入位图 */
|
|
|
*doc_path, /* 文档目录的路径 */
|
|
|
*target_path, /* 目标二进制文件路径 */
|
|
|
*orig_cmdline; /* 原始命令行 */
|
|
|
|
|
|
EXP_ST u32 exec_tmout = EXEC_TIMEOUT; /* 可配置的执行超时 (ms) */
|
|
|
static u32 hang_tmout = EXEC_TIMEOUT; /* 用于挂起的超时时间 (ms) */
|
|
|
|
|
|
EXP_ST u64 mem_limit = MEM_LIMIT; /* 子进程的内存限制 (MB) */
|
|
|
|
|
|
EXP_ST u32 cpu_to_bind = 0; /* 待绑定的空闲CPU核id */
|
|
|
|
|
|
static u32 stats_update_freq = 1; /* 统计更新频率 (执行次数)*/
|
|
|
|
|
|
EXP_ST u8 skip_deterministic, /* 跳过确定性阶段? */
|
|
|
force_deterministic, /* 强制执行确定性阶段? */
|
|
|
use_splicing, /* 重组输入文件? */
|
|
|
dumb_mode, /* 在非插桩模式下运行? */
|
|
|
score_changed, /* 收藏项的评分改变了? */
|
|
|
kill_signal, /* 导致子进程退出的信号 */
|
|
|
resuming_fuzz, /* 恢复旧的模糊测试工作? */
|
|
|
timeout_given, /* 给出了特定的超时时间? */
|
|
|
cpu_to_bind_given, /* 指定了cpu_to_bind? */
|
|
|
not_on_tty, /* 标准输出不是tty */
|
|
|
term_too_small, /* 终端尺寸太小 */
|
|
|
uses_asan, /* 目标使用ASAN? */
|
|
|
no_forkserver, /* 禁用forkserver? */
|
|
|
crash_mode, /* 崩溃模式!耶! */
|
|
|
in_place_resume, /* 尝试就地恢复? */
|
|
|
auto_changed, /* 自动生成的令牌改变了? */
|
|
|
no_cpu_meter_red, /* 状态屏幕上的风水 */
|
|
|
no_arith, /* 跳过大多数算术操作 */
|
|
|
shuffle_queue, /* 打乱输入队列? */
|
|
|
bitmap_changed = 1, /* 更新位图的时间? */
|
|
|
qemu_mode, /* 在QEMU模式下运行? */
|
|
|
skip_requested, /* 通过SIGUSR1跳过请求 */
|
|
|
run_over10m, /* 运行时间超过10分钟? */
|
|
|
persistent_mode, /* 在持久模式下运行? */
|
|
|
deferred_mode, /* 延迟forkserver模式? */
|
|
|
fast_cal; /* 尝试更快地校准? */
|
|
|
|
|
|
static s32 out_fd, /* 用于out_file的持久文件描述符 */
|
|
|
dev_urandom_fd = -1, /* 用于/dev/urandom的持久文件描述符 */
|
|
|
dev_null_fd = -1, /* 用于/dev/null的持久文件描述符 */
|
|
|
fsrv_ctl_fd, /* fork服务器控制管道(写入) */
|
|
|
fsrv_st_fd; /* fork服务器状态管道(读取) */
|
|
|
|
|
|
static s32 forksrv_pid, /* fork服务器的进程ID */
|
|
|
child_pid = -1, /* 被模糊测试的程序的进程ID */
|
|
|
out_dir_fd = -1; /* 锁定文件的文件描述符 */
|
|
|
|
|
|
EXP_ST u8* trace_bits; /* 与插桩位图共享的共享内存 */
|
|
|
|
|
|
EXP_ST u8 virgin_bits[MAP_SIZE], /* 尚未被模糊测试触及的区域 */
|
|
|
virgin_tmout[MAP_SIZE], /* 我们在超时中尚未见过的位 */
|
|
|
virgin_crash[MAP_SIZE]; /* 我们在崩溃中尚未见过的位 */
|
|
|
|
|
|
static u8 var_bytes[MAP_SIZE]; /* 看起来是可变的字节 */
|
|
|
|
|
|
static s32 shm_id; /* 共享内存区域的ID */
|
|
|
|
|
|
static volatile u8 stop_soon, /* Ctrl-C 被按下了吗? */
|
|
|
clear_screen = 1, /* 窗口被调整大小了吗? */
|
|
|
child_timed_out; /* 被追踪的进程超时了吗? */
|
|
|
|
|
|
EXP_ST u32 queued_paths, /* 排队的测试用例总数 */
|
|
|
queued_variable, /* 具有可变行为的测试用例 */
|
|
|
queued_at_start, /* 初始输入的总数 */
|
|
|
queued_discovered, /* 在此运行期间发现的项目 */
|
|
|
queued_imported, /* 通过-S导入的项目 */
|
|
|
queued_favored, /* 被认为有利的路径 */
|
|
|
queued_with_cov, /* 有新的覆盖字节的路径 */
|
|
|
pending_not_fuzzed, /* 已排队但尚未完成 */
|
|
|
pending_favored, /* 等待中的优先路径 */
|
|
|
cur_skipped_paths, /* 当前周期中放弃的输入 */
|
|
|
cur_depth, /* 当前路径深度 */
|
|
|
max_depth, /* 最大路径深度 */
|
|
|
useless_at_start, /* 无用的起始路径数量 */
|
|
|
var_byte_count, /* 具有可变行为的位图字节 */
|
|
|
current_entry, /* 当前队列条目ID */
|
|
|
havoc_div = 1; /* 对havoc的循环计数除数 */
|
|
|
|
|
|
EXP_ST u64 total_crashes, /* 崩溃总数 */
|
|
|
unique_crashes, /* 具有唯一签名的崩溃 */
|
|
|
total_tmouts, /* 超时总数 */
|
|
|
unique_tmouts, /* 具有唯一签名的超时 */
|
|
|
unique_hangs, /* 具有唯一签名的挂起 */
|
|
|
total_execs, /* 总的execve()调用次数 */
|
|
|
slowest_exec_ms, /* 最慢的测试用例非挂起时间(ms) */
|
|
|
start_time, /* Unix开始时间(ms) */
|
|
|
last_path_time, /* 最近路径的时间(ms) */
|
|
|
last_crash_time, /* 最近崩溃的时间(ms) */
|
|
|
last_hang_time, /* 最近挂起的时间(ms) */
|
|
|
last_crash_execs, /* 上次崩溃时的执行计数器 */
|
|
|
queue_cycle, /* 队列循环计数器 */
|
|
|
cycles_wo_finds, /* 没有发现新路径的周期数 */
|
|
|
trim_execs, /* 用于修剪输入文件的执行次数 */
|
|
|
bytes_trim_in, /* 进入修剪器的字节 */
|
|
|
bytes_trim_out, /* 从修剪器出来的字节 */
|
|
|
blocks_eff_total, /* 受效应器映射影响的块 */
|
|
|
blocks_eff_select; /* 被选为可模糊化的块 */
|
|
|
|
|
|
static u32 subseq_tmouts; /* 连续超时的次数 */
|
|
|
|
|
|
static u8 *stage_name = "init", /* 当前模糊测试阶段的名称 */
|
|
|
*stage_short, /* 简短的阶段名称 */
|
|
|
*syncing_party; /* 当前正在同步的... */
|
|
|
|
|
|
static s32 stage_cur, stage_max; /* 阶段进度 */
|
|
|
static s32 splicing_with = -1; /* 与哪个测试用例混合? */
|
|
|
|
|
|
static u32 master_id, master_max; /* 主实例工作分割 */
|
|
|
|
|
|
static u32 syncing_case; /* 与案例#...同步 */
|
|
|
|
|
|
static s32 stage_cur_byte, /* 当前阶段操作的字节偏移量 */
|
|
|
stage_cur_val; /* 用于阶段操作的值 */
|
|
|
|
|
|
static u8 stage_val_type; /* 值类型(STAGE_VAL_*) */
|
|
|
|
|
|
static u64 stage_finds[32], /* 每个模糊测试阶段发现的模式 */
|
|
|
stage_cycles[32]; /* 每个模糊测试阶段的执行次数 */
|
|
|
|
|
|
static u32 rand_cnt; /* 随机数计数器 */
|
|
|
|
|
|
static u64 total_cal_us, /* 总校准时间(微秒) */
|
|
|
total_cal_cycles; /* 总校准周期 */
|
|
|
|
|
|
static u64 total_bitmap_size, /* 所有位图的总位数 */
|
|
|
total_bitmap_entries; /* 计算的位图数量 */
|
|
|
|
|
|
static s32 cpu_core_count; /* CPU核心数 */
|
|
|
|
|
|
#ifdef HAVE_AFFINITY
|
|
|
|
|
|
static s32 cpu_aff = -1; /* 选择的CPU核心 */
|
|
|
|
|
|
#endif /* HAVE_AFFINITY */
|
|
|
|
|
|
static FILE* plot_file; /* Gnuplot输出文件 */
|
|
|
|
|
|
struct queue_entry {
|
|
|
|
|
|
u8* fname; /* 测试用例的文件名 */
|
|
|
u32 len; /* 输入长度 */
|
|
|
|
|
|
u8 cal_failed, /* 校准失败? */
|
|
|
trim_done, /* 修剪完成? */
|
|
|
was_fuzzed, /* 之前进行过模糊测试吗? */
|
|
|
passed_det, /* 通过了确定性阶段吗? */
|
|
|
has_new_cov, /* 触发了新的代码覆盖吗? */
|
|
|
var_behavior, /* 表现出可变行为吗? */
|
|
|
favored, /* 当前是否被优先考虑? */
|
|
|
fs_redundant; /* 在文件系统中被标记为冗余了吗? */
|
|
|
|
|
|
u32 bitmap_size, /* 位图中设置的位数 */
|
|
|
exec_cksum; /* 执行轨迹的校验和 */
|
|
|
|
|
|
u64 exec_us, /* 执行时间(微秒) */
|
|
|
handicap, /* 在队列中的落后周期数 */
|
|
|
depth; /* 路径深度 */
|
|
|
|
|
|
u8* trace_mini; /* 如果保留,执行轨迹的字节 */
|
|
|
u32 tc_ref; /* 执行轨迹字节的引用计数 */
|
|
|
|
|
|
struct queue_entry *next, /* 下一个元素,如果有的话 */
|
|
|
*next_100; /* 100个元素之后的元素 */
|
|
|
|
|
|
};
|
|
|
|
|
|
static struct queue_entry *queue, /* 模糊测试队列(链表) */
|
|
|
*queue_cur, /* 队列中的当前偏移量 */
|
|
|
*queue_top, /* 列表的顶部 */
|
|
|
*q_prev100; /* 上一个100个标记 */
|
|
|
|
|
|
static struct queue_entry*
|
|
|
top_rated[MAP_SIZE]; /* 位图字节的顶级条目 */
|
|
|
|
|
|
struct extra_data {
|
|
|
u8* data; /* 词典令牌数据 */
|
|
|
u32 len; /* 词典令牌长度 */
|
|
|
u32 hit_cnt; /* 在语料库中的使用次数 */
|
|
|
};
|
|
|
|
|
|
static struct extra_data* extras; /* 用于模糊测试的额外令牌 */
|
|
|
static u32 extras_cnt; /* 读取的令牌总数 */
|
|
|
|
|
|
static struct extra_data* a_extras; /* 自动选择的额外令牌 */
|
|
|
static u32 a_extras_cnt; /* 可用的令牌总数 */
|
|
|
|
|
|
static u8* (*post_handler)(u8* buf, u32* len);
|
|
|
|
|
|
/* 根据config.h中的设置interesting的值 */
|
|
|
|
|
|
static s8 interesting_8[] = { INTERESTING_8 };
|
|
|
static s16 interesting_16[] = { INTERESTING_8, INTERESTING_16 };
|
|
|
static s32 interesting_32[] = { INTERESTING_8, INTERESTING_16, INTERESTING_32 };
|
|
|
|
|
|
/* 模糊测试阶段 */
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
/*阶段值类型 */
|
|
|
|
|
|
enum {
|
|
|
/* 00 */ STAGE_VAL_NONE,
|
|
|
/* 01 */ STAGE_VAL_LE,
|
|
|
/* 02 */ STAGE_VAL_BE
|
|
|
};
|
|
|
|
|
|
/* 执行状态故障代码 */
|
|
|
|
|
|
enum {
|
|
|
/* 00 */ FAULT_NONE,
|
|
|
/* 01 */ FAULT_TMOUT,
|
|
|
/* 02 */ FAULT_CRASH,
|
|
|
/* 03 */ FAULT_ERROR,
|
|
|
/* 04 */ FAULT_NOINST,
|
|
|
/* 05 */ FAULT_NOBITS
|
|
|
};
|
|
|
|
|
|
|
|
|
/* 获取毫秒级的Unix时间戳 */
|
|
|
|
|
|
static u64 get_cur_time(void) {
|
|
|
|
|
|
struct timeval tv;
|
|
|
struct timezone tz;
|
|
|
|
|
|
gettimeofday(&tv, &tz);
|
|
|
|
|
|
return (tv.tv_sec * 1000ULL) + (tv.tv_usec / 1000);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 获取微秒级的Unix时间戳 */
|
|
|
|
|
|
static u64 get_cur_time_us(void) {
|
|
|
|
|
|
struct timeval tv;
|
|
|
struct timezone tz;
|
|
|
|
|
|
gettimeofday(&tv, &tz);
|
|
|
|
|
|
return (tv.tv_sec * 1000000ULL) + tv.tv_usec;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 生成一个随机数(从0到限值减1)。这可能存在轻微的偏差。 */
|
|
|
|
|
|
static inline u32 UR(u32 limit) {
|
|
|
|
|
|
if (unlikely(!rand_cnt--)) {
|
|
|
|
|
|
u32 seed[2];
|
|
|
|
|
|
ck_read(dev_urandom_fd, &seed, sizeof(seed), "/dev/urandom");
|
|
|
|
|
|
srandom(seed[0]);
|
|
|
rand_cnt = (RESEED_RNG / 2) + (seed[1] % RESEED_RNG);
|
|
|
|
|
|
}
|
|
|
|
|
|
return random() % limit;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 洗牌一个指针数组。可能会有轻微的偏差。 */
|
|
|
|
|
|
static void shuffle_ptrs(void** ptrs, u32 cnt) {
|
|
|
|
|
|
u32 i;
|
|
|
|
|
|
for (i = 0; i < cnt - 2; i++) {
|
|
|
|
|
|
u32 j = i + UR(cnt - i);
|
|
|
void *s = ptrs[i];
|
|
|
ptrs[i] = ptrs[j];
|
|
|
ptrs[j] = s;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
#ifdef HAVE_AFFINITY
|
|
|
|
|
|
/* 构建绑定到特定核心的进程列表。如果找不到任何东西,返回-1。假设cpu上限为4k。 */
|
|
|
|
|
|
static void bind_to_free_cpu(void) {
|
|
|
|
|
|
DIR* d;
|
|
|
struct dirent* de;
|
|
|
cpu_set_t c;
|
|
|
|
|
|
u8 cpu_used[4096] = { 0 };
|
|
|
u32 i;
|
|
|
|
|
|
if (cpu_core_count < 2) return;
|
|
|
|
|
|
if (getenv("AFL_NO_AFFINITY")) {
|
|
|
|
|
|
WARNF("Not binding to a CPU core (AFL_NO_AFFINITY set).");
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
d = opendir("/proc");
|
|
|
|
|
|
if (!d) {
|
|
|
|
|
|
WARNF("Unable to access /proc - can't scan for free CPU cores.");
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
ACTF("Checking CPU core loadout...");
|
|
|
|
|
|
/* 引入一些抖动,以防多个AFL任务在同一时间做同一件事情... */
|
|
|
|
|
|
usleep(R(1000) * 250);
|
|
|
|
|
|
/* 扫描所有 /proc/<pid>/status 条目,检查 Cpus_allowed_list。
|
|
|
使用 cpu_used[] 标记所有绑定到特定 CPU 的进程。
|
|
|
这在某些特殊的绑定设置中可能会失败,但在几乎所有现实世界的用例中都足够好。 */
|
|
|
|
|
|
while ((de = readdir(d))) {
|
|
|
|
|
|
u8* fn;
|
|
|
FILE* f;
|
|
|
u8 tmp[MAX_LINE];
|
|
|
u8 has_vmsize = 0;
|
|
|
|
|
|
if (!isdigit(de->d_name[0])) continue;
|
|
|
|
|
|
fn = alloc_printf("/proc/%s/status", de->d_name);
|
|
|
|
|
|
if (!(f = fopen(fn, "r"))) {
|
|
|
ck_free(fn);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
while (fgets(tmp, MAX_LINE, f)) {
|
|
|
|
|
|
u32 hval;
|
|
|
|
|
|
/* 没有VmSize的进程可能是内核任务。 */
|
|
|
|
|
|
if (!strncmp(tmp, "VmSize:\t", 8)) has_vmsize = 1;
|
|
|
|
|
|
if (!strncmp(tmp, "Cpus_allowed_list:\t", 19) &&
|
|
|
!strchr(tmp, '-') && !strchr(tmp, ',') &&
|
|
|
sscanf(tmp + 19, "%u", &hval) == 1 && hval < sizeof(cpu_used) &&
|
|
|
has_vmsize) {
|
|
|
|
|
|
cpu_used[hval] = 1;
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
ck_free(fn);
|
|
|
fclose(f);
|
|
|
|
|
|
}
|
|
|
|
|
|
closedir(d);
|
|
|
if (cpu_to_bind_given) {
|
|
|
|
|
|
if (cpu_to_bind >= cpu_core_count)
|
|
|
FATAL("The CPU core id to bind should be between 0 and %u", cpu_core_count - 1);
|
|
|
|
|
|
if (cpu_used[cpu_to_bind])
|
|
|
FATAL("The CPU core #%u to bind is not free!", cpu_to_bind);
|
|
|
|
|
|
i = cpu_to_bind;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
for (i = 0; i < cpu_core_count; i++) if (!cpu_used[i]) break;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (i == cpu_core_count) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Uh-oh, looks like all %u CPU cores on your system are allocated to\n"
|
|
|
" other instances of afl-fuzz (or similar CPU-locked tasks). Starting\n"
|
|
|
" another fuzzer on this machine is probably a bad plan, but if you are\n"
|
|
|
" absolutely sure, you can set AFL_NO_AFFINITY and try again.\n",
|
|
|
cpu_core_count);
|
|
|
|
|
|
FATAL("No more free CPU cores");
|
|
|
|
|
|
}
|
|
|
|
|
|
OKF("Found a free CPU core, binding to #%u.", i);
|
|
|
|
|
|
cpu_aff = i;
|
|
|
|
|
|
CPU_ZERO(&c);
|
|
|
CPU_SET(i, &c);
|
|
|
|
|
|
if (sched_setaffinity(0, sizeof(c), &c))
|
|
|
PFATAL("sched_setaffinity failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* HAVE_AFFINITY */
|
|
|
|
|
|
#ifndef IGNORE_FINDS
|
|
|
|
|
|
/* 辅助函数用于比较缓冲区;返回第一个和最后一个不同的偏移量。我们用这个来找到合理的地点来拼接两个文件。 */
|
|
|
|
|
|
static void locate_diffs(u8* ptr1, u8* ptr2, u32 len, s32* first, s32* last) {
|
|
|
|
|
|
s32 f_loc = -1;
|
|
|
s32 l_loc = -1;
|
|
|
u32 pos;
|
|
|
|
|
|
for (pos = 0; pos < len; pos++) {
|
|
|
|
|
|
if (*(ptr1++) != *(ptr2++)) {
|
|
|
|
|
|
if (f_loc == -1) f_loc = pos;
|
|
|
l_loc = pos;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
*first = f_loc;
|
|
|
*last = l_loc;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* !IGNORE_FINDS */
|
|
|
|
|
|
|
|
|
/*描述整数。使用12个循环的静态缓冲区来存储返回值。
|
|
|
对于我们合理预期会看到的所有的整数,返回的值应该是五个字符或更少。 */
|
|
|
|
|
|
static u8* DI(u64 val) {
|
|
|
|
|
|
static u8 tmp[12][16];
|
|
|
static u8 cur;
|
|
|
|
|
|
cur = (cur + 1) % 12;
|
|
|
|
|
|
#define CHK_FORMAT(_divisor, _limit_mult, _fmt, _cast) do { \
|
|
|
if (val < (_divisor) * (_limit_mult)) { \
|
|
|
sprintf(tmp[cur], _fmt, ((_cast)val) / (_divisor)); \
|
|
|
return tmp[cur]; \
|
|
|
} \
|
|
|
} while (0)
|
|
|
|
|
|
/* 0-9999 */
|
|
|
CHK_FORMAT(1, 10000, "%llu", u64);
|
|
|
|
|
|
/* 10.0k - 99.9k */
|
|
|
CHK_FORMAT(1000, 99.95, "%0.01fk", double);
|
|
|
|
|
|
/* 100k - 999k */
|
|
|
CHK_FORMAT(1000, 1000, "%lluk", u64);
|
|
|
|
|
|
/* 1.00M - 9.99M */
|
|
|
CHK_FORMAT(1000 * 1000, 9.995, "%0.02fM", double);
|
|
|
|
|
|
/* 10.0M - 99.9M */
|
|
|
CHK_FORMAT(1000 * 1000, 99.95, "%0.01fM", double);
|
|
|
|
|
|
/* 100M - 999M */
|
|
|
CHK_FORMAT(1000 * 1000, 1000, "%lluM", u64);
|
|
|
|
|
|
/* 1.00G - 9.99G */
|
|
|
CHK_FORMAT(1000LL * 1000 * 1000, 9.995, "%0.02fG", double);
|
|
|
|
|
|
/* 10.0G - 99.9G */
|
|
|
CHK_FORMAT(1000LL * 1000 * 1000, 99.95, "%0.01fG", double);
|
|
|
|
|
|
/* 100G - 999G */
|
|
|
CHK_FORMAT(1000LL * 1000 * 1000, 1000, "%lluG", u64);
|
|
|
|
|
|
/* 1.00T - 9.99G */
|
|
|
CHK_FORMAT(1000LL * 1000 * 1000 * 1000, 9.995, "%0.02fT", double);
|
|
|
|
|
|
/* 10.0T - 99.9T */
|
|
|
CHK_FORMAT(1000LL * 1000 * 1000 * 1000, 99.95, "%0.01fT", double);
|
|
|
|
|
|
/* 100T+ */
|
|
|
strcpy(tmp[cur], "infty");
|
|
|
return tmp[cur];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 描述浮点数。与上面的类似,只是使用单个静态缓冲区。 */
|
|
|
|
|
|
static u8* DF(double val) {
|
|
|
|
|
|
static u8 tmp[16];
|
|
|
|
|
|
if (val < 99.995) {
|
|
|
sprintf(tmp, "%0.02f", val);
|
|
|
return tmp;
|
|
|
}
|
|
|
|
|
|
if (val < 999.95) {
|
|
|
sprintf(tmp, "%0.01f", val);
|
|
|
return tmp;
|
|
|
}
|
|
|
|
|
|
return DI((u64)val);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 将整数描述为内存大小。*/
|
|
|
|
|
|
static u8* DMS(u64 val) {
|
|
|
|
|
|
static u8 tmp[12][16];
|
|
|
static u8 cur;
|
|
|
|
|
|
cur = (cur + 1) % 12;
|
|
|
|
|
|
/* 0-9999 */
|
|
|
CHK_FORMAT(1, 10000, "%llu B", u64);
|
|
|
|
|
|
/* 10.0k - 99.9k */
|
|
|
CHK_FORMAT(1024, 99.95, "%0.01f kB", double);
|
|
|
|
|
|
/* 100k - 999k */
|
|
|
CHK_FORMAT(1024, 1000, "%llu kB", u64);
|
|
|
|
|
|
/* 1.00M - 9.99M */
|
|
|
CHK_FORMAT(1024 * 1024, 9.995, "%0.02f MB", double);
|
|
|
|
|
|
/* 10.0M - 99.9M */
|
|
|
CHK_FORMAT(1024 * 1024, 99.95, "%0.01f MB", double);
|
|
|
|
|
|
/* 100M - 999M */
|
|
|
CHK_FORMAT(1024 * 1024, 1000, "%llu MB", u64);
|
|
|
|
|
|
/* 1.00G - 9.99G */
|
|
|
CHK_FORMAT(1024LL * 1024 * 1024, 9.995, "%0.02f GB", double);
|
|
|
|
|
|
/* 10.0G - 99.9G */
|
|
|
CHK_FORMAT(1024LL * 1024 * 1024, 99.95, "%0.01f GB", double);
|
|
|
|
|
|
/* 100G - 999G */
|
|
|
CHK_FORMAT(1024LL * 1024 * 1024, 1000, "%llu GB", u64);
|
|
|
|
|
|
/* 1.00T - 9.99G */
|
|
|
CHK_FORMAT(1024LL * 1024 * 1024 * 1024, 9.995, "%0.02f TB", double);
|
|
|
|
|
|
/* 10.0T - 99.9T */
|
|
|
CHK_FORMAT(1024LL * 1024 * 1024 * 1024, 99.95, "%0.01f TB", double);
|
|
|
|
|
|
#undef CHK_FORMAT
|
|
|
|
|
|
/* 100T+ */
|
|
|
strcpy(tmp[cur], "infty");
|
|
|
return tmp[cur];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 描述时间增量。返回一个静态缓冲区,长度不超过34个字符。 */
|
|
|
|
|
|
static u8* DTD(u64 cur_ms, u64 event_ms) {
|
|
|
|
|
|
static u8 tmp[64];
|
|
|
u64 delta;
|
|
|
s32 t_d, t_h, t_m, t_s;
|
|
|
|
|
|
if (!event_ms) return "none seen yet";
|
|
|
|
|
|
delta = cur_ms - event_ms;
|
|
|
|
|
|
t_d = delta / 1000 / 60 / 60 / 24;
|
|
|
t_h = (delta / 1000 / 60 / 60) % 24;
|
|
|
t_m = (delta / 1000 / 60) % 60;
|
|
|
t_s = (delta / 1000) % 60;
|
|
|
|
|
|
sprintf(tmp, "%s days, %u hrs, %u min, %u sec", DI(t_d), t_h, t_m, t_s);
|
|
|
return tmp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 标记特定队列条目的确定性检查为已完成。我们使用.state文件来避免在恢复中断的扫描时重复进行确定性模糊测试。 */
|
|
|
|
|
|
static void mark_as_det_done(struct queue_entry* q) {
|
|
|
|
|
|
u8* fn = strrchr(q->fname, '/');
|
|
|
s32 fd;
|
|
|
|
|
|
fn = alloc_printf("%s/queue/.state/deterministic_done/%s", out_dir, fn + 1);
|
|
|
|
|
|
fd = open(fn, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", fn);
|
|
|
close(fd);
|
|
|
|
|
|
ck_free(fn);
|
|
|
|
|
|
q->passed_det = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 标记为变量。如果可能的话,创建符号链接以便于检查文件。 */
|
|
|
|
|
|
static void mark_as_variable(struct queue_entry* q) {
|
|
|
|
|
|
u8 *fn = strrchr(q->fname, '/') + 1, *ldest;
|
|
|
|
|
|
ldest = alloc_printf("../../%s", fn);
|
|
|
fn = alloc_printf("%s/queue/.state/variable_behavior/%s", out_dir, fn);
|
|
|
|
|
|
if (symlink(ldest, fn)) {
|
|
|
|
|
|
s32 fd = open(fn, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", fn);
|
|
|
close(fd);
|
|
|
|
|
|
}
|
|
|
|
|
|
ck_free(ldest);
|
|
|
ck_free(fn);
|
|
|
|
|
|
q->var_behavior = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 标记/取消标记为冗余(仅边)。这并不用于恢复状态,但可能对后处理数据集有用。 */
|
|
|
|
|
|
static void mark_as_redundant(struct queue_entry* q, u8 state) {
|
|
|
|
|
|
u8* fn;
|
|
|
s32 fd;
|
|
|
|
|
|
if (state == q->fs_redundant) return;
|
|
|
|
|
|
q->fs_redundant = state;
|
|
|
|
|
|
fn = strrchr(q->fname, '/');
|
|
|
fn = alloc_printf("%s/queue/.state/redundant_edges/%s", out_dir, fn + 1);
|
|
|
|
|
|
if (state) {
|
|
|
|
|
|
fd = open(fn, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", fn);
|
|
|
close(fd);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (unlink(fn)) PFATAL("Unable to remove '%s'", fn);
|
|
|
|
|
|
}
|
|
|
|
|
|
ck_free(fn);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 向队列追加新的测试用例。 */
|
|
|
|
|
|
static void add_to_queue(u8* fname, u32 len, u8 passed_det) {
|
|
|
|
|
|
struct queue_entry* q = ck_alloc(sizeof(struct queue_entry));
|
|
|
|
|
|
q->fname = fname;
|
|
|
q->len = len;
|
|
|
q->depth = cur_depth + 1;
|
|
|
q->passed_det = passed_det;
|
|
|
|
|
|
if (q->depth > max_depth) max_depth = q->depth;
|
|
|
|
|
|
if (queue_top) {
|
|
|
|
|
|
queue_top->next = q;
|
|
|
queue_top = q;
|
|
|
|
|
|
} else q_prev100 = queue = queue_top = q;
|
|
|
|
|
|
queued_paths++;
|
|
|
pending_not_fuzzed++;
|
|
|
|
|
|
cycles_wo_finds = 0;
|
|
|
|
|
|
/* 为每100个元素(索引0,100等)设置next_100指针,以允许更快的迭代。 */
|
|
|
if ((queued_paths - 1) % 100 == 0 && queued_paths > 1) {
|
|
|
|
|
|
q_prev100->next_100 = q;
|
|
|
q_prev100 = q;
|
|
|
|
|
|
}
|
|
|
|
|
|
last_path_time = get_cur_time();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 销毁整个队列。 */
|
|
|
|
|
|
EXP_ST void destroy_queue(void) {
|
|
|
|
|
|
struct queue_entry *q = queue, *n;
|
|
|
|
|
|
while (q) {
|
|
|
|
|
|
n = q->next;
|
|
|
ck_free(q->fname);
|
|
|
ck_free(q->trace_mini);
|
|
|
ck_free(q);
|
|
|
q = n;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 将位图写入文件。位图主要用于秘密的 -B 选项,以便在不重新发现所有
|
|
|
其他内容的情况下,将单独的模糊测试会话集中在某个特定的有趣输入上。*/
|
|
|
|
|
|
EXP_ST void write_bitmap(void) {
|
|
|
|
|
|
u8* fname;
|
|
|
s32 fd;
|
|
|
|
|
|
if (!bitmap_changed) return;
|
|
|
bitmap_changed = 0;
|
|
|
|
|
|
fname = alloc_printf("%s/fuzz_bitmap", out_dir);
|
|
|
fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to open '%s'", fname);
|
|
|
|
|
|
ck_write(fd, virgin_bits, MAP_SIZE, fname);
|
|
|
|
|
|
close(fd);
|
|
|
ck_free(fname);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 从文件中读取位图。这是-B选项。 */
|
|
|
|
|
|
EXP_ST void read_bitmap(u8* fname) {
|
|
|
|
|
|
s32 fd = open(fname, O_RDONLY);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to open '%s'", fname);
|
|
|
|
|
|
ck_read(fd, virgin_bits, MAP_SIZE, fname);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 检查当前的执行路径是否带来了新的内容。
|
|
|
更新virgin bits以反映发现的内容。如果唯一的变化是某个特定元组的命中次数,则返回1;如果看到了新的元组,则返回2。
|
|
|
更新映射,因此后续调用总是返回0。
|
|
|
|
|
|
这个函数在每次对相当大的缓冲区执行exec()之后被调用,所以它需要快速。我们在32位和64位版本中都这样做。 */
|
|
|
|
|
|
static inline u8 has_new_bits(u8* virgin_map) {
|
|
|
|
|
|
#ifdef WORD_SIZE_64
|
|
|
|
|
|
u64* current = (u64*)trace_bits;
|
|
|
u64* virgin = (u64*)virgin_map;
|
|
|
|
|
|
u32 i = (MAP_SIZE >> 3);
|
|
|
|
|
|
#else
|
|
|
|
|
|
u32* current = (u32*)trace_bits;
|
|
|
u32* virgin = (u32*)virgin_map;
|
|
|
|
|
|
u32 i = (MAP_SIZE >> 2);
|
|
|
|
|
|
#endif /* ^WORD_SIZE_64 */
|
|
|
|
|
|
u8 ret = 0;
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
|
/* 针对(*current & *virgin)== 0进行优化——即,当前位图中没有未从virgin map中清除的位——因为这种情况几乎总是会出现。 */
|
|
|
|
|
|
if (unlikely(*current) && unlikely(*current & *virgin)) {
|
|
|
|
|
|
if (likely(ret < 2)) {
|
|
|
|
|
|
u8* cur = (u8*)current;
|
|
|
u8* vir = (u8*)virgin;
|
|
|
|
|
|
/* 看起来我们还没有找到任何新的字节;检查current[]中的任何非零字节是否在virgin[]中是未被触碰的。*/
|
|
|
|
|
|
#ifdef WORD_SIZE_64
|
|
|
|
|
|
if ((cur[0] && vir[0] == 0xff) || (cur[1] && vir[1] == 0xff) ||
|
|
|
(cur[2] && vir[2] == 0xff) || (cur[3] && vir[3] == 0xff) ||
|
|
|
(cur[4] && vir[4] == 0xff) || (cur[5] && vir[5] == 0xff) ||
|
|
|
(cur[6] && vir[6] == 0xff) || (cur[7] && vir[7] == 0xff)) ret = 2;
|
|
|
else ret = 1;
|
|
|
|
|
|
#else
|
|
|
|
|
|
if ((cur[0] && vir[0] == 0xff) || (cur[1] && vir[1] == 0xff) ||
|
|
|
(cur[2] && vir[2] == 0xff) || (cur[3] && vir[3] == 0xff)) ret = 2;
|
|
|
else ret = 1;
|
|
|
|
|
|
#endif /* ^WORD_SIZE_64 */
|
|
|
|
|
|
}
|
|
|
|
|
|
*virgin &= ~*current;
|
|
|
|
|
|
}
|
|
|
|
|
|
current++;
|
|
|
virgin++;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ret && virgin_map == virgin_bits) bitmap_changed = 1;
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 计算提供的位图中设置的位数。它每秒多次用于状态屏幕,不需要太快。 */
|
|
|
|
|
|
static u32 count_bits(u8* mem) {
|
|
|
|
|
|
u32* ptr = (u32*)mem;
|
|
|
u32 i = (MAP_SIZE >> 2);
|
|
|
u32 ret = 0;
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
|
u32 v = *(ptr++);
|
|
|
|
|
|
/* 这个函数被调用在相反的,virgin bitmap上;针对稀疏数据进行优化。 */
|
|
|
|
|
|
if (v == 0xffffffff) {
|
|
|
ret += 32;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
v -= ((v >> 1) & 0x55555555);
|
|
|
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
|
|
|
ret += (((v + (v >> 4)) & 0xF0F0F0F) * 0x01010101) >> 24;
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
#define FF(_b) (0xff << ((_b) << 3))
|
|
|
|
|
|
/* 计算位图中设置的字节数。调用相对不频繁,主要用于更新状态屏幕或校准和检查确认的新路径。 */
|
|
|
|
|
|
static u32 count_bytes(u8* mem) {
|
|
|
|
|
|
u32* ptr = (u32*)mem;
|
|
|
u32 i = (MAP_SIZE >> 2);
|
|
|
u32 ret = 0;
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
|
u32 v = *(ptr++);
|
|
|
|
|
|
if (!v) continue;
|
|
|
if (v & FF(0)) ret++;
|
|
|
if (v & FF(1)) ret++;
|
|
|
if (v & FF(2)) ret++;
|
|
|
if (v & FF(3)) ret++;
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 计算位图中非255字节的数量。仅用于状态屏幕,每秒大约有几调用。*/
|
|
|
|
|
|
static u32 count_non_255_bytes(u8* mem) {
|
|
|
|
|
|
u32* ptr = (u32*)mem;
|
|
|
u32 i = (MAP_SIZE >> 2);
|
|
|
u32 ret = 0;
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
|
u32 v = *(ptr++);
|
|
|
|
|
|
/* 这个函数是在virgin bitmap上调用的,因此要针对最可能的情况进行优化。 */
|
|
|
|
|
|
if (v == 0xffffffff) continue;
|
|
|
if ((v & FF(0)) != FF(0)) ret++;
|
|
|
if ((v & FF(1)) != FF(1)) ret++;
|
|
|
if ((v & FF(2)) != FF(2)) ret++;
|
|
|
if ((v & FF(3)) != FF(3)) ret++;
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 通过消除命中次数信息,并根据元组是否被命中,用0x80或0x01替换,
|
|
|
来破坏性地简化跟踪信息。在每次新的崩溃或超时时被调用,应该足够快。 */
|
|
|
|
|
|
static const u8 simplify_lookup[256] = {
|
|
|
|
|
|
[0] = 1,
|
|
|
[1 ... 255] = 128
|
|
|
|
|
|
};
|
|
|
|
|
|
#ifdef WORD_SIZE_64
|
|
|
|
|
|
static void simplify_trace(u64* mem) {
|
|
|
|
|
|
u32 i = MAP_SIZE >> 3;
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
|
/* 优化稀疏位图 */
|
|
|
|
|
|
if (unlikely(*mem)) {
|
|
|
|
|
|
u8* mem8 = (u8*)mem;
|
|
|
|
|
|
mem8[0] = simplify_lookup[mem8[0]];
|
|
|
mem8[1] = simplify_lookup[mem8[1]];
|
|
|
mem8[2] = simplify_lookup[mem8[2]];
|
|
|
mem8[3] = simplify_lookup[mem8[3]];
|
|
|
mem8[4] = simplify_lookup[mem8[4]];
|
|
|
mem8[5] = simplify_lookup[mem8[5]];
|
|
|
mem8[6] = simplify_lookup[mem8[6]];
|
|
|
mem8[7] = simplify_lookup[mem8[7]];
|
|
|
|
|
|
} else *mem = 0x0101010101010101ULL;
|
|
|
|
|
|
mem++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
|
|
static void simplify_trace(u32* mem) {
|
|
|
|
|
|
u32 i = MAP_SIZE >> 2;
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
|
/* 优化稀疏位图 */
|
|
|
|
|
|
if (unlikely(*mem)) {
|
|
|
|
|
|
u8* mem8 = (u8*)mem;
|
|
|
|
|
|
mem8[0] = simplify_lookup[mem8[0]];
|
|
|
mem8[1] = simplify_lookup[mem8[1]];
|
|
|
mem8[2] = simplify_lookup[mem8[2]];
|
|
|
mem8[3] = simplify_lookup[mem8[3]];
|
|
|
|
|
|
} else *mem = 0x01010101;
|
|
|
|
|
|
mem++;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* ^WORD_SIZE_64 */
|
|
|
|
|
|
|
|
|
/* 破坏性地对跟踪中的执行计数进行分类。这用作任何新获得的跟踪的预处理步骤。每次执行时都会被调用,必须快速。 */
|
|
|
|
|
|
static const u8 count_class_lookup8[256] = {
|
|
|
|
|
|
[0] = 0,
|
|
|
[1] = 1,
|
|
|
[2] = 2,
|
|
|
[3] = 4,
|
|
|
[4 ... 7] = 8,
|
|
|
[8 ... 15] = 16,
|
|
|
[16 ... 31] = 32,
|
|
|
[32 ... 127] = 64,
|
|
|
[128 ... 255] = 128
|
|
|
|
|
|
};
|
|
|
|
|
|
static u16 count_class_lookup16[65536];
|
|
|
|
|
|
|
|
|
EXP_ST void init_count_class16(void) {
|
|
|
|
|
|
u32 b1, b2;
|
|
|
|
|
|
for (b1 = 0; b1 < 256; b1++)
|
|
|
for (b2 = 0; b2 < 256; b2++)
|
|
|
count_class_lookup16[(b1 << 8) + b2] =
|
|
|
(count_class_lookup8[b1] << 8) |
|
|
|
count_class_lookup8[b2];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
#ifdef WORD_SIZE_64
|
|
|
|
|
|
static inline void classify_counts(u64* mem) {
|
|
|
|
|
|
u32 i = MAP_SIZE >> 3;
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
|
/* 优化稀疏位图 */
|
|
|
|
|
|
if (unlikely(*mem)) {
|
|
|
|
|
|
u16* mem16 = (u16*)mem;
|
|
|
|
|
|
mem16[0] = count_class_lookup16[mem16[0]];
|
|
|
mem16[1] = count_class_lookup16[mem16[1]];
|
|
|
mem16[2] = count_class_lookup16[mem16[2]];
|
|
|
mem16[3] = count_class_lookup16[mem16[3]];
|
|
|
|
|
|
}
|
|
|
|
|
|
mem++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
|
|
static inline void classify_counts(u32* mem) {
|
|
|
|
|
|
u32 i = MAP_SIZE >> 2;
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
|
/* 优化稀疏位图 */
|
|
|
|
|
|
if (unlikely(*mem)) {
|
|
|
|
|
|
u16* mem16 = (u16*)mem;
|
|
|
|
|
|
mem16[0] = count_class_lookup16[mem16[0]];
|
|
|
mem16[1] = count_class_lookup16[mem16[1]];
|
|
|
|
|
|
}
|
|
|
|
|
|
mem++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* ^WORD_SIZE_64 */
|
|
|
|
|
|
|
|
|
/* 摆脱共享内存(atexit处理程序) */
|
|
|
|
|
|
static void remove_shm(void) {
|
|
|
|
|
|
shmctl(shm_id, IPC_RMID, NULL);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 将跟踪字节压缩到更小的位图中。我们实际上在这里只是丢弃了计数信息。这个函数只在一些新路径上偶尔被调用。 */
|
|
|
|
|
|
static void minimize_bits(u8* dst, u8* src) {
|
|
|
|
|
|
u32 i = 0;
|
|
|
|
|
|
while (i < MAP_SIZE) {
|
|
|
|
|
|
if (*(src++)) dst[i >> 3] |= 1 << (i & 7);
|
|
|
i++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 当我们遇到一个新的路径时,我们会调用这个函数来查看这个路径是否看起来比现有的任何路径都更“favorable”。
|
|
|
“favorable”的目的是拥有一个最小的路径集合,这些路径触发了迄今为止在位图中看到的所有位,
|
|
|
并专注于对这些路径进行模糊测试,而牺牲其他路径。
|
|
|
|
|
|
这个过程的第一步是为位图中的每个字节维护一个top_rated[]条目列表。如果之前没有竞争者,
|
|
|
或者竞争者具有更有利的速度x大小因子,我们就赢得了那个位置。
|
|
|
|
|
|
对于trace_bits[]中设置的每个字节,看看是否有之前的获胜者,以及它与我们的比较情况。 */
|
|
|
|
|
|
static void update_bitmap_score(struct queue_entry* q) {
|
|
|
|
|
|
u32 i;
|
|
|
u64 fav_factor = q->exec_us * q->len;
|
|
|
|
|
|
/* 对于trace_bits[]中设置的每个字节,看看是否有之前的获胜者,以及它与我们的比较情况。 */
|
|
|
|
|
|
for (i = 0; i < MAP_SIZE; i++)
|
|
|
|
|
|
if (trace_bits[i]) {
|
|
|
|
|
|
if (top_rated[i]) {
|
|
|
|
|
|
/* 执行速度更快或更小的测试用例是受青睐的。*/
|
|
|
|
|
|
if (fav_factor > top_rated[i]->exec_us * top_rated[i]->len) continue;
|
|
|
|
|
|
/* 看来我们要赢了。为之前的获胜者减少引用计数,如果需要的话,丢弃它的trace_bits[]。 */
|
|
|
|
|
|
if (!--top_rated[i]->tc_ref) {
|
|
|
ck_free(top_rated[i]->trace_mini);
|
|
|
top_rated[i]->trace_mini = 0;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 将自己插入为新的获胜者。 */
|
|
|
|
|
|
top_rated[i] = q;
|
|
|
q->tc_ref++;
|
|
|
|
|
|
if (!q->trace_mini) {
|
|
|
q->trace_mini = ck_alloc(MAP_SIZE >> 3);
|
|
|
minimize_bits(q->trace_mini, trace_bits);
|
|
|
}
|
|
|
|
|
|
score_changed = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/*
|
|
|
上述机制的第二部分是一个例程,它遍历top_rated[]条目,然后依次获取之前未见过的字节(temp_v)的获胜者,
|
|
|
并将它们标记为受青睐的,至少直到下一次运行。在所有的模糊测试步骤中,受青睐的条目会被给予更多的测试时间。*/
|
|
|
|
|
|
static void cull_queue(void) {
|
|
|
|
|
|
struct queue_entry* q;
|
|
|
static u8 temp_v[MAP_SIZE >> 3];
|
|
|
u32 i;
|
|
|
|
|
|
if (dumb_mode || !score_changed) return;
|
|
|
|
|
|
score_changed = 0;
|
|
|
|
|
|
memset(temp_v, 255, MAP_SIZE >> 3);
|
|
|
|
|
|
queued_favored = 0;
|
|
|
pending_favored = 0;
|
|
|
|
|
|
q = queue;
|
|
|
|
|
|
while (q) {
|
|
|
q->favored = 0;
|
|
|
q = q->next;
|
|
|
}
|
|
|
|
|
|
/* 让我们看看位图中是否有未在temp_v中捕获的内容。如果有,并且它在top_rated[]中有竞争者,那么我们就使用它。 */
|
|
|
|
|
|
for (i = 0; i < MAP_SIZE; i++)
|
|
|
if (top_rated[i] && (temp_v[i >> 3] & (1 << (i & 7)))) {
|
|
|
|
|
|
u32 j = MAP_SIZE >> 3;
|
|
|
|
|
|
/* 从temp_v中删除属于当前条目的所有位。 */
|
|
|
|
|
|
while (j--)
|
|
|
if (top_rated[i]->trace_mini[j])
|
|
|
temp_v[j] &= ~top_rated[i]->trace_mini[j];
|
|
|
|
|
|
top_rated[i]->favored = 1;
|
|
|
queued_favored++;
|
|
|
|
|
|
if (!top_rated[i]->was_fuzzed) pending_favored++;
|
|
|
|
|
|
}
|
|
|
|
|
|
q = queue;
|
|
|
|
|
|
while (q) {
|
|
|
mark_as_redundant(q, !q->favored);
|
|
|
q = q->next;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 配置共享内存和virgin_bits。这在启动时被调用。 */
|
|
|
|
|
|
EXP_ST void setup_shm(void) {
|
|
|
|
|
|
u8* shm_str;
|
|
|
|
|
|
if (!in_bitmap) memset(virgin_bits, 255, MAP_SIZE);
|
|
|
|
|
|
memset(virgin_tmout, 255, MAP_SIZE);
|
|
|
memset(virgin_crash, 255, MAP_SIZE);
|
|
|
|
|
|
shm_id = shmget(IPC_PRIVATE, MAP_SIZE, IPC_CREAT | IPC_EXCL | 0600);
|
|
|
|
|
|
if (shm_id < 0) PFATAL("shmget() failed");
|
|
|
|
|
|
atexit(remove_shm);
|
|
|
|
|
|
shm_str = alloc_printf("%d", shm_id);
|
|
|
|
|
|
/* 如果有人要求我们在dump模式下对插桩的二进制文件进行模糊测试,我们不希望他们检测到插桩,
|
|
|
因为我们不会发送fork服务器命令。这应该在以后被更好的自动检测所取代,对吗? */
|
|
|
|
|
|
if (!dumb_mode) setenv(SHM_ENV_VAR, shm_str, 1);
|
|
|
|
|
|
ck_free(shm_str);
|
|
|
|
|
|
trace_bits = shmat(shm_id, NULL, 0);
|
|
|
|
|
|
if (trace_bits == (void *)-1) PFATAL("shmat() failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 加载后处理器(如果可用) */
|
|
|
|
|
|
static void setup_post(void) {
|
|
|
|
|
|
void* dh;
|
|
|
u8* fn = getenv("AFL_POST_LIBRARY");
|
|
|
u32 tlen = 6;
|
|
|
|
|
|
if (!fn) return;
|
|
|
|
|
|
ACTF("Loading postprocessor from '%s'...", fn);
|
|
|
|
|
|
dh = dlopen(fn, RTLD_NOW);
|
|
|
if (!dh) FATAL("%s", dlerror());
|
|
|
|
|
|
post_handler = dlsym(dh, "afl_postprocess");
|
|
|
if (!post_handler) FATAL("Symbol 'afl_postprocess' not found.");
|
|
|
|
|
|
/* 做一个快速测试。现在分段故障比以后好=) */
|
|
|
|
|
|
post_handler("hello", &tlen);
|
|
|
|
|
|
OKF("Postprocessor installed successfully.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 从输入目录读取所有测试用例,然后将它们排队进行测试。在启动时调用。*/
|
|
|
|
|
|
static void read_testcases(void) {
|
|
|
|
|
|
struct dirent **nl;
|
|
|
s32 nl_cnt;
|
|
|
u32 i;
|
|
|
u8* fn;
|
|
|
|
|
|
/* 自动检测非原位恢复尝试。 */
|
|
|
|
|
|
fn = alloc_printf("%s/queue", in_dir);
|
|
|
if (!access(fn, F_OK)) in_dir = fn; else ck_free(fn);
|
|
|
|
|
|
ACTF("Scanning '%s'...", in_dir);
|
|
|
|
|
|
/* 我们使用 scandir() + alphasort() 而不是 readdir(),因为如果不这样,测试用例的排序会有些随机,并且难以控制。 */
|
|
|
|
|
|
nl_cnt = scandir(in_dir, &nl, NULL, alphasort);
|
|
|
|
|
|
if (nl_cnt < 0) {
|
|
|
|
|
|
if (errno == ENOENT || errno == ENOTDIR)
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"The input directory does not seem to be valid - try again. The fuzzer needs\n"
|
|
|
" one or more test case to start with - ideally, a small file under 1 kB\n"
|
|
|
" or so. The cases must be stored as regular files directly in the input\n"
|
|
|
" directory.\n");
|
|
|
|
|
|
PFATAL("Unable to open '%s'", in_dir);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (shuffle_queue && nl_cnt > 1) {
|
|
|
|
|
|
ACTF("Shuffling queue...");
|
|
|
shuffle_ptrs((void**)nl, nl_cnt);
|
|
|
|
|
|
}
|
|
|
|
|
|
for (i = 0; i < nl_cnt; i++) {
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
|
u8* fn = alloc_printf("%s/%s", in_dir, nl[i]->d_name);
|
|
|
u8* dfn = alloc_printf("%s/.state/deterministic_done/%s", in_dir, nl[i]->d_name);
|
|
|
|
|
|
u8 passed_det = 0;
|
|
|
|
|
|
free(nl[i]); /* not tracked */
|
|
|
|
|
|
if (lstat(fn, &st) || access(fn, R_OK))
|
|
|
PFATAL("Unable to access '%s'", fn);
|
|
|
|
|
|
/* 这也照顾.和. . */
|
|
|
|
|
|
if (!S_ISREG(st.st_mode) || !st.st_size || strstr(fn, "/README.testcases")) {
|
|
|
|
|
|
ck_free(fn);
|
|
|
ck_free(dfn);
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (st.st_size > MAX_FILE)
|
|
|
FATAL("Test case '%s' is too big (%s, limit is %s)", fn,
|
|
|
DMS(st.st_size), DMS(MAX_FILE));
|
|
|
|
|
|
/* 检查元数据,以确定是否已经完成了这项条目的确定性模糊测试。
|
|
|
我们不想在恢复中断的扫描时重复进行确定性模糊测试,因为这样做毫无意义,而且可能非常耗时。 */
|
|
|
|
|
|
if (!access(dfn, F_OK)) passed_det = 1;
|
|
|
ck_free(dfn);
|
|
|
|
|
|
add_to_queue(fn, st.st_size, passed_det);
|
|
|
|
|
|
}
|
|
|
|
|
|
free(nl); /* not tracked */
|
|
|
|
|
|
if (!queued_paths) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Looks like there are no valid test cases in the input directory! The fuzzer\n"
|
|
|
" needs one or more test case to start with - ideally, a small file under\n"
|
|
|
" 1 kB or so. The cases must be stored as regular files directly in the\n"
|
|
|
" input directory.\n");
|
|
|
|
|
|
FATAL("No usable test cases in '%s'", in_dir);
|
|
|
|
|
|
}
|
|
|
|
|
|
last_path_time = 0;
|
|
|
queued_at_start = queued_paths;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* load_extras的辅助函数 */
|
|
|
|
|
|
static int compare_extras_len(const void* p1, const void* p2) {
|
|
|
struct extra_data *e1 = (struct extra_data*)p1,
|
|
|
*e2 = (struct extra_data*)p2;
|
|
|
|
|
|
return e1->len - e2->len;
|
|
|
}
|
|
|
|
|
|
static int compare_extras_use_d(const void* p1, const void* p2) {
|
|
|
struct extra_data *e1 = (struct extra_data*)p1,
|
|
|
*e2 = (struct extra_data*)p2;
|
|
|
|
|
|
return e2->hit_cnt - e1->hit_cnt;
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 从文件中读取额外内容,按大小排序。 */
|
|
|
|
|
|
static void load_extras_file(u8* fname, u32* min_len, u32* max_len,
|
|
|
u32 dict_level) {
|
|
|
|
|
|
FILE* f;
|
|
|
u8 buf[MAX_LINE];
|
|
|
u8 *lptr;
|
|
|
u32 cur_line = 0;
|
|
|
|
|
|
f = fopen(fname, "r");
|
|
|
|
|
|
if (!f) PFATAL("Unable to open '%s'", fname);
|
|
|
|
|
|
while ((lptr = fgets(buf, MAX_LINE, f))) {
|
|
|
|
|
|
u8 *rptr, *wptr;
|
|
|
u32 klen = 0;
|
|
|
|
|
|
cur_line++;
|
|
|
|
|
|
/* 修剪左右 */
|
|
|
|
|
|
while (isspace(*lptr)) lptr++;
|
|
|
|
|
|
rptr = lptr + strlen(lptr) - 1;
|
|
|
while (rptr >= lptr && isspace(*rptr)) rptr--;
|
|
|
rptr++;
|
|
|
*rptr = 0;
|
|
|
|
|
|
/* 跳过空行和注释 */
|
|
|
|
|
|
if (!*lptr || *lptr == '#') continue;
|
|
|
|
|
|
/* 所有其他行必须以‘ " ’结尾,我们可以使用它。 */
|
|
|
|
|
|
rptr--;
|
|
|
|
|
|
if (rptr < lptr || *rptr != '"')
|
|
|
FATAL("Malformed name=\"value\" pair in line %u.", cur_line);
|
|
|
|
|
|
*rptr = 0;
|
|
|
|
|
|
/* 跳过字母数字和破折号(标签) */
|
|
|
|
|
|
while (isalnum(*lptr) || *lptr == '_') lptr++;
|
|
|
|
|
|
/* 如果后面跟着@number,解析它 */
|
|
|
|
|
|
if (*lptr == '@') {
|
|
|
|
|
|
lptr++;
|
|
|
if (atoi(lptr) > dict_level) continue;
|
|
|
while (isdigit(*lptr)) lptr++;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 跳过空格和=号 */
|
|
|
|
|
|
while (isspace(*lptr) || *lptr == '=') lptr++;
|
|
|
|
|
|
/* 消费开放‘ " ’ 。 */
|
|
|
|
|
|
if (*lptr != '"')
|
|
|
FATAL("Malformed name=\"keyword\" pair in line %u.", cur_line);
|
|
|
|
|
|
lptr++;
|
|
|
|
|
|
if (!*lptr) FATAL("Empty keyword in line %u.", cur_line);
|
|
|
|
|
|
/* 好的,我们来分配内存并在 "..." 之间复制数据,同时处理 \xNN 转义、\ 和 "*/
|
|
|
|
|
|
extras = ck_realloc_block(extras, (extras_cnt + 1) *
|
|
|
sizeof(struct extra_data));
|
|
|
|
|
|
wptr = extras[extras_cnt].data = ck_alloc(rptr - lptr);
|
|
|
|
|
|
while (*lptr) {
|
|
|
|
|
|
char* hexdigits = "0123456789abcdef";
|
|
|
|
|
|
switch (*lptr) {
|
|
|
|
|
|
case 1 ... 31:
|
|
|
case 128 ... 255:
|
|
|
FATAL("Non-printable characters in line %u.", cur_line);
|
|
|
|
|
|
case '\\':
|
|
|
|
|
|
lptr++;
|
|
|
|
|
|
if (*lptr == '\\' || *lptr == '"') {
|
|
|
*(wptr++) = *(lptr++);
|
|
|
klen++;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (*lptr != 'x' || !isxdigit(lptr[1]) || !isxdigit(lptr[2]))
|
|
|
FATAL("Invalid escaping (not \\xNN) in line %u.", cur_line);
|
|
|
|
|
|
*(wptr++) =
|
|
|
((strchr(hexdigits, tolower(lptr[1])) - hexdigits) << 4) |
|
|
|
(strchr(hexdigits, tolower(lptr[2])) - hexdigits);
|
|
|
|
|
|
lptr += 3;
|
|
|
klen++;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
*(wptr++) = *(lptr++);
|
|
|
klen++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
extras[extras_cnt].len = klen;
|
|
|
|
|
|
if (extras[extras_cnt].len > MAX_DICT_FILE)
|
|
|
FATAL("Keyword too big in line %u (%s, limit is %s)", cur_line,
|
|
|
DMS(klen), DMS(MAX_DICT_FILE));
|
|
|
|
|
|
if (*min_len > klen) *min_len = klen;
|
|
|
if (*max_len < klen) *max_len = klen;
|
|
|
|
|
|
extras_cnt++;
|
|
|
|
|
|
}
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 从extras目录中读取extras,并按大小排序。 */
|
|
|
|
|
|
static void load_extras(u8* dir) {
|
|
|
|
|
|
DIR* d;
|
|
|
struct dirent* de;
|
|
|
u32 min_len = MAX_DICT_FILE, max_len = 0, dict_level = 0;
|
|
|
u8* x;
|
|
|
|
|
|
/* 如果名称以@结尾,则提取level并继续 */
|
|
|
|
|
|
if ((x = strchr(dir, '@'))) {
|
|
|
|
|
|
*x = 0;
|
|
|
dict_level = atoi(x + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
ACTF("Loading extra dictionary from '%s' (level %u)...", dir, dict_level);
|
|
|
|
|
|
d = opendir(dir);
|
|
|
|
|
|
if (!d) {
|
|
|
|
|
|
if (errno == ENOTDIR) {
|
|
|
load_extras_file(dir, &min_len, &max_len, dict_level);
|
|
|
goto check_and_sort;
|
|
|
}
|
|
|
|
|
|
PFATAL("Unable to open '%s'", dir);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (x) FATAL("Dictionary levels not supported for directories.");
|
|
|
|
|
|
while ((de = readdir(d))) {
|
|
|
|
|
|
struct stat st;
|
|
|
u8* fn = alloc_printf("%s/%s", dir, de->d_name);
|
|
|
s32 fd;
|
|
|
|
|
|
if (lstat(fn, &st) || access(fn, R_OK))
|
|
|
PFATAL("Unable to access '%s'", fn);
|
|
|
|
|
|
/* 这也照顾.和. . */
|
|
|
if (!S_ISREG(st.st_mode) || !st.st_size) {
|
|
|
|
|
|
ck_free(fn);
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (st.st_size > MAX_DICT_FILE)
|
|
|
FATAL("Extra '%s' is too big (%s, limit is %s)", fn,
|
|
|
DMS(st.st_size), DMS(MAX_DICT_FILE));
|
|
|
|
|
|
if (min_len > st.st_size) min_len = st.st_size;
|
|
|
if (max_len < st.st_size) max_len = st.st_size;
|
|
|
|
|
|
extras = ck_realloc_block(extras, (extras_cnt + 1) *
|
|
|
sizeof(struct extra_data));
|
|
|
|
|
|
extras[extras_cnt].data = ck_alloc(st.st_size);
|
|
|
extras[extras_cnt].len = st.st_size;
|
|
|
|
|
|
fd = open(fn, O_RDONLY);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to open '%s'", fn);
|
|
|
|
|
|
ck_read(fd, extras[extras_cnt].data, st.st_size, fn);
|
|
|
|
|
|
close(fd);
|
|
|
ck_free(fn);
|
|
|
|
|
|
extras_cnt++;
|
|
|
|
|
|
}
|
|
|
|
|
|
closedir(d);
|
|
|
|
|
|
check_and_sort:
|
|
|
|
|
|
if (!extras_cnt) FATAL("No usable files in '%s'", dir);
|
|
|
|
|
|
qsort(extras, extras_cnt, sizeof(struct extra_data), compare_extras_len);
|
|
|
|
|
|
OKF("Loaded %u extra tokens, size range %s to %s.", extras_cnt,
|
|
|
DMS(min_len), DMS(max_len));
|
|
|
|
|
|
if (max_len > 32)
|
|
|
WARNF("Some tokens are relatively large (%s) - consider trimming.",
|
|
|
DMS(max_len));
|
|
|
|
|
|
if (extras_cnt > MAX_DET_EXTRAS)
|
|
|
WARNF("More than %u tokens - will use them probabilistically.",
|
|
|
MAX_DET_EXTRAS);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* maybe_add_auto()的辅助函数 */
|
|
|
|
|
|
static inline u8 memcmp_nocase(u8* m1, u8* m2, u32 len) {
|
|
|
|
|
|
while (len--) if (tolower(*(m1++)) ^ tolower(*(m2++))) return 1;
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 也许可以加上自动附加功能 */
|
|
|
|
|
|
static void maybe_add_auto(u8* mem, u32 len) {
|
|
|
|
|
|
u32 i;
|
|
|
|
|
|
/* 允许用户指定他们不需要自动字典 */
|
|
|
|
|
|
if (!MAX_AUTO_EXTRAS || !USE_AUTO_EXTRAS) return;
|
|
|
|
|
|
/* 跳过相同字节的运行 */
|
|
|
|
|
|
for (i = 1; i < len; i++)
|
|
|
if (mem[0] ^ mem[i]) break;
|
|
|
|
|
|
if (i == len) return;
|
|
|
|
|
|
/* 拒绝内置有趣的值 */
|
|
|
|
|
|
if (len == 2) {
|
|
|
|
|
|
i = sizeof(interesting_16) >> 1;
|
|
|
|
|
|
while (i--)
|
|
|
if (*((u16*)mem) == interesting_16[i] ||
|
|
|
*((u16*)mem) == SWAP16(interesting_16[i])) return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (len == 4) {
|
|
|
|
|
|
i = sizeof(interesting_32) >> 2;
|
|
|
|
|
|
while (i--)
|
|
|
if (*((u32*)mem) == interesting_32[i] ||
|
|
|
*((u32*)mem) == SWAP32(interesting_32[i])) return;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 拒绝任何与现有额外内容匹配的条目。进行不区分大小写的匹配。我们通过利用extras[]按大小排序的事实来优化这个过程。 */
|
|
|
|
|
|
for (i = 0; i < extras_cnt; i++)
|
|
|
if (extras[i].len >= len) break;
|
|
|
|
|
|
for (; i < extras_cnt && extras[i].len == len; i++)
|
|
|
if (!memcmp_nocase(extras[i].data, mem, len)) return;
|
|
|
|
|
|
/* 最后但同样重要的是,检查 a_extras[] 中是否有匹配项。没有保证特定的排序顺序。 */
|
|
|
|
|
|
auto_changed = 1;
|
|
|
|
|
|
for (i = 0; i < a_extras_cnt; i++) {
|
|
|
|
|
|
if (a_extras[i].len == len && !memcmp_nocase(a_extras[i].data, mem, len)) {
|
|
|
|
|
|
a_extras[i].hit_cnt++;
|
|
|
goto sort_a_extras;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 在这一点上,看起来我们正在处理一个新的条目。所以,如果有空间的话,
|
|
|
让我们把它追加上去。否则,让我们从列表的下半部分随机驱逐其他某个条目。 */
|
|
|
|
|
|
if (a_extras_cnt < MAX_AUTO_EXTRAS) {
|
|
|
|
|
|
a_extras = ck_realloc_block(a_extras, (a_extras_cnt + 1) *
|
|
|
sizeof(struct extra_data));
|
|
|
|
|
|
a_extras[a_extras_cnt].data = ck_memdup(mem, len);
|
|
|
a_extras[a_extras_cnt].len = len;
|
|
|
a_extras_cnt++;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
i = MAX_AUTO_EXTRAS / 2 +
|
|
|
UR((MAX_AUTO_EXTRAS + 1) / 2);
|
|
|
|
|
|
ck_free(a_extras[i].data);
|
|
|
|
|
|
a_extras[i].data = ck_memdup(mem, len);
|
|
|
a_extras[i].len = len;
|
|
|
a_extras[i].hit_cnt = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
sort_a_extras:
|
|
|
|
|
|
/* 首先,按使用次数降序对所有自动附加项进行排序 */
|
|
|
|
|
|
qsort(a_extras, a_extras_cnt, sizeof(struct extra_data),
|
|
|
compare_extras_use_d);
|
|
|
|
|
|
/* 然后,按大小对最上面的USE_AUTO_EXTRAS条目进行排序 */
|
|
|
|
|
|
qsort(a_extras, MIN(USE_AUTO_EXTRAS, a_extras_cnt),
|
|
|
sizeof(struct extra_data), compare_extras_len);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 保存自动生成的附加内容 */
|
|
|
|
|
|
static void save_auto(void) {
|
|
|
|
|
|
u32 i;
|
|
|
|
|
|
if (!auto_changed) return;
|
|
|
auto_changed = 0;
|
|
|
|
|
|
for (i = 0; i < MIN(USE_AUTO_EXTRAS, a_extras_cnt); i++) {
|
|
|
|
|
|
u8* fn = alloc_printf("%s/queue/.state/auto_extras/auto_%06u", out_dir, i);
|
|
|
s32 fd;
|
|
|
|
|
|
fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", fn);
|
|
|
|
|
|
ck_write(fd, a_extras[i].data, a_extras[i].len, fn);
|
|
|
|
|
|
close(fd);
|
|
|
ck_free(fn);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 加载自动生成的附加内容 */
|
|
|
|
|
|
static void load_auto(void) {
|
|
|
|
|
|
u32 i;
|
|
|
|
|
|
for (i = 0; i < USE_AUTO_EXTRAS; i++) {
|
|
|
|
|
|
u8 tmp[MAX_AUTO_EXTRA + 1];
|
|
|
u8* fn = alloc_printf("%s/.state/auto_extras/auto_%06u", in_dir, i);
|
|
|
s32 fd, len;
|
|
|
|
|
|
fd = open(fn, O_RDONLY, 0600);
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
|
if (errno != ENOENT) PFATAL("Unable to open '%s'", fn);
|
|
|
ck_free(fn);
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 我们多读了一个字节,以低成本检测过长的标记(并跳过它们) */
|
|
|
|
|
|
len = read(fd, tmp, MAX_AUTO_EXTRA + 1);
|
|
|
|
|
|
if (len < 0) PFATAL("Unable to read from '%s'", fn);
|
|
|
|
|
|
if (len >= MIN_AUTO_EXTRA && len <= MAX_AUTO_EXTRA)
|
|
|
maybe_add_auto(tmp, len);
|
|
|
|
|
|
close(fd);
|
|
|
ck_free(fn);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (i) OKF("Loaded %u auto-discovered dictionary tokens.", i);
|
|
|
else OKF("No auto-generated dictionary tokens to reuse.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 销毁额外的数据 */
|
|
|
|
|
|
static void destroy_extras(void) {
|
|
|
|
|
|
u32 i;
|
|
|
|
|
|
for (i = 0; i < extras_cnt; i++)
|
|
|
ck_free(extras[i].data);
|
|
|
|
|
|
ck_free(extras);
|
|
|
|
|
|
for (i = 0; i < a_extras_cnt; i++)
|
|
|
ck_free(a_extras[i].data);
|
|
|
|
|
|
ck_free(a_extras);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 启动fork服务器(仅在插桩模式下)。这个想法在这里有解释:
|
|
|
|
|
|
http://lcamtuf.blogspot.com/2014/10/fuzzing-binaries-without-execve.html
|
|
|
|
|
|
本质上,插桩允许我们跳过execve(),只需持续克隆一个已停止的子进程。
|
|
|
因此,我们只需要执行一次,然后通过管道发送命令。这部分逻辑的另一部分在afl-as.h中。 */
|
|
|
|
|
|
EXP_ST void init_forkserver(char** argv) {
|
|
|
|
|
|
static struct itimerval it;
|
|
|
int st_pipe[2], ctl_pipe[2];
|
|
|
int status;
|
|
|
s32 rlen;
|
|
|
|
|
|
ACTF("Spinning up the fork server...");
|
|
|
|
|
|
if (pipe(st_pipe) || pipe(ctl_pipe)) PFATAL("pipe() failed");
|
|
|
|
|
|
forksrv_pid = fork();
|
|
|
|
|
|
if (forksrv_pid < 0) PFATAL("fork() failed");
|
|
|
|
|
|
if (!forksrv_pid) {
|
|
|
|
|
|
struct rlimit r;
|
|
|
|
|
|
/* 在OpenBSD上,默认的文件描述符(fd)限制对于root用户设置为软限制128。让我们尝试修复这个问题... */
|
|
|
|
|
|
if (!getrlimit(RLIMIT_NOFILE, &r) && r.rlim_cur < FORKSRV_FD + 2) {
|
|
|
|
|
|
r.rlim_cur = FORKSRV_FD + 2;
|
|
|
setrlimit(RLIMIT_NOFILE, &r); /* 忽略错误 */
|
|
|
|
|
|
}
|
|
|
|
|
|
if (mem_limit) {
|
|
|
|
|
|
r.rlim_max = r.rlim_cur = ((rlim_t)mem_limit) << 20;
|
|
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
|
|
|
setrlimit(RLIMIT_AS, &r); /* 忽略错误 */
|
|
|
|
|
|
#else
|
|
|
|
|
|
/* 这考虑到了OpenBSD,它没有RLIMIT_AS,但根据可靠来源,
|
|
|
RLIMIT_DATA涵盖了匿名映射——因此我们应该能够很好地防止内存溢出(OOM)错误。 */
|
|
|
|
|
|
setrlimit(RLIMIT_DATA, &r); /* Ignore errors */
|
|
|
|
|
|
#endif /* ^RLIMIT_AS */
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 转储核心(core dumping)是缓慢的,如果在转储完成之前发送了SIGKILL信号,可能会导致异常。 */
|
|
|
|
|
|
r.rlim_max = r.rlim_cur = 0;
|
|
|
|
|
|
setrlimit(RLIMIT_CORE, &r); /* Ignore errors */
|
|
|
|
|
|
/* 隔离进程并配置标准描述符。如果指定了out_file,那么stdin就是/dev/null;否则,会克隆out_fd。*/
|
|
|
|
|
|
setsid();
|
|
|
|
|
|
dup2(dev_null_fd, 1);
|
|
|
dup2(dev_null_fd, 2);
|
|
|
|
|
|
if (out_file) {
|
|
|
|
|
|
dup2(dev_null_fd, 0);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
dup2(out_fd, 0);
|
|
|
close(out_fd);
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 建立控制和状态管道,关闭不需要的原始文件。 */
|
|
|
|
|
|
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]);
|
|
|
|
|
|
close(out_dir_fd);
|
|
|
close(dev_null_fd);
|
|
|
close(dev_urandom_fd);
|
|
|
close(fileno(plot_file));
|
|
|
|
|
|
/* 这应该会稍微提高性能,因为它阻止了链接器在fork()之后进行额外的工作。 */
|
|
|
|
|
|
if (!getenv("LD_BIND_LAZY")) setenv("LD_BIND_NOW", "1", 0);
|
|
|
|
|
|
/* 如果没有其他指定,则为ASAN设置相同的默认值。 */
|
|
|
|
|
|
setenv("ASAN_OPTIONS", "abort_on_error=1:"
|
|
|
"detect_leaks=0:"
|
|
|
"symbolize=0:"
|
|
|
"allocator_may_return_null=1", 0);
|
|
|
|
|
|
/* MSAN(MemorySanitizer)有点棘手,因为目前它不支持abort_on_error=1这个选项。
|
|
|
因此,我们用一种非常hacky(即临时、不正规)的方式来实现这一点。 */
|
|
|
|
|
|
setenv("MSAN_OPTIONS", "exit_code=" STRINGIFY(MSAN_ERROR) ":"
|
|
|
"symbolize=0:"
|
|
|
"abort_on_error=1:"
|
|
|
"allocator_may_return_null=1:"
|
|
|
"msan_track_origins=0", 0);
|
|
|
|
|
|
execv(target_path, argv);
|
|
|
|
|
|
/* 使用一个独特的位图签名来告诉父进程execv()调用已经成功执行。*/
|
|
|
|
|
|
*(u32*)trace_bits = EXEC_FAIL_SIG;
|
|
|
exit(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 关闭不需要的端点 */
|
|
|
|
|
|
close(ctl_pipe[0]);
|
|
|
close(st_pipe[1]);
|
|
|
|
|
|
fsrv_ctl_fd = ctl_pipe[1];
|
|
|
fsrv_st_fd = st_pipe[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);
|
|
|
|
|
|
/* 如果我们从服务器收到了一个四字节的'hello'消息,那么一切就绪。否则,尝试弄清楚出了什么问题。 */
|
|
|
|
|
|
if (rlen == 4) {
|
|
|
OKF("All right - fork server is up.");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (child_timed_out)
|
|
|
FATAL("Timeout while initializing fork server (adjusting -t may help)");
|
|
|
|
|
|
if (waitpid(forksrv_pid, &status, 0) <= 0)
|
|
|
PFATAL("waitpid() failed");
|
|
|
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
|
|
if (mem_limit && mem_limit < 500 && uses_asan) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Whoops, the target binary crashed suddenly, before receiving any input\n"
|
|
|
" from the fuzzer! Since it seems to be built with ASAN and you have a\n"
|
|
|
" restrictive memory limit configured, this is expected; please read\n"
|
|
|
" %s/notes_for_asan.txt for help.\n", doc_path);
|
|
|
|
|
|
} else if (!mem_limit) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Whoops, the target binary crashed suddenly, before receiving any input\n"
|
|
|
" from the fuzzer! There are several probable explanations:\n\n"
|
|
|
|
|
|
" - The binary is just buggy and explodes entirely on its own. If so, you\n"
|
|
|
" need to fix the underlying problem or find a better replacement.\n\n"
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
" - On MacOS X, the semantics of fork() syscalls are non-standard and may\n"
|
|
|
" break afl-fuzz performance optimizations when running platform-specific\n"
|
|
|
" targets. To fix this, set AFL_NO_FORKSRV=1 in the environment.\n\n"
|
|
|
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
|
" - Less likely, there is a horrible bug in the fuzzer. If other options\n"
|
|
|
" fail, poke <lcamtuf@coredump.cx> for troubleshooting tips.\n");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Whoops, the target binary crashed suddenly, before receiving any input\n"
|
|
|
" from the fuzzer! There are several probable explanations:\n\n"
|
|
|
|
|
|
" - The current memory limit (%s) is too restrictive, causing the\n"
|
|
|
" target to hit an OOM condition in the dynamic linker. Try bumping up\n"
|
|
|
" the limit with the -m setting in the command line. A simple way confirm\n"
|
|
|
" this diagnosis would be:\n\n"
|
|
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
" ( ulimit -Sv $[%llu << 10]; /path/to/fuzzed_app )\n\n"
|
|
|
#else
|
|
|
" ( ulimit -Sd $[%llu << 10]; /path/to/fuzzed_app )\n\n"
|
|
|
#endif /* ^RLIMIT_AS */
|
|
|
|
|
|
" Tip: you can use http://jwilk.net/software/recidivm to quickly\n"
|
|
|
" estimate the required amount of virtual memory for the binary.\n\n"
|
|
|
|
|
|
" - The binary is just buggy and explodes entirely on its own. If so, you\n"
|
|
|
" need to fix the underlying problem or find a better replacement.\n\n"
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
" - On MacOS X, the semantics of fork() syscalls are non-standard and may\n"
|
|
|
" break afl-fuzz performance optimizations when running platform-specific\n"
|
|
|
" targets. To fix this, set AFL_NO_FORKSRV=1 in the environment.\n\n"
|
|
|
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
|
" - Less likely, there is a horrible bug in the fuzzer. If other options\n"
|
|
|
" fail, poke <lcamtuf@coredump.cx> for troubleshooting tips.\n",
|
|
|
DMS(mem_limit << 20), mem_limit - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
FATAL("Fork server crashed with signal %d", WTERMSIG(status));
|
|
|
|
|
|
}
|
|
|
|
|
|
if (*(u32*)trace_bits == EXEC_FAIL_SIG)
|
|
|
FATAL("Unable to execute target application ('%s')", argv[0]);
|
|
|
|
|
|
if (mem_limit && mem_limit < 500 && uses_asan) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Hmm, looks like the target binary terminated before we could complete a\n"
|
|
|
" handshake with the injected code. Since it seems to be built with ASAN and\n"
|
|
|
" you have a restrictive memory limit configured, this is expected; please\n"
|
|
|
" read %s/notes_for_asan.txt for help.\n", doc_path);
|
|
|
|
|
|
} else if (!mem_limit) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Hmm, looks like the target binary terminated before we could complete a\n"
|
|
|
" handshake with the injected code. Perhaps there is a horrible bug in the\n"
|
|
|
" fuzzer. Poke <lcamtuf@coredump.cx> for troubleshooting tips.\n");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Hmm, looks like the target binary terminated before we could complete a\n"
|
|
|
" handshake with the injected code. There are %s probable explanations:\n\n"
|
|
|
|
|
|
"%s"
|
|
|
" - The current memory limit (%s) is too restrictive, causing an OOM\n"
|
|
|
" fault in the dynamic linker. This can be fixed with the -m option. A\n"
|
|
|
" simple way to confirm the diagnosis may be:\n\n"
|
|
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
" ( ulimit -Sv $[%llu << 10]; /path/to/fuzzed_app )\n\n"
|
|
|
#else
|
|
|
" ( ulimit -Sd $[%llu << 10]; /path/to/fuzzed_app )\n\n"
|
|
|
#endif /* ^RLIMIT_AS */
|
|
|
|
|
|
" Tip: you can use http://jwilk.net/software/recidivm to quickly\n"
|
|
|
" estimate the required amount of virtual memory for the binary.\n\n"
|
|
|
|
|
|
" - Less likely, there is a horrible bug in the fuzzer. If other options\n"
|
|
|
" fail, poke <lcamtuf@coredump.cx> for troubleshooting tips.\n",
|
|
|
getenv(DEFER_ENV_VAR) ? "three" : "two",
|
|
|
getenv(DEFER_ENV_VAR) ?
|
|
|
" - You are using deferred forkserver, but __AFL_INIT() is never\n"
|
|
|
" reached before the program terminates.\n\n" : "",
|
|
|
DMS(mem_limit << 20), mem_limit - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
FATAL("Fork server handshake failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 执行目标应用程序,监控超时情况。返回状态信息。被调用的程序将更新trace_bits[] */
|
|
|
|
|
|
static u8 run_target(char** argv, u32 timeout) {
|
|
|
|
|
|
static struct itimerval it;
|
|
|
static u32 prev_timed_out = 0;
|
|
|
static u64 exec_ms = 0;
|
|
|
|
|
|
int status = 0;
|
|
|
u32 tb4;
|
|
|
|
|
|
child_timed_out = 0;
|
|
|
|
|
|
/* 在这次memset之后,trace_bits[]实际上变成了易失性的,因此我们必须阻止任何早期操作进入该领域。 */
|
|
|
|
|
|
memset(trace_bits, 0, MAP_SIZE);
|
|
|
MEM_BARRIER();
|
|
|
|
|
|
/* 如果我们在‘dump’模式下运行,我们不能依赖于目标程序中编译的fork服务器逻辑,所以我们将不断调用execve()。
|
|
|
这里的代码和init_forkserver()之间有一些重复,但这就是生活。 */
|
|
|
|
|
|
if (dumb_mode == 1 || no_forkserver) {
|
|
|
|
|
|
child_pid = fork();
|
|
|
|
|
|
if (child_pid < 0) PFATAL("fork() failed");
|
|
|
|
|
|
if (!child_pid) {
|
|
|
|
|
|
struct rlimit r;
|
|
|
|
|
|
if (mem_limit) {
|
|
|
|
|
|
r.rlim_max = r.rlim_cur = ((rlim_t)mem_limit) << 20;
|
|
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
|
|
|
setrlimit(RLIMIT_AS, &r); /* 忽略错误 */
|
|
|
|
|
|
#else
|
|
|
|
|
|
setrlimit(RLIMIT_DATA, &r); /* 忽略错误 */
|
|
|
|
|
|
#endif /* ^RLIMIT_AS */
|
|
|
|
|
|
}
|
|
|
|
|
|
r.rlim_max = r.rlim_cur = 0;
|
|
|
|
|
|
setrlimit(RLIMIT_CORE, &r); /* 忽略错误 */
|
|
|
|
|
|
/* 隔离进程并配置标准描述符。如果指定了out_file,那么stdin就是/dev/null;否则,将克隆out_fd。 */
|
|
|
|
|
|
setsid();
|
|
|
|
|
|
dup2(dev_null_fd, 1);
|
|
|
dup2(dev_null_fd, 2);
|
|
|
|
|
|
if (out_file) {
|
|
|
|
|
|
dup2(dev_null_fd, 0);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
dup2(out_fd, 0);
|
|
|
close(out_fd);
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 在Linux上,使用O_CLOEXEC会更快。也许TODO */
|
|
|
|
|
|
close(dev_null_fd);
|
|
|
close(out_dir_fd);
|
|
|
close(dev_urandom_fd);
|
|
|
close(fileno(plot_file));
|
|
|
|
|
|
/* 如果没有其他指定,则为ASAN设置相同的默认值 */
|
|
|
|
|
|
setenv("ASAN_OPTIONS", "abort_on_error=1:"
|
|
|
"detect_leaks=0:"
|
|
|
"symbolize=0:"
|
|
|
"allocator_may_return_null=1", 0);
|
|
|
|
|
|
setenv("MSAN_OPTIONS", "exit_code=" STRINGIFY(MSAN_ERROR) ":"
|
|
|
"symbolize=0:"
|
|
|
"msan_track_origins=0", 0);
|
|
|
|
|
|
execv(target_path, argv);
|
|
|
|
|
|
/* 使用一个独特的位图值来告知父进程execv()调用已经顺利执行 */
|
|
|
|
|
|
*(u32*)trace_bits = EXEC_FAIL_SIG;
|
|
|
exit(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
s32 res;
|
|
|
|
|
|
/* 在non-dump模式下,我们的fork服务器正在运行,所以只需告诉它开始执行,然后读取返回的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?)");
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 根据用户的要求配置超时,然后等待子进程终止 */
|
|
|
|
|
|
it.it_value.tv_sec = (timeout / 1000);
|
|
|
it.it_value.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
|
|
setitimer(ITIMER_REAL, &it, NULL);
|
|
|
|
|
|
/* SIGALRM处理程序只是终止child_pid并设置child_timed_out */
|
|
|
|
|
|
if (dumb_mode == 1 || no_forkserver) {
|
|
|
|
|
|
if (waitpid(child_pid, &status, 0) <= 0) PFATAL("waitpid() failed");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
s32 res;
|
|
|
|
|
|
if ((res = read(fsrv_st_fd, &status, 4)) != 4) {
|
|
|
|
|
|
if (stop_soon) return 0;
|
|
|
RPFATAL(res, "Unable to communicate with fork server (OOM?)");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!WIFSTOPPED(status)) child_pid = 0;
|
|
|
|
|
|
getitimer(ITIMER_REAL, &it);
|
|
|
exec_ms = (u64) timeout - (it.it_value.tv_sec * 1000 +
|
|
|
it.it_value.tv_usec / 1000);
|
|
|
|
|
|
it.it_value.tv_sec = 0;
|
|
|
it.it_value.tv_usec = 0;
|
|
|
|
|
|
setitimer(ITIMER_REAL, &it, NULL);
|
|
|
|
|
|
total_execs++;
|
|
|
|
|
|
/* 编译器不能将对trace_bits的任何后续操作移动到这一点以下。
|
|
|
在这个位置之后,trace_bits[]表现得非常正常,不需要被当作易失性的处理。 */
|
|
|
|
|
|
MEM_BARRIER();
|
|
|
|
|
|
tb4 = *(u32*)trace_bits;
|
|
|
|
|
|
#ifdef WORD_SIZE_64
|
|
|
classify_counts((u64*)trace_bits);
|
|
|
#else
|
|
|
classify_counts((u32*)trace_bits);
|
|
|
#endif /* ^WORD_SIZE_64 */
|
|
|
|
|
|
prev_timed_out = child_timed_out;
|
|
|
|
|
|
/* 向调用者报告结果 */
|
|
|
|
|
|
if (WIFSIGNALED(status) && !stop_soon) {
|
|
|
|
|
|
kill_signal = WTERMSIG(status);
|
|
|
|
|
|
if (child_timed_out && kill_signal == SIGKILL) return FAULT_TMOUT;
|
|
|
|
|
|
return FAULT_CRASH;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 这是针对MSAN的一个有点恶劣的hack,因为MSAN不支持abort_on_error,必须使用一个特殊的退出代码。 */
|
|
|
|
|
|
if (uses_asan && WEXITSTATUS(status) == MSAN_ERROR) {
|
|
|
kill_signal = 0;
|
|
|
return FAULT_CRASH;
|
|
|
}
|
|
|
|
|
|
if ((dumb_mode == 1 || no_forkserver) && tb4 == EXEC_FAIL_SIG)
|
|
|
return FAULT_ERROR;
|
|
|
|
|
|
/* 只有在测试用例在用户定义的超时下运行时,才有必要只考虑最慢的单元 */
|
|
|
if (!(timeout > exec_tmout) && (slowest_exec_ms < exec_ms)) {
|
|
|
slowest_exec_ms = exec_ms;
|
|
|
}
|
|
|
|
|
|
return FAULT_NONE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 将修改后的数据写入文件以进行测试。如果设置了out_file,旧文件将被解除链接,并创建一个新的文件。否则,out_fd将被回卷并截断。 */
|
|
|
|
|
|
static void write_to_testcase(void* mem, u32 len) {
|
|
|
|
|
|
s32 fd = out_fd;
|
|
|
|
|
|
if (out_file) {
|
|
|
|
|
|
unlink(out_file); /* Ignore errors. */
|
|
|
|
|
|
fd = open(out_file, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", out_file);
|
|
|
|
|
|
} else lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
ck_write(fd, mem, len, out_file);
|
|
|
|
|
|
if (!out_file) {
|
|
|
|
|
|
if (ftruncate(fd, len)) PFATAL("ftruncate() failed");
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
} else close(fd);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 相同的,但有一个可调节的间隙。用于修剪。 */
|
|
|
|
|
|
static void write_with_gap(void* mem, u32 len, u32 skip_at, u32 skip_len) {
|
|
|
|
|
|
s32 fd = out_fd;
|
|
|
u32 tail_len = len - skip_at - skip_len;
|
|
|
|
|
|
if (out_file) {
|
|
|
|
|
|
unlink(out_file); /* Ignore errors. */
|
|
|
|
|
|
fd = open(out_file, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", out_file);
|
|
|
|
|
|
} else lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
if (skip_at) ck_write(fd, mem, skip_at, out_file);
|
|
|
|
|
|
if (tail_len) ck_write(fd, mem + skip_at + skip_len, tail_len, out_file);
|
|
|
|
|
|
if (!out_file) {
|
|
|
|
|
|
if (ftruncate(fd, len - skip_len)) PFATAL("ftruncate() failed");
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
} else close(fd);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
static void show_stats(void);
|
|
|
|
|
|
/* 校准一个新的测试用例。这在处理输入目录时完成,目的是为了提早警告
|
|
|
关于不稳定或其他有问题的测试用例;以及在发现新路径时,用于检测可变行为等。 */
|
|
|
|
|
|
static u8 calibrate_case(char** argv, struct queue_entry* q, u8* use_mem,
|
|
|
u32 handicap, u8 from_queue) {
|
|
|
|
|
|
static u8 first_trace[MAP_SIZE];
|
|
|
|
|
|
u8 fault = 0, new_bits = 0, var_detected = 0, hnb = 0,
|
|
|
first_run = (q->exec_cksum == 0);
|
|
|
|
|
|
u64 start_us, stop_us;
|
|
|
|
|
|
s32 old_sc = stage_cur, old_sm = stage_max;
|
|
|
u32 use_tmout = exec_tmout;
|
|
|
u8* old_sn = stage_name;
|
|
|
|
|
|
/* 在恢复会话或尝试校准已经添加的发现时,对超时更加宽容一些。
|
|
|
这有助于避免由于间歇性延迟引起的问题。 */
|
|
|
|
|
|
if (!from_queue || resuming_fuzz)
|
|
|
use_tmout = MAX(exec_tmout + CAL_TMOUT_ADD,
|
|
|
exec_tmout * CAL_TMOUT_PERC / 100);
|
|
|
|
|
|
q->cal_failed++;
|
|
|
|
|
|
stage_name = "calibration";
|
|
|
stage_max = fast_cal ? 3 : CAL_CYCLES;
|
|
|
|
|
|
/* 在我们做任何事情之前,确保fork服务器已经启动,并且我们不将其启动时间计入二进制校准。 */
|
|
|
|
|
|
if (dumb_mode != 1 && !no_forkserver && !forksrv_pid)
|
|
|
init_forkserver(argv);
|
|
|
|
|
|
if (q->exec_cksum) {
|
|
|
|
|
|
memcpy(first_trace, trace_bits, MAP_SIZE);
|
|
|
hnb = has_new_bits(virgin_bits);
|
|
|
if (hnb > new_bits) new_bits = hnb;
|
|
|
|
|
|
}
|
|
|
|
|
|
start_us = get_cur_time_us();
|
|
|
|
|
|
for (stage_cur = 0; stage_cur < stage_max; stage_cur++) {
|
|
|
|
|
|
u32 cksum;
|
|
|
|
|
|
if (!first_run && !(stage_cur % stats_update_freq)) show_stats();
|
|
|
|
|
|
write_to_testcase(use_mem, q->len);
|
|
|
|
|
|
fault = run_target(argv, use_tmout);
|
|
|
|
|
|
/* 如果按下Ctrl+C,stop_soon会被设置,我们希望快速退出。 */
|
|
|
|
|
|
if (stop_soon || fault != crash_mode) goto abort_calibration;
|
|
|
|
|
|
if (!dumb_mode && !stage_cur && !count_bytes(trace_bits)) {
|
|
|
fault = FAULT_NOINST;
|
|
|
goto abort_calibration;
|
|
|
}
|
|
|
|
|
|
cksum = hash32(trace_bits, MAP_SIZE, HASH_CONST);
|
|
|
|
|
|
if (q->exec_cksum != cksum) {
|
|
|
|
|
|
hnb = has_new_bits(virgin_bits);
|
|
|
if (hnb > new_bits) new_bits = hnb;
|
|
|
|
|
|
if (q->exec_cksum) {
|
|
|
|
|
|
u32 i;
|
|
|
|
|
|
for (i = 0; i < MAP_SIZE; i++) {
|
|
|
|
|
|
if (!var_bytes[i] && first_trace[i] != trace_bits[i]) {
|
|
|
|
|
|
var_bytes[i] = 1;
|
|
|
stage_max = CAL_CYCLES_LONG;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
var_detected = 1;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
q->exec_cksum = cksum;
|
|
|
memcpy(first_trace, trace_bits, MAP_SIZE);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
stop_us = get_cur_time_us();
|
|
|
|
|
|
total_cal_us += stop_us - start_us;
|
|
|
total_cal_cycles += stage_max;
|
|
|
|
|
|
/* 好的,我们来收集一些关于这个测试用例性能的统计数据。
|
|
|
这将用于在calculate_score()中计算模糊测试的时间。 */
|
|
|
|
|
|
q->exec_us = (stop_us - start_us) / stage_max;
|
|
|
q->bitmap_size = count_bytes(trace_bits);
|
|
|
q->handicap = handicap;
|
|
|
q->cal_failed = 0;
|
|
|
|
|
|
total_bitmap_size += q->bitmap_size;
|
|
|
total_bitmap_entries++;
|
|
|
|
|
|
update_bitmap_score(q);
|
|
|
|
|
|
/* 如果这个案例没有产生新的来自插桩的输出,告诉父进程。
|
|
|
这是一个非关键性问题,但值得警告用户。 */
|
|
|
|
|
|
if (!dumb_mode && first_run && !fault && !new_bits) fault = FAULT_NOBITS;
|
|
|
|
|
|
abort_calibration:
|
|
|
|
|
|
if (new_bits == 2 && !q->has_new_cov) {
|
|
|
q->has_new_cov = 1;
|
|
|
queued_with_cov++;
|
|
|
}
|
|
|
|
|
|
/* 标记可变路径。 */
|
|
|
|
|
|
if (var_detected) {
|
|
|
|
|
|
var_byte_count = count_bytes(var_bytes);
|
|
|
|
|
|
if (!q->var_behavior) {
|
|
|
mark_as_variable(q);
|
|
|
queued_variable++;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
stage_name = old_sn;
|
|
|
stage_cur = old_sc;
|
|
|
stage_max = old_sm;
|
|
|
|
|
|
if (!first_run) show_stats();
|
|
|
|
|
|
return fault;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 检查地图覆盖率。为第一个测试用例调用一次。 */
|
|
|
|
|
|
static void check_map_coverage(void) {
|
|
|
|
|
|
u32 i;
|
|
|
|
|
|
if (count_bytes(trace_bits) < 100) return;
|
|
|
|
|
|
for (i = (1 << (MAP_SIZE_POW2 - 1)); i < MAP_SIZE; i++)
|
|
|
if (trace_bits[i]) return;
|
|
|
|
|
|
WARNF("Recompile binary with newer version of afl to improve coverage!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 对所有测试用例执行一次不实际执行的预运行,以确认应用程序按预期工作。这只针对初始输入进行,并且只执行一次。 */
|
|
|
|
|
|
static void perform_dry_run(char** argv) {
|
|
|
|
|
|
struct queue_entry* q = queue;
|
|
|
u32 cal_failures = 0;
|
|
|
u8* skip_crashes = getenv("AFL_SKIP_CRASHES");
|
|
|
|
|
|
while (q) {
|
|
|
|
|
|
u8* use_mem;
|
|
|
u8 res;
|
|
|
s32 fd;
|
|
|
|
|
|
u8* fn = strrchr(q->fname, '/') + 1;
|
|
|
|
|
|
ACTF("Attempting dry run with '%s'...", fn);
|
|
|
|
|
|
fd = open(q->fname, O_RDONLY);
|
|
|
if (fd < 0) PFATAL("Unable to open '%s'", q->fname);
|
|
|
|
|
|
use_mem = ck_alloc_nozero(q->len);
|
|
|
|
|
|
if (read(fd, use_mem, q->len) != q->len)
|
|
|
FATAL("Short read from '%s'", q->fname);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
res = calibrate_case(argv, q, use_mem, 0, 1);
|
|
|
ck_free(use_mem);
|
|
|
|
|
|
if (stop_soon) return;
|
|
|
|
|
|
if (res == crash_mode || res == FAULT_NOBITS)
|
|
|
SAYF(cGRA " len = %u, map size = %u, exec speed = %llu us\n" cRST,
|
|
|
q->len, q->bitmap_size, q->exec_us);
|
|
|
|
|
|
switch (res) {
|
|
|
|
|
|
case FAULT_NONE:
|
|
|
|
|
|
if (q == queue) check_map_coverage();
|
|
|
|
|
|
if (crash_mode) FATAL("Test case '%s' does *NOT* crash", fn);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case FAULT_TMOUT:
|
|
|
|
|
|
if (timeout_given) {
|
|
|
|
|
|
/* 命令行中的 -t nn+ 语法将 timeout_given 设置为 '2',并且指示 afl-fuzz 容忍但跳过超时的队列条目。 */
|
|
|
|
|
|
if (timeout_given > 1) {
|
|
|
WARNF("Test case results in a timeout (skipping)");
|
|
|
q->cal_failed = CAL_CHANCES;
|
|
|
cal_failures++;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"The program took more than %u ms to process one of the initial test cases.\n"
|
|
|
" Usually, the right thing to do is to relax the -t option - or to delete it\n"
|
|
|
" altogether and allow the fuzzer to auto-calibrate. That said, if you know\n"
|
|
|
" what you are doing and want to simply skip the unruly test cases, append\n"
|
|
|
" '+' at the end of the value passed to -t ('-t %u+').\n", exec_tmout,
|
|
|
exec_tmout);
|
|
|
|
|
|
FATAL("Test case '%s' results in a timeout", fn);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"The program took more than %u ms to process one of the initial test cases.\n"
|
|
|
" This is bad news; raising the limit with the -t option is possible, but\n"
|
|
|
" will probably make the fuzzing process extremely slow.\n\n"
|
|
|
|
|
|
" If this test case is just a fluke, the other option is to just avoid it\n"
|
|
|
" altogether, and find one that is less of a CPU hog.\n", exec_tmout);
|
|
|
|
|
|
FATAL("Test case '%s' results in a timeout", fn);
|
|
|
|
|
|
}
|
|
|
|
|
|
case FAULT_CRASH:
|
|
|
|
|
|
if (crash_mode) break;
|
|
|
|
|
|
if (skip_crashes) {
|
|
|
WARNF("Test case results in a crash (skipping)");
|
|
|
q->cal_failed = CAL_CHANCES;
|
|
|
cal_failures++;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (mem_limit) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Oops, the program crashed with one of the test cases provided. There are\n"
|
|
|
" several possible explanations:\n\n"
|
|
|
|
|
|
" - The test case causes known crashes under normal working conditions. If\n"
|
|
|
" so, please remove it. The fuzzer should be seeded with interesting\n"
|
|
|
" inputs - but not ones that cause an outright crash.\n\n"
|
|
|
|
|
|
" - The current memory limit (%s) is too low for this program, causing\n"
|
|
|
" it to die due to OOM when parsing valid files. To fix this, try\n"
|
|
|
" bumping it up with the -m setting in the command line. If in doubt,\n"
|
|
|
" try something along the lines of:\n\n"
|
|
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
" ( ulimit -Sv $[%llu << 10]; /path/to/binary [...] <testcase )\n\n"
|
|
|
#else
|
|
|
" ( ulimit -Sd $[%llu << 10]; /path/to/binary [...] <testcase )\n\n"
|
|
|
#endif /* ^RLIMIT_AS */
|
|
|
|
|
|
" Tip: you can use http://jwilk.net/software/recidivm to quickly\n"
|
|
|
" estimate the required amount of virtual memory for the binary. Also,\n"
|
|
|
" if you are using ASAN, see %s/notes_for_asan.txt.\n\n"
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
" - On MacOS X, the semantics of fork() syscalls are non-standard and may\n"
|
|
|
" break afl-fuzz performance optimizations when running platform-specific\n"
|
|
|
" binaries. To fix this, set AFL_NO_FORKSRV=1 in the environment.\n\n"
|
|
|
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
|
" - Least likely, there is a horrible bug in the fuzzer. If other options\n"
|
|
|
" fail, poke <lcamtuf@coredump.cx> for troubleshooting tips.\n",
|
|
|
DMS(mem_limit << 20), mem_limit - 1, doc_path);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Oops, the program crashed with one of the test cases provided. There are\n"
|
|
|
" several possible explanations:\n\n"
|
|
|
|
|
|
" - The test case causes known crashes under normal working conditions. If\n"
|
|
|
" so, please remove it. The fuzzer should be seeded with interesting\n"
|
|
|
" inputs - but not ones that cause an outright crash.\n\n"
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
" - On MacOS X, the semantics of fork() syscalls are non-standard and may\n"
|
|
|
" break afl-fuzz performance optimizations when running platform-specific\n"
|
|
|
" binaries. To fix this, set AFL_NO_FORKSRV=1 in the environment.\n\n"
|
|
|
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
|
" - Least likely, there is a horrible bug in the fuzzer. If other options\n"
|
|
|
" fail, poke <lcamtuf@coredump.cx> for troubleshooting tips.\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
FATAL("Test case '%s' results in a crash", fn);
|
|
|
|
|
|
case FAULT_ERROR:
|
|
|
|
|
|
FATAL("Unable to execute target application ('%s')", argv[0]);
|
|
|
|
|
|
case FAULT_NOINST:
|
|
|
|
|
|
FATAL("No instrumentation detected");
|
|
|
|
|
|
case FAULT_NOBITS:
|
|
|
|
|
|
useless_at_start++;
|
|
|
|
|
|
if (!in_bitmap && !shuffle_queue)
|
|
|
WARNF("No new instrumentation output, test case may be useless.");
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (q->var_behavior) WARNF("Instrumentation output varies across runs.");
|
|
|
|
|
|
q = q->next;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (cal_failures) {
|
|
|
|
|
|
if (cal_failures == queued_paths)
|
|
|
FATAL("All test cases time out%s, giving up!",
|
|
|
skip_crashes ? " or crash" : "");
|
|
|
|
|
|
WARNF("Skipped %u test cases (%0.02f%%) due to timeouts%s.", cal_failures,
|
|
|
((double)cal_failures) * 100 / queued_paths,
|
|
|
skip_crashes ? " or crashes" : "");
|
|
|
|
|
|
if (cal_failures * 5 > queued_paths)
|
|
|
WARNF(cLRD "High percentage of rejected test cases, check settings!");
|
|
|
|
|
|
}
|
|
|
|
|
|
OKF("All test cases processed.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 辅助函数:link(),如果可能,复制 */
|
|
|
|
|
|
static void link_or_copy(u8* old_path, u8* new_path) {
|
|
|
|
|
|
s32 i = link(old_path, new_path);
|
|
|
s32 sfd, dfd;
|
|
|
u8* tmp;
|
|
|
|
|
|
if (!i) return;
|
|
|
|
|
|
sfd = open(old_path, O_RDONLY);
|
|
|
if (sfd < 0) PFATAL("Unable to open '%s'", old_path);
|
|
|
|
|
|
dfd = open(new_path, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
if (dfd < 0) PFATAL("Unable to create '%s'", new_path);
|
|
|
|
|
|
tmp = ck_alloc(64 * 1024);
|
|
|
|
|
|
while ((i = read(sfd, tmp, 64 * 1024)) > 0)
|
|
|
ck_write(dfd, tmp, i, new_path);
|
|
|
|
|
|
if (i < 0) PFATAL("read() failed");
|
|
|
|
|
|
ck_free(tmp);
|
|
|
close(sfd);
|
|
|
close(dfd);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
static void nuke_resume_dir(void);
|
|
|
|
|
|
/* 在输出目录中为输入测试用例创建硬链接,选择合适的名字并相应地进行调整。 */
|
|
|
|
|
|
static void pivot_inputs(void) {
|
|
|
|
|
|
struct queue_entry* q = queue;
|
|
|
u32 id = 0;
|
|
|
|
|
|
ACTF("Creating hard links for all input files...");
|
|
|
|
|
|
while (q) {
|
|
|
|
|
|
u8 *nfn, *rsl = strrchr(q->fname, '/');
|
|
|
u32 orig_id;
|
|
|
|
|
|
if (!rsl) rsl = q->fname; else rsl++;
|
|
|
|
|
|
/* 如果原始文件名符合语法,并且记录的ID与我们要分配的ID匹配,就直接使用原始文件名。这对于恢复模糊测试运行非常有价值。 */
|
|
|
|
|
|
#ifndef SIMPLE_FILES
|
|
|
# define CASE_PREFIX "id:"
|
|
|
#else
|
|
|
# define CASE_PREFIX "id_"
|
|
|
#endif /* ^!SIMPLE_FILES */
|
|
|
|
|
|
if (!strncmp(rsl, CASE_PREFIX, 3) &&
|
|
|
sscanf(rsl + 3, "%06u", &orig_id) == 1 && orig_id == id) {
|
|
|
|
|
|
u8* src_str;
|
|
|
u32 src_id;
|
|
|
|
|
|
resuming_fuzz = 1;
|
|
|
nfn = alloc_printf("%s/queue/%s", out_dir, rsl);
|
|
|
|
|
|
/* 既然我们已经在做了,让我们也尝试找到父项,并确定这个条目的适当深度。 */
|
|
|
|
|
|
src_str = strchr(rsl + 3, ':');
|
|
|
|
|
|
if (src_str && sscanf(src_str + 1, "%06u", &src_id) == 1) {
|
|
|
|
|
|
struct queue_entry* s = queue;
|
|
|
while (src_id-- && s) s = s->next;
|
|
|
if (s) q->depth = s->depth + 1;
|
|
|
|
|
|
if (max_depth < q->depth) max_depth = q->depth;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
/* 没有成功 - 创建一个新名字,并将原始名字作为子字符串包含在内。 */
|
|
|
|
|
|
#ifndef SIMPLE_FILES
|
|
|
|
|
|
u8* use_name = strstr(rsl, ",orig:");
|
|
|
|
|
|
if (use_name) use_name += 6; else use_name = rsl;
|
|
|
nfn = alloc_printf("%s/queue/id:%06u,orig:%s", out_dir, id, use_name);
|
|
|
|
|
|
#else
|
|
|
|
|
|
nfn = alloc_printf("%s/queue/id_%06u", out_dir, id);
|
|
|
|
|
|
#endif /* ^!SIMPLE_FILES */
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 转向新的队列条目。 */
|
|
|
|
|
|
link_or_copy(q->fname, nfn);
|
|
|
ck_free(q->fname);
|
|
|
q->fname = nfn;
|
|
|
|
|
|
/* 确保passd_det值也被传递。 */
|
|
|
|
|
|
if (q->passed_det) mark_as_det_done(q);
|
|
|
|
|
|
q = q->next;
|
|
|
id++;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (in_place_resume) nuke_resume_dir();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
#ifndef SIMPLE_FILES
|
|
|
|
|
|
/* 为新测试用例构造一个包含导致其被发现的操作的文件名。使用一个静态缓冲区。 */
|
|
|
|
|
|
static u8* describe_op(u8 hnb) {
|
|
|
|
|
|
static u8 ret[256];
|
|
|
|
|
|
if (syncing_party) {
|
|
|
|
|
|
sprintf(ret, "sync:%s,src:%06u", syncing_party, syncing_case);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
sprintf(ret, "src:%06u", current_entry);
|
|
|
|
|
|
if (splicing_with >= 0)
|
|
|
sprintf(ret + strlen(ret), "+%06u", splicing_with);
|
|
|
|
|
|
sprintf(ret + strlen(ret), ",op:%s", stage_short);
|
|
|
|
|
|
if (stage_cur_byte >= 0) {
|
|
|
|
|
|
sprintf(ret + strlen(ret), ",pos:%u", stage_cur_byte);
|
|
|
|
|
|
if (stage_val_type != STAGE_VAL_NONE)
|
|
|
sprintf(ret + strlen(ret), ",val:%s%+d",
|
|
|
(stage_val_type == STAGE_VAL_BE) ? "be:" : "",
|
|
|
stage_cur_val);
|
|
|
|
|
|
} else sprintf(ret + strlen(ret), ",rep:%u", stage_cur_val);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (hnb == 2) strcat(ret, ",+cov");
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* !SIMPLE_FILES */
|
|
|
|
|
|
|
|
|
/* 为崩溃目录写一个伴随消息:-) */
|
|
|
|
|
|
static void write_crash_readme(void) {
|
|
|
|
|
|
u8* fn = alloc_printf("%s/crashes/README.txt", out_dir);
|
|
|
s32 fd;
|
|
|
FILE* f;
|
|
|
|
|
|
fd = open(fn, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
ck_free(fn);
|
|
|
|
|
|
/* 不要死于错误——那是不礼貌的 */
|
|
|
|
|
|
if (fd < 0) return;
|
|
|
|
|
|
f = fdopen(fd, "w");
|
|
|
|
|
|
if (!f) {
|
|
|
close(fd);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
fprintf(f, "Command line used to find this crash:\n\n"
|
|
|
|
|
|
"%s\n\n"
|
|
|
|
|
|
"If you can't reproduce a bug outside of afl-fuzz, be sure to set the same\n"
|
|
|
"memory limit. The limit used for this fuzzing session was %s.\n\n"
|
|
|
|
|
|
"Need a tool to minimize test cases before investigating the crashes or sending\n"
|
|
|
"them to a vendor? Check out the afl-tmin that comes with the fuzzer!\n\n"
|
|
|
|
|
|
"Found any cool bugs in open-source tools using afl-fuzz? If yes, please drop\n"
|
|
|
"me a mail at <lcamtuf@coredump.cx> once the issues are fixed - I'd love to\n"
|
|
|
"add your finds to the gallery at:\n\n"
|
|
|
|
|
|
" http://lcamtuf.coredump.cx/afl/\n\n"
|
|
|
|
|
|
"Thanks :-)\n",
|
|
|
|
|
|
orig_cmdline, DMS(mem_limit << 20)); /* ignore errors */
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 检查在常规模糊测试期间的execve()结果是否有趣,如果有趣,则保存
|
|
|
或将输入测试用例排队以进行进一步分析。如果条目被保存,则返回1,否则返回0。 */
|
|
|
|
|
|
static u8 save_if_interesting(char** argv, void* mem, u32 len, u8 fault) {
|
|
|
|
|
|
u8 *fn = "";
|
|
|
u8 hnb;
|
|
|
s32 fd;
|
|
|
u8 keeping = 0, res;
|
|
|
|
|
|
if (fault == crash_mode) {
|
|
|
|
|
|
/* 仅当映射中有新的位时才保留,将其添加到队列中以供将来的模糊测试等。 */
|
|
|
|
|
|
if (!(hnb = has_new_bits(virgin_bits))) {
|
|
|
if (crash_mode) total_crashes++;
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
#ifndef SIMPLE_FILES
|
|
|
|
|
|
fn = alloc_printf("%s/queue/id:%06u,%s", out_dir, queued_paths,
|
|
|
describe_op(hnb));
|
|
|
|
|
|
#else
|
|
|
|
|
|
fn = alloc_printf("%s/queue/id_%06u", out_dir, queued_paths);
|
|
|
|
|
|
#endif /* ^!SIMPLE_FILES */
|
|
|
|
|
|
add_to_queue(fn, len, 0);
|
|
|
|
|
|
if (hnb == 2) {
|
|
|
queue_top->has_new_cov = 1;
|
|
|
queued_with_cov++;
|
|
|
}
|
|
|
|
|
|
queue_top->exec_cksum = hash32(trace_bits, MAP_SIZE, HASH_CONST);
|
|
|
|
|
|
/* 尝试进行内联校准;成功时也会调用update_bitmap_score()。 */
|
|
|
|
|
|
res = calibrate_case(argv, queue_top, mem, queue_cycle - 1, 0);
|
|
|
|
|
|
if (res == FAULT_ERROR)
|
|
|
FATAL("Unable to execute target application");
|
|
|
|
|
|
fd = open(fn, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", fn);
|
|
|
ck_write(fd, mem, len, fn);
|
|
|
close(fd);
|
|
|
|
|
|
keeping = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
switch (fault) {
|
|
|
|
|
|
case FAULT_TMOUT:
|
|
|
|
|
|
/* 超时并不是很有趣的情况,但我们仍然需要保留一些样本。我们使用在特定挂起的
|
|
|
位图中出现的新位作为独特性的信号。在“哑”模式下,我们只是保留所有内容。 */
|
|
|
|
|
|
total_tmouts++;
|
|
|
|
|
|
if (unique_hangs >= KEEP_UNIQUE_HANG) return keeping;
|
|
|
|
|
|
if (!dumb_mode) {
|
|
|
|
|
|
#ifdef WORD_SIZE_64
|
|
|
simplify_trace((u64*)trace_bits);
|
|
|
#else
|
|
|
simplify_trace((u32*)trace_bits);
|
|
|
#endif /* ^WORD_SIZE_64 */
|
|
|
|
|
|
if (!has_new_bits(virgin_tmout)) return keeping;
|
|
|
|
|
|
}
|
|
|
|
|
|
unique_tmouts++;
|
|
|
|
|
|
/* 在保存之前,我们通过使用更宽裕的超时时间重新运行目标程序来确保这是一个真正的挂起(除非默认的超时时间已经足够宽裕)。*/
|
|
|
|
|
|
if (exec_tmout < hang_tmout) {
|
|
|
|
|
|
u8 new_fault;
|
|
|
write_to_testcase(mem, len);
|
|
|
new_fault = run_target(argv, hang_tmout);
|
|
|
|
|
|
/* 一个用户报告的特殊情况:增加超时时间实际上揭示了一个崩溃。确保如果出现这种情况,我们不会丢弃它。 */
|
|
|
|
|
|
if (!stop_soon && new_fault == FAULT_CRASH) goto keep_as_crash;
|
|
|
|
|
|
if (stop_soon || new_fault != FAULT_TMOUT) return keeping;
|
|
|
|
|
|
}
|
|
|
|
|
|
#ifndef SIMPLE_FILES
|
|
|
|
|
|
fn = alloc_printf("%s/hangs/id:%06llu,%s", out_dir,
|
|
|
unique_hangs, describe_op(0));
|
|
|
|
|
|
#else
|
|
|
|
|
|
fn = alloc_printf("%s/hangs/id_%06llu", out_dir,
|
|
|
unique_hangs);
|
|
|
|
|
|
#endif /* ^!SIMPLE_FILES */
|
|
|
|
|
|
unique_hangs++;
|
|
|
|
|
|
last_hang_time = get_cur_time();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case FAULT_CRASH:
|
|
|
|
|
|
keep_as_crash:
|
|
|
|
|
|
/* 这与处理超时的方式大致相似,只是限制略有不同,并且不需要重新运行测试用例。 */
|
|
|
|
|
|
total_crashes++;
|
|
|
|
|
|
if (unique_crashes >= KEEP_UNIQUE_CRASH) return keeping;
|
|
|
|
|
|
if (!dumb_mode) {
|
|
|
|
|
|
#ifdef WORD_SIZE_64
|
|
|
simplify_trace((u64*)trace_bits);
|
|
|
#else
|
|
|
simplify_trace((u32*)trace_bits);
|
|
|
#endif /* ^WORD_SIZE_64 */
|
|
|
|
|
|
if (!has_new_bits(virgin_crash)) return keeping;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!unique_crashes) write_crash_readme();
|
|
|
|
|
|
#ifndef SIMPLE_FILES
|
|
|
|
|
|
fn = alloc_printf("%s/crashes/id:%06llu,sig:%02u,%s", out_dir,
|
|
|
unique_crashes, kill_signal, describe_op(0));
|
|
|
|
|
|
#else
|
|
|
|
|
|
fn = alloc_printf("%s/crashes/id_%06llu_%02u", out_dir, unique_crashes,
|
|
|
kill_signal);
|
|
|
|
|
|
#endif /* ^!SIMPLE_FILES */
|
|
|
|
|
|
unique_crashes++;
|
|
|
|
|
|
last_crash_time = get_cur_time();
|
|
|
last_crash_execs = total_execs;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case FAULT_ERROR: FATAL("Unable to execute target application");
|
|
|
|
|
|
default: return keeping;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 如果我们在这里,显然我们也想要保存崩溃或挂起的测试用例。 */
|
|
|
|
|
|
fd = open(fn, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", fn);
|
|
|
ck_write(fd, mem, len, fn);
|
|
|
close(fd);
|
|
|
|
|
|
ck_free(fn);
|
|
|
|
|
|
return keeping;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 在恢复时,尝试找到开始的队列位置。这只在恢复时有意义,并且当我们能找到原始的fuzzer_stats时。 */
|
|
|
|
|
|
static u32 find_start_position(void) {
|
|
|
|
|
|
static u8 tmp[4096]; /* 对任何人来说都足够了 */
|
|
|
|
|
|
u8 *fn, *off;
|
|
|
s32 fd, i;
|
|
|
u32 ret;
|
|
|
|
|
|
if (!resuming_fuzz) return 0;
|
|
|
|
|
|
if (in_place_resume) fn = alloc_printf("%s/fuzzer_stats", out_dir);
|
|
|
else fn = alloc_printf("%s/../fuzzer_stats", in_dir);
|
|
|
|
|
|
fd = open(fn, O_RDONLY);
|
|
|
ck_free(fn);
|
|
|
|
|
|
if (fd < 0) return 0;
|
|
|
|
|
|
i = read(fd, tmp, sizeof(tmp) - 1); (void)i; /* Ignore errors */
|
|
|
close(fd);
|
|
|
|
|
|
off = strstr(tmp, "cur_path : ");
|
|
|
if (!off) return 0;
|
|
|
|
|
|
ret = atoi(off + 20);
|
|
|
if (ret >= queued_paths) ret = 0;
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 同样,但针对超时情况。想法是在没有给定-t参数的情况下恢复会话时,我们不想一遍又一遍地自动调整超时时间,以防止它因为随机的异常而不断增长。 */
|
|
|
|
|
|
static void find_timeout(void) {
|
|
|
|
|
|
static u8 tmp[4096]; /* 对任何人来说都足够了 */
|
|
|
|
|
|
u8 *fn, *off;
|
|
|
s32 fd, i;
|
|
|
u32 ret;
|
|
|
|
|
|
if (!resuming_fuzz) return;
|
|
|
|
|
|
if (in_place_resume) fn = alloc_printf("%s/fuzzer_stats", out_dir);
|
|
|
else fn = alloc_printf("%s/../fuzzer_stats", in_dir);
|
|
|
|
|
|
fd = open(fn, O_RDONLY);
|
|
|
ck_free(fn);
|
|
|
|
|
|
if (fd < 0) return;
|
|
|
|
|
|
i = read(fd, tmp, sizeof(tmp) - 1); (void)i; /* Ignore errors */
|
|
|
close(fd);
|
|
|
|
|
|
off = strstr(tmp, "exec_timeout : ");
|
|
|
if (!off) return;
|
|
|
|
|
|
ret = atoi(off + 20);
|
|
|
if (ret <= 4) return;
|
|
|
|
|
|
exec_tmout = ret;
|
|
|
timeout_given = 3;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 更新无人值守监控的统计文件 */
|
|
|
|
|
|
static void write_stats_file(double bitmap_cvg, double stability, double eps) {
|
|
|
|
|
|
static double last_bcvg, last_stab, last_eps;
|
|
|
static struct rusage usage;
|
|
|
|
|
|
u8* fn = alloc_printf("%s/fuzzer_stats", out_dir);
|
|
|
s32 fd;
|
|
|
FILE* f;
|
|
|
|
|
|
fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", fn);
|
|
|
|
|
|
ck_free(fn);
|
|
|
|
|
|
f = fdopen(fd, "w");
|
|
|
|
|
|
if (!f) PFATAL("fdopen() failed");
|
|
|
|
|
|
/* 保留最后的值,以防我们被调用到另一个上下文中,那里每秒执行次数的统计数据等并不立即可用。 */
|
|
|
|
|
|
if (!bitmap_cvg && !stability && !eps) {
|
|
|
bitmap_cvg = last_bcvg;
|
|
|
stability = last_stab;
|
|
|
eps = last_eps;
|
|
|
} else {
|
|
|
last_bcvg = bitmap_cvg;
|
|
|
last_stab = stability;
|
|
|
last_eps = eps;
|
|
|
}
|
|
|
|
|
|
fprintf(f, "start_time : %llu\n"
|
|
|
"last_update : %llu\n"
|
|
|
"fuzzer_pid : %u\n"
|
|
|
"cycles_done : %llu\n"
|
|
|
"execs_done : %llu\n"
|
|
|
"execs_per_sec : %0.02f\n"
|
|
|
"paths_total : %u\n"
|
|
|
"paths_favored : %u\n"
|
|
|
"paths_found : %u\n"
|
|
|
"paths_imported : %u\n"
|
|
|
"max_depth : %u\n"
|
|
|
"cur_path : %u\n" /* 必须匹配find_start_position() */
|
|
|
"pending_favs : %u\n"
|
|
|
"pending_total : %u\n"
|
|
|
"variable_paths : %u\n"
|
|
|
"stability : %0.02f%%\n"
|
|
|
"bitmap_cvg : %0.02f%%\n"
|
|
|
"unique_crashes : %llu\n"
|
|
|
"unique_hangs : %llu\n"
|
|
|
"last_path : %llu\n"
|
|
|
"last_crash : %llu\n"
|
|
|
"last_hang : %llu\n"
|
|
|
"execs_since_crash : %llu\n"
|
|
|
"exec_timeout : %u\n" /* 必须匹配find_timeout() */
|
|
|
"afl_banner : %s\n"
|
|
|
"afl_version : " VERSION "\n"
|
|
|
"target_mode : %s%s%s%s%s%s%s\n"
|
|
|
"command_line : %s\n"
|
|
|
"slowest_exec_ms : %llu\n",
|
|
|
start_time / 1000, get_cur_time() / 1000, getpid(),
|
|
|
queue_cycle ? (queue_cycle - 1) : 0, total_execs, eps,
|
|
|
queued_paths, queued_favored, queued_discovered, queued_imported,
|
|
|
max_depth, current_entry, pending_favored, pending_not_fuzzed,
|
|
|
queued_variable, stability, bitmap_cvg, unique_crashes,
|
|
|
unique_hangs, last_path_time / 1000, last_crash_time / 1000,
|
|
|
last_hang_time / 1000, total_execs - last_crash_execs,
|
|
|
exec_tmout, use_banner,
|
|
|
qemu_mode ? "qemu " : "", dumb_mode ? " dumb " : "",
|
|
|
no_forkserver ? "no_forksrv " : "", crash_mode ? "crash " : "",
|
|
|
persistent_mode ? "persistent " : "", deferred_mode ? "deferred " : "",
|
|
|
(qemu_mode || dumb_mode || no_forkserver || crash_mode ||
|
|
|
persistent_mode || deferred_mode) ? "" : "default",
|
|
|
orig_cmdline, slowest_exec_ms);
|
|
|
/* ignore errors */
|
|
|
|
|
|
/* 从子进程中获取rss值。在调用getrusage之前,我们必须已经杀死了fork服务器进程并调用了waitpid。 */
|
|
|
if (getrusage(RUSAGE_CHILDREN, &usage)) {
|
|
|
WARNF("getrusage failed");
|
|
|
} else if (usage.ru_maxrss == 0) {
|
|
|
fprintf(f, "peak_rss_mb : not available while afl is running\n");
|
|
|
} else {
|
|
|
#ifdef __APPLE__
|
|
|
fprintf(f, "peak_rss_mb : %zu\n", usage.ru_maxrss >> 20);
|
|
|
#else
|
|
|
fprintf(f, "peak_rss_mb : %zu\n", usage.ru_maxrss >> 10);
|
|
|
#endif /* ^__APPLE__ */
|
|
|
}
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 如果有必要,请更新情节文件。 */
|
|
|
|
|
|
static void maybe_update_plot_file(double bitmap_cvg, double eps) {
|
|
|
|
|
|
static u32 prev_qp, prev_pf, prev_pnf, prev_ce, prev_md;
|
|
|
static u64 prev_qc, prev_uc, prev_uh;
|
|
|
|
|
|
if (prev_qp == queued_paths && prev_pf == pending_favored &&
|
|
|
prev_pnf == pending_not_fuzzed && prev_ce == current_entry &&
|
|
|
prev_qc == queue_cycle && prev_uc == unique_crashes &&
|
|
|
prev_uh == unique_hangs && prev_md == max_depth) return;
|
|
|
|
|
|
prev_qp = queued_paths;
|
|
|
prev_pf = pending_favored;
|
|
|
prev_pnf = pending_not_fuzzed;
|
|
|
prev_ce = current_entry;
|
|
|
prev_qc = queue_cycle;
|
|
|
prev_uc = unique_crashes;
|
|
|
prev_uh = unique_hangs;
|
|
|
prev_md = max_depth;
|
|
|
|
|
|
/* 文件中的字段包括::
|
|
|
|
|
|
unix_time, cycles_done, cur_path, paths_total, paths_not_fuzzed,
|
|
|
favored_not_fuzzed, unique_crashes, unique_hangs, max_depth,
|
|
|
execs_per_sec */
|
|
|
|
|
|
fprintf(plot_file,
|
|
|
"%llu, %llu, %u, %u, %u, %u, %0.02f%%, %llu, %llu, %u, %0.02f\n",
|
|
|
get_cur_time() / 1000, queue_cycle - 1, current_entry, queued_paths,
|
|
|
pending_not_fuzzed, pending_favored, bitmap_cvg, unique_crashes,
|
|
|
unique_hangs, max_depth, eps); /* ignore errors */
|
|
|
|
|
|
fflush(plot_file);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* maybe_delete_out_dir()的辅助函数,用于删除目录中所有具有特定前缀的文件 */
|
|
|
|
|
|
static u8 delete_files(u8* path, u8* prefix) {
|
|
|
|
|
|
DIR* d;
|
|
|
struct dirent* d_ent;
|
|
|
|
|
|
d = opendir(path);
|
|
|
|
|
|
if (!d) return 0;
|
|
|
|
|
|
while ((d_ent = readdir(d))) {
|
|
|
|
|
|
if (d_ent->d_name[0] != '.' && (!prefix ||
|
|
|
!strncmp(d_ent->d_name, prefix, strlen(prefix)))) {
|
|
|
|
|
|
u8* fname = alloc_printf("%s/%s", path, d_ent->d_name);
|
|
|
if (unlink(fname)) PFATAL("Unable to delete '%s'", fname);
|
|
|
ck_free(fname);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
closedir(d);
|
|
|
|
|
|
return !!rmdir(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 通过一些简单的平滑,获得可运行进程的数量。 */
|
|
|
|
|
|
static double get_runnable_processes(void) {
|
|
|
|
|
|
static double res;
|
|
|
|
|
|
#if defined(__APPLE__) || defined(__FreeBSD__) || defined (__OpenBSD__)
|
|
|
|
|
|
/*我没有看到任何可移植的sysctl命令或其他工具可以快速给出可运行进程的数量;不过,1分钟负载平均值可以作为一个半像样的近似值。 */
|
|
|
|
|
|
if (getloadavg(&res, 1) != 1) return 0;
|
|
|
|
|
|
#else
|
|
|
|
|
|
/* 在Linux上,/proc/stat可能是最佳方式;负载平均值的计算方式有些奇特,有时不能很好地反映非常短暂的进程。 */
|
|
|
|
|
|
FILE* f = fopen("/proc/stat", "r");
|
|
|
u8 tmp[1024];
|
|
|
u32 val = 0;
|
|
|
|
|
|
if (!f) return 0;
|
|
|
|
|
|
while (fgets(tmp, sizeof(tmp), f)) {
|
|
|
|
|
|
if (!strncmp(tmp, "procs_running ", 14) ||
|
|
|
!strncmp(tmp, "procs_blocked ", 14)) val += atoi(tmp + 14);
|
|
|
|
|
|
}
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
if (!res) {
|
|
|
|
|
|
res = val;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
res = res * (1.0 - 1.0 / AVG_SMOOTHING) +
|
|
|
((double)val) * (1.0 / AVG_SMOOTHING);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* ^(__APPLE__ || __FreeBSD__ || __OpenBSD__) */
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 删除用于就地恢复会话的临时目录。*/
|
|
|
|
|
|
static void nuke_resume_dir(void) {
|
|
|
|
|
|
u8* fn;
|
|
|
|
|
|
fn = alloc_printf("%s/_resume/.state/deterministic_done", out_dir);
|
|
|
if (delete_files(fn, CASE_PREFIX)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/_resume/.state/auto_extras", out_dir);
|
|
|
if (delete_files(fn, "auto_")) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/_resume/.state/redundant_edges", out_dir);
|
|
|
if (delete_files(fn, CASE_PREFIX)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/_resume/.state/variable_behavior", out_dir);
|
|
|
if (delete_files(fn, CASE_PREFIX)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/_resume/.state", out_dir);
|
|
|
if (rmdir(fn) && errno != ENOENT) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/_resume", out_dir);
|
|
|
if (delete_files(fn, CASE_PREFIX)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
return;
|
|
|
|
|
|
dir_cleanup_failed:
|
|
|
|
|
|
FATAL("_resume directory cleanup failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 如果识别输出目录为我们的,并且fuzzer当前没有在运行,以及上次运行时间不是很长,就删除fuzzer的输出目录。 */
|
|
|
|
|
|
static void maybe_delete_out_dir(void) {
|
|
|
|
|
|
FILE* f;
|
|
|
u8 *fn = alloc_printf("%s/fuzzer_stats", out_dir);
|
|
|
|
|
|
/* 检查输出目录是否被锁定。如果是,就退出。如果不是,创建一个在进程生命周期内持续存在的锁(这需要保持描述符打开)。*/
|
|
|
|
|
|
out_dir_fd = open(out_dir, O_RDONLY);
|
|
|
if (out_dir_fd < 0) PFATAL("Unable to open '%s'", out_dir);
|
|
|
|
|
|
#ifndef __sun
|
|
|
|
|
|
if (flock(out_dir_fd, LOCK_EX | LOCK_NB) && errno == EWOULDBLOCK) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Looks like the job output directory is being actively used by another\n"
|
|
|
" instance of afl-fuzz. You will need to choose a different %s\n"
|
|
|
" or stop the other process first.\n",
|
|
|
sync_id ? "fuzzer ID" : "output location");
|
|
|
|
|
|
FATAL("Directory '%s' is in use", out_dir);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* !__sun */
|
|
|
|
|
|
f = fopen(fn, "r");
|
|
|
|
|
|
if (f) {
|
|
|
|
|
|
u64 start_time, last_update;
|
|
|
|
|
|
if (fscanf(f, "start_time : %llu\n"
|
|
|
"last_update : %llu\n", &start_time, &last_update) != 2)
|
|
|
FATAL("Malformed data in '%s'", fn);
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
/* 让我们看看有多少工作要做 */
|
|
|
|
|
|
if (!in_place_resume && last_update - start_time > OUTPUT_GRACE * 60) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"The job output directory already exists and contains the results of more\n"
|
|
|
" than %u minutes worth of fuzzing. To avoid data loss, afl-fuzz will *NOT*\n"
|
|
|
" automatically delete this data for you.\n\n"
|
|
|
|
|
|
" If you wish to start a new session, remove or rename the directory manually,\n"
|
|
|
" or specify a different output location for this job. To resume the old\n"
|
|
|
" session, put '-' as the input directory in the command line ('-i -') and\n"
|
|
|
" try again.\n", OUTPUT_GRACE);
|
|
|
|
|
|
FATAL("At-risk data found in '%s'", out_dir);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
ck_free(fn);
|
|
|
|
|
|
/* 就地恢复的想法非常简单:我们暂时将旧的queue/移动到一个新位置,
|
|
|
一旦导入到新queue/完成,这个新位置就会被删除。如果_resume/已经存在,
|
|
|
当前的queue/可能因为之前的中断而不完整,所以我们想使用旧的_resume/目录,我们让rename()默默失败。 */
|
|
|
|
|
|
if (in_place_resume) {
|
|
|
|
|
|
u8* orig_q = alloc_printf("%s/queue", out_dir);
|
|
|
|
|
|
in_dir = alloc_printf("%s/_resume", out_dir);
|
|
|
|
|
|
rename(orig_q, in_dir); /* Ignore errors */
|
|
|
|
|
|
OKF("Output directory exists, will attempt session resume.");
|
|
|
|
|
|
ck_free(orig_q);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
OKF("Output directory exists but deemed OK to reuse.");
|
|
|
|
|
|
}
|
|
|
|
|
|
ACTF("Deleting old session data...");
|
|
|
|
|
|
/* 好的,让我们开始吧!首先,如果存在,我们需要清除<out_dir>/.synced/.../id:*中的条目。 */
|
|
|
|
|
|
if (!in_place_resume) {
|
|
|
|
|
|
fn = alloc_printf("%s/.synced", out_dir);
|
|
|
if (delete_files(fn, NULL)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 接下来,我们需要清理 <out_dir>/queue/.state/ 子目录: */
|
|
|
|
|
|
fn = alloc_printf("%s/queue/.state/deterministic_done", out_dir);
|
|
|
if (delete_files(fn, CASE_PREFIX)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/queue/.state/auto_extras", out_dir);
|
|
|
if (delete_files(fn, "auto_")) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/queue/.state/redundant_edges", out_dir);
|
|
|
if (delete_files(fn, CASE_PREFIX)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/queue/.state/variable_behavior", out_dir);
|
|
|
if (delete_files(fn, CASE_PREFIX)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
/* 然后,删除 .state 子目录本身(现在应该已经为空),以及所有匹配 <out_dir>/queue/id:* 的内容。 */
|
|
|
|
|
|
fn = alloc_printf("%s/queue/.state", out_dir);
|
|
|
if (rmdir(fn) && errno != ENOENT) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/queue", out_dir);
|
|
|
if (delete_files(fn, CASE_PREFIX)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
/* 好的,让我们处理 <out_dir>/crashes/id:* 和 <out_dir>/hangs/id:* */
|
|
|
|
|
|
if (!in_place_resume) {
|
|
|
|
|
|
fn = alloc_printf("%s/crashes/README.txt", out_dir);
|
|
|
unlink(fn); /* Ignore errors */
|
|
|
ck_free(fn);
|
|
|
|
|
|
}
|
|
|
|
|
|
fn = alloc_printf("%s/crashes", out_dir);
|
|
|
|
|
|
/* 如果在进行就地恢复,并且crashes目录不为空,则备份该目录。 */
|
|
|
|
|
|
if (in_place_resume && rmdir(fn)) {
|
|
|
|
|
|
time_t cur_t = time(0);
|
|
|
struct tm* t = localtime(&cur_t);
|
|
|
|
|
|
#ifndef SIMPLE_FILES
|
|
|
|
|
|
u8* nfn = alloc_printf("%s.%04u-%02u-%02u-%02u:%02u:%02u", fn,
|
|
|
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
|
|
|
t->tm_hour, t->tm_min, t->tm_sec);
|
|
|
|
|
|
#else
|
|
|
|
|
|
u8* nfn = alloc_printf("%s_%04u%02u%02u%02u%02u%02u", fn,
|
|
|
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
|
|
|
t->tm_hour, t->tm_min, t->tm_sec);
|
|
|
|
|
|
#endif /* ^!SIMPLE_FILES */
|
|
|
|
|
|
rename(fn, nfn); /* Ignore errors. */
|
|
|
ck_free(nfn);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (delete_files(fn, CASE_PREFIX)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/hangs", out_dir);
|
|
|
|
|
|
/* 备份也挂起了 */
|
|
|
|
|
|
if (in_place_resume && rmdir(fn)) {
|
|
|
|
|
|
time_t cur_t = time(0);
|
|
|
struct tm* t = localtime(&cur_t);
|
|
|
|
|
|
#ifndef SIMPLE_FILES
|
|
|
|
|
|
u8* nfn = alloc_printf("%s.%04u-%02u-%02u-%02u:%02u:%02u", fn,
|
|
|
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
|
|
|
t->tm_hour, t->tm_min, t->tm_sec);
|
|
|
|
|
|
#else
|
|
|
|
|
|
u8* nfn = alloc_printf("%s_%04u%02u%02u%02u%02u%02u", fn,
|
|
|
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
|
|
|
t->tm_hour, t->tm_min, t->tm_sec);
|
|
|
|
|
|
#endif /* ^!SIMPLE_FILES */
|
|
|
|
|
|
rename(fn, nfn); /* Ignore errors. */
|
|
|
ck_free(nfn);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (delete_files(fn, CASE_PREFIX)) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
/* 现在,做一些收尾工作 */
|
|
|
|
|
|
fn = alloc_printf("%s/.cur_input", out_dir);
|
|
|
if (unlink(fn) && errno != ENOENT) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
fn = alloc_printf("%s/fuzz_bitmap", out_dir);
|
|
|
if (unlink(fn) && errno != ENOENT) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
if (!in_place_resume) {
|
|
|
fn = alloc_printf("%s/fuzzer_stats", out_dir);
|
|
|
if (unlink(fn) && errno != ENOENT) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
}
|
|
|
|
|
|
fn = alloc_printf("%s/plot_data", out_dir);
|
|
|
if (unlink(fn) && errno != ENOENT) goto dir_cleanup_failed;
|
|
|
ck_free(fn);
|
|
|
|
|
|
OKF("Output dir cleanup successful.");
|
|
|
|
|
|
/* 哇……就这些吗?如果是,那就庆祝吧! */
|
|
|
|
|
|
return;
|
|
|
|
|
|
dir_cleanup_failed:
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Whoops, the fuzzer tried to reuse your output directory, but bumped into\n"
|
|
|
" some files that shouldn't be there or that couldn't be removed - so it\n"
|
|
|
" decided to abort! This happened while processing this path:\n\n"
|
|
|
|
|
|
" %s\n\n"
|
|
|
" Please examine and manually delete the files, or specify a different\n"
|
|
|
" output location for the tool.\n", fn);
|
|
|
|
|
|
FATAL("Output directory cleanup failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
static void check_term_size(void);
|
|
|
|
|
|
|
|
|
/* 一个时髦的复古统计屏幕!这在每stats_update_freq次execve()调用时被调用,以及在其他几种情况下。 */
|
|
|
|
|
|
static void show_stats(void) {
|
|
|
|
|
|
static u64 last_stats_ms, last_plot_ms, last_ms, last_execs;
|
|
|
static double avg_exec;
|
|
|
double t_byte_ratio, stab_ratio;
|
|
|
|
|
|
u64 cur_ms;
|
|
|
u32 t_bytes, t_bits;
|
|
|
|
|
|
u32 banner_len, banner_pad;
|
|
|
u8 tmp[256];
|
|
|
|
|
|
cur_ms = get_cur_time();
|
|
|
|
|
|
/* 如果上次UI更新后没有经过足够的时间,就退出。 */
|
|
|
|
|
|
if (cur_ms - last_ms < 1000 / UI_TARGET_HZ) return;
|
|
|
|
|
|
/* 检查我们是否过了10分钟。 */
|
|
|
|
|
|
if (cur_ms - start_time > 10 * 60 * 1000) run_over10m = 1;
|
|
|
|
|
|
/* 计算平滑执行速度统计。 */
|
|
|
|
|
|
if (!last_execs) {
|
|
|
|
|
|
avg_exec = ((double)total_execs) * 1000 / (cur_ms - start_time);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
double cur_avg = ((double)(total_execs - last_execs)) * 1000 /
|
|
|
(cur_ms - last_ms);
|
|
|
|
|
|
/* 如果速度有显著(超过5倍)的提升,那么更快速地重置指示器。 */
|
|
|
|
|
|
if (cur_avg * 5 < avg_exec || cur_avg / 5 > avg_exec)
|
|
|
avg_exec = cur_avg;
|
|
|
|
|
|
avg_exec = avg_exec * (1.0 - 1.0 / AVG_SMOOTHING) +
|
|
|
cur_avg * (1.0 / AVG_SMOOTHING);
|
|
|
|
|
|
}
|
|
|
|
|
|
last_ms = cur_ms;
|
|
|
last_execs = total_execs;
|
|
|
|
|
|
/* 告诉来电者何时与我们联系(以高管为衡量标准) */
|
|
|
|
|
|
stats_update_freq = avg_exec / (UI_TARGET_HZ * 10);
|
|
|
if (!stats_update_freq) stats_update_freq = 1;
|
|
|
|
|
|
/* 做一些位图统计 */
|
|
|
|
|
|
t_bytes = count_non_255_bytes(virgin_bits);
|
|
|
t_byte_ratio = ((double)t_bytes * 100) / MAP_SIZE;
|
|
|
|
|
|
if (t_bytes)
|
|
|
stab_ratio = 100 - ((double)var_byte_count) * 100 / t_bytes;
|
|
|
else
|
|
|
stab_ratio = 100;
|
|
|
|
|
|
/* 大约每分钟更新fuzzer统计数据并保存自动标记。 */
|
|
|
|
|
|
if (cur_ms - last_stats_ms > STATS_UPDATE_SEC * 1000) {
|
|
|
|
|
|
last_stats_ms = cur_ms;
|
|
|
write_stats_file(t_byte_ratio, stab_ratio, avg_exec);
|
|
|
save_auto();
|
|
|
write_bitmap();
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 每隔一段时间,写一些情节数据。 */
|
|
|
|
|
|
if (cur_ms - last_plot_ms > PLOT_UPDATE_SEC * 1000) {
|
|
|
|
|
|
last_plot_ms = cur_ms;
|
|
|
maybe_update_plot_file(t_byte_ratio, avg_exec);
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 尊重AFL_EXIT_WHEN_DONE和AFL_BENCH_UNTIL_CRASH。 */
|
|
|
|
|
|
if (!dumb_mode && cycles_wo_finds > 100 && !pending_not_fuzzed &&
|
|
|
getenv("AFL_EXIT_WHEN_DONE")) stop_soon = 2;
|
|
|
|
|
|
if (total_crashes && getenv("AFL_BENCH_UNTIL_CRASH")) stop_soon = 2;
|
|
|
|
|
|
/* 如果我们不在TTY(终端)上,就退出。 */
|
|
|
|
|
|
if (not_on_tty) return;
|
|
|
|
|
|
/* 计算一些稍微有用的位图统计。 */
|
|
|
|
|
|
t_bits = (MAP_SIZE << 3) - count_bits(virgin_bits);
|
|
|
|
|
|
/* 现在,对于视觉效果… */
|
|
|
|
|
|
if (clear_screen) {
|
|
|
|
|
|
SAYF(TERM_CLEAR CURSOR_HIDE);
|
|
|
clear_screen = 0;
|
|
|
|
|
|
check_term_size();
|
|
|
|
|
|
}
|
|
|
|
|
|
SAYF(TERM_HOME);
|
|
|
|
|
|
if (term_too_small) {
|
|
|
|
|
|
SAYF(cBRI "Your terminal is too small to display the UI.\n"
|
|
|
"Please resize terminal window to at least 80x25.\n" cRST);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 让我们从绘制居中横幅开始。*/
|
|
|
|
|
|
banner_len = (crash_mode ? 24 : 22) + strlen(VERSION) + strlen(use_banner);
|
|
|
banner_pad = (80 - banner_len) / 2;
|
|
|
memset(tmp, ' ', banner_pad);
|
|
|
|
|
|
sprintf(tmp + banner_pad, "%s " cLCY VERSION cLGN
|
|
|
" (%s)", crash_mode ? cPIN "peruvian were-rabbit" :
|
|
|
cYEL "american fuzzy lop", use_banner);
|
|
|
|
|
|
SAYF("\n%s\n\n", tmp);
|
|
|
|
|
|
/* "Handy" 快捷键绘制框… */
|
|
|
|
|
|
#define bSTG bSTART cGRA
|
|
|
#define bH2 bH bH
|
|
|
#define bH5 bH2 bH2 bH
|
|
|
#define bH10 bH5 bH5
|
|
|
#define bH20 bH10 bH10
|
|
|
#define bH30 bH20 bH10
|
|
|
#define SP5 " "
|
|
|
#define SP10 SP5 SP5
|
|
|
#define SP20 SP10 SP10
|
|
|
|
|
|
/* Lord, forgive me this. */
|
|
|
|
|
|
SAYF(SET_G1 bSTG bLT bH bSTOP cCYA " process timing " bSTG bH30 bH5 bH2 bHB
|
|
|
bH bSTOP cCYA " overall results " bSTG bH5 bRT "\n");
|
|
|
|
|
|
if (dumb_mode) {
|
|
|
|
|
|
strcpy(tmp, cRST);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
u64 min_wo_finds = (cur_ms - last_path_time) / 1000 / 60;
|
|
|
|
|
|
/* 第一个排队周期:现在不要停下来! */
|
|
|
if (queue_cycle == 1 || min_wo_finds < 15) strcpy(tmp, cMGN); else
|
|
|
|
|
|
/* 在后续的测试周期中,但我们仍在发现新问题。 */
|
|
|
if (cycles_wo_finds < 25 || min_wo_finds < 30) strcpy(tmp, cYEL); else
|
|
|
|
|
|
/* 很长一段时间没有发现,也没有测试用例可以尝试。 */
|
|
|
if (cycles_wo_finds > 100 && !pending_not_fuzzed && min_wo_finds > 120)
|
|
|
strcpy(tmp, cLGN);
|
|
|
|
|
|
/* 默认情况:谨慎地询问是否可以停止? */
|
|
|
else strcpy(tmp, cLBL);
|
|
|
|
|
|
}
|
|
|
|
|
|
SAYF(bV bSTOP " run time : " cRST "%-34s " bSTG bV bSTOP
|
|
|
" cycles done : %s%-5s " bSTG bV "\n",
|
|
|
DTD(cur_ms, start_time), tmp, DI(queue_cycle - 1));
|
|
|
|
|
|
/* 我们想要警告人们,在完成一个完整周期后没有看到新路径的情况,除非是在恢复模糊测试或在非插桩模式下运行。 */
|
|
|
|
|
|
if (!dumb_mode && (last_path_time || resuming_fuzz || queue_cycle == 1 ||
|
|
|
in_bitmap || crash_mode)) {
|
|
|
|
|
|
SAYF(bV bSTOP " last new path : " cRST "%-34s ",
|
|
|
DTD(cur_ms, last_path_time));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (dumb_mode)
|
|
|
|
|
|
SAYF(bV bSTOP " last new path : " cPIN "n/a" cRST
|
|
|
" (non-instrumented mode) ");
|
|
|
|
|
|
else
|
|
|
|
|
|
SAYF(bV bSTOP " last new path : " cRST "none yet " cLRD
|
|
|
"(odd, check syntax!) ");
|
|
|
|
|
|
}
|
|
|
|
|
|
SAYF(bSTG bV bSTOP " total paths : " cRST "%-5s " bSTG bV "\n",
|
|
|
DI(queued_paths));
|
|
|
|
|
|
/* 如果在发现崩溃时用红色高亮显示,并在超过KEEP_UNIQUE_CRASH限制时,在计数后追加一个'+'符号来表示。*/
|
|
|
|
|
|
sprintf(tmp, "%s%s", DI(unique_crashes),
|
|
|
(unique_crashes >= KEEP_UNIQUE_CRASH) ? "+" : "");
|
|
|
|
|
|
SAYF(bV bSTOP " last uniq crash : " cRST "%-34s " bSTG bV bSTOP
|
|
|
" uniq crashes : %s%-6s " bSTG bV "\n",
|
|
|
DTD(cur_ms, last_crash_time), unique_crashes ? cLRD : cRST,
|
|
|
tmp);
|
|
|
|
|
|
sprintf(tmp, "%s%s", DI(unique_hangs),
|
|
|
(unique_hangs >= KEEP_UNIQUE_HANG) ? "+" : "");
|
|
|
|
|
|
SAYF(bV bSTOP " last uniq hang : " cRST "%-34s " bSTG bV bSTOP
|
|
|
" uniq hangs : " cRST "%-6s " bSTG bV "\n",
|
|
|
DTD(cur_ms, last_hang_time), tmp);
|
|
|
|
|
|
SAYF(bVR bH bSTOP cCYA " cycle progress " bSTG bH20 bHB bH bSTOP cCYA
|
|
|
" map coverage " bSTG bH bHT bH20 bH2 bH bVL "\n");
|
|
|
|
|
|
/* 这有点有趣,因为我们想要一起打印几个可变长度的变量,但随后又想将它们
|
|
|
塞进一个固定宽度的字段——所以我们需要先将它们放入一个临时缓冲区。*/
|
|
|
|
|
|
sprintf(tmp, "%s%s (%0.02f%%)", DI(current_entry),
|
|
|
queue_cur->favored ? "" : "*",
|
|
|
((double)current_entry * 100) / queued_paths);
|
|
|
|
|
|
SAYF(bV bSTOP " now processing : " cRST "%-17s " bSTG bV bSTOP, tmp);
|
|
|
|
|
|
sprintf(tmp, "%0.02f%% / %0.02f%%", ((double)queue_cur->bitmap_size) *
|
|
|
100 / MAP_SIZE, t_byte_ratio);
|
|
|
|
|
|
SAYF(" map density : %s%-21s " bSTG bV "\n", t_byte_ratio > 70 ? cLRD :
|
|
|
((t_bytes < 200 && !dumb_mode) ? cPIN : cRST), tmp);
|
|
|
|
|
|
sprintf(tmp, "%s (%0.02f%%)", DI(cur_skipped_paths),
|
|
|
((double)cur_skipped_paths * 100) / queued_paths);
|
|
|
|
|
|
SAYF(bV bSTOP " paths timed out : " cRST "%-17s " bSTG bV, tmp);
|
|
|
|
|
|
sprintf(tmp, "%0.02f bits/tuple",
|
|
|
t_bytes ? (((double)t_bits) / t_bytes) : 0);
|
|
|
|
|
|
SAYF(bSTOP " count coverage : " cRST "%-21s " bSTG bV "\n", tmp);
|
|
|
|
|
|
SAYF(bVR bH bSTOP cCYA " stage progress " bSTG bH20 bX bH bSTOP cCYA
|
|
|
" findings in depth " bSTG bH20 bVL "\n");
|
|
|
|
|
|
sprintf(tmp, "%s (%0.02f%%)", DI(queued_favored),
|
|
|
((double)queued_favored) * 100 / queued_paths);
|
|
|
|
|
|
/* 是的……它还在继续……帮忙? */
|
|
|
|
|
|
SAYF(bV bSTOP " now trying : " cRST "%-21s " bSTG bV bSTOP
|
|
|
" favored paths : " cRST "%-22s " bSTG bV "\n", stage_name, tmp);
|
|
|
|
|
|
if (!stage_max) {
|
|
|
|
|
|
sprintf(tmp, "%s/-", DI(stage_cur));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
sprintf(tmp, "%s/%s (%0.02f%%)", DI(stage_cur), DI(stage_max),
|
|
|
((double)stage_cur) * 100 / stage_max);
|
|
|
|
|
|
}
|
|
|
|
|
|
SAYF(bV bSTOP " stage execs : " cRST "%-21s " bSTG bV bSTOP, tmp);
|
|
|
|
|
|
sprintf(tmp, "%s (%0.02f%%)", DI(queued_with_cov),
|
|
|
((double)queued_with_cov) * 100 / queued_paths);
|
|
|
|
|
|
SAYF(" new edges on : " cRST "%-22s " bSTG bV "\n", tmp);
|
|
|
|
|
|
sprintf(tmp, "%s (%s%s unique)", DI(total_crashes), DI(unique_crashes),
|
|
|
(unique_crashes >= KEEP_UNIQUE_CRASH) ? "+" : "");
|
|
|
|
|
|
if (crash_mode) {
|
|
|
|
|
|
SAYF(bV bSTOP " total execs : " cRST "%-21s " bSTG bV bSTOP
|
|
|
" new crashes : %s%-22s " bSTG bV "\n", DI(total_execs),
|
|
|
unique_crashes ? cLRD : cRST, tmp);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
SAYF(bV bSTOP " total execs : " cRST "%-21s " bSTG bV bSTOP
|
|
|
" total crashes : %s%-22s " bSTG bV "\n", DI(total_execs),
|
|
|
unique_crashes ? cLRD : cRST, tmp);
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 显示关于缓慢执行的警告。 */
|
|
|
|
|
|
if (avg_exec < 100) {
|
|
|
|
|
|
sprintf(tmp, "%s/sec (%s)", DF(avg_exec), avg_exec < 20 ?
|
|
|
"zzzz..." : "slow!");
|
|
|
|
|
|
SAYF(bV bSTOP " exec speed : " cLRD "%-21s ", tmp);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
sprintf(tmp, "%s/sec", DF(avg_exec));
|
|
|
SAYF(bV bSTOP " exec speed : " cRST "%-21s ", tmp);
|
|
|
|
|
|
}
|
|
|
|
|
|
sprintf(tmp, "%s (%s%s unique)", DI(total_tmouts), DI(unique_tmouts),
|
|
|
(unique_hangs >= KEEP_UNIQUE_HANG) ? "+" : "");
|
|
|
|
|
|
SAYF (bSTG bV bSTOP " total tmouts : " cRST "%-22s " bSTG bV "\n", tmp);
|
|
|
|
|
|
/* Aaaalmost那里……坚持住! */
|
|
|
|
|
|
SAYF(bVR bH cCYA bSTOP " fuzzing strategy yields " bSTG bH10 bH bHT bH10
|
|
|
bH5 bHB bH bSTOP cCYA " path geometry " bSTG bH5 bH2 bH bVL "\n");
|
|
|
|
|
|
if (skip_deterministic) {
|
|
|
|
|
|
strcpy(tmp, "n/a, n/a, n/a");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
sprintf(tmp, "%s/%s, %s/%s, %s/%s",
|
|
|
DI(stage_finds[STAGE_FLIP1]), DI(stage_cycles[STAGE_FLIP1]),
|
|
|
DI(stage_finds[STAGE_FLIP2]), DI(stage_cycles[STAGE_FLIP2]),
|
|
|
DI(stage_finds[STAGE_FLIP4]), DI(stage_cycles[STAGE_FLIP4]));
|
|
|
|
|
|
}
|
|
|
|
|
|
SAYF(bV bSTOP " bit flips : " cRST "%-37s " bSTG bV bSTOP " levels : "
|
|
|
cRST "%-10s " bSTG bV "\n", tmp, DI(max_depth));
|
|
|
|
|
|
if (!skip_deterministic)
|
|
|
sprintf(tmp, "%s/%s, %s/%s, %s/%s",
|
|
|
DI(stage_finds[STAGE_FLIP8]), DI(stage_cycles[STAGE_FLIP8]),
|
|
|
DI(stage_finds[STAGE_FLIP16]), DI(stage_cycles[STAGE_FLIP16]),
|
|
|
DI(stage_finds[STAGE_FLIP32]), DI(stage_cycles[STAGE_FLIP32]));
|
|
|
|
|
|
SAYF(bV bSTOP " byte flips : " cRST "%-37s " bSTG bV bSTOP " pending : "
|
|
|
cRST "%-10s " bSTG bV "\n", tmp, DI(pending_not_fuzzed));
|
|
|
|
|
|
if (!skip_deterministic)
|
|
|
sprintf(tmp, "%s/%s, %s/%s, %s/%s",
|
|
|
DI(stage_finds[STAGE_ARITH8]), DI(stage_cycles[STAGE_ARITH8]),
|
|
|
DI(stage_finds[STAGE_ARITH16]), DI(stage_cycles[STAGE_ARITH16]),
|
|
|
DI(stage_finds[STAGE_ARITH32]), DI(stage_cycles[STAGE_ARITH32]));
|
|
|
|
|
|
SAYF(bV bSTOP " arithmetics : " cRST "%-37s " bSTG bV bSTOP " pend fav : "
|
|
|
cRST "%-10s " bSTG bV "\n", tmp, DI(pending_favored));
|
|
|
|
|
|
if (!skip_deterministic)
|
|
|
sprintf(tmp, "%s/%s, %s/%s, %s/%s",
|
|
|
DI(stage_finds[STAGE_INTEREST8]), DI(stage_cycles[STAGE_INTEREST8]),
|
|
|
DI(stage_finds[STAGE_INTEREST16]), DI(stage_cycles[STAGE_INTEREST16]),
|
|
|
DI(stage_finds[STAGE_INTEREST32]), DI(stage_cycles[STAGE_INTEREST32]));
|
|
|
|
|
|
SAYF(bV bSTOP " known ints : " cRST "%-37s " bSTG bV bSTOP " own finds : "
|
|
|
cRST "%-10s " bSTG bV "\n", tmp, DI(queued_discovered));
|
|
|
|
|
|
if (!skip_deterministic)
|
|
|
sprintf(tmp, "%s/%s, %s/%s, %s/%s",
|
|
|
DI(stage_finds[STAGE_EXTRAS_UO]), DI(stage_cycles[STAGE_EXTRAS_UO]),
|
|
|
DI(stage_finds[STAGE_EXTRAS_UI]), DI(stage_cycles[STAGE_EXTRAS_UI]),
|
|
|
DI(stage_finds[STAGE_EXTRAS_AO]), DI(stage_cycles[STAGE_EXTRAS_AO]));
|
|
|
|
|
|
SAYF(bV bSTOP " dictionary : " cRST "%-37s " bSTG bV bSTOP
|
|
|
" imported : " cRST "%-10s " bSTG bV "\n", tmp,
|
|
|
sync_id ? DI(queued_imported) : (u8*)"n/a");
|
|
|
|
|
|
sprintf(tmp, "%s/%s, %s/%s",
|
|
|
DI(stage_finds[STAGE_HAVOC]), DI(stage_cycles[STAGE_HAVOC]),
|
|
|
DI(stage_finds[STAGE_SPLICE]), DI(stage_cycles[STAGE_SPLICE]));
|
|
|
|
|
|
SAYF(bV bSTOP " havoc : " cRST "%-37s " bSTG bV bSTOP, tmp);
|
|
|
|
|
|
if (t_bytes) sprintf(tmp, "%0.02f%%", stab_ratio);
|
|
|
else strcpy(tmp, "n/a");
|
|
|
|
|
|
SAYF(" stability : %s%-10s " bSTG bV "\n", (stab_ratio < 85 && var_byte_count > 40)
|
|
|
? cLRD : ((queued_variable && (!persistent_mode || var_byte_count > 20))
|
|
|
? cMGN : cRST), tmp);
|
|
|
|
|
|
if (!bytes_trim_out) {
|
|
|
|
|
|
sprintf(tmp, "n/a, ");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
sprintf(tmp, "%0.02f%%/%s, ",
|
|
|
((double)(bytes_trim_in - bytes_trim_out)) * 100 / bytes_trim_in,
|
|
|
DI(trim_execs));
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!blocks_eff_total) {
|
|
|
|
|
|
u8 tmp2[128];
|
|
|
|
|
|
sprintf(tmp2, "n/a");
|
|
|
strcat(tmp, tmp2);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
u8 tmp2[128];
|
|
|
|
|
|
sprintf(tmp2, "%0.02f%%",
|
|
|
((double)(blocks_eff_total - blocks_eff_select)) * 100 /
|
|
|
blocks_eff_total);
|
|
|
|
|
|
strcat(tmp, tmp2);
|
|
|
|
|
|
}
|
|
|
|
|
|
SAYF(bV bSTOP " trim : " cRST "%-37s " bSTG bVR bH20 bH2 bH2 bRB "\n"
|
|
|
bLB bH30 bH20 bH2 bH bRB bSTOP cRST RESET_G1, tmp);
|
|
|
|
|
|
/* 提供一些CPU利用率统计信息。 */
|
|
|
|
|
|
if (cpu_core_count) {
|
|
|
|
|
|
double cur_runnable = get_runnable_processes();
|
|
|
u32 cur_utilization = cur_runnable * 100 / cpu_core_count;
|
|
|
|
|
|
u8* cpu_color = cCYA;
|
|
|
|
|
|
/* 如果我们仍然可以运行一个或多个进程,则使用绿色。 */
|
|
|
|
|
|
if (cpu_core_count > 1 && cur_runnable + 1 <= cpu_core_count)
|
|
|
cpu_color = cLGN;
|
|
|
|
|
|
/* 如果我们明显超额认购,就用红色。 */
|
|
|
|
|
|
if (!no_cpu_meter_red && cur_utilization >= 150) cpu_color = cLRD;
|
|
|
|
|
|
#ifdef HAVE_AFFINITY
|
|
|
|
|
|
if (cpu_aff >= 0) {
|
|
|
|
|
|
SAYF(SP10 cGRA "[cpu%03u:%s%3u%%" cGRA "]\r" cRST,
|
|
|
MIN(cpu_aff, 999), cpu_color,
|
|
|
MIN(cur_utilization, 999));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
SAYF(SP10 cGRA " [cpu:%s%3u%%" cGRA "]\r" cRST,
|
|
|
cpu_color, MIN(cur_utilization, 999));
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
|
|
SAYF(SP10 cGRA " [cpu:%s%3u%%" cGRA "]\r" cRST,
|
|
|
cpu_color, MIN(cur_utilization, 999));
|
|
|
|
|
|
#endif /* ^HAVE_AFFINITY */
|
|
|
|
|
|
} else SAYF("\r");
|
|
|
|
|
|
/* Hallelujah! */
|
|
|
|
|
|
fflush(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 在处理完输入目录后显示快速统计信息,以及一堆警告。一些校准内容也
|
|
|
最终放在这里,连同几个硬编码的常数。也许最终会清理一下。 */
|
|
|
|
|
|
static void show_init_stats(void) {
|
|
|
|
|
|
struct queue_entry* q = queue;
|
|
|
u32 min_bits = 0, max_bits = 0;
|
|
|
u64 min_us = 0, max_us = 0;
|
|
|
u64 avg_us = 0;
|
|
|
u32 max_len = 0;
|
|
|
|
|
|
if (total_cal_cycles) avg_us = total_cal_us / total_cal_cycles;
|
|
|
|
|
|
while (q) {
|
|
|
|
|
|
if (!min_us || q->exec_us < min_us) min_us = q->exec_us;
|
|
|
if (q->exec_us > max_us) max_us = q->exec_us;
|
|
|
|
|
|
if (!min_bits || q->bitmap_size < min_bits) min_bits = q->bitmap_size;
|
|
|
if (q->bitmap_size > max_bits) max_bits = q->bitmap_size;
|
|
|
|
|
|
if (q->len > max_len) max_len = q->len;
|
|
|
|
|
|
q = q->next;
|
|
|
|
|
|
}
|
|
|
|
|
|
SAYF("\n");
|
|
|
|
|
|
if (avg_us > (qemu_mode ? 50000 : 10000))
|
|
|
WARNF(cLRD "The target binary is pretty slow! See %s/perf_tips.txt.",
|
|
|
doc_path);
|
|
|
|
|
|
/* 让我们继续使用慢二进制代码。 */
|
|
|
|
|
|
if (avg_us > 50000) havoc_div = 10; /* 0-19 execs/sec */
|
|
|
else if (avg_us > 20000) havoc_div = 5; /* 20-49 execs/sec */
|
|
|
else if (avg_us > 10000) havoc_div = 2; /* 50-100 execs/sec */
|
|
|
|
|
|
if (!resuming_fuzz) {
|
|
|
|
|
|
if (max_len > 50 * 1024)
|
|
|
WARNF(cLRD "Some test cases are huge (%s) - see %s/perf_tips.txt!",
|
|
|
DMS(max_len), doc_path);
|
|
|
else if (max_len > 10 * 1024)
|
|
|
WARNF("Some test cases are big (%s) - see %s/perf_tips.txt.",
|
|
|
DMS(max_len), doc_path);
|
|
|
|
|
|
if (useless_at_start && !in_bitmap)
|
|
|
WARNF(cLRD "Some test cases look useless. Consider using a smaller set.");
|
|
|
|
|
|
if (queued_paths > 100)
|
|
|
WARNF(cLRD "You probably have far too many input files! Consider trimming down.");
|
|
|
else if (queued_paths > 20)
|
|
|
WARNF("You have lots of input files; try starting small.");
|
|
|
|
|
|
}
|
|
|
|
|
|
OKF("Here are some useful stats:\n\n"
|
|
|
|
|
|
cGRA " Test case count : " cRST "%u favored, %u variable, %u total\n"
|
|
|
cGRA " Bitmap range : " cRST "%u to %u bits (average: %0.02f bits)\n"
|
|
|
cGRA " Exec timing : " cRST "%s to %s us (average: %s us)\n",
|
|
|
queued_favored, queued_variable, queued_paths, min_bits, max_bits,
|
|
|
((double)total_bitmap_size) / (total_bitmap_entries ? total_bitmap_entries : 1),
|
|
|
DI(min_us), DI(max_us), DI(avg_us));
|
|
|
|
|
|
if (!timeout_given) {
|
|
|
|
|
|
/* 确定合适的超时时间。基本思路是:5倍的平均值或者1倍的最大值,四舍五入到EXEC_TM_ROUND毫秒,并限制在1秒内。
|
|
|
|
|
|
如果程序运行缓慢,乘数会降低到2倍或3倍,因为随机调度抖动不太可能有任何影响,而且我们的耐心正在逐渐减少=) */
|
|
|
|
|
|
if (avg_us > 50000) exec_tmout = avg_us * 2 / 1000;
|
|
|
else if (avg_us > 10000) exec_tmout = avg_us * 3 / 1000;
|
|
|
else exec_tmout = avg_us * 5 / 1000;
|
|
|
|
|
|
exec_tmout = MAX(exec_tmout, max_us / 1000);
|
|
|
exec_tmout = (exec_tmout + EXEC_TM_ROUND) / EXEC_TM_ROUND * EXEC_TM_ROUND;
|
|
|
|
|
|
if (exec_tmout > EXEC_TIMEOUT) exec_tmout = EXEC_TIMEOUT;
|
|
|
|
|
|
ACTF("No -t option specified, so I'll use exec timeout of %u ms.",
|
|
|
exec_tmout);
|
|
|
|
|
|
timeout_given = 1;
|
|
|
|
|
|
} else if (timeout_given == 3) {
|
|
|
|
|
|
ACTF("Applying timeout settings from resumed session (%u ms).", exec_tmout);
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 在dump模式下,用宽裕的时间限制重新运行每个超时的测试用例代价很高,因此我们选择一个更保守的默认值。 */
|
|
|
|
|
|
if (dumb_mode && !getenv("AFL_HANG_TMOUT"))
|
|
|
hang_tmout = MIN(EXEC_TIMEOUT, exec_tmout * 2 + 100);
|
|
|
|
|
|
OKF("All set and ready to roll!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 求大于等于val的2的第一次幂(假设val小于
|
|
|
2^31). */
|
|
|
|
|
|
static u32 next_p2(u32 val) {
|
|
|
|
|
|
u32 ret = 1;
|
|
|
while (val > ret) ret <<= 1;
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 在进行确定性检查时,修剪所有新的测试用例以节省周期。修剪器使用二的幂次方增加
|
|
|
量,范围在文件大小的1/16到1/1024之间,以保持这个阶段简短而高效。 */
|
|
|
|
|
|
static u8 trim_case(char** argv, struct queue_entry* q, u8* in_buf) {
|
|
|
|
|
|
static u8 tmp[64];
|
|
|
static u8 clean_trace[MAP_SIZE];
|
|
|
|
|
|
u8 needs_write = 0, fault = 0;
|
|
|
u32 trim_exec = 0;
|
|
|
u32 remove_len;
|
|
|
u32 len_p2;
|
|
|
|
|
|
/* 尽管在检测到可变行为时,修剪器的效用会降低,但它仍然会有一定程度的作用,因此我们不对此进行检查。 */
|
|
|
|
|
|
if (q->len < 5) return 0;
|
|
|
|
|
|
stage_name = tmp;
|
|
|
bytes_trim_in += q->len;
|
|
|
|
|
|
/* 选择初始块len,从较大的步骤开始。*/
|
|
|
|
|
|
len_p2 = next_p2(q->len);
|
|
|
|
|
|
remove_len = MAX(len_p2 / TRIM_START_STEPS, TRIM_MIN_BYTES);
|
|
|
|
|
|
/* 继续进行,直到步骤数变得过高或步长变得过小。 */
|
|
|
|
|
|
while (remove_len >= MAX(len_p2 / TRIM_END_STEPS, TRIM_MIN_BYTES)) {
|
|
|
|
|
|
u32 remove_pos = remove_len;
|
|
|
|
|
|
sprintf(tmp, "trim %s/%s", DI(remove_len), DI(remove_len));
|
|
|
|
|
|
stage_cur = 0;
|
|
|
stage_max = q->len / remove_len;
|
|
|
|
|
|
while (remove_pos < q->len) {
|
|
|
|
|
|
u32 trim_avail = MIN(remove_len, q->len - remove_pos);
|
|
|
u32 cksum;
|
|
|
|
|
|
write_with_gap(in_buf, q->len, remove_pos, trim_avail);
|
|
|
|
|
|
fault = run_target(argv, exec_tmout);
|
|
|
trim_execs++;
|
|
|
|
|
|
if (stop_soon || fault == FAULT_ERROR) goto abort_trimming;
|
|
|
|
|
|
/* 请注意,我们在这里不跟踪崩溃或挂起;也许做? */
|
|
|
|
|
|
cksum = hash32(trace_bits, MAP_SIZE, HASH_CONST);
|
|
|
|
|
|
/* 如果删除对跟踪没有影响,使其成为永久性的。这对于变量路径输入来说并不
|
|
|
完美,但我们只是在尽力而为,所以如果我们偶尔出现假阴性,也不是什么大问题。 */
|
|
|
|
|
|
if (cksum == q->exec_cksum) {
|
|
|
|
|
|
u32 move_tail = q->len - remove_pos - trim_avail;
|
|
|
|
|
|
q->len -= trim_avail;
|
|
|
len_p2 = next_p2(q->len);
|
|
|
|
|
|
memmove(in_buf + remove_pos, in_buf + remove_pos + trim_avail,
|
|
|
move_tail);
|
|
|
|
|
|
/* 让我们保存一个干净的跟踪信息,在我们完成修剪工作后,update_bitmap_score函数将会需要它。*/
|
|
|
|
|
|
if (!needs_write) {
|
|
|
|
|
|
needs_write = 1;
|
|
|
memcpy(clean_trace, trace_bits, MAP_SIZE);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else remove_pos += remove_len;
|
|
|
|
|
|
/* 因为这可能很慢,所以要时不时地更新屏幕。 */
|
|
|
|
|
|
if (!(trim_exec++ % stats_update_freq)) show_stats();
|
|
|
stage_cur++;
|
|
|
|
|
|
}
|
|
|
|
|
|
remove_len >>= 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 如果我们对in_buf做了更改,我们还需要更新磁盘上的测试用例版本。 */
|
|
|
|
|
|
if (needs_write) {
|
|
|
|
|
|
s32 fd;
|
|
|
|
|
|
unlink(q->fname); /* ignore errors */
|
|
|
|
|
|
fd = open(q->fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", q->fname);
|
|
|
|
|
|
ck_write(fd, in_buf, q->len, q->fname);
|
|
|
close(fd);
|
|
|
|
|
|
memcpy(trace_bits, clean_trace, MAP_SIZE);
|
|
|
update_bitmap_score(q);
|
|
|
|
|
|
}
|
|
|
|
|
|
abort_trimming:
|
|
|
|
|
|
bytes_trim_out += q->len;
|
|
|
return fault;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 写入一个修改后的测试用例,运行程序,处理结果。处理错误条件,如果到
|
|
|
了该退出的时候则返回1。这是fuzz_one()的辅助函数。 */
|
|
|
|
|
|
EXP_ST u8 common_fuzz_stuff(char** argv, u8* out_buf, u32 len) {
|
|
|
|
|
|
u8 fault;
|
|
|
|
|
|
if (post_handler) {
|
|
|
|
|
|
out_buf = post_handler(out_buf, &len);
|
|
|
if (!out_buf || !len) return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
write_to_testcase(out_buf, len);
|
|
|
|
|
|
fault = run_target(argv, exec_tmout);
|
|
|
|
|
|
if (stop_soon) return 1;
|
|
|
|
|
|
if (fault == FAULT_TMOUT) {
|
|
|
|
|
|
if (subseq_tmouts++ > TMOUT_LIMIT) {
|
|
|
cur_skipped_paths++;
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
} else subseq_tmouts = 0;
|
|
|
|
|
|
/* 用户可以通过发送SIGUSR1信号来请求放弃当前的输入。 */
|
|
|
|
|
|
if (skip_requested) {
|
|
|
|
|
|
skip_requested = 0;
|
|
|
cur_skipped_paths++;
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 这将为我们处理FAULT_ERROR: */
|
|
|
|
|
|
queued_discovered += save_if_interesting(argv, out_buf, len, fault);
|
|
|
|
|
|
if (!(stage_cur % stats_update_freq) || stage_cur + 1 == stage_max)
|
|
|
show_stats();
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 在fuzz_one()中用于选择块操作的随机块长度的辅助函数。只要最大长度max_len大于0,就不会返回零。 */
|
|
|
|
|
|
static u32 choose_block_len(u32 limit) {
|
|
|
|
|
|
u32 min_value, max_value;
|
|
|
u32 rlim = MIN(queue_cycle, 3);
|
|
|
|
|
|
if (!run_over10m) rlim = 1;
|
|
|
|
|
|
switch (UR(rlim)) {
|
|
|
|
|
|
case 0: min_value = 1;
|
|
|
max_value = HAVOC_BLK_SMALL;
|
|
|
break;
|
|
|
|
|
|
case 1: min_value = HAVOC_BLK_SMALL;
|
|
|
max_value = HAVOC_BLK_MEDIUM;
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
if (UR(10)) {
|
|
|
|
|
|
min_value = HAVOC_BLK_MEDIUM;
|
|
|
max_value = HAVOC_BLK_LARGE;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
min_value = HAVOC_BLK_LARGE;
|
|
|
max_value = HAVOC_BLK_XL;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (min_value >= limit) min_value = 1;
|
|
|
|
|
|
return min_value + UR(MIN(max_value, limit) - min_value + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 计算案例的期望分数,以调整havoc模糊测试的长度。这是fuzz_one()的辅助函数。也许这些常数中的一些应该放入config.h中。*/
|
|
|
|
|
|
static u32 calculate_score(struct queue_entry* q) {
|
|
|
|
|
|
u32 avg_exec_us = total_cal_us / total_cal_cycles;
|
|
|
u32 avg_bitmap_size = total_bitmap_size / total_bitmap_entries;
|
|
|
u32 perf_score = 100;
|
|
|
|
|
|
/* 根据此路径的执行速度与全局平均速度的比较,调整分数。乘数范围从0.1x到3x。快速输入的模糊测试成本较低,因此我们给予它们更多的测试时间。 */
|
|
|
|
|
|
if (q->exec_us * 0.1 > avg_exec_us) perf_score = 10;
|
|
|
else if (q->exec_us * 0.25 > avg_exec_us) perf_score = 25;
|
|
|
else if (q->exec_us * 0.5 > avg_exec_us) perf_score = 50;
|
|
|
else if (q->exec_us * 0.75 > avg_exec_us) perf_score = 75;
|
|
|
else if (q->exec_us * 4 < avg_exec_us) perf_score = 300;
|
|
|
else if (q->exec_us * 3 < avg_exec_us) perf_score = 200;
|
|
|
else if (q->exec_us * 2 < avg_exec_us) perf_score = 150;
|
|
|
|
|
|
/* 根据位图大小调整分数。工作理论是更好的覆盖率转化为更好的目标。乘数从0.25x到3x。 */
|
|
|
|
|
|
if (q->bitmap_size * 0.3 > avg_bitmap_size) perf_score *= 3;
|
|
|
else if (q->bitmap_size * 0.5 > avg_bitmap_size) perf_score *= 2;
|
|
|
else if (q->bitmap_size * 0.75 > avg_bitmap_size) perf_score *= 1.5;
|
|
|
else if (q->bitmap_size * 3 < avg_bitmap_size) perf_score *= 0.25;
|
|
|
else if (q->bitmap_size * 2 < avg_bitmap_size) perf_score *= 0.5;
|
|
|
else if (q->bitmap_size * 1.5 < avg_bitmap_size) perf_score *= 0.75;
|
|
|
|
|
|
/* 根据handicap(障碍)调整分数。Handicap与我们在游戏中了解这条路径的时间成比例。后来者被允许运行更长时间,直到它们赶上其他路径。 */
|
|
|
|
|
|
if (q->handicap >= 4) {
|
|
|
|
|
|
perf_score *= 4;
|
|
|
q->handicap -= 4;
|
|
|
|
|
|
} else if (q->handicap) {
|
|
|
|
|
|
perf_score *= 2;
|
|
|
q->handicap--;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 基于输入深度的最终调整,假设模糊测试更深层的测试用例更有可能揭示传统模糊测试工具无法发现的问题。 */
|
|
|
|
|
|
switch (q->depth) {
|
|
|
|
|
|
case 0 ... 3: break;
|
|
|
case 4 ... 7: perf_score *= 2; break;
|
|
|
case 8 ... 13: perf_score *= 3; break;
|
|
|
case 14 ... 25: perf_score *= 4; break;
|
|
|
default: perf_score *= 5;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 确保我们不超过极限 */
|
|
|
|
|
|
if (perf_score > HAVOC_MAX_MULT * 100) perf_score = HAVOC_MAX_MULT * 100;
|
|
|
|
|
|
return perf_score;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 辅助函数,用于检查特定的变化(xor_val = old ^ new)是否可能是由afl-fuzz尝试的长度
|
|
|
和步长确定性位翻转产生的结果。这用于避免在一些跟随位翻转的确定性模糊测试操作中的重复
|
|
|
项。如果xor_val为零,我们也返回1,这意味着旧值和尝试的新值是相同的,执行将是一种时间
|
|
|
浪费。*/
|
|
|
|
|
|
static u8 could_be_bitflip(u32 xor_val) {
|
|
|
|
|
|
u32 sh = 0;
|
|
|
|
|
|
if (!xor_val) return 1;
|
|
|
|
|
|
/* 左移直到第一个位设置 */
|
|
|
|
|
|
while (!(xor_val & 1)) { sh++; xor_val >>= 1; }
|
|
|
|
|
|
/* 1-、2-和4-bit模式在任何地方都是可以的。 */
|
|
|
|
|
|
if (xor_val == 1 || xor_val == 3 || xor_val == 15) return 1;
|
|
|
|
|
|
/* 8-,、16-、和 32-bit模式只有当位移因子能被8整除时,模式才有效,因为这是这些操作的步长。 */
|
|
|
|
|
|
if (sh & 7) return 0;
|
|
|
|
|
|
if (xor_val == 0xff || xor_val == 0xffff || xor_val == 0xffffffff)
|
|
|
return 1;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 辅助函数,用于检查是否可以通过算术操作达到某个特定值。用于类似的目的。 */
|
|
|
|
|
|
static u8 could_be_arith(u32 old_val, u32 new_val, u8 blen) {
|
|
|
|
|
|
u32 i, ov = 0, nv = 0, diffs = 0;
|
|
|
|
|
|
if (old_val == new_val) return 1;
|
|
|
|
|
|
/* 看看对任何字节进行一个字节的调整是否会产生此结果。 */
|
|
|
|
|
|
for (i = 0; i < blen; i++) {
|
|
|
|
|
|
u8 a = old_val >> (8 * i),
|
|
|
b = new_val >> (8 * i);
|
|
|
|
|
|
if (a != b) { diffs++; ov = a; nv = b; }
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 如果只有一个字节不同并且值在范围内,则返回1。*/
|
|
|
|
|
|
if (diffs == 1) {
|
|
|
|
|
|
if ((u8)(ov - nv) <= ARITH_MAX ||
|
|
|
(u8)(nv - ov) <= ARITH_MAX) return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (blen == 1) return 0;
|
|
|
|
|
|
/* 看看对任何字节进行两个字节的调整是否会产生此结果。 */
|
|
|
|
|
|
diffs = 0;
|
|
|
|
|
|
for (i = 0; i < blen / 2; i++) {
|
|
|
|
|
|
u16 a = old_val >> (16 * i),
|
|
|
b = new_val >> (16 * i);
|
|
|
|
|
|
if (a != b) { diffs++; ov = a; nv = b; }
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 如果只有一个字不同且值在范围内,则返回1。 */
|
|
|
|
|
|
if (diffs == 1) {
|
|
|
|
|
|
if ((u16)(ov - nv) <= ARITH_MAX ||
|
|
|
(u16)(nv - ov) <= ARITH_MAX) return 1;
|
|
|
|
|
|
ov = SWAP16(ov); nv = SWAP16(nv);
|
|
|
|
|
|
if ((u16)(ov - nv) <= ARITH_MAX ||
|
|
|
(u16)(nv - ov) <= ARITH_MAX) return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 最后,让我们对dwords做同样的事情。 */
|
|
|
|
|
|
if (blen == 4) {
|
|
|
|
|
|
if ((u32)(old_val - new_val) <= ARITH_MAX ||
|
|
|
(u32)(new_val - old_val) <= ARITH_MAX) return 1;
|
|
|
|
|
|
new_val = SWAP32(new_val);
|
|
|
old_val = SWAP32(old_val);
|
|
|
|
|
|
if ((u32)(old_val - new_val) <= ARITH_MAX ||
|
|
|
(u32)(new_val - old_val) <= ARITH_MAX) return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* 最后但同样重要的是,一个类似的辅助函数,用于检查在给定较短blen的插入操作后,
|
|
|
插入一个有趣的整数是否是多余的。最后一个参数(check_le)如果设置,意味着调用者
|
|
|
已经为当前blen执行了LE(小端)插入,并希望查看传入的新值中的BE(大端)变体是否是唯一的。 */
|
|
|
|
|
|
static u8 could_be_interest(u32 old_val, u32 new_val, u8 blen, u8 check_le) {
|
|
|
|
|
|
u32 i, j;
|
|
|
|
|
|
if (old_val == new_val) return 1;
|
|
|
|
|
|
/* See if one-byte insertions from interesting_8 over old_val could
|
|
|
produce new_val. */
|
|
|
|
|
|
for (i = 0; i < blen; i++) {
|
|
|
|
|
|
for (j = 0; j < sizeof(interesting_8); j++) {
|
|
|
|
|
|
u32 tval = (old_val & ~(0xff << (i * 8))) |
|
|
|
(((u8)interesting_8[j]) << (i * 8));
|
|
|
|
|
|
if (new_val == tval) return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Bail out unless we're also asked to examine two-byte LE insertions
|
|
|
as a preparation for BE attempts. */
|
|
|
|
|
|
if (blen == 2 && !check_le) return 0;
|
|
|
|
|
|
/* See if two-byte insertions over old_val could give us new_val. */
|
|
|
|
|
|
for (i = 0; i < blen - 1; i++) {
|
|
|
|
|
|
for (j = 0; j < sizeof(interesting_16) / 2; j++) {
|
|
|
|
|
|
u32 tval = (old_val & ~(0xffff << (i * 8))) |
|
|
|
(((u16)interesting_16[j]) << (i * 8));
|
|
|
|
|
|
if (new_val == tval) return 1;
|
|
|
|
|
|
/* Continue here only if blen > 2. */
|
|
|
|
|
|
if (blen > 2) {
|
|
|
|
|
|
tval = (old_val & ~(0xffff << (i * 8))) |
|
|
|
(SWAP16(interesting_16[j]) << (i * 8));
|
|
|
|
|
|
if (new_val == tval) return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (blen == 4 && check_le) {
|
|
|
|
|
|
/* See if four-byte insertions could produce the same result
|
|
|
(LE only). */
|
|
|
|
|
|
for (j = 0; j < sizeof(interesting_32) / 4; j++)
|
|
|
if (new_val == (u32)interesting_32[j]) return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Take the current entry from the queue, fuzz it for a while. This
|
|
|
function is a tad too long... returns 0 if fuzzed successfully, 1 if
|
|
|
skipped or bailed out. */
|
|
|
|
|
|
static u8 fuzz_one(char** argv) {
|
|
|
|
|
|
s32 len, fd, temp_len, i, j;
|
|
|
u8 *in_buf, *out_buf, *orig_in, *ex_tmp, *eff_map = 0;
|
|
|
u64 havoc_queued, orig_hit_cnt, new_hit_cnt;
|
|
|
u32 splice_cycle = 0, perf_score = 100, orig_perf, prev_cksum, eff_cnt = 1;
|
|
|
|
|
|
u8 ret_val = 1, doing_det = 0;
|
|
|
|
|
|
u8 a_collect[MAX_AUTO_EXTRA];
|
|
|
u32 a_len = 0;
|
|
|
|
|
|
#ifdef IGNORE_FINDS
|
|
|
|
|
|
/* In IGNORE_FINDS mode, skip any entries that weren't in the
|
|
|
initial data set. */
|
|
|
|
|
|
if (queue_cur->depth > 1) return 1;
|
|
|
|
|
|
#else
|
|
|
|
|
|
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 ((queue_cur->was_fuzzed || !queue_cur->favored) &&
|
|
|
UR(100) < SKIP_TO_NEW_PROB) return 1;
|
|
|
|
|
|
} else if (!dumb_mode && !queue_cur->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 && !queue_cur->was_fuzzed) {
|
|
|
|
|
|
if (UR(100) < SKIP_NFAV_NEW_PROB) return 1;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (UR(100) < SKIP_NFAV_OLD_PROB) return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* ^IGNORE_FINDS */
|
|
|
|
|
|
if (not_on_tty) {
|
|
|
ACTF("Fuzzing test case #%u (%u total, %llu uniq crashes found)...",
|
|
|
current_entry, queued_paths, unique_crashes);
|
|
|
fflush(stdout);
|
|
|
}
|
|
|
|
|
|
/* Map the test case into memory. */
|
|
|
|
|
|
fd = open(queue_cur->fname, O_RDONLY);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to open '%s'", queue_cur->fname);
|
|
|
|
|
|
len = queue_cur->len;
|
|
|
|
|
|
orig_in = in_buf = mmap(0, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
|
|
|
|
|
|
if (orig_in == MAP_FAILED) PFATAL("Unable to mmap '%s'", queue_cur->fname);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
/* We could mmap() out_buf as MAP_PRIVATE, but we end up clobbering every
|
|
|
single byte anyway, so it wouldn't give us any performance or memory usage
|
|
|
benefits. */
|
|
|
|
|
|
out_buf = ck_alloc_nozero(len);
|
|
|
|
|
|
subseq_tmouts = 0;
|
|
|
|
|
|
cur_depth = queue_cur->depth;
|
|
|
|
|
|
/*******************************************
|
|
|
* CALIBRATION (only if failed earlier on) *
|
|
|
*******************************************/
|
|
|
|
|
|
if (queue_cur->cal_failed) {
|
|
|
|
|
|
u8 res = FAULT_TMOUT;
|
|
|
|
|
|
if (queue_cur->cal_failed < CAL_CHANCES) {
|
|
|
|
|
|
/* Reset exec_cksum to tell calibrate_case to re-execute the testcase
|
|
|
avoiding the usage of an invalid trace_bits.
|
|
|
For more info: https://github.com/AFLplusplus/AFLplusplus/pull/425 */
|
|
|
|
|
|
queue_cur->exec_cksum = 0;
|
|
|
|
|
|
res = calibrate_case(argv, queue_cur, in_buf, queue_cycle - 1, 0);
|
|
|
|
|
|
if (res == FAULT_ERROR)
|
|
|
FATAL("Unable to execute target application");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (stop_soon || res != crash_mode) {
|
|
|
cur_skipped_paths++;
|
|
|
goto abandon_entry;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/************
|
|
|
* TRIMMING *
|
|
|
************/
|
|
|
|
|
|
if (!dumb_mode && !queue_cur->trim_done) {
|
|
|
|
|
|
u8 res = trim_case(argv, queue_cur, in_buf);
|
|
|
|
|
|
if (res == FAULT_ERROR)
|
|
|
FATAL("Unable to execute target application");
|
|
|
|
|
|
if (stop_soon) {
|
|
|
cur_skipped_paths++;
|
|
|
goto abandon_entry;
|
|
|
}
|
|
|
|
|
|
/* Don't retry trimming, even if it failed. */
|
|
|
|
|
|
queue_cur->trim_done = 1;
|
|
|
|
|
|
if (len != queue_cur->len) len = queue_cur->len;
|
|
|
|
|
|
}
|
|
|
|
|
|
memcpy(out_buf, in_buf, len);
|
|
|
|
|
|
/*********************
|
|
|
* PERFORMANCE SCORE *
|
|
|
*********************/
|
|
|
|
|
|
orig_perf = perf_score = calculate_score(queue_cur);
|
|
|
|
|
|
/* Skip right away if -d is given, if we have done deterministic fuzzing on
|
|
|
this entry ourselves (was_fuzzed), or if it has gone through deterministic
|
|
|
testing in earlier, resumed runs (passed_det). */
|
|
|
|
|
|
if (skip_deterministic || queue_cur->was_fuzzed || queue_cur->passed_det)
|
|
|
goto havoc_stage;
|
|
|
|
|
|
/* Skip deterministic fuzzing if exec path checksum puts this out of scope
|
|
|
for this master instance. */
|
|
|
|
|
|
if (master_max && (queue_cur->exec_cksum % master_max) != master_id - 1)
|
|
|
goto havoc_stage;
|
|
|
|
|
|
doing_det = 1;
|
|
|
|
|
|
/*********************************************
|
|
|
* SIMPLE BITFLIP (+dictionary construction) *
|
|
|
*********************************************/
|
|
|
|
|
|
#define FLIP_BIT(_ar, _b) do { \
|
|
|
u8* _arf = (u8*)(_ar); \
|
|
|
u32 _bf = (_b); \
|
|
|
_arf[(_bf) >> 3] ^= (128 >> ((_bf) & 7)); \
|
|
|
} while (0)
|
|
|
|
|
|
/* Single walking bit. */
|
|
|
|
|
|
stage_short = "flip1";
|
|
|
stage_max = len << 3;
|
|
|
stage_name = "bitflip 1/1";
|
|
|
|
|
|
stage_val_type = STAGE_VAL_NONE;
|
|
|
|
|
|
orig_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
prev_cksum = queue_cur->exec_cksum;
|
|
|
|
|
|
for (stage_cur = 0; stage_cur < stage_max; stage_cur++) {
|
|
|
|
|
|
stage_cur_byte = stage_cur >> 3;
|
|
|
|
|
|
FLIP_BIT(out_buf, stage_cur);
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
|
|
|
FLIP_BIT(out_buf, stage_cur);
|
|
|
|
|
|
/* While flipping the least significant bit in every byte, pull of an extra
|
|
|
trick to detect possible syntax tokens. In essence, the idea is that if
|
|
|
you have a binary blob like this:
|
|
|
|
|
|
xxxxxxxxIHDRxxxxxxxx
|
|
|
|
|
|
...and changing the leading and trailing bytes causes variable or no
|
|
|
changes in program flow, but touching any character in the "IHDR" string
|
|
|
always produces the same, distinctive path, it's highly likely that
|
|
|
"IHDR" is an atomically-checked magic value of special significance to
|
|
|
the fuzzed format.
|
|
|
|
|
|
We do this here, rather than as a separate stage, because it's a nice
|
|
|
way to keep the operation approximately "free" (i.e., no extra execs).
|
|
|
|
|
|
Empirically, performing the check when flipping the least significant bit
|
|
|
is advantageous, compared to doing it at the time of more disruptive
|
|
|
changes, where the program flow may be affected in more violent ways.
|
|
|
|
|
|
The caveat is that we won't generate dictionaries in the -d mode or -S
|
|
|
mode - but that's probably a fair trade-off.
|
|
|
|
|
|
This won't work particularly well with paths that exhibit variable
|
|
|
behavior, but fails gracefully, so we'll carry out the checks anyway.
|
|
|
|
|
|
*/
|
|
|
|
|
|
if (!dumb_mode && (stage_cur & 7) == 7) {
|
|
|
|
|
|
u32 cksum = hash32(trace_bits, MAP_SIZE, HASH_CONST);
|
|
|
|
|
|
if (stage_cur == stage_max - 1 && cksum == prev_cksum) {
|
|
|
|
|
|
/* If at end of file and we are still collecting a string, grab the
|
|
|
final character and force output. */
|
|
|
|
|
|
if (a_len < MAX_AUTO_EXTRA) a_collect[a_len] = out_buf[stage_cur >> 3];
|
|
|
a_len++;
|
|
|
|
|
|
if (a_len >= MIN_AUTO_EXTRA && a_len <= MAX_AUTO_EXTRA)
|
|
|
maybe_add_auto(a_collect, a_len);
|
|
|
|
|
|
} else if (cksum != prev_cksum) {
|
|
|
|
|
|
/* Otherwise, if the checksum has changed, see if we have something
|
|
|
worthwhile queued up, and collect that if the answer is yes. */
|
|
|
|
|
|
if (a_len >= MIN_AUTO_EXTRA && a_len <= MAX_AUTO_EXTRA)
|
|
|
maybe_add_auto(a_collect, a_len);
|
|
|
|
|
|
a_len = 0;
|
|
|
prev_cksum = cksum;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Continue collecting string, but only if the bit flip actually made
|
|
|
any difference - we don't want no-op tokens. */
|
|
|
|
|
|
if (cksum != queue_cur->exec_cksum) {
|
|
|
|
|
|
if (a_len < MAX_AUTO_EXTRA) a_collect[a_len] = out_buf[stage_cur >> 3];
|
|
|
a_len++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_FLIP1] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_FLIP1] += stage_max;
|
|
|
|
|
|
/* Two walking bits. */
|
|
|
|
|
|
stage_name = "bitflip 2/1";
|
|
|
stage_short = "flip2";
|
|
|
stage_max = (len << 3) - 1;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (stage_cur = 0; stage_cur < stage_max; stage_cur++) {
|
|
|
|
|
|
stage_cur_byte = stage_cur >> 3;
|
|
|
|
|
|
FLIP_BIT(out_buf, stage_cur);
|
|
|
FLIP_BIT(out_buf, stage_cur + 1);
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
|
|
|
FLIP_BIT(out_buf, stage_cur);
|
|
|
FLIP_BIT(out_buf, stage_cur + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_FLIP2] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_FLIP2] += stage_max;
|
|
|
|
|
|
/* Four walking bits. */
|
|
|
|
|
|
stage_name = "bitflip 4/1";
|
|
|
stage_short = "flip4";
|
|
|
stage_max = (len << 3) - 3;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (stage_cur = 0; stage_cur < stage_max; stage_cur++) {
|
|
|
|
|
|
stage_cur_byte = stage_cur >> 3;
|
|
|
|
|
|
FLIP_BIT(out_buf, stage_cur);
|
|
|
FLIP_BIT(out_buf, stage_cur + 1);
|
|
|
FLIP_BIT(out_buf, stage_cur + 2);
|
|
|
FLIP_BIT(out_buf, stage_cur + 3);
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
|
|
|
FLIP_BIT(out_buf, stage_cur);
|
|
|
FLIP_BIT(out_buf, stage_cur + 1);
|
|
|
FLIP_BIT(out_buf, stage_cur + 2);
|
|
|
FLIP_BIT(out_buf, stage_cur + 3);
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_FLIP4] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_FLIP4] += stage_max;
|
|
|
|
|
|
/* Effector map setup. These macros calculate:
|
|
|
|
|
|
EFF_APOS - position of a particular file offset in the map.
|
|
|
EFF_ALEN - length of a map with a particular number of bytes.
|
|
|
EFF_SPAN_ALEN - map span for a sequence of bytes.
|
|
|
|
|
|
*/
|
|
|
|
|
|
#define EFF_APOS(_p) ((_p) >> EFF_MAP_SCALE2)
|
|
|
#define EFF_REM(_x) ((_x) & ((1 << EFF_MAP_SCALE2) - 1))
|
|
|
#define EFF_ALEN(_l) (EFF_APOS(_l) + !!EFF_REM(_l))
|
|
|
#define EFF_SPAN_ALEN(_p, _l) (EFF_APOS((_p) + (_l) - 1) - EFF_APOS(_p) + 1)
|
|
|
|
|
|
/* Initialize effector map for the next step (see comments below). Always
|
|
|
flag first and last byte as doing something. */
|
|
|
|
|
|
eff_map = ck_alloc(EFF_ALEN(len));
|
|
|
eff_map[0] = 1;
|
|
|
|
|
|
if (EFF_APOS(len - 1) != 0) {
|
|
|
eff_map[EFF_APOS(len - 1)] = 1;
|
|
|
eff_cnt++;
|
|
|
}
|
|
|
|
|
|
/* Walking byte. */
|
|
|
|
|
|
stage_name = "bitflip 8/8";
|
|
|
stage_short = "flip8";
|
|
|
stage_max = len;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (stage_cur = 0; stage_cur < stage_max; stage_cur++) {
|
|
|
|
|
|
stage_cur_byte = stage_cur;
|
|
|
|
|
|
out_buf[stage_cur] ^= 0xFF;
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
|
|
|
/* We also use this stage to pull off a simple trick: we identify
|
|
|
bytes that seem to have no effect on the current execution path
|
|
|
even when fully flipped - and we skip them during more expensive
|
|
|
deterministic stages, such as arithmetics or known ints. */
|
|
|
|
|
|
if (!eff_map[EFF_APOS(stage_cur)]) {
|
|
|
|
|
|
u32 cksum;
|
|
|
|
|
|
/* If in dumb mode or if the file is very short, just flag everything
|
|
|
without wasting time on checksums. */
|
|
|
|
|
|
if (!dumb_mode && len >= EFF_MIN_LEN)
|
|
|
cksum = hash32(trace_bits, MAP_SIZE, HASH_CONST);
|
|
|
else
|
|
|
cksum = ~queue_cur->exec_cksum;
|
|
|
|
|
|
if (cksum != queue_cur->exec_cksum) {
|
|
|
eff_map[EFF_APOS(stage_cur)] = 1;
|
|
|
eff_cnt++;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
out_buf[stage_cur] ^= 0xFF;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* If the effector map is more than EFF_MAX_PERC dense, just flag the
|
|
|
whole thing as worth fuzzing, since we wouldn't be saving much time
|
|
|
anyway. */
|
|
|
|
|
|
if (eff_cnt != EFF_ALEN(len) &&
|
|
|
eff_cnt * 100 / EFF_ALEN(len) > EFF_MAX_PERC) {
|
|
|
|
|
|
memset(eff_map, 1, EFF_ALEN(len));
|
|
|
|
|
|
blocks_eff_select += EFF_ALEN(len);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
blocks_eff_select += eff_cnt;
|
|
|
|
|
|
}
|
|
|
|
|
|
blocks_eff_total += EFF_ALEN(len);
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_FLIP8] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_FLIP8] += stage_max;
|
|
|
|
|
|
/* Two walking bytes. */
|
|
|
|
|
|
if (len < 2) goto skip_bitflip;
|
|
|
|
|
|
stage_name = "bitflip 16/8";
|
|
|
stage_short = "flip16";
|
|
|
stage_cur = 0;
|
|
|
stage_max = len - 1;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (i = 0; i < len - 1; i++) {
|
|
|
|
|
|
/* Let's consult the effector map... */
|
|
|
|
|
|
if (!eff_map[EFF_APOS(i)] && !eff_map[EFF_APOS(i + 1)]) {
|
|
|
stage_max--;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
*(u16*)(out_buf + i) ^= 0xFFFF;
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
*(u16*)(out_buf + i) ^= 0xFFFF;
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_FLIP16] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_FLIP16] += stage_max;
|
|
|
|
|
|
if (len < 4) goto skip_bitflip;
|
|
|
|
|
|
/* Four walking bytes. */
|
|
|
|
|
|
stage_name = "bitflip 32/8";
|
|
|
stage_short = "flip32";
|
|
|
stage_cur = 0;
|
|
|
stage_max = len - 3;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (i = 0; i < len - 3; i++) {
|
|
|
|
|
|
/* Let's consult the effector map... */
|
|
|
if (!eff_map[EFF_APOS(i)] && !eff_map[EFF_APOS(i + 1)] &&
|
|
|
!eff_map[EFF_APOS(i + 2)] && !eff_map[EFF_APOS(i + 3)]) {
|
|
|
stage_max--;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
*(u32*)(out_buf + i) ^= 0xFFFFFFFF;
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
*(u32*)(out_buf + i) ^= 0xFFFFFFFF;
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_FLIP32] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_FLIP32] += stage_max;
|
|
|
|
|
|
skip_bitflip:
|
|
|
|
|
|
if (no_arith) goto skip_arith;
|
|
|
|
|
|
/**********************
|
|
|
* ARITHMETIC INC/DEC *
|
|
|
**********************/
|
|
|
|
|
|
/* 8-bit arithmetics. */
|
|
|
|
|
|
stage_name = "arith 8/8";
|
|
|
stage_short = "arith8";
|
|
|
stage_cur = 0;
|
|
|
stage_max = 2 * len * ARITH_MAX;
|
|
|
|
|
|
stage_val_type = STAGE_VAL_LE;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
|
|
u8 orig = out_buf[i];
|
|
|
|
|
|
/* Let's consult the effector map... */
|
|
|
|
|
|
if (!eff_map[EFF_APOS(i)]) {
|
|
|
stage_max -= 2 * ARITH_MAX;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
for (j = 1; j <= ARITH_MAX; j++) {
|
|
|
|
|
|
u8 r = orig ^ (orig + j);
|
|
|
|
|
|
/* Do arithmetic operations only if the result couldn't be a product
|
|
|
of a bitflip. */
|
|
|
|
|
|
if (!could_be_bitflip(r)) {
|
|
|
|
|
|
stage_cur_val = j;
|
|
|
out_buf[i] = orig + j;
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
r = orig ^ (orig - j);
|
|
|
|
|
|
if (!could_be_bitflip(r)) {
|
|
|
|
|
|
stage_cur_val = -j;
|
|
|
out_buf[i] = orig - j;
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
out_buf[i] = orig;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_ARITH8] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_ARITH8] += stage_max;
|
|
|
|
|
|
/* 16-bit arithmetics, both endians. */
|
|
|
|
|
|
if (len < 2) goto skip_arith;
|
|
|
|
|
|
stage_name = "arith 16/8";
|
|
|
stage_short = "arith16";
|
|
|
stage_cur = 0;
|
|
|
stage_max = 4 * (len - 1) * ARITH_MAX;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (i = 0; i < len - 1; i++) {
|
|
|
|
|
|
u16 orig = *(u16*)(out_buf + i);
|
|
|
|
|
|
/* Let's consult the effector map... */
|
|
|
|
|
|
if (!eff_map[EFF_APOS(i)] && !eff_map[EFF_APOS(i + 1)]) {
|
|
|
stage_max -= 4 * ARITH_MAX;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
for (j = 1; j <= ARITH_MAX; j++) {
|
|
|
|
|
|
u16 r1 = orig ^ (orig + j),
|
|
|
r2 = orig ^ (orig - j),
|
|
|
r3 = orig ^ SWAP16(SWAP16(orig) + j),
|
|
|
r4 = orig ^ SWAP16(SWAP16(orig) - j);
|
|
|
|
|
|
/* Try little endian addition and subtraction first. Do it only
|
|
|
if the operation would affect more than one byte (hence the
|
|
|
& 0xff overflow checks) and if it couldn't be a product of
|
|
|
a bitflip. */
|
|
|
|
|
|
stage_val_type = STAGE_VAL_LE;
|
|
|
|
|
|
if ((orig & 0xff) + j > 0xff && !could_be_bitflip(r1)) {
|
|
|
|
|
|
stage_cur_val = j;
|
|
|
*(u16*)(out_buf + i) = orig + j;
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
if ((orig & 0xff) < j && !could_be_bitflip(r2)) {
|
|
|
|
|
|
stage_cur_val = -j;
|
|
|
*(u16*)(out_buf + i) = orig - j;
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
/* Big endian comes next. Same deal. */
|
|
|
|
|
|
stage_val_type = STAGE_VAL_BE;
|
|
|
|
|
|
|
|
|
if ((orig >> 8) + j > 0xff && !could_be_bitflip(r3)) {
|
|
|
|
|
|
stage_cur_val = j;
|
|
|
*(u16*)(out_buf + i) = SWAP16(SWAP16(orig) + j);
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
if ((orig >> 8) < j && !could_be_bitflip(r4)) {
|
|
|
|
|
|
stage_cur_val = -j;
|
|
|
*(u16*)(out_buf + i) = SWAP16(SWAP16(orig) - j);
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
*(u16*)(out_buf + i) = orig;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_ARITH16] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_ARITH16] += stage_max;
|
|
|
|
|
|
/* 32-bit arithmetics, both endians. */
|
|
|
|
|
|
if (len < 4) goto skip_arith;
|
|
|
|
|
|
stage_name = "arith 32/8";
|
|
|
stage_short = "arith32";
|
|
|
stage_cur = 0;
|
|
|
stage_max = 4 * (len - 3) * ARITH_MAX;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (i = 0; i < len - 3; i++) {
|
|
|
|
|
|
u32 orig = *(u32*)(out_buf + i);
|
|
|
|
|
|
/* Let's consult the effector map... */
|
|
|
|
|
|
if (!eff_map[EFF_APOS(i)] && !eff_map[EFF_APOS(i + 1)] &&
|
|
|
!eff_map[EFF_APOS(i + 2)] && !eff_map[EFF_APOS(i + 3)]) {
|
|
|
stage_max -= 4 * ARITH_MAX;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
for (j = 1; j <= ARITH_MAX; j++) {
|
|
|
|
|
|
u32 r1 = orig ^ (orig + j),
|
|
|
r2 = orig ^ (orig - j),
|
|
|
r3 = orig ^ SWAP32(SWAP32(orig) + j),
|
|
|
r4 = orig ^ SWAP32(SWAP32(orig) - j);
|
|
|
|
|
|
/* Little endian first. Same deal as with 16-bit: we only want to
|
|
|
try if the operation would have effect on more than two bytes. */
|
|
|
|
|
|
stage_val_type = STAGE_VAL_LE;
|
|
|
|
|
|
if ((orig & 0xffff) + j > 0xffff && !could_be_bitflip(r1)) {
|
|
|
|
|
|
stage_cur_val = j;
|
|
|
*(u32*)(out_buf + i) = orig + j;
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
if ((orig & 0xffff) < j && !could_be_bitflip(r2)) {
|
|
|
|
|
|
stage_cur_val = -j;
|
|
|
*(u32*)(out_buf + i) = orig - j;
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
/* Big endian next. */
|
|
|
|
|
|
stage_val_type = STAGE_VAL_BE;
|
|
|
|
|
|
if ((SWAP32(orig) & 0xffff) + j > 0xffff && !could_be_bitflip(r3)) {
|
|
|
|
|
|
stage_cur_val = j;
|
|
|
*(u32*)(out_buf + i) = SWAP32(SWAP32(orig) + j);
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
if ((SWAP32(orig) & 0xffff) < j && !could_be_bitflip(r4)) {
|
|
|
|
|
|
stage_cur_val = -j;
|
|
|
*(u32*)(out_buf + i) = SWAP32(SWAP32(orig) - j);
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
*(u32*)(out_buf + i) = orig;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_ARITH32] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_ARITH32] += stage_max;
|
|
|
|
|
|
skip_arith:
|
|
|
|
|
|
/**********************
|
|
|
* INTERESTING VALUES *
|
|
|
**********************/
|
|
|
|
|
|
stage_name = "interest 8/8";
|
|
|
stage_short = "int8";
|
|
|
stage_cur = 0;
|
|
|
stage_max = len * sizeof(interesting_8);
|
|
|
|
|
|
stage_val_type = STAGE_VAL_LE;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
/* Setting 8-bit integers. */
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
|
|
u8 orig = out_buf[i];
|
|
|
|
|
|
/* Let's consult the effector map... */
|
|
|
|
|
|
if (!eff_map[EFF_APOS(i)]) {
|
|
|
stage_max -= sizeof(interesting_8);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
for (j = 0; j < sizeof(interesting_8); j++) {
|
|
|
|
|
|
/* Skip if the value could be a product of bitflips or arithmetics. */
|
|
|
|
|
|
if (could_be_bitflip(orig ^ (u8)interesting_8[j]) ||
|
|
|
could_be_arith(orig, (u8)interesting_8[j], 1)) {
|
|
|
stage_max--;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
stage_cur_val = interesting_8[j];
|
|
|
out_buf[i] = interesting_8[j];
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
|
|
|
out_buf[i] = orig;
|
|
|
stage_cur++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_INTEREST8] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_INTEREST8] += stage_max;
|
|
|
|
|
|
/* Setting 16-bit integers, both endians. */
|
|
|
|
|
|
if (no_arith || len < 2) goto skip_interest;
|
|
|
|
|
|
stage_name = "interest 16/8";
|
|
|
stage_short = "int16";
|
|
|
stage_cur = 0;
|
|
|
stage_max = 2 * (len - 1) * (sizeof(interesting_16) >> 1);
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (i = 0; i < len - 1; i++) {
|
|
|
|
|
|
u16 orig = *(u16*)(out_buf + i);
|
|
|
|
|
|
/* Let's consult the effector map... */
|
|
|
|
|
|
if (!eff_map[EFF_APOS(i)] && !eff_map[EFF_APOS(i + 1)]) {
|
|
|
stage_max -= sizeof(interesting_16);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
for (j = 0; j < sizeof(interesting_16) / 2; j++) {
|
|
|
|
|
|
stage_cur_val = interesting_16[j];
|
|
|
|
|
|
/* Skip if this could be a product of a bitflip, arithmetics,
|
|
|
or single-byte interesting value insertion. */
|
|
|
|
|
|
if (!could_be_bitflip(orig ^ (u16)interesting_16[j]) &&
|
|
|
!could_be_arith(orig, (u16)interesting_16[j], 2) &&
|
|
|
!could_be_interest(orig, (u16)interesting_16[j], 2, 0)) {
|
|
|
|
|
|
stage_val_type = STAGE_VAL_LE;
|
|
|
|
|
|
*(u16*)(out_buf + i) = interesting_16[j];
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
if ((u16)interesting_16[j] != SWAP16(interesting_16[j]) &&
|
|
|
!could_be_bitflip(orig ^ SWAP16(interesting_16[j])) &&
|
|
|
!could_be_arith(orig, SWAP16(interesting_16[j]), 2) &&
|
|
|
!could_be_interest(orig, SWAP16(interesting_16[j]), 2, 1)) {
|
|
|
|
|
|
stage_val_type = STAGE_VAL_BE;
|
|
|
|
|
|
*(u16*)(out_buf + i) = SWAP16(interesting_16[j]);
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
}
|
|
|
|
|
|
*(u16*)(out_buf + i) = orig;
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_INTEREST16] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_INTEREST16] += stage_max;
|
|
|
|
|
|
if (len < 4) goto skip_interest;
|
|
|
|
|
|
/* Setting 32-bit integers, both endians. */
|
|
|
|
|
|
stage_name = "interest 32/8";
|
|
|
stage_short = "int32";
|
|
|
stage_cur = 0;
|
|
|
stage_max = 2 * (len - 3) * (sizeof(interesting_32) >> 2);
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (i = 0; i < len - 3; i++) {
|
|
|
|
|
|
u32 orig = *(u32*)(out_buf + i);
|
|
|
|
|
|
/* Let's consult the effector map... */
|
|
|
|
|
|
if (!eff_map[EFF_APOS(i)] && !eff_map[EFF_APOS(i + 1)] &&
|
|
|
!eff_map[EFF_APOS(i + 2)] && !eff_map[EFF_APOS(i + 3)]) {
|
|
|
stage_max -= sizeof(interesting_32) >> 1;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
for (j = 0; j < sizeof(interesting_32) / 4; j++) {
|
|
|
|
|
|
stage_cur_val = interesting_32[j];
|
|
|
|
|
|
/* Skip if this could be a product of a bitflip, arithmetics,
|
|
|
or word interesting value insertion. */
|
|
|
|
|
|
if (!could_be_bitflip(orig ^ (u32)interesting_32[j]) &&
|
|
|
!could_be_arith(orig, interesting_32[j], 4) &&
|
|
|
!could_be_interest(orig, interesting_32[j], 4, 0)) {
|
|
|
|
|
|
stage_val_type = STAGE_VAL_LE;
|
|
|
|
|
|
*(u32*)(out_buf + i) = interesting_32[j];
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
if ((u32)interesting_32[j] != SWAP32(interesting_32[j]) &&
|
|
|
!could_be_bitflip(orig ^ SWAP32(interesting_32[j])) &&
|
|
|
!could_be_arith(orig, SWAP32(interesting_32[j]), 4) &&
|
|
|
!could_be_interest(orig, SWAP32(interesting_32[j]), 4, 1)) {
|
|
|
|
|
|
stage_val_type = STAGE_VAL_BE;
|
|
|
|
|
|
*(u32*)(out_buf + i) = SWAP32(interesting_32[j]);
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
stage_cur++;
|
|
|
|
|
|
} else stage_max--;
|
|
|
|
|
|
}
|
|
|
|
|
|
*(u32*)(out_buf + i) = orig;
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_INTEREST32] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_INTEREST32] += stage_max;
|
|
|
|
|
|
skip_interest:
|
|
|
|
|
|
/********************
|
|
|
* DICTIONARY STUFF *
|
|
|
********************/
|
|
|
|
|
|
if (!extras_cnt) goto skip_user_extras;
|
|
|
|
|
|
/* Overwrite with user-supplied extras. */
|
|
|
|
|
|
stage_name = "user extras (over)";
|
|
|
stage_short = "ext_UO";
|
|
|
stage_cur = 0;
|
|
|
stage_max = extras_cnt * len;
|
|
|
|
|
|
stage_val_type = STAGE_VAL_NONE;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
|
|
u32 last_len = 0;
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
/* Extras are sorted by size, from smallest to largest. This means
|
|
|
that we don't have to worry about restoring the buffer in
|
|
|
between writes at a particular offset determined by the outer
|
|
|
loop. */
|
|
|
|
|
|
for (j = 0; j < extras_cnt; j++) {
|
|
|
|
|
|
/* Skip extras probabilistically if extras_cnt > MAX_DET_EXTRAS. Also
|
|
|
skip them if there's no room to insert the payload, if the token
|
|
|
is redundant, or if its entire span has no bytes set in the effector
|
|
|
map. */
|
|
|
|
|
|
if ((extras_cnt > MAX_DET_EXTRAS && UR(extras_cnt) >= MAX_DET_EXTRAS) ||
|
|
|
extras[j].len > len - i ||
|
|
|
!memcmp(extras[j].data, out_buf + i, extras[j].len) ||
|
|
|
!memchr(eff_map + EFF_APOS(i), 1, EFF_SPAN_ALEN(i, extras[j].len))) {
|
|
|
|
|
|
stage_max--;
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
last_len = extras[j].len;
|
|
|
memcpy(out_buf + i, extras[j].data, last_len);
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
|
|
|
stage_cur++;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Restore all the clobbered memory. */
|
|
|
memcpy(out_buf + i, in_buf + i, last_len);
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_EXTRAS_UO] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_EXTRAS_UO] += stage_max;
|
|
|
|
|
|
/* Insertion of user-supplied extras. */
|
|
|
|
|
|
stage_name = "user extras (insert)";
|
|
|
stage_short = "ext_UI";
|
|
|
stage_cur = 0;
|
|
|
stage_max = extras_cnt * (len + 1);
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
ex_tmp = ck_alloc(len + MAX_DICT_FILE);
|
|
|
|
|
|
for (i = 0; i <= len; i++) {
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
for (j = 0; j < extras_cnt; j++) {
|
|
|
|
|
|
if (len + extras[j].len > MAX_FILE) {
|
|
|
stage_max--;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
/* Insert token */
|
|
|
memcpy(ex_tmp + i, extras[j].data, extras[j].len);
|
|
|
|
|
|
/* Copy tail */
|
|
|
memcpy(ex_tmp + i + extras[j].len, out_buf + i, len - i);
|
|
|
|
|
|
if (common_fuzz_stuff(argv, ex_tmp, len + extras[j].len)) {
|
|
|
ck_free(ex_tmp);
|
|
|
goto abandon_entry;
|
|
|
}
|
|
|
|
|
|
stage_cur++;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Copy head */
|
|
|
ex_tmp[i] = out_buf[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
ck_free(ex_tmp);
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_EXTRAS_UI] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_EXTRAS_UI] += stage_max;
|
|
|
|
|
|
skip_user_extras:
|
|
|
|
|
|
if (!a_extras_cnt) goto skip_extras;
|
|
|
|
|
|
stage_name = "auto extras (over)";
|
|
|
stage_short = "ext_AO";
|
|
|
stage_cur = 0;
|
|
|
stage_max = MIN(a_extras_cnt, USE_AUTO_EXTRAS) * len;
|
|
|
|
|
|
stage_val_type = STAGE_VAL_NONE;
|
|
|
|
|
|
orig_hit_cnt = new_hit_cnt;
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
|
|
u32 last_len = 0;
|
|
|
|
|
|
stage_cur_byte = i;
|
|
|
|
|
|
for (j = 0; j < MIN(a_extras_cnt, USE_AUTO_EXTRAS); j++) {
|
|
|
|
|
|
/* See the comment in the earlier code; extras are sorted by size. */
|
|
|
|
|
|
if (a_extras[j].len > len - i ||
|
|
|
!memcmp(a_extras[j].data, out_buf + i, a_extras[j].len) ||
|
|
|
!memchr(eff_map + EFF_APOS(i), 1, EFF_SPAN_ALEN(i, a_extras[j].len))) {
|
|
|
|
|
|
stage_max--;
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
last_len = a_extras[j].len;
|
|
|
memcpy(out_buf + i, a_extras[j].data, last_len);
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, len)) goto abandon_entry;
|
|
|
|
|
|
stage_cur++;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Restore all the clobbered memory. */
|
|
|
memcpy(out_buf + i, in_buf + i, last_len);
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
stage_finds[STAGE_EXTRAS_AO] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_EXTRAS_AO] += stage_max;
|
|
|
|
|
|
skip_extras:
|
|
|
|
|
|
/* If we made this to here without jumping to havoc_stage or abandon_entry,
|
|
|
we're properly done with deterministic steps and can mark it as such
|
|
|
in the .state/ directory. */
|
|
|
|
|
|
if (!queue_cur->passed_det) mark_as_det_done(queue_cur);
|
|
|
|
|
|
/****************
|
|
|
* RANDOM HAVOC *
|
|
|
****************/
|
|
|
|
|
|
havoc_stage:
|
|
|
|
|
|
stage_cur_byte = -1;
|
|
|
|
|
|
/* The havoc stage mutation code is also invoked when splicing files; if the
|
|
|
splice_cycle variable is set, generate different descriptions and such. */
|
|
|
|
|
|
if (!splice_cycle) {
|
|
|
|
|
|
stage_name = "havoc";
|
|
|
stage_short = "havoc";
|
|
|
stage_max = (doing_det ? HAVOC_CYCLES_INIT : HAVOC_CYCLES) *
|
|
|
perf_score / havoc_div / 100;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
static u8 tmp[32];
|
|
|
|
|
|
perf_score = orig_perf;
|
|
|
|
|
|
sprintf(tmp, "splice %u", splice_cycle);
|
|
|
stage_name = tmp;
|
|
|
stage_short = "splice";
|
|
|
stage_max = SPLICE_HAVOC * perf_score / havoc_div / 100;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (stage_max < HAVOC_MIN) stage_max = HAVOC_MIN;
|
|
|
|
|
|
temp_len = len;
|
|
|
|
|
|
orig_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
havoc_queued = queued_paths;
|
|
|
|
|
|
/* We essentially just do several thousand runs (depending on perf_score)
|
|
|
where we take the input file and make random stacked tweaks. */
|
|
|
|
|
|
for (stage_cur = 0; stage_cur < stage_max; stage_cur++) {
|
|
|
|
|
|
u32 use_stacking = 1 << (1 + UR(HAVOC_STACK_POW2));
|
|
|
|
|
|
stage_cur_val = use_stacking;
|
|
|
|
|
|
for (i = 0; i < use_stacking; i++) {
|
|
|
|
|
|
switch (UR(15 + ((extras_cnt + a_extras_cnt) ? 2 : 0))) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
/* Flip a single bit somewhere. Spooky! */
|
|
|
|
|
|
FLIP_BIT(out_buf, UR(temp_len << 3));
|
|
|
break;
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
/* Set byte to interesting value. */
|
|
|
|
|
|
out_buf[UR(temp_len)] = interesting_8[UR(sizeof(interesting_8))];
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
/* Set word to interesting value, randomly choosing endian. */
|
|
|
|
|
|
if (temp_len < 2) break;
|
|
|
|
|
|
if (UR(2)) {
|
|
|
|
|
|
*(u16*)(out_buf + UR(temp_len - 1)) =
|
|
|
interesting_16[UR(sizeof(interesting_16) >> 1)];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
*(u16*)(out_buf + UR(temp_len - 1)) = SWAP16(
|
|
|
interesting_16[UR(sizeof(interesting_16) >> 1)]);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
/* Set dword to interesting value, randomly choosing endian. */
|
|
|
|
|
|
if (temp_len < 4) break;
|
|
|
|
|
|
if (UR(2)) {
|
|
|
|
|
|
*(u32*)(out_buf + UR(temp_len - 3)) =
|
|
|
interesting_32[UR(sizeof(interesting_32) >> 2)];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
*(u32*)(out_buf + UR(temp_len - 3)) = SWAP32(
|
|
|
interesting_32[UR(sizeof(interesting_32) >> 2)]);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
/* Randomly subtract from byte. */
|
|
|
|
|
|
out_buf[UR(temp_len)] -= 1 + UR(ARITH_MAX);
|
|
|
break;
|
|
|
|
|
|
case 5:
|
|
|
|
|
|
/* Randomly add to byte. */
|
|
|
|
|
|
out_buf[UR(temp_len)] += 1 + UR(ARITH_MAX);
|
|
|
break;
|
|
|
|
|
|
case 6:
|
|
|
|
|
|
/* Randomly subtract from word, random endian. */
|
|
|
|
|
|
if (temp_len < 2) break;
|
|
|
|
|
|
if (UR(2)) {
|
|
|
|
|
|
u32 pos = UR(temp_len - 1);
|
|
|
|
|
|
*(u16*)(out_buf + pos) -= 1 + UR(ARITH_MAX);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
u32 pos = UR(temp_len - 1);
|
|
|
u16 num = 1 + UR(ARITH_MAX);
|
|
|
|
|
|
*(u16*)(out_buf + pos) =
|
|
|
SWAP16(SWAP16(*(u16*)(out_buf + pos)) - num);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 7:
|
|
|
|
|
|
/* Randomly add to word, random endian. */
|
|
|
|
|
|
if (temp_len < 2) break;
|
|
|
|
|
|
if (UR(2)) {
|
|
|
|
|
|
u32 pos = UR(temp_len - 1);
|
|
|
|
|
|
*(u16*)(out_buf + pos) += 1 + UR(ARITH_MAX);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
u32 pos = UR(temp_len - 1);
|
|
|
u16 num = 1 + UR(ARITH_MAX);
|
|
|
|
|
|
*(u16*)(out_buf + pos) =
|
|
|
SWAP16(SWAP16(*(u16*)(out_buf + pos)) + num);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 8:
|
|
|
|
|
|
/* Randomly subtract from dword, random endian. */
|
|
|
|
|
|
if (temp_len < 4) break;
|
|
|
|
|
|
if (UR(2)) {
|
|
|
|
|
|
u32 pos = UR(temp_len - 3);
|
|
|
|
|
|
*(u32*)(out_buf + pos) -= 1 + UR(ARITH_MAX);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
u32 pos = UR(temp_len - 3);
|
|
|
u32 num = 1 + UR(ARITH_MAX);
|
|
|
|
|
|
*(u32*)(out_buf + pos) =
|
|
|
SWAP32(SWAP32(*(u32*)(out_buf + pos)) - num);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 9:
|
|
|
|
|
|
/* Randomly add to dword, random endian. */
|
|
|
|
|
|
if (temp_len < 4) break;
|
|
|
|
|
|
if (UR(2)) {
|
|
|
|
|
|
u32 pos = UR(temp_len - 3);
|
|
|
|
|
|
*(u32*)(out_buf + pos) += 1 + UR(ARITH_MAX);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
u32 pos = UR(temp_len - 3);
|
|
|
u32 num = 1 + UR(ARITH_MAX);
|
|
|
|
|
|
*(u32*)(out_buf + pos) =
|
|
|
SWAP32(SWAP32(*(u32*)(out_buf + pos)) + num);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 10:
|
|
|
|
|
|
/* Just set a random byte to a random value. Because,
|
|
|
why not. We use XOR with 1-255 to eliminate the
|
|
|
possibility of a no-op. */
|
|
|
|
|
|
out_buf[UR(temp_len)] ^= 1 + UR(255);
|
|
|
break;
|
|
|
|
|
|
case 11 ... 12: {
|
|
|
|
|
|
/* Delete bytes. We're making this a bit more likely
|
|
|
than insertion (the next option) in hopes of keeping
|
|
|
files reasonably small. */
|
|
|
|
|
|
u32 del_from, del_len;
|
|
|
|
|
|
if (temp_len < 2) break;
|
|
|
|
|
|
/* Don't delete too much. */
|
|
|
|
|
|
del_len = choose_block_len(temp_len - 1);
|
|
|
|
|
|
del_from = UR(temp_len - del_len + 1);
|
|
|
|
|
|
memmove(out_buf + del_from, out_buf + del_from + del_len,
|
|
|
temp_len - del_from - del_len);
|
|
|
|
|
|
temp_len -= del_len;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 13:
|
|
|
|
|
|
if (temp_len + HAVOC_BLK_XL < MAX_FILE) {
|
|
|
|
|
|
/* Clone bytes (75%) or insert a block of constant bytes (25%). */
|
|
|
|
|
|
u8 actually_clone = UR(4);
|
|
|
u32 clone_from, clone_to, clone_len;
|
|
|
u8* new_buf;
|
|
|
|
|
|
if (actually_clone) {
|
|
|
|
|
|
clone_len = choose_block_len(temp_len);
|
|
|
clone_from = UR(temp_len - clone_len + 1);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
clone_len = choose_block_len(HAVOC_BLK_XL);
|
|
|
clone_from = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
clone_to = UR(temp_len);
|
|
|
|
|
|
new_buf = ck_alloc_nozero(temp_len + clone_len);
|
|
|
|
|
|
/* Head */
|
|
|
|
|
|
memcpy(new_buf, out_buf, clone_to);
|
|
|
|
|
|
/* Inserted part */
|
|
|
|
|
|
if (actually_clone)
|
|
|
memcpy(new_buf + clone_to, out_buf + clone_from, clone_len);
|
|
|
else
|
|
|
memset(new_buf + clone_to,
|
|
|
UR(2) ? UR(256) : out_buf[UR(temp_len)], clone_len);
|
|
|
|
|
|
/* Tail */
|
|
|
memcpy(new_buf + clone_to + clone_len, out_buf + clone_to,
|
|
|
temp_len - clone_to);
|
|
|
|
|
|
ck_free(out_buf);
|
|
|
out_buf = new_buf;
|
|
|
temp_len += clone_len;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 14: {
|
|
|
|
|
|
/* Overwrite bytes with a randomly selected chunk (75%) or fixed
|
|
|
bytes (25%). */
|
|
|
|
|
|
u32 copy_from, copy_to, copy_len;
|
|
|
|
|
|
if (temp_len < 2) break;
|
|
|
|
|
|
copy_len = choose_block_len(temp_len - 1);
|
|
|
|
|
|
copy_from = UR(temp_len - copy_len + 1);
|
|
|
copy_to = UR(temp_len - copy_len + 1);
|
|
|
|
|
|
if (UR(4)) {
|
|
|
|
|
|
if (copy_from != copy_to)
|
|
|
memmove(out_buf + copy_to, out_buf + copy_from, copy_len);
|
|
|
|
|
|
} else memset(out_buf + copy_to,
|
|
|
UR(2) ? UR(256) : out_buf[UR(temp_len)], copy_len);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Values 15 and 16 can be selected only if there are any extras
|
|
|
present in the dictionaries. */
|
|
|
|
|
|
case 15: {
|
|
|
|
|
|
/* Overwrite bytes with an extra. */
|
|
|
|
|
|
if (!extras_cnt || (a_extras_cnt && UR(2))) {
|
|
|
|
|
|
/* No user-specified extras or odds in our favor. Let's use an
|
|
|
auto-detected one. */
|
|
|
|
|
|
u32 use_extra = UR(a_extras_cnt);
|
|
|
u32 extra_len = a_extras[use_extra].len;
|
|
|
u32 insert_at;
|
|
|
|
|
|
if (extra_len > temp_len) break;
|
|
|
|
|
|
insert_at = UR(temp_len - extra_len + 1);
|
|
|
memcpy(out_buf + insert_at, a_extras[use_extra].data, extra_len);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
/* No auto extras or odds in our favor. Use the dictionary. */
|
|
|
|
|
|
u32 use_extra = UR(extras_cnt);
|
|
|
u32 extra_len = extras[use_extra].len;
|
|
|
u32 insert_at;
|
|
|
|
|
|
if (extra_len > temp_len) break;
|
|
|
|
|
|
insert_at = UR(temp_len - extra_len + 1);
|
|
|
memcpy(out_buf + insert_at, extras[use_extra].data, extra_len);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 16: {
|
|
|
|
|
|
u32 use_extra, extra_len, insert_at = UR(temp_len + 1);
|
|
|
u8* new_buf;
|
|
|
|
|
|
/* Insert an extra. Do the same dice-rolling stuff as for the
|
|
|
previous case. */
|
|
|
|
|
|
if (!extras_cnt || (a_extras_cnt && UR(2))) {
|
|
|
|
|
|
use_extra = UR(a_extras_cnt);
|
|
|
extra_len = a_extras[use_extra].len;
|
|
|
|
|
|
if (temp_len + extra_len >= MAX_FILE) break;
|
|
|
|
|
|
new_buf = ck_alloc_nozero(temp_len + extra_len);
|
|
|
|
|
|
/* Head */
|
|
|
memcpy(new_buf, out_buf, insert_at);
|
|
|
|
|
|
/* Inserted part */
|
|
|
memcpy(new_buf + insert_at, a_extras[use_extra].data, extra_len);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
use_extra = UR(extras_cnt);
|
|
|
extra_len = extras[use_extra].len;
|
|
|
|
|
|
if (temp_len + extra_len >= MAX_FILE) break;
|
|
|
|
|
|
new_buf = ck_alloc_nozero(temp_len + extra_len);
|
|
|
|
|
|
/* Head */
|
|
|
memcpy(new_buf, out_buf, insert_at);
|
|
|
|
|
|
/* Inserted part */
|
|
|
memcpy(new_buf + insert_at, extras[use_extra].data, extra_len);
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Tail */
|
|
|
memcpy(new_buf + insert_at + extra_len, out_buf + insert_at,
|
|
|
temp_len - insert_at);
|
|
|
|
|
|
ck_free(out_buf);
|
|
|
out_buf = new_buf;
|
|
|
temp_len += extra_len;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (common_fuzz_stuff(argv, out_buf, temp_len))
|
|
|
goto abandon_entry;
|
|
|
|
|
|
/* out_buf might have been mangled a bit, so let's restore it to its
|
|
|
original size and shape. */
|
|
|
|
|
|
if (temp_len < len) out_buf = ck_realloc(out_buf, len);
|
|
|
temp_len = len;
|
|
|
memcpy(out_buf, in_buf, len);
|
|
|
|
|
|
/* If we're finding new stuff, let's run for a bit longer, limits
|
|
|
permitting. */
|
|
|
|
|
|
if (queued_paths != havoc_queued) {
|
|
|
|
|
|
if (perf_score <= HAVOC_MAX_MULT * 100) {
|
|
|
stage_max *= 2;
|
|
|
perf_score *= 2;
|
|
|
}
|
|
|
|
|
|
havoc_queued = queued_paths;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
new_hit_cnt = queued_paths + unique_crashes;
|
|
|
|
|
|
if (!splice_cycle) {
|
|
|
stage_finds[STAGE_HAVOC] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_HAVOC] += stage_max;
|
|
|
} else {
|
|
|
stage_finds[STAGE_SPLICE] += new_hit_cnt - orig_hit_cnt;
|
|
|
stage_cycles[STAGE_SPLICE] += stage_max;
|
|
|
}
|
|
|
|
|
|
#ifndef IGNORE_FINDS
|
|
|
|
|
|
/************
|
|
|
* SPLICING *
|
|
|
************/
|
|
|
|
|
|
/* This is a last-resort strategy triggered by a full round with no findings.
|
|
|
It takes the current input file, randomly selects another input, and
|
|
|
splices them together at some offset, then relies on the havoc
|
|
|
code to mutate that blob. */
|
|
|
|
|
|
retry_splicing:
|
|
|
|
|
|
if (use_splicing && splice_cycle++ < SPLICE_CYCLES &&
|
|
|
queued_paths > 1 && queue_cur->len > 1) {
|
|
|
|
|
|
struct queue_entry* target;
|
|
|
u32 tid, split_at;
|
|
|
u8* new_buf;
|
|
|
s32 f_diff, l_diff;
|
|
|
|
|
|
/* First of all, if we've modified in_buf for havoc, let's clean that
|
|
|
up... */
|
|
|
|
|
|
if (in_buf != orig_in) {
|
|
|
ck_free(in_buf);
|
|
|
in_buf = orig_in;
|
|
|
len = queue_cur->len;
|
|
|
}
|
|
|
|
|
|
/* Pick a random queue entry and seek to it. Don't splice with yourself. */
|
|
|
|
|
|
do { tid = UR(queued_paths); } while (tid == current_entry);
|
|
|
|
|
|
splicing_with = tid;
|
|
|
target = queue;
|
|
|
|
|
|
while (tid >= 100) { target = target->next_100; tid -= 100; }
|
|
|
while (tid--) target = target->next;
|
|
|
|
|
|
/* Make sure that the target has a reasonable length. */
|
|
|
|
|
|
while (target && (target->len < 2 || target == queue_cur)) {
|
|
|
target = target->next;
|
|
|
splicing_with++;
|
|
|
}
|
|
|
|
|
|
if (!target) goto retry_splicing;
|
|
|
|
|
|
/* Read the testcase into a new buffer. */
|
|
|
|
|
|
fd = open(target->fname, O_RDONLY);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to open '%s'", target->fname);
|
|
|
|
|
|
new_buf = ck_alloc_nozero(target->len);
|
|
|
|
|
|
ck_read(fd, new_buf, target->len, target->fname);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
/* Find a suitable splicing location, somewhere between the first and
|
|
|
the last differing byte. Bail out if the difference is just a single
|
|
|
byte or so. */
|
|
|
|
|
|
locate_diffs(in_buf, new_buf, MIN(len, target->len), &f_diff, &l_diff);
|
|
|
|
|
|
if (f_diff < 0 || l_diff < 2 || f_diff == l_diff) {
|
|
|
ck_free(new_buf);
|
|
|
goto retry_splicing;
|
|
|
}
|
|
|
|
|
|
/* Split somewhere between the first and last differing byte. */
|
|
|
|
|
|
split_at = f_diff + UR(l_diff - f_diff);
|
|
|
|
|
|
/* Do the thing. */
|
|
|
|
|
|
len = target->len;
|
|
|
memcpy(new_buf, in_buf, split_at);
|
|
|
in_buf = new_buf;
|
|
|
|
|
|
ck_free(out_buf);
|
|
|
out_buf = ck_alloc_nozero(len);
|
|
|
memcpy(out_buf, in_buf, len);
|
|
|
|
|
|
goto havoc_stage;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* !IGNORE_FINDS */
|
|
|
|
|
|
ret_val = 0;
|
|
|
|
|
|
abandon_entry:
|
|
|
|
|
|
splicing_with = -1;
|
|
|
|
|
|
/* Update pending_not_fuzzed count if we made it through the calibration
|
|
|
cycle and have not seen this entry before. */
|
|
|
|
|
|
if (!stop_soon && !queue_cur->cal_failed && !queue_cur->was_fuzzed) {
|
|
|
queue_cur->was_fuzzed = 1;
|
|
|
pending_not_fuzzed--;
|
|
|
if (queue_cur->favored) pending_favored--;
|
|
|
}
|
|
|
|
|
|
munmap(orig_in, queue_cur->len);
|
|
|
|
|
|
if (in_buf != orig_in) ck_free(in_buf);
|
|
|
ck_free(out_buf);
|
|
|
ck_free(eff_map);
|
|
|
|
|
|
return ret_val;
|
|
|
|
|
|
#undef FLIP_BIT
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Grab interesting test cases from other fuzzers. */
|
|
|
|
|
|
static void sync_fuzzers(char** argv) {
|
|
|
|
|
|
DIR* sd;
|
|
|
struct dirent* sd_ent;
|
|
|
u32 sync_cnt = 0;
|
|
|
|
|
|
sd = opendir(sync_dir);
|
|
|
if (!sd) PFATAL("Unable to open '%s'", sync_dir);
|
|
|
|
|
|
stage_max = stage_cur = 0;
|
|
|
cur_depth = 0;
|
|
|
|
|
|
/* Look at the entries created for every other fuzzer in the sync directory. */
|
|
|
|
|
|
while ((sd_ent = readdir(sd))) {
|
|
|
|
|
|
static u8 stage_tmp[128];
|
|
|
|
|
|
DIR* qd;
|
|
|
struct dirent* qd_ent;
|
|
|
u8 *qd_path, *qd_synced_path;
|
|
|
u32 min_accept = 0, next_min_accept;
|
|
|
|
|
|
s32 id_fd;
|
|
|
|
|
|
/* Skip dot files and our own output directory. */
|
|
|
|
|
|
if (sd_ent->d_name[0] == '.' || !strcmp(sync_id, sd_ent->d_name)) continue;
|
|
|
|
|
|
/* Skip anything that doesn't have a queue/ subdirectory. */
|
|
|
|
|
|
qd_path = alloc_printf("%s/%s/queue", sync_dir, sd_ent->d_name);
|
|
|
|
|
|
if (!(qd = opendir(qd_path))) {
|
|
|
ck_free(qd_path);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
/* Retrieve the ID of the last seen test case. */
|
|
|
|
|
|
qd_synced_path = alloc_printf("%s/.synced/%s", out_dir, sd_ent->d_name);
|
|
|
|
|
|
id_fd = open(qd_synced_path, O_RDWR | O_CREAT, 0600);
|
|
|
|
|
|
if (id_fd < 0) PFATAL("Unable to create '%s'", qd_synced_path);
|
|
|
|
|
|
if (read(id_fd, &min_accept, sizeof(u32)) > 0)
|
|
|
lseek(id_fd, 0, SEEK_SET);
|
|
|
|
|
|
next_min_accept = min_accept;
|
|
|
|
|
|
/* Show stats */
|
|
|
|
|
|
sprintf(stage_tmp, "sync %u", ++sync_cnt);
|
|
|
stage_name = stage_tmp;
|
|
|
stage_cur = 0;
|
|
|
stage_max = 0;
|
|
|
|
|
|
/* For every file queued by this fuzzer, parse ID and see if we have looked at
|
|
|
it before; exec a test case if not. */
|
|
|
|
|
|
while ((qd_ent = readdir(qd))) {
|
|
|
|
|
|
u8* path;
|
|
|
s32 fd;
|
|
|
struct stat st;
|
|
|
|
|
|
if (qd_ent->d_name[0] == '.' ||
|
|
|
sscanf(qd_ent->d_name, CASE_PREFIX "%06u", &syncing_case) != 1 ||
|
|
|
syncing_case < min_accept) continue;
|
|
|
|
|
|
/* OK, sounds like a new one. Let's give it a try. */
|
|
|
|
|
|
if (syncing_case >= next_min_accept)
|
|
|
next_min_accept = syncing_case + 1;
|
|
|
|
|
|
path = alloc_printf("%s/%s", qd_path, qd_ent->d_name);
|
|
|
|
|
|
/* Allow this to fail in case the other fuzzer is resuming or so... */
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
|
|
if (fd < 0) {
|
|
|
ck_free(path);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if (fstat(fd, &st)) PFATAL("fstat() failed");
|
|
|
|
|
|
/* Ignore zero-sized or oversized files. */
|
|
|
|
|
|
if (st.st_size && st.st_size <= MAX_FILE) {
|
|
|
|
|
|
u8 fault;
|
|
|
u8* mem = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
|
|
if (mem == MAP_FAILED) PFATAL("Unable to mmap '%s'", path);
|
|
|
|
|
|
/* See what happens. We rely on save_if_interesting() to catch major
|
|
|
errors and save the test case. */
|
|
|
|
|
|
write_to_testcase(mem, st.st_size);
|
|
|
|
|
|
fault = run_target(argv, exec_tmout);
|
|
|
|
|
|
if (stop_soon) return;
|
|
|
|
|
|
syncing_party = sd_ent->d_name;
|
|
|
queued_imported += save_if_interesting(argv, mem, st.st_size, fault);
|
|
|
syncing_party = 0;
|
|
|
|
|
|
munmap(mem, st.st_size);
|
|
|
|
|
|
if (!(stage_cur++ % stats_update_freq)) show_stats();
|
|
|
|
|
|
}
|
|
|
|
|
|
ck_free(path);
|
|
|
close(fd);
|
|
|
|
|
|
}
|
|
|
|
|
|
ck_write(id_fd, &next_min_accept, sizeof(u32), qd_synced_path);
|
|
|
|
|
|
close(id_fd);
|
|
|
closedir(qd);
|
|
|
ck_free(qd_path);
|
|
|
ck_free(qd_synced_path);
|
|
|
|
|
|
}
|
|
|
|
|
|
closedir(sd);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Handle stop signal (Ctrl-C, etc). */
|
|
|
|
|
|
static void handle_stop_sig(int sig) {
|
|
|
|
|
|
stop_soon = 1;
|
|
|
|
|
|
if (child_pid > 0) kill(child_pid, SIGKILL);
|
|
|
if (forksrv_pid > 0) kill(forksrv_pid, SIGKILL);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Handle skip request (SIGUSR1). */
|
|
|
|
|
|
static void handle_skipreq(int sig) {
|
|
|
|
|
|
skip_requested = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Handle timeout (SIGALRM). */
|
|
|
|
|
|
static void handle_timeout(int sig) {
|
|
|
|
|
|
if (child_pid > 0) {
|
|
|
|
|
|
child_timed_out = 1;
|
|
|
kill(child_pid, SIGKILL);
|
|
|
|
|
|
} else if (child_pid == -1 && forksrv_pid > 0) {
|
|
|
|
|
|
child_timed_out = 1;
|
|
|
kill(forksrv_pid, SIGKILL);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Do a PATH search and find target binary to see that it exists and
|
|
|
isn't a shell script - a common and painful mistake. We also check for
|
|
|
a valid ELF header and for evidence of AFL instrumentation. */
|
|
|
|
|
|
EXP_ST void check_binary(u8* fname) {
|
|
|
|
|
|
u8* env_path = 0;
|
|
|
struct stat st;
|
|
|
|
|
|
s32 fd;
|
|
|
u8* f_data;
|
|
|
u32 f_len = 0;
|
|
|
|
|
|
ACTF("Validating target binary...");
|
|
|
|
|
|
if (strchr(fname, '/') || !(env_path = getenv("PATH"))) {
|
|
|
|
|
|
target_path = ck_strdup(fname);
|
|
|
if (stat(target_path, &st) || !S_ISREG(st.st_mode) ||
|
|
|
!(st.st_mode & 0111) || (f_len = st.st_size) < 4)
|
|
|
FATAL("Program '%s' not found or not executable", fname);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
while (env_path) {
|
|
|
|
|
|
u8 *cur_elem, *delim = strchr(env_path, ':');
|
|
|
|
|
|
if (delim) {
|
|
|
|
|
|
cur_elem = ck_alloc(delim - env_path + 1);
|
|
|
memcpy(cur_elem, env_path, delim - env_path);
|
|
|
delim++;
|
|
|
|
|
|
} else cur_elem = ck_strdup(env_path);
|
|
|
|
|
|
env_path = delim;
|
|
|
|
|
|
if (cur_elem[0])
|
|
|
target_path = alloc_printf("%s/%s", cur_elem, fname);
|
|
|
else
|
|
|
target_path = ck_strdup(fname);
|
|
|
|
|
|
ck_free(cur_elem);
|
|
|
|
|
|
if (!stat(target_path, &st) && S_ISREG(st.st_mode) &&
|
|
|
(st.st_mode & 0111) && (f_len = st.st_size) >= 4) break;
|
|
|
|
|
|
ck_free(target_path);
|
|
|
target_path = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!target_path) FATAL("Program '%s' not found or not executable", fname);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (getenv("AFL_SKIP_BIN_CHECK")) return;
|
|
|
|
|
|
/* Check for blatant user errors. */
|
|
|
|
|
|
if ((!strncmp(target_path, "/tmp/", 5) && !strchr(target_path + 5, '/')) ||
|
|
|
(!strncmp(target_path, "/var/tmp/", 9) && !strchr(target_path + 9, '/')))
|
|
|
FATAL("Please don't keep binaries in /tmp or /var/tmp");
|
|
|
|
|
|
fd = open(target_path, O_RDONLY);
|
|
|
|
|
|
if (fd < 0) PFATAL("Unable to open '%s'", target_path);
|
|
|
|
|
|
f_data = mmap(0, f_len, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
|
|
if (f_data == MAP_FAILED) PFATAL("Unable to mmap file '%s'", target_path);
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
if (f_data[0] == '#' && f_data[1] == '!') {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Oops, the target binary looks like a shell script. Some build systems will\n"
|
|
|
" sometimes generate shell stubs for dynamically linked programs; try static\n"
|
|
|
" library mode (./configure --disable-shared) if that's the case.\n\n"
|
|
|
|
|
|
" Another possible cause is that you are actually trying to use a shell\n"
|
|
|
" wrapper around the fuzzed component. Invoking shell can slow down the\n"
|
|
|
" fuzzing process by a factor of 20x or more; it's best to write the wrapper\n"
|
|
|
" in a compiled language instead.\n");
|
|
|
|
|
|
FATAL("Program '%s' is a shell script", target_path);
|
|
|
|
|
|
}
|
|
|
|
|
|
#ifndef __APPLE__
|
|
|
|
|
|
if (f_data[0] != 0x7f || memcmp(f_data + 1, "ELF", 3))
|
|
|
FATAL("Program '%s' is not an ELF binary", target_path);
|
|
|
|
|
|
#else
|
|
|
|
|
|
if (f_data[0] != 0xCF || f_data[1] != 0xFA || f_data[2] != 0xED)
|
|
|
FATAL("Program '%s' is not a 64-bit Mach-O binary", target_path);
|
|
|
|
|
|
#endif /* ^!__APPLE__ */
|
|
|
|
|
|
if (!qemu_mode && !dumb_mode &&
|
|
|
!memmem(f_data, f_len, SHM_ENV_VAR, strlen(SHM_ENV_VAR) + 1)) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Looks like the target binary is not instrumented! The fuzzer depends on\n"
|
|
|
" compile-time instrumentation to isolate interesting test cases while\n"
|
|
|
" mutating the input data. For more information, and for tips on how to\n"
|
|
|
" instrument binaries, please see %s/README.\n\n"
|
|
|
|
|
|
" When source code is not available, you may be able to leverage QEMU\n"
|
|
|
" mode support. Consult the README for tips on how to enable this.\n"
|
|
|
|
|
|
" (It is also possible to use afl-fuzz as a traditional, \"dumb\" fuzzer.\n"
|
|
|
" For that, you can use the -n option - but expect much worse results.)\n",
|
|
|
doc_path);
|
|
|
|
|
|
FATAL("No instrumentation detected");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (qemu_mode &&
|
|
|
memmem(f_data, f_len, SHM_ENV_VAR, strlen(SHM_ENV_VAR) + 1)) {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"This program appears to be instrumented with afl-gcc, but is being run in\n"
|
|
|
" QEMU mode (-Q). This is probably not what you want - this setup will be\n"
|
|
|
" slow and offer no practical benefits.\n");
|
|
|
|
|
|
FATAL("Instrumentation found in -Q mode");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (memmem(f_data, f_len, "libasan.so", 10) ||
|
|
|
memmem(f_data, f_len, "__msan_init", 11)) uses_asan = 1;
|
|
|
|
|
|
/* Detect persistent & deferred init signatures in the binary. */
|
|
|
|
|
|
if (memmem(f_data, f_len, PERSIST_SIG, strlen(PERSIST_SIG) + 1)) {
|
|
|
|
|
|
OKF(cPIN "Persistent mode binary detected.");
|
|
|
setenv(PERSIST_ENV_VAR, "1", 1);
|
|
|
persistent_mode = 1;
|
|
|
|
|
|
} else if (getenv("AFL_PERSISTENT")) {
|
|
|
|
|
|
WARNF("AFL_PERSISTENT is no longer supported and may misbehave!");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (memmem(f_data, f_len, DEFER_SIG, strlen(DEFER_SIG) + 1)) {
|
|
|
|
|
|
OKF(cPIN "Deferred forkserver binary detected.");
|
|
|
setenv(DEFER_ENV_VAR, "1", 1);
|
|
|
deferred_mode = 1;
|
|
|
|
|
|
} else if (getenv("AFL_DEFER_FORKSRV")) {
|
|
|
|
|
|
WARNF("AFL_DEFER_FORKSRV is no longer supported and may misbehave!");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (munmap(f_data, f_len)) PFATAL("unmap() failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Trim and possibly create a banner for the run. */
|
|
|
|
|
|
static void fix_up_banner(u8* name) {
|
|
|
|
|
|
if (!use_banner) {
|
|
|
|
|
|
if (sync_id) {
|
|
|
|
|
|
use_banner = sync_id;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
u8* trim = strrchr(name, '/');
|
|
|
if (!trim) use_banner = name; else use_banner = trim + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (strlen(use_banner) > 40) {
|
|
|
|
|
|
u8* tmp = ck_alloc(44);
|
|
|
sprintf(tmp, "%.40s...", use_banner);
|
|
|
use_banner = tmp;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Check if we're on TTY. */
|
|
|
|
|
|
static void check_if_tty(void) {
|
|
|
|
|
|
struct winsize ws;
|
|
|
|
|
|
if (getenv("AFL_NO_UI")) {
|
|
|
OKF("Disabling the UI because AFL_NO_UI is set.");
|
|
|
not_on_tty = 1;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (ioctl(1, TIOCGWINSZ, &ws)) {
|
|
|
|
|
|
if (errno == ENOTTY) {
|
|
|
OKF("Looks like we're not running on a tty, so I'll be a bit less verbose.");
|
|
|
not_on_tty = 1;
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Check terminal dimensions after resize. */
|
|
|
|
|
|
static void check_term_size(void) {
|
|
|
|
|
|
struct winsize ws;
|
|
|
|
|
|
term_too_small = 0;
|
|
|
|
|
|
if (ioctl(1, TIOCGWINSZ, &ws)) return;
|
|
|
|
|
|
if (ws.ws_row == 0 && ws.ws_col == 0) return;
|
|
|
if (ws.ws_row < 25 || ws.ws_col < 80) term_too_small = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Display usage hints. */
|
|
|
|
|
|
static void usage(u8* argv0) {
|
|
|
|
|
|
SAYF("\n%s [ options ] -- /path/to/fuzzed_app [ ... ]\n\n"
|
|
|
|
|
|
"Required parameters:\n\n"
|
|
|
|
|
|
" -i dir - input directory with test cases\n"
|
|
|
" -o dir - output directory for fuzzer findings\n\n"
|
|
|
|
|
|
"Execution control settings:\n\n"
|
|
|
|
|
|
" -f file - location read by the fuzzed program (stdin)\n"
|
|
|
" -t msec - timeout for each run (auto-scaled, 50-%u ms)\n"
|
|
|
" -m megs - memory limit for child process (%u MB)\n"
|
|
|
" -Q - use binary-only instrumentation (QEMU mode)\n\n"
|
|
|
|
|
|
"Fuzzing behavior settings:\n\n"
|
|
|
|
|
|
" -d - quick & dirty mode (skips deterministic steps)\n"
|
|
|
" -n - fuzz without instrumentation (dumb mode)\n"
|
|
|
" -x dir - optional fuzzer dictionary (see README)\n\n"
|
|
|
|
|
|
"Other stuff:\n\n"
|
|
|
|
|
|
" -T text - text banner to show on the screen\n"
|
|
|
" -M / -S id - distributed mode (see parallel_fuzzing.txt)\n"
|
|
|
" -C - crash exploration mode (the peruvian rabbit thing)\n"
|
|
|
" -V - show version number and exit\n\n"
|
|
|
" -b cpu_id - bind the fuzzing process to the specified CPU core\n\n"
|
|
|
|
|
|
"For additional tips, please consult %s/README.\n\n",
|
|
|
|
|
|
argv0, EXEC_TIMEOUT, MEM_LIMIT, doc_path);
|
|
|
|
|
|
exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Prepare output directories and fds. */
|
|
|
|
|
|
EXP_ST void setup_dirs_fds(void) {
|
|
|
|
|
|
u8* tmp;
|
|
|
s32 fd;
|
|
|
|
|
|
ACTF("Setting up output directories...");
|
|
|
|
|
|
if (sync_id && mkdir(sync_dir, 0700) && errno != EEXIST)
|
|
|
PFATAL("Unable to create '%s'", sync_dir);
|
|
|
|
|
|
if (mkdir(out_dir, 0700)) {
|
|
|
|
|
|
if (errno != EEXIST) PFATAL("Unable to create '%s'", out_dir);
|
|
|
|
|
|
maybe_delete_out_dir();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (in_place_resume)
|
|
|
FATAL("Resume attempted but old output directory not found");
|
|
|
|
|
|
out_dir_fd = open(out_dir, O_RDONLY);
|
|
|
|
|
|
#ifndef __sun
|
|
|
|
|
|
if (out_dir_fd < 0 || flock(out_dir_fd, LOCK_EX | LOCK_NB))
|
|
|
PFATAL("Unable to flock() output directory.");
|
|
|
|
|
|
#endif /* !__sun */
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Queue directory for any starting & discovered paths. */
|
|
|
|
|
|
tmp = alloc_printf("%s/queue", out_dir);
|
|
|
if (mkdir(tmp, 0700)) PFATAL("Unable to create '%s'", tmp);
|
|
|
ck_free(tmp);
|
|
|
|
|
|
/* Top-level directory for queue metadata used for session
|
|
|
resume and related tasks. */
|
|
|
|
|
|
tmp = alloc_printf("%s/queue/.state/", out_dir);
|
|
|
if (mkdir(tmp, 0700)) PFATAL("Unable to create '%s'", tmp);
|
|
|
ck_free(tmp);
|
|
|
|
|
|
/* Directory for flagging queue entries that went through
|
|
|
deterministic fuzzing in the past. */
|
|
|
|
|
|
tmp = alloc_printf("%s/queue/.state/deterministic_done/", out_dir);
|
|
|
if (mkdir(tmp, 0700)) PFATAL("Unable to create '%s'", tmp);
|
|
|
ck_free(tmp);
|
|
|
|
|
|
/* Directory with the auto-selected dictionary entries. */
|
|
|
|
|
|
tmp = alloc_printf("%s/queue/.state/auto_extras/", out_dir);
|
|
|
if (mkdir(tmp, 0700)) PFATAL("Unable to create '%s'", tmp);
|
|
|
ck_free(tmp);
|
|
|
|
|
|
/* The set of paths currently deemed redundant. */
|
|
|
|
|
|
tmp = alloc_printf("%s/queue/.state/redundant_edges/", out_dir);
|
|
|
if (mkdir(tmp, 0700)) PFATAL("Unable to create '%s'", tmp);
|
|
|
ck_free(tmp);
|
|
|
|
|
|
/* The set of paths showing variable behavior. */
|
|
|
|
|
|
tmp = alloc_printf("%s/queue/.state/variable_behavior/", out_dir);
|
|
|
if (mkdir(tmp, 0700)) PFATAL("Unable to create '%s'", tmp);
|
|
|
ck_free(tmp);
|
|
|
|
|
|
/* Sync directory for keeping track of cooperating fuzzers. */
|
|
|
|
|
|
if (sync_id) {
|
|
|
|
|
|
tmp = alloc_printf("%s/.synced/", out_dir);
|
|
|
|
|
|
if (mkdir(tmp, 0700) && (!in_place_resume || errno != EEXIST))
|
|
|
PFATAL("Unable to create '%s'", tmp);
|
|
|
|
|
|
ck_free(tmp);
|
|
|
|
|
|
}
|
|
|
|
|
|
/* All recorded crashes. */
|
|
|
|
|
|
tmp = alloc_printf("%s/crashes", out_dir);
|
|
|
if (mkdir(tmp, 0700)) PFATAL("Unable to create '%s'", tmp);
|
|
|
ck_free(tmp);
|
|
|
|
|
|
/* All recorded hangs. */
|
|
|
|
|
|
tmp = alloc_printf("%s/hangs", out_dir);
|
|
|
if (mkdir(tmp, 0700)) PFATAL("Unable to create '%s'", tmp);
|
|
|
ck_free(tmp);
|
|
|
|
|
|
/* Generally useful file descriptors. */
|
|
|
|
|
|
dev_null_fd = open("/dev/null", O_RDWR);
|
|
|
if (dev_null_fd < 0) PFATAL("Unable to open /dev/null");
|
|
|
|
|
|
dev_urandom_fd = open("/dev/urandom", O_RDONLY);
|
|
|
if (dev_urandom_fd < 0) PFATAL("Unable to open /dev/urandom");
|
|
|
|
|
|
/* Gnuplot output file. */
|
|
|
|
|
|
tmp = alloc_printf("%s/plot_data", out_dir);
|
|
|
fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
if (fd < 0) PFATAL("Unable to create '%s'", tmp);
|
|
|
ck_free(tmp);
|
|
|
|
|
|
plot_file = fdopen(fd, "w");
|
|
|
if (!plot_file) PFATAL("fdopen() failed");
|
|
|
|
|
|
fprintf(plot_file, "# unix_time, cycles_done, cur_path, paths_total, "
|
|
|
"pending_total, pending_favs, map_size, unique_crashes, "
|
|
|
"unique_hangs, max_depth, execs_per_sec\n");
|
|
|
/* ignore errors */
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Setup the output file for fuzzed data, if not using -f. */
|
|
|
|
|
|
EXP_ST void setup_stdio_file(void) {
|
|
|
|
|
|
u8* fn = alloc_printf("%s/.cur_input", out_dir);
|
|
|
|
|
|
unlink(fn); /* Ignore errors */
|
|
|
|
|
|
out_fd = open(fn, O_RDWR | O_CREAT | O_EXCL, 0600);
|
|
|
|
|
|
if (out_fd < 0) PFATAL("Unable to create '%s'", fn);
|
|
|
|
|
|
ck_free(fn);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Make sure that core dumps don't go to a program. */
|
|
|
|
|
|
static void check_crash_handling(void) {
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
/* Yuck! There appears to be no simple C API to query for the state of
|
|
|
loaded daemons on MacOS X, and I'm a bit hesitant to do something
|
|
|
more sophisticated, such as disabling crash reporting via Mach ports,
|
|
|
until I get a box to test the code. So, for now, we check for crash
|
|
|
reporting the awful way. */
|
|
|
|
|
|
if (system("launchctl list 2>/dev/null | grep -q '\\.ReportCrash$'")) return;
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Whoops, your system is configured to forward crash notifications to an\n"
|
|
|
" external crash reporting utility. This will cause issues due to the\n"
|
|
|
" extended delay between the fuzzed binary malfunctioning and this fact\n"
|
|
|
" being relayed to the fuzzer via the standard waitpid() API.\n\n"
|
|
|
" To avoid having crashes misinterpreted as timeouts, please run the\n"
|
|
|
" following commands:\n\n"
|
|
|
|
|
|
" SL=/System/Library; PL=com.apple.ReportCrash\n"
|
|
|
" launchctl unload -w ${SL}/LaunchAgents/${PL}.plist\n"
|
|
|
" sudo launchctl unload -w ${SL}/LaunchDaemons/${PL}.Root.plist\n");
|
|
|
|
|
|
if (!getenv("AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES"))
|
|
|
FATAL("Crash reporter detected");
|
|
|
|
|
|
#else
|
|
|
|
|
|
/* This is Linux specific, but I don't think there's anything equivalent on
|
|
|
*BSD, so we can just let it slide for now. */
|
|
|
|
|
|
s32 fd = open("/proc/sys/kernel/core_pattern", O_RDONLY);
|
|
|
u8 fchar;
|
|
|
|
|
|
if (fd < 0) return;
|
|
|
|
|
|
ACTF("Checking core_pattern...");
|
|
|
|
|
|
if (read(fd, &fchar, 1) == 1 && fchar == '|') {
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Hmm, your system is configured to send core dump notifications to an\n"
|
|
|
" external utility. This will cause issues: there will be an extended delay\n"
|
|
|
" between stumbling upon a crash and having this information relayed to the\n"
|
|
|
" fuzzer via the standard waitpid() API.\n\n"
|
|
|
|
|
|
" To avoid having crashes misinterpreted as timeouts, please log in as root\n"
|
|
|
" and temporarily modify /proc/sys/kernel/core_pattern, like so:\n\n"
|
|
|
|
|
|
" echo core >/proc/sys/kernel/core_pattern\n");
|
|
|
|
|
|
if (!getenv("AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES"))
|
|
|
FATAL("Pipe at the beginning of 'core_pattern'");
|
|
|
|
|
|
}
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
#endif /* ^__APPLE__ */
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Check CPU governor. */
|
|
|
|
|
|
static void check_cpu_governor(void) {
|
|
|
|
|
|
FILE* f;
|
|
|
u8 tmp[128];
|
|
|
u64 min = 0, max = 0;
|
|
|
|
|
|
if (getenv("AFL_SKIP_CPUFREQ")) return;
|
|
|
|
|
|
f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor", "r");
|
|
|
if (!f) return;
|
|
|
|
|
|
ACTF("Checking CPU scaling governor...");
|
|
|
|
|
|
if (!fgets(tmp, 128, f)) PFATAL("fgets() failed");
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
if (!strncmp(tmp, "perf", 4)) return;
|
|
|
|
|
|
f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq", "r");
|
|
|
|
|
|
if (f) {
|
|
|
if (fscanf(f, "%llu", &min) != 1) min = 0;
|
|
|
fclose(f);
|
|
|
}
|
|
|
|
|
|
f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", "r");
|
|
|
|
|
|
if (f) {
|
|
|
if (fscanf(f, "%llu", &max) != 1) max = 0;
|
|
|
fclose(f);
|
|
|
}
|
|
|
|
|
|
if (min == max) return;
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Whoops, your system uses on-demand CPU frequency scaling, adjusted\n"
|
|
|
" between %llu and %llu MHz. Unfortunately, the scaling algorithm in the\n"
|
|
|
" kernel is imperfect and can miss the short-lived processes spawned by\n"
|
|
|
" afl-fuzz. To keep things moving, run these commands as root:\n\n"
|
|
|
|
|
|
" cd /sys/devices/system/cpu\n"
|
|
|
" echo performance | tee cpu*/cpufreq/scaling_governor\n\n"
|
|
|
|
|
|
" You can later go back to the original state by replacing 'performance' with\n"
|
|
|
" 'ondemand'. If you don't want to change the settings, set AFL_SKIP_CPUFREQ\n"
|
|
|
" to make afl-fuzz skip this check - but expect some performance drop.\n",
|
|
|
min / 1024, max / 1024);
|
|
|
|
|
|
FATAL("Suboptimal CPU scaling governor");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Count the number of logical CPU cores. */
|
|
|
|
|
|
static void get_core_count(void) {
|
|
|
|
|
|
u32 cur_runnable = 0;
|
|
|
|
|
|
#if defined(__APPLE__) || defined(__FreeBSD__) || defined (__OpenBSD__)
|
|
|
|
|
|
size_t s = sizeof(cpu_core_count);
|
|
|
|
|
|
/* On *BSD systems, we can just use a sysctl to get the number of CPUs. */
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
if (sysctlbyname("hw.logicalcpu", &cpu_core_count, &s, NULL, 0) < 0)
|
|
|
return;
|
|
|
|
|
|
#else
|
|
|
|
|
|
int s_name[2] = { CTL_HW, HW_NCPU };
|
|
|
|
|
|
if (sysctl(s_name, 2, &cpu_core_count, &s, NULL, 0) < 0) return;
|
|
|
|
|
|
#endif /* ^__APPLE__ */
|
|
|
|
|
|
#else
|
|
|
|
|
|
#ifdef HAVE_AFFINITY
|
|
|
|
|
|
cpu_core_count = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
|
|
#else
|
|
|
|
|
|
FILE* f = fopen("/proc/stat", "r");
|
|
|
u8 tmp[1024];
|
|
|
|
|
|
if (!f) return;
|
|
|
|
|
|
while (fgets(tmp, sizeof(tmp), f))
|
|
|
if (!strncmp(tmp, "cpu", 3) && isdigit(tmp[3])) cpu_core_count++;
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
#endif /* ^HAVE_AFFINITY */
|
|
|
|
|
|
#endif /* ^(__APPLE__ || __FreeBSD__ || __OpenBSD__) */
|
|
|
|
|
|
if (cpu_core_count > 0) {
|
|
|
|
|
|
cur_runnable = (u32)get_runnable_processes();
|
|
|
|
|
|
#if defined(__APPLE__) || defined(__FreeBSD__) || defined (__OpenBSD__)
|
|
|
|
|
|
/* Add ourselves, since the 1-minute average doesn't include that yet. */
|
|
|
|
|
|
cur_runnable++;
|
|
|
|
|
|
#endif /* __APPLE__ || __FreeBSD__ || __OpenBSD__ */
|
|
|
|
|
|
OKF("You have %u CPU core%s and %u runnable tasks (utilization: %0.0f%%).",
|
|
|
cpu_core_count, cpu_core_count > 1 ? "s" : "",
|
|
|
cur_runnable, cur_runnable * 100.0 / cpu_core_count);
|
|
|
|
|
|
if (cpu_core_count > 1) {
|
|
|
|
|
|
if (cur_runnable > cpu_core_count * 1.5) {
|
|
|
|
|
|
WARNF("System under apparent load, performance may be spotty.");
|
|
|
|
|
|
} else if (cur_runnable + 1 <= cpu_core_count) {
|
|
|
|
|
|
OKF("Try parallel jobs - see %s/parallel_fuzzing.txt.", doc_path);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cpu_core_count = 0;
|
|
|
WARNF("Unable to figure out the number of CPU cores.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Validate and fix up out_dir and sync_dir when using -S. */
|
|
|
|
|
|
static void fix_up_sync(void) {
|
|
|
|
|
|
u8* x = sync_id;
|
|
|
|
|
|
if (dumb_mode)
|
|
|
FATAL("-S / -M and -n are mutually exclusive");
|
|
|
|
|
|
if (skip_deterministic) {
|
|
|
|
|
|
if (force_deterministic)
|
|
|
FATAL("use -S instead of -M -d");
|
|
|
else
|
|
|
FATAL("-S already implies -d");
|
|
|
|
|
|
}
|
|
|
|
|
|
while (*x) {
|
|
|
|
|
|
if (!isalnum(*x) && *x != '_' && *x != '-')
|
|
|
FATAL("Non-alphanumeric fuzzer ID specified via -S or -M");
|
|
|
|
|
|
x++;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (strlen(sync_id) > 32) FATAL("Fuzzer ID too long");
|
|
|
|
|
|
x = alloc_printf("%s/%s", out_dir, sync_id);
|
|
|
|
|
|
sync_dir = out_dir;
|
|
|
out_dir = x;
|
|
|
|
|
|
if (!force_deterministic) {
|
|
|
skip_deterministic = 1;
|
|
|
use_splicing = 1;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Handle screen resize (SIGWINCH). */
|
|
|
|
|
|
static void handle_resize(int sig) {
|
|
|
clear_screen = 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Check ASAN options. */
|
|
|
|
|
|
static void check_asan_opts(void) {
|
|
|
u8* x = getenv("ASAN_OPTIONS");
|
|
|
|
|
|
if (x) {
|
|
|
|
|
|
if (!strstr(x, "abort_on_error=1"))
|
|
|
FATAL("Custom ASAN_OPTIONS set without abort_on_error=1 - please fix!");
|
|
|
|
|
|
if (!strstr(x, "symbolize=0"))
|
|
|
FATAL("Custom ASAN_OPTIONS set without symbolize=0 - please fix!");
|
|
|
|
|
|
}
|
|
|
|
|
|
x = getenv("MSAN_OPTIONS");
|
|
|
|
|
|
if (x) {
|
|
|
|
|
|
if (!strstr(x, "exit_code=" STRINGIFY(MSAN_ERROR)))
|
|
|
FATAL("Custom MSAN_OPTIONS set without exit_code="
|
|
|
STRINGIFY(MSAN_ERROR) " - please fix!");
|
|
|
|
|
|
if (!strstr(x, "symbolize=0"))
|
|
|
FATAL("Custom MSAN_OPTIONS set without symbolize=0 - please fix!");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Detect @@ in args. */
|
|
|
|
|
|
EXP_ST void detect_file_args(char** argv) {
|
|
|
|
|
|
u32 i = 0;
|
|
|
u8* cwd = getcwd(NULL, 0);
|
|
|
|
|
|
if (!cwd) PFATAL("getcwd() failed");
|
|
|
|
|
|
while (argv[i]) {
|
|
|
|
|
|
u8* aa_loc = strstr(argv[i], "@@");
|
|
|
|
|
|
if (aa_loc) {
|
|
|
|
|
|
u8 *aa_subst, *n_arg;
|
|
|
|
|
|
/* If we don't have a file name chosen yet, use a safe default. */
|
|
|
|
|
|
if (!out_file)
|
|
|
out_file = alloc_printf("%s/.cur_input", out_dir);
|
|
|
|
|
|
/* Be sure that we're always using fully-qualified paths. */
|
|
|
|
|
|
if (out_file[0] == '/') aa_subst = out_file;
|
|
|
else aa_subst = alloc_printf("%s/%s", cwd, out_file);
|
|
|
|
|
|
/* Construct a replacement argv value. */
|
|
|
|
|
|
*aa_loc = 0;
|
|
|
n_arg = alloc_printf("%s%s%s", argv[i], aa_subst, aa_loc + 2);
|
|
|
argv[i] = n_arg;
|
|
|
*aa_loc = '@';
|
|
|
|
|
|
if (out_file[0] != '/') ck_free(aa_subst);
|
|
|
|
|
|
}
|
|
|
|
|
|
i++;
|
|
|
|
|
|
}
|
|
|
|
|
|
free(cwd); /* not tracked */
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Set up signal handlers. More complicated that needs to be, because libc on
|
|
|
Solaris doesn't resume interrupted reads(), sets SA_RESETHAND when you call
|
|
|
siginterrupt(), and does other unnecessary things. */
|
|
|
|
|
|
EXP_ST void setup_signal_handlers(void) {
|
|
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
sa.sa_handler = NULL;
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
sa.sa_sigaction = NULL;
|
|
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
|
|
/* Various ways of saying "stop". */
|
|
|
|
|
|
sa.sa_handler = handle_stop_sig;
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
|
|
/* Exec timeout notifications. */
|
|
|
|
|
|
sa.sa_handler = handle_timeout;
|
|
|
sigaction(SIGALRM, &sa, NULL);
|
|
|
|
|
|
/* Window resize */
|
|
|
|
|
|
sa.sa_handler = handle_resize;
|
|
|
sigaction(SIGWINCH, &sa, NULL);
|
|
|
|
|
|
/* SIGUSR1: skip entry */
|
|
|
|
|
|
sa.sa_handler = handle_skipreq;
|
|
|
sigaction(SIGUSR1, &sa, NULL);
|
|
|
|
|
|
/* Things we don't care about. */
|
|
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
sigaction(SIGTSTP, &sa, NULL);
|
|
|
sigaction(SIGPIPE, &sa, NULL);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Rewrite argv for QEMU. */
|
|
|
|
|
|
static char** get_qemu_argv(u8* own_loc, char** argv, int argc) {
|
|
|
|
|
|
char** new_argv = ck_alloc(sizeof(char*) * (argc + 4));
|
|
|
u8 *tmp, *cp, *rsl, *own_copy;
|
|
|
|
|
|
/* Workaround for a QEMU stability glitch. */
|
|
|
|
|
|
setenv("QEMU_LOG", "nochain", 1);
|
|
|
|
|
|
memcpy(new_argv + 3, 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]. */
|
|
|
|
|
|
tmp = getenv("AFL_PATH");
|
|
|
|
|
|
if (tmp) {
|
|
|
|
|
|
cp = alloc_printf("%s/afl-qemu-trace", tmp);
|
|
|
|
|
|
if (access(cp, X_OK))
|
|
|
FATAL("Unable to find '%s'", tmp);
|
|
|
|
|
|
target_path = new_argv[0] = cp;
|
|
|
return new_argv;
|
|
|
|
|
|
}
|
|
|
|
|
|
own_copy = ck_strdup(own_loc);
|
|
|
rsl = strrchr(own_copy, '/');
|
|
|
|
|
|
if (rsl) {
|
|
|
|
|
|
*rsl = 0;
|
|
|
|
|
|
cp = alloc_printf("%s/afl-qemu-trace", own_copy);
|
|
|
ck_free(own_copy);
|
|
|
|
|
|
if (!access(cp, X_OK)) {
|
|
|
|
|
|
target_path = new_argv[0] = cp;
|
|
|
return new_argv;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else ck_free(own_copy);
|
|
|
|
|
|
if (!access(BIN_PATH "/afl-qemu-trace", X_OK)) {
|
|
|
|
|
|
target_path = new_argv[0] = ck_strdup(BIN_PATH "/afl-qemu-trace");
|
|
|
return new_argv;
|
|
|
|
|
|
}
|
|
|
|
|
|
SAYF("\n" cLRD "[-] " cRST
|
|
|
"Oops, unable to find the 'afl-qemu-trace' binary. The binary must be built\n"
|
|
|
" separately by following the instructions in qemu_mode/README.qemu. If you\n"
|
|
|
" already have the binary installed, you may need to specify AFL_PATH in the\n"
|
|
|
" environment.\n\n"
|
|
|
|
|
|
" Of course, even without QEMU, afl-fuzz can still work with binaries that are\n"
|
|
|
" instrumented at compile time with afl-gcc. It is also possible to use it as a\n"
|
|
|
" traditional \"dumb\" fuzzer by specifying '-n' in the command line.\n");
|
|
|
|
|
|
FATAL("Failed to locate 'afl-qemu-trace'.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/* Make a copy of the current command line. */
|
|
|
|
|
|
static void save_cmdline(u32 argc, char** argv) {
|
|
|
|
|
|
u32 len = 1, i;
|
|
|
u8* buf;
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
len += strlen(argv[i]) + 1;
|
|
|
|
|
|
buf = orig_cmdline = ck_alloc(len);
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
|
|
u32 l = strlen(argv[i]);
|
|
|
|
|
|
memcpy(buf, argv[i], l);
|
|
|
buf += l;
|
|
|
|
|
|
if (i != argc - 1) *(buf++) = ' ';
|
|
|
|
|
|
}
|
|
|
|
|
|
*buf = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
#ifndef AFL_LIB
|
|
|
|
|
|
/* Main entry point */
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
|
s32 opt;
|
|
|
u64 prev_queued = 0;
|
|
|
u32 sync_interval_cnt = 0, seek_to;
|
|
|
u8 *extras_dir = 0;
|
|
|
u8 mem_limit_given = 0;
|
|
|
u8 exit_1 = !!getenv("AFL_BENCH_JUST_ONE");
|
|
|
char** use_argv;
|
|
|
|
|
|
struct timeval tv;
|
|
|
struct timezone tz;
|
|
|
|
|
|
SAYF(cCYA "afl-fuzz " cBRI VERSION cRST " by <lcamtuf@google.com>\n");
|
|
|
|
|
|
doc_path = access(DOC_PATH, F_OK) ? "docs" : DOC_PATH;
|
|
|
|
|
|
gettimeofday(&tv, &tz);
|
|
|
srandom(tv.tv_sec ^ tv.tv_usec ^ getpid());
|
|
|
|
|
|
while ((opt = getopt(argc, argv, "+i:o:f:m:b:t:T:dnCB:S:M:x:QV")) > 0)
|
|
|
|
|
|
switch (opt) {
|
|
|
|
|
|
case 'i': /* input dir */
|
|
|
|
|
|
if (in_dir) FATAL("Multiple -i options not supported");
|
|
|
in_dir = optarg;
|
|
|
|
|
|
if (!strcmp(in_dir, "-")) in_place_resume = 1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'o': /* output dir */
|
|
|
|
|
|
if (out_dir) FATAL("Multiple -o options not supported");
|
|
|
out_dir = optarg;
|
|
|
break;
|
|
|
|
|
|
case 'M': { /* master sync ID */
|
|
|
|
|
|
u8* c;
|
|
|
|
|
|
if (sync_id) FATAL("Multiple -S or -M options not supported");
|
|
|
sync_id = ck_strdup(optarg);
|
|
|
|
|
|
if ((c = strchr(sync_id, ':'))) {
|
|
|
|
|
|
*c = 0;
|
|
|
|
|
|
if (sscanf(c + 1, "%u/%u", &master_id, &master_max) != 2 ||
|
|
|
!master_id || !master_max || master_id > master_max ||
|
|
|
master_max > 1000000) FATAL("Bogus master ID passed to -M");
|
|
|
|
|
|
}
|
|
|
|
|
|
force_deterministic = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'S':
|
|
|
|
|
|
if (sync_id) FATAL("Multiple -S or -M options not supported");
|
|
|
sync_id = ck_strdup(optarg);
|
|
|
break;
|
|
|
|
|
|
case 'f': /* target file */
|
|
|
|
|
|
if (out_file) FATAL("Multiple -f options not supported");
|
|
|
out_file = optarg;
|
|
|
break;
|
|
|
|
|
|
case 'x': /* dictionary */
|
|
|
|
|
|
if (extras_dir) FATAL("Multiple -x options not supported");
|
|
|
extras_dir = optarg;
|
|
|
break;
|
|
|
|
|
|
case 't': { /* timeout */
|
|
|
|
|
|
u8 suffix = 0;
|
|
|
|
|
|
if (timeout_given) FATAL("Multiple -t options not supported");
|
|
|
|
|
|
if (sscanf(optarg, "%u%c", &exec_tmout, &suffix) < 1 ||
|
|
|
optarg[0] == '-') FATAL("Bad syntax used for -t");
|
|
|
|
|
|
if (exec_tmout < 5) FATAL("Dangerously low value of -t");
|
|
|
|
|
|
if (suffix == '+') timeout_given = 2; else timeout_given = 1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 'm': { /* mem limit */
|
|
|
|
|
|
u8 suffix = 'M';
|
|
|
|
|
|
if (mem_limit_given) FATAL("Multiple -m options not supported");
|
|
|
mem_limit_given = 1;
|
|
|
|
|
|
if (!strcmp(optarg, "none")) {
|
|
|
|
|
|
mem_limit = 0;
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (sscanf(optarg, "%llu%c", &mem_limit, &suffix) < 1 ||
|
|
|
optarg[0] == '-') FATAL("Bad syntax used for -m");
|
|
|
|
|
|
switch (suffix) {
|
|
|
|
|
|
case 'T': mem_limit *= 1024 * 1024; break;
|
|
|
case 'G': mem_limit *= 1024; break;
|
|
|
case 'k': mem_limit /= 1024; break;
|
|
|
case 'M': break;
|
|
|
|
|
|
default: FATAL("Unsupported suffix or bad syntax for -m");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (mem_limit < 5) FATAL("Dangerously low value of -m");
|
|
|
|
|
|
if (sizeof(rlim_t) == 4 && mem_limit > 2000)
|
|
|
FATAL("Value of -m out of range on 32-bit systems");
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'b': { /* bind CPU core */
|
|
|
|
|
|
if (cpu_to_bind_given) FATAL("Multiple -b options not supported");
|
|
|
cpu_to_bind_given = 1;
|
|
|
|
|
|
if (sscanf(optarg, "%u", &cpu_to_bind) < 1 ||
|
|
|
optarg[0] == '-') FATAL("Bad syntax used for -b");
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 'd': /* skip deterministic */
|
|
|
|
|
|
if (skip_deterministic) FATAL("Multiple -d options not supported");
|
|
|
skip_deterministic = 1;
|
|
|
use_splicing = 1;
|
|
|
break;
|
|
|
|
|
|
case 'B': /* load bitmap */
|
|
|
|
|
|
/* This is a secret undocumented option! It is useful if you find
|
|
|
an interesting test case during a normal fuzzing process, and want
|
|
|
to mutate it without rediscovering any of the test cases already
|
|
|
found during an earlier run.
|
|
|
|
|
|
To use this mode, you need to point -B to the fuzz_bitmap produced
|
|
|
by an earlier run for the exact same binary... and that's it.
|
|
|
|
|
|
I only used this once or twice to get variants of a particular
|
|
|
file, so I'm not making this an official setting. */
|
|
|
|
|
|
if (in_bitmap) FATAL("Multiple -B options not supported");
|
|
|
|
|
|
in_bitmap = optarg;
|
|
|
read_bitmap(in_bitmap);
|
|
|
break;
|
|
|
|
|
|
case 'C': /* crash mode */
|
|
|
|
|
|
if (crash_mode) FATAL("Multiple -C options not supported");
|
|
|
crash_mode = FAULT_CRASH;
|
|
|
break;
|
|
|
|
|
|
case 'n': /* dumb mode */
|
|
|
|
|
|
if (dumb_mode) FATAL("Multiple -n options not supported");
|
|
|
if (getenv("AFL_DUMB_FORKSRV")) dumb_mode = 2; else dumb_mode = 1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'T': /* banner */
|
|
|
|
|
|
if (use_banner) FATAL("Multiple -T options not supported");
|
|
|
use_banner = optarg;
|
|
|
break;
|
|
|
|
|
|
case 'Q': /* QEMU mode */
|
|
|
|
|
|
if (qemu_mode) FATAL("Multiple -Q options not supported");
|
|
|
qemu_mode = 1;
|
|
|
|
|
|
if (!mem_limit_given) mem_limit = MEM_LIMIT_QEMU;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'V': /* Show version number */
|
|
|
|
|
|
/* Version number has been printed already, just quit. */
|
|
|
exit(0);
|
|
|
|
|
|
default:
|
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (optind == argc || !in_dir || !out_dir) usage(argv[0]);
|
|
|
|
|
|
setup_signal_handlers();
|
|
|
check_asan_opts();
|
|
|
|
|
|
if (sync_id) fix_up_sync();
|
|
|
|
|
|
if (!strcmp(in_dir, out_dir))
|
|
|
FATAL("Input and output directories can't be the same");
|
|
|
|
|
|
if (dumb_mode) {
|
|
|
|
|
|
if (crash_mode) FATAL("-C and -n are mutually exclusive");
|
|
|
if (qemu_mode) FATAL("-Q and -n are mutually exclusive");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (getenv("AFL_NO_FORKSRV")) no_forkserver = 1;
|
|
|
if (getenv("AFL_NO_CPU_RED")) no_cpu_meter_red = 1;
|
|
|
if (getenv("AFL_NO_ARITH")) no_arith = 1;
|
|
|
if (getenv("AFL_SHUFFLE_QUEUE")) shuffle_queue = 1;
|
|
|
if (getenv("AFL_FAST_CAL")) fast_cal = 1;
|
|
|
|
|
|
if (getenv("AFL_HANG_TMOUT")) {
|
|
|
hang_tmout = atoi(getenv("AFL_HANG_TMOUT"));
|
|
|
if (!hang_tmout) FATAL("Invalid value of AFL_HANG_TMOUT");
|
|
|
}
|
|
|
|
|
|
if (dumb_mode == 2 && no_forkserver)
|
|
|
FATAL("AFL_DUMB_FORKSRV and AFL_NO_FORKSRV are mutually exclusive");
|
|
|
|
|
|
if (getenv("AFL_PRELOAD")) {
|
|
|
setenv("LD_PRELOAD", getenv("AFL_PRELOAD"), 1);
|
|
|
setenv("DYLD_INSERT_LIBRARIES", getenv("AFL_PRELOAD"), 1);
|
|
|
}
|
|
|
|
|
|
if (getenv("AFL_LD_PRELOAD"))
|
|
|
FATAL("Use AFL_PRELOAD instead of AFL_LD_PRELOAD");
|
|
|
|
|
|
save_cmdline(argc, argv);
|
|
|
|
|
|
fix_up_banner(argv[optind]);
|
|
|
|
|
|
check_if_tty();
|
|
|
|
|
|
get_core_count();
|
|
|
|
|
|
#ifdef HAVE_AFFINITY
|
|
|
bind_to_free_cpu();
|
|
|
#endif /* HAVE_AFFINITY */
|
|
|
|
|
|
check_crash_handling();
|
|
|
check_cpu_governor();
|
|
|
|
|
|
setup_post();
|
|
|
setup_shm();
|
|
|
init_count_class16();
|
|
|
|
|
|
setup_dirs_fds();
|
|
|
read_testcases();
|
|
|
load_auto();
|
|
|
|
|
|
pivot_inputs();
|
|
|
|
|
|
if (extras_dir) load_extras(extras_dir);
|
|
|
|
|
|
if (!timeout_given) find_timeout();
|
|
|
|
|
|
detect_file_args(argv + optind + 1);
|
|
|
|
|
|
if (!out_file) setup_stdio_file();
|
|
|
|
|
|
check_binary(argv[optind]);
|
|
|
|
|
|
start_time = get_cur_time();
|
|
|
|
|
|
if (qemu_mode)
|
|
|
use_argv = get_qemu_argv(argv[0], argv + optind, argc - optind);
|
|
|
else
|
|
|
use_argv = argv + optind;
|
|
|
|
|
|
perform_dry_run(use_argv);
|
|
|
|
|
|
cull_queue();
|
|
|
|
|
|
show_init_stats();
|
|
|
|
|
|
seek_to = find_start_position();
|
|
|
|
|
|
write_stats_file(0, 0, 0);
|
|
|
save_auto();
|
|
|
|
|
|
if (stop_soon) goto stop_fuzzing;
|
|
|
|
|
|
/* Woop woop woop */
|
|
|
|
|
|
if (!not_on_tty) {
|
|
|
sleep(4);
|
|
|
start_time += 4000;
|
|
|
if (stop_soon) goto stop_fuzzing;
|
|
|
}
|
|
|
|
|
|
while (1) {
|
|
|
|
|
|
u8 skipped_fuzz;
|
|
|
|
|
|
cull_queue();
|
|
|
|
|
|
if (!queue_cur) {
|
|
|
|
|
|
queue_cycle++;
|
|
|
current_entry = 0;
|
|
|
cur_skipped_paths = 0;
|
|
|
queue_cur = queue;
|
|
|
|
|
|
while (seek_to) {
|
|
|
current_entry++;
|
|
|
seek_to--;
|
|
|
queue_cur = queue_cur->next;
|
|
|
}
|
|
|
|
|
|
show_stats();
|
|
|
|
|
|
if (not_on_tty) {
|
|
|
ACTF("Entering queue cycle %llu.", queue_cycle);
|
|
|
fflush(stdout);
|
|
|
}
|
|
|
|
|
|
/* If we had a full queue cycle with no new finds, try
|
|
|
recombination strategies next. */
|
|
|
|
|
|
if (queued_paths == prev_queued) {
|
|
|
|
|
|
if (use_splicing) cycles_wo_finds++; else use_splicing = 1;
|
|
|
|
|
|
} else cycles_wo_finds = 0;
|
|
|
|
|
|
prev_queued = queued_paths;
|
|
|
|
|
|
if (sync_id && queue_cycle == 1 && getenv("AFL_IMPORT_FIRST"))
|
|
|
sync_fuzzers(use_argv);
|
|
|
|
|
|
}
|
|
|
|
|
|
skipped_fuzz = fuzz_one(use_argv);
|
|
|
|
|
|
if (!stop_soon && sync_id && !skipped_fuzz) {
|
|
|
|
|
|
if (!(sync_interval_cnt++ % SYNC_INTERVAL))
|
|
|
sync_fuzzers(use_argv);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!stop_soon && exit_1) stop_soon = 2;
|
|
|
|
|
|
if (stop_soon) break;
|
|
|
|
|
|
queue_cur = queue_cur->next;
|
|
|
current_entry++;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (queue_cur) show_stats();
|
|
|
|
|
|
/* If we stopped programmatically, we kill the forkserver and the current runner.
|
|
|
If we stopped manually, this is done by the signal handler. */
|
|
|
if (stop_soon == 2) {
|
|
|
if (child_pid > 0) kill(child_pid, SIGKILL);
|
|
|
if (forksrv_pid > 0) kill(forksrv_pid, SIGKILL);
|
|
|
}
|
|
|
/* Now that we've killed the forkserver, we wait for it to be able to get rusage stats. */
|
|
|
if (waitpid(forksrv_pid, NULL, 0) <= 0) {
|
|
|
WARNF("error waitpid\n");
|
|
|
}
|
|
|
|
|
|
write_bitmap();
|
|
|
write_stats_file(0, 0, 0);
|
|
|
save_auto();
|
|
|
|
|
|
stop_fuzzing:
|
|
|
|
|
|
SAYF(CURSOR_SHOW cLRD "\n\n+++ Testing aborted %s +++\n" cRST,
|
|
|
stop_soon == 2 ? "programmatically" : "by user");
|
|
|
|
|
|
/* Running for more than 30 minutes but still doing first cycle? */
|
|
|
|
|
|
if (queue_cycle == 1 && get_cur_time() - start_time > 30 * 60 * 1000) {
|
|
|
|
|
|
SAYF("\n" cYEL "[!] " cRST
|
|
|
"Stopped during the first cycle, results may be incomplete.\n"
|
|
|
" (For info on resuming, see %s/README.)\n", doc_path);
|
|
|
|
|
|
}
|
|
|
|
|
|
fclose(plot_file);
|
|
|
destroy_queue();
|
|
|
destroy_extras();
|
|
|
ck_free(target_path);
|
|
|
ck_free(sync_id);
|
|
|
|
|
|
alloc_report();
|
|
|
|
|
|
OKF("We're done here. Have a nice day!\n");
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif /* !AFL_LIB */
|