fix(libkernel): reject socket addresses longer than sockaddr_in or sockaddr_in6 with EINVAL #9108

Merged
Retr0G28 merged 1 commit from upstream-pr/9108 into main 2026-10-23 20:12:40 +00:00
Retr0G28 commented 2026-10-21 12:32:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-21T12:32:14Z.


What

bind and connect accepted a socket address longer than sockaddr_in or sockaddr_in6. FreeBSD 11.4's getsockaddr sets sa_len to the length argument (sys/kern/uipc_syscalls.c), and every bind and connect path for TCP and UDP compares it with the size of the socket family's address and answers EINVAL when they differ: tcp_usr_bind, tcp_usr_connect, tcp6_usr_bind and tcp6_usr_connect (sys/netinet/tcp_usrreq.c), in_pcbbind_setup and in_pcbconnect_setup (sys/netinet/in_pcb.c, reached from udp_bind and udp_connect), and in6_pcbbind and in6_pcbladdr (sys/netinet6/in6_pcb.c, reached from udp6_bind and udp6_connect). udp6_send (sys/netinet6/udp6_usrreq.c) makes the same test before anything else when sendto gives an address.

bind and connect on an AF_INET or AF_INET6 socket now fail with EINVAL when the length is above 16 or 28 bytes. connect makes the test after the listening-socket check (EOPNOTSUPP in soconnectat comes first) and before the port-0 check, as in_pcbconnect_setup does. The one path FreeBSD leaves unchecked is kept: on an AF_INET6 datagram socket with IPV6_V6ONLY clear, udp6_bind and udp6_connect hand an IPv4-mapped address to the IPv4 code as a fresh sockaddr_in, so the length is not tested there. sendto on an AF_INET6 datagram socket fails a longer address with EINVAL too, including an IPv4-mapped one.

sendto on an AF_INET datagram socket is left alone, because udp_output (sys/netinet/udp_usrreq.c) tests the length only when the socket has no local address or port or the destination is INADDR_ANY or INADDR_BROADCAST. Stream sendto is also left alone: FreeBSD checks the length only during the implied connect of an unconnected socket. The debt entry for bind, connect and sendto in docs/dev/debt/libkernel.md now names just these two sendto cases.

Test changes:

  • guest_socket_address_length binds and connects with every length from 17 (AF_INET) or 29 (AF_INET6) up to 255, on stream and datagram sockets, and expects EINVAL. The same sockets still bind and connect with the exact length. On an AF_INET6 datagram socket, sendto with every longer length fails and sendto with the exact length sends.
  • A 29-byte IPv4-mapped address fails bind with IPV6_V6ONLY set and binds with it cleared. sendto with that address still fails.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, -DBUILD_TESTING=ON, on main 11bb53a347.

  • Before the fix, guest_socket_address_length with the new cases fails (exit 0xc0000409): bind with an address longer than sockaddr_in or sockaddr_in6 must fail with EINVAL (22), because a 17-byte AF_INET bind succeeds. With this PR it passes.
  • ctest -R "socket" -E "^agc_": 27 of 27 pass when run serially.
  • python tools/check_conventions.py --base origin/main reports no errors.
  • Not tested on Linux. The checks run before any host call and use getsockopt(IPV6_V6ONLY), which both hosts have, so Linux CI should give the same result.
  • No title or homebrew run.

Overlap: tools/pr_overlap.py reports textual conflicts in Udp.cpp with #8499 (port-0 sendto from an unbound IPv4 datagram socket) and #8682 (wildcard local address after a UDP association is dissolved). Both add code next to the new helper and the bind and sendto lines, and neither checks address length. #8392 (address arguments checked before the descriptor) moves the short-length, ENAMETOOLONG and EFAULT checks ahead of the descriptor lookup and merges cleanly with this PR. The PRs it could not compare that touch Udp.cpp (#5021, #6977, #7088, #7201, #8483, #8670) do not change these checks. The rest touch only other lines of docs/dev/debt/libkernel.md. #9105 (connect to port 0 on a connected datagram socket) also changes connect in Udp.cpp, and the two branches merge without conflict.

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4951587236 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9108). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-10-21T12:32:14Z. --- ### What `bind` and `connect` accepted a socket address longer than `sockaddr_in` or `sockaddr_in6`. FreeBSD 11.4's `getsockaddr` sets `sa_len` to the length argument (`sys/kern/uipc_syscalls.c`), and every bind and connect path for TCP and UDP compares it with the size of the socket family's address and answers `EINVAL` when they differ: `tcp_usr_bind`, `tcp_usr_connect`, `tcp6_usr_bind` and `tcp6_usr_connect` (`sys/netinet/tcp_usrreq.c`), `in_pcbbind_setup` and `in_pcbconnect_setup` (`sys/netinet/in_pcb.c`, reached from `udp_bind` and `udp_connect`), and `in6_pcbbind` and `in6_pcbladdr` (`sys/netinet6/in6_pcb.c`, reached from `udp6_bind` and `udp6_connect`). `udp6_send` (`sys/netinet6/udp6_usrreq.c`) makes the same test before anything else when `sendto` gives an address. `bind` and `connect` on an `AF_INET` or `AF_INET6` socket now fail with `EINVAL` when the length is above 16 or 28 bytes. `connect` makes the test after the listening-socket check (`EOPNOTSUPP` in `soconnectat` comes first) and before the port-0 check, as `in_pcbconnect_setup` does. The one path FreeBSD leaves unchecked is kept: on an `AF_INET6` datagram socket with `IPV6_V6ONLY` clear, `udp6_bind` and `udp6_connect` hand an IPv4-mapped address to the IPv4 code as a fresh `sockaddr_in`, so the length is not tested there. `sendto` on an `AF_INET6` datagram socket fails a longer address with `EINVAL` too, including an IPv4-mapped one. `sendto` on an `AF_INET` datagram socket is left alone, because `udp_output` (`sys/netinet/udp_usrreq.c`) tests the length only when the socket has no local address or port or the destination is `INADDR_ANY` or `INADDR_BROADCAST`. Stream `sendto` is also left alone: FreeBSD checks the length only during the implied connect of an unconnected socket. The debt entry for `bind`, `connect` and `sendto` in `docs/dev/debt/libkernel.md` now names just these two `sendto` cases. Test changes: - `guest_socket_address_length` binds and connects with every length from 17 (`AF_INET`) or 29 (`AF_INET6`) up to 255, on stream and datagram sockets, and expects `EINVAL`. The same sockets still bind and connect with the exact length. On an `AF_INET6` datagram socket, `sendto` with every longer length fails and `sendto` with the exact length sends. - A 29-byte IPv4-mapped address fails `bind` with `IPV6_V6ONLY` set and binds with it cleared. `sendto` with that address still fails. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, `-DBUILD_TESTING=ON`, on `main` 11bb53a347. - Before the fix, `guest_socket_address_length` with the new cases fails (exit 0xc0000409): `bind with an address longer than sockaddr_in or sockaddr_in6 must fail with EINVAL (22)`, because a 17-byte `AF_INET` bind succeeds. With this PR it passes. - ctest `-R "socket" -E "^agc_"`: 27 of 27 pass when run serially. - `python tools/check_conventions.py --base origin/main` reports no errors. - Not tested on Linux. The checks run before any host call and use `getsockopt(IPV6_V6ONLY)`, which both hosts have, so Linux CI should give the same result. - No title or homebrew run. Overlap: `tools/pr_overlap.py` reports textual conflicts in `Udp.cpp` with #8499 (port-0 `sendto` from an unbound IPv4 datagram socket) and #8682 (wildcard local address after a UDP association is dissolved). Both add code next to the new helper and the `bind` and `sendto` lines, and neither checks address length. #8392 (address arguments checked before the descriptor) moves the short-length, `ENAMETOOLONG` and `EFAULT` checks ahead of the descriptor lookup and merges cleanly with this PR. The PRs it could not compare that touch `Udp.cpp` (#5021, #6977, #7088, #7201, #8483, #8670) do not change these checks. The rest touch only other lines of `docs/dev/debt/libkernel.md`. #9105 (connect to port 0 on a connected datagram socket) also changes `connect` in `Udp.cpp`, and the two branches merge without conflict. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!9108
No description provided.