CVE-2026-95821: Dangling stack pointer left installed as TCP receive callback after a failed SOCKS5 handshake
The SOCKS5 client in subsys/net/lib/socks/socks.c passes the addresses of two of socks5tcpconnect()'s own stack variables, methodrsp and cmdrsp, to netcontextrecv() as the user data for the negotiation and CONNECT response callbacks. For a TCP context netcontextrecv() reaches nettcprecv(), which only stores context->recvcb and conn->recvuserdata and never removes them. On the success path zsockconnectctx() immediately replaces them with zsockreceivedcb(), but on every error path nothing did, so the network context was left holding a pointer into a stack frame that had already been destroyed.
Any remote peer acting as the proxy can arm this with a protocol-legal response: answering the method negotiation with NO ACCEPTABLE METHODS (0xFF), sending a bad version byte, returning a non-success REP in the CONNECT reply, or simply stalling until CONFIGNETSOCKETSCONNECTTIMEOUT expires all make socks5tcpconnect() return an error while the callbacks stay installed. The dangling pointer is then dereferenced asynchronously from the networking threads: tcpconnclose() in subsys/net/ip/tcp.c invokes context->recvcb() on FIN, RST or timeout, and the callback memset()s through the stale pointer, while any further data on the socket reaches netcontextpacketreceived() and makes netpktread() write peer-supplied bytes there.
The result is a write of two to four bytes (zeroes on the teardown path, peer-chosen bytes on the data path) into a stack region of the thread that called connect(), at a moment when that thread may have reused the memory for a live frame. The write address is not attacker-controlled, so the practical impact is limited corruption or a crash rather than a directed memory-write primitive; the author observed it as a SEGV in tcpconnclose() under CONFIGASAN. Only configurations with CONFIGSOCKS enabled and a SOCKS5 proxy configured on the socket are affected.
The fix routes the error returns through a common exit that calls netcontextrecv(ctx, NULL, KNOWAIT, NULL), uninstalling the callback and the user-data pointer before the stack frame goes away.
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Compensating control
Route every error return in socks5_tcp_connect() through a common exit that calls net_context_recv(ctx, NULL, K_NO_WAIT, NULL) to uninstall the receive callback and user-data pointer before the stack frame goes away.
Event History
Frequently Asked Questions
Which deployments are exposed to this issue?
Deployments using the Zephyr SOCKS5 client for TCP connections are exposed when a remote peer acting as the proxy can control or disrupt the SOCKS5 handshake. The issue is triggered when the handshake fails and the TCP context remains active with the negotiation callback installed.
What can a malicious or unreliable SOCKS5 proxy do to trigger the vulnerable state?
The proxy can send a protocol-valid NO ACCEPTABLE METHODS response, a response with a bad version byte, or a CONNECT reply with a non-success REP value. It can also trigger the condition by stalling until CONFIG_NET_SOCKETS_CONNECT_TIMEOUT expires.
When is the stale callback used after a failed connection attempt?
After socks5_tcp_connect() returns an error, later TCP connection closure processing can invoke the still-installed receive callback. FIN, RST, or timeout handling in tcp_conn_close() can cause this asynchronous invocation.