使用 libFuzzer 進行模糊測試

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

Fuzzing 只是簡單地提供潛在的無效、意外或隨機數據作為程序的輸入,是在大型軟件系統中發現錯誤的一種極其有效的方法,並且是軟件開發生命週期的重要組成部分。

Android 的構建系統通過包含 LLVM 編譯器基礎設施項目項目中的libFuzzer來支持模糊測試。 LibFuzzer 與被測庫鏈接,並處理在模糊測試會話期間發生的所有輸入選擇、變異和崩潰報告。 LLVM 的消毒劑用於幫助檢測內存損壞和代碼覆蓋率指標。

本文介紹了 Android 上的 libFuzzer 以及如何執行檢測構建。它還包括編寫、運行和自定義模糊器的說明。

設置和構建

為確保您在設備上運行工作映像,您可以下載出廠映像並刷新設備。或者,您可以下載 AOSP 源代碼並按照下面的設置和構建示例進行操作。

設置示例

此示例假設目標設備是 Pixel ( taimen ) 並且已經準備好進行 USB 調試 ( aosp_taimen-userdebug )。您可以從Driver Binaries下載其他 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 master
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 開發版本。

  1. 通過發出以下命令執行初始構建:
    m
  2. 為了讓您可以刷寫您的設備,請使用適當的組合鍵將您的設備引導至快速啟動模式。
  3. 使用以下命令解鎖引導加載程序並刷新新編譯的映像。
    fastboot oem unlock
    fastboot flashall
    

目標設備現在應該可以進行 libFuzzer 模糊測試了。

寫一個模糊器

為了說明在 Android 中使用 libFuzzer 編寫端到端的 fuzzer,請使用以下易受攻擊的代碼作為測試用例。這有助於測試模糊器,確保一切正常,並說明崩潰數據的樣子。

這是測試功能。

#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
}

要構建和運行這個測試模糊器:

  1. 模糊目標由兩個文件組成:構建文件和模糊目標源代碼。在要進行模糊測試的庫旁邊的位置創建文件。為 fuzzer 起一個描述 fuzzer 功能的名稱。
  2. 使用 libFuzzer 編寫一個模糊目標。 fuzz 目標是一個函數,它獲取指定大小的數據塊並將其傳遞給要進行模糊測試的函數。這是易受攻擊的測試功能的基本模糊器:
    #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);
    }
    
  3. 告訴 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",
    }
    
  4. 使模糊器在目標(設備)上運行:
    SANITIZE_TARGET=hwaddress m fuzz_me_fuzzer
    
  5. 使模糊器在主機上運行:
    SANITIZE_HOST=address m fuzz_me_fuzzer
    

為方便起見,定義一些 shell 變量,其中包含 fuzz 目標的路徑和二進製文件的名稱(來自您之前編寫的構建文件)。

export FUZZER_NAME=your_fuzz_target

完成這些步驟後,您應該有一個內置的模糊器。模糊器的默認位置(對於此示例 Pixel 構建)是:

  • $ ANDROID_PRODUCT_OUT /data/fuzz/$ TARGET_ARCH /$ FUZZER_NAME /$ FUZZER_NAME用於設備。
  • $ ANDROID_HOST_OUT /fuzz/$ TARGET_ARCH /$ FUZZER_NAME /$ FUZZER_NAME用於主機。
  • 在主機上運行你的 fuzzer

  • 添加到您的 Android.bp 構建文件:
    host_supported: true,
    請注意,只有當您希望進行模糊測試的庫受主機支持時,才能應用此選項。
  • 通過簡單地運行構建的模糊器二進製文件在主機上運行模糊器:
    $ANDROID_HOST_OUT/fuzz/x86_64/$FUZZER_NAME/$FUZZER_NAME
  • 在設備上運行你的 fuzzer

    我們想使用adb將其複製到您的設備上。

    1. 要將這些文件上傳到設備上的目錄,請運行以下命令:
      adb root
      adb sync data
       
    2. 使用以下命令在設備上運行測試模糊器:
      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,則這是一個直接的越界讀取。

    在你運行你的 fuzzer 之後,輸出通常會導致崩潰,並且有問題的輸入會保存在語料庫中並給出一個 ID。在示例輸出中,這是crash-0eb8e4ed029b774d80f2b66408203801cb982a60

    要在設備上進行模糊測試時檢索崩潰信息,請發出以下命令,指定您的崩潰 ID:

    adb pull /data/fuzz/arm64/fuzz_me_fuzzer/corpus/CRASH_ID
    請注意,要將測試用例保存到正確的目錄,您可以使用語料庫文件夾(如上例所示)或使用 artifact_prefix 參數(例如 `-artifact_prefix=/data/fuzz/where/my/crashes /去`)。

    在主機上進行模糊測試時,崩潰信息出現在運行模糊器的本地文件夾中的崩潰文件夾中。

    有關 libFuzzer 的更多信息,請參閱上游文檔