Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 1 addition & 7 deletions src/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This module should provide `fill_inner` with the signature
//! `fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>`.
//! The function MUST fully initialize `dest` when `Ok(())` is returned;
//! the function may need to use `sanitizer::unpoison` as well.
//! the function may need to use `unpoison` as well.
//! The function MUST NOT ever write uninitialized bytes into `dest`,
//! regardless of what value it returns.

Expand All @@ -13,11 +13,9 @@ cfg_if! {
pub use custom::*;
} else if #[cfg(getrandom_backend = "linux_getrandom")] {
mod getrandom;
mod sanitizer;
pub use getrandom::*;
} else if #[cfg(getrandom_backend = "linux_raw")] {
mod linux_raw;
mod sanitizer;
pub use linux_raw::*;
} else if #[cfg(getrandom_backend = "rdrand")] {
mod rdrand;
Expand Down Expand Up @@ -49,7 +47,6 @@ cfg_if! {
pub use unsupported::*;
} else if #[cfg(all(target_os = "linux", target_env = ""))] {
mod linux_raw;
mod sanitizer;
pub use linux_raw::*;
} else if #[cfg(target_os = "espidf")] {
mod esp_idf;
Expand Down Expand Up @@ -117,7 +114,6 @@ cfg_if! {
))] {
mod use_file;
mod linux_android_with_fallback;
mod sanitizer;
pub use linux_android_with_fallback::*;
} else if #[cfg(any(
target_os = "android",
Expand All @@ -132,8 +128,6 @@ cfg_if! {
all(target_os = "horizon", target_arch = "arm"),
))] {
mod getrandom;
#[cfg(any(target_os = "android", target_os = "linux"))]
mod sanitizer;
pub use getrandom::*;
} else if #[cfg(target_os = "solaris")] {
mod solaris;
Expand Down
5 changes: 2 additions & 3 deletions src/backends/getentropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ use core::{ffi::c_void, mem::MaybeUninit};

pub use crate::util::{inner_u32, inner_u64};

#[path = "../util_libc.rs"]
mod util_libc;
crate::impl_utils!(get_errno, last_os_error);

#[inline]
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
for chunk in dest.chunks_mut(256) {
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr().cast::<c_void>(), chunk.len()) };
if ret != 0 {
return Err(util_libc::last_os_error());
return Err(last_os_error());
}
}
Ok(())
Expand Down
9 changes: 5 additions & 4 deletions src/backends/getrandom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ use core::mem::MaybeUninit;

pub use crate::util::{inner_u32, inner_u64};

#[path = "../util_libc.rs"]
mod util_libc;
crate::impl_utils!(get_errno, last_os_error, sys_fill_exact);

#[inline]
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
util_libc::sys_fill_exact(dest, |buf| {
sys_fill_exact(dest, |buf| {
let ret = unsafe { libc::getrandom(buf.as_mut_ptr().cast(), buf.len(), 0) };

#[cfg(any(target_os = "android", target_os = "linux"))]
unsafe {
super::sanitizer::unpoison_linux_getrandom_result(buf, ret);
crate::impl_utils!(unpoison_linux_getrandom_result);

unpoison_linux_getrandom_result(buf, ret);
}

ret
Expand Down
11 changes: 6 additions & 5 deletions src/backends/linux_android_with_fallback.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! Implementation for Linux / Android with `/dev/urandom` fallback
use super::{sanitizer, use_file};
use super::use_file::{self, last_os_error, sys_fill_exact};
use crate::Error;
use core::{
ffi::c_void,
mem::{MaybeUninit, transmute},
ptr::NonNull,
sync::atomic::{AtomicPtr, Ordering},
};
use use_file::util_libc;

pub use crate::util::{inner_u32, inner_u64};

Expand All @@ -19,6 +18,8 @@ const NOT_AVAILABLE: NonNull<c_void> = unsafe { NonNull::new_unchecked(usize::MA

static GETRANDOM_FN: AtomicPtr<c_void> = AtomicPtr::new(core::ptr::null_mut());

crate::impl_utils!(unpoison_linux_getrandom_result);

#[cold]
#[inline(never)]
fn init() -> NonNull<c_void> {
Expand All @@ -44,7 +45,7 @@ fn init() -> NonNull<c_void> {
if cfg!(getrandom_test_linux_fallback) {
NOT_AVAILABLE
} else if res.is_negative() {
match util_libc::last_os_error().raw_os_error() {
match 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
Expand Down Expand Up @@ -94,9 +95,9 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
} else {
// note: `transmute` is currently the only way to convert a pointer into a function reference
let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) };
util_libc::sys_fill_exact(dest, |buf| unsafe {
sys_fill_exact(dest, |buf| unsafe {
let ret = getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0);
sanitizer::unpoison_linux_getrandom_result(buf, ret);
unpoison_linux_getrandom_result(buf, ret);
ret
})
}
Expand Down
5 changes: 3 additions & 2 deletions src/backends/linux_raw.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Implementation for Linux / Android using `asm!`-based syscalls.
use super::sanitizer;
pub use crate::util::{inner_u32, inner_u64};
use crate::{Error, MaybeUninit};

Expand Down Expand Up @@ -140,14 +139,16 @@ unsafe fn getrandom_syscall(buf: *mut u8, buflen: usize, flags: u32) -> isize {
r0
}

crate::impl_utils!(unpoison_linux_getrandom_result);

#[inline]
pub fn fill_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
// Value of this error code is stable across all target arches.
const EINTR: isize = -4;

loop {
let ret = unsafe { getrandom_syscall(dest.as_mut_ptr().cast(), dest.len(), 0) };
unsafe { sanitizer::unpoison_linux_getrandom_result(dest, ret) };
unsafe { unpoison_linux_getrandom_result(dest, ret) };
match usize::try_from(ret) {
Ok(0) => return Err(Error::UNEXPECTED),
Ok(len) => {
Expand Down
5 changes: 2 additions & 3 deletions src/backends/netbsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use core::{

pub use crate::util::{inner_u32, inner_u64};

#[path = "../util_libc.rs"]
mod util_libc;
crate::impl_utils!(get_errno, last_os_error, sys_fill_exact);

unsafe extern "C" fn polyfill_using_kern_arand(
buf: *mut c_void,
Expand Down Expand Up @@ -72,7 +71,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
fptr = init();
}
let fptr = unsafe { mem::transmute::<*mut c_void, GetRandomFn>(fptr) };
util_libc::sys_fill_exact(dest, |buf| unsafe {
sys_fill_exact(dest, |buf| unsafe {
fptr(buf.as_mut_ptr().cast::<c_void>(), buf.len(), 0)
})
}
55 changes: 0 additions & 55 deletions src/backends/sanitizer.rs

This file was deleted.

5 changes: 2 additions & 3 deletions src/backends/solaris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use core::{ffi::c_void, mem::MaybeUninit};

pub use crate::util::{inner_u32, inner_u64};

#[path = "../util_libc.rs"]
mod util_libc;
crate::impl_utils!(get_errno, last_os_error);

const MAX_BYTES: usize = 1024;

Expand All @@ -33,7 +32,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
// Good. Keep going.
Ok(ret) if ret == chunk.len() => {}
// The syscall failed.
Ok(0) => return Err(util_libc::last_os_error()),
Ok(0) => return Err(last_os_error()),
// All other cases should be impossible.
_ => return Err(Error::UNEXPECTED),
}
Expand Down
11 changes: 5 additions & 6 deletions src/backends/use_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use core::{
#[cfg(not(any(target_os = "android", target_os = "linux")))]
pub use crate::util::{inner_u32, inner_u64};

#[path = "../util_libc.rs"]
pub(super) mod util_libc;

/// For all platforms, we use `/dev/urandom` rather than `/dev/random`.
/// For more information see the linked man pages in lib.rs.
/// - On Linux, "/dev/urandom is preferred and sufficient in all use cases".
Expand Down Expand Up @@ -40,13 +37,15 @@ const FD_ONGOING_INIT: libc::c_int = -2;
// `Ordering::Acquire` to synchronize with it.
static FD: AtomicI32 = AtomicI32::new(FD_UNINIT);

crate::impl_utils!(get_errno, last_os_error, sys_fill_exact);

#[inline]
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
let mut fd = FD.load(Ordering::Acquire);
if fd == FD_UNINIT || fd == FD_ONGOING_INIT {
fd = open_or_wait()?;
}
util_libc::sys_fill_exact(dest, |buf| unsafe {
sys_fill_exact(dest, |buf| unsafe {
libc::read(fd, buf.as_mut_ptr().cast::<c_void>(), buf.len())
})
}
Expand All @@ -58,7 +57,7 @@ fn open_readonly(path: &CStr) -> Result<libc::c_int, Error> {
if fd >= 0 {
return Ok(fd);
}
let err = util_libc::last_os_error();
let err = last_os_error();
// We should try again if open() was interrupted.
if err.raw_os_error() != Some(libc::EINTR) {
return Err(err);
Expand Down Expand Up @@ -136,7 +135,7 @@ mod sync {

#[cfg(any(target_os = "android", target_os = "linux"))]
mod sync {
use super::{Error, FD, FD_ONGOING_INIT, open_readonly, util_libc::last_os_error};
use super::{Error, FD, FD_ONGOING_INIT, last_os_error, open_readonly};

/// Wait for atomic `FD` to change value from `FD_ONGOING_INIT` to something else.
///
Expand Down
8 changes: 4 additions & 4 deletions src/backends/vxworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use core::{
sync::atomic::{AtomicBool, Ordering::Relaxed},
};

#[path = "../util_libc.rs"]
mod util_libc;

pub use crate::util::{inner_u32, inner_u64};

static RNG_INIT: AtomicBool = AtomicBool::new(false);

use libc::errnoGet as get_errno;
crate::impl_utils!(last_os_error);

#[cold]
fn init() -> Result<(), Error> {
let ret = unsafe { libc::randSecure() };
Expand Down Expand Up @@ -42,7 +42,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
let p: *mut libc::c_uchar = chunk.as_mut_ptr().cast();
let ret = unsafe { libc::randABytes(p, chunk_len) };
if ret != 0 {
return Err(util_libc::last_os_error());
return Err(last_os_error());
}
}
Ok(())
Expand Down
Loading