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 Signed-off-by: Kyuwon Cho --- 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