samples/subsys/debug/fuzz/README.html · samples/subsys/debug/fuzz/README.rst 공식 원문 ↗

Zephyr 3.7.0 · Samples · Debug · libFuzzer

Fuzzing example

Instrumented coverage로 input을 진화시켜 check6의 zero-page write까지 찾아냅니다.

Official pathsamples/subsys/debug/fuzz/README.html
Source filesamples/subsys/debug/fuzz/README.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

Clang 14 요구 사항, corpus·coverage 변화와 UBSan crash output을 exact block으로 보존합니다.

Engine

LLVM libFuzzer

Target

native_sim 64-bit

Failure

UBSan SEGV

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 527433A97E7918FBB341CE5E364392DB9A4118A7B1C98BE8339D2DF4AE5A9C0E
Fuzzing Example
###############

Overview
********

This is a simple example of fuzz test integration with Zephyr apps
that displays LLVM libfuzzer's most important feature: it's ability to
detect and explore deep and complicated call trees by exploiting
coverage information gleaned from instrumented binaries.

Building and Running
********************

Right now, the only toolchain that works with libfuzzer is a recent 64
bit clang (clang 14 was used at development time).  Make sure such a
toolchain is installed in your host environment, and build with:

.. code-block:: console

   $ clang --version
   clang version 14.0.6
   Target: x86_64-pc-linux-gnu
   Thread model: posix
   InstalledDir: /usr/bin
   $ export ZEPHYR_TOOLCHAIN_VARIANT=llvm
   $ west build -t run -b native_sim/native/64 samples/subsys/debug/fuzz

Over 10-20 seconds or so (runtimes can be quite variable) you will see
it discover and recurse deeper into the test's deliberately
constructed call tree, eventually crashing when it reaches the final
state and reporting the failure.

Example output:

.. code-block:: console

   -- west build: running target run
   [0/1] cd /home/andy/z/zephyr/build && .../andy/z/zephyr/build/zephyr/zephyr.exe
   INFO: Running with entropic power schedule (0xFF, 100).
   INFO: Seed: 108038547
   INFO: Loaded 1 modules   (2112 inline 8-bit counters): 2112 [0x55cbe336ec55, 0x55cbe336f495),
   INFO: Loaded 1 PC tables (2112 PCs): 2112 [0x55cbe336f498,0x55cbe3377898),
   INFO: -max_len is not provided; libFuzzer will not generate inputs larger than 4096 bytes
   *** Booting Zephyr OS build zephyr-v3.1.0-3976-g806034e02865  ***
   Hello World! native_sim/native/64
   INFO: A corpus is not provided, starting from an empty corpus
   #2	INITED cov: 101 ft: 102 corp: 1/1b exec/s: 0 rss: 30Mb
   #
   # Found key 0
   #
   NEW_FUNC[1/6]: 0x55cbe3339c45 in check1 /home/andy/z/zephyr/samples/subsys/debug/fuzz/src/main.c:43
   NEW_FUNC[2/6]: 0x55cbe333c8d8 in char_out /home/andy/z/zephyr/lib/os/printk.c:108
   ...
   ...
   ...
   #418965	REDUCE cov: 165 ft: 166 corp: 15/400b lim: 4052 exec/s: 38087 rss: 31Mb L: 5/256 MS: 1 EraseBytes-
   #524288	pulse  cov: 165 ft: 166 corp: 15/400b lim: 4096 exec/s: 40329 rss: 31Mb
   #
   # Found key 5
   #
   NEW_FUNC[1/1]: 0x55cbe3339ff7 in check6 /home/andy/z/zephyr/samples/subsys/debug/fuzz/src/main.c:48
   #579131	NEW    cov: 168 ft: 169 corp: 16/406b lim: 4096 exec/s: 38608 rss: 31Mb L: 6/256 MS: 1 InsertByte-
   #579432	NEW    cov: 170 ft: 171 corp: 17/414b lim: 4096 exec/s: 38628 rss: 31Mb L: 8/256 MS: 1 PersAutoDict- DE: "\000\000"-
   #579948	REDUCE cov: 170 ft: 171 corp: 17/413b lim: 4096 exec/s: 38663 rss: 31Mb L: 7/256 MS: 1 EraseBytes-
   #
   # Found key 6
   #
   UndefinedBehaviorSanitizer:DEADLYSIGNAL
   ==3243305==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x55cbe333a09d bp 0x7f3114afadf0 sp 0x7f3114afade0 T3243308)
   ==3243305==The signal is caused by a WRITE memory access.
   ==3243305==Hint: address points to the zero page.
       #0 0x55cbe333a09d in check6 /home/andy/z/zephyr/samples/subsys/debug/fuzz/src/main.c:48:1

Part 3

한국어 전문 번역

개요

Zephyr application에 fuzz test를 통합하는 간단한 example입니다. LLVM libFuzzer의 핵심 기능인 coverage-guided exploration을 보여 줍니다.

Instrumented binary에서 얻은 coverage information을 feedback으로 사용해 아직 지나가지 않은 branch와 function을 만드는 input을 계속 변형합니다. 이 방식으로 단순 random input보다 깊고 복잡한 call tree를 효과적으로 발견합니다.

빌드 및 실행

현재 이 sample에서 libFuzzer와 동작하는 toolchain은 최근 64-bit Clang뿐입니다. 개발 당시 Clang 14를 사용했습니다. Host에 해당 toolchain을 설치하고 ZEPHYR_TOOLCHAIN_VARIANT=llvm을 설정한 뒤 64-bit native_sim target을 build·run합니다.

$ clang --version
clang version 14.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ export ZEPHYR_TOOLCHAIN_VARIANT=llvm
$ west build -t run -b native_sim/native/64 samples/subsys/debug/fuzz

약 10~20초 뒤 deliberate하게 만든 test call tree 안으로 점점 깊이 들어가는 모습을 볼 수 있습니다. Runtime은 상당히 달라질 수 있으며, final state에 도달하면 의도한 crash를 일으키고 failure를 보고합니다.

공식 output은 다음과 같습니다.

-- west build: running target run
[0/1] cd /home/andy/z/zephyr/build && .../andy/z/zephyr/build/zephyr/zephyr.exe
INFO: Running with entropic power schedule (0xFF, 100).
INFO: Seed: 108038547
INFO: Loaded 1 modules   (2112 inline 8-bit counters): 2112 [0x55cbe336ec55, 0x55cbe336f495),
INFO: Loaded 1 PC tables (2112 PCs): 2112 [0x55cbe336f498,0x55cbe3377898),
INFO: -max_len is not provided; libFuzzer will not generate inputs larger than 4096 bytes
*** Booting Zephyr OS build zephyr-v3.1.0-3976-g806034e02865  ***
Hello World! native_sim/native/64
INFO: A corpus is not provided, starting from an empty corpus
#2   INITED cov: 101 ft: 102 corp: 1/1b exec/s: 0 rss: 30Mb
#
# Found key 0
#
NEW_FUNC[1/6]: 0x55cbe3339c45 in check1 /home/andy/z/zephyr/samples/subsys/debug/fuzz/src/main.c:43
NEW_FUNC[2/6]: 0x55cbe333c8d8 in char_out /home/andy/z/zephyr/lib/os/printk.c:108
...
...
...
#418965      REDUCE cov: 165 ft: 166 corp: 15/400b lim: 4052 exec/s: 38087 rss: 31Mb L: 5/256 MS: 1 EraseBytes-
#524288      pulse  cov: 165 ft: 166 corp: 15/400b lim: 4096 exec/s: 40329 rss: 31Mb
#
# Found key 5
#
NEW_FUNC[1/1]: 0x55cbe3339ff7 in check6 /home/andy/z/zephyr/samples/subsys/debug/fuzz/src/main.c:48
#579131      NEW    cov: 168 ft: 169 corp: 16/406b lim: 4096 exec/s: 38608 rss: 31Mb L: 6/256 MS: 1 InsertByte-
#579432      NEW    cov: 170 ft: 171 corp: 17/414b lim: 4096 exec/s: 38628 rss: 31Mb L: 8/256 MS: 1 PersAutoDict- DE: "\000\000"-
#579948      REDUCE cov: 170 ft: 171 corp: 17/413b lim: 4096 exec/s: 38663 rss: 31Mb L: 7/256 MS: 1 EraseBytes-
#
# Found key 6
#
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==3243305==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x55cbe333a09d bp 0x7f3114afadf0 sp 0x7f3114afade0 T3243308)
==3243305==The signal is caused by a WRITE memory access.
==3243305==Hint: address points to the zero page.
    #0 0x55cbe333a09d in check6 /home/andy/z/zephyr/samples/subsys/debug/fuzz/src/main.c:48:1

처음에는 corpus 없이 시작해 coverage cov와 feature count ft를 늘립니다. Found key 0 뒤 check1을 발견하고, 수십만 execution 뒤 Found key 5와 check6까지 도달합니다. NEW는 새 coverage를 만든 input, REDUCE는 같은 behavior를 더 작은 corpus input으로 유지한 결과입니다.

마지막 Found key 6 뒤 UndefinedBehaviorSanitizer가 address 0x000000000000의 WRITE access로 인한 SEGV를 보고합니다. Stack location은 samples/subsys/debug/fuzz/src/main.c:48의 check6이며, 이는 coverage-guided input이 의도한 deepest state와 zero-page fault까지 찾아냈음을 뜻합니다.

출력의 Zephyr build string과 seed, address, execution rate, RSS, corpus size는 example run에서 나온 값이므로 환경과 실행마다 달라질 수 있습니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.