diff --git a/fuzz_runner/src/nyx/qemu_process.rs b/fuzz_runner/src/nyx/qemu_process.rs index 757ba05..d062d87 100644 --- a/fuzz_runner/src/nyx/qemu_process.rs +++ b/fuzz_runner/src/nyx/qemu_process.rs @@ -569,3 +569,38 @@ impl QemuProcess { (shm_work_dir_path, file_lock) } } + +/* Helper function to remove a Nyx workdir safely. Returns an error if + * expected sub dirs are missing or the path does not exist */ +pub fn remove_workdir_safe(workdir: &str) -> Result<(), String> { + let folders = vec![ + "/corpus/normal", + "/corpus/crash", + "/corpus/kasan", + "/corpus/timeout", + "/imports", + "/seeds", + "/snapshot", + "/forced_imports", + ]; + + if !Path::new(&format!("{}/", workdir)).exists() { + return Err(format!("\"{}/\" does not exists", workdir)); + } + + /* check if all sub dirs exists */ + for folder in folders.iter() { + if !Path::new(&format!("{}/{}", workdir, folder)).exists() { + return Err(format!("\"{}/{}\" does not exists", workdir, folder)); + } + } + + /* remove if all sub dirs exists */ + for folder in folders.iter() { + let _ = fs::remove_dir_all(format!("{}/{}", workdir, folder)); + } + + let _ = fs::remove_dir_all(workdir); + + return Ok(()); +} diff --git a/libnyx/src/ffi.rs b/libnyx/src/ffi.rs index 0a608a9..61ffa9f 100644 --- a/libnyx/src/ffi.rs +++ b/libnyx/src/ffi.rs @@ -275,3 +275,24 @@ pub extern "C" fn nyx_set_hprintf_fd(nyx_process: * mut NyxProcess, fd: i32) { (*__nyx_process_check_ptr(nyx_process)).process.set_hprintf_fd(fd); } } + +/* Helper function to remove a given Nyx workdir safely. + * This function will return an error if the path does not exist or it does + * not appear to be a Nyx workdir (e.g. specific sub directories are + * missing). */ +#[no_mangle] +pub extern "C" fn nyx_remove_work_dir(workdir: *const c_char) -> bool { + unsafe{ + let workdir = CStr::from_ptr(workdir).to_str().unwrap(); + + match remove_work_dir(workdir) { + Ok(_) => { + true + }, + Err(e) => { + eprintln!("[!] libnyx failed to remove workdir: {}", e); + false + } + } + } +} \ No newline at end of file diff --git a/libnyx/src/lib.rs b/libnyx/src/lib.rs index 2d53729..68a6949 100644 --- a/libnyx/src/lib.rs +++ b/libnyx/src/lib.rs @@ -367,3 +367,7 @@ impl NyxProcess { } } + +pub fn remove_work_dir(workdir: &str) -> Result<(), String> { + fuzz_runner::nyx::qemu_process::remove_workdir_safe(workdir) +} diff --git a/libnyx/test.c b/libnyx/test.c index a00d19a..a222e25 100644 --- a/libnyx/test.c +++ b/libnyx/test.c @@ -59,6 +59,7 @@ void hexdump(void *mem, unsigned int len) } } +#define WORKDIR_PATH "/tmp/wdir" int main(int argc, char** argv){ @@ -69,7 +70,7 @@ int main(int argc, char** argv){ //nyx_config_debug(nyx_config); - nyx_config_set_workdir_path(nyx_config, "/tmp/wdir"); + nyx_config_set_workdir_path(nyx_config, WORKDIR_PATH); nyx_config_set_input_buffer_size(nyx_config, 0x2000); int fd = open("/tmp/nyx_test_output.log", O_WRONLY | O_CREAT | O_TRUNC, 0644); @@ -113,4 +114,9 @@ int main(int argc, char** argv){ } nyx_shutdown(nyx_runner); + + if(!nyx_remove_work_dir(WORKDIR_PATH) ){ + printf("Error: Failed to remove work dir\n"); + } + }