Re: [PATCH net v4 1/2] tls: restrict device-down TX fallback to hardware TX sockets

From: Rishikesh Jethwani

Date: Mon Nov 16 2026 - 13:34:52 EST


On Sun, Nov 15, 2026 at 4:02 PM <netdev-bot+sashiko@xxxxxxxxxx> 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 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().
>
> > diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c
> > index 4cdfbb57ecff0..a42cbc89db2bb 100644
> > --- a/net/tls/tls_device.c
> > +++ b/net/tls/tls_device.c
>
> [ ... ]
>
> > @@ -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.

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

> >
> > /* 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