Skip to contentSkip to conversation

esp: fix page frag reference leak on skb_to_sgvec failure

PATCH In review Alessandro Schino 2 participants 2 messages May 16

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

PATCH 1/1 esp: fix page frag reference leak on skb_to_sgvec failure

From: e521588 <[email protected]>

In esp_output_tail(), when esp->inplace is false, the old skb page frags are replaced with a new page from the xfrm page_frag cache. The source scatterlist (sg) is built from the old frags before the replacement, and esp_ssg_unref() is responsible for releasing the old page references after the crypto operation completes.

However, if the second skb_to_sgvec() call (which builds the destination scatterlist from the new page) fails, the code jumps to error_free which only calls kfree(tmp). The old page frag references captured in the source scatterlist are never released:

1. sg[] is built from old frags via skb_to_sgvec() (no extra get_page)
2. nr_frags is set to 1 and frag[0] is replaced with the new page
3. Second skb_to_sgvec() fails -> goto error_free
4. kfree(tmp) frees the sg[] memory but old frags are not unref'd
5. kfree_skb() only releases frag[0] (the new page), not the old ones

Fix this by introducing esp_ssg_unref_frags(), a variant of esp_ssg_unref() that unconditionally unrefs the source scatterlist frags without checking req->src and req->dst, since those fields are not yet initialized at this point. The new function is called before the goto error_free when the second skb_to_sgvec() fails, at which point the source scatterlist in tmp is fully populated.

The same issue exists in both esp4 and esp6 as the code is identical.

Fixes cac2661c53f3 ("esp4: Avoid skb_cow_data whenever possible")
Fixes 03e2a30f6a27 ("esp6: Avoid skb_cow_data whenever possible")
---
 net/ipv4/esp4.c | 22 +++++++++++++++++++++-
 net/ipv6/esp6.c | 22 +++++++++++++++++++++-
 2 files changed, 42 insertions(+), 2 deletions(-)
net/ipv4/esp4.c +21 -1
index 5c4b162c1bb5..bc5245c6a29a 100644
@@ -119,6 +119,26 @@ static void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff *skb)
 				       skb->pp_recycle);
 }
 
+static void esp_ssg_unref_frags(struct xfrm_state *x, void *tmp,
+                                struct sk_buff *skb)
+{
+	struct crypto_aead *aead = x->data;
+	int extralen = 0;
+	u8 *iv;
+	struct aead_request *req;
+	struct scatterlist *sg;
+
+	if (x->props.flags & XFRM_STATE_ESN)
+		extralen += sizeof(struct esp_output_extra);
+
+	iv = esp_tmp_iv(aead, tmp, extralen);
+	req = esp_tmp_req(aead, iv);
+
+	for (sg = sg_next(req->src); sg; sg = sg_next(sg))
+		skb_page_unref(page_to_netmem(sg_page(sg)),
+			       skb->pp_recycle);
+}
+
 #ifdef CONFIG_INET_ESPINTCP
 static struct sock *esp_find_tcp_sk(struct xfrm_state *x)
 {
@@ -570,7 +590,7 @@ int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *
 			           (unsigned char *)esph - skb->data,
 			           assoclen + ivlen + esp->clen + alen);
 		if (unlikely(err < 0)) {
-			esp_ssg_unref(x, tmp, skb);
+			esp_ssg_unref_frags(x, tmp, skb);
 			goto error_free;
 		}
 	}
net/ipv6/esp6.c +21 -1
index 9166e1da3615..9bb7242543d8 100644
@@ -136,6 +136,26 @@ static void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff *skb)
 				       skb->pp_recycle);
 }
 
+static void esp_ssg_unref_frags(struct xfrm_state *x, void *tmp,
+                                struct sk_buff *skb)
+{
+	struct crypto_aead *aead = x->data;
+	int extralen = 0;
+	u8 *iv;
+	struct aead_request *req;
+	struct scatterlist *sg;
+
+	if (x->props.flags & XFRM_STATE_ESN)
+		extralen += sizeof(struct esp_output_extra);
+
+	iv = esp_tmp_iv(aead, tmp, extralen);
+	req = esp_tmp_req(aead, iv);
+
+	for (sg = sg_next(req->src); sg; sg = sg_next(sg))
+		skb_page_unref(page_to_netmem(sg_page(sg)),
+			       skb->pp_recycle);
+}
+
 #ifdef CONFIG_INET6_ESPINTCP
 static struct sock *esp6_find_tcp_sk(struct xfrm_state *x)
 {
@@ -601,7 +621,7 @@ int esp6_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info
 			           (unsigned char *)esph - skb->data,
 			           assoclen + ivlen + esp->clen + alen);
 		if (unlikely(err < 0)) {
-			esp_ssg_unref(x, tmp, skb);
+			esp_ssg_unref_frags(x, tmp, skb);
 			goto error_free;
 		}
 	}
Signature · 1 line
2.41.0.windows.3
maintainer
#1 lore raw
Quoted 54 lines · Alessandro Schino

From: e521588 <[email protected]>

In esp_output_tail(), when esp->inplace is false, the old skb page frags are replaced with a new page from the xfrm page_frag cache. The source scatterlist (sg) is built from the old frags before the replacement, and esp_ssg_unref() is responsible for releasing the old page references after the crypto operation completes.

However, if the second skb_to_sgvec() call (which builds the destination scatterlist from the new page) fails, the code jumps to error_free which only calls kfree(tmp). The old page frag references captured in the source scatterlist are never released:

1. sg[] is built from old frags via skb_to_sgvec() (no extra get_page)
2. nr_frags is set to 1 and frag[0] is replaced with the new page
3. Second skb_to_sgvec() fails -> goto error_free
4. kfree(tmp) frees the sg[] memory but old frags are not unref'd
5. kfree_skb() only releases frag[0] (the new page), not the old ones

Fix this by introducing esp_ssg_unref_frags(), a variant of esp_ssg_unref() that unconditionally unrefs the source scatterlist frags without checking req->src and req->dst, since those fields are not yet initialized at this point. The new function is called before the goto error_free when the second skb_to_sgvec() fails, at which point the source scatterlist in tmp is fully populated.

The same issue exists in both esp4 and esp6 as the code is identical.

Fixes cac2661c53f3 ("esp4: Avoid skb_cow_data whenever possible")
Fixes 03e2a30f6a27 ("esp6: Avoid skb_cow_data whenever possible")
---
 net/ipv4/esp4.c | 22 +++++++++++++++++++++-
 net/ipv6/esp6.c | 22 +++++++++++++++++++++-
 2 files changed, 42 insertions(+), 2 deletions(-)
net/ipv4/esp4.c +9 -0
index 5c4b162c1bb5..bc5245c6a29a 100644
@@ -119,6 +119,26 @@ static void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff *skb)
 				       skb->pp_recycle);
 }
 
+static void esp_ssg_unref_frags(struct xfrm_state *x, void *tmp,
+                                struct sk_buff *skb)
+{
+	struct crypto_aead *aead = x->data;
+	int extralen = 0;
+	u8 *iv;
+	struct aead_request *req;
+	struct scatterlist *sg;
+

net/ipv4/esp4.c static void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff *skb) · line 140

Quoted 10 lines
net/ipv4/esp4.c +10 -0
+	if (x->props.flags & XFRM_STATE_ESN)
+		extralen += sizeof(struct esp_output_extra);
+
+	iv = esp_tmp_iv(aead, tmp, extralen);
+	req = esp_tmp_req(aead, iv);
+
+	for (sg = sg_next(req->src); sg; sg = sg_next(sg))
+		skb_page_unref(page_to_netmem(sg_page(sg)),
+			       skb->pp_recycle);
+}

Sorry, I wasn't exactly precise. Instead of duplicating esp_ssg_unref() it might make more sense to add a flag to esp_ssg_unref() and check src and dst of the crypto request based on that flag.