Repository navigation
nstun: validate inner peer address in SOCKS5 UDP replies - #407
Open
alepacheco wants to merge 1 commit into
Open
alepacheco wants to merge 1 commit into
alepacheco wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Validate the inner peer address and port in RFC 1928 §7 SOCKS5 UDP reply headers before forwarding datagrams into an
nstunguest flow.Problem
For SOCKS5 UDP flows (
flow->use_socks5),udp_peer_matches_flow()innstun/udp_peer.hvalidates only the outer UDP packet source against the negotiated SOCKS5 relay (bnd_ip:bnd_port). Inhandle_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), andSOCKS5_ATYP_DOMAINheaders were accepted on numeric IP flows.Because
udp_push_to_guest()rewrites the packet source delivered to the guest toflow->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 with0.0.0.0:0bysend_socks5_udp_associate()) was presented to the guest as if it had originated fromorig_dest, enabling off-path UDP/DNS reply injection.Fix
socks5_udp_unwrap_for_flow()innstun/udp_peer.h(using alignment-safememcpy) to reject truncated datagrams, non-zerorsv/frag,SOCKS5_ATYP_DOMAIN, cross-family headers, and any inner IP or port that does not match the flow'sorig_dest.socks5_udp_unwrap_for_flow()fromhandle_host_udp()innstun/udp.ccand drop mismatched datagrams with a warning log.tests/nstun_udp_peer_test.cc(and itsMakefileheader dependencies) with IPv4 and IPv6 unit tests covering matching payloads, empty payloads, mismatched IP/port, non-zerorsv/frag, truncated headers,SOCKS5_ATYP_DOMAIN, and cross-family mismatches.Validation
make -j2: PASStests/nstun_udp_peer_test(default-O2and-fsanitize=address,undefined): PASStests/nstun_buffer_budget_test,tests/nstun_policy_test,tests/nstun_ip_test: PASSmake test-cmdline: PASShandle_host_udp()test confirming mismatched inner peers from the relay are dropped while matching inner peers are delivered: PASSgit diff --check: PASS