tls: restrict device-down TX fallback to hardware TX sockets
Subsystems (4)
PATCH v4 1/2 tls: restrict device-down TX fallback to hardware TX sockets
tls_device_down() installs the hardware TX fallback on RX-only and software-TX sockets. The fallback expects a hardware context in priv_ctx_tx, so an RX-only socket crashes on its next transmit:
BUG: kernel NULL pointer dereference, address: 0000000000000008 tls_sw_fallback+0x11b/0x450 validate_xmit_skb.isra.0+0x14f/0x420 __dev_queue_xmit+0x5e8/0xf30
Switch only tls_validate_xmit_skb() to tls_validate_xmit_skb_sw(), and use the cmpxchg result to decide whether to delete driver TX state. Publish the hardware validator before releasing device_offload_lock.
Driver deletion cannot rely on tx_conf or rx_conf: they are published after setup releases the lock, so device-down can miss a completed attachment and leak driver state. Track RX state with TLS_RX_DEV_CLOSED:
set it at context creation and after deletion, and clear it after a successful driver add under device_offload_lock. This avoids deleting absent RX state and reading configuration bitfields while their shared byte is updated.
Found by code inspection. The reproduced RX-only crash is fixed: the socket survives interface down/up and continues receiving TLS records. Tested on Mellanox ConnectX-6 Dx (Crypto Enabled).
--- include/net/tls.h | 8 ++++---- net/tls/tls_device.c | 19 ++++++++++++------- net/tls/tls_main.c | 1 + 3 files changed, 17 insertions(+), 11 deletions(-)
include/net/tls.h +4 -4
@@ -187,10 +187,10 @@ enum tls_context_flags {* to be atomic. */ TLS_TX_SYNC_SCHED =1 , -/* tls_dev_del was called for the RX side, device state was released, -* but tls_ctx->netdev might still be kept, because TX-side driver -* resources might not be released yet. Used to prevent the second -* tls_dev_del call in tls_device_down if it happens simultaneously. +/* No RX device state is installed. Set at context creation and after +* RX tls_dev_del, cleared after a successful RX tls_dev_add. Prevents +* deleting absent or already released RX state while ctx->netdev may +* still be kept for TX. */ TLS_RX_DEV_CLOSED = 2, };
net/tls/tls_device.c +12 -7
@@ -1195,13 +1195,13 @@ int tls_set_device_offload(struct sock *sk) goto release_lock;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 ;
@@ -1282,6 +1282,7 @@ int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx) goto free_sw_resources;tls_device_attach (ctx, sk, netdev); +clear_bit (TLS_RX_DEV_CLOSED , &ctx->flags);up_read (&device_offload_lock);dev_put (netdev);
@@ -1314,12 +1315,11 @@ void tls_device_offload_cleanup_rx(struct sock *sk) netdev->tlsdev_ops->tls_dev_del (netdev, tls_ctx,TLS_OFFLOAD_CTX_DIR_RX ); +set_bit (TLS_RX_DEV_CLOSED , &tls_ctx->flags);if (tls_ctx->tx_conf !=TLS_HW ) {dev_put (netdev);rcu_assign_pointer (tls_ctx->netdev,NULL ); - }else { -set_bit (TLS_RX_DEV_CLOSED , &tls_ctx->flags); } out:up_read (&device_offload_lock);
@@ -1350,10 +1350,14 @@ static int tls_device_down(struct net_device *netdev)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. */ -WRITE_ONCE (ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw); + 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.
@@ -1370,13 +1374,14 @@ static int tls_device_down(struct net_device *netdev)synchronize_net ();/* Release the offload context on the driver side. */ -if (ctx->tx_conf ==TLS_HW ) +if (tx_hw) netdev->tlsdev_ops->tls_dev_del (netdev, ctx,TLS_OFFLOAD_CTX_DIR_TX ); -if (ctx->rx_conf ==TLS_HW && - !test_bit (TLS_RX_DEV_CLOSED , &ctx->flags)) +if (!test_bit (TLS_RX_DEV_CLOSED , &ctx->flags)) { netdev->tlsdev_ops->tls_dev_del (netdev, ctx,TLS_OFFLOAD_CTX_DIR_RX ); +set_bit (TLS_RX_DEV_CLOSED , &ctx->flags); + }dev_put (netdev);
net/tls/tls_main.c +1 -0
@@ -935,6 +935,7 @@ struct tls_context *tls_ctx_create(struct sock *sk)return NULL ;mutex_init (&ctx->tx_lock); +set_bit (TLS_RX_DEV_CLOSED , &ctx->flags); ctx->sk_proto =READ_ONCE (sk->sk_prot); ctx->sk = sk;/* Release semantic of rcu_assign_pointer() ensures that
Signature · 1 line
2.50.1
PATCH v4 2/2 tls: fix socket use-after-free during device-down
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).
--- include/net/tls.h | 2 ++ net/tls/tls_device.c | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-)
include/net/tls.h +2 -0
@@ -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 {
net/tls/tls_device.c +12 -10
@@ -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)) {
Signature · 1 line
2.50.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] In tls_device_down() (net/tls/tls_device.c), this patch decides whether to call tls_dev_del(TX) from the result of…
--
tls: restrict device-down TX fallback to hardware TX sockets
This commit changes tls_device_down() so the TX validator is switched to the software fallback only for sockets that use hardware TX. The cmpxchg result now decides whether the driver TX state is deleted. RX driver state is tracked with TLS_RX_DEV_CLOSED, which is set at context creation and cleared after a successful RX tls_dev_add().
Quoted 4 lines
net/tls/tls_device.c +0
-0
[ ... ]
net/tls/tls_device.c static int tls_device_down(struct net_device *netdev) · line 1360
Quoted 13 lines
net/tls/tls_device.c +5
-1@@ -1350,10 +1350,14 @@ static int tls_device_down(struct net_device *netdev)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. */ -WRITE_ONCE (ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw); + tx_hw =cmpxchg (&ctx->sk->sk_validate_xmit_skb, + tls_validate_xmit_skb, + tls_validate_xmit_skb_sw) == tls_validate_xmit_skb;
[Severity: High]
Is ctx->sk still guaranteed to be allocated when this cmpxchg runs?
The cmpxchg is in the second loop, after tls_device_lock has been dropped. The refcount_inc_not_zero() in the first loop pins only the tls_context, not the socket.
The final reference on a hardware TX socket can be dropped from skb freeing, and that path does not take device_offload_lock. The socket destructor then sees the extra ctx reference held by tls_device_down() and returns early:
__sk_destruct()
tls_device_sk_destruct()
tls_device_queue_ctx_destruction()
spin_lock_irqsave(&tls_device_lock, flags);
if (unlikely(!refcount_dec_and_test(&ctx->refcount))) {
spin_unlock_irqrestore(&tls_device_lock, flags);
return;
}The ctx survives, but the sock is freed. ctx->sk is left dangling. Each earlier context in the list adds a synchronize_net() before this one is reached, so the window can be fairly wide.
If that happens, can the cmpxchg read and write freed or reused socket memory? There seem to be two bad outcomes:
If the compare fails, tx_hw is false and tls_dev_del() is never called for TLS_OFFLOAD_CTX_DIR_TX. ctx->netdev has already been set to NULL, and the final refcount_dec_and_test() in tls_device_down() goes to tls_device_free_ctx(), which never calls tls_dev_del(). Would this leak the driver TX offload state?
If the memory has been reused by another TLS socket whose validator is tls_validate_xmit_skb, the cmpxchg would switch that unrelated socket to the software fallback.
The unconditional WRITE_ONCE() through ctx->sk in the second loop was already a use-after-free before this patch, so that part is pre-existing. What this patch adds is that driver TX teardown now depends on that unprotected access, where before it used the pinned ctx->tx_conf.
The next patch in this series, "tls: fix socket use-after-free during device-down", seems to address this. It moves the cmpxchg into the first loop under tls_device_lock, records the result in TLS_TX_DEV_DOWN, and tests that flag in the second loop. Would it make sense to fold that fix into this patch, or reorder the two? Then this commit would not add the dependency on its own, which matters since it is marked for stable.
Quoted 11 lines
net/tls/tls_device.c +1
-1/* Stop the RX and TX resync. * tls_dev_resync must not be called after tls_dev_del. @@ -1370,13 +1374,14 @@ static int tls_device_down(struct net_device *netdev)synchronize_net ();/* Release the offload context on the driver side. */ -if (ctx->tx_conf ==TLS_HW ) +if (tx_hw) netdev->tlsdev_ops->tls_dev_del (netdev, ctx,TLS_OFFLOAD_CTX_DIR_TX );
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261111232930.2532497-1-rjethwani%40purestorage.com
Quoted 89 lines · On Sun, Nov 15, 2026 at 4:02 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] In
tls_device_down()(net/tls/tls_device.c), this patch decides whether to calltls_dev_del(TX)from the result of…--
tls: restrict device-down TX fallback to hardware TX sockets
This commit changes
tls_device_down()so the TX validator is switched to the software fallback only for sockets that use hardware TX. The cmpxchg result now decides whether the driver TX state is deleted. RX driver state is tracked withTLS_RX_DEV_CLOSED, which is set at context creation and cleared after a successful RXtls_dev_add().Quoted 4 lines
net/tls/tls_device.c +0
-0[ ... ]
net/tls/tls_device.c static int tls_device_down(struct net_device *netdev) · line 1360
Quoted 13 lines
net/tls/tls_device.c +5
-1@@ -1350,10 +1350,14 @@ static int tls_device_down(struct net_device *netdev)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. */ -WRITE_ONCE (ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw); + tx_hw =cmpxchg (&ctx->sk->sk_validate_xmit_skb, + tls_validate_xmit_skb, + tls_validate_xmit_skb_sw) == tls_validate_xmit_skb;[Severity: High]
Isctx->skstill guaranteed to be allocated when this cmpxchg runs?The cmpxchg is in the second loop, after
tls_device_lockhas been dropped. Therefcount_inc_not_zero()in the first loop pins only thetls_context, not the socket.The final reference on a hardware TX socket can be dropped from skb freeing, and that path does not take
device_offload_lock. The socket destructor then sees the extra ctx reference held bytls_device_down()and returns early:
__sk_destruct()
tls_device_sk_destruct()tls_device_queue_ctx_destruction() spin_lock_irqsave(&tls_device_lock, flags); if (unlikely(!refcount_dec_and_test(&ctx->refcount))) { spin_unlock_irqrestore(&tls_device_lock, flags); return; }The ctx survives, but the sock is freed.
ctx->skis left dangling. Each earlier context in the list adds asynchronize_net()before this one is reached, so the window can be fairly wide.If that happens, can the cmpxchg read and write freed or reused socket memory? There seem to be two bad outcomes:
If the compare fails,
tx_hwis false andtls_dev_del()is never called forTLS_OFFLOAD_CTX_DIR_TX.ctx->netdevhas already been set to NULL, and the finalrefcount_dec_and_test()intls_device_down()goes totls_device_free_ctx(), which never callstls_dev_del(). Would this leak the driver TX offload state?If the memory has been reused by another TLS socket whose validator is
tls_validate_xmit_skb, the cmpxchg would switch that unrelated socket to the software fallback.The unconditional
WRITE_ONCE()throughctx->skin the second loop was already a use-after-free before this patch, so that part is pre-existing. What this patch adds is that driver TX teardown now depends on that unprotected access, where before it used the pinnedctx->tx_conf.
Quoted 6 lines
The next patch in this series, "tls: fix socket use-after-free during device-down", seems to address this. It moves the cmpxchg into the first loop under
tls_device_lock, records the result inTLS_TX_DEV_DOWN, and tests that flag in the second loop. Would it make sense to fold that fix into this patch, or reorder the two? Then this commit would not add the dependency on its own, which matters since it is marked for stable.
Thanks, agreed. I'll fix in v5 by reordering instead of folding: the UAF fix now comes first and moves the validator switch under tls_device_lock. The restrict patch then turns it into the cmpxchg under that same lock, with TLS_TX_DEV_DOWN recording the result. No commit dereferences ctx->sk outside the lock. I'll keep them as two patches because the fixes have different Fixes tags and backport prerequisites. The final code will be unchanged from v4.
pw-bot: cr
Quoted 16 lines
net/tls/tls_device.c static int tls_device_down(struct net_device *netdev) · line 1379
Quoted 11 lines
net/tls/tls_device.c +1
-1/* Stop the RX and TX resync. * tls_dev_resync must not be called after tls_dev_del. @@ -1370,13 +1374,14 @@ static int tls_device_down(struct net_device *netdev)synchronize_net ();/* Release the offload context on the driver side. */ -if (ctx->tx_conf ==TLS_HW ) +if (tx_hw) netdev->tlsdev_ops->tls_dev_del (netdev, ctx,TLS_OFFLOAD_CTX_DIR_TX );[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261111232930.2532497-1-rjethwani%40purestorage.com