-
Notifications
You must be signed in to change notification settings - Fork 182
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
linux_android: use libc::getrandom
#508
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,80 @@ | ||
//! Implementation for Linux / Android with `/dev/urandom` fallback | ||
use crate::{lazy::LazyBool, linux_android, use_file, util_libc::last_os_error, Error}; | ||
use core::mem::MaybeUninit; | ||
use crate::{use_file, util_libc, Error}; | ||
use core::{ | ||
ffi::c_void, | ||
mem::{self, MaybeUninit}, | ||
ptr::{self, NonNull}, | ||
sync::atomic::{AtomicPtr, Ordering}, | ||
}; | ||
|
||
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
// getrandom(2) was introduced in Linux 3.17 | ||
static HAS_GETRANDOM: LazyBool = LazyBool::new(); | ||
if HAS_GETRANDOM.unsync_init(is_getrandom_available) { | ||
linux_android::getrandom_inner(dest) | ||
} else { | ||
// prevent inlining of the fallback implementation | ||
#[inline(never)] | ||
fn inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
use_file::getrandom_inner(dest) | ||
type GetRandomFn = unsafe extern "C" fn(*mut c_void, libc::size_t, libc::c_uint) -> libc::ssize_t; | ||
|
||
/// Sentinel value which indicates that `libc::getrandom` either not available, | ||
/// or not supported by kernel. | ||
const NOT_AVAILABLE: NonNull<c_void> = unsafe { NonNull::new_unchecked(usize::MAX as *mut c_void) }; | ||
|
||
static GETRANDOM_FN: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut()); | ||
|
||
#[cold] | ||
fn init() -> NonNull<c_void> { | ||
static NAME: &[u8] = b"getrandom\0"; | ||
let name_ptr = NAME.as_ptr().cast::<libc::c_char>(); | ||
let raw_ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) }; | ||
let res_ptr = match NonNull::new(raw_ptr) { | ||
Some(fptr) => { | ||
let getrandom_fn = unsafe { mem::transmute::<NonNull<c_void>, GetRandomFn>(fptr) }; | ||
let dangling_ptr = ptr::NonNull::dangling().as_ptr(); | ||
// Check that `getrandom` syscall is supported by kernel | ||
let res = unsafe { getrandom_fn(dangling_ptr, 0, 0) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can skip this check and assume that if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, getrandom is documented as returning ENOSYS. |
||
if cfg!(getrandom_test_linux_fallback) { | ||
NOT_AVAILABLE | ||
} else if res.is_negative() { | ||
match util_libc::last_os_error().raw_os_error() { | ||
Some(libc::ENOSYS) => NOT_AVAILABLE, // No kernel support | ||
// The fallback on EPERM is intentionally not done on Android since this workaround | ||
// seems to be needed only for specific Linux-based products that aren't based | ||
// on Android. See https://github.com/rust-random/getrandom/issues/229. | ||
#[cfg(target_os = "linux")] | ||
Some(libc::EPERM) => NOT_AVAILABLE, // Blocked by seccomp | ||
_ => fptr, | ||
} | ||
} else { | ||
fptr | ||
} | ||
} | ||
None => NOT_AVAILABLE, | ||
}; | ||
|
||
inner(dest) | ||
} | ||
GETRANDOM_FN.store(res_ptr.as_ptr(), Ordering::Release); | ||
res_ptr | ||
} | ||
|
||
fn is_getrandom_available() -> bool { | ||
if cfg!(getrandom_test_linux_fallback) { | ||
false | ||
} else if linux_android::getrandom_syscall(&mut []) < 0 { | ||
match last_os_error().raw_os_error() { | ||
Some(libc::ENOSYS) => false, // No kernel support | ||
// The fallback on EPERM is intentionally not done on Android since this workaround | ||
// seems to be needed only for specific Linux-based products that aren't based | ||
// on Android. See https://github.com/rust-random/getrandom/issues/229. | ||
#[cfg(target_os = "linux")] | ||
Some(libc::EPERM) => false, // Blocked by seccomp | ||
_ => true, | ||
} | ||
// prevent inlining of the fallback implementation | ||
#[inline(never)] | ||
fn use_file_fallback(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
use_file::getrandom_inner(dest) | ||
} | ||
|
||
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
// Despite being only a single atomic variable, we still cannot always use | ||
// Ordering::Relaxed, as we need to make sure a successful call to `init` | ||
// is "ordered before" any data read through the returned pointer (which | ||
// occurs when the function is called). Our implementation mirrors that of | ||
// the one in libstd, meaning that the use of non-Relaxed operations is | ||
// probably unnecessary. | ||
let raw_ptr = GETRANDOM_FN.load(Ordering::Acquire); | ||
let fptr = match NonNull::new(raw_ptr) { | ||
Some(p) => p, | ||
None => init(), | ||
}; | ||
|
||
if fptr == NOT_AVAILABLE { | ||
use_file_fallback(dest) | ||
} else { | ||
true | ||
// note: `transume` is currently the only way to convert pointer into function reference | ||
let getrandom_fn = unsafe { mem::transmute::<NonNull<c_void>, GetRandomFn>(fptr) }; | ||
util_libc::sys_fill_exact(dest, |buf| unsafe { | ||
getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is complicated. Is there really such a big performance penalty for unconditionally calling getrandom each time and then falling back to /dev/urandom when getrandom returns ENOSYS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...or just using an AtomicBool like before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by "complicated"? This value is guaranteed to not be a valid function pointer, so we use it as "uninitialized" marker.
dlsym
can be a relatively heavy operation, so I would prefer to cache its result.With
AtomicBool
we would need a separate atomic to store function pointer. Why introduce two atomic loads when one would suffice?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I missed the dlsym bit and assumed you were calling libc::getrandom.