-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Windows: Use ProcessPrng for random keys #121337
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
compiler/rustc_codegen_cranelift/patches/0029-stdlib-rawdylib-processprng.patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
From 9f65e742ba3e41474e6126c6c4469c48eaa6ca7e Mon Sep 17 00:00:00 2001 | ||
From: Chris Denton <chris@chrisdenton.dev> | ||
Date: Tue, 20 Feb 2024 16:01:40 -0300 | ||
Subject: [PATCH] Don't use raw-dylib in std | ||
|
||
--- | ||
library/std/src/sys/pal/windows/c.rs | 2 +- | ||
library/std/src/sys/pal/windows/rand.rs | 3 +-- | ||
2 files changed, 2 insertions(+), 3 deletions(-) | ||
|
||
diff --git a/library/std/src/sys/pal/windows/c.rs b/library/std/src/sys/pal/windows/c.rs | ||
index ad8e01bfa9b..9ca8e4c16ce 100644 | ||
--- a/library/std/src/sys/pal/windows/c.rs | ||
+++ b/library/std/src/sys/pal/windows/c.rs | ||
@@ -323,7 +323,7 @@ pub unsafe fn NtWriteFile( | ||
|
||
// Use raw-dylib to import ProcessPrng as we can't rely on there being an import library. | ||
cfg_if::cfg_if! { | ||
-if #[cfg(not(target_vendor = "win7"))] { | ||
+if #[cfg(any())] { | ||
#[cfg(target_arch = "x86")] | ||
#[link(name = "bcryptprimitives", kind = "raw-dylib", import_name_type = "undecorated")] | ||
extern "system" { | ||
diff --git a/library/std/src/sys/pal/windows/rand.rs b/library/std/src/sys/pal/windows/rand.rs | ||
index e427546222a..f2fe42a4d51 100644 | ||
--- a/library/std/src/sys/pal/windows/rand.rs | ||
+++ b/library/std/src/sys/pal/windows/rand.rs | ||
@@ -2,7 +2,7 @@ | ||
use core::mem; | ||
use core::ptr; | ||
|
||
-#[cfg(not(target_vendor = "win7"))] | ||
+#[cfg(any())] | ||
#[inline] | ||
pub fn hashmap_random_keys() -> (u64, u64) { | ||
let mut v = (0, 0); | ||
@@ -13,7 +13,6 @@ pub fn hashmap_random_keys() -> (u64, u64) { | ||
v | ||
} | ||
|
||
-#[cfg(target_vendor = "win7")] | ||
pub fn hashmap_random_keys() -> (u64, u64) { | ||
use crate::ffi::c_void; | ||
use crate::io; | ||
-- | ||
2.42.0.windows.2 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,27 @@ | ||
use crate::mem; | ||
use crate::ptr; | ||
use crate::sys::c; | ||
use core::mem; | ||
use core::ptr; | ||
|
||
#[cfg(not(target_vendor = "win7"))] | ||
#[inline] | ||
pub fn hashmap_random_keys() -> (u64, u64) { | ||
let mut v = (0, 0); | ||
let ret = unsafe { | ||
c::BCryptGenRandom( | ||
ptr::null_mut(), | ||
core::ptr::addr_of_mut!(v) as *mut u8, | ||
mem::size_of_val(&v) as c::ULONG, | ||
c::BCRYPT_USE_SYSTEM_PREFERRED_RNG, | ||
) | ||
}; | ||
if c::nt_success(ret) { v } else { fallback_rng() } | ||
let ret = unsafe { c::ProcessPrng(ptr::addr_of_mut!(v).cast::<u8>(), mem::size_of_val(&v)) }; | ||
// ProcessPrng is documented as always returning `TRUE`. | ||
// https://learn.microsoft.com/en-us/windows/win32/seccng/processprng#return-value | ||
debug_assert_eq!(ret, c::TRUE); | ||
v | ||
} | ||
|
||
/// Generate random numbers using the fallback RNG function (RtlGenRandom) | ||
/// | ||
/// This is necessary because of a failure to load the SysWOW64 variant of the | ||
/// bcryptprimitives.dll library from code that lives in bcrypt.dll | ||
/// See <https://bugzilla.mozilla.org/show_bug.cgi?id=1788004#c9> | ||
#[cfg(not(target_vendor = "uwp"))] | ||
#[inline(never)] | ||
fn fallback_rng() -> (u64, u64) { | ||
#[cfg(target_vendor = "win7")] | ||
pub fn hashmap_random_keys() -> (u64, u64) { | ||
use crate::ffi::c_void; | ||
use crate::io; | ||
|
||
let mut v = (0, 0); | ||
let ret = unsafe { | ||
c::RtlGenRandom(core::ptr::addr_of_mut!(v) as *mut c_void, mem::size_of_val(&v) as c::ULONG) | ||
c::RtlGenRandom(ptr::addr_of_mut!(v).cast::<c_void>(), mem::size_of_val(&v) as c::ULONG) | ||
}; | ||
|
||
if ret != 0 { v } else { panic!("fallback RNG broken: {}", io::Error::last_os_error()) } | ||
} | ||
|
||
/// We can't use RtlGenRandom with UWP, so there is no fallback | ||
#[cfg(target_vendor = "uwp")] | ||
#[inline(never)] | ||
fn fallback_rng() -> (u64, u64) { | ||
panic!("fallback RNG broken: RtlGenRandom() not supported on UWP"); | ||
if ret != 0 { v } else { panic!("RNG broken: {}", io::Error::last_os_error()) } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.