Skip to content

Commit

Permalink
Allowing passing a null pointer to getrandom() when length is 0
Browse files Browse the repository at this point in the history
The Linux kernel will handle a null pointer passed to 'getrandom'
without error, as long as the length is also 0. The `getrandom` crate
relies on this behavior: https://github.com/rust-random/getrandom/blob/ab44edf3c7af721a00e22648b6c811ccb559ba81/src/linux_android.rs#L26

Since it works fine on the actual kernel (and should continue to, due to
the kernel's backwards-compatibility guarantees), Miri should support it
as well.
  • Loading branch information
Aaron1011 committed Aug 4, 2019
1 parent d9d6df9 commit 8f4989c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
match this.read_scalar(args[0])?.to_usize(this)? {
id if id == sys_getrandom => {
let ptr = this.read_scalar(args[1])?.not_undef()?;
let ptr = this.read_scalar(args[1])?;
let len = this.read_scalar(args[2])?.to_usize(this)?;

// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
// neither of which have any effect on our current PRNG
let _flags = this.read_scalar(args[3])?.to_i32()?;

this.gen_random(len as usize, ptr)?;
if len != 0 {
// Linux allows passing a null pointer as
// long as the length is also 0. THerefore,
// we only call 'not_undef' if we
// have a non-zero length.
this.gen_random(len as usize, ptr.not_undef()?)?;
}
this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
}
id => {
Expand Down

0 comments on commit 8f4989c

Please sign in to comment.