linux/include/linux/sunrpc
Chuck Lever 46bc86c833 SUNRPC: pin upper rpc_clnt across the TLS connect_worker
The TLS connect path has a use-after-free: nothing pins the
upper rpc_clnt across the delayed connect_worker. xs_connect()
stores task->tk_client in sock_xprt::clnt as a raw pointer
and queues the worker; for TLS-secured transports that worker
is xs_tcp_tls_setup_socket(), which reads several fields out
of the saved pointer (cl_timeout, cl_program, cl_prog,
cl_vers, cl_cred, cl_stats) to construct the args for the
inner handshake rpc_clnt.

The xprt does not reference the rpc_clnt; the rpc_clnt
references the xprt. xs_destroy() does cancel the
connect_worker, but it runs only when the xprt's refcount
drops to zero, which cannot happen until the rpc_clnt
releases its cl_xprt reference in rpc_free_client_work().
When a TLS handshake fails fatally (for example, an mTLS
mount whose client cert does not match the server), the
connecting task is woken with -EACCES and exits, the mount
caller invokes rpc_shutdown_client(), and the upper rpc_clnt
is freed before the queued connect_worker fires.
xs_tcp_tls_setup_socket() then dereferences the freed clnt,
producing the refcount_t underflow Michael Nemanov reported.

Take a reference on the upper rpc_clnt in xs_connect() for
TLS transports via a new rpc_hold_client() helper, and drop
it in the connect_worker's exit path with rpc_release_client().
The xprt_lock_connect() / xprt_unlock_connect() pairing
already serialises xs_connect() with xs_tcp_tls_setup_socket(),
so the take and release are balanced one-for-one.

The non-TLS connect worker (xs_tcp_setup_socket) never reads
sock_xprt::clnt, so leave that path alone and avoid the
clnt-holds-xprt-holds-clnt cycle that would otherwise prevent
xprt destruction.

Reported-by: Michael Nemanov <michael.nemanov@vastdata.com>
Closes: https://lore.kernel.org/linux-nfs/40e3d522-dfcf-4fc1-9c55-b5e81f1536d5@vastdata.com/
Fixes: 75eb6af7ac ("SUNRPC: Add a TCP-with-TLS RPC transport class")
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
Tested-by: Michael Nemanov <michael.nemanov@vastdata.com>
Reviewed-by: Michael Nemanov <michael.nemanov@vastdata.com>
Signed-off-by: Anna Schumaker <anna.schumaker@hammerspace.com>
2026-07-08 14:07:19 -04:00
..
xdrgen Documentation: Add the RPC language description of NLM version 3 2026-06-09 16:32:59 -04:00
addr.h
auth_gss.h
auth.h
bc_xprt.h SUNRPC: new helper function for stopping backchannel server 2025-11-23 15:30:12 -05:00
cache.h sunrpc: add a generic netlink family for cache upcalls 2026-06-01 11:08:18 -04:00
clnt.h SUNRPC: pin upper rpc_clnt across the TLS connect_worker 2026-07-08 14:07:19 -04:00
debug.h sunrpc: Fix compilation error (make W=1) when dprintk() is no-op 2026-03-29 21:25:09 -04:00
gss_api.h
gss_err.h
gss_krb5.h SUNRPC: Remove dead rpcsec_gss_krb5 definitions 2026-06-09 16:32:59 -04:00
metrics.h
msg_prot.h sunrpc: fix handling of unknown auth status codes 2025-07-14 12:46:47 -04:00
rdma_rn.h
rpc_pipe_fs.h
rpc_rdma_cid.h
rpc_rdma.h
sched.h sunrpc: Fix compilation error (make W=1) when dprintk() is no-op 2026-03-29 21:25:09 -04:00
stats.h
svc_rdma_pcl.h
svc_rdma.h svcrdma: wake sq waiters when the transport closes 2026-06-09 16:32:59 -04:00
svc_xprt.h SUNRPC: Move the svc_rpcb_cleanup() call sites 2025-09-23 13:28:19 -04:00
svc.h NFSD 7.1 Release Notes 2026-04-20 10:44:02 -07:00
svcauth_gss.h
svcauth.h sunrpc: remove SVC_SYSERR 2025-07-14 12:46:48 -04:00
svcsock.h sunrpc: allow svc_recv() to return -ETIMEDOUT and -EBUSY 2026-01-28 10:15:42 -05:00
timer.h
types.h
xdr.h SUNRPC: Return an error from xdr_buf_to_bvec() on overflow 2026-06-09 16:32:59 -04:00
xprt.h xprtrdma: Close lost-wakeup race in xprt_rdma_alloc_slot 2026-04-13 11:56:20 -07:00
xprtmultipath.h
xprtrdma.h
xprtsock.h