模糊,單純提供可能無效、未預期的或隨機搜尋結果 視為程式的輸入內容,有效找出 大型軟體系統的開發工作 是軟體開發中相當重要的一環 生命週期
Android 的建構系統已納入 libFuzzer 註冊於 LLVM 編譯器基礎架構專案 LibFuzzer 與測試中的程式庫相連結,並處理所有輸入來源 模糊工作階段期間的選擇、變動和當機回報 LLVM 的掃毒程式可協助偵測記憶體毀損情形,並偵測程式碼 涵蓋率指標
本文將介紹 Android 版 libFuzzer 及如何執行 檢測版本其中也包含編寫、執行及 來自訂模糊內容
設定與建構
如要確保您的裝置具備可運作的映像檔,您可以下載 工廠 映像檔,並刷新裝置。或者,您也可以下載 Android 開放原始碼計畫來源。 並按照下方的設定與建構範例操作。
設定範例
本例假設目標裝置是 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 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 }
如要建構並執行這個測試模糊工具:
- 模糊目標由兩個檔案組成:建構檔案和模糊目標原始碼。 在有問題的程式庫旁邊建立檔案。試試模糊的聲音 這個名稱。
- 使用 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
為了方便起見,請定義一些包含模糊程式碼路徑的殼層變數 目標和二進位檔的名稱 (根據您先前編寫的建構檔案)。
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
在輸出內容範例中,當機原因為 fuzz_me_fuzzer.cpp
第 10 行:
data[3] == 'Z'; // :(
如果 data
長度為 3,這會使讀取作業不超出邊界。
執行模糊測試後,輸出內容通常會導致當機和違規行為
系統會將輸入內容儲存在語料庫中,並給予 ID在輸出內容範例中,
crash-0eb8e4ed029b774d80f2b66408203801cb982a60
。
如要在裝置模糊不清時擷取當機資訊,請發出這個指令: 指定當機 ID:
adb pull /data/fuzz/arm64/fuzz_me_fuzzer/corpus/CRASH_ID請注意,如要將測試案例儲存至正確的目錄,可以使用 corpus 資料夾 (如上例所示),或使用 artifact_prefix 引數 (例如 `-artifact_prefix=/data/fuzz/where/my/crashes/go`)。
主機模糊時,當機資訊會顯示在 執行模糊工具的本機資料夾
產生線涵蓋率
線條涵蓋率對開發人員來說非常實用,因為他們可以在程式碼中找出 缺少程式碼的區域 並據此更新毛茸茸星,以便在日後的模糊結果中出擊。
- 如要產生模糊涵蓋率報表,請執行下列步驟:
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
- 如要產生涵蓋範圍報告,請先從裝置中提取剖析檔案,然後產生 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