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>
13 #include <sys/syscall.h>
14 #include <sys/types.h>
20 #include "../kselftest.h"
21 #include "clone3_selftests.h"
26 CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG,
27 CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG,
28 CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG,
29 CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG,
32 static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
34 struct __clone_args args = {
36 .exit_signal = SIGCHLD,
39 struct clone_args_extended {
40 struct __clone_args args;
41 __aligned_u64 excess_space[2];
47 memset(&args_ext, 0, sizeof(args_ext));
48 if (size > sizeof(struct __clone_args))
49 args_ext.excess_space[1] = 1;
52 size = sizeof(struct __clone_args);
55 case CLONE3_ARGS_NO_TEST:
57 * Uses default 'flags' and 'SIGCHLD'
61 case CLONE3_ARGS_ALL_0:
65 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG:
66 args.exit_signal = 0xbadc0ded00000000ULL;
68 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG:
69 args.exit_signal = 0x0000000080000000ULL;
71 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG:
72 args.exit_signal = 0x0000000000000100ULL;
74 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG:
75 args.exit_signal = 0x00000000000000f0ULL;
79 memcpy(&args_ext.args, &args, sizeof(struct __clone_args));
81 pid = sys_clone3((struct __clone_args *)&args_ext, size);
83 ksft_print_msg("%s - Failed to create new process\n",
89 ksft_print_msg("I am the child, my PID is %d\n", getpid());
93 ksft_print_msg("I am the parent (%d). My child's pid is %d\n",
96 if (waitpid(-1, &status, __WALL) < 0) {
97 ksft_print_msg("Child returned %s\n", strerror(errno));
100 if (WEXITSTATUS(status))
101 return WEXITSTATUS(status);
106 static void test_clone3(uint64_t flags, size_t size, int expected,
107 enum test_mode test_mode)
112 "[%d] Trying clone3() with flags %#" PRIx64 " (size %zu)\n",
113 getpid(), flags, size);
114 ret = call_clone3(flags, size, test_mode);
115 ksft_print_msg("[%d] clone3() with flags says: %d expected %d\n",
116 getpid(), ret, expected);
118 ksft_test_result_fail(
119 "[%d] Result (%d) is different than expected (%d)\n",
120 getpid(), ret, expected);
122 ksft_test_result_pass(
123 "[%d] Result (%d) matches expectation (%d)\n",
124 getpid(), ret, expected);
127 int main(int argc, char *argv[])
129 uid_t uid = getuid();
133 test_clone3_supported();
135 /* Just a simple clone3() should return 0.*/
136 test_clone3(0, 0, 0, CLONE3_ARGS_NO_TEST);
138 /* Do a clone3() in a new PID NS.*/
140 test_clone3(CLONE_NEWPID, 0, 0, CLONE3_ARGS_NO_TEST);
142 ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
144 /* Do a clone3() with CLONE_ARGS_SIZE_VER0. */
145 test_clone3(0, CLONE_ARGS_SIZE_VER0, 0, CLONE3_ARGS_NO_TEST);
147 /* Do a clone3() with CLONE_ARGS_SIZE_VER0 - 8 */
148 test_clone3(0, CLONE_ARGS_SIZE_VER0 - 8, -EINVAL, CLONE3_ARGS_NO_TEST);
150 /* Do a clone3() with sizeof(struct clone_args) + 8 */
151 test_clone3(0, sizeof(struct __clone_args) + 8, 0, CLONE3_ARGS_NO_TEST);
153 /* Do a clone3() with exit_signal having highest 32 bits non-zero */
154 test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG);
156 /* Do a clone3() with negative 32-bit exit_signal */
157 test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG);
159 /* Do a clone3() with exit_signal not fitting into CSIGNAL mask */
160 test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG);
162 /* Do a clone3() with NSIG < exit_signal < CSIG */
163 test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG);
165 test_clone3(0, sizeof(struct __clone_args) + 8, 0, CLONE3_ARGS_ALL_0);
167 test_clone3(0, sizeof(struct __clone_args) + 16, -E2BIG,
170 test_clone3(0, sizeof(struct __clone_args) * 2, -E2BIG,
173 /* Do a clone3() with > page size */
174 test_clone3(0, getpagesize() + 8, -E2BIG, CLONE3_ARGS_NO_TEST);
176 /* Do a clone3() with CLONE_ARGS_SIZE_VER0 in a new PID NS. */
178 test_clone3(CLONE_NEWPID, CLONE_ARGS_SIZE_VER0, 0,
179 CLONE3_ARGS_NO_TEST);
181 ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
183 /* Do a clone3() with CLONE_ARGS_SIZE_VER0 - 8 in a new PID NS */
184 test_clone3(CLONE_NEWPID, CLONE_ARGS_SIZE_VER0 - 8, -EINVAL,
185 CLONE3_ARGS_NO_TEST);
187 /* Do a clone3() with sizeof(struct clone_args) + 8 in a new PID NS */
189 test_clone3(CLONE_NEWPID, sizeof(struct __clone_args) + 8, 0,
190 CLONE3_ARGS_NO_TEST);
192 ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
194 /* Do a clone3() with > page size in a new PID NS */
195 test_clone3(CLONE_NEWPID, getpagesize() + 8, -E2BIG,
196 CLONE3_ARGS_NO_TEST);
198 return !ksft_get_fail_cnt() ? ksft_exit_pass() : ksft_exit_fail();