Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/clone3: Avoid OS-defined clone_args

As the UAPI headers start to appear in distros, we need to avoid
outdated versions of struct clone_args to be able to test modern
features, named "struct __clone_args". Additionally update the struct
size macro names to match UAPI names.

Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/lkml/20200921075432.u4gis3s2o5qrsb5g@wittgenstein/
Signed-off-by: Kees Cook <keescook@chromium.org>

+41 -44
+19 -26
tools/testing/selftests/clone3/clone3.c
··· 20 20 #include "../kselftest.h" 21 21 #include "clone3_selftests.h" 22 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 23 enum test_mode { 31 24 CLONE3_ARGS_NO_TEST, 32 25 CLONE3_ARGS_ALL_0, ··· 31 38 32 39 static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) 33 40 { 34 - struct clone_args args = { 41 + struct __clone_args args = { 35 42 .flags = flags, 36 43 .exit_signal = SIGCHLD, 37 44 }; 38 45 39 46 struct clone_args_extended { 40 - struct clone_args args; 47 + struct __clone_args args; 41 48 __aligned_u64 excess_space[2]; 42 49 } args_ext; 43 50 ··· 45 52 int status; 46 53 47 54 memset(&args_ext, 0, sizeof(args_ext)); 48 - if (size > sizeof(struct clone_args)) 55 + if (size > sizeof(struct __clone_args)) 49 56 args_ext.excess_space[1] = 1; 50 57 51 58 if (size == 0) 52 - size = sizeof(struct clone_args); 59 + size = sizeof(struct __clone_args); 53 60 54 61 switch (test_mode) { 55 62 case CLONE3_ARGS_ALL_0: ··· 70 77 break; 71 78 } 72 79 73 - memcpy(&args_ext.args, &args, sizeof(struct clone_args)); 80 + memcpy(&args_ext.args, &args, sizeof(struct __clone_args)); 74 81 75 - pid = sys_clone3((struct clone_args *)&args_ext, size); 82 + pid = sys_clone3((struct __clone_args *)&args_ext, size); 76 83 if (pid < 0) { 77 84 ksft_print_msg("%s - Failed to create new process\n", 78 85 strerror(errno)); ··· 137 144 else 138 145 ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n"); 139 146 140 - /* Do a clone3() with CLONE3_ARGS_SIZE_V0. */ 141 - test_clone3(0, CLONE3_ARGS_SIZE_V0, 0, CLONE3_ARGS_NO_TEST); 147 + /* Do a clone3() with CLONE_ARGS_SIZE_VER0. */ 148 + test_clone3(0, CLONE_ARGS_SIZE_VER0, 0, CLONE3_ARGS_NO_TEST); 142 149 143 - /* Do a clone3() with CLONE3_ARGS_SIZE_V0 - 8 */ 144 - test_clone3(0, CLONE3_ARGS_SIZE_V0 - 8, -EINVAL, CLONE3_ARGS_NO_TEST); 150 + /* Do a clone3() with CLONE_ARGS_SIZE_VER0 - 8 */ 151 + test_clone3(0, CLONE_ARGS_SIZE_VER0 - 8, -EINVAL, CLONE3_ARGS_NO_TEST); 145 152 146 153 /* Do a clone3() with sizeof(struct clone_args) + 8 */ 147 - test_clone3(0, sizeof(struct clone_args) + 8, 0, CLONE3_ARGS_NO_TEST); 154 + test_clone3(0, sizeof(struct __clone_args) + 8, 0, CLONE3_ARGS_NO_TEST); 148 155 149 156 /* Do a clone3() with exit_signal having highest 32 bits non-zero */ 150 157 test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG); ··· 158 165 /* Do a clone3() with NSIG < exit_signal < CSIG */ 159 166 test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG); 160 167 161 - test_clone3(0, sizeof(struct clone_args) + 8, 0, CLONE3_ARGS_ALL_0); 168 + test_clone3(0, sizeof(struct __clone_args) + 8, 0, CLONE3_ARGS_ALL_0); 162 169 163 - test_clone3(0, sizeof(struct clone_args) + 16, -E2BIG, 170 + test_clone3(0, sizeof(struct __clone_args) + 16, -E2BIG, 164 171 CLONE3_ARGS_ALL_0); 165 172 166 - test_clone3(0, sizeof(struct clone_args) * 2, -E2BIG, 173 + test_clone3(0, sizeof(struct __clone_args) * 2, -E2BIG, 167 174 CLONE3_ARGS_ALL_0); 168 175 169 176 /* Do a clone3() with > page size */ 170 177 test_clone3(0, getpagesize() + 8, -E2BIG, CLONE3_ARGS_NO_TEST); 171 178 172 - /* Do a clone3() with CLONE3_ARGS_SIZE_V0 in a new PID NS. */ 179 + /* Do a clone3() with CLONE_ARGS_SIZE_VER0 in a new PID NS. */ 173 180 if (uid == 0) 174 - test_clone3(CLONE_NEWPID, CLONE3_ARGS_SIZE_V0, 0, 181 + test_clone3(CLONE_NEWPID, CLONE_ARGS_SIZE_VER0, 0, 175 182 CLONE3_ARGS_NO_TEST); 176 183 else 177 184 ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n"); 178 185 179 - /* Do a clone3() with CLONE3_ARGS_SIZE_V0 - 8 in a new PID NS */ 180 - test_clone3(CLONE_NEWPID, CLONE3_ARGS_SIZE_V0 - 8, -EINVAL, 186 + /* Do a clone3() with CLONE_ARGS_SIZE_VER0 - 8 in a new PID NS */ 187 + test_clone3(CLONE_NEWPID, CLONE_ARGS_SIZE_VER0 - 8, -EINVAL, 181 188 CLONE3_ARGS_NO_TEST); 182 189 183 190 /* Do a clone3() with sizeof(struct clone_args) + 8 in a new PID NS */ 184 191 if (uid == 0) 185 - test_clone3(CLONE_NEWPID, sizeof(struct clone_args) + 8, 0, 192 + test_clone3(CLONE_NEWPID, sizeof(struct __clone_args) + 8, 0, 186 193 CLONE3_ARGS_NO_TEST); 187 194 else 188 195 ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
+2 -2
tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c
··· 44 44 int status; 45 45 pid_t pid = -1; 46 46 47 - struct clone_args args = { 47 + struct __clone_args args = { 48 48 .exit_signal = SIGCHLD, 49 49 .set_tid = ptr_to_u64(set_tid), 50 50 .set_tid_size = set_tid_size, 51 51 }; 52 52 53 - pid = sys_clone3(&args, sizeof(struct clone_args)); 53 + pid = sys_clone3(&args, sizeof(args)); 54 54 if (pid < 0) { 55 55 TH_LOG("%s - Failed to create new process", strerror(errno)); 56 56 return -errno;
+1 -1
tools/testing/selftests/clone3/clone3_clear_sighand.c
··· 47 47 { 48 48 int ret; 49 49 pid_t pid; 50 - struct clone_args args = {}; 50 + struct __clone_args args = {}; 51 51 struct sigaction act; 52 52 53 53 /*
+14 -10
tools/testing/selftests/clone3/clone3_selftests.h
··· 19 19 #define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */ 20 20 #endif 21 21 22 - #ifndef CLONE_ARGS_SIZE_VER0 23 - #define CLONE_ARGS_SIZE_VER0 64 24 - #endif 25 - 26 22 #ifndef __NR_clone3 27 23 #define __NR_clone3 -1 28 - struct clone_args { 24 + #endif 25 + 26 + struct __clone_args { 29 27 __aligned_u64 flags; 30 28 __aligned_u64 pidfd; 31 29 __aligned_u64 child_tid; ··· 32 34 __aligned_u64 stack; 33 35 __aligned_u64 stack_size; 34 36 __aligned_u64 tls; 35 - #define CLONE_ARGS_SIZE_VER1 80 37 + #ifndef CLONE_ARGS_SIZE_VER0 38 + #define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */ 39 + #endif 36 40 __aligned_u64 set_tid; 37 41 __aligned_u64 set_tid_size; 38 - #define CLONE_ARGS_SIZE_VER2 88 42 + #ifndef CLONE_ARGS_SIZE_VER1 43 + #define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */ 44 + #endif 39 45 __aligned_u64 cgroup; 46 + #ifndef CLONE_ARGS_SIZE_VER2 47 + #define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */ 48 + #endif 40 49 }; 41 - #endif /* __NR_clone3 */ 42 50 43 - static pid_t sys_clone3(struct clone_args *args, size_t size) 51 + static pid_t sys_clone3(struct __clone_args *args, size_t size) 44 52 { 45 53 fflush(stdout); 46 54 fflush(stderr); ··· 56 52 static inline void test_clone3_supported(void) 57 53 { 58 54 pid_t pid; 59 - struct clone_args args = {}; 55 + struct __clone_args args = {}; 60 56 61 57 if (__NR_clone3 < 0) 62 58 ksft_exit_skip("clone3() syscall is not supported\n");
+2 -2
tools/testing/selftests/clone3/clone3_set_tid.c
··· 46 46 int status; 47 47 pid_t pid = -1; 48 48 49 - struct clone_args args = { 49 + struct __clone_args args = { 50 50 .flags = flags, 51 51 .exit_signal = SIGCHLD, 52 52 .set_tid = ptr_to_u64(set_tid), 53 53 .set_tid_size = set_tid_size, 54 54 }; 55 55 56 - pid = sys_clone3(&args, sizeof(struct clone_args)); 56 + pid = sys_clone3(&args, sizeof(args)); 57 57 if (pid < 0) { 58 58 ksft_print_msg("%s - Failed to create new process\n", 59 59 strerror(errno));
+1 -1
tools/testing/selftests/pidfd/pidfd_setns_test.c
··· 75 75 76 76 pid_t create_child(int *pidfd, unsigned flags) 77 77 { 78 - struct clone_args args = { 78 + struct __clone_args args = { 79 79 .flags = CLONE_PIDFD | flags, 80 80 .exit_signal = SIGCHLD, 81 81 .pidfd = ptr_to_u64(pidfd),
+2 -2
tools/testing/selftests/seccomp/seccomp_bpf.c
··· 3817 3817 long ret; 3818 3818 int status; 3819 3819 struct pollfd pollfd; 3820 - struct clone_args args = { 3820 + struct __clone_args args = { 3821 3821 .flags = CLONE_FILES, 3822 3822 .exit_signal = SIGCHLD, 3823 3823 }; ··· 3871 3871 long ret; 3872 3872 int status; 3873 3873 struct pollfd pollfd; 3874 - struct clone_args args = { 3874 + struct __clone_args args = { 3875 3875 .flags = CLONE_FILES, 3876 3876 .exit_signal = SIGCHLD, 3877 3877 };