fix(libkernel): return EFAULT from system version queries for invalid memory #13552

Merged
pushkarreddyy merged 5 commits from upstream-pr/13552 into main 2026-10-31 04:10:41 +00:00
pushkarreddyy commented 2026-10-30 18:05:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-30T18:05:52Z.


What

In core/libs/prx/libkernel/System/src/SystemConfiguration.cpp, sceKernelGetSystemSwVersion_nid_postfix, sceKernelGetSystemExVersion, sysKernelGetLowerLimitSysexVersion_nid_postfix, and sysKernelGetLowerLimitUpdVersion_nid_postfix directly wrote to caller-provided guest memory pointers (std::memset(version->versionString, ...), *first = '\0', *version = 0). Passing an unmapped or unwritable pointer caused an unhandled host segmentation fault (SIGSEGV) instead of returning SCE_KERNEL_ERROR_EFAULT (or 14).

This change validates and copies memory using GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch, returning SCE_KERNEL_ERROR_EFAULT (or 14) on copy failure. Also adds regression test coverage in core/libs/tests/GuestSystemConfiguration.cpp and updates technical debt documentation in docs/dev/debt/libkernel.md.

Fixes #13548.

Tested

Windows 11, unit test suite (core/libs/tests/GuestSystemConfiguration.cpp) verifying that passing invalid memory pointers (reinterpret_cast<char*>(0x10), reinterpret_cast<std::uint32_t*>(0x10), and reinterpret_cast<SceKernelSystemSwVersion*>(0x10)) cleanly returns SCE_KERNEL_ERROR_EFAULT or 14.

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: #13548
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5088905745 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13552). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-30T18:05:52Z. --- ### What In `core/libs/prx/libkernel/System/src/SystemConfiguration.cpp`, `sceKernelGetSystemSwVersion_nid_postfix`, `sceKernelGetSystemExVersion`, `sysKernelGetLowerLimitSysexVersion_nid_postfix`, and `sysKernelGetLowerLimitUpdVersion_nid_postfix` directly wrote to caller-provided guest memory pointers (`std::memset(version->versionString, ...)`, `*first = '\0'`, `*version = 0`). Passing an unmapped or unwritable pointer caused an unhandled host segmentation fault (`SIGSEGV`) instead of returning `SCE_KERNEL_ERROR_EFAULT` (or `14`). This change validates and copies memory using `GuestCopyIn_nid_no_patch` and `GuestCopyOut_nid_no_patch`, returning `SCE_KERNEL_ERROR_EFAULT` (or `14`) on copy failure. Also adds regression test coverage in `core/libs/tests/GuestSystemConfiguration.cpp` and updates technical debt documentation in `docs/dev/debt/libkernel.md`. Fixes #13548. ### Tested Windows 11, unit test suite (`core/libs/tests/GuestSystemConfiguration.cpp`) verifying that passing invalid memory pointers (`reinterpret_cast<char*>(0x10)`, `reinterpret_cast<std::uint32_t*>(0x10)`, and `reinterpret_cast<SceKernelSystemSwVersion*>(0x10)`) cleanly returns `SCE_KERNEL_ERROR_EFAULT` or `14`. ### 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 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: #13548 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-30 18:10:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T18:10:58Z.

Review: APPROVED


Reviewed — looks good

I found no blocking defect in the invalid-memory handling on head 6906580bc3f1856f85fe664a10ccd4fbf55ef1b1; the checked-copy paths return the expected errors, and the new cases cover each output pointer.

What I ran

PR base: 4bc9e779aa7abb2109c65ee3bca19e1b85df3be8; merge preview against that current main was clean. Environment: Linux x86_64, Python 3.11.2, GCC 12.2.0.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13552
python3 tools/pr_abi_audit.py --base 4bc9e779aa7abb2109c65ee3bca19e1b85df3be8 --head 6906580bc3f1856f85fe664a10ccd4fbf55ef1b1 --json
git diff --check 4bc9e779aa7abb2109c65ee3bca19e1b85df3be8...refs/review/13552
git grep -n 'guest_system_configuration' refs/review/13552 -- '*CMakeLists.txt' '*cmake'

The precheck's diff and conventions/template checks passed. Its temporary checkout could not find the workspace-local ABI and test-quality helpers; I reran the ABI audit directly, which passed with no findings. The test-quality heuristic warning about registration is a false positive: the CMake target registers guest_system_configuration. The author reports the Windows unit test passed; I did not independently reproduce it.

Sources

Not verified

No local build or CTest was run. At the final refresh, hosted checks were still queued.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5716657589 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13552#pullrequestreview-5716657589). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-30T18:10:58Z. **Review:** APPROVED --- # Reviewed — looks good I found no blocking defect in the invalid-memory handling on head `6906580bc3f1856f85fe664a10ccd4fbf55ef1b1`; the checked-copy paths return the expected errors, and the new cases cover each output pointer. ## What I ran PR base: `4bc9e779aa7abb2109c65ee3bca19e1b85df3be8`; merge preview against that current `main` was clean. Environment: Linux x86_64, Python 3.11.2, GCC 12.2.0. ```sh python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13552 python3 tools/pr_abi_audit.py --base 4bc9e779aa7abb2109c65ee3bca19e1b85df3be8 --head 6906580bc3f1856f85fe664a10ccd4fbf55ef1b1 --json git diff --check 4bc9e779aa7abb2109c65ee3bca19e1b85df3be8...refs/review/13552 git grep -n 'guest_system_configuration' refs/review/13552 -- '*CMakeLists.txt' '*cmake' ``` The precheck's diff and conventions/template checks passed. Its temporary checkout could not find the workspace-local ABI and test-quality helpers; I reran the ABI audit directly, which passed with no findings. The test-quality heuristic warning about registration is a false positive: the CMake target registers `guest_system_configuration`. The author reports the Windows unit test passed; I did not independently reproduce it. ## Sources - [Changed implementation at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/6906580bc3f1856f85fe664a10ccd4fbf55ef1b1/core/libs/prx/libkernel/System/src/SystemConfiguration.cpp#L683-L731) - [Checked guest-copy helpers at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/6906580bc3f1856f85fe664a10ccd4fbf55ef1b1/core/libs/prx/libc/src/General.cpp#L823-L860) - [Registered guest system configuration test](https://github.com/boykopovar/AnyPS5/blob/6906580bc3f1856f85fe664a10ccd4fbf55ef1b1/core/libs/prx/libkernel/CMakeLists.txt#L1588-L1594) ## Not verified No local build or CTest was run. At the final refresh, hosted checks were still queued.
jotitadev commented 2026-10-30 21:31:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-30T21:31:41Z.

Review: COMMENTED


Linux run of head 6906580bc3, merged onto main 6472c708ae (clean merge). Ubuntu 26.04, i7-8700, GCC 15.2, Release, BUILD_TESTING=ON, Ninja.

guest_system_configuration aborts 3/3 on this head. The new EFAULT checks pass. The failure is the existing check right after them:

Require(sysKernelGetUpdVersion_nid_postfix(nullptr) == SCE_KERNEL_ERROR_EINVAL && *__error_nid_postfix() == 13);

errno is 14 there. On Linux, GuestCopyIn_nid_no_patch / GuestCopyOut_nid_no_patch use process_vm_readv / process_vm_writev, which leave the host errno at EFAULT on a bad address, and the guest reads the same errno. The Windows path (VirtualQuery + memcpy) doesn't touch errno, which is probably why it passes there. (Require here is a bare std::abort(), so I found the line by temporarily adding __LINE__ to it.)

Fix I verified: save and restore errno around the copies in SystemConfiguration.cpp, through two small helpers used by the five changed call sites:

static bool CopyOutKeepErrno(void* destination, const void* source, std::size_t bytes) {
    const int saved = errno;
    const bool copied = GuestCopyOut_nid_no_patch(destination, source, bytes);
    errno = saved;
    return copied;
}

(plus the same for GuestCopyIn). With that, guest_system_configuration passes 3/3, and so do guest_kernel_sys_sdk_version and guest_kernel_sys_upd_version. If the console sets errno on these EFAULT returns instead, then the test should reset errno after the new checks. Either way, the two hosts should agree. Without the fix, reverting only SystemConfiguration.cpp to main segfaults 3/3 on the new 0x10 checks, so they do protect the change. public_headers passes.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5718882730 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13552#pullrequestreview-5718882730). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-30T21:31:41Z. **Review:** COMMENTED --- Linux run of head `6906580bc3`, merged onto `main` `6472c708ae` (clean merge). Ubuntu 26.04, i7-8700, GCC 15.2, Release, `BUILD_TESTING=ON`, Ninja. `guest_system_configuration` aborts 3/3 on this head. The new EFAULT checks pass. The failure is the existing check right after them: ```cpp Require(sysKernelGetUpdVersion_nid_postfix(nullptr) == SCE_KERNEL_ERROR_EINVAL && *__error_nid_postfix() == 13); ``` errno is 14 there. On Linux, `GuestCopyIn_nid_no_patch` / `GuestCopyOut_nid_no_patch` use `process_vm_readv` / `process_vm_writev`, which leave the host errno at EFAULT on a bad address, and the guest reads the same errno. The Windows path (`VirtualQuery` + `memcpy`) doesn't touch errno, which is probably why it passes there. (`Require` here is a bare `std::abort()`, so I found the line by temporarily adding `__LINE__` to it.) Fix I verified: save and restore errno around the copies in `SystemConfiguration.cpp`, through two small helpers used by the five changed call sites: ```cpp static bool CopyOutKeepErrno(void* destination, const void* source, std::size_t bytes) { const int saved = errno; const bool copied = GuestCopyOut_nid_no_patch(destination, source, bytes); errno = saved; return copied; } ``` (plus the same for `GuestCopyIn`). With that, `guest_system_configuration` passes 3/3, and so do `guest_kernel_sys_sdk_version` and `guest_kernel_sys_upd_version`. If the console sets errno on these EFAULT returns instead, then the test should reset errno after the new checks. Either way, the two hosts should agree. Without the fix, reverting only `SystemConfiguration.cpp` to `main` segfaults 3/3 on the new `0x10` checks, so they do protect the change. `public_headers` passes. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!13552
No description provided.