Fuzz Testing in Rust

Fuzz testing, or fuzzing, is a technique used to discover vulnerabilities in software by providing random data as input to a program. In Rust, fuzzing can be effectively implemented using the cargo-fuzz crate, which automates the process of generating random inputs to test functions for potential errors or crashes.

Setting Up Fuzz Testing with cargo-fuzz

To begin fuzz testing in a Rust project, you first need to initialize the necessary boilerplate code. This is done by running the command cargo fuzz init, which sets up a new project structure within your existing project. The structure created by cargo-fuzz is as follows:

$ tree .
.
├── Cargo.lock
├── Cargo.toml
├── fuzz
│   ├── Cargo.lock
│   ├── Cargo.toml
│   └── fuzz_targets
│       └── fuzz_target_1.rs
└── src
   └── lib.rs

3 directories, 6 files

The fuzz subdirectory contains a new project with a test file named fuzz_target_1.rs. You can list the available fuzz targets using the command cargo fuzz list, which will display fuzz_target_1.

Writing a Fuzz Test

Once the setup is complete, the next step is to write a fuzz test. The test involves calling the function you want to test with random data. The Arbitrary crate is used to derive the necessary data format. Below is an example fuzz test:

Listing 7.11 Fuzz test

#![no_main]
use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
 
#[derive(Arbitrary, Debug)]         #1
struct Input {
    s: String,                      #2
}
 
fuzz_target!(|input: Input| {       #3
    use fuzzme::parse_integer;
 
    parse_integer(&input.s);        #4
});
  1. The Arbitrary and Debug traits are automatically generated for the Input struct.
  2. The Input struct contains a single string field, which will be populated with random data by the fuzzer.
  3. The fuzz_target! macro defines the entry point for the fuzz test.
  4. The function parse_integer is called with the random string data.

Running the Fuzz Test

After writing the fuzz test, you can run it using the command cargo fuzz run fuzz_target_1. This will execute the test and produce output indicating whether any errors or crashes were encountered. Below is an example of the output you might see:

Listing 7.12 Output of cargo fuzz run fuzz_target_1

cargo fuzz run fuzz_target_1
  Compiling fuzzme-fuzz v0.0.0
  (/Users/brenden/dev/code-like-a-pro-in-rust/code/c7/7.5/fuzzme/fuzz)
   Finished release [optimized] target(s) in 1.07s
   Finished release [optimized] target(s) in 0.01s
    Running `fuzz/target/x86_64-apple-darwin/release/fuzz_target_1
    -artifact_prefix=/Users/brenden/dev/code-like-a-pro-in-rust/
 code/c7/7.5/fuzzme/fuzz/artifacts/fuzz_target_1/
/Users/brenden/dev/code-like-a-pro-in-rust/code/c7/7.5/fuzzme/
 fuzz/corpus/fuzz_target_1`
fuzz_target_1(14537,0x10d0a6600) malloc: nano zone abandoned due to
inability to preallocate reserved vm space.
INFO: Running with entropic power schedule (0xFF, 100).
... snip ...

Failing input:

   fuzz/artifacts/fuzz_target_1/
   crash-105eb7135ad863be4e095db6ffe64dc1b9a1a466

Output of `std::fmt::Debug`:

   Input {
       s: "8884844484",
   }

Reproduce with:

   cargo fuzz run fuzz_target_1 fuzz/artifacts/fuzz_target_1/
   crash-105eb7135ad863be4e095db6ffe64dc1b9a1a466

Minimize test case with:

   cargo fuzz tmin fuzz_target_1 fuzz/artifacts/fuzz_target_1/
   crash-105eb7135ad863be4e095db6ffe64dc1b9a1a466

The output indicates a failing input that caused the function to crash. It also provides commands to reproduce the crash and minimize the test case, which are useful for debugging and fixing the underlying issue.