All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] sunrpc: pin svc_xprt across the asynchronous TLS handshake callback
@ 2026-05-22 13:39 Chuck Lever
  2026-05-22 13:39 ` [PATCH v2 2/2] sunrpc: wait for in-flight TLS handshake callback when cancel loses race Chuck Lever
  0 siblings, 1 reply; 3+ messages in thread
From: Chuck Lever @ 2026-05-22 13:39 UTC (permalink / raw)
  To: NeilBrown, Jeff Layton, Olga Kornievskaia, Dai Ngo, Tom Talpey
  Cc: linux-nfs, Chris Mason

From: Chris Mason <clm@meta.com>

svc_tcp_handshake() stores the raw svc_xprt pointer in
tls_handshake_args.ta_data and submits the request through
tls_server_hello_x509(). The handshake core takes only
sock_hold(req->hr_sk); nothing references the embedding struct
svc_sock that svc_tcp_handshake_done() reaches via container_of().

Two close races leave the in-flight callback writing through a freed
svc_sock. svc_sock_free() calls tls_handshake_cancel() and discards
its return value: a false return means handshake_complete() has
already set HANDSHAKE_F_REQ_COMPLETED but hp_done() may not have
finished, yet svc_sock_free() proceeds to kfree(svsk). The
cancel-loser fall-through inside svc_tcp_handshake() itself produces
the same window: when wait_for_completion_interruptible_timeout()
returns <= 0 (timeout or signal) and tls_handshake_cancel() returns
false, the function does not drain, returns, and svc_handle_xprt()
calls svc_xprt_received(), which clears XPT_BUSY and can drop the
last reference. A concurrent close then runs svc_sock_free() while
svc_tcp_handshake_done() is still updating xpt_flags and walking
svsk->sk_handshake_done.

The corruption surfaces as set_bit/clear_bit RMW into the freed
xpt_flags slab slot and as complete_all() walking and writing the
freed wait_queue_head_t list embedded in sk_handshake_done -- a
slab-corruption primitive, not a benign read. The path is reachable
on any TLS-enabled NFS server whenever a connection close overlaps
the tlshd downcall delivery window; the interruptible wait means
signal delivery suffices, not just SVC_HANDSHAKE_TO expiry.

Take svc_xprt_get(xprt) immediately before tls_server_hello_x509()
so the in-flight callback owns its own reference. Release it on the
two edges where the callback is guaranteed not to fire -- submission
failure from tls_server_hello_x509() and a successful
tls_handshake_cancel() -- and at the tail of
svc_tcp_handshake_done() after complete_all().

Fixes: b3cbf98e2fdf ("SUNRPC: Support TLS handshake in the server-side TCP socket code")
Assisted-by: kres (claude-opus-4-7)
Signed-off-by: Chris Mason <clm@meta.com>
[cel: rewrote commit message to describe the actual change]
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---
 net/sunrpc/svcsock.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
index 7be3de1a1aed..c8e194fce622 100644
--- a/net/sunrpc/svcsock.c
+++ b/net/sunrpc/svcsock.c
@@ -471,6 +471,7 @@ static void svc_tcp_handshake_done(void *data, int status, key_serial_t peerid)
 	}
 	clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
 	complete_all(&svsk->sk_handshake_done);
+	svc_xprt_put(xprt);
 }
 
 /**
@@ -494,9 +495,13 @@ static void svc_tcp_handshake(struct svc_xprt *xprt)
 	clear_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
 	init_completion(&svsk->sk_handshake_done);
 
+	/* Pin the transport across the asynchronous handshake callback. */
+	svc_xprt_get(xprt);
+
 	ret = tls_server_hello_x509(&args, GFP_KERNEL);
 	if (ret) {
 		trace_svc_tls_not_started(xprt);
+		svc_xprt_put(xprt);
 		goto out_failed;
 	}
 
@@ -505,6 +510,7 @@ static void svc_tcp_handshake(struct svc_xprt *xprt)
 	if (ret <= 0) {
 		if (tls_handshake_cancel(sk)) {
 			trace_svc_tls_timed_out(xprt);
+			svc_xprt_put(xprt);
 			goto out_close;
 		}
 	}
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] sunrpc: wait for in-flight TLS handshake callback when cancel loses race
  2026-05-22 13:39 [PATCH v2 1/2] sunrpc: pin svc_xprt across the asynchronous TLS handshake callback Chuck Lever
@ 2026-05-22 13:39 ` Chuck Lever
  2026-05-22 13:45   ` Jeff Layton
  0 siblings, 1 reply; 3+ messages in thread
From: Chuck Lever @ 2026-05-22 13:39 UTC (permalink / raw)
  To: NeilBrown, Jeff Layton, Olga Kornievskaia, Dai Ngo, Tom Talpey
  Cc: linux-nfs, Chuck Lever

From: Chuck Lever <chuck.lever@oracle.com>

When wait_for_completion_interruptible_timeout() in
svc_tcp_handshake() returns 0 (timeout) or -ERESTARTSYS (signal) and
tls_handshake_cancel() then returns false, handshake_complete() has
won the cancellation race: it has set HANDSHAKE_F_REQ_COMPLETED and
is about to invoke svc_tcp_handshake_done(), but the callback's
side effects on xpt_flags and on svsk->sk_handshake_done have not
yet committed.

The current code reads xpt_flags immediately to decide whether the
session succeeded. Two races result.

If the callback has executed set_bit(XPT_TLS_SESSION) but not yet
clear_bit(XPT_HANDSHAKE), svc_tcp_handshake() sees a session,
enqueues the transport, and returns. svc_xprt_received() then
clears XPT_BUSY, a worker thread picks the transport up, the
dispatcher in svc_handle_xprt() observes XPT_HANDSHAKE still set,
and xpo_handshake is invoked a second time. That svc_tcp_handshake()
calls init_completion(&svsk->sk_handshake_done) while the original
callback concurrently calls complete_all() on it, corrupting the
embedded swait_queue.

If the callback has set HANDSHAKE_F_REQ_COMPLETED but not yet
entered svc_tcp_handshake_done(), svc_tcp_handshake() reads
XPT_TLS_SESSION as clear and tears the connection down even though
the handshake is about to succeed.

Wait for the callback to commit before inspecting xpt_flags. The
completion is guaranteed to fire because handshake_complete()
invokes svc_tcp_handshake_done() unconditionally once it has set
HANDSHAKE_F_REQ_COMPLETED.

Fixes: b3cbf98e2fdf ("SUNRPC: Support TLS handshake in the server-side TCP socket code")
Closes: https://sashiko.dev/#/patchset/20260522014850.206768-1-cel%40kernel.org
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---
 net/sunrpc/svcsock.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
index c8e194fce622..eb747493db82 100644
--- a/net/sunrpc/svcsock.c
+++ b/net/sunrpc/svcsock.c
@@ -513,6 +513,10 @@ static void svc_tcp_handshake(struct svc_xprt *xprt)
 			svc_xprt_put(xprt);
 			goto out_close;
 		}
+		/* Cancellation lost to handshake_complete(): the
+		 * callback is in flight and should finish quickly.
+		 */
+		wait_for_completion(&svsk->sk_handshake_done);
 	}
 
 	if (!test_bit(XPT_TLS_SESSION, &xprt->xpt_flags)) {
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 2/2] sunrpc: wait for in-flight TLS handshake callback when cancel loses race
  2026-05-22 13:39 ` [PATCH v2 2/2] sunrpc: wait for in-flight TLS handshake callback when cancel loses race Chuck Lever
@ 2026-05-22 13:45   ` Jeff Layton
  0 siblings, 0 replies; 3+ messages in thread
From: Jeff Layton @ 2026-05-22 13:45 UTC (permalink / raw)
  To: Chuck Lever, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey
  Cc: linux-nfs, Chuck Lever

On Fri, 2026-05-22 at 09:39 -0400, Chuck Lever wrote:
> From: Chuck Lever <chuck.lever@oracle.com>
> 
> When wait_for_completion_interruptible_timeout() in
> svc_tcp_handshake() returns 0 (timeout) or -ERESTARTSYS (signal) and
> tls_handshake_cancel() then returns false, handshake_complete() has
> won the cancellation race: it has set HANDSHAKE_F_REQ_COMPLETED and
> is about to invoke svc_tcp_handshake_done(), but the callback's
> side effects on xpt_flags and on svsk->sk_handshake_done have not
> yet committed.
> 
> The current code reads xpt_flags immediately to decide whether the
> session succeeded. Two races result.
> 
> If the callback has executed set_bit(XPT_TLS_SESSION) but not yet
> clear_bit(XPT_HANDSHAKE), svc_tcp_handshake() sees a session,
> enqueues the transport, and returns. svc_xprt_received() then
> clears XPT_BUSY, a worker thread picks the transport up, the
> dispatcher in svc_handle_xprt() observes XPT_HANDSHAKE still set,
> and xpo_handshake is invoked a second time. That svc_tcp_handshake()
> calls init_completion(&svsk->sk_handshake_done) while the original
> callback concurrently calls complete_all() on it, corrupting the
> embedded swait_queue.
> 
> If the callback has set HANDSHAKE_F_REQ_COMPLETED but not yet
> entered svc_tcp_handshake_done(), svc_tcp_handshake() reads
> XPT_TLS_SESSION as clear and tears the connection down even though
> the handshake is about to succeed.
> 
> Wait for the callback to commit before inspecting xpt_flags. The
> completion is guaranteed to fire because handshake_complete()
> invokes svc_tcp_handshake_done() unconditionally once it has set
> HANDSHAKE_F_REQ_COMPLETED.
> 
> Fixes: b3cbf98e2fdf ("SUNRPC: Support TLS handshake in the server-side TCP socket code")
> Closes: https://sashiko.dev/#/patchset/20260522014850.206768-1-cel%40kernel.org
> Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
> ---
>  net/sunrpc/svcsock.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
> index c8e194fce622..eb747493db82 100644
> --- a/net/sunrpc/svcsock.c
> +++ b/net/sunrpc/svcsock.c
> @@ -513,6 +513,10 @@ static void svc_tcp_handshake(struct svc_xprt *xprt)
>  			svc_xprt_put(xprt);
>  			goto out_close;
>  		}
> +		/* Cancellation lost to handshake_complete(): the
> +		 * callback is in flight and should finish quickly.
> +		 */
> +		wait_for_completion(&svsk->sk_handshake_done);
>  	}
>  
>  	if (!test_bit(XPT_TLS_SESSION, &xprt->xpt_flags)) {

Reviewed-by: Jeff Layton <jlayton@kernel.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-05-22 13:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-22 13:39 [PATCH v2 1/2] sunrpc: pin svc_xprt across the asynchronous TLS handshake callback Chuck Lever
2026-05-22 13:39 ` [PATCH v2 2/2] sunrpc: wait for in-flight TLS handshake callback when cancel loses race Chuck Lever
2026-05-22 13:45   ` Jeff Layton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.