From: Anant Garg <anantxgarg@gmail.com>
To: netdev@vger.kernel.org
Cc: John Fastabend <john.fastabend@gmail.com>,
Jakub Kicinski <kuba@kernel.org>,
Sabrina Dubroca <sd@queasysnail.net>,
"David S . Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>,
Simon Horman <horms@kernel.org>,
Anant Garg <anantxgarg@gmail.com>
Subject: [PATCH net] net/tls: publish offload state inside device_offload_lock
Date: Sat, 28 Nov 2026 14:11:37 +0530 [thread overview]
Message-ID: <20261128084137.94590-1-anantxgarg@gmail.com> (raw)
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
next reply other threads:[~2026-11-28 8:41 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 8:41 Anant Garg [this message]
2026-11-28 8:43 ` [PATCH net] net/tls: publish offload state inside device_offload_lock netdev-bot+sinfo
2026-11-28 8:58 ` Anant Garg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261128084137.94590-1-anantxgarg@gmail.com \
--to=anantxgarg@gmail.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sd@queasysnail.net \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox