Thread View: linux.kernel.netdev
4 messages
4 total messages
Started by Kyuwon Cho
Mon, 23 Nov 2026 04:29
[PATCH ipsec v2 0/1] xfrm: free the policies already cloned when a socket clone fails
Author: Kyuwon Cho
Date: Mon, 23 Nov 2026 04:29
Date: Mon, 23 Nov 2026 04:29
27 lines
11718 bytes
11718 bytes
When cloning a listener's second socket policy fails in sk_clone(), the clone of the first one is leaked until its namespace goes away, where xfrm_policy_fini() warns. The patch deletes the policies cloned so far before __xfrm_sk_clone_policy() returns the error. The issue was found by an LLM-guided test generator that we are developing as a research prototype. v2: - Check the new socket's own slot instead of reading the parent's slot again, which a concurrent setsockopt() on the listener can change (Sashiko review). - Send the patch inline, with its own changelog. v1: https://lore.kernel.org/netdev/179505710269.3511679.8893709762919027206@gmail.com/ The patch was written with LLM assistance; we reviewed and tested it. With it, the reproducer from v1 no longer triggers the warning. Kyuwon Cho (1): xfrm: free the policies already cloned when a socket clone fails net/xfrm/xfrm_policy.c | 7 +++++++ 1 file changed, 7 insertions(+) -- 2.43.0
[PATCH ipsec v2 1/1] xfrm: free the policies already cloned when a socket clone fails
Author: Kyuwon Cho
Date: Mon, 23 Nov 2026 04:29
Date: Mon, 23 Nov 2026 04:29
45 lines
12612 bytes
12612 bytes
__xfrm_sk_clone_policy() clones the parent's socket policies one
direction at a time and returns -ENOMEM at the first failure. If the
policy for the second direction cannot be cloned, the clone of the
first one is already set in the new socket and linked on
net->xfrm.policy_all. sk_clone() then frees the new socket without
xfrm_sk_free_policy(), and the policy is leaked until the network
namespace goes away:
WARNING: net/xfrm/xfrm_policy.c:4303 at xfrm_policy_fini+0x24c/0x300
Call Trace:
xfrm_net_exit (net/xfrm/xfrm_policy.c:4372)
ops_undo_list (net/core/net_namespace.c:253)
cleanup_net (net/core/net_namespace.c:713)
Delete the policies cloned so far before returning the error.
xfrm_sk_clone_policy() clears the new socket's slots before cloning,
so a slot that is set holds one of these clones.
Fixes: d188ba86dd07 ("xfrm: add rcu protection to sk->sk_policy[]")
Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
---
net/xfrm/xfrm_policy.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index cea6842e6454..dba1851f0b3d 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);
+ }
+ }
rcu_read_unlock();
return ret;
}
--
2.43.0
Re: [PATCH ipsec v2 1/1] xfrm: free the policies already cloned when a socket clone fails
Author: Steffen Klassert
Date: Thu, 26 Nov 2026 08:57
Date: Thu, 26 Nov 2026 08:57
45 lines
11226 bytes
11226 bytes
On Mon, Nov 23, 2026 at 01:29:42PM +0900, Kyuwon Cho wrote:
> __xfrm_sk_clone_policy() clones the parent's socket policies one
> direction at a time and returns -ENOMEM at the first failure. If the
> policy for the second direction cannot be cloned, the clone of the
> first one is already set in the new socket and linked on
> net->xfrm.policy_all. sk_clone() then frees the new socket without
> xfrm_sk_free_policy(), and the policy is leaked until the network
> namespace goes away:
>
> WARNING: net/xfrm/xfrm_policy.c:4303 at xfrm_policy_fini+0x24c/0x300
> Call Trace:
> xfrm_net_exit (net/xfrm/xfrm_policy.c:4372)
> ops_undo_list (net/core/net_namespace.c:253)
> cleanup_net (net/core/net_namespace.c:713)
>
> Delete the policies cloned so far before returning the error.
> xfrm_sk_clone_policy() clears the new socket's slots before cloning,
> so a slot that is set holds one of these clones.
>
> Fixes: d188ba86dd07 ("xfrm: add rcu protection to sk->sk_policy[]")
> Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
> ---
> net/xfrm/xfrm_policy.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
> index cea6842e6454..dba1851f0b3d 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);
> + }
> + }
Do you really need an unroll loop here? This function clones
at most two policies. If the second fails, just delete the
first one.
Re: [PATCH ipsec v2 1/1] xfrm: free the policies already cloned when a socket clone fails
Author: Steffen Klassert
Date: Thu, 26 Nov 2026 08:59
Date: Thu, 26 Nov 2026 08:59
88 lines
12774 bytes
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;
> }
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads