[PATCH net 2/2] xfrm: fix use-after-free in xfrm_lookup_with_ifid()

From: Zhou Qiuyue

Date: Fri Nov 27 2026 - 04:16:37 EST


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@xxxxxxxxxx>
Link: https://lore.kernel.org/netdev/20260908224901.1591334-1-kuba@xxxxxxxxxx/
Fixes: 2d151d39073a ("xfrm: Add possibility to set the default to block if we have no policy")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Zhou Qiuyue <n0ps1edzz@xxxxxxxxx>
---
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