1 // SPDX-License-Identifier: GPL-2.0
3 /* Based on Christian Brauner's clone3() example */
8 #include <linux/types.h>
9 #include <linux/sched.h>
14 #include <sys/syscall.h>
15 #include <sys/types.h>
21 #include "../kselftest.h"
22 #include "clone3_selftests.h"
27 CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG,
28 CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG,
29 CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG,
30 CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG,
33 static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
35 struct __clone_args args = {
37 .exit_signal = SIGCHLD,
40 struct clone_args_extended {
41 struct __clone_args args;
42 __aligned_u64 excess_space[2];
48 memset(&args_ext, 0, sizeof(args_ext));
49 if (size > sizeof(struct __clone_args))
50 args_ext.excess_space[1] = 1;
53 size = sizeof(struct __clone_args);
56 case CLONE3_ARGS_NO_TEST:
58 * Uses default 'flags' and 'SIGCHLD'
62 case CLONE3_ARGS_ALL_0:
66 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG:
67 args.exit_signal = 0xbadc0ded00000000ULL;
69 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG:
70 args.exit_signal = 0x0000000080000000ULL;
72 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG:
73 args.exit_signal = 0x0000000000000100ULL;
75 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG:
76 args.exit_signal = 0x00000000000000f0ULL;
80 memcpy(&args_ext.args, &args, sizeof(struct __clone_args));
82 pid = sys_clone3((struct __clone_args *)&args_ext, size);
84 ksft_print_msg("%s - Failed to create new process\n",
90 ksft_print_msg("I am the child, my PID is %d\n", getpid());
94 ksft_print_msg("I am the parent (%d). My child's pid is %d\n",
97 if (waitpid(-1, &status, __WALL) < 0) {
98 ksft_print_msg("waitpid() returned %s\n", strerror(errno));
101 if (!WIFEXITED(status)) {
102 ksft_print_msg("Child did not exit normally, status 0x%x\n",
106 if (WEXITSTATUS(status))
107 return WEXITSTATUS(status);
112 static bool test_clone3(uint64_t flags, size_t size, int expected,
113 enum test_mode test_mode)
118 "[%d] Trying clone3() with flags %#" PRIx64 " (size %zu)\n",
119 getpid(), flags, size);
120 ret = call_clone3(flags, size, test_mode);
121 ksft_print_msg("[%d] clone3() with flags says: %d expected %d\n",
122 getpid(), ret, expected);
123 if (ret != expected) {
125 "[%d] Result (%d) is different than expected (%d)\n",
126 getpid(), ret, expected);
133 typedef bool (*filter_function)(void);
134 typedef size_t (*size_function)(void);
136 static bool not_root(void)
139 ksft_print_msg("Not running as root\n");
146 static bool no_timenamespace(void)
151 if (!access("/proc/self/ns/time", F_OK))
154 ksft_print_msg("Time namespaces are not supported\n");
158 static size_t page_size_plus_8(void)
160 return getpagesize() + 8;
167 size_function size_function;
169 enum test_mode test_mode;
170 filter_function filter;
173 static const struct test tests[] = {
175 .name = "simple clone3()",
179 .test_mode = CLONE3_ARGS_NO_TEST,
182 .name = "clone3() in a new PID_NS",
183 .flags = CLONE_NEWPID,
186 .test_mode = CLONE3_ARGS_NO_TEST,
190 .name = "CLONE_ARGS_SIZE_VER0",
192 .size = CLONE_ARGS_SIZE_VER0,
194 .test_mode = CLONE3_ARGS_NO_TEST,
197 .name = "CLONE_ARGS_SIZE_VER0 - 8",
199 .size = CLONE_ARGS_SIZE_VER0 - 8,
201 .test_mode = CLONE3_ARGS_NO_TEST,
204 .name = "sizeof(struct clone_args) + 8",
206 .size = sizeof(struct __clone_args) + 8,
208 .test_mode = CLONE3_ARGS_NO_TEST,
211 .name = "exit_signal with highest 32 bits non-zero",
215 .test_mode = CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG,
218 .name = "negative 32-bit exit_signal",
222 .test_mode = CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG,
225 .name = "exit_signal not fitting into CSIGNAL mask",
229 .test_mode = CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG,
232 .name = "NSIG < exit_signal < CSIG",
236 .test_mode = CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG,
239 .name = "Arguments sizeof(struct clone_args) + 8",
241 .size = sizeof(struct __clone_args) + 8,
243 .test_mode = CLONE3_ARGS_ALL_0,
246 .name = "Arguments sizeof(struct clone_args) + 16",
248 .size = sizeof(struct __clone_args) + 16,
250 .test_mode = CLONE3_ARGS_ALL_0,
253 .name = "Arguments sizeof(struct clone_arg) * 2",
255 .size = sizeof(struct __clone_args) + 16,
257 .test_mode = CLONE3_ARGS_ALL_0,
260 .name = "Arguments > page size",
262 .size_function = page_size_plus_8,
264 .test_mode = CLONE3_ARGS_NO_TEST,
267 .name = "CLONE_ARGS_SIZE_VER0 in a new PID NS",
268 .flags = CLONE_NEWPID,
269 .size = CLONE_ARGS_SIZE_VER0,
271 .test_mode = CLONE3_ARGS_NO_TEST,
275 .name = "CLONE_ARGS_SIZE_VER0 - 8 in a new PID NS",
276 .flags = CLONE_NEWPID,
277 .size = CLONE_ARGS_SIZE_VER0 - 8,
279 .test_mode = CLONE3_ARGS_NO_TEST,
282 .name = "sizeof(struct clone_args) + 8 in a new PID NS",
283 .flags = CLONE_NEWPID,
284 .size = sizeof(struct __clone_args) + 8,
286 .test_mode = CLONE3_ARGS_NO_TEST,
290 .name = "Arguments > page size in a new PID NS",
291 .flags = CLONE_NEWPID,
292 .size_function = page_size_plus_8,
294 .test_mode = CLONE3_ARGS_NO_TEST,
297 .name = "New time NS",
298 .flags = CLONE_NEWTIME,
301 .test_mode = CLONE3_ARGS_NO_TEST,
302 .filter = no_timenamespace,
305 .name = "exit signal (SIGCHLD) in flags",
309 .test_mode = CLONE3_ARGS_NO_TEST,
313 int main(int argc, char *argv[])
319 ksft_set_plan(ARRAY_SIZE(tests));
320 test_clone3_supported();
322 for (i = 0; i < ARRAY_SIZE(tests); i++) {
323 if (tests[i].filter && tests[i].filter()) {
324 ksft_test_result_skip("%s\n", tests[i].name);
328 if (tests[i].size_function)
329 size = tests[i].size_function();
331 size = tests[i].size;
333 ksft_print_msg("Running test '%s'\n", tests[i].name);
335 ksft_test_result(test_clone3(tests[i].flags, size,
338 "%s\n", tests[i].name);