Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: n0ps1edzz@gmail.com
Cc: netdev@vger.kernel.org, dsahern@kernel.org, idosch@nvidia.com,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, willemb@google.com,
	kuniyu@google.com, hangbin.liu@linux.dev
Subject: Re: [PATCH net v2 2/4] ipv4: route: fix use-after-free in ipv4_confirm_neigh()
Date: Sat, 28 Nov 2026 06:42:38 +0000	[thread overview]
Message-ID: <179584815806.3135.2658352587378278244@kernel.org> (raw)
In-Reply-To: <1a0a6b41df2422c4cea67f1571772b9d22ab200d.1795754281.git.n0ps1edzz@gmail.com>

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] The fix makes ipv4_confirm_neigh() (net/ipv4/route.c) load the
  device with dst_dev_rcu() inside rcu_read_lock().

--

ipv4: route: fix use-after-free in ipv4_confirm_neigh()

This patch makes ipv4_confirm_neigh() hold rcu_read_lock() and load the
egress device with dst_dev_rcu() instead of dst_dev(). It relies on the
grace period in netdev_run_todo() to keep the old net_device alive after
a concurrent dst_dev_put() swaps dst->dev to blackhole_netdev.

Link: https://lore.kernel.org/netdev/20261123133000.2.n0ps1edzz@gmail.com/

> diff --git a/net/ipv4/route.c b/net/ipv4/route.c
> index cd69242afab07..5ceea01f27b4e 100644
> --- a/net/ipv4/route.c
> +++ b/net/ipv4/route.c
> @@ -441,19 +441,25 @@ static struct neighbour *ipv4_dst_neigh_lookup(const struct dst_entry *dst,
>  static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr)
>  {
>  	const struct rtable *rt = container_of(dst, struct rtable, dst);
> -	struct net_device *dev = dst_dev(dst);
> +	struct net_device *dev;
>  	const __be32 *pkey = daddr;
>  
> +	rcu_read_lock();
> +	dev = dst_dev_rcu(dst);

[Severity: High]
The commit message says:

  The grace period added to netdev_run_todo() ensures that the
  device remains alive until the read-side critical section finishes.

Does that hold for dst->dev swaps done from RCU callbacks?

The synchronize_net() in netdev_run_todo() only covers dst_dev_put()
calls that finish before it starts. The commit in the Fixes tag also added
dst_dev_put() to rt_fibinfo_free_cpus() in net/ipv4/fib_semantics.c, which
works on per-CPU cached output routes. Those routes are not on
rt_uncached_list, so rt_flush_dev() never touches them.

For routes that use nexthop objects (ip nexthop add id N dev X; ip route
add ... nhid N), it looks like the teardown can be pushed past that grace
period:

  unregister (RTNL held)
    nexthop_flush_dev()->remove_nexthop()->nexthop_put()
      /* not the last ref, fi->nh holds one from fib_create_info() */
    fib_flush()->fib_release_info()->fib_info_put()->free_fib_info()
      call_rcu_hurry(free_fib_info_rcu)

  netdev_run_todo()
    rcu_barrier()
      free_fib_info_rcu()
        nexthop_put(fi->nh)
          call_rcu_hurry(&nh->rcu, nexthop_free_rcu)  /* chained */
    rt_flush_dev(NULL)
    rt6_uncached_list_flush_dev(NULL)
    synchronize_net()

  later, from the chained callback:
    nexthop_free_rcu()->nexthop_free_single()->fib_nh_release()
      ->fib_nh_common_release()->rt_fibinfo_free_cpus()
        ->dst_dev_put()
            rcu_assign_pointer(dst->dev_rcu, blackhole_netdev);
            netdev_ref_replace(dev, blackhole_netdev, ...);

rcu_barrier() does not wait for callbacks queued by the callbacks it
waited for. That means nexthop_free_rcu() can run during or after the new
synchronize_net().

After that, netdev_wait_allrefs_any() hands the device back as soon as
netdev_refcnt_read(dev) == 1. This can happen on its first check or right
after an rcu_barrier(), and neither of those implies a grace period after
the dst_dev_put() above. netdev_run_todo() then frees the device.

Here is the sequence I have in mind. A udp_sendmsg() caller using
MSG_CONFIRM|MSG_PROBE holds that per-CPU route. It enters rcu_read_lock()
here after the synchronize_net() has started. It loads the old device
through dst_dev_rcu() just before the swap. It is still inside the
read-side section when the device is freed.

Wouldn't __ipv4_confirm_neigh()->__ipv4_neigh_lookup_noref() then read
dev->flags from the freed net_device?

The same window seems to exist when a switchdev or offload driver (mlxsw,
prestera, rocker, mlx5 lag, netdevsim) holds a fib_info reference with
fib_info_hold() and drops it from a work item after netdev_run_todo() has
gone past its rcu_barrier() and synchronize_net().

Is another grace period needed between the last dst_dev_put() reference
drop and the free? One option would be a synchronize_net() after
netdev_wait_allrefs_any() sees the refcount reach 1.

> +
>  	if (rt->rt_gw_family == AF_INET) {
>  		pkey = (const __be32 *)&rt->rt_gw4;

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795754281.git.n0ps1edzz%40gmail.com

  reply	other threads:[~2026-11-28  6:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  4:38 [PATCH net v2 0/4] net: fix dst->dev lifetime races Zhou Qiuyue
2026-11-27  4:03 ` [PATCH net v2 1/4] net: wait for RCU readers after flushing routes Zhou Qiuyue
2026-11-28  6:42   ` netdev-bot+sashiko
2026-11-27  4:03 ` [PATCH net v2 2/4] ipv4: route: fix use-after-free in ipv4_confirm_neigh() Zhou Qiuyue
2026-11-28  6:42   ` netdev-bot+sashiko [this message]
2026-11-27  4:03 ` [PATCH net v2 3/4] ipv6: route: fix use-after-free in ip6_confirm_neigh() Zhou Qiuyue
2026-11-27  4:03 ` [PATCH net v2 4/4] ipv6: route: fix use-after-free in ip6_dst_neigh_lookup() Zhou Qiuyue

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=179584815806.3135.2658352587378278244@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=hangbin.liu@linux.dev \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=n0ps1edzz@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.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