From nobody Mon May 25 06:41:57 2026 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.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 60F7B29D264 for ; Sun, 17 May 2026 11:13:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779016415; cv=none; b=Yw0BRJeZAxf9BcBw8t6Ka0sDGB4tWUrNqXZumMcfkTh+Sl8IvWVNOQpeeJ3XLBaeT7UEFhdf6eCE3EmPHjnz9KKKA5DnPcy8jptY+yDzZS3AvKeg7gS+G4eBrJhjBRpna5QbOomEHknzzLwuNasjEhixvcPyWklWpdnInld9vpk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779016415; c=relaxed/simple; bh=CkxPU9HMSAxjzXvdPzgS6n1OakeS4AGDFkQ7bV+qml4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=nV+U3lgw5kRd6fMVZfaoWYnEfkDfnG8xPUeCEFd335dnVdwMtsUiHO7NDSYTqlE53o2PvkmkDEpzBMkdKChqe0jPwuHZeHQtpQ7WKzmbWODwtGZSytQuIJrkx+DvGhRrHymoCFuHS3BSY85OVijovu401UMfsrXpWubYwIQr0Ac= 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=D4ubqSHz; arc=none smtp.client-ip=209.85.128.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="D4ubqSHz" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-48e56c1bf5dso5212925e9.3 for ; Sun, 17 May 2026 04:13:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779016413; x=1779621213; 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=2Hq4bWdD9APUqfcF0QdwConc0D+0mUZE86sUdlR/xO8=; b=D4ubqSHzOEKcu4yTYsYUpAnF/e1R9Jlnfov2zQgiq0eUfpUMlqqvGoMljQ511t15Ww K5bIQG01aPjWGldLTL6ts/TdyaLPfrI9aNR1BaNa0OVWvsa8/zcKRd4Cu04cd17wezGa CDL62rYUHZg7ISS2nhB4ZVXuBsbYIQn1748wNCcR86eFV9Luk6qaBafO6hAtT+TZtiGZ Ubd+b08cPjE1rKoqLlBJ0XNmySRR5ZIWw8oLat2EjcwzrUNUoLZakGDUonDr00fuUu0H SVzWd1Hx+vWmcpYZsFJ6a8XwqxMlXx7yuv74m++mco06qE0p6+EmQZqXtjQUhta8ZBRm wDgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779016413; x=1779621213; 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=2Hq4bWdD9APUqfcF0QdwConc0D+0mUZE86sUdlR/xO8=; b=VoUndMCrFtj/3WEMBBfHX8Ec6T44iTUXpqBJperF2YuLGXXd6t4gornQl6F4tOs5RK nUdboV5wc/SLl4YnDrwqpRWUYk6z+MoZg49EUiXlx5pCJYDsopch9I4rnCQFD8k1KV44 VroI0nCi4QQjH4AJU9Z1Csqs6s0OKzeQaEDxlg4W+heXkZ+uhcr/Qn8UfnZ1jX/XgWFq /KH3F6aJpYFcM7V1jx/q/QGVNylH7SRjspUf9lP6Gc5uOpFkRqCHgdCObRk9T7Aiylci M/itpkBy2CebG/CaEl3RzX3yfLKHcveP5HQ/o7ruosJlDR1tRu1HnC55KsLObC2bj2Z2 8xSw== X-Forwarded-Encrypted: i=1; AFNElJ+nGCwBducW4l/J63g1+nQ9xZbeeP1GDX6p9KtlQs5lwIx+ZdliwksqjeSiXE33GhcTK4qx17gs0mh5lOI=@vger.kernel.org X-Gm-Message-State: AOJu0YyajrUitBke0T2saK1ZAuJRAbPKCzvNfcY1hDzQNr9719oXhtBG w4iK1r7d49Vkeuq8+DG4070XHzd8wpEcyu5/S4OGtlWyvI0ABlTpnU2N X-Gm-Gg: Acq92OGUCdk5E/Am+m38Up9jh9kquAd0T44pfQHKUYw9ZTjp/YpqlJ/N34QS+lQltfx T8Mvv5pnsQCfPDWN3bE5DCuoys15xlpSUs5d1rjhWKWEcnOP11uYn0DZAD4gfejgdajeODUunB4 rIHlM9paQM7bXNGPiMn86QbHjAvsBxIbi/BSVRRFaA3JH77cwRcZYAlxoOFndWx5p15BgnmTlD6 7lrCYA+9LesAfYyKCsZ1kEScRq1xFByXDtHbYiEzI1BSk6hbEF2AK/JPJXKZ7LvFRoMeHpuSf02 XXs1vqnVo44/07OBPGvS4iUPeNNunhHrvq21TtiREJPlbQO/VH+CWC7P5SAxX8AoNBK7ZfeM4g+ pbd5DAwJBlPsRVSWgB3Iyu9tagF7FjPdk8L1MqTRQ5/mv5Eb90AC/pB+fr60iHRp0Iw7Yej8BTr 1Qm2oDPJAAGwBNUMsNmazrBkGwljiSKKjNh8YT4zMKCVXKg4Wncnf/E1eDPDfah/JqJIkZCqhET EWcj9E= X-Received: by 2002:a05:600c:4e87:b0:48a:93d2:60d2 with SMTP id 5b1f17b1804b1-48fe59ab8c8mr164353235e9.0.1779016412523; Sun, 17 May 2026 04:13:32 -0700 (PDT) Received: from localhost.localdomain ([2a01:827:3b28:500:b184:4b99:e746:532b]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48feab2a23dsm57757125e9.5.2026.05.17.04.13.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 May 2026 04:13:31 -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, linux-kernel@vger.kernel.org, e521588 , Alessandro Schino <7991aleschino@gmail.com> Subject: [PATCH] esp: fix page frag reference leak on skb_to_sgvec failure Date: Sun, 17 May 2026 13:13:06 +0200 Message-ID: <20260517111306.1881-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" From: e521588 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 adding a bool parameter to esp_ssg_unref() that, when true, unconditionally unrefs the source scatterlist frags without checking req->src and req->dst, since those fields are not yet initialized by aead_request_set_crypt() at the point of the error. 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 | 10 +++++----- net/ipv6/esp6.c | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c index 5c4b162c1bb5..8314d7bddcb7 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,7 +113,7 @@ 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) + if (already_unref || req->src !=3D req->dst) for (sg =3D sg_next(req->src); sg; 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)) { @@ -570,7 +570,7 @@ int esp_output_tail(struct xfrm_state *x, struct sk_buf= f *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(x, tmp, skb, true); goto error_free; } } @@ -604,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 9166e1da3615..9d0c4957ac62 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,7 +130,7 @@ 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) + if (already_unref || req->src !=3D req->dst) for (sg =3D sg_next(req->src); sg; 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); @@ -601,7 +601,7 @@ int esp6_output_tail(struct xfrm_state *x, struct sk_bu= ff *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(x, tmp, skb, true); goto error_free; } } @@ -636,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