24 Commits

Author SHA1 Message Date
robertswiecki
c398ceda2f Merge pull request #300 from skaiea13-ai/codex/nstun-aggregate-budget
Limit nstun TCP receive buffers with a shared payload budget
2026-08-25 21:20:12 +02:00
robertswiecki
3bd39a9acf Merge pull request #303 from Trithem90/fix/nstun-host-to-guest-policy
nstun: enforce HOST_TO_GUEST policy on inbound TCP and UDP
2026-08-25 21:17:38 +02:00
skaiea13-ai
b4cff719c2 Limit nstun TCP receive buffers with a shared payload budget
Cap retained guest-to-host TCP payload across flows so a stalled sink cannot grow the supervisor toward the per-flow limit times the maximum flow count.
2026-08-22 04:30:29 +09:00
Trithem90
70094631bb nstun: enforce HOST_TO_GUEST policy on inbound TCP and UDP 2026-08-21 11:01:22 +02:00
g0w6y
a460c1b6a0 nstun/tcp: validate guest ACK numbers against the send window
The guest is untrusted and fully controls the acknowledgement number of
every TCP segment it emits. tcp_process_data() accepted any forward ACK
(acked_bytes > 0) without checking that it stayed within the send window,
so the guest could acknowledge data that was never sent.

Each such ACK advances tx_acked_offset by up to 2^31-1. When tx_buffer is
empty the erase step below is a no-op, so tx_acked_offset accumulates and
can be driven past 2^32 with a few pure ACK segments. push_to_guest() then
computes:

    int32_t available = tx_buffer.size() - tx_acked_offset;
    const uint8_t* data = tx_buffer.data() + tx_acked_offset + in_flight;

available is an int32_t truncation of a size_t subtraction, so an oversized
tx_acked_offset wraps it back positive and defeats the in_flight >= available
guard. data then points at least 2 GB past the heap buffer and
tcp_send_packet() reads up to NSTUN_MTU bytes from it.

The nstun network loop runs in the nsjail parent (supervisor) process, and
SIGSEGV is not handled, so the out of bounds read lets an untrusted jailed
process crash its own supervisor. The minimum out of bounds distance is 2 GB,
so under ASLR the access reliably faults rather than returning data.

Add the RFC 793 receive check (SEG.ACK <= SND.NXT): reject ACKs whose
sequence is beyond seq_to_guest. This keeps tx_acked_offset within
tx_buffer.size(), which the framing in push_to_guest() relies on.
2026-07-15 20:17:22 +05:30
Robert Swiecki
16099aebd0 nstun: handle setsockopt failures 2026-04-03 11:28:21 +02:00
Robert Swiecki
86530d8068 nstun/tcp: set flow_success=true on accept paths so the defer guard doesn't immediately destroy every inbound connection 2026-04-03 01:47:02 +02:00
Robert Swiecki
508d9bf3b5 Refactor nstun TCP/UDP state machines and harden IPv6 handling
* Replace switch/goto dispatch with table-driven per-state handlers; deduplicate flow init
 * Eliminate magic numbers - use named constants for buffer limits, timeouts, and struct sizes
 * Block IPv4-compatible IPv6 addresses (SSRF) and cache redirect destinations per-flow

Replace switch/goto dispatch with table-driven per-state handlers; deduplicate flow init
Eliminate magic numbers: use named constants for buffer limits, timeouts, and struct sizes
Block IPv4-compatible IPv6 addresses (SSRF) and cache redirect destinations per-flow
2026-04-02 14:03:31 +02:00
Robert Swiecki
68832ab865 nstun: remove dead code, and change some functions to static 2026-04-02 01:31:57 +02:00
Robert Swiecki
1984f83a45 nstun/tcp: remove dead mss/window code 2026-04-02 01:18:26 +02:00
Robert Swiecki
15f730cdba nstun: handle EPOLLHUP/EPOLLERR for TCP 2026-04-02 00:59:14 +02:00
Robert Swiecki
15e16bc93b nstun: make Flow a base clase, with derivative classes for each supported proto 2026-04-02 00:52:22 +02:00
Robert Swiecki
b6bf68c4c1 nstun: Harden networking stack and modernize to C++20
- Migrate TCP/UDP flow management to std::unique_ptr and packet parsing to std::span
- Add mandatory checksum validation for UDP (IPv4 optional, IPv6 per RFC 8200), closing parity with TCP/ICMP
- Handle IPv6 Authentication Header (AH) in extension header parser to prevent firewall rule bypass
- Add defense-in-depth MTU cap in tcp_process_data to prevent int32_t overflow in sequence arithmetic
- Fix uint16_t port loop overflow in HOST_TO_GUEST listener setup (infinite loop when dport_end=65535)
- Block SSRF via forged loopback/v4mapped destinations in both IPv4 and IPv6 TCP/UDP paths
- Extract policy evaluation and proxy encapsulation into standalone policy.cc and encap.cc modules
- Replace all raw inet_ntop+char[] patterns with ip4_to_string/ip6_to_string helpers
2026-04-01 19:38:13 +02:00
Robert Swiecki
0381754dfc nstun: add support for ENCAP_CONNECT - http connect encapsulation 2026-03-31 23:54:57 +02:00
Robert Swiecki
0f8b4a389e nstun: IPv6 implementation 2026-03-31 06:55:27 +02:00
Robert Swiecki
17836b71a3 nstun: use writev 2026-03-30 17:05:21 +02:00
Robert Swiecki
6b850698a5 nstun: use designated initializers with structs 2026-03-30 13:03:13 +02:00
Robert Swiecki
6c7788d62a nstun: add missing TCP state - TcpState::CLOSING 2026-03-30 11:17:16 +02:00
Robert Swiecki
53584b422e nstun: centralize src ip address checks to ip.cc 2026-03-30 09:25:37 +02:00
Robert Swiecki
033adf6128 nstun: faster downloads by draining upstream TCP sockets 2026-03-30 09:00:05 +02:00
Robert Swiecki
a2bd0b0ad1 nstun: Implement HOST_TO_GUEST forwarding 2026-03-30 07:12:52 +02:00
Robert Swiecki
8c570c865b nstun: increase MTU to 32kB, and stop clearing stack buffers where not needed 2026-03-29 14:07:47 +02:00
Robert Swiecki
254478dcf3 nstun: move checksum to a central file, use defer{} more liberally 2026-03-29 01:56:34 +01:00
Robert Swiecki
6276ae6be2 Add nstun, an experimental user-mode networking stack.
It provides lightweight IP-level connectivity for jailed processes via a TUN device and an epoll-based NAT proxy thread, as an alternative to pasta.

Supports TCP, UDP, and ICMP proxying with per-flow tracking, configurable firewall/redirect rules, SOCKS5 encapsulation for TCP and UDP.
2026-03-29 00:30:24 +01:00