runtime: add offline loopback-only Winsock2 (ws2_32.dll) shim #357

Manually merged
github merged 4 commits from gh-pr/357/head into gh-pr/357/base 2026-11-26 08:55:18 +00:00
Owner

@⁠Andretuta on GitHub · 2026-11-24 03:53 UTC · original
ubiquitarianism:feat/ws2-offline-shim → main · +1567 −8 · 8 files · 4 commits · merged 2026-11-24 13:44 UTC

Claimed in the coordination issue as: RT-WS2-OFFLINE (#14)

What and why

Closes the Winsock2 (ws2_32.dll) offline boundary for mapped titles so nothing reaches external hosts or sends DNS queries while loopback IPC continues to work:

  • Adds runtime/shims/ws2_offline.cpp and registers ws2_32.dll as a partially overridden host DLL in runtime/shims/registry.cpp (socket bookkeeping such as WSAStartup, socket, closesocket, select, recv, send, and freeaddrinfo stay native).
  • Restricts connect (#4), WSAConnect, WSAConnectByNameA/W, WSAConnectByList, sendto (#20), WSASendTo, WSASendMsg, and WSAIoctl-acquired ConnectEx / WSASendMsg extension pointers to IPv4 loopback (127.0.0.0/8), IPv6 loopback (::1), and IPv4-mapped IPv6 loopback (::ffff:127.0.0.0/104), refusing external destinations with WSAENETUNREACH and logging each refused target once. Refuses SIO_GET_MULTIPLE_EXTENSION_FUNCTION_POINTER with WSAEINVAL.
  • Rewrites wildcard bind (#2) addresses (0.0.0.0, ::) to 127.0.0.1 / ::1 and refuses non-loopback binds with WSAEADDRNOTAVAIL.
  • Restricts getaddrinfo, GetAddrInfoW, GetAddrInfoExA/W, gethostbyname (#52), gethostbyaddr (#51), and WSAAsyncGetHostByName/Addr (#103/#102) to null/empty nodes, localhost, and loopback literals (WSAHOST_NOT_FOUND otherwise), and forces NI_NUMERICHOST (or WSAHOST_NOT_FOUND with NI_NAMEREQD) on getnameinfo / GetNameInfoW for non-loopback addresses.
  • Allows ordinal shims (#2, #4, #20, #51, #52, #102, #103) on partially overridden host DLLs in runtime/shims/library_loader.cpp so dynamic GetProcAddress by ordinal hits the same offline wrappers as static IAT binding.
  • Resolves host ws2_32.dll dynamically from System32 (LOAD_LIBRARY_SEARCH_SYSTEM32) so the runtime needs no extra static link library.
  • Adds ws2_offline_test in runtime/CMakeLists.txt, adds shims\ws2_offline.cpp to scripts/build_runtime.ps1, and updates docs/boot.md and docs/runtime_imports.md.

Tested

  • Compiled and ran ws2_offline_test locally (Clang 23.1.3 -std=c++20 -Wall -Wextra -Werror): 39 check(s), 0 failure(s) (verifying name + ordinal resolution, external IPv4/IPv6/hostname refusals, wrapped ConnectEx/WSASendMsg via WSAIoctl, wildcard bind narrowing to 127.0.0.1, DNS refusals + localhost allowance, and a real loopback UDP round-trip).
  • Syntax-checked runtime/shims/library_loader.cpp (-fsyntax-only -Wall -Wextra -Werror).
  • Ran python -I scripts/check_no_game_data.py on all 8 touched files (exit 0) and python -I tests/test_docs_links.py (16 tests OK).

GPUs tested: n/a

AI-assisted: yes

Update after review (merged with current main)

  • ConnectEx and WSASendMsg extension pointers are looked up per socket through the host WSAIoctl, so each provider's own function is used.
  • WSALookupServiceBeginA/W are refused (WSASERVICE_NOT_FOUND), so name lookups cannot go around the shim.
  • WSAConnectByNameA/W connect only to localhost/loopback literals; IPv4 literals with leading-zero octets (0127.0.0.1) are refused.
  • wsock32.dll exports (bind, connect, sendto, gethostbyname, gethostbyaddr and the async lookups, by name and ordinal) go through the same checks; wsock32.dll is a partially overridden host DLL in owns_dll.
  • scripts/build_runtime.ps1 keeps main's dstorage.cpp; docs/runtime_imports.md no longer says --run can open real sockets and names the remaining dnsapi.dll gap.
  • Tested: ws2_offline_test 52 checks, 0 failures (clang 23 / mingw x64); docs tests.

Update after the second review (implicit binds and multicast)

  • Implicit UDP bind: sendto, WSASendTo, WSASendMsg (and its WSAIoctl pointer) and WSAJoinLeaf on an unbound socket bind it to 127.0.0.1 (or ::1 / ::ffff:127.0.0.1 for IPv6) before the host call. Microsoft's sendto, WSASendTo and WSAJoinLeaf pages say an unbound socket is bound implicitly to the wildcard address. The new test shows the host doing exactly that (control: getsockname gives 0.0.0.0) and the shim giving 127.0.0.1 instead.
  • Multicast: setsockopt (#21) refuses group joins with WSAEADDRNOTAVAIL (IP_ADD_MEMBERSHIP, IP_ADD_SOURCE_MEMBERSHIP, MCAST_JOIN_GROUP, MCAST_JOIN_SOURCE_GROUP, IPV6_ADD_MEMBERSHIP); other options go to the host. wsock32.dll's setsockopt (#21) is its own export, not a forwarder (dumpbin on Windows 10), and takes Winsock 1 option numbers. So its shim also refuses IP_ADD_MEMBERSHIP as 5 (Wine's wsock32 remap table: 5 -> 12) and forwards everything else to the host wsock32.dll.
  • WSAJoinLeaf (by name: ws2_32 has no fixed ordinal for it) refuses non-loopback groups with WSAENETUNREACH.
  • What the title reaches: its import table (tools/pe_inspect.py) lists WS2_32.dll ordinals #20 (sendto) and #21 (setsockopt) among its 25 Winsock imports, so the implicit bind and the join refusals are on paths it links. It does not import WSAJoinLeaf, WSASendTo or wsock32.dll; those stay covered in case GetProcAddress or a package DLL reaches them.
  • Tested: MSVC 14.44 / Windows SDK 10.0.26100, ws2_offline_test 71 checks, 0 failures. The 19 new checks cover: the resolution of #21, WSAJoinLeaf and the wsock32 shim; the implicit binds against the host control; IPv4, IPv6 and Winsock 1 joins refused; TTL and buffer options passed through; WSAJoinLeaf refused. Docs tests: 47 passed.

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

**@⁠Andretuta** on GitHub · 2026-11-24 03:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/357) `ubiquitarianism:feat/ws2-offline-shim` → `main` · +1567 −8 · 8 files · 4 commits · merged 2026-11-24 13:44 UTC Claimed in the coordination issue as: RT-WS2-OFFLINE (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why Closes the Winsock2 (`ws2_32.dll`) offline boundary for mapped titles so nothing reaches external hosts or sends DNS queries while loopback IPC continues to work: - Adds `runtime/shims/ws2_offline.cpp` and registers `ws2_32.dll` as a partially overridden host DLL in `runtime/shims/registry.cpp` (socket bookkeeping such as `WSAStartup`, `socket`, `closesocket`, `select`, `recv`, `send`, and `freeaddrinfo` stay native). - Restricts `connect` (`#4`), `WSAConnect`, `WSAConnectByNameA/W`, `WSAConnectByList`, `sendto` (`#20`), `WSASendTo`, `WSASendMsg`, and `WSAIoctl`-acquired `ConnectEx` / `WSASendMsg` extension pointers to IPv4 loopback (`127.0.0.0/8`), IPv6 loopback (`::1`), and IPv4-mapped IPv6 loopback (`::ffff:127.0.0.0/104`), refusing external destinations with `WSAENETUNREACH` and logging each refused target once. Refuses `SIO_GET_MULTIPLE_EXTENSION_FUNCTION_POINTER` with `WSAEINVAL`. - Rewrites wildcard `bind` (`#2`) addresses (`0.0.0.0`, `::`) to `127.0.0.1` / `::1` and refuses non-loopback binds with `WSAEADDRNOTAVAIL`. - Restricts `getaddrinfo`, `GetAddrInfoW`, `GetAddrInfoExA/W`, `gethostbyname` (`#52`), `gethostbyaddr` (`#51`), and `WSAAsyncGetHostByName/Addr` (`#103`/`#102`) to null/empty nodes, `localhost`, and loopback literals (`WSAHOST_NOT_FOUND` otherwise), and forces `NI_NUMERICHOST` (or `WSAHOST_NOT_FOUND` with `NI_NAMEREQD`) on `getnameinfo` / `GetNameInfoW` for non-loopback addresses. - Allows ordinal shims (`#2`, `#4`, `#20`, `#51`, `#52`, `#102`, `#103`) on partially overridden host DLLs in `runtime/shims/library_loader.cpp` so dynamic `GetProcAddress` by ordinal hits the same offline wrappers as static IAT binding. - Resolves host `ws2_32.dll` dynamically from System32 (`LOAD_LIBRARY_SEARCH_SYSTEM32`) so the runtime needs no extra static link library. - Adds `ws2_offline_test` in `runtime/CMakeLists.txt`, adds `shims\ws2_offline.cpp` to `scripts/build_runtime.ps1`, and updates `docs/boot.md` and `docs/runtime_imports.md`. ### Tested - Compiled and ran `ws2_offline_test` locally (Clang 23.1.3 `-std=c++20 -Wall -Wextra -Werror`): **39 check(s), 0 failure(s)** (verifying name + ordinal resolution, external IPv4/IPv6/hostname refusals, wrapped `ConnectEx`/`WSASendMsg` via `WSAIoctl`, wildcard `bind` narrowing to `127.0.0.1`, DNS refusals + `localhost` allowance, and a real loopback UDP round-trip). - Syntax-checked `runtime/shims/library_loader.cpp` (`-fsyntax-only -Wall -Wextra -Werror`). - Ran `python -I scripts/check_no_game_data.py` on all 8 touched files (exit 0) and `python -I tests/test_docs_links.py` (16 tests OK). GPUs tested: n/a AI-assisted: yes ### Update after review (merged with current `main`) - `ConnectEx` and `WSASendMsg` extension pointers are looked up per socket through the host `WSAIoctl`, so each provider's own function is used. - `WSALookupServiceBeginA/W` are refused (`WSASERVICE_NOT_FOUND`), so name lookups cannot go around the shim. - `WSAConnectByNameA/W` connect only to `localhost`/loopback literals; IPv4 literals with leading-zero octets (`0127.0.0.1`) are refused. - `wsock32.dll` exports (`bind`, `connect`, `sendto`, `gethostbyname`, `gethostbyaddr` and the async lookups, by name and ordinal) go through the same checks; `wsock32.dll` is a partially overridden host DLL in `owns_dll`. - `scripts/build_runtime.ps1` keeps main's `dstorage.cpp`; `docs/runtime_imports.md` no longer says `--run` can open real sockets and names the remaining `dnsapi.dll` gap. - Tested: `ws2_offline_test` 52 checks, 0 failures (clang 23 / mingw x64); docs tests. ### Update after the second review (implicit binds and multicast) - **Implicit UDP bind:** `sendto`, `WSASendTo`, `WSASendMsg` (and its `WSAIoctl` pointer) and `WSAJoinLeaf` on an unbound socket bind it to `127.0.0.1` (or `::1` / `::ffff:127.0.0.1` for IPv6) before the host call. Microsoft's `sendto`, `WSASendTo` and `WSAJoinLeaf` pages say an unbound socket is bound implicitly to the wildcard address. The new test shows the host doing exactly that (control: `getsockname` gives `0.0.0.0`) and the shim giving `127.0.0.1` instead. - **Multicast:** `setsockopt` (`#21`) refuses group joins with `WSAEADDRNOTAVAIL` (`IP_ADD_MEMBERSHIP`, `IP_ADD_SOURCE_MEMBERSHIP`, `MCAST_JOIN_GROUP`, `MCAST_JOIN_SOURCE_GROUP`, `IPV6_ADD_MEMBERSHIP`); other options go to the host. `wsock32.dll`'s `setsockopt` (`#21`) is its own export, not a forwarder (dumpbin on Windows 10), and takes Winsock 1 option numbers. So its shim also refuses `IP_ADD_MEMBERSHIP` as 5 (Wine's `wsock32` remap table: 5 -> 12) and forwards everything else to the host `wsock32.dll`. - **`WSAJoinLeaf`** (by name: ws2_32 has no fixed ordinal for it) refuses non-loopback groups with `WSAENETUNREACH`. - **What the title reaches:** its import table (`tools/pe_inspect.py`) lists `WS2_32.dll` ordinals `#20` (`sendto`) and `#21` (`setsockopt`) among its 25 Winsock imports, so the implicit bind and the join refusals are on paths it links. It does not import `WSAJoinLeaf`, `WSASendTo` or `wsock32.dll`; those stay covered in case `GetProcAddress` or a package DLL reaches them. - Tested: MSVC 14.44 / Windows SDK 10.0.26100, `ws2_offline_test` 71 checks, 0 failures. The 19 new checks cover: the resolution of `#21`, `WSAJoinLeaf` and the `wsock32` shim; the implicit binds against the host control; IPv4, IPv6 and Winsock 1 joins refused; TTL and buffer options passed through; `WSAJoinLeaf` refused. Docs tests: 47 passed. ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
# Conflicts:
#	scripts/build_runtime.ps1
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 <[email protected]>
Closes the gaps from the security review on #357:

- sendto, WSASendTo, WSASendMsg (both exports) and WSAJoinLeaf on an
  unbound socket made the host bind it to the wildcard address
  (Microsoft's sendto remarks), so its port took datagrams from other
  hosts. The shim now binds such a socket to 127.0.0.1, ::1 or
  ::ffff:127.0.0.1 first and fails with WSAEADDRNOTAVAIL if it stays
  unbound.
- setsockopt (ws2_32 name and #21) refuses IP_ADD_MEMBERSHIP,
  IP_ADD_SOURCE_MEMBERSHIP, IPV6_ADD_MEMBERSHIP, MCAST_JOIN_GROUP and
  MCAST_JOIN_SOURCE_GROUP with WSAEADDRNOTAVAIL. wsock32's setsockopt is
  its own export (dumpbin on Windows 10 22H2: not a forwarder) that remaps
  the Winsock 1.1 IP_ numbers (Wine dlls/wsock32/socket.c has the same
  table), so it gets its own wrapper that also refuses the 1.1 join (5)
  and forwards everything else to the host wsock32.
- WSAJoinLeaf gets connect's loopback check, so a group is refused.

Tested: ws2_offline_test 71 checks, 0 failures, MSVC 14.44 / SDK
10.0.26100 on Windows 10, including a control check that the host sendto
alone binds to 0.0.0.0; docs tests 47 passed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:55:16 +00:00
github manually merged commit 1f88b25d9a into gh-pr/357/base 2026-11-26 08:55:18 +00:00
Author
Owner

@⁠QemmHD on GitHub · 2026-11-24 05:28 UTC · original

The network boundary looks right: it fails closed, the host DLL is loaded with LOAD_LIBRARY_SEARCH_SYSTEM32, wildcard binds are narrowed to loopback, ConnectEx and WSASendMsg are wrapped, and the RIO multi-pointer request is refused. Gaps worth closing so "loopback only" holds: WSAJoinLeaf isn't wrapped (a native send() can then reach a multicast group), WSALookupServiceBegin/Next aren't wrapped (DNS can still go out), an unbound UDP sendto gets an implicit wildcard bind, and multicast joins via setsockopt aren't blocked; dnsapi, iphlpapi ICMP and mswsock GetAddressByName are outside the shim. Also needs a rebase.

Security review; read only, nothing run.
AI-assisted (Claude Code, Claude Opus 5.5).
Discord: bigpeppa.

**@⁠QemmHD** on GitHub · 2026-11-24 05:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/357#issuecomment-7122985672) The network boundary looks right: it fails closed, the host DLL is loaded with `LOAD_LIBRARY_SEARCH_SYSTEM32`, wildcard binds are narrowed to loopback, ConnectEx and WSASendMsg are wrapped, and the RIO multi-pointer request is refused. Gaps worth closing so "loopback only" holds: `WSAJoinLeaf` isn't wrapped (a native `send()` can then reach a multicast group), `WSALookupServiceBegin/Next` aren't wrapped (DNS can still go out), an unbound UDP `sendto` gets an implicit wildcard bind, and multicast joins via `setsockopt` aren't blocked; `dnsapi`, `iphlpapi` ICMP and mswsock `GetAddressByName` are outside the shim. Also needs a rebase. Security review; read only, nothing run. AI-assisted (Claude Code, Claude Opus 5.5). Discord: bigpeppa.
Author
Owner

@⁠QemmHD on GitHub · 2026-11-24 14:10 UTC · original

Post-merge check of main d0da7dade on Windows 11 / RTX 4070 SUPER (VS 2022, Ninja, RelWithDebInfo): ws2_offline passes, and full ctest is 304/306 with the same two pre-existing failures as before the merge (d3d12_execute_indirect_validation, d3d12_depth_resolve).

One gap, low severity, reproduced on this PC: multicast joins through WSAIoctl with SIOCSIPMSFILTER (0x8004747D, what ws2tcpip.h's inline setipv4sourcefilter uses) or SIOCSMSFILTER reach the host. wsa_ioctl_offline passes those codes through (runtime/shims/ws2_offline.cpp:885). A UDP socket bound to 127.0.0.1 joined 239.255.77.77 on the real Wi-Fi interface. That contradicts the "every join is refused" comment at ws2_offline.cpp:511-512. The title doesn't import WSAIoctl statically (docs/runtime_imports.md), so this is LAN-only and needs a dynamic lookup. Refusing those two ioctl codes the same way IP_ADD_MEMBERSHIP is refused would close it.

I found no DNS leak: [::1] and zone-suffixed loopback forms resolve locally, and the getaddrinfo family stays offline.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-24 14:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/357#issuecomment-7133955567) Post-merge check of main d0da7dade on Windows 11 / RTX 4070 SUPER (VS 2022, Ninja, RelWithDebInfo): `ws2_offline` passes, and full ctest is 304/306 with the same two pre-existing failures as before the merge (d3d12_execute_indirect_validation, d3d12_depth_resolve). One gap, low severity, reproduced on this PC: multicast joins through `WSAIoctl` with `SIOCSIPMSFILTER` (0x8004747D, what ws2tcpip.h's inline `setipv4sourcefilter` uses) or `SIOCSMSFILTER` reach the host. `wsa_ioctl_offline` passes those codes through (`runtime/shims/ws2_offline.cpp:885`). A UDP socket bound to 127.0.0.1 joined 239.255.77.77 on the real Wi-Fi interface. That contradicts the "every join is refused" comment at `ws2_offline.cpp:511-512`. The title doesn't import `WSAIoctl` statically (docs/runtime_imports.md), so this is LAN-only and needs a dynamic lookup. Refusing those two ioctl codes the same way `IP_ADD_MEMBERSHIP` is refused would close it. I found no DNS leak: `[::1]` and zone-suffixed loopback forms resolve locally, and the getaddrinfo family stays offline. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.