๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #22981

Re: [PATCH ipsec v2 1/1] xfrm: free the policies already cloned when a socket clone fails

#22981
From: Steffen Klassert
Date: Thu, 26 Nov 2026 08:59
88 lines
12774 bytes
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
---
Author: Kyuwon Cho <kyuwoncho18@gmail.com>

xfrm: free the policies already cloned when a socket clone fails

This patch adds a rollback loop to __xfrm_sk_clone_policy(). If cloning
the second direction fails, the loop deletes the per-socket policy
already cloned for the first direction. Without it, that clone stays on
net->xfrm.policy_all until the netns is torn down.

> diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
> index d2e938d4db9aa..e5b51f5f64306 100644
> --- a/net/xfrm/xfrm_policy.c
> +++ b/net/xfrm/xfrm_policy.c
> @@ -2460,6 +2460,13 @@ int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
>  			rcu_assign_pointer(sk->sk_policy[i], np);
>  		}
>  	}
> +	while (unlikely(ret) && i--) {
> +		np = rcu_dereference(sk->sk_policy[i]);
> +		if (np) {
> +			RCU_INIT_POINTER(sk->sk_policy[i], NULL);
> +			xfrm_policy_delete(np, XFRM_POLICY_MAX + i);
> +		}
> +	}

Discovered by: opus-5-5 ยท Missed by: sashiko-gemini
This rollback only covers a failure inside __xfrm_sk_clone_policy().
What about a failure later in sk_clone(), after xfrm_sk_clone_policy()
has already succeeded?

In net/core/sock.c:sk_clone():

	if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
		...
		goto free;
	}

	if (bpf_sk_storage_clone(sk, newsk))
		goto free;
	...
free:
	newsk->sk_destruct = NULL;
	if (lock)
		bh_unlock_sock(newsk);
	sk_free(newsk);

Once xfrm_sk_clone_policy() succeeds, newsk->sk_policy[0] and
newsk->sk_policy[1] hold fresh clones. clone_policy() has already linked
each one on net->xfrm.policy_all and dropped the allocation reference:

	xfrm_sk_policy_link(newp, dir);
	spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
	xfrm_pol_put(newp);

bpf_sk_storage_clone() can then return -ENOMEM in three ways: the
bpf_selem_alloc() optmem charge fails, bpf_selem_link_map() fails, or
bpf_local_storage_alloc() fails. The free label only calls sk_free().
__sk_destruct() does not call xfrm_sk_free_policy().

Won't both cloned policies stay linked until netns exit and trigger the
same xfrm_policy_fini() warning quoted in the commit message?

A TCP listener can reach this path from remote traffic through
inet_csk_clone_lock()->sk_clone(). The listener needs IP_XFRM_POLICY or
IPV6_XFRM_POLICY set and a BPF_F_CLONE sk_storage map attached. The
storage clone can then fail under GFP_ATOMIC pressure, or every time
once the charge reaches optmem_max.

Would the cleanup fit better in the free path of sk_clone(), so that it
covers both cases?

That cleanup could not be a plain xfrm_sk_free_policy(newsk) call. On
the !is_charged short circuit, xfrm_sk_clone_policy() never runs.
newsk->sk_policy[] still holds the parent's pointers from sock_copy(),
because sk_policy is outside the __sk_common dontcopy range. Those slots
would need to be cleared on that path first, or the parent's policies
would be deleted.

>  	rcu_read_unlock();
>  	return ret;
>  }
Technical Details
Message-ID: <awf0_Ksi65pXTtEo@gauss4>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-371973-linux-kernel-netdev=dewy.ru
References: <179540803323.3139700.5340002553684152759@gmail.com> <179540803323.3139700.15088508372382876389@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-371973-linux-kernel-netdev=dewy.ru
From: steffen.klassert@secunet.com (Steffen Klassert)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH ipsec v2 1/1] xfrm: free the policies already cloned when
 a socket clone fails
Date: Thu, 26 Nov 2026 09:59:40 +0100
Organization: Usenet Network
Message-ID: <awf0_Ksi65pXTtEo@gauss4>
References: <179540803323.3139700.5340002553684152759@gmail.com>
 <179540803323.3139700.15088508372382876389@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="421671"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:bMjwUidRhC1y25TBYqoeO34wNQ4= sha256:esePgb2HTd8DgnoyHVjfu2cqgtK0u914Ap93Upr1yw0=
	sha1:Z1c6KJVnNLya66nNPnUmOtEqQSw= sha256:u8BLwr5Hn+NjDMFeVoHvoJjfEAeHuPwrh4nuN4W59MQ=
Content-Disposition: inline
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id E4C4B125EB6; Thu, 26 Nov 2026 12:00:02 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.051
X-Spam-Status: No, score=-3.051 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=secunet.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id Nsh79Yy01CHw;
	Thu, 26 Nov 2026 11:59:59 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-371973-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=secunet.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id AC3C3125EB5; Thu, 26 Nov 2026 11:59:48 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 77725350DD; Thu, 26 Nov 2026 08:59:46 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795683586;
	b=Oz1ZEuL0e6DVaKgTqM5qWpQw+clYLtHmekSj5ok5ju5f4xa+VB5TKys096kIJOKW8vnt
	 +fJdBN/yQdFfvgccWs6J6Tw3JO8YEE1fa61n1FqRUAKpC0SX2evpg8zuXjNQynbaMH5q5
	 uVDrO4udkcoOOmCCyw9snjTJkiyhPwyYskpnPdgTL35oq5CqbQgJHWc7+HQ1rS5kvy/E4
	 UDhZN+wJFJ5wlH2IBlcjR86GWq6sK4bvyt7nJwjDuOvpKmLz1exDr194/FkRQd/HzUgLX
	 pRYLLVWFtXI7Na4VhpXfCG7KkXbUFLBCXaqs0tIv3gL1bEY5+wQGQKYbW93fApoCjng==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795683586;
	h=X-Original-To:Received:DMARC-Filter:Received:X-Virus-Scanned:
	 Received:Received:DKIM-Filter:DKIM-Signature:Received:Received:Date:
	 From:To:CC:Subject:Message-ID:References:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version:Content-Type:Content-Disposition:
	 Content-Transfer-Encoding:In-Reply-To:X-ClientProxiedBy;
	bh=EL1asoxpJj3mS6PiWh3IZjA5s9JX+P0EoH0x0jJdp2g=;
	b=oHTABHBBAks6D+/Mm/sKH3Ttna/TcvFJ5z+GZOSVZQ9V/8OYuUPI+wG7KA5sXE1atpfK
	 q0XoPHwnRfM0OE4eGfZhQzrzCHnp5eCXfic9POOTvtDKQt1FHPzMYtAv5aEmX6q4ry+CY
	 mRyJgVO/VWS075M+tly0SGSJwNLCYdhXYWjG67spkO0tR4NKvUpe0jelHzyEGYBj//t0l
	 OsAVsVMKQSbxoaqVf7EqCTyBMB5ac6Eqi6/Mg7yDtDoBlfYfpxKyIVUP5rgkHjHUZ4pW1
	 F3tA4alLOPwSJTazfaxwzlUNujEZcNrBX9jlfzMXP4mnPtDkItm7gMbIw7Dda0YwCUA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=RvkrkkIe;
	dmarc=pass header.from=secunet.com;
	spf=pass smtp.mailfrom=secunet.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 09D8A606A321;
	Thu, 26 Nov 2026 08:59:46 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=RvkrkkIe
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=62.96.220.36
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795683585;
	b=1y/htCoSPQuMgwiMfY/8Yg6FyIEj/mIEeHxaSLBaKsG+MsozWIo2k7QuQp3c23q0D3GK
	 nExYk+/iUbjxV/BB0Q9bgBBpYxDgDyyPpMxAyzXPjKoPnKflz9cGy7IVmm1N8waOQk6Pt
	 AaaS2ReyCO67CN3lNfQYV0uLYDtSD8YaCWtAiQTRIaZxws/hzzsaCBeL1INJpCFzDT5t3
	 9P7/5EnV/oK0gJ0fEX3JnXg/F4IquGhTNy2xNLGSviTv00tQRwJ8GSqG9y96BNaUZZbr/
	 Zd4jBrFxWa2+czzF6p6WSNB4WK4u66DsRBXT3hN0kBmNnYiHEfI7mYOHuxAQ61d45hg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795683585;
	h=DMARC-Filter:Received:X-Virus-Scanned:Received:Received:DKIM-Filter:
	 DKIM-Signature:Received:Received:Date:From:To:CC:Subject:Message-ID:
	 References:MIME-Version:Content-Type:Content-Disposition:
	 Content-Transfer-Encoding:In-Reply-To:X-ClientProxiedBy;
	bh=EL1asoxpJj3mS6PiWh3IZjA5s9JX+P0EoH0x0jJdp2g=;
	b=qSJcmIZWkUU0N9ppBNXEuIGokbKdWOe4CstG4XD54grpBSmmbdOQPFlpZGhlyglVT6+F
	 CtYkjitUw5CoC3klvAvnzGRxK2JsgMTAEKSvT8HOmfR2R7q7zx2ufzSM0p8YZwDO6BZma
	 wmfsxKOJZ3Gf6WLCCbboL/hiCMRrGwf6ZLHWS1xaUGNf54tiJkHrmbwGeL9jZbI7ikMrb
	 3PM63c1KivAlLTqqBy+9MXir9bTTX2/FrE68OhNyHcI4LlBl+D3SVctmzR6SxuOpRt1Bz
	 IYXfWRknMjdzP4/6khRfHIQ8VJVuX1+h3v+sUiH94+os41Ve4pvdfS0ZfXlmWtc2pHg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=secunet.com;
	spf=pass smtp.mailfrom=secunet.com;
	dkim=pass header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=RvkrkkIe;
	arc=none smtp.remote-ip=62.96.220.36
X-Received: from mx1.secunet.com (mx1.secunet.com [62.96.220.36])
	(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DCAF606A31C;
	Thu, 26 Nov 2026 08:59:43 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2DCAF606A31C
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=secunet.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=secunet.com
X-Received: from localhost (localhost [127.0.0.1])
	by mx1.secunet.com (Postfix) with ESMTP id 0235020758;
	Thu, 26 Nov 2026 09:59:42 +0100 (CET)
X-Virus-Scanned: by secunet
X-Received: from mx1.secunet.com ([127.0.0.1])
 by localhost (mx1.secunet.com [127.0.0.1]) (amavisd-new, port 10024)
 with ESMTP id VGYa7rv3mnB7; Thu, 26 Nov 2026 09:59:41 +0100 (CET)
X-Received: from EXCH-01.secunet.de (rl1.secunet.de [10.32.0.231])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by mx1.secunet.com (Postfix) with ESMTPS id 513D2206B1;
	Thu, 26 Nov 2026 09:59:41 +0100 (CET)
X-DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.secunet.com 513D2206B1
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secunet.com;
	s=202301; t=1795683581;
	bh=EL1asoxpJj3mS6PiWh3IZjA5s9JX+P0EoH0x0jJdp2g=;
	h=Date:From:To:CC:Subject:References:In-Reply-To:From;
	b=RvkrkkIebhzr0A3lbnE+9iLG/GwSN9NTC7XwPIGoECQChEN+etcsdjmc/1bC0doOv
	 Bte4BMRxYrMAlNtZJ7QLhT98i0jxxb5d/d7Av1UVcmc5VbNt5/w64eMZlwtC6myLNf
	 cZcira65E68qJMSgNPHqPMm8c19G9yiIEUpsdrY6pZeg6nY+dYDnrWiEe9ZiLlGtp1
	 rJWgi3OzrJ30/dFufABaUHIR2syV/VpAFpg9kW28H4gkZa68d/mWI9i9oBXaFcB593
	 VFvUPDIX5bl822WnLO4XX389qO8uzFmYb7u62WK9m/4RVgvV01KJlO9kfJpI9VA3EN
	 MWmgUtxpwOObg==
X-Received: from gauss4 (10.182.7.102) by EXCH-01.secunet.de (10.32.0.171) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 26 Nov
 2026 09:59:40 +0100
X-Received: (nullmailer pid 2023310 invoked by uid 1000);
	Thu, 26 Nov 2026 08:59:40 -0000
X-CC: Herbert Xu <herbert@gondor.apana.org.au>, "David S. Miller"
	<davem@davemloft.net>, Eric Dumazet <edumazet@kernel.org>, Jakub Kicinski
	<kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Willem de Bruijn
	<willemb@google.com>, Simon Horman <horms@kernel.org>, Kuniyuki Iwashima
	<kuniyu@google.com>, <netdev@vger.kernel.org>, <ipsec@lists.linux.dev>,
	<linux-kernel@vger.kernel.org>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <179540803323.3139700.15088508372382876389@gmail.com>
X-ClientProxiedBy: EXCH-01.secunet.de (10.32.0.171) To EXCH-01.secunet.de
 (10.32.0.171)