Skip to content

Commit e953aea

Browse files
committed
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 <[email protected]> Link: https://lore.kernel.org/lkml/20200921075432.u4gis3s2o5qrsb5g@wittgenstein/ Signed-off-by: Kees Cook <[email protected]>
1 parent a39caac commit e953aea

File tree

7 files changed

+41
-44
lines changed

7 files changed

+41
-44
lines changed

tools/testing/selftests/clone3/clone3.c

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@
2020
#include "../kselftest.h"
2121
#include "clone3_selftests.h"
2222

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-
3023
enum test_mode {
3124
CLONE3_ARGS_NO_TEST,
3225
CLONE3_ARGS_ALL_0,
@@ -38,25 +31,25 @@ enum test_mode {
3831

3932
static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
4033
{
41-
struct clone_args args = {
34+
struct __clone_args args = {
4235
.flags = flags,
4336
.exit_signal = SIGCHLD,
4437
};
4538

4639
struct clone_args_extended {
47-
struct clone_args args;
40+
struct __clone_args args;
4841
__aligned_u64 excess_space[2];
4942
} args_ext;
5043

5144
pid_t pid = -1;
5245
int status;
5346

5447
memset(&args_ext, 0, sizeof(args_ext));
55-
if (size > sizeof(struct clone_args))
48+
if (size > sizeof(struct __clone_args))
5649
args_ext.excess_space[1] = 1;
5750

5851
if (size == 0)
59-
size = sizeof(struct clone_args);
52+
size = sizeof(struct __clone_args);
6053

6154
switch (test_mode) {
6255
case CLONE3_ARGS_ALL_0:
@@ -77,9 +70,9 @@ static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
7770
break;
7871
}
7972

80-
memcpy(&args_ext.args, &args, sizeof(struct clone_args));
73+
memcpy(&args_ext.args, &args, sizeof(struct __clone_args));
8174

82-
pid = sys_clone3((struct clone_args *)&args_ext, size);
75+
pid = sys_clone3((struct __clone_args *)&args_ext, size);
8376
if (pid < 0) {
8477
ksft_print_msg("%s - Failed to create new process\n",
8578
strerror(errno));
@@ -144,14 +137,14 @@ int main(int argc, char *argv[])
144137
else
145138
ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
146139

147-
/* Do a clone3() with CLONE3_ARGS_SIZE_V0. */
148-
test_clone3(0, CLONE3_ARGS_SIZE_V0, 0, CLONE3_ARGS_NO_TEST);
140+
/* Do a clone3() with CLONE_ARGS_SIZE_VER0. */
141+
test_clone3(0, CLONE_ARGS_SIZE_VER0, 0, CLONE3_ARGS_NO_TEST);
149142

150-
/* Do a clone3() with CLONE3_ARGS_SIZE_V0 - 8 */
151-
test_clone3(0, CLONE3_ARGS_SIZE_V0 - 8, -EINVAL, CLONE3_ARGS_NO_TEST);
143+
/* Do a clone3() with CLONE_ARGS_SIZE_VER0 - 8 */
144+
test_clone3(0, CLONE_ARGS_SIZE_VER0 - 8, -EINVAL, CLONE3_ARGS_NO_TEST);
152145

153146
/* Do a clone3() with sizeof(struct clone_args) + 8 */
154-
test_clone3(0, sizeof(struct clone_args) + 8, 0, CLONE3_ARGS_NO_TEST);
147+
test_clone3(0, sizeof(struct __clone_args) + 8, 0, CLONE3_ARGS_NO_TEST);
155148

156149
/* Do a clone3() with exit_signal having highest 32 bits non-zero */
157150
test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG);
@@ -165,31 +158,31 @@ int main(int argc, char *argv[])
165158
/* Do a clone3() with NSIG < exit_signal < CSIG */
166159
test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG);
167160

168-
test_clone3(0, sizeof(struct clone_args) + 8, 0, CLONE3_ARGS_ALL_0);
161+
test_clone3(0, sizeof(struct __clone_args) + 8, 0, CLONE3_ARGS_ALL_0);
169162

170-
test_clone3(0, sizeof(struct clone_args) + 16, -E2BIG,
163+
test_clone3(0, sizeof(struct __clone_args) + 16, -E2BIG,
171164
CLONE3_ARGS_ALL_0);
172165

173-
test_clone3(0, sizeof(struct clone_args) * 2, -E2BIG,
166+
test_clone3(0, sizeof(struct __clone_args) * 2, -E2BIG,
174167
CLONE3_ARGS_ALL_0);
175168

176169
/* Do a clone3() with > page size */
177170
test_clone3(0, getpagesize() + 8, -E2BIG, CLONE3_ARGS_NO_TEST);
178171

179-
/* Do a clone3() with CLONE3_ARGS_SIZE_V0 in a new PID NS. */
172+
/* Do a clone3() with CLONE_ARGS_SIZE_VER0 in a new PID NS. */
180173
if (uid == 0)
181-
test_clone3(CLONE_NEWPID, CLONE3_ARGS_SIZE_V0, 0,
174+
test_clone3(CLONE_NEWPID, CLONE_ARGS_SIZE_VER0, 0,
182175
CLONE3_ARGS_NO_TEST);
183176
else
184177
ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
185178

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,
179+
/* Do a clone3() with CLONE_ARGS_SIZE_VER0 - 8 in a new PID NS */
180+
test_clone3(CLONE_NEWPID, CLONE_ARGS_SIZE_VER0 - 8, -EINVAL,
188181
CLONE3_ARGS_NO_TEST);
189182

190183
/* Do a clone3() with sizeof(struct clone_args) + 8 in a new PID NS */
191184
if (uid == 0)
192-
test_clone3(CLONE_NEWPID, sizeof(struct clone_args) + 8, 0,
185+
test_clone3(CLONE_NEWPID, sizeof(struct __clone_args) + 8, 0,
193186
CLONE3_ARGS_NO_TEST);
194187
else
195188
ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");

tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ static int call_clone3_set_tid(struct __test_metadata *_metadata,
4444
int status;
4545
pid_t pid = -1;
4646

47-
struct clone_args args = {
47+
struct __clone_args args = {
4848
.exit_signal = SIGCHLD,
4949
.set_tid = ptr_to_u64(set_tid),
5050
.set_tid_size = set_tid_size,
5151
};
5252

53-
pid = sys_clone3(&args, sizeof(struct clone_args));
53+
pid = sys_clone3(&args, sizeof(args));
5454
if (pid < 0) {
5555
TH_LOG("%s - Failed to create new process", strerror(errno));
5656
return -errno;

tools/testing/selftests/clone3/clone3_clear_sighand.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void test_clone3_clear_sighand(void)
4747
{
4848
int ret;
4949
pid_t pid;
50-
struct clone_args args = {};
50+
struct __clone_args args = {};
5151
struct sigaction act;
5252

5353
/*

tools/testing/selftests/clone3/clone3_selftests.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
#define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */
2020
#endif
2121

22-
#ifndef CLONE_ARGS_SIZE_VER0
23-
#define CLONE_ARGS_SIZE_VER0 64
24-
#endif
25-
2622
#ifndef __NR_clone3
2723
#define __NR_clone3 -1
28-
struct clone_args {
24+
#endif
25+
26+
struct __clone_args {
2927
__aligned_u64 flags;
3028
__aligned_u64 pidfd;
3129
__aligned_u64 child_tid;
@@ -34,15 +32,21 @@ struct clone_args {
3432
__aligned_u64 stack;
3533
__aligned_u64 stack_size;
3634
__aligned_u64 tls;
37-
#define CLONE_ARGS_SIZE_VER1 80
35+
#ifndef CLONE_ARGS_SIZE_VER0
36+
#define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */
37+
#endif
3838
__aligned_u64 set_tid;
3939
__aligned_u64 set_tid_size;
40-
#define CLONE_ARGS_SIZE_VER2 88
40+
#ifndef CLONE_ARGS_SIZE_VER1
41+
#define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */
42+
#endif
4143
__aligned_u64 cgroup;
44+
#ifndef CLONE_ARGS_SIZE_VER2
45+
#define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */
46+
#endif
4247
};
43-
#endif /* __NR_clone3 */
4448

45-
static pid_t sys_clone3(struct clone_args *args, size_t size)
49+
static pid_t sys_clone3(struct __clone_args *args, size_t size)
4650
{
4751
fflush(stdout);
4852
fflush(stderr);
@@ -52,7 +56,7 @@ static pid_t sys_clone3(struct clone_args *args, size_t size)
5256
static inline void test_clone3_supported(void)
5357
{
5458
pid_t pid;
55-
struct clone_args args = {};
59+
struct __clone_args args = {};
5660

5761
if (__NR_clone3 < 0)
5862
ksft_exit_skip("clone3() syscall is not supported\n");

tools/testing/selftests/clone3/clone3_set_tid.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ static int call_clone3_set_tid(pid_t *set_tid,
4646
int status;
4747
pid_t pid = -1;
4848

49-
struct clone_args args = {
49+
struct __clone_args args = {
5050
.flags = flags,
5151
.exit_signal = SIGCHLD,
5252
.set_tid = ptr_to_u64(set_tid),
5353
.set_tid_size = set_tid_size,
5454
};
5555

56-
pid = sys_clone3(&args, sizeof(struct clone_args));
56+
pid = sys_clone3(&args, sizeof(args));
5757
if (pid < 0) {
5858
ksft_print_msg("%s - Failed to create new process\n",
5959
strerror(errno));

tools/testing/selftests/pidfd/pidfd_setns_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static int sys_waitid(int which, pid_t pid, int options)
7575

7676
pid_t create_child(int *pidfd, unsigned flags)
7777
{
78-
struct clone_args args = {
78+
struct __clone_args args = {
7979
.flags = CLONE_PIDFD | flags,
8080
.exit_signal = SIGCHLD,
8181
.pidfd = ptr_to_u64(pidfd),

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3817,7 +3817,7 @@ TEST(user_notification_filter_empty)
38173817
long ret;
38183818
int status;
38193819
struct pollfd pollfd;
3820-
struct clone_args args = {
3820+
struct __clone_args args = {
38213821
.flags = CLONE_FILES,
38223822
.exit_signal = SIGCHLD,
38233823
};
@@ -3871,7 +3871,7 @@ TEST(user_notification_filter_empty_threaded)
38713871
long ret;
38723872
int status;
38733873
struct pollfd pollfd;
3874-
struct clone_args args = {
3874+
struct __clone_args args = {
38753875
.flags = CLONE_FILES,
38763876
.exit_signal = SIGCHLD,
38773877
};

0 commit comments

Comments
 (0)