diff --git a/docs/runtime_imports.md b/docs/runtime_imports.md index e38dfbf744..d8081e6971 100644 --- a/docs/runtime_imports.md +++ b/docs/runtime_imports.md @@ -815,8 +815,9 @@ works for loopback traffic: - `connect`, `WSAConnect`, `ConnectEx`, `sendto`, `WSASendTo`, `WSASendMsg`, `WSAConnectByNameA/W` and `WSAConnectByList` allow only IPv4 loopback (`127.0.0.0/8`), IPv6 loopback (`::1`) and IPv4-mapped loopback (`::ffff:127.0.0.0/104`); everything else fails with `WSAENETUNREACH`. -- `WSAConnectByNameA/W` pass `localhost` to the host as the literal `127.0.0.1`, so the host never reads its hosts - file. Numeric loopback literals pass through unchanged. +- `WSAConnectByNameA/W` pass `localhost` to the host as a loopback literal, so the host never reads its hosts file: + `::1` for an IPv6 socket with `IPV6_V6ONLY` set (Windows' default for `AF_INET6`, which cannot reach `127.0.0.1`), + `127.0.0.1` for an IPv4 or dual-mode socket. 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. - `bind` narrows wildcard binds (`0.0.0.0`, `::`) to loopback (`127.0.0.1`, `::1`) and refuses other non-loopback @@ -836,7 +837,8 @@ works for loopback traffic: - `WSALookupServiceBeginA/W` (name-service queries) always fail with `WSASERVICE_NOT_FOUND`. `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 +aliases, `WSAConnectByNameA/W` to `localhost` on IPv4, IPv6-only and dual-mode sockets, 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. diff --git a/runtime/shims/ws2_offline.cpp b/runtime/shims/ws2_offline.cpp index 0a278eb99f..d8ad0cd633 100644 --- a/runtime/shims/ws2_offline.cpp +++ b/runtime/shims/ws2_offline.cpp @@ -13,7 +13,8 @@ // - connect (#4), WSAConnect, WSAConnectByNameA/W, WSAConnectByList, ConnectEx (from WSAIoctl), sendto (#20), // WSASendTo, and WSASendMsg (direct and via WSAIoctl) permit only IPv4 loopback (127.0.0.0/8), IPv6 loopback // (::1), and IPv4-mapped IPv6 loopback (::ffff:127.0.0.0/104). Every other destination fails with WSAENETUNREACH. -// WSAConnectByNameA/W pass "localhost" to the host as the literal 127.0.0.1, so the hosts file is never read. +// WSAConnectByNameA/W pass "localhost" to the host as a loopback literal (::1 for an IPv6 socket with +// IPV6_V6ONLY set, 127.0.0.1 otherwise), 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. // - getaddrinfo, GetAddrInfoW, GetAddrInfoExA/W, gethostbyname (#52), gethostbyaddr (#51), and @@ -252,6 +253,24 @@ bool is_localhost_name_a(const char* node) { return ascii_iequals(node, "localhost") || ascii_iequals(node, "localhost."); } +// Windows resolves "localhost" for WSAConnectByName to ::1 and 127.0.0.1 and tries them in turn. An IPv6 socket with +// IPV6_V6ONLY set (Windows' default) reaches only ::1: given 127.0.0.1 the host fails with WSAHOST_NOT_FOUND. Such a +// socket gets the ::1 literal; an IPv4 or dual-mode IPv6 socket keeps 127.0.0.1. +bool is_ipv6_only_socket(SOCKET s) { + using GetSockOptFn = int(WSAAPI*)(SOCKET, int, int, char*, int*); + static GetSockOptFn real_getsockopt = host_proc("getsockopt"); + if (!real_getsockopt) return false; + WSAPROTOCOL_INFOW info{}; + int len = sizeof(info); + if (real_getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFOW, reinterpret_cast(&info), &len) != 0 || + info.iAddressFamily != AF_INET6) { + return false; + } + DWORD v6only = 0; + len = sizeof(v6only); + return real_getsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&v6only), &len) == 0 && v6only != 0; +} + // ---- Socket connect / send / bind shims ---- int WSAAPI connect_offline(SOCKET s, const sockaddr* name, int namelen) { @@ -302,7 +321,7 @@ BOOL WSAAPI wsa_connect_by_name_a_offline(SOCKET s, LPCSTR nodename, LPCSTR serv return FALSE; } // The host would resolve "localhost" through its hosts file; hand it the literal instead. - LPCSTR node = is_localhost_name_a(nodename) ? "127.0.0.1" : nodename; + LPCSTR node = !is_localhost_name_a(nodename) ? nodename : is_ipv6_only_socket(s) ? "::1" : "127.0.0.1"; return real_fn(s, node, servicename, local_addr_len, local_addr, remote_addr_len, remote_addr, timeout, reserved); } @@ -323,8 +342,11 @@ BOOL WSAAPI wsa_connect_by_name_w_offline(SOCKET s, LPWSTR nodename, LPWSTR serv return FALSE; } // The host would resolve "localhost" through its hosts file; hand it the literal instead. - wchar_t loopback[] = L"127.0.0.1"; - LPWSTR node = is_localhost_name_a(narrow_ascii(nodename).c_str()) ? loopback : nodename; + wchar_t loopback4[] = L"127.0.0.1"; + wchar_t loopback6[] = L"::1"; + LPWSTR node = !is_localhost_name_a(narrow_ascii(nodename).c_str()) ? nodename + : is_ipv6_only_socket(s) ? loopback6 + : loopback4; return real_fn(s, node, servicename, local_addr_len, local_addr, remote_addr_len, remote_addr, timeout, reserved); } diff --git a/runtime/test/ws2_offline_test.cpp b/runtime/test/ws2_offline_test.cpp index 32cdda48c2..806b7b7488 100644 --- a/runtime/test/ws2_offline_test.cpp +++ b/runtime/test/ws2_offline_test.cpp @@ -4,7 +4,8 @@ // checks that wsock32.dll resolves its shared entries to the same wrappers, and verifies that external addresses // (RFC 5737 192.0.2.1 / RFC 3849 2001:db8::1), .invalid hostnames, octal-looking 0127.0.0.1 and name-service queries // are refused in-process while loopback (127.0.0.1 / ::1 / localhost) still works. Sends on unbound UDP sockets bind -// them to loopback, and multicast joins (setsockopt on both DLLs, WSAJoinLeaf) are refused. Nothing touches the network. +// them to loopback, and multicast joins (setsockopt on both DLLs, WSAJoinLeaf) are refused. WSAConnectByName reaches +// localhost from IPv4, IPv6-only and dual-mode sockets. Nothing touches the network. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS #define _WINSOCK_DEPRECATED_NO_WARNINGS #endif @@ -424,6 +425,98 @@ int main() { closesocket(listener); } + // 8. WSAConnectByName reaches "localhost" from every socket Windows reaches it from. Windows tries ::1 and then + // 127.0.0.1; an IPv6 socket with IPV6_V6ONLY set (the default) reaches only ::1, so the shim's literal follows it. + { + const auto tcp_listener = [&](int family, char (&service)[8]) { + const SOCKET l = socket(family, SOCK_STREAM, IPPROTO_TCP); + sockaddr_storage addr{}; + int len = 0; + if (family == AF_INET) { + *reinterpret_cast(&addr) = make_v4(127, 0, 0, 1, 0); + len = sizeof(sockaddr_in); + } else { + auto* v6 = reinterpret_cast(&addr); + v6->sin6_family = AF_INET6; + reinterpret_cast(&v6->sin6_addr)[15] = 1; // ::1 + len = sizeof(sockaddr_in6); + } + service[0] = '\0'; + if (l != INVALID_SOCKET && sh_bind(l, reinterpret_cast(&addr), len) == 0 && + listen(l, 4) == 0 && getsockname(l, reinterpret_cast(&addr), &len) == 0) { + const uint16_t port = family == AF_INET ? reinterpret_cast(&addr)->sin_port + : reinterpret_cast(&addr)->sin6_port; + std::snprintf(service, sizeof(service), "%u", static_cast(ntohs(port))); + } + return l; + }; + // Connects s to "localhost" through the A or W shim; returns the remote address family (0 on failure) and + // points remote_bytes at the remote address. + sockaddr_storage remote{}; + const auto connect_localhost = [&](SOCKET s, const char* service, bool wide, const uint8_t** remote_bytes) { + remote = sockaddr_storage{}; + DWORD remote_len = sizeof(remote); + timeval timeout{5, 0}; + BOOL ok = FALSE; + if (wide) { + wchar_t node_w[] = L"localhost"; + wchar_t service_w[8] = {}; + for (int i = 0; i < 7 && service[i]; ++i) service_w[i] = static_cast(service[i]); + ok = sh_connect_name_w(s, node_w, service_w, nullptr, nullptr, &remote_len, + reinterpret_cast(&remote), &timeout, nullptr); + } else { + ok = sh_connect_name_a(s, "localhost", service, nullptr, nullptr, &remote_len, + reinterpret_cast(&remote), &timeout, nullptr); + } + *remote_bytes = remote.ss_family == AF_INET + ? reinterpret_cast(&reinterpret_cast(&remote)->sin_addr) + : reinterpret_cast(&reinterpret_cast(&remote)->sin6_addr); + return ok ? static_cast(remote.ss_family) : 0; + }; + const auto is_v6_loopback = [](const uint8_t* b) { + for (int i = 0; i < 15; ++i) { + if (b[i]) return false; + } + return b[15] == 1; + }; + + char service4[8], service6[8]; + const SOCKET listener4 = tcp_listener(AF_INET, service4); + check(service4[0] != '\0', "IPv4 loopback TCP listener is up"); + const uint8_t* remote_ip = nullptr; + + const SOCKET client4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + check(connect_localhost(client4, service4, false, &remote_ip) == AF_INET && remote_ip[0] == 127, + "WSAConnectByNameA connects an IPv4 socket to localhost at 127.0.0.1"); + closesocket(client4); + + const SOCKET listener6 = tcp_listener(AF_INET6, service6); + if (service6[0]) { + const SOCKET v6only = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); + check(connect_localhost(v6only, service6, false, &remote_ip) == AF_INET6 && is_v6_loopback(remote_ip), + "WSAConnectByNameA connects an IPv6-only socket to localhost at ::1"); + closesocket(v6only); + + const SOCKET v6only_w = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); + check(connect_localhost(v6only_w, service6, true, &remote_ip) == AF_INET6 && is_v6_loopback(remote_ip), + "WSAConnectByNameW connects an IPv6-only socket to localhost at ::1"); + closesocket(v6only_w); + + // A dual-mode socket keeps 127.0.0.1, which it reaches as ::ffff:127.0.0.1. + const SOCKET dual = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); + DWORD off = 0; + check(setsockopt(dual, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&off), sizeof(off)) == 0 && + connect_localhost(dual, service4, false, &remote_ip) == AF_INET6 && remote_ip[10] == 0xff && + remote_ip[11] == 0xff && remote_ip[12] == 127, + "WSAConnectByNameA connects a dual-mode IPv6 socket to localhost at ::ffff:127.0.0.1"); + closesocket(dual); + } else { + std::printf("SKIP IPv6 loopback unavailable: WSAConnectByName on IPv6 sockets\n"); + } + if (listener6 != INVALID_SOCKET) closesocket(listener6); + closesocket(listener4); + } + // Implicit binds, multicast joins and WSAJoinLeaf. A send on an unbound datagram socket binds it to the wildcard // address (Microsoft's sendto remarks); the shim binds it to loopback first. Joins are refused before the host. {