Net: a datagram socket that sends before it is bound is bound to loop… #2831
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2833"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by miatson on 2026-11-25T21:41:13Z.
Problem
net.rsnarrows a bind to all interfaces to loopback, "so nothing on the network canconnect in". But a datagram socket that sends before it is bound is bound by Winsock
itself, implicitly, to every interface:
sendto/WSASendTo/WSASendMsgto 127.0.0.1on an unbound UDP socket leaves it bound to
0.0.0.0:<ephemeral>([::]for IPv6).bindis never called, so nothing narrows it, and other machines on the network cansend datagrams to the title's socket.
Reproduced on
main(13f91ca) with the tests below:getsocknamereports0.0.0.0:<port>(IPv4) and[::]:<port>(IPv6);Change
Before a vetted datagram send,
bound_to_loopbackchecks the socket withgetsockname.If it isn't bound yet (
WSAEINVAL), it binds it to loopback on port 0 first, matchingthe destination:
127.0.0.1,[::1], or[::ffff:127.0.0.1]for a dual-stack socketsending to 127.0.0.1. Sockets already bound, and handles that aren't sockets, are left
to the host as before. If the bind fails, the send is refused with the bind's error
rather than letting the host bind to every interface.
sendto,WSASendToandWSASendMsg(including the pointerWSAIoctlhands out) useit through
datagram_allowed. Stream sockets and sends to a connected peer are unchanged.The module doc now mentions the implicit bind.
Tests
a_datagram_socket_sending_unbound_is_bound_to_loopback: IPv4 and IPv6sendto,and
WSASendMsg, each leave the socket bound to loopback.the_network_cannot_send_to_a_title_datagram_socket: end to end. After a loopbacksend, a datagram to the machine's LAN address at the socket's port does not arrive
(skipped without a network).
Both failed on
main(bound to 0.0.0.0:…,bound to [::]:…, and the datagram arrived)and pass with the change.
cargo clippy --workspace --all-targets [--all-features] --locked -- -D warningsis clean, and
cargo test --workspace --lockedpasses (781 passed, 0 failed).Tested on
mainat13f91caNotes
[::1]implicitly through its first send and later sends to::ffff:127.0.0.1on the same socket would now be refused. Before, the wildcardbind allowed both. Binding both families explicitly is unaffected.
Written with the help of Claude Code.
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T21:58:02Z.
Review: COMMENTED
Consistent with the existing loopback-only sandbox: an unbound datagram socket gets host-bound to all interfaces on first send, so binding it to loopback:0 first preserves the 'nothing on the network can reach it' invariant. sockaddr layout is right (family LE @[0..2], port BE @[2..4], v4 @[4..8] len 16, v6 @[8..24] len 28), IPv4-mapped v6 handled, and the getsockname/WSAEINVAL probe only fires the bind for genuinely unbound sockets. LGTM.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:17:41Z.
Review: COMMENTED
Verified head
2bf910afcleanly merged ontoorigin/main(63bcca43) on Windows 11 x64.Code & Isolation Review
sendto,WSASendTo, andWSASendMsgthroughdatagram_allowedchecksreachablefirst (WSAENETUNREACHon non-loopback targets) and then checksgetsockname; only when unbound (WSAEINVAL) does it bind via hostws2_32.dll!bindto the matching ephemeral loopback address (127.0.0.1:0,[::1]:0, or[::ffff:127.0.0.1]:0), preventing implicit wildcard interface binding.WSAEINVALerrors (e.g. invalid socket handle) pass through unaltered, andbindfailure preservesWSAGetLastError().Local Verification
cargo test --locked -p victory-hle --lib -- modules::net: 4/4 passed (includinga_datagram_socket_sending_unbound_is_bound_to_loopbackandthe_network_cannot_send_to_a_title_datagram_socket).cargo clippy --locked -p victory-hle --tests -- -D warnings(default and--all-features) &cargo xtask policy(191 files ok): clean.Looks clean and ready to merge.
Imported from Marvisak/VIctory. Originally posted by Kajanos on 2026-11-26T00:07:33Z.
Windows 11 / NVIDIA test run, head
2bf910af1fmerged ontomain63bcca43c6.Setup: Windows 11 Home build 26300, RTX 5080, NVIDIA 621.40, rustc 1.98.1 MSVC, VS 2022 Build Tools, Windows SDK 10.0.26100 (
dxil.dllfrom the SDK).cargo test --locked -p victory-hle: 153 passed, 1 failed.lifecycle::tests::showing_a_title_window_shows_its_stand_in, and it also fails onmainin this run. The tests ran over SSH, i.e. in session 0 with no interactive desktop, so the window can't be shown. On the desktop session the same test passes onmain(12/12lifecycle). This PR doesn't touch lifecycle.Not run: the retail title, clippy, and other crates.
AI-assisted (Claude Code)
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T03:24:22Z.
Automated triage for the maintainers.
Verdict: merge candidate. This tightens the offline guard in
crates/victory-hle/src/modules/net.rs; it does not loosen it. A UDP socket that sends before anybindgets an implicit wildcard bind from Winsock, which the existingbindhook never sees. The newbound_to_loopbackruns insidedatagram_allowedforsendto,WSASendToandWSASendMsg. It usesgetsockname, and only onWSAEINVAL(not bound) binds the socket to127.0.0.1,[::1]or[::ffff:127.0.0.1]to match the destination. If that bind fails, the send fails with the bind's error. Sockets that are already bound, stream sockets and sends to a connected peer are unchanged. Checked:AF_INET/AF_INET6areu16, so thesockaddrhelper's byte layout is right, andreachablestill runs first.The tests create a socket in the test process and, in the end-to-end one, send a datagram to the machine's own LAN address; nothing leaves the host. The trade-off in the PR notes (an implicit
[::1]bind later sending to a v4-mapped address is refused) seems acceptable.CI: not run on this head yet (awaiting workflow approval).
Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T17:54:10Z.
Review: APPROVED
Reviewed the diff and verified locally on Windows 11 x64:
sendto/WSASendTocleanly prevents Winsock from opening all-interface wildcard bindings.cargo test -p victory-hle --lib modules::net:: -- --test-threads=1: 4 passed, 0 failed (includinga_datagram_socket_sending_unbound_is_bound_to_loopback).cargo clippy -p victory-hle --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.