feat(libkernel_sys): implement kldsym argument checks #11231

Merged
Gruzzberry merged 1 commit from upstream-pr/11231 into main 2026-10-28 07:56:23 +00:00
Gruzzberry commented 2026-10-25 22:17:20 +00:00 (Migrated from github.com)

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


What

kldsym was a throwing stub in libkernel_sys. FreeBSD 11.4's sys_kldsym first copies the 32-byte struct kld_sym_lookup in from the guest (an unreadable data gives EFAULT), then fails with EINVAL if version is not sizeof(struct kld_sym_lookup) or the command is not KLDSYM_LOOKUP (1), then copies the symbol name in with copyinstr (MAXPATHLEN; here CopyGuestPath_nid_no_patch, so an unreadable pointer gives EFAULT and 1024 bytes or more ENAMETOOLONG), and only then looks the file up: a file id that no file has gives ENOENT (linker_find_file_by_id). kldload never loads anything here, so the only file assumed to exist is the kernel (id 1, the first the linker numbers): every id other than 0 and 1 gets ENOENT, and a lookup in id 0 (every file) or 1 would read a symbol table that nothing here models, so it throws std::runtime_error. Modules a console preloads would have ids of their own and are not modelled; the debt entry says so. The structure is read with a new GuestCopyIn_nid_no_patch, the read counterpart of GuestCopyOut_nid_no_patch, which returns false for a null or unreadable source. The new debt entry records that kernel symbol lookup by titles is unverified.

Tested

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

  • guest_kernel_sys: new checks next to the chroot ones for a null and an unreadable 0x10 structure (EFAULT), versions 0, 24, 31 and 33 and commands 0, 2 and -1 (EINVAL, also with a null symbol name, which pins the order against the name copy), a null, an unreadable 0x10 and a 1024-byte symbol name (EFAULT, EFAULT, ENAMETOOLONG), and a valid structure with file ids -1, INT_MIN, 2, 3, 100 and INT_MAX (ENOENT). With the body reverted to the stub the test aborts (0xc0000409); with this change it passes, three runs out of three.
  • Full suite: the only failures are the known environment ones (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, and the throwing lookup.

Overlaps

  • #10975 adds the same GuestCopyIn_nid_no_patch declaration and definition, line for line: proposed on it that it goes first and this PR rebases (the duplicate then drops out).
  • #9161 (statfs/symlinkat) adds code at Export.cpp L534-535 and #10799 edits libkernel_sys.md L41: proposed on each that it goes first and this PR rebases.
  • #11031 (getfsstat), #11034 (swapon) and #11216 (kldload), all mine, touch the same debt line, Export.cpp L534 and the chroot 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: #11216; merge order: after #10975, #9161, #10799, #11031, #11034 and #11216 (neighbouring lines, shared test region and debt file)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5009271338 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11231). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-25T22:17:20Z. --- ### What `kldsym` was a throwing stub in libkernel_sys. FreeBSD 11.4's [`sys_kldsym`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_linker.c) first copies the 32-byte `struct kld_sym_lookup` in from the guest (an unreadable `data` gives `EFAULT`), then fails with `EINVAL` if `version` is not `sizeof(struct kld_sym_lookup)` or the command is not `KLDSYM_LOOKUP` (1), then copies the symbol name in with `copyinstr` (`MAXPATHLEN`; here `CopyGuestPath_nid_no_patch`, so an unreadable pointer gives `EFAULT` and 1024 bytes or more `ENAMETOOLONG`), and only then looks the file up: a file id that no file has gives `ENOENT` (`linker_find_file_by_id`). `kldload` never loads anything here, so the only file assumed to exist is the kernel (id 1, the first the linker numbers): every id other than 0 and 1 gets `ENOENT`, and a lookup in id 0 (every file) or 1 would read a symbol table that nothing here models, so it throws `std::runtime_error`. Modules a console preloads would have ids of their own and are not modelled; the debt entry says so. The structure is read with a new `GuestCopyIn_nid_no_patch`, the read counterpart of `GuestCopyOut_nid_no_patch`, which returns false for a null or unreadable source. The new debt entry records that kernel symbol lookup by titles is unverified. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main`. - `guest_kernel_sys`: new checks next to the `chroot` ones for a null and an unreadable `0x10` structure (`EFAULT`), versions 0, 24, 31 and 33 and commands 0, 2 and -1 (`EINVAL`, also with a null symbol name, which pins the order against the name copy), a null, an unreadable `0x10` and a 1024-byte symbol name (`EFAULT`, `EFAULT`, `ENAMETOOLONG`), and a valid structure with file ids -1, INT_MIN, 2, 3, 100 and INT_MAX (`ENOENT`). With the body reverted to the stub the test aborts (`0xc0000409`); with this change it passes, three runs out of three. - Full suite: the only failures are the known environment ones (`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, and the throwing lookup. ### Overlaps - #10975 adds the same `GuestCopyIn_nid_no_patch` declaration and definition, line for line: proposed on it that it goes first and this PR rebases (the duplicate then drops out). - #9161 (`statfs`/`symlinkat`) adds code at `Export.cpp` L534-535 and #10799 edits `libkernel_sys.md` L41: proposed on each that it goes first and this PR rebases. - #11031 (`getfsstat`), #11034 (`swapon`) and #11216 (`kldload`), all mine, touch the same debt line, `Export.cpp` L534 and the `chroot` 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: #11216; merge order: after #10975, #9161, #10799, #11031, #11034 and #11216 (neighbouring lines, shared test region and debt file) - [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)
MarcZX10 commented 2026-10-25 22:19:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T22:19:39Z.

Review: CHANGES_REQUESTED


The current conventions check fails because the checklist parser does not accept Depends on: after #11216 (...); it requires exactly a dependency marker such as Depends on: #11216 (or none). Please put the primary dependency in that form and keep the additional merge-order details on a separate checklist line or in the PR description. I verified the listed PRs (#10975, #9161, #10799, #11031, #11034 and #11216) are open, so retaining their ordering notes is useful. Local git diff --check passes; I have not run the Windows/Linux product tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645637619 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11231#pullrequestreview-5645637619). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T22:19:39Z. **Review:** CHANGES_REQUESTED --- The current `conventions` check fails because the checklist parser does not accept `Depends on: after #11216 (...)`; it requires exactly a dependency marker such as `Depends on: #11216` (or `none`). Please put the primary dependency in that form and keep the additional merge-order details on a separate checklist line or in the PR description. I verified the listed PRs (#10975, #9161, #10799, #11031, #11034 and #11216) are open, so retaining their ordering notes is useful. Local `git diff --check` passes; I have not run the Windows/Linux product tests.
MarcZX10 commented 2026-10-25 23:53:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T23:53:24Z.

Review: DISMISSED


The checklist now declares the dependency order and the exact-head convention check passes. I found one remaining behavioral gap against the linked FreeBSD 11.4 implementation: for every nonzero fileid, sys_kldsym calls linker_find_file_by_id; if no file is found, it returns ENOENT. This implementation returns ENOENT only for negative IDs, then throws for every positive ID. Please clarify how this emulator distinguishes a valid loaded module ID from an unavailable positive ID, and add a focused test for an unavailable positive ID returning ENOENT. If module IDs are not modeled at all, the current positive-ID path appears to be classifying an ordinary missing-module case as unsupported.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5646030783 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11231#pullrequestreview-5646030783). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:53:24Z. **Review:** DISMISSED --- The checklist now declares the dependency order and the exact-head convention check passes. I found one remaining behavioral gap against the linked FreeBSD 11.4 implementation: for every nonzero `fileid`, `sys_kldsym` calls `linker_find_file_by_id`; if no file is found, it returns `ENOENT`. This implementation returns `ENOENT` only for negative IDs, then throws for every positive ID. Please clarify how this emulator distinguishes a valid loaded module ID from an unavailable positive ID, and add a focused test for an unavailable positive ID returning `ENOENT`. If module IDs are not modeled at all, the current positive-ID path appears to be classifying an ordinary missing-module case as unsupported.
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!11231
No description provided.