Netdev List
 help / color / mirror / Atom feed
From: Steffen Klassert <steffen.klassert@secunet.com>
To: Kyuwon Cho <kyuwoncho18@gmail.com>
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>
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	[thread overview]
Message-ID: <awf0_Ksi65pXTtEo@gauss4> (raw)
In-Reply-To: <179540803323.3139700.15088508372382876389@gmail.com>

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;
>  }

      parent reply	other threads:[~2026-11-26  8:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  4:29 [PATCH ipsec v2 0/1] xfrm: free the policies already cloned when a socket clone fails Kyuwon Cho
2026-11-23  4:29 ` [PATCH ipsec v2 1/1] " Kyuwon Cho
2026-11-26  8:57   ` Steffen Klassert
2026-11-26  8:59   ` Steffen Klassert [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=awf0_Ksi65pXTtEo@gauss4 \
    --to=steffen.klassert@secunet.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=ipsec@lists.linux.dev \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=kyuwoncho18@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox