模糊测试(将可能无效的数据、异常数据或随机数据作为输入内容提供给程序)是在大型软件系统中查找 bug 的一种非常有效的方式,也是软件开发生命周期的重要组成部分。
Android 编译系统通过从 LLVM 编译器基础架构项目项目纳入 libFuzzer 来支持模糊测试。LibFuzzer 与被测库相关联,并处理在模糊测试会话期间出现的所有输入选择、变更和崩溃报告。LLVM 的排错程序用于协助内存损坏检测以及提供代码覆盖率指标。
本文介绍了 Android 上的 libFuzzer 以及如何执行插桩 build,还介绍了如何编写、运行和自定义模糊测试工具。
设置和构建
为了确保在设备上运行正在运行的映像,您可以下载出厂映像并刷写设备。或者,您也可以下载 AOSP 源代码,然后按照下面的设置和构建示例进行操作。
设置示例
本例假设目标设备是 Pixel (taimen
),并已准备好进行 USB 调试 (aosp_taimen-userdebug
)。您可以从驱动程序二进制文件中下载其他 Pixel 二进制文件。
mkdir ~/bin
export PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
repo init -u https://android.googlesource.com/platform/manifest -b main
repo sync -c -j8
wget https://dl.google.com/dl/android/aosp/google_devices-taimen-qq1a.191205.008-f4537f93.tgz
tar xvf google_devices-taimen-qq1a.191205.008-f4537f93.tgz
./extract-google_devices-taimen.sh
wget https://dl.google.com/dl/android/aosp/qcom-taimen-qq1a.191205.008-760afa6e.tgz
tar xvf qcom-taimen-qq1a.191205.008-760afa6e.tgz
./extract-qcom-taimen.sh
. build/envsetup.sh
lunch aosp_taimen-userdebug
构建示例
运行模糊测试目标的第一步是获取新的系统映像。我们建议至少使用最新的 Android 开发版本。
- 通过执行以下命令来执行初始构建:
m
- 如需刷写设备,请在启动设备后使用相应的键组合进入 fastboot 模式。
- 解锁引导加载程序,并使用下列命令刷入新编译的映像
fastboot oem unlock
fastboot flashall
目标设备现在应该已经准备好进行 libFuzzer 模糊测试。
编写模糊测试工具
为了说明如何在 Android 中使用 libFuzzer 编写端到端的模糊测试工具,请将以下易受攻击的代码作为测试用例。这样做有助于对模糊测试工具进行测试,确保一切运行正常,并说明崩溃数据是什么样的。
以下是测试函数。
#include <stdint.h> #include <stddef.h> bool FuzzMe(const char *data, size_t dataSize) { return dataSize >= 3 && data[0] == 'F' && data[1] == 'U' && data[2] == 'Z' && data[3] == 'Z'; // ← Out of bounds access }
如需构建并运行此模糊测试工具,请执行以下操作:
- 模糊测试目标包含两个文件:一个 build 文件和模糊测试目标源代码。使用要模糊的库旁边的位置创建文件。为模糊测试工具命名,描述模糊测试工具的作用。
- 使用 libFuzzer 编写模糊测试目标。模糊测试目标是一个函数,该函数可接收指定大小的 blob 数据,并将其传递给要接受模糊测试的函数。以下是针对易受攻击的测试函数的基本模糊测试工具:
#include <stddef.h> #include <stdint.h> extern "C" int LLVMFuzzerTestOneInput(const char *data, size_t size) { // ... // Use the data to call the library you are fuzzing. // ... return FuzzMe(data, size); }
- 指示 Android 的编译系统创建模糊测试工具二进制文件。
如需构建模糊测试工具,请将以下代码添加到
Android.bp
文件中:cc_fuzz { name: "fuzz_me_fuzzer", srcs: [ "fuzz_me_fuzzer.cpp", ], // If the fuzzer has a dependent library, uncomment the following section and // include it. // static_libs: [ // "libfoo", // Dependent library // ], // // The advanced features below allow you to package your corpus and // dictionary files during building. You can find more information about // these features at: // - Corpus: https://llvm.org/docs/LibFuzzer.html#corpus // - Dictionaries: https://llvm.org/docs/LibFuzzer.html#dictionaries // These features are not required for fuzzing, but are highly recommended // to gain extra coverage. // To include a corpus folder, uncomment the following line. // corpus: ["corpus/*"], // To include a dictionary, uncomment the following line. // dictionary: "fuzz_me_fuzzer.dict", }
- 要使模糊测试工具在目标(设备)上运行,请执行以下操作:
SANITIZE_TARGET=hwaddress m fuzz_me_fuzzer
- 要使模糊测试程序在主机上运行,请执行以下操作:
SANITIZE_HOST=address m fuzz_me_fuzzer
为方便起见,请定义一些指向模糊测试目标的路径的 shell 变量和二进制文件的名称(来自您之前编写的构建文件)。
export FUZZER_NAME=your_fuzz_target
完成这些步骤之后,您便会得到一个编译好的模糊测试工具。模糊测试工具的默认位置(本例中为 Pixel 版本):
在主机上运行模糊测试工具
host_supported: true,
$ANDROID_HOST_OUT/fuzz/x86_64/$FUZZER_NAME/$FUZZER_NAME
在设备上运行模糊测试工具
我们希望使用adb
将这些内容复制到您的设备上。
- 如需将这些文件上传到设备上的某个目录下,请运行以下命令:
adb root
adb sync data
- 使用以下命令在设备上运行测试模糊测试工具:
adb shell /data/fuzz/$(get_build_var TARGET_ARCH)/$FUZZER_NAME/$FUZZER_NAME \ /data/fuzz/$(get_build_var TARGET_ARCH)/$FUZZER_NAME/corpus
执行此操作后,系统将输出类似于下方示例的内容。
INFO: Seed: 913963180 INFO: Loaded 2 modules (16039 inline 8-bit counters): 16033 [0x7041769b88, 0x704176da29), 6 [0x60e00f4df0, 0x60e00f4df6), INFO: Loaded 2 PC tables (16039 PCs): 16033 [0x704176da30,0x70417ac440), 6 [0x60e00f4df8,0x60e00f4e58), INFO: -max_len is not provided; libFuzzer will not generate inputs larger than 4096 bytes INFO: A corpus is not provided, starting from an empty corpus #2 INITED cov: 5 ft: 5 corp: 1/1b exec/s: 0 rss: 24Mb #10 NEW cov: 6 ft: 6 corp: 2/4b lim: 4 exec/s: 0 rss: 24Mb L: 3/3 MS: 3 CopyPart-ChangeByte-InsertByte- #712 NEW cov: 7 ft: 7 corp: 3/9b lim: 8 exec/s: 0 rss: 24Mb L: 5/5 MS: 2 InsertByte-InsertByte- #744 REDUCE cov: 7 ft: 7 corp: 3/7b lim: 8 exec/s: 0 rss: 25Mb L: 3/3 MS: 2 ShuffleBytes-EraseBytes- #990 REDUCE cov: 8 ft: 8 corp: 4/10b lim: 8 exec/s: 0 rss: 25Mb L: 3/3 MS: 1 ChangeByte- ==18631==ERROR: HWAddressSanitizer: tag-mismatch on address 0x0041e00b4183 at pc 0x0060e00c5144 READ of size 1 at 0x0041e00b4183 tags: f8/03 (ptr/mem) in thread T0 #0 0x60e00c5140 (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0xf140) #1 0x60e00ca130 (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x14130) #2 0x60e00c9b8c (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x13b8c) #3 0x60e00cb188 (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x15188) #4 0x60e00cbdec (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x15dec) #5 0x60e00d8fbc (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x22fbc) #6 0x60e00f0a98 (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x3aa98) #7 0x7041b75d34 (/data/fuzz/arm64/lib/libc.so+0xa9d34) [0x0041e00b4180,0x0041e00b41a0) is a small allocated heap chunk; size: 32 offset: 3 0x0041e00b4183 is located 0 bytes to the right of 3-byte region [0x0041e00b4180,0x0041e00b4183) allocated here: #0 0x70418392bc (/data/fuzz/arm64/lib/libclang_rt.hwasan-aarch64-android.so+0x212bc) #1 0x60e00ca040 (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x14040) #2 0x60e00c9b8c (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x13b8c) #3 0x60e00cb188 (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x15188) #4 0x60e00cbdec (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x15dec) #5 0x60e00d8fbc (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x22fbc) #6 0x60e00f0a98 (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x3aa98) #7 0x7041b75d34 (/data/fuzz/arm64/lib/libc.so+0xa9d34) #8 0x60e00c504c (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0xf04c) #9 0x70431aa9c4 (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0x519c4) Thread: T1 0x006700006000 stack: [0x007040c55000,0x007040d4ecc0) sz: 1023168 tls: [0x000000000000,0x000000000000) Thread: T0 0x006700002000 stack: [0x007fe51f3000,0x007fe59f3000) sz: 8388608 tls: [0x000000000000,0x000000000000) Memory tags around the buggy address (one tag corresponds to 16 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 00 cf 08 dc 08 cd 08 b9 08 1a 1a 0b 00 04 3f => 27 00 08 00 bd bd 2d 07 [03] 73 66 66 27 27 20 f6 <= 5b 5b 87 87 03 00 01 00 4f 04 24 24 03 39 2c 2c 05 00 04 00 be be 85 85 04 00 4a 4a 05 05 5f 5f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Tags for short granules around the buggy address (one tag corresponds to 16 bytes): 04 .. .. cf .. dc .. cd .. b9 .. .. 3f .. 57 .. => .. .. 21 .. .. .. .. 2d [f8] .. .. .. .. .. .. .. <= .. .. .. .. 9c .. e2 .. .. 4f .. .. 99 .. .. .. See https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html#short-granules for a description of short granule tags Registers where the failure occurred (pc 0x0060e00c5144): x0 f8000041e00b4183 x1 000000000000005a x2 0000000000000006 x3 000000704176d9c0 x4 00000060e00f4df6 x5 0000000000000004 x6 0000000000000046 x7 000000000000005a x8 00000060e00f4df0 x9 0000006800000000 x10 0000000000000001 x11 00000060e0126a00 x12 0000000000000001 x13 0000000000000231 x14 0000000000000000 x15 000e81434c909ede x16 0000007041838b14 x17 0000000000000003 x18 0000007042b80000 x19 f8000041e00b4180 x20 0000006800000000 x21 000000000000005a x22 24000056e00b4000 x23 00000060e00f5200 x24 00000060e0128c88 x25 00000060e0128c20 x26 00000060e0128000 x27 00000060e0128000 x28 0000007fe59f16e0 x29 0000007fe59f1400 x30 00000060e00c5144 SUMMARY: HWAddressSanitizer: tag-mismatch (/data/fuzz/arm64/example_fuzzer/example_fuzzer+0xf140) MS: 1 ChangeByte-; base unit: e09f9c158989c56012ccd88111b82f778a816eae 0x46,0x55,0x5a, FUZ artifact_prefix='./'; Test unit written to ./crash-0eb8e4ed029b774d80f2b66408203801cb982a60 Base64: RlVa
在示例输出中,崩溃是由第 10 行 fuzz_me_fuzzer.cpp
导致的:
data[3] == 'Z'; // :(
如果 data
长度为 3,会导致出界读取错误。
运行模糊测试工具后,输出常常会导致崩溃,而导致问题的输入则会保存在语料库中,并提供一个 ID。在示例输出中,这是 crash-0eb8e4ed029b774d80f2b66408203801cb982a60
。
要在在设备上进行模糊测试时检索崩溃信息,发出以下命令,指定您的崩溃 ID:
adb pull /data/fuzz/arm64/fuzz_me_fuzzer/corpus/CRASH_ID
对主机进行模糊测试时,崩溃信息会出现在运行模糊测试的本地文件夹的崩溃文件夹中。
生成代码行覆盖率
代码行覆盖率对于开发者来说非常有用,能让他们精确定位代码中未覆盖的区域,并相应地更新模糊测试工具,以便在以后的模糊测试运行中覆盖这些区域。
- 如需生成模糊测试工具覆盖率报告,请执行以下步骤:
CLANG_COVERAGE=true NATIVE_COVERAGE_PATHS='*' make ${FUZZER_NAME}
- 将模糊测试工具及其依赖项推送到设备后,使用
LLVM_PROFILE_FILE
运行模糊测试目标,如下所示:DEVICE_TRACE_PATH=/data/fuzz/$(get_build_var TARGET_ARCH)/${FUZZER_NAME}/data.profraw
adb shell LLVM_PROFILE_FILE=${DEVICE_TRACE_PATH} /data/fuzz/$(get_build_var TARGET_ARCH)/${FUZZER_NAME}/${FUZZER_NAME} -runs=1000
- 如需生成覆盖率报告,请首先从设备中提取 profraw 文件,然后在名为 coverage-html 的文件夹中生成 html 报告,如下所示:
adb pull ${DEVICE_TRACE_PATH} data.profraw
llvm-profdata merge --sparse data.profraw --output data.profdata
llvm-cov show --format=html --instr-profile=data.profdata \ symbols/data/fuzz/$(get_build_var TARGET_ARCH)/${FUZZER_NAME}/${FUZZER_NAME} \ --output-dir=coverage-html --path-equivalence=/proc/self/cwd/,$ANDROID_BUILD_TOP