add helper function to delete a workdir safely

This commit is contained in:
Sergej Schumilo 2023-04-14 04:18:31 +02:00
parent a9efaee0db
commit 186ee5f857
4 changed files with 67 additions and 1 deletions

View File

@ -569,3 +569,38 @@ impl QemuProcess {
(shm_work_dir_path, file_lock) (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(());
}

View File

@ -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); (*__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
}
}
}
}

View File

@ -367,3 +367,7 @@ impl NyxProcess {
} }
} }
pub fn remove_work_dir(workdir: &str) -> Result<(), String> {
fuzz_runner::nyx::qemu_process::remove_workdir_safe(workdir)
}

View File

@ -59,6 +59,7 @@ void hexdump(void *mem, unsigned int len)
} }
} }
#define WORKDIR_PATH "/tmp/wdir"
int main(int argc, char** argv){ int main(int argc, char** argv){
@ -69,7 +70,7 @@ int main(int argc, char** argv){
//nyx_config_debug(nyx_config); //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); nyx_config_set_input_buffer_size(nyx_config, 0x2000);
int fd = open("/tmp/nyx_test_output.log", O_WRONLY | O_CREAT | O_TRUNC, 0644); 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); nyx_shutdown(nyx_runner);
if(!nyx_remove_work_dir(WORKDIR_PATH) ){
printf("Error: Failed to remove work dir\n");
}
} }