fix(libSceNet): refuse a connect to a loopback port held without listening on macOS #22442

Merged
011010 merged 1 commit from upstream-pr/22442 into main 2026-11-17 17:00:48 +00:00
011010 commented 2026-11-17 16:19:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 011010 on 2026-11-17T16:19:42Z.


What

guest_sce_net fails on macOS at Require(Failed(sceNetConnect(refused, closedAddress.data(), closedAddress.size()), 61)) with sceNet errno 35 (EAGAIN) instead of 61 (ECONNREFUSED), when the destination is a loopback port that another guest socket bound without listening.

The cause is the host. XNU sends no reset for a SYN to a port a socket holds without listen; it drops it. A nonblocking connect then poll(POLLOUT, 1500) on macOS 27.0.1 gets poll 0 with SO_ERROR 0 after 1501 ms for that port, and POLLHUP with SO_ERROR 61 at once for a port no socket holds. So connect_within waits out the timeout and reports the expiry. FreeBSD resets the connection, which is what the console reports.

A blocking sceNetConnect to a loopback address now fails with ECONNREFUSED when a guest socket in the same family holds that port on the loopback or wildcard address without listening. Nothing else changes: a port with a listener still connects, a port no guest socket holds still goes to the host (which refuses it at once on macOS), and a retry after an expired connect still returns EADDRINUSE first.

The debt fragment records what is not covered: a non-blocking connect to such a port still waits for the host's TCP connect timeout, a port held by a host socket outside the guest set is not detected, and the listen backlog below.

Tested

  • macOS 27.0.1 (26A434), Apple M4 Mac mini, Apple clang 21.0.0, x86-64 Release under Rosetta 2, main 1adc0e2f4b.
  • Before: guest_sce_net check failed at line 1188, sceNet errno 35.
  • After: that check passes and the test gets further. It now stops in CheckConnectTimeoutExpiry with connect timeout expiry: no connect stayed pending after 64 completed connects (last result 0x0, threw 0, 0 ms), which is a separate macOS gap, not this one; it is the second debt entry and gets its own issue.
  • guest_socket_options fails with and without this change (a connect after a refused nonblocking one must fail with ECONNREFUSED (61), not start another attempt (errno 56)), so it is unrelated.
  • guest_socket_pair, guest_tcp, guest_udp and guest_net_ctl pass.
  • Host probes used to establish the behaviour, both x86-64 under Rosetta 2: the poll/SO_ERROR table above, and listen(0) followed by 64 connects, which completes all 64 with none refused.
  • python3 tools/check_conventions.py --base origin/main: all conventions checks passed.
  • python3 tools/pr_overlap.py --names GuestHoldsUnlistenedLoopback: not declared on main and on no open pull request.
  • python3 tools/pr_overlap.py --branch fix/macos-refuse-unlistened-loopback-connect: five open pull requests share libSceNet/Export.cpp, all libkernel cancellation work that calls sceNetConnect from its tests; none of them edits the function.
  • Not tested: a console, and a title.

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: #22317
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5360622458 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22442). Originally posted by [011010](https://github.com/011010) on 2026-11-17T16:19:42Z. --- ### What `guest_sce_net` fails on macOS at `Require(Failed(sceNetConnect(refused, closedAddress.data(), closedAddress.size()), 61))` with `sceNet errno 35` (`EAGAIN`) instead of 61 (`ECONNREFUSED`), when the destination is a loopback port that another guest socket bound without listening. The cause is the host. XNU sends no reset for a SYN to a port a socket holds without `listen`; it drops it. A nonblocking `connect` then `poll(POLLOUT, 1500)` on macOS 27.0.1 gets `poll` 0 with `SO_ERROR` 0 after 1501 ms for that port, and `POLLHUP` with `SO_ERROR` 61 at once for a port no socket holds. So `connect_within` waits out the timeout and reports the expiry. FreeBSD resets the connection, which is what the console reports. A blocking `sceNetConnect` to a loopback address now fails with `ECONNREFUSED` when a guest socket in the same family holds that port on the loopback or wildcard address without listening. Nothing else changes: a port with a listener still connects, a port no guest socket holds still goes to the host (which refuses it at once on macOS), and a retry after an expired connect still returns `EADDRINUSE` first. The debt fragment records what is not covered: a non-blocking connect to such a port still waits for the host's TCP connect timeout, a port held by a host socket outside the guest set is not detected, and the listen backlog below. ### Tested - macOS 27.0.1 (26A434), Apple M4 Mac mini, Apple clang 21.0.0, x86-64 Release under Rosetta 2, `main` `1adc0e2f4b`. - Before: `guest_sce_net check failed at line 1188, sceNet errno 35`. - After: that check passes and the test gets further. It now stops in `CheckConnectTimeoutExpiry` with `connect timeout expiry: no connect stayed pending after 64 completed connects (last result 0x0, threw 0, 0 ms)`, which is a separate macOS gap, not this one; it is the second debt entry and gets its own issue. - `guest_socket_options` fails with and without this change (`a connect after a refused nonblocking one must fail with ECONNREFUSED (61), not start another attempt (errno 56)`), so it is unrelated. - `guest_socket_pair`, `guest_tcp`, `guest_udp` and `guest_net_ctl` pass. - Host probes used to establish the behaviour, both x86-64 under Rosetta 2: the `poll`/`SO_ERROR` table above, and `listen(0)` followed by 64 connects, which completes all 64 with none refused. - `python3 tools/check_conventions.py --base origin/main`: all conventions checks passed. - `python3 tools/pr_overlap.py --names GuestHoldsUnlistenedLoopback`: not declared on `main` and on no open pull request. - `python3 tools/pr_overlap.py --branch fix/macos-refuse-unlistened-loopback-connect`: five open pull requests share `libSceNet/Export.cpp`, all libkernel cancellation work that calls `sceNetConnect` from its tests; none of them edits the function. - Not tested: a console, and a title. ### 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 (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: #22317 - [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!22442
No description provided.