xref: /aosp_15_r20/system/nfc/src/fuzzers/README.md (revision 7eba2f3b06c51ae21384f6a4f14577b668a869b3)
1*7eba2f3bSAndroid Build Coastguard Worker# Summary
2*7eba2f3bSAndroid Build Coastguard Worker
3*7eba2f3bSAndroid Build Coastguard WorkerThis directory contains a couple NFC fuzzers targeting different part of the
4*7eba2f3bSAndroid Build Coastguard WorkerNFC code. Due to the complexity of NFC codebase, it's not easy to directly
5*7eba2f3bSAndroid Build Coastguard Workerfuzzing an E2E NFC scenario. Instead, it's much easier to simply target a
6*7eba2f3bSAndroid Build Coastguard Workerspecific function which does the packet parsing, and link only related code
7*7eba2f3bSAndroid Build Coastguard Workerwith some external dependencies stubbed out. That's how these fuzzers are
8*7eba2f3bSAndroid Build Coastguard Workercreated.
9*7eba2f3bSAndroid Build Coastguard Worker
10*7eba2f3bSAndroid Build Coastguard Worker
11*7eba2f3bSAndroid Build Coastguard Worker* nci:
12*7eba2f3bSAndroid Build Coastguard WorkerNCI packet parsing code -- vulnerabilites found in this fuzzer usually require
13*7eba2f3bSAndroid Build Coastguard WorkerNFC firmware compromise.
14*7eba2f3bSAndroid Build Coastguard Worker
15*7eba2f3bSAndroid Build Coastguard Worker* rw:
16*7eba2f3bSAndroid Build Coastguard WorkerNFC tag Read/Write code -- vulnerabilities found here are possibly reachable by
17*7eba2f3bSAndroid Build Coastguard Workeran attacker countrolled NFC tag or tag emulator.
18*7eba2f3bSAndroid Build Coastguard Worker
19*7eba2f3bSAndroid Build Coastguard Worker* ce:
20*7eba2f3bSAndroid Build Coastguard WorkerNFC tag emulation code -- vulnerabilities found here are possibly reachable by
21*7eba2f3bSAndroid Build Coastguard Workeran attacker controlled NFC tag reader.
22*7eba2f3bSAndroid Build Coastguard Worker
23*7eba2f3bSAndroid Build Coastguard Worker
24*7eba2f3bSAndroid Build Coastguard Worker# Running fuzzers
25*7eba2f3bSAndroid Build Coastguard Worker
26*7eba2f3bSAndroid Build Coastguard WorkerHere are the steps to run these fuzzers locally. These steps are verified with
27*7eba2f3bSAndroid Build Coastguard Workeran engineering Android device, and my linux workstation with all the necessary
28*7eba2f3bSAndroid Build Coastguard Workertools installed:
29*7eba2f3bSAndroid Build Coastguard Worker
30*7eba2f3bSAndroid Build Coastguard Worker1. Getting an Android device capable of HWASAN
31*7eba2f3bSAndroid Build Coastguard Worker
32*7eba2f3bSAndroid Build Coastguard Worker    I've been using Pixel 3 (Blueline) and it works great. So we will use
33*7eba2f3bSAndroid Build Coastguard Worker    "blueline" for the rest of the steps. If you are using a different device,
34*7eba2f3bSAndroid Build Coastguard Worker    you need to adjust the command accordingly.
35*7eba2f3bSAndroid Build Coastguard Worker
36*7eba2f3bSAndroid Build Coastguard Worker2. Preparing the device:
37*7eba2f3bSAndroid Build Coastguard Worker
38*7eba2f3bSAndroid Build Coastguard Worker    You need to flash the device with a HWASAN userdebug build. For a blueline
39*7eba2f3bSAndroid Build Coastguard Worker    device, it will be "blueline_hwasan-userdebug". After that, make sure the
40*7eba2f3bSAndroid Build Coastguard Worker    device connects to your workstation and adb root access is enabled.
41*7eba2f3bSAndroid Build Coastguard Worker
42*7eba2f3bSAndroid Build Coastguard Worker3. Preparing build environment:
43*7eba2f3bSAndroid Build Coastguard Worker
44*7eba2f3bSAndroid Build Coastguard Worker    All the fuzzers in this change need to be built with "hwasan userdebug"
45*7eba2f3bSAndroid Build Coastguard Worker    configuration. For the blueline device, that will be the
46*7eba2f3bSAndroid Build Coastguard Worker    "blueline_hwasan-userdebug" option from lunch menu. All the commands from
47*7eba2f3bSAndroid Build Coastguard Worker    the rest steps need to run from this environment.
48*7eba2f3bSAndroid Build Coastguard Worker
49*7eba2f3bSAndroid Build Coastguard Worker4. `fuzz.sh` helper script:
50*7eba2f3bSAndroid Build Coastguard Worker
51*7eba2f3bSAndroid Build Coastguard Worker    There are quite a lot setups required to build and run the fuzzer. To make
52*7eba2f3bSAndroid Build Coastguard Worker    it easier, a `fuzz.sh` helper script is included. It needs to be run with
53*7eba2f3bSAndroid Build Coastguard Worker    current working directory set to the fuzzer directory. You can also source
54*7eba2f3bSAndroid Build Coastguard Worker    it and then use the "fuzz" function defined in it.
55*7eba2f3bSAndroid Build Coastguard Worker
56*7eba2f3bSAndroid Build Coastguard Worker5. Building the fuzzer:
57*7eba2f3bSAndroid Build Coastguard Worker
58*7eba2f3bSAndroid Build Coastguard Worker    Here we use "nci" fuzzer as an example for building and running. The same
59*7eba2f3bSAndroid Build Coastguard Worker    steps apply to the other two fuzzers.
60*7eba2f3bSAndroid Build Coastguard Worker
61*7eba2f3bSAndroid Build Coastguard Worker    Run the following commands to build the fuzzer:
62*7eba2f3bSAndroid Build Coastguard Worker
63*7eba2f3bSAndroid Build Coastguard Worker        cd $ANDROID_BUILD_TOP/system/nfc/src/fuzzers/nci
64*7eba2f3bSAndroid Build Coastguard Worker        ../fuzz.sh build
65*7eba2f3bSAndroid Build Coastguard Worker
66*7eba2f3bSAndroid Build Coastguard Worker    The above commands will build the nci fuzzer and push it to the device.
67*7eba2f3bSAndroid Build Coastguard Worker
68*7eba2f3bSAndroid Build Coastguard Worker6. Running the fuzzer:
69*7eba2f3bSAndroid Build Coastguard Worker
70*7eba2f3bSAndroid Build Coastguard Worker    To run the fuzzer for a single iteration, assuming you are at the fuzzer
71*7eba2f3bSAndroid Build Coastguard Worker    directory ($ANDROID_BUILD_TOP/system/nfc/src/fuzzers/nci), all you need is
72*7eba2f3bSAndroid Build Coastguard Worker    this command:
73*7eba2f3bSAndroid Build Coastguard Worker
74*7eba2f3bSAndroid Build Coastguard Worker          ../fuzz.sh run --once
75*7eba2f3bSAndroid Build Coastguard Worker
76*7eba2f3bSAndroid Build Coastguard Worker    This will run the fuzzer and stop when a crash is found. The crash log will
77*7eba2f3bSAndroid Build Coastguard Worker    be printed on console directly.
78*7eba2f3bSAndroid Build Coastguard Worker
79*7eba2f3bSAndroid Build Coastguard Worker    To run the fuzzer repeatedly, simply use the same command but without
80*7eba2f3bSAndroid Build Coastguard Worker    "--once" option. It will keep the fuzzer running, and do the following
81*7eba2f3bSAndroid Build Coastguard Worker    processfor every time a crash is found:
82*7eba2f3bSAndroid Build Coastguard Worker
83*7eba2f3bSAndroid Build Coastguard Worker    1. Collecting the binary input causing the crash
84*7eba2f3bSAndroid Build Coastguard Worker    2. Re-running the fuzzer with this input to verify the crash reproduces
85*7eba2f3bSAndroid Build Coastguard Worker    3. Parsing the crash log to find out the crashing source location
86*7eba2f3bSAndroid Build Coastguard Worker    4. Archiving all the logs into a directory named by the crashing function
87*7eba2f3bSAndroid Build Coastguard Worker    5. Collecting code goverage data
88*7eba2f3bSAndroid Build Coastguard Worker
89*7eba2f3bSAndroid Build Coastguard Worker    By default "fuzz run" will continue with whatever left last time. However,
90*7eba2f3bSAndroid Build Coastguard Worker    you can make it a fresh restart by passing a "-c" argument.
91*7eba2f3bSAndroid Build Coastguard Worker
92*7eba2f3bSAndroid Build Coastguard Worker7. Other tools provided by `fuzz.sh`:
93*7eba2f3bSAndroid Build Coastguard Worker
94*7eba2f3bSAndroid Build Coastguard Worker    Here are some extra options provided by `fuzz.sh` script:
95*7eba2f3bSAndroid Build Coastguard Worker    * "fuzz gcov" pulls the coverage data from device, and generate a
96*7eba2f3bSAndroid Build Coastguard Worker    visualization of accumulated code coverage.
97*7eba2f3bSAndroid Build Coastguard Worker    * "fuzz gdb" setup gdb instance with the fuzz input so you can debug a
98*7eba2f3bSAndroid Build Coastguard Worker    specific crash.
99*7eba2f3bSAndroid Build Coastguard Worker
100