Repository navigation
nstun: route packet and proxy parsing through bounds-checked ByteReader - #417
Open
phenomenon0 wants to merge 1 commit into
Open
phenomenon0 wants to merge 1 commit into
phenomenon0 wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
phenomenon0
force-pushed
the
nstun-bounds-checked-reader
branch
from
December 5, 2026 06:53
30663ef to
66e202c
Compare
Introduce a small header-only nstun::ByteReader cursor over std::span<const uint8_t> that keeps remaining pointer and length bound together and copies wire structs via memcpy() rather than raw pointer casts. Route untrusted guest and proxy reply parsing across nstun through ByteReader: - Replace raw (ptr, rem) arithmetic in skip_ipv6_ext_headers() with ByteReader. - Replace scattered length checks and reinterpret_cast header reads in handle_tun_frame, handle_ip4/6, handle_udp4/6, handle_icmp4/6, handle_host_icmp, handle_tcp4/6, tcp_process_data, parse_socks5_*_reply, parse_http_connect_reply, and socks5_udp_unwrap_for_flow. - Fix oversized-struct reinterpret_cast in handle_socks5_connecting_host() (which cast a 4-byte buffer to const socks5_req* / 5-byte buffer to const socks5_req_domain*) by reading ATYP and domain length through ByteReader. - Validate UDP header length (udp->len >= sizeof(udp_hdr) and <= available L4 bytes) in handle_udp4() and handle_udp6() and truncate trailing IP padding before checksum validation and forwarding. - Add tests/nstun_byte_reader_test.cc covering ByteReader primitives and malformed/truncated IPv4, IPv6 extension-header, UDP, and ICMP frames. Refs google#309, google#360.
phenomenon0
force-pushed
the
nstun-bounds-checked-reader
branch
from
December 5, 2026 19:24
66e202c to
1f94568
Compare<
7BD0
/span>
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
nstunparses untrusted guest packets and proxy replies acrosstun.cc,ip.cc,udp.cc,icmp.cc,tcp.cc,encap.cc, andudp_peer.husing separate(pointer, length)variables and directreinterpret_castheader reads. While auditing those paths we also found two concrete parsing defects:handle_udp4()andhandle_udp6()innstun/udp.ccdid not validate the UDP header'slenfield (udp->len), accepting< 8/ oversized lengths and including trailing IP padding in UDP checksum verification and forwarded payloads.handle_socks5_connecting_host()innstun/tcp.cccast a 4-byteproxy_rx_buffertoconst socks5_req*(10 bytes) and a 5-byte buffer toconst socks5_req_domain*(7 bytes) while inspectingatyp/domain_len.This PR addresses #309 (part 1: cursor/parser) and #360 while keeping
nstun/tcp.ccedits tightly scoped to header/framing reads to avoid conflicts with open TCP lifecycle/buffering PRs:nstun/byte_reader.h(nstun::ByteReader): Header-only,-fno-exceptionsbounds-checked cursor overstd::span<const uint8_t>that binds remaining pointer and length together, rejects out-of-bounds/overflow reads and skips, and copies wire structs viamemcpy()to eliminate unalignedreinterpret_castreads.nstunthroughByteReader:nstun/tun.cc:handle_tun_frame()nstun/ip.cc:skip_ipv6_ext_headers()(replacing rawconst uint8_t*& ptr, size_t& rempointer arithmetic),handle_ip4(), andhandle_ip6()(preserving the validated contiguous IP header pointer required bysend_icmp{4,6}_error())nstun/udp.cc:handle_udp4()andhandle_udp6()— validatesudp_len >= sizeof(udp_hdr)andudp_len <= available L4 bytesand truncates trailing IP padding before checksum verification and forwardingnstun/icmp.cc:handle_icmp4(),handle_icmp6(), andhandle_host_icmp()nstun/tcp.cc:handle_tcp4(),handle_tcp6(),tcp_process_data()(taking already-sliced payload span instead of rawpayload.data() + doff), andhandle_socks5_connecting_host()(eliminating the oversizedsocks5_req*/socks5_req_domain*casts)nstun/encap.cc:parse_socks5_auth_reply(),parse_socks5_connect_reply(), andparse_http_connect_reply()nstun/udp_peer.h:socks5_udp_unwrap_for_flow()tests/nstun_byte_reader_test.cc(wired intoMakefile): CoversByteReaderprimitives, null/overflow edge cases, and end-to-end malformed/truncated IPv4, IPv6 extension-header chain, UDP length/padding, and ICMP/ICMPv6 frames.Testing
Compiled and ran with GCC 15.2.1 (
-std=c++20 -O2 -g -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -fPIE -fno-exceptions -D_GLIBCXX_ASSERTIONS -D_FORTIFY_SOURCE=3 -fstack-protector-strong -Wformat -Wformat-security -Wall -Wextra -Werror):nstun_byte_reader_test: PASSnstun_ip_test: PASSnstun_encap_test: PASSnstun_udp_peer_test: PASSnstun_buffer_budget_test: PASSnstun_flow_limit_test: PASShandle_tcp4/handle_tcp6boundary/doff/checksum harness: PASS (0 warnings)