Skip to content

[minipal] Add getentropy() fallback for platforms without getrandom()#126481

Open
lewing wants to merge 1 commit intodotnet:mainfrom
lewing:wasi-random-getentropy
Open

[minipal] Add getentropy() fallback for platforms without getrandom()#126481
lewing wants to merge 1 commit intodotnet:mainfrom
lewing:wasi-random-getentropy

Conversation

@lewing
Copy link
Copy Markdown
Member

@lewing lewing commented Apr 2, 2026

Adds getentropy() as a fallback in minipal/random.c for platforms that provide it but lack getrandom() — notably WASI (via wasi-libc → __wasi_random_get()).

Changes

  • random.c — Add #elif HAVE_GETENTROPY fallback between HAVE_BCRYPT_H and HAVE_GETRANDOM in the RNG chain
  • configure.cmake — Add check_symbol_exists(getentropy "unistd.h" HAVE_GETENTROPY)
  • minipalconfig.h.in — Add #cmakedefine01 HAVE_GETENTROPY

Motivation

NativeAOT-LLVM targeting wasi-wasm currently fails at RandomNumberGenerator.Fill() with PlatformNotSupportedException because minipal_get_cryptographically_random_bytes returns -1getrandom() returns ENOSYS and /dev/urandom doesn't exist on WASI.

However, wasi-libc already provides getentropy() backed by __wasi_random_get(). The C runtime startup already uses this path (arc4random_bufgetentropy__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 with getentropy() but not getrandom().

Fixes #126480

Copilot AI review requested due to automatic review settings April 2, 2026 18:26
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in minipal_get_cryptographically_secure_random_bytes.
  • Use getentropy() (chunked to 256 bytes per call) as the secure RNG source on WASI.

@lewing
Copy link
Copy Markdown
Member Author

lewing commented Apr 2, 2026

Updated per @am11's suggestion — now uses CMake HAVE_GETENTROPY feature detection instead of __wasi__. Added check_symbol_exists(getentropy "unistd.h" HAVE_GETENTROPY) to configure.cmake and the corresponding #cmakedefine01 to minipalconfig.h.in. The random.c fallback chain is now: arc4random_bufBCryptgetentropygetrandom/dev/urandom.

@lewing lewing changed the title [WASI] Use getentropy() for crypto random on WASI targets [minipal] Add getentropy() fallback for platforms without getrandom() Apr 2, 2026
@lewing lewing marked this pull request as ready for review April 2, 2026 22:49
Copilot AI review requested due to automatic review settings April 2, 2026 22:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

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
@lewing lewing force-pushed the wasi-random-getentropy branch from 819ec1c to b18ad00 Compare April 2, 2026 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[WASI] RandomNumberGenerator.Fill throws PlatformNotSupportedException — missing getentropy/random_get wiring

3 participants