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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ endif

BIN = nsjail
LIBS = kafel/libkafel.a
TEST_BINS = tests/util_msg_test tests/nstun_buffer_budget_test tests/nstun_flow_limit_test tests/nstun_policy_test tests/nstun_ip_test tests/nstun_udp_peer_test tests/nstun_encap_test tests/nstun_tcp_capacity_test tests/mount_source_anchor_test tests/mount_staging_unbindable_test tests/mnt_options_test tests/unotify_syscall_test tests/unotify_stats_test
TEST_BINS = tests/util_msg_test tests/nstun_buffer_budget_test tests/nstun_byte_reader_test tests/nstun_flow_limit_test tests/nstun_policy_test tests/nstun_ip_test tests/nstun_udp_peer_test tests/nstun_encap_test tests/nstun_tcp_capacity_test tests/mount_source_anchor_test tests/mount_staging_unbindable_test tests/mnt_options_test tests/unotify_syscall_test tests/unotify_stats_test

# If PASTA_BIN_PATH is not provided in env, dynamically search for it if EMBED_PASTA is requested
# or fallback to it naturally.
Expand Down Expand Up @@ -178,6 +178,7 @@ tests/mount_staging_unbindable_test: tests/mount_staging_unbindable_test.c
test: $(BIN) $(TEST_BINS) test-cmdline test-newapi-recursive-readonly test-mnt-dst-symlink test-mnt-prefix-env test-nstun-tcp-capacity
$(call run_test, ./tests/util_msg_test, 0)
$(call run_test, ./tests/nstun_buffer_budget_test, 0)
$(call run_test, ./tests/nstun_byte_reader_test, 0)
$(call run_test, ./tests/nstun_flow_limit_test, 0)
$(call run_test, ./tests/nstun_policy_test, 0)
$(call run_test, ./tests/nstun_ip_test, 0)
Expand Down Expand Up @@ -328,16 +329,19 @@ tests/nstun_tcp_capacity_test: tests/nstun_tcp_capacity_test.cc $(filter-out nsj
tests/nstun_buffer_budget_test: tests/nstun_buffer_budget_test.cc nstun/buffer_budget.h
$(CXX) $(filter-out -c,$(CXXFLAGS)) $< -o $@

tests/nstun_byte_reader_test: tests/nstun_byte_reader_test.cc nstun/byte_reader.h nstun/tun.cc nstun/ip.cc nstun/udp.cc nstun/icmp.cc nstun/encap.cc nstun/net_defs.h nstun/udp_peer.h logs.h
$(CXX) $(filter-out -c,$(CXXFLAGS)) tests/nstun_byte_reader_test.cc nstun/tun.cc nstun/ip.cc nstun/udp.cc nstun/icmp.cc nstun/encap.cc -o $@

tests/nstun_flow_limit_test: tests/nstun_flow_limit_test.cc nstun/flow_limit.h
$(CXX) $(filter-out -c,$(CXXFLAGS)) $< -o $@

tests/nstun_policy_test: tests/nstun_policy_test.cc nstun/policy.cc logs.cc util.cc $(SRCS_PB_CXX) $(SRCS_PB_H)
$(CXX) $(filter-out -c,$(CXXFLAGS)) tests/nstun_policy_test.cc nstun/policy.cc logs.cc util.cc $(SRCS_PB_CXX) -o $@ $(LDFLAGS)

tests/nstun_ip_test: tests/nstun_ip_test.cc nstun/ip.cc nstun/net_defs.h logs.h
tests/nstun_ip_test: tests/nstun_ip_test.cc nstun/ip.cc nstun/byte_reader.h nstun/net_defs.h logs.h
$(CXX) $(filter-out -c,$(CXXFLAGS)) tests/nstun_ip_test.cc nstun/ip.cc -o $@

tests/nstun_udp_peer_test: tests/nstun_udp_peer_test.cc nstun/udp_peer.h nstun/core.h nstun/encap.h
tests/nstun_udp_peer_test: tests/nstun_udp_peer_test.cc nstun/udp_peer.h nstun/byte_reader.h nstun/core.h nstun/encap.h
$(CXX) $(filter-out -c,$(CXXFLAGS)) tests/nstun_udp_peer_test.cc -o $@

tests/mnt_options_test: tests/mnt_options_test.cc mnt_options.h
Expand All @@ -349,7 +353,7 @@ tests/unotify_syscall_test: tests/unotify_syscall_test.cc unotify/syscall.cc uno
tests/unotify_stats_test: tests/unotify_stats_test.cc unotify/stats.cc unotify/stats.h unotify/record.h logs.cc util.cc $(SRCS_PB_CXX) $(SRCS_PB_H)
$(CXX) $(filter-out -c,$(CXXFLAGS)) tests/unotify_stats_test.cc unotify/stats.cc logs.cc util.cc $(SRCS_PB_CXX) -o $@ $(LDFLAGS)

tests/nstun_encap_test: tests/nstun_encap_test.cc nstun/encap.cc nstun/encap.h nstun/net_defs.h
tests/nstun_encap_test: tests/nstun_encap_test.cc nstun/encap.cc nstun/encap.h nstun/byte_reader.h nstun/net_defs.h
$(CXX) $(filter-out -c,$(CXXFLAGS)) tests/nstun_encap_test.cc nstun/encap.cc -o $@

# Dependencies (Generated by makedepend)
Expand Down Expand Up @@ -397,20 +401,20 @@ nstun/nstun.o: macros.h nstun/policy.h nstun/tcp.h nstun/tun.h nstun/udp.h
nstun/nstun.o: util.h nsjail.h config.pb.h
nstun/policy.o: nstun/policy.h nstun/core.h nstun/buffer_budget.h
nstun/policy.o: nstun/net_defs.h nstun/nstun.h logs.h config.pb.h nsjail.h
nstun/encap.o: nstun/encap.h nstun/net_defs.h logs.h
nstun/encap.o: nstun/encap.h nstun/byte_reader.h nstun/net_defs.h logs.h
nstun/iface.o: nstun/iface.h logs.h macros.h nstun/net_defs.h nsjail.h
nstun/iface.o: config.pb.h nstun/nstun.h
nstun/tun.o: nstun/tun.h nstun/core.h nstun/buffer_budget.h nstun/net_defs.h
nstun/tun.o: nstun/tun.h nstun/byte_reader.h nstun/core.h nstun/buffer_budget.h nstun/net_defs.h
nstun/tun.o: nstun/nstun.h nstun/icmp.h nstun/ip.h logs.h
nstun/ip.o: nstun/ip.h nstun/core.h nstun/buffer_budget.h nstun/net_defs.h
nstun/ip.o: nstun/ip.h nstun/byte_reader.h nstun/core.h nstun/buffer_budget.h nstun/net_defs.h
nstun/ip.o: nstun/nstun.h nstun/icmp.h logs.h nstun/tcp.h nstun/udp.h
nstun/icmp.o: nstun/icmp.h nstun/core.h nstun/buffer_budget.h
nstun/icmp.o: nstun/icmp.h nstun/byte_reader.h nstun/core.h nstun/buffer_budget.h
nstun/icmp.o: nstun/net_defs.h nstun/nstun.h logs.h macros.h nstun/policy.h
nstun/icmp.o: nstun/tun.h
nstun/udp.o: nstun/udp.h nstun/core.h nstun/buffer_budget.h nstun/net_defs.h
nstun/udp.o: nstun/udp.h nstun/byte_reader.h nstun/core.h nstun/buffer_budget.h nstun/net_defs.h
nstun/udp.o: nstun/nstun.h nstun/encap.h nstun/icmp.h logs.h macros.h
nstun/udp.o: nstun/policy.h nstun/tun.h
nstun/tcp.o: nstun/tcp.h nstun/core.h nstun/buffer_budget.h nstun/net_defs.h
nstun/udp.o: nstun/policy.h nstun/tun.h nstun/udp_peer.h
nstun/tcp.o: nstun/tcp.h nstun/byte_reader.h nstun/core.h nstun/buffer_budget.h nstun/net_defs.h
nstun/tcp.o: nstun/nstun.h nstun/encap.h logs.h macros.h nstun/policy.h
nstun/tcp.o: nstun/tun.h util.h nsjail.h config.pb.h
config.pb.o: config.pb.h
Expand Down
141 changes: 141 additions & 0 deletions nstun/byte_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#ifndef NSTUN_BYTE_READER_H_
#define NSTUN_BYTE_READER_H_

#include <arpa/inet.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include <span>
#include <type_traits>

namespace nstun {

/*
* Bounds-checked, alignment-safe cursor over a contiguous byte span.
*
* Keeps the remaining pointer and length bound together in a single object so
* packet parsers cannot drift out of sync or index past the end of untrusted
* guest/proxy buffers. All typed reads copy via memcpy() to avoid unaligned
* pointer dereferences and strict-aliasing violations.
*/
class ByteReader {
public:
constexpr explicit ByteReader(std::span<const uint8_t> data) : rem_(data) {}

constexpr size_t remaining() const {
return rem_.size();
}

constexpr bool empty() const {
return rem_.empty();
}

constexpr std::span<const uint8_t> rest() const {
return rem_;
}

constexpr const uint8_t* data() const {
return rem_.data();
}

bool truncate(size_t max_len) {
if (max_len > rem_.size()) {
return false;
}
rem_ = rem_.first(max_len);
return true;
}

bool skip(size_t n) {
if (n > rem_.size()) {
return false;
}
rem_ = rem_.subspan(n);
return true;
}

bool peek_bytes(size_t n, std::span<const uint8_t>* out) const {
if (out == nullptr || n > rem_.size()) {
return false;
}
*out = rem_.first(n);
return true;
}

bool read_bytes(size_t n, std::span<const uint8_t>* out) {
if (out == nullptr || n > rem_.size()) {
return false;
}
*out = rem_.first(n);
rem_ = rem_.subspan(n);
return true;
}

bool peek_u8(uint8_t* out) const {
if (out == nullptr || rem_.empty()) {
return false;
}
*out = rem_[0];
return true;
}

bool read_u8(uint8_t* out) {
if (!peek_u8(out)) {
return false;
}
rem_ = rem_.subspan(1);
return true;
}

bool read_be16(uint16_t* out) {
if (out == nullptr) {
return false;
}
uint16_t raw = 0;
if (!read_struct(&raw)) {
return false;
}
*out = ntohs(raw);
return true;
}

bool read_be32(uint32_t* out) {
if (out == nullptr) {
return false;
}
uint32_t raw = 0;
if (!read_struct(&raw)) {
return false;
}
*out = ntohl(raw);
return true;
}

template <typename T>
bool peek_struct(T* out) const {
static_assert(std::is_trivially_copyable_v<T>,
"ByteReader::peek_struct requires a trivially copyable type");
if (out == nullptr || rem_.size() < sizeof(T)) {
return false;
}
memcpy(out, rem_.data(), sizeof(T));
return true;
}

template <typename T>
bool read_struct(T* out) {
if (!peek_struct(out)) {
return false;
}
rem_ = rem_.subspan(sizeof(T));
return true;
}

private:
std::span<const uint8_t> rem_;
};

} // namespace nstun

#endif /* NSTUN_BYTE_READER_H_ */
94 changes: 51 additions & 43 deletions nstun/encap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>
#include <sys/socket.h>

#include "byte_reader.h"
#include "logs.h"
#include "net_defs.h"

Expand All @@ -22,8 +23,10 @@ int send_socks5_greeting(int fd) {
}

bool parse_socks5_auth_reply(std::span<const uint8_t> data) {
if (data.size() < 2) return false;
return data[0] == SOCKS5_VERSION && data[1] == SOCKS5_AUTH_NONE;
ByteReader reader(data);
socks5_auth_reply reply = {};
if (!reader.read_struct(&reply)) return false;
return reply.ver == SOCKS5_VERSION && reply.method == SOCKS5_AUTH_NONE;
}

int send_socks5_connect(int fd, const uint8_t* addr, uint16_t port_nbo, bool is_ipv6) {
Expand Down Expand Up @@ -74,36 +77,40 @@ int send_socks5_udp_associate(int fd) {
}

bool parse_socks5_connect_reply(std::span<const uint8_t> data, Socks5Reply* out) {
if (out == nullptr || data.size() < 4) return false;
if (out == nullptr) return false;

/* data[0]=ver, data[1]=rep, data[2]=rsv, data[3]=atyp */
if (data[0] != SOCKS5_VERSION) return false;
if (data[1] != SOCKS5_REP_SUCCESS) return false;
if (data[2] != 0x00) return false;
ByteReader reader(data);
uint8_t ver = 0, rep = 0, rsv = 0, atyp = 0;
if (!reader.read_u8(&ver) || !reader.read_u8(&rep) || !reader.read_u8(&rsv) ||
!reader.read_u8(&atyp)) {
return false;
}

if (ver != SOCKS5_VERSION) return false;
if (rep != SOCKS5_REP_SUCCESS) return false;
if (rsv != 0x00) return false;

Socks5Reply parsed = {};
parsed.atyp = data[3];
parsed.atyp = atyp;

if (parsed.atyp == SOCKS5_ATYP_IPV4) {
/* Full reply: 4-byte header + 4-byte IPv4 + 2-byte port */
if (data.size() < sizeof(socks5_req)) return false;
const auto* reply = reinterpret_cast<const socks5_req*>(data.data());
memcpy(&parsed.bind_ip4, &reply->dst_ip4, sizeof(reply->dst_ip4));
memcpy(&parsed.bind_port, &reply->dst_port, sizeof(reply->dst_port));
/* Remaining: 4-byte IPv4 + 2-byte port */
if (!reader.read_struct(&parsed.bind_ip4) ||
!reader.read_struct(&parsed.bind_port)) {
return false;
}
} else if (parsed.atyp == SOCKS5_ATYP_IPV6) {
/* Full reply: 4-byte header + 16-byte IPv6 + 2-byte port */
if (data.size() < sizeof(socks5_req6)) return false;
const auto* reply = reinterpret_cast<const socks5_req6*>(data.data());
memcpy(&parsed.bind_port, &reply->dst_port, sizeof(reply->dst_port));
/* Remaining: 16-byte IPv6 + 2-byte port */
if (!reader.skip(IPV6_ADDR_LEN) || !reader.read_struct(&parsed.bind_port)) {
return false;
}
} else if (parsed.atyp == SOCKS5_ATYP_DOMAIN) {
/* Full reply: 4-byte header + 1-byte domain_len + domain + 2-byte port */
if (data.size() < sizeof(socks5_req_domain)) return false;
uint8_t domain_len = data[4];
size_t needed =
sizeof(socks5_req_domain) + static_cast<size_t>(domain_len) + sizeof(uint16_t);
if (data.size() < needed) return false;
memcpy(&parsed.bind_port, data.data() + sizeof(socks5_req_domain) + domain_len,
sizeof(parsed.bind_port));
/* Remaining: 1-byte domain_len + domain + 2-byte port */
uint8_t domain_len = 0;
if (!reader.read_u8(&domain_len) || !reader.skip(domain_len) ||
!reader.read_struct(&parsed.bind_port)) {
return false;
}
} else {
return false;
}
Expand Down Expand Up @@ -150,26 +157,27 @@ bool parse_http_connect_reply(const std::vector<uint8_t>& buf) {
* Validate HTTP/1.0 or HTTP/1.1 version, single space, 3-digit 2xx status
* code, and a valid delimiter if trailing bytes follow. */
static constexpr std::string_view HTTP_VERSION_PREFIX = "HTTP/1.";
/* Offsets in "HTTP/1.x NNN": H(0)T(1)T(2)P(3)/(4)1(5).(6)x(7) (8)N(9)N(10)N(11) */
static constexpr size_t HTTP_MINOR_OFFSET = 7;
static constexpr size_t HTTP_SPACE_OFFSET = 8;
static constexpr size_t HTTP_STATUS_DIGIT_OFFSET = 9;
static constexpr size_t HTTP_RESPONSE_MIN_LEN =
HTTP_STATUS_DIGIT_OFFSET + 3; /* three status-code digits */

if (buf.size() < HTTP_RESPONSE_MIN_LEN) return false;
if (memcmp(buf.data(), HTTP_VERSION_PREFIX.data(), HTTP_VERSION_PREFIX.size()) != 0)
return false;
if (buf[HTTP_MINOR_OFFSET] != '0' && buf[HTTP_MINOR_OFFSET] != '1') return false;
if (buf[HTTP_SPACE_OFFSET] != ' ') return false;
/* Accept any 3-digit 2xx status code */
if (buf[HTTP_STATUS_DIGIT_OFFSET] != '2') return false;
if (buf[HTTP_STATUS_DIGIT_OFFSET + 1] < '0' || buf[HTTP_STATUS_DIGIT_OFFSET + 1] > '9')
ByteReader reader(buf);

std::span<const uint8_t> prefix;
if (!reader.read_bytes(HTTP_VERSION_PREFIX.size(), &prefix)) return false;
if (memcmp(prefix.data(), HTTP_VERSION_PREFIX.data(), HTTP_VERSION_PREFIX.size()) != 0)
return false;
if (buf[HTTP_STATUS_DIGIT_OFFSET + 2] < '0' || buf[HTTP_STATUS_DIGIT_OFFSET + 2] > '9')

uint8_t minor = 0, space = 0, d0 = 0, d1 = 0, d2 = 0;
if (!reader.read_u8(&minor) || !reader.read_u8(&space) || !reader.read_u8(&d0) ||
!reader.read_u8(&d1) || !reader.read_u8(&d2)) {
return false;
if (buf.size() > HTTP_RESPONSE_MIN_LEN) {
uint8_t delim = buf[HTTP_RESPONSE_MIN_LEN];
}
if (minor != '0' && minor != '1') return false;
if (space != ' ') return false;
/* Accept any 3-digit 2xx status code */
if (d0 != '2') return false;
if (d1 < '0' || d1 > '9') return false;
if (d2 < '0' || d2 > '9') return false;

uint8_t delim = 0;
if (reader.peek_u8(&delim)) {
if (delim != ' ' && delim != '\r' && delim != '\n') return false;
}
return true;
Expand Down
Loading
0