From 24a033de31f5dc2dcef0497b04bf5f5a95fb1496 Mon Sep 17 00:00:00 2001 From: Grant Hernandez Date: Sat, 10 Apr 2021 02:16:35 -0700 Subject: [PATCH] Add tool detection to build.rs, improve README (#43) Co-authored-by: Dominik Maier --- fuzzers/frida_libpng/build.rs | 257 +++++++++++++++------------- fuzzers/libfuzzer_libpng/Cargo.toml | 13 ++ fuzzers/libfuzzer_libpng/README.md | 31 +++- 3 files changed, 179 insertions(+), 122 deletions(-) diff --git a/fuzzers/frida_libpng/build.rs b/fuzzers/frida_libpng/build.rs index 63c8ae17ee..211ff7c400 100644 --- a/fuzzers/frida_libpng/build.rs +++ b/fuzzers/frida_libpng/build.rs @@ -1,119 +1,138 @@ -// build.rs - -use std::{ - env, - path::Path, - process::{exit, Command}, -}; - -const LIBPNG_URL: &str = - "https://deac-fra.dl.sourceforge.net/project/libpng/libpng16/1.6.37/libpng-1.6.37.tar.xz"; - -fn main() { - if cfg!(windows) { - println!("cargo:warning=Skipping libpng frida example on Windows"); - exit(0); - } - - let out_dir = env::var_os("OUT_DIR").unwrap(); - let cwd = env::current_dir().unwrap().to_string_lossy().to_string(); - let out_dir = out_dir.to_string_lossy().to_string(); - 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=../libfuzzer_runtime/rt.c",); - println!("cargo:rerun-if-changed=harness.cc"); - - 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); - - // Enforce clang for its -fsanitize-coverage support. - let clang = match env::var("CLANG_PATH") { - Ok(path) => path, - Err(_) => "clang".to_string(), - }; - let clangpp = format!("{}++", &clang); - std::env::set_var("CC", &clang); - std::env::set_var("CXX", &clangpp); - let ldflags = match env::var("LDFLAGS") { - Ok(val) => val, - Err(_) => "".to_string(), - }; - - // println!("cargo:warning=output path is {}", libpng); - if !libpng_path.is_dir() { - if !Path::new(&libpng_tar).is_file() { - println!("cargo:warning=Libpng not found, downloading..."); - // Download libpng - Command::new("wget") - .arg("-c") - .arg(LIBPNG_URL) - .arg("-O") - .arg(&libpng_tar) - .status() - .unwrap(); - } - Command::new("tar") - .current_dir(&out_dir_path) - .arg("xvf") - .arg(&libpng_tar) - .status() - .unwrap(); - Command::new(format!("{}/configure", &libpng)) - .current_dir(&libpng_path) - .args(&[ - "--disable-shared", - &format!("--host={}", env::var("TARGET").unwrap())[..], - ]) - .env("CC", &clang) - .env("CXX", &clangpp) - .env( - "CFLAGS", - "-O3 -g -D_DEFAULT_SOURCE -fPIC -fno-omit-frame-pointer", - ) - .env( - "CXXFLAGS", - "-O3 -g -D_DEFAULT_SOURCE -fPIC -fno-omit-frame-pointer", - ) - .env( - "LDFLAGS", - //format!("-g -fPIE -fsanitize=address {}", ldflags), - format!("-g -fPIE {}", ldflags), - ) - .status() - .unwrap(); - Command::new("make") - .current_dir(&libpng_path) - .status() - .unwrap(); - } - - let status = cc::Build::new() - .cpp(true) - .get_compiler() - .to_command() - .current_dir(&cwd) - .arg("-I") - .arg(&libpng) - //.arg("-D") - //.arg("HAS_DUMMY_CRASH=1") - .arg("-fPIC") - .arg("-shared") - .arg(if env::var("CARGO_CFG_TARGET_OS").unwrap() == "android" { - "-static-libstdc++" - } else { - "" - }) - .arg("-o") - .arg(format!("{}/libpng-harness.so", &out_dir)) - .arg("./harness.cc") - .arg(format!("{}/.libs/libpng16.a", &libpng)) - .arg("-l") - .arg("z") - .status() - .unwrap(); - assert!(status.success()); - - println!("cargo:rerun-if-changed=build.rs"); -} +// build.rs + +use std::{ + env, + path::Path, + 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"); + exit(0); + } + + let out_dir = env::var_os("OUT_DIR").unwrap(); + let cwd = env::current_dir().unwrap().to_string_lossy().to_string(); + let out_dir = out_dir.to_string_lossy().to_string(); + 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); + + // Enforce clang for its -fsanitize-coverage support. + let clang = match env::var("CLANG_PATH") { + Ok(path) => path, + Err(_) => "clang".to_string(), + }; + let clangpp = format!("{}++", &clang); + std::env::set_var("CC", &clang); + std::env::set_var("CXX", &clangpp); + let ldflags = match env::var("LDFLAGS") { + Ok(val) => val, + Err(_) => "".to_string(), + }; + + // println!("cargo:warning=output path is {}", libpng); + if !libpng_path.is_dir() { + if !Path::new(&libpng_tar).is_file() { + println!("cargo:warning=Libpng not found, downloading..."); + // Download libpng + Command::new("wget") + .arg("-c") + .arg(LIBPNG_URL) + .arg("-O") + .arg(&libpng_tar) + .status() + .unwrap(); + } + Command::new("tar") + .current_dir(&out_dir_path) + .arg("xvf") + .arg(&libpng_tar) + .status() + .unwrap(); + Command::new(format!("{}/configure", &libpng)) + .current_dir(&libpng_path) + .args(&[ + "--disable-shared", + &format!("--host={}", env::var("TARGET").unwrap())[..], + ]) + .env("CC", &clang) + .env("CXX", &clangpp) + .env( + "CFLAGS", + "-O3 -g -D_DEFAULT_SOURCE -fPIC -fno-omit-frame-pointer", + ) + .env( + "CXXFLAGS", + "-O3 -g -D_DEFAULT_SOURCE -fPIC -fno-omit-frame-pointer", + ) + .env( + "LDFLAGS", + //format!("-g -fPIE -fsanitize=address {}", ldflags), + format!("-g -fPIE {}", ldflags), + ) + .status() + .unwrap(); + Command::new("make") + .current_dir(&libpng_path) + .status() + .unwrap(); + } + + let status = cc::Build::new() + .cpp(true) + .get_compiler() + .to_command() + .current_dir(&cwd) + .arg("-I") + .arg(&libpng) + //.arg("-D") + //.arg("HAS_DUMMY_CRASH=1") + .arg("-fPIC") + .arg("-shared") + .arg(if env::var("CARGO_CFG_TARGET_OS").unwrap() == "android" { + "-static-libstdc++" + } else { + "" + }) + .arg("-o") + .arg(format!("{}/libpng-harness.so", &out_dir)) + .arg("./harness.cc") + .arg(format!("{}/.libs/libpng16.a", &libpng)) + .arg("-l") + .arg("z") + .status() + .unwrap(); + assert!(status.success()); + +} diff --git a/fuzzers/libfuzzer_libpng/Cargo.toml b/fuzzers/libfuzzer_libpng/Cargo.toml index 1f125edadc..3bb4df74f4 100644 --- a/fuzzers/libfuzzer_libpng/Cargo.toml +++ b/fuzzers/libfuzzer_libpng/Cargo.toml @@ -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/" } diff --git a/fuzzers/libfuzzer_libpng/README.md b/fuzzers/libfuzzer_libpng/README.md index acb7426f8a..17035c6ddc 100644 --- a/fuzzers/libfuzzer_libpng/README.md +++ b/fuzzers/libfuzzer_libpng/README.md @@ -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.