From 55dfd6bc3266b8c3fd343a74499e258fbdcf299b Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:59:02 +0100 Subject: [PATCH 1/2] ws2_offline: let connect dissolve a UDP association (WS2-UDP-DISCONNECT) connect's remarks: on a connectionless socket, connecting to an all-zero address dissolves the association, after which send and recv fail with WSAENOTCONN. connect and WSAConnect refused that address as a non-loopback destination (WSAENETUNREACH), so a datagram socket stayed tied to its first peer for good: datagrams from any other address were still dropped. Windows 11 dissolves the association for 0.0.0.0:0, [::]:0 and an AF_UNSPEC address and refuses 0.0.0.0 with a port (WSAEADDRNOTAVAIL); a TCP socket refuses all of them. None reaches a peer, so those forms now go to the host; 0.0.0.0 with a port stays refused before it. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/ws2_offline.cpp | 28 +++++++++- runtime/test/ws2_offline_test.cpp | 93 +++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/runtime/shims/ws2_offline.cpp b/runtime/shims/ws2_offline.cpp index 0a278eb99f..64ef637496 100644 --- a/runtime/shims/ws2_offline.cpp +++ b/runtime/shims/ws2_offline.cpp @@ -16,6 +16,8 @@ // WSAConnectByNameA/W pass "localhost" to the host as the literal 127.0.0.1, so the hosts file is never read. // - bind (#2) rewrites wildcard binds (0.0.0.0 or ::) to 127.0.0.1 or ::1 so no external peer can connect in, // allows explicit loopback binds, and refuses any other address with WSAEADDRNOTAVAIL. +// - connect and WSAConnect also pass the two forms that dissolve a datagram socket's association instead of +// reaching a peer: an AF_UNSPEC address, and the unspecified address (0.0.0.0 or ::) with port 0. // - getaddrinfo, GetAddrInfoW, GetAddrInfoExA/W, gethostbyname (#52), gethostbyaddr (#51), and // WSAAsyncGetHostByName/Addr (#103/#102) permit only null/empty nodes, "localhost", and loopback IP literals; // everything else fails with WSAHOST_NOT_FOUND without sending DNS packets. A dotted octet with a leading zero @@ -134,6 +136,28 @@ bool is_loopback_sockaddr(const sockaddr* sa, int namelen) { return false; } +// connect's remarks: on a connectionless socket, an all-zero address dissolves the association, after which send and +// recv fail with WSAENOTCONN. Windows 11 does this for 0.0.0.0:0, [::]:0 and an AF_UNSPEC address (connect and +// WSAConnect return 0) and refuses 0.0.0.0 with a port; a TCP socket refuses all three with an error. None of them +// reaches a peer, so they go to the host. +bool is_disconnect_sockaddr(const sockaddr* sa, int namelen) { + if (!sa || namelen < static_cast(sizeof(ADDRESS_FAMILY))) return false; + if (sa->sa_family == AF_UNSPEC) return true; + if (sa->sa_family == AF_INET) { + if (namelen < static_cast(sizeof(sockaddr_in))) return false; + sockaddr_in v4{}; + std::memcpy(&v4, sa, sizeof(v4)); + return v4.sin_port == 0 && is_ipv4_unspecified_bytes(reinterpret_cast(&v4.sin_addr)); + } + if (sa->sa_family == AF_INET6) { + if (namelen < static_cast(sizeof(sockaddr_in6))) return false; + sockaddr_in6 v6{}; + std::memcpy(&v6, sa, sizeof(v6)); + return v6.sin6_port == 0 && is_ipv6_unspecified_bytes(reinterpret_cast(&v6.sin6_addr)); + } + return false; +} + std::string format_sockaddr(const sockaddr* sa, int namelen) { if (!sa || namelen < static_cast(sizeof(ADDRESS_FAMILY))) return ""; char buf[96] = {}; @@ -255,7 +279,7 @@ bool is_localhost_name_a(const char* node) { // ---- Socket connect / send / bind shims ---- int WSAAPI connect_offline(SOCKET s, const sockaddr* name, int namelen) { - if (!is_loopback_sockaddr(name, namelen)) { + if (!is_loopback_sockaddr(name, namelen) && !is_disconnect_sockaddr(name, namelen)) { log_refused("connect", format_sockaddr(name, namelen)); set_wsa_error(WSAENETUNREACH); return SOCKET_ERROR; @@ -271,7 +295,7 @@ int WSAAPI connect_offline(SOCKET s, const sockaddr* name, int namelen) { int WSAAPI wsa_connect_offline(SOCKET s, const sockaddr* name, int namelen, LPWSABUF caller_data, LPWSABUF callee_data, LPQOS sqos, LPQOS gqos) { - if (!is_loopback_sockaddr(name, namelen)) { + if (!is_loopback_sockaddr(name, namelen) && !is_disconnect_sockaddr(name, namelen)) { log_refused("WSAConnect", format_sockaddr(name, namelen)); set_wsa_error(WSAENETUNREACH); return SOCKET_ERROR; diff --git a/runtime/test/ws2_offline_test.cpp b/runtime/test/ws2_offline_test.cpp index 32cdda48c2..62a116c93a 100644 --- a/runtime/test/ws2_offline_test.cpp +++ b/runtime/test/ws2_offline_test.cpp @@ -356,6 +356,99 @@ int main() { closesocket(recv_sock); } + // 5b. connect and WSAConnect to an all-zero address dissolve a datagram socket's association (connect's remarks; + // Windows 11 does it for 0.0.0.0:0, [::]:0 and AF_UNSPEC). Nothing is sent, so the shim passes them to the host. + { + const auto loop_udp = [&](sockaddr_in& bound) { + const SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + const sockaddr_in loop = make_v4(127, 0, 0, 1, 0); + int len = sizeof(bound); + const DWORD timeout_ms = 2000; + if (sh_bind(s, reinterpret_cast(&loop), sizeof(loop)) != 0 || + getsockname(s, reinterpret_cast(&bound), &len) != 0 || + setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast(&timeout_ms), sizeof(timeout_ms))) { + bound = sockaddr_in{}; + } + return s; + }; + const auto not_connected = [](SOCKET s) { + sockaddr_storage peer{}; + int peer_len = sizeof(peer); + return getpeername(s, reinterpret_cast(&peer), &peer_len) == SOCKET_ERROR && + WSAGetLastError() == WSAENOTCONN; + }; + sockaddr_in addr_a{}, addr_b{}, addr_s{}; + const SOCKET a = loop_udp(addr_a); + const SOCKET b = loop_udp(addr_b); + const SOCKET s = loop_udp(addr_s); + check(addr_a.sin_port && addr_b.sin_port && addr_s.sin_port, "three loopback UDP sockets are bound"); + const auto* to_a = reinterpret_cast(&addr_a); + const sockaddr_in zero4 = make_v4(0, 0, 0, 0, 0); + const auto* zero = reinterpret_cast(&zero4); + + check(sh_connect(s, to_a, sizeof(addr_a)) == 0 && !not_connected(s), "connect associates the socket with A"); + check(sh_connect(s, zero, sizeof(zero4)) == 0, "connect to 0.0.0.0:0 succeeds on a connected UDP socket"); + check(not_connected(s), "after it getpeername fails with WSAENOTCONN"); + check(send(s, "x", 1, 0) == SOCKET_ERROR && WSAGetLastError() == WSAENOTCONN, + "after it send fails with WSAENOTCONN"); + char got[8] = {}; + check(sh_sendto(b, "b", 1, 0, reinterpret_cast(&addr_s), sizeof(addr_s)) == 1 && + recv(s, got, sizeof(got), 0) == 1 && got[0] == 'b', + "after it a datagram from B arrives (a connected socket takes only its peer's)"); + + check(sh_wsa_connect(s, to_a, sizeof(addr_a), nullptr, nullptr, nullptr, nullptr) == 0 && + sh_wsa_connect(s, zero, sizeof(zero4), nullptr, nullptr, nullptr, nullptr) == 0 && not_connected(s), + "WSAConnect to 0.0.0.0:0 dissolves the association too"); + + sockaddr_in unspec{}; + unspec.sin_family = AF_UNSPEC; + check(sh_connect(s, to_a, sizeof(addr_a)) == 0 && + sh_connect(s, reinterpret_cast(&unspec), sizeof(unspec)) == 0 && not_connected(s), + "connect to an AF_UNSPEC address dissolves the association"); + + // Only the disconnect forms pass: the unspecified address with a port stays refused before the host. + const sockaddr_in zero_port = make_v4(0, 0, 0, 0, 4321); + WSASetLastError(0); + check(sh_connect(s, to_a, sizeof(addr_a)) == 0 && + sh_connect(s, reinterpret_cast(&zero_port), sizeof(zero_port)) == SOCKET_ERROR && + WSAGetLastError() == WSAENETUNREACH && !not_connected(s), + "connect still refuses 0.0.0.0:4321 with WSAENETUNREACH and keeps the association"); + + // A TCP socket has no association to dissolve: the host refuses both forms. + const SOCKET tcp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + WSASetLastError(0); + check(sh_connect(tcp, zero, sizeof(zero4)) == SOCKET_ERROR && WSAGetLastError() == WSAEADDRNOTAVAIL, + "connect to 0.0.0.0:0 on a TCP socket fails with the host's WSAEADDRNOTAVAIL"); + WSASetLastError(0); + check(sh_connect(tcp, reinterpret_cast(&unspec), sizeof(unspec)) == SOCKET_ERROR && + WSAGetLastError() == WSAEAFNOSUPPORT, + "connect to AF_UNSPEC on a TCP socket fails with the host's WSAEAFNOSUPPORT"); + closesocket(tcp); + + const SOCKET s6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); + const SOCKET a6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); + if (s6 != INVALID_SOCKET && a6 != INVALID_SOCKET) { + sockaddr_in6 loop6{}; + loop6.sin6_family = AF_INET6; + reinterpret_cast(&loop6.sin6_addr)[15] = 1; // ::1 + sockaddr_in6 addr_a6{}; + int len6 = sizeof(addr_a6); + const bool bound6 = sh_bind(s6, reinterpret_cast(&loop6), sizeof(loop6)) == 0 && + sh_bind(a6, reinterpret_cast(&loop6), sizeof(loop6)) == 0 && + getsockname(a6, reinterpret_cast(&addr_a6), &len6) == 0; + sockaddr_in6 zero6{}; + zero6.sin6_family = AF_INET6; + check(bound6 && sh_connect(s6, reinterpret_cast(&addr_a6), sizeof(addr_a6)) == 0 && + sh_connect(s6, reinterpret_cast(&zero6), sizeof(zero6)) == 0 && not_connected(s6), + "connect to [::]:0 dissolves an IPv6 UDP socket's association"); + } + if (a6 != INVALID_SOCKET) closesocket(a6); + if (s6 != INVALID_SOCKET) closesocket(s6); + closesocket(s); + closesocket(b); + closesocket(a); + } + // 6. Name-service queries are refused with WSASERVICE_NOT_FOUND. { const auto sh_lookup_w = resolve("WSALookupServiceBeginW"); From 1c6f552cc110fe6cddb6fa5f719f3b20bb10ff6a Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Thu, 26 Nov 2026 15:30:47 +0100 Subject: [PATCH 2/2] docs: runtime_imports describes the WS2-UDP-DISCONNECT behaviour The Winsock2 section still described the shim before this change; it now names the new behaviour and the test that covers it. Co-Authored-By: Claude Opus 5.5 --- docs/runtime_imports.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/runtime_imports.md b/docs/runtime_imports.md index e38dfbf744..c5b9651919 100644 --- a/docs/runtime_imports.md +++ b/docs/runtime_imports.md @@ -819,6 +819,10 @@ works for loopback traffic: file. Numeric loopback literals pass through unchanged. - `ConnectEx` and `WSASendMsg` from `WSAIoctl` are the host's own pointers, fetched for the socket they are called on (each Winsock provider has its own), after the same loopback check. +- `connect` and `WSAConnect` also pass the two forms that dissolve a datagram socket's association instead of reaching + a peer: an `AF_UNSPEC` address, and `0.0.0.0` or `::` with port 0 (measured on Windows 11: the socket is no longer + connected and takes datagrams from any address again; a TCP socket gets the host's error). `0.0.0.0` with a port is + still refused. - `bind` narrows wildcard binds (`0.0.0.0`, `::`) to loopback (`127.0.0.1`, `::1`) and refuses other non-loopback addresses with `WSAEADDRNOTAVAIL`. - A send to a loopback address on an unbound socket (`sendto`, `WSASendTo`, `WSASendMsg`, `WSAJoinLeaf`) would bind it @@ -838,8 +842,9 @@ works for loopback traffic: `runtime/test/ws2_offline_test.cpp` (ctest `ws2_offline`) verifies name and ordinal bindings, the `wsock32.dll` aliases, external refusals using RFC 5737 / RFC 3849 / `.invalid` targets, the `0127.0.0.1` refusal, the `WSASERVICE_NOT_FOUND` refusal, wildcard-bind narrowing, a `ConnectEx` pointer from `WSAIoctl` connecting to a loopback -listener, a local loopback UDP round-trip, loopback binds for sends on unbound UDP sockets (with a control showing -the host's own `sendto` binds to `0.0.0.0`), and the multicast and `WSAJoinLeaf` refusals. +listener, a local loopback UDP round-trip, a UDP association dissolved by each of the disconnect forms, loopback binds +for sends on unbound UDP sockets (with a control showing the host's own `sendto` binds to `0.0.0.0`), and the multicast +and `WSAJoinLeaf` refusals. ## What this proves and what it does not