Commit Graph

5 Commits

Author SHA1 Message Date
37163d100f ws2_offline: the host reports an extension lookup's completion; AI_NUMERICHOST keeps localhost a name
From timbot103's review of #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>
2026-11-27 20:07:18 -06:00
c3cf8a042d ws2_offline: refuse the local computer's name, answer localhost with loopback literals, keep WSAIoctl synchronous
From kevincommits' finding (batch 18, item 3) in #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>
2026-11-27 19:10:03 -06:00
1f88b25d9a ws2_offline: loopback-bind unbound sends, refuse multicast joins and WSAJoinLeaf groups
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 <noreply@anthropic.com>
2026-11-24 09:19:38 -03:00
d7cdfcf255 ws2_offline: per-socket extension pointers, refuse name-service lookups, close connect-by-name and octal bypasses, cover wsock32
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 <noreply@anthropic.com>
2026-11-24 02:42:07 -03:00
9b59db02f9 ws2_offline: add offline loopback-only Winsock2 (ws2_32.dll) shim (RT-WS2-OFFLINE) 2026-11-24 00:53:14 -03:00