Skip to content

Commit

Permalink
Fix LLVMFuzzerCustomMutator with different sizes (#2347)
Browse files Browse the repository at this point in the history
* Fix LLVMFuzzerCustomMutator with different sizes

* removed needles extra thingy

* clippy

* more clip
  • Loading branch information
domenukk authored Jun 28, 2024
1 parent 50d7542 commit 602bce4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
5 changes: 2 additions & 3 deletions libafl_qemu/src/helpers/injections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,8 @@ where
} else {
libs.iter()
.filter_map(|lib| find_function(qemu, &lib.name, name, lib.off).unwrap())
.map(|func_pc| {
log::info!("Injections: Function {name} found at {func_pc:#x}",);
func_pc
.inspect(|&func_pc| {
log::info!("Injections: Function {name} found at {func_pc:#x}");
})
.collect()
};
Expand Down
47 changes: 28 additions & 19 deletions libafl_targets/src/libfuzzer/mutators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use libafl::{
state::{HasCorpus, HasMaxSize, HasRand},
Error,
};
use libafl_bolts::{rands::Rand, AsSlice, Named};
use libafl_bolts::{rands::Rand, AsSlice, HasLen, Named};

extern "C" {
fn libafl_targets_has_libfuzzer_custom_mutator() -> bool;
Expand Down Expand Up @@ -322,10 +322,9 @@ where
input: &mut S::Input,
) -> Result<MutationResult, Error> {
let seed = state.rand_mut().next();
let target = input.bytes();
let mut bytes = Vec::with_capacity(state.max_size());
bytes.extend_from_slice(target.as_slice());
bytes.resize(state.max_size(), 0);
let len_orig = input.bytes().len();
let len_max = state.max_size();
input.resize(len_max, 0);

// we assume that the fuzzer did not use this mutator, but instead utilised their own
let result = Rc::new(RefCell::new(Ok(MutationResult::Mutated)));
Expand All @@ -334,11 +333,11 @@ where
let mut mutator = mutator.borrow_mut();
mutator.replace(Box::new(proxy.weak()))
});
let new_size = unsafe {
let new_len = unsafe {
libafl_targets_libfuzzer_custom_mutator(
bytes.as_mut_ptr(),
target.as_slice().len(),
bytes.len(),
input.bytes_mut().as_mut_ptr(),
len_orig,
len_max,
seed as u32,
)
};
Expand All @@ -350,15 +349,17 @@ where
if result.deref().borrow().is_err() {
return result.replace(Ok(MutationResult::Skipped));
}
bytes.truncate(new_size);
input.bytes_mut().copy_from_slice(&bytes);
if new_len > len_max {
return Err(Error::illegal_state("LLVMFuzzerCustomMutator returned more bytes than allowed. Expected up to {max_len} but got {new_len}"));
}
input.resize(new_len, 0);
Ok(MutationResult::Mutated)
}
}

impl<MT, SM> Named for LLVMCustomMutator<MT, SM, true> {
fn name(&self) -> &Cow<'static, str> {
static NAME: Cow<'static, str> = Cow::Borrowed("LLVMCustomCrossover");
static NAME: Cow<'static, str> = Cow::Borrowed("LLVMCustomMutator");
&NAME
}
}
Expand Down Expand Up @@ -411,7 +412,11 @@ where

let seed = state.rand_mut().next();
let mut out = vec![0u8; state.max_size()];
let data1 = input.bytes();

let len_max = state.max_size();
let len_orig = input.len();

input.resize(len_max, 0);

// we assume that the fuzzer did not use this mutator, but instead utilised their own
let result = Rc::new(RefCell::new(Ok(MutationResult::Mutated)));
Expand All @@ -420,14 +425,14 @@ where
let mut mutator = mutator.borrow_mut();
mutator.replace(Box::new(proxy.weak()))
});
let new_size = unsafe {
let new_len = unsafe {
libafl_targets_libfuzzer_custom_crossover(
data1.as_ptr(),
data1.len(),
input.bytes_mut().as_mut_ptr(),
len_orig,
data2.as_ptr(),
data2.len(),
out.as_mut_ptr(),
out.len(),
len_max,
seed as u32,
)
};
Expand All @@ -439,8 +444,12 @@ where
if result.deref().borrow().is_err() {
return result.replace(Ok(MutationResult::Skipped));
}
out.truncate(new_size);
input.bytes_mut().copy_from_slice(&out);

if new_len > len_max {
return Err(Error::illegal_state("LLVMFuzzerCustomCrossOver returned more bytes than allowed. Expected up to {max_len} but got {new_len}"));
}

input.resize(new_len, 0);
Ok(MutationResult::Mutated)
}
}

0 comments on commit 602bce4

Please sign in to comment.