upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/ws2_offline.cpp
diff options
context:
space:
mode:
authorandretuta <andreviniciusmerlia@gmail.com>2026-11-24 02:42:07 -0300
committerandretuta <andreviniciusmerlia@gmail.com>2026-11-24 02:42:07 -0300
commitd7cdfcf25500503fbd694fb6eb949b694ae71bdf (patch)
treeb373008d61f4d167d4f30bccd6ad803aa6b6aa14 /runtime/shims/ws2_offline.cpp
parent7f7a5968563718098c3c592193ecb8da14e7d49a (diff)
ws2_offline: per-socket extension pointers, refuse name-service lookups, close connect-by-name and octal bypasses, cover wsock32
Review fixes for #357: - ConnectEx / WSASendMsg wrappers ask the host for the pointer of the socket they run on, instead of one process-wide pointer that the last WSAIoctl on any provider overwrote. - WSALookupServiceBeginA/W fail with WSASERVICE_NOT_FOUND. - WSAConnectByNameA/W hand the host the literal 127.0.0.1 for "localhost", so the hosts file is never read. - A dotted octet with a leading zero (0127.0.0.1) is refused: Windows reads it as octal. - wsock32.dll maps bind/connect/sendto/gethostby*/WSAAsyncGetHostBy* (names and ordinals) to the same wrappers and stays host-owned in owns_dll, like ws2_32.dll. - docs/runtime_imports.md no longer says a --run can open real sockets; it names the dnsapi.dll gap. Tested: ws2_offline_test built with clang 23 (msys2 clang64) on Windows: 52 checks, 0 failures. MSVC build not available locally; CI covers it. tests/test_docs_*.py, test_text_write_line_endings.py, test_runtime_paths.py pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/ws2_offline.cpp')
-rw-r--r--runtime/shims/ws2_offline.cpp96
1 files changed, 79 insertions, 17 deletions
diff --git a/runtime/shims/ws2_offline.cpp b/runtime/shims/ws2_offline.cpp
index 9a5980d2c..a7561f6a5 100644
--- a/runtime/shims/ws2_offline.cpp
+++ b/runtime/shims/ws2_offline.cpp
@@ -1,9 +1,10 @@
1// ws2_offline: Winsock2 (ws2_32.dll) for a mapped title, restricted to loopback only. 1// ws2_offline: Winsock2 (ws2_32.dll and its wsock32.dll alias) for a mapped title, restricted to loopback only.
2// 2//
3// Nothing in Leonida goes online. While winhttp_offline.cpp owns winhttp.dll, a title can also import Winsock2 3// Nothing in Leonida goes online. While winhttp_offline.cpp owns winhttp.dll, a title can also import Winsock2
4// functions directly (both by name and by standard Windows ws2_32.dll ordinal) or resolve them through 4// functions directly (both by name and by standard Windows ws2_32.dll ordinal) or resolve them through
5// GetProcAddress / WSAIoctl. Bound straight to the host's ws2_32.dll, those calls could open external sockets or 5// GetProcAddress / WSAIoctl. Bound straight to the host's ws2_32.dll, those calls could open external sockets or
6// trigger DNS queries. 6// trigger DNS queries. wsock32.dll exports forward to ws2_32 natively, so its bind/connect/sendto/gethostby* entries
7// go to the same wrappers.
7// 8//
8// ws2_32.dll stays host-owned in rt::shims::owns_dll (partial override, like kernel32.dll), so socket bookkeeping 9// ws2_32.dll stays host-owned in rt::shims::owns_dll (partial override, like kernel32.dll), so socket bookkeeping
9// (WSAStartup, socket, closesocket, select, setsockopt, recv, send, byte-order helpers, freeaddrinfo) continues to 10// (WSAStartup, socket, closesocket, select, setsockopt, recv, send, byte-order helpers, freeaddrinfo) continues to
@@ -12,15 +13,19 @@
12// - connect (#4), WSAConnect, WSAConnectByNameA/W, WSAConnectByList, ConnectEx (from WSAIoctl), sendto (#20), 13// - connect (#4), WSAConnect, WSAConnectByNameA/W, WSAConnectByList, ConnectEx (from WSAIoctl), sendto (#20),
13// WSASendTo, and WSASendMsg (direct and via WSAIoctl) permit only IPv4 loopback (127.0.0.0/8), IPv6 loopback 14// WSASendTo, and WSASendMsg (direct and via WSAIoctl) permit only IPv4 loopback (127.0.0.0/8), IPv6 loopback
14// (::1), and IPv4-mapped IPv6 loopback (::ffff:127.0.0.0/104). Every other destination fails with WSAENETUNREACH. 15// (::1), and IPv4-mapped IPv6 loopback (::ffff:127.0.0.0/104). Every other destination fails with WSAENETUNREACH.
16// WSAConnectByNameA/W pass "localhost" to the host as the literal 127.0.0.1, so the hosts file is never read.
15// - bind (#2) rewrites wildcard binds (0.0.0.0 or ::) to 127.0.0.1 or ::1 so no external peer can connect in, 17// - bind (#2) rewrites wildcard binds (0.0.0.0 or ::) to 127.0.0.1 or ::1 so no external peer can connect in,
16// allows explicit loopback binds, and refuses any other address with WSAEADDRNOTAVAIL. 18// allows explicit loopback binds, and refuses any other address with WSAEADDRNOTAVAIL.
17// - getaddrinfo, GetAddrInfoW, GetAddrInfoExA/W, gethostbyname (#52), gethostbyaddr (#51), and 19// - getaddrinfo, GetAddrInfoW, GetAddrInfoExA/W, gethostbyname (#52), gethostbyaddr (#51), and
18// WSAAsyncGetHostByName/Addr (#103/#102) permit only null/empty nodes, "localhost", and loopback IP literals; 20// WSAAsyncGetHostByName/Addr (#103/#102) permit only null/empty nodes, "localhost", and loopback IP literals;
19// everything else fails with WSAHOST_NOT_FOUND without sending DNS packets. 21// everything else fails with WSAHOST_NOT_FOUND without sending DNS packets. A dotted octet with a leading zero
22// (0127.0.0.1) is refused: Windows reads it as octal, so it is not the loopback name it looks like.
23// - WSALookupServiceBeginA/W (name-service queries) always fail with WSASERVICE_NOT_FOUND.
20// - getnameinfo / GetNameInfoW force NI_NUMERICHOST for non-loopback addresses (or return WSAHOST_NOT_FOUND when 24// - getnameinfo / GetNameInfoW force NI_NUMERICHOST for non-loopback addresses (or return WSAHOST_NOT_FOUND when
21// NI_NAMEREQD is set), avoiding reverse DNS lookups. 25// NI_NAMEREQD is set), avoiding reverse DNS lookups.
22// - WSAIoctl wraps WSAID_CONNECTEX and WSAID_WSASENDMSG and refuses SIO_GET_MULTIPLE_EXTENSION_FUNCTION_POINTER 26// - WSAIoctl wraps WSAID_CONNECTEX and WSAID_WSASENDMSG and refuses SIO_GET_MULTIPLE_EXTENSION_FUNCTION_POINTER
23// with WSAEINVAL so extension functions cannot bypass the checks. 27// with WSAEINVAL so extension functions cannot bypass the checks. The wrappers ask the host for the native
28// pointer of the socket they are called on, since each Winsock provider has its own.
24// 29//
25// Every host ws2_32.dll call is resolved dynamically from System32 so gta6rt needs no static ws2_32.lib link 30// Every host ws2_32.dll call is resolved dynamically from System32 so gta6rt needs no static ws2_32.lib link
26// dependency. Each refused destination is logged once ("[rt] ws2: offline, refused <op> <target>") with no payload. 31// dependency. Each refused destination is logged once ("[rt] ws2: offline, refused <op> <target>") with no payload.
@@ -39,7 +44,6 @@
39#include <mswsock.h> 44#include <mswsock.h>
40#include <windows.h> 45#include <windows.h>
41 46
42#include <atomic>
43#include <cstdint> 47#include <cstdint>
44#include <cstdio> 48#include <cstdio>
45#include <cstring> 49#include <cstring>
@@ -188,6 +192,7 @@ bool parse_ipv4_literal(const char* s, uint8_t out[4]) {
188 const char* p = s; 192 const char* p = s;
189 for (int part = 0; part < 4; ++part) { 193 for (int part = 0; part < 4; ++part) {
190 if (*p < '0' || *p > '9') return false; 194 if (*p < '0' || *p > '9') return false;
195 const char* start = p;
191 unsigned val = 0; 196 unsigned val = 0;
192 int digits = 0; 197 int digits = 0;
193 while (*p >= '0' && *p <= '9') { 198 while (*p >= '0' && *p <= '9') {
@@ -197,6 +202,7 @@ bool parse_ipv4_literal(const char* s, uint8_t out[4]) {
197 ++digits; 202 ++digits;
198 } 203 }
199 if (digits == 0) return false; 204 if (digits == 0) return false;
205 if (digits > 1 && *start == '0') return false; // a leading zero is octal to Windows: 0127 is 87, not 127
200 out[part] = static_cast<uint8_t>(val); 206 out[part] = static_cast<uint8_t>(val);
201 if (part < 3) { 207 if (part < 3) {
202 if (*p != '.') return false; 208 if (*p != '.') return false;
@@ -242,6 +248,10 @@ bool is_loopback_node_w(const wchar_t* node) {
242 return is_loopback_node_a(ascii.c_str()); 248 return is_loopback_node_a(ascii.c_str());
243} 249}
244 250
251bool is_localhost_name_a(const char* node) {
252 return ascii_iequals(node, "localhost") || ascii_iequals(node, "localhost.");
253}
254
245// ---- Socket connect / send / bind shims ---- 255// ---- Socket connect / send / bind shims ----
246 256
247int WSAAPI connect_offline(SOCKET s, const sockaddr* name, int namelen) { 257int WSAAPI connect_offline(SOCKET s, const sockaddr* name, int namelen) {
@@ -291,8 +301,9 @@ BOOL WSAAPI wsa_connect_by_name_a_offline(SOCKET s, LPCSTR nodename, LPCSTR serv
291 set_wsa_error(WSAENETUNREACH); 301 set_wsa_error(WSAENETUNREACH);
292 return FALSE; 302 return FALSE;
293 } 303 }
294 return real_fn(s, nodename, servicename, local_addr_len, local_addr, remote_addr_len, remote_addr, timeout, 304 // The host would resolve "localhost" through its hosts file; hand it the literal instead.
295 reserved); 305 LPCSTR node = is_localhost_name_a(nodename) ? "127.0.0.1" : nodename;
306 return real_fn(s, node, servicename, local_addr_len, local_addr, remote_addr_len, remote_addr, timeout, reserved);
296} 307}
297 308
298BOOL WSAAPI wsa_connect_by_name_w_offline(SOCKET s, LPWSTR nodename, LPWSTR servicename, 309BOOL WSAAPI wsa_connect_by_name_w_offline(SOCKET s, LPWSTR nodename, LPWSTR servicename,
@@ -311,8 +322,10 @@ BOOL WSAAPI wsa_connect_by_name_w_offline(SOCKET s, LPWSTR nodename, LPWSTR serv
311 set_wsa_error(WSAENETUNREACH); 322 set_wsa_error(WSAENETUNREACH);
312 return FALSE; 323 return FALSE;
313 } 324 }
314 return real_fn(s, nodename, servicename, local_addr_len, local_addr, remote_addr_len, remote_addr, timeout, 325 // The host would resolve "localhost" through its hosts file; hand it the literal instead.
315 reserved); 326 wchar_t loopback[] = L"127.0.0.1";
327 LPWSTR node = is_localhost_name_a(narrow_ascii(nodename).c_str()) ? loopback : nodename;
328 return real_fn(s, node, servicename, local_addr_len, local_addr, remote_addr_len, remote_addr, timeout, reserved);
316} 329}
317 330
318BOOL WSAAPI wsa_connect_by_list_offline(SOCKET s, PSOCKET_ADDRESS_LIST addr_list, LPDWORD local_addr_len, 331BOOL WSAAPI wsa_connect_by_list_offline(SOCKET s, PSOCKET_ADDRESS_LIST addr_list, LPDWORD local_addr_len,
@@ -617,10 +630,42 @@ INT WSAAPI get_name_info_w_offline(const sockaddr* sa, socklen_t salen, PWCHAR h
617 return real_fn(sa, salen, host, host_size, serv, serv_size, flags); 630 return real_fn(sa, salen, host, host_size, serv, serv_size, flags);
618} 631}
619 632
633// ---- Name-service queries (WSALookupServiceBegin) ----
634
635// Name-service (SAP/RNR) queries would reach the host's providers, so every begin call is refused.
636int WSAAPI wsa_lookup_service_begin_a_offline(LPWSAQUERYSETA restrictions, DWORD, LPHANDLE lookup) {
637 if (lookup) *lookup = nullptr;
638 const bool named = restrictions && restrictions->lpszServiceInstanceName;
639 log_refused("WSALookupServiceBeginA", named ? restrictions->lpszServiceInstanceName : "<null>");
640 set_wsa_error(WSASERVICE_NOT_FOUND);
641 return SOCKET_ERROR;
642}
643
644int WSAAPI wsa_lookup_service_begin_w_offline(LPWSAQUERYSETW restrictions, DWORD, LPHANDLE lookup) {
645 if (lookup) *lookup = nullptr;
646 const bool named = restrictions && restrictions->lpszServiceInstanceName;
647 log_refused("WSALookupServiceBeginW", named ? narrow_ascii(restrictions->lpszServiceInstanceName) : "<null>");
648 set_wsa_error(WSASERVICE_NOT_FOUND);
649 return SOCKET_ERROR;
650}
651
620// ---- WSAIoctl extension-function interception (ConnectEx / WSASendMsg) ---- 652// ---- WSAIoctl extension-function interception (ConnectEx / WSASendMsg) ----
621 653
622std::atomic<LPFN_CONNECTEX> g_host_connect_ex{nullptr}; 654// The provider's own extension function for `s`. Asked per socket: each Winsock provider has its own pointers.
623std::atomic<LPFN_WSASENDMSG> g_host_wsa_send_msg_ext{nullptr}; 655template <typename Fn>
656Fn host_extension(SOCKET s, const GUID& guid) {
657 using IoctlFn = int(WSAAPI*)(SOCKET, DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPWSAOVERLAPPED,
658 LPWSAOVERLAPPED_COMPLETION_ROUTINE);
659 static IoctlFn ioctl = host_proc<IoctlFn>("WSAIoctl");
660 if (!ioctl) return nullptr;
661 GUID requested = guid;
662 Fn fn = nullptr;
663 DWORD returned = 0;
664 if (ioctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &requested, sizeof(requested), &fn, sizeof(fn), &returned, nullptr,
665 nullptr) != 0)
666 return nullptr;
667 return fn;
668}
624 669
625BOOL PASCAL connect_ex_offline(SOCKET s, const sockaddr* name, int namelen, PVOID send_buf, DWORD send_len, 670BOOL PASCAL connect_ex_offline(SOCKET s, const sockaddr* name, int namelen, PVOID send_buf, DWORD send_len,
626 LPDWORD bytes_sent, LPOVERLAPPED overlapped) { 671 LPDWORD bytes_sent, LPOVERLAPPED overlapped) {
@@ -629,7 +674,8 @@ BOOL PASCAL connect_ex_offline(SOCKET s, const sockaddr* name, int namelen, PVOI
629 set_wsa_error(WSAENETUNREACH); 674 set_wsa_error(WSAENETUNREACH);
630 return FALSE; 675 return FALSE;
631 } 676 }
632 LPFN_CONNECTEX real_fn = g_host_connect_ex.load(std::memory_order_acquire); 677 static constexpr GUID kConnectExGuid = WSAID_CONNECTEX;
678 LPFN_CONNECTEX real_fn = host_extension<LPFN_CONNECTEX>(s, kConnectExGuid);
633 if (!real_fn) { 679 if (!real_fn) {
634 set_wsa_error(WSAENETUNREACH); 680 set_wsa_error(WSAENETUNREACH);
635 return FALSE; 681 return FALSE;
@@ -644,7 +690,8 @@ int PASCAL wsa_send_msg_ext_offline(SOCKET s, LPWSAMSG msg, DWORD flags, LPDWORD
644 set_wsa_error(WSAENETUNREACH); 690 set_wsa_error(WSAENETUNREACH);
645 return SOCKET_ERROR; 691 return SOCKET_ERROR;
646 } 692 }
647 LPFN_WSASENDMSG real_fn = g_host_wsa_send_msg_ext.load(std::memory_order_acquire); 693 static constexpr GUID kSendMsgGuid = WSAID_WSASENDMSG;
694 LPFN_WSASENDMSG real_fn = host_extension<LPFN_WSASENDMSG>(s, kSendMsgGuid);
648 if (!real_fn) { 695 if (!real_fn) {
649 set_wsa_error(WSAENETUNREACH); 696 set_wsa_error(WSAENETUNREACH);
650 return SOCKET_ERROR; 697 return SOCKET_ERROR;
@@ -684,7 +731,6 @@ int WSAAPI wsa_ioctl_offline(SOCKET s, DWORD io_control_code, LPVOID in_buf, DWO
684 const int rc = real_fn(s, io_control_code, in_buf, in_len, &real_ext, sizeof(real_ext), &returned, 731 const int rc = real_fn(s, io_control_code, in_buf, in_len, &real_ext, sizeof(real_ext), &returned,
685 overlapped, completion); 732 overlapped, completion);
686 if (rc != 0) return rc; 733 if (rc != 0) return rc;
687 g_host_connect_ex.store(real_ext, std::memory_order_release);
688 LPFN_CONNECTEX wrapped = &connect_ex_offline; 734 LPFN_CONNECTEX wrapped = &connect_ex_offline;
689 std::memcpy(out_buf, &wrapped, sizeof(wrapped)); 735 std::memcpy(out_buf, &wrapped, sizeof(wrapped));
690 *bytes_returned = sizeof(wrapped); 736 *bytes_returned = sizeof(wrapped);
@@ -700,7 +746,6 @@ int WSAAPI wsa_ioctl_offline(SOCKET s, DWORD io_control_code, LPVOID in_buf, DWO
700 const int rc = real_fn(s, io_control_code, in_buf, in_len, &real_ext, sizeof(real_ext), &returned, 746 const int rc = real_fn(s, io_control_code, in_buf, in_len, &real_ext, sizeof(real_ext), &returned,
701 overlapped, completion); 747 overlapped, completion);
702 if (rc != 0) return rc; 748 if (rc != 0) return rc;
703 g_host_wsa_send_msg_ext.store(real_ext, std::memory_order_release);
704 LPFN_WSASENDMSG wrapped = &wsa_send_msg_ext_offline; 749 LPFN_WSASENDMSG wrapped = &wsa_send_msg_ext_offline;
705 std::memcpy(out_buf, &wrapped, sizeof(wrapped)); 750 std::memcpy(out_buf, &wrapped, sizeof(wrapped));
706 *bytes_returned = sizeof(wrapped); 751 *bytes_returned = sizeof(wrapped);
@@ -718,9 +763,10 @@ namespace {
718using namespace rt::ws2; 763using namespace rt::ws2;
719} // namespace 764} // namespace
720 765
721// Both named exports and the fixed Windows ws2_32.dll ordinals (#2 bind, #4 connect, #20 sendto, #51 gethostbyaddr, 766// Both named exports and the fixed Windows ordinals (#2 bind, #4 connect, #20 sendto, #51 gethostbyaddr,
722// #52 gethostbyname, #102 WSAAsyncGetHostByAddr, #103 WSAAsyncGetHostByName) are registered so static imports by 767// #52 gethostbyname, #102 WSAAsyncGetHostByAddr, #103 WSAAsyncGetHostByName) are registered so static imports by
723// ordinal and dynamic GetProcAddress lookups resolve to the same offline wrappers. 768// ordinal and dynamic GetProcAddress lookups resolve to the same offline wrappers. wsock32.dll gets the same
769// wrappers for the entries it shares with ws2_32, since its exports forward to ws2_32 natively.
724SHIM_TABLE(ws2_offline, 770SHIM_TABLE(ws2_offline,
725 {"ws2_32.dll", "bind", reinterpret_cast<void*>(&bind_offline)}, 771 {"ws2_32.dll", "bind", reinterpret_cast<void*>(&bind_offline)},
726 {"ws2_32.dll", "#2", reinterpret_cast<void*>(&bind_offline)}, 772 {"ws2_32.dll", "#2", reinterpret_cast<void*>(&bind_offline)},
@@ -749,4 +795,20 @@ SHIM_TABLE(ws2_offline,
749 {"ws2_32.dll", "WSASendTo", reinterpret_cast<void*>(&wsa_send_to_offline)}, 795 {"ws2_32.dll", "WSASendTo", reinterpret_cast<void*>(&wsa_send_to_offline)},
750 {"ws2_32.dll", "WSASendMsg", reinterpret_cast<void*>(&wsa_send_msg_offline)}, 796 {"ws2_32.dll", "WSASendMsg", reinterpret_cast<void*>(&wsa_send_msg_offline)},
751 {"ws2_32.dll", "WSAIoctl", reinterpret_cast<void*>(&wsa_ioctl_offline)}, 797 {"ws2_32.dll", "WSAIoctl", reinterpret_cast<void*>(&wsa_ioctl_offline)},
798 {"ws2_32.dll", "WSALookupServiceBeginA", reinterpret_cast<void*>(&wsa_lookup_service_begin_a_offline)},
799 {"ws2_32.dll", "WSALookupServiceBeginW", reinterpret_cast<void*>(&wsa_lookup_service_begin_w_offline)},
800 {"wsock32.dll", "bind", reinterpret_cast<void*>(&bind_offline)},
801 {"wsock32.dll", "#2", reinterpret_cast<void*>(&bind_offline)},
802 {"wsock32.dll", "connect", reinterpret_cast<void*>(&connect_offline)},
803 {"wsock32.dll", "#4", reinterpret_cast<void*>(&connect_offline)},
804 {"wsock32.dll", "sendto", reinterpret_cast<void*>(&sendto_offline)},
805 {"wsock32.dll", "#20", reinterpret_cast<void*>(&sendto_offline)},
806 {"wsock32.dll", "gethostbyaddr", reinterpret_cast<void*>(&gethostbyaddr_offline)},
807 {"wsock32.dll", "#51", reinterpret_cast<void*>(&gethostbyaddr_offline)},
808 {"wsock32.dll", "gethostbyname", reinterpret_cast<void*>(&gethostbyname_offline)},
809 {"wsock32.dll", "#52", reinterpret_cast<void*>(&gethostbyname_offline)},
810 {"wsock32.dll", "WSAAsyncGetHostByAddr", reinterpret_cast<void*>(&wsa_async_get_host_by_addr_offline)},
811 {"wsock32.dll", "#102", reinterpret_cast<void*>(&wsa_async_get_host_by_addr_offline)},
812 {"wsock32.dll", "WSAAsyncGetHostByName", reinterpret_cast<void*>(&wsa_async_get_host_by_name_offline)},
813 {"wsock32.dll", "#103", reinterpret_cast<void*>(&wsa_async_get_host_by_name_offline)},
752); 814);