fix(libkernel_sys): validate guest destination buffer in readlink and readlinkat #13286

Merged
Kikoarl merged 1 commit from upstream-pr/13286 into main 2026-11-05 08:51:40 +00:00
Kikoarl commented 2026-10-30 11:49:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-30T11:49:00Z.


What

In CopyLinkTarget (core/libs/prx/libkernel_sys/Export.cpp), readlink and readlinkat previously only checked if buffer == nullptr before copying target link characters via std::memcpy(buffer, target.data(), count).

If a guest program passed an invalid or unmapped destination pointer (e.g. out of bounds address) with non-zero count, the unchecked std::memcpy caused a host process segmentation fault instead of returning EFAULT (14).

This PR:

  • Validates writes to buffer using GuestCopyOut_nid_no_patch(buffer, target.data(), count).
  • Returns Fail(GuestEfault) (14) if the destination memory is unmapped or protected against writes.
  • Updates debt documentation in docs/dev/debt/libkernel_sys.md.
  • Adds regression unit tests in core/libs/tests/GuestKernelSys.cpp asserting that readlink_nid_postfix and readlinkat_nid_postfix return -1 with errno == 14 when provided an unmapped buffer address (0x800000000000).

Tested

  • Unit tests (guest_kernel_sys): passed 100%.
  • Ran CTest in RAM: ctest --test-dir /tmp/anyps5_build -R "^guest_kernel_sys$" --output-on-failure.
  • Verified conventions: python3 tools/check_conventions.py passed with 0 errors.

Checklist

  • Tested on local hardware
  • Code conventions verified via python3 tools/check_conventions.py
  • AI-assisted: yes
  • Depends on: none
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5083850221 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13286). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-30T11:49:00Z. --- ### What In `CopyLinkTarget` (`core/libs/prx/libkernel_sys/Export.cpp`), `readlink` and `readlinkat` previously only checked if `buffer == nullptr` before copying target link characters via `std::memcpy(buffer, target.data(), count)`. If a guest program passed an invalid or unmapped destination pointer (e.g. out of bounds address) with non-zero count, the unchecked `std::memcpy` caused a host process segmentation fault instead of returning `EFAULT` (`14`). This PR: - Validates writes to `buffer` using `GuestCopyOut_nid_no_patch(buffer, target.data(), count)`. - Returns `Fail(GuestEfault)` (`14`) if the destination memory is unmapped or protected against writes. - Updates debt documentation in `docs/dev/debt/libkernel_sys.md`. - Adds regression unit tests in `core/libs/tests/GuestKernelSys.cpp` asserting that `readlink_nid_postfix` and `readlinkat_nid_postfix` return `-1` with `errno == 14` when provided an unmapped buffer address (`0x800000000000`). ### Tested - Unit tests (`guest_kernel_sys`): passed 100%. - Ran CTest in RAM: `ctest --test-dir /tmp/anyps5_build -R "^guest_kernel_sys$" --output-on-failure`. - Verified conventions: `python3 tools/check_conventions.py` passed with 0 errors. ### Checklist - [x] Tested on local hardware - [x] Code conventions verified via `python3 tools/check_conventions.py` - [x] AI-assisted: yes - [x] Depends on: none
MarcZX10 commented 2026-10-30 11:51:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T11:51:09Z.

Review: APPROVED


Reviewed — looks good

The change routes non-empty readlink and readlinkat results through the existing checked guest-memory copy helper and adds invalid-destination regressions at head 257b6ee53bc423cd5ace35f8393e892d9ba76ab2.

What I ran

Reviewed the exact head above against PR base 8e68a7d7b9f6aeedb0ca03eee115571640555988; the local merge preview against current main 99f7b51d2b49daa8f07fc822c51252ea704a46f0 was clean. Environment: Debian 12 x86-64, Python 3.11.2, Git 2.39.5. I did not compile or run C++ tests.

git diff --check 8e68a7d7b9f6aeedb0ca03eee115571640555988...257b6ee53bc423cd5ace35f8393e892d9ba76ab2
python3 tools/check_conventions.py --base 8e68a7d7b9f6aeedb0ca03eee115571640555988 --head 257b6ee53bc423cd5ace35f8393e892d9ba76ab2 --title 'fix(libkernel_sys): validate guest destination buffer in readlink and readlinkat' --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13286-257b6ee53bc4/pr-body.md
python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13286

Results

Check Result
Diff whitespace Passed; git diff --check produced no output.
Conventions and PR template Passed.
Precheck Diff, conventions, live mergeability passed. ABI and test-quality helper steps were unavailable in its isolated checkout; neither applies to this change. No build-dependent C++ test ran.
Focused guest_kernel_sys CTest The author reports 100% pass using ctest --test-dir /tmp/anyps5_build -R "^guest_kernel_sys$" --output-on-failure; I did not run it.
Hosted CI Pending at final refresh: intake, conventions, labels, eligibility, and progress report.
Unedited local command output
All conventions checks passed.

Findings

Blocking

None found in the reviewed diff.

Non-blocking

The focused C++ test result is author-reported, and hosted checks were still pending at recheck. I verified by source inspection that GuestCopyOut_nid_no_patch checks destination accessibility before copying and that the new regression cases exercise invalid buffers through both APIs.

Sources

Not verified

I did not build or run the C++ test locally, test on a console, or observe the pending hosted CI completion. The precheck could not run its ABI and test-quality helper scripts because those scripts are not present in its isolated checkout.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5711818148 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13286#pullrequestreview-5711818148). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-30T11:51:09Z. **Review:** APPROVED --- # Reviewed — looks good The change routes non-empty `readlink` and `readlinkat` results through the existing checked guest-memory copy helper and adds invalid-destination regressions at head `257b6ee53bc423cd5ace35f8393e892d9ba76ab2`. ## What I ran Reviewed the exact head above against PR base `8e68a7d7b9f6aeedb0ca03eee115571640555988`; the local merge preview against current `main` `99f7b51d2b49daa8f07fc822c51252ea704a46f0` was clean. Environment: Debian 12 x86-64, Python 3.11.2, Git 2.39.5. I did not compile or run C++ tests. ```sh git diff --check 8e68a7d7b9f6aeedb0ca03eee115571640555988...257b6ee53bc423cd5ace35f8393e892d9ba76ab2 python3 tools/check_conventions.py --base 8e68a7d7b9f6aeedb0ca03eee115571640555988 --head 257b6ee53bc423cd5ace35f8393e892d9ba76ab2 --title 'fix(libkernel_sys): validate guest destination buffer in readlink and readlinkat' --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13286-257b6ee53bc4/pr-body.md python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13286 ``` ## Results | Check | Result | |---|---| | Diff whitespace | Passed; `git diff --check` produced no output. | | Conventions and PR template | Passed. | | Precheck | Diff, conventions, live mergeability passed. ABI and test-quality helper steps were unavailable in its isolated checkout; neither applies to this change. No build-dependent C++ test ran. | | Focused `guest_kernel_sys` CTest | The author reports 100% pass using `ctest --test-dir /tmp/anyps5_build -R "^guest_kernel_sys$" --output-on-failure`; I did not run it. | | Hosted CI | Pending at final refresh: intake, conventions, labels, eligibility, and progress report. | <details> <summary>Unedited local command output</summary> ```text All conventions checks passed. ``` </details> ## Findings ### Blocking None found in the reviewed diff. ### Non-blocking The focused C++ test result is author-reported, and hosted checks were still pending at recheck. I verified by source inspection that `GuestCopyOut_nid_no_patch` checks destination accessibility before copying and that the new regression cases exercise invalid buffers through both APIs. ## Sources - [Changed `CopyLinkTarget` and readlink paths at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/257b6ee53bc423cd5ace35f8393e892d9ba76ab2/core/libs/prx/libkernel_sys/Export.cpp#L213-L216). - [Existing checked guest-memory copy implementation at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/257b6ee53bc423cd5ace35f8393e892d9ba76ab2/core/libs/prx/libc/src/General.cpp#L815-L839). - [New `readlink` and `readlinkat` invalid-buffer assertions](https://github.com/boykopovar/AnyPS5/blob/257b6ee53bc423cd5ace35f8393e892d9ba76ab2/core/libs/tests/GuestKernelSys.cpp#L1108-L1129). ## Not verified I did not build or run the C++ test locally, test on a console, or observe the pending hosted CI completion. The precheck could not run its ABI and test-quality helper scripts because those scripts are not present in its isolated checkout.
jotitadev commented 2026-10-30 23:31:42 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Linux run (the thread has a Windows run from maskym; the Tested section doesn't name the OS).

Machine: i7-8700, Ubuntu 26.04, GCC 15.2, Release, BUILD_TESTING=ON, Ninja. Head 257b6ee53b merged (--no-commit) onto main 2a1ae91175.

Check Result
guest_kernel_sys passes 3/3
^guest_kernel_sys (26 tests) 26/26
public_headers passes
libkernel_sys/Export.cpp reverted to main, test kept guest_kernel_sys segfaults 3/3

On Linux GuestCopyOut goes through process_vm_writev, so this is the path the Windows run doesn't exercise; the three new unmapped-buffer cases return EFAULT and the successful reads after them still return the 14-byte target.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5719782741 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13286#pullrequestreview-5719782741). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-30T23:31:42Z. **Review:** COMMENTED --- Linux run (the thread has a Windows run from maskym; the Tested section doesn't name the OS). Machine: i7-8700, Ubuntu 26.04, GCC 15.2, Release, `BUILD_TESTING=ON`, Ninja. Head `257b6ee53b` merged (`--no-commit`) onto `main` `2a1ae91175`. | Check | Result | |---|---| | `guest_kernel_sys` | passes 3/3 | | `^guest_kernel_sys` (26 tests) | 26/26 | | `public_headers` | passes | | `libkernel_sys/Export.cpp` reverted to `main`, test kept | `guest_kernel_sys` segfaults 3/3 | On Linux `GuestCopyOut` goes through `process_vm_writev`, so this is the path the Windows run doesn't exercise; the three new unmapped-buffer cases return `EFAULT` and the successful reads after them still return the 14-byte target. 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!13286
No description provided.