tls_device_down() takes a reference on each TLS context, then drops tls_device_lock before setting ctx->sk->sk_validate_xmit_skb to tls_validate_xmit_skb_sw(). The socket can be freed in between: tls_device_sk_destruct() calls tls_device_queue_ctx_destruction(), which drops the socket's context reference but does not free the context because device-down still holds one, and __sk_destruct() then frees the socket. Set sk_validate_xmit_skb while walking tls_device_list under tls_device_lock. tls_device_queue_ctx_destruction() takes the same lock to drop its reference and remove the context from the list, so the socket cannot be freed before the store completes. Checking sk_refcnt instead would not work: it can already be zero while queued SKBs still hold sk_wmem_alloc and need the software fallback. 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 # needs commit f08d8c1bb97c ("net/tls: Fix race in TLS device down flow") Signed-off-by: Rishikesh Jethwani --- net/tls/tls_device.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c index fa7170cd4fd1..3ea764bdc949 100644 --- a/net/tls/tls_device.c +++ b/net/tls/tls_device.c @@ -1345,6 +1345,11 @@ 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. + */ + WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw); + list_move(&ctx->list, &list); } spin_unlock_irqrestore(&tls_device_lock, flags); @@ -1352,11 +1357,6 @@ static int tls_device_down(struct net_device *netdev) list_for_each_entry_safe(ctx, tmp, &list, list) { bool free_ctx; - /* 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); - /* Stop the RX and TX resync. * tls_dev_resync must not be called after tls_dev_del. */ -- 2.50.1