Skip to content

Commit ea6ceb9

Browse files
committed
add TARGET_HASH support
1 parent b700fcd commit ea6ceb9

File tree

7 files changed

+92
-5
lines changed

7 files changed

+92
-5
lines changed

acat/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use fuzz_runner::nyx::aux_buffer;
1+
use fuzz_runner::nyx::aux_buffer::{self, AUX_BUFFER_SIZE};
22

33
use clap::{App, Arg, AppSettings};
44

@@ -135,7 +135,7 @@ fn main() {
135135
.read(true)
136136
.open(aux_buffer_file)
137137
.expect("couldn't open aux buffer file");
138-
let aux_buffer = aux_buffer::AuxBuffer::new_readonly(aux_shm_f, true);
138+
let aux_buffer = aux_buffer::AuxBuffer::new_readonly(aux_shm_f, true, AUX_BUFFER_SIZE);
139139

140140
aux_buffer.validate_header().unwrap();
141141

config/src/config.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::io::Read;
12
use std::time::Duration;
23
use serde_derive::Serialize;
34
use serde_derive::Deserialize;
@@ -140,8 +141,32 @@ pub struct FuzzerConfig {
140141
pub write_protected_input_buffer: bool,
141142
pub cow_primary_size: Option<u64>,
142143
pub ipt_filters: [IptFilter;4],
144+
pub target_hash: Option<[u8; 20]>
143145
}
144146
impl FuzzerConfig{
147+
148+
fn load_target_hash(sharedir: &str) -> Option<[u8; 20]> {
149+
let mut file = File::open(format!("{}/TARGET_HASH", sharedir)).ok()?;
150+
let mut content = String::new();
151+
file.read_to_string(&mut content).ok()?;
152+
153+
let content = content.trim();
154+
155+
if content.len() < 40 {
156+
return None;
157+
}
158+
159+
let mut bytes = [0u8; 20];
160+
for i in 0..20 {
161+
match u8::from_str_radix(&content[2 * i..2 * i + 2], 16) {
162+
Ok(byte) => bytes[i] = byte,
163+
Err(_) => return None,
164+
}
165+
}
166+
167+
Some(bytes)
168+
}
169+
145170
pub fn new_from_loader(sharedir: &str, default: FuzzerConfigLoader, config: FuzzerConfigLoader) -> Self {
146171

147172
let seed_path = config.seed_path.or(default.seed_path).unwrap();
@@ -152,6 +177,8 @@ impl FuzzerConfig{
152177
Some(into_absolute_path(&sharedir, seed_path))
153178
};
154179

180+
let target_hash = Self::load_target_hash(&sharedir);
181+
155182
Self{
156183
spec_path: format!("{}/spec.msgp",sharedir),
157184
workdir_path: config.workdir_path.or(default.workdir_path).expect("no workdir_path specified"),
@@ -172,6 +199,7 @@ impl FuzzerConfig{
172199
config.ip2,
173200
config.ip3,
174201
],
202+
target_hash: target_hash,
175203
}
176204
}
177205
}

fuzz_runner/src/nyx/aux_buffer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub const NYX_INPUT_WRITE: u8 = 4;
2020
pub const NYX_ABORT: u8 = 5;
2121

2222

23-
const AUX_BUFFER_SIZE: usize = 4096;
23+
pub const AUX_BUFFER_SIZE: usize = 4096;
2424

2525
const AUX_MAGIC: u64 = 0x54502d554d4551_u64;
2626
const QEMU_PT_VERSION: u16 = 3; /* let's start at 1 for the initial version using the aux buffer */

libnyx/src/ffi.rs

+40
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ pub extern "C" fn nyx_config_load(sharedir: *const c_char) -> *mut c_void {
6060
Box::into_raw(Box::new(cfg)) as *mut c_void
6161
}
6262

63+
#[no_mangle]
64+
pub extern "C" fn nyx_config_free(config: * mut c_void) {
65+
if config.is_null() { return; }
66+
let cfg = __nyx_config_check_ptr(config);
67+
68+
unsafe {
69+
drop(Box::from_raw(cfg));
70+
}
71+
}
72+
6373
/* Simple debug function to print the entire config object to stdout. */
6474
#[no_mangle]
6575
pub extern "C" fn nyx_config_debug(config: * mut c_void) {
@@ -203,6 +213,36 @@ pub extern "C" fn nyx_get_bitmap_buffer_size(nyx_process: * mut NyxProcess) -> u
203213
}
204214
}
205215

216+
#[no_mangle]
217+
pub extern "C" fn nyx_get_target_hash(config: * mut c_void, buffer: *mut u8) -> bool {
218+
let cfg = __nyx_config_check_ptr(config);
219+
220+
unsafe{
221+
match NyxConfig::target_hash(&mut *cfg) {
222+
Some(mut x) => {
223+
let val = x.as_mut_ptr();
224+
std::ptr::copy(val, buffer, 20);
225+
true
226+
},
227+
None => false,
228+
}
229+
}
230+
}
231+
232+
#[no_mangle]
233+
pub extern "C" fn nyx_get_target_hash64(config: * mut c_void) -> u64 {
234+
let cfg = __nyx_config_check_ptr(config);
235+
236+
unsafe{
237+
match NyxConfig::target_hash(&mut *cfg) {
238+
Some(x) => {
239+
u64::from_be_bytes(x[0..8].try_into().unwrap())
240+
},
241+
None => 0,
242+
}
243+
}
244+
}
245+
206246
#[no_mangle]
207247
pub extern "C" fn nyx_shutdown(nyx_process: * mut NyxProcess) {
208248
unsafe{

libnyx/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ impl NyxConfig {
134134
return Some(process_cfg.ramfs);
135135
}
136136

137+
/* Returns the SHA1 target hash (basically the content of the TARGET_HASH file).
138+
* If the TARGET_HASH file does not exist, this function returns None.
139+
*/
140+
pub fn target_hash(&self) -> Option<[u8; 20]> {
141+
self.config.fuzz.target_hash
142+
}
143+
137144
/* Returns the configured timeout threshold as a std::time::Duration object. */
138145
pub fn timeout(&self) -> std::time::Duration {
139146
self.config.fuzz.time_limit

libnyx/test.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include <sys/types.h>
1010
#include <sys/stat.h>
1111
#include <fcntl.h>
12-
12+
#include <stdlib.h>
13+
#include <string.h>
14+
1315
#ifndef HEXDUMP_COLS
1416
#define HEXDUMP_COLS 16
1517
#endif
@@ -68,6 +70,15 @@ int main(int argc, char** argv){
6870

6971
void* nyx_config = nyx_config_load("/tmp/nyx_libxml2/");
7072

73+
uint8_t* target_hash = malloc(20);
74+
memset(target_hash, 0, 20);
75+
if (nyx_get_target_hash(nyx_config, target_hash) == true) {
76+
hexdump(target_hash, 20);
77+
}
78+
79+
printf("TARGET-HASH: %lx\n", nyx_get_target_hash64(nyx_config));
80+
free(target_hash);
81+
7182
//nyx_config_debug(nyx_config);
7283

7384
nyx_config_set_workdir_path(nyx_config, WORKDIR_PATH);
@@ -118,5 +129,6 @@ int main(int argc, char** argv){
118129
if(!nyx_remove_work_dir(WORKDIR_PATH) ){
119130
printf("Error: Failed to remove work dir\n");
120131
}
132+
nyx_config_free(nyx_config);
121133

122134
}

libnyx/test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cargo build && gcc test.c target/debug/liblibnyx.a -o app -pthread -ldl -lrt && ./app
1+
cargo build && gcc test.c target/debug/liblibnyx.a -o app -pthread -ldl -lrt -lm && ./app

0 commit comments

Comments
 (0)