Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Gruzzberry:fix/sockname-faults
Nov 2, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Gruzzberry:fix/sockname-faults

Conversation

@Gruzzberry

@Gruzzberry Gruzzberry commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

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

Checklist

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…usable pointer

FreeBSD 11.4's getsockname1 and getpeername1 copy the length in before
looking up the descriptor, copy the address out only when its length is
not 0, and copy the length out last; a bad pointer gives EFAULT. The
address was written through the guest pointer directly.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

One additional behavioral mismatch: GuestAddress() sets *length to the number of bytes copied (min(input capacity, native sockaddr length)). CopyAddressOut() writes that value back, so the new zero-capacity case returns length 0, and a short buffer reports only its copied size. FreeBSD 11.4's user_getsockname() / user_getpeername() uses the truncated length only for the address copy, then resets the output length to the full ss.ss_len before copying it back (source). Thus a null address with capacity 0 should succeed and still report 16 bytes for IPv4 (28 for IPv6); a short nonzero buffer should likewise report the full required size. Please preserve the capacity separately from the returned required length and add zero/short-buffer tests—the current zero-length assertion expects the wrong result for the FreeBSD behavior this PR cites.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Gruzzberry
Gruzzberry marked this pull request as ready for review October 25, 2026 23:58
@Gruzzberry

Copy link
Copy Markdown
Contributor Author

Checklist fixed (Depends on: #10975, with the full order after it). On the returned length: the 11.4 source I read (release/11.4.0 sys/kern/uipc_syscalls.c, kern_getsockname) does len = MIN(*alen, (*sa)->sa_len); *alen = len; and getsockname1 copies that len back, so on 11.4 capacity 0 returns length 0 and a short buffer returns the truncated length. Returning the full required length is the behaviour of later FreeBSD releases (the 12.x change to kern_getsockname), not 11.4. getpeername has the same MIN in kern_getpeername. The tests therefore expect 0; I kept it.

@Gruzzberry

Copy link
Copy Markdown
Contributor Author

Correction to my last comment: I wrote that later FreeBSD releases return the full length, and that is wrong. I checked release/12.0.0 and release/13.0.0 and kern_getsockname keeps the same len = MIN(*alen, (*sa)->sa_len); *alen = len;. So the length written back is the truncated one in 11.4 and later, and the tests (capacity 0 gives 0) stand.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 14310 core/libs/prx/libkernel/Socket/src/Udp.cpp L1597-1598

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 9948 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp

@wachi35676

Copy link
Copy Markdown
Contributor

Windows and Linux runs of head 09746b556e6f merged onto main 0f34ebb85. Windows used WinLibs GCC 15.2.0, Linux used WSL with GCC 13; both Release. The description lists Linux as not tested.

AI-assisted: yes (Claude Code); results above were run on this machine.

@XatiX

XatiX commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

This overlaps with #14310 at the top of getsockname in Udp.cpp, and both changes have to be there. I don't think either needs to go first, so I've left Depends on: none. If #14310 lands second, I'll put its forward check after your length copy-in, so the EFAULT order stays as you have it. If yours lands second, the same resolution applies. If you see a reason for a set order, tell me and I'll declare it.

@oneandonlydean
oneandonlydean merged commit 4ad64dc into boykopovar:main Nov 2, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants