Skip to content

ws2_offline: refuse the local computer's name, answer localhost with loopback literals, keep WSAIoctl synchronous (RT-WS2-OFFLINE-LOOKUP-LEAKS) - #2533

Merged
MoonEyes merged 3 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-ws2-offline-lookup-leaks
Nov 28, 2026
Merged

MoonEyes merged 3 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-ws2-offline-lookup-leaks

Conversation

@Tyrannys

@Tyrannys Tyrannys commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-WS2-OFFLINE-LOOKUP-LEAKS (#1608 (comment))

What and why

Item 3 of @kevincommits' batch 18 in #1608 (#1608 (comment)). Thanks for the line-by-line report.

runtime/shims/ws2_offline.cpp is what keeps a title off the network. Three gaps:

  • The local computer's own addresses. is_loopback_node_a/w returned true for a null and an empty name. So getaddrinfo, GetAddrInfoW, GetAddrInfoExA/W, gethostbyname and WSAAsyncGetHostByName passed "" (and, for the last two, NULL) to the host. The host answers those with every address of the local computer: its LAN and external adapters. WSAConnectByNameA/W already refused them.
    • Now an empty name is not loopback, and gethostbyname/WSAAsyncGetHostByName also refuse a null one (WSAHOST_NOT_FOUND).
    • A null getaddrinfo node is still answered: it is a service-only lookup that gives loopback, or with AI_PASSIVE the wildcard, which the shim's bind narrows to loopback.
  • localhost and the hosts file. WSAConnectByNameA/W hand the host the literal 127.0.0.1 for localhost, so the hosts file is never read. The six lookup shims passed the name itself, so a hosts entry could map it off loopback.
    • They now pass a loopback literal too: ::1 for an AF_INET6 lookup, else 127.0.0.1. gethostbyname and WSAAsyncGetHostByName are IPv4 only, so they get 127.0.0.1.
    • An AF_UNSPEC lookup of localhost therefore answers 127.0.0.1 only, where Windows also lists ::1.
  • Overlapped WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER). For WSAID_CONNECTEX and WSAID_WSASENDMSG, the shim passed the caller's overlapped and completion routine to the host, together with &real_ext and &returned in its own stack frame. If the host completed later, it would write into a frame that was gone.
    • The host now writes its pointer and byte count into a small heap block of the shim's (wrap_extension_lookup). The block is freed when the call completes and kept if the host reports it pending.
    • The caller's overlapped and routine still go to the host, so the completion (port packet, event, routine, or nothing for a socket without WSA_FLAG_OVERLAPPED) is reported as Winsock reports it.
    • The wrapper is in the caller's buffer before the host is called, so the caller never sees the native pointer. On failure the buffer gets its old contents back.
  • Under AI_NUMERICHOST, localhost is passed on unchanged, so the host refuses it (EAI_NONAME).

The header comment and docs/runtime_imports.md describe the new rules.

Overlap: #2241 (connect and UDP) and #2242 (WSAConnectByName to localhost on IPv6 sockets) change other functions of this file and test. Whichever lands second may need a small rebase of the test file, which all three append to. With #2242, WSAConnectByName and these lookups pick ::1 the same way: by the socket's or the lookup's family.

Tested

Windows 11, MSVC (VS 18), RelWithDebInfo, on main 431ceba. Loopback only, no network.

  • ws2_offline_test gains section 9:
    • gethostbyname(NULL) and gethostbyname(""), WSAAsyncGetHostByName with a null or empty name, getaddrinfo("") and GetAddrInfoW(L"") are refused;
    • getaddrinfo(NULL, "80") is still answered;
    • getaddrinfo("localhost", AF_INET) answers 127.0.0.1 only, getaddrinfo("localhost.", AF_INET6) ::1 only, and gethostbyname("LOCALHOST") loopback only;
    • an overlapped WSAIoctl for ConnectEx returns the same wrapped pointer as a plain one and sets its event;
    • on a completion-port socket, it posts its packet with the wrapped pointer already in the buffer;
    • on a socket without WSA_FLAG_OVERLAPPED, the overlapped ends as the host's own call leaves it;
    • getaddrinfo("localhost", AI_NUMERICHOST) is EAI_NONAME;
    • one for WSASendMsg with a completion routine runs it once at the next alertable wait.
  • With main's shim the test fails at gethostbyname(NULL) (the host answered with this computer's addresses) and then crashes later in section 9. With the fix, 87/87 checks pass (the review's three cases fail with the first version of this PR).
  • scripts/check_no_game_data.py: clean.

Not tested: a hosts file that maps localhost elsewhere (the test does not edit the system's hosts file), and Wine.

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU tests only)

AI-assisted: yes (Claude Code)

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. (n/a: no translator change)

🤖 Generated with Claude Code

Tyrannys and others added 2 commits November 27, 2026 19:10
…loopback literals, keep WSAIoctl synchronous

From kevincommits' finding (batch 18, item 3) in ProjectLeonida#1608.

- An empty name ("") passed is_loopback_node_a, so getaddrinfo, GetAddrInfoW,
  GetAddrInfoExA/W, gethostbyname and WSAAsyncGetHostByName forwarded it to the
  host, which answers with every address of the local computer (its LAN and
  external adapters). A null name did the same for gethostbyname and
  WSAAsyncGetHostByName. Both are refused now; a null getaddrinfo node (a
  service-only lookup: loopback, or the wildcard that bind narrows) is still
  answered.
- "localhost" went to the host as a name, so its hosts file could map it off
  loopback. The lookups now hand the host a loopback literal for it (::1 for an
  AF_INET6 lookup, else 127.0.0.1), as WSAConnectByNameA/W already do.
- WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER) for ConnectEx and WSASendMsg
  passed the caller's overlapped and completion routine to the host along with
  pointers into its own stack frame. The host lookup is now synchronous, as in
  host_extension, and an overlapped request is reported as completed at once:
  status and byte count in the OVERLAPPED, its event set or its routine queued.

ws2_offline_test gains section 9 for each case. With main's shim the test
fails at gethostbyname(NULL) and then crashes; 83/83 checks pass with the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…SAIoctl lookup

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timbot103

Copy link
Copy Markdown
Contributor

Reviewed at head f589998bf on Windows 11 (MSVC 2022 19.44).

Verification

  • Built ws2_offline_test.
  • ws2_offline_test: 83/83 checks passed.

Findings (Request changes)

  1. Synthetic complete_now drops IOCP completions for overlapped sockets (ws2_offline.cpp:851-874, 903-931):
    Both wrapped WSAIoctl GUID handlers pass nullptr, nullptr for overlapped/completion to real_fn and then call complete_now(). For an overlapped socket associated with an I/O completion port where the caller provides an OVERLAPPED without an event or APC routine (and without FILE_SKIP_COMPLETION_PORT_ON_SUCCESS), complete_now() only fills Internal/InternalHigh and posts no IOCP packet, leaving a caller waiting on GetQueuedCompletionStatus hung indefinitely. (The new tests in ws2_offline_test.cpp:601-635 exercise event and APC only.)
  2. Non-overlapped sockets receive completions they must ignore (ws2_offline.cpp:851-874, 903-931):
    complete_now() unconditionally writes *overlapped and signals hEvent or queues an APC whenever overlapped != nullptr, without checking whether s was created with WSA_FLAG_OVERLAPPED. Per the Winsock WSAIoctl contract, both lpOverlapped and lpCompletionRoutine are ignored on non-overlapped sockets.
  3. Minor (ws2_offline.cpp:264-272, 612-668, 858-870):
    • Substituting loopback literals for "localhost" does not check AI_NUMERICHOST first, so getaddrinfo("localhost", ..., AI_NUMERICHOST) can succeed instead of returning EAI_NONAME.
    • In complete_now(), heap-allocating Call for QueueUserAPC leaks if the calling thread exits before entering an alertable wait.

AI-assisted review + local MSVC verification.

…MERICHOST keeps localhost a name

From timbot103's review of ProjectLeonida#2533.

- The ConnectEx and WSASendMsg lookups no longer complete the caller's
  overlapped themselves (complete_now). That posted no packet for a socket on
  an I/O completion port, so a caller waiting in GetQueuedCompletionStatus
  hung, and it signalled an overlapped a socket without WSA_FLAG_OVERLAPPED
  must ignore; its APC record also leaked if the thread never waited
  alertably. The host's WSAIoctl now gets the caller's overlapped and
  completion routine again, so the completion is reported as Winsock reports
  it, but writes its own pointer and byte count into storage of the shim's
  (wrap_extension_lookup): freed when the call completed, kept if the host
  reports it pending. The caller's buffer holds the wrapper before the host
  is called, so a packet another thread dequeues already finds it, and gets
  its old contents back on failure.
- With AI_NUMERICHOST, "localhost" is passed on as the name, so the host
  refuses it with EAI_NONAME instead of the shim answering with a literal.

ws2_offline_test: a completion-port socket gets its packet with the wrapped
pointer; on a socket without WSA_FLAG_OVERLAPPED the overlapped ends as the
host's own WSAIoctl leaves it; getaddrinfo(localhost, AI_NUMERICHOST) is
EAI_NONAME. The event and routine cases follow what Windows 11 does for this
lookup (measured: event set, routine run once, packet posted, 0 bytes in the
OVERLAPPED). The previous commit fails these 3 checks; 87/87 pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Tyrannys

Copy link
Copy Markdown
Contributor Author

@timbot103 thanks, all four points were right. Fixed in 37163d1.

1 and 2 (completion port, non-overlapped sockets): complete_now is gone. The shim no longer completes the caller's overlapped itself. The host's WSAIoctl gets the caller's lpOverlapped and lpCompletionRoutine again, so the completion is reported exactly as Winsock reports it for that socket: a port packet, the event, the routine, or nothing for a socket without WSA_FLAG_OVERLAPPED.

The original problem was the stack frame, and that is fixed in wrap_extension_lookup:

  • The host writes its own pointer and byte count into a small heap block of the shim's, not into &real_ext on the stack and not into the caller's buffer.
  • The block is freed when the call completes (the normal case). It is kept if the host ever reports WSA_IO_PENDING, since the host may still write to it then.
  • The wrapper is written into the caller's buffer before the host is called, so a packet another thread dequeues already finds the wrapped ConnectEx/WSASendMsg, never the native one.
  • On failure the buffer gets its old contents back.

3a (AI_NUMERICHOST): localhost is passed on unchanged under AI_NUMERICHOST, so the host refuses it with EAI_NONAME and does no lookup.

3b (the APC record leak): gone with complete_now.

Evidence. What Windows 11 itself does for this lookup with an overlapped (a standalone probe on an overlapped socket):

  • event: set;
  • routine: run once at the next alertable wait;
  • completion port: one packet;
  • in each case rc == 0, lpcbBytesReturned == 8, and Internal/InternalHigh 0.

ws2_offline_test now checks:

  • a completion-port socket with an OVERLAPPED without event or routine: GetQueuedCompletionStatus returns the packet (our key, our OVERLAPPED), and the buffer holds the wrapped ConnectEx;
  • a socket without WSA_FLAG_OVERLAPPED: the event and OVERLAPPED end the same as after the host's own WSAIoctl on the same socket;
  • getaddrinfo("localhost", AI_NUMERICHOST) is EAI_NONAME, and "127.0.0.1" is still answered.

The event and routine checks now expect what Windows does (no byte count in the OVERLAPPED).

With the previous commit's shim these 3 new checks fail. With this one, 87/87 pass. Windows 11, MSVC (VS 18), RelWithDebInfo.

AI-assisted (Claude Code)

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2533 | head 37163d1 (merged cleanly with main 6dfca64) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^ws2_offline$" --output-on-failure | red on main: ws2_offline_test fails and crashes with 0xC0000005 (STATUS_ACCESS_VIOLATION) after gethostbyname(NULL) / "" was allowed through is_loopback_node_a instead of being refused with WSAHOST_NOT_FOUND | green on PR+main: 1/1 CTest passes (ws2_offline 87/87 checks in 1.08s, verifying empty/null local-host name refusal, localhost -> 127.0.0.1/::1 literal rewrite, AI_NUMERICHOST preservation, and overlapped/IOCP/completion-routine/non-overlapped WSAIoctl extension-pointer wrapping); check_no_game_data.py 0 violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

@timbot103 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static code audit against 37163d100 and ec740bcc3. I would request changes for four issues.

  • runtime/shims/ws2_offline.cpp:879 adds a direct WSAGetLastError import. The standalone test links ws2_32, but the runtime without the optional Rust decoders has no such link dependency. Resolve it through the existing host lookup before calling the provider.
  • runtime/shims/ws2_offline.cpp:270 and :274 drop ::1 for AF_UNSPEC and null hints. A client cannot discover an IPv6-only localhost listener through these calls. Preserve both loopback families under those hints with native result ownership.
  • runtime/shims/ws2_offline.cpp:876 writes the wrapper before the provider reads in_buf. With shared input and output storage that overwrites the GUID. Pass the saved GUID through storage that survives the provider's use.
  • runtime/shims/ws2_offline.cpp:885 leaks the native scratch allocation on every WSA_IO_PENDING return, even after completion or cancellation. Give the storage a completion lifetime or guarantee synchronous lookup while preserving native notification behavior.

The reported Windows tests cover event, IOCP, APC and non-overlapped cases. Please add coverage for overlapping buffers, a forced pending result, both loopback families with default hints, and the Ex variants.

AI-assisted.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed and locally tested 37163d100 (runtime/shims/ws2_offline.cpp, runtime/test/ws2_offline_test.cpp).

Local verification:

  • Built ws2_offline_test (MSVC Release / Ninja) and ran ctest -R ^ws2_offline$: 1/1 passed.
  • python -I scripts/check_no_game_data.py: passed.

Holding off on approval because code inspection confirms the remaining issues in runtime/shims/ws2_offline.cpp:

  1. wrap_extension_lookup (runtime/shims/ws2_offline.cpp:879) calls WSAGetLastError() directly instead of resolving it dynamically through host_proc (unlike set_wsa_error and the other host ws2_32.dll calls), introducing a direct import-library dependency on ws2_32.
  2. wrap_extension_lookup (runtime/shims/ws2_offline.cpp:876) overwrites out_buf with &wrapped before invoking real_fn(..., in_buf, ...). If a caller aliases in_buf == out_buf, the requested extension GUID is clobbered before Winsock reads it.
  3. On WSA_IO_PENDING (runtime/shims/ws2_offline.cpp:885), the heap-allocated HostAnswer is never freed.
  4. For AF_UNSPEC, rewriting "localhost" to "127.0.0.1" drops ::1 loopback results.

AI-assisted (Antigravity, Gemini).

@OreoFR

OreoFR commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR

  • Base: main c1ecf04ed + PR head 37163d100
  • Test: ctest -R "^ws2_offline$" + gta6rt build
  • Red on main: crash (SegFault, "geth" failure)
  • Green with the PR: ws2_offline OK, but gta6rt.exe does not link: LNK2019 __imp_WSAGetLastError in ws2_offline.cpp (wrap_extension_lookup); gta6rt does not link ws2_32; reproduced on main + this PR alone
  • Verdict: FAILS (only ws2_offline_test links ws2_32; needs another call than WSAGetLastError, or ws2_32 linked into gta6rt)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

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

@TheGeogeo

TheGeogeo commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Correction: I missed a link failure in this review. With this change, gta6rt.exe calls WSAGetLastError directly in rt::ws2::wrap_extension_lookup (runtime/shims/ws2_offline.cpp:879), while the rest of the shim reaches it through host_proc. gta6rt does not link ws2_32, so the link fails with LNK2019 __imp_WSAGetLastError, then LNK1120.

My build did not catch it: in my configuration the Rust XMA2/Opus decoders' interface libraries bring ws2_32 into the gta6rt link. I reproduced it here by linking gta6rt.exe without ws2_32.lib. That adds exactly one unresolved symbol over main: __imp_WSAGetLastError, referenced in wrap_extension_lookup. A fix through host_proc (no ws2_32) is being prepared by another tester.

Original comment below.


Tested 37163d100 merged (no commit) with main aa918e5d9. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40, Windows 11 10.0.26300; MSVC 19.52 (Visual Studio 18), RelWithDebInfo, Ninja.

  • Green: ws2_offline passes.
  • Red: with runtime/shims/ws2_offline.cpp back to main, ws2_offline prints FAIL geth (the output stops there) and then crashes: ctest reports SEGFAULT.
  • No regression: the merge relinks 9 executables, and their 20 other tests pass.

No blocker from my side. AI-assisted (Claude Code).

@MoonEyes
MoonEyes merged commit 7f0b878 into ProjectLeonida:main Nov 28, 2026
6 of 12 checks passed
@MoonEyes

Copy link
Copy Markdown
Collaborator

Merged after TheGeogeo's red/green check (check: green with this PR, red with main's code, no regression over the relinked tests) and a re-check here: no conflict with main 1c97c43, same head as checked, no-game-data green, and the only ctest failures in the Windows/macOS CI jobs are tests this diff does not touch (known on main or Vulkan: vk_xdxr_postbuild_readback vk_bindless_dimensions d3d12_compile_pool xbox_present ). The Python jobs fail on main too.

AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.

@MoonEyes

Copy link
Copy Markdown
Collaborator

Heads-up after merge: this breaks the gta6rt.exe link on Windows/MSVC builds without the Rust XMA decoder (LNK2019 __imp_WSAGetLastError, then LNK1120, reported by OreoFR on main a9e247c). ws2_offline.cpp:36 resolves every host ws2_32 call dynamically so gta6rt needs no ws2_32.lib; the new direct WSAGetLastError() call does not, and CI only linked because the XMA decoder target brings ws2_32 (runtime/CMakeLists.txt:155). A fix through the existing host_proc<LastErrFn>("WSAGetLastError") (ws2_offline.cpp:388) is being prepared by OreoFR's agent. Until it lands, rebuild runtime/ from 1bc055e6c. My merge, my miss.

AI-assisted (Claude Code, Claude Opus 5.5), posted for MoonEyes.

MoonEyes pushed a commit that referenced this pull request Nov 28, 2026
…s without ws2_32

wrap_extension_lookup (#2533) called WSAGetLastError directly. gta6rt does not link ws2_32 (only
ws2_offline_test does), so the MSVC link of gta6rt.exe failed with LNK2019 __imp_WSAGetLastError. The shim
reaches the host's Winsock through host_proc everywhere else; wsa_last_error does the same, mirroring
set_wsa_error, with GetLastError as the fallback. gta6rt.exe imports the same DLLs as before #2533, no WS2_32.

Co-authored-by: Tyrannys <80750971+Tyrannys@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants