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

Merged
Andretuta merged 4 commits from upstream-pr/357 into main 2026-11-24 13:44:14 +00:00
Andretuta commented 2026-11-24 03:53:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-24T03:53:34Z.


Claimed in the coordination issue as: RT-WS2-OFFLINE (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7121196296)

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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5465758415 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/357). Originally posted by [Andretuta](https://github.com/Andretuta) on 2026-11-24T03:53:34Z. --- Claimed in the coordination issue as: RT-WS2-OFFLINE (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7121196296) ### 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)
QemmHD commented 2026-11-24 05:28:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by QemmHD on 2026-11-24T05:28:45Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7122985672 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/357#issuecomment-7122985672). Originally posted by [QemmHD](https://github.com/QemmHD) on 2026-11-24T05:28:45Z. --- 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 commented 2026-11-24 14:10:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by QemmHD on 2026-11-24T14:10:30Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7133955567 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/357#issuecomment-7133955567). Originally posted by [QemmHD](https://github.com/QemmHD) on 2026-11-24T14:10:30Z. --- 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).
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
ProjectLeonida/mandy!357
No description provided.