* [PATCH v2] xfrm: avoid stale tunnel pointers in deferred input callbacks
@ 2026-11-12 11:19 Jun Yao
2026-11-26 8:19 ` Steffen Klassert
2026-11-26 8:20 ` Steffen Klassert
0 siblings, 2 replies; 3+ messages in thread
From: Jun Yao @ 2026-11-12 11:19 UTC (permalink / raw)
To: netdev
Cc: linux-kernel, davem, edumazet, kuba, pabeni, horms,
steffen.klassert, herbert, dsahern, idosch, lkp, victor, Jun Yao
IP-TFS input can be deferred while its reordering queue waits for a missing
sequence number. The VTI receive callback stores a pointer to the tunnel in
skb->cb, and the device can be removed and freed before the callback runs.
The issue was discovered by Qihoo 360's Tulongfeng multi-agent system.
This IPv4 VTI use-after-free was reproduced on Linux 7.4-rc2 in an x86-64
QEMU guest. When the IP-TFS reorder timer resumes input after VTI deletion,
KASAN reports an 8-byte read from freed memory in vti_rcv_cb(). Relevant
parts of the report are:
BUG: KASAN: slab-use-after-free in vti_rcv_cb+0x696/0x770
Read of size 8 at addr ffff88810eae4b58 by task swapper/3/0
Call Trace:
[...]
vti_rcv_cb+0x696/0x770
xfrm4_rcv_cb+0xa6/0x110
xfrm_rcv_cb+0xca/0x140
xfrm_input+0x11eb/0x6660
__input_process_payload+0xb87/0x24c0
iptfs_input_ordered+0x560/0x8e0
iptfs_drop_timer+0x464/0x8e0
__hrtimer_run_queues+0x270/0x600
hrtimer_run_softirq+0x14b/0x270
[...]
Freed by task 394:
[...]
kfree+0x23c/0x490
kvfree+0x31/0x40
netdev_release+0x6d/0x90
device_release+0xce/0x250
kobject_put+0x18d/0x4f0
netdev_run_todo+0x724/0x1360
rtnl_dellink+0x401/0x1020
[...]
Keep only scalar tunnel metadata in an skb extension. The VTI callbacks
look up the device by ifindex under RCU and check its link configuration
before using its private data. If the VTI no longer exists, drop the
deferred packet. Use the same metadata to preserve tunnel mark handling
and IPv6 tunnel detection, and clear it when a new XFRM receive path starts
and after the callback completes.
Guard the skb extension accesses with CONFIG_XFRM and provide no-op or
NULL-returning helpers when it is disabled, since xfrm.h is also included
by code built without XFRM support.
Signed-off-by: Jun Yao <yaojun8558363@gmail.com>
---
v1: https://lore.kernel.org/lkml/20261111094629.230568-1-yaojun8558363@gmail.com/T/#u
Changes in v2:
- Group the tunnel info helpers under CONFIG_XFRM and provide NULL-returning
or no-op stubs when it is disabled, fixing the !CONFIG_XFRM build errors.
- Document the runtime reproduction and include a trimmed KASAN report.
Build failure reported by the kernel test robot:
https://lore.kernel.org/oe-kbuild-all/202611120519.EG3LbKfu-lkp@intel.com/
Build-tested bzImage and modules using x86_64_defconfig-based configurations
with CONFIG_WERROR=y and GCC 13.3.0:
- CONFIG_XFRM=y, including IPv4/IPv6 VTI and ESP offload, XFRM interface,
and IP-TFS.
- CONFIG_XFRM=n and CONFIG_SKB_EXTENSIONS=n.
Both builds completed without errors or warnings.
diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 6787fcf6a..f97412b95 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -5044,6 +5044,7 @@ enum skb_ext_id {
#endif
#ifdef CONFIG_XFRM
SKB_EXT_SEC_PATH,
+ SKB_EXT_XFRM_TUNNEL,
#endif
#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
TC_SKB_EXT,
diff --git a/include/net/xfrm.h b/include/net/xfrm.h
index 445223a8d..38815faca 100644
--- a/include/net/xfrm.h
+++ b/include/net/xfrm.h
@@ -382,6 +382,7 @@ struct xfrm_dst_lookup_params {
};
struct net_device;
+struct ip6_tnl;
struct xfrm_type;
struct xfrm_dst;
struct xfrm_policy_afinfo {
@@ -740,14 +741,70 @@ struct xfrm_tunnel_skb_cb {
struct inet_skb_parm h4;
struct inet6_skb_parm h6;
} header;
+ /* Keep the layout of the other XFRM skb control blocks stable. */
+ unsigned long reserved;
+};
+
+enum xfrm_tunnel_info_type {
+ XFRM_TUNNEL_INFO_VTI4,
+ XFRM_TUNNEL_INFO_VTI6,
+ XFRM_TUNNEL_INFO_IP6,
+};
+/* Stored in an skb extension so it survives asynchronous XFRM input. */
+struct xfrm_tunnel_info {
+ int ifindex;
+ __be32 i_key;
+ u8 type;
union {
- struct ip_tunnel *ip4;
- struct ip6_tnl *ip6;
- } tunnel;
+ struct {
+ int link;
+ __be32 saddr;
+ __be32 daddr;
+ } ip4;
+ struct {
+ int link;
+ u8 proto;
+ struct in6_addr laddr;
+ struct in6_addr raddr;
+ } ip6;
+ };
};
-#define XFRM_TUNNEL_SKB_CB(__skb) ((struct xfrm_tunnel_skb_cb *)&((__skb)->cb[0]))
+#ifdef CONFIG_XFRM
+static inline struct xfrm_tunnel_info *
+xfrm_tunnel_info_add(struct sk_buff *skb)
+{
+ return skb_ext_add(skb, SKB_EXT_XFRM_TUNNEL);
+}
+
+static inline const struct xfrm_tunnel_info *
+xfrm_tunnel_info(const struct sk_buff *skb)
+{
+ return skb_ext_find(skb, SKB_EXT_XFRM_TUNNEL);
+}
+
+static inline void xfrm_tunnel_info_clear(struct sk_buff *skb)
+{
+ skb_ext_del(skb, SKB_EXT_XFRM_TUNNEL);
+}
+#else
+static inline struct xfrm_tunnel_info *
+xfrm_tunnel_info_add(struct sk_buff *skb)
+{
+ return NULL;
+}
+
+static inline const struct xfrm_tunnel_info *
+xfrm_tunnel_info(const struct sk_buff *skb)
+{
+ return NULL;
+}
+
+static inline void xfrm_tunnel_info_clear(struct sk_buff *skb)
+{
+}
+#endif /* CONFIG_XFRM */
/*
* This structure is used for the duration where packets are being
@@ -1851,7 +1908,7 @@ int xfrm4_rcv(struct sk_buff *skb);
static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
{
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
+ xfrm_tunnel_info_clear(skb);
XFRM_SPI_SKB_CB(skb)->family = AF_INET;
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
return xfrm_input(skb, nexthdr, spi, 0);
@@ -2300,15 +2357,17 @@ static inline int xfrm_if_id_put(struct sk_buff *skb, __u32 if_id)
static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,
unsigned int family)
{
+ const struct xfrm_tunnel_info *info = xfrm_tunnel_info(skb);
bool tunnel = false;
switch(family) {
case AF_INET:
- if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
+ if (info && info->type == XFRM_TUNNEL_INFO_VTI4)
tunnel = true;
break;
case AF_INET6:
- if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
+ if (info && (info->type == XFRM_TUNNEL_INFO_VTI6 ||
+ info->type == XFRM_TUNNEL_INFO_IP6))
tunnel = true;
break;
}
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 8da53d5b7..32f41370d 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -5241,6 +5241,7 @@ static const u8 skb_ext_type_len[] = {
#endif
#ifdef CONFIG_XFRM
[SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
+ [SKB_EXT_XFRM_TUNNEL] = SKB_EXT_CHUNKSIZEOF(struct xfrm_tunnel_info),
#endif
#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
[TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
diff --git a/net/ipv4/esp4_offload.c b/net/ipv4/esp4_offload.c
index abd77162f..fa0a2a73b 100644
--- a/net/ipv4/esp4_offload.c
+++ b/net/ipv4/esp4_offload.c
@@ -81,7 +81,7 @@ static struct sk_buff *esp4_gro_receive(struct list_head *head,
if (NAPI_GRO_CB(skb)->proto == IPPROTO_UDP)
encap_type = UDP_ENCAP_ESPINUDP;
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
+ xfrm_tunnel_info_clear(skb);
XFRM_SPI_SKB_CB(skb)->family = AF_INET;
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
XFRM_SPI_SKB_CB(skb)->seq = seq;
diff --git a/net/ipv4/ip_vti.c b/net/ipv4/ip_vti.c
index c4f14d42d..3594254df 100644
--- a/net/ipv4/ip_vti.c
+++ b/net/ipv4/ip_vti.c
@@ -58,10 +58,25 @@ static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags,
iph->saddr, iph->daddr, 0);
if (tunnel) {
+ struct xfrm_tunnel_info *info;
+
if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
goto drop;
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
+ info = xfrm_tunnel_info_add(skb);
+ if (!info)
+ goto drop;
+
+ *info = (struct xfrm_tunnel_info) {
+ .ifindex = tunnel->dev->ifindex,
+ .i_key = tunnel->parms.i_key,
+ .type = XFRM_TUNNEL_INFO_VTI4,
+ .ip4 = {
+ .link = tunnel->parms.link,
+ .saddr = tunnel->parms.iph.saddr,
+ .daddr = tunnel->parms.iph.daddr,
+ },
+ };
if (update_skb_dev)
skb->dev = tunnel->dev;
@@ -96,18 +111,28 @@ static int vti_rcv_proto(struct sk_buff *skb)
static int vti_rcv_cb(struct sk_buff *skb, int err)
{
+ const struct xfrm_tunnel_info *info = xfrm_tunnel_info(skb);
unsigned short family;
struct net_device *dev;
struct xfrm_state *x;
const struct xfrm_mode *inner_mode;
- struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
+ struct ip_tunnel *tunnel;
u32 orig_mark = skb->mark;
int ret;
- if (!tunnel)
+ if (!info || info->type != XFRM_TUNNEL_INFO_VTI4)
return 1;
- dev = tunnel->dev;
+ dev = dev_get_by_index_rcu(dev_net(skb->dev), info->ifindex);
+ if (!dev || dev->rtnl_link_ops != &vti_link_ops)
+ return err ? 1 : -ENODEV;
+
+ tunnel = netdev_priv(dev);
+ if (tunnel->parms.i_key != info->i_key ||
+ tunnel->parms.link != info->ip4.link ||
+ tunnel->parms.iph.saddr != info->ip4.saddr ||
+ tunnel->parms.iph.daddr != info->ip4.daddr)
+ return err ? 1 : -ENODEV;
if (err) {
DEV_STATS_INC(dev, rx_errors);
@@ -131,7 +156,7 @@ static int vti_rcv_cb(struct sk_buff *skb, int err)
family = inner_mode->family;
- skb->mark = be32_to_cpu(tunnel->parms.i_key);
+ skb->mark = be32_to_cpu(info->i_key);
ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
skb->mark = orig_mark;
diff --git a/net/ipv4/xfrm4_protocol.c b/net/ipv4/xfrm4_protocol.c
index 4ee624d8e..c31426ea8 100644
--- a/net/ipv4/xfrm4_protocol.c
+++ b/net/ipv4/xfrm4_protocol.c
@@ -65,7 +65,7 @@ int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
struct xfrm4_protocol *handler;
struct xfrm4_protocol __rcu **head = proto_handlers(nexthdr);
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
+ xfrm_tunnel_info_clear(skb);
XFRM_SPI_SKB_CB(skb)->family = AF_INET;
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
@@ -98,7 +98,7 @@ static int xfrm4_esp_rcv(struct sk_buff *skb)
int ret;
struct xfrm4_protocol *handler;
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
+ xfrm_tunnel_info_clear(skb);
for_each_protocol_rcu(esp4_handlers, handler)
if ((ret = handler->handler(skb)) != -EINVAL)
@@ -126,7 +126,7 @@ static int xfrm4_ah_rcv(struct sk_buff *skb)
int ret;
struct xfrm4_protocol *handler;
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
+ xfrm_tunnel_info_clear(skb);
for_each_protocol_rcu(ah4_handlers, handler)
if ((ret = handler->handler(skb)) != -EINVAL)
@@ -154,7 +154,7 @@ static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
int ret;
struct xfrm4_protocol *handler;
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
+ xfrm_tunnel_info_clear(skb);
for_each_protocol_rcu(ipcomp4_handlers, handler)
if ((ret = handler->handler(skb)) != -EINVAL)
diff --git a/net/ipv6/esp6_offload.c b/net/ipv6/esp6_offload.c
index 22895521a..00cdae2c7 100644
--- a/net/ipv6/esp6_offload.c
+++ b/net/ipv6/esp6_offload.c
@@ -110,7 +110,7 @@ static struct sk_buff *esp6_gro_receive(struct list_head *head,
goto out;
IP6CB(skb)->nhoff = nhoff;
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
+ xfrm_tunnel_info_clear(skb);
XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
XFRM_SPI_SKB_CB(skb)->seq = seq;
diff --git a/net/ipv6/ip6_vti.c b/net/ipv6/ip6_vti.c
index c79027839..6cf40b000 100644
--- a/net/ipv6/ip6_vti.c
+++ b/net/ipv6/ip6_vti.c
@@ -302,6 +302,8 @@ static int vti6_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
rcu_read_lock();
t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
if (t) {
+ struct xfrm_tunnel_info *info;
+
if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
rcu_read_unlock();
goto discard;
@@ -319,9 +321,26 @@ static int vti6_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
goto discard;
}
+ info = xfrm_tunnel_info_add(skb);
+ if (!info) {
+ rcu_read_unlock();
+ goto discard;
+ }
+
+ *info = (struct xfrm_tunnel_info) {
+ .ifindex = t->dev->ifindex,
+ .i_key = t->parms.i_key,
+ .type = XFRM_TUNNEL_INFO_VTI6,
+ .ip6 = {
+ .link = t->parms.link,
+ .proto = t->parms.proto,
+ .laddr = t->parms.laddr,
+ .raddr = t->parms.raddr,
+ },
+ };
+
rcu_read_unlock();
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
return xfrm_input(skb, nexthdr, spi, encap_type);
@@ -342,18 +361,29 @@ static int vti6_rcv(struct sk_buff *skb)
static int vti6_rcv_cb(struct sk_buff *skb, int err)
{
+ const struct xfrm_tunnel_info *info = xfrm_tunnel_info(skb);
unsigned short family;
struct net_device *dev;
struct xfrm_state *x;
const struct xfrm_mode *inner_mode;
- struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
+ struct ip6_tnl *t;
u32 orig_mark = skb->mark;
int ret;
- if (!t)
+ if (!info || info->type != XFRM_TUNNEL_INFO_VTI6)
return 1;
- dev = t->dev;
+ dev = dev_get_by_index_rcu(dev_net(skb->dev), info->ifindex);
+ if (!dev || dev->rtnl_link_ops != &vti6_link_ops)
+ return err ? 1 : -ENODEV;
+
+ t = netdev_priv(dev);
+ if (t->parms.i_key != info->i_key ||
+ t->parms.link != info->ip6.link ||
+ t->parms.proto != info->ip6.proto ||
+ !ipv6_addr_equal(&t->parms.laddr, &info->ip6.laddr) ||
+ !ipv6_addr_equal(&t->parms.raddr, &info->ip6.raddr))
+ return err ? 1 : -ENODEV;
if (err) {
DEV_STATS_INC(dev, rx_errors);
@@ -377,7 +407,7 @@ static int vti6_rcv_cb(struct sk_buff *skb, int err)
family = inner_mode->family;
- skb->mark = be32_to_cpu(t->parms.i_key);
+ skb->mark = be32_to_cpu(info->i_key);
ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
skb->mark = orig_mark;
diff --git a/net/ipv6/xfrm6_input.c b/net/ipv6/xfrm6_input.c
index 07edef258..9aed0b21d 100644
--- a/net/ipv6/xfrm6_input.c
+++ b/net/ipv6/xfrm6_input.c
@@ -15,6 +15,7 @@
#include <linux/netfilter.h>
#include <linux/netfilter_ipv6.h>
#include <net/ipv6.h>
+#include <net/ip6_tunnel.h>
#include <net/xfrm.h>
#include <net/protocol.h>
#include <net/gro.h>
@@ -22,7 +23,28 @@
int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
struct ip6_tnl *t)
{
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
+ struct xfrm_tunnel_info *info;
+
+ xfrm_tunnel_info_clear(skb);
+ if (t) {
+ info = xfrm_tunnel_info_add(skb);
+ if (!info) {
+ kfree_skb(skb);
+ return 0;
+ }
+
+ *info = (struct xfrm_tunnel_info) {
+ .ifindex = t->dev->ifindex,
+ .i_key = t->parms.i_key,
+ .type = XFRM_TUNNEL_INFO_IP6,
+ .ip6 = {
+ .link = t->parms.link,
+ .proto = t->parms.proto,
+ .laddr = t->parms.laddr,
+ .raddr = t->parms.raddr,
+ },
+ };
+ }
XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
return xfrm_input(skb, nexthdr, spi, 0);
diff --git a/net/ipv6/xfrm6_protocol.c b/net/ipv6/xfrm6_protocol.c
index 9b586fcec..2e93029a3 100644
--- a/net/ipv6/xfrm6_protocol.c
+++ b/net/ipv6/xfrm6_protocol.c
@@ -66,7 +66,7 @@ int xfrm6_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
struct xfrm6_protocol *handler;
struct xfrm6_protocol __rcu **head = proto_handlers(nexthdr);
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
+ xfrm_tunnel_info_clear(skb);
XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
@@ -113,7 +113,7 @@ static int xfrm6_esp_rcv(struct sk_buff *skb)
int ret;
struct xfrm6_protocol *handler;
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
+ xfrm_tunnel_info_clear(skb);
for_each_protocol_rcu(esp6_handlers, handler)
if ((ret = handler->handler(skb)) != -EINVAL)
@@ -142,7 +142,7 @@ static int xfrm6_ah_rcv(struct sk_buff *skb)
int ret;
struct xfrm6_protocol *handler;
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
+ xfrm_tunnel_info_clear(skb);
for_each_protocol_rcu(ah6_handlers, handler)
if ((ret = handler->handler(skb)) != -EINVAL)
@@ -171,7 +171,7 @@ static int xfrm6_ipcomp_rcv(struct sk_buff *skb)
int ret;
struct xfrm6_protocol *handler;
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
+ xfrm_tunnel_info_clear(skb);
for_each_protocol_rcu(ipcomp6_handlers, handler)
if ((ret = handler->handler(skb)) != -EINVAL)
diff --git a/net/xfrm/xfrm_input.c b/net/xfrm/xfrm_input.c
index 5ed87d513..a5d67d506 100644
--- a/net/xfrm/xfrm_input.c
+++ b/net/xfrm/xfrm_input.c
@@ -466,6 +466,7 @@ static int xfrm_inner_mode_input(struct xfrm_state *x,
int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
{
const struct xfrm_state_afinfo *afinfo;
+ const struct xfrm_tunnel_info *tunnel_info;
struct net *net = dev_net(skb->dev);
struct net_device *dev = skb->dev;
int err;
@@ -555,16 +556,20 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
}
family = XFRM_SPI_SKB_CB(skb)->family;
+ tunnel_info = xfrm_tunnel_info(skb);
/* if tunnel is present override skb->mark value with tunnel i_key */
switch (family) {
case AF_INET:
- if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
- mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
+ if (tunnel_info &&
+ tunnel_info->type == XFRM_TUNNEL_INFO_VTI4)
+ mark = be32_to_cpu(tunnel_info->i_key);
break;
case AF_INET6:
- if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
- mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
+ if (tunnel_info &&
+ (tunnel_info->type == XFRM_TUNNEL_INFO_VTI6 ||
+ tunnel_info->type == XFRM_TUNNEL_INFO_IP6))
+ mark = be32_to_cpu(tunnel_info->i_key);
break;
}
@@ -739,6 +744,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
goto drop;
}
+ xfrm_tunnel_info_clear(skb);
nf_reset_ct(skb);
if (decaps) {
diff --git a/net/xfrm/xfrm_interface_core.c b/net/xfrm/xfrm_interface_core.c
index 9835fbe63..d91b5f22d 100644
--- a/net/xfrm/xfrm_interface_core.c
+++ b/net/xfrm/xfrm_interface_core.c
@@ -322,10 +322,10 @@ static int xfrmi_input(struct sk_buff *skb, int nexthdr, __be32 spi,
XFRM_SPI_SKB_CB(skb)->family = family;
if (family == AF_INET) {
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
+ xfrm_tunnel_info_clear(skb);
} else {
XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
- XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
+ xfrm_tunnel_info_clear(skb);
}
return xfrm_input(skb, nexthdr, spi, encap_type);
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] xfrm: avoid stale tunnel pointers in deferred input callbacks
2026-11-12 11:19 [PATCH v2] xfrm: avoid stale tunnel pointers in deferred input callbacks Jun Yao
@ 2026-11-26 8:19 ` Steffen Klassert
2026-11-26 8:20 ` Steffen Klassert
1 sibling, 0 replies; 3+ messages in thread
From: Steffen Klassert @ 2026-11-26 8:19 UTC (permalink / raw)
To: Jun Yao
Cc: netdev, linux-kernel, davem, edumazet, kuba, pabeni, horms,
herbert, dsahern, idosch, lkp, victor
Sorry for the delay, but I had to think a bit longer about this.
On Thu, Nov 12, 2026 at 07:19:46PM +0800, Jun Yao wrote:
> IP-TFS input can be deferred while its reordering queue waits for a missing
> sequence number. The VTI receive callback stores a pointer to the tunnel in
> skb->cb, and the device can be removed and freed before the callback runs.
>
> The issue was discovered by Qihoo 360's Tulongfeng multi-agent system.
>
> This IPv4 VTI use-after-free was reproduced on Linux 7.4-rc2 in an x86-64
> QEMU guest. When the IP-TFS reorder timer resumes input after VTI deletion,
> KASAN reports an 8-byte read from freed memory in vti_rcv_cb(). Relevant
> parts of the report are:
>
> BUG: KASAN: slab-use-after-free in vti_rcv_cb+0x696/0x770
> Read of size 8 at addr ffff88810eae4b58 by task swapper/3/0
> Call Trace:
> [...]
> vti_rcv_cb+0x696/0x770
> xfrm4_rcv_cb+0xa6/0x110
> xfrm_rcv_cb+0xca/0x140
> xfrm_input+0x11eb/0x6660
> __input_process_payload+0xb87/0x24c0
> iptfs_input_ordered+0x560/0x8e0
> iptfs_drop_timer+0x464/0x8e0
> __hrtimer_run_queues+0x270/0x600
> hrtimer_run_softirq+0x14b/0x270
> [...]
>
> Freed by task 394:
> [...]
> kfree+0x23c/0x490
> kvfree+0x31/0x40
> netdev_release+0x6d/0x90
> device_release+0xce/0x250
> kobject_put+0x18d/0x4f0
> netdev_run_todo+0x724/0x1360
> rtnl_dellink+0x401/0x1020
> [...]
>
> Keep only scalar tunnel metadata in an skb extension. The VTI callbacks
> look up the device by ifindex under RCU and check its link configuration
> before using its private data. If the VTI no longer exists, drop the
> deferred packet. Use the same metadata to preserve tunnel mark handling
> and IPv6 tunnel detection, and clear it when a new XFRM receive path starts
> and after the callback completes.
>
> Guard the skb extension accesses with CONFIG_XFRM and provide no-op or
> NULL-returning helpers when it is disabled, since xfrm.h is also included
> by code built without XFRM support.
>
> Signed-off-by: Jun Yao <yaojun8558363@gmail.com>
Fixes tag is missing.
>
> ---
> v1: https://lore.kernel.org/lkml/20261111094629.230568-1-yaojun8558363@gmail.com/T/#u
>
> Changes in v2:
> - Group the tunnel info helpers under CONFIG_XFRM and provide NULL-returning
> or no-op stubs when it is disabled, fixing the !CONFIG_XFRM build errors.
> - Document the runtime reproduction and include a trimmed KASAN report.
>
> Build failure reported by the kernel test robot:
> https://lore.kernel.org/oe-kbuild-all/202611120519.EG3LbKfu-lkp@intel.com/
>
> Build-tested bzImage and modules using x86_64_defconfig-based configurations
> with CONFIG_WERROR=y and GCC 13.3.0:
> - CONFIG_XFRM=y, including IPv4/IPv6 VTI and ESP offload, XFRM interface,
> and IP-TFS.
> - CONFIG_XFRM=n and CONFIG_SKB_EXTENSIONS=n.
> Both builds completed without errors or warnings.
>
> diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
> index 6787fcf6a..f97412b95 100644
> --- a/include/linux/skbuff.h
> +++ b/include/linux/skbuff.h
> @@ -5044,6 +5044,7 @@ enum skb_ext_id {
> #endif
> #ifdef CONFIG_XFRM
> SKB_EXT_SEC_PATH,
> + SKB_EXT_XFRM_TUNNEL,
> #endif
> #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
> TC_SKB_EXT,
> diff --git a/include/net/xfrm.h b/include/net/xfrm.h
> index 445223a8d..38815faca 100644
> --- a/include/net/xfrm.h
> +++ b/include/net/xfrm.h
> @@ -382,6 +382,7 @@ struct xfrm_dst_lookup_params {
> };
>
> struct net_device;
> +struct ip6_tnl;
> struct xfrm_type;
> struct xfrm_dst;
> struct xfrm_policy_afinfo {
> @@ -740,14 +741,70 @@ struct xfrm_tunnel_skb_cb {
> struct inet_skb_parm h4;
> struct inet6_skb_parm h6;
> } header;
> + /* Keep the layout of the other XFRM skb control blocks stable. */
> + unsigned long reserved;
The comment above only makes sense if you know why it was
introduced.
> diff --git a/net/ipv6/xfrm6_input.c b/net/ipv6/xfrm6_input.c
> index 07edef258..9aed0b21d 100644
> --- a/net/ipv6/xfrm6_input.c
> +++ b/net/ipv6/xfrm6_input.c
> @@ -15,6 +15,7 @@
> #include <linux/netfilter.h>
> #include <linux/netfilter_ipv6.h>
> #include <net/ipv6.h>
> +#include <net/ip6_tunnel.h>
> #include <net/xfrm.h>
> #include <net/protocol.h>
> #include <net/gro.h>
> @@ -22,7 +23,28 @@
> int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
> struct ip6_tnl *t)
> {
> - XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
> + struct xfrm_tunnel_info *info;
> +
> + xfrm_tunnel_info_clear(skb);
> + if (t) {
I don't think this can happen. All current callers
of xfrm6_rcv_spi() set t = NULL.
> diff --git a/net/xfrm/xfrm_input.c b/net/xfrm/xfrm_input.c
> index 5ed87d513..a5d67d506 100644
> --- a/net/xfrm/xfrm_input.c
> +++ b/net/xfrm/xfrm_input.c
> @@ -466,6 +466,7 @@ static int xfrm_inner_mode_input(struct xfrm_state *x,
> int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
> {
> const struct xfrm_state_afinfo *afinfo;
> + const struct xfrm_tunnel_info *tunnel_info;
> struct net *net = dev_net(skb->dev);
> struct net_device *dev = skb->dev;
> int err;
> @@ -555,16 +556,20 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
> }
>
> family = XFRM_SPI_SKB_CB(skb)->family;
> + tunnel_info = xfrm_tunnel_info(skb);
Touching the skb extensions are usually an expensive operation.
Did you do some performance benchmarks?
VTI is superseeded by the xfrm interfaces and will go away
some day. So we should try to not add more VTI speciffic
code to the generic codepath. This patch is quite big for
a fix, something shorter would be preferable. But if we
really need to do this, we should try to move it to VTI.
>
> /* if tunnel is present override skb->mark value with tunnel i_key */
> switch (family) {
> case AF_INET:
> - if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
> - mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
> + if (tunnel_info &&
> + tunnel_info->type == XFRM_TUNNEL_INFO_VTI4)
> + mark = be32_to_cpu(tunnel_info->i_key);
> break;
> case AF_INET6:
> - if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
> - mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
> + if (tunnel_info &&
> + (tunnel_info->type == XFRM_TUNNEL_INFO_VTI6 ||
> + tunnel_info->type == XFRM_TUNNEL_INFO_IP6))
> + mark = be32_to_cpu(tunnel_info->i_key);
> break;
> }
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] xfrm: avoid stale tunnel pointers in deferred input callbacks
2026-11-12 11:19 [PATCH v2] xfrm: avoid stale tunnel pointers in deferred input callbacks Jun Yao
2026-11-26 8:19 ` Steffen Klassert
@ 2026-11-26 8:20 ` Steffen Klassert
1 sibling, 0 replies; 3+ messages in thread
From: Steffen Klassert @ 2026-11-26 8:20 UTC (permalink / raw)
To: Jun Yao
Cc: netdev, linux-kernel, davem, edumazet, kuba, pabeni, horms,
herbert, dsahern, idosch, lkp, victor
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
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.
---
Author: Jun Yao <yaojun8558363@gmail.com>
> xfrm: avoid stale tunnel pointers in deferred input callbacks
>
> IP-TFS input can be deferred while its reordering queue waits for a missing
> sequence number. The VTI receive callback stores a pointer to the tunnel in
> skb->cb, and the device can be removed and freed before the callback runs.
>
> The issue was discovered by Qihoo 360's Tulongfeng multi-agent system.
>
> This IPv4 VTI use-after-free was reproduced on Linux 7.4-rc2 in an x86-64
> QEMU guest. When the IP-TFS reorder timer resumes input after VTI deletion,
> KASAN reports an 8-byte read from freed memory in vti_rcv_cb(). Relevant
> parts of the report are:
>
> BUG: KASAN: slab-use-after-free in vti_rcv_cb+0x696/0x770
> Read of size 8 at addr ffff88810eae4b58 by task swapper/3/0
> Call Trace:
> [...]
> vti_rcv_cb+0x696/0x770
> xfrm4_rcv_cb+0xa6/0x110
> xfrm_rcv_cb+0xca/0x140
> xfrm_input+0x11eb/0x6660
> __input_process_payload+0xb87/0x24c0
> iptfs_input_ordered+0x560/0x8e0
> iptfs_drop_timer+0x464/0x8e0
> __hrtimer_run_queues+0x270/0x600
> hrtimer_run_softirq+0x14b/0x270
> [...]
>
> Freed by task 394:
> [...]
> kfree+0x23c/0x490
> kvfree+0x31/0x40
> netdev_release+0x6d/0x90
> device_release+0xce/0x250
> kobject_put+0x18d/0x4f0
> netdev_run_todo+0x724/0x1360
> rtnl_dellink+0x401/0x1020
> [...]
>
> Keep only scalar tunnel metadata in an skb extension. The VTI callbacks
> look up the device by ifindex under RCU and check its link configuration
> before using its private data. If the VTI no longer exists, drop the
> deferred packet. Use the same metadata to preserve tunnel mark handling
> and IPv6 tunnel detection, and clear it when a new XFRM receive path starts
> and after the callback completes.
>
> Guard the skb extension accesses with CONFIG_XFRM and provide no-op or
> NULL-returning helpers when it is disabled, since xfrm.h is also included
> by code built without XFRM support.
>
> Signed-off-by: Jun Yao <yaojun8558363@gmail.com>
Should this bug fix include a Fixes tag for the commit that introduced
the tunnel pointer in skb->cb:
Fixes: 70be6c91c865 ("xfrm: Add xfrm_tunnel_skb_cb to the skb common buffer")
[ ... ]
> diff --git a/net/ipv4/ip_vti.c b/net/ipv4/ip_vti.c
> index c4f14d42df248..3594254df9de9 100644
> --- a/net/ipv4/ip_vti.c
> +++ b/net/ipv4/ip_vti.c
> @@ -58,10 +58,25 @@ static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
> tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags,
> iph->saddr, iph->daddr, 0);
> if (tunnel) {
> + struct xfrm_tunnel_info *info;
> +
> if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
> goto drop;
>
> - XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
> + info = xfrm_tunnel_info_add(skb);
> + if (!info)
> + goto drop;
> +
> + *info = (struct xfrm_tunnel_info) {
> + .ifindex = tunnel->dev->ifindex,
> + .i_key = tunnel->parms.i_key,
> + .type = XFRM_TUNNEL_INFO_VTI4,
> + .ip4 = {
> + .link = tunnel->parms.link,
> + .saddr = tunnel->parms.iph.saddr,
> + .daddr = tunnel->parms.iph.daddr,
> + },
> + };
>
> if (update_skb_dev)
> skb->dev = tunnel->dev;
> @@ -96,18 +111,28 @@ static int vti_rcv_proto(struct sk_buff *skb)
>
> static int vti_rcv_cb(struct sk_buff *skb, int err)
> {
> + const struct xfrm_tunnel_info *info = xfrm_tunnel_info(skb);
> unsigned short family;
> struct net_device *dev;
> struct xfrm_state *x;
> const struct xfrm_mode *inner_mode;
> - struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
> + struct ip_tunnel *tunnel;
> u32 orig_mark = skb->mark;
> int ret;
>
> - if (!tunnel)
> + if (!info || info->type != XFRM_TUNNEL_INFO_VTI4)
> return 1;
>
> - dev = tunnel->dev;
> + dev = dev_get_by_index_rcu(dev_net(skb->dev), info->ifindex);
Does looking up info->ifindex in dev_net(skb->dev) break cross-netns VTI
and VTI6 tunnels in vti_rcv_cb() and vti6_rcv_cb()?
When a VTI or VTI6 interface is created in or moved to a different network
namespace from its underlay link netns (tunnel->net != dev_net(tunnel->dev),
which skb_scrub_packet() at the end of vti_rcv_cb() and vti6_rcv_cb()
handles), vti_input() is called with update_skb_dev = false, so skb->dev
is still the underlay ingress device in tunnel->net when the callback runs.
Looking up tunnel->dev->ifindex in dev_net(skb->dev) searches the underlay
namespace instead of the overlay namespace where tunnel->dev is registered,
dropping all inbound packets with -ENODEV (or matching an unrelated VTI
device with the same ifindex and parameters in the underlay netns).
> + if (!dev || dev->rtnl_link_ops != &vti_link_ops)
> + return err ? 1 : -ENODEV;
> +
> + tunnel = netdev_priv(dev);
> + if (tunnel->parms.i_key != info->i_key ||
> + tunnel->parms.link != info->ip4.link ||
> + tunnel->parms.iph.saddr != info->ip4.saddr ||
> + tunnel->parms.iph.daddr != info->ip4.daddr)
> + return err ? 1 : -ENODEV;
Should vti_rcv_cb() and vti6_rcv_cb() return 0 rather than 1 when err != 0
and the tunnel device is gone or reconfigured?
In xfrm4_rcv_cb() and xfrm6_rcv_cb(), cb_handler callbacks run in priority
order until one returns <= 0; returning 1 means the packet was not claimed
by this protocol handler. Here info->type already shows VTI claimed the
packet on input, and when vti_rcv_cb(skb, 0) returns -ENODEV, xfrm_input()
jumps to drop: and calls xfrm_rcv_cb(skb, family, xfrm_proto, -1).
Returning 1 on that second call lets xfrm4_rcv_cb() continue to
lower-priority handlers such as xfrmi_rcv_cb(), which can match an xfrmi
interface and increment rx_errors and rx_dropped on that unrelated device.
[ ... ]
> diff --git a/net/ipv6/ip6_vti.c b/net/ipv6/ip6_vti.c
> index c790278391519..6cf40b0009845 100644
> --- a/net/ipv6/ip6_vti.c
> +++ b/net/ipv6/ip6_vti.c
[ ... ]
> @@ -342,18 +361,29 @@ static int vti6_rcv(struct sk_buff *skb)
>
> static int vti6_rcv_cb(struct sk_buff *skb, int err)
> {
> + const struct xfrm_tunnel_info *info = xfrm_tunnel_info(skb);
> unsigned short family;
> struct net_device *dev;
> struct xfrm_state *x;
> const struct xfrm_mode *inner_mode;
> - struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
> + struct ip6_tnl *t;
> u32 orig_mark = skb->mark;
> int ret;
>
> - if (!t)
> + if (!info || info->type != XFRM_TUNNEL_INFO_VTI6)
> return 1;
>
> - dev = t->dev;
> + dev = dev_get_by_index_rcu(dev_net(skb->dev), info->ifindex);
Can dereferencing skb->dev via dev_net(skb->dev) before checking err cause
a use-after-free in vti_rcv_cb() and vti6_rcv_cb()?
When asynchronous crypto resumes in xfrm_input() via xfrm_input_resume()
(encap_type == -1, async = 1) and later fails, the drop path releases the
reference on skb->dev before running xfrm_rcv_cb():
net/xfrm/xfrm_input.c:xfrm_input() {
...
drop:
if (async)
dev_put(dev);
xfrm_rcv_cb(skb, family, xfrm_proto, -1);
kfree_skb(skb);
return 0;
}
Previously, vti_rcv_cb() and vti6_rcv_cb() checked if (err) before touching
skb->dev. If the ingress device was unregistered while async crypto was in
flight, dev_put(dev) can drop its last reference and allow netdev_run_todo()
to free skb->dev before vti_rcv_cb() or vti6_rcv_cb() reads
dev_net(skb->dev).
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-26 8:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 11:19 [PATCH v2] xfrm: avoid stale tunnel pointers in deferred input callbacks Jun Yao
2026-11-26 8:19 ` Steffen Klassert
2026-11-26 8:20 ` Steffen Klassert
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox