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

Open
Tyrannys wants to merge 3 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-ws2-offline-lookup-leaks
Open

Tyrannys wants to merge 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.

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.

3 participants