[minipal] Add getentropy() fallback for platforms without getrandom()#126481
Open
lewing wants to merge 1 commit intodotnet:mainfrom
Open
[minipal] Add getentropy() fallback for platforms without getrandom()#126481lewing wants to merge 1 commit intodotnet:mainfrom
lewing wants to merge 1 commit intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a WASI-specific entropy source to minipal so managed cryptography can obtain secure random bytes on WASI targets.
Changes:
- Add a
__wasi__branch inminipal_get_cryptographically_secure_random_bytes. - Use
getentropy()(chunked to 256 bytes per call) as the secure RNG source on WASI.
am11
reviewed
Apr 2, 2026
6ddf8f9 to
819ec1c
Compare
Member
Author
|
Updated per @am11's suggestion — now uses CMake |
Add getentropy() as a fallback after getrandom() but before /dev/urandom. Uses CMake feature detection (check_symbol_exists) rather than hardcoded platform checks. Fallback chain: arc4random → BCrypt → getrandom → getentropy → /dev/urandom This covers WASI (via wasi-libc → __wasi_random_get()) and any other platform that provides getentropy() but not getrandom(). Fixes dotnet#126480
819ec1c to
b18ad00
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds
getentropy()as a fallback inminipal/random.cfor platforms that provide it but lackgetrandom()— notably WASI (via wasi-libc →__wasi_random_get()).Changes
random.c— Add#elif HAVE_GETENTROPYfallback betweenHAVE_BCRYPT_HandHAVE_GETRANDOMin the RNG chainconfigure.cmake— Addcheck_symbol_exists(getentropy "unistd.h" HAVE_GETENTROPY)minipalconfig.h.in— Add#cmakedefine01 HAVE_GETENTROPYMotivation
NativeAOT-LLVM targeting
wasi-wasmcurrently fails atRandomNumberGenerator.Fill()withPlatformNotSupportedExceptionbecauseminipal_get_cryptographically_random_bytesreturns-1—getrandom()returnsENOSYSand/dev/urandomdoesn't exist on WASI.However, wasi-libc already provides
getentropy()backed by__wasi_random_get(). The C runtime startup already uses this path (arc4random_buf→getentropy→__wasi_random_get) for stack canaries, so the symbol is linked and functional.Uses CMake feature detection (
check_symbol_exists) rather than a hardcoded__wasi__check, so this also covers other platforms withgetentropy()but notgetrandom().Fixes #126480