Skip to content

nstun: validate inner peer address in SOCKS5 UDP replies - #407

Open
alepacheco wants to merge 1 commit into
google:masterfrom
alepacheco:nstun-socks5-udp-inner-peer
Open

alepacheco wants to merge 1 commit into
google:masterfrom
alepacheco:nstun-socks5-udp-inner-peer

Conversation

@alepacheco

Copy link
Copy Markdown

Summary

Validate the inner peer address and port in RFC 1928 §7 SOCKS5 UDP reply headers before forwarding datagrams into an nstun guest flow.

Problem

For SOCKS5 UDP flows (flow->use_socks5), udp_peer_matches_flow() in nstun/udp_peer.h validates only the outer UDP packet source against the negotiated SOCKS5 relay (bnd_ip:bnd_port). In handle_host_udp() (nstun/udp.cc), the inner SOCKS5 UDP header (RSV, FRAG, ATYP, DST.ADDR, DST.PORT) was stripped without checking whether the inner address family, IP address, or port matched the flow's target (flow->is_ipv6, flow->orig_dest_ip4/orig_dest_ip6, flow->orig_dest_port), and SOCKS5_ATYP_DOMAIN headers were accepted on numeric IP flows.

Because udp_push_to_guest() rewrites the packet source delivered to the guest to flow->orig_dest_ip{4,6}:flow->orig_dest_port, any datagram forwarded by the SOCKS5 relay from an unrelated remote peer on the associated UDP port (associated with 0.0.0.0:0 by send_socks5_udp_associate()) was presented to the guest as if it had originated from orig_dest, enabling off-path UDP/DNS reply injection.

Fix

  • Add socks5_udp_unwrap_for_flow() in nstun/udp_peer.h (using alignment-safe memcpy) to reject truncated datagrams, non-zero rsv/frag, SOCKS5_ATYP_DOMAIN, cross-family headers, and any inner IP or port that does not match the flow's orig_dest.
  • Call socks5_udp_unwrap_for_flow() from handle_host_udp() in nstun/udp.cc and drop mismatched datagrams with a warning log.
  • Extend tests/nstun_udp_peer_test.cc (and its Makefile header dependencies) with IPv4 and IPv6 unit tests covering matching payloads, empty payloads, mismatched IP/port, non-zero rsv/frag, truncated headers, SOCKS5_ATYP_DOMAIN, and cross-family mismatches.

Validation

  • make -j2: PASS
  • tests/nstun_udp_peer_test (default -O2 and -fsanitize=address,undefined): PASS
  • tests/nstun_buffer_budget_test, tests/nstun_policy_test, tests/nstun_ip_test: PASS
  • make test-cmdline: PASS
  • Loopback handle_host_udp() test confirming mismatched inner peers from the relay are dropped while matching inner peers are delivered: PASS
  • git diff --check: PASS

For SOCKS5 UDP flows, udp_peer_matches_flow() only validates the outer
SOCKS5 relay address (bnd_ip:bnd_port), while handle_host_udp() stripped
the RFC 1928 §7 header without checking the inner ATYP, IP address, or
port against the flow's orig_dest before udp_push_to_guest() rewrote the
source to orig_dest.

Add socks5_udp_unwrap_for_flow() in nstun/udp_peer.h to validate rsv,
frag, address family, IP, and port (rejecting SOCKS5_ATYP_DOMAIN and
cross-family headers) using alignment-safe memcpy, call it from
handle_host_udp(), and add IPv4/IPv6 unit tests in
tests/nstun_udp_peer_test.cc.

This branch has not been deployed

No deployments
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.

1 participant