Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

fix(linux): a connection reset mid-transfer no longer kills the process with SIGPIPE (#268)

anof-cyber0172c6cb5cd7
diff --git a/Linux/src/pb_relay.c b/Linux/src/pb_relay.c
index d1bcd08..649a050 100644
--- a/Linux/src/pb_relay.c
+++ b/Linux/src/pb_relay.c
@@ -11,6 +11,7 @@
#include <netinet/tcp.h>
#include <poll.h>
#include <pthread.h>
+#include <signal.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdio.h>
@@ -236,9 +237,12 @@ static void res_unref(struct resreq *r) // res_mu held
if (--r->refs == 0) { pthread_cond_destroy(&r->cv); free(r); }
}
+static bool block_sigpipe(void);
+
static void *res_thread(void *arg)
{
struct resreq *r = arg;
+ block_sigpipe();
struct addrinfo hints, *res = NULL;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
@@ -668,6 +672,18 @@ static void proxy_desc(const struct pb_proxy *p, char *out, size_t n)
static void *worker_main(void *arg);
+// splice() has no MSG_NOSIGNAL: writing into a connection the peer already reset raises SIGPIPE, whose default action ends
+// the whole process (the CLI or GUI that hosts this library). Every relay thread blocks the signal for its lifetime, so the
+// write fails with EPIPE and the connection is closed by the ordinary error path. The embedding application's own signal
+// handling is not touched. Returns false when the mask could not be set.
+static bool block_sigpipe(void)
+{
+ sigset_t set;
+ sigemptyset(&set);
+ sigaddset(&set, SIGPIPE);
+ return pthread_sigmask(SIG_BLOCK, &set, NULL) == 0;
+}
+
// Caller holds R.jmu.
static void worker_spawn_locked(void)
{
@@ -881,6 +897,7 @@ out:
static void *worker_main(void *arg)
{
(void)arg;
+ block_sigpipe();
pthread_mutex_lock(&R.jmu);
while (!atomic_load(&R.stopping)) {
struct job *prev = NULL, *j;
@@ -917,6 +934,7 @@ static void *worker_main(void *arg)
static void *acceptor_main(void *arg)
{
(void)arg;
+ block_sigpipe();
int64_t last_cap_log = 0;
while (!atomic_load(&R.stopping)) {
struct pollfd p[2] = { { .fd = R.lfd, .events = POLLIN }, { .fd = R.stopfd, .events = POLLIN } };
@@ -1193,6 +1211,7 @@ static void free_grave(struct dthread *t)
static void *data_main(void *arg)
{
struct dthread *t = arg;
+ if (!block_sigpipe()) t->nosplice = true; // the copy path sends with MSG_NOSIGNAL
struct epoll_event evs[64];
int64_t last_sweep = now_ms();
int64_t sweep_every = (int64_t)R.cfg.tcp_idle_timeout_s * 250; // idle/4, clamped to 1s..5s
@@ -1647,6 +1666,7 @@ static void udp_sweep(void)
static void *udp_main(void *arg)
{
(void)arg;
+ block_sigpipe();
struct epoll_event evs[64];
while (!atomic_load(&R.stopping)) {
int n = epoll_wait(R.uep, evs, 64, 1000);
diff --git a/Linux/src/tests/relay_test.c b/Linux/src/tests/relay_test.c
index 717d8ae..c6f4435 100644
--- a/Linux/src/tests/relay_test.c
+++ b/Linux/src/tests/relay_test.c
@@ -1033,6 +1033,57 @@ static void test_throughput(void)
}
}
+// A server that streams data until the connection breaks, and one that resets every connection at once.
+static void burst_handler(int fd, const struct srvcfg *c)
+{
+ (void)c;
+ char buf[16384];
+ memset(buf, 'b', sizeof buf);
+ for (int i = 0; i < 400; i++)
+ if (send(fd, buf, sizeof buf, MSG_NOSIGNAL) < 0) break;
+}
+static void reset_handler(int fd, const struct srvcfg *c)
+{
+ (void)c;
+ struct linger lg = { 1, 0 };
+ setsockopt(fd, SOL_SOCKET, SO_LINGER, &lg, sizeof lg); // close() sends RST
+}
+
+// splice() into a connection the peer already reset raises SIGPIPE, which would end the process that hosts the relay
+// (GitHub issue 268). Every such write must just fail and close that one connection; other connections keep working.
+static void test_sigpipe(void)
+{
+ struct srv *burst = srv_start(burst_handler, NULL), *rst = srv_start(reset_handler, NULL);
+ CHECK(burst && rst, "SIGPIPE: helper servers started");
+ if (!burst || !rst) return;
+ for (int i = 0; i < 20; i++) { // server -> client: the client resets while data is still being forwarded
+ uint16_t src = new_src();
+ set_tcp(src, "127.0.0.1", burst->port, PB_ACT_DIRECT, 0);
+ int fd = relay_connect(src);
+ if (fd < 0) continue;
+ char b[64];
+ recv(fd, b, sizeof b, 0);
+ struct linger lg = { 1, 0 };
+ setsockopt(fd, SOL_SOCKET, SO_LINGER, &lg, sizeof lg);
+ close(fd);
+ }
+ msleep(300);
+ CHECK(tcp_roundtrip("127.0.0.1", g_echo->port, PB_ACT_DIRECT, 0, 50000, false) == 50000, "SIGPIPE: relay still serves traffic after clients reset mid-transfer");
+ for (int i = 0; i < 20; i++) { // client -> server: the server resets, the client keeps sending
+ uint16_t src = new_src();
+ set_tcp(src, "127.0.0.1", rst->port, PB_ACT_DIRECT, 0);
+ int fd = relay_connect(src);
+ if (fd < 0) continue;
+ char buf[65536];
+ memset(buf, 'c', sizeof buf);
+ for (int k = 0; k < 8; k++) if (send(fd, buf, sizeof buf, MSG_NOSIGNAL) < 0) break;
+ close(fd);
+ }
+ msleep(300);
+ CHECK(tcp_roundtrip("127.0.0.1", g_echo->port, PB_ACT_DIRECT, 0, 50000, false) == 50000, "SIGPIPE: relay still serves traffic after servers reset mid-transfer");
+ CHECK(tcp_roundtrip("127.0.0.1", g_echo->port, PB_ACT_PROXY, 1, 50000, false) == 50000, "SIGPIPE: proxied traffic also unaffected");
+}
+
static void test_tcp_halfclose(void)
{
CHECK(tcp_roundtrip("127.0.0.1", g_echo->port, PB_ACT_PROXY, 1, 300000, true) == 300000, "half-close via SOCKS5: all echoed then EOF");
@@ -1915,6 +1966,7 @@ int main(void)
RUN(test_tcp_identity);
RUN(test_tcp_bad_proxy);
RUN(test_tcp_halfclose);
+ RUN(test_sigpipe);
RUN(test_udp);
RUN(test_flows);
RUN(test_proxy_process_loop);
diff --git a/Linux/src/tests/run_relay_test.sh b/Linux/src/tests/run_relay_test.sh
index c9cf79d..1d27b80 100755
--- a/Linux/src/tests/run_relay_test.sh
+++ b/Linux/src/tests/run_relay_test.sh
@@ -8,8 +8,12 @@ CF="-std=gnu11 -Wall -Wextra -O1 -g -D_GNU_SOURCE -fstack-protector-strong -I..
if [ "$mode" = plain ] || [ "$mode" = both ]; then
gcc $CF -O2 relay_test.c ../pb_relay.c -o "$out/relay_test" -lpthread
"$out/relay_test"
+ gcc $CF sigpipe_test.c -o "$out/sigpipe_test" -lpthread
+ "$out/sigpipe_test"
fi
if [ "$mode" = asan ] || [ "$mode" = both ]; then
gcc $CF -fsanitize=address,undefined -fno-omit-frame-pointer relay_test.c ../pb_relay.c -o "$out/relay_test_asan" -lpthread
+ gcc $CF -fsanitize=address,undefined -fno-omit-frame-pointer sigpipe_test.c -o "$out/sigpipe_test_asan" -lpthread
+ "$out/sigpipe_test_asan"
PB_TEST_WATCHDOG="${PB_TEST_WATCHDOG:-900}" LARGE_MB="${LARGE_MB:-4}" THRU_MB="${THRU_MB:-32}" PB_CHURN_TCP="${PB_CHURN_TCP:-10000}" PB_CHURN_UDP="${PB_CHURN_UDP:-3000}" PB_CHURN_RSS_MB="${PB_CHURN_RSS_MB:-48}" PB_PERF_UDP=10000 PB_PERF_CONNS=400 ASAN_OPTIONS=detect_leaks=1:quarantine_size_mb=1 UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 "$out/relay_test_asan"
fi
diff --git a/Linux/src/tests/sigpipe_test.c b/Linux/src/tests/sigpipe_test.c
new file mode 100644
index 0000000..af7607b
--- /dev/null
+++ b/Linux/src/tests/sigpipe_test.c
@@ -0,0 +1,110 @@
+// White-box regression for GitHub issue 268: splice() has no MSG_NOSIGNAL, so forwarding into a connection that the peer reset
+// raises SIGPIPE, which ends the whole process that hosts the relay (the CLI or the GUI). This test includes the relay source and
+// drives its real pump_splice() and its real data thread entry.
+// * unprotected thread: the write kills the process with SIGPIPE (this proves the test reproduces the problem)
+// * a relay data thread: the same write fails with EPIPE, the pipe is left clean, the next connection still forwards
+#include "../pb_relay.c"
+#include <sys/wait.h>
+
+// the engine side of the relay: nothing of it is used on these paths
+int pb_core_take_tcp_orig(uint32_t a, uint16_t b, struct pb_orig *o) { (void)a; (void)b; (void)o; return -1; }
+int pb_core_get_udp_flow(uint32_t a, struct pb_orig *o) { (void)a; (void)o; return -1; }
+int pb_core_name_for(const struct pb_orig *o, char *out, size_t n) { (void)o; (void)out; (void)n; return -1; }
+int pb_core_is_proxy_process(uint32_t a, uint32_t b) { (void)a; (void)b; return 0; }
+int pb_core_udp_sender_ok(const struct pb_orig *o, uint16_t p) { (void)o; (void)p; return 1; }
+void pb_core_decide(const struct pb_orig *o, struct pb_decision *d) { (void)o; d->action = PB_ACT_DIRECT; d->proxy_id = 0; }
+int pb_core_get_proxy(uint32_t id, struct pb_proxy *p) { (void)id; (void)p; return -1; }
+void pb_core_log(const char *fmt, ...) { (void)fmt; }
+void pb_core_flow_event(const struct pb_orig *o, int action) { (void)o; (void)action; }
+void pb_core_dns_observe(const uint8_t *m, size_t n) { (void)m; (void)n; }
+
+static int failures;
+#define CHECK(c, ...) do { if (!(c)) { failures++; printf("FAIL " __VA_ARGS__); printf("\n"); } else { printf("PASS " __VA_ARGS__); printf("\n"); } } while (0)
+
+// a connected TCP pair on loopback: fd[0] is the relay's side, fd[1] the far end
+static void tcp_pair(int fd[2])
+{
+ int l = socket(AF_INET, SOCK_STREAM, 0);
+ struct sockaddr_in a = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK) };
+ bind(l, (struct sockaddr *)&a, sizeof a);
+ socklen_t len = sizeof a;
+ getsockname(l, (struct sockaddr *)&a, &len);
+ listen(l, 1);
+ fd[1] = socket(AF_INET, SOCK_STREAM, 0);
+ connect(fd[1], (struct sockaddr *)&a, len);
+ fd[0] = accept4(l, NULL, NULL, SOCK_NONBLOCK);
+ close(l);
+}
+
+// Forward one chunk from src into a destination that the far end reset, after the reset error was already reported once, so
+// the next write is EPIPE (and raises SIGPIPE). Exits 0 when pump_splice() just reports the failure.
+static int forward_into_reset_peer(int direction)
+{
+ struct dthread t = {0};
+ struct conn c = {0};
+ int src[2], dst[2], pfd[2];
+ tcp_pair(src);
+ tcp_pair(dst);
+ if (pipe2(pfd, O_NONBLOCK) != 0) return 2;
+ t.pfd[0] = pfd[0]; t.pfd[1] = pfd[1];
+ c.fd[direction] = src[0];
+ c.fd[1 - direction] = dst[0];
+ struct linger rst = { 1, 0 };
+ setsockopt(dst[1], SOL_SOCKET, SO_LINGER, &rst, sizeof rst);
+ close(dst[1]); // RST towards dst[0]
+ struct pollfd p = { dst[0], POLLIN, 0 };
+ char one;
+ if (poll(&p, 1, 2000) != 1 || recv(dst[0], &one, 1, 0) != -1 || errno != ECONNRESET) return 3;
+ send(src[1], "payload", 7, MSG_NOSIGNAL);
+ int r = pump_splice(&t, &c, direction);
+ return r == -1 ? 0 : 4;
+}
+
+static void *relay_thread(void *arg)
+{
+ atomic_store(&R.stopping, true); // run the real thread entry through its shutdown path only
+ struct dthread t = {0};
+ data_main(&t);
+ *(int *)arg = forward_into_reset_peer(0) == 0 && forward_into_reset_peer(1) == 0;
+ return NULL;
+}
+
+static int status_of(void (*fn)(void), const char *what)
+{
+ pid_t pid = fork();
+ if (pid == 0) { alarm(10); fn(); _exit(0); }
+ int st = 0;
+ waitpid(pid, &st, 0);
+ (void)what;
+ return st;
+}
+
+static void unprotected(void) { if (forward_into_reset_peer(0)) _exit(1); }
+static void protected_by_relay(void)
+{
+ pthread_t th;
+ int ok = 0;
+ pthread_create(&th, NULL, relay_thread, &ok);
+ pthread_join(th, NULL);
+ _exit(ok ? 0 : 1);
+}
+
+int main(void)
+{
+ signal(SIGPIPE, SIG_DFL); // never depend on what the parent shell set
+ sigset_t s;
+ sigemptyset(&s); sigaddset(&s, SIGPIPE);
+ pthread_sigmask(SIG_UNBLOCK, &s, NULL);
+
+ int st = status_of(unprotected, "plain thread");
+ CHECK(WIFSIGNALED(st) && WTERMSIG(st) == SIGPIPE, "a thread without protection is killed by SIGPIPE when splicing into a reset peer (test reproduces issue 268)");
+ st = status_of(protected_by_relay, "relay thread");
+ CHECK(WIFEXITED(st) && WEXITSTATUS(st) == 0, "a relay data thread survives: pump_splice reports the failure, both directions");
+
+ // the signal mask is per thread: the thread that started the relay keeps its own signal handling
+ sigset_t cur;
+ pthread_sigmask(SIG_BLOCK, NULL, &cur);
+ CHECK(!sigismember(&cur, SIGPIPE), "the embedding application's own SIGPIPE handling is untouched");
+ printf("\nRESULT: %d failed\n", failures);
+ return failures != 0;
+}
diff --git a/Linux/tests/gui_headless.sh b/Linux/tests/gui_headless.sh
index dd91f3d..887f890 100755
--- a/Linux/tests/gui_headless.sh
+++ b/Linux/tests/gui_headless.sh
@@ -11,13 +11,14 @@ PASS=0; FAIL=0; DNSIP=10.77.0.4
ok() { echo " PASS $1"; PASS=$((PASS+1)); }
bad() { echo " FAIL $1"; FAIL=$((FAIL+1)); }
has() { [[ "$2" == *"$3"* ]] && ok "$1" || bad "$1 (got: ${2:0:160})"; }
-cleanup() { kill $GUI_PID $FAKE $DNSP $BW $WWW 2>/dev/null; wait 2>/dev/null; ip addr del $DNSIP/32 dev lo 2>/dev/null; rm -rf "$WORK"; }
+cleanup() { kill $GUI_PID 2>/dev/null; wait $GUI_PID 2>/dev/null; kill $FAKE $DNSP $BW $WWW 2>/dev/null; wait 2>/dev/null; # the client first: the display daemon aborts when its client vanishes
+ ip addr del $DNSIP/32 dev lo 2>/dev/null; rm -rf "$WORK"; }
trap cleanup EXIT
[ "$(id -u)" = 0 ] || { echo "run as root"; exit 1; }
ip addr add $DNSIP/32 dev lo 2>/dev/null
python3 "$HERE/fake_proxies.py" "$PLOG" >/dev/null 2>&1 & FAKE=$!
python3 "$HERE/fake_dns.py" $DNSIP "$DLOG" >/dev/null 2>&1 & DNSP=$!
-broadwayd :9 >/dev/null 2>&1 & BW=$!
+( ulimit -c 0; exec broadwayd :9 ) >/dev/null 2>&1 & BW=$! # no core dump (see gui_ui.sh)
sleep 1.5
# Seeded in the format of the previous GUI versions, so the first start also exercises the migration to .pbprofile files.
diff --git a/Linux/tests/gui_ui.sh b/Linux/tests/gui_ui.sh
index aa44912..71be0e5 100755
--- a/Linux/tests/gui_ui.sh
+++ b/Linux/tests/gui_ui.sh
@@ -22,7 +22,7 @@ gcc -O1 -g -w -D_GNU_SOURCE -DPB_TEST_HOOKS ${EXTRA_CFLAGS:-} -I"$ROOT/src" -I"$
-L"$LIBDIR" -Wl,-rpath,"$LIBDIR" -lproxybridge -lpthread $(pkg-config --libs gtk+-3.0) ${EXTRA_LDFLAGS:-} || exit 1
python3 "$HERE/fake_proxies.py" "$WORK/proxy.log" > /dev/null 2>&1 & FAKE=$!
-broadwayd :8 > /dev/null 2>&1 & BW=$!
+( ulimit -c 0; exec broadwayd :8 ) > /dev/null 2>&1 & BW=$! # no core dump: apport would pop up a crash report when the daemon aborts on a client that vanished
sleep 1.5
mkdir -p "$WORK/cfg"
GDK_BACKEND=broadway BROADWAY_DISPLAY=:8 PROXYBRIDGE_CONFIG_DIR="$WORK/cfg" LD_LIBRARY_PATH="$LIBDIR" \