Skip to content

Allowing passing a null pointer to getrandom() when length is 0 #884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
ptr: Scalar<Tag>,
len: usize,
) -> InterpResult<'tcx> {
// Some programs pass in a null pointer and a length of 0
// to their platform's random-generation function (e.g. getrandom())
// on Linux. For compatibility with these programs, we don't perform
// any additional checks - it's okay if the pointer is invalid,
// since we wouldn't actually be writing to it.
if len == 0 {
return Ok(());
}
let this = self.eval_context_mut();

let ptr = match this.memory().check_ptr_access(ptr, Size::from_bytes(len as u64), Align::from_bytes(1).unwrap())? {
Some(ptr) => ptr,
None => return Ok(()), // zero-sized access
};
let ptr = this.memory().check_ptr_access(
ptr,
Size::from_bytes(len as u64),
Align::from_bytes(1).unwrap()
)?.expect("we already checked for size 0");

let rng = this.memory_mut().extra.rng.get_mut();
let mut data = vec![0; len];
Expand Down