xfrm: free the policies already cloned when a socket clone fails
Subsystems (3)
v2 cover xfrm: free the policies already cloned when a socket clone fails
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/[email protected]/
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(+)
Signature · 1 line
2.43.0
PATCH v2 1/1 xfrm: free the policies already cloned when a socket clone fails
__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.
--- net/xfrm/xfrm_policy.c | 7 +++++++ 1 file changed, 7 insertions(+)
net/xfrm/xfrm_policy.c +7 -0
@@ -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; }
Signature · 1 line
2.43.0