fix(libs): take guest entropy from the host OS instead of std::random_device #4200

Merged
winstrex merged 1 commit from upstream-pr/4200 into main 2026-10-12 01:42:53 +00:00
winstrex commented 2026-10-12 01:09:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by winstrex on 2026-10-12T01:09:54Z.


What

Guest entropy now comes from the host OS instead of std::random_device. With libstdc++ on x86 Linux, the default std::random_device reads the CPU's RDSEED instruction when the CPU has it (32-bit form, up to 100 retries before falling back to RDRAND; random.cc in GCC 13.3, selected for the "default" token at lines 380–406), so the quality and speed of every random number a title gets depend on that one instruction on the host CPU. libc exports HostEntropy_nid_no_patch(buffer, size), which uses getentropy in chunks of at most 256 bytes on Linux, and rand_s (backed by RtlGenRandom) on Windows. On macOS it takes getentropy from <sys/random.h>; CI does not build libc there and I have not built it. If the OS cannot provide entropy it throws std::system_error; there is no fallback.

On Windows rand_s needs no new link dependency, unlike BCryptGenRandom (only ffmpeg links bcrypt today), and the default std::random_device there already used rand_s, which libstdc++ tries before RDSEED (GCC 15.2 random.cc, lines 385–391), so Windows behaviour does not change in practice.

Sites changed. Only the entropy source changes; sizes, error codes and the UUID format stay as they were:

  • arc4random, arc4random_buf (libScePosixForWebKit): thread_local std::random_device → HostEntropy_nid_no_patch
  • sceRandomGetRandomNumber (libSceRandom): thread_local std::random_device → HostEntropy_nid_no_patch, after the existing null and 64-byte checks
  • /dev/random and /dev/urandom reads (libkernel RandomDevice.cpp): static std::random_device → HostEntropy_nid_no_patch, still under the descriptor lock
  • std::_Random_device (libc RuntimeSupport.cpp): static std::random_device and the mutex that guarded it → HostEntropy_nid_no_patch
  • sceKernelUuidCreate (libkernel Process.cpp): std::uniform_int_distribution<std::uint32_t> over std::random_device → the four words from HostEntropy_nid_no_patch. Over the full 32-bit range libstdc++'s distribution returns the generator's value unchanged, so this is equivalent; the version and variant bits are set as before.

docs/dev/TechnicalDebt.md: the arc4random and /dev/random and /dev/urandom entries now say the bytes come from host OS entropy instead of std::random_device.

Overlaps: #4029 also changes the thread_local std::random_device in arc4random_buf, sceRandomGetRandomNumber and sceKernelUuidCreate; this PR removes those objects, so if it merges first those three hunks are no longer needed, and if #4029 merges first I will resolve the conflict here. #3849 edits another part of the /dev/random and /dev/urandom TechnicalDebt entry; whichever merges second needs a small rebase.

Not changed here: sysctl kern.rng_pseudo (SystemConfiguration.cpp) also reads std::random_device. #3700 is open on that file, so it can follow once #3700 is merged. mkstemp and the Windows shared-memory file names also use std::random_device, but only for names, not for entropy returned to the title.

Why

guest_posix_random fails at line 55 (repeats < 16 in 65536 arc4random draws) on some Linux CI runners. A correct 32-bit generator gives about 0.5 repeats in 65536 draws, so 16 is essentially never reached by chance. On an AMD EPYC 9V45 (Zen 5) runner, a diagnostic build with this change measured each source in the same job, three trials of 65536 draws each (run 38188337038):

source repeats time per call
std::random_device with the "default" and "rdseed" tokens 93, 110, 133 and 119, 92, 94 23.5–23.7 µs
std::random_device("rdrand") 1, 0, 0 0.03 µs
std::random_device("/dev/urandom") 1, 1, 1 0.7 µs
std::random_device("getentropy") 1, 1, 0 0.5 µs
arc4random with this change 0, 0, 0 0.4 µs

Before the change, on the same CPU model, arc4random itself gave 105, 124 and 135 repeats at 23.5 µs per call (run 38184860103). On Windows (WinLibs GCC 15.2.0, local), arc4random takes about 0.04 µs per call before and after, with 0–1 repeats.

Context only: AMD-SB-7055 (CVE-2025-62626) reports that the 16- and 32-bit forms of RDSEED on Zen 5 can return 0 too often while signalling success. What we measured is different: repeated non-zero values, not zeros, on an Azure VM, where the hypervisor may also play a part. Microcode and kernel updates can mitigate the bulletin's issue on updated systems, so this is not a claim about every Zen 5 machine. The point of the change is that guest entropy should not depend on one CPU instruction at all.

Tested

  • guest_posix_random passes 1000/1000 locally (Windows 11, WinLibs GCC 15.2.0 r7, Release), and after rebasing on 7a84b222 500/500.
  • guest_random, guest_filesystem, guest_dup, guest_file_vector, guest_sce_net and guest_posix_filesystem, which read these sources, pass.
  • New checks where there were none: guest_app_info checks that sceKernelUuidCreate rejects a null pointer, keeps the version 4 and variant bits and returns different UUIDs; guest_crt checks that nine calls to std::_Random_device do not all return the same value.
  • Full CI on my fork, this branch rebased on 7a84b222 and merged into main: run 38189128814. guest_posix_random, guest_app_info and guest_crt pass on Linux and Windows; the failures are the same as main at that commit in run 38188308120 (agc_driver_prepared_shaders_registered_workgroup_sync and agc_vector_window on Linux, guest_kernel_stack_protect on Windows).
  • Full local suite (NVIDIA RTX 5070 Ti): 13 failures. agc_write_tracking, agc_driver_prepared_shaders_registered_workgroup_sync and guest_kernel_stack_protect fail the same way on unmodified main (bca7a29d, the base before the rebase); the other 10 are agc_driver_* Vulkan tests that fail on this GPU on main as well (#3291).

tools/check_conventions.py and tools/check_test_registration.py pass.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823752303 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4200). Originally posted by [winstrex](https://github.com/winstrex) on 2026-10-12T01:09:54Z. --- ### What Guest entropy now comes from the host OS instead of `std::random_device`. With libstdc++ on x86 Linux, the default `std::random_device` reads the CPU's `RDSEED` instruction when the CPU has it (32-bit form, up to 100 retries before falling back to `RDRAND`; [`random.cc` in GCC 13.3](https://github.com/gcc-mirror/gcc/blob/releases/gcc-13.3.0/libstdc%2B%2B-v3/src/c%2B%2B11/random.cc#L113-L141), selected for the `"default"` token at [lines 380–406](https://github.com/gcc-mirror/gcc/blob/releases/gcc-13.3.0/libstdc%2B%2B-v3/src/c%2B%2B11/random.cc#L380-L406)), so the quality and speed of every random number a title gets depend on that one instruction on the host CPU. libc exports `HostEntropy_nid_no_patch(buffer, size)`, which uses `getentropy` in chunks of at most 256 bytes on Linux, and `rand_s` (backed by `RtlGenRandom`) on Windows. On macOS it takes `getentropy` from `<sys/random.h>`; CI does not build libc there and I have not built it. If the OS cannot provide entropy it throws `std::system_error`; there is no fallback. On Windows `rand_s` needs no new link dependency, unlike `BCryptGenRandom` (only ffmpeg links `bcrypt` today), and the default `std::random_device` there already used `rand_s`, which libstdc++ tries before `RDSEED` ([GCC 15.2 `random.cc`, lines 385–391](https://github.com/gcc-mirror/gcc/blob/releases/gcc-15.2.0/libstdc%2B%2B-v3/src/c%2B%2B11/random.cc#L385-L391)), so Windows behaviour does not change in practice. Sites changed. Only the entropy source changes; sizes, error codes and the UUID format stay as they were: - `arc4random`, `arc4random_buf` (libScePosixForWebKit): `thread_local std::random_device` → `HostEntropy_nid_no_patch` - `sceRandomGetRandomNumber` (libSceRandom): `thread_local std::random_device` → `HostEntropy_nid_no_patch`, after the existing null and 64-byte checks - `/dev/random` and `/dev/urandom` reads (libkernel `RandomDevice.cpp`): `static std::random_device` → `HostEntropy_nid_no_patch`, still under the descriptor lock - `std::_Random_device` (libc `RuntimeSupport.cpp`): `static std::random_device` and the mutex that guarded it → `HostEntropy_nid_no_patch` - `sceKernelUuidCreate` (libkernel `Process.cpp`): `std::uniform_int_distribution<std::uint32_t>` over `std::random_device` → the four words from `HostEntropy_nid_no_patch`. Over the full 32-bit range libstdc++'s distribution returns the generator's value unchanged, so this is equivalent; the version and variant bits are set as before. `docs/dev/TechnicalDebt.md`: the `arc4random` and `/dev/random and /dev/urandom` entries now say the bytes come from host OS entropy instead of `std::random_device`. Overlaps: #4029 also changes the `thread_local std::random_device` in `arc4random_buf`, `sceRandomGetRandomNumber` and `sceKernelUuidCreate`; this PR removes those objects, so if it merges first those three hunks are no longer needed, and if #4029 merges first I will resolve the conflict here. #3849 edits another part of the `/dev/random and /dev/urandom` TechnicalDebt entry; whichever merges second needs a small rebase. Not changed here: `sysctl kern.rng_pseudo` (`SystemConfiguration.cpp`) also reads `std::random_device`. #3700 is open on that file, so it can follow once #3700 is merged. `mkstemp` and the Windows shared-memory file names also use `std::random_device`, but only for names, not for entropy returned to the title. ### Why `guest_posix_random` fails at line 55 (`repeats < 16` in 65536 `arc4random` draws) on some Linux CI runners. A correct 32-bit generator gives about 0.5 repeats in 65536 draws, so 16 is essentially never reached by chance. On an AMD EPYC 9V45 (Zen 5) runner, a diagnostic build with this change measured each source in the same job, three trials of 65536 draws each ([run 38188337038](https://github.com/winstrex/AnyPS5/actions/runs/38188337038)): | source | repeats | time per call | |---|---|---| | `std::random_device` with the `"default"` and `"rdseed"` tokens | 93, 110, 133 and 119, 92, 94 | 23.5–23.7 µs | | `std::random_device("rdrand")` | 1, 0, 0 | 0.03 µs | | `std::random_device("/dev/urandom")` | 1, 1, 1 | 0.7 µs | | `std::random_device("getentropy")` | 1, 1, 0 | 0.5 µs | | `arc4random` with this change | 0, 0, 0 | 0.4 µs | Before the change, on the same CPU model, `arc4random` itself gave 105, 124 and 135 repeats at 23.5 µs per call ([run 38184860103](https://github.com/winstrex/AnyPS5/actions/runs/38184860103)). On Windows (WinLibs GCC 15.2.0, local), `arc4random` takes about 0.04 µs per call before and after, with 0–1 repeats. Context only: [AMD-SB-7055](https://www.amd.com/en/resources/product-security/bulletin/amd-sb-7055.html) (CVE-2025-62626) reports that the 16- and 32-bit forms of `RDSEED` on Zen 5 can return 0 too often while signalling success. What we measured is different: repeated non-zero values, not zeros, on an Azure VM, where the hypervisor may also play a part. Microcode and kernel updates can mitigate the bulletin's issue on updated systems, so this is not a claim about every Zen 5 machine. The point of the change is that guest entropy should not depend on one CPU instruction at all. ### Tested - `guest_posix_random` passes 1000/1000 locally (Windows 11, WinLibs GCC 15.2.0 r7, Release), and after rebasing on 7a84b222 500/500. - `guest_random`, `guest_filesystem`, `guest_dup`, `guest_file_vector`, `guest_sce_net` and `guest_posix_filesystem`, which read these sources, pass. - New checks where there were none: `guest_app_info` checks that `sceKernelUuidCreate` rejects a null pointer, keeps the version 4 and variant bits and returns different UUIDs; `guest_crt` checks that nine calls to `std::_Random_device` do not all return the same value. - Full CI on my fork, this branch rebased on 7a84b222 and merged into `main`: [run 38189128814](https://github.com/winstrex/AnyPS5/actions/runs/38189128814). `guest_posix_random`, `guest_app_info` and `guest_crt` pass on Linux and Windows; the failures are the same as `main` at that commit in [run 38188308120](https://github.com/winstrex/AnyPS5/actions/runs/38188308120) (`agc_driver_prepared_shaders_registered_workgroup_sync` and `agc_vector_window` on Linux, `guest_kernel_stack_protect` on Windows). - Full local suite (NVIDIA RTX 5070 Ti): 13 failures. `agc_write_tracking`, `agc_driver_prepared_shaders_registered_workgroup_sync` and `guest_kernel_stack_protect` fail the same way on unmodified `main` (bca7a29d, the base before the rebase); the other 10 are `agc_driver_*` Vulkan tests that fail on this GPU on `main` as well (#3291). `tools/check_conventions.py` and `tools/check_test_registration.py` pass. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-12 01:18:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T01:18:14Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4029 core/libs/prx/libScePosixForWebKit/Export.cpp L1-2, L175-180
core/libs/prx/libSceRandom/Export.cpp L7, L20-25
core/libs/prx/libkernel/System/src/Process.cpp L14-15, L345-346
#4129 core/libs/prx/libc/CMakeLists.txt L16
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116044401 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4200#issuecomment-6116044401). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T01:18:14Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4029 | `core/libs/prx/libScePosixForWebKit/Export.cpp` [L1-2](https://github.com/boykopovar/AnyPS5/blob/695b8e552c5d0e061572a9d6c6cec73e8352adac/core/libs/prx/libScePosixForWebKit/Export.cpp#L1-L2), [L175-180](https://github.com/boykopovar/AnyPS5/blob/695b8e552c5d0e061572a9d6c6cec73e8352adac/core/libs/prx/libScePosixForWebKit/Export.cpp#L175-L180)<br>`core/libs/prx/libSceRandom/Export.cpp` [L7](https://github.com/boykopovar/AnyPS5/blob/695b8e552c5d0e061572a9d6c6cec73e8352adac/core/libs/prx/libSceRandom/Export.cpp#L7), [L20-25](https://github.com/boykopovar/AnyPS5/blob/695b8e552c5d0e061572a9d6c6cec73e8352adac/core/libs/prx/libSceRandom/Export.cpp#L20-L25)<br>`core/libs/prx/libkernel/System/src/Process.cpp` [L14-15](https://github.com/boykopovar/AnyPS5/blob/695b8e552c5d0e061572a9d6c6cec73e8352adac/core/libs/prx/libkernel/System/src/Process.cpp#L14-L15), [L345-346](https://github.com/boykopovar/AnyPS5/blob/695b8e552c5d0e061572a9d6c6cec73e8352adac/core/libs/prx/libkernel/System/src/Process.cpp#L345-L346) | | | | #4129 | `core/libs/prx/libc/CMakeLists.txt` [L16](https://github.com/boykopovar/AnyPS5/blob/695b8e552c5d0e061572a9d6c6cec73e8352adac/core/libs/prx/libc/CMakeLists.txt#L16) | | |
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4200
No description provided.