fix(libkernel_sys): read a link through the path its 32-link walk resolved #23767

Closed
Colewiz7 wants to merge 1 commit from upstream-pr/23767 into main
Colewiz7 commented 2026-11-19 16:06:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Colewiz7 on 2026-11-19T16:06:10Z.


What

guest_kernel_sys_paths_readlink_limits, guest_kernel_sys_paths_readlinkat_limits and guest_kernel_sys_paths_readlinkat_limits_patched abort now and then in a parallel ctest run and pass alone. They show up as one-off failures in our full-suite runs on main. There are two causes:

  1. Host ELOOP on long chains. ReadNativeLink first walks the path itself (SymbolicLinkLimitError, FreeBSD's 32-link limit), then calls symlink_status and read_symlink on the original path, so Linux walks the same chain again. On this machine's kernel, lstat through a chain of more than 20 links sometimes fails with ELOOP, though the limit is 40. A loop of unshare -rm true in another process triggers it reliably. It also happened, less often, while our full test suite was running on the same machine; I haven't pinned down what in the suite triggers it. In the test that shows up as readlink("p2/cycle") returning -1 with errno 62, when the walk had accepted the path. Now the walk hands back the host path it resolved, and readlink reads the last component there, so the host follows no links. A trailing / is kept, and the Windows branch is unchanged.
  2. Root name collision. The test names its directory after steady_clock alone. The variants start together under ctest -j, and twice two of them got the same name, so the second one's create_directory failed at line 116. A std::random_device suffix fixes it.

The host behaviour, measured with Python os.lstat on a chain of links ending in a link, 150,000 calls per row, while a loop ran unshare -rm true:

links in the prefix ELOOP
31 4,748
23 3,536
22 3,707
21 3,396
20 0
18 0

Without that loop, 31-link chains gave 0 in 150,000 calls, and so did a loop that only created, renamed and deleted files. My guess is that the kernel counts the links twice when a mount change makes its lockless lookup fall back to the locked one: 2 × 21 = 42 is over 40, 2 × 20 = 40 is not. I have not confirmed this in the kernel source.

Tested

Linux 7.3.4 (CachyOS), GCC 16.2.1 Release, main a8b2a7d2a5.

  • ctest -R '^guest_kernel_sys': 66/66.
  • The two readlinkat_limits variants and readlink_limits started together, 1,000 rounds each:
while a loop ran unshare -rm true while our verify batch ran the full suite
main 614 of 3,000 failed 17 of 3,000 failed
this PR 0 of 3,000 0 of 4,500

The MinGW cross build succeeds. Its guest_kernel_sys_paths tests fail under wine on main too, so they say nothing here.

Not tested: Windows hosts, other Linux kernels, macOS.

Overlap

#9389 (sigqueue) and #15037 (mount privilege checks) edit other functions in libkernel_sys/Export.cpp. link and linkat use the same walk and still pass the original path to the host; this PR leaves them alone.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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, no GPU code
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5398454067 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23767). Originally posted by [Colewiz7](https://github.com/Colewiz7) on 2026-11-19T16:06:10Z. --- ### What `guest_kernel_sys_paths_readlink_limits`, `guest_kernel_sys_paths_readlinkat_limits` and `guest_kernel_sys_paths_readlinkat_limits_patched` abort now and then in a parallel `ctest` run and pass alone. They show up as one-off failures in our full-suite runs on `main`. There are two causes: 1. **Host `ELOOP` on long chains.** `ReadNativeLink` first walks the path itself (`SymbolicLinkLimitError`, FreeBSD's 32-link limit), then calls `symlink_status` and `read_symlink` on the original path, so Linux walks the same chain again. On this machine's kernel, `lstat` through a chain of more than 20 links sometimes fails with `ELOOP`, though the limit is 40. A loop of `unshare -rm true` in another process triggers it reliably. It also happened, less often, while our full test suite was running on the same machine; I haven't pinned down what in the suite triggers it. In the test that shows up as `readlink("p2/cycle")` returning -1 with errno 62, when the walk had accepted the path. Now the walk hands back the host path it resolved, and `readlink` reads the last component there, so the host follows no links. A trailing `/` is kept, and the Windows branch is unchanged. 2. **Root name collision.** The test names its directory after `steady_clock` alone. The variants start together under `ctest -j`, and twice two of them got the same name, so the second one's `create_directory` failed at line 116. A `std::random_device` suffix fixes it. The host behaviour, measured with Python `os.lstat` on a chain of links ending in a link, 150,000 calls per row, while a loop ran `unshare -rm true`: | links in the prefix | `ELOOP` | |---|---| | 31 | 4,748 | | 23 | 3,536 | | 22 | 3,707 | | 21 | 3,396 | | 20 | 0 | | 18 | 0 | Without that loop, 31-link chains gave 0 in 150,000 calls, and so did a loop that only created, renamed and deleted files. My guess is that the kernel counts the links twice when a mount change makes its lockless lookup fall back to the locked one: 2 × 21 = 42 is over 40, 2 × 20 = 40 is not. I have not confirmed this in the kernel source. ### Tested Linux 7.3.4 (CachyOS), GCC 16.2.1 Release, `main` `a8b2a7d2a5`. - `ctest -R '^guest_kernel_sys'`: 66/66. - The two `readlinkat_limits` variants and `readlink_limits` started together, 1,000 rounds each: | | while a loop ran `unshare -rm true` | while our verify batch ran the full suite | |---|---|---| | `main` | 614 of 3,000 failed | 17 of 3,000 failed | | this PR | 0 of 3,000 | 0 of 4,500 | The MinGW cross build succeeds. Its `guest_kernel_sys_paths` tests fail under wine on `main` too, so they say nothing here. Not tested: Windows hosts, other Linux kernels, macOS. ### Overlap #9389 (`sigqueue`) and #15037 (`mount` privilege checks) edit other functions in `libkernel_sys/Export.cpp`. `link` and `linkat` use the same walk and still pass the original path to the host; this PR leaves them alone. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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, no GPU code - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-19 16:16:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T16:16:20Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against ce75ed2cc1:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_sys_paths_tests built cleanly.
  • CTest (guest_kernel_sys_paths_statfs, guest_kernel_sys_paths_statfs_patched, guest_kernel_sys_paths_symlinkat, guest_kernel_sys_paths_symlinkat_patched, guest_kernel_sys_paths_symlinkat_errors, guest_kernel_sys_paths_symlinkat_errors_patched, guest_kernel_sys_paths_futimesat, guest_kernel_sys_paths_futimesat_patched, guest_kernel_sys_paths_readlink_limits, guest_kernel_sys_paths_readlink_limits_patched, guest_kernel_sys_paths_readlinkat_limits, guest_kernel_sys_paths_readlinkat_limits_patched):
    2/12 Test #824: guest_kernel_sys_paths_readlinkat_limits_patched ...   Passed    0.08 sec
    3/12 Test #821: guest_kernel_sys_paths_readlink_limits .............   Passed    0.04 sec
    4/12 Test #822: guest_kernel_sys_paths_readlink_limits_patched .....   Passed    0.04 sec
    5/12 Test #815: guest_kernel_sys_paths_symlinkat ...................   Passed    0.03 sec
    6/12 Test #816: guest_kernel_sys_paths_symlinkat_patched ...........   Passed    0.03 sec
    7/12 Test #814: guest_kernel_sys_paths_statfs_patched ..............   Passed    0.03 sec
    8/12 Test #820: guest_kernel_sys_paths_futimesat_patched ...........   Passed    0.03 sec
    9/12 Test #819: guest_kernel_sys_paths_futimesat ...................   Passed    0.03 sec
    10/12 Test #817: guest_kernel_sys_paths_symlinkat_errors ............   Passed    0.02 sec
    11/12 Test #813: guest_kernel_sys_paths_statfs ......................   Passed    0.03 sec
    12/12 Test #818: guest_kernel_sys_paths_symlinkat_errors_patched ....   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 12
    
  • Negative control: with core/libs/prx/libkernel_sys/Export.cpp reverted to main and the new tests kept, the suite fails:
    6/12 Test #815: guest_kernel_sys_paths_symlinkat ...................   Passed    0.04 sec
    7/12 Test #820: guest_kernel_sys_paths_futimesat_patched ...........   Passed    0.04 sec
    8/12 Test #819: guest_kernel_sys_paths_futimesat ...................   Passed    0.04 sec
    9/12 Test #813: guest_kernel_sys_paths_statfs ......................   Passed    0.03 sec
    10/12 Test #814: guest_kernel_sys_paths_statfs_patched ..............   Passed    0.03 sec
    11/12 Test #817: guest_kernel_sys_paths_symlinkat_errors ............   Passed    0.03 sec
    12/12 Test #818: guest_kernel_sys_paths_symlinkat_errors_patched ....   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 12
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified SymbolicLinkLimitError path resolution correctly handles intermediate symlinks while leaving the final component unresolved when followLast=false
    • Checked ReadNativeLink in core/libs/prx/libkernel_sys/Export.cpp on Windows and Linux targets, ensuring symlink_status and read_symlink receive the accurately resolved parent path
    • Verified guest pointer handling and bounds checking in CopyLinkTarget and ReadNativeLink (GuestCopyOut_nid_no_patch and GuestEfault checks)
    • Checked GuestKernelSysPaths.cpp test root directory name generation using steady_clock and std::random_device to prevent parallel CTest collisions
    • Confirmed AGENTS.md rules compliance, conventional commit requirements, and lack of improper comments or special-cased values

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5978869286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23767#pullrequestreview-5978869286). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T16:16:20Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `ce75ed2cc1`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_kernel_sys_paths_tests` built cleanly. - **CTest** (`guest_kernel_sys_paths_statfs`, `guest_kernel_sys_paths_statfs_patched`, `guest_kernel_sys_paths_symlinkat`, `guest_kernel_sys_paths_symlinkat_patched`, `guest_kernel_sys_paths_symlinkat_errors`, `guest_kernel_sys_paths_symlinkat_errors_patched`, `guest_kernel_sys_paths_futimesat`, `guest_kernel_sys_paths_futimesat_patched`, `guest_kernel_sys_paths_readlink_limits`, `guest_kernel_sys_paths_readlink_limits_patched`, `guest_kernel_sys_paths_readlinkat_limits`, `guest_kernel_sys_paths_readlinkat_limits_patched`): ``` 2/12 Test #824: guest_kernel_sys_paths_readlinkat_limits_patched ... Passed 0.08 sec 3/12 Test #821: guest_kernel_sys_paths_readlink_limits ............. Passed 0.04 sec 4/12 Test #822: guest_kernel_sys_paths_readlink_limits_patched ..... Passed 0.04 sec 5/12 Test #815: guest_kernel_sys_paths_symlinkat ................... Passed 0.03 sec 6/12 Test #816: guest_kernel_sys_paths_symlinkat_patched ........... Passed 0.03 sec 7/12 Test #814: guest_kernel_sys_paths_statfs_patched .............. Passed 0.03 sec 8/12 Test #820: guest_kernel_sys_paths_futimesat_patched ........... Passed 0.03 sec 9/12 Test #819: guest_kernel_sys_paths_futimesat ................... Passed 0.03 sec 10/12 Test #817: guest_kernel_sys_paths_symlinkat_errors ............ Passed 0.02 sec 11/12 Test #813: guest_kernel_sys_paths_statfs ...................... Passed 0.03 sec 12/12 Test #818: guest_kernel_sys_paths_symlinkat_errors_patched .... Passed 0.02 sec 100% tests passed, 0 tests failed out of 12 ``` - **Negative control**: with `core/libs/prx/libkernel_sys/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 6/12 Test #815: guest_kernel_sys_paths_symlinkat ................... Passed 0.04 sec 7/12 Test #820: guest_kernel_sys_paths_futimesat_patched ........... Passed 0.04 sec 8/12 Test #819: guest_kernel_sys_paths_futimesat ................... Passed 0.04 sec 9/12 Test #813: guest_kernel_sys_paths_statfs ...................... Passed 0.03 sec 10/12 Test #814: guest_kernel_sys_paths_statfs_patched .............. Passed 0.03 sec 11/12 Test #817: guest_kernel_sys_paths_symlinkat_errors ............ Passed 0.03 sec 12/12 Test #818: guest_kernel_sys_paths_symlinkat_errors_patched .... Passed 0.02 sec 100% tests passed, 0 tests failed out of 12 ``` Restoring the change makes it pass again. - **Code reading**: - Verified SymbolicLinkLimitError path resolution correctly handles intermediate symlinks while leaving the final component unresolved when followLast=false - Checked ReadNativeLink in core/libs/prx/libkernel_sys/Export.cpp on Windows and Linux targets, ensuring symlink_status and read_symlink receive the accurately resolved parent path - Verified guest pointer handling and bounds checking in CopyLinkTarget and ReadNativeLink (GuestCopyOut_nid_no_patch and GuestEfault checks) - Checked GuestKernelSysPaths.cpp test root directory name generation using steady_clock and std::random_device to prevent parallel CTest collisions - Confirmed AGENTS.md rules compliance, conventional commit requirements, and lack of improper comments or special-cased values Looks Good To Me

Pull request closed

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!23767
No description provided.