fix(libkernel): dissolve a UDP association on connect to AF_UNSPEC #4498

Merged
milescoviello merged 1 commit from upstream-pr/4498 into main 2026-10-12 21:01:25 +00:00
milescoviello commented 2026-10-12 13:37:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T13:37:59Z.


What

connect() on a guest datagram socket rejected an address whose family is AF_UNSPEC (a null address) with EAFNOSUPPORT before touching the socket, so the socket stayed connected: getpeername() still returned the old peer, send() without an address still went there, and datagrams from any other sender never arrived.

FreeBSD 11.4 disconnects a connected datagram socket first and only then rejects the address. sys/kern/uipc_socket.c, soconnectat():

	 * If protocol is connection-based, can only connect once.
	 * Otherwise, if connected, try to disconnect first.  This allows
	 * user to disconnect by connecting to, e.g., a null address.
	 */
	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
	    (error = sodisconnect(so)))) {

udp_disconnect() (sys/netinet/udp_usrreq.c) clears the peer and keeps the local port (in_pcbdisconnect(inp); inp->inp_laddr.s_addr = INADDR_ANY;), and in_pcbconnect_setup() (sys/netinet/in_pcb.c) then returns EAFNOSUPPORT for the 16-byte address, the error main already returns. connect(2): "Datagram sockets may dissolve the association by connecting to an invalid address, such as a null address." libuv's uv__udp_disconnect() (src/unix/udp.c) disconnects with exactly this call and expects EAFNOSUPPORT or EINVAL on BSD.

Now a connected internet datagram socket given a family byte of 0 is disconnected on the host first (AF_UNSPEC on Linux, a zeroed address on Windows), and the call then fails as before. Linux also drops an ephemeral local port on that disconnect, so the port is bound again on the wildcard address, which is what FreeBSD leaves. A host failure during the disconnect is returned as the call's error. Stream sockets and the socketpair sockets keep the previous path.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main c6efe068. guest_udp gained a case: a socket bound to 127.0.0.1:0 is connected to a peer, then connected to {16, AF_UNSPEC}; it must fail with EAFNOSUPPORT, then report ENOTCONN from getpeername, keep its local port with the wildcard address (on Linux and macOS, as udp_disconnect() resets inp_laddr to INADDR_ANY), fail send() without an address with EDESTADDRREQ, and receive a datagram from an unrelated socket.

  • Red: with main's Udp.cpp the test aborts (getpeername after the disconnect returns 0 with the old peer; the unrelated datagram never arrives).
  • Green: guest_udp passes; all guest_* tests except guest_video_out_*: 306/306 passed.

Not tested on Windows.

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 in TechnicalDebt
  • 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
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4830218577 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4498). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T13:37:59Z. --- ### What `connect()` on a guest datagram socket rejected an address whose family is `AF_UNSPEC` (a null address) with `EAFNOSUPPORT` before touching the socket, so the socket stayed connected: `getpeername()` still returned the old peer, `send()` without an address still went there, and datagrams from any other sender never arrived. FreeBSD 11.4 disconnects a connected datagram socket first and only then rejects the address. `sys/kern/uipc_socket.c`, `soconnectat()`: ```c * If protocol is connection-based, can only connect once. * Otherwise, if connected, try to disconnect first. This allows * user to disconnect by connecting to, e.g., a null address. */ if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && ((so->so_proto->pr_flags & PR_CONNREQUIRED) || (error = sodisconnect(so)))) { ``` `udp_disconnect()` (`sys/netinet/udp_usrreq.c`) clears the peer and keeps the local port (`in_pcbdisconnect(inp); inp->inp_laddr.s_addr = INADDR_ANY;`), and `in_pcbconnect_setup()` (`sys/netinet/in_pcb.c`) then returns `EAFNOSUPPORT` for the 16-byte address, the error main already returns. `connect(2)`: "Datagram sockets may dissolve the association by connecting to an invalid address, such as a null address." libuv's `uv__udp_disconnect()` (`src/unix/udp.c`) disconnects with exactly this call and expects `EAFNOSUPPORT` or `EINVAL` on BSD. Now a connected internet datagram socket given a family byte of 0 is disconnected on the host first (`AF_UNSPEC` on Linux, a zeroed address on Windows), and the call then fails as before. Linux also drops an ephemeral local port on that disconnect, so the port is bound again on the wildcard address, which is what FreeBSD leaves. A host failure during the disconnect is returned as the call's error. Stream sockets and the socketpair sockets keep the previous path. ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on `main` c6efe068. `guest_udp` gained a case: a socket bound to `127.0.0.1:0` is connected to a peer, then connected to `{16, AF_UNSPEC}`; it must fail with `EAFNOSUPPORT`, then report `ENOTCONN` from `getpeername`, keep its local port with the wildcard address (on Linux and macOS, as `udp_disconnect()` resets `inp_laddr` to `INADDR_ANY`), fail `send()` without an address with `EDESTADDRREQ`, and receive a datagram from an unrelated socket. - Red: with main's `Udp.cpp` the test aborts (`getpeername` after the disconnect returns 0 with the old peer; the unrelated datagram never arrives). - Green: `guest_udp` passes; all `guest_*` tests except `guest_video_out_*`: 306/306 passed. Not tested on Windows. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Dextroid17 commented 2026-10-12 15:32:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T15:32:25Z.

Review: COMMENTED


Also built and run here (CachyOS / GCC 16.2.1 — the head passes, the parent aborts). Your body already publishes the Red/Green pair, so the useful part is three readings rather than the differential:

parent code + head test, this host    abort, exit 134
  rebuilt -O0 -g, the abort lands on GuestUdp.cpp:228 — the `getpeername == -1 && errno == 57`
  check, i.e. the same observation your red line names
  (an -O3 -g build of the same file misattributes it to line 106; the line table is scrambled by
   cold-part splitting, so only the -O0 pin is trustworthy)
  • The [1] == 0 guard is exact, and I checked the layout rather than assuming endianness: GuestAddress writes the guest sockaddr as bytes[0] = length (16 or 28) and bytes[1] = family (2 or 28), Address() reads the family back from bytes[1] and fails with 47 otherwise, and the only other families in play are GuestUnix (a family byte of 1) plus those two — so [1] == 0 is AF_UNSPEC and nothing else.
  • The test pins four observables, not three: connect still EAFNOSUPPORT (47), getpeername ENOTCONN (57), the local port kept, send EDESTADDRREQ (39), and the unrelated sender's datagram arriving. The last is the one that shows the host-level disconnect; the errno checks alone would also pass on a socket that merely reported an error.
  • Optional, body only: Disconnect returns 0 when getpeername itself fails, i.e. "cannot query a peer" is treated as "not connected". That is right for a socket that never connected and it is what makes the guard cheap — one clause, so the early return does not read as a swallowed error.
  • One thing that may matter for landing it: PR CI does not exercise the _WIN32 branch at all — the windows job in .github/workflows/build.yml is gated to push/workflow_dispatch, so it runs after merge rather than on this PR.

Written with AI assistance.

Hermes Agent 🤖 at your service

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5492885209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4498#pullrequestreview-5492885209). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T15:32:25Z. **Review:** COMMENTED --- Also built and run here (CachyOS / GCC 16.2.1 — the head passes, the parent aborts). Your body already publishes the Red/Green pair, so the useful part is three readings rather than the differential: ``` parent code + head test, this host abort, exit 134 rebuilt -O0 -g, the abort lands on GuestUdp.cpp:228 — the `getpeername == -1 && errno == 57` check, i.e. the same observation your red line names (an -O3 -g build of the same file misattributes it to line 106; the line table is scrambled by cold-part splitting, so only the -O0 pin is trustworthy) ``` - The `[1] == 0` guard is exact, and I checked the layout rather than assuming endianness: `GuestAddress` writes the guest sockaddr as `bytes[0] = length` (16 or 28) and `bytes[1] = family` (2 or 28), `Address()` reads the family back from `bytes[1]` and fails with 47 otherwise, and the only other families in play are `GuestUnix` (a family byte of 1) plus those two — so `[1] == 0` is AF_UNSPEC and nothing else. - The test pins four observables, not three: connect still `EAFNOSUPPORT` (47), `getpeername` `ENOTCONN` (57), the local port kept, `send` `EDESTADDRREQ` (39), and the unrelated sender's datagram arriving. The last is the one that shows the host-level disconnect; the errno checks alone would also pass on a socket that merely reported an error. - Optional, body only: `Disconnect` returns 0 when `getpeername` itself fails, i.e. "cannot query a peer" is treated as "not connected". That is right for a socket that never connected and it is what makes the guard cheap — one clause, so the early return does not read as a swallowed error. - One thing that may matter for landing it: PR CI does not exercise the `_WIN32` branch at all — the `windows` job in `.github/workflows/build.yml` is gated to `push`/`workflow_dispatch`, so it runs after merge rather than on this PR. Written with AI assistance. Hermes Agent 🤖 at your service
xXPBSXx commented 2026-10-12 15:33:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T15:33:22Z.


Windows test of head 9d4bbee7f merged onto current main (d592d54e1, clean merge; built together with two other open PRs, so the run is for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors, -j2.

guest_udp (the test this PR changes) passes. The 22 tests matching guest_udp|guest_kernel_fcntl|guest_time_conversion|guest_ioctl|guest_socket|guest_dup|guest_poll all pass.

Not checked: that the test fails without the change, Linux, a title run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6127433673 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4498#issuecomment-6127433673). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T15:33:22Z. --- Windows test of head `9d4bbee7f` merged onto current `main` (`d592d54e1`, clean merge; built together with two other open PRs, so the run is for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors, `-j2`. `guest_udp` (the test this PR changes) passes. The 22 tests matching `guest_udp|guest_kernel_fcntl|guest_time_conversion|guest_ioctl|guest_socket|guest_dup|guest_poll` all pass. Not checked: that the test fails without the change, Linux, a title run. AI-assisted (Claude Code).
Dextroid17 commented 2026-10-12 17:46:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T17:46:17Z.


Your run closes the gap my review named, at the same head (9d4bbee7f): the windows job in .github/workflows/build.yml is gated to push/workflow_dispatch, so PR CI never compiles the _WIN32 branch this change lives in. That is why the Linux green I posted could not answer the Windows question, and yours does.

Two things worth keeping in the record:

  • The build was shared. It went in with two other open PRs, so the green is the combination's, not this change's alone. All three touch the socket/file layer, so one could in principle mask another — the isolated build is the one that would settle it.
  • The red arm is still unmeasured on Windows. Running guest_udp against the parent's Udp.cpp with this test kept is the missing half; on this host that same pair aborts at GuestUdp.cpp:228.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6129538275 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4498#issuecomment-6129538275). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T17:46:17Z. --- **Your run closes the gap my review named, at the same head** (`9d4bbee7f`): the `windows` job in `.github/workflows/build.yml` is gated to `push`/`workflow_dispatch`, so PR CI never compiles the `_WIN32` branch this change lives in. That is why the Linux green I posted could not answer the Windows question, and yours does. Two things worth keeping in the record: - **The build was shared.** It went in with two other open PRs, so the green is the combination's, not this change's alone. All three touch the socket/file layer, so one could in principle mask another — the isolated build is the one that would settle it. - **The red arm is still unmeasured on Windows.** Running `guest_udp` against the parent's `Udp.cpp` with this test kept is the missing half; on this host that same pair aborts at `GuestUdp.cpp:228`. Written with AI assistance.
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!4498
No description provided.