คลุมเครือด้วย libFuzzer

Fuzzing ซึ่งเป็นเพียงการให้ข้อมูลที่อาจไม่ถูกต้อง คาดไม่ถึง หรือสุ่มเป็นอินพุตให้กับโปรแกรม เป็นวิธีที่มีประสิทธิภาพอย่างยิ่งในการค้นหาจุดบกพร่องในระบบซอฟต์แวร์ขนาดใหญ่ และเป็นส่วนสำคัญของวงจรชีวิตการพัฒนาซอฟต์แวร์

ระบบการสร้างของ Android รองรับการฟัซซิ่งผ่านการรวม libFuzzer จากโครงการโครงสร้างพื้นฐานคอมไพเลอร์ LLVM LibFuzzer เชื่อมโยงกับไลบรารีภายใต้การทดสอบและจัดการการเลือกอินพุต การกลายพันธุ์ และการรายงานข้อขัดข้องทั้งหมดที่เกิดขึ้นระหว่างเซสชันฟัซซิง น้ำยาฆ่าเชื้อของ LLVM ใช้เพื่อช่วยในการตรวจจับความเสียหายของหน่วยความจำและตัวชี้วัดความครอบคลุมของรหัส

บทความนี้ให้ข้อมูลเบื้องต้นเกี่ยวกับ libFuzzer บน Android และวิธีการสร้างเครื่องมือ นอกจากนี้ยังมีคำแนะนำในการเขียน รัน และปรับแต่งฟัซเซอร์

ตั้งค่าและสร้าง

เพื่อให้แน่ใจว่าคุณมีอิมเมจที่ใช้งานอยู่ในอุปกรณ์ คุณสามารถดาวน์โหลด อิมเมจจากโรงงาน และแฟลชอุปกรณ์ได้ หรือคุณสามารถดาวน์โหลดซอร์สโค้ด AOSP และทำตามตัวอย่างการตั้งค่าและสร้างด้านล่าง

ตัวอย่างการตั้งค่า

ตัวอย่างนี้ถือว่าอุปกรณ์เป้าหมายคือ Pixel ( taimen ) และเตรียมพร้อมสำหรับการดีบัก USB ( aosp_taimen-userdebug ) แล้ว คุณสามารถดาวน์โหลด Pixel ไบนารีอื่นๆ ได้จาก Driver Binaries

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

ตัวอย่างการสร้าง

ขั้นตอนแรกของการเรียกใช้ fuzz targets คือการได้รับอิมเมจระบบใหม่ เราขอแนะนำให้ใช้ Android เวอร์ชันพัฒนาล่าสุดเป็นอย่างน้อย

  1. ดำเนินการสร้างเริ่มต้นโดยออก:
    m
  2. เพื่อให้คุณสามารถแฟลชอุปกรณ์ได้ ให้บู๊ตอุปกรณ์เข้าสู่โหมด fastboot โดยใช้ คีย์ผสมที่เหมาะสม
  3. ปลดล็อก bootloader และแฟลชอิมเมจที่คอมไพล์ใหม่ด้วยคำสั่งต่อไปนี้
    fastboot oem unlock
    fastboot flashall
    

อุปกรณ์เป้าหมายควรจะพร้อมสำหรับ libFuzzer fuzzing

เขียนฟัซเซอร์

เพื่อแสดงให้เห็นการเขียน fuzzer แบบ end-to-end โดยใช้ libFuzzer ใน Android ให้ใช้รหัสที่มีช่องโหว่ต่อไปนี้เป็นกรณีทดสอบ สิ่งนี้ช่วยในการทดสอบฟัซเซอร์ ตรวจสอบให้แน่ใจว่าทุกอย่างทำงานอย่างถูกต้อง และแสดงให้เห็นว่าข้อมูลการหยุดทำงานมีลักษณะอย่างไร

นี่คือฟังก์ชั่นการทดสอบ

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

ในการสร้างและรันการทดสอบ fuzzer:

  1. fuzz target ประกอบด้วยไฟล์สองไฟล์: ไฟล์ build และซอร์สโค้ดเป้าหมาย fuzz สร้างไฟล์ของคุณในตำแหน่งถัดจากไลบรารี่ที่คุณกำลังฟัซซิ่ง ตั้งชื่อ fuzzer ที่อธิบายการทำงานของ fuzzer
  2. เขียน Fuzz Target โดยใช้ libFuzzer Fuzz Target คือฟังก์ชันที่รับข้อมูลที่มีขนาดตามที่กำหนดและส่งไปยังฟังก์ชันที่จะ Fuzzed นี่คือ Fuzzer พื้นฐานสำหรับฟังก์ชันการทดสอบที่มีช่องโหว่:
    #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 ให้สร้าง fuzzer binary หากต้องการสร้าง fuzzer ให้เพิ่มโค้ดนี้ในไฟล์ 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. ในการสร้าง fuzzer สำหรับการทำงานบนเป้าหมาย (อุปกรณ์):
    SANITIZE_TARGET=hwaddress m fuzz_me_fuzzer
    
  5. ในการสร้างฟัซเซอร์สำหรับการรันบนโฮสต์:
    SANITIZE_HOST=address m fuzz_me_fuzzer
    

เพื่อความสะดวก ให้กำหนดตัวแปรเชลล์ที่มีพาธไปยัง fuzz เป้าหมายและชื่อของไบนารี (จากไฟล์ build ที่คุณเขียนไว้ก่อนหน้านี้)

export FUZZER_NAME=your_fuzz_target

หลังจากทำตามขั้นตอนเหล่านี้แล้ว คุณควรมี fuzzer ในตัว ตำแหน่งเริ่มต้นสำหรับ fuzzer (สำหรับ Pixel build ตัวอย่างนี้) คือ:

  • $ ANDROID_PRODUCT_OUT /data/fuzz/$ TARGET_ARCH /$ FUZZER_NAME /$ FUZZER_NAME สำหรับอุปกรณ์
  • $ ANDROID_HOST_OUT /fuzz/$ TARGET_ARCH /$ FUZZER_NAME /$ FUZZER_NAME สำหรับโฮสต์
  • เรียกใช้ฟัซเซอร์ของคุณบนโฮสต์

  • เพิ่มไปยังไฟล์บิลด์ Android.bp ของคุณ:
    host_supported: true,
    โปรดทราบว่าสิ่งนี้สามารถใช้ได้เฉพาะเมื่อไลบรารีที่คุณต้องการฟัซได้รับการสนับสนุนโฮสต์
  • เรียกใช้ fuzzer บนโฮสต์เพียงแค่เรียกใช้ fuzzer binary ที่สร้างขึ้น:
    $ANDROID_HOST_OUT/fuzz/x86_64/$FUZZER_NAME/$FUZZER_NAME
  • เรียกใช้ฟัซเซอร์ของคุณบนอุปกรณ์

    เราต้องการคัดลอกไปยังอุปกรณ์ของคุณโดยใช้ adb

    1. หากต้องการอัปโหลดไฟล์เหล่านี้ไปยังไดเร็กทอรีบนอุปกรณ์ ให้รันคำสั่งเหล่านี้:
      adb root
      adb sync data
      
    2. เรียกใช้การทดสอบ fuzzer บนอุปกรณ์ด้วยคำสั่งนี้:
      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

    หลังจากที่คุณเรียกใช้ fuzzer เอาต์พุตมักจะส่งผลให้เกิดการหยุดทำงาน และอินพุตที่ละเมิดจะถูกบันทึกไว้ในคลังข้อมูลและกำหนด ID ในผลลัพธ์ตัวอย่าง นี่คือ crash-0eb8e4ed029b774d80f2b66408203801cb982a60

    หากต้องการดึงข้อมูลข้อขัดข้องเมื่อฟัซซิ่งบนอุปกรณ์ ให้ออกคำสั่งนี้โดยระบุ ID ข้อขัดข้องของคุณ:

    adb pull /data/fuzz/arm64/fuzz_me_fuzzer/corpus/CRASH_ID
    โปรดทราบว่าหากต้องการบันทึก testcase ลงในไดเรกทอรีที่ถูกต้อง คุณสามารถใช้โฟลเดอร์คลังข้อมูล (ตามตัวอย่างด้านบน) หรือใช้อาร์กิวเมนต์ artifact_prefix (เช่น `-artifact_prefix=/data/fuzz/where/my/crashes /ไป`).

    เมื่อ fuzzing บนโฮสต์ ข้อมูลข้อขัดข้องจะปรากฏในโฟลเดอร์ข้อขัดข้องในโฟลเดอร์ในเครื่องที่ fuzzer กำลังทำงาน

    สร้างความครอบคลุมของสาย

    การครอบคลุมของบรรทัดมีประโยชน์มากสำหรับนักพัฒนาเนื่องจากพวกเขาสามารถระบุพื้นที่ในโค้ดที่ไม่ครอบคลุมและอัปเดตฟัซเซอร์ตามนั้นเพื่อเข้าถึงพื้นที่เหล่านั้นในการรันฟัซซิ่งในอนาคต

    1. ในการสร้างรายงานความครอบคลุมของฟัซเซอร์ ให้ทำตามขั้นตอนต่อไปนี้:
      CLANG_COVERAGE=true NATIVE_COVERAGE_PATHS='*' make ${FUZZER_NAME}
      
    2. หลังจากส่ง fuzzer และการอ้างอิงไปยังอุปกรณ์แล้ว ให้รันเป้าหมาย fuzz ด้วย 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
      
    3. สร้างรายงานความครอบคลุมโดยดึงไฟล์ profraw ออกจากอุปกรณ์ก่อน จากนั้นสร้างรายงาน html ไปยังโฟลเดอร์ชื่อ coverage-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
      

    สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ libFuzzer โปรดดู เอกสารประกอบต้นทาง