41 Commits

Author SHA1 Message Date
Robert Swiecki
44e08fd4e5 make indent depend 2026-08-26 21:39:07 +02:00
Robert Swiecki
5d53b5c28b Merge pull request #282 from carrerasdarren-cell/nstun-cloud-local-destinations
nstun: block cloud-local service destinations
2026-08-26 20:08:34 +02:00
Robert Swiecki
79d12b54f2 Merge pull request #289 from carrerasdarren-cell/fix/nstun-policy-validation
nstun: reject invalid policy rule fields
2026-08-26 11:48:27 +02:00
Robert Swiecki
1a923df739 nstun: drop IPv6 unspecified and multicast packets in SSRF gate 2026-08-26 11:45:35 +02:00
robertswiecki
2ed350a0f2 Merge pull request #302 from mustafagozhamam/fix/nstun-parent-lifecycle
Fix NSTUN parent lifecycle cleanup
2026-08-26 09:23:13 +02:00
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
62f41cbfb7 Merge pull request #306 from Trithem90/fix/nstun-any-proxy-icmp
nstun: fail closed for proxy actions matched by ICMP
2026-08-25 21:19:00 +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
robertswiecki
c39f43ddcb Merge pull request #304 from Trithem90/fix/nstun-zero-network-cidr
nstun: preserve zero-network CIDR selectors
2026-08-25 21:14:35 +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
8a751f0166 nstun: fail closed for proxy actions matched by ICMP 2026-08-21 13:57:16 +02:00
Trithem90
6673fe6758 nstun: preserve zero-network CIDR selectors 2026-08-21 12:15:24 +02:00
Trithem90
70094631bb nstun: enforce HOST_TO_GUEST policy on inbound TCP and UDP 2026-08-21 11:01:22 +02:00
Mustafa
9f166b999f Fix NSTUN parent lifecycle cleanup 2026-08-18 21:31:15 +03:00
Darren Carreras
0d905f1da2 nstun: reject invalid policy rule fields 2026-07-21 19:59:04 -04:00
Darren Carreras
bd9439b3f2 nstun: block cloud-local service destinations 2026-07-16 18:42:09 -04: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
6356dee0c6 Makefile/all: make libnl3 optional 2026-04-14 14:26:28 +02:00
Robert Swiecki
16099aebd0 nstun: handle setsockopt failures 2026-04-03 11:28:21 +02:00
Robert Swiecki
3c92a17d2b nstun: set IPV6_V6ONLY on IPv6 so it doesn't collide with IPv4 2026-04-03 02:01:11 +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
2ee9590ba6 nstun: use redirect_port correctly for REDIRECT's 2026-03-30 09:39:04 +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
dd3b8cdd27 nstun: ntohl is not always constexpr 2026-03-29 14:10:53 +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
95617dd234 config+net: Refactored config.proto to make nstun the default user_net backend. 2026-03-29 13:59:40 +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