linux/net/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
..
auth_gss SUNRPC: Remove dead rpcsec_gss_krb5 definitions 2026-06-09 16:32:59 -04:00
xprtrdma NFS Client Updates for Linux 7.2 2026-06-23 18:36:41 -07:00
addr.c net: sunrpc: Fix an off by one in rpc_sockaddr2uaddr() 2024-02-28 16:18:18 -05:00
auth_null.c
auth_tls.c SUNRPC: Fail quickly when server does not recognize TLS 2023-09-27 15:16:40 -04:00
auth_unix.c treewide: Replace kmalloc with kmalloc_obj for non-scalar types 2026-02-21 01:02:28 -08:00
auth.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
backchannel_rqst.c treewide: Replace kmalloc with kmalloc_obj for non-scalar types 2026-02-21 01:02:28 -08:00
cache.c sunrpc: add a generic netlink family for cache upcalls 2026-06-01 11:08:18 -04:00
clnt.c SUNRPC: pin upper rpc_clnt across the TLS connect_worker 2026-07-08 14:07:19 -04:00
debugfs.c sunrpc: add netns inum and srcaddr to debugfs rpc_xprt info 2025-01-22 15:53:31 -05:00
fail.h
Kconfig SUNRPC: Remove redundant crypto Kconfig dependencies 2026-06-09 16:32:59 -04:00
Makefile sunrpc: add a generic netlink family for cache upcalls 2026-06-01 11:08:18 -04:00
netlink.c NFSD: Put cache get-reqs dump attrs under reply 2026-06-01 11:08:18 -04:00
netlink.h sunrpc: add SUNRPC_CMD_CACHE_FLUSH netlink command 2026-06-01 11:08:18 -04:00
netns.h
rpc_pipe.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
rpcb_clnt.c treewide: Replace kmalloc with kmalloc_obj for non-scalar types 2026-02-21 01:02:28 -08:00
sched.c sunrpc: unexport rpc_malloc() and rpc_free() 2025-09-30 16:04:03 -04:00
socklib.c SUNRPC: Remove redundant __GFP_NOWARN 2025-09-23 13:29:50 -04:00
socklib.h
stats.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
sunrpc_syms.c sunrpc: add a generic netlink family for cache upcalls 2026-06-01 11:08:18 -04:00
sunrpc.h SUNRPC: make various functions static, or not exported. 2024-09-01 10:04:56 -04:00
svc_xprt.c sunrpc: skip svc_xprt_enqueue when transport is busy 2026-06-01 11:08:18 -04:00
svc.c sunrpc: harden rq_procinfo lifecycle to prevent double-free 2026-06-09 16:32:59 -04:00
svcauth_unix.c sunrpc: add SUNRPC_CMD_CACHE_FLUSH netlink command 2026-06-01 11:08:18 -04:00
svcauth.c SUNRPC: add svcauth_map_clnt_to_svc_cred_local 2024-09-23 15:03:30 -04:00
svcsock.c sunrpc: wait for in-flight TLS handshake callback when cancel loses race 2026-06-09 16:32:59 -04:00
sysctl.c sysctl: treewide: constify the ctl_table argument of proc_handlers 2024-07-24 20:59:29 +02:00
sysfs.c sunrpc: fix uninitialized xprt_create_args structure 2026-06-08 12:06:41 -04:00
sysfs.h NFS: Add sysfs links to sunrpc clients for nfs_clients 2023-06-19 15:04:13 -04:00
timer.c
xdr.c SUNRPC: Return an error from xdr_buf_to_bvec() on overflow 2026-06-09 16:32:59 -04:00
xprt.c xprtrdma: Close lost-wakeup race in xprt_rdma_alloc_slot 2026-04-13 11:56:20 -07:00
xprtmultipath.c treewide: Replace kmalloc with kmalloc_obj for non-scalar types 2026-02-21 01:02:28 -08:00
xprtsock.c SUNRPC: pin upper rpc_clnt across the TLS connect_worker 2026-07-08 14:07:19 -04:00