Net: a datagram socket that sends before it is bound is bound to loop… #2831

Open
miatson wants to merge 1 commit from upstream-pr/2833 into main
miatson commented 2026-11-25 21:41:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by miatson on 2026-11-25T21:41:13Z.


Problem

net.rs narrows a bind to all interfaces to loopback, "so nothing on the network can
connect in". But a datagram socket that sends before it is bound is bound by Winsock
itself, implicitly, to every interface: sendto/WSASendTo/WSASendMsg to 127.0.0.1
on an unbound UDP socket leaves it bound to 0.0.0.0:<ephemeral> ([::] for IPv6).
bind is never called, so nothing narrows it, and other machines on the network can
send datagrams to the title's socket.

Reproduced on main (13f91ca) with the tests below:

  • after one loopback send, getsockname reports 0.0.0.0:<port> (IPv4) and [::]:<port> (IPv6);
  • a datagram sent to the machine's own LAN address at that port reached the title's socket.

Change

Before a vetted datagram send, bound_to_loopback checks the socket with getsockname.
If it isn't bound yet (WSAEINVAL), it binds it to loopback on port 0 first, matching
the destination: 127.0.0.1, [::1], or [::ffff:127.0.0.1] for a dual-stack socket
sending 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, WSASendTo and WSASendMsg (including the pointer WSAIoctl hands out) use
it 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 IPv6 sendto,
    and WSASendMsg, each leave the socket bound to loopback.
  • the_network_cannot_send_to_a_title_datagram_socket: end to end. After a loopback
    send, 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 warnings
is clean, and cargo test --workspace --locked passes (781 passed, 0 failed).

Tested on

  • Windows 11 Pro 26H2 (build 26300)
  • AMD Radeon RX 7900 XT, driver 32.0.32015.7029
  • AMD Ryzen 7 9700X, 32 GB RAM
  • rustc 1.98.1 (the pinned toolchain)
  • Based on main at 13f91ca

Notes

  • A title that binds [::1] implicitly through its first send and later sends to
    ::ffff:127.0.0.1 on the same socket would now be refused. Before, the wildcard
    bind allowed both. Binding both families explicitly is unaffected.
  • Not tested in game (no game data); only the synthetic tests above.

Written with the help of Claude Code.


<!-- greyfinch-sync:Marvisak/VIctory:issue:6614314611 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2833). Originally posted by [miatson](https://github.com/miatson) on 2026-11-25T21:41:13Z. --- ## Problem `net.rs` narrows a bind to all interfaces to loopback, "so nothing on the network can connect in". But a datagram socket that sends before it is bound is bound by Winsock itself, implicitly, to every interface: `sendto`/`WSASendTo`/`WSASendMsg` to 127.0.0.1 on an unbound UDP socket leaves it bound to `0.0.0.0:<ephemeral>` (`[::]` for IPv6). `bind` is never called, so nothing narrows it, and other machines on the network can send datagrams to the title's socket. Reproduced on `main` (13f91ca) with the tests below: - after one loopback send, `getsockname` reports `0.0.0.0:<port>` (IPv4) and `[::]:<port>` (IPv6); - a datagram sent to the machine's own LAN address at that port reached the title's socket. ## Change Before a vetted datagram send, `bound_to_loopback` checks the socket with `getsockname`. If it isn't bound yet (`WSAEINVAL`), it binds it to loopback on port 0 first, matching the destination: `127.0.0.1`, `[::1]`, or `[::ffff:127.0.0.1]` for a dual-stack socket sending 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`, `WSASendTo` and `WSASendMsg` (including the pointer `WSAIoctl` hands out) use it 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 IPv6 `sendto`, and `WSASendMsg`, each leave the socket bound to loopback. - `the_network_cannot_send_to_a_title_datagram_socket`: end to end. After a loopback send, 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 warnings` is clean, and `cargo test --workspace --locked` passes (781 passed, 0 failed). ## Tested on - Windows 11 Pro 26H2 (build 26300) - AMD Radeon RX 7900 XT, driver 32.0.32015.7029 - AMD Ryzen 7 9700X, 32 GB RAM - rustc 1.98.1 (the pinned toolchain) - Based on `main` at 13f91ca ## Notes - A title that binds `[::1]` implicitly through its first send and later sends to `::ffff:127.0.0.1` on the same socket would now be refused. Before, the wildcard bind allowed both. Binding both families explicitly is unaffected. - Not tested in game (no game data); only the synthetic tests above. Written with the help of Claude Code. ---
kotekompotek commented 2026-11-25 21:58:02 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6065553036 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2833#pullrequestreview-6065553036). Originally posted by [kotekompotek](https://github.com/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.
fuzeofficlal commented 2026-11-26 00:17:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:17:41Z.

Review: COMMENTED


Verified head 2bf910af cleanly merged onto origin/main (63bcca43) on Windows 11 x64.

Code & Isolation Review

  • Routing sendto, WSASendTo, and WSASendMsg through datagram_allowed checks reachable first (WSAENETUNREACH on non-loopback targets) and then checks getsockname; only when unbound (WSAEINVAL) does it bind via host ws2_32.dll!bind to the matching ephemeral loopback address (127.0.0.1:0, [::1]:0, or [::ffff:127.0.0.1]:0), preventing implicit wildcard interface binding.
  • Non-WSAEINVAL errors (e.g. invalid socket handle) pass through unaltered, and bind failure preserves WSAGetLastError().

Local Verification

  • cargo test --locked -p victory-hle --lib -- modules::net: 4/4 passed (including a_datagram_socket_sending_unbound_is_bound_to_loopback and the_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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066835414 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2833#pullrequestreview-6066835414). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T00:17:41Z. **Review:** COMMENTED --- Verified head `2bf910af` cleanly merged onto `origin/main` (`63bcca43`) on Windows 11 x64. ### Code & Isolation Review - Routing `sendto`, `WSASendTo`, and `WSASendMsg` through `datagram_allowed` checks `reachable` first (`WSAENETUNREACH` on non-loopback targets) and then checks `getsockname`; only when unbound (`WSAEINVAL`) does it bind via host `ws2_32.dll!bind` to the matching ephemeral loopback address (`127.0.0.1:0`, `[::1]:0`, or `[::ffff:127.0.0.1]:0`), preventing implicit wildcard interface binding. - Non-`WSAEINVAL` errors (e.g. invalid socket handle) pass through unaltered, and `bind` failure preserves `WSAGetLastError()`. ### Local Verification - `cargo test --locked -p victory-hle --lib -- modules::net`: **4/4 passed** (including `a_datagram_socket_sending_unbound_is_bound_to_loopback` and `the_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.
Kajanos commented 2026-11-26 01:03:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Kajanos on 2026-11-26T00:07:33Z.


Windows 11 / NVIDIA test run, head 2bf910af1f merged onto main 63bcca43c6.

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.dll from the SDK).

  • cargo test --locked -p victory-hle: 153 passed, 1 failed.
  • The one failure is lifecycle::tests::showing_a_title_window_shows_its_stand_in, and it also fails on main in 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 on main (12/12 lifecycle). This PR doesn't touch lifecycle.

Not run: the retail title, clippy, and other crates.

AI-assisted (Claude Code)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7178421439 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2833#issuecomment-7178421439). Originally posted by [Kajanos](https://github.com/Kajanos) on 2026-11-26T00:07:33Z. --- **Windows 11 / NVIDIA test run**, head `2bf910af1f` merged onto `main` `63bcca43c6`. 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.dll` from the SDK). - `cargo test --locked -p victory-hle`: 153 passed, 1 failed. - The one failure is `lifecycle::tests::showing_a_title_window_shows_its_stand_in`, and it also fails on `main` in 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 on `main` (12/12 `lifecycle`). This PR doesn't touch lifecycle. Not run: the retail title, clippy, and other crates. AI-assisted (Claude Code)
Marvisak commented 2026-11-26 04:06:18 +00:00 (Migrated from github.com)

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 any bind gets an implicit wildcard bind from Winsock, which the existing bind hook never sees. The new bound_to_loopback runs inside datagram_allowed for sendto, WSASendTo and WSASendMsg. It uses getsockname, and only on WSAEINVAL (not bound) binds the socket to 127.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_INET6 are u16, so the sockaddr helper's byte layout is right, and reachable still 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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7181690200 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2833#issuecomment-7181690200). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T03:24:22Z. --- <!-- victory-triage v1 head=2bf910af1f3126cbde4921cb4c8f05beac626513 verdict=merge-candidate at=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 any `bind` gets an implicit wildcard bind from Winsock, which the existing `bind` hook never sees. The new `bound_to_loopback` runs inside `datagram_allowed` for `sendto`, `WSASendTo` and `WSASendMsg`. It uses `getsockname`, and only on `WSAEINVAL` (not bound) binds the socket to `127.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_INET6` are `u16`, so the `sockaddr` helper's byte layout is right, and `reachable` still 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](https://claude.ai/code)_
Dukestar69 commented 2026-11-26 17:54:10 +00:00 (Migrated from github.com)

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:

  • Implicitly binding unbound datagram sockets to loopback (IPv4, IPv6, or IPv4-mapped IPv6 for dual-stack) before sendto / WSASendTo cleanly prevents Winsock from opening all-interface wildcard bindings.
  • cargo test -p victory-hle --lib modules::net:: -- --test-threads=1: 4 passed, 0 failed (including a_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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6078657385 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2833#pullrequestreview-6078657385). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T17:54:10Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64: - Implicitly binding unbound datagram sockets to loopback (IPv4, IPv6, or IPv4-mapped IPv6 for dual-stack) before `sendto` / `WSASendTo` cleanly prevents Winsock from opening all-interface wildcard bindings. - `cargo test -p victory-hle --lib modules::net:: -- --test-threads=1`: 4 passed, 0 failed (including `a_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).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2833:upstream-pr/2833'
git switch 'upstream-pr/2833'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/2833'
git switch 'upstream-pr/2833'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2833'
git switch 'upstream-pr/2833'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2833'
git switch 'main'
git merge --squash 'upstream-pr/2833'
git switch 'main'
git merge --ff-only 'upstream-pr/2833'
git switch 'main'
git merge 'upstream-pr/2833'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
Marvisak/VIctory!2831
No description provided.