pull request (net): ipsec 2025-11-18
Subsystems (3)
Steffen Klassert
maintainercover pull request (net): ipsec 2025-11-18
1) Misc fixes for xfrm_state creation/modification/deletion. Patchset from Sabrina Dubroca.
2) Fix inner packet family determination for xfrm offloads. From Jianbo Liu.
3) Don't push locally generated packets directly to L2 tunnel mode offloading, they still need processing from the standard xfrm path. From Jianbo Liu.
4) Fix memory leaks in xfrm_add_acquire for policy offloads and policy security contexts. From Zilin Guan.
Please pull or let me know if there are problems.
Thanks!
The following changes since commit f584239a9ed25057496bf397c370cc5163dde419:
net/smc: fix general protection fault in __smc_diag_dump (2025-10-20 17:46:06 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git tags/ipsec-2025-11-18
for you to fetch changes up to a55ef3bff84f11ee8c84a1ae29b071ffd4ccbbd9:
xfrm: fix memory leak in xfrm_add_acquire() (2025-11-14 10:12:36 +0100)
----------------------------------------------------------------
ipsec-2025-11-18
----------------------------------------------------------------
Jianbo Liu (3):
xfrm: Check inner packet family directly from skb_dst
xfrm: Determine inner GSO type from packet inner protocol
xfrm: Prevent locally generated packets from direct output in tunnel mode
Sabrina Dubroca (6):
xfrm: drop SA reference in xfrm_state_update if dir doesn't match
xfrm: also call xfrm_state_delete_tunnel at destroy time for states that were never added
xfrm: make state as DEAD before final put when migrate fails
xfrm: call xfrm_dev_state_delete when xfrm_state_migrate fails to add the state
xfrm: set err and extack on failure to create pcpu SA
xfrm: check all hash buckets for leftover states during netns deletion
Zilin Guan (1):
xfrm: fix memory leak in xfrm_add_acquire()
include/net/xfrm.h | 3 ++- net/ipv4/esp4_offload.c | 6 ++++-- net/ipv6/esp6_offload.c | 6 ++++-- net/xfrm/xfrm_device.c | 2 +- net/xfrm/xfrm_output.c | 8 ++++++-- net/xfrm/xfrm_state.c | 30 ++++++++++++++++++++++-------- net/xfrm/xfrm_user.c | 8 +++++++- 7 files changed, 46 insertions(+), 17 deletions(-)
Steffen Klassert
maintainerPATCH 1/10 xfrm: drop SA reference in xfrm_state_update if dir doesn't match
From: Sabrina Dubroca <[email protected]>
We're not updating x1, but we still need to put() it.
--- net/xfrm/xfrm_state.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
net/xfrm/xfrm_state.c +6 -2
@@ -2191,14 +2191,18 @@ int xfrm_state_update(struct xfrm_state *x) }if (x1->km.state ==XFRM_STATE_ACQ ) { -if (x->dir && x1->dir != x->dir) +if (x->dir && x1->dir != x->dir) { + to_put = x1; goto out; + }__xfrm_state_insert (x); x =NULL ; }else { -if (x1->dir != x->dir) +if (x1->dir != x->dir) { + to_put = x1; goto out; + } } err =0 ;
Signature · 1 line
2.43.0
Re: [PATCH 01/10] xfrm: drop SA reference in xfrm_state_update if dir doesn't match
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 9 lines · On Tue, 18 Nov 2025 09:52:34 +0100 you wrote:
From: Sabrina Dubroca <[email protected]>
We're not updating x1, but we still need to
put()it.Fixes a4a87fa4e96c ("xfrm: Add Direction to the SA in or out")Signed-off-by Sabrina Dubroca <[email protected]>Signed-off-by Steffen Klassert <[email protected]>[...]
Here is the summary with links:
- [01/10] xfrm: drop SA reference in xfrm_state_update if dir doesn't match
https://git.kernel.org/netdev/net/c/8d2a2a49c30f - [02/10] xfrm: also call xfrm_state_delete_tunnel at destroy time for states that were never added https://git.kernel.org/netdev/net/c/10deb6986484 - [03/10] xfrm: make state as DEAD before final put when migrate fails https://git.kernel.org/netdev/net/c/5502bc4746e8 - [04/10] xfrm: call xfrm_dev_state_delete when xfrm_state_migrate fails to add the state https://git.kernel.org/netdev/net/c/7f0228576479 - [05/10] xfrm: set err and extack on failure to create pcpu SA https://git.kernel.org/netdev/net/c/1dcf617bec5c - [06/10] xfrm: check all hash buckets for leftover states during netns deletion https://git.kernel.org/netdev/net/c/f2bc8231fd43 - [07/10] xfrm: Check inner packet family directly from skb_dst https://git.kernel.org/netdev/net/c/082ef944e55d - [08/10] xfrm: Determine inner GSO type from packet inner protocol https://git.kernel.org/netdev/net/c/61fafbee6cfe - [09/10] xfrm: Prevent locally generated packets from direct output in tunnel mode https://git.kernel.org/netdev/net/c/59630e2ccd72 - [10/10] xfrm: fix memory leak in xfrm_add_acquire() https://git.kernel.org/netdev/net/c/a55ef3bff84f
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: also call xfrm_state_delete_tunnel at destroy time for states that were never added
In reply to Steffen KlassertFrom: Sabrina Dubroca <[email protected]>
In commit b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x"), I missed the case where state creation fails between full initialization (->init_state has been called) and being inserted on the lists.
In this situation, ->init_state has been called, so for IPcomp tunnels, the fallback tunnel has been created and added onto the lists, but the user state never gets added, because we fail before that. The user state doesn't go through __xfrm_state_delete, so we don't call xfrm_state_delete_tunnel for those states, and we end up leaking the FB tunnel.
There are several codepaths affected by this: the add/update paths, in both net/key and xfrm, and the migrate code (xfrm_migrate, xfrm_state_migrate). A "proper" rollback of the init_state work would probably be doable in the add/update code, but for migrate it gets more complicated as multiple states may be involved.
At some point, the new (not-inserted) state will be destroyed, so call xfrm_state_delete_tunnel during xfrm_state_gc_destroy. Most states will have their fallback tunnel cleaned up during __xfrm_state_delete, which solves the issue that b441cf3f8c4b (and other patches before it) aimed at. All states (including FB tunnels) will be removed from the lists once xfrm_state_fini has called flush_work(&xfrm_state_gc_work).
--- net/xfrm/xfrm_state.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/xfrm/xfrm_state.c +2 -1
@@ -592,6 +592,7 @@ void xfrm_state_free(struct xfrm_state *x) }EXPORT_SYMBOL (xfrm_state_free); +static void xfrm_state_delete_tunnel (struct xfrm_state *x);static void xfrm_state_gc_destroy (struct xfrm_state *x) {if (x->mode_cbs && x->mode_cbs->destroy_state)
@@ -607,6 +608,7 @@ static void xfrm_state_gc_destroy(struct xfrm_state *x)kfree (x->replay_esn);kfree (x->preplay_esn);xfrm_unset_type_offload (x); +xfrm_state_delete_tunnel (x);if (x->type) { x->type->destructor (x);xfrm_put_type (x->type);
@@ -806,7 +808,6 @@ void __xfrm_state_destroy(struct xfrm_state *x) }EXPORT_SYMBOL (__xfrm_state_destroy); -static void xfrm_state_delete_tunnel (struct xfrm_state *x);int __xfrm_state_delete (struct xfrm_state *x) {struct net *net =xs_net (x);
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 3/10 xfrm: make state as DEAD before final put when migrate fails
In reply to Steffen KlassertFrom: Sabrina Dubroca <[email protected]>
xfrm_state_migrate/xfrm_state_clone_and_setup create a new state, and call xfrm_state_put to destroy it in case of
failure. __xfrm_state_destroy expects the state to be in XFRM_STATE_DEAD, but we currently don't do that.
--- net/xfrm/xfrm_state.c | 2 ++ 1 file changed, 2 insertions(+)
net/xfrm/xfrm_state.c +2 -0
@@ -2074,6 +2074,7 @@ static struct xfrm_state *xfrm_state_clone_and_setup(struct xfrm_state *orig,return x; error: + x->km.state =XFRM_STATE_DEAD ;xfrm_state_put (x); out:return NULL ;
@@ -2163,6 +2164,7 @@ struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,return xc; error: + xc->km.state =XFRM_STATE_DEAD ;xfrm_state_put (xc);return NULL ; }
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 4/10 xfrm: call xfrm_dev_state_delete when xfrm_state_migrate fails to add the state
In reply to Steffen KlassertFrom: Sabrina Dubroca <[email protected]>
In case xfrm_state_migrate fails after calling xfrm_dev_state_add, we directly release the last reference and destroy the new state, without calling xfrm_dev_state_delete (this only happens in __xfrm_state_delete, which we're not calling on this path, since the state was never added).
Call xfrm_dev_state_delete on error when an offload configuration was provided.
--- net/xfrm/xfrm_state.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/xfrm/xfrm_state.c +4 -1
@@ -2159,10 +2159,13 @@ struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,xfrm_state_insert (xc); }else {if (xfrm_state_add (xc) <0 ) - goto error; + goto error_add; }return xc; +error_add: +if (xuo) +xfrm_dev_state_delete (xc); error: xc->km.state =XFRM_STATE_DEAD ;xfrm_state_put (xc);
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 5/10 xfrm: set err and extack on failure to create pcpu SA
In reply to Steffen KlassertFrom: Sabrina Dubroca <[email protected]>
xfrm_state_construct can fail without setting an error if the requested pcpu_num value is too big. Set err and add an extack message to avoid confusing userspace.
--- net/xfrm/xfrm_user.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/xfrm/xfrm_user.c +4 -1
@@ -947,8 +947,11 @@ static struct xfrm_state *xfrm_state_construct(struct net *net,if (attrs[XFRMA_SA_PCPU ]) { x->pcpu_num =nla_get_u32 (attrs[XFRMA_SA_PCPU ]); -if (x->pcpu_num >=num_possible_cpus ()) +if (x->pcpu_num >=num_possible_cpus ()) { + err = -ERANGE ; +NL_SET_ERR_MSG (extack,"pCPU number too big" ); goto error; + } } err =__xfrm_init_state (x, extack);
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 6/10 xfrm: check all hash buckets for leftover states during netns deletion
In reply to Steffen KlassertFrom: Sabrina Dubroca <[email protected]>
The current hlist_empty checks only test the first bucket of each hashtable, ignoring any other bucket. They should be caught by the WARN_ON for state_all, but better to make all the checks accurate.
--- net/xfrm/xfrm_state.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
net/xfrm/xfrm_state.c +8 -4
@@ -3308,6 +3308,7 @@ int __net_init xfrm_state_init(struct net *net)void xfrm_state_fini (struct net *net) {unsigned int sz; +int i;flush_work (&net->xfrm.state_hash_work);xfrm_state_flush (net,0 , false);
@@ -3315,14 +3316,17 @@ void xfrm_state_fini(struct net *net)WARN_ON (!list_empty (&net->xfrm.state_all)); +for (i =0 ; i <= net->xfrm.state_hmask; i++) { +WARN_ON (!hlist_empty (net->xfrm.state_byseq + i)); +WARN_ON (!hlist_empty (net->xfrm.state_byspi + i)); +WARN_ON (!hlist_empty (net->xfrm.state_bysrc + i)); +WARN_ON (!hlist_empty (net->xfrm.state_bydst + i)); + } + sz = (net->xfrm.state_hmask +1 ) *sizeof (struct hlist_head ); -WARN_ON (!hlist_empty (net->xfrm.state_byseq));xfrm_hash_free (net->xfrm.state_byseq, sz); -WARN_ON (!hlist_empty (net->xfrm.state_byspi));xfrm_hash_free (net->xfrm.state_byspi, sz); -WARN_ON (!hlist_empty (net->xfrm.state_bysrc));xfrm_hash_free (net->xfrm.state_bysrc, sz); -WARN_ON (!hlist_empty (net->xfrm.state_bydst));xfrm_hash_free (net->xfrm.state_bydst, sz);free_percpu (net->xfrm.state_cache_input); }
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 7/10 xfrm: Check inner packet family directly from skb_dst
In reply to Steffen KlassertFrom: Jianbo Liu <[email protected]>
In the output path, xfrm_dev_offload_ok and xfrm_get_inner_ipproto need to determine the protocol family of the inner packet (skb) before it gets encapsulated.
In xfrm_dev_offload_ok, the code checked x->inner_mode.family. This is unreliable because, for states handling both IPv4 and IPv6, the relevant inner family could be either x->inner_mode.family or x->inner_mode_iaf.family. Checking only the former can lead to a mismatch with the actual packet being processed.
In xfrm_get_inner_ipproto, the code checked x->outer_mode.family. This is also incorrect for tunnel mode, as the inner packet's family can be different from the outer header's family.
At both of these call sites, the skb variable holds the original inner packet. The most direct and reliable source of truth for its protocol family is its destination entry. This patch fixes the issue by using skb_dst(skb)->ops->family to ensure protocol-specific headers are only accessed for the correct packet type.
--- net/xfrm/xfrm_device.c | 2 +- net/xfrm/xfrm_output.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
net/xfrm/xfrm_device.c +1 -1
@@ -438,7 +438,7 @@ bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x) check_tunnel_size = x->xso.type ==XFRM_DEV_OFFLOAD_PACKET && x->props.mode ==XFRM_MODE_TUNNEL ; -switch (x->inner_mode.family) { +switch (skb_dst (skb)->ops->family) {case AF_INET :/* Check for IPv4 options */ if (ip_hdr (skb)->ihl !=5 )
net/xfrm/xfrm_output.c +1 -1
@@ -698,7 +698,7 @@ static void xfrm_get_inner_ipproto(struct sk_buff *skb, struct xfrm_state *x)return ;if (x->outer_mode.encap ==XFRM_MODE_TUNNEL ) { -switch (x->outer_mode.family) { +switch (skb_dst (skb)->ops->family) {case AF_INET : xo->inner_ipproto =ip_hdr (skb)->protocol;break ;
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 8/10 xfrm: Determine inner GSO type from packet inner protocol
In reply to Steffen KlassertFrom: Jianbo Liu <[email protected]>
The GSO segmentation functions for ESP tunnel mode (xfrm4_tunnel_gso_segment and xfrm6_tunnel_gso_segment) were determining the inner packet's L2 protocol type by checking the static x->inner_mode.family field from the xfrm state.
This is unreliable. In tunnel mode, the state's actual inner family could be defined by x->inner_mode.family or by x->inner_mode_iaf.family. Checking only the former can lead to a mismatch with the actual packet being processed, causing GSO to create segments with the wrong L2 header type.
This patch fixes the bug by deriving the inner mode directly from the packet's inner protocol stored in XFRM_MODE_SKB_CB(skb)->protocol.
Instead of replicating the code, this patch modifies the xfrm_ip2inner_mode helper function. It now correctly returns &x->inner_mode if the selector family (x->sel.family) is already specified, thereby handling both specific and AF_UNSPEC cases appropriately.
With this change, ESP GSO can use xfrm_ip2inner_mode to get the correct inner mode. It doesn't affect existing callers, as the updated logic now mirrors the checks they were already performing externally.
--- include/net/xfrm.h | 3 ++- net/ipv4/esp4_offload.c | 6 ++++-- net/ipv6/esp6_offload.c | 6 ++++-- 3 files changed, 10 insertions(+), 5 deletions(-)
include/net/xfrm.h +2 -1
@@ -536,7 +536,8 @@ static inline int xfrm_af2proto(unsigned int family)static inline const struct xfrm_mode *xfrm_ip2inner_mode (struct xfrm_state *x,int ipproto) { -if ((ipproto ==IPPROTO_IPIP && x->props.family ==AF_INET ) || +if ((x->sel.family !=AF_UNSPEC ) || + (ipproto ==IPPROTO_IPIP && x->props.family ==AF_INET ) || (ipproto ==IPPROTO_IPV6 && x->props.family ==AF_INET6 ))return &x->inner_mode;else
net/ipv4/esp4_offload.c +4 -2
@@ -122,8 +122,10 @@ static struct sk_buff *xfrm4_tunnel_gso_segment(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features) { -__be16 type = x->inner_mode.family ==AF_INET6 ?htons (ETH_P_IPV6 ) - :htons (ETH_P_IP ); +const struct xfrm_mode *inner_mode =xfrm_ip2inner_mode (x, +XFRM_MODE_SKB_CB (skb)->protocol); +__be16 type = inner_mode->family ==AF_INET6 ?htons (ETH_P_IPV6 ) + :htons (ETH_P_IP );return skb_eth_gso_segment (skb, features, type); }
net/ipv6/esp6_offload.c +4 -2
@@ -158,8 +158,10 @@ static struct sk_buff *xfrm6_tunnel_gso_segment(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features) { -__be16 type = x->inner_mode.family ==AF_INET ?htons (ETH_P_IP ) - :htons (ETH_P_IPV6 ); +const struct xfrm_mode *inner_mode =xfrm_ip2inner_mode (x, +XFRM_MODE_SKB_CB (skb)->protocol); +__be16 type = inner_mode->family ==AF_INET ?htons (ETH_P_IP ) + :htons (ETH_P_IPV6 );return skb_eth_gso_segment (skb, features, type); }
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 9/10 xfrm: Prevent locally generated packets from direct output in tunnel mode
In reply to Steffen KlassertFrom: Jianbo Liu <[email protected]>
Add a check to ensure locally generated packets (skb->sk != NULL) do not use direct output in tunnel mode, as these packets require proper L2 header setup that is handled by the normal XFRM processing path.
--- net/xfrm/xfrm_output.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
net/xfrm/xfrm_output.c +5 -1
@@ -772,8 +772,12 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)/* Exclusive direct xmit for tunnel mode, as * some filtering or matching rules may apply * in transport mode. +* Locally generated packets also require +* the normal XFRM path for L2 header setup, +* as the hardware needs the L2 header to match +* for encryption, so skip direct output as well. */ -if (x->props.mode ==XFRM_MODE_TUNNEL ) +if (x->props.mode ==XFRM_MODE_TUNNEL && !skb->sk)return xfrm_dev_direct_output (sk, x, skb);return xfrm_output_resume (sk, skb,0 );
Signature · 1 line
2.43.0
Steffen Klassert
maintainerPATCH 10/10 xfrm: fix memory leak in xfrm_add_acquire()
In reply to Steffen KlassertFrom: Zilin Guan <[email protected]>
The xfrm_add_acquire() function constructs an xfrm policy by calling xfrm_policy_construct(). This allocates the policy structure and potentially associates a security context and a device policy with it.
However, at the end of the function, the policy object is freed using only kfree() . This skips the necessary cleanup for the security context and device policy, leading to a memory leak.
To fix this, invoke the proper cleanup functions xfrm_dev_policy_delete(), xfrm_dev_policy_free(), and security_xfrm_policy_free() before freeing the policy object. This approach mirrors the error handling path in xfrm_add_policy(), ensuring that all associated resources are correctly released.
--- net/xfrm/xfrm_user.c | 3 +++ 1 file changed, 3 insertions(+)
net/xfrm/xfrm_user.c +3 -0
@@ -3038,6 +3038,9 @@ static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, }xfrm_state_free (x); +xfrm_dev_policy_delete (xp); +xfrm_dev_policy_free (xp); +security_xfrm_policy_free (xp->security);kfree (xp);return 0 ;
Signature · 1 line
2.43.0