From nobody Mon May 25 01:16:19 2026 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (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 518B432E75A for ; Wed, 20 May 2026 07:27:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779262060; cv=none; b=jK0DREghe/T6+hxgb1J/HT5PiibKJckZMxnR/SRGpGbHbW5D+Zf6I/f3aniCc0DqJI7Kv/EAZW+FXVNutO06k134Wu8QGUyNLUr6Ty6iS0bnPdSJywEbXGy81rSKiuj0oMvmzjHcLa2sV1BjqNrPN19fGZxddayP00MFd3nVS2E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779262060; c=relaxed/simple; bh=4FRxNnqDMNdEkwcqxtdH8RepBGpls1NqJol4l4qjRAw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=gRBQVajX+XYlqmuhwFerXWWNjFqPjY42XbAh2x13m9pmfCV8V3DNT4FY+97YJwRzVQ18brM22TA/l0pai1aw3rJGpnE3IwdMoVfIJ0IZALgmOPBG/7YuRq5IbH+fG7ieNZyPx7qR50Enn8RJCQIjWXX8bkE19ufdIrzV37GAAew= 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=DnvG4t/e; arc=none smtp.client-ip=209.85.221.45 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="DnvG4t/e" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-43d77f6092eso2470453f8f.2 for ; Wed, 20 May 2026 00:27:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779262057; x=1779866857; 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=YFCpyBr8c2WuPvvXn0j2wRYx2mdw2tGa8zkYj1VPho8=; b=DnvG4t/es7kOIsXbZdDJArAzcr5IwUoq36YDnFoVV6dr137DrKgnkl1ayDnixQ11uF YSLYpTbegvb2rPzi1jQ4kIRtNMJjlAzIn43kJWpexrUG96Zitc2Ob3A6AfKxzVdBQUHl 0NRG98UOj+uYLUcSLjd3LONjLV6qoRJvZ72rDRoC9WasF7DW841op0qek1tv3Yf0i/Hq fQrBbXKIrO1q3sLRq7N3Jge+waj47R0G1HjTDe/dcWN5rqVfW1OjXn9hRs0rjw2IsR8i cChdcYpx/IlMiz3+dhBUe1egGGBe3tCTlvjdNALF/A7FrQsnkDFgsVT1kx+sC9VTn+EO JwQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779262057; x=1779866857; 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=YFCpyBr8c2WuPvvXn0j2wRYx2mdw2tGa8zkYj1VPho8=; b=YVGkcQxr2AlrV/3OPG4YZvtc3KTKK4nwQNhfIUFbqgM5vl3nzsgIlh1ewItMtcWVHf PClVirBU1f+79MbR+i0CMkqu4TNE241kZXCPMK2PO+U6GpGj5m1LYv3npBIFwjxTcgMP lRTiF65+rZfGF9kjaiVjf7/Hc8UHQw8TWgyTU1lD1Yfp9zcd40tfC4FZT4UYC3Am2xLm KudDgBC5KhkiRefTFlY/uboXl2M3Btd6ptU5jgkN70L0UOVkD6kernTV9P6DSGPbcZnR CDiZ9AjMvL5ZqHOsdw4/CcyB/HnvZeKILhn3NpjdhWkOPbT3fbq+xdRhcflyvxz+nCFx xzHA== X-Forwarded-Encrypted: i=1; AFNElJ/jG4467R0bnzYvKJgWxfGDHegHiKwiEZFjDO4WTCWsXhoydRTUduj/5bmkDs3iSkknmOssD+9efYGnqLY=@vger.kernel.org X-Gm-Message-State: AOJu0Yyh64PDv7Z7gfpPFNRDmz38QXsEDYqS3yIV0MMBJC+PGsGIyrcN YAcT9I+lzJIDzaCy2+7ie9vypq6r1lw2GRGT7ToucuP9mxa4YQ6bPEPm X-Gm-Gg: Acq92OGoEjL8OlEPCEqwych4y5DszfNhbS3doPXESZDs7u9lntg7fza72FYra7EiUVN Dz3dPD5dvhM23J0/SQklwlvlUovxeigezWQsTmm1ewzAD9GgZtwtikCDHeOROyYDVpAy6OImu4X VQs8zC75vDVmM73rS5WtXMFsYfe8xiWlCXlSSYwcbf3PGbsaVrwJwirhZlJvRPnIYLsqOf0ACns iYr6nn8dbiVIVNsEB8iArbTE4PGP4fspvGF8z99NjlhtoslcXJMIcuAV0Nj0JgUqeZQLENfhTii 8EZYL0bi8bGaqhp6tl86s471ueLLqoIFWJAGDPF7jL/kPXfjc8+eeK0+h/SjJvDvgmcegY8WtDs F7I68ZR02cI4Oz2BJfVuOoHw3iFgLCdKtPxjaOc+cF4rMO9v9ro+3Yby8qB89Bj+sBfTWmNh6O0 E2a1REaFQnSNibGW6LjlxqnkFFWonEipdeqpDbUJn80hDjhOLLVnAzCdFlqonnU+DPkqFybaP23 j5Z50xdDaxQfcZdz9S8jJevGFrA/bBjT5GAqS0XslKR86cnwMRPKtiiT6986CiEpt0= X-Received: by 2002:a05:6000:4305:b0:43c:fe66:43ec with SMTP id ffacd0b85a97d-45e5c5af3e1mr38635027f8f.14.1779262056517; Wed, 20 May 2026 00:27:36 -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-45da0fe13a7sm53915917f8f.29.2026.05.20.00.27.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 00:27:36 -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 ipsec v2] esp: fix page frag reference leak on skb_to_sgvec failure Date: Wed, 20 May 2026 09:27:17 +0200 Message-ID: <20260520072717.2043-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 | 12 +++++++----- net/ipv6/esp6.c | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c index 6a5febbdbee4..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)) { @@ -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 9c06c5a1419d..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); @@ -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