fix(libkernel): fail bind and connect with EFAULT on an unreadable address #17253

Merged
Gruzzberry merged 1 commit from upstream-pr/17253 into main 2026-11-16 20:29:46 +00:00
Gruzzberry commented 2026-11-08 16:19:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-08T16:19:32Z.


What

bind and connect read the guest sockaddr through the raw pointer after only checking the length and the null pointer, so an unusable address such as 0x10 or an unreadable page crashed the host. FreeBSD 11.4's sys_bind and sys_connect call getsockaddr before they look the descriptor up: a length above SOCK_MAXADDRLEN (255) gives ENAMETOOLONG, a length below the size of sa_family gives EINVAL, and an address that cannot be copied in gives EFAULT. Both functions now copy the address into a local 255-byte buffer with GuestCopyIn_nid_no_patch right after AddressLengthError, return EFAULT when that fails and use the copy from then on, which is what sendto already does. The existing order is kept, so bind(-1, ...) and connect(-1, ...) with an unreadable address give EFAULT before EBADF, as in the kernel, and every later read stays within the copied length.

Tested

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

  • guest_socket_address_length: new checks that bind and connect fail with EFAULT for the address 0x10 and an unreadable page on an invalid descriptor, on a stream socket and on a datagram socket. With Socket/src/Udp.cpp reverted to main the test fails (an unreadable address on a valid socket crashed the host, and on descriptor -1 main answered EBADF); with this change it passes, three runs out of three.
  • The other socket, UDP, TCP, connect, bind, poll and select tests that build here (91 tests) pass.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run, the full suite.

Overlaps

  • #12688 (preserve blocking sockets over nonblocking native transport), #12700 and #12965 (a stack by one author) rewrite connect_nid_postfix in Socket/src/Udp.cpp, where this PR adds three lines at the top of connect and bind. They already conflict with main, so pr_overlap.py did not compare them. This PR goes first and they rebase onto it (three lines each); asked on #12688.
  • #17248 (mine) edits GuestSockets::Readv and Writev in the same file, far from bind and connect. #11896 changes recv; #12682, #15785, #16826, #7088 and #5021 touch other parts of Udp.cpp.

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: none; merge order: before #12688, #12700, #12965
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5212949316 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17253). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-08T16:19:32Z. --- ### What `bind` and `connect` read the guest `sockaddr` through the raw pointer after only checking the length and the null pointer, so an unusable address such as `0x10` or an unreadable page crashed the host. FreeBSD 11.4's `sys_bind` and `sys_connect` call [`getsockaddr`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c) before they look the descriptor up: a length above `SOCK_MAXADDRLEN` (255) gives `ENAMETOOLONG`, a length below the size of `sa_family` gives `EINVAL`, and an address that cannot be copied in gives `EFAULT`. Both functions now copy the address into a local 255-byte buffer with `GuestCopyIn_nid_no_patch` right after `AddressLengthError`, return `EFAULT` when that fails and use the copy from then on, which is what `sendto` already does. The existing order is kept, so `bind(-1, ...)` and `connect(-1, ...)` with an unreadable address give `EFAULT` before `EBADF`, as in the kernel, and every later read stays within the copied `length`. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on `main` f5fe807353. - `guest_socket_address_length`: new checks that `bind` and `connect` fail with `EFAULT` for the address `0x10` and an unreadable page on an invalid descriptor, on a stream socket and on a datagram socket. With `Socket/src/Udp.cpp` reverted to `main` the test fails (an unreadable address on a valid socket crashed the host, and on descriptor -1 `main` answered `EBADF`); with this change it passes, three runs out of three. - The other socket, UDP, TCP, connect, bind, poll and select tests that build here (91 tests) pass. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run, the full suite. ### Overlaps - #12688 (`preserve blocking sockets over nonblocking native transport`), #12700 and #12965 (a stack by one author) rewrite `connect_nid_postfix` in `Socket/src/Udp.cpp`, where this PR adds three lines at the top of `connect` and `bind`. They already conflict with `main`, so `pr_overlap.py` did not compare them. This PR goes first and they rebase onto it (three lines each); asked on #12688. - #17248 (mine) edits `GuestSockets::Readv` and `Writev` in the same file, far from `bind` and `connect`. #11896 changes `recv`; #12682, #15785, #16826, #7088 and #5021 touch other parts of `Udp.cpp`. ### 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: none; merge order: before #12688, #12700, #12965 - [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)
gabichuuuuu commented 2026-11-09 08:52:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-09T08:52:32Z.

Review: COMMENTED


Windows run of head 9ca464d50a, merged onto main 286b14d2f7 in one tree with 47 other small open PRs that touch different files. Setup: Windows 11, WinLibs GCC 15.2 r7 (x86_64-ucrt-posix-seh, the CI toolchain), Release, BUILD_TESTING=ON.

  • guest_socket_address_length with --repeat until-fail:5: 5 / 5 pass.
  • Negative control, with main's Udp.cpp and this PR's test: 5 / 5 runs fail.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5823552878 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17253#pullrequestreview-5823552878). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-09T08:52:32Z. **Review:** COMMENTED --- Windows run of head `9ca464d50a`, merged onto `main` `286b14d2f7` in one tree with 47 other small open PRs that touch different files. Setup: Windows 11, WinLibs GCC 15.2 r7 (`x86_64-ucrt-posix-seh`, the CI toolchain), Release, `BUILD_TESTING=ON`. - `guest_socket_address_length` with `--repeat until-fail:5`: 5 / 5 pass. - **Negative control,** with `main`'s `Udp.cpp` and this PR's test: 5 / 5 runs fail.
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!17253
No description provided.