fix(libs): take guest entropy from the host OS instead of std::random_device #4200
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4200"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 defaultstd::random_devicereads the CPU'sRDSEEDinstruction when the CPU has it (32-bit form, up to 100 retries before falling back toRDRAND;random.ccin 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 exportsHostEntropy_nid_no_patch(buffer, size), which usesgetentropyin chunks of at most 256 bytes on Linux, andrand_s(backed byRtlGenRandom) on Windows. On macOS it takesgetentropyfrom<sys/random.h>; CI does not build libc there and I have not built it. If the OS cannot provide entropy it throwsstd::system_error; there is no fallback.On Windows
rand_sneeds no new link dependency, unlikeBCryptGenRandom(only ffmpeg linksbcrypttoday), and the defaultstd::random_devicethere already usedrand_s, which libstdc++ tries beforeRDSEED(GCC 15.2random.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_patchsceRandomGetRandomNumber(libSceRandom):thread_local std::random_device→HostEntropy_nid_no_patch, after the existing null and 64-byte checks/dev/randomand/dev/urandomreads (libkernelRandomDevice.cpp):static std::random_device→HostEntropy_nid_no_patch, still under the descriptor lockstd::_Random_device(libcRuntimeSupport.cpp):static std::random_deviceand the mutex that guarded it →HostEntropy_nid_no_patchsceKernelUuidCreate(libkernelProcess.cpp):std::uniform_int_distribution<std::uint32_t>overstd::random_device→ the four words fromHostEntropy_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: thearc4randomand/dev/random and /dev/urandomentries now say the bytes come from host OS entropy instead ofstd::random_device.Overlaps: #4029 also changes the
thread_local std::random_deviceinarc4random_buf,sceRandomGetRandomNumberandsceKernelUuidCreate; 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/urandomTechnicalDebt entry; whichever merges second needs a small rebase.Not changed here:
sysctl kern.rng_pseudo(SystemConfiguration.cpp) also readsstd::random_device. #3700 is open on that file, so it can follow once #3700 is merged.mkstempand the Windows shared-memory file names also usestd::random_device, but only for names, not for entropy returned to the title.Why
guest_posix_randomfails at line 55 (repeats < 16in 65536arc4randomdraws) 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):std::random_devicewith the"default"and"rdseed"tokensstd::random_device("rdrand")std::random_device("/dev/urandom")std::random_device("getentropy")arc4randomwith this changeBefore the change, on the same CPU model,
arc4randomitself gave 105, 124 and 135 repeats at 23.5 µs per call (run 38184860103). On Windows (WinLibs GCC 15.2.0, local),arc4randomtakes 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
RDSEEDon 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_randompasses 1000/1000 locally (Windows 11, WinLibs GCC 15.2.0 r7, Release), and after rebasing on7a84b222500/500.guest_random,guest_filesystem,guest_dup,guest_file_vector,guest_sce_netandguest_posix_filesystem, which read these sources, pass.guest_app_infochecks thatsceKernelUuidCreaterejects a null pointer, keeps the version 4 and variant bits and returns different UUIDs;guest_crtchecks that nine calls tostd::_Random_devicedo not all return the same value.7a84b222and merged intomain: run 38189128814.guest_posix_random,guest_app_infoandguest_crtpass on Linux and Windows; the failures are the same asmainat that commit in run 38188308120 (agc_driver_prepared_shaders_registered_workgroup_syncandagc_vector_windowon Linux,guest_kernel_stack_protecton Windows).agc_write_tracking,agc_driver_prepared_shaders_registered_workgroup_syncandguest_kernel_stack_protectfail the same way on unmodifiedmain(bca7a29d, the base before the rebase); the other 10 areagc_driver_*Vulkan tests that fail on this GPU onmainas well (#3291).tools/check_conventions.pyandtools/check_test_registration.pypass.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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.
main)core/libs/prx/libScePosixForWebKit/Export.cppL1-2, L175-180core/libs/prx/libSceRandom/Export.cppL7, L20-25core/libs/prx/libkernel/System/src/Process.cppL14-15, L345-346core/libs/prx/libc/CMakeLists.txtL16