fix(libkernel): fail sysctl and sysctlbyname with EFAULT on an unreadable or unwritable pointer #11264

Merged
Gruzzberry merged 1 commit from upstream-pr/11264 into main 2026-10-27 20:16:22 +00:00
Gruzzberry commented 2026-10-25 23:01:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-25T23:01:31Z.


What

sysctl read its name array and its oldlenp length and wrote its value and the length back through raw guest pointers, so an unreadable or unwritable one crashed the host instead of failing. FreeBSD 11.4's sys___sysctl range-checks namelen (EINVAL outside 2 to 24), copies the name in (copyin, EFAULT), and userland_sysctl copies *oldlenp in (EFAULT) before the lookup, so an unreadable name or length fails with EFAULT ahead of ENOENT for an unknown MIB. After the handler, sysctl_old_user copies the value out (EFAULT for an unwritable oldp when at least one byte fits; ENOMEM when the buffer is short, with *oldlenp set to the bytes copied) and sys___sysctl copies the length out last (EFAULT). sysctl now does the same with GuestCopyIn_nid_no_patch (new, the read counterpart of GuestCopyOut_nid_no_patch) and GuestCopyOut_nid_no_patch, in that order, for the shared copy-out helper and for kern.arandom. Results and errnos for readable pointers are unchanged. sysctlbyname copies its name in with CopyGuestPath_nid_no_patch (EFAULT for an unreadable or unterminated name, ENAMETOOLONG from 1024 bytes, as sysctl_sysctl_name2oid does for MAXPATHLEN). FreeBSD's libc runs strlen on that name in user space (sysctlbyname calls sysctlnametomib), so on a console an unmapped name crashes the process instead; failing with EFAULT here avoids crashing the host, as the null-name check already did. sysctlnametomib still reads its name and size directly, and a new debt entry records it.

Tested

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

  • guest_system_configuration: new CheckSysctlPointerFaults for an unreadable 0x10 and a null name, an unreadable oldlenp (with a readable MIB and with the measured-absent MIB {1, 6}, which pins EFAULT before ENOENT), an unreadable oldp with the length left unchanged, an unreadable oldp with a zero length (ENOMEM, no fault), an oldlenp on a read-only page (value copied, then EFAULT; the same with a null oldp), namelen 1 and 25 with an unreadable name (EINVAL), and, on kern.arandom, an unreadable oldp and an unreadable oldlenp; for sysctlbyname, an unreadable 0x10 name, a null name, a name whose terminator would lie on an inaccessible page, and an unreadable oldp and oldlenp after a valid name. On main this test segfaults; 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). guest_libc_cnd_timed_wait, guest_pthread_start and video_out_flip_eop_aftergpu failed once under ctest -j 12 and pass alone three times out of three.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, a console, a title run.

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).
  • #10921 changes the memcpy in CopyOut that this PR replaces, and #11170 edits the test main list: proposed on each that it goes 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: #10975; merge order: after #10975, #10921 and #11170 (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:5009576930 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11264). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-25T23:01:31Z. --- ### What `sysctl` read its `name` array and its `oldlenp` length and wrote its value and the length back through raw guest pointers, so an unreadable or unwritable one crashed the host instead of failing. FreeBSD 11.4's [`sys___sysctl`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_sysctl.c) range-checks `namelen` (`EINVAL` outside 2 to 24), copies the name in (`copyin`, `EFAULT`), and `userland_sysctl` copies `*oldlenp` in (`EFAULT`) before the lookup, so an unreadable name or length fails with `EFAULT` ahead of `ENOENT` for an unknown MIB. After the handler, `sysctl_old_user` copies the value out (`EFAULT` for an unwritable `oldp` when at least one byte fits; `ENOMEM` when the buffer is short, with `*oldlenp` set to the bytes copied) and `sys___sysctl` copies the length out last (`EFAULT`). `sysctl` now does the same with `GuestCopyIn_nid_no_patch` (new, the read counterpart of `GuestCopyOut_nid_no_patch`) and `GuestCopyOut_nid_no_patch`, in that order, for the shared copy-out helper and for `kern.arandom`. Results and errnos for readable pointers are unchanged. `sysctlbyname` copies its name in with `CopyGuestPath_nid_no_patch` (`EFAULT` for an unreadable or unterminated name, `ENAMETOOLONG` from 1024 bytes, as `sysctl_sysctl_name2oid` does for `MAXPATHLEN`). FreeBSD's libc runs `strlen` on that name in user space (`sysctlbyname` calls `sysctlnametomib`), so on a console an unmapped name crashes the process instead; failing with `EFAULT` here avoids crashing the host, as the null-name check already did. `sysctlnametomib` still reads its name and `size` directly, and a new debt entry records it. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main`. - `guest_system_configuration`: new `CheckSysctlPointerFaults` for an unreadable `0x10` and a null name, an unreadable `oldlenp` (with a readable MIB and with the measured-absent MIB `{1, 6}`, which pins `EFAULT` before `ENOENT`), an unreadable `oldp` with the length left unchanged, an unreadable `oldp` with a zero length (`ENOMEM`, no fault), an `oldlenp` on a read-only page (value copied, then `EFAULT`; the same with a null `oldp`), `namelen` 1 and 25 with an unreadable name (`EINVAL`), and, on `kern.arandom`, an unreadable `oldp` and an unreadable `oldlenp`; for `sysctlbyname`, an unreadable `0x10` name, a null name, a name whose terminator would lie on an inaccessible page, and an unreadable `oldp` and `oldlenp` after a valid name. On `main` this test segfaults; 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`). `guest_libc_cnd_timed_wait`, `guest_pthread_start` and `video_out_flip_eop_aftergpu` failed once under `ctest -j 12` and pass alone three times out of three. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, a console, a title run. ### 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). - #10921 changes the `memcpy` in `CopyOut` that this PR replaces, and #11170 edits the test `main` list: proposed on each that it goes 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: #10975; merge order: after #10975, #10921 and #11170 (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)
MarcZX10 commented 2026-10-25 23:03:35 +00:00 (Migrated from github.com)

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

Review: COMMENTED


The sysctl MIB and output-pointer handling is clearly scoped and the Windows regression cases are useful. One remaining related gap: sysctlbyname_nid_postfix still dereferences its guest name with strcmp after checking only for null, before it reaches the checked sysctl path. An unmapped non-null name therefore still faults the host instead of returning -1 with EFAULT. Please either copy/validate the guest string before these comparisons (with a bound and a cross-page regression) or explicitly narrow this PR's scope and track the by-name fault separately. This PR is still a draft; I did not cast a formal vote. Local git diff --check and tools/check_conventions.py pass; the hosted policy checks were still running at review time.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645797645 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11264#pullrequestreview-5645797645). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:03:35Z. **Review:** COMMENTED --- The sysctl MIB and output-pointer handling is clearly scoped and the Windows regression cases are useful. One remaining related gap: `sysctlbyname_nid_postfix` still dereferences its guest `name` with `strcmp` after checking only for null, before it reaches the checked sysctl path. An unmapped non-null name therefore still faults the host instead of returning `-1` with `EFAULT`. Please either copy/validate the guest string before these comparisons (with a bound and a cross-page regression) or explicitly narrow this PR's scope and track the by-name fault separately. This PR is still a draft; I did not cast a formal vote. Local `git diff --check` and `tools/check_conventions.py` pass; the hosted policy checks were still running at review time.
MarcZX10 commented 2026-10-25 23:19:36 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


The new sysctl MIB path validates its pointers, but the adjacent sysctlbyname_nid_postfix path still dereferences a non-null guest name with strcmp before reaching the checked copy-out code. An unmapped pointer therefore still faults the host, so the stated unreadable-pointer handling is incomplete for the name-based API. Please copy/validate the guest string with a bounded read before comparing it and add an unmapped/cross-page regression, or narrow the PR title and report to the numeric-MIB API and track the sysctlbyname name-pointer fault as explicit debt. The focused Windows tests are author-reported; hosted relinker and sanitizer checks pass, while full product jobs are skipped. I ran exact-head git diff --check and tools/check_conventions.py --base upstream/main; both pass. This blocks approval until the exposed scope is accurate and the raw guest dereference is handled or explicitly tracked.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645848067 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11264#pullrequestreview-5645848067). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:19:36Z. **Review:** CHANGES_REQUESTED --- The new `sysctl` MIB path validates its pointers, but the adjacent `sysctlbyname_nid_postfix` path still dereferences a non-null guest `name` with `strcmp` before reaching the checked copy-out code. An unmapped pointer therefore still faults the host, so the stated unreadable-pointer handling is incomplete for the name-based API. Please copy/validate the guest string with a bounded read before comparing it and add an unmapped/cross-page regression, or narrow the PR title and report to the numeric-MIB API and track the `sysctlbyname` name-pointer fault as explicit debt. The focused Windows tests are author-reported; hosted relinker and sanitizer checks pass, while full product jobs are skipped. I ran exact-head `git diff --check` and `tools/check_conventions.py --base upstream/main`; both pass. This blocks approval until the exposed scope is accurate and the raw guest dereference is handled or explicitly tracked.
MarcZX10 commented 2026-10-25 23:57:24 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


The prior sysctlbyname name-pointer issue is addressed at this head: the name now goes through CopyGuestPath_nid_no_patch, with unmapped, null and cross-page-unterminated regressions. Local exact-head convention and whitespace checks pass.

Please correct the dependency checklist before approval. The body says #10975, #10921 and #11170 must go first and this PR will rebase, but the checklist says Depends on: none. All three are still open. I fetched their current heads and git merge-tree reports conflicts between this PR and #10921 in SystemConfiguration.cpp, and between this PR and #11170 in both SystemConfiguration.cpp and GuestSystemConfiguration.cpp; #10975 also supplies the same GuestCopyIn_nid_no_patch declaration/definition this PR adds. Please list the required first PR in the Depends on: #N checklist field (and retain any additional ordering detail separately), or rebase/resolve the overlap and update the checklist to match the resulting merge order. The current conventions job is pending; the local convention check passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5646054874 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11264#pullrequestreview-5646054874). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:57:24Z. **Review:** CHANGES_REQUESTED --- The prior `sysctlbyname` name-pointer issue is addressed at this head: the name now goes through `CopyGuestPath_nid_no_patch`, with unmapped, null and cross-page-unterminated regressions. Local exact-head convention and whitespace checks pass. Please correct the dependency checklist before approval. The body says #10975, #10921 and #11170 must go first and this PR will rebase, but the checklist says `Depends on: none`. All three are still open. I fetched their current heads and `git merge-tree` reports conflicts between this PR and #10921 in `SystemConfiguration.cpp`, and between this PR and #11170 in both `SystemConfiguration.cpp` and `GuestSystemConfiguration.cpp`; #10975 also supplies the same `GuestCopyIn_nid_no_patch` declaration/definition this PR adds. Please list the required first PR in the `Depends on: #N` checklist field (and retain any additional ordering detail separately), or rebase/resolve the overlap and update the checklist to match the resulting merge order. The current conventions job is pending; the local convention check passes.
MarcZX10 commented 2026-10-26 05:40:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T05:40:37Z.

Review: DISMISSED


The checklist mismatch from my previous review is fixed, and the exact-head Windows/Linux focused test report is now present. I am keeping changes requested only because this head is 1 ahead / 196 behind current main and depends on the still-open #10975, #10921 and #11170 stack. Please refresh onto current main after the required predecessors land, rerun guest_system_configuration on that exact head, and I will re-review. I found no new source-code issue in the unchanged diff.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5647830763 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11264#pullrequestreview-5647830763). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T05:40:37Z. **Review:** DISMISSED --- The checklist mismatch from my previous review is fixed, and the exact-head Windows/Linux focused test report is now present. I am keeping changes requested only because this head is 1 ahead / 196 behind current main and depends on the still-open #10975, #10921 and #11170 stack. Please refresh onto current main after the required predecessors land, rerun `guest_system_configuration` on that exact head, and I will re-review. I found no new source-code issue in the unchanged diff.
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!11264
No description provided.