1 // SPDX-License-Identifier: GPL-2.0
4 * Based on Christian Brauner's clone3() example.
5 * These tests are assuming to be running in the host's
11 #include <linux/types.h>
12 #include <linux/sched.h>
16 #include <sys/syscall.h>
17 #include <sys/types.h>
23 #include "../kselftest.h"
24 #include "clone3_selftests.h"
26 #define MAX_PID_NS_LEVEL 32
31 static void child_exit(int ret)
38 static int call_clone3_set_tid(pid_t *set_tid,
47 struct __clone_args args = {
49 .exit_signal = SIGCHLD,
50 .set_tid = ptr_to_u64(set_tid),
51 .set_tid_size = set_tid_size,
54 pid = sys_clone3(&args, sizeof(args));
56 ksft_print_msg("%s - Failed to create new process\n",
64 int exit_code = EXIT_SUCCESS;
66 ksft_print_msg("I am the child, my PID is %d (expected %d)\n",
67 getpid(), set_tid[0]);
69 ksft_print_msg("[%d] Child is ready and waiting\n",
72 /* Signal the parent that the child is ready */
74 ret = write(pipe_1[1], &tmp, 1);
77 "Writing to pipe returned %d", ret);
78 exit_code = EXIT_FAILURE;
82 ret = read(pipe_2[0], &tmp, 1);
85 "Reading from pipe returned %d", ret);
86 exit_code = EXIT_FAILURE;
91 if (set_tid[0] != getpid())
92 child_exit(EXIT_FAILURE);
93 child_exit(exit_code);
96 if (expected_pid == 0 || expected_pid == pid) {
97 ksft_print_msg("I am the parent (%d). My child's pid is %d\n",
101 "Expected child pid %d does not match actual pid %d\n",
106 if (waitpid(pid, &status, 0) < 0) {
107 ksft_print_msg("Child returned %s\n", strerror(errno));
111 if (!WIFEXITED(status))
114 return WEXITSTATUS(status);
117 static void test_clone3_set_tid(const char *desc,
128 "[%d] Trying clone3() with CLONE_SET_TID to %d and 0x%x\n",
129 getpid(), set_tid[0], flags);
130 ret = call_clone3_set_tid(set_tid, set_tid_size, flags, expected_pid,
133 "[%d] clone3() with CLONE_SET_TID %d says: %d - expected %d\n",
134 getpid(), set_tid[0], ret, expected);
136 ksft_test_result(ret == expected, "%s with %zu TIDs and flags 0x%x\n",
137 desc, set_tid_size, flags);
140 int main(int argc, char *argv[])
149 uid_t uid = getuid();
150 char proc_path[100] = {0};
151 pid_t pid, ns1, ns2, ns3, ns_pid;
152 pid_t set_tid[MAX_PID_NS_LEVEL * 2];
156 test_clone3_supported();
158 if (pipe(pipe_1) < 0 || pipe(pipe_2) < 0)
159 ksft_exit_fail_msg("pipe() failed\n");
161 f = fopen("/proc/sys/kernel/pid_max", "r");
164 "%s - Could not open /proc/sys/kernel/pid_max\n",
166 fscanf(f, "%d", &pid_max);
168 ksft_print_msg("/proc/sys/kernel/pid_max %d\n", pid_max);
170 /* Try invalid settings */
171 memset(&set_tid, 0, sizeof(set_tid));
172 test_clone3_set_tid("invalid size, 0 TID",
173 set_tid, MAX_PID_NS_LEVEL + 1, 0, -EINVAL, 0, 0);
175 test_clone3_set_tid("invalid size, 0 TID",
176 set_tid, MAX_PID_NS_LEVEL * 2, 0, -EINVAL, 0, 0);
178 test_clone3_set_tid("invalid size, 0 TID",
179 set_tid, MAX_PID_NS_LEVEL * 2 + 1, 0,
182 test_clone3_set_tid("invalid size, 0 TID",
183 set_tid, MAX_PID_NS_LEVEL * 42, 0, -EINVAL, 0, 0);
186 * This can actually work if this test running in a MAX_PID_NS_LEVEL - 1
187 * nested PID namespace.
189 test_clone3_set_tid("invalid size, 0 TID",
190 set_tid, MAX_PID_NS_LEVEL - 1, 0, -EINVAL, 0, 0);
192 memset(&set_tid, 0xff, sizeof(set_tid));
193 test_clone3_set_tid("invalid size, TID all 1s",
194 set_tid, MAX_PID_NS_LEVEL + 1, 0, -EINVAL, 0, 0);
196 test_clone3_set_tid("invalid size, TID all 1s",
197 set_tid, MAX_PID_NS_LEVEL * 2, 0, -EINVAL, 0, 0);
199 test_clone3_set_tid("invalid size, TID all 1s",
200 set_tid, MAX_PID_NS_LEVEL * 2 + 1, 0,
203 test_clone3_set_tid("invalid size, TID all 1s",
204 set_tid, MAX_PID_NS_LEVEL * 42, 0, -EINVAL, 0, 0);
207 * This can actually work if this test running in a MAX_PID_NS_LEVEL - 1
208 * nested PID namespace.
210 test_clone3_set_tid("invalid size, TID all 1s",
211 set_tid, MAX_PID_NS_LEVEL - 1, 0, -EINVAL, 0, 0);
213 memset(&set_tid, 0, sizeof(set_tid));
214 /* Try with an invalid PID */
216 test_clone3_set_tid("valid size, 0 TID",
217 set_tid, 1, 0, -EINVAL, 0, 0);
220 test_clone3_set_tid("valid size, -1 TID",
221 set_tid, 1, 0, -EINVAL, 0, 0);
223 /* Claim that the set_tid array actually contains 2 elements. */
224 test_clone3_set_tid("2 TIDs, -1 and 0",
225 set_tid, 2, 0, -EINVAL, 0, 0);
227 /* Try it in a new PID namespace */
229 test_clone3_set_tid("valid size, -1 TID",
230 set_tid, 1, CLONE_NEWPID, -EINVAL, 0, 0);
232 ksft_test_result_skip("Clone3() with set_tid requires root\n");
234 /* Try with a valid PID (1) this should return -EEXIST. */
237 test_clone3_set_tid("duplicate PID 1",
238 set_tid, 1, 0, -EEXIST, 0, 0);
240 ksft_test_result_skip("Clone3() with set_tid requires root\n");
242 /* Try it in a new PID namespace */
244 test_clone3_set_tid("duplicate PID 1",
245 set_tid, 1, CLONE_NEWPID, 0, 0, 0);
247 ksft_test_result_skip("Clone3() with set_tid requires root\n");
249 /* pid_max should fail everywhere */
250 set_tid[0] = pid_max;
251 test_clone3_set_tid("set TID to maximum",
252 set_tid, 1, 0, -EINVAL, 0, 0);
255 test_clone3_set_tid("set TID to maximum",
256 set_tid, 1, CLONE_NEWPID, -EINVAL, 0, 0);
258 ksft_test_result_skip("Clone3() with set_tid requires root\n");
262 * All remaining tests require root. Tell the framework
263 * that all those tests are skipped as non-root.
265 ksft_cnt.ksft_xskip += ksft_plan - ksft_test_num();
269 /* Find the current active PID */
272 ksft_print_msg("Child has PID %d\n", getpid());
273 child_exit(EXIT_SUCCESS);
275 if (waitpid(pid, &status, 0) < 0)
276 ksft_exit_fail_msg("Waiting for child %d failed", pid);
278 /* After the child has finished, its PID should be free. */
280 test_clone3_set_tid("reallocate child TID",
281 set_tid, 1, 0, 0, 0, 0);
283 /* This should fail as there is no PID 1 in that namespace */
284 test_clone3_set_tid("duplicate child TID",
285 set_tid, 1, CLONE_NEWPID, -EINVAL, 0, 0);
288 * Creating a process with PID 1 in the newly created most nested
289 * PID namespace and PID 'pid' in the parent PID namespace. This
294 test_clone3_set_tid("create PID 1 in new NS",
295 set_tid, 2, CLONE_NEWPID, 0, pid, 0);
297 ksft_print_msg("unshare PID namespace\n");
298 if (unshare(CLONE_NEWPID) == -1)
299 ksft_exit_fail_msg("unshare(CLONE_NEWPID) failed: %s\n",
304 /* This should fail as there is no PID 1 in that namespace */
305 test_clone3_set_tid("duplicate PID 1",
306 set_tid, 1, 0, -EINVAL, 0, 0);
308 /* Let's create a PID 1 */
312 * This and the next test cases check that all pid-s are
313 * released on error paths.
317 test_clone3_set_tid("check leak on invalid TID -1",
318 set_tid, 2, 0, -EINVAL, 0, 0);
322 test_clone3_set_tid("check leak on invalid specific TID",
323 set_tid, 2, 0, 0, 43, 0);
325 ksft_print_msg("Child in PID namespace has PID %d\n", getpid());
327 test_clone3_set_tid("create PID 2 in child NS",
328 set_tid, 1, 0, 0, 2, 0);
333 /* This should fail as there is invalid PID at level '1'. */
334 test_clone3_set_tid("fail due to invalid TID at level 1",
335 set_tid, 3, CLONE_NEWPID, -EINVAL, 0, 0);
341 * This should fail as there are not enough active PID
342 * namespaces. Again assuming this is running in the host's
343 * PID namespace. Not yet nested.
345 test_clone3_set_tid("fail due to too few active PID NSs",
346 set_tid, 4, CLONE_NEWPID, -EINVAL, 0, 0);
349 * This should work and from the parent we should see
350 * something like 'NSpid: pid 42 1'.
352 test_clone3_set_tid("verify that we have 3 PID NSs",
353 set_tid, 3, CLONE_NEWPID, 0, 42, true);
355 child_exit(ksft_cnt.ksft_fail);
360 while (read(pipe_1[0], &buf, 1) > 0) {
361 ksft_print_msg("[%d] Child is ready and waiting\n", getpid());
365 snprintf(proc_path, sizeof(proc_path), "/proc/%d/status", pid);
366 f = fopen(proc_path, "r");
369 "%s - Could not open %s\n",
370 strerror(errno), proc_path);
372 while (getline(&line, &len, f) != -1) {
373 if (strstr(line, "NSpid")) {
376 /* Verify that all generated PIDs are as expected. */
377 i = sscanf(line, "NSpid:\t%d\t%d\t%d",
381 "Unexpected 'NSPid:' entry: %s",
392 /* Tell the clone3()'d child to finish. */
393 write(pipe_2[1], &buf, 1);
396 if (waitpid(ns_pid, &status, 0) < 0) {
397 ksft_print_msg("Child returned %s\n", strerror(errno));
402 if (!WIFEXITED(status))
403 ksft_test_result_fail("Child error\n");
405 ksft_cnt.ksft_pass += 6 - (ksft_cnt.ksft_fail - WEXITSTATUS(status));
406 ksft_cnt.ksft_fail = WEXITSTATUS(status);
408 ksft_print_msg("Expecting PIDs %d, 42, 1\n", pid);
409 ksft_print_msg("Have PIDs in namespaces: %d, %d, %d\n", ns3, ns2, ns1);
410 ksft_test_result(ns3 == pid && ns2 == 42 && ns1 == 1,
411 "PIDs in all namespaces as expected\n");