Netdev List
 help / color / mirror / Atom feed
From: Steffen Klassert <steffen.klassert@secunet.com>
To: <netdev-bot+sashiko@kernel.org>
Cc: <davem@davemloft.net>, <kuba@kernel.org>,
	<herbert@gondor.apana.org.au>, <netdev@vger.kernel.org>,
	Jianbo Liu <jianbol@nvidia.com>
Subject: Re: [PATCH 19/22] xfrm: Serialize device state teardown
Date: Thu, 3 Dec 2026 08:24:20 +0100	[thread overview]
Message-ID: <axEZJOdNvY7qosxB@gauss4> (raw)
In-Reply-To: <179621715233.3146.12822374373454454587@kernel.org>

Ccing the author of the patch.

On Wed, Dec 02, 2026 at 01:12:32PM +0000, netdev-bot+sashiko@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issue(s) to consider.
> 
> Critical: 0 · High: 1 · Medium: 1 · 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] The fix for a NULL device in xfrm_dev_direct_output() only
>   checks a local snapshot, so the NULL dereference is still reachable.
> - [Medium] xfrm_dev_state_flush() can return while a state on
>   xfrm_state_dev_gc_list still has xso->dev == dev, its driver SA and…
> 
> --
> 
> xfrm: Serialize device state teardown
> 
> This commit makes xfrm_dev_state_free() claim the device by clearing
> xso->dev under x->lock and xfrm_state_dev_gc_lock. With that, only one
> caller unlinks the state and drops the netdev reference. The device flush
> now takes a state reference before collecting states from the device GC
> list, and xfrm_dev_offload_ok() rejects offloaded states that have no
> device.
> 
> Link: https://lore.kernel.org/netdev/20260908224854.1591115-1-kuba@kernel.org/
> 
> > diff --git a/net/xfrm/xfrm_device.c b/net/xfrm/xfrm_device.c
> > index f153bf695b9dd..3bbb970cd337f 100644
> > --- a/net/xfrm/xfrm_device.c
> > +++ b/net/xfrm/xfrm_device.c
> > @@ -422,11 +422,11 @@ bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
> >  	int mtu;
> >  	struct dst_entry *dst = skb_dst(skb);
> >  	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
> > -	struct net_device *dev = x->xso.dev;
> > +	struct net_device *dev = READ_ONCE(x->xso.dev);
> >  	bool check_tunnel_size;
> >  
> >  	if (!x->type_offload ||
> > -	    (x->xso.type == XFRM_DEV_OFFLOAD_UNSPECIFIED && x->encap))
> > +	    (x->xso.type == XFRM_DEV_OFFLOAD_UNSPECIFIED ? !!x->encap : !dev))
> >  		return false;
> >  
> >  	if ((!dev || dev == xfrm_dst_path(dst)->dev) &&
> 
> [Severity: High]
> The commit message says this check stops xfrm_output() from using "the
> NULL device in xfrm_dev_direct_output()". Only the local snapshot of
> x->xso.dev is checked here, so is that window really closed?
> 
> Nothing that runs after this check receives the snapshot. Take a tunnel
> mode packet offload state with !skb->sk. xfrm_output() calls
> xfrm_dev_direct_output(), which runs the LOCAL_OUT hooks and then loads
> the pointer again:
> 
> net/xfrm/xfrm_output.c:xfrm_dev_direct_output() {
>     ...
> 	err = skb_dst(skb)->ops->local_out(net, sk, skb);
> 	if (unlikely(err != 1))
> 		return err;
>     ...
> 	skb->dev = x->xso.dev;
> 	__skb_push(skb, skb->dev->hard_header_len);
> 	return dev_queue_xmit(skb);
> }
> 
> The driver callback at the end of xfrm_dev_offload_ok() is also passed x
> rather than dev. For bonding, bond_ipsec_offload_ok()->bond_ipsec_dev()
> loads the pointer again and dereferences it without a NULL check. This
> happens for crypto offload as well:
> 
> drivers/net/bonding/bond_main.c:bond_ipsec_dev() {
> 	struct net_device *bond_dev = xs->xso.dev;
>     ...
> 	bond = netdev_priv(bond_dev);
> 	if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
> 		return NULL;
>     ...
> }
> 
> The bond_ipsec_dev() re-read predates this patch. It is the same NULL
> device use that the commit message says is handled.

Jianbo Liu already commented on this when the patch was posted
originally to the netdev list:

https://lore.kernel.org/ipsec/dba4f4d6-d710-4891-a0f6-5d7a3aae6c5e@nvidia.com/

Should be addressed with a separate fix.

> 
> Now that xso->type stays set after detach, can this sequence oops?
> 
> CPU1 (TX)                           CPU2 (NETDEV_DOWN/UNREGISTER)
> xfrm_dev_offload_ok()
>   dev = READ_ONCE(x->xso.dev)
>   /* non-NULL, check passes */
>                                     xfrm_dev_state_flush()
>                                       xfrm_dev_state_free()
>                                         WRITE_ONCE(xso->dev, NULL);
> xfrm_output()
>   xfrm_dev_direct_output()
>     skb->dev = x->xso.dev;   /* NULL */
>     skb->dev->hard_header_len
> 
> xfrm_dev_state_free() clears the pointer under x->lock and
> xfrm_state_dev_gc_lock. The TX path takes neither lock.
> 
> A later patch in this series, "xfrm: Wait for an RCU grace period before
> freeing device state", adds synchronize_rcu() before
> xfrm_dev_state_free() in the flush. That makes the window smaller but
> does not close it. xso->dev is cleared after the grace period rather than
> unpublished before it.
> 
> A TX user that holds a counted bundle dst taken before the delete can
> still enter xfrm_output() in a new RCU section after the grace period.
> Two examples:
> 
> - a forwarded packet that is NF_QUEUE'd and reinjected later
> - a UDP sender that sleeps while copying user data after its route
>   lookup
> 
> Could dev be read once and passed down to xfrm_dev_direct_output() and
> the driver callbacks? Alternatively, it could be checked again at each
> use, or TX users could be kept from reaching x before xso->dev is
> cleared.
> 
> > diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
> > index 9c101be250ee8..6f98c5c1a823a 100644
> > --- a/net/xfrm/xfrm_state.c
> > +++ b/net/xfrm/xfrm_state.c
> 
> [ ... ]
> 
> > @@ -1012,21 +1021,29 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali
> >  
> >  	mutex_lock(&xfrm_state_gc_mutex);
> >  	spin_lock_bh(&xfrm_state_dev_gc_lock);
> > -restart_gc:
> >  	hlist_for_each_entry_safe(x, tmp, &xfrm_state_dev_gc_list, dev_gclist) {
> >  		xso = &x->xso;
> >  
> >  		if (xso->dev == dev) {
> > -			spin_unlock_bh(&xfrm_state_dev_gc_lock);
> > -			xfrm_dev_state_free(x);
> > -			spin_lock_bh(&xfrm_state_dev_gc_lock);
> > -			goto restart_gc;
> > +			if (!refcount_inc_not_zero(&x->refcnt))
> > +				continue;
> 
> [Severity: Medium]
> Can xfrm_dev_state_flush() return while a state on
> xfrm_state_dev_gc_list still has xso->dev == dev, along with its driver
> SA and its netdev reference?
> 
> This skip assumes the state GC already owns any state whose refcount is
> zero, and that xfrm_flush_gc() at the end will wait for it. However,
> xfrm_state_put() drops the refcount to zero before __xfrm_state_destroy()
> queues the state and schedules the work:
> 
> xfrm_state_put()
>   refcount_dec_and_test(&x->refcnt)   /* refcnt is now 0 */
>   __xfrm_state_destroy()
>     spin_lock_bh(&xfrm_state_gc_lock);
>     hlist_add_head(&x->gclist, &xfrm_state_gc_list);
>     spin_unlock_bh(&xfrm_state_gc_lock);
>     schedule_work(&xfrm_state_gc_work);
> 
> If the flush walks the dev GC list between those two points, it skips
> the state. xfrm_flush_gc() then calls flush_work() on a work item that is
> neither pending nor running, so it returns at once.
> 
> Before this patch, the flush freed the device state of every matching
> entry regardless of its refcount. Here the GC later calls
> xdo_dev_state_free() on a device the flush has already reported as
> clean. For unregister, netdev_wait_allrefs_any() has to wait for the
> reference.
> 
> The later patch "xfrm: Wait for an RCU grace period before freeing device
> state" only handles the case where the flush itself drops the last
> reference. This skip is still there at the end of the series.
> 
> [ ... ]
> 
> -- 
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201124252.1163254-2-steffen.klassert%40secunet.com

  reply	other threads:[~2026-12-03  7:24 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 12:41 [PATCH 0/22] pull request (net): ipsec 2026-12-01 Steffen Klassert
2026-12-01 12:41 ` [PATCH 01/22] xfrm: ipcomp: account full fragment pages in truesize Steffen Klassert
2026-12-01 13:36   ` bot+bpf-ci
2026-12-01 12:41 ` [PATCH 02/22] xfrm: esp: reset outer TCP GSO metadata on espintcp input Steffen Klassert
2026-12-01 12:41 ` [PATCH 03/22] xfrm: keep the mode module pinned until destroy_state returns Steffen Klassert
2026-12-01 12:41 ` [PATCH 04/22] xfrm: serialize SA activation with state deletion Steffen Klassert
2026-12-01 12:41 ` [PATCH 05/22] xfrm: keep old state references until migration deletion completes Steffen Klassert
2026-12-01 12:41 ` [PATCH 06/22] xfrm: hold migrated states through rollback Steffen Klassert
2026-12-01 12:41 ` [PATCH 07/22] xfrm: check inner family via skb_dst for small_ipv6 DF clearing Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-03  6:58     ` Steffen Klassert
2026-12-01 12:41 ` [PATCH 08/22] xfrm: account XFRMA_MARK in xfrm_sa_len() Steffen Klassert
2026-12-01 12:41 ` [PATCH 09/22] xfrm: replay: Fix late packet handling with ESN crypto offload Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-03  7:08     ` Steffen Klassert
2026-12-01 12:41 ` [PATCH 10/22] xfrm: fix IPv6 first-fragment protocol decode Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-01 12:41 ` [PATCH 11/22] xfrm: fix selector matching for IPv6 atomic fragments Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-03  7:15     ` Steffen Klassert
2026-12-01 12:41 ` [PATCH 12/22] xfrm: espintcp: read the socket error queue for MSG_ERRQUEUE Steffen Klassert
2026-12-01 12:41 ` [PATCH 13/22] xfrm: iptfs: force a dst reference before queueing an skb Steffen Klassert
2026-12-01 13:36   ` bot+bpf-ci
2026-12-01 12:41 ` [PATCH 14/22] xfrm: espintcp: select receive path by skb protocol Steffen Klassert
2026-12-01 12:41 ` [PATCH 15/22] net: xfrm: interface: restore dst_orig on every xfrm interface Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-01 12:41 ` [PATCH 16/22] xfrm: iptfs: make room before copying a continuation in reassembly Steffen Klassert
2026-12-01 12:41 ` [PATCH 17/22] xfrm/compat: pass on the translated length of a socket policy Steffen Klassert
2026-12-01 12:41 ` [PATCH 18/22] xfrm: Give GC list nodes their own storage Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-01 12:41 ` [PATCH 19/22] xfrm: Serialize device state teardown Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-03  7:24     ` Steffen Klassert [this message]
2026-12-01 12:41 ` [PATCH 20/22] xfrm: Wait for an RCU grace period before freeing device state Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-01 12:41 ` [PATCH 21/22] Revert "xfrm: serialize state GC with device state flush" Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-01 12:41 ` [PATCH 22/22] xfrm: Serialize offload stats updates with x->lock Steffen Klassert
2026-12-02 16:06 ` [PATCH 0/22] pull request (net): ipsec 2026-12-01 Paolo Abeni

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=axEZJOdNvY7qosxB@gauss4 \
    --to=steffen.klassert@secunet.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=jianbol@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=netdev-bot+sashiko@kernel.org \
    --cc=netdev@vger.kernel.org \
    /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