Skip to contentSkip to conversation

pull request (net): ipsec 2025-05-21

PATCH Applied Steffen Klassert 2 participants 7 messages May 21, 2025 – May 22, 2025

Subsystems (3)
lore
Share & tools
lore

Steffen Klassert

author maintainer
#0 lore raw

cover pull request (net): ipsec 2025-05-21

1) Fix some missing kfree_skb in the error paths of espintcp. From Sabrina Dubroca.

2) Fix a reference leak in espintcp.
From Sabrina Dubroca.

3) Fix UDP GRO handling for ESPINUDP.
From Tobias Brunner.

4) Fix ipcomp truesize computation on the receive path. From Sabrina Dubroca.

5) Sanitize marks before policy/state insertation. From Paul Chaignon.

Please pull or let me know if there are problems.

Thanks!

The following changes since commit cfe82469a00f0c0983bf4652de3a2972637dfc56:

ipv6: add exception routes to GC list in rt6_insert_exception (2025-04-10 20:09:05 -0700)

are available in the Git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git tags/ipsec-2025-05-21

for you to fetch changes up to 0b91fda3a1f044141e1e615456ff62508c32b202:

xfrm: Sanitize marks before insert (2025-05-14 07:18:58 +0200)

----------------------------------------------------------------
ipsec-2025-05-21

----------------------------------------------------------------
Paul Chaignon (1):
xfrm: Sanitize marks before insert

Sabrina Dubroca (3):
espintcp: fix skb leaks
espintcp: remove encap socket caching to avoid reference leak
xfrm: ipcomp: fix truesize computation on receive

Tobias Brunner (1):
xfrm: Fix UDP GRO handling for some corner cases

 include/net/xfrm.h     |  1 -
 net/ipv4/esp4.c        | 53 +++++++-------------------------------------------
 net/ipv4/xfrm4_input.c | 18 +++++++++--------
 net/ipv6/esp6.c        | 53 +++++++-------------------------------------------
 net/ipv6/xfrm6_input.c | 18 +++++++++--------
 net/xfrm/espintcp.c    |  4 +++-
 net/xfrm/xfrm_ipcomp.c |  3 +--
 net/xfrm/xfrm_policy.c |  3 +++
 net/xfrm/xfrm_state.c  |  6 +++---
 9 files changed, 44 insertions(+), 115 deletions(-)
author maintainer
#1 lore raw

PATCH 1/5 espintcp: fix skb leaks

From: Sabrina Dubroca <[email protected]>

A few error paths are missing a kfree_skb.

Fixes e27cca96cd68 ("xfrm: add espintcp (RFC 8229)")
---
 net/ipv4/esp4.c     | 4 +++-
 net/ipv6/esp6.c     | 4 +++-
 net/xfrm/espintcp.c | 4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)
net/ipv4/esp4.c +3 -1
index 0e4076866c0a..876df672c0bf 100644
@@ -199,8 +199,10 @@ static int esp_output_tcp_finish(struct xfrm_state *x, struct sk_buff *skb)
 
 	sk = esp_find_tcp_sk(x);
 	err = PTR_ERR_OR_ZERO(sk);
-	if (err)
+	if (err) {
+		kfree_skb(skb);
 		goto out;
+	}
 
 	bh_lock_sock(sk);
 	if (sock_owned_by_user(sk))
net/ipv6/esp6.c +3 -1
index 9e73944e3b53..574989b82179 100644
@@ -216,8 +216,10 @@ static int esp_output_tcp_finish(struct xfrm_state *x, struct sk_buff *skb)
 
 	sk = esp6_find_tcp_sk(x);
 	err = PTR_ERR_OR_ZERO(sk);
-	if (err)
+	if (err) {
+		kfree_skb(skb);
 		goto out;
+	}
 
 	bh_lock_sock(sk);
 	if (sock_owned_by_user(sk))
net/xfrm/espintcp.c +3 -1
index fe82e2d07300..fc7a603b04f1 100644
@@ -171,8 +171,10 @@ int espintcp_queue_out(struct sock *sk, struct sk_buff *skb)
 	struct espintcp_ctx *ctx = espintcp_getctx(sk);
 
 	if (skb_queue_len(&ctx->out_queue) >=
-	    READ_ONCE(net_hotdata.max_backlog))
+	    READ_ONCE(net_hotdata.max_backlog)) {
+		kfree_skb(skb);
 		return -ENOBUFS;
+	}
 
 	__skb_queue_tail(&ctx->out_queue, skb);
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/5] espintcp: fix skb leaks

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 10 lines · On Wed, 21 May 2025 07:43:44 +0200 you wrote:

From: Sabrina Dubroca <[email protected]>

A few error paths are missing a kfree_skb.

Fixes e27cca96cd68 ("xfrm: add espintcp (RFC 8229)")

[...]

Here is the summary with links:
- [1/5] espintcp: fix skb leaks

    https://git.kernel.org/netdev/net/c/63c1f19a3be3
  - [2/5] espintcp: remove encap socket caching to avoid reference leak
    https://git.kernel.org/netdev/net/c/028363685bd0
  - [3/5] xfrm: Fix UDP GRO handling for some corner cases
    https://git.kernel.org/netdev/net/c/e3fd05777685
  - [4/5] xfrm: ipcomp: fix truesize computation on receive
    https://git.kernel.org/netdev/net/c/417fae2c4089
  - [5/5] xfrm: Sanitize marks before insert
    https://git.kernel.org/netdev/net/c/0b91fda3a1f0

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
author maintainer
#3 lore raw

PATCH 2/5 espintcp: remove encap socket caching to avoid reference leak

In reply to Steffen Klassert

From: Sabrina Dubroca <[email protected]>

The current scheme for caching the encap socket can lead to reference leaks when we try to delete the netns.

The reference chain is: xfrm_state -> enacp_sk -> netns

Since the encap socket is a userspace socket, it holds a reference on the netns. If we delete the espintcp state (through flush or individual delete) before removing the netns, the reference on the socket is dropped and the netns is correctly deleted. Otherwise, the netns may not be reachable anymore (if all processes within the ns have terminated), so we cannot delete the xfrm state to drop its reference on the socket.

This patch results in a small (~2% in my tests) performance regression.

A GC-type mechanism could be added for the socket cache, to clear references if the state hasn't been used "recently", but it's a lot more complex than just not caching the socket.

Fixes e27cca96cd68 ("xfrm: add espintcp (RFC 8229)")
---
 include/net/xfrm.h    |  1 -
 net/ipv4/esp4.c       | 49 ++++---------------------------------------
 net/ipv6/esp6.c       | 49 ++++---------------------------------------
 net/xfrm/xfrm_state.c |  3 ---
 4 files changed, 8 insertions(+), 94 deletions(-)
include/net/xfrm.h +0 -1
index 39365fd2ea17..06ab2a3d2ebd 100644
@@ -236,7 +236,6 @@ struct xfrm_state {
 
 	/* Data for encapsulator */
 	struct xfrm_encap_tmpl	*encap;
-	struct sock __rcu	*encap_sk;
 
 	/* NAT keepalive */
 	u32			nat_keepalive_interval; /* seconds */
net/ipv4/esp4.c +4 -45

83 lines not on this page · raw message

net/ipv6/esp6.c +4 -45

83 lines not on this page · raw message

net/xfrm/xfrm_state.c +0 -3
index 341d79ecb5c2..2f57dabcc70b 100644
@@ -838,9 +838,6 @@ int __xfrm_state_delete(struct xfrm_state *x)
 		xfrm_nat_keepalive_state_updated(x);
 		spin_unlock(&net->xfrm.xfrm_state_lock);
 
-		if (x->encap_sk)
-			sock_put(rcu_dereference_raw(x->encap_sk));
-
 		xfrm_dev_state_delete(x);
 
 		/* All xfrm_state objects are created by xfrm_state_alloc.
Signature · 1 line
2.34.1
author maintainer
#4 lore raw

PATCH 3/5 xfrm: Fix UDP GRO handling for some corner cases

In reply to Steffen Klassert

From: Tobias Brunner <[email protected]>

This fixes an issue that's caused if there is a mismatch between the data offset in the GRO header and the length fields in the regular sk_buff due to the pskb_pull()/skb_push() calls. That's because the UDP GRO layer stripped off the UDP header via skb_gro_pull() already while the UDP header was explicitly not pulled/pushed in this function.

For example, an IKE packet that triggered this had len=data_len=1268 and the data_offset in the GRO header was 28 (IPv4 + UDP). So pskb_pull() was called with an offset of 28-8=20, which reduced len to 1248 and via pskb_may_pull() and __pskb_pull_tail() it also set data_len to 1248. As the ESP offload module was not loaded, the function bailed out and called skb_push(), which restored len to 1268, however, data_len remained at 1248.

So while skb_headlen() was 0 before, it was now 20. The latter caused a difference of 8 instead of 28 (or 0 if pskb_pull()/skb_push() was called with the complete GRO data_offset) in gro_try_pull_from_frag0() that triggered a call to gro_pull_from_frag0() that corrupted the packet.

This change uses a more GRO-like approach seen in other GRO receivers via skb_gro_header() to just read the actual data we are interested in and does not try to "restore" the UDP header at this point to call the existing function. If the offload module is not loaded, it immediately bails out, otherwise, it only does a quick check to see if the packet is an IKE or keepalive packet instead of calling the existing function.

Fixes 172bf009c18d ("xfrm: Support GRO for IPv4 ESP in UDP encapsulation")
Fixes 221ddb723d90 ("xfrm: Support GRO for IPv6 ESP in UDP encapsulation")
---
 net/ipv4/xfrm4_input.c | 18 ++++++++++--------
 net/ipv6/xfrm6_input.c | 18 ++++++++++--------
 2 files changed, 20 insertions(+), 16 deletions(-)
net/ipv4/xfrm4_input.c +10 -8
index b5b06323cfd9..0d31a8c108d4 100644
@@ -182,11 +182,15 @@ struct sk_buff *xfrm4_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
 	int offset = skb_gro_offset(skb);
 	const struct net_offload *ops;
 	struct sk_buff *pp = NULL;
-	int ret;
-
-	offset = offset - sizeof(struct udphdr);
+	int len, dlen;
+	__u8 *udpdata;
+	__be32 *udpdata32;
 
-	if (!pskb_pull(skb, offset))
+	len = skb->len - offset;
+	dlen = offset + min(len, 8);
+	udpdata = skb_gro_header(skb, dlen, offset);
+	udpdata32 = (__be32 *)udpdata;
+	if (unlikely(!udpdata))
 		return NULL;
 
 	rcu_read_lock();
@@ -194,11 +198,10 @@ struct sk_buff *xfrm4_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
 	if (!ops || !ops->callbacks.gro_receive)
 		goto out;
 
-	ret = __xfrm4_udp_encap_rcv(sk, skb, false);
-	if (ret)
+	/* check if it is a keepalive or IKE packet */
+	if (len <= sizeof(struct ip_esp_hdr) || udpdata32[0] == 0)
 		goto out;
 
-	skb_push(skb, offset);
 	NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
 
 	pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
@@ -208,7 +211,6 @@ struct sk_buff *xfrm4_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
 
 out:
 	rcu_read_unlock();
-	skb_push(skb, offset);
 	NAPI_GRO_CB(skb)->same_flow = 0;
 	NAPI_GRO_CB(skb)->flush = 1;
 
net/ipv6/xfrm6_input.c +10 -8
index 4abc5e9d6322..841c81abaaf4 100644
@@ -179,14 +179,18 @@ struct sk_buff *xfrm6_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
 	int offset = skb_gro_offset(skb);
 	const struct net_offload *ops;
 	struct sk_buff *pp = NULL;
-	int ret;
+	int len, dlen;
+	__u8 *udpdata;
+	__be32 *udpdata32;
 
 	if (skb->protocol == htons(ETH_P_IP))
 		return xfrm4_gro_udp_encap_rcv(sk, head, skb);
 
-	offset = offset - sizeof(struct udphdr);
-
-	if (!pskb_pull(skb, offset))
+	len = skb->len - offset;
+	dlen = offset + min(len, 8);
+	udpdata = skb_gro_header(skb, dlen, offset);
+	udpdata32 = (__be32 *)udpdata;
+	if (unlikely(!udpdata))
 		return NULL;
 
 	rcu_read_lock();
@@ -194,11 +198,10 @@ struct sk_buff *xfrm6_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
 	if (!ops || !ops->callbacks.gro_receive)
 		goto out;
 
-	ret = __xfrm6_udp_encap_rcv(sk, skb, false);
-	if (ret)
+	/* check if it is a keepalive or IKE packet */
+	if (len <= sizeof(struct ip_esp_hdr) || udpdata32[0] == 0)
 		goto out;
 
-	skb_push(skb, offset);
 	NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
 
 	pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
@@ -208,7 +211,6 @@ struct sk_buff *xfrm6_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
 
 out:
 	rcu_read_unlock();
-	skb_push(skb, offset);
 	NAPI_GRO_CB(skb)->same_flow = 0;
 	NAPI_GRO_CB(skb)->flush = 1;
 
Signature · 1 line
2.34.1
author maintainer
#5 lore raw

PATCH 4/5 xfrm: ipcomp: fix truesize computation on receive

In reply to Steffen Klassert

From: Sabrina Dubroca <[email protected]>

ipcomp_post_acomp currently drops all frags (via pskb_trim_unique(skb, 0)), and then subtracts the old skb->data_len from truesize. This adjustment has already be done during trimming (in skb_condense), so we don't need to do it again.

This shows up for example when running fragmented traffic over ipcomp, we end up hitting the WARN_ON_ONCE in skb_try_coalesce.

Fixes eb2953d26971 ("xfrm: ipcomp: Use crypto_acomp interface")
---
 net/xfrm/xfrm_ipcomp.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/xfrm/xfrm_ipcomp.c +1 -2
index 0c1420534394..907c3ccb440d 100644
@@ -48,7 +48,6 @@ static int ipcomp_post_acomp(struct sk_buff *skb, int err, int hlen)
 {
 	struct acomp_req *req = ipcomp_cb(skb)->req;
 	struct ipcomp_req_extra *extra;
-	const int plen = skb->data_len;
 	struct scatterlist *dsg;
 	int len, dlen;
 
@@ -64,7 +63,7 @@ static int ipcomp_post_acomp(struct sk_buff *skb, int err, int hlen)
 
 	/* Only update truesize on input. */
 	if (!hlen)
-		skb->truesize += dlen - plen;
+		skb->truesize += dlen;
 	skb->data_len = dlen;
 	skb->len += dlen;
 
Signature · 1 line
2.34.1
author maintainer
#6 lore raw

PATCH 5/5 xfrm: Sanitize marks before insert

In reply to Steffen Klassert

From: Paul Chaignon <[email protected]>

Prior to this patch, the mark is sanitized (applying the state's mask to the state's value) only on inserts when checking if a conflicting XFRM state or policy exists.

We discovered in Cilium that this same sanitization does not occur in the hot-path __xfrm_state_lookup. In the hot-path, the sk_buff's mark is simply compared to the state's value:

    if ((mark & x->mark.m) != x->mark.v)
        continue;

Therefore, users can define unsanitized marks (ex. 0xf42/0xf00) which will never match any packet.

This commit updates __xfrm_state_insert and xfrm_policy_insert to store the sanitized marks, thus removing this footgun.

This has the side effect of changing the ip output, as the returned mark will have the mask applied to it when printed.

Fixes 3d6acfa7641f ("xfrm: SA lookups with mark")
---
 net/xfrm/xfrm_policy.c | 3 +++
 net/xfrm/xfrm_state.c  | 3 +++
 2 files changed, 6 insertions(+)
net/xfrm/xfrm_policy.c +3 -0
index 143ac3aa7537..f4bad8c895d6 100644
@@ -1581,6 +1581,9 @@ int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
 	struct xfrm_policy *delpol;
 	struct hlist_head *chain;
 
+	/* Sanitize mark before store */
+	policy->mark.v &= policy->mark.m;
+
 	spin_lock_bh(&net->xfrm.xfrm_policy_lock);
 	chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
 	if (chain)
net/xfrm/xfrm_state.c +3 -0
index 2f57dabcc70b..07fe8e5daa32 100644
@@ -1718,6 +1718,9 @@ static void __xfrm_state_insert(struct xfrm_state *x)
 
 	list_add(&x->km.all, &net->xfrm.state_all);
 
+	/* Sanitize mark before store */
+	x->mark.v &= x->mark.m;
+
 	h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
 			  x->props.reqid, x->props.family);
 	XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h,
Signature · 1 line
2.34.1