1 // SPDX-License-Identifier: GPL-2.0
4 * Based on Christian Brauner's clone3() example.
5 * These tests are assuming to be running in the host's
9 /* capabilities related code based on selftests/bpf/test_verifier.c */
13 #include <linux/types.h>
14 #include <linux/sched.h>
18 #include <sys/capability.h>
19 #include <sys/prctl.h>
20 #include <sys/syscall.h>
21 #include <sys/types.h>
27 #include "../kselftest_harness.h"
28 #include "clone3_selftests.h"
30 static void child_exit(int ret
)
37 static int call_clone3_set_tid(struct __test_metadata
*_metadata
,
38 pid_t
*set_tid
, size_t set_tid_size
)
43 struct __clone_args args
= {
44 .exit_signal
= SIGCHLD
,
45 .set_tid
= ptr_to_u64(set_tid
),
46 .set_tid_size
= set_tid_size
,
49 pid
= sys_clone3(&args
, sizeof(args
));
51 TH_LOG("%s - Failed to create new process", strerror(errno
));
59 TH_LOG("I am the child, my PID is %d (expected %d)", getpid(), set_tid
[0]);
61 if (set_tid
[0] != getpid())
62 child_exit(EXIT_FAILURE
);
63 child_exit(EXIT_SUCCESS
);
66 TH_LOG("I am the parent (%d). My child's pid is %d", getpid(), pid
);
68 if (waitpid(pid
, &status
, 0) < 0) {
69 TH_LOG("Child returned %s", strerror(errno
));
73 if (!WIFEXITED(status
))
76 return WEXITSTATUS(status
);
79 static int test_clone3_set_tid(struct __test_metadata
*_metadata
,
80 pid_t
*set_tid
, size_t set_tid_size
)
84 TH_LOG("[%d] Trying clone3() with CLONE_SET_TID to %d", getpid(), set_tid
[0]);
85 ret
= call_clone3_set_tid(_metadata
, set_tid
, set_tid_size
);
86 TH_LOG("[%d] clone3() with CLONE_SET_TID %d says:%d", getpid(), set_tid
[0], ret
);
91 struct __user_cap_header_struct hdr
;
92 struct __user_cap_data_struct data
[2];
95 static int set_capability(void)
97 cap_value_t cap_values
[] = { CAP_SETUID
, CAP_SETGID
};
102 caps
= cap_get_proc();
104 perror("cap_get_proc");
108 /* Drop all capabilities */
109 if (cap_clear(caps
)) {
114 cap_set_flag(caps
, CAP_EFFECTIVE
, 2, cap_values
, CAP_SET
);
115 cap_set_flag(caps
, CAP_PERMITTED
, 2, cap_values
, CAP_SET
);
117 cap
= (struct libcap
*) caps
;
119 /* 40 -> CAP_CHECKPOINT_RESTORE */
120 cap
->data
[1].effective
|= 1 << (40 - 32);
121 cap
->data
[1].permitted
|= 1 << (40 - 32);
123 if (cap_set_proc(caps
)) {
124 perror("cap_set_proc");
134 TEST(clone3_cap_checkpoint_restore
)
141 test_clone3_supported();
143 EXPECT_EQ(getuid(), 0)
144 SKIP(return, "Skipping all tests as non-root");
146 memset(&set_tid
, 0, sizeof(set_tid
));
148 /* Find the current active PID */
151 TH_LOG("Child has PID %d", getpid());
152 child_exit(EXIT_SUCCESS
);
154 ASSERT_GT(waitpid(pid
, &status
, 0), 0)
155 TH_LOG("Waiting for child %d failed", pid
);
157 /* After the child has finished, its PID should be free. */
160 ASSERT_EQ(set_capability(), 0)
161 TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
163 ASSERT_EQ(prctl(PR_SET_KEEPCAPS
, 1, 0, 0, 0), 0);
165 EXPECT_EQ(setgid(65534), 0)
166 TH_LOG("Failed to setgid(65534)");
167 ASSERT_EQ(setuid(65534), 0);
170 /* This would fail without CAP_CHECKPOINT_RESTORE */
171 ASSERT_EQ(test_clone3_set_tid(_metadata
, set_tid
, 1), -EPERM
);
172 ASSERT_EQ(set_capability(), 0)
173 TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
174 /* This should work as we have CAP_CHECKPOINT_RESTORE as non-root */
175 ASSERT_EQ(test_clone3_set_tid(_metadata
, set_tid
, 1), 0);