fix(libkernel): validate address family against socket family in bind #9169

Merged
slopgobblercobbler merged 1 commit from upstream-pr/9169 into main 2026-10-25 02:17:00 +00:00
slopgobblercobbler commented 2026-10-21 14:09:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slopgobblercobbler on 2026-10-21T14:09:54Z.


What

bind now validates that the sockaddr family matches the socket family (native.ss_family != (socket->family == 2 ? AF_INET : AF_INET6)), returning -1 with EAFNOSUPPORT (47) on mismatch like FreeBSD 11.4 and consistent with connect and sendto. Also extended GuestSocketAddressLength.cpp with tests for family mismatches and unknown address families across bind, connect, and sendto.

Tested

Windows 10/11 x64:

  • GuestSocketAddressLength.cpp verifies:
    • bind with an AF_INET6 address on an AF_INET socket fails with EAFNOSUPPORT (47).
    • bind with an AF_INET address on an AF_INET6 socket fails with EAFNOSUPPORT (47).
    • connect and sendto family mismatch paths fail with EAFNOSUPPORT (47).
    • Unknown address families fail with EAFNOSUPPORT (47).
    • bind with a short AF_INET6 address (< 28) fails with EINVAL (22).
  • python tools/check_conventions.py --base upstream/main --head HEAD: All conventions checks passed (0 findings, 0 errors).
  • Autonomous agent test suite: 42 passed in 6.54s.

Checklist

  • Based on current main; no other open PR implements the same functions
  • 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); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4952979839 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9169). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-10-21T14:09:54Z. --- ### What `bind` now validates that the sockaddr family matches the socket family (`native.ss_family != (socket->family == 2 ? AF_INET : AF_INET6)`), returning `-1` with `EAFNOSUPPORT` (47) on mismatch like FreeBSD 11.4 and consistent with `connect` and `sendto`. Also extended `GuestSocketAddressLength.cpp` with tests for family mismatches and unknown address families across `bind`, `connect`, and `sendto`. ### Tested Windows 10/11 x64: - `GuestSocketAddressLength.cpp` verifies: - `bind` with an AF_INET6 address on an AF_INET socket fails with `EAFNOSUPPORT` (47). - `bind` with an AF_INET address on an AF_INET6 socket fails with `EAFNOSUPPORT` (47). - `connect` and `sendto` family mismatch paths fail with `EAFNOSUPPORT` (47). - Unknown address families fail with `EAFNOSUPPORT` (47). - `bind` with a short AF_INET6 address (< 28) fails with `EINVAL` (22). - `python tools/check_conventions.py --base upstream/main --head HEAD`: All conventions checks passed (0 findings, 0 errors). - Autonomous agent test suite: 42 passed in 6.54s. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [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`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
xXPBSXx commented 2026-10-21 16:44:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T16:44:19Z.


Linux data point for ad7d9abec4: WSL2 Ubuntu 24.04 (virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, BUILD_TESTING=ON, PR head alone (not merged onto main; its merge-base is 1fd67185b).

  • With the PR: ctest -R '^guest_socket_address_length$' passes 3 of 3.
  • With libkernel/Socket/src/Udp.cpp restored to the merge-base (the PR's tests kept): fails (Subprocess aborted).
    Not tested: macOS, Windows, a title run.
    AI-assisted (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6321373816 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9169#issuecomment-6321373816). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T16:44:19Z. --- Linux data point for `ad7d9abec4`: WSL2 Ubuntu 24.04 (virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, `BUILD_TESTING=ON`, PR head alone (not merged onto `main`; its merge-base is `1fd67185b`). - With the PR: `ctest -R '^guest_socket_address_length$'` passes 3 of 3. - With `libkernel/Socket/src/Udp.cpp` restored to the merge-base (the PR's tests kept): fails (`Subprocess aborted`). Not tested: macOS, Windows, a title run. AI-assisted (Claude Code).
harisibrar1 commented 2026-10-24 12:56:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:56:53Z.

Review: COMMENTED


Checked against main 99feaf0eb8 and FreeBSD 11.4 (releng/11.4) sys/netinet/in_pcb.c, tcp_usrreq.c, udp_usrreq.c, sys/netinet6/in6_pcb.c, udp6_usrreq.c:

  • This still conflicts with main in Udp.cpp bind and in GuestSocketAddressLength.cpp (3-way git merge-tree), so the rebase requested above is still needed.
  • One correction to the earlier note about family 99: FreeBSD 11.4 does not check the family on an AF_INET bind. tcp_usr_bind (tcp_usrreq.c:301-310) and udp_bind only test sa_len, and the sin_family != AF_INET check in in_pcbbind_setup sits under #ifdef notdef (in_pcb.c:584-591, "old programs incorrectly fail to initialize it"). So bind(AF_INET socket, {16, 99, ...}, 16) binds on FreeBSD 11.4, while connect does fail with EAFNOSUPPORT (in_pcbconnect_setup, in_pcb.c:1031-1032). Main already returns 47 for that bind from Address(), so the new assertion pins behaviour the source disagrees with. Could you drop it or link a console measurement?
  • After rebasing, the new line is only reached for an AF_INET6 socket given an AF_INET address: on AF_INET sockets, Address() already returns 47 for unknown families and LongAddressError returns 22 for 28-byte AF_INET6 addresses. For AF_INET6, in6_pcbbind checks sa_len != 28 (EINVAL) before sa_family != AF_INET6 (EAFNOSUPPORT) (in6_pcb.c:141-147), so 47 is right for a 28-byte AF_INET address and 22 for a 16-byte one.
  • Could the precedence you settle on also go into the bind/connect/sendto entry in docs/dev/debt/libkernel.md (line 213), since none of it is measured on a console?

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638320112 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9169#pullrequestreview-5638320112). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:56:53Z. **Review:** COMMENTED --- Checked against main 99feaf0eb8 and FreeBSD 11.4 (releng/11.4) `sys/netinet/in_pcb.c`, `tcp_usrreq.c`, `udp_usrreq.c`, `sys/netinet6/in6_pcb.c`, `udp6_usrreq.c`: - This still conflicts with main in `Udp.cpp` bind and in `GuestSocketAddressLength.cpp` (3-way `git merge-tree`), so the rebase requested above is still needed. - One correction to the earlier note about family 99: FreeBSD 11.4 does not check the family on an AF_INET bind. `tcp_usr_bind` (tcp_usrreq.c:301-310) and `udp_bind` only test `sa_len`, and the `sin_family != AF_INET` check in `in_pcbbind_setup` sits under `#ifdef notdef` (in_pcb.c:584-591, "old programs incorrectly fail to initialize it"). So `bind(AF_INET socket, {16, 99, ...}, 16)` binds on FreeBSD 11.4, while `connect` does fail with `EAFNOSUPPORT` (`in_pcbconnect_setup`, in_pcb.c:1031-1032). Main already returns 47 for that bind from `Address()`, so the new assertion pins behaviour the source disagrees with. Could you drop it or link a console measurement? - After rebasing, the new line is only reached for an AF_INET6 socket given an AF_INET address: on AF_INET sockets, `Address()` already returns 47 for unknown families and `LongAddressError` returns 22 for 28-byte AF_INET6 addresses. For AF_INET6, `in6_pcbbind` checks `sa_len != 28` (EINVAL) before `sa_family != AF_INET6` (EAFNOSUPPORT) (in6_pcb.c:141-147), so 47 is right for a 28-byte AF_INET address and 22 for a 16-byte one. - Could the precedence you settle on also go into the bind/connect/sendto entry in `docs/dev/debt/libkernel.md` (line 213), since none of it is measured on a console? Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9169
No description provided.