mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kyuwon Cho <kyuwoncho18@gmail.com>
To: Steffen Klassert <steffen.klassert@secunet.com>,
	Herbert Xu <herbert@gondor.apana.org.au>
Cc: "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>,
	Josef Bacik <josef@toxicpanda.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>,
	netdev@vger.kernel.org, ipsec@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: [PATCH ipsec v3] net: free the socket policies when sk_clone() fails
Date: Fri, 27 Nov 2026 13:58:10 +0900	[thread overview]
Message-ID: <179568863376.462699.9896914557163021386@gmail.com> (raw)

sk_clone() clones the parent's socket policies with
xfrm_sk_clone_policy() and then its BPF socket storage with
bpf_sk_storage_clone(). If either fails once a policy has been cloned,
the new socket goes to the "free" label, which only clears sk_destruct
and calls sk_free(), as the socket is still a raw copy of the parent.
Nothing frees the cloned policies: they stay on the namespace's policy
list and in its count, and the namespace teardown warns in
xfrm_policy_fini().

__xfrm_sk_clone_policy() fails that way when cloning the second policy
fails, with the clone of the first already in the new socket.
bpf_sk_storage_clone() fails when memory is short or when the copy does
not fit into net.core.optmem_max.

Free the policies in a new label at the end of the function. A failed
filter charge still goes to the old one: xfrm_sk_clone_policy() has not
run then, and sk_policy[] still holds the parent's pointers.

Fixes: d188ba86dd07 ("xfrm: add rcu protection to sk->sk_policy[]")
Fixes: 8f51dfc73bf1 ("bpf: support cloning sk storage on accept()")
Suggested-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
---
v3:
- Free the policies at the end of sk_clone() instead of in
  __xfrm_sk_clone_policy(), so there is no unwind loop (Steffen), and a
  later failure of bpf_sk_storage_clone() is covered too (the AI review
  Steffen forwarded). This is the unwind Jakub asked for on Josef's
  patch for the second case:
  https://lore.kernel.org/all/20261020192044.7c3a58a1@kernel.org/
- Hence the new subject; nothing in net/xfrm/ changes any more.
v2: https://lore.kernel.org/all/179540803323.3139700.5340002553684152759@gmail.com/
v1: https://lore.kernel.org/netdev/179505710269.3511679.8893709762919027206@gmail.com/

Both cases reproduce as root in a new network namespace with a loopback
TCP listener that has IP_XFRM_POLICY set in both directions. For the
first, failslab fails the second clone_policy() (a stack filter on
__xfrm_sk_clone_policy() and interval 2). For the second, the listener
also gets an element of a BPF_F_CLONE sk_storage map, and
net.core.optmem_max is then lowered so that the child's copy cannot be
charged. Without the patch, the clones are left behind and the
namespace teardown warns in xfrm_policy_fini(); with it, it does not.

The patch was written with LLM assistance; we reviewed and tested it.

 net/core/sock.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/net/core/sock.c b/net/core/sock.c
index 5675053625e6..d291433749d2 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -2598,19 +2598,20 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
 	RCU_INIT_POINTER(newsk->sk_filter, filter);
 	rcu_read_unlock();
 
-	if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
+	if (unlikely(!is_charged)) {
 		/* We need to make sure that we don't uncharge the new
 		 * socket if we couldn't charge it in the first place
 		 * as otherwise we uncharge the parent's filter.
 		 */
-		if (!is_charged)
-			RCU_INIT_POINTER(newsk->sk_filter, NULL);
-
+		RCU_INIT_POINTER(newsk->sk_filter, NULL);
 		goto free;
 	}
 
+	if (unlikely(xfrm_sk_clone_policy(newsk, sk)))
+		goto free_policy;
+
 	if (bpf_sk_storage_clone(sk, newsk))
-		goto free;
+		goto free_policy;
 
 	/* Clear sk_user_data if parent had the pointer tagged
 	 * as not suitable for copying when cloning.
@@ -2638,6 +2639,8 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
 		sk_sockets_allocated_inc(newsk);
 out:
 	return newsk;
+free_policy:
+	xfrm_sk_free_policy(newsk);
 free:
 	/* It is still raw copy of parent, so invalidate
 	 * destructor and make plain sk_free()
-- 
2.43.0


                 reply	other threads:[~2026-11-27  4:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=179568863376.462699.9896914557163021386@gmail.com \
    --to=kyuwoncho18@gmail.com \
    --cc=ast@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=ipsec@lists.linux.dev \
    --cc=josef@toxicpanda.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    --cc=steffen.klassert@secunet.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

all inboxes | Powered by JetHome®