| 1 | # About fuzzing |
| 2 | |
| 3 | Fuzzing is done by [OSS-Fuzz](https://google.github.io/oss-fuzz/). |
| 4 | It works by creating a project-specific binary that combines fuzzer |
| 5 | itself and a project provided entry point named |
| 6 | `LLVMFuzzerTestOneInput()`. When invoked, this executable either |
| 7 | searches for new test cases or runs an existing test case. |
| 8 | |
| 9 | File `fuzz/bpf-object-fuzzer.c` defines an entry point for the robot: |
| 10 | - robot supplies bytes supposed to be an ELF file; |
| 11 | - wrapper invokes `bpf_object__open_mem()` to process these bytes. |
| 12 | |
| 13 | File `scripts/build-fuzzers.sh` provides a recipe for fuzzer |
| 14 | infrastructure on how to build the executable described above (see |
| 15 | [here](https://github.com/google/oss-fuzz/tree/master/projects/libbpf)). |
| 16 | |
| 17 | # Reproducing fuzzing errors |
| 18 | |
| 19 | ## Official way |
| 20 | |
| 21 | OSS-Fuzz project describes error reproduction steps in the official |
| 22 | [documentation](https://google.github.io/oss-fuzz/advanced-topics/reproducing/). |
| 23 | Suppose you received an email linking to the fuzzer generated test |
| 24 | case, or got one as an artifact of the `CIFuzz` job (e.g. like |
| 25 | [here](https://github.com/libbpf/libbpf/actions/runs/16375110681)). |
| 26 | Actions to reproduce the error locally are: |
| 27 | |
| 28 | ```sh |
| 29 | git clone --depth=1 https://github.com/google/oss-fuzz.git |
| 30 | cd oss-fuzz |
| 31 | python infra/helper.py pull_images |
| 32 | python infra/helper.py build_image libbpf |
| 33 | python infra/helper.py build_fuzzers --sanitizer address libbpf <path-to-libbpf-checkout> |
| 34 | python infra/helper.py reproduce libbpf bpf-object-fuzzer <path-to-test-case> |
| 35 | ``` |
| 36 | |
| 37 | `<path-to-test-case>` is usually a `crash-<many-hex-digits>` file w/o |
| 38 | extension, CI job wraps it into zip archive and attaches as an artifact. |
| 39 | |
| 40 | To recompile after some fixes, repeat the `build_fuzzers` and |
| 41 | `reproduce` steps after modifying source code in |
| 42 | `<path-to-libbpf-checkout>`. |
| 43 | |
| 44 | Note: the `build_fuzzers` step creates a binary |
| 45 | `build/out/libbpf/bpf-object-fuzzer`, it can be executed directly if |
| 46 | your environment is compatible. |
| 47 | |
| 48 | ## Simple way |
| 49 | |
| 50 | From the project root: |
| 51 | |
| 52 | ```sh |
| 53 | SKIP_LIBELF_REBUILD=1 scripts/build-fuzzers.sh |
| 54 | out/bpf-object-fuzzer <path-to-test-case> |
| 55 | ``` |
| 56 | |
| 57 | `out/bpf-object-fuzzer` is the fuzzer executable described earlier, |
| 58 | can be run with gdb etc. |