fix(libkernel): check socket address families and lengths as measured on a PS5 #10299

Merged
orezraey merged 10 commits from upstream-pr/10299 into main 2026-10-25 15:45:06 +00:00
orezraey commented 2026-10-23 16:37:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-23T16:37:46Z.


What

socket, bind and connect measured on a retail PS5 (firmware 13.60) through libkernel_sys.sprx from a payload process. The source and console log are in this gist (pinet; the socket option cases in the same log are for a separate PR). The debt entries had these answers from the FreeBSD 11.4 source only.

The console:

  • fails an AF_INET address of any length but 16 with EINVAL, 17, 28 and 255 included, and an AF_INET6 address of any length but 28 (24, 27, 29, 64); 256 is still ENAMETOOLONG;

  • fails another family on an AF_INET socket with EAFNOSUPPORT (AF_INET6, AF_UNIX, AF_UNSPEC), and takes the length, not sa_len, as the size;

  • disconnects a connected datagram socket when connect to port 0 fails with EADDRNOTAVAIL, so a following send fails with EDESTADDRREQ (#9105 has since landed this on main; this PR now only adds the measurement to its debt entry);

  • fails SOCK_SEQPACKET like SOCK_RDM and an unknown type: EPROTOTYPE with protocol 0, EPROTONOSUPPORT with another.

AnyPS5 accepted longer addresses, checked the length before the socket's family, and failed SOCK_SEQPACKET with EPROTONOSUPPORT for protocol 0. Address now takes the socket's family and checks it in the order FreeBSD's udp_bind and in6_pcbbind do; connect still refuses a listening socket with EOPNOTSUPP first, as soconnect does. The one exception, kept from #9108, is an IPv4-mapped address longer than 28 bytes on an AF_INET6 datagram socket with IPV6_V6ONLY cleared: bind and now connect use its first 28 bytes, as FreeBSD's udp6_bind and udp6_connect hand it to in_pcbbind and in_pcbconnect as a sockaddr_in; the console was measured only with ::1 and IPV6_V6ONLY at its default, so this has its own debt entry, and sendto still fails it with EINVAL. Every address case was measured on datagram sockets, so the address debt entry says stream sockets follow the same order unmeasured, and the order tests use datagram sockets. No probe gave bind or connect on an AF_INET socket another family with a wrong length, so their family-first order is listed as unmeasured too (FreeBSD 11.4's tcp_usr_bind and tcp_usr_connect check the length first) and is not tested. A connected stream socket's sendto now checks only the address length and ignores the address, as FreeBSD's tcp_usr_send does once the connection has started, instead of failing a 17-byte address with EINVAL. AF_UNSPEC was tried only with 127.0.0.1, so its INADDR_ANY bind, which FreeBSD accepts, is listed as unmeasured. The socket, address and port 0 debt entries now say what was measured and what still differs (SOCK_RAW, IPPROTO_UDPLITE and AF_UNIX sockets, which the console created).

Tested

Windows 11, MinGW (winlibs), and Linux (WSL Ubuntu, GCC), at head 20b38adb09 (main merged again after #9363 landed; only the mlockall and socket debt lines met, keeping main's mlockall wording and this PR's measured socket entry). At this head the 30 Windows tests matching ^guest_(socket|udp|tcp) and the six Linux tests below pass three runs in a row. Earlier, main was merged after #9169 landed in 65e712b9c6. #9169's MismatchedAndUnknownFamiliesFail is kept with its unknown-family cases and the AF_INET address on an AF_INET6 socket; its three cases that order the length and family checks of an AF_INET socket the other way (bind and connect of a 28-byte AF_INET6 address on a stream socket expecting EINVAL, sendto of one on a datagram socket expecting EAFNOSUPPORT) were dropped, because the console fails sendto with EINVAL, as LongAddressesAndOtherFamiliesFail checks; the bind and connect order of a wrong family and length on an AF_INET socket was not measured, so the debt entry lists it as unmeasured and no test asserts it.

  • Windows: the 30 tests matching ^guest_(socket|udp|tcp) pass three runs in a row. Linux: guest_socket_address_length, guest_socket_port_zero, guest_udp, guest_tcp, guest_socket_connect_listening and guest_socket_error pass three runs in a row.

  • guest_socket_address_length checks the longer addresses and the other families, and #9108's long mapped bind (plus connect) on a dual-stack datagram socket, and sendto with a 17-byte address on a connected stream socket (it fails without the change); guest_socket_port_zero checks the disconnect, with #9105's test kept after merging main. The socket tests pass three runs in a row (guest_kqueue failed under -j 8 each time and passes alone, as on main).

  • The full suite (-E "^agc_|^select_tests$") passes apart from windows_diagnostic_fixture_isolation, which timed out under load and passes alone.

  • tools/check_conventions.py passes.

  • tools/pr_overlap.py: #7475, #8728, #8921, #9105, #9108, #9169, #9363 touch nearby lines; whichever lands second keeps both.

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 (attach them to this PR)

  • New third-party code is a submodule built from source

  • Generic behaviour, not specific to one title

  • Depends on: #7475 (merge order only: it edits the same lines; this PR is refreshed after it; #9363 has merged); #11002 and #11042 edit the debt entries next to this PR's (merge order only)

  • Fixes: none

  • Unblocks: none

  • GPUs tested: n/a

  • AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4986097451 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10299). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-23T16:37:46Z. --- ### What `socket`, `bind` and `connect` measured on a retail PS5 (firmware 13.60) through `libkernel_sys.sprx` from a payload process. The source and console log are in [this gist](https://gist.github.com/orezraey/d9efe0da49405f19d72cb35279c0db52) (`pinet`; the socket option cases in the same log are for a separate PR). The debt entries had these answers from the FreeBSD 11.4 source only. The console: - fails an `AF_INET` address of any length but 16 with `EINVAL`, 17, 28 and 255 included, and an `AF_INET6` address of any length but 28 (24, 27, 29, 64); 256 is still `ENAMETOOLONG`; - fails another family on an `AF_INET` socket with `EAFNOSUPPORT` (`AF_INET6`, `AF_UNIX`, `AF_UNSPEC`), and takes the length, not `sa_len`, as the size; - disconnects a connected datagram socket when `connect` to port 0 fails with `EADDRNOTAVAIL`, so a following `send` fails with `EDESTADDRREQ` (#9105 has since landed this on `main`; this PR now only adds the measurement to its debt entry); - fails `SOCK_SEQPACKET` like `SOCK_RDM` and an unknown type: `EPROTOTYPE` with protocol 0, `EPROTONOSUPPORT` with another. AnyPS5 accepted longer addresses, checked the length before the socket's family, and failed `SOCK_SEQPACKET` with `EPROTONOSUPPORT` for protocol 0. `Address` now takes the socket's family and checks it in the order FreeBSD's `udp_bind` and `in6_pcbbind` do; `connect` still refuses a listening socket with `EOPNOTSUPP` first, as `soconnect` does. The one exception, kept from #9108, is an IPv4-mapped address longer than 28 bytes on an `AF_INET6` datagram socket with `IPV6_V6ONLY` cleared: `bind` and now `connect` use its first 28 bytes, as FreeBSD's `udp6_bind` and `udp6_connect` hand it to `in_pcbbind` and `in_pcbconnect` as a `sockaddr_in`; the console was measured only with `::1` and `IPV6_V6ONLY` at its default, so this has its own debt entry, and `sendto` still fails it with `EINVAL`. Every address case was measured on datagram sockets, so the address debt entry says stream sockets follow the same order unmeasured, and the order tests use datagram sockets. No probe gave `bind` or `connect` on an `AF_INET` socket another family with a wrong length, so their family-first order is listed as unmeasured too (FreeBSD 11.4's `tcp_usr_bind` and `tcp_usr_connect` check the length first) and is not tested. A connected stream socket's `sendto` now checks only the address length and ignores the address, as FreeBSD's `tcp_usr_send` does once the connection has started, instead of failing a 17-byte address with `EINVAL`. `AF_UNSPEC` was tried only with 127.0.0.1, so its `INADDR_ANY` bind, which FreeBSD accepts, is listed as unmeasured. The `socket`, address and port 0 debt entries now say what was measured and what still differs (`SOCK_RAW`, `IPPROTO_UDPLITE` and `AF_UNIX` sockets, which the console created). ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, MinGW (winlibs), and Linux (WSL Ubuntu, GCC), at head 20b38adb09 (`main` merged again after #9363 landed; only the `mlockall` and `socket` debt lines met, keeping `main`'s `mlockall` wording and this PR's measured `socket` entry). At this head the 30 Windows tests matching `^guest_(socket|udp|tcp)` and the six Linux tests below pass three runs in a row. Earlier, `main` was merged after #9169 landed in 65e712b9c6. #9169's `MismatchedAndUnknownFamiliesFail` is kept with its unknown-family cases and the `AF_INET` address on an `AF_INET6` socket; its three cases that order the length and family checks of an `AF_INET` socket the other way (`bind` and `connect` of a 28-byte `AF_INET6` address on a stream socket expecting `EINVAL`, `sendto` of one on a datagram socket expecting `EAFNOSUPPORT`) were dropped, because the console fails `sendto` with `EINVAL`, as `LongAddressesAndOtherFamiliesFail` checks; the `bind` and `connect` order of a wrong family and length on an `AF_INET` socket was not measured, so the debt entry lists it as unmeasured and no test asserts it. - Windows: the 30 tests matching `^guest_(socket|udp|tcp)` pass three runs in a row. Linux: `guest_socket_address_length`, `guest_socket_port_zero`, `guest_udp`, `guest_tcp`, `guest_socket_connect_listening` and `guest_socket_error` pass three runs in a row. - `guest_socket_address_length` checks the longer addresses and the other families, and #9108's long mapped `bind` (plus `connect`) on a dual-stack datagram socket, and `sendto` with a 17-byte address on a connected stream socket (it fails without the change); `guest_socket_port_zero` checks the disconnect, with #9105's test kept after merging `main`. The socket tests pass three runs in a row (`guest_kqueue` failed under `-j 8` each time and passes alone, as on `main`). - The full suite (`-E "^agc_|^select_tests$"`) passes apart from `windows_diagnostic_fixture_isolation`, which timed out under load and passes alone. - `tools/check_conventions.py` passes. - `tools/pr_overlap.py`: #7475, #8728, #8921, #9105, #9108, #9169, #9363 touch nearby lines; whichever lands second keeps both. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #7475 (merge order only: it edits the same lines; this PR is refreshed after it; #9363 has merged); #11002 and #11042 edit the debt entries next to this PR's (merge order only) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 05:19:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T05:19:11Z.

Review: COMMENTED


Checked against main 7966e590ef, FreeBSD 11.4/13.2 sources (in_pcb.c, udp_usrreq.c, tcp_usrreq.c, in6_pcb.c, udp6_usrreq.c) and the pinet/pinet5 logs in your gist:

  • Thanks for the probes. Every datagram case in pinet.log and pinet5.log matches the new Address order, and the length/EFAULT checks still come before EBADF/ENOTSOCK. The console looks like a hybrid: family-first on AF_INET bind/connect matches 13.x udp_bind/udp_connect (11.4's in_pcbbind_setup has the family check under #ifdef notdef), while length-first on AF_INET6 and on AF_INET sendto matches 11.4. The measurement is the right source here.
  • All the address cases in pinet.c/pinet5.c use SOCK_DGRAM. For TCP the sources disagree: 11.4 tcp_usr_bind/tcp_usr_connect check the length first and ignore the family, while 13.2 tcp6_usr_bind/tcp6_usr_connect check the family first. Could the debt entry say the order was measured on datagram sockets only? GuestSocketAddressLength.cpp:95,98,102 assert the stream order as if it were measured.
  • sendto on a connected stream socket: FreeBSD (11.4 tcp_usr_send L955, 13.2 L1021) only reads the address while t_state < TCPS_SYN_SENT. With this PR, sendto(connected_tcp, "x", 1, 0, &sin, 17) now fails with EINVAL from Address(..., true) (Udp.cpp:1429), while main accepted the 17-byte address and passed it on to the host. sendmsg forwards to it too. Maybe skip the address checks for a connected stream socket, or keep the old debt note that stream sendto isn't checked.
  • Small one: bind with sa_family == AF_UNSPEC and INADDR_ANY is accepted by both 11.4 (no family check) and 13.2 (the udp_bind/tcp_usr_bind compatibility branch). The probe only tried AF_UNSPEC with 127.0.0.1, so I'd mark that case as unmeasured in the debt entry rather than listing it under "another family fails with EAFNOSUPPORT".

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5636983840 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10299#pullrequestreview-5636983840). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T05:19:11Z. **Review:** COMMENTED --- Checked against main 7966e590ef, FreeBSD 11.4/13.2 sources (`in_pcb.c`, `udp_usrreq.c`, `tcp_usrreq.c`, `in6_pcb.c`, `udp6_usrreq.c`) and the `pinet`/`pinet5` logs in your gist: - Thanks for the probes. Every datagram case in `pinet.log` and `pinet5.log` matches the new `Address` order, and the length/EFAULT checks still come before EBADF/ENOTSOCK. The console looks like a hybrid: family-first on AF_INET bind/connect matches 13.x `udp_bind`/`udp_connect` (11.4's `in_pcbbind_setup` has the family check under `#ifdef notdef`), while length-first on AF_INET6 and on AF_INET `sendto` matches 11.4. The measurement is the right source here. - All the address cases in `pinet.c`/`pinet5.c` use `SOCK_DGRAM`. For TCP the sources disagree: 11.4 `tcp_usr_bind`/`tcp_usr_connect` check the length first and ignore the family, while 13.2 `tcp6_usr_bind`/`tcp6_usr_connect` check the family first. Could the debt entry say the order was measured on datagram sockets only? `GuestSocketAddressLength.cpp:95,98,102` assert the stream order as if it were measured. - `sendto` on a connected stream socket: FreeBSD (11.4 `tcp_usr_send` L955, 13.2 L1021) only reads the address while `t_state < TCPS_SYN_SENT`. With this PR, `sendto(connected_tcp, "x", 1, 0, &sin, 17)` now fails with EINVAL from `Address(..., true)` (Udp.cpp:1429), while main accepted the 17-byte address and passed it on to the host. `sendmsg` forwards to it too. Maybe skip the address checks for a connected stream socket, or keep the old debt note that stream `sendto` isn't checked. - Small one: bind with `sa_family == AF_UNSPEC` and `INADDR_ANY` is accepted by both 11.4 (no family check) and 13.2 (the `udp_bind`/`tcp_usr_bind` compatibility branch). The probe only tried `AF_UNSPEC` with 127.0.0.1, so I'd mark that case as unmeasured in the debt entry rather than listing it under "another family fails with EAFNOSUPPORT". Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-10-24 21:18:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T21:18:35Z.

Review: CHANGES_REQUESTED


The previously identified behavior gaps now appear addressed in this head: datagram tests distinguish family/length precedence, the connected-stream sendto case is covered, and the unmeasured AF_UNSPEC/stream scope is recorded. The Windows socket test runs are useful and accurately report the unrelated guest_kqueue parallel failure.

Please correct one test-report discrepancy: the body says Linux is covered by CI, but the current hosted linux and windows product checks are both SKIPPED; the reported socket runs are local Windows runs. Either record Linux as not tested or add an actual Linux run after integration.

The declared merge-order PRs #7475, #9363 and #9169 are all still open. This branch is based on 7f3e661, 6,670 commits behind current main, and the changed socket/test/debt paths have moved since. After the stated order is resolved, please refresh onto current main, preserve the measured cases and run the focused socket tests plus conventions/overlap checks on the final head. I found no new functional issue in the current diff.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640070534 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10299#pullrequestreview-5640070534). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T21:18:35Z. **Review:** CHANGES_REQUESTED --- The previously identified behavior gaps now appear addressed in this head: datagram tests distinguish family/length precedence, the connected-stream `sendto` case is covered, and the unmeasured `AF_UNSPEC`/stream scope is recorded. The Windows socket test runs are useful and accurately report the unrelated `guest_kqueue` parallel failure. Please correct one test-report discrepancy: the body says Linux is covered by CI, but the current hosted `linux` and `windows` product checks are both `SKIPPED`; the reported socket runs are local Windows runs. Either record Linux as not tested or add an actual Linux run after integration. The declared merge-order PRs #7475, #9363 and #9169 are all still open. This branch is based on `7f3e661`, 6,670 commits behind current `main`, and the changed socket/test/debt paths have moved since. After the stated order is resolved, please refresh onto current `main`, preserve the measured cases and run the focused socket tests plus conventions/overlap checks on the final head. I found no new functional issue in the current diff.
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!10299
No description provided.