fix(libkernel_sys): follow a symbolic link in link on Windows #9336

Merged
ycbuzi merged 5 commits from upstream-pr/9336 into main 2026-10-22 22:02:54 +00:00
ycbuzi commented 2026-10-21 18:45:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T18:45:32Z.


What

link follows a symbolic link source, as FreeBSD's does (7cba4ce60a). It decided that with std::filesystem::is_symlink, and MinGW-w64's libstdc++ never reports a Windows symbolic link as one: symlink_status of a CreateSymbolicLinkW link with FILE_ATTRIBUTE_REPARSE_POINT set returns a regular file. So on Windows link made a hard link to the symbolic link itself. The test for following is under #ifndef _WIN32, so nothing showed it.

On Windows link now checks the source for FILE_ATTRIBUTE_REPARSE_POINT, as fchmodat in the same file does. It opens a reparse point source with CreateFileW without FILE_FLAG_OPEN_REPARSE_POINT and links to GetFinalPathNameByHandleW of that handle. A dangling link returns ENOENT and a loop (ERROR_CANT_RESOLVE_FILENAME) ELOOP, instead of linking the reparse point. A source that resolves to a directory returns EPERM, as before. Linux and macOS are unchanged.

The link-through-a-link case of guest_kernel_sys now runs on Windows too, when symbolic links can be created. It uses dot-link for its link to ., because the Windows part of the test already makes a directory-link earlier. On Windows it checks that the new name is not a reparse point, since is_symlink cannot tell there.

Tested

Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON, on main 950388bfd2:

  • guest_kernel_sys, elevated so symbolic links can be created: passes 3 of 3.
  • Negative control, the test change with Export.cpp from main: fails at GuestKernelSys.cpp:692 (guest errno 2), the new name is a reparse point.
  • Unelevated (Medium integrity, Developer Mode off): passes. The guest symlink fails there, so the new block is skipped, which the test shows with symbolic links unavailable: the symlink success cases not tested. (The the link cases not tested line comes from an earlier block that uses std::filesystem::create_symlink, which MinGW's libstdc++ does not implement.)
  • After #8522 merged, main was merged into this branch (339d5c11d7). The only conflict was its ENOTDIR return on the destination line next to this change; both are kept. guest_kernel_sys passes 3 of 3 on the merge.
  • After review, a loop case on Windows (37ba44ce4d): after a guest symlink("loop-b", "loop-a") and symlink("loop-a", "loop-b"), link from loop-a returns ELOOP (62). _wstat64 succeeds on the loop, so the existence check lets it through, and CreateFileW then fails with ERROR_CANT_RESOLVE_FILENAME (1921). guest_kernel_sys passes 2 of 2 with it. A chain of 40 links is linked, where FreeBSD stops at 32; the debt file records it (e9e345fbf3).
  • A standalone probe against the unpatched libraries: a guest symlink("owned.txt", "owned.link"), then link("owned.link", "other.txt"). On main, other.txt is a reparse point, owned.link has 2 links and owned.txt 1. With this change other.txt is a plain file and owned.txt has 2 links.
  • python tools/check_conventions.py --base origin/main passes.
  • Overlap (tools/pr_overlap.py): #8522, now merged, is handled above. #6044 moves GuestKernelSys.cpp. #5528 changes only readlink, and #5021 and #7201 do not touch link.
  • Not run: Linux and macOS (the code there is unchanged), a title.

Found while running #9103's new linkat test on Windows; its AT_SYMLINK_FOLLOW case goes through this link.

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: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4956638997 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9336). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T18:45:32Z. --- ### What `link` follows a symbolic link source, as FreeBSD's does (7cba4ce60a). It decided that with `std::filesystem::is_symlink`, and MinGW-w64's libstdc++ never reports a Windows symbolic link as one: `symlink_status` of a `CreateSymbolicLinkW` link with `FILE_ATTRIBUTE_REPARSE_POINT` set returns a regular file. So on Windows `link` made a hard link to the symbolic link itself. The test for following is under `#ifndef _WIN32`, so nothing showed it. On Windows `link` now checks the source for `FILE_ATTRIBUTE_REPARSE_POINT`, as `fchmodat` in the same file does. It opens a reparse point source with `CreateFileW` without `FILE_FLAG_OPEN_REPARSE_POINT` and links to `GetFinalPathNameByHandleW` of that handle. A dangling link returns `ENOENT` and a loop (`ERROR_CANT_RESOLVE_FILENAME`) `ELOOP`, instead of linking the reparse point. A source that resolves to a directory returns `EPERM`, as before. Linux and macOS are unchanged. The link-through-a-link case of `guest_kernel_sys` now runs on Windows too, when symbolic links can be created. It uses `dot-link` for its link to `.`, because the Windows part of the test already makes a `directory-link` earlier. On Windows it checks that the new name is not a reparse point, since `is_symlink` cannot tell there. ### Tested Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, `BUILD_TESTING=ON`, on `main` 950388bfd2: - `guest_kernel_sys`, elevated so symbolic links can be created: passes 3 of 3. - Negative control, the test change with `Export.cpp` from `main`: fails at `GuestKernelSys.cpp:692` (guest errno 2), the new name is a reparse point. - Unelevated (Medium integrity, Developer Mode off): passes. The guest `symlink` fails there, so the new block is skipped, which the test shows with `symbolic links unavailable: the symlink success cases not tested`. (The `the link cases not tested` line comes from an earlier block that uses `std::filesystem::create_symlink`, which MinGW's libstdc++ does not implement.) - After #8522 merged, `main` was merged into this branch (339d5c11d7). The only conflict was its ENOTDIR return on the destination line next to this change; both are kept. `guest_kernel_sys` passes 3 of 3 on the merge. - After review, a loop case on Windows (37ba44ce4d): after a guest `symlink("loop-b", "loop-a")` and `symlink("loop-a", "loop-b")`, `link` from `loop-a` returns `ELOOP` (62). `_wstat64` succeeds on the loop, so the existence check lets it through, and `CreateFileW` then fails with `ERROR_CANT_RESOLVE_FILENAME` (1921). `guest_kernel_sys` passes 2 of 2 with it. A chain of 40 links is linked, where FreeBSD stops at 32; the debt file records it (e9e345fbf3). - A standalone probe against the unpatched libraries: a guest `symlink("owned.txt", "owned.link")`, then `link("owned.link", "other.txt")`. On `main`, `other.txt` is a reparse point, `owned.link` has 2 links and `owned.txt` 1. With this change `other.txt` is a plain file and `owned.txt` has 2 links. - `python tools/check_conventions.py --base origin/main` passes. - Overlap (`tools/pr_overlap.py`): #8522, now merged, is handled above. #6044 moves `GuestKernelSys.cpp`. #5528 changes only `readlink`, and #5021 and #7201 do not touch `link`. - Not run: Linux and macOS (the code there is unchanged), a title. Found while running #9103's new `linkat` test on Windows; its `AT_SYMLINK_FOLLOW` case goes through this `link`. ### 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: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
cadenasynyster commented 2026-10-21 19:08:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-21T19:08:08Z.


Independent Windows run of this PR (head a29386856).

Environment: Windows 11 10.0.26300, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 (MinGW), Release, BUILD_TESTING=ON. PowerShell not elevated, Developer Mode on.

Results:

  • PR head: guest_kernel_sys passes 3/3 (--repeat until-fail:3).
  • Baseline: I kept the PR's test and replaced only core/libs/prx/libkernel_sys/Export.cpp with the merge-base version. guest_kernel_sys fails with "Kernel sys check failed at line 692 (guest errno 2)", the same as the description's negative control. I restored the PR's file afterwards.

On the unelevated note in the description: my run also printed "symbolic links unavailable: the link cases not tested", but the new link-through-a-link cases still ran. The base failure at line 692 is inside that block, and that block's own skip message ("the symlink success cases not tested") was not printed. The "link cases not tested" message comes from the earlier readlink block, which creates its link with std::filesystem::create_symlink, and that call failed here. The new block depends on the guest symlink instead, which passes SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE, and on this machine it succeeded without elevation. So whether the new cases ran is shown by the absence of "the symlink success cases not tested", not by the "link cases not tested" line. I did not check why create_symlink fails here.

Not run: a symbolic link loop or a chain of links, Linux/macOS, the full repository CTest, and any title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6324263077 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9336#issuecomment-6324263077). Originally posted by [cadenasynyster](https://github.com/cadenasynyster) on 2026-10-21T19:08:08Z. --- Independent Windows run of this PR (head a29386856). **Environment:** Windows 11 10.0.26300, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 (MinGW), Release, BUILD_TESTING=ON. PowerShell not elevated, Developer Mode on. **Results:** - PR head: `guest_kernel_sys` passes 3/3 (`--repeat until-fail:3`). - Baseline: I kept the PR's test and replaced only `core/libs/prx/libkernel_sys/Export.cpp` with the merge-base version. `guest_kernel_sys` fails with "Kernel sys check failed at line 692 (guest errno 2)", the same as the description's negative control. I restored the PR's file afterwards. **On the unelevated note in the description:** my run also printed "symbolic links unavailable: the link cases not tested", but the new link-through-a-link cases still ran. The base failure at line 692 is inside that block, and that block's own skip message ("the symlink success cases not tested") was not printed. The "link cases not tested" message comes from the earlier readlink block, which creates its link with `std::filesystem::create_symlink`, and that call failed here. The new block depends on the guest `symlink` instead, which passes `SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE`, and on this machine it succeeded without elevation. So whether the new cases ran is shown by the absence of "the symlink success cases not tested", not by the "link cases not tested" line. I did not check why `create_symlink` fails here. **Not run:** a symbolic link loop or a chain of links, Linux/macOS, the full repository CTest, and any title. AI-assisted: yes
ycbuzi commented 2026-10-21 19:14:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T19:14:02Z.


@cadenasynyster thanks for the run and for the correction. You're right: the link cases not tested comes from the earlier block, whose std::filesystem::create_symlink cannot succeed on Windows with MinGW's libstdc++ (it fails with "Function not implemented" here even elevated). The skip line for the new block is the symlink success cases not tested. My unelevated run had Developer Mode off, so the guest symlink failed and that line was printed too. I've corrected the description.

Since #8522 landed in the meantime, I merged main into the branch (339d5c11d7). The conflict was its ENOTDIR return on the destination line just above this change; both are kept, and guest_kernel_sys passes 3 of 3 on the merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6324380286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9336#issuecomment-6324380286). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T19:14:02Z. --- @cadenasynyster thanks for the run and for the correction. You're right: `the link cases not tested` comes from the earlier block, whose `std::filesystem::create_symlink` cannot succeed on Windows with MinGW's libstdc++ (it fails with "Function not implemented" here even elevated). The skip line for the new block is `the symlink success cases not tested`. My unelevated run had Developer Mode off, so the guest `symlink` failed and that line was printed too. I've corrected the description. Since #8522 landed in the meantime, I merged `main` into the branch (339d5c11d7). The conflict was its ENOTDIR return on the destination line just above this change; both are kept, and `guest_kernel_sys` passes 3 of 3 on the merge.
harisibrar1 commented 2026-10-22 13:41:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T13:41:43Z.

Review: COMMENTED


Checked against main 8b3e39832a and the Windows SDK 10.0.26100 UCRT source (ucrt/filesystem/stat.cpp):

  • This merges cleanly with current main, and main still uses is_symlink here, so the fix is still needed.
  • I think the loop → ELOOP case can't be reached. link_nid_postfix runs std::filesystem::status(source) and returns ENOENT/ENOTDIR when it doesn't exist (Export.cpp:374-375), before FollowLinkPath (:381). On MinGW that status is UCRT _wstat64, and when CreateFileW fails on a path that isn't a drive root, UCRT stat reports ERROR_FILE_NOT_FOUND (stat.cpp:378-388). So a loop comes back as ENOENT before FollowLinkPath runs. Example: guest symlink("b","a"), symlink("a","b"), link("a","x") should give 62 (ELOOP) as on FreeBSD, but would give 2. Could FollowLinkPath run before the existence check on Windows, with a loop case added to guest_kernel_sys? I couldn't run this locally (Smart App Control blocked my probe), so a run on your side would confirm it.
  • For the same reason, the ERROR_FILE_NOT_FOUND → ENOENT branch only matters in a race. Dangling links still return ENOENT correctly through the earlier check.
  • Minor: Windows follows up to 63 reparse hops and FreeBSD stops at 32 (MAXSYMLINKS), so a 33-63 link chain succeeds here where the console returns ELOOP. Fine to leave for the debt file if you prefer.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5615159004 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9336#pullrequestreview-5615159004). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T13:41:43Z. **Review:** COMMENTED --- Checked against main 8b3e39832a and the Windows SDK 10.0.26100 UCRT source (ucrt/filesystem/stat.cpp): - This merges cleanly with current main, and main still uses `is_symlink` here, so the fix is still needed. - I think the loop → ELOOP case can't be reached. `link_nid_postfix` runs `std::filesystem::status(source)` and returns ENOENT/ENOTDIR when it doesn't exist (Export.cpp:374-375), before `FollowLinkPath` (:381). On MinGW that `status` is UCRT `_wstat64`, and when CreateFileW fails on a path that isn't a drive root, UCRT stat reports ERROR_FILE_NOT_FOUND (stat.cpp:378-388). So a loop comes back as ENOENT before `FollowLinkPath` runs. Example: guest `symlink("b","a")`, `symlink("a","b")`, `link("a","x")` should give 62 (ELOOP) as on FreeBSD, but would give 2. Could `FollowLinkPath` run before the existence check on Windows, with a loop case added to `guest_kernel_sys`? I couldn't run this locally (Smart App Control blocked my probe), so a run on your side would confirm it. - For the same reason, the ERROR_FILE_NOT_FOUND → ENOENT branch only matters in a race. Dangling links still return ENOENT correctly through the earlier check. - Minor: Windows follows up to 63 reparse hops and FreeBSD stops at 32 (MAXSYMLINKS), so a 33-63 link chain succeeds here where the console returns ELOOP. Fine to leave for the debt file if you prefer. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
harisibrar1 commented 2026-10-22 18:09:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T18:09:04Z.

Review: APPROVED


Rechecked at e9e345fbf3:

  • Thanks for running it. I was wrong about the loop: I read UCRT's _wstat64 error path but didn't run it, and on a real loop it succeeds, so the existence check lets it through and FollowLinkPath returns ELOOP as intended. Sorry for the noise.
  • The new Windows loop case in guest_kernel_sys (37ba44ce4d) pins that, and the debt line (e9e345fbf3) records that longer chains link here, where FreeBSD stops at MAXSYMLINKS (32). Nothing left from my side.

Reviewed with AI assistance (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5618745785 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9336#pullrequestreview-5618745785). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T18:09:04Z. **Review:** APPROVED --- Rechecked at e9e345fbf3: - Thanks for running it. I was wrong about the loop: I read UCRT's `_wstat64` error path but didn't run it, and on a real loop it succeeds, so the existence check lets it through and `FollowLinkPath` returns `ELOOP` as intended. Sorry for the noise. - The new Windows loop case in `guest_kernel_sys` (37ba44ce4d) pins that, and the debt line (e9e345fbf3) records that longer chains link here, where FreeBSD stops at `MAXSYMLINKS` (32). Nothing left from my side. Reviewed with AI assistance (Claude Code).
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!9336
No description provided.