From 2a3baba0ccceeb815f73eaff0f0f59b9ac464040 Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:34:21 +0100 Subject: [PATCH] bcrypt_rng: BCryptGenRandom refuses flags the host does not take (BCRYPT-GEN-RANDOM-FLAGS) BCryptGenRandom passed any flags to the host, and the host's refusal (STATUS_INVALID_PARAMETER for every flag but BCRYPT_RNG_USE_ENTROPY_IN_BUFFER and BCRYPT_USE_SYSTEM_PREFERRED_RNG, measured on Windows 11 for 0x4, 0x10 and 0x80000000) fell through to the SystemFunction036 fallback, which ignores flags: the call returned STATUS_SUCCESS with random bytes. The seeded stream ignored them too. The flags are now checked where the host checks them, after the handle and before a zero-byte call is answered. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/bcrypt_rng.cpp | 7 +++++++ runtime/test/bcrypt_rng_test.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/runtime/shims/bcrypt_rng.cpp b/runtime/shims/bcrypt_rng.cpp index d6e8b58a75..9509889c38 100644 --- a/runtime/shims/bcrypt_rng.cpp +++ b/runtime/shims/bcrypt_rng.cpp @@ -197,6 +197,13 @@ NTSTATUS WINAPI gen_random(BCRYPT_ALG_HANDLE hAlgorithm, PUCHAR pbBuffer, ULONG if (!is_rng) return kStatusInvalidHandle; } + // The host takes only these two flags and refuses any other with STATUS_INVALID_PARAMETER, after the handle checks + // above and before a zero-byte call is answered (measured on Windows 11 for 0x4, 0x10 and 0x80000000). Checked here + // because neither the seeded stream nor the SystemFunction036 fallback below reads the flags: the host's refusal + // fell through to that fallback, and a call Windows refuses came back as a success with random bytes. + if (dwFlags & ~static_cast(BCRYPT_RNG_USE_ENTROPY_IN_BUFFER | BCRYPT_USE_SYSTEM_PREFERRED_RNG)) + return kStatusInvalidParameter; + if (cbBuffer == 0) return kStatusSuccess; if (g_seeded_mode) { diff --git a/runtime/test/bcrypt_rng_test.cpp b/runtime/test/bcrypt_rng_test.cpp index ffdd701559..253a59f3a2 100644 --- a/runtime/test/bcrypt_rng_test.cpp +++ b/runtime/test/bcrypt_rng_test.cpp @@ -71,6 +71,31 @@ void test_provider_handles_and_validation() { "BCryptGenRandom rejects null handle without BCRYPT_USE_SYSTEM_PREFERRED_RNG"); check(gen_random(h, buf, sizeof(buf), BCRYPT_USE_SYSTEM_PREFERRED_RNG) == kStatusInvalidParameter, "BCryptGenRandom rejects non-null handle with BCRYPT_USE_SYSTEM_PREFERRED_RNG"); + // Flags the host does not take are STATUS_INVALID_PARAMETER and write nothing, also for zero bytes and next to + // BCRYPT_USE_SYSTEM_PREFERRED_RNG; a bad handle is still reported first (measured on Windows 11). In both modes: + // the seeded stream and the SystemFunction036 fallback do not read the flags. + { + bool refused = true; + for (const ULONG flags : {0x4ul, 0x10ul, 0x80000000ul}) { + std::memset(buf, 0xA5, sizeof(buf)); + const NTSTATUS st = gen_random(h, buf, sizeof(buf), flags); + bool untouched = true; + for (uint8_t b : buf) untouched = untouched && b == 0xA5; + if (st != kStatusInvalidParameter || !untouched) { + std::printf(" flags 0x%lx: status 0x%08lx%s\n", flags, static_cast(st), + untouched ? "" : ", buffer written"); + refused = false; + } + } + check(refused, "BCryptGenRandom refuses unknown flags with STATUS_INVALID_PARAMETER and writes nothing"); + check(gen_random(h, buf, 0, 0x4) == kStatusInvalidParameter, "an unknown flag is refused for zero bytes too"); + check(gen_random(nullptr, buf, sizeof(buf), BCRYPT_USE_SYSTEM_PREFERRED_RNG | 0x4) == kStatusInvalidParameter, + "an unknown flag next to BCRYPT_USE_SYSTEM_PREFERRED_RNG is refused"); + check(gen_random(nullptr, buf, sizeof(buf), 0x4) == kStatusInvalidHandle, + "a null handle with an unknown flag is still STATUS_INVALID_HANDLE, as on the host"); + check(gen_random(h, buf, sizeof(buf), BCRYPT_RNG_USE_ENTROPY_IN_BUFFER) == kStatusSuccess, + "BCRYPT_RNG_USE_ENTROPY_IN_BUFFER is accepted, as on the host"); + } std::memset(buf, 0, sizeof(buf)); check(gen_random(h, buf, sizeof(buf), 0) == kStatusSuccess,