pull request (net): ipsec 2025-05-21
Subsystems (3)
Steffen Klassert
maintainercover 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(-)
Steffen Klassert
maintainerPATCH 1/5 espintcp: fix skb leaks
From: Sabrina Dubroca <[email protected]>
A few error paths are missing a kfree_skb.
--- 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
@@ -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
@@ -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
@@ -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
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)")Signed-off-by Sabrina Dubroca <[email protected]>Reviewed-by Simon Horman <[email protected]>Signed-off-by Steffen Klassert <[email protected]>[...]
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
Steffen Klassert
maintainerPATCH 2/5 espintcp: remove encap socket caching to avoid reference leak
In reply to Steffen KlassertFrom: 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.
--- 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
@@ -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
@@ -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
Steffen Klassert
maintainerPATCH 3/5 xfrm: Fix UDP GRO handling for some corner cases
In reply to Steffen KlassertFrom: 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.
--- 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
@@ -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
@@ -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
Steffen Klassert
maintainerPATCH 4/5 xfrm: ipcomp: fix truesize computation on receive
In reply to Steffen KlassertFrom: 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.
--- net/xfrm/xfrm_ipcomp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/xfrm/xfrm_ipcomp.c +1 -2
@@ -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
Steffen Klassert
maintainerPATCH 5/5 xfrm: Sanitize marks before insert
In reply to Steffen KlassertFrom: 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.
--- net/xfrm/xfrm_policy.c | 3 +++ net/xfrm/xfrm_state.c | 3 +++ 2 files changed, 6 insertions(+)
net/xfrm/xfrm_policy.c +3 -0
@@ -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
@@ -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