From nobody Mon Jun 8 06:36:45 2026 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 9B7D43ABD80 for ; Mon, 1 Jun 2026 13:46:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780321569; cv=none; b=Itu+wZ1XnA4QKtq4lzgZPPZsRLDI6O/ga/BtuwHuqtFNzUORiaMpuR4I5tocs8k/JGxhHhYFpsS964LevVhl60FF5PN8JW5l0paO354JlHbDFjfr24pd5BQzNlLQ/VnxCZARag6wkTI9BB94P/nR/Xw7Gjt6PWlrgJB6GgmrC4I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780321569; c=relaxed/simple; bh=dyZ86Cv8vGqv7gq9+mywke5I/pjRLYDP/XV/eYvJOr4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=c9Q9gqi8vDVAkuNeToHRCLKTE1laa8rQ8XUaWSnhR3rvMnBc1DCCooHNlu6M7oTPATQpIUcXLDgB/cAdKn3M2bZnoaaunx/nP6pKpUcymbxZIvWE4/ZUOM/zcAp049kxgBGTCYMo1oiuH6sFSDMicWRlchvuuF2/tI+1ffneBUo= 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=dH3/d67Z; arc=none smtp.client-ip=209.85.128.48 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="dH3/d67Z" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-490426d72f7so88876465e9.3 for ; Mon, 01 Jun 2026 06:46:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780321566; x=1780926366; 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=TDFWzLbwsPQytHDEG9PwUoghyauZNQfRZ1OL2Tq9T1Y=; b=dH3/d67Z2N/4nzqziY5Iqq1S0LULfYtWRKv7tLhT/29bWsWoQVioRuFNIcwsIVNJaL 6BcQNnN0+WWjNmUF6yrgHwcD5+vBA0/1PgiOloU4+cGpVxHX2M4xbF/MRl5QBFXFgxJI oFLZ69GyC0gEFCU5fY0wHckdEWIPVRsI+7SYKNYjLvhAVw/n4syaQu+UAWh/BLfY/a6Q 8lp1hC2n6QzxWQ6b5WiSv4tJLtgnpjumnPbELnAgY/rVkC/0ODi+GiSSCI+u2iVyRb0B a47f36Q5OThSOQ9xIi4ieHt9pTW9CNkKS0CPiwDswyHsUYiz4HqHjVZzRzae3tvmVW3W XQGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780321566; x=1780926366; 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=TDFWzLbwsPQytHDEG9PwUoghyauZNQfRZ1OL2Tq9T1Y=; b=E3Em4h++CFgLVXWN0vs6gQgUMzeptRGyauLkIX0PtfEgmSgqCJiy5ZSLcm+essYska FDYJf2fbrpkxlvChb/6SEjVuECBHI+zK6yIcr9C7F+6qnTWN6RFzPgz4xznntlMwqq20 WyRghsuwJ2U2amunce1mH7Kw043UFQW/QCUEKxPVQx4jEFCw2PlIdxZfjvd9rmLTif3z /vJOfOKm0mlZgNfGYfsK9thePEtSDINfTZf6jeRb7pRlEcsjxZqzSqzK/ek/v8PCYMhs FUEPRLLaQCEMjrCsZcK/v9cbpkskOlos8gALbcrRdbHHtWHQY5CFeKdigdDSNzeGDXes kgEg== X-Forwarded-Encrypted: i=1; AFNElJ/FdEIclvmSdj8xGIxjed/fJEXpMubHIIn7jNyQcHPvSM6jByCIOvJw1kDpXxMkY01bjkULIARjix5jbjg=@vger.kernel.org X-Gm-Message-State: AOJu0Yx8W/57F47Z9WovTLLXkMXl1eWXqVuTc45MOfBIMBE502EBSwg9 zTdE0c6GodvmsLy8M5G55XWRe5qI5hpsS9W8NxjNS05DTJj8/FaAfpIM X-Gm-Gg: Acq92OGp0nEIs///UL33Atdpz4PEe8oMzrCS8nStcOIge8RO125U16kbeWAwTYftOTM /sr2Yv8SuobHOr0G7JiVrCxqiEbtezds5l036QFUEmP9pUQBUHWOlukWw7WMFa2em4U1i0fZkOi YUlsQvE51zgUaIqz8U4GPqEYZLkWb/F088LGPpILa/Av1+XH8jjMeVS9I0IXNG3OMGlPtVLgSnB whuV+M5/qXCK7Y461MAORdJNiJ6BIUGKq7Ybrb6Qn8laWevdJ100YB01VsyK8lvf+Wh/ECjSydY biREJhtsvzbcq21KCo3VtbCUKVm8Lvd7n8VsQKslUTKkm7jWu9sv6l9LDuvg0Ojhcz0FHoDTblH UYUFiJiSUOKDQy0N2WOaranxootWz9RC9ygTxGxmr3oR/Jeq36qnkNrsn7vzpS1k3ux5V9Fep/T zDbwcDxsjKPuP6y/kYvHeiOlnAXI0dVvjUOAH34/MPVZxgftXkyfjPLpRsH/9luxsLhuftvUm74 p6EXpsRd9kTNRis+QNjg9HWsq4Q X-Received: by 2002:a05:600c:5395:b0:490:4b89:535d with SMTP id 5b1f17b1804b1-490a2904b29mr247202275e9.1.1780321565884; Mon, 01 Jun 2026 06:46:05 -0700 (PDT) Received: from localhost.localdomain ([2a01:827:3b28:500:30ab:2400:2a9c:6742]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490aaebf4f1sm94981775e9.12.2026.06.01.06.46.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jun 2026 06:46:05 -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] esp: fix page frag reference leak on skb_to_sgvec failure Date: Mon, 1 Jun 2026 15:45:25 +0200 Message-ID: <20260601134526.82-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> --- net/ipv4/esp4.c | 14 ++++++++------ net/ipv6/esp6.c | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c index 513c8215c947..2429c7845984 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,8 +113,8 @@ static void esp_ssg_unref(struct xfrm_state *x, void *t= mp, 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) + for (sg =3D sg_next(already_unref ? esp_req_sg(aead, req) : req->src); s= g; sg =3D sg_next(sg)) skb_page_unref(page_to_netmem(sg_page(sg)), skb->pp_recycle); } @@ -220,7 +220,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 +569,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 +604,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..50af6ab9b8fc 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,8 +130,8 @@ static void esp_ssg_unref(struct xfrm_state *x, void *t= mp, 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) + for (sg =3D sg_next(already_unref ? esp_req_sg(aead, req) : req->src); s= g; sg =3D sg_next(sg)) skb_page_unref(page_to_netmem(sg_page(sg)), skb->pp_recycle); } @@ -254,7 +254,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 +600,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 +636,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