Skip to content

nstun: prevent SOCKS5 UDP control-channel busy loops - #387

Open
botegalucas wants to merge 10 commits into
google:masterfrom
botegalucas:fix/nstun-socks5-udp-control-spin-20261108
Open

botegalucas wants to merge 10 commits into
google:masterfrom
botegalucas:fix/nstun-socks5-udp-control-spin-20261108

Conversation

@botegalucas

Copy link
Copy Markdown

The nstun SOCKS5 UDP control path uses recv(..., MSG_PEEK | MSG_DONTWAIT) with level-triggered epoll. When a proxy sends a partial authentication or UDP ASSOCIATE reply, handlers return without consuming the readable bytes. The same event is then reported repeatedly, while the control callback also refreshes last_active. A complete IPv6 BND reply can become stuck because the first peek only captures ten bytes while the handler waits for 22 bytes. After establishment, an unexpected byte on the TCP control socket was also peeked indefinitely.
This change:
Consumes SOCKS5 authentication and UDP ASSOCIATE responses incrementally in a bounded per-flow buffer, retaining partial frames across events.
Updates activity only when bytes are actually received, instead of on every readable event.
Rejects unsupported non-IPv4 UDP ASSOCIATE BND replies explicitly; the current UDP relay path requires an IPv4 BND address.
Tears down the UDP association if its established TCP control connection receives unexpected data or closes.
Adds a socketpair/epoll regression test, wired into make test, covering partial reads, absence of repeated readiness after consumption, completed replies, EOF, and invalid lengths.
Reproduction and validation
An isolated harness ran the original and patched nsjail binaries with a real TUN interface and a local fake SOCKS5 proxy in docker run --privileged --network none. The test measured supervisor CPU using /proc//stat over approximately 0.9 seconds per case.
Proxy behavior
Original CPU (one core)
Patched CPU (one core)
1-byte auth reply
0.989
<0.01
Partial UDP ASSOCIATE response
1.000
<0.01
Complete IPv6 BND reply (unsupported in this UDP relay path)
1.000
<0.01
Unexpected byte after UDP ASSOCIATE
0.989
<0.01
Normal IPv4 UDP ASSOCIATE and successful handshakes with valid replies arriving in multiple fragments also passed in both versions. In those positive cases the local UDP relay received and verified the encapsulated guest datagram; neither version spun.
Real TUN + SOCKS5 integration, six scenarios: https://github.com/botegalucas/nsjail/actions/runs/42498067104
Clean PR candidate: full Dockerfile compilation, whitespace check and make-integrated regression test: https://github.com/botegalucas/nsjail/actions/runs/42498513031
Isolated integration harness (not part of this PR): https://github.com/botegalucas/nsjail/tree/investigate/socks5-udp-control-loop-20261108
Scope
This addresses resource-exhaustion behavior caused by a malformed or misbehaving configured SOCKS5 proxy. It is not a demonstrated sandbox escape, cross-jail compromise, or proof that an arbitrary remote endpoint can control the configured proxy. The integration tests are bounded and use a local proxy only. The PR deliberately contains production changes and an upstream-run regression test, without the privileged experimental integration workflow.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant