前言

AFL,全称“American Fuzzy Lop”,是由安全研究员Michal Zalewski开发的一款基于覆盖引导(Coverage-guided)的模糊测试工具,它通过记录输入样本的代码覆盖率(代码执行路径的覆盖情况),以此进行反馈,对输入样本进行调整以提高覆盖率,从而提升发现漏洞的可能性。AFL可以针对有源码和无源码的程序进行模糊测试,其设计思想和实现方案在模糊测试领域具有十分重要的意义。

深入分析AFL源码,对理解AFL的设计理念和其中用到的技巧有着巨大的帮助,对于后期进行定制化Fuzzer开发也具有深刻的指导意义。所以,阅读AFL源码是学习AFL必不可少的一个关键步骤。

(注:需要强调的是,本文的主要目的是协助fuzz爱好者阅读AFL的源码,所以需要在了解AFL基本工作流程和原理的前提下进行阅读,本文并不会在原理侧做过多说明。)

当别人都要快的时候,你要慢下来。

宏观

首先在宏观上看一下AFL的源码结构:

主要的代码在 afl-fuzz.c 文件中,然后是几个独立模块的实现代码,llvm_modeqemu_mode 的代码量大致相当,所以分析的重点应该还是在AFL的根目录下的几个核心功能的实现上,尤其是 afl-fuzz.c,属于核心中的重点。

各个模块的主要功能和作用的简要说明:

  • 插桩模块

    1. afl-as.h, afl-as.c, afl-gcc.c:普通插桩模式,针对源码插桩,编译器可以使用gcc, clang;
    2. llvm_mode:llvm 插桩模式,针对源码插桩,编译器使用clang;
    3. qemu_mode:qemu 插桩模式,针对二进制文件插桩。
  • fuzzer 模块

    afl-fuzz.c:fuzzer 实现的核心代码,AFL 的主体。

  • 其他辅助模块

    1. afl-analyze:对测试用例进行分析,通过分析给定的用例,确定是否可以发现用例中有意义的字段;
    2. afl-plot:生成测试任务的状态图;
    3. afl-tmin:对测试用例进行最小化;
    4. afl-cmin:对语料库进行精简操作;
    5. afl-showmap:对单个测试用例进行执行路径跟踪;
    6. afl-whatsup:各并行例程fuzzing结果统计;
    7. afl-gotcpu:查看当前CPU状态。
  • 部分头文件说明

    1. alloc-inl.h:定义带检测功能的内存分配和释放操作;
    2. config.h:定义配置信息;
    3. debug.h:与提示信息相关的宏定义;
    4. hash.h:哈希函数的实现定义;
    5. types.h:部分类型及宏的定义。

一、AFL的插桩——普通插桩

(一) 、AFL 的 gcc —— afl-gcc.c

1. 概述

afl-gcc 是GCC 或 clang 的一个wrapper(封装),常规的使用方法是在调用 ./configure 时通过 CC 将路径传递给 afl-gccafl-clang。(对于 C++ 代码,则使用 CXX 并将其指向 afl-g++ / afl-clang++。)afl-clang, afl-clang++afl-g++ 均为指向 afl-gcc 的一个符号链接。

afl-gcc 的主要作用是实现对于关键节点的代码插桩,属于汇编级,从而记录程序执行路径之类的关键信息,对程序的运行情况进行反馈。

2. 源码

1. 关键变量

在开始函数代码分析前,首先要明确几个关键变量:

static u8*  as_path;                /* Path to the AFL 'as' wrapper,AFL的as的路径      */
static u8** cc_params;              /* Parameters passed to the real CC,CC实际使用的编译器参数 */
static u32  cc_par_cnt = 1;         /* Param count, including argv0 ,参数计数 */
static u8   be_quiet,               /* Quiet mode,静默模式      */
            clang_mode;             /* Invoked as afl-clang*? ,是否使用afl-clang*模式 */

# 数据类型说明
# typedef uint8_t  u8;
# typedef uint16_t u16;
# typedef uint32_t u32;
2. main函数

main 函数全部逻辑如下:

其中主要有如下三个函数的调用:

  • find_as(argv[0]) :查找使用的汇编器
  • edit_params(argc, argv):处理传入的编译参数,将确定好的参数放入 cc_params[] 数组
  • 调用 execvp(cc_params[0], (cahr**)cc_params) 执行 afl-gcc

这里添加了部分代码打印出传入的参数 arg[0] - arg[7] ,其中一部分是我们指定的参数,另外一部分是自动添加的编译选项。

3. find_as 函数

函数的核心作用:寻找 afl-as

函数内部大概的流程如下(软件自动生成,控制流程图存在误差,但关键逻辑没有问题):

  1. 首先检查环境变量 AFL_PATH ,如果存在直接赋值给 afl_path ,然后检查 afl_path/as 文件是否可以访问,如果可以,as_path = afl_path
  2. 如果不存在环境变量 AFL_PATH ,检查 argv[0] (如“/Users/v4ler1an/AFL/afl-gcc”)中是否存在 "/" ,如果存在则取最后“/” 前面的字符串作为 dir,然后检查 dir/afl-as 是否可以访问,如果可以,将 as_path = dir
  3. 以上两种方式都失败,抛出异常。
4. edit_params 函数

核心作用:将 argv 拷贝到 u8 **cc_params,然后进行相应的处理。

函数内部的大概流程如下:

  1. 调用 ch_alloc()cc_params 分配大小为 (argc + 128) * 8 的内存(u8的类型为1byte无符号整数)

  2. 检查 argv[0] 中是否存在/,如果不存在则 name = argv[0],如果存在则一直找到最后一个/,并将其后面的字符串赋值给 name

  3. 对比 name和固定字符串afl-clang

    1. 若相同,设置clang_mode = 1,设置环境变量CLANG_ENV_VAR为1

      1. 对比name和固定字符串afl-clang++::
        1. 若相同,则获取环境变量AFL_CXX的值,如果存在,则将该值赋值给cc_params[0],否则将afl-clang++赋值给cc_params[0]。这里的cc_params为保存编译参数的数组;
        2. 若不相同,则获取环境变量AFL_CC的值,如果存在,则将该值赋值给cc_params[0],否则将afl-clang赋值给cc_params[0]
    2. 如果不相同,并且是Apple平台,会进入 #ifdef __APPLE__。在Apple平台下,开始对 name 进行对比,并通过 cc_params[0] = getenv("")cc_params[0]进行赋值;如果是非Apple平台,对比 name 和 固定字符串afl-g++(此处忽略对Java环境的处理过程):

      1. 若相同,则获取环境变量AFL_CXX的值,如果存在,则将该值赋值给cc_params[0],否则将g++赋值给cc_params[0]

      2. 若不相同,则获取环境变量AFL_CC的值,如果存在,则将该值赋值给cc_params[0],否则将gcc赋值给cc_params[0]

  4. 进入 while 循环,遍历从argv[1]开始的argv参数:

    • 如果扫描到 -B-B选项用于设置编译器的搜索路径,直接跳过。(因为在这之前已经处理过as_path了);

    • 如果扫描到 -integrated-as,跳过;

    • 如果扫描到 -pipe,跳过;

    • 如果扫描到 -fsanitize=address-fsanitize=memory 告诉 gcc 检查内存访问的错误,比如数组越界之类,设置 asan_set = 1;

    • 如果扫描到 FORTIFY_SOURCE ,设置 fortify_set = 1FORTIFY_SOURCE 主要进行缓冲区溢出问题的检查,检查的常见函数有memcpy, mempcpy, memmove, memset, strcpy, stpcpy, strncpy, strcat, strncat, sprintf, vsprintf, snprintf, gets 等;

    • cc_params 进行赋值:cc_params[cc_par_cnt++] = cur;

  5. 跳出 while 循环,设置其他参数:

    1. 取出前面计算出的 as_path ,设置 -B as_path
  6. 如果为 clang_mode ,则设置-no-integrated-as
    1. 如果存在环境变量 AFL_HARDEN,则设置-fstack-protector-all。且如果没有设置 fortify_set ,追加 -D_FORTIFY_SOURCE=2
  7. sanitizer相关,通过多个if进行判断:

    • 如果 asan_set 在前面被设置为1,则设置环境变量 AFL_USE_ASAN 为1;
      • 如果 asan_set 不为1且,存在 AFL_USE_ASAN 环境变量,则设置-U_FORTIFY_SOURCE -fsanitize=address
    • 如果不存在 AFL_USE_ASAN 环境变量,但存在 AFL_USE_MSAN 环境变量,则设置-fsanitize=memory(不能同时指定AFL_USE_ASAN或者AFL_USE_MSAN,也不能同时指定 AFL_USE_MSANAFL_HARDEN,因为这样运行时速度过慢;

      • 如果不存在 AFL_DONT_OPTIMIZE 环境变量,则设置-g -O3 -funroll-loops -D__AFL_COMPILER=1 -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1
      • 如果存在 AFL_NO_BUILTIN 环境变量,则表示允许进行优化,设置-fno-builtin-strcmp -fno-builtin-strncmp -fno-builtin-strcasecmp -fno-builtin-strncasecmp -fno-builtin-memcmp -fno-builtin-strstr -fno-builtin-strcasestr
  8. 最后补充cc_params[cc_par_cnt] = NULL;cc_params 参数数组编辑完成。

(二)、AFL的插桩 —— afl-as.c

1. 概述

afl-gcc 是 GNU as 的一个wrapper(封装),唯一目的是预处理由 GCC/clang 生成的汇编文件,并注入包含在 afl-as.h 中的插桩代码。 使用 afl-gcc / afl-clang 编译程序时,工具链会自动调用它。该wapper的目标并不是为了实现向 .sasm 代码块中插入手写的代码。

experiment/clang_asm_normalize/ 中可以找到可能允许 clang 用户进行手动插入自定义代码的解决方案,GCC并不能实现该功能。

2. 源码

1. 关键变量

在开始函数代码分析前,首先要明确几个关键变量:

static u8** as_params;          /* Parameters passed to the real 'as',传递给as的参数   */

static u8*  input_file;         /* Originally specified input file ,输入文件     */
static u8*  modified_file;      /* Instrumented file for the real 'as',as进行插桩处理的文件  */

static u8   be_quiet,           /* Quiet mode (no stderr output) ,静默模式,没有标准输出       */
            clang_mode,         /* Running in clang mode?    是否运行在clang模式           */
            pass_thru,          /* Just pass data through?   只通过数据           */
            just_version,       /* Just show version?        只显示版本   */
            sanitizer;          /* Using ASAN / MSAN         是否使用ASAN/MSAN           */

static u32  inst_ratio = 100,   /* Instrumentation probability (%)  插桩覆盖率    */
            as_par_cnt = 1;     /* Number of params to 'as'    传递给as的参数数量初始值         */

注:如果在参数中没有指明 --m32--m64 ,则默认使用在编译时使用的选项。

2. main函数

main 函数全部逻辑如下:

  1. 首先获取环境变量 AFL_INST_RATIO ,赋值给 inst_ratio_str,该环境变量主要控制检测每个分支的概率,取值为0到100%,设置为0时则只检测函数入口的跳转,而不会检测函数分支的跳转;
  2. 通过 gettimeofday(&tv,&tz);获取时区和时间,然后设置 srandom() 的随机种子 rand_seed = tv.tv_sec ^ tv.tv_usec ^ getpid();
  3. 调用 edit_params(argc, argv) 函数进行参数处理;
  4. 检测 inst_ratio_str 的值是否合法范围内,并设置环境变量 AFL_LOOP_ENV_VAR
  5. 读取环境变量`AFL_USE_ASANAFL_USE_MSAN的值,如果其中有一个为1,则设置sanitizer为1,且将inst_ratio除3。这是因为在进行ASAN的编译时,AFL无法识别出ASAN特定的分支,导致插入很多无意义的桩代码,所以直接暴力地将插桩概率/3;
  6. 调用 add_instrumentation() 函数,这是实际的插桩函数;
  7. fork 一个子进程来执行 execvp(as_params[0], (char**)as_params);。这里采用的是 fork 一个子进程的方式来执行插桩。这其实是因为我们的 execvp 执行的时候,会用 as_params[0] 来完全替换掉当前进程空间中的程序,如果不通过子进程来执行实际的 as,那么后续就无法在执行完实际的as之后,还能unlink掉modified_file;
  8. 调用 waitpid(pid, &status, 0) 等待子进程执行结束;
  9. 读取环境变量 AFL_KEEP_ASSEMBLY 的值,如果没有设置这个环境变量,就unlink掉 modified_file(已插完桩的文件)。设置该环境变量主要是为了防止 afl-as 删掉插桩后的汇编文件,设置为1则会保留插桩后的汇编文件。

可以通过在main函数中添加如下代码来打印实际执行的参数:

print("\n");

for (int i = 0; i < sizeof(as_params); i++){
  peinrf("as_params[%d]:%s\n", i, as_params[i]);

}

在插桩完成后,会生成 .s 文件,内容如下(具体的文件位置与设置的环境变量相关):

3. add_instrumentation函数

add_instrumentation 函数负责处理输入文件,生成 modified_file ,将 instrumentation 插入所有适当的位置。其整体控制流程如下:

整体逻辑看上去有点复杂,但是关键内容并不算很多。在main函数中调用完 edit_params() 函数完成 as_params 参数数组的处理后,进入到该函数。

  1. 判断 input_file 是否为空,如果不为空则尝试打开文件获取fd赋值给 inf,失败则抛出异常;input_file 为空则 inf 设置为标准输入;

  2. 打开 modified_file ,获取fd赋值给 outfd,失败返回异常;进一步验证该文件是否可写,不可写返回异常;

  3. while 循环读取 inf 指向文件的每一行到 line 数组,每行最多 MAX_LINE = 8192个字节(含末尾的‘\0’),从line数组里将读取到的内容写入到 outf 指向的文件,然后进入到真正的插桩逻辑。这里需要注意的是,插桩只向 .text 段插入,:

    1. 首先跳过标签、宏、注释;

    2. 这里结合部分关键代码进行解释。需要注意的是,变量 instr_ok 本质上是一个flag,用于表示是否位于.text段。变量设置为1,表示位于 .text 中,如果不为1,则表示不再。于是,如果instr_ok 为1,就会在分支处执行插桩逻辑,否则就不插桩。

      1. 首先判断读入的行是否以‘\t’ 开头,本质上是在匹配.s文件中声明的段,然后判断line[1]是否为.

        if (line[0] == '\t' && line[1] == '.') {
        
              /* OpenBSD puts jump tables directly inline with the code, which is
                 a bit annoying. They use a specific format of p2align directives
                 around them, so we use that as a signal. */
        
              if (!clang_mode && instr_ok && !strncmp(line + 2, "p2align ", 8) &&
                  isdigit(line[10]) && line[11] == '\n') skip_next_label = 1;
        
              if (!strncmp(line + 2, "text\n", 5) ||
                  !strncmp(line + 2, "section\t.text", 13) ||
                  !strncmp(line + 2, "section\t__TEXT,__text", 21) ||
                  !strncmp(line + 2, "section __TEXT,__text", 21)) {
                instr_ok = 1;
                continue; 
              }
        
              if (!strncmp(line + 2, "section\t", 8) ||
                  !strncmp(line + 2, "section ", 8) ||
                  !strncmp(line + 2, "bss\n", 4) ||
                  !strncmp(line + 2, "data\n", 5)) {
                instr_ok = 0;
                continue;
              }
        
            }
        
        1. '\t'开头,且line[1]=='.',检查是否为 p2align 指令,如果是,则设置 skip_next_label = 1
        2. 尝试匹配 "text\n" "section\t.text" "section\t__TEXT,__text" "section __TEXT,__text" 其中任意一个,匹配成功, 设置 instr_ok = 1, 表示位于 .text 段中,continue 跳出,进行下一次遍历;
        3. 尝试匹配"section\t" "section " "bss\n" "data\n" 其中任意一个,匹配成功,设置 instr_ok = 0,表位于其他段中,continue 跳出,进行下一次遍历;
      2. 接下来通过几个 if 判断,来设置一些标志信息,包括 off-flavor assemblyIntel/AT&T的块处理方式、ad-hoc __asm__块的处理方式等;

        /* Detect off-flavor assembly (rare, happens in gdb). When this is
               encountered, we set skip_csect until the opposite directive is
               seen, and we do not instrument. */
        
            if (strstr(line, ".code")) {
        
              if (strstr(line, ".code32")) skip_csect = use_64bit;
              if (strstr(line, ".code64")) skip_csect = !use_64bit;
        
            }
        
            /* Detect syntax changes, as could happen with hand-written assembly.
               Skip Intel blocks, resume instrumentation when back to AT&T. */
        
            if (strstr(line, ".intel_syntax")) skip_intel = 1;
            if (strstr(line, ".att_syntax")) skip_intel = 0;
        
            /* Detect and skip ad-hoc __asm__ blocks, likewise skipping them. */
        
            if (line[0] == '#' || line[1] == '#') {
        
              if (strstr(line, "#APP")) skip_app = 1;
              if (strstr(line, "#NO_APP")) skip_app = 0;
        
            }
        
      3. AFL在插桩时重点关注的内容包括:^main, ^.L0, ^.LBB0_0, ^\tjnz foo (_main函数, gcc和clang下的分支标记,条件跳转分支标记),这些内容通常标志了程序的流程变化,因此AFL会重点在这些位置进行插桩:

        对于形如\tj[^m].格式的指令,即条件跳转指令,且R(100)产生的随机数小于插桩密度inst_ratio,直接使用fprintftrampoline_fmt_64(插桩部分的指令)写入 outf 指向的文件,写入大小为小于 MAP_SIZE的随机数——R(MAP_SIZE)

        ,然后插桩计数ins_lines加一,continue 跳出,进行下一次遍历;

        /* If we're in the right mood for instrumenting, check for function
               names or conditional labels. This is a bit messy, but in essence,
               we want to catch:
        
                 ^main:      - function entry point (always instrumented)
                 ^.L0:       - GCC branch label
                 ^.LBB0_0:   - clang branch label (but only in clang mode)
                 ^\tjnz foo  - conditional branches
        
               ...but not:
        
                 ^# BB#0:    - clang comments
                 ^ # BB#0:   - ditto
                 ^.Ltmp0:    - clang non-branch labels
                 ^.LC0       - GCC non-branch labels
                 ^.LBB0_0:   - ditto (when in GCC mode)
                 ^\tjmp foo  - non-conditional jumps
        
               Additionally, clang and GCC on MacOS X follow a different convention
               with no leading dots on labels, hence the weird maze of #ifdefs
               later on.
        
             */
        
            if (skip_intel || skip_app || skip_csect || !instr_ok ||
                line[0] == '#' || line[0] == ' ') continue;
        
            /* Conditional branch instruction (jnz, etc). We append the instrumentation
               right after the branch (to instrument the not-taken path) and at the
               branch destination label (handled later on). */
        
            if (line[0] == '\t') {
        
              if (line[1] == 'j' && line[2] != 'm' && R(100) < inst_ratio) {
        
                fprintf(outf, use_64bit ? trampoline_fmt_64 : trampoline_fmt_32,
                        R(MAP_SIZE));
        
                ins_lines++;
        
              }
        
              continue;
        
            }
        
      4. 对于label的相关评估,有一些label可能是一些分支的目的地,需要自己评判

        首先检查该行中是否存在:,然后检查是否以.开始

        1. 如果以.开始,则代表想要插桩^.L0:或者 ^.LBB0_0:这样的branch label,即 style jump destination

          1. 检查 line[2]是否为数字 或者 如果是在clang_mode下,比较从line[1]开始的三个字节是否为LBB.,前述所得结果和R(100) < inst_ratio)相与。如果结果为真,则设置instrument_next = 1
        2. 否则代表这是一个function,插桩^func:,function entry point,直接设置instrument_next = 1(defer mode)。
        /* Label of some sort. This may be a branch destination, but we need to
               tread carefully and account for several different formatting
               conventions. */
        
        #ifdef __APPLE__
        
            /* Apple: L<whatever><digit>: */
        
            if ((colon_pos = strstr(line, ":"))) {
        
              if (line[0] == 'L' && isdigit(*(colon_pos - 1))) {
        
        #else
        
            /* Everybody else: .L<whatever>: */
        
            if (strstr(line, ":")) {
        
              if (line[0] == '.') {
        
        #endif /* __APPLE__ */
        
                /* .L0: or LBB0_0: style jump destination */
        
        #ifdef __APPLE__
        
                /* Apple: L<num> / LBB<num> */
        
                if ((isdigit(line[1]) || (clang_mode && !strncmp(line, "LBB", 3)))
                    && R(100) < inst_ratio) {
        
        #else
        
                /* Apple: .L<num> / .LBB<num> */
        
                if ((isdigit(line[2]) || (clang_mode && !strncmp(line + 1, "LBB", 3)))
                    && R(100) < inst_ratio) {
        
        #endif /* __APPLE__ */
        
                  /* An optimization is possible here by adding the code only if the
                     label is mentioned in the code in contexts other than call / jmp.
                     That said, this complicates the code by requiring two-pass
                     processing (messy with stdin), and results in a speed gain
                     typically under 10%, because compilers are generally pretty good
                     about not generating spurious intra-function jumps.
        
                     We use deferred output chiefly to avoid disrupting
                     .Lfunc_begin0-style exception handling calculations (a problem on
                     MacOS X). */
        
                  if (!skip_next_label) instrument_next = 1; else skip_next_label = 0;
        
                }
        
              } else {
        
                /* Function label (always instrumented, deferred mode). */
        
                instrument_next = 1;
        
              }
            }
          }
        
      5. 上述过程完成后,来到 while 循环的下一个循环,在 while 的开头,可以看到对以 defered mode 进行插桩的位置进行了真正的插桩处理:

        if (!pass_thru && !skip_intel && !skip_app && !skip_csect && instr_ok &&
             instrument_next && line[0] == '\t' && isalpha(line[1])) {
        
           fprintf(outf, use_64bit ? trampoline_fmt_64 : trampoline_fmt_32,
                   R(MAP_SIZE));
        
           instrument_next = 0;
           ins_lines++;
        
         }
        

        这里对 instr_ok, instrument_next 变量进行了检验是否为1,而且进一步校验是否位于 .text 段中,且设置了 defered mode 进行插桩,则就进行插桩操作,写入 trampoline_fmt_64/32

至此,插桩函数 add_instrumentation 的主要逻辑已梳理完成。

4. edit_params函数

edit_params,该函数主要是设置变量 as_params 的值,以及 use_64bit/modified_file 的值, 其整体控制流程如下:

  1. 获取环境变量 TMPDIRAFL_AS;

  2. 对于 __APPLE_ 宏, 如果当前在 clang_mode 且没有设置 AFL_AS 环境变量,会设置 use_clang_mode = 1,并设置 afl-asAFL_CC/AFL_CXX/clang中的一种;

  3. 设置 tmp_dir ,尝试获取的环境变量依次为 TEMP, TMP,如果都失败,则直接设置为 /tmp

  4. 调用 ck_alloc() 函数为 as_params 参数数组分配内存,大小为(argc + 32) * 8;

  5. 设置 afl-as 路径:as_params[0] = afl_as ? afl_as : (u8*)"as";

  6. 设置 as_params[argc] = 0; ,as_par_cnt 初始值为1;

  7. 遍历从 argv[1]argv[argc-1] 之前的每个 argv:

    1. 如果存在字符串 --64, 则设置 use_64bit = 1 ;如果存在字符串 --32 ,则设置 use_64bit = 0。对于__APPLE__ ,如果存在-arch x86_64,设置 use_64bit=1,并跳过-q-Q选项;
    2. as_params[as_par_cnt++] = argv[i],设置as_params的值为argv对应的参数值
  8. 开始设置其他参数:

    1. 对于 __APPLE__,如果设置了 use_clang_as,则追加 -c -x assembler

    2. 设置 input_file 变量:input_file = argv[argc - 1];,把最后一个参数的值作为 input_file

      1. 如果 input_file 的首字符为-

        1. 如果后续为 -version,则 just_version = 1, modified_file = input_file,然后跳转到wrap_things_up。这里就只是做version的查询;
        2. 如果后续不为 -version,抛出异常;
      2. 如果 input_file 首字符不为-,比较 input_filetmp_dir/var/tmp/tmp/的前 strlen(tmp_dir)/9/5个字节是否相同,如果不相同,就设置 pass_thru 为1;

    3. 设置 modified_filemodified_file = alloc_printf("%s/.afl-%u-%u.s", tmp_dir, getpid(), (u32)time(NULL));,即为tmp_dir/afl-pid-tim.s 格式的字符串

      1. 设置as_params[as_par_cnt++] = modified_fileas_params[as_par_cnt] = NULL;

3. instrumentation trampoline 和 main_payload

trampoline 的含义是“蹦床”,直译过来就是“插桩蹦床”。个人感觉直接使用英文更能表达出其代表的真实含义和作用,可以简单理解为桩代码。

1. trampoline_fmt_64/32

根据前面内容知道,在64位环境下,AFL会插入 trampoline_fmt_64 到文件中,在32位环境下,AFL会插入trampoline_fmt_32 到文件中。trampoline_fmt_64/32定义在 afl-as.h 头文件中:

static const u8* trampoline_fmt_32 =  "\n"  "/* --- AFL TRAMPOLINE (32-BIT) --- */\n"  "\n"  ".align 4\n"  "\n"  "leal -16(%%esp), %%esp\n"  "movl %%edi,  0(%%esp)\n"  "movl %%edx,  4(%%esp)\n"  "movl %%ecx,  8(%%esp)\n"  "movl %%eax, 12(%%esp)\n"  "movl $0x%08x, %%ecx\n"    // 向ecx中存入识别代码块的随机桩代码id  "call __afl_maybe_log\n"   // 调用 __afl_maybe_log 函数  "movl 12(%%esp), %%eax\n"  "movl  8(%%esp), %%ecx\n"  "movl  4(%%esp), %%edx\n"  "movl  0(%%esp), %%edi\n"  "leal 16(%%esp), %%esp\n"  "\n"  "/* --- END --- */\n"  "\n";static const u8* trampoline_fmt_64 =  "\n"  "/* --- AFL TRAMPOLINE (64-BIT) --- */\n"  "\n"  ".align 4\n"  "\n"  "leaq -(128+24)(%%rsp), %%rsp\n"  "movq %%rdx,  0(%%rsp)\n"  "movq %%rcx,  8(%%rsp)\n"  "movq %%rax, 16(%%rsp)\n"  "movq $0x%08x, %%rcx\n"  // 64位下使用的寄存器为rcx  "call __afl_maybe_log\n" // 调用 __afl_maybe_log 函数  "movq 16(%%rsp), %%rax\n"  "movq  8(%%rsp), %%rcx\n"  "movq  0(%%rsp), %%rdx\n"  "leaq (128+24)(%%rsp), %%rsp\n"  "\n"  "/* --- END --- */\n"  "\n";

上面列出的插桩代码与我们在 .s 文件和IDA逆向中看到的插桩代码是一样的:

.s 文件中的桩代码:

IDA逆向中显示的桩代码:

上述代码执行的主要功能包括:

  • 保存 rdxrcxrax 寄存器
  • rcx 的值设置为 fprintf() 函数将要打印的变量内容
  • 调用 __afl_maybe_log 函数
  • 恢复寄存器

在以上的功能中, __afl_maybe_log 才是核心内容。

__afl_maybe_log 函数开始,后续的处理流程大致如下(图片来自ScUpax0s师傅):

首先对上面流程中涉及到的几个bss段的变量进行简单说明(以64位为例,从main_payload_64中提取):

.AFL_VARS:   .comm   __afl_area_ptr, 8  .comm   __afl_prev_loc, 8  .comm   __afl_fork_pid, 4  .comm   __afl_temp, 4  .comm   __afl_setup_failure, 1  .comm    __afl_global_area_ptr, 8, 8
  • __afl_area_ptr:共享内存地址;
  • __afl_prev_loc:上一个插桩位置(id为R(100)随机数的值);
  • __afl_fork_pid:由fork产生的子进程的pid;
  • __afl_temp:缓冲区;
  • __afl_setup_failure:标志位,如果置位则直接退出;
  • __afl_global_area_ptr:全局指针。

说明

以下介绍的指令段均来自于 main_payload_64

2. __afl_maybe_log
__afl_maybe_log:   /* 源码删除无关内容后 */   lahf  seto  %al   /* Check if SHM region is already mapped. */   movq  __afl_area_ptr(%rip), %rdx  testq %rdx, %rdx  je    __afl_setup

首先,使用 lahf 指令(加载状态标志位到AH)将EFLAGS寄存器的低八位复制到 AH,被复制的标志位包括:符号标志位(SF)、零标志位(ZF)、辅助进位标志位(AF)、奇偶标志位(PF)和进位标志位(CF),使用该指令可以方便地将标志位副本保存在变量中;

然后,使用 seto 指令溢出置位;

接下来检查共享内存是否进行了设置,判断 __afl_area_ptr 是否为NULL:

  • 如果为NULL,跳转到 __afl_setup 函数进行设置;
  • 如果不为NULL,继续进行。
3. __afl_setup
__afl_setup:        /* Do not retry setup is we had previous failues. */        cmpb $0, __afl_setup_failure(%rip)      jne __afl_return                /* Check out if we have a global pointer on file. */        movq __afl_global_area_ptr(%rip), %rdx      testq %rdx, %rdx        je __afl_setup_first                movq %rdx, __afl_area_ptr(%rip)     jmp  __afl_store

该部分的主要作用为初始化 __afl_area_ptr ,且只在运行到第一个桩时进行本次初始化。

首先,如果 __afl_setup_failure 不为0,直接跳转到 __afl_return 返回;

然后,检查 __afl_global_area_ptr 文件指针是否为NULL:

  • 如果为NULL,跳转到 __afl_setup_first 进行接下来的工作;
  • 如果不为NULL,将 __afl_global_area_ptr 的值赋给 __afl_area_ptr,然后跳转到 __afl_store
4. __afl_setup_first
__afl_setup_first:   /* Save everything that is not yet saved and that may be touched by     getenv() and several other libcalls we'll be relying on. */   leaq -352(%rsp), %rsp   movq %rax,   0(%rsp)  movq %rcx,   8(%rsp)  movq %rdi,  16(%rsp)  movq %rsi,  32(%rsp)  movq %r8,   40(%rsp)  movq %r9,   48(%rsp)  movq %r10,  56(%rsp)  movq %r11,  64(%rsp)   movq %xmm0,  96(%rsp)  movq %xmm1,  112(%rsp)  movq %xmm2,  128(%rsp)  movq %xmm3,  144(%rsp)  movq %xmm4,  160(%rsp)  movq %xmm5,  176(%rsp)  movq %xmm6,  192(%rsp)  movq %xmm7,  208(%rsp)  movq %xmm8,  224(%rsp)  movq %xmm9,  240(%rsp)  movq %xmm10, 256(%rsp)  movq %xmm11, 272(%rsp)  movq %xmm12, 288(%rsp)  movq %xmm13, 304(%rsp)  movq %xmm14, 320(%rsp)  movq %xmm15, 336(%rsp)   /* Map SHM, jumping to __afl_setup_abort if something goes wrong. */   /* The 64-bit ABI requires 16-byte stack alignment. We'll keep the     original stack ptr in the callee-saved r12. */   pushq %r12  movq  %rsp, %r12  subq  $16, %rsp  andq  $0xfffffffffffffff0, %rsp   leaq .AFL_SHM_ENV(%rip), %rdicall _getenv   testq %rax, %rax  je    __afl_setup_abort   movq  %rax, %rdicall _atoi   xorq %rdx, %rdx   /* shmat flags    */  xorq %rsi, %rsi   /* requested addr */  movq %rax, %rdi   /* SHM ID         */call _shmat   cmpq $-1, %rax  je   __afl_setup_abort   /* Store the address of the SHM region. */   movq %rax, %rdx  movq %rax, __afl_area_ptr(%rip)   movq %rax, __afl_global_area_ptr(%rip)  movq %rax, %rdx

首先,保存所有寄存器的值,包括 xmm 寄存器组;

然后,进行 rsp 的对齐;

然后,获取环境变量 __AFL_SHM_ID,该环境变量保存的是共享内存的ID:

  • 如果获取失败,跳转到 __afl_setup_abort
  • 如果获取成功,调用 _shmat ,启用对共享内存的访问,启用失败跳转到 __afl_setup_abort

接下来,将 _shmat 返回的共享内存地址存储在 __afl_area_ptr__afl_global_area_ptr 变量中。

后面即开始运行 __afl_forkserver

5. __afl_forkserver
__afl_forkserver:    /* Enter the fork server mode to avoid the overhead of execve() calls. We     push rdx (area ptr) twice to keep stack alignment neat. */   pushq %rdx  pushq %rdx   /* Phone home and tell the parent that we're OK. (Note that signals with     no SA_RESTART will mess it up). If this fails, assume that the fd is     closed because we were execve()d from an instrumented binary, or because     the parent doesn't want to use the fork server. */   movq $4, %rdx               /* length    */  leaq __afl_temp(%rip), %rsi /* data      */  movq $" STRINGIFY((FORKSRV_FD + 1)) ", %rdi       /* file desc */CALL_L64("write")   cmpq $4, %rax  jne  __afl_fork_resume

这一段实现的主要功能是向 FORKSRV_FD+1 (也就是198+1)号描述符(即状态管道)中写 __afl_temp 中的4个字节,告诉 fork server (将在后续的文章中进行详细解释)已经成功启动。

6. __afl_fork_wait_loop
__afl_fork_wait_loop:   /* Wait for parent by reading from the pipe. Abort if read fails. */   movq $4, %rdx               /* length    */  leaq __afl_temp(%rip), %rsi /* data      */  movq $" STRINGIFY(FORKSRV_FD) ", %rdi            /* file desc */CALL_L64("read")  cmpq $4, %rax  jne  __afl_die   /* Once woken up, create a clone of our process. This is an excellent use     case for syscall(__NR_clone, 0, CLONE_PARENT), but glibc boneheadedly     caches getpid() results and offers no way to update the value, breaking     abort(), raise(), and a bunch of other things :-( */ CALL_L64("fork")  cmpq $0, %rax  jl   __afl_die  je   __afl_fork_resume   /* In parent process: write PID to pipe, then wait for child. */   movl %eax, __afl_fork_pid(%rip)   movq $4, %rdx                   /* length    */  leaq __afl_fork_pid(%rip), %rsi /* data      */  movq $" STRINGIFY((FORKSRV_FD + 1)) ", %rdi             /* file desc */CALL_L64("write")   movq $0, %rdx                   /* no flags  */  leaq __afl_temp(%rip), %rsi     /* status    */  movq __afl_fork_pid(%rip), %rdi /* PID       */CALL_L64("waitpid")  cmpq $0, %rax  jle  __afl_die   /* Relay wait status to pipe, then loop back. */   movq $4, %rdx               /* length    */  leaq __afl_temp(%rip), %rsi /* data      */  movq $" STRINGIFY((FORKSRV_FD + 1)) ", %rdi         /* file desc */CALL_L64("write")   jmp  __afl_fork_wait_loop
  1. 等待fuzzer通过控制管道发送过来的命令,读入到 __afl_temp 中:
    • 读取失败,跳转到 __afl_die ,结束循环;
    • 读取成功,继续;
  2. fork 一个子进程,子进程执行 __afl_fork_resume
  3. 将子进程的pid赋给 __afl_fork_pid,并写到状态管道中通知父进程;
  4. 等待子进程执行完成,写入状态管道告知 fuzzer;
  5. 重新执行下一轮 __afl_fork_wait_loop
7. __afl_fork_resume
__afl_fork_resume:/* In child process: close fds, resume execution. */   movq $" STRINGIFY(FORKSRV_FD) ", %rdiCALL_L64("close")   movq $(" STRINGIFY(FORKSRV_FD) " + 1), %rdiCALL_L64("close")   popq %rdx  popq %rdx   movq %r12, %rsp  popq %r12   movq  0(%rsp), %rax  movq  8(%rsp), %rcx  movq 16(%rsp), %rdi  movq 32(%rsp), %rsi  movq 40(%rsp), %r8  movq 48(%rsp), %r9  movq 56(%rsp), %r10  movq 64(%rsp), %r11   movq  96(%rsp), %xmm0  movq 112(%rsp), %xmm1  movq 128(%rsp), %xmm2  movq 144(%rsp), %xmm3  movq 160(%rsp), %xmm4  movq 176(%rsp), %xmm5  movq 192(%rsp), %xmm6  movq 208(%rsp), %xmm7  movq 224(%rsp), %xmm8  movq 240(%rsp), %xmm9  movq 256(%rsp), %xmm10  movq 272(%rsp), %xmm11  movq 288(%rsp), %xmm12  movq 304(%rsp), %xmm13  movq 320(%rsp), %xmm14  movq 336(%rsp), %xmm15   leaq 352(%rsp), %rsp   jmp  __afl_store
  1. 关闭子进程中的fd;
  2. 恢复子进程的寄存器状态;
  3. 跳转到 __afl_store 执行。
8. __afl_store
__afl_store:   /* Calculate and store hit for the code location specified in rcx. */   xorq __afl_prev_loc(%rip), %rcx  xorq %rcx, __afl_prev_loc(%rip)  shrq $1, __afl_prev_loc(%rip)   incb (%rdx, %rcx, 1)

我们直接看反编译的代码:

这里第一步的异或中的 a4 ,其实是调用 __afl_maybe_log 时传入 的参数:

再往上追溯到插桩代码:

可以看到传入 rcx 的,实际上就是用于标记当前桩的随机id, 而 _afl_prev_loc 其实是上一个桩的随机id。

经过两次异或之后,再将 _afl_prev_loc 右移一位作为新的 _afl_prev_loc,最后再共享内存中存储当前插桩位置的地方计数加一。

二、AFL 的插桩 —— llvm_mode

(一)、LLVM 前置知识

LLVM 主要为了解决编译时多种多样的前端和后端导致编译环境复杂、苛刻的问题,其核心为设计了一个称为 LLVM IR 的中间表示,并以库的形式提供一些列接口,以提供诸如操作 IR 、生成目标平台代码等等后端的功能。其整体架构如下所示:

不同的前端和后端使用统一的中间代码LLVM InterMediate Representation(LLVM IR),其结果就是如果需要支持一门新的编程语言,只需要实现一个新的前端;如果需要支持一款新的硬件设备,只需要实现一个新的后端;优化阶段为通用阶段,针对统一的 LLVM IR ,与新的编程语言和硬件设备无关。

GCC 的前后端耦合在一起,没有进行分离,所以GCC为了支持一门新的编程语言或一个新的硬件设备,需要重新开发前端到后端的完整过程。

Clang 是 LLVM 项目的一个子项目,它是 LLVM 架构下的 C/C++/Objective-C 的编译器,是 LLVM 前端的一部分。相较于GCC,具备编译速度快、占用内存少、模块化设计、诊断信息可读性强、设计清晰简单等优点。

最终从源码到机器码的流程如下(以 Clang 做编译器为例):

(LLVM Pass 是一些中间过程处理 IR 的可以用户自定义的内容,可以用来遍历、修改 IR 以达到插桩、优化、静态分析等目的。)

代码首先由编译器前端clang处理后得到中间代码IR,然后经过各 LLVM Pass 进行优化和转换,最终交给编译器后端生成机器码。

(二)、 AFL的afl-clang-fast

1. 概述

AFL的 llvm_mode 可以实现编译器级别的插桩,可以替代 afl-gccafl-clang 使用的比较“粗暴”的汇编级别的重写的方法,且具备如下几个优势:

  1. 编译器可以进行很多优化以提升效率;
  2. 可以实现CPU无关,可以在非 x86 架构上进行fuzz;
  3. 可以更好地处理多线程目标。

在AFL的 llvm_mode 文件夹下包含3个文件: afl-clang-fast.cafl-llvm-pass.so.ccafl-llvm-rt.o.c

afl-llvm-rt.o.c 文件主要是重写了 afl-as.h 文件中的 main_payload 部分,方便调用;

afl-llvm-pass.so.cc 文件主要是当通过 afl-clang-fast 调用 clang 时,这个pass被插入到 LLVM 中,告诉编译器添加与 `afl-as.h 中大致等效的代码;

afl-clang-fast.c 文件本质上是 clang 的 wrapper,最终调用的还是 clang 。但是与 afl-gcc 一样,会进行一些参数处理。

llvm_mode 的插桩思路就是通过编写pass来实现信息记录,对每个基本块都插入探针,具体代码在 afl-llvm-pass.so.cc 文件中,初始化和forkserver操作通过链接完成。

2. 源码

1. afl-clang-fast.c
1. main 函数

main 函数的全部逻辑如下:

主要是对 find_obj(), edit_params(), execvp() 函数的调用,

其中主要有以下三个函数的调用:

  • find_obj(argv[0]):查找运行时library
  • edit_params(argc, argv):处理传入的编译参数,将确定好的参数放入 cc_params[] 数组
  • execvp(cc_params[0], (cahr**)cc_params):替换进程空间,传递参数,执行要调用的clang

这里后两个函数的作用与 afl-gcc.c 中的作用基本相同,只是对参数的处理过程存在不同,不同的主要是 find_obj() 函数。

2. find_obj 函数

find_obj()函数的控制流逻辑如下:

  • 首先,读取环境变量 AFL_PATH 的值:
    • 如果读取成功,确认 AFL_PATH/afl-llvm-rt.o 是否可以访问;如果可以访问,设置该目录为 obj_path ,然后直接返回;
    • 如果读取失败,检查 arg0 中是否存在 / 字符,如果存在,则判断最后一个 / 前面的路径为 AFL 的根目录;然后读取afl-llvm-rt.o文件,成功读取,设置该目录为 obj_path ,然后直接返回。
  • 如果上面两种方式都失败,到/usr/local/lib/afl 目录下查找是否存在 afl-llvm-rt.o ,如果存在,则设置为 obj_path 并直接返回(之所以向该路径下寻找,是因为默认的AFL的MakeFile在编译时,会定义一个名为AFL_PATH的宏,该宏会指向该路径);
  • 如果以上全部失败,抛出异常提示找不到 afl-llvm-rt.o 文件或 afl-llvm-pass.so 文件,并要求设置 AFL_PATH 环境变量 。

函数的主要功能是在寻找AFL的路径以找到 afl-llvm-rt.o 文件,该文件即为要用到的运行时库。

3. edit_params 函数

该函数的主要作用仍然为编辑参数数组,其控制流程如下:

  • 首先,判断执行的是否为 afl-clang-fast++

    • 如果是,设置 cc_params[0] 为环境变量 AFL_CXX;如果环境变量为空,则设置为 clang++
    • 如果不是,设置 cc_params[0] 为环境变量 AFL_CC;如果环境变量为空,则设置为 clang
  • 判断是否定义了 USE_TRACE_PC 宏,如果有,添加 -fsanitize-coverage=trace-pc-guard -mllvm(only Android) -sanitizer-coverage-block-threshold=0(only Android) 选项到参数数组;如果没有,依次将 -Xclang -load -Xclang obj_path/afl-llvm-pass.so -Qunused-arguments 选项添加到参数数组;(这里涉及到llvm_mode使用的2种插桩方式:默认使用的是传统模式,使用 afl-llvm-pass.so 注入来进行插桩,这种方式较为稳定;另外一种是处于实验阶段的方式——trace-pc-guard 模式,对于该模式的详细介绍可以参考llvm相关文档——tracing-pcs-with-guards

  • 遍历传递给 afl-clang-fast 的参数,进行一定的检查和设置,并添加到 cc_params 数组:

    • 如果存在 -m32armv7a-linux-androideabi ,设置 bit_mode 为32;
    • 如果存在 -m64 ,设置 bit_mode 为64;
    • 如果存在 -x ,设置 x_set 为1;
    • 如果存在 -fsanitize=address-fsanitize=memory,设置 asan_set 为1;
    • 如果存在 -Wl,-z,defs-Wl,--no-undefined,则直接pass掉。
  • 检查环境变量是否设置了 AFL_HARDEN

    • 如果有,添加 -fstack-protector-all 选项;
    • 如果有且没有设置 FORTIFY_SOURCE ,添加 -D_FORTIFY_SOURCE=2 选项;
  • 检查参数中是否存在 -fsanitize=memory,即 asan_set 为0:

    • 如果没有,尝试读取环境变量 AFL_USE_ASAN,如果存在,添加 -U_FORTIFY_SOURCE -fsanitize=address
    • 接下来对环境变量AFL_USE_MSAN的处理方式与 AFL_USE_ASAN 类似,添加的选项为 -U_FORTIFY_SOURCE -fsanitize=memory
  • 检查是否定义了 USE_TRACE_PC 宏,如果存在定义,检查是否存在环境变量 AFL_INST_RATIO,如果存在,抛出异常AFL_INST_RATIO 无法在trace-pc时使用;

  • 检查环境变量 AFL_NO_BUILTIN ,如果没有设置,添加 -g -O3 -funroll-loops
  • 检查环境变量 AFL_NO_BUILTIN,如果进行了设置,添加 -fno-builtin-strcmp -fno-builtin-strncmp -fno-builtin-strcasecmp -fno-builtin-strcasecmp -fno-builtin-memcmp
  • 添加参数 -D__AFL_HAVE_MANUAL_CONTROL=1 -D__AFL_COMPILER=1 -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1
  • 定义了两个宏 __AFL_LOOP(), __AFL_INIT()
  • 检查是否设置了 x_set, 如果有添加 -x none
  • 检查是否设置了宏 __ANDORID__ ,如果没有,判断 bit_mode 的值:
    • 如果为0,即没有-m32-m64,添加 obj_path/afl-llvm-rt.o
    • 如果为32,添加 obj_path/afl-llvm-rt-32.o
    • 如果为64,添加 obj_path/afl-llvm-rt-64.o
2. afl-llvm-pass.so.cc

afl-llvm-pass.so.cc 文件实现了 LLVM-mode 下的一个插桩 LLVM Pass。

本文不过多关心如何实现一个LLVM Pass,重点分析该pass的实现逻辑。

该文件只有一个Transform pass:AFLCoverage,继承自 ModulePass,实现了一个 runOnModule 函数,这也是我们需要重点分析的函数。

namespace {  class AFLCoverage : public ModulePass {    public:      static char ID;      AFLCoverage() : ModulePass(ID) { }      bool runOnModule(Module &M) override;      // StringRef getPassName() const override {      //  return "American Fuzzy Lop Instrumentation";      // }  };}
1. pass注册

对pass进行注册的部分源码如下:

static void registerAFLPass(const PassManagerBuilder &,                            legacy::PassManagerBase &PM) {  PM.add(new AFLCoverage());}static RegisterStandardPasses RegisterAFLPass(    PassManagerBuilder::EP_ModuleOptimizerEarly, registerAFLPass);static RegisterStandardPasses RegisterAFLPass0(    PassManagerBuilder::EP_EnabledOnOptLevel0, registerAFLPass);

其核心功能为向PassManager注册新的pass,每个pass相互独立。

对于pass注册的细节部分请读者自行研究llvm的相关内容。

2. runOnModule 函数

该函数为该文件中的关键函数,其控制流程图如下:

  • 首先,通过 getContext() 来获取 LLVMContext ,获取进程上下文:

    LLVMContext &C = M.getContext();IntegerType *Int8Ty  = IntegerType::getInt8Ty(C);IntegerType *Int32Ty = IntegerType::getInt32Ty(C);
    
  • 设置插桩密度:读取环境变量 AFL_INST_RATIO ,并赋值给 inst_ratio,其值默认为100,范围为 1~100,该值表示插桩概率;

  • 获取只想共享内存shm的指针以及上一个基本块的随机ID:

    GlobalVariable *AFLMapPtr =  new GlobalVariable(M, PointerType::get(Int8Ty, 0), false,                     GlobalValue::ExternalLinkage, 0, "__afl_area_ptr");GlobalVariable *AFLPrevLoc = new GlobalVariable(  M, Int32Ty, false, GlobalValue::ExternalLinkage, 0, "__afl_prev_loc",  0, GlobalVariable::GeneralDynamicTLSModel, 0, false);
    
  • 进入插桩过程:

    • 通过 for 循环遍历每个BB(基本块),寻找BB中适合插入桩代码的位置,然后通过初始化 IRBuilder 实例执行插入;

      BasicBlock::iterator IP = BB.getFirstInsertionPt();      IRBuilder<> IRB(&(*IP));
      
    • 随机创建当前BB的ID,然后插入load指令,获取前一个BB的ID;

      if (AFL_R(100) >= inst_ratio) continue; // 如果大于插桩密度,进行随机插桩/* Make up cur_loc */unsigned int cur_loc = AFL_R(MAP_SIZE);ConstantInt *CurLoc = ConstantInt::get(Int32Ty, cur_loc);  // 随机创建当前基本块ID/* Load prev_loc */LoadInst *PrevLoc = IRB.CreateLoad(AFLPrevLoc);PrevLoc->setMetadata(M.getMDKindID("nosanitize"), MDNode::get(C, None));Value *PrevLocCasted = IRB.CreateZExt(PrevLoc, IRB.getInt32Ty()); // 获取上一个基本块的随机ID
      
    • 插入load指令,获取共享内存的地址,并调用 CreateGEP 函数获取共享内存中指定index的地址;

      /* Load SHM pointer */LoadInst *MapPtr = IRB.CreateLoad(AFLMapPtr);MapPtr->setMetadata(M.getMDKindID("nosanitize"), MDNode::get(C, None));Value *MapPtrIdx =  IRB.CreateGEP(MapPtr, IRB.CreateXor(PrevLocCasted, CurLoc));
      
    • 插入load指令,获取对应index地址的值;插入add指令加一,然后创建store指令写入新值,并更新共享内存;

      /* Update bitmap */LoadInst *Counter = IRB.CreateLoad(MapPtrIdx);Counter->setMetadata(M.getMDKindID("nosanitize"), MDNode::get(C, None));Value *Incr = IRB.CreateAdd(Counter, ConstantInt::get(Int8Ty, 1));IRB.CreateStore(Incr, MapPtrIdx)        ->setMetadata(M.getMDKindID("nosanitize"), MDNode::get(C, None));
      
    • 右移 cur_loc ,插入store指令,更新 __afl_prev_loc

      /* Set prev_loc to cur_loc >> 1 */StoreInst *Store =  IRB.CreateStore(ConstantInt::get(Int32Ty, cur_loc >> 1), AFLPrevLoc);Store->setMetadata(M.getMDKindID("nosanitize"), MDNode::get(C, None));
      
    • 最后对插桩计数加1;

    • 扫描下一个BB,根据设置是否为quiet模式等,并判断 inst_blocks 是否为0,如果为0则说明没有进行插桩;

      if (!be_quiet) {    if (!inst_blocks) WARNF("No instrumentation targets found.");    else OKF("Instrumented %u locations (%s mode, ratio %u%%).",             inst_blocks, getenv("AFL_HARDEN") ? "hardened" :             ((getenv("AFL_USE_ASAN") || getenv("AFL_USE_MSAN")) ?              "ASAN/MSAN" : "non-hardened"), inst_ratio);  }
      

整个插桩过程较为清晰,没有冗余动作和代码。

3. afl-llvm-rt.o.c

该文件主要实现了llvm_mode的3个特殊功能:deferred instrumentation, persistent mode,trace-pc-guard mode

1. deferred instrumentation

AFL会尝试通过只执行一次目标二进制文件来提升性能,在 main() 之前暂停程序,然后克隆“主”进程获得一个稳定的可进行持续fuzz的目标。简言之,避免目标二进制文件的多次、重复的完整运行,而是采取了一种类似快照的机制。

虽然这种机制可以减少程序运行在操作系统、链接器和libc级别的消耗,但是在面对大型配置文件的解析时,优势并不明显。

在这种情况下,可以将 forkserver 的初始化放在大部分初始化工作完成之后、二进制文件解析之前来进行,这在某些情况下可以提升10倍以上的性能。我们把这种方式称为LLVM模式下的 deferred instrumentation

首先,在代码中寻找可以进行延迟克隆的合适的、不会破坏原二进制文件的位置,然后添加如下代码:

#ifdef __AFL_HAVE_MANUAL_CONTROL    __AFL_INIT();#endif

以上代码插入,在 afl-clang-fast.c 文件中有说明:

cc_params[cc_par_cnt++] = "-D__AFL_INIT()="    "do { static volatile char *_A __attribute__((used)); "    " _A = (char*)\"" DEFER_SIG "\"; "#ifdef __APPLE__    "__attribute__((visibility(\"default\"))) "    "void _I(void) __asm__(\"___afl_manual_init\"); "#else    "__attribute__((visibility(\"default\"))) "    "void _I(void) __asm__(\"__afl_manual_init\"); "#endif /* ^__APPLE__ */

__afl_manual_init() 函数实现如下:

/* This one can be called from user code when deferred forkserver mode    is enabled. */void __afl_manual_init(void) {  static u8 init_done;  if (!init_done) {    __afl_map_shm();    __afl_start_forkserver();    init_done = 1;  }}

首先,判断是否进行了初始化,没有则调用 __afl_map_shm() 函数进行共享内存初始化。 __afl_map_shm() 函数如下:

/* SHM setup. */static void __afl_map_shm(void) {  u8 *id_str = getenv(SHM_ENV_VAR); // 读取环境变量 SHM_ENV_VAR 获取id  if (id_str) { // 成功读取id    u32 shm_id = atoi(id_str);    __afl_area_ptr = shmat(shm_id, NULL, 0); // 获取shm地址,赋给 __afl_area_ptr    /* Whooooops. */    if (__afl_area_ptr == (void *)-1) _exit(1);  // 异常则退出    /* Write something into the bitmap so that even with low AFL_INST_RATIO,       our parent doesn't give up on us. */    __afl_area_ptr[0] = 1; // 进行设置  }}

然后,调用 __afl_start_forkserver() 函数开始执行forkserver:

/* Fork server logic. */static void __afl_start_forkserver(void) {  static u8 tmp[4];  s32 child_pid;  u8  child_stopped = 0;  /* Phone home and tell the parent that we're OK. If parent isn't there,     assume we're not running in forkserver mode and just execute program. */  if (write(FORKSRV_FD + 1, tmp, 4) != 4) return; // 写入4字节到状态管道,通知 fuzzer已准备完成  while (1) {    u32 was_killed;    int status;    /* Wait for parent by reading from the pipe. Abort if read fails. */    if (read(FORKSRV_FD, &was_killed, 4) != 4) _exit(1);     /* If we stopped the child in persistent mode, but there was a race       condition and afl-fuzz already issued SIGKILL, write off the old       process. */         // 处于persistent mode且子进程已被killed    if (child_stopped && was_killed) {      child_stopped = 0;      if (waitpid(child_pid, &status, 0) < 0) _exit(1);    }    if (!child_stopped) {       /* Once woken up, create a clone of our process. */      child_pid = fork(); // 重新fork      if (child_pid < 0) _exit(1);      /* In child process: close fds, resume execution. */      if (!child_pid) {        close(FORKSRV_FD); // 关闭fd,        close(FORKSRV_FD + 1);        return;        }    } else {      /* Special handling for persistent mode: if the child is alive but         currently stopped, simply restart it with SIGCONT. */                  // 子进程只是暂停,则进行重启      kill(child_pid, SIGCONT);      child_stopped = 0;    }    /* In parent process: write PID to pipe, then wait for child. */    if (write(FORKSRV_FD + 1, &child_pid, 4) != 4) _exit(1);    if (waitpid(child_pid, &status, is_persistent ? WUNTRACED : 0) < 0)      _exit(1);    /* In persistent mode, the child stops itself with SIGSTOP to indicate       a successful run. In this case, we want to wake it up without forking       again. */    if (WIFSTOPPED(status)) child_stopped = 1;    /* Relay wait status to pipe, then loop back. */    if (write(FORKSRV_FD + 1, &status, 4) != 4) _exit(1);  }}

上述逻辑可以概括如下:

  • 首先,设置 child_stopped = 0,写入4字节到状态管道,通知fuzzer已准备完成;

  • 进入 while ,开启fuzz循环:

    • 调用 read 从控制管道读取4字节,判断子进程是否超时。如果管道内读取失败,发生阻塞,读取成功则表示AFL指示forkserver执行fuzz;
    • 如果 child_stopped 为0,则fork出一个子进程执行fuzz,关闭和控制管道和状态管道相关的fd,跳出fuzz循环;

    • 如果 child_stopped 为1,在 persistent mode 下进行的特殊处理,此时子进程还活着,只是被暂停了,可以通过kill(child_pid, SIGCONT)来简单的重启,然后设置child_stopped为0;

    • forkserver向状态管道 FORKSRV_FD + 1 写入子进程的pid,然后等待子进程结束;
    • WIFSTOPPED(status) 宏确定返回值是否对应于一个暂停子进程,因为在 persistent mode 里子进程会通过 SIGSTOP 信号来暂停自己,并以此指示运行成功,我们需要通过 SIGCONT信号来唤醒子进程继续执行,不需要再进行一次fuzz,设置child_stopped为1;
    • 子进程结束后,向状态管道 FORKSRV_FD + 1 写入4个字节,通知AFL本次执行结束。
2. persistent mode

persistent mode 并没有通过fork子进程的方式来执行fuzz。一些库中提供的API是无状态的,或者可以在处理不同输入文件之间进行重置,恢复到之前的状态。执行此类重置时,可以使用一个长期存活的进程来测试多个用例,以这种方式来减少重复的 fork() 调用和操作系统的开销。不得不说,这种思路真的很优秀。

一个基础的框架大概如下:

while (__AFL_LOOP(1000)) {  /* Read input data. */  /* Call library code to be fuzzed. */  /* Reset state. */}/* Exit normally */

设置一个 while 循环,并指定循环次数。在每次循环内,首先读取数据,然后调用想fuzz的库代码,然后重置状态,继续循环。(本质上也是一种快照。)

对于循环次数的设置,循环次数控制了AFL从头重新启动过程之前的最大迭代次数,较小的循环次数可以降低内存泄漏类故障的影响,官方建议的数值为1000。(循环次数设置过高可能出现较多意料之外的问题,并不建议设置过高。)

一个 persistent mode 的样例程序如下:

#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <signal.h>#include <string.h>/* Main entry point. */int main(int argc, char** argv) {  char buf[100]; /* Example-only buffer, you'd replace it with other global or                    local variables appropriate for your use case. */  while (__AFL_LOOP(1000)) {    /*** PLACEHOLDER CODE ***/    /* STEP 1: 初始化所有变量 */    memset(buf, 0, 100);    /* STEP 2: 读取输入数据,从文件读入时需要先关闭旧的fd然后重新打开文件*/    read(0, buf, 100);    /* STEP 3: 调用待fuzz的code*/    if (buf[0] == 'f') {      printf("one\n");      if (buf[1] == 'o') {        printf("two\n");        if (buf[2] == 'o') {          printf("three\n");          if (buf[3] == '!') {            printf("four\n");            abort();          }        }      }    }    /*** END PLACEHOLDER CODE ***/  }  /* 循环结束,正常结束。AFL会重启进程,并清理内存、剩余fd等 */  return 0;}

宏定义 __AFL_LOOP 内部调用 __afl_persistent_loop 函数:

cc_params[cc_par_cnt++] = "-D__AFL_LOOP(_A)="    "({ static volatile char *_B __attribute__((used)); "    " _B = (char*)\"" PERSIST_SIG "\"; "#ifdef __APPLE__    "__attribute__((visibility(\"default\"))) "    "int _L(unsigned int) __asm__(\"___afl_persistent_loop\"); "#else    "__attribute__((visibility(\"default\"))) "    "int _L(unsigned int) __asm__(\"__afl_persistent_loop\"); "#endif /* ^__APPLE__ */    "_L(_A); })";

__afl_persistent_loop(unsigned int max_cnt) 的逻辑如下:

结合源码梳理一下其逻辑:

/* A simplified persistent mode handler, used as explained in README.llvm. */int __afl_persistent_loop(unsigned int max_cnt) {  static u8  first_pass = 1;  static u32 cycle_cnt;  if (first_pass) {    if (is_persistent) {      memset(__afl_area_ptr, 0, MAP_SIZE);      __afl_area_ptr[0] = 1;      __afl_prev_loc = 0;    }    cycle_cnt  = max_cnt;    first_pass = 0;    return 1;  }  if (is_persistent) {    if (--cycle_cnt) {      raise(SIGSTOP);      __afl_area_ptr[0] = 1;      __afl_prev_loc = 0;      return 1;    } else {      __afl_area_ptr = __afl_area_initial;    }  }  return 0;}
  • 首先判读是否为第一次执行循环,如果是第一次:

    • 如果 is_persistent 为1,清空 __afl_area_ptr,设置 __afl_area_ptr[0] 为1,__afl_prev_loc 为0;
    • 设置 cycle_cnt 的值为传入的 max_cnt 参数,然后设置 first_pass=0 表示初次循环结束,返回1;
  • 如果不是第一次执行循环,在 persistent mode 下,且 --cycle_cnt 大于1:

    • 发出信号 SIGSTOP 让当前进程暂停
    • 设置 __afl_area_ptr[0] 为1,__afl_prev_loc 为0,然后直接返回1

    • 如果 cycle_cnt 为0,设置__afl_area_ptr指向数组 __afl_area_initial

  • 最后返回0

重新总结一下上面的逻辑:

  • 第一次执行loop循环,进行初始化,然后返回1,此时满足 while(__AFL_LOOP(1000), 于是执行一次fuzz,计数器cnt减1,抛出SIGSTOP信号暂停子进程;
  • 第二次执行loop循环,恢复之前暂停的子进程继续执行,并设置 child_stopped 为0。此时相当于重新执行了一次程序,重新对 __afl_prev_loc 进行设置,随后返回1,再次进入 while(_AFL_LOOP(1000)) ,执行一次fuzz,计数器cnt减1,抛出SIGSTOP信号暂停子进程;
  • 第1000次执行,计数器cnt此时为0,不再暂停子进程,令 __afl_area_ptr 指向无关数组 __afl_area_initial ,随后子进程结束。
3. trace-pc-guard mode

该功能的使用需要设置宏 AFL_TRACE_PC=1 ,然后再执行 afl-clang-fast 时传入参数 -fsanitize-coverage=trace-pc-guard

该功能的主要特点是会在每个edge插入桩代码,函数 __sanitizer_cov_trace_pc_guard 会在每个edge进行调用,该函数利用函数参数 guard 指针所指向的 uint32 值来确定共享内存上所对应的地址:

void __sanitizer_cov_trace_pc_guard(uint32_t* guard) {  __afl_area_ptr[*guard]++;}

guard 的初始化位于函数 __sanitizer_cov_trace_pc_guard_init 中:

void __sanitizer_cov_trace_pc_guard_init(uint32_t* start, uint32_t* stop) {  u32 inst_ratio = 100;  u8* x;  if (start == stop || *start) return;  x = getenv("AFL_INST_RATIO");  if (x) inst_ratio = atoi(x);  if (!inst_ratio || inst_ratio > 100) {    fprintf(stderr, "[-] ERROR: Invalid AFL_INST_RATIO (must be 1-100).\n");    abort();  }  *(start++) = R(MAP_SIZE - 1) + 1;  while (start < stop) { // 这里如果计算stop-start,就是程序里总计的edge数    if (R(100) < inst_ratio) *start = R(MAP_SIZE - 1) + 1;    else *start = 0;    start++;  }}

参考文献

  1. http://lcamtuf.coredump.cx/afl/
  2. https://eternalsakura13.com/2020/08/23/afl/
  3. https://bbs.pediy.com/thread-265936.htm
  4. https://bbs.pediy.com/thread-249912.htm#msg_header_h3_3
点击收藏 | 1 关注 | 1
  • 动动手指,沙发就是你的了!
登录 后跟帖