[PR #357] [MERGED] runtime: add offline loopback-only Winsock2 (ws2_32.dll) shim #410

Closed
opened 2026-11-26 20:17:48 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/357
Author: @Andretuta
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/24/2026
Merged by: @ubiquitarianism

Base: main ← Head: feat/ws2-offline-shim


📝 Commits (4)

  • 9b59db0 ws2_offline: add offline loopback-only Winsock2 (ws2_32.dll) shim (RT-WS2-OFFLINE)
  • 7f7a596 Merge remote-tracking branch 'ubiq/main' into feat/ws2-offline-shim
  • d7cdfcf ws2_offline: per-socket extension pointers, refuse name-service lookups, close connect-by-name and octal bypasses, cover wsock32
  • 1f88b25 ws2_offline: loopback-bind unbound sends, refuse multicast joins and WSAJoinLeaf groups

📊 Changes

8 files changed (+1567 additions, -8 deletions)

View changed files

📝 docs/boot.md (+3 -2)
📝 docs/runtime_imports.md (+48 -3)
📝 runtime/CMakeLists.txt (+16 -0)
📝 runtime/shims/library_loader.cpp (+1 -1)
📝 runtime/shims/registry.cpp (+2 -1)
➕ runtime/shims/ws2_offline.cpp (+949 -0)
➕ runtime/test/ws2_offline_test.cpp (+547 -0)
📝 scripts/build_runtime.ps1 (+1 -1)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/357 **Author:** [@Andretuta](https://github.com/Andretuta) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/24/2026 **Merged by:** [@ubiquitarianism](https://github.com/ubiquitarianism) **Base:** `main` ← **Head:** `feat/ws2-offline-shim` --- ### 📝 Commits (4) - [`9b59db0`](https://github.com/ubiquitarianism/leonida/commit/9b59db02f974edc453bab4c2bf69e52f87447ebf) ws2_offline: add offline loopback-only Winsock2 (ws2_32.dll) shim (RT-WS2-OFFLINE) - [`7f7a596`](https://github.com/ubiquitarianism/leonida/commit/7f7a5968563718098c3c592193ecb8da14e7d49a) Merge remote-tracking branch 'ubiq/main' into feat/ws2-offline-shim - [`d7cdfcf`](https://github.com/ubiquitarianism/leonida/commit/d7cdfcf25500503fbd694fb6eb949b694ae71bdf) ws2_offline: per-socket extension pointers, refuse name-service lookups, close connect-by-name and octal bypasses, cover wsock32 - [`1f88b25`](https://github.com/ubiquitarianism/leonida/commit/1f88b25d9a3bd2b1266a83f8ef7abe8287298fde) ws2_offline: loopback-bind unbound sends, refuse multicast joins and WSAJoinLeaf groups ### 📊 Changes **8 files changed** (+1567 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `docs/boot.md` (+3 -2) 📝 `docs/runtime_imports.md` (+48 -3) 📝 `runtime/CMakeLists.txt` (+16 -0) 📝 `runtime/shims/library_loader.cpp` (+1 -1) 📝 `runtime/shims/registry.cpp` (+2 -1) ➕ `runtime/shims/ws2_offline.cpp` (+949 -0) ➕ `runtime/test/ws2_offline_test.cpp` (+547 -0) 📝 `scripts/build_runtime.ps1` (+1 -1) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:17:48 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#410
No description provided.