fix(libkernel): fail the signal action, mask, stack and pending calls with EFAULT on an unusable pointer #11273

Merged
Gruzzberry merged 1 commit from upstream-pr/11273 into main 2026-10-27 16:55:36 +00:00
Gruzzberry commented 2026-10-25 23:19:09 +00:00 (Migrated from github.com)

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


What

sigaction, sigprocmask (and _sigprocmask, which pthread_sigmask also reaches), sigaltstack and sigpending read and wrote their guest pointers directly, so an unreadable or unwritable one crashed the host instead of failing. FreeBSD 11.4's sys_sigaction, sys_sigprocmask, sys_sigaltstack and sys_sigpending copy the new value in first (copyin, EFAULT), then validate it (EINVAL for a bad signal number, how, or flags; ENOMEM for a small stack), apply it, and only then copy the old value out (EFAULT, with the change already made). The calls now do the same with the new GuestCopyIn_nid_no_patch (the read counterpart of GuestCopyOut_nid_no_patch) and GuestCopyOut_nid_no_patch. The old mask is stored before the handlers of newly unblocked pending signals run, as on FreeBSD. pthread_sigmask returns the error number (EFAULT) rather than -1, leaves errno as it was, and no longer checks how before the kernel path reads the set, so a bad how with an unreadable set gives EFAULT as in sys_sigprocmask. Results and errnos for readable pointers are unchanged. sigsuspend is left alone: its null-mask throw is asserted by guest_signals.

Tested

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

  • guest_signals: new CheckSignalPointerFaults for an unreadable 0x10 action, set and stack and an unreadable or read-only old value (EFAULT), the signal number 0 and how 9 with an unreadable input (EFAULT ahead of EINVAL, pinning the order) and with a readable one (EINVAL), a sigprocmask whose old value is unwritable still applying the block, and a null and unwritable sigpending buffer, and pthread_sigmask returning 14 for an unreadable set (also with a bad how) and for a read-only old mask but 22 for a bad how with a readable set. On main the 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).
  • 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).
  • #3986 (per-thread signal mask) rewrites sigaction, sigaltstack and _sigprocmask in Signals.cpp, #10749 edits the sigaction registration call, and #11002 edits Signals.cpp and GuestSignals.cpp: proposed on each that it goes first and this PR rebases.
  • #11264 (sysctl EFAULT, mine) adds the same GuestCopyIn_nid_no_patch lines: it was opened first, so it goes first and the duplicate drops out when 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, #3986, #10749, #11002 and #11264 (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:5009693852 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11273). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-25T23:19:09Z. --- ### What `sigaction`, `sigprocmask` (and `_sigprocmask`, which `pthread_sigmask` also reaches), `sigaltstack` and `sigpending` read and wrote their guest pointers directly, so an unreadable or unwritable one crashed the host instead of failing. FreeBSD 11.4's [`sys_sigaction`, `sys_sigprocmask`, `sys_sigaltstack` and `sys_sigpending`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_sig.c) copy the new value in first (`copyin`, `EFAULT`), then validate it (`EINVAL` for a bad signal number, `how`, or flags; `ENOMEM` for a small stack), apply it, and only then copy the old value out (`EFAULT`, with the change already made). The calls now do the same with the new `GuestCopyIn_nid_no_patch` (the read counterpart of `GuestCopyOut_nid_no_patch`) and `GuestCopyOut_nid_no_patch`. The old mask is stored before the handlers of newly unblocked pending signals run, as on FreeBSD. `pthread_sigmask` returns the error number (`EFAULT`) rather than -1, leaves `errno` as it was, and no longer checks `how` before the kernel path reads the set, so a bad `how` with an unreadable set gives `EFAULT` as in `sys_sigprocmask`. Results and errnos for readable pointers are unchanged. `sigsuspend` is left alone: its null-mask throw is asserted by `guest_signals`. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main`. - `guest_signals`: new `CheckSignalPointerFaults` for an unreadable `0x10` action, set and stack and an unreadable or read-only old value (`EFAULT`), the signal number 0 and `how` 9 with an unreadable input (`EFAULT` ahead of `EINVAL`, pinning the order) and with a readable one (`EINVAL`), a `sigprocmask` whose old value is unwritable still applying the block, and a null and unwritable `sigpending` buffer, and `pthread_sigmask` returning 14 for an unreadable set (also with a bad `how`) and for a read-only old mask but 22 for a bad `how` with a readable set. On `main` the 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`). - `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). - #3986 (per-thread signal mask) rewrites `sigaction`, `sigaltstack` and `_sigprocmask` in `Signals.cpp`, #10749 edits the `sigaction` registration call, and #11002 edits `Signals.cpp` and `GuestSignals.cpp`: proposed on each that it goes first and this PR rebases. - #11264 (`sysctl` EFAULT, mine) adds the same `GuestCopyIn_nid_no_patch` lines: it was opened first, so it goes first and the duplicate drops out when 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, #3986, #10749, #11002 and #11264 (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:20:35 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


The new raw sigprocmask path returns -1 and sets errno = EFAULT for an unreadable set or previousSet. pthread_sigmask_nid_postfix forwards that result directly, but the POSIX thread API returns the error number itself (for example, EFAULT/14), not -1; FreeBSD's _pthread_sigmask returns errno when its internal operation fails. The new tests cover only sigprocmask_nid_postfix, so they miss this wrapper-specific behavior. Please translate the checked-copy failures to the direct error result in the pthread_sigmask wrapper and add tests for unreadable input/output pointers there, including the wrapper's ordering when how is invalid. I ran exact-head git diff --check and tools/check_conventions.py --base upstream/main; both pass. Hosted policy checks pass, but all cross-platform relinker/sanitizer jobs are still running and full product jobs are skipped; the Windows focused tests are author-reported.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645851163 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11273#pullrequestreview-5645851163). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:20:35Z. **Review:** CHANGES_REQUESTED --- The new raw `sigprocmask` path returns `-1` and sets `errno = EFAULT` for an unreadable `set` or `previousSet`. `pthread_sigmask_nid_postfix` forwards that result directly, but the POSIX thread API returns the error number itself (for example, `EFAULT`/14), not `-1`; FreeBSD's `_pthread_sigmask` returns `errno` when its internal operation fails. The new tests cover only `sigprocmask_nid_postfix`, so they miss this wrapper-specific behavior. Please translate the checked-copy failures to the direct error result in the `pthread_sigmask` wrapper and add tests for unreadable input/output pointers there, including the wrapper's ordering when `how` is invalid. I ran exact-head `git diff --check` and `tools/check_conventions.py --base upstream/main`; both pass. Hosted policy checks pass, but all cross-platform relinker/sanitizer jobs are still running and full product jobs are skipped; the Windows focused tests are author-reported.
MarcZX10 commented 2026-10-25 23:27:38 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


Re-reviewed exact head 35f349ed81852e64ee17f8068526f1bfb551f0be. The new pthread_sigmask error-number translation fixes the prior -1/errno issue, but its invalid-how fast path still runs before reading set: pthread_sigmask_nid_postfix(9, unreadable, nullptr) returns 22, and the new test codifies that result. This conflicts with the PR’s stated FreeBSD ordering. In FreeBSD 11.4, __thr_sigprocmask copies a non-null set for every how except SIG_UNBLOCK, then calls __sys_sigprocmask; the kernel sys_sigprocmask copies in the set before kern_sigprocmask validates how. For how == 9 and an unreadable set, that path therefore returns EFAULT (14). Please remove/defer the wrapper precheck for this case and change the regression to expect 14; keep the readable invalid-how case expecting 22. Source: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libthr/thread/thr_sig.c and https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_sig.c. I ran exact-head git diff --check upstream/main...HEAD and tools/check_conventions.py --base upstream/main; both pass. Hosted policy checks pass, but the refreshed relinker/sanitizer/macOS jobs are still pending, product jobs are skipped, and the focused Windows run is author-reported rather than independently run here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645873782 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11273#pullrequestreview-5645873782). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:27:38Z. **Review:** CHANGES_REQUESTED --- Re-reviewed exact head `35f349ed81852e64ee17f8068526f1bfb551f0be`. The new `pthread_sigmask` error-number translation fixes the prior `-1`/`errno` issue, but its invalid-`how` fast path still runs before reading `set`: `pthread_sigmask_nid_postfix(9, unreadable, nullptr)` returns 22, and the new test codifies that result. This conflicts with the PR’s stated FreeBSD ordering. In FreeBSD 11.4, `__thr_sigprocmask` copies a non-null set for every `how` except `SIG_UNBLOCK`, then calls `__sys_sigprocmask`; the kernel `sys_sigprocmask` copies in the set before `kern_sigprocmask` validates `how`. For `how == 9` and an unreadable set, that path therefore returns `EFAULT` (14). Please remove/defer the wrapper precheck for this case and change the regression to expect 14; keep the readable invalid-`how` case expecting 22. Source: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libthr/thread/thr_sig.c and https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_sig.c. I ran exact-head `git diff --check upstream/main...HEAD` and `tools/check_conventions.py --base upstream/main`; both pass. Hosted policy checks pass, but the refreshed relinker/sanitizer/macOS jobs are still pending, product jobs are skipped, and the focused Windows run is author-reported rather than independently run here.
MarcZX10 commented 2026-10-25 23:41:50 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


Re-reviewed exact head fc123bf178c762ab7a9f467c7478b54a07a3c960. The wrapper now copies set before validating how, and the new pthread_sigmask(9, unreadable, nullptr) == EFAULT regression addresses my prior FreeBSD-ordering finding. The remaining blocking issue is the checklist: the Overlaps section says this PR must follow #10975, #3986, #10749, #11002 and #11264, but Depends on still says none. Please put the required merge-order PRs on that checklist line (or revise the overlap statement if any are no longer prerequisites). I reviewed the current diff; hosted conventions and policy checks pass, cross-platform relinker jobs are still running, product jobs are skipped, and the Windows focused tests remain author-reported rather than independently run.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645954753 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11273#pullrequestreview-5645954753). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:41:50Z. **Review:** CHANGES_REQUESTED --- Re-reviewed exact head `fc123bf178c762ab7a9f467c7478b54a07a3c960`. The wrapper now copies `set` before validating `how`, and the new `pthread_sigmask(9, unreadable, nullptr) == EFAULT` regression addresses my prior FreeBSD-ordering finding. The remaining blocking issue is the checklist: the Overlaps section says this PR must follow #10975, #3986, #10749, #11002 and #11264, but `Depends on` still says `none`. Please put the required merge-order PRs on that checklist line (or revise the overlap statement if any are no longer prerequisites). I reviewed the current diff; hosted conventions and policy checks pass, cross-platform relinker jobs are still running, product jobs are skipped, and the Windows focused tests remain author-reported rather than independently run.
MarcZX10 commented 2026-10-25 23:45:15 +00:00 (Migrated from github.com)

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

Review: DISMISSED


Re-reviewed exact head 25d53dd03f9ddf1d6a0004202ee8d7df7f63d8c6. The new pthread_sigmask ordering and error-number behavior address my prior code findings. One blocking checklist mismatch remains: the Overlaps section says this PR must follow #10975, #3986, #10749, #11002 and #11264, but the checklist still says Depends on: none. Please list the actual required merge-order PRs on the checklist line, or revise the overlap section if any are no longer prerequisites. Current conventions and policy checks pass; cross-platform relinker/sanitizer jobs are still running, and Linux/Windows product jobs are skipped by the workflow. The focused Windows tests are author-reported, not independently run here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645974629 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11273#pullrequestreview-5645974629). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:45:15Z. **Review:** DISMISSED --- Re-reviewed exact head `25d53dd03f9ddf1d6a0004202ee8d7df7f63d8c6`. The new `pthread_sigmask` ordering and error-number behavior address my prior code findings. One blocking checklist mismatch remains: the Overlaps section says this PR must follow #10975, #3986, #10749, #11002 and #11264, but the checklist still says `Depends on: none`. Please list the actual required merge-order PRs on the checklist line, or revise the overlap section if any are no longer prerequisites. Current conventions and policy checks pass; cross-platform relinker/sanitizer jobs are still running, and Linux/Windows product jobs are skipped by the workflow. The focused Windows tests are author-reported, not independently run here.
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!11273
No description provided.