// 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 #include #include #include #include #include #include #include #include #include #include #include #include #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; }