8000 nstun: prevent SOCKS5 UDP control-channel busy loops by botegalucas · Pull Request #387 · 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
6 changes: 5 additions & 1 deletion 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/nstun_buffer_budget_test tests/nstun_policy_test tests/nstun_ip_test tests/nstun_udp_peer_test tests/mount_source_anchor_test
TEST_BINS = tests/nstun_buffer_budget_test tests/nstun_policy_test tests/nstun_ip_test tests/nstun_udp_peer_test tests/mount_source_anchor_test tests/socks5_control_reader_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 @@ -175,6 +175,7 @@ test: $(BIN) $(TEST_BINS) test-cmdline test-newapi-recursive-readonly
$(call run_test, ./tests/nstun_ip_test, 0)
$(call run_test, ./tests/nstun_udp_peer_test, 0)
$(call run_test, ./tests/mount_source_anchor_test, 0)
$(call run_test, ./tests/socks5_control_reader_test, 0)
# --- Basic sanity tests ---
$(call run_test, ./nsjail -q -Mo --chroot / --user 99999 --group 99999 -- /bin/true, 0)
$(call run_test, ./nsjail -q -Mo --chroot / --user 99999 --group 99999 -- /bin/false, 1)
Expand Down Expand Up @@ -299,6 +300,9 @@ tests/nstun_ip_test: tests/nstun_ip_test.cc nstun/ip.cc nstun/net_defs.h logs.h
tests/nstun_udp_peer_test: tests/nstun_udp_peer_test.cc nstun/udp_peer.h nstun/core.h
$(CXX) $(filter-out -c,$(CXXFLAGS)) tests/nstun_udp_peer_test.cc -o $@

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

# Dependencies (Generated by makedepend)
# DO NOT DELETE THIS LINE -- make depend depends on it.

Expand Down
1 change: 1 addition & 0 deletions nstun/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ struct UdpFlow : public Flow {

bool host_fd_is_listener = false;
std::deque<std::vector<uint8_t>> tx_queue;
std::vector<uint8_t> socks5_control_rx; /* Consumed partial TCP handshake bytes */

~UdpFlow() override {
if (host_fd != -1 && !host_fd_is_listener) ::close(host_fd);
Expand Down
33 changes: 33 additions & 0 deletions nstun/socks5_control_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef NSTUN_SOCKS5_CONTROL_READER_H_
#define NSTUN_SOCKS5_CONTROL_READER_H_
#include <algorithm>
#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <sys/socket.h>
#include <vector>
namespace nstun {
enum class Socks5ControlRead { READY, PENDING, CLOSED };
inline Socks5ControlRead read_socks5_control(int fd, std::vector<uint8_t>* pending,
size_t required, time_t* last_active) {
if (pending == nullptr || last_active == nullptr || required > 512 ||
pending->size() > required) return Socks5ControlRead::CLOSED;
while (pending->size() < required) {
uint8_t buf[512];
const size_t want = std::min(sizeof(buf), required - pending->size());
const ssize_t n = recv(fd, buf, want, MSG_DONTWAIT);
if (n > 0) {
pending->insert(pending->end(), buf, buf + n);
*last_active = time(nullptr);
continue;
}
if (n == 0) return Socks5ControlRead::CLOSED;
if (errno == EINTR) continue;
if (errno == EAGAIN || errno == EWOULDBLOCK) return Socks5ControlRead::PENDING;
return Socks5ControlRead::CLOSED;
}
return Socks5ControlRead::READY;
}
}
#endif
158 changes: 75 additions & 83 deletions nstun/udp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "logs.h"
#include "macros.h"
#include "policy.h"
#include "socks5_control_reader.h"
#include "tun.h"
#include "udp_peer.h"

Expand Down Expand Up @@ -210,6 +211,7 @@ static void handle_udp_tcp_connecting(Context* ctx, UdpFlow* flow, uint32_t even

if (flow->use_socks5) {
flow->state = UdpSocks5State::SOCKS5_GREETING;
flow->last_active = time(NULL);
if (nstun::send_socks5_greeting(fd) < 0) {
udp_destroy_flow(ctx, flow);
return;
Expand All @@ -218,80 +220,70 @@ static void handle_udp_tcp_connecting(Context* ctx, UdpFlow* flow, uint32_t even
}

static void handle_udp_socks5_greeting(Context* ctx, UdpFlow* flow, uint32_t events) {
int fd = flow->tcp_fd;
socks5_auth_reply reply;
ssize_t n = recv(fd, &reply, sizeof(reply), MSG_PEEK | MSG_DONTWAIT);
if (n <= 0) {
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return;
udp_destroy_flow(ctx, flow);
return;
}
if (n < (ssize_t)sizeof(reply)) return;
recv(fd, &reply, sizeof(reply), MSG_DONTWAIT);
if (!nstun::parse_socks5_auth_reply(std::span<const uint8_t>(
reinterpret_cast<const uint8_t*>(&reply), sizeof(reply)))) {
udp_destroy_flow(ctx, flow);
return;
}
flow->state = UdpSocks5State::SOCKS5_ASSOCIATE;
if (nstun::send_socks5_udp_associate(fd) < 0) {
udp_destroy_flow(ctx, flow);
return;
}
if (events & (EPOLLERR | EPOLLHUP)) {
udp_destroy_flow(ctx, flow);
return;
}
const auto status = read_socks5_control(flow->tcp_fd, &flow->socks5_control_rx,
sizeof(socks5_auth_reply), &flow->last_active);
if (status == Socks5ControlRead::PENDING) return;
if (status == Socks5ControlRead::CLOSED) {
udp_destroy_flow(ctx, flow);
return;
}

const auto& rx = flow->socks5_control_rx;
if (!parse_socks5_auth_reply(std::span<const uint8_t>(rx.data(), rx.size()))) {
udp_destroy_flow(ctx, flow);
return;
}
flow->socks5_control_rx.clear();
flow->state = UdpSocks5State::SOCKS5_ASSOCIATE;
if (send_socks5_udp_associate(flow->tcp_fd) < 0) {
udp_destroy_flow(ctx, flow);
return;
}
}

static void handle_udp_socks5_associate(Context* ctx, UdpFlow* flow, uint32_t events) {
int fd = flow->tcp_fd;
socks5_req req = {};
ssize_t n = recv(fd, &req, sizeof(req), MSG_PEEK | MSG_DONTWAIT);
if (n <= 0) {
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return;
udp_destroy_flow(ctx, flow);
return;
}
if (n < 4) return;
if (req.ver != SOCKS5_VERSION || req.cmd != SOCKS5_REP_SUCCESS) {
udp_destroy_flow(ctx, flow);
return;
}

size_t expected_len = 0;
if (req.atyp == SOCKS5_ATYP_IPV4) {
expected_len = sizeof(socks5_req); /* header+ip4+port */
} else if (req.atyp == SOCKS5_ATYP_IPV6) {
expected_len = sizeof(socks5_req6); /* header+ip6+port */
} else if (req.atyp == SOCKS5_ATYP_DOMAIN) {
socks5_max_buf buf;
n = recv(fd, &buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT);
if (n < 5) return;
socks5_req_domain* dreq = reinterpret_cast<socks5_req_domain*>(&buf);
expected_len = 5 + dreq->domain_len + 2;
} else {
udp_destroy_flow(ctx, flow);
return;
}

if ((size_t)n < expected_len) return;

socks5_max_buf buf;
recv(fd, &buf, expected_len, MSG_DONTWAIT);

nstun::Socks5Reply res;
if (!nstun::parse_socks5_connect_reply(
std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(&buf), expected_len),
&res)) {
udp_destroy_flow(ctx, flow);
return;
}

if (res.atyp == SOCKS5_ATYP_IPV4) {
memcpy(&flow->bnd_ip, &res.bind_ip4, sizeof(flow->bnd_ip));
memcpy(&flow->bnd_port, &res.bind_port, sizeof(flow->bnd_port));
} else {
LOG_E("SOCKS5 UDP proxy returned unsupported address type");
udp_destroy_flow(ctx, flow);
return;
}
if (events & (EPOLLERR | EPOLLHUP)) {
udp_destroy_flow(ctx, flow);
return;
}
const int fd = flow->tcp_fd;
auto status = read_socks5_control(fd, &flow->socks5_control_rx, 4, &flow->last_active);
if (status == Socks5ControlRead::PENDING) return;
if (status == Socks5ControlRead::CLOSED) {
udp_destroy_flow(ctx, flow);
return;
}

const auto& rx = flow->socks5_control_rx;
/* This UDP path supports only IPv4 BND replies. In particular an IPv6
* BND response can never pass the original 10-byte MSG_PEEK limit,
* causing a perpetual EPOLLIN busy loop even when the full reply arrived. */
if (rx[0] != SOCKS5_VERSION || rx[1] != SOCKS5_REP_SUCCESS ||
rx[2] != 0 || rx[3] != SOCKS5_ATYP_IPV4) {
LOG_W("Unsupported or invalid SOCKS5 UDP ASSOCIATE reply");
udp_destroy_flow(ctx, flow);
return;
}
status = read_socks5_control(fd, &flow->socks5_control_rx, sizeof(socks5_req),
&flow->last_active);
if (status == Socks5ControlRead::PENDING) return;
if (status == Socks5ControlRead::CLOSED) {
udp_destroy_flow(ctx, flow);
return;
}
Socks5Reply res = {};
if (!parse_socks5_connect_reply(std::span<const uint8_t>(
rx.data(), rx.size()), &res)) {
udp_destroy_flow(ctx, flow);
return;
}
flow->socks5_control_rx.clear();
memcpy(&flow->bnd_ip, &res.bind_ip4, sizeof(flow->bnd_ip));
memcpy(&flow->bnd_port, &res.bind_port, sizeof(flow->bnd_port));

int udp_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (udp_fd == -1) {
Expand Down Expand Up @@ -349,17 +341,18 @@ static void handle_udp_socks5_associate(Context* ctx, UdpFlow* flow, uint32_t ev
}

static void handle_udp_established(Context* ctx, UdpFlow* flow, uint32_t events) {
int fd = flow->tcp_fd;
/* Detect SOCKS5 TCP control channel death */
if (events & (EPOLLERR | EPOLLHUP | EPOLLIN)) {
uint8_t buf[1];
ssize_t n = recv(fd, buf, 1, MSG_PEEK | MSG_DONTWAIT);
if (n <= 0) {
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return;
LOG_D("SOCKS5 TCP control channel died, destroying UDP flow");
udp_destroy_flow(ctx, flow);
}
}
if (!(events & (EPOLLERR | EPOLLHUP | EPOLLIN))) return;
/* The SOCKS5 UDP ASSOCIATE TCP control channel carries no data after
* negotiation; any unexpected byte must be consumed or cause teardown.
* Peeking indefinitely at readable data otherwise spins on epoll. */
uint8_t byte;
const ssize_t n = recv(flow->tcp_fd, &byte, 1, MSG_DONTWAIT);
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
!(events & (EPOLLERR | EPOLLHUP))) {
return;
}
if (n > 0) LOG_W("Unexpected data on SOCKS5 UDP control connection");
udp_destroy_flow(ctx, flow);
}

static const UdpStateHandlers kUdpStateTable[] = {
Expand All @@ -370,7 +363,6 @@ static const UdpStateHandlers kUdpStateTable[] = {
};

static void handle_host_udp_control(Context* ctx, UdpFlow* flow, uint32_t events) {
flow->last_active = time(NULL);

size_t state_idx = static_cast<size_t>(flow->state);
if (state_idx >= sizeof(kUdpStateTable) / sizeof(kUdpStateTable[0])) {
Expand Down
73 changes: 73 additions & 0 deletions tests/socks5_control_reader_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <sys/epoll.h>
#include <sys/socket.h>
#include <unistd.h>

#include <cassert>
#include <cstdio>
#include <ctime>
#include <vector>

#include "nstun/socks5_control_reader.h"

static void test_partial(const std::vector<uint8_t>& first,
const std::vector<uint8_t>& remainder, size_t expected_length) {
int fds[2];
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0);
const int epfd = epoll_create1(EPOLL_CLOEXEC);
assert(epfd >= 0);

struct epoll_event registration = {
.events = EPOLLIN | EPOLLERR | EPOLLHUP, .data = {.fd = fds[0]}};
assert(epoll_ctl(epfd, EPOLL_CTL_ADD, fds[0], &registration) == 0);

std::vector<uint8_t> received;
time_t last_active = 0;
assert(write(fds[1], first.data(), first.size()) == (ssize_t)first.size());

struct epoll_event event = {};
assert(epoll_wait(epfd, &event, 1, 0) == 1);
auto result = nstun::read_socks5_control(
fds[0], &received, expected_length, &last_active);
assert(result == nstun::Socks5ControlRead::PENDING);
assert(received == first);
assert(last_active != 0);

/* Consumed partial replies must not keep waking level-triggered epoll. */
for (int i = 0; i < 1000; i++) {
assert(epoll_wait(epfd, &event, 1, 0) == 0);
}

assert(write(fds[1], remainder.data(), remainder.size()) ==
(ssize_t)remainder.size());
assert(epoll_wait(epfd, &event, 1, 0) == 1);
result = nstun::read_socks5_control(
fds[0], &received, expected_length, &last_active);
assert(result == nstun::Socks5ControlRead::READY);
assert(received.size() == expected_length);

close(epfd);
close(fds[0]);
close(fds[1]);
}

static void test_eof_and_invalid_length() {
int fds[2];
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0);
std::vector<uint8_t> received;
time_t last_active = 0;
close(fds[1]);

assert(nstun::read_socks5_control(fds[0], &received, 2, &last_active) ==
nstun::Socks5ControlRead::CLOSED);
assert(nstun::read_socks5_control(fds[0], &received, 513, &last_active) ==
nstun::Socks5ControlRead::CLOSED);
close(fds[0]);
}

int main() {
test_partial({5}, {0}, 2);
test_partial({5, 0, 0, 1}, {0, 0, 0, 0, 0, 0}, 10);
test_partial({5, 0, 0, 4}, std::vector<uint8_t>(18), 22);
test_eof_and_invalid_length();
puts("SOCKS5 UDP control-reader regression: PASS");
}
0