* [BUG] xfrm: a socket policy cloned for a TCP child leaks when cloning the second policy fails
@ 2026-11-19 3:04 Kyuwon Cho
0 siblings, 0 replies; only message in thread
From: Kyuwon Cho @ 2026-11-19 3:04 UTC (permalink / raw)
To: Steffen Klassert, Herbert Xu
Cc: Eric Dumazet, Kuniyuki Iwashima, David S. Miller, Jakub Kicinski,
Paolo Abeni, Simon Horman, Alexei Starovoitov, Josef Bacik,
netdev, ipsec, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 3644 bytes --]
Hi Steffen, Herbert,
While testing v7.4-rc3 with an LLM-guided test generator that we are
developing as a research prototype, we found that a TCP listener with
socket policies in both directions leaks one xfrm policy when cloning
its second policy for a new child fails. The leaked policy stays on
net->xfrm.policy_all, and the network namespace teardown warns:
WARNING: net/xfrm/xfrm_policy.c:4303 at xfrm_policy_fini+0x24c/0x300, CPU#0: kworker/u8:4/88
RIP: 0010:xfrm_policy_fini (net/xfrm/xfrm_policy.c:4303)
Call Trace:
<TASK>
xfrm_net_exit (net/xfrm/xfrm_policy.c:4372)
ops_undo_list (net/core/net_namespace.c:200 net/core/net_namespace.c:253)
cleanup_net (net/core/net_namespace.c:713)
process_one_work (kernel/workqueue.c:3495)
worker_thread (kernel/workqueue.c:3578 kernel/workqueue.c:3659)
kthread (kernel/kthread.c:436)
ret_from_fork (arch/x86/kernel/process.c:158)
ret_from_fork_asm (arch/x86/entry/entry_64.S:245)
</TASK>
__xfrm_sk_clone_policy() clones the parent's policies into the new
socket one direction at a time and stops at the first failure:
for (i = 0; i < 2; i++) {
p = rcu_dereference(osk->sk_policy[i]);
if (p) {
np = clone_policy(p, i);
if (unlikely(!np)) {
ret = -ENOMEM;
break;
}
rcu_assign_pointer(sk->sk_policy[i], np);
}
}
When clone_policy() fails for sk_policy[1], sk_policy[0] already holds
a new policy that is linked on net->xfrm.policy_all. sk_clone() then
takes its "free" path, which treats the socket as a raw copy of the
parent:
if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
...
goto free;
}
...
free:
/* It is still raw copy of parent, so invalidate
* destructor and make plain sk_free()
*/
newsk->sk_destruct = NULL;
Socket policies are only freed by xfrm_sk_free_policy() from
inet_csk_destroy_sock() and sk_common_release(), so nothing frees the
clone of sk_policy[0]. xfrm_policy_flush() skips socket policies, and
xfrm_policy_fini() finds it when the namespace goes away. The leak
needs one allocation failure during the handshake, in softirq context.
Josef and Alexei's "[PATCH net] net: free the policies if a socket's
clone fails late" (2026-10-17) frees the policies when
bpf_sk_storage_clone() fails, and its message explains why the "free"
label cannot do that for every user: after a failed filter charge,
sk_policy[] still holds the parent's pointers. A partial clone leaves
the same mix, a new sk_policy[0] next to the parent's sk_policy[1], so
it is not covered there.
The reproducer runs as root. In a new network namespace per iteration,
it sets IP_XFRM_POLICY in both directions on a loopback TCP listener
and completes one handshake while failslab fails one slab allocation
(probability 5%, at most one failure). On an x86_64 QEMU guest with 2
vCPUs and KASAN (gcc 13.3), the warning shows up in every run.
IP_XFRM_POLICY itself only needs CAP_NET_ADMIN in the user namespace
that owns the network namespace.
The loop and its early exit come from d188ba86dd07 ("xfrm: add rcu
protection to sk->sk_policy[]").
Checked against v7.4-rc3 (aebb72744764); xfrm_policy.c and sock.c are
the same in net (2a324a58a084).
A patch is attached (git format-patch against v7.4-rc3; it applies to
the ipsec tree):
0001-xfrm-free-the-policies-already-cloned-when-a-socket-.patch
It deletes the policies that __xfrm_sk_clone_policy() already cloned
before it returns the error. It was written with LLM assistance; we
reviewed and tested it. With it, the reproducer no longer triggers the
report.
The reproducer (repro.c), the kernel config and the patch are attached.
Thanks,
Kyuwon Cho
[-- Attachment #2: repro.c --]
[-- Type: text/plain, Size: 2767 bytes --]
// WARNING in xfrm_policy_fini: a socket policy cloned for an accepted TCP child is leaked when cloning the
// listener's second policy fails.
// Each iteration, in a new network namespace, sets an IP_XFRM_POLICY in both directions on a TCP listener and
// completes one loopback handshake while failslab fails one slab allocation (5% per allocation, at most one).
// When the failed allocation is clone_policy() for sk_policy[1], __xfrm_sk_clone_policy() returns -ENOMEM with
// the clone of sk_policy[0] already in the child; sk_clone() frees the child without it, the policy stays on
// net->xfrm.policy_all, and the namespace teardown warns.
// Runs as root (failslab through debugfs).
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/xfrm.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#ifndef IP_XFRM_POLICY
#define IP_XFRM_POLICY 17
#endif
static void failslab(const char *knob, const char *val)
{
char path[96];
int fd;
snprintf(path, sizeof(path), "/sys/kernel/debug/failslab/%s", knob);
fd = open(path, O_WRONLY);
if (fd < 0 || write(fd, val, strlen(val)) < 0)
perror(path);
close(fd);
}
static void one_handshake(int port)
{
struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(port),
.sin_addr.s_addr = htonl(INADDR_LOOPBACK) };
struct ifreq ifr = { .ifr_name = "lo", .ifr_flags = IFF_UP };
int ls, c, s;
unshare(CLONE_NEWNET);
s = socket(AF_INET, SOCK_DGRAM, 0);
ioctl(s, SIOCSIFFLAGS, &ifr);
close(s);
ls = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
bind(ls, (struct sockaddr *)&a, sizeof(a));
listen(ls, 16);
for (int dir = 0; dir < 2; dir++) {
struct xfrm_userpolicy_info pi = { .sel.family = AF_INET, .dir = dir, .action = XFRM_POLICY_ALLOW,
.lft = { XFRM_INF, XFRM_INF, XFRM_INF, XFRM_INF } };
if (setsockopt(ls, SOL_IP, IP_XFRM_POLICY, &pi, sizeof(pi)))
perror("IP_XFRM_POLICY");
}
failslab("times", "1"); /* fail at most one allocation from here */
c = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
connect(c, (struct sockaddr *)&a, sizeof(a));
s = accept(ls, NULL, NULL);
failslab("times", "0");
close(s);
close(c);
close(ls);
}
int main(void)
{
mount("debugfs", "/sys/kernel/debug", "debugfs", 0, NULL);
failslab("ignore-gfp-wait", "0");
failslab("task-filter", "0");
failslab("interval", "1");
failslab("probability", "5");
for (int k = 1; k <= 120; k++) {
pid_t pid = fork();
if (!pid) {
one_handshake(20000 + k);
_exit(0);
}
waitpid(pid, NULL, 0);
}
sleep(40); /* the namespaces are torn down asynchronously */
printf("done\n");
return 0;
}
[-- Attachment #3: config.gz --]
[-- Type: application/gzip, Size: 38966 bytes --]
[-- Attachment #4: 0001-xfrm-free-the-policies-already-cloned-when-a-socket-.patch --]
[-- Type: text/plain, Size: 1722 bytes --]
>From 84f048c2bd57b87dcf83233cf59c4ba1fee855b1 Mon Sep 17 00:00:00 2001
From: Kyuwon Cho <kyuwoncho18@gmail.com>
Date: Thu, 19 Nov 2026 11:36:52 +0900
Subject: [PATCH ipsec] 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 linked on net->xfrm.policy_all. sk_clone() then
frees the new socket as a raw copy of the parent, 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.
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..c6d5c7582af7 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 (rcu_access_pointer(osk->sk_policy[i])) {
+ RCU_INIT_POINTER(sk->sk_policy[i], NULL);
+ xfrm_policy_delete(np, XFRM_POLICY_MAX + i);
+ }
+ }
rcu_read_unlock();
return ret;
}
--
2.43.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-19 3:04 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 3:04 [BUG] xfrm: a socket policy cloned for a TCP child leaks when cloning the second policy fails Kyuwon Cho
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox