fix(libkernel_sys): resolve linkat paths relative to a directory descriptor #10434

Merged
Retr0G28 merged 2 commits from upstream-pr/10434 into main 2026-10-25 06:16:11 +00:00
Retr0G28 commented 2026-10-24 00:12:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-24T00:12:58Z.


What

linkat threw linkat: a relative path with a directory descriptor whenever fd1 or fd2 was not AT_FDCWD and its path was relative. FreeBSD 11's kern_linkat looks path1 up from fd1 and path2 from fd2, like every other *at call.

Each path is now resolved on its own, as renameat, readlinkat and unlinkat already do. AT_FDCWD or an absolute path goes through ResolvePathError_nid_no_patch. Any other relative path is joined to the descriptor's host directory from GetDirectoryDescriptorPath_nid_no_patch, which fails with EBADF for a descriptor that is not open and ENOTDIR for one that is not a directory, as namei does. A path of GuestPathMax or longer, or one with a component that is too long, fails with ENAMETOOLONG, as in renameat. The body of link after path resolution moves into LinkNative without changes, so link and linkat share the checks for EPERM on a directory, EEXIST, ENOENT, ENOTDIR and EXDEV.

kern_linkat finishes namei on path1, including the EPERM for a directory, before it looks at path2. linkat follows the same order. The source checks move out of LinkNative into LinkSourceError. linkat runs, in order: the EFAULT and ENOENT checks for path1, its resolution, and LinkSourceError. Only then does it check and resolve path2. So a missing source gives ENOENT and a directory source gives EPERM, even when fd2 is not open or path2 is null or empty. link resolves both paths and then calls LinkSourceError and LinkNative, so its order is unchanged.

An empty path now fails with ENOENT in linkat itself, as it did before through link. A symbolic link as the source without AT_SYMLINK_FOLLOW still throws, now also when it is named relative to a descriptor. The throw comes before the source checks, which would follow the link.

The debt entry takes the sentence readlinkat and unlinkat use for descriptor-relative paths, with the same limits: guest path aliases and case-insensitive name matching do not apply, and a .. component is not kept inside the guest root.

Test: the two ThrowsNaming checks in guest_kernel_sys become real links:

  • from a directory descriptor to an absolute path, then the other way round, each checked by hard_link_count;
  • EEXIST and ENOENT with both paths relative to the descriptor;
  • EBADF for an unopened fd1 or fd2, and ENOTDIR for a regular file's descriptor as fd1 or fd2;
  • the source is checked first: a missing source gives ENOENT with an unopened fd2 or a null path2, and a directory source gives EPERM with an unopened fd2 or an empty path2. A valid source with a null or empty path2 gives EFAULT or ENOENT;
  • after the failed calls, the target name does not exist.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, -DBUILD_TESTING=ON, on main a23f8fc9fb.

  • With main's Export.cpp, guest_kernel_sys fails (exit 0xc0000409: the call throws where the test expects a link). With this PR it passes.
  • With the first commit's Export.cpp (path2 resolved before the source check), the new ordering checks fail (exit 0xc0000409). With head 2d32e8bd85 they pass.
  • guest_kernel_sys, guest_kernel_rename, guest_rename_dot and conventions_doc_links pass on 2d32e8bd85.
  • python tools/check_conventions.py --base origin/main reports no errors.
  • Linux: amazedturnip ran guest_kernel_sys on the first commit d44d5685eb (Ubuntu 24.04 on WSL2, GCC 13.3.0), and it passed. The ordering commit has not been run on Linux. No title or homebrew run.

Overlap: tools/pr_overlap.py finds no open PR that conflicts on the changed lines. Of the PRs it could not compare:

  • #9423 (copying guest path arguments) and #9693, which carries the same change, rewrite the null and empty checks at the top of link. This PR moves link below LinkNative. Whichever merges second needs a small rebase, and linkat would then take the same CopyPath checks.
  • #5021, #6044, #7201, #9097, #9442, #9826 and #10016 touch Export.cpp or GuestKernelSys.cpp, and none of them changes link or linkat.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4990867811 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10434). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-10-24T00:12:58Z. --- ### What `linkat` threw `linkat: a relative path with a directory descriptor` whenever `fd1` or `fd2` was not `AT_FDCWD` and its path was relative. FreeBSD 11's [`kern_linkat`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_syscalls.c#L1357-L1441) looks `path1` up from `fd1` and `path2` from `fd2`, like every other `*at` call. Each path is now resolved on its own, as `renameat`, `readlinkat` and `unlinkat` already do. `AT_FDCWD` or an absolute path goes through `ResolvePathError_nid_no_patch`. Any other relative path is joined to the descriptor's host directory from `GetDirectoryDescriptorPath_nid_no_patch`, which fails with `EBADF` for a descriptor that is not open and `ENOTDIR` for one that is not a directory, as `namei` does. A path of `GuestPathMax` or longer, or one with a component that is too long, fails with `ENAMETOOLONG`, as in `renameat`. The body of `link` after path resolution moves into `LinkNative` without changes, so `link` and `linkat` share the checks for `EPERM` on a directory, `EEXIST`, `ENOENT`, `ENOTDIR` and `EXDEV`. `kern_linkat` finishes `namei` on `path1`, including the `EPERM` for a directory, before it looks at `path2`. `linkat` follows the same order. The source checks move out of `LinkNative` into `LinkSourceError`. `linkat` runs, in order: the `EFAULT` and `ENOENT` checks for `path1`, its resolution, and `LinkSourceError`. Only then does it check and resolve `path2`. So a missing source gives `ENOENT` and a directory source gives `EPERM`, even when `fd2` is not open or `path2` is null or empty. `link` resolves both paths and then calls `LinkSourceError` and `LinkNative`, so its order is unchanged. An empty path now fails with `ENOENT` in `linkat` itself, as it did before through `link`. A symbolic link as the source without `AT_SYMLINK_FOLLOW` still throws, now also when it is named relative to a descriptor. The throw comes before the source checks, which would follow the link. The debt entry takes the sentence `readlinkat` and `unlinkat` use for descriptor-relative paths, with the same limits: guest path aliases and case-insensitive name matching do not apply, and a `..` component is not kept inside the guest root. Test: the two `ThrowsNaming` checks in `guest_kernel_sys` become real links: - from a directory descriptor to an absolute path, then the other way round, each checked by `hard_link_count`; - `EEXIST` and `ENOENT` with both paths relative to the descriptor; - `EBADF` for an unopened `fd1` or `fd2`, and `ENOTDIR` for a regular file's descriptor as `fd1` or `fd2`; - the source is checked first: a missing source gives `ENOENT` with an unopened `fd2` or a null `path2`, and a directory source gives `EPERM` with an unopened `fd2` or an empty `path2`. A valid source with a null or empty `path2` gives `EFAULT` or `ENOENT`; - after the failed calls, the target name does not exist. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, `-DBUILD_TESTING=ON`, on `main` a23f8fc9fb. - With `main`'s `Export.cpp`, `guest_kernel_sys` fails (exit 0xc0000409: the call throws where the test expects a link). With this PR it passes. - With the first commit's `Export.cpp` (path2 resolved before the source check), the new ordering checks fail (exit 0xc0000409). With head 2d32e8bd85 they pass. - `guest_kernel_sys`, `guest_kernel_rename`, `guest_rename_dot` and `conventions_doc_links` pass on 2d32e8bd85. - `python tools/check_conventions.py --base origin/main` reports no errors. - Linux: amazedturnip ran `guest_kernel_sys` on the first commit d44d5685eb (Ubuntu 24.04 on WSL2, GCC 13.3.0), and it passed. The ordering commit has not been run on Linux. No title or homebrew run. Overlap: `tools/pr_overlap.py` finds no open PR that conflicts on the changed lines. Of the PRs it could not compare: - #9423 (copying guest path arguments) and #9693, which carries the same change, rewrite the null and empty checks at the top of `link`. This PR moves `link` below `LinkNative`. Whichever merges second needs a small rebase, and `linkat` would then take the same `CopyPath` checks. - #5021, #6044, #7201, #9097, #9442, #9826 and #10016 touch `Export.cpp` or `GuestKernelSys.cpp`, and none of them changes `link` or `linkat`. ### 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
harisibrar1 commented 2026-10-24 01:05:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T01:05:24Z.

Review: COMMENTED


Checked against main 11d6efa06c and FreeBSD 11.4 sys_link/sys_linkat/kern_linkat (vfs_syscalls.c) and namei (vfs_lookup.c):

  • The resolution matches main's other *at calls (mkdirat, unlinkat, renameat, readlinkat), and the EBADF/ENOTDIR for the descriptor line up with namei. The flag handling and the throw for a symlink source without AT_SYMLINK_FOLLOW match sys_linkat, and it still merges cleanly into main.
  • One ordering detail: kern_linkat runs namei on path1 completely, including the EPERM for a directory source, before it looks at path2. Here ResolveAtPath(fd2, …) (Export.cpp:491) runs before LinkNative checks the source. So linkat(dirfd, "missing.txt", 4321, "x", 0) returns EBADF where FreeBSD returns ENOENT, and a directory source with a bad fd2 gives EBADF instead of EPERM. Checking the source (existence and directory) before resolving path2 would fix it, and a test like linkat(directory, "missing.txt", 4321, "absent.txt", 0) == -1 && Error() == 2 would cover it.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5635992144 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10434#pullrequestreview-5635992144). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T01:05:24Z. **Review:** COMMENTED --- Checked against main 11d6efa06c and FreeBSD 11.4 `sys_link`/`sys_linkat`/`kern_linkat` (vfs_syscalls.c) and `namei` (vfs_lookup.c): - The resolution matches main's other *at calls (`mkdirat`, `unlinkat`, `renameat`, `readlinkat`), and the EBADF/ENOTDIR for the descriptor line up with namei. The flag handling and the throw for a symlink source without `AT_SYMLINK_FOLLOW` match `sys_linkat`, and it still merges cleanly into main. - One ordering detail: `kern_linkat` runs `namei` on path1 completely, including the `EPERM` for a directory source, before it looks at path2. Here `ResolveAtPath(fd2, …)` (Export.cpp:491) runs before `LinkNative` checks the source. So `linkat(dirfd, "missing.txt", 4321, "x", 0)` returns `EBADF` where FreeBSD returns `ENOENT`, and a directory source with a bad `fd2` gives `EBADF` instead of `EPERM`. Checking the source (existence and directory) before resolving path2 would fix it, and a test like `linkat(directory, "missing.txt", 4321, "absent.txt", 0) == -1 && Error() == 2` would cover it. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
harisibrar1 commented 2026-10-24 13:15:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:15:08Z.

Review: APPROVED


Re-checked head 2d32e8bd85 against main 6cd36e6517:

  • linkat now finishes path1 before it touches path2: resolution, the symlink throw, then LinkSourceError (Export.cpp:495-497), and only then the path2 EFAULT/ENOENT checks and ResolveAtPath(fd2, …) (Export.cpp:498-500), which matches the order in kern_linkat. linkat(directory, "missing.txt", 4321, "absent.txt", 0) now gives ENOENT, and a directory source with a bad fd2 gives EPERM.
  • Moving the checks into LinkSourceError keeps link's behaviour the same; LinkNative now only handles the destination and the native link.
  • The new cases in GuestKernelSys.cpp:174-179 cover each ordering case I raised.
  • It still merges cleanly into main, and main's linkat still throws for relative paths with a descriptor, so this is still needed. Thanks, looks good to me.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638379180 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10434#pullrequestreview-5638379180). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:15:08Z. **Review:** APPROVED --- Re-checked head 2d32e8bd85 against main 6cd36e6517: - `linkat` now finishes `path1` before it touches `path2`: resolution, the symlink throw, then `LinkSourceError` (Export.cpp:495-497), and only then the `path2` EFAULT/ENOENT checks and `ResolveAtPath(fd2, …)` (Export.cpp:498-500), which matches the order in `kern_linkat`. `linkat(directory, "missing.txt", 4321, "absent.txt", 0)` now gives ENOENT, and a directory source with a bad `fd2` gives EPERM. - Moving the checks into `LinkSourceError` keeps `link`'s behaviour the same; `LinkNative` now only handles the destination and the native link. - The new cases in GuestKernelSys.cpp:174-179 cover each ordering case I raised. - It still merges cleanly into main, and main's `linkat` still throws for relative paths with a descriptor, so this is still needed. Thanks, looks good to me. Reviewed with AI assistance (Claude Code); the checks above were run locally 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!10434
No description provided.