feat(libkernel_sys): implement lchmod #9200

Merged
KernelPanicNT merged 1 commit from upstream-pr/9200 into main 2026-10-22 23:53:20 +00:00
KernelPanicNT commented 2026-10-21 15:08:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by KernelPanicNT on 2026-10-21T15:08:24Z.


What

lchmod(path, mode) in libkernel_sys now changes a file's mode without following a final symbolic link, as FreeBSD 11.4's sys_lchmod does. Until now it was one of the throwing stubs that #8837 added to Export.Discovery.cpp.

It forwards to the existing fchmodat with AT_FDCWD and AT_SYMLINK_NOFOLLOW, so the path lookup, the error codes (EFAULT, ENOENT, ELOOP, ENAMETOOLONG) and the 07777 mask are fchmodat's.

Source of the behaviour.

  • FreeBSD 11.4 at 8c639945: sys_lchmod is exactly kern_fchmodat(td, AT_FDCWD, path, UIO_USERSPACE, mode, AT_SYMLINK_NOFOLLOW). kern_fchmodat looks the path up with NOFOLLOW and calls setfmode, which masks the mode with ALLPERMS (07777). The prototype is int lchmod(char *path, mode_t mode), syscall 274.
  • PS5 firmware 10.01: my reading of the native SELF syscall table in the 10.01 kernel data has syscall 274 lchmod with 2 arguments and a native handler, and nosys (compat.lchmod) in the PS4 SELF table, like the other path calls that only libkernel_sys exports.

What is kept as a throw, and why.

  • A symbolic link on Linux and Windows: FreeBSD changes the link's own mode, but neither host can. fchmodat with AT_SYMLINK_NOFOLLOW returns success without a change here, which its debt entry lists as a silent stub. lchmod exists for this case, so it throws instead of adding a second silent stub. On macOS fchmodat changes the link's mode, and lchmod does too.

Changes:

  • Export.cpp: lchmod_nid_postfix(const char*, std::uint16_t). Outside macOS it resolves the path first and throws for a symbolic link (on Windows, any reparse point, as fchmodat does); then it calls fchmodat_nid_postfix(AT_FDCWD, path, mode, AT_SYMLINK_NOFOLLOW).
  • Export.Discovery.cpp: the lchmod stub is removed.
  • guest_kernel_sys: lchmod of a file by host path and by guest path (0600, then 0100644 masked to 0644), a missing file (ENOENT), a null path (EFAULT) and an empty one (ENOENT); outside Windows, a lookup through a link loop (ELOOP), a 256-byte component (ENAMETOOLONG), and a symbolic link that throws on Linux (changes the link on macOS) while the file it points to keeps its mode.
  • docs/dev/debt/libkernel_sys.md: a new lchmod entry for the symbolic link throw.

Unblocks: none. No title is known to import lchmod, and I found no homebrew port that calls it in source (Porpoise's tools/core-imports.py routes its cores' fchmodat, not lchmod).

Since this has a FreeBSD source and a firmware syscall table reading, but no title that is known to call it, where is the line for the title rule? Would a sourced FreeBSD syscall like this be accepted, or only one seen in a relinked title's imports? If it is not acceptable, please close it, and I will resubmit it when a title needs it.

Tested

Linux (GitHub Codespace, 4 cores, native build of this branch on main cecb540a6):

  • guest_kernel_sys_tests: 5/5 runs pass.
  • Negative control: with Export.cpp and Export.Discovery.cpp from main, guest_kernel_sys_tests aborts with what(): lchmod_nid_postfix not implemented. 5/5 pass again after restoring them.

Not tested: Windows and macOS (the symbolic link case differs there and is not built here), a console, a title run.

Checks:

  • python3 tools/check_conventions.py --base origin/main passes.
  • tools/progress.py: unchanged (4609/10947 done on base and head; the counter tracks the exported symbol, which is present as a stub on main and as the implementation here).

Duplicate and overlap check

Searches of open and closed pull requests for lchmod find none that implements it, and none of the open libkernel_sys or libkernel pull requests changes it in its diff. #9103 (chown, lchown, fchownat, linkat) removes the neighbouring stubs in Export.Discovery.cpp.

python3 tools/pr_overlap.py --branch feat/libkernel-sys-lchmod reports text conflicts on neighbouring lines, none on the same export or the same new file:

  • #9103 (chown, lchown, fchownat, linkat): removes the adjacent stubs in Export.Discovery.cpp where the lchmod stub was.
  • #6525 (fpathconf): adds a debt entry next to the new lchmod one in docs/dev/debt/libkernel_sys.md.

Whichever lands second keeps both changes. It could not compare #5021, #5528, #6044 and #7201, which conflict with main on Export.cpp or GuestKernelSys.cpp. Checked by hand: none of them touches lchmod (#5528 is readlink path length, #6044 and #7201 are test and macOS build changes, #5021 is the AOT compiler).

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 (no title is known to import lchmod)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4953808174 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9200). Originally posted by [KernelPanicNT](https://github.com/KernelPanicNT) on 2026-10-21T15:08:24Z. --- ### What `lchmod(path, mode)` in libkernel_sys now changes a file's mode without following a final symbolic link, as FreeBSD 11.4's `sys_lchmod` does. Until now it was one of the throwing stubs that #8837 added to `Export.Discovery.cpp`. It forwards to the existing [`fchmodat`](https://github.com/boykopovar/AnyPS5/blob/cecb540a63f614d2b06cbb557c6d1f6f02475fa3/core/libs/prx/libkernel_sys/Export.cpp#L495-L533) with `AT_FDCWD` and `AT_SYMLINK_NOFOLLOW`, so the path lookup, the error codes (`EFAULT`, `ENOENT`, `ELOOP`, `ENAMETOOLONG`) and the `07777` mask are `fchmodat`'s. **Source of the behaviour.** - FreeBSD 11.4 at `8c639945`: [`sys_lchmod`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_syscalls.c#L2490-L2496) is exactly `kern_fchmodat(td, AT_FDCWD, path, UIO_USERSPACE, mode, AT_SYMLINK_NOFOLLOW)`. [`kern_fchmodat`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_syscalls.c#L2498-L2516) looks the path up with `NOFOLLOW` and calls [`setfmode`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_syscalls.c#L2420-L2440), which masks the mode with `ALLPERMS` (`07777`). The prototype is [`int lchmod(char *path, mode_t mode)`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/syscalls.master#L503), syscall 274. - PS5 firmware 10.01: my reading of the native SELF syscall table in the 10.01 kernel data has syscall 274 `lchmod` with 2 arguments and a native handler, and `nosys` (`compat.lchmod`) in the PS4 SELF table, like the other path calls that only libkernel_sys exports. **What is kept as a throw, and why.** - **A symbolic link on Linux and Windows:** FreeBSD changes the link's own mode, but neither host can. `fchmodat` with `AT_SYMLINK_NOFOLLOW` returns success without a change here, which its debt entry lists as a silent stub. `lchmod` exists for this case, so it throws instead of adding a second silent stub. On macOS `fchmodat` changes the link's mode, and `lchmod` does too. Changes: - `Export.cpp`: `lchmod_nid_postfix(const char*, std::uint16_t)`. Outside macOS it resolves the path first and throws for a symbolic link (on Windows, any reparse point, as `fchmodat` does); then it calls `fchmodat_nid_postfix(AT_FDCWD, path, mode, AT_SYMLINK_NOFOLLOW)`. - `Export.Discovery.cpp`: the `lchmod` stub is removed. - `guest_kernel_sys`: `lchmod` of a file by host path and by guest path (`0600`, then `0100644` masked to `0644`), a missing file (`ENOENT`), a null path (`EFAULT`) and an empty one (`ENOENT`); outside Windows, a lookup through a link loop (`ELOOP`), a 256-byte component (`ENAMETOOLONG`), and a symbolic link that throws on Linux (changes the link on macOS) while the file it points to keeps its mode. - `docs/dev/debt/libkernel_sys.md`: a new `lchmod` entry for the symbolic link throw. Unblocks: none. No title is known to import `lchmod`, and I found no homebrew port that calls it in source (Porpoise's `tools/core-imports.py` routes its cores' `fchmodat`, not `lchmod`). Since this has a FreeBSD source and a firmware syscall table reading, but no title that is known to call it, where is the line for the title rule? Would a sourced FreeBSD syscall like this be accepted, or only one seen in a relinked title's imports? If it is not acceptable, please close it, and I will resubmit it when a title needs it. ### Tested Linux (GitHub Codespace, 4 cores, native build of this branch on `main` `cecb540a6`): - `guest_kernel_sys_tests`: 5/5 runs pass. - Negative control: with `Export.cpp` and `Export.Discovery.cpp` from `main`, `guest_kernel_sys_tests` aborts with `what(): lchmod_nid_postfix not implemented`. 5/5 pass again after restoring them. Not tested: Windows and macOS (the symbolic link case differs there and is not built here), a console, a title run. Checks: - `python3 tools/check_conventions.py --base origin/main` passes. - `tools/progress.py`: unchanged (4609/10947 done on base and head; the counter tracks the exported symbol, which is present as a stub on `main` and as the implementation here). ### Duplicate and overlap check Searches of open and closed pull requests for `lchmod` find none that implements it, and none of the open libkernel_sys or libkernel pull requests changes it in its diff. #9103 (chown, lchown, fchownat, linkat) removes the neighbouring stubs in `Export.Discovery.cpp`. `python3 tools/pr_overlap.py --branch feat/libkernel-sys-lchmod` reports text conflicts on neighbouring lines, none on the same export or the same new file: - #9103 (chown, lchown, fchownat, linkat): removes the adjacent stubs in `Export.Discovery.cpp` where the `lchmod` stub was. - #6525 (fpathconf): adds a debt entry next to the new `lchmod` one in `docs/dev/debt/libkernel_sys.md`. Whichever lands second keeps both changes. It could not compare #5021, #5528, #6044 and #7201, which conflict with `main` on `Export.cpp` or `GuestKernelSys.cpp`. Checked by hand: none of them touches `lchmod` (#5528 is readlink path length, #6044 and #7201 are test and macOS build changes, #5021 is the AOT compiler). ### 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 (no title is known to import `lchmod`) - [x] GPUs tested: n/a - [x] AI-assisted: yes
ycbuzi commented 2026-10-21 20:49:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T20:49:23Z.


Windows run of 0858e024fb merged onto main 0c6a5bd593 (8378a6fb3c, 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 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. On Windows the extended guest_kernel_sys block calls lchmod on a file by host and guest path, on a missing file, a null path and an empty one; the symbolic link case and the mode checks are built only outside Windows, so the reparse-point branch is not exercised here. Not run: a title.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6326238911 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9200#issuecomment-6326238911). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T20:49:23Z. --- Windows run of 0858e024fb merged onto `main` 0c6a5bd593 (8378a6fb3c, 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 `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. On Windows the extended `guest_kernel_sys` block calls `lchmod` on a file by host and guest path, on a missing file, a null path and an empty one; the symbolic link case and the mode checks are built only outside Windows, so the reparse-point branch is not exercised here. Not run: a title.
Gruzzberry commented 2026-10-22 06:49:16 +00:00 (Migrated from github.com)

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


#9442 removes the lchflags stub right next to the lchmod stub this PR removes in Export.Discovery.cpp, so the two deletions are adjacent; text only, whichever lands second rebases.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6334634456 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9200#issuecomment-6334634456). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-22T06:49:16Z. --- #9442 removes the `lchflags` stub right next to the `lchmod` stub this PR removes in `Export.Discovery.cpp`, so the two deletions are adjacent; text only, whichever lands second rebases.
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!9200
No description provided.