Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net/tls: publish offload state inside device_offload_lock
@ 2026-11-28  8:41 Anant Garg
  2026-11-28  8:43 ` netdev-bot+sinfo
  0 siblings, 1 reply; 3+ messages in thread
From: Anant Garg @ 2026-11-28  8:41 UTC (permalink / raw)
  To: netdev
  Cc: John Fastabend, Jakub Kicinski, Sabrina Dubroca, David S . Miller,
	Eric Dumazet, Paolo Abeni, Willem de Bruijn, Simon Horman,
	Anant Garg

tls_set_device_offload() and tls_set_device_offload_rx() drop
device_offload_lock before do_tls_setsockopt_conf() sets ctx->tx_conf or
ctx->rx_conf to TLS_HW, and tls_set_device_offload() also installs
sk->sk_validate_xmit_skb after up_read(&device_offload_lock).

Because do_tls_setsockopt_conf() holds lock_sock(sk) without rtnl_lock()
and tls_device_down() holds rtnl_lock() without lock_sock(sk), a
concurrent NETDEV_DOWN can acquire down_write(&device_offload_lock) right
after up_read(&device_offload_lock) and before TLS_HW is recorded:

1. tls_device_down() sees ctx->tx_conf != TLS_HW and/or
   ctx->rx_conf != TLS_HW, skips netdev->tlsdev_ops->tls_dev_del(), and
   clears ctx->netdev = NULL. Subsequent socket teardown also skips
   tls_dev_del() once ctx->netdev is NULL, leaking driver offload state
   and leaving driver async work/completions (e.g. mlx5 RX resync/rule
   workers) holding dangling sock/context pointers (use-after-free).
2. For TX, tls_device_down() switches sk->sk_validate_xmit_skb to
   tls_validate_xmit_skb_sw, which tls_set_device_offload() can then
   overwrite back to tls_validate_xmit_skb.

Additionally, tls_device_down() unconditionally writes
tls_validate_xmit_skb_sw to ctx->sk->sk_validate_xmit_skb even when only
RX is offloaded (tx_conf != TLS_HW), causing subsequent outbound packets
to enter tls_sw_fallback() with priv_ctx_tx being NULL or
tls_sw_context_tx.

Set ctx->tx_conf / ctx->rx_conf to TLS_HW before tls_device_attach(),
install sk->sk_validate_xmit_skb before up_read(&device_offload_lock),
only assign TLS_SW in do_tls_setsockopt_conf() on software fallback, and
gate the sk_validate_xmit_skb_sw switch in tls_device_down() on
ctx->tx_conf == TLS_HW.

Compile-tested net/tls/ with W=1, CONFIG_KASAN=y, and
CONFIG_PROVE_LOCKING=y for both CONFIG_TLS_DEVICE=y and
CONFIG_TLS_DEVICE=n, and ran tools/testing/selftests/net/tls (965/965
passed under QEMU/KVM); hardware tlsdev_ops + NETDEV_DOWN race not
runtime-tested due to lack of physical NIC hardware.

Fixes: 4799ac81e52a ("tls: Add rx inline crypto offload")
Fixes: 3544c98acd09 ("net/tls: narrow down the critical area of device_offload_lock")
Fixes: c55dcdd435aa ("net/tls: Fix use-after-free after the TLS device goes down and up")
Cc: stable@vger.kernel.org
Signed-off-by: Anant Garg <anantxgarg@gmail.com>
---
 net/tls/tls_device.c | 10 +++++++---
 net/tls/tls_main.c   | 11 ++---------
 2 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c
index fa7170cd4fd..fa59a3ad3da 100644
--- a/net/tls/tls_device.c
+++ b/net/tls/tls_device.c
@@ -1194,14 +1194,15 @@ int tls_set_device_offload(struct sock *sk)
 	if (rc)
 		goto release_lock;
 
+	ctx->tx_conf = TLS_HW;
 	tls_device_attach(ctx, sk, netdev);
-	up_read(&device_offload_lock);
-
 	/* following this assignment tls_is_skb_tx_device_offloaded
 	 * will return true and the context might be accessed
 	 * by the netdev's xmit function.
 	 */
 	smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb);
+	up_read(&device_offload_lock);
+
 	dev_put(netdev);
 
 	return 0;
@@ -1281,6 +1282,7 @@ int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
 	if (rc)
 		goto free_sw_resources;
 
+	ctx->rx_conf = TLS_HW;
 	tls_device_attach(ctx, sk, netdev);
 	up_read(&device_offload_lock);
 
@@ -1355,7 +1357,9 @@ static int tls_device_down(struct net_device *netdev)
 		/* Stop offloaded TX and switch to the fallback.
 		 * tls_is_skb_tx_device_offloaded will return false.
 		 */
-		WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw);
+		if (ctx->tx_conf == TLS_HW)
+			WRITE_ONCE(ctx->sk->sk_validate_xmit_skb,
+				   tls_validate_xmit_skb_sw);
 
 		/* Stop the RX and TX resync.
 		 * tls_dev_resync must not be called after tls_dev_del.
diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
index d24e86e7f42..f0ee17acf62 100644
--- a/net/tls/tls_main.c
+++ b/net/tls/tls_main.c
@@ -629,7 +629,6 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
 	union tls_crypto_context tmp = {};
 	bool update = false;
 	int rc = 0;
-	int conf;
 
 	/* TLS and sockmap are mutually exclusive. A socket already in a
 	 * sockmap (i.e. with a psock attached) cannot be upgraded to TLS.
@@ -709,7 +708,6 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
 
 	if (tx) {
 		rc = tls_set_device_offload(sk);
-		conf = TLS_HW;
 		if (!rc) {
 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXDEVICE);
 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
@@ -725,11 +723,10 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
 				TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXSW);
 				TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
 			}
-			conf = TLS_SW;
+			ctx->tx_conf = TLS_SW;
 		}
 	} else {
 		rc = tls_set_device_offload_rx(sk, ctx);
-		conf = TLS_HW;
 		if (!rc) {
 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICE);
 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
@@ -745,16 +742,12 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
 				TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXSW);
 				TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
 			}
-			conf = TLS_SW;
+			ctx->rx_conf = TLS_SW;
 		}
 		if (!update)
 			tls_sw_strparser_arm(sk, ctx);
 	}
 
-	if (tx)
-		ctx->tx_conf = conf;
-	else
-		ctx->rx_conf = conf;
 	update_sk_prot(sk, ctx);
 
 	if (update)
-- 
2.43.0


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

end of thread, other threads:[~2026-11-28  9:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28  8:41 [PATCH net] net/tls: publish offload state inside device_offload_lock Anant Garg
2026-11-28  8:43 ` netdev-bot+sinfo
2026-11-28  8:58   ` Anant Garg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox