|
| 1 | +# Fuzzing |
| 2 | + |
| 3 | +One of the use cases of concolic execution, which is demonstrated to be effective, |
| 4 | +is hybrid fuzzing, in which fuzzing is aided with solver-found inputs generated by symbolic |
| 5 | +execution to take certain paths inside the program that other techniques are inefficient |
| 6 | +to find. |
| 7 | + |
| 8 | +Leaf is aimed to be suitable for this purpose and comes with a built-in support for [LibAFL][1], |
| 9 | +a customizable fuzzing framework with modern architecture written in Rust. |
| 10 | +Crate `libafl_leaf` contains facilities for using Leaf-instrumented programs with |
| 11 | +the fuzzers written using this library. |
| 12 | + |
| 13 | +## Hybrid Fuzzing for libFuzzer |
| 14 | + |
| 15 | +In an abstract manner, hybrid fuzzing for LibAFL-based fuzzers is achievable using |
| 16 | +a stage that generates diverging inputs from the current test case. |
| 17 | +This stage should perform the concolic execution using the current test case to derive the diverging inputs |
| 18 | +and offer them to the fuzzer for evaluation. |
| 19 | +Thus, the following steps are presumable for an execution-based concolic executor like Leaf. |
| 20 | +1. Build an executable equivalent to the fuzz target, which is suitable for concolic execution. |
| 21 | +1. Define a mutator stage that runs the built executable and obtains new inputs. |
| 22 | +1. Add the stage to the fuzzer. |
| 23 | + |
| 24 | +The mentioned ingredients are provided by Leaf; |
| 25 | +`leafc` instruments your target program, |
| 26 | +`leafo_onetime` helps with collecting the diverging inputs, |
| 27 | +and `libafl_leaf` provides the stage. |
| 28 | +As `libFuzzer` (through `cargo-fuzz`) is one the most-used tools to perform fuzzing |
| 29 | +for Rust projects, a rudimentary support is also provided for harnesses |
| 30 | +written based on [libfuzzer-sys](https://github.com/rust-fuzz/libfuzzer) |
| 31 | +to upgrade them to a hybrid fuzzer. |
| 32 | +It is developed as an extension of [LibAFL][1]'s implementation of `libFuzzer`, so |
| 33 | +the [same instructions and options](https://github.com/AFLplusplus/LibAFL/tree/main/libafl_libfuzzer) |
| 34 | +apply. |
| 35 | + |
| 36 | +### Recipe |
| 37 | + |
| 38 | +With an understanding of the general procedure above, you can follow the instruction |
| 39 | +below to upgrade your existing fuzzer to a hybrid one. |
| 40 | + |
| 41 | +* Prerequisites |
| 42 | + 1. The one-time orchestrator (`leafo_onetime`) is installed in your environment. |
| 43 | + If not, install it similarly to `leafc` using the following command in Leaf's root folder. |
| 44 | + ```console |
| 45 | + leaf$ cargo install --path ./orchestrator |
| 46 | + ``` |
| 47 | + |
| 48 | + 1. You have a fuzzer written using `libfuzzer-sys`. |
| 49 | + We assume it is named `fuzz_target_1` and has the following template. |
| 50 | + ```rust |
| 51 | + #![no_main] |
| 52 | + |
| 53 | + use libfuzzer_sys::fuzz_target; |
| 54 | + |
| 55 | + fuzz_target!(|data: &[u8]| { |
| 56 | + // fuzzed code goes here |
| 57 | + }); |
| 58 | + ``` |
| 59 | + |
| 60 | +1. Replace `libfuzzer-sys` source to Leaf's implementation in `Cargo.toml` of your fuzz project. |
| 61 | + ```toml |
| 62 | + # From |
| 63 | + libfuzzer-sys = { version = "...", features = ["your", "features", "here"] } |
| 64 | + |
| 65 | + # To |
| 66 | + libfuzzer-sys = { git = "https://github.com/sfu-rsl/leaf.git", package = "libafl_libfuzzer", features = ["your", "features", "here"]} |
| 67 | + ``` |
| 68 | + |
| 69 | +1. Change `fuzz_target` macro invocation to `hybrid_fuzz_target`, and make |
| 70 | + `no_main` attribute conditional based on compilation with `leafc`. |
| 71 | + ```rust |
| 72 | + #![cfg_attr(not(leafc), no_main)] |
| 73 | + |
| 74 | + use libfuzzer_sys::hybrid_fuzz_target; |
| 75 | + |
| 76 | + hybrid_fuzz_target!(|data: &[u8]| { |
| 77 | + // fuzzed code goes here |
| 78 | + }); |
| 79 | + ``` |
| 80 | + (`hybrid_fuzz_target` additionally writes a program with a `main` function that reads |
| 81 | + the whole standard input, marks it as symbolic, and passes to the closure.) |
| 82 | + |
| 83 | +1. Build your fuzz target with `leafc` in a *separate* cargo target directory like below. |
| 84 | + ```console |
| 85 | + fuzz$ RUSTC=leafc cargo build --bin fuzz_target_1 --target-dir ./target/leaf |
| 86 | + ``` |
| 87 | + |
| 88 | +1. Build your fuzzer normally, e.g., |
| 89 | + ```console |
| 90 | + fuzz$ cargo fuzz build fuzz_target_1 |
| 91 | + ``` |
| 92 | + |
| 93 | +1. Run your fuzzer with the additional argument `conc_program` which points to the |
| 94 | + instrumented executable built using `leafc`. |
| 95 | + ```console |
| 96 | + fuzz$ cargo fuzz run fuzz_target_1 -- -conc_program=./target/leaf/debug/fuzz_target_1 |
| 97 | + ``` |
| 98 | + |
| 99 | +----------------- |
| 100 | + |
| 101 | +Please refer to the technical documentation for further details about the components and steps mentioned above. |
| 102 | + |
| 103 | + |
| 104 | +[1]: https://github.com/AFLplusplus/LibAFL |
0 commit comments