Fuzzing ซึ่งเป็นเพียงการให้ข้อมูลที่อาจไม่ถูกต้องไม่คาดคิดหรือสุ่มเป็นข้อมูลเข้าในโปรแกรมเป็นวิธีที่มีประสิทธิภาพอย่างมากในการค้นหาจุดบกพร่องในระบบซอฟต์แวร์ขนาดใหญ่และเป็นส่วนสำคัญของวงจรชีวิตการพัฒนาซอฟต์แวร์
ระบบ บิว ด์ของ Android รองรับการ ฟัซเซอร์ ผ่านการรวม libFuzzer จากโครงการโครงสร้างพื้นฐานของคอมไพเลอร์ LLVM LibFuzzer เชื่อมโยงกับไลบรารีภายใต้การทดสอบและจัดการกับการเลือกอินพุตการกลายพันธุ์และการรายงานข้อขัดข้องทั้งหมดที่เกิดขึ้นระหว่างเซสชันที่คลุมเครือ น้ำยาฆ่าเชื้อของ LLVM ใช้เพื่อช่วยในการตรวจจับความเสียหายของหน่วยความจำและตัวชี้วัดความครอบคลุมของรหัส
บทความนี้ให้ข้อมูลเบื้องต้นเกี่ยวกับ libFuzzer บน Android และวิธีการสร้างเครื่องมือ นอกจากนี้ยังมีคำแนะนำในการเขียนเรียกใช้และปรับแต่งฟัซเซอร์
ติดตั้งและสร้าง
เพื่อให้แน่ใจว่าคุณมีภาพที่ใช้งานได้บนอุปกรณ์คุณสามารถดาวน์โหลด อิมเมจจากโรงงาน และแฟลชอุปกรณ์ได้ หรือคุณสามารถดาวน์โหลดซอร์สโค้ด 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 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 เวอร์ชันพัฒนาล่าสุดเป็นอย่างน้อย
- ดำเนินการสร้างเริ่มต้นโดยการออก:
m
- เพื่อให้คุณสามารถแฟลชอุปกรณ์ของคุณได้ให้บูตอุปกรณ์ของคุณเข้าสู่โหมด fastboot โดยใช้ คีย์ผสมที่เหมาะสม
- ปลดล็อก bootloader และแฟลชอิมเมจที่คอมไพล์ใหม่ด้วยคำสั่งต่อไปนี้
fastboot oem unlock
fastboot flashall
ตอนนี้อุปกรณ์เป้าหมายควรพร้อมสำหรับ libFuzzer fuzzing
เขียนฟิวเซอร์
ในการแสดงการเขียนฟัซเซอร์แบบ end-to-end โดยใช้ libFuzzer ใน Android ให้ใช้โค้ดที่มีช่องโหว่ต่อไปนี้เป็นกรณีทดสอบ วิธีนี้ช่วยในการทดสอบ 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 }
ในการสร้างและเรียกใช้ตัวทดสอบนี้:
- เป้าหมาย fuzz ประกอบด้วยสองไฟล์: ไฟล์ build และซอร์สโค้ดเป้าหมาย fuzz สร้างไฟล์ของคุณในตำแหน่งถัดจากไลบรารีที่คุณกำลังเลือน ตั้งชื่อให้กับฟัซเซอร์ที่อธิบายถึงสิ่งที่ฟัซเซอร์ทำ
- เขียนเป้าหมาย fuzz โดยใช้ 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); }
- บอกระบบการสร้างของ 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", }
- วิธีสร้างฟิซเซอร์สำหรับการทำงานบนเป้าหมาย (อุปกรณ์):
SANITIZE_TARGET=hwaddress m fuzz_me_fuzzer
- ในการสร้าง fuzzer สำหรับการทำงานบนโฮสต์:
SANITIZE_HOST=address m fuzz_me_fuzzer
เพื่อความสะดวกให้กำหนดตัวแปรเชลล์ที่มีเส้นทางไปยังเป้าหมาย fuzz ของคุณและชื่อของไบนารี (จากไฟล์ build ที่คุณเขียนไว้ก่อนหน้านี้)
export FUZZER_NAME=your_fuzz_target
หลังจากทำตามขั้นตอนเหล่านี้คุณควรมีฟิวเซอร์ในตัว ตำแหน่งเริ่มต้นสำหรับ fuzzer (สำหรับตัวอย่าง Pixel build) คือ:
เรียกใช้ fuzzer ของคุณบนโฮสต์
host_supported: true,โปรดทราบว่าสิ่งนี้สามารถใช้ได้ก็ต่อเมื่อรองรับไลบรารีที่คุณต้องการใช้ fuzz เท่านั้น
$ANDROID_HOST_OUT/fuzz/x86_64/$FUZZER_NAME/$FUZZER_NAME
เรียกใช้ fuzzer ของคุณบนอุปกรณ์
เราต้องการคัดลอกสิ่งนี้ไปยังอุปกรณ์ของคุณโดยใช้ adb
- ในการอัปโหลดไฟล์เหล่านี้ไปยังไดเร็กทอรีบนอุปกรณ์ให้รันคำสั่งเหล่านี้:
adb root
adb sync data
- รันการทดสอบ 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
ในการดึงข้อมูลข้อขัดข้องเมื่อทำการฟัซซ์บนอุปกรณ์ให้ใช้คำสั่งนี้โดยระบุรหัสข้อขัดข้องของคุณ:
adb pull /data/fuzz/arm64/fuzz_me_fuzzer/corpus/CRASH_IDโปรดทราบว่าในการรับ testcases เพื่อบันทึกลงในไดเร็กทอรีที่ถูกต้องคุณสามารถใช้โฟลเดอร์ corpus (ดังตัวอย่างด้านบน) หรือใช้อาร์กิวเมนต์ artifact_prefix (เช่น `-artifact_prefix = / data / fuzz / where / my / crash / go `).
เมื่อฟัซเซอร์บนโฮสต์ข้อมูลข้อขัดข้องจะปรากฏในโฟลเดอร์ที่ขัดข้องในโฟลเดอร์ภายในเครื่องที่กำลังเรียกใช้ฟิซเซอร์
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ libFuzzer โปรดดู เอกสาร คู่มือ ต้นน้ำ