* [PATCH net 0/2] ipv4/xfrm: fix two dst->dev lifetime races
@ 2026-11-27 8:45 Zhou Qiuyue
2026-11-27 6:38 ` [PATCH net 1/2] ipv4: route: fix use-after-free in ip_route_output_flow() Zhou Qiuyue
2026-11-27 6:39 ` [PATCH net 2/2] xfrm: fix use-after-free in xfrm_lookup_with_ifid() Zhou Qiuyue
0 siblings, 2 replies; 3+ messages in thread
From: Zhou Qiuyue @ 2026-11-27 8:45 UTC (permalink / raw)
To: netdev
Cc: David Ahern, Ido Schimmel, Kuniyuki Iwashima, Steffen Klassert,
Herbert Xu, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Willem de Bruijn, Hangbin Liu, ipsec,
linux-kernel
These two dst->dev readers were found after the four-patch v2 series had
already been sent. This follow-up contains only the new fixes and does
not resend any patch from that series.
Both patches depend on the post-route-flush grace period added by:
[PATCH net v2 1/4] net: wait for RCU readers after flushing routes
https://lore.kernel.org/netdev/ae140ad9c69d3403d3596b1187b86fdd232292e8.1795754281.git.n0ps1edzz@gmail.com/
Please apply them after or together with that patch. The
prerequisite-patch-id below records the same dependency.
dst_dev_put() can replace dst->dev with blackhole_netdev and drop the
reference to the original device. The route-output and XFRM no-policy
paths still had direct readers that could race with the original device
being freed. Convert both readers to dst_dev_rcu() under an RCU
read-side critical section.
On a v7.4-rc2 net-next KASAN kernel, the supplied reproducer triggered
the route report in 2 of 2 runs and the XFRM report in 3 of 3 runs while
deleting and recreating a dummy interface. Fixed-patch runtime reruns
have not been performed.
An x86_64 defconfig W=1 build of net/core/dev.o, net/ipv4/route.o, and
net/xfrm/xfrm_policy.o completed successfully on the exact
prerequisite-plus-two-patch tree.
OpenAI Codex was used to analyze the supplied KASAN traces and
reproducer and to draft these patches and changelogs.
Zhou Qiuyue (2):
ipv4: route: fix use-after-free in ip_route_output_flow()
xfrm: fix use-after-free in xfrm_lookup_with_ifid()
net/ipv4/route.c | 4 +++-
net/xfrm/xfrm_policy.c | 9 ++++++++-
2 files changed, 11 insertions(+), 2 deletions(-)
base-commit: e1055cbfd6eb578417f3ce983cf44462e8465267
prerequisite-patch-id: 0e86b27e6eeb1f9bdcc54de4115415ecd29d4e70
--
2.56.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH net 1/2] ipv4: route: fix use-after-free in ip_route_output_flow()
2026-11-27 8:45 [PATCH net 0/2] ipv4/xfrm: fix two dst->dev lifetime races Zhou Qiuyue
@ 2026-11-27 6:38 ` Zhou Qiuyue
2026-11-27 6:39 ` [PATCH net 2/2] xfrm: fix use-after-free in xfrm_lookup_with_ifid() Zhou Qiuyue
1 sibling, 0 replies; 3+ messages in thread
From: Zhou Qiuyue @ 2026-11-27 6:38 UTC (permalink / raw)
To: netdev
Cc: David Ahern, Ido Schimmel, Kuniyuki Iwashima, Steffen Klassert,
Herbert Xu, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Willem de Bruijn, Hangbin Liu, ipsec,
linux-kernel
ip_route_output_flow() drops the RCU read lock taken during route lookup
before reading rt->dst.dev->ifindex. The rtable remains referenced, but
route teardown can replace dst->dev with blackhole_netdev and drop its
reference to the old net_device.
A concurrent netdev_run_todo() can therefore free the old device after
the pointer is loaded and before its ifindex is read. KASAN reported the
resulting use-after-free twice while UDP sends raced with dummy-device
teardown.
Load and dereference the device through dst_dev_rcu() in an RCU read-side
critical section. This requires netdev_run_todo() to wait for RCU readers
after flushing routes; that fix is a prerequisite of this patch.
OpenAI Codex was used to analyze the supplied KASAN traces and reproducer
and to draft this patch and changelog.
Fixes: 95c47f9cf5e0 ("ipv4: call dst_dev_put() properly")
Cc: stable@vger.kernel.org
Signed-off-by: Zhou Qiuyue <n0ps1edzz@gmail.com>
---
net/ipv4/route.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index cd69242af..b16a9d05f 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -2982,7 +2982,9 @@ struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
return rt;
if (flp4->flowi4_proto) {
- flp4->flowi4_oif = rt->dst.dev->ifindex;
+ rcu_read_lock();
+ flp4->flowi4_oif = dst_dev_rcu(&rt->dst)->ifindex;
+ rcu_read_unlock();
rt = dst_rtable(xfrm_lookup_route(net, &rt->dst,
flowi4_to_flowi(flp4),
sk, 0));
--
2.56.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH net 2/2] xfrm: fix use-after-free in xfrm_lookup_with_ifid()
2026-11-27 8:45 [PATCH net 0/2] ipv4/xfrm: fix two dst->dev lifetime races Zhou Qiuyue
2026-11-27 6:38 ` [PATCH net 1/2] ipv4: route: fix use-after-free in ip_route_output_flow() Zhou Qiuyue
@ 2026-11-27 6:39 ` Zhou Qiuyue
1 sibling, 0 replies; 3+ messages in thread
From: Zhou Qiuyue @ 2026-11-27 6:39 UTC (permalink / raw)
To: netdev
Cc: David Ahern, Ido Schimmel, Kuniyuki Iwashima, Steffen Klassert,
Herbert Xu, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Willem de Bruijn, Hangbin Liu, ipsec,
linux-kernel
dst_dev_put() can replace a dst's device with the blackhole device and
drop the reference to the original device. The device can then be freed
after the unregistering netdevice's todo work runs.
xfrm_lookup_with_ifid() reads dst_orig->dev directly in its no-policy
path. It can therefore dereference the original device after that device
has been freed. KASAN reported this race three times while a reproducer
repeatedly deleted and recreated a dummy interface and exercised the XFRM
no-policy path.
Read the device through dst_dev_rcu() in an RCU read-side critical section.
This requires netdev_run_todo() to wait for RCU readers after flushing
routes; that fix is a prerequisite of this patch.
This access was also identified during review of an earlier XFRM device
lifetime fix and left for a follow-up.
OpenAI Codex was used to analyze the supplied KASAN traces and reproducer
and to draft this patch and changelog.
Reported-by: Jakub Kicinski <kuba@kernel.org>
Link: https://lore.kernel.org/netdev/20260908224901.1591334-1-kuba@kernel.org/
Fixes: 2d151d39073a ("xfrm: Add possibility to set the default to block if we have no policy")
Cc: stable@vger.kernel.org
Signed-off-by: Zhou Qiuyue <n0ps1edzz@gmail.com>
---
net/xfrm/xfrm_policy.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index cea6842e6..eafd090e3 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -3186,8 +3186,10 @@ struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
struct xfrm_dst *xdst;
struct dst_entry *dst, *route;
+ struct net_device *dev;
u16 family = dst_orig->ops->family;
u8 dir = XFRM_POLICY_OUT;
+ bool is_loopback;
int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
dst = NULL;
@@ -3312,7 +3314,12 @@ struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
return dst;
nopol:
- if ((!dst_orig->dev || !(dst_orig->dev->flags & IFF_LOOPBACK)) &&
+ rcu_read_lock();
+ dev = dst_dev_rcu(dst_orig);
+ is_loopback = dev && (dev->flags & IFF_LOOPBACK);
+ rcu_read_unlock();
+
+ if (!is_loopback &&
READ_ONCE(net->xfrm.policy_default[dir]) == XFRM_USERPOLICY_BLOCK) {
err = -EPERM;
goto error;
--
2.56.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-27 9:16 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 8:45 [PATCH net 0/2] ipv4/xfrm: fix two dst->dev lifetime races Zhou Qiuyue
2026-11-27 6:38 ` [PATCH net 1/2] ipv4: route: fix use-after-free in ip_route_output_flow() Zhou Qiuyue
2026-11-27 6:39 ` [PATCH net 2/2] xfrm: fix use-after-free in xfrm_lookup_with_ifid() Zhou Qiuyue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox