feat(libkernel_sys): implement kldload argument and privilege checks #11216

Merged
Gruzzberry merged 1 commit from upstream-pr/11216 into main 2026-11-02 10:27:14 +00:00
Gruzzberry commented 2026-10-25 21:58:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-25T21:58:26Z.


What

kldload was a throwing stub in libkernel_sys. FreeBSD 11.4's sys_kldload copies the name in with copyinstr (MAXPATHLEN), then kern_kldload checks securelevel_gt and priv_check(td, PRIV_KLD_LOAD) before any search or open. A title runs jailed and not as root, and prison_priv_check grants a jail no PRIV_KLD_LOAD, so every name that was copied in gives EPERM. Here the name is copied with CopyGuestPath_nid_no_patch: a null or unreadable pointer gives EFAULT and 1024 bytes or more ENAMETOOLONG, both before the privilege check, as in FreeBSD. Nothing is looked up or read afterwards, so a missing, an existing, a directory and a non-ELF name all give EPERM. The new debt entry records that this follows the jailed-caller model, taken from prison_priv_check and not measured on a console.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on current main (rebased).

  • guest_kernel_sys: new checks for a null name and the unreadable address 0x10, plus the unreadable-page fixture (EFAULT), a name of 1024 bytes (ENAMETOOLONG), and the empty name (copied in successfully, as copyinstr accepts it), a bare module name, a missing path, an existing file, a directory and a path through a file (all EPERM). With the stub on main the test aborts at kldload_nid_postfix not implemented; with this change it passes, three runs out of three.
  • Full suite: compared with a main baseline; only the known environment failures (pr_overlap_encoding, agc_host_import_cut_lifetime, agc_driver_depth_plane_partial_store).
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, a console, a title run. Whether a title's kldload reaches the file lookup is unverified.

Overlaps

  • #9161 (statfs/symlinkat) removes neighbouring stubs in Export.Discovery.cpp and include/Api.hpp and adds code at Export.cpp L534-535: proposed on it that it goes first and this PR rebases.
  • #10799 and #11202 add debt entries at libkernel_sys.md L41: proposed on each that it goes first and this PR rebases.
  • #11031 (getfsstat) and #11034 (swapon), both mine, touch the same debt line and the same test region: they were opened first, so they go first and this PR rebases.

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); 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: #9161; merge order: after #9161, #10799, #11202, #11031 and #11034 (neighbouring lines)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5009122777 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11216). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-25T21:58:26Z. --- ### What `kldload` was a throwing stub in libkernel_sys. FreeBSD 11.4's [`sys_kldload`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_linker.c) copies the name in with `copyinstr` (`MAXPATHLEN`), then `kern_kldload` checks `securelevel_gt` and `priv_check(td, PRIV_KLD_LOAD)` before any search or open. A title runs jailed and not as root, and `prison_priv_check` grants a jail no `PRIV_KLD_LOAD`, so every name that was copied in gives `EPERM`. Here the name is copied with `CopyGuestPath_nid_no_patch`: a null or unreadable pointer gives `EFAULT` and 1024 bytes or more `ENAMETOOLONG`, both before the privilege check, as in FreeBSD. Nothing is looked up or read afterwards, so a missing, an existing, a directory and a non-ELF name all give `EPERM`. The new debt entry records that this follows the jailed-caller model, taken from `prison_priv_check` and not measured on a console. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main` (rebased). - `guest_kernel_sys`: new checks for a null name and the unreadable address `0x10`, plus the unreadable-page fixture (`EFAULT`), a name of 1024 bytes (`ENAMETOOLONG`), and the empty name (copied in successfully, as `copyinstr` accepts it), a bare module name, a missing path, an existing file, a directory and a path through a file (all `EPERM`). With the stub on `main` the test aborts at `kldload_nid_postfix not implemented`; with this change it passes, three runs out of three. - Full suite: compared with a `main` baseline; only the known environment failures (`pr_overlap_encoding`, `agc_host_import_cut_lifetime`, `agc_driver_depth_plane_partial_store`). - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, a console, a title run. Whether a title's `kldload` reaches the file lookup is unverified. ### Overlaps - #9161 (`statfs`/`symlinkat`) removes neighbouring stubs in `Export.Discovery.cpp` and `include/Api.hpp` and adds code at `Export.cpp` L534-535: proposed on it that it goes first and this PR rebases. - #10799 and #11202 add debt entries at `libkernel_sys.md` L41: proposed on each that it goes first and this PR rebases. - #11031 (`getfsstat`) and #11034 (`swapon`), both mine, touch the same debt line and the same test region: they were opened first, so they go first and this PR rebases. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [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`); 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: #9161; merge order: after #9161, #10799, #11202, #11031 and #11034 (neighbouring lines) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/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!11216
No description provided.