ฟัซซิ่งด้วย libFuzzer

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

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

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

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

เพื่อให้แน่ใจว่าคุณมีอิมเมจที่ใช้งานได้บนอุปกรณ์ คุณสามารถดาวน์โหลด อิมเมจจากโรงงาน และแฟลชอุปกรณ์ได้ หรือคุณสามารถดาวน์โหลดซอร์สโค้ด 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 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. เพื่อให้คุณสามารถแฟลชอุปกรณ์ได้ ให้บูตอุปกรณ์เข้าสู่โหมด fastboot โดยใช้ คีย์ผสมที่เหมาะสม
  3. ปลดล็อกโปรแกรมโหลดบูตและแฟลชอิมเมจที่คอมไพล์ใหม่ด้วยคำสั่งต่อไปนี้
    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. เป้าหมายแบบฟัซซี่ประกอบด้วยสองไฟล์: ไฟล์บิลด์และซอร์สโค้ดเป้าหมายแบบฟัซ สร้างไฟล์ของคุณในตำแหน่งถัดจากไลบรารีที่คุณกำลังสับสน ตั้งชื่อให้เครื่องพ่นสีที่อธิบายสิ่งที่เครื่องพ่นสีทำ
  2. เขียนเป้าหมายที่คลุมเครือโดยใช้ libFuzzer เป้าหมายที่คลุมเครือเป็นฟังก์ชันที่นำหยดของข้อมูลที่มีขนาดที่ระบุ และส่งผ่านไปยังฟังก์ชันเพื่อให้ฟัซซ์ นี่คือฟิวเซอร์พื้นฐานสำหรับฟังก์ชันการทดสอบที่มีช่องโหว่:
    #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 ให้เพิ่มรหัสนี้ในไฟล์ 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. เพื่อให้ fuzzer ทำงานบนโฮสต์:
    SANITIZE_HOST=address m fuzz_me_fuzzer
    

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

export FUZZER_NAME=your_fuzz_target

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

  • $ 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,
    โปรดทราบว่าสามารถใช้ได้เฉพาะเมื่อไลบรารีที่คุณต้องการ fuzz ได้รับการสนับสนุน
  • เรียกใช้ fuzzer บนโฮสต์โดยเพียงแค่เรียกใช้ fuzzer binary ที่สร้างขึ้น:
    $ANDROID_HOST_OUT/fuzz/x86_64/$FUZZER_NAME/$FUZZER_NAME
  • เรียกใช้ fuzzer ของคุณบนอุปกรณ์

    เราต้องการคัดลอกสิ่งนี้ไปยังอุปกรณ์ของคุณโดยใช้ 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';  // :(
    

    นี่เป็นการอ่านนอกขอบเขตอย่างตรงไปตรงมาหากข้อมูลมีความยาว 3

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

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

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

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

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