From nobody Mon Jun 8 05:26:07 2026 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8FB9A29ACFD for ; Fri, 5 Jun 2026 12:23:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780662185; cv=none; b=ZJlBBeyU0L9KJsaUK/tbUjSWBw3CM/I5vys76LWkr1dAIfsptgKNtkASzL9fDtcDIJJuOiKAo3Z2J8GcYC5V3S70yu95jxCgifXAc5Vgn6qdaxQUSt1+RkfY8qe9DLImCDz90tDdk+30GUkGCH77CCJAAgF2UBav/Ii1dDzdrlM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780662185; c=relaxed/simple; bh=t2nRP0VEBjoAykAAoJ4oNQ6spfklBcCjwKZn2cex6/A=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=nWCXXgbhOLzOfYdudYpxXpY4096eJHus3lW/PS04YPy0f/AnQkjxBpWWW29ASb1+YSqi2NryYu8r67mKNbnL0VkXaOwFtBQ8IMyzQW6KthxdfNuhbW7JWkWJAptgeDOKrd8nOjOHrEd1+7MXWht6ez0e1SIzfNayNJxtGkKHCaM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=GJBRxOVL; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GJBRxOVL" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-45ef372c58aso1107773f8f.0 for ; Fri, 05 Jun 2026 05:23:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780662182; x=1781266982; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=RwKzwPDvG1cFhc0LM4+dg+6HLT8lqaSdVjB/A31B/ic=; b=GJBRxOVLd7WeMDxyB/uCLdlmJcasDp1rEvBSnz+JG2JV73s9boDctodLOE2lS0uc0V 7F9lKgKkdOyW0s3g31GzTbl4ykmU13y9NE8h1DtRCX/v0VNRTjS6ux1XredD63KBQcDd ms+Tfo9tW32OliScHv+9c3p9RuFDrMaIRk3Byexch64EE3v2/oRJFE3m/sjk/j5+vNbh uCrK6GfGr41wi4S2SffIghkdBs4kmP9QNyvDiRedxsY+wWsjBWR5Fx3wpeMobjhlzR2I fif4YYZmVfRhAvJmmud0So26uju0LyCiheyMakGP5U5igSaheU/JGBdKA19OjZ27Q/rf l0uA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780662182; x=1781266982; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=RwKzwPDvG1cFhc0LM4+dg+6HLT8lqaSdVjB/A31B/ic=; b=YMq4UUWXJpF4D8s7kjCkcA17x77PIsFQL6YPoBvsBgxdlZC369ItTtT+SDmZk1jhUN NuK3hSuuLyr5acGrLEZz8OZ/b97UfLV/xE9BTKpZlJdTdZ1ZYo+yTz6NPecttQHcMPOL BHchOOSvRaEH+FyvCSXjsGixNW5OOmggVTAYl0yHdwiFU3iu/EycGBApFbJCseV8S/Oa 5uwCh7sJEY7wGWJXYai/IljFXE0IYFqMcYZnRxjA9COpP0wWfjGeEVCgWoI58CuU959O QUe1+b54wK8+rhj4VZITNLZy3eb4NdQRgM1fZ/rNBwI+20LXNAbYKNDZ6y07Et/6huyB d2Gg== X-Forwarded-Encrypted: i=1; AFNElJ/1betELLGH/DZZ6Q5620nZHA0sGM9rHEjOYkZOeMbIt1JdOt+jbPd8ruNn1blDoKEYcYJtPDjzW4kkUNY=@vger.kernel.org X-Gm-Message-State: AOJu0Ywd8lNkiGxb74edJbINOtYRRYHFbsKYAJb45NM9vd7elnUv0o1X kyWXz9Q8VTdZKwKyNKIi3gMJFt0OHTceGzekF7zw6BFQ+1biFFUAOB3D X-Gm-Gg: Acq92OEJRP8z8CD1Y14Ul043GJHSF1PUMxSO06Z1FTDLctcw+hugmvq63Wq+kOfMi2p HybKfK4t6L+Mw9azhKhNzsyGawjfmY7k9rfLFfCvzYTe/diNeXm5JAMj0q/7+huYsy4HeSQ9fE4 Ul4DkXDFVWSc5c9HJxze6/ks5Wd9H9xEDSPeE0Mi/oxwXE2KZVA1tKflJBz+E1ARrGcMuGhmAst dV/jk8Z1QmMGwR9+F0NcYDsKxnAoJj0W07g05ti7syAlHLTeOvdMPtPaRK4iIhylakoXd9SHU36 fDh7G7PZGuJHC4uwAAZYOLINo5LfyruSv2v4p8RfPYCPLiEqVQI2AzEBvqox2H4HhcXB5W39s/0 SPBJGMjm4fAb6gnc5ktJxK4dC/ztTySCTe8hX+/QrI2ABZmGkk18lac7NO/a37sDmAygdAuHvL9 QTAyJE0CplpE/Rzyn/qohlyGDRLYY8p8MfywLdpOOZdxnbcREPH9/gUFTJU1xlS3oswKU0gtIxN FIzICg6uzEnwV517k/3bja4sM6rl/XuxPnTcPBPDdF8MX3+0yuuxCEpOSM2OmUsRewn6qB7Ff9X jg== X-Received: by 2002:a5d:690b:0:b0:44d:1338:46b4 with SMTP id ffacd0b85a97d-460304fad8emr3981968f8f.9.1780662181843; Fri, 05 Jun 2026 05:23:01 -0700 (PDT) Received: from localhost.localdomain (host-217-59-207-212.business.telecomitalia.it. [217.59.207.212]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4601f2dcad5sm27808021f8f.5.2026.06.05.05.23.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jun 2026 05:23:01 -0700 (PDT) From: Alessandro Schino <7991aleschino@gmail.com> To: netdev@vger.kernel.org Cc: steffen.klassert@secunet.com, herbert@gondor.apana.org.au, davem@davemloft.net, pabeni@redhat.com, linux-kernel@vger.kernel.org, Alessandro Schino <7991aleschino@gmail.com> Subject: [PATCH ipsec v3] esp: fix page frag reference leak on skb_to_sgvec failure Date: Fri, 5 Jun 2026 14:22:15 +0200 Message-ID: <20260605122215.825-1-7991aleschino@gmail.com> X-Mailer: git-send-email 2.41.0.windows.3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Fix this by adding a bool parameter to esp_ssg_unref() that, when true, unconditionally unrefs the source scatterlist frags. Since req->src is not yet initialized by aead_request_set_crypt() at the point of the error, the source scatterlist is obtained directly via esp_req_sg() Existing callers pass false to preserve the original behavior 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") Signed-off-by: Alessandro Schino <7991aleschino@gmail.com> --- v3: regenerated on top of the current ipsec tree; the v2 was a diff against an already-modified tree and failed to apply v2: split the long line in esp_ssg_unref() to stay under 100 columns net/ipv4/esp4.c | 17 +++++++++++------ net/ipv6/esp6.c | 17 +++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c index 513c8215c947..dfc81ee969ae 100644 --- a/net/ipv4/esp4.c +++ b/net/ipv4/esp4.c @@ -96,7 +96,7 @@ static inline struct scatterlist *esp_req_sg(struct crypt= o_aead *aead, __alignof__(struct scatterlist)); } =20 -static void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff = *skb) +static void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff = *skb, bool already_unref) { struct crypto_aead *aead =3D x->data; int extralen =3D 0; @@ -113,10 +113,13 @@ static void esp_ssg_unref(struct xfrm_state *x, void = *tmp, struct sk_buff *skb) /* Unref skb_frag_pages in the src scatterlist if necessary. * Skip the first sg which comes from skb->data. */ - if (req->src !=3D req->dst) - for (sg =3D sg_next(req->src); sg; sg =3D sg_next(sg)) + if (already_unref || req->src !=3D req->dst) { + struct scatterlist *src =3D already_unref ? esp_req_sg(aead, req) : req-= >src; + + for (sg =3D sg_next(src); sg; sg =3D sg_next(sg)) skb_page_unref(page_to_netmem(sg_page(sg)), skb->pp_recycle); + } } =20 #ifdef CONFIG_INET_ESPINTCP @@ -220,7 +223,7 @@ static void esp_output_done(void *data, int err) } =20 tmp =3D ESP_SKB_CB(skb)->tmp; - esp_ssg_unref(x, tmp, skb); + esp_ssg_unref(x, tmp, skb, false); kfree(tmp); =20 if (xo && (xo->flags & XFRM_DEV_RESUME)) { @@ -569,8 +572,10 @@ int esp_output_tail(struct xfrm_state *x, struct sk_bu= ff *skb, struct esp_info * err =3D skb_to_sgvec(skb, dsg, (unsigned char *)esph - skb->data, assoclen + ivlen + esp->clen + alen); - if (unlikely(err < 0)) + if (unlikely(err < 0)) { + esp_ssg_unref(x, tmp, skb, true); goto error_free; + } } =20 if ((x->props.flags & XFRM_STATE_ESN)) @@ -602,7 +607,7 @@ int esp_output_tail(struct xfrm_state *x, struct sk_buf= f *skb, struct esp_info * } =20 if (sg !=3D dsg) - esp_ssg_unref(x, tmp, skb); + esp_ssg_unref(x, tmp, skb, false); =20 if (!err && x->encap && x->encap->encap_type =3D=3D TCP_ENCAP_ESPINTCP) err =3D esp_output_tail_tcp(x, skb); diff --git a/net/ipv6/esp6.c b/net/ipv6/esp6.c index 57481e423e59..296b57926abb 100644 --- a/net/ipv6/esp6.c +++ b/net/ipv6/esp6.c @@ -113,7 +113,7 @@ static inline struct scatterlist *esp_req_sg(struct cry= pto_aead *aead, __alignof__(struct scatterlist)); } =20 -static void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff = *skb) +static void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff = *skb, bool already_unref) { struct crypto_aead *aead =3D x->data; int extralen =3D 0; @@ -130,10 +130,13 @@ static void esp_ssg_unref(struct xfrm_state *x, void = *tmp, struct sk_buff *skb) /* Unref skb_frag_pages in the src scatterlist if necessary. * Skip the first sg which comes from skb->data. */ - if (req->src !=3D req->dst) - for (sg =3D sg_next(req->src); sg; sg =3D sg_next(sg)) + if (already_unref || req->src !=3D req->dst) { + struct scatterlist *src =3D already_unref ? esp_req_sg(aead, req) : req-= >src; + + for (sg =3D sg_next(src); sg; sg =3D sg_next(sg)) skb_page_unref(page_to_netmem(sg_page(sg)), skb->pp_recycle); + } } =20 #ifdef CONFIG_INET6_ESPINTCP @@ -254,7 +257,7 @@ static void esp_output_done(void *data, int err) } =20 tmp =3D ESP_SKB_CB(skb)->tmp; - esp_ssg_unref(x, tmp, skb); + esp_ssg_unref(x, tmp, skb, false); kfree(tmp); =20 esp_output_encap_csum(skb); @@ -600,8 +603,10 @@ int esp6_output_tail(struct xfrm_state *x, struct sk_b= uff *skb, struct esp_info err =3D skb_to_sgvec(skb, dsg, (unsigned char *)esph - skb->data, assoclen + ivlen + esp->clen + alen); - if (unlikely(err < 0)) + if (unlikely(err < 0)) { + esp_ssg_unref(x, tmp, skb, true); goto error_free; + } } =20 if ((x->props.flags & XFRM_STATE_ESN)) @@ -634,7 +639,7 @@ int esp6_output_tail(struct xfrm_state *x, struct sk_bu= ff *skb, struct esp_info } =20 if (sg !=3D dsg) - esp_ssg_unref(x, tmp, skb); + esp_ssg_unref(x, tmp, skb, false); =20 if (!err && x->encap && x->encap->encap_type =3D=3D TCP_ENCAP_ESPINTCP) err =3D esp_output_tail_tcp(x, skb); --=20 2.41.0.windows.3