pull request (net): ipsec 2026-07-10
Subsystems (3)
Steffen Klassert
maintainercover pull request (net): ipsec 2026-07-10
1) xfrm: propagate -EINPROGRESS from validate_xmit_xfrm() Return -EINPROGRESS from xfrm_output_one when validate_xmit_xfrm requeues the packet asynchronously, so the caller doesn't treat it as a real error and free the skb.
2) xfrm: fix stale skb->prev after async crypto steals a GSO segment Re-derive skb->prev from the fragment list after async crypto splits a GSO skb, keeping the linked-list pointers validi.
3) xfrm: nat_keepalive: avoid double free on send error
Hold a state ref while the nat_keepalive timer is active and drop the timer before freeing the state, preventing a re-entered free on send error.
4) xfrm: fix sk_dst_cache double-free in xfrm_user_policy() Null the skb dst cache before freeing the policy so a later skb destructor doesn't double-free it.
5) xfrm: cache the offload ifindex for netlink dumps
Cache the device ifindex at state-add time and use it for netlink dumps instead of dereferencing dst->dev, which may have changed by the time the dump runs.
6) xfrm: reject optional IPTFS templates in outbound policies Reject outbound policies with an optional IPTFS template, IPTFS must always be used if configured.
7) xfrm: clear mode callbacks after failed mode setup
Clear the mode->init_flags and init_state callbacks on the error path after xfrm_init_mode fails, so a partially-initialised mode isn't reused in xfrm_state_construct.
8) xfrm: iptfs: propagate SKBFL_SHARED_FRAG in iptfs_skb_add_frags() Propagate SKBFL_SHARED_FRAG from the original skb to fragments allocated by iptfs_skb_add_frags, keeping shared-fragment accounting correct after IPTFS reassembly.
9) xfrm6: clear dst.dev on error to avoid double netdev_put in xfrm6_fill_dst() Clear dst->dev on the error path of xfrm6_fill_dst() so the caller doesn't release the netdev reference twice via dst_release.
10) xfrm: policy: preallocate inexact bins before xfrm_hash_rebuild reinsert Preallocate all inexact hash bins before existing entries are reinserted during xfrm_hash_rebuild, so reinsertion always hits an existing bin.
Please pull or let me know if there are problems.
Thanks!
The following changes since commit 805185b7c7a1069e407b6f7b3bc98e44d415f484:
Merge tag 'net-7.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net (2026-06-25 12:25:36 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git tags/ipsec-2026-07-10
for you to fetch changes up to f38f8cce2f7e79775b3db7e8a5eacda04ac908e4:
xfrm: policy: preallocate inexact bins before xfrm_hash_rebuild reinsert (2026-07-06 08:30:02 +0200)
----------------------------------------------------------------
ipsec-2026-07-10
----------------------------------------------------------------
Antony Antony (1):
xfrm: reject optional IPTFS templates in outbound policies
Cen Zhang (2):
xfrm: cache the offload ifindex for netlink dumps
xfrm: clear mode callbacks after failed mode setup
Chen YanJun (1):
xfrm: iptfs: propagate SKBFL_SHARED_FRAG in iptfs_skb_add_frags()
Petr Wozniak (2):
xfrm: propagate -EINPROGRESS from validate_xmit_xfrm()
xfrm: fix stale skb->prev after async crypto steals a GSO segment
Qianyu Luo (1):
xfrm: nat_keepalive: avoid double free on send error
Xiang Mei (Microsoft) (3):
xfrm: fix sk_dst_cache double-free in xfrm_user_policy()
xfrm6: clear dst.dev on error to avoid double netdev_put in xfrm6_fill_dst()
xfrm: policy: preallocate inexact bins before xfrm_hash_rebuild reinsert
include/net/xfrm.h | 2 ++ net/core/dev.c | 10 ++++++++-- net/ipv6/xfrm6_policy.c | 1 + net/xfrm/xfrm_device.c | 13 +++++++++++-- net/xfrm/xfrm_iptfs.c | 1 + net/xfrm/xfrm_nat_keepalive.c | 15 +++++++++------ net/xfrm/xfrm_policy.c | 4 ++-- net/xfrm/xfrm_state.c | 12 +++++++++--- net/xfrm/xfrm_user.c | 41 ++++++++++++++++++++++++++++++----------- 9 files changed, 73 insertions(+), 26 deletions(-)
Steffen Klassert
maintainerPATCH 1/10 xfrm: propagate -EINPROGRESS from validate_xmit_xfrm()
From: Petr Wozniak <[email protected]>
validate_xmit_xfrm() returns NULL both when a packet is dropped and when it is stolen by async crypto (-EINPROGRESS from ->xmit()). Callers cannot distinguish the two cases.
f53c723902d1 ("net: Add asynchronous callbacks for xfrm on layer 2.") changed the semantics of a NULL return from "dropped" to "stolen or dropped", but __dev_queue_xmit() was not updated. On virtual/bridge interfaces (noqueue qdisc) __dev_queue_xmit() initialises rc=-ENOMEM and jumps to out: when skb is NULL, returning -ENOMEM to the caller even though the packet will be delivered correctly via xfrm_dev_resume().
Return ERR_PTR(-EINPROGRESS) from validate_xmit_xfrm() for the async case so callers can tell it apart from a real drop. Update __dev_queue_xmit() to handle ERR_PTR(-EINPROGRESS) from validate_xmit_skb() correctly. Update validate_xmit_skb_list() to use IS_ERR_OR_NULL() so that ERR_PTR(-EINPROGRESS) is not mistakenly added to the transmitted list.
--- net/core/dev.c | 10 ++++++++-- net/xfrm/xfrm_device.c | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-)
net/core/dev.c +8 -2
@@ -4018,6 +4018,9 @@ static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,return NULL ; } +/* Returns the skb on success, NULL if dropped, or ERR_PTR(-EINPROGRESS) +* if stolen by async xfrm crypto (delivered via xfrm_dev_resume()). +*/ static struct sk_buff *validate_xmit_skb (struct sk_buff *skb,struct net_device *dev,bool *again) {netdev_features_t features;
@@ -4089,7 +4092,7 @@ struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *d skb->prev = skb; skb =validate_xmit_skb (skb, dev, again); -if (!skb) +if (IS_ERR_OR_NULL (skb))continue ;if (!head)
@@ -4860,8 +4863,11 @@ int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) goto recursion_alert; skb =validate_xmit_skb (skb, dev, &again); -if (!skb) +if (IS_ERR_OR_NULL (skb)) { +if (PTR_ERR (skb) == -EINPROGRESS ) + rc =NET_XMIT_SUCCESS ; goto out; + }HARD_TX_LOCK (dev, txq, cpu);
net/xfrm/xfrm_device.c +2 -2
@@ -182,7 +182,7 @@ struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t featur err = x->type_offload->xmit (x, skb, esp_features);if (err) {if (err == -EINPROGRESS ) -return NULL ; +return ERR_PTR (-EINPROGRESS );XFRM_INC_STATS (xs_net (x),LINUX_MIB_XFRMOUTSTATEPROTOERROR );kfree_skb (skb);
@@ -224,7 +224,7 @@ struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t featur pskb = skb2; } -return skb; +return skb ? skb :ERR_PTR (-EINPROGRESS ); }EXPORT_SYMBOL_GPL (validate_xmit_xfrm);
Signature · 1 line
2.43.0
Re: [PATCH 01/10] xfrm: propagate -EINPROGRESS from validate_xmit_xfrm()
This series was applied to netdev/net.git (main) by Steffen Klassert <[email protected]>:
Hello:
This series was applied to netdev/net.git (main) by Steffen Klassert <[email protected]>:
Quoted 6 lines · On Fri, 10 Jul 2026 11:03:14 +0200 you wrote:
From: Petr Wozniak <[email protected]>
validate_xmit_xfrm()returns NULL both when a packet is dropped and when it is stolen by async crypto (-EINPROGRESSfrom ->xmit()). Callers cannot distinguish the two cases.
Quoted 8 lines
f53c723902d1("net: Add asynchronous callbacks for xfrm on layer 2.") changed the semantics of a NULL return from "dropped" to "stolen or dropped", but__dev_queue_xmit()was not updated. On virtual/bridge interfaces (noqueue qdisc)__dev_queue_xmit()initialises rc=-ENOMEM and jumps to out: when skb is NULL, returning-ENOMEMto the caller even though the packet will be delivered correctly viaxfrm_dev_resume().[...]
Here is the summary with links:
- [01/10] xfrm: propagate -EINPROGRESS from validate_xmit_xfrm()
https://git.kernel.org/netdev/net/c/6860b467f569 - [02/10] xfrm: fix stale skb->prev after async crypto steals a GSO segment https://git.kernel.org/netdev/net/c/3f4c3919baf0 - [03/10] xfrm: nat_keepalive: avoid double free on send error https://git.kernel.org/netdev/net/c/226f4a490d1a - [04/10] xfrm: fix sk_dst_cache double-free in xfrm_user_policy() https://git.kernel.org/netdev/net/c/c283e9ada7fc - [05/10] xfrm: cache the offload ifindex for netlink dumps https://git.kernel.org/netdev/net/c/c4a5f0071cc6 - [06/10] xfrm: reject optional IPTFS templates in outbound policies https://git.kernel.org/netdev/net/c/ea528f18231e - [07/10] xfrm: clear mode callbacks after failed mode setup https://git.kernel.org/netdev/net/c/2538bd3cd1ff - [08/10] xfrm: iptfs: propagate SKBFL_SHARED_FRAG in iptfs_skb_add_frags() https://git.kernel.org/netdev/net/c/430ea57d6daf - [09/10] xfrm6: clear dst.dev on error to avoid double netdev_put in xfrm6_fill_dst() https://git.kernel.org/netdev/net/c/136992de9bb9 - [10/10] xfrm: policy: preallocate inexact bins before xfrm_hash_rebuild reinsert https://git.kernel.org/netdev/net/c/f38f8cce2f7e
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Steffen Klassert
maintainerPATCH 2/10 xfrm: fix stale skb->prev after async crypto steals a GSO segment
In reply to Steffen KlassertFrom: Petr Wozniak <[email protected]>
skb_gso_segment() leaves the segment list head with ->prev pointing at the last segment, an invariant validate_xmit_skb_list() relies on when it sets its tail pointer (tail = skb->prev).
When validate_xmit_xfrm() walks a GSO list and some segments are stolen by async crypto (->xmit() returns -EINPROGRESS), those segments are unlinked from the list but the head ->prev is never updated. If the last segment is the one stolen, the returned head still has ->prev pointing at it, even though it is now owned by the crypto engine and may be freed. validate_xmit_skb_list() later does tail->next = skb, writing through that stale pointer -- a use-after-free.
Repoint skb->prev at the last retained segment before returning.
--- net/xfrm/xfrm_device.c | 8 ++++++++ 1 file changed, 8 insertions(+)
net/xfrm/xfrm_device.c +8 -0
@@ -224,6 +224,14 @@ struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t featur pskb = skb2; } +/* skb_gso_segment() set skb->prev to the last segment, but async +* crypto may have stolen it above without updating ->prev. Repoint +* it at the last retained segment so validate_xmit_skb_list() does +* not chain onto a segment now owned by the crypto engine. +*/ +if (skb) + skb->prev = pskb; +return skb ? skb :ERR_PTR (-EINPROGRESS ); }EXPORT_SYMBOL_GPL (validate_xmit_xfrm);
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 3/10 xfrm: nat_keepalive: avoid double free on send error
In reply to Steffen KlassertFrom: Qianyu Luo <[email protected]>
nat_keepalive_send() frees the keepalive skb whenever the IPv4 or IPv6 send helper reports an error.
That cleanup is only correct before the skb is handed to the output path. Once ip_build_and_send_pkt() or ip6_xmit() takes ownership, the networking stack may already have consumed the skb before returning an error, so freeing it again is unsafe.
Handle the pre-handoff failure cases inside nat_keepalive_send_ipv4() and nat_keepalive_send_ipv6(), where the caller still owns the skb, and keep nat_keepalive_send() responsible only for family dispatch and the unsupported-family cleanup path.
--- net/xfrm/xfrm_nat_keepalive.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
net/xfrm/xfrm_nat_keepalive.c +9 -6
@@ -55,8 +55,10 @@ static int nat_keepalive_send_ipv4(struct sk_buff *skb, ka->encap_sport, sock_net_uid(net, NULL)); rt =ip_route_output_key (net, &fl4); -if (IS_ERR (rt)) +if (IS_ERR (rt)) { +kfree_skb (skb);return PTR_ERR (rt); + }skb_dst_set (skb, &rt->dst);
@@ -101,6 +103,7 @@ static int nat_keepalive_send_ipv6(struct sk_buff *skb, dst =ip6_dst_lookup_flow (net, sk, &fl6,NULL );if (IS_ERR (dst)) {local_unlock_nested_bh (&nat_keepalive_sk_ipv6.bh_lock); +kfree_skb (skb);return PTR_ERR (dst); }
@@ -118,7 +121,6 @@ static void nat_keepalive_send(struct nat_keepalive *ka) sizeof(struct ipv6hdr)) +sizeof (struct udphdr );const u8 nat_ka_payload =0xFF ; -int err = -EAFNOSUPPORT ;struct sk_buff *skb;struct udphdr *uh;
@@ -140,16 +142,17 @@ static void nat_keepalive_send(struct nat_keepalive *ka)switch (ka->family) {case AF_INET : - err =nat_keepalive_send_ipv4 (skb, ka); +nat_keepalive_send_ipv4 (skb, ka);break ;#if IS_ENABLED (CONFIG_IPV6 )case AF_INET6 : - err =nat_keepalive_send_ipv6 (skb, ka, uh); +nat_keepalive_send_ipv6 (skb, ka, uh);break ;#endif - } -if (err) +default :kfree_skb (skb); +break ; + } }struct nat_keepalive_work_ctx {
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 4/10 xfrm: fix sk_dst_cache double-free in xfrm_user_policy()
In reply to Steffen KlassertFrom: "Xiang Mei (Microsoft)" <[email protected]>
xfrm_user_policy() clears the socket dst cache with __sk_dst_reset(), i.e. the non-atomic __sk_dst_set(sk, NULL): it reads sk_dst_cache with rcu_dereference_protected(), stores NULL and dst_release()s the old dst. That is only safe if no other thread modifies sk_dst_cache concurrently.
For a connected UDP socket that does not hold: the transmit fast path (udp_sendmsg -> sk_dst_check -> sk_dst_reset) resets the cache locklessly with an atomic xchg(). A per-socket policy change racing a send can make both sides observe the same old dst and each dst_release() it, dropping the socket's single reference twice and freeing the xfrm_dst bundle while it is still referenced:
BUG: KASAN: slab-use-after-free in dst_release Write of size 4 at addr ffff88801897b6c0 by task exploit/155 Call Trace: ... dst_release (... ./include/linux/rcuref.h:109) xfrm_user_policy (./include/net/sock.h:2239 ./include/net/sock.h:2256 net/xfrm/xfrm_state.c:3053) do_ip_setsockopt (net/ipv4/ip_sockglue.c:1347) ip_setsockopt (net/ipv4/ip_sockglue.c:1417) do_sock_setsockopt (net/socket.c:2368) __sys_setsockopt (net/socket.c:2393) __x64_sys_setsockopt (net/socket.c:2396) do_syscall_64 (arch/x86/entry/syscall_64.c:94) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
Reachable by an unprivileged user via a user+network namespace.
Use the atomic sk_dst_reset() so the cache is cleared and released with a single xchg(): whichever side wins releases the dst once, the other sees NULL and does nothing. Behaviour is otherwise unchanged.
--- net/xfrm/xfrm_state.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/xfrm/xfrm_state.c +2 -2
@@ -3010,7 +3010,7 @@ int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)if (sockptr_is_null (optval) && !optlen) {xfrm_sk_policy_insert (sk,XFRM_POLICY_IN ,NULL );xfrm_sk_policy_insert (sk,XFRM_POLICY_OUT ,NULL ); -__sk_dst_reset (sk); +sk_dst_reset (sk);return 0 ; }
@@ -3050,7 +3050,7 @@ int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)if (err >=0 ) {xfrm_sk_policy_insert (sk, err, pol);xfrm_pol_put (pol); -__sk_dst_reset (sk); +sk_dst_reset (sk); err =0 ; }
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 5/10 xfrm: cache the offload ifindex for netlink dumps
In reply to Steffen KlassertFrom: Cen Zhang <[email protected]>
copy_to_user_state_extra() only holds a reference to the outer xfrm_state. That does not pin x->xso.dev. NETDEV_DOWN and NETDEV_UNREGISTER can race through xfrm_dev_state_flush(), xfrm_state_delete(), and xfrm_dev_state_free(), which clears xso->dev and drops the netdev reference before the GETSA dump reaches xso_to_xuo() and reads xso->dev->ifindex.
The buggy scenario involves two paths, with each column showing the order within that path:
XFRM_MSG_GETSA dump path: NETDEV teardown path: 1. xfrm_get_sa() gets xfrm_state 1. xfrm_dev_state_flush() finds x 2. copy_to_user_state_extra() sees 2. xfrm_state_delete() removes x
x->xso.dev from the SAD
3. copy_user_offload() calls 3. xfrm_dev_state_free() clears
xso_to_xuo() xso->dev
4. xso->dev->ifindex dereferences 4. netdev_put() drops the device
a detached net_device reference
Avoid following the live net_device from the dump paths. Cache the attached ifindex in xfrm_dev_offload when state or policy offload is bound to a device, and serialize that snapshot instead. This preserves the user-visible XFRMA_OFFLOAD_DEV value without depending on the embedded net_device lifetime.
Validation reproduced this kernel report:
Oops: general protection fault Call Trace: <TASK> copy_to_user_state_extra+0xb8d/0x1370 [xfrm_user] ? __pfx_copy_to_user_state_extra+0x10/0x10 [xfrm_user] ? __asan_memset+0x23/0x50 ? srso_alias_return_thunk+0x5/0xfbef5 ? __alloc_skb+0x342/0x960 ? srso_alias_return_thunk+0x5/0xfbef5 ? __asan_memset+0x23/0x50 ? srso_alias_return_thunk+0x5/0xfbef5 ? __nlmsg_put+0x147/0x1b0 dump_one_state+0x1c7/0x3e0 [xfrm_user] xfrm_state_netlink+0xcb/0x130 [xfrm_user] ? __pfx_xfrm_state_netlink+0x10/0x10 [xfrm_user] ? srso_alias_return_thunk+0x5/0xfbef5 ? xfrm_user_state_lookup.constprop.0+0x230/0x310 [xfrm_user] xfrm_get_sa+0x102/0x250 [xfrm_user] ? __pfx_xfrm_get_sa+0x10/0x10 [xfrm_user] xfrm_user_rcv_msg+0x504/0xaa0 [xfrm_user] ? __pfx_xfrm_user_rcv_msg+0x10/0x10 [xfrm_user] ? srso_alias_return_thunk+0x5/0xfbef5 ? stack_trace_save+0x8e/0xc0 ? __pfx_stack_trace_save+0x10/0x10 netlink_rcv_skb+0x11f/0x350 ? __pfx_xfrm_user_rcv_msg+0x10/0x10 [xfrm_user] ? __pfx_netlink_rcv_skb+0x10/0x10 ? __pfx_mutex_lock+0x10/0x10 ? srso_alias_return_thunk+0x5/0xfbef5 xfrm_netlink_rcv+0x65/0x80 [xfrm_user] netlink_unicast+0x600/0x870 ? __pfx_netlink_unicast+0x10/0x10 ? srso_alias_return_thunk+0x5/0xfbef5 ? __pfx_stack_trace_save+0x10/0x10 netlink_sendmsg+0x75d/0xc10 ? __pfx_netlink_sendmsg+0x10/0x10 ? srso_alias_return_thunk+0x5/0xfbef5 ____sys_sendmsg+0x77a/0x900 ? srso_alias_return_thunk+0x5/0xfbef5 ? __pfx_____sys_sendmsg+0x10/0x10 ? __pfx_copy_msghdr_from_user+0x10/0x10 ? release_sock+0x1a/0x1d0 ? srso_alias_return_thunk+0x5/0xfbef5 ? netlink_insert+0x143/0xec0 ___sys_sendmsg+0xff/0x180 ? __pfx____sys_sendmsg+0x10/0x10 ? _raw_spin_lock_irqsave+0x85/0xe0 ? do_getsockname+0xf9/0x170 ? srso_alias_return_thunk+0x5/0xfbef5 ? fdget+0x53/0x3b0 __sys_sendmsg+0x111/0x1a0 ? __pfx___sys_sendmsg+0x10/0x10 ? srso_alias_return_thunk+0x5/0xfbef5 ? __sys_getsockname+0x8c/0x100 do_syscall_64+0x102/0x5a0 entry_SYSCALL_64_after_hwframe+0x77/0x7f
--- include/net/xfrm.h | 2 ++ net/xfrm/xfrm_device.c | 1 + net/xfrm/xfrm_state.c | 1 + net/xfrm/xfrm_user.c | 38 +++++++++++++++++++++++++++++--------- 4 files changed, 33 insertions(+), 9 deletions(-)
include/net/xfrm.h +2 -0
@@ -162,6 +162,8 @@ struct xfrm_dev_offload {*/ struct net_device *real_dev;unsigned long offload_handle; +/* Snapshot the attached device index for dump paths. */ +int ifindex; u8 dir : 2; u8 type : 2; u8 flags : 2;
net/xfrm/xfrm_device.c +1 -0
@@ -321,6 +321,7 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, } xso->dev = dev; + xso->ifindex = dev->ifindex;netdev_tracker_alloc (dev, &xso->dev_tracker,GFP_ATOMIC );if (xuo->flags &XFRM_OFFLOAD_INBOUND )
net/xfrm/xfrm_state.c +1 -0
@@ -1547,6 +1547,7 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr, xso->type =XFRM_DEV_OFFLOAD_PACKET ; xso->dir = xdo->dir; xso->dev = dev; + xso->ifindex = dev->ifindex; xso->flags =XFRM_DEV_OFFLOAD_FLAG_ACQ ;netdev_hold (dev, &xso->dev_tracker,GFP_ATOMIC ); error = dev->xfrmdev_ops->xdo_dev_state_add(dev, x,
net/xfrm/xfrm_user.c +29 -9
@@ -1201,17 +1201,26 @@ static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)return 0 ; } -static void xso_to_xuo (const struct xfrm_dev_offload *xso, -struct xfrm_user_offload *xuo) +static void xso_to_xuo_ifindex (const struct xfrm_dev_offload *xso,int ifindex, +struct xfrm_user_offload *xuo) { - xuo->ifindex = xso->dev->ifindex; + xuo->ifindex = ifindex;if (xso->dir ==XFRM_DEV_OFFLOAD_IN ) xuo->flags =XFRM_OFFLOAD_INBOUND ;if (xso->type ==XFRM_DEV_OFFLOAD_PACKET ) xuo->flags |=XFRM_OFFLOAD_PACKET ; } -static int copy_user_offload (struct xfrm_dev_offload *xso,struct sk_buff *skb) +#ifdef CONFIG_XFRM_MIGRATE +static void xso_to_xuo (const struct xfrm_dev_offload *xso, +struct xfrm_user_offload *xuo) +{ +xso_to_xuo_ifindex (xso, xso->dev->ifindex, xuo); +} +#endif + +static int copy_user_offload_ifindex (const struct xfrm_dev_offload *xso, +int ifindex,struct sk_buff *skb) {struct xfrm_user_offload *xuo;struct nlattr *attr;
@@ -1222,11 +1231,22 @@ static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb) xuo =nla_data (attr);memset (xuo,0 ,sizeof (*xuo)); -xso_to_xuo (xso, xuo); +xso_to_xuo_ifindex (xso, ifindex, xuo);return 0 ; } +static int copy_user_offload (struct xfrm_dev_offload *xso,struct sk_buff *skb) +{ +return copy_user_offload_ifindex (xso, xso->dev->ifindex, skb); +} + +static int copy_user_state_offload (const struct xfrm_dev_offload *xso, +struct sk_buff *skb) +{ +return copy_user_offload_ifindex (xso,READ_ONCE (xso->ifindex), skb); +} +static bool xfrm_redact (void ) { return IS_ENABLED(CONFIG_SECURITY) &&
@@ -1433,8 +1453,8 @@ static int copy_to_user_state_extra(struct xfrm_state *x, &x->replay);if (ret) goto out; -if (x->xso.dev) - ret =copy_user_offload (&x->xso, skb); +if (READ_ONCE (x->xso.dev)) + ret =copy_user_state_offload (&x->xso, skb);if (ret) goto out;if (x->if_id) {
@@ -4046,8 +4066,8 @@ static inline unsigned int xfrm_sa_len(struct xfrm_state *x) l +=nla_total_size (sizeof (*x->coaddr));if (x->props.extra_flags) l +=nla_total_size (sizeof (x->props.extra_flags)); -if (x->xso.dev) - l +=nla_total_size (sizeof (struct xfrm_user_offload )); +if (READ_ONCE (x->xso.dev)) + l +=nla_total_size (sizeof (struct xfrm_user_offload ));if (x->props.smark.v | x->props.smark.m) { l +=nla_total_size (sizeof (x->props.smark.v)); l +=nla_total_size (sizeof (x->props.smark.m));
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 6/10 xfrm: reject optional IPTFS templates in outbound policies
In reply to Steffen KlassertFrom: Antony Antony <[email protected]>
syzbot reported a stack-out-of-bounds read in xfrm_state_find() which flows from xfrm_tmpl_resolve_one().
Commit 3d776e31c841 ("xfrm: Reject optional tunnel/BEET mode templates in outbound policies") disallowed optional tunnel and BEET in outbound policies to prevent this. Later when IPTFS added, it was not covered by that fix and can still trigger the out-of-bounds read;
Extend the check to disallow optional IPTFS in outbound policies as well. IPTFS should be identical to tunnel mode.
IN and FWD policies are not affected: xfrm_tmpl_resolve_one() is only reachable via the outbound path.
Reproducer, before:
ip link add dummy0 type dummy
ip link set dummy0 up
ip addr add 10.1.1.1/24 dev dummy0
ip xfrm policy add src 10.1.1.1/32 dst 10.1.1.2/32 dir out tmpl
src fc00::dead:1 dst fc00::dead:2 proto esp reqid 1 mode iptfs level use tmpl src fc00::dead:1 dst fc00::dead:2 proto esp reqid 2 mode transport
ping -W 1 -c 1 10.1.1.2
PING 10.1.1.2 (10.1.1.2) 56(84) bytes of data.
[ 64.168420] ================================================================== [ 64.169977] BUG: KASAN: stack-out-of-bounds in __xfrm6_addr_hash+0x11e/0x170 [ 64.169977] Read of size 4 at addr ffff88800e1ffd20 by task ping/2844
[ 64.169977] CPU: 2 UID: 0 PID: 2844 Comm: ping Not tainted 7.1.0-rc7-00180-geb23b588430a #98 PREEMPT(full) [ 64.169977] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 64.169977] Call Trace: [ 64.169977] <TASK> [ 64.169977] dump_stack_lvl+0x47/0x70 [ 64.169977] ? __xfrm6_addr_hash+0x11e/0x170 [ 64.169977] print_report+0x152/0x4b0 [ 64.169977] ? ksys_mmap_pgoff+0x6d/0xa0 [ 64.169977] ? entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 64.169977] ? rcu_read_unlock_sched+0xa/0x20 [ 64.169977] ? __virt_addr_valid+0x21b/0x230 [ 64.169977] ? __xfrm6_addr_hash+0x11e/0x170 [ 64.169977] kasan_report+0xa8/0xd0 [ 64.169977] ? __xfrm6_addr_hash+0x11e/0x170 [ 64.169977] __xfrm6_addr_hash+0x11e/0x170 [ 64.169977] __xfrm_dst_hash+0x24/0xc0 [ 64.169977] xfrm_state_find+0xa2d/0x2f90 [ 64.169977] ? __pfx_xfrm_state_find+0x10/0x10 [ 64.169977] ? __pfx_ftrace_graph_ret_addr+0x10/0x10 [ 64.169977] ? __pfx_ftrace_graph_ret_addr+0x10/0x10 [ 64.169977] xfrm_tmpl_resolve_one+0x210/0x570 [ 64.169977] ? __pfx_xfrm_tmpl_resolve_one+0x10/0x10 [ 64.169977] ? __pfx_stack_trace_consume_entry+0x10/0x10 [ 64.169977] ? kernel_text_address+0x5b/0x80 [ 64.169977] ? __kernel_text_address+0xe/0x30 [ 64.169977] ? unwind_get_return_address+0x5e/0x90 [ 64.169977] ? arch_stack_walk+0x8c/0xe0 [ 64.169977] xfrm_tmpl_resolve+0x130/0x200 [ 64.169977] ? __pfx_xfrm_tmpl_resolve+0x10/0x10 [ 64.169977] ? __pfx_xfrm_policy_inexact_lookup_rcu+0x10/0x10 [ 64.169977] ? __refcount_add_not_zero.constprop.0+0xb2/0x110 [ 64.169977] ? __pfx___refcount_add_not_zero.constprop.0+0x10/0x10 [ 64.169977] xfrm_resolve_and_create_bundle+0xd5/0x310 [ 64.169977] ? __pfx_xfrm_resolve_and_create_bundle+0x10/0x10 [ 64.169977] ? __pfx_xfrm_policy_lookup_bytype+0x10/0x10 [ 64.169977] ? __pfx_xfrm_policy_lookup_bytype+0x10/0x10 [ 64.169977] xfrm_lookup_with_ifid+0x3d8/0xb80 [ 64.169977] ? __pfx_xfrm_lookup_with_ifid+0x10/0x10 [ 64.169977] ? ip_route_output_key_hash+0xc6/0x110 [ 64.169977] ? kasan_save_track+0x10/0x30 [ 64.169977] xfrm_lookup_route+0x18/0xe0 [ 64.169977] ip4_datagram_release_cb+0x4c9/0x530 [ 64.169977] ? __pfx_ip4_datagram_release_cb+0x10/0x10 [ 64.169977] ? do_raw_spin_lock+0x71/0xc0 [ 64.169977] ? __pfx_do_raw_spin_lock+0x10/0x10 [ 64.169977] release_sock+0xb0/0x170 [ 64.169977] udp_connect+0x43/0x50 [ 64.169977] __sys_connect+0xa6/0x100 [ 64.169977] ? alloc_fd+0x2e9/0x300 [ 64.169977] ? __pfx___sys_connect+0x10/0x10 [ 64.169977] ? preempt_latency_start+0x1f/0x70 [ 64.169977] ? fd_install+0x7e/0x150 [ 64.169977] ? rcu_read_unlock_sched+0xa/0x20 [ 64.169977] ? __sys_socket+0xdf/0x130 [ 64.169977] ? __pfx___sys_socket+0x10/0x10 [ 64.169977] ? vma_refcount_put+0x43/0xa0 [ 64.169977] __x64_sys_connect+0x7e/0x90 [ 64.169977] do_syscall_64+0x11b/0x2b0 [ 64.169977] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 64.169977] RIP: 0033:0x7f4851ecb570 [ 64.169977] Code: 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 80 3d f9 ca 0d 00 00 74 17 b8 2a 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 58 c3 0f 1f 80 00 00 00 00 48 83 ec 18 89 54 [ 64.169977] RSP: 002b:00007ffc830e3498 EFLAGS: 00000202 ORIG_RAX: 000000000000002a [ 64.169977] RAX: ffffffffffffffda RBX: 00007ffc830e34d0 RCX: 00007f4851ecb570 [ 64.169977] RDX: 0000000000000010 RSI: 00007ffc830e34d0 RDI: 0000000000000005 [ 64.169977] RBP: 0000000000000000 R08: 0000000000000003 R09: 0000000000000000 [ 64.169977] R10: 0000000000000006 R11: 0000000000000202 R12: 0000000000000005 [ 64.169977] R13: 0000000000000000 R14: 00005619a863f340 R15: 0000000000000000 [ 64.169977] </TASK>
[ 64.169977] The buggy address belongs to stack of task ping/2844 [ 64.169977] and is located at offset 88 in frame: [ 64.169977] ip4_datagram_release_cb+0x0/0x530
[ 64.169977] This frame has 1 object: [ 64.169977] [32, 88) 'fl4'
[ 64.169977] The buggy address belongs to the physical page: [ 64.169977] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0xe1ff [ 64.169977] flags: 0x4000000000000000(zone=1) [ 64.169977] raw: 4000000000000000 0000000000000000 ffffea0000387fc8 0000000000000000 [ 64.169977] raw: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000000 [ 64.169977] page dumped because: kasan: bad access detected
[ 64.169977] Memory state around the buggy address: [ 64.169977] ffff88800e1ffc00: f2 f2 00 00 f3 f3 00 00 00 00 00 00 00 00 00 00 [ 64.169977] ffff88800e1ffc80: 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1 00 00 00 [ 64.169977] >ffff88800e1ffd00: 00 00 00 00 f3 f3 f3 f3 f3 00 00 00 00 00 00 00 [ 64.169977] ^ [ 64.169977] ffff88800e1ffd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 [ 64.169977] ffff88800e1ffe00: f1 f1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 64.169977] ================================================================== [ 64.245153] Disabling lock debugging due to kernel taint
After the fix:
ip xfrm policy add src 10.1.1.1/32 dst 10.1.1.2/32 dir out tmpl \
src fc00::dead:1 dst fc00::dead:2 proto esp reqid 1 mode iptfs \
level use tmpl src fc00::dead:1 dst fc00::dead:2 proto esp reqid 2 \
mode transport
Error: Mode in optional template not allowed in outbound policy.
--- net/xfrm/xfrm_user.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/xfrm/xfrm_user.c +1 -2
@@ -2124,13 +2124,12 @@ static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family,switch (ut[i].mode) {case XFRM_MODE_TUNNEL :case XFRM_MODE_BEET : +case XFRM_MODE_IPTFS :if (ut[i].optional && dir ==XFRM_POLICY_OUT ) {NL_SET_ERR_MSG (extack,"Mode in optional template not allowed in outbound policy" );return -EINVAL ; }break ; -case XFRM_MODE_IPTFS : -break ;default :if (ut[i].family != prev_family) {NL_SET_ERR_MSG (extack,"Mode in template doesn't support a family change" );
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 7/10 xfrm: clear mode callbacks after failed mode setup
In reply to Steffen KlassertFrom: Cen Zhang <[email protected]>
xfrm_state_gc_task can run long after a failed IPTFS state setup. In the reproduced case, __xfrm_init_state() cached x->mode_cbs, IPTFS setup returned -ENOMEM before publishing mode_data, and the temporary module reference from xfrm_get_mode_cbs() was dropped immediately. The dead state then kept x->mode_cbs until deferred GC ran after xfrm_iptfs had been unloaded.
Clear x->mode_cbs when mode init or clone fails before publishing mode_data. Those states never installed mode-specific state or the long-term IPTFS module pin, so deferred GC has nothing mode-specific to destroy and must not retain a callback table pointer past the temporary lookup reference.
The buggy scenario involves two paths, with each column showing the order within that path:
failed setup path:
1. cache x->mode_cbs
2. mode setup fails before mode_data
3. drop the temporary module ref
4. dead state keeps x->mode_cbs cached
GC/unload path:
1. xfrm_state_put() queues GC work
2. xfrm_iptfs unloads later
3. xfrm_state_gc_task runs
4. GC dereferences stale x->mode_cbs
This also covers the failed clone path where clone_state() returns before publishing mode_data.
Validation reproduced this kernel report:
Kernel panic - not syncing: Fatal exception
CONFIG_FAULT_INJECTION_STACKTRACE_FILTER=y failslab_stacktrace_filter matched xfrm_iptfs frames ack_error=-12FAULT_INJECTION: forcing a failure
BUG: unable to handle page fault Workqueue: events xfrm_state_gc_task RIP: xfrm_state_gc_task+0x142/0x650 Modules linked in: esp4_offload xfrm_user [last unloaded: xfrm_iptfs] Kernel panic - not syncing: Fatal exception
--- net/xfrm/xfrm_state.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
net/xfrm/xfrm_state.c +6 -1
@@ -2072,8 +2072,11 @@ static struct xfrm_state *xfrm_state_clone_and_setup(struct xfrm_state *orig, x->mode_cbs = orig->mode_cbs;if (x->mode_cbs && x->mode_cbs->clone_state) { -if (x->mode_cbs->clone_state (x, orig)) +if (x->mode_cbs->clone_state (x, orig)) { +if (!x->mode_data) + x->mode_cbs =NULL ; goto error; + } } x->props.reqid = m->new_reqid;
@@ -3292,6 +3295,8 @@ int __xfrm_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)if (x->mode_cbs->init_state) err = x->mode_cbs->init_state (x);module_put (x->mode_cbs->owner); +if (err && !x->mode_data) + x->mode_cbs =NULL ; } error:return err;
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 8/10 xfrm: iptfs: propagate SKBFL_SHARED_FRAG in iptfs_skb_add_frags()
In reply to Steffen KlassertFrom: Chen YanJun <[email protected]>
When iptfs_skb_add_frags() copies frag references from the source frag walk into a new SKB, it increments the page reference count via __skb_frag_ref() but does not propagate SKBFL_SHARED_FRAG to the destination SKB's skb_shinfo->flags.
If the source SKB carries shared frags (e.g. from a page-pool backed receive path), the new inner SKB will appear to ESP as having privately owned frags. A subsequent esp_input() call for a nested transport-mode SA then takes the no-COW fast path and decrypts in place, writing over pages that are still referenced by the outer IPTFS SKB. This causes kernel-visible memory corruption and can trigger a panic.
All other frag-transfer helpers in the kernel (skb_try_coalesce, skb_gro_receive, __pskb_copy_fclone, skb_shift, skb_segment) correctly propagate SKBFL_SHARED_FRAG; align iptfs_skb_add_frags() with this convention by setting the flag inside the loop immediately after __skb_frag_ref() and nr_frags++, so every exit path that attaches a frag unconditionally propagates SKBFL_SHARED_FRAG.
--- net/xfrm/xfrm_iptfs.c | 1 + 1 file changed, 1 insertion(+)
net/xfrm/xfrm_iptfs.c +1 -0
@@ -480,6 +480,7 @@ static int iptfs_skb_add_frags(struct sk_buff *skb, }__skb_frag_ref (tofrag); shinfo->nr_frags++; + shinfo->flags |=SKBFL_SHARED_FRAG ;/* see if we are done */ fraglen = tofrag->len;
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 9/10 xfrm6: clear dst.dev on error to avoid double netdev_put in xfrm6_fill_dst()
In reply to Steffen KlassertFrom: "Xiang Mei (Microsoft)" <[email protected]>
On the error path where in6_dev_get(dev) returns NULL, xfrm6_fill_dst() releases the device reference with netdev_put() but leaves xdst->u.dst.dev set. dst_destroy() later calls netdev_put(dst->dev) again, so the same net_device reference is released twice, underflowing its refcount (ref_tracker WARNING + "unregister_netdevice: waiting for <dev> to become free").
Clear xdst->u.dst.dev after the netdev_put(), the same way the XFRM device-offload paths xfrm_dev_state_add() and xfrm_dev_policy_add() in net/xfrm/xfrm_device.c NULL ->dev when releasing the reference on error.
ref_tracker: reference already released.
ref_tracker: allocated in:
xfrm6_fill_dst (net/ipv6/xfrm6_policy.c:86)
...
udpv6_sendmsg (net/ipv6/udp.c:1696)
...
ref_tracker: freed in:
xfrm6_fill_dst (net/ipv6/xfrm6_policy.c:90)
...
WARNING: lib/ref_tracker.c:322 at ref_tracker_free+0x58b/0x780 dst_destroy (net/core/dst.c:115) rcu_core handle_softirqs ...
--- net/ipv6/xfrm6_policy.c | 1 + 1 file changed, 1 insertion(+)
net/ipv6/xfrm6_policy.c +1 -0
@@ -88,6 +88,7 @@ static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, xdst->u.rt6.rt6i_idev =in6_dev_get (dev);if (!xdst->u.rt6.rt6i_idev) {netdev_put (dev, &xdst->u.dst.dev_tracker); + xdst->u.dst.dev =NULL ;return -ENODEV ; }
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 10/10 xfrm: policy: preallocate inexact bins before xfrm_hash_rebuild reinsert
In reply to Steffen KlassertFrom: "Xiang Mei (Microsoft)" <[email protected]>
xfrm_hash_rebuild()'s first loop preallocates the bins/chains the reinsert loop needs, so the reinsert (after hlist_del_rcu()) cannot allocate or fail. But its guard is inverted: it skips policies with prefixlen < threshold and preallocates for the rest.
prefixlen < threshold is exactly when policy_hash_bysel() returns NULL and the reinsert takes the allocating xfrm_policy_inexact_insert() path. So the loop preallocates for the exact policies (which never allocate) and skips the inexact ones, whose bin/node is then allocated GFP_ATOMIC during reinsert. On failure the error path only WARN_ONCE()s and continues, leaving a poisoned bydst node; the next rebuild's hlist_del_rcu() dereferences LIST_POISON2 and takes a GPF. Reachable under memory pressure, deterministic via failslab.
Invert the guard so preallocation covers exactly the reinserted policies; the reinsert then allocates nothing and cannot fail.
Crash:
Oops: general protection fault, probably for non-canonical address 0xfbd59c0000000024: 0000 [#1] SMP KASAN NOPTI KASAN: maybe wild-memory-access in range [0xdead...] ... Workqueue: events xfrm_hash_rebuild RIP: 0010:xfrm_hash_rebuild+0x5b3/0x1190 RAX: dead000000000122 (LIST_POISON2 + offset) ... Call Trace: hlist_del_rcu (include/linux/rculist.h:599) xfrm_hash_rebuild (net/xfrm/xfrm_policy.c:1365) process_one_work (kernel/workqueue.c:3322) worker_thread (kernel/workqueue.c:3486) kthread (kernel/kthread.c:436) ret_from_fork (arch/x86/kernel/process.c:158) ret_from_fork_asm (arch/x86/entry/entry_64.S:245) ... Kernel panic - not syncing: Fatal exception in interrupt
--- net/xfrm/xfrm_policy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/xfrm/xfrm_policy.c +2 -2
@@ -1329,8 +1329,8 @@ static void xfrm_hash_rebuild(struct work_struct *work) } } -if (policy->selector.prefixlen_d < dbits || - policy->selector.prefixlen_s < sbits) +if (policy->selector.prefixlen_d >= dbits && + policy->selector.prefixlen_s >= sbits)continue ; bin =xfrm_policy_inexact_alloc_bin (policy, dir);
Signature · 1 line
2.43.0