From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795755498; b=Wq1B462LKoWd0BLKJLZ/NHxHHjp53wxAoY3WUpheYyzyRUKf7UuB1bV8ndSGHM/IwVPN +pPgnvKB+MagAFnaY0ZqC1jbK0LutC5yCQYbX4leFZ7/AbEviM8wWPINTSkbj2uHZwTD6 g108UmKyw52JEZfFIPyrIC6mOIkJCZMyKr0hmNNLM+1bfK5IB+VDPhHfneNgr8XzskY55 /aTdeTRJmu2NXFWjcXku95ZnaDAJSG3lXB6OE66+NVzdqH+znwDFWRsq4uz/hsOrWapjB q2udCRKv4HO5QvbhwZObp9TSBxIzGH2Uss5Kr2ep6xQhuCb9QlJB+Up3pUwIR5Ih3XQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795755498; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:MIME-Version: Content-Transfer-Encoding; bh=M7vTOR3oemTA1W0hJKqVqhgWngwg5/C2IpV/104sVMI=; b=NjXuo/6M/bkPnrCQ17FX3Zncip1P39gTyqjKvQ0SUoaQ+6e9sB2LNPOjiJDBEqSQFc9U kuyLYzto8YaW6/FnfGuHfCALQGmUdhE9GYC1lo8PQ0rMhYqq5qOZCOb4qxuCCk3DWZFNE DV9q/Rr2NcRZpWIvsCaJ/MHy26I/I1Zg/zlAUtvGOiJcLIU5f/XHW3s5/gGmuCIpyjD8R cpuSZ1+eViUn57qiJ4ggnQmIT8Cxa07jtDNN83jtYObcKwIkCaE5MEfO1oP2HdbWvb2Yp aBvvhjahKXLpLJxJzizC1QiHM6JMVOdifxUoMQCpTaCiCl/tjHIuJsflAKqdGlSM4Rw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=egq0a59s; arc=none smtp.remote-ip=209.85.216.54 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBF0A606A31E for ; Fri, 27 Nov 2026 04:58:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CBF0A606A31E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=egq0a59s Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-3b4a93d0814so2673829a91.1 for ; Thu, 26 Nov 2026 20:58:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795755497; x=1796360297; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=M7vTOR3oemTA1W0hJKqVqhgWngwg5/C2IpV/104sVMI=; b=egq0a59sOKC+hNlgn9DJt91f1YQF25DaRHQraiur/NltXmKS8MZRSXYKFKrzI0ZH9j MBSuj/hFLnKTo7Vm1nEs7TCOQtmjJf6JF3flABg8LZGf/jRG92FME/2IDw5vvjV+fEHe fo+C7ZZUZWeC260YoAKD6/txowXhpH2vg1hFSJ1lBMqtfO1wWOgbEh63t/OqO6iV7O0I 9qK48o3pgZjFOAsG+5uDpOofXWZ4xMqBmzhimV+ZkLEMSSgt7hLtGZXfbPG6CgX9hf7M K69jUs8qlvYczyKNzk/6i1z/qCFF18Em+mh6+ZhzzbpTl6cabrD/Vnp7AvtF5iD6R8eQ 3BFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795755497; x=1796360297; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=M7vTOR3oemTA1W0hJKqVqhgWngwg5/C2IpV/104sVMI=; b=R5iz3gjezwGnl3zHsfd6qLp70H8ysWkHpONqzs5XvVrTuasVp1D2XgNSkb9zF9ffzj 8vqaykwsb3RsSQEcRrycJhXqeXcL2p4uWJGNkmBRgOktWVSau96sVPxr78CJQ/fOYeYF fK52VDB6RyRmBXLLBK+YVH1EqfVu/bX/AlObGpatDi0PWA2cRP3tw0WHj5m3Gcamatry CbX97yD1d7UA1ygmgT/eEOzIw6hSR72EL0KP3Zmohm7aEhANF4Pja0AXklNiU8IO+LA8 +DTmWNeWnlLuXtcsUfkfBuWVCZmjckp/jaB+hvrdu+rjUYnvXwAqBXMhcB/1aMIh3Qy0 HoSA== X-Forwarded-Encrypted: i=1; AL+hErFzg62XUytYuBCbtdWXouthixZu3zzyiJq4J+xf5Cd1mWYXOmsyHXEH6lBfEAGH3k7QXG7K96xhphxkRec=@vger.kernel.org X-Gm-Message-State: APuU4Z8EXVN44nthfd/KbYb7zJvzyRFZRrBbfLt/WUSeP0EdXz/1PLiV tmAG5q+uGB3NslYUQ+/B7nyPmlRZ7eNril7aKEk/FDVfHVvTRe447t0i X-Gm-Gg: AZFdTZegEOSG/6q+k8TWD7y+GGhd7WxdMWZ88KG0JPfMZMC+uGeECnhdwL9ajL4qm0c KIgDwOkqjlwaReP0QnZLjyQmjBku1qt6YjsplObPjVk/qJrZGs9rk0eUyvU338hLa2+F57wjELS arcYtpxhMgGKtQYd2bF0t11aNDh4UYK5pstYKD9rPiFxlWsR28zWE91BZKLcCRuwD9DAYmb4Mkn 3caj4Z/Tgw5RhCmvtntLFwrAyK7XOFXMXn9kr2YzbYhJroO08KIkmvzP+0l8PV7PS72+huB40fU Q2j8RwOyUMAA3sQ/s+cA3pD7BApoFeSboq+NzXqo76Q+hVSaVVhBXDtCHLFWfyqkJL3G8m7q3Nu Q+Frz+KfJQ/jEnypUB/IYBkMOG7AFGhKxx9w76lIS1rTikdpGuvKAHdqT/VRGUHho9zywE0edpU tOEcBi8l3ZeBay0Rb4AM5x1U+VYyhvM0iWrZlkeLKNFtPI+MMYi/rbiUtSNLmSpgugPd/Yx0Vsy 0ANItG/e90gGURq5IeVTCRwS7Mbb5TdTb8AsR0Nkw3MBZ4lPs1+oE1C6qVLQqKQZw03tT+JrnjG VPDiJIvtFzgoFvWkanWcgLhVxlta3djV2yvFwYc5wOVwUB/LCkfeDEkJQC30Od5hmos= X-Received: by 2002:a17:90b:2e8c:b0:3ad:d4aa:8559 with SMTP id 98e67ed59e1d1-3b41c8e105bmr19924654a91.17.1795755497037; Thu, 26 Nov 2026 20:58:17 -0800 (PST) Received: from d20be28ee783.tail196be.ts.net (vldb.skku.ac.kr. [115.145.173.180]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b405d96bd3sm39010808a91.12.2026.11.26.20.58.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Nov 2026 20:58:16 -0800 (PST) From: Kyuwon Cho To: Steffen Klassert , Herbert Xu Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Simon Horman , Kuniyuki Iwashima , Josef Bacik , Alexei Starovoitov , Stanislav Fomichev , 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 Message-ID: <179568863376.462699.9896914557163021386@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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