fix(libkernel): fail getsockname and getpeername with EFAULT on an unusable pointer #11281

Merged
Gruzzberry merged 1 commit from upstream-pr/11281 into main 2026-11-02 10:27:30 +00:00
Gruzzberry commented 2026-10-25 23:40:10 +00:00 (Migrated from github.com)

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


What

getsockname and getpeername wrote the address and the length through the guest pointers directly, so an unreadable or unwritable one crashed the host instead of failing. FreeBSD 11.4's getsockname1 and getpeername1 copy the length in first (copyin, EFAULT, ahead of the descriptor lookup and its EBADF), look the socket up (ENOTCONN for an unconnected getpeername), copy the address out only when its truncated length is not 0 (EFAULT for an unwritable or null address), and copy the length out last (EFAULT). 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. A null address with a zero length now succeeds, as on FreeBSD, where it used to fail with EFAULT; a null address with a non-zero length still fails with EFAULT. The results for readable pointers are otherwise unchanged. accept is left alone (#7485 and #7507 cover its pointers).

Tested

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

  • guest_tcp: new SocketNameFaults, for both calls, an unreadable and a null length (EFAULT, also with the descriptor -1, which pins the order against EBADF), the descriptor -1 with readable pointers (EBADF), an unreadable, a null and a read-only address with the length left unchanged (EFAULT), a length on a read-only page after the address was written (EFAULT), a null or unreadable address with a zero length (success, length 0), and a readable call (16 bytes); plus an unconnected socket (ENOTCONN, and EFAULT for an unreadable length ahead of it). 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).
  • guest_select passed in that full run but now fails on this machine at line 143 (20 selects of 1 ms must take under 200 ms), as it does in other branches of mine that do not touch sockets: the host timer resolution, not this change.
  • 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).
  • #11264 (sysctl) and #11273 (signal calls), both mine, add the same GuestCopyIn_nid_no_patch lines: they were opened first, so they go 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, #11264 and #11273 (the shared helper 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:5009841244 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11281). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-25T23:40:10Z. --- ### What `getsockname` and `getpeername` wrote the address and the length through the guest pointers directly, so an unreadable or unwritable one crashed the host instead of failing. FreeBSD 11.4's [`getsockname1` and `getpeername1`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c) copy the length in first (`copyin`, `EFAULT`, ahead of the descriptor lookup and its `EBADF`), look the socket up (`ENOTCONN` for an unconnected `getpeername`), copy the address out only when its truncated length is not 0 (`EFAULT` for an unwritable or null address), and copy the length out last (`EFAULT`). 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`. A null address with a zero length now succeeds, as on FreeBSD, where it used to fail with `EFAULT`; a null address with a non-zero length still fails with `EFAULT`. The results for readable pointers are otherwise unchanged. `accept` is left alone (#7485 and #7507 cover its pointers). ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main`. - `guest_tcp`: new `SocketNameFaults`, for both calls, an unreadable and a null length (`EFAULT`, also with the descriptor -1, which pins the order against `EBADF`), the descriptor -1 with readable pointers (`EBADF`), an unreadable, a null and a read-only address with the length left unchanged (`EFAULT`), a length on a read-only page after the address was written (`EFAULT`), a null or unreadable address with a zero length (success, length 0), and a readable call (16 bytes); plus an unconnected socket (`ENOTCONN`, and `EFAULT` for an unreadable length ahead of it). 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`). - `guest_select` passed in that full run but now fails on this machine at line 143 (20 selects of 1 ms must take under 200 ms), as it does in other branches of mine that do not touch sockets: the host timer resolution, not this change. - `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). - #11264 (`sysctl`) and #11273 (signal calls), both mine, add the same `GuestCopyIn_nid_no_patch` lines: they were opened first, so they go 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, #11264 and #11273 (the shared helper 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:41:49 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


The Overlaps section says this PR must follow #10975, #11264 and #11273 because they add the same GuestCopyIn_nid_no_patch helper, but its checklist says Depends on: none and then lists those PRs only after a semicolon. CONTRIBUTING.md expects the prerequisites on the checklist line. Please set Depends on: #10975, #11264, #11273 (plus any other actual prerequisites) or revise the stated order if it is not required. I have not compared or reused code from any of those branches.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645954724 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11281#pullrequestreview-5645954724). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:41:49Z. **Review:** CHANGES_REQUESTED --- The Overlaps section says this PR must follow #10975, #11264 and #11273 because they add the same `GuestCopyIn_nid_no_patch` helper, but its checklist says `Depends on: none` and then lists those PRs only after a semicolon. `CONTRIBUTING.md` expects the prerequisites on the checklist line. Please set `Depends on: #10975, #11264, #11273` (plus any other actual prerequisites) or revise the stated order if it is not required. I have not compared or reused code from any of those branches.
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: CHANGES_REQUESTED


Re-reviewed exact head 09746b556e6fca208357b1cb60c397a5e06125a8. The checklist still says Depends on: none while Overlaps states this must follow #10975, #11264 and #11273; please put the actual prerequisites on that checklist line. I also found a behavior mismatch in GuestAddress() / CopyAddressOut(): GuestAddress() writes the copied byte count into *length, and that value is returned to the guest. The PR cites FreeBSD 11.4, where the truncated length is used for copying the address, then the full sockaddr length is returned. Therefore capacity 0 should succeed with required length 16 for IPv4 (28 for IPv6), and a short buffer should still report the full required length. The current tests expect 0; please preserve capacity separately, return required length, and add zero/short-buffer assertions. Hosted eligibility, conventions, intake, labels and report checks pass; ASan and Ubuntu relinker pass, with Windows/macOS jobs pending. The focused Windows run is author-reported.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645974646 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11281#pullrequestreview-5645974646). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:45:15Z. **Review:** CHANGES_REQUESTED --- Re-reviewed exact head `09746b556e6fca208357b1cb60c397a5e06125a8`. The checklist still says `Depends on: none` while Overlaps states this must follow #10975, #11264 and #11273; please put the actual prerequisites on that checklist line. I also found a behavior mismatch in `GuestAddress()` / `CopyAddressOut()`: `GuestAddress()` writes the copied byte count into `*length`, and that value is returned to the guest. The PR cites FreeBSD 11.4, where the truncated length is used for copying the address, then the full sockaddr length is returned. Therefore capacity 0 should succeed with required length 16 for IPv4 (28 for IPv6), and a short buffer should still report the full required length. The current tests expect 0; please preserve capacity separately, return required length, and add zero/short-buffer assertions. Hosted eligibility, conventions, intake, labels and report checks pass; ASan and Ubuntu relinker pass, with Windows/macOS jobs pending. The focused Windows run is author-reported.
MarcZX10 commented 2026-10-26 00:05:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T00:05:42Z.

Review: APPROVED


Correction to my earlier review: my zero/short-buffer finding was wrong. I checked FreeBSD 11.4's kern_getsockname and kern_getpeername implementations directly: both set *alen = MIN(*alen, (*sa)->sa_len), and the syscall copies that truncated length back. The PR's zero-capacity expectation is therefore consistent with the cited 11.4 behavior. Thanks for pointing me to the correct kernel path. The dependency checklist is also now consistent with the overlap section.

I re-reviewed exact head 09746b556e6fca208357b1cb60c397a5e06125a8. The new wrapper copies the input length before descriptor lookup, uses that capacity to bound the address copy, and writes back the truncated length last; the tests cover the pointer faults and error ordering described. Current hosted conventions, intake, eligibility, labels, relinker, ASan, register-context, and write-watch checks pass; product Linux/Windows jobs are skipped by path policy. The focused Windows guest_tcp run is author-reported, not independently reproduced here. I found no remaining blocker and approve this head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5646088735 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11281#pullrequestreview-5646088735). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T00:05:42Z. **Review:** APPROVED --- Correction to my earlier review: my zero/short-buffer finding was wrong. I checked FreeBSD 11.4's `kern_getsockname` and `kern_getpeername` implementations directly: both set `*alen = MIN(*alen, (*sa)->sa_len)`, and the syscall copies that truncated length back. The PR's zero-capacity expectation is therefore consistent with the cited 11.4 behavior. Thanks for pointing me to the correct kernel path. The dependency checklist is also now consistent with the overlap section. I re-reviewed exact head `09746b556e6fca208357b1cb60c397a5e06125a8`. The new wrapper copies the input length before descriptor lookup, uses that capacity to bound the address copy, and writes back the truncated length last; the tests cover the pointer faults and error ordering described. Current hosted conventions, intake, eligibility, labels, relinker, ASan, register-context, and write-watch checks pass; product Linux/Windows jobs are skipped by path policy. The focused Windows `guest_tcp` run is author-reported, not independently reproduced here. I found no remaining blocker and approve this head.
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!11281
No description provided.