1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/types.h>
15 #include "../kselftest_harness.h"
16 #include "../pidfd/pidfd.h"
18 #define __STACK_SIZE (8 * 1024 * 1024)
19 static pid_t do_clone(int (*fn)(void *), void *arg, int flags)
24 stack = malloc(__STACK_SIZE);
29 ret = __clone2(fn, stack, __STACK_SIZE, flags | SIGCHLD, arg);
31 ret = clone(fn, stack + __STACK_SIZE, flags | SIGCHLD, arg);
37 static int pid_max_cb(void *data)
42 ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
44 fprintf(stderr, "%m - Failed to make rootfs private mount\n");
48 umount2("/proc", MNT_DETACH);
50 ret = mount("proc", "/proc", "proc", 0, NULL);
52 fprintf(stderr, "%m - Failed to mount proc\n");
56 fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
58 fprintf(stderr, "%m - Failed to open pid_max\n");
62 ret = write(fd, "500", sizeof("500") - 1);
64 fprintf(stderr, "%m - Failed to write pid_max\n");
68 for (int i = 0; i < 501; i++) {
74 fprintf(stderr, "Managed to create pid number beyond limit\n");
82 static int pid_max_nested_inner(void *data)
88 ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
90 fprintf(stderr, "%m - Failed to make rootfs private mount\n");
94 umount2("/proc", MNT_DETACH);
96 ret = mount("proc", "/proc", "proc", 0, NULL);
98 fprintf(stderr, "%m - Failed to mount proc\n");
102 fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
104 fprintf(stderr, "%m - Failed to open pid_max\n");
108 ret = write(fd, "500", sizeof("500") - 1);
111 fprintf(stderr, "%m - Failed to write pid_max\n");
117 fprintf(stderr, "Failed to create first new process\n");
125 wait_for_pid(pids[0]);
129 wait_for_pid(pids[1]);
131 fprintf(stderr, "Managed to create process even though ancestor pid namespace had a limit\n");
135 /* Now make sure that we wrap pids at 400. */
136 for (i = 0; i < 510; i++) {
148 fprintf(stderr, "Managed to create process with pid %d beyond configured limit\n", pid);
156 static int pid_max_nested_outer(void *data)
158 int fret = -1, nr_procs = 400;
163 ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
165 fprintf(stderr, "%m - Failed to make rootfs private mount\n");
169 umount2("/proc", MNT_DETACH);
171 ret = mount("proc", "/proc", "proc", 0, NULL);
173 fprintf(stderr, "%m - Failed to mount proc\n");
177 fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
179 fprintf(stderr, "%m - Failed to open pid_max\n");
183 ret = write(fd, "400", sizeof("400") - 1);
186 fprintf(stderr, "%m - Failed to write pid_max\n");
191 * Create 397 processes. This leaves room for do_clone() (398) and
192 * one more 399. So creating another process needs to fail.
194 for (nr_procs = 0; nr_procs < 396; nr_procs++) {
202 pids[nr_procs] = pid;
205 pid = do_clone(pid_max_nested_inner, NULL, CLONE_NEWPID | CLONE_NEWNS);
207 fprintf(stderr, "%m - Failed to clone nested pidns\n");
211 if (wait_for_pid(pid)) {
212 fprintf(stderr, "%m - Nested pid_max failed\n");
219 for (int i = 0; i < nr_procs; i++)
220 wait_for_pid(pids[i]);
225 static int pid_max_nested_limit_inner(void *data)
227 int fret = -1, nr_procs = 400;
232 ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
234 fprintf(stderr, "%m - Failed to make rootfs private mount\n");
238 umount2("/proc", MNT_DETACH);
240 ret = mount("proc", "/proc", "proc", 0, NULL);
242 fprintf(stderr, "%m - Failed to mount proc\n");
246 fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
248 fprintf(stderr, "%m - Failed to open pid_max\n");
252 ret = write(fd, "500", sizeof("500") - 1);
255 fprintf(stderr, "%m - Failed to write pid_max\n");
259 for (nr_procs = 0; nr_procs < 500; nr_procs++) {
267 pids[nr_procs] = pid;
270 if (nr_procs >= 400) {
271 fprintf(stderr, "Managed to create processes beyond the configured outer limit\n");
278 for (int i = 0; i < nr_procs; i++)
279 wait_for_pid(pids[i]);
284 static int pid_max_nested_limit_outer(void *data)
289 ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
291 fprintf(stderr, "%m - Failed to make rootfs private mount\n");
295 umount2("/proc", MNT_DETACH);
297 ret = mount("proc", "/proc", "proc", 0, NULL);
299 fprintf(stderr, "%m - Failed to mount proc\n");
303 fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
305 fprintf(stderr, "%m - Failed to open pid_max\n");
309 ret = write(fd, "400", sizeof("400") - 1);
312 fprintf(stderr, "%m - Failed to write pid_max\n");
316 pid = do_clone(pid_max_nested_limit_inner, NULL, CLONE_NEWPID | CLONE_NEWNS);
318 fprintf(stderr, "%m - Failed to clone nested pidns\n");
322 if (wait_for_pid(pid)) {
323 fprintf(stderr, "%m - Nested pid_max failed\n");
335 pid = do_clone(pid_max_cb, NULL, CLONE_NEWPID | CLONE_NEWNS);
337 ASSERT_EQ(0, wait_for_pid(pid));
340 TEST(pid_max_nested_limit)
344 pid = do_clone(pid_max_nested_limit_outer, NULL, CLONE_NEWPID | CLONE_NEWNS);
346 ASSERT_EQ(0, wait_for_pid(pid));
353 pid = do_clone(pid_max_nested_outer, NULL, CLONE_NEWPID | CLONE_NEWNS);
355 ASSERT_EQ(0, wait_for_pid(pid));