Add tool detection to build.rs, improve README (#43)

Co-authored-by: Dominik Maier <domenukk@gmail.com>
This commit is contained in:
Grant Hernandez 2021-04-10 02:16:35 -07:00 committed by GitHub
parent f4d5c045b4
commit 24a033de31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 179 additions and 122 deletions

View File

@ -6,9 +6,26 @@ use std::{
process::{exit, Command},
};
use which::which;
const LIBPNG_URL: &str =
"https://deac-fra.dl.sourceforge.net/project/libpng/libpng16/1.6.37/libpng-1.6.37.tar.xz";
fn build_dep_check(tools: &[&str]) {
for tool in tools.into_iter() {
println!("Checking for build tool {}...", tool);
match which::which(tool) {
Ok(path) => println!("Found build tool {}", path.to_str().unwrap()),
Err(_) => {
println!("ERROR: missing build tool {}", tool);
exit(1);
}
};
}
}
fn main() {
if cfg!(windows) {
println!("cargo:warning=Skipping libpng frida example on Windows");
@ -21,9 +38,12 @@ fn main() {
let out_dir_path = Path::new(&out_dir);
std::fs::create_dir_all(&out_dir).expect(&format!("Failed to create {}", &out_dir));
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=../libfuzzer_runtime/rt.c",);
println!("cargo:rerun-if-changed=harness.cc");
build_dep_check(&["clang", "clang++", "wget", "tar", "make"]);
let libpng = format!("{}/libpng-1.6.37", &out_dir);
let libpng_path = Path::new(&libpng);
let libpng_tar = format!("{}/libpng-1.6.37.tar.xz", &cwd);
@ -115,5 +135,4 @@ fn main() {
.unwrap();
assert!(status.success());
println!("cargo:rerun-if-changed=build.rs");
}

View File

@ -8,11 +8,24 @@ edition = "2018"
default = ["std"]
std = []
<<<<<<< HEAD
#[profile.release]
#lto = true
#codegen-units = 1
#opt-level = 3
#debug = true
[build-dependencies]
cc = { version = "1.0", features = ["parallel"] }
which = { version = "4.0.2" }
num_cpus = "1.0"
=======
[profile.release]
lto = true
codegen-units = 1
opt-level = 3
debug = true
>>>>>>> dev
[dependencies]
libafl = { path = "../../libafl/" }

View File

@ -1,11 +1,18 @@
# Libfuzzer for libpng
This folder contains an example fuzzer for libpng, using LLMP for fast multi-process fuzzing and crash detection.
To show off crash detection, we added a ud2 instruction to the harness, edit harness.cc if you want a non-crashing example.
To show off crash detection, we added a `ud2` instruction to the harness, edit harness.cc if you want a non-crashing example.
It has been tested on Linux.
## Build
<<<<<<< HEAD
You will need `clang` and `clang++` along with basic build dependencies for this example to compile.
To build this example, run `cargo build --example libfuzzer_libpng --release`.
This will call [the build.rs](./build.rs), which in turn downloads a libpng archive from the web.
Then, it will link [the fuzzer](./src/fuzzer.rs) against [the C++ harness](./harness.cc) and the instrumented `libpng`.
Afterwards, the fuzzer will be ready to run, from `../../target/examples/libfuzzer_libpng`.
=======
To build this example, run `cargo build --release`.
This will build the library with the fuzzer (src/lib.rs) with the libfuzzer compatibility layer and the SanitizerCoverage runtime functions for coverage feedback.
In addition, it will build also two C and C++ compiler wrappers (bin/c(c/xx).rs) that you must use to compile the target.
@ -29,12 +36,30 @@ Now, we have to build the libfuzzer harness and link all togheter to create our
```
Afterwards, the fuzzer will be ready to run simply executing `./fuzzer`.
>>>>>>> dev
## Run
The first time you run the binary, the broker will open a tcp port (currently on port `1337`), waiting for fuzzer clients to connect. This port is local and only used for the initial handshake. All further communication happens via shared map, to be independent of the kernel.
The first time you run the binary, the broker will open a tcp port (currently on port `1337`), waiting for fuzzer clients to connect. This port is local and only used for the initial handshake. All further communication happens via shared map, to be independent of the kernel. Currently you must run the clients from the libfuzzer_libpng directory for them to be able to access the PNG corpus.
```
cargo run --release --example libfuzzer_libpng
[libafl/src/bolts/llmp.rs:407] "We're the broker" = "We\'re the broker"
Doing broker things. Run this tool again to start fuzzing in a client.
```
And after running the above again in a separate terminal:
```
[libafl/src/bolts/llmp.rs:1464] "New connection" = "New connection"
[libafl/src/bolts/llmp.rs:1464] addr = 127.0.0.1:33500
[libafl/src/bolts/llmp.rs:1464] stream.peer_addr().unwrap() = 127.0.0.1:33500
[LOG Debug]: Loaded 4 initial testcases.
[New Testcase #2] clients: 3, corpus: 6, objectives: 0, executions: 5, exec/sec: 0
< fuzzing stats >
```
Each following execution will run a fuzzer client.
As this example uses in-process fuzzing, we added a Restarting Event Manager (`setup_restarting_mgr`).
This means each client will start itself again to listen for crashes and timeouts.
By restarting the actual fuzzer, it can recover from these exit conditions.