fix(kernel): safely copy guest path arguments #9423

Merged
AliZedan1A merged 2 commits from upstream-pr/9423 into main 2026-10-25 09:48:00 +00:00
AliZedan1A commented 2026-10-21 21:30:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AliZedan1A on 2026-10-21T21:30:19Z.


What

Snapshot guest path strings through checked host reads before syscall wrappers inspect or resolve them. Unreadable pointers now return guest EFAULT, unterminated 1024-byte paths return ENAMETOOLONG, and two-path calls preserve source-before-destination validation order. Add cross-platform guard-page coverage for boundary lengths, relative *at calls, error precedence, and filesystem side effects.

The overlap check reports line conflicts with several unrelated filesystem PRs. In particular, #8723 adds a symlink target length check that is also enforced by the checked 1024-byte snapshot here; this PR does not depend on it. #9442 will follow this PR and retain checked path copying in its shared chflags/lchflags helper.

Tested

Linux x86-64, head f552230ad rebased onto main 8b3e39832:

  • Built guest_kernel_sys_tests, guest_filesystem_tests, guest_path_errors_tests, guest_kernel_rename_tests, guest_rename_dot_tests, guest_posix_scalar_io_tests, and agc_driver_storage_file_write_tests with GCC 16.2.1, Release, BUILD_TESTING=ON.
  • ctest --test-dir build-pr-9423 -R "^guest_(filesystem|path_errors|kernel_rename|rename_dot|posix_scalar_io|kernel_sys)$" --repeat until-fail:3 --output-on-failure: 6/6 passed on all three runs.
  • agc_driver_storage_file_write passed on all three runs with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation on an NVIDIA GeForce RTX 3050 Laptop GPU. The verbose log had no missing-layer message or VUID- validation errors.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch fix/guest-path-efault: refreshed; only known shared-file overlaps remain.
  • git diff --check origin/main...HEAD: passed.
  • Not rerun on Windows after the rebase. Windows results reported in the discussion apply to the previous head 4d7b47abe7 and old base.

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: Fixes #9368
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3050 Laptop GPU (AGC regression test)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4958633977 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9423). Originally posted by [AliZedan1A](https://github.com/AliZedan1A) on 2026-10-21T21:30:19Z. --- ### What Snapshot guest path strings through checked host reads before syscall wrappers inspect or resolve them. Unreadable pointers now return guest `EFAULT`, unterminated 1024-byte paths return `ENAMETOOLONG`, and two-path calls preserve source-before-destination validation order. Add cross-platform guard-page coverage for boundary lengths, relative `*at` calls, error precedence, and filesystem side effects. The overlap check reports line conflicts with several unrelated filesystem PRs. In particular, #8723 adds a symlink target length check that is also enforced by the checked 1024-byte snapshot here; this PR does not depend on it. #9442 will follow this PR and retain checked path copying in its shared `chflags`/`lchflags` helper. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux x86-64, head `f552230ad` rebased onto `main` `8b3e39832`: - Built `guest_kernel_sys_tests`, `guest_filesystem_tests`, `guest_path_errors_tests`, `guest_kernel_rename_tests`, `guest_rename_dot_tests`, `guest_posix_scalar_io_tests`, and `agc_driver_storage_file_write_tests` with GCC 16.2.1, Release, `BUILD_TESTING=ON`. - `ctest --test-dir build-pr-9423 -R "^guest_(filesystem|path_errors|kernel_rename|rename_dot|posix_scalar_io|kernel_sys)$" --repeat until-fail:3 --output-on-failure`: 6/6 passed on all three runs. - `agc_driver_storage_file_write` passed on all three runs with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` on an NVIDIA GeForce RTX 3050 Laptop GPU. The verbose log had no missing-layer message or `VUID-` validation errors. - `python3 tools/check_conventions.py --base origin/main`: passed. - `python3 tools/pr_overlap.py --branch fix/guest-path-efault`: refreshed; only known shared-file overlaps remain. - `git diff --check origin/main...HEAD`: passed. - Not rerun on Windows after the rebase. Windows results reported in the discussion apply to the previous head `4d7b47abe7` and old base. ### 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: Fixes #9368 - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 3050 Laptop GPU (AGC regression test) - [x] AI-assisted: yes
ycbuzi commented 2026-10-21 22:41:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T22:41:38Z.


Windows run of 4d7b47abe7 merged onto main a6dcea9279 (3393625503, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

Built libc, libkernel, libkernel_sys and guest_kernel_sys_tests with no errors or warnings. ctest --repeat until-fail:2 on guest_kernel_sys: passes on both repeats. Not run: a title.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6328162283 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9423#issuecomment-6328162283). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T22:41:38Z. --- Windows run of 4d7b47abe7 merged onto `main` a6dcea9279 (3393625503, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, `BUILD_TESTING=ON`. Built `libc`, `libkernel`, `libkernel_sys` and `guest_kernel_sys_tests` with no errors or warnings. `ctest --repeat until-fail:2` on `guest_kernel_sys`: passes on both repeats. Not run: a title.
wachi35676 commented 2026-10-21 23:40:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-21T23:40:08Z.


Windows run of the wider related set, a negative check, and the path exports this doesn't cover yet.

Head 4d7b47abe7 merged onto main a6dcea927 (clean). Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON.

  • guest_kernel_sys, guest_filesystem, guest_kernel_rename, guest_rename_dot, guest_posix_scalar_io: 5/5, 3 runs. (guest_path_errors is UNIX-only.)
  • check_conventions.py --base a6dcea927: passes.

Probe, with an unreadable page (VirtualAlloc PAGE_NOACCESS) as the path:

Call PR library sources from main
stat -1, EFAULT access violation (0xc0000005)
access (libc Filesystem.cpp) access violation access violation
chdir (libc General.cpp) access violation access violation
remove (libc Filesystem.cpp) access violation access violation
opendir (libc Directory.cpp) access violation access violation

So the fix works where it is applied. The libc path exports access, chdir, remove, opendir and realpath still read the guest string directly. That may be out of scope here, but the title promises guest path arguments in general.

Cost: 20,000 stat calls on an existing file average 85-90 µs on the PR and 84-86 µs on main (14-byte path), and 286-312 µs vs 306-310 µs on a 200-byte missing path, three rounds each. That is within run-to-run noise, so the ReadProcessMemory copy doesn't show.

On the claims: 1024 bytes without a NUL gives ENAMETOOLONG and 1023 bytes is accepted, as FreeBSD's namei copyinstr(..., MAXPATHLEN) does. The new order in link and renameat (source looked up before the destination string is copied) matches kern_linkat and kern_renameat. The copy is per page, so a string that ends right before an unreadable page is accepted (the test's Boundary case passes here).

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329018108 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9423#issuecomment-6329018108). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-21T23:40:08Z. --- Windows run of the wider related set, a negative check, and the path exports this doesn't cover yet. Head `4d7b47abe7` merged onto main `a6dcea927` (clean). Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0 (MinGW-w64 UCRT), Release, `BUILD_TESTING=ON`. - `guest_kernel_sys`, `guest_filesystem`, `guest_kernel_rename`, `guest_rename_dot`, `guest_posix_scalar_io`: 5/5, 3 runs. (`guest_path_errors` is UNIX-only.) - `check_conventions.py --base a6dcea927`: passes. Probe, with an unreadable page (`VirtualAlloc` `PAGE_NOACCESS`) as the path: | Call | PR | library sources from main | |---|---|---| | `stat` | -1, `EFAULT` | access violation (`0xc0000005`) | | `access` (libc `Filesystem.cpp`) | access violation | access violation | | `chdir` (libc `General.cpp`) | access violation | access violation | | `remove` (libc `Filesystem.cpp`) | access violation | access violation | | `opendir` (libc `Directory.cpp`) | access violation | access violation | So the fix works where it is applied. The libc path exports `access`, `chdir`, `remove`, `opendir` and `realpath` still read the guest string directly. That may be out of scope here, but the title promises guest path arguments in general. Cost: 20,000 `stat` calls on an existing file average 85-90 µs on the PR and 84-86 µs on main (14-byte path), and 286-312 µs vs 306-310 µs on a 200-byte missing path, three rounds each. That is within run-to-run noise, so the `ReadProcessMemory` copy doesn't show. On the claims: 1024 bytes without a NUL gives `ENAMETOOLONG` and 1023 bytes is accepted, as FreeBSD's `namei` `copyinstr(..., MAXPATHLEN)` does. The new order in `link` and `renameat` (source looked up before the destination string is copied) matches `kern_linkat` and `kern_renameat`. The copy is per page, so a string that ends right before an unreadable page is accepted (the test's `Boundary` case passes here). AI-assisted: yes (Claude Code); results above were run on this machine.
MarcZX10 commented 2026-10-22 01:36:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T01:36:10Z.


The current overlap scan and manual diff review show that #9442 refactors chflags in Stdio.cpp while #9423 changes the shared PathError contract and updates chflags to copy guest path input. Please coordinate with #9442’s author on order and confirm that the final code retains both checked path copying and the new no-follow lchflags path; record a dependency if one must land first.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330556916 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9423#issuecomment-6330556916). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T01:36:10Z. --- The current overlap scan and manual diff review show that #9442 refactors `chflags` in `Stdio.cpp` while #9423 changes the shared `PathError` contract and updates `chflags` to copy guest path input. Please coordinate with #9442’s author on order and confirm that the final code retains both checked path copying and the new no-follow `lchflags` path; record a dependency if one must land first.
MarcZX10 commented 2026-10-22 04:57:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:57:04Z.

Review: DISMISSED


The focused guard-page coverage and the Windows/Linux runs reported on this PR are useful, but this branch is still based on merge base 0c6a5bd5938f; current main is 524398151f1e. GitHub reports the comparison as diverged, 417 commits behind and 1 ahead. The repository contribution rules require PR branches to stay current with main, and the hosted checks shown here are only conventions, intake and labels; no product build/test run is reported for this head.

Please rebase onto current main, refresh the overlap report and resolve any remaining shared-file ordering with the affected authors (including the existing Stdio.cpp/chflags coordination with #9442), then rerun the focused path tests on the rebased head and update the tested-base/CI report. The already reported Linux and Windows results are on the old base and should remain identified as such. I did not run local tests in this pass because there is no matching configured build for this PR head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609444005 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9423#pullrequestreview-5609444005). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T04:57:04Z. **Review:** DISMISSED --- The focused guard-page coverage and the Windows/Linux runs reported on this PR are useful, but this branch is still based on merge base `0c6a5bd5938f`; current `main` is `524398151f1e`. GitHub reports the comparison as diverged, 417 commits behind and 1 ahead. The repository contribution rules require PR branches to stay current with `main`, and the hosted checks shown here are only conventions, intake and labels; no product build/test run is reported for this head. Please rebase onto current `main`, refresh the overlap report and resolve any remaining shared-file ordering with the affected authors (including the existing `Stdio.cpp`/`chflags` coordination with #9442), then rerun the focused path tests on the rebased head and update the tested-base/CI report. The already reported Linux and Windows results are on the old base and should remain identified as such. I did not run local tests in this pass because there is no matching configured build for this PR head.
Gruzzberry commented 2026-10-22 06:49:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-22T06:49:08Z.


#9442 (lchflags) moves the body of chflags_nid_postfix in Stdio.cpp into a shared ChangeFileFlags_nid_no_patch(function, path, flags, follow), so it overlaps the PathError change here. I suggest this PR lands first, since it covers many callers; I will then rebase #9442 so the helper keeps the copied path (std::string copy; PathError(path, copy); path = copy.c_str();) for both chflags and lchflags. If #9442 happens to land first, the change on your side is that one call moved into the helper. Does that order work for you? (#9469 changes LookupError next to it, not chflags, so it does not overlap this PR.)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6334632602 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9423#issuecomment-6334632602). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-22T06:49:08Z. --- #9442 (`lchflags`) moves the body of `chflags_nid_postfix` in `Stdio.cpp` into a shared `ChangeFileFlags_nid_no_patch(function, path, flags, follow)`, so it overlaps the `PathError` change here. I suggest this PR lands first, since it covers many callers; I will then rebase #9442 so the helper keeps the copied path (`std::string copy; PathError(path, copy); path = copy.c_str();`) for both `chflags` and `lchflags`. If #9442 happens to land first, the change on your side is that one call moved into the helper. Does that order work for you? (#9469 changes `LookupError` next to it, not `chflags`, so it does not overlap this PR.)
Gruzzberry commented 2026-10-22 11:49:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-22T11:49:34Z.


Update: #9442 and #9693 will both go after this PR. #9693 now uses CopyGuestPath_nid_no_patch for the setlogin name, so it carries this PR's commit until it merges; nothing is needed here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6340012152 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9423#issuecomment-6340012152). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-22T11:49:34Z. --- Update: #9442 and #9693 will both go after this PR. #9693 now uses `CopyGuestPath_nid_no_patch` for the `setlogin` name, so it carries this PR's commit until it merges; nothing is needed here.
Gruzzberry commented 2026-10-22 12:09:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-22T12:09:42Z.


A Windows regression I hit while stacking #9693 on this PR: agc_driver_storage_file_write passes on main 8b3a6ba88 and fails with only this PR's commit on top (3 runs out of 3; Windows 11, MinGW-w64 GCC 15.2.0 r7, Release). It stops in CheckShutdownAndGuard (core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp) with the uncaught exception file source read guard.

The test installs AgcDriver::GuestMemory::SetReadGuard with a guard that throws for any read, then calls file.Open() before its try around sceKernelWrite. With this PR, open copies the path through CopyGuestBytes, which calls GuestArenaPrepareHostRead_nid_no_patch, so the AGC read hook now runs for the path copy and the guard throws out of Open(). On main the path is not read through that hook, so the guard only meets the sceKernelWrite source as intended.

Not checked: Linux. Full suite with this PR plus #9693's two setlogin commits (which only touch the login code) on main 8b3a6ba88: 1892 tests, the same results as main apart from this one.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6340382146 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9423#issuecomment-6340382146). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-22T12:09:42Z. --- A Windows regression I hit while stacking #9693 on this PR: `agc_driver_storage_file_write` passes on `main` 8b3a6ba88 and fails with only this PR's commit on top (3 runs out of 3; Windows 11, MinGW-w64 GCC 15.2.0 r7, Release). It stops in `CheckShutdownAndGuard` (`core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp`) with the uncaught exception `file source read guard`. The test installs `AgcDriver::GuestMemory::SetReadGuard` with a guard that throws for any read, then calls `file.Open()` before its `try` around `sceKernelWrite`. With this PR, `open` copies the path through `CopyGuestBytes`, which calls `GuestArenaPrepareHostRead_nid_no_patch`, so the AGC read hook now runs for the path copy and the guard throws out of `Open()`. On `main` the path is not read through that hook, so the guard only meets the `sceKernelWrite` source as intended. Not checked: Linux. Full suite with this PR plus #9693's two `setlogin` commits (which only touch the login code) on `main` 8b3a6ba88: 1892 tests, the same results as `main` apart from this one.
AliZedan1A commented 2026-10-22 13:58:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AliZedan1A on 2026-10-22T13:58:48Z.


@MarcZX10 Rebased onto main 8b3e39832 and pushed head f552230ad. The agc_driver_storage_file_write regression is fixed by opening the output before installing the source-read guard, while checked path reads continue through the host-read hook. The six focused path tests and the AGC regression test each passed three runs on Linux; conventions, overlap, and diff checks pass. The PR description now records the exact commands, base, GPU, and validation result. Ready for re-review.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6342595425 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9423#issuecomment-6342595425). Originally posted by [AliZedan1A](https://github.com/AliZedan1A) on 2026-10-22T13:58:48Z. --- @MarcZX10 Rebased onto `main` `8b3e39832` and pushed head `f552230ad`. The `agc_driver_storage_file_write` regression is fixed by opening the output before installing the source-read guard, while checked path reads continue through the host-read hook. The six focused path tests and the AGC regression test each passed three runs on Linux; conventions, overlap, and diff checks pass. The PR description now records the exact commands, base, GPU, and validation result. Ready for re-review.
adriadam10 commented 2026-10-22 23:58:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-22T23:58:09Z.

Review: CHANGES_REQUESTED


Head 4e2119bfb still merges cleanly onto current main, with one gap: main now has lchmod (#9200), and lchmod_nid_postfix in core/libs/prx/libkernel_sys/Export.cpp (~line 579) dereferences *path and passes the raw pointer to ResolvePathError_nid_no_patch before calling fchmodat_nid_postfix. An unreadable non-null pointer faults the host there, which is what this PR closes for the other path wrappers.

  • Copy the path with CopyPath at the top of lchmod_nid_postfix (return Fail(error)), and use the copy for the empty-string check and the resolve.
  • Add an lchmod row to the guard-page test in GuestKernelSys.cpp.

access in libc/Filesystem.cpp stays out of scope as agreed (#9368 stays open for it). @MarcZX10 your change request was about the rebase and the #9442/#9693 order, which are done; once the lchmod point is pushed, could you re-review?

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5622353295 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9423#pullrequestreview-5622353295). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-22T23:58:09Z. **Review:** CHANGES_REQUESTED --- Head `4e2119bfb` still merges cleanly onto current `main`, with one gap: `main` now has `lchmod` (#9200), and `lchmod_nid_postfix` in `core/libs/prx/libkernel_sys/Export.cpp` (~line 579) dereferences `*path` and passes the raw pointer to `ResolvePathError_nid_no_patch` before calling `fchmodat_nid_postfix`. An unreadable non-null pointer faults the host there, which is what this PR closes for the other path wrappers. - Copy the path with `CopyPath` at the top of `lchmod_nid_postfix` (return `Fail(error)`), and use the copy for the empty-string check and the resolve. - Add an `lchmod` row to the guard-page test in `GuestKernelSys.cpp`. `access` in `libc/Filesystem.cpp` stays out of scope as agreed (#9368 stays open for it). @MarcZX10 your change request was about the rebase and the #9442/#9693 order, which are done; once the `lchmod` point is pushed, could you re-review?
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!9423
No description provided.