8000 nstun: route packet and proxy parsing through bounds-checked ByteReader by phenomenon0 · Pull Request #417 · google/nsjail · GitHub
[go: up one dir, main page]

Skip to content

nstun: route packet and proxy parsing through bounds-checked ByteReader - #417

Open
phenomenon0 wants to merge 1 commit into
google:masterfrom
phenomenon0:nstun-bounds-checked-reader
Open

phenomenon0 wants to merge 1 commit into
google:masterfrom
phenomenon0:nstun-bounds-checked-reader

Conversation

@phenomenon0
Copy link
Copy Markdown

Summary

nstun parses untrusted guest packets and proxy replies across tun.cc, ip.cc, udp.cc, icmp.cc, tcp.cc, encap.cc, and udp_peer.h using separate (pointer, length) variables and direct reinterpret_cast header reads. While auditing those paths we also found two concrete parsing defects:

  • handle_udp4() and handle_udp6() in nstun/udp.cc did not validate the UDP header's len field (udp->len), accepting < 8 / oversized lengths and including trailing IP padding in UDP checksum verification and forwarded payloads.
  • handle_socks5_connecting_host() in nstun/tcp.cc cast a 4-byte proxy_rx_buffer to const socks5_req* (10 bytes) and a 5-byte buffer to const socks5_req_domain* (7 bytes) while inspecting atyp / domain_len.

This PR addresses #309 (part 1: cursor/parser) and #360 while keeping nstun/tcp.cc edits tightly scoped to header/framing reads to avoid conflicts with open TCP lifecycle/buffering PRs:

  1. Adds nstun/byte_reader.h (nstun::ByteReader): Header-only, -fno-exceptions bounds-checked cursor over std::span<const uint8_t> that binds remaining pointer and length together, rejects out-of-bounds/overflow reads and skips, and copies wire structs via memcpy() to eliminate unaligned reinterpret_cast reads.
  2. Routes packet and proxy parsing across nstun through ByteReader:
    • nstun/tun.cc: handle_tun_frame()
    • nstun/ip.cc: skip_ipv6_ext_headers() (replacing raw const uint8_t*& ptr, size_t& rem pointer arithmetic), handle_ip4(), and handle_ip6() (preserving the validated contiguous IP header pointer required by send_icmp{4,6}_error())
    • nstun/udp.cc: handle_udp4() and handle_udp6() — validates udp_len >= sizeof(udp_hdr) and udp_len <= available L4 bytes and truncates trailing IP padding before checksum verification and forwarding
    • nstun/icmp.cc: handle_icmp4(), handle_icmp6(), and handle_host_icmp()
    • nstun/tcp.cc: handle_tcp4(), handle_tcp6(), tcp_process_data() (taking already-sliced payload span instead of raw payload.data() + doff), and handle_socks5_connecting_host() (eliminating the oversized socks5_req*/socks5_req_domain* casts)
    • nstun/encap.cc: parse_socks5_auth_reply(), parse_socks5_connect_reply(), and parse_http_connect_reply()
    • nstun/udp_peer.h: socks5_udp_unwrap_for_flow()
  3. Adds tests/nstun_byte_reader_test.cc (wired into Makefile): Covers ByteReader primitives, 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: PASS
  • nstun_ip_test: PASS
  • nstun_encap_test: PASS
  • nstun_udp_peer_test: PASS
  • nstun_buffer_budget_test: PASS
  • nstun_flow_limit_test: PASS
  • Standalone TCP handle_tcp4/handle_tcp6 boundary/doff/checksum harness: PASS (0 warnings)

@google-cla
google-cla Bot commented Dec 5, 2026
Copy link
Copy Markdown

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.

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.

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.

2 participants

0