summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/pid_namespace
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-11-21 17:22:21 -0800
committerLinus Torvalds <torvalds@linux-foundation.org>2026-11-21 17:22:21 -0800
commit4843f6873f7c4835e62f2c73ba0fd538e57a2f22 (patch)
treeee620501a1ffbad9a83141e3a70154e1fe641040 /tools/testing/selftests/pid_namespace
downloadlinux-master.tar.gz
linux-master.zip
Merge tag 'sysctl-7.04-fixes-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctlgraftedHEADmaster
Pull sysctl fix from Joel Granados: "Fix sysctl jiffy / milliseconds conversion: - Writing a previously read value "X" shall keep the same "X" value - Remove truncation that was causing incorrect saturation of valid timeouts" * tag 'sysctl-7.04-fixes-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctl: sysctl: Do jiffy / ms conversion in ulong space
Diffstat (limited to 'tools/testing/selftests/pid_namespace')
-rw-r--r--tools/testing/selftests/pid_namespace/.gitignore4
-rw-r--r--tools/testing/selftests/pid_namespace/Makefile8
-rw-r--r--tools/testing/selftests/pid_namespace/config2
-rw-r--r--tools/testing/selftests/pid_namespace/pid_max.c451
-rw-r--r--tools/testing/selftests/pid_namespace/pidns_dead_ancestor.c174
-rw-r--r--tools/testing/selftests/pid_namespace/pidns_init_via_setns.c238
-rw-r--r--tools/testing/selftests/pid_namespace/regression_enomem.c44
7 files changed, 921 insertions, 0 deletions
diff --git a/tools/testing/selftests/pid_namespace/.gitignore b/tools/testing/selftests/pid_namespace/.gitignore
new file mode 100644
index 000000000..b87295802
--- /dev/null
+++ b/tools/testing/selftests/pid_namespace/.gitignore
@@ -0,0 +1,4 @@
+pid_max
+pidns_dead_ancestor
+pidns_init_via_setns
+regression_enomem
diff --git a/tools/testing/selftests/pid_namespace/Makefile b/tools/testing/selftests/pid_namespace/Makefile
new file mode 100644
index 000000000..3199e6beb
--- /dev/null
+++ b/tools/testing/selftests/pid_namespace/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+CFLAGS += -g $(KHDR_INCLUDES)
+
+TEST_GEN_PROGS = regression_enomem pid_max pidns_dead_ancestor pidns_init_via_setns
+
+LOCAL_HDRS += $(selfdir)/pidfd/pidfd.h
+
+include ../lib.mk
diff --git a/tools/testing/selftests/pid_namespace/config b/tools/testing/selftests/pid_namespace/config
new file mode 100644
index 000000000..26cdb27e7
--- /dev/null
+++ b/tools/testing/selftests/pid_namespace/config
@@ -0,0 +1,2 @@
+CONFIG_PID_NS=y
+CONFIG_USER_NS=y
diff --git a/tools/testing/selftests/pid_namespace/pid_max.c b/tools/testing/selftests/pid_namespace/pid_max.c
new file mode 100644
index 000000000..5d686a09a
--- /dev/null
+++ b/tools/testing/selftests/pid_namespace/pid_max.c
@@ -0,0 +1,451 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#define _GNU_SOURCE
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/types.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syscall.h>
+#include <sys/mount.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+#include "../pidfd/pidfd.h"
+
+/*
+ * The kernel computes the minimum allowed pid_max as:
+ * max(RESERVED_PIDS + 1, PIDS_PER_CPU_MIN * num_possible_cpus())
+ * Mirror that here so the test values are always valid.
+ *
+ * Note: glibc's get_nprocs_conf() returns the number of *configured*
+ * (present) CPUs, not *possible* CPUs. The kernel uses
+ * num_possible_cpus() which corresponds to /sys/devices/system/cpu/possible.
+ * These can differ significantly (e.g. 16 configured vs 128 possible).
+ */
+#define RESERVED_PIDS 300
+#define PIDS_PER_CPU_MIN 8
+
+/* Count CPUs from a range list like "0-31" or "0-15,32-47". */
+static int num_possible_cpus(void)
+{
+ FILE *f;
+ int count = 0;
+ int lo, hi;
+
+ f = fopen("/sys/devices/system/cpu/possible", "r");
+ if (!f)
+ return 0;
+
+ while (fscanf(f, "%d", &lo) == 1) {
+ if (fscanf(f, "-%d", &hi) == 1)
+ count += hi - lo + 1;
+ else
+ count++;
+ /* skip comma separator */
+ fscanf(f, ",");
+ }
+
+ fclose(f);
+ return count;
+}
+
+static int pid_min(void)
+{
+ int cpu_min = PIDS_PER_CPU_MIN * num_possible_cpus();
+
+ return cpu_min > (RESERVED_PIDS + 1) ? cpu_min : (RESERVED_PIDS + 1);
+}
+
+/*
+ * Outer and inner pid_max limits used by the tests. The outer limit is
+ * the more restrictive ancestor; the inner limit is set higher in a
+ * nested namespace but must still be capped by the outer limit.
+ * Both are derived from the kernel's minimum so they are always writable.
+ *
+ * Global so that clone callbacks can access them without parameter plumbing.
+ */
+static int outer_limit;
+static int inner_limit;
+
+static int write_int_to_fd(int fd, int val)
+{
+ char buf[12];
+ int len = snprintf(buf, sizeof(buf), "%d", val);
+
+ return write(fd, buf, len);
+}
+
+#define __STACK_SIZE (8 * 1024 * 1024)
+static pid_t do_clone(int (*fn)(void *), void *arg, int flags)
+{
+ char *stack;
+ pid_t ret;
+
+ stack = malloc(__STACK_SIZE);
+ if (!stack)
+ return -ENOMEM;
+
+#ifdef __ia64__
+ ret = __clone2(fn, stack, __STACK_SIZE, flags | SIGCHLD, arg);
+#else
+ ret = clone(fn, stack + __STACK_SIZE, flags | SIGCHLD, arg);
+#endif
+ free(stack);
+ return ret;
+}
+
+static int pid_max_cb(void *data)
+{
+ int fd, ret;
+ pid_t pid;
+
+ ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
+ if (ret) {
+ fprintf(stderr, "%m - Failed to make rootfs private mount\n");
+ return -1;
+ }
+
+ umount2("/proc", MNT_DETACH);
+
+ ret = mount("proc", "/proc", "proc", 0, NULL);
+ if (ret) {
+ fprintf(stderr, "%m - Failed to mount proc\n");
+ return -1;
+ }
+
+ fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
+ if (fd < 0) {
+ fprintf(stderr, "%m - Failed to open pid_max\n");
+ return -1;
+ }
+
+ ret = write_int_to_fd(fd, inner_limit);
+ if (ret < 0) {
+ fprintf(stderr, "%m - Failed to write pid_max\n");
+ return -1;
+ }
+
+ for (int i = 0; i < inner_limit + 1; i++) {
+ pid = fork();
+ if (pid == 0)
+ exit(EXIT_SUCCESS);
+ wait_for_pid(pid);
+ if (pid > inner_limit) {
+ fprintf(stderr, "Managed to create pid number beyond limit\n");
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static int pid_max_nested_inner(void *data)
+{
+ int fret = -1;
+ pid_t pids[2];
+ int fd, i, ret;
+
+ ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
+ if (ret) {
+ fprintf(stderr, "%m - Failed to make rootfs private mount\n");
+ return fret;
+ }
+
+ umount2("/proc", MNT_DETACH);
+
+ ret = mount("proc", "/proc", "proc", 0, NULL);
+ if (ret) {
+ fprintf(stderr, "%m - Failed to mount proc\n");
+ return fret;
+ }
+
+ fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
+ if (fd < 0) {
+ fprintf(stderr, "%m - Failed to open pid_max\n");
+ return fret;
+ }
+
+ ret = write_int_to_fd(fd, inner_limit);
+ close(fd);
+ if (ret < 0) {
+ fprintf(stderr, "%m - Failed to write pid_max\n");
+ return fret;
+ }
+
+ pids[0] = fork();
+ if (pids[0] < 0) {
+ fprintf(stderr, "Failed to create first new process\n");
+ return fret;
+ }
+
+ if (pids[0] == 0)
+ exit(EXIT_SUCCESS);
+
+ pids[1] = fork();
+ wait_for_pid(pids[0]);
+ if (pids[1] >= 0) {
+ if (pids[1] == 0)
+ exit(EXIT_SUCCESS);
+ wait_for_pid(pids[1]);
+
+ fprintf(stderr, "Managed to create process even though ancestor pid namespace had a limit\n");
+ return fret;
+ }
+
+ /* Now make sure that we wrap pids at outer_limit. */
+ for (i = 0; i < inner_limit + 10; i++) {
+ pid_t pid;
+
+ pid = fork();
+ if (pid < 0)
+ return fret;
+
+ if (pid == 0)
+ exit(EXIT_SUCCESS);
+
+ wait_for_pid(pid);
+ if (pid >= inner_limit) {
+ fprintf(stderr, "Managed to create process with pid %d beyond configured limit\n", pid);
+ return fret;
+ }
+ }
+
+ return 0;
+}
+
+static int pid_max_nested_outer(void *data)
+{
+ int fret = -1, nr_procs = 0;
+ pid_t *pids;
+ int fd, ret;
+ pid_t pid;
+
+ pids = malloc(outer_limit * sizeof(pid_t));
+ if (!pids)
+ return -1;
+
+ ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
+ if (ret) {
+ fprintf(stderr, "%m - Failed to make rootfs private mount\n");
+ goto out;
+ }
+
+ umount2("/proc", MNT_DETACH);
+
+ ret = mount("proc", "/proc", "proc", 0, NULL);
+ if (ret) {
+ fprintf(stderr, "%m - Failed to mount proc\n");
+ goto out;
+ }
+
+ fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
+ if (fd < 0) {
+ fprintf(stderr, "%m - Failed to open pid_max\n");
+ goto out;
+ }
+
+ ret = write_int_to_fd(fd, outer_limit);
+ close(fd);
+ if (ret < 0) {
+ fprintf(stderr, "%m - Failed to write pid_max\n");
+ goto out;
+ }
+
+ /*
+ * Create (outer_limit - 4) processes. This leaves room for
+ * do_clone() and one more. So creating another process needs
+ * to fail.
+ */
+ for (nr_procs = 0; nr_procs < outer_limit - 4; nr_procs++) {
+ pid = fork();
+ if (pid < 0)
+ goto reap;
+
+ if (pid == 0)
+ exit(EXIT_SUCCESS);
+
+ pids[nr_procs] = pid;
+ }
+
+ pid = do_clone(pid_max_nested_inner, NULL, CLONE_NEWPID | CLONE_NEWNS);
+ if (pid < 0) {
+ fprintf(stderr, "%m - Failed to clone nested pidns\n");
+ goto reap;
+ }
+
+ if (wait_for_pid(pid)) {
+ fprintf(stderr, "%m - Nested pid_max failed\n");
+ goto reap;
+ }
+
+ fret = 0;
+
+reap:
+ for (int i = 0; i < nr_procs; i++)
+ wait_for_pid(pids[i]);
+
+out:
+ free(pids);
+ return fret;
+}
+
+static int pid_max_nested_limit_inner(void *data)
+{
+ int fret = -1, nr_procs = 0;
+ int fd, ret;
+ pid_t pid;
+ pid_t *pids;
+
+ pids = malloc(inner_limit * sizeof(pid_t));
+ if (!pids)
+ return -1;
+
+ ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
+ if (ret) {
+ fprintf(stderr, "%m - Failed to make rootfs private mount\n");
+ goto out;
+ }
+
+ umount2("/proc", MNT_DETACH);
+
+ ret = mount("proc", "/proc", "proc", 0, NULL);
+ if (ret) {
+ fprintf(stderr, "%m - Failed to mount proc\n");
+ goto out;
+ }
+
+ fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
+ if (fd < 0) {
+ fprintf(stderr, "%m - Failed to open pid_max\n");
+ goto out;
+ }
+
+ ret = write_int_to_fd(fd, inner_limit);
+ close(fd);
+ if (ret < 0) {
+ fprintf(stderr, "%m - Failed to write pid_max\n");
+ goto out;
+ }
+
+ for (nr_procs = 0; nr_procs < inner_limit; nr_procs++) {
+ pid = fork();
+ if (pid < 0)
+ break;
+
+ if (pid == 0)
+ exit(EXIT_SUCCESS);
+
+ pids[nr_procs] = pid;
+ }
+
+ if (nr_procs >= outer_limit) {
+ fprintf(stderr, "Managed to create processes beyond the configured outer limit\n");
+ goto reap;
+ }
+
+ fret = 0;
+
+reap:
+ for (int i = 0; i < nr_procs; i++)
+ wait_for_pid(pids[i]);
+
+out:
+ free(pids);
+ return fret;
+}
+
+static int pid_max_nested_limit_outer(void *data)
+{
+ int fd, ret;
+ pid_t pid;
+
+ ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
+ if (ret) {
+ fprintf(stderr, "%m - Failed to make rootfs private mount\n");
+ return -1;
+ }
+
+ umount2("/proc", MNT_DETACH);
+
+ ret = mount("proc", "/proc", "proc", 0, NULL);
+ if (ret) {
+ fprintf(stderr, "%m - Failed to mount proc\n");
+ return -1;
+ }
+
+ fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
+ if (fd < 0) {
+ fprintf(stderr, "%m - Failed to open pid_max\n");
+ return -1;
+ }
+
+ ret = write_int_to_fd(fd, outer_limit);
+ close(fd);
+ if (ret < 0) {
+ fprintf(stderr, "%m - Failed to write pid_max\n");
+ return -1;
+ }
+
+ pid = do_clone(pid_max_nested_limit_inner, NULL, CLONE_NEWPID | CLONE_NEWNS);
+ if (pid < 0) {
+ fprintf(stderr, "%m - Failed to clone nested pidns\n");
+ return -1;
+ }
+
+ if (wait_for_pid(pid)) {
+ fprintf(stderr, "%m - Nested pid_max failed\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+FIXTURE(pid_max) {
+ int dummy;
+};
+
+FIXTURE_SETUP(pid_max)
+{
+ int min = pid_min();
+
+ outer_limit = min + 100;
+ inner_limit = min + 200;
+}
+
+FIXTURE_TEARDOWN(pid_max)
+{
+}
+
+TEST_F(pid_max, simple)
+{
+ pid_t pid;
+
+ pid = do_clone(pid_max_cb, NULL, CLONE_NEWPID | CLONE_NEWNS);
+ ASSERT_GT(pid, 0);
+ ASSERT_EQ(0, wait_for_pid(pid));
+}
+
+TEST_F(pid_max, nested_limit)
+{
+ pid_t pid;
+
+ pid = do_clone(pid_max_nested_limit_outer, NULL, CLONE_NEWPID | CLONE_NEWNS);
+ ASSERT_GT(pid, 0);
+ ASSERT_EQ(0, wait_for_pid(pid));
+}
+
+TEST_F(pid_max, nested)
+{
+ pid_t pid;
+
+ pid = do_clone(pid_max_nested_outer, NULL, CLONE_NEWPID | CLONE_NEWNS);
+ ASSERT_GT(pid, 0);
+ ASSERT_EQ(0, wait_for_pid(pid));
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/pid_namespace/pidns_dead_ancestor.c b/tools/testing/selftests/pid_namespace/pidns_dead_ancestor.c
new file mode 100644
index 000000000..7524d4975
--- /dev/null
+++ b/tools/testing/selftests/pid_namespace/pidns_dead_ancestor.c
@@ -0,0 +1,174 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#define STACK_SIZE (1024 * 1024)
+
+struct pidns_init_args {
+ int ready_read;
+ int ready_write;
+ int release_read;
+ int release_write;
+};
+
+static int pidns_init(void *data)
+{
+ struct pidns_init_args *args = data;
+ char release;
+ int error = 0;
+
+ close(args->ready_read);
+ close(args->release_write);
+ if (unshare(CLONE_NEWPID))
+ error = errno;
+ if (write(args->ready_write, &error, sizeof(error)) != sizeof(error))
+ return 1;
+ close(args->ready_write);
+ if (error)
+ return 0;
+ if (read(args->release_read, &release, sizeof(release)) != sizeof(release))
+ return 1;
+ close(args->release_read);
+ return 0;
+}
+
+static int create_unborn_pidns(void)
+{
+ struct pidns_init_args args;
+ char path[64], release = 0;
+ void *stack;
+ pid_t child;
+ int ready[2], release_pipe[2], error, status, nsfd = -1;
+ ssize_t n;
+
+ if (pipe(ready))
+ return -1;
+ if (pipe(release_pipe)) {
+ error = errno;
+ close(ready[0]);
+ close(ready[1]);
+ errno = error;
+ return -1;
+ }
+
+ stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
+ if (stack == MAP_FAILED) {
+ error = errno;
+ close(ready[0]);
+ close(ready[1]);
+ close(release_pipe[0]);
+ close(release_pipe[1]);
+ errno = error;
+ return -1;
+ }
+
+ args = (struct pidns_init_args) {
+ .ready_read = ready[0],
+ .ready_write = ready[1],
+ .release_read = release_pipe[0],
+ .release_write = release_pipe[1],
+ };
+ child = clone(pidns_init, stack + STACK_SIZE, CLONE_NEWPID | SIGCHLD,
+ &args);
+ if (child < 0)
+ goto err_munmap;
+
+ close(ready[1]);
+ close(release_pipe[0]);
+ n = read(ready[0], &error, sizeof(error));
+ if (n != sizeof(error)) {
+ error = n < 0 ? errno : EIO;
+ goto release_child;
+ }
+ if (error)
+ goto release_child;
+
+ snprintf(path, sizeof(path), "/proc/%d/ns/pid_for_children", child);
+ nsfd = open(path, O_RDONLY | O_CLOEXEC);
+ if (nsfd < 0) {
+ error = errno;
+ goto release_child;
+ }
+
+release_child:
+ write(release_pipe[1], &release, sizeof(release));
+ close(release_pipe[1]);
+ close(ready[0]);
+ if (waitpid(child, &status, 0) != child || !WIFEXITED(status) ||
+ WEXITSTATUS(status)) {
+ if (!error)
+ error = ECHILD;
+ if (nsfd >= 0)
+ close(nsfd);
+ nsfd = -1;
+ }
+ munmap(stack, STACK_SIZE);
+ if (error) {
+ errno = error;
+ return -1;
+ }
+ return nsfd;
+
+err_munmap:
+ error = errno;
+ munmap(stack, STACK_SIZE);
+ close(ready[0]);
+ close(ready[1]);
+ close(release_pipe[0]);
+ close(release_pipe[1]);
+ errno = error;
+ return -1;
+}
+
+TEST(pidns_dead_ancestor_rejects_fork)
+{
+ int original_fd, target_fd, fork_errno, ret;
+ pid_t child;
+
+ target_fd = create_unborn_pidns();
+ if (target_fd < 0) {
+ if (errno == ENOENT)
+ SKIP(return, "PID namespace handle unavailable through procfs\n");
+ if (errno == EPERM || errno == EACCES || errno == EINVAL)
+ SKIP(return, "PID namespace creation requires privileges\n");
+ ASSERT_GE(target_fd, 0);
+ }
+
+ original_fd = open("/proc/self/ns/pid_for_children", O_RDONLY | O_CLOEXEC);
+ ASSERT_GE(original_fd, 0);
+ ret = setns(target_fd, CLONE_NEWPID);
+ if (ret) {
+ if (errno == EPERM || errno == EACCES)
+ SKIP(return, "setns into PID namespace requires privileges\n");
+ }
+ ASSERT_EQ(0, ret);
+
+ child = fork();
+ fork_errno = errno;
+ if (child == 0)
+ _exit(0);
+
+ if (child > 0)
+ ASSERT_EQ(child, waitpid(child, NULL, 0));
+ ret = setns(original_fd, CLONE_NEWPID);
+ close(original_fd);
+ close(target_fd);
+ ASSERT_EQ(0, ret);
+
+ ASSERT_EQ(-1, child);
+ ASSERT_EQ(ENOMEM, fork_errno);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/pid_namespace/pidns_init_via_setns.c b/tools/testing/selftests/pid_namespace/pidns_init_via_setns.c
new file mode 100644
index 000000000..520835ca4
--- /dev/null
+++ b/tools/testing/selftests/pid_namespace/pidns_init_via_setns.c
@@ -0,0 +1,238 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <sched.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+#include "../pidfd/pidfd.h"
+
+/*
+ * Test that a process can become PID 1 (init) in a new PID namespace
+ * created via unshare() and joined via setns().
+ *
+ * Flow:
+ * 1. Parent creates a pipe for synchronization.
+ * 2. Parent forks a child.
+ * 3. Parent calls unshare(CLONE_NEWPID) to create a new PID namespace.
+ * 4. Parent signals the child via the pipe.
+ * 5. Child opens parent's /proc/<ppid>/ns/pid_for_children and calls
+ * setns(fd, CLONE_NEWPID) to join the new namespace.
+ * 6. Child forks a grandchild.
+ * 7. Grandchild verifies getpid() == 1.
+ */
+TEST(pidns_init_via_setns)
+{
+ pid_t child, parent_pid;
+ int pipe_fd[2];
+ char buf;
+
+ if (geteuid())
+ ASSERT_EQ(0, unshare(CLONE_NEWUSER));
+
+ parent_pid = getpid();
+
+ ASSERT_EQ(0, pipe(pipe_fd));
+
+ child = fork();
+ ASSERT_GE(child, 0);
+
+ if (child == 0) {
+ char path[256];
+ int nsfd;
+ pid_t grandchild;
+
+ close(pipe_fd[1]);
+
+ /* Wait for parent to complete unshare */
+ ASSERT_EQ(1, read_nointr(pipe_fd[0], &buf, 1));
+ close(pipe_fd[0]);
+
+ snprintf(path, sizeof(path),
+ "/proc/%d/ns/pid_for_children", parent_pid);
+ nsfd = open(path, O_RDONLY);
+ ASSERT_GE(nsfd, 0);
+
+ ASSERT_EQ(0, setns(nsfd, CLONE_NEWPID));
+ close(nsfd);
+
+ grandchild = fork();
+ ASSERT_GE(grandchild, 0);
+
+ if (grandchild == 0) {
+ /* Should be init (PID 1) in the new namespace */
+ if (getpid() != 1)
+ _exit(1);
+ _exit(0);
+ }
+
+ ASSERT_EQ(0, wait_for_pid(grandchild));
+ _exit(0);
+ }
+
+ close(pipe_fd[0]);
+
+ ASSERT_EQ(0, unshare(CLONE_NEWPID));
+
+ /* Signal child that the new PID namespace is ready */
+ buf = 0;
+ ASSERT_EQ(1, write_nointr(pipe_fd[1], &buf, 1));
+ close(pipe_fd[1]);
+
+ ASSERT_EQ(0, wait_for_pid(child));
+}
+
+/*
+ * Similar to pidns_init_via_setns, but:
+ * 1. Parent enters a new PID namespace right from the start to be able to
+ * later freely use pid 1001 in it.
+ * 2. After forking child, parent also calls unshare(CLONE_NEWUSER)
+ * before unshare(CLONE_NEWPID) so that new old and new pid namespaces have
+ * different user namespace owners.
+ * 3. Child uses clone3() with set_tid={1, 1001} instead of fork() and
+ * grandchild checks that it gets desired pids .
+ *
+ * Flow:
+ * 1. Test process creates a new PID namespace and forks a wrapper
+ * (PID 1 in the outer namespace).
+ * 2. Wrapper forks a child.
+ * 3. Wrapper calls unshare(CLONE_NEWUSER) + unshare(CLONE_NEWPID)
+ * to create an inner PID namespace.
+ * 4. Wrapper signals the child via pipe.
+ * 5. Child opens wrapper's /proc/<pid>/ns/pid_for_children and calls
+ * setns(fd, CLONE_NEWPID) to join the inner namespace.
+ * 6. Child calls clone3() with set_tid={1, 1001}.
+ * 7. Grandchild verifies its NSpid ends with "1001 1".
+ */
+
+pid_t set_tid[] = {1, 1001};
+
+static int pidns_init_via_setns_set_tid_grandchild(struct __test_metadata *_metadata)
+{
+ char *line = NULL;
+ size_t len = 0;
+ int found = 0;
+ FILE *gf;
+
+ gf = fopen("/proc/self/status", "r");
+ ASSERT_NE(gf, NULL);
+
+ while (getline(&line, &len, gf) != -1) {
+ if (strncmp(line, "NSpid:", 6) != 0)
+ continue;
+
+ for (int i = 0; i < 2; i++) {
+ char *last = strrchr(line, '\t');
+ pid_t pid;
+
+ ASSERT_NE(last, NULL);
+ ASSERT_EQ(sscanf(last, "%d", &pid), 1);
+ ASSERT_EQ(pid, set_tid[i]);
+ *last = '\0';
+ }
+
+ found = true;
+ break;
+ }
+
+ free(line);
+ fclose(gf);
+ ASSERT_TRUE(found);
+ return 0;
+}
+
+static int pidns_init_via_setns_set_tid_child(struct __test_metadata *_metadata,
+ pid_t parent_pid, int pipe_fd[2])
+{
+ struct __clone_args args = {
+ .exit_signal = SIGCHLD,
+ .set_tid = ptr_to_u64(set_tid),
+ .set_tid_size = 2,
+ };
+ pid_t grandchild;
+ char path[256];
+ char buf;
+ int nsfd;
+
+ close(pipe_fd[1]);
+
+ ASSERT_EQ(1, read_nointr(pipe_fd[0], &buf, 1));
+ close(pipe_fd[0]);
+
+ snprintf(path, sizeof(path),
+ "/proc/%d/ns/pid_for_children", parent_pid);
+ nsfd = open(path, O_RDONLY);
+ ASSERT_GE(nsfd, 0);
+
+ ASSERT_EQ(0, setns(nsfd, CLONE_NEWPID));
+ close(nsfd);
+
+ grandchild = sys_clone3(&args, sizeof(args));
+ ASSERT_GE(grandchild, 0);
+
+ if (grandchild == 0)
+ _exit(pidns_init_via_setns_set_tid_grandchild(_metadata));
+
+ ASSERT_EQ(0, wait_for_pid(grandchild));
+ return 0;
+}
+
+static int pidns_init_via_setns_set_tid_wrapper(struct __test_metadata *_metadata)
+{
+ int pipe_fd[2];
+ pid_t child, parent_pid;
+ char buf;
+ FILE *f;
+
+ /*
+ * We are PID 1 inside the new namespace, but /proc is
+ * mounted from the host. Read our host-visible PID so
+ * the child can reach our pid_for_children via /proc.
+ */
+ f = fopen("/proc/self/stat", "r");
+ ASSERT_NE(f, NULL);
+ ASSERT_EQ(fscanf(f, "%d", &parent_pid), 1);
+ ASSERT_EQ(0, pipe(pipe_fd));
+
+ child = fork();
+ ASSERT_GE(child, 0);
+
+ if (child == 0)
+ _exit(pidns_init_via_setns_set_tid_child(_metadata, parent_pid, pipe_fd));
+
+ close(pipe_fd[0]);
+
+ ASSERT_EQ(0, unshare(CLONE_NEWUSER));
+ ASSERT_EQ(0, unshare(CLONE_NEWPID));
+
+ buf = 0;
+ ASSERT_EQ(1, write_nointr(pipe_fd[1], &buf, 1));
+ close(pipe_fd[1]);
+
+ ASSERT_EQ(0, wait_for_pid(child));
+
+ fclose(f);
+ return 0;
+}
+
+TEST(pidns_init_via_setns_set_tid)
+{
+ pid_t wrapper;
+
+ if (geteuid())
+ SKIP(return, "This test needs root to run!");
+
+ ASSERT_EQ(0, unshare(CLONE_NEWPID));
+
+ wrapper = fork();
+ ASSERT_GE(wrapper, 0);
+
+ if (wrapper == 0)
+ _exit(pidns_init_via_setns_set_tid_wrapper(_metadata));
+
+ ASSERT_EQ(0, wait_for_pid(wrapper));
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/pid_namespace/regression_enomem.c b/tools/testing/selftests/pid_namespace/regression_enomem.c
new file mode 100644
index 000000000..059e7ec5b
--- /dev/null
+++ b/tools/testing/selftests/pid_namespace/regression_enomem.c
@@ -0,0 +1,44 @@
+#define _GNU_SOURCE
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/types.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syscall.h>
+#include <sys/wait.h>
+
+#include "kselftest_harness.h"
+#include "../pidfd/pidfd.h"
+
+/*
+ * Regression test for:
+ * 35f71bc0a09a ("fork: report pid reservation failure properly")
+ * b26ebfe12f34 ("pid: Fix error return value in some cases")
+ */
+TEST(regression_enomem)
+{
+ pid_t pid;
+
+ if (geteuid())
+ EXPECT_EQ(0, unshare(CLONE_NEWUSER));
+
+ EXPECT_EQ(0, unshare(CLONE_NEWPID));
+
+ pid = fork();
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0)
+ exit(EXIT_SUCCESS);
+
+ EXPECT_EQ(0, wait_for_pid(pid));
+
+ pid = fork();
+ ASSERT_LT(pid, 0);
+ ASSERT_EQ(errno, ENOMEM);
+}
+
+TEST_HARNESS_MAIN