fix(libkernel_sys): fail symlink with ENAMETOOLONG for a target of 1024 bytes or more #8723

Merged
xXPBSXx merged 2 commits from upstream-pr/8723 into main 2026-10-22 17:36:51 +00:00
xXPBSXx commented 2026-10-20 22:25:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T22:25:00Z.


What

symlink_nid_postfix (libkernel_sys/Export.cpp) had no length check on the target. FreeBSD 11.4 kern_symlinkat copies the target with copyinstr(path1, syspath, MAXPATHLEN, NULL) (vfs_syscalls.c:1490) before it looks up the link name, and MAXPATHLEN is 1024 (sys/syslimits.h:63), so a target of 1024 bytes or more fails with ENAMETOOLONG (63). symlink now does the same before any other check of the target or the link name.

Fixes #8722

git merge-tree against upstream/main, #8522, #8680, #8312 and #8603 is clean; #5528 (readlink path length, open) conflicts in Export.cpp (one conflict); whichever lands second rebases. pr_overlap was not run.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main + head b3a9fbe3ea:

  • guest_kernel_sys: targets of 1024 and 1100 bytes return -1 with errno 63 and create no link. Without the change it fails at GuestKernelSys.cpp:301 (errno 1); with it it passes.
  • symlink_nid_postfix is called only from GuestKernelSys.cpp, so there is no other caller test to run.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux/macOS, the 1023-byte boundary (creating a link needs a symlink privilege on Windows, so only the failing side is asserted), a console.

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; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4943335418 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8723). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T22:25:00Z. --- ### What `symlink_nid_postfix` (`libkernel_sys/Export.cpp`) had no length check on the target. FreeBSD 11.4 `kern_symlinkat` copies the target with `copyinstr(path1, syspath, MAXPATHLEN, NULL)` ([vfs_syscalls.c:1490](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_syscalls.c#L1490)) before it looks up the link name, and `MAXPATHLEN` is 1024 (`sys/syslimits.h:63`), so a target of 1024 bytes or more fails with `ENAMETOOLONG` (63). `symlink` now does the same before any other check of the target or the link name. Fixes #8722 `git merge-tree` against `upstream/main`, #8522, #8680, #8312 and #8603 is clean; #5528 (readlink path length, open) conflicts in `Export.cpp` (one conflict); whichever lands second rebases. `pr_overlap` was not run. ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on `upstream/main` + head `b3a9fbe3ea`: - `guest_kernel_sys`: targets of 1024 and 1100 bytes return -1 with errno 63 and create no link. Without the change it fails at `GuestKernelSys.cpp:301` (errno 1); with it it passes. - `symlink_nid_postfix` is called only from `GuestKernelSys.cpp`, so there is no other caller test to run. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux/macOS, the 1023-byte boundary (creating a link needs a symlink privilege on Windows, so only the failing side is asserted), a console. ### 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 - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-20 22:29:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T22:29:09Z.

Review: APPROVED


Reviewed the current diff and upstream/main: GuestPathMax is 1024, and applying the same bound to the symlink target before resolving the link path matches the cited FreeBSD copyinstr limit. The existing ResolvePathError_nid_no_patch already uses this boundary for guest paths. The Windows test checks both 1024-byte and longer targets return ENAMETOOLONG and leave no link. The author reports the focused Windows test and conventions check passing. Hosted platform build jobs are skipped and Linux/macOS were not tested, so this approval is based on the focused result and the small, localized change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5592589404 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8723#pullrequestreview-5592589404). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T22:29:09Z. **Review:** APPROVED --- Reviewed the current diff and `upstream/main`: `GuestPathMax` is 1024, and applying the same bound to the symlink target before resolving the link path matches the cited FreeBSD `copyinstr` limit. The existing `ResolvePathError_nid_no_patch` already uses this boundary for guest paths. The Windows test checks both 1024-byte and longer targets return `ENAMETOOLONG` and leave no link. The author reports the focused Windows test and conventions check passing. Hosted platform build jobs are skipped and Linux/macOS were not tested, so this approval is based on the focused result and the small, localized change.
MarcZX10 commented 2026-10-21 03:53:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T03:53:39Z.

Review: COMMENTED


One useful boundary case is still missing from the regression: the current test checks 1024 and 1100 bytes, but not 1023. Since copyinstr(..., MAXPATHLEN, ...) permits at most MAXPATHLEN - 1 payload bytes plus the NUL, a 1023-byte target should still succeed while 1024 fails. Adding the accepted boundary case would protect against changing >= GuestPathMax to > GuestPathMax (or accidentally tightening the guest limit).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5594768496 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8723#pullrequestreview-5594768496). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T03:53:39Z. **Review:** COMMENTED --- One useful boundary case is still missing from the regression: the current test checks 1024 and 1100 bytes, but not 1023. Since `copyinstr(..., MAXPATHLEN, ...)` permits at most `MAXPATHLEN - 1` payload bytes plus the NUL, a 1023-byte target should still succeed while 1024 fails. Adding the accepted boundary case would protect against changing `>= GuestPathMax` to `> GuestPathMax` (or accidentally tightening the guest limit).
xXPBSXx commented 2026-10-21 04:29:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T04:29:00Z.


Thanks, added. GuestKernelSys.cpp now also tries a 1023-byte target: it must not fail with ENAMETOOLONG (it either links, or fails for a host reason such as missing symlink privilege, and a created link is removed again). 1024 and 1100 bytes still fail with 63. To check that the new case guards the boundary I changed the comparison in symlink_nid_postfix to >= GuestPathMax - 1: guest_kernel_sys then fails at GuestKernelSys.cpp:308 (errno 63); with the real comparison it passes (Windows 10, MinGW-w64 GCC 15.2, Release; commit 241c8d5973). Not tested again on Linux/macOS after this commit.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6308060019 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8723#issuecomment-6308060019). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T04:29:00Z. --- Thanks, added. `GuestKernelSys.cpp` now also tries a 1023-byte target: it must not fail with ENAMETOOLONG (it either links, or fails for a host reason such as missing symlink privilege, and a created link is removed again). 1024 and 1100 bytes still fail with 63. To check that the new case guards the boundary I changed the comparison in `symlink_nid_postfix` to `>= GuestPathMax - 1`: `guest_kernel_sys` then fails at GuestKernelSys.cpp:308 (errno 63); with the real comparison it passes (Windows 10, MinGW-w64 GCC 15.2, Release; commit 241c8d5973). Not tested again on Linux/macOS after this commit. AI-assisted (Claude Code).
ycbuzi commented 2026-10-21 04:46:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T04:46:12Z.


Windows run of 241c8d5973 merged onto main aa408677b0 (d74d8acda9, clean), covering the boundary 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, run elevated, so symbolic links can be created here.

  • guest_kernel_sys: passes (1.5 s).
  • A probe linked against this tree's libkernel_sys.prx, calling symlink with targets of 1022, 1023, 1024 and 1100 bytes into a scratch folder: 1022 and 1023 return 0, readlink gives the target back at its full length, and the link is removed; 1024 and 1100 return -1 with errno 63 and readlink finds no link. So the boundary sits where the PR puts it. One observation, data only: the probe zeroed errno before each call, and after the two successful symlink calls errno read 2.

Not run: Linux or macOS.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6308271261 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8723#issuecomment-6308271261). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T04:46:12Z. --- Windows run of 241c8d5973 merged onto `main` aa408677b0 (d74d8acda9, clean), covering the boundary 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`, run elevated, so symbolic links can be created here. - `guest_kernel_sys`: passes (1.5 s). - A probe linked against this tree's `libkernel_sys.prx`, calling `symlink` with targets of 1022, 1023, 1024 and 1100 bytes into a scratch folder: 1022 and 1023 return 0, `readlink` gives the target back at its full length, and the link is removed; 1024 and 1100 return -1 with errno 63 and `readlink` finds no link. So the boundary sits where the PR puts it. One observation, data only: the probe zeroed errno before each call, and after the two successful `symlink` calls errno read 2. Not run: Linux or macOS.
xXPBSXx commented 2026-10-21 05:34:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T05:34:19Z.


Thanks for the run and the boundary data.

On the errno of 2 after a successful symlink: it is left behind by a host call, not set by this change. symlink_nid_postfix looks at std::filesystem::is_directory(parent / target, error) to choose between a file and a directory link, and the target here does not exist. A standalone probe (Windows 10, MinGW-w64 GCC 15.2) shows that call setting the C errno itself: is_directory of a missing path returns false with ec = 2 and errno = 2, while the same call on an existing directory leaves errno at 0. The success path of symlink_nid_postfix does not touch errno, so it keeps that value. A caller must not read errno after a call that returned 0 (POSIX leaves it unspecified there), so I would leave this as it is; this PR only adds the length check. The same is_directory(native.parent_path() / contents, error) call is already on main (libkernel_sys/Export.cpp, symlink_nid_postfix), so the stale value is not introduced by this change. Not tested: whether other libkernel_sys calls leave a stale errno the same way.

Probe output (Windows 10, MinGW-w64 GCC 15.2, std::filesystem::is_directory with an error_code):

is_directory(missing)=0 errno=2 ec=2
is_directory(existing)=1 errno=0

I did not check whether another call in the function also leaves errno set.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6308876655 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8723#issuecomment-6308876655). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T05:34:19Z. --- Thanks for the run and the boundary data. On the errno of 2 after a successful `symlink`: it is left behind by a host call, not set by this change. `symlink_nid_postfix` looks at `std::filesystem::is_directory(parent / target, error)` to choose between a file and a directory link, and the target here does not exist. A standalone probe (Windows 10, MinGW-w64 GCC 15.2) shows that call setting the C `errno` itself: `is_directory` of a missing path returns false with `ec` = 2 and `errno` = 2, while the same call on an existing directory leaves `errno` at 0. The success path of `symlink_nid_postfix` does not touch errno, so it keeps that value. A caller must not read `errno` after a call that returned 0 (POSIX leaves it unspecified there), so I would leave this as it is; this PR only adds the length check. The same `is_directory(native.parent_path() / contents, error)` call is already on `main` (`libkernel_sys/Export.cpp`, `symlink_nid_postfix`), so the stale value is not introduced by this change. Not tested: whether other `libkernel_sys` calls leave a stale errno the same way. Probe output (Windows 10, MinGW-w64 GCC 15.2, `std::filesystem::is_directory` with an `error_code`): ``` is_directory(missing)=0 errno=2 ec=2 is_directory(existing)=1 errno=0 ``` I did not check whether another call in the function also leaves errno set. AI-assisted (Claude Code).
ycbuzi commented 2026-10-21 06:03:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T06:03:42Z.


Thanks for tracing it: agreed that a stale errno after a 0 return is outside what a caller may read, and that is_directory on main is where it comes from, not this change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6309262580 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8723#issuecomment-6309262580). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T06:03:42Z. --- Thanks for tracing it: agreed that a stale errno after a 0 return is outside what a caller may read, and that `is_directory` on `main` is where it comes from, not this change.
jotitadev commented 2026-10-21 22:53:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-21T22:53:10Z.

Review: COMMENTED


Confirmed red→green on Linux, which the description lists as not tested (both the author's run and @ycbuzi's are Windows). The fix is in symlink_nid_postfix, before any platform-specific work, so it applies to Linux too — and the unfixed path behaves differently there.

Tested

Ubuntu 26.04, i7-8700, GCC 15.2.0, Release, BUILD_TESTING=ON, on main a6dcea927 (head 241c8d5973 applies cleanly on it).

guest_kernel_sys result
main a6dcea927 + the test only (Export.cpp reverted) fail, GuestKernelSys.cpp:301, guest errno 36
this PR pass

On Linux, with the length check reverted, the 1024-byte target reaches the host symlink() and the guest ends up with errno 36 (Linux ENAMETOOLONG) rather than the FreeBSD ENAMETOOLONG 63 the test asserts — so the unfixed value differs from the Windows errno 1 reported earlier. With the PR, symlink_nid_postfix returns 63 before the host call and the test passes, including the 1023-byte boundary case added in 241c8d5973.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607201227 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8723#pullrequestreview-5607201227). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-21T22:53:10Z. **Review:** COMMENTED --- Confirmed `red→green` on Linux, which the description lists as not tested (both the author's run and @ycbuzi's are Windows). The fix is in `symlink_nid_postfix`, before any platform-specific work, so it applies to Linux too — and the unfixed path behaves differently there. ### Tested Ubuntu 26.04, i7-8700, GCC 15.2.0, Release, `BUILD_TESTING=ON`, on `main` `a6dcea927` (head `241c8d5973` applies cleanly on it). | `guest_kernel_sys` | result | |---|---| | `main` `a6dcea927` + the test only (`Export.cpp` reverted) | **fail**, `GuestKernelSys.cpp:301`, guest errno **36** | | this PR | **pass** | On Linux, with the length check reverted, the 1024-byte target reaches the host `symlink()` and the guest ends up with errno **36** (Linux `ENAMETOOLONG`) rather than the FreeBSD `ENAMETOOLONG` **63** the test asserts — so the unfixed value differs from the Windows errno 1 reported earlier. With the PR, `symlink_nid_postfix` returns `63` before the host call and the test passes, including the 1023-byte boundary case added in `241c8d5973`. 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!8723
No description provided.