fix(libSceRandom): return INVALID from sceRandomGetRandomNumber for an unwritable buffer #14980

Merged
fogydes merged 3 commits from upstream-pr/14980 into main 2026-11-06 01:25:08 +00:00
fogydes commented 2026-11-02 07:45:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-11-02T07:45:46Z.


What

sceRandomGetRandomNumber passed the caller buffer directly to HostEntropy_nid_no_patch, which executed std::memcpy on unvalidated memory. Calling the export with an unwritable destination pointer caused an unhandled host access violation (0xc0000005).

Random bytes are now staged in a local stack buffer and copied to guest memory through GuestCopyOut_nid_no_patch preserving errno, returning SCE_RANDOM_ERROR_INVALID (0x817C0016) when copy-out fails as a defensive host-protection fallback.

Tested

  • Windows 11 Enterprise (x86_64)
  • Unit test assertion added to core/libs/tests/GuestRandom.cpp:
    • sceRandomGetRandomNumber(0x10, 16) == SCE_RANDOM_ERROR_INVALID (reproduced unhandled host access violation 0xc0000005 on main, passing after fix)
  • CTest suite guest_random (Test #2547) passed 100% (0.02 s)
  • python tools/check_conventions.py --base origin/main passed cleanly with 0 findings
  • python tools/pr_overlap.py --branch fix/libSceRandom-pointer-validation verified 0 overlaps across 1,915 open PRs

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5116797662 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14980). Originally posted by [fogydes](https://github.com/fogydes) on 2026-11-02T07:45:46Z. --- ### What `sceRandomGetRandomNumber` passed the caller buffer directly to `HostEntropy_nid_no_patch`, which executed `std::memcpy` on unvalidated memory. Calling the export with an unwritable destination pointer caused an unhandled host access violation (`0xc0000005`). Random bytes are now staged in a local stack buffer and copied to guest memory through `GuestCopyOut_nid_no_patch` preserving `errno`, returning `SCE_RANDOM_ERROR_INVALID` (`0x817C0016`) when copy-out fails as a defensive host-protection fallback. ### Tested - Windows 11 Enterprise (x86_64) - Unit test assertion added to `core/libs/tests/GuestRandom.cpp`: - `sceRandomGetRandomNumber(0x10, 16) == SCE_RANDOM_ERROR_INVALID` (reproduced unhandled host access violation `0xc0000005` on main, passing after fix) - CTest suite `guest_random` (Test #2547) passed 100% (0.02 s) - `python tools/check_conventions.py --base origin/main` passed cleanly with 0 findings - `python tools/pr_overlap.py --branch fix/libSceRandom-pointer-validation` verified 0 overlaps across 1,915 open PRs ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
BunnyFl commented 2026-11-02 10:51:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T10:51:11Z.

Review: COMMENTED


A firmware note for the debt entry, not a blocker. The checked copy does keep the host from faulting.

Firmware 13.42's libSceRandom.sprx (sha256 700a43a95c4eb9bea260339978c00c2be5075b35d34bdfbec0a48dd07b15f1b7) at 0xd0 (PI7jIZj4pcE), read to the return, from disassembly only, not measured on a console:

11c: test r14, r14 ... 13b: cmp rbx, 0x41 ; 144: mov eax, 0x817c0016   ; null buffer or size >= 65
166: call sysctlbyname ; 16d: mov eax, 0x817c00ff on failure
17a: lea rsi, [rbp-0x80] ; mov rdi, r14 ; mov rdx, rbx
184: call memcpy ; 189: xor eax, eax         ; result unchecked, returns 0

The module tests only for null, then stores straight through the pointer. There's no syscall, copyout or kernel query before the store. So for a non-null pointer it can't write, the console faults rather than returning a code, and main already matches that.

The debt text calls the console behaviour unmeasured, but the module settles it. Could the entry say what happens instead? For example: "Firmware 13.42 checks only for null and stores directly, so the console faults on an unwritable non-null pointer; AnyPS5 returns INVALID (0x817C0016) instead." That matches how the libkernel entries word their "returns EFAULT here, where the console faults" fallbacks. The new test lines then pin a deliberate host-side fallback, which is fine once the entry says so.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5735433026 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14980#pullrequestreview-5735433026). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T10:51:11Z. **Review:** COMMENTED --- A firmware note for the debt entry, not a blocker. The checked copy does keep the host from faulting. Firmware 13.42's `libSceRandom.sprx` (sha256 `700a43a95c4eb9bea260339978c00c2be5075b35d34bdfbec0a48dd07b15f1b7`) at 0xd0 (`PI7jIZj4pcE`), read to the return, from disassembly only, not measured on a console: ``` 11c: test r14, r14 ... 13b: cmp rbx, 0x41 ; 144: mov eax, 0x817c0016 ; null buffer or size >= 65 166: call sysctlbyname ; 16d: mov eax, 0x817c00ff on failure 17a: lea rsi, [rbp-0x80] ; mov rdi, r14 ; mov rdx, rbx 184: call memcpy ; 189: xor eax, eax ; result unchecked, returns 0 ``` The module tests only for null, then stores straight through the pointer. There's no syscall, `copyout` or kernel query before the store. So for a non-null pointer it can't write, the console faults rather than returning a code, and `main` already matches that. The debt text calls the console behaviour unmeasured, but the module settles it. Could the entry say what happens instead? For example: "Firmware 13.42 checks only for null and stores directly, so the console faults on an unwritable non-null pointer; AnyPS5 returns INVALID (0x817C0016) instead." That matches how the libkernel entries word their "returns EFAULT here, where the console faults" fallbacks. The new test lines then pin a deliberate host-side fallback, which is fine once the entry says so.
BunnyFl commented 2026-11-02 14:46:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T14:46:19Z.

Review: APPROVED


Thanks, @fogydes. The debt entry now says what firmware 13.42 does (null check only, direct store, so the console faults) and where AnyPS5 deliberately differs, with the evidence wording kept. The code is unchanged since my last read.

One optional wording point: the entry still calls returning INVALID for a null buffer of size 0 'an assumption, not measured on hardware'. The module settles that case too. At 0x11c it tests the buffer for null before the size check, so a null buffer returns 0x817c0016 whatever the size (read from disassembly, not measured on a console). That agrees with this library and fpPS4, not with shadPS4 or KytyPS5.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5738321781 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14980#pullrequestreview-5738321781). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T14:46:19Z. **Review:** APPROVED --- Thanks, @fogydes. The debt entry now says what firmware 13.42 does (null check only, direct store, so the console faults) and where AnyPS5 deliberately differs, with the evidence wording kept. The code is unchanged since my last read. One optional wording point: the entry still calls returning `INVALID` for a null buffer of size 0 'an assumption, not measured on hardware'. The module settles that case too. At `0x11c` it tests the buffer for null before the size check, so a null buffer returns `0x817c0016` whatever the size (read from disassembly, not measured on a console). That agrees with this library and fpPS4, not with shadPS4 or KytyPS5.
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!14980
No description provided.