1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Based on Christian Brauner's clone3() example */ 4 5 #define _GNU_SOURCE 6 #include <errno.h> 7 #include <inttypes.h> 8 #include <linux/types.h> 9 #include <linux/sched.h> 10 #include <stdint.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <sys/syscall.h> 14 #include <sys/types.h> 15 #include <sys/un.h> 16 #include <sys/wait.h> 17 #include <unistd.h> 18 #include <sched.h> 19 20 #include "../kselftest.h" 21 #include "clone3_selftests.h" 22 23 /* 24 * Different sizes of struct clone_args 25 */ 26 #ifndef CLONE3_ARGS_SIZE_V0 27 #define CLONE3_ARGS_SIZE_V0 64 28 #endif 29 30 enum test_mode { 31 CLONE3_ARGS_NO_TEST, 32 CLONE3_ARGS_ALL_0, 33 CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG, 34 CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG, 35 CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG, 36 CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG, 37 }; 38 39 static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) 40 { 41 struct clone_args args = { 42 .flags = flags, 43 .exit_signal = SIGCHLD, 44 }; 45 46 struct clone_args_extended { 47 struct clone_args args; 48 __aligned_u64 excess_space[2]; 49 } args_ext; 50 51 pid_t pid = -1; 52 int status; 53 54 memset(&args_ext, 0, sizeof(args_ext)); 55 if (size > sizeof(struct clone_args)) 56 args_ext.excess_space[1] = 1; 57 58 if (size == 0) 59 size = sizeof(struct clone_args); 60 61 switch (test_mode) { 62 case CLONE3_ARGS_ALL_0: 63 args.flags = 0; 64 args.exit_signal = 0; 65 break; 66 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG: 67 args.exit_signal = 0xbadc0ded00000000ULL; 68 break; 69 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG: 70 args.exit_signal = 0x0000000080000000ULL; 71 break; 72 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG: 73 args.exit_signal = 0x0000000000000100ULL; 74 break; 75 case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG: 76 args.exit_signal = 0x00000000000000f0ULL; 77 break; 78 } 79 80 memcpy(&args_ext.args, &args, sizeof(struct clone_args)); 81 82 pid = sys_clone3((struct clone_args *)&args_ext, size); 83 if (pid < 0) { 84 ksft_print_msg("%s - Failed to create new process\n", 85 strerror(errno)); 86 return -errno; 87 } 88 89 if (pid == 0) { 90 ksft_print_msg("I am the child, my PID is %d\n", getpid()); 91 _exit(EXIT_SUCCESS); 92 } 93 94 ksft_print_msg("I am the parent (%d). My child's pid is %d\n", 95 getpid(), pid); 96 97 if (waitpid(-1, &status, __WALL) < 0) { 98 ksft_print_msg("Child returned %s\n", strerror(errno)); 99 return -errno; 100 } 101 if (WEXITSTATUS(status)) 102 return WEXITSTATUS(status); 103 104 return 0; 105 } 106 107 static void test_clone3(uint64_t flags, size_t size, int expected, 108 enum test_mode test_mode) 109 { 110 int ret; 111 112 ksft_print_msg( 113 "[%d] Trying clone3() with flags %#" PRIx64 " (size %zu)\n", 114 getpid(), flags, size); 115 ret = call_clone3(flags, size, test_mode); 116 ksft_print_msg("[%d] clone3() with flags says: %d expected %d\n", 117 getpid(), ret, expected); 118 if (ret != expected) 119 ksft_test_result_fail( 120 "[%d] Result (%d) is different than expected (%d)\n", 121 getpid(), ret, expected); 122 else 123 ksft_test_result_pass( 124 "[%d] Result (%d) matches expectation (%d)\n", 125 getpid(), ret, expected); 126 } 127 128 int main(int argc, char *argv[]) 129 { 130 pid_t pid; 131 132 uid_t uid = getuid(); 133 134 ksft_print_header(); 135 ksft_set_plan(17); 136 test_clone3_supported(); 137 138 /* Just a simple clone3() should return 0.*/ 139 test_clone3(0, 0, 0, CLONE3_ARGS_NO_TEST); 140 141 /* Do a clone3() in a new PID NS.*/ 142 if (uid == 0) 143 test_clone3(CLONE_NEWPID, 0, 0, CLONE3_ARGS_NO_TEST); 144 else 145 ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n"); 146 147 /* Do a clone3() with CLONE3_ARGS_SIZE_V0. */ 148 test_clone3(0, CLONE3_ARGS_SIZE_V0, 0, CLONE3_ARGS_NO_TEST); 149 150 /* Do a clone3() with CLONE3_ARGS_SIZE_V0 - 8 */ 151 test_clone3(0, CLONE3_ARGS_SIZE_V0 - 8, -EINVAL, CLONE3_ARGS_NO_TEST); 152 153 /* Do a clone3() with sizeof(struct clone_args) + 8 */ 154 test_clone3(0, sizeof(struct clone_args) + 8, 0, CLONE3_ARGS_NO_TEST); 155 156 /* Do a clone3() with exit_signal having highest 32 bits non-zero */ 157 test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG); 158 159 /* Do a clone3() with negative 32-bit exit_signal */ 160 test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG); 161 162 /* Do a clone3() with exit_signal not fitting into CSIGNAL mask */ 163 test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG); 164 165 /* Do a clone3() with NSIG < exit_signal < CSIG */ 166 test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG); 167 168 test_clone3(0, sizeof(struct clone_args) + 8, 0, CLONE3_ARGS_ALL_0); 169 170 test_clone3(0, sizeof(struct clone_args) + 16, -E2BIG, 171 CLONE3_ARGS_ALL_0); 172 173 test_clone3(0, sizeof(struct clone_args) * 2, -E2BIG, 174 CLONE3_ARGS_ALL_0); 175 176 /* Do a clone3() with > page size */ 177 test_clone3(0, getpagesize() + 8, -E2BIG, CLONE3_ARGS_NO_TEST); 178 179 /* Do a clone3() with CLONE3_ARGS_SIZE_V0 in a new PID NS. */ 180 if (uid == 0) 181 test_clone3(CLONE_NEWPID, CLONE3_ARGS_SIZE_V0, 0, 182 CLONE3_ARGS_NO_TEST); 183 else 184 ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n"); 185 186 /* Do a clone3() with CLONE3_ARGS_SIZE_V0 - 8 in a new PID NS */ 187 test_clone3(CLONE_NEWPID, CLONE3_ARGS_SIZE_V0 - 8, -EINVAL, 188 CLONE3_ARGS_NO_TEST); 189 190 /* Do a clone3() with sizeof(struct clone_args) + 8 in a new PID NS */ 191 if (uid == 0) 192 test_clone3(CLONE_NEWPID, sizeof(struct clone_args) + 8, 0, 193 CLONE3_ARGS_NO_TEST); 194 else 195 ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n"); 196 197 /* Do a clone3() with > page size in a new PID NS */ 198 test_clone3(CLONE_NEWPID, getpagesize() + 8, -E2BIG, 199 CLONE3_ARGS_NO_TEST); 200 201 return !ksft_get_fail_cnt() ? ksft_exit_pass() : ksft_exit_fail(); 202 } 203