Re: [PATCH ipsec v3] net: free the socket policies when sk_clone() fails

From: Simon Horman

Date: Fri Dec 04 2026 - 15:20:24 EST


On Fri, Nov 27, 2026 at 01:58:10PM +0900, Kyuwon Cho wrote:
> 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@xxxxxxxxxx>
> Signed-off-by: Kyuwon Cho <kyuwoncho18@xxxxxxxxx>
> ---
> 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@xxxxxxxxxx/
> - Hence the new subject; nothing in net/xfrm/ changes any more.
> v2: https://lore.kernel.org/all/179540803323.3139700.5340002553684152759@xxxxxxxxx/
> v1: https://lore.kernel.org/netdev/179505710269.3511679.8893709762919027206@xxxxxxxxx/
>
> 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.

Reviewed-by: Simon Horman <horms@xxxxxxxxxx>