-
Notifications
You must be signed in to change notification settings - Fork 13.4k
std: sys: random: uefi: Provide rdrand based fallback #141324
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
base: master
Are you sure you want to change the base?
Conversation
rustbot has assigned @workingjubilee. Use |
Unfortunately, the |
@seijikun Can you check if your hardware passes the checks in getrandom rand backend? |
@Ayush1325 Sure! I don't have access to it at the moment, unfortunately - but I will do it monday morning. |
|
@Ayush1325 Looks good: |
Great, I will update the PR soon |
d4710c0
to
1c3abfb
Compare
@seijikun Can you take a look now? |
Sorry, took me a while to find out how this custom-std stuff works. This is how I did it:
And then, I built the project (consisting of ~3 code lines that just use a
|
@Ayush1325 There is currently still a bug in the implementation. So the normal case has to be edited to look like this: --- a/library/std/src/sys/random/uefi.rs
+++ b/library/std/src/sys/random/uefi.rs
@@ -3,22 +3,22 @@
use crate::sys::pal::helpers;
pub fn fill_bytes(bytes: &mut [u8]) {
- let handles =
- helpers::locate_handles(rng::PROTOCOL_GUID).expect("failed to generate random data");
- for handle in handles {
- if let Ok(protocol) = helpers::open_protocol::<rng::Protocol>(handle, rng::PROTOCOL_GUID) {
- let r = unsafe {
- ((*protocol.as_ptr()).get_rng)(
- protocol.as_ptr(),
- crate::ptr::null_mut(),
- bytes.len(),
- bytes.as_mut_ptr(),
- )
- };
- if r.is_error() {
- continue;
- } else {
- return;
+ if let Ok(handles) = helpers::locate_handles(rng::PROTOCOL_GUID) {
+ for handle in handles {
+ if let Ok(protocol) = helpers::open_protocol::<rng::Protocol>(handle, rng::PROTOCOL_GUID) {
+ let r = unsafe {
+ ((*protocol.as_ptr()).get_rng)(
+ protocol.as_ptr(),
+ crate::ptr::null_mut(),
+ bytes.len(),
+ bytes.as_mut_ptr(),
+ )
+ };
+ if r.is_error() {
+ continue;
+ } else {
+ return;
+ }
}
}
} |
I made the mentioned change on top of your branch locally. And as soon as that is changed...: use std::{collections::HashMap, time::Duration};
fn main() {
let mut map = HashMap::new();
map.insert("Ayush1325", "is awesome");
println!("{:?}", map);
std::thread::sleep(Duration::from_millis(31337));
} |
1c3abfb
to
5c59ba8
Compare
Thanks for reporting this. I have updated the PR (and also done some refactoring). |
Just tested against the newest commit: 5c59ba8 |
@joboet Does it look fine now? |
r? @joboet |
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 looks fine to me, only the zero-request case is handled incorrectly.
@@ -1,27 +1,156 @@ | |||
use r_efi::protocols::rng; | |||
pub fn fill_bytes(bytes: &mut [u8]) { | |||
// Try EFI_RNG_PROTOCOL |
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.
A zero-byte request should always succeed, but will currently panic if rdrand
is not available as the UEFI protocol will return EFI_INVALID_PARAMETER
.
// Try EFI_RNG_PROTOCOL | |
if bytes.is_empty() { | |
return; | |
} | |
// Try EFI_RNG_PROTOCOL |
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.
Done
library/std/src/sys/random/uefi.rs
Outdated
unsafe fn rdrand_exact(dest: &mut [u8]) -> Option<()> { | ||
// We use chunks_exact_mut instead of chunks_mut as it allows almost all | ||
// calls to memcpy to be elided by the compiler. | ||
let mut chunks = dest.chunks_exact_mut(size_of::<Word>()); |
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.
You could also use array_chunks_mut
to eliminate the copy_from_slice
.
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.
Done
5c59ba8
to
a6b2195
Compare
This comment has been minimized.
This comment has been minimized.
Some UEFI systems based on American Megatrends Inc. v3.3 do not provide RNG support [1]. So fallback to rdrand in such cases. [1]: rust-lang#138252 (comment) Signed-off-by: Ayush Singh <[email protected]>
a6b2195
to
669564d
Compare
Some UEFI systems based on American Megatrends Inc. v3.3 do not provide RNG support 1. So fallback to rdrand in such cases.
Fixes #138252
cc @seijikun