From: Rishikesh Jethwani <rjethwani@purestorage.com>
To: netdev@vger.kernel.org
Cc: Rishikesh Jethwani <rjethwani@purestorage.com>,
stable@vger.kernel.org, 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>, Simon Horman <horms@kernel.org>,
Tariq Toukan <tariqt@nvidia.com>,
Alice Mikityanska <alice.kernel@fastmail.im>,
linux-kernel@vger.kernel.org
Subject: [PATCH net v4 2/2] tls: fix socket use-after-free during device-down
Date: Wed, 11 Nov 2026 16:29:29 -0700 [thread overview]
Message-ID: <20261111232930.2532497-2-rjethwani@purestorage.com> (raw)
In-Reply-To: <20261111232930.2532497-1-rjethwani@purestorage.com>
tls_device_down() pins each TLS context, then drops tls_device_lock before
switching ctx->sk's TX validator. The socket destructor can run between
these steps: the context survives, but __sk_destruct() frees the socket.
Switch the validator while selecting contexts from tls_device_list under
tls_device_lock. The destructor uses the same lock for its context put
and list removal, so ctx->sk remains allocated through the switch. Record
the result in TLS_TX_DEV_DOWN for the later driver cleanup. Contexts never
return to the active list, so the bit needs no reset.
Checking sk_refcnt would miss sockets whose queued SKBs still hold
sk_wmem_alloc and require fallback after sk_refcnt reaches zero.
Requires commit f08d8c1bb97c ("net/tls: Fix race in TLS device down flow"),
which moved the destructor's context put under tls_device_lock. Include
this prerequisite in older backports.
Found by code inspection. An ASan harness running the extracted teardown
functions with userspace shims reproduces the UAF before this fix and
passes afterward. Tested on Mellanox ConnectX-6 Dx (Crypto Enabled).
Fixes: c55dcdd435aa ("net/tls: Fix use-after-free after the TLS device goes down and up")
Cc: stable@vger.kernel.org
Signed-off-by: Rishikesh Jethwani <rjethwani@purestorage.com>
---
include/net/tls.h | 2 ++
net/tls/tls_device.c | 22 ++++++++++++----------
2 files changed, 14 insertions(+), 10 deletions(-)
diff --git a/include/net/tls.h b/include/net/tls.h
index 915f39bddc83..24928c1243d1 100644
--- a/include/net/tls.h
+++ b/include/net/tls.h
@@ -193,6 +193,8 @@ enum tls_context_flags {
* still be kept for TX.
*/
TLS_RX_DEV_CLOSED = 2,
+ /* Records the TX fallback switch for device-down's cleanup loop. */
+ TLS_TX_DEV_DOWN = 3,
};
struct cipher_context {
diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c
index a42cbc89db2b..ae434c304151 100644
--- a/net/tls/tls_device.c
+++ b/net/tls/tls_device.c
@@ -1345,20 +1345,22 @@ static int tls_device_down(struct net_device *netdev)
!refcount_inc_not_zero(&ctx->refcount))
continue;
+ /* Stop offloaded TX and switch to the fallback.
+ * tls_is_skb_tx_device_offloaded will return false.
+ *
+ * The context reference pins ctx; tls_device_lock keeps ctx->sk
+ * allocated until the validator has been switched.
+ */
+ if (cmpxchg(&ctx->sk->sk_validate_xmit_skb,
+ tls_validate_xmit_skb,
+ tls_validate_xmit_skb_sw) == tls_validate_xmit_skb)
+ set_bit(TLS_TX_DEV_DOWN, &ctx->flags);
+
list_move(&ctx->list, &list);
}
spin_unlock_irqrestore(&tls_device_lock, flags);
list_for_each_entry_safe(ctx, tmp, &list, list) {
- bool tx_hw;
-
- /* Stop offloaded TX and switch to the fallback.
- * tls_is_skb_tx_device_offloaded will return false.
- */
- tx_hw = cmpxchg(&ctx->sk->sk_validate_xmit_skb,
- tls_validate_xmit_skb,
- tls_validate_xmit_skb_sw) == tls_validate_xmit_skb;
-
/* Stop the RX and TX resync.
* tls_dev_resync must not be called after tls_dev_del.
*/
@@ -1374,7 +1376,7 @@ static int tls_device_down(struct net_device *netdev)
synchronize_net();
/* Release the offload context on the driver side. */
- if (tx_hw)
+ if (test_bit(TLS_TX_DEV_DOWN, &ctx->flags))
netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
TLS_OFFLOAD_CTX_DIR_TX);
if (!test_bit(TLS_RX_DEV_CLOSED, &ctx->flags)) {
--
2.50.1
next prev parent reply other threads:[~2026-11-11 23:29 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 23:29 [PATCH net v4 1/2] tls: restrict device-down TX fallback to hardware TX sockets Rishikesh Jethwani
2026-11-11 23:29 ` Rishikesh Jethwani [this message]
2026-11-16 0:02 ` netdev-bot+sashiko
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=20261111232930.2532497-2-rjethwani@purestorage.com \
--to=rjethwani@purestorage.com \
--cc=alice.kernel@fastmail.im \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sd@queasysnail.net \
--cc=stable@vger.kernel.org \
--cc=tariqt@nvidia.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
all inboxes | Powered by JetHome®