Skip to contentSkip to conversation

fix some leaks in espintcp

PATCH In review Sabrina Dubroca 3 participants 6 messages Apr 9, 2025 – Apr 14, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover fix some leaks in espintcp

kmemleak spotted a few leaks that have been here since the beginning.

Sabrina Dubroca (2):
espintcp: fix skb leaks
espintcp: remove encap socket caching to avoid reference leak

 include/net/xfrm.h    |  1 -
 net/ipv4/esp4.c       | 53 ++++++-------------------------------------
 net/ipv6/esp6.c       | 53 ++++++-------------------------------------
 net/xfrm/espintcp.c   |  4 +++-
 net/xfrm/xfrm_state.c |  3 ---
 5 files changed, 17 insertions(+), 97 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/2 espintcp: fix skb leaks

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.49.0
#3 lore raw

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

In reply to Sabrina Dubroca

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 ed4b83696c77..7e698b0306a8 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 ad2202fa82f3..3acf6c14de08 100644
@@ -840,9 +840,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.49.0
reviewer Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH ipsec 2/2] espintcp: remove encap socket caching to avoid reference leak

Quoted 13 lines · Sabrina Dubroca

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.

Quoted 6 lines

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.

Less is more :)

Quoted 2 lines
Fixes e27cca96cd68 ("xfrm: add espintcp (RFC 8229)")
maintainer
#5 lore raw
In reply to Sabrina Dubroca
Quoted 5 lines · Sabrina Dubroca

kmemleak spotted a few leaks that have been here since the beginning.

Sabrina Dubroca (2):
espintcp: fix skb leaks
espintcp: remove encap socket caching to avoid reference leak

Series applied, thanks Sabrina!