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 #ifndef MAX_PID_NS_LEVEL
31 #define MAX_PID_NS_LEVEL 32
34 static void child_exit(int ret)
41 static int call_clone3_set_tid(struct __test_metadata *_metadata,
42 pid_t *set_tid, size_t set_tid_size)
47 struct __clone_args args = {
48 .exit_signal = SIGCHLD,
49 .set_tid = ptr_to_u64(set_tid),
50 .set_tid_size = set_tid_size,
53 pid = sys_clone3(&args, sizeof(args));
55 TH_LOG("%s - Failed to create new process", strerror(errno));
63 TH_LOG("I am the child, my PID is %d (expected %d)", getpid(), set_tid[0]);
65 if (set_tid[0] != getpid())
66 child_exit(EXIT_FAILURE);
67 child_exit(EXIT_SUCCESS);
70 TH_LOG("I am the parent (%d). My child's pid is %d", getpid(), pid);
72 if (waitpid(pid, &status, 0) < 0) {
73 TH_LOG("Child returned %s", strerror(errno));
77 if (!WIFEXITED(status))
80 return WEXITSTATUS(status);
83 static int test_clone3_set_tid(struct __test_metadata *_metadata,
84 pid_t *set_tid, size_t set_tid_size)
88 TH_LOG("[%d] Trying clone3() with CLONE_SET_TID to %d", getpid(), set_tid[0]);
89 ret = call_clone3_set_tid(_metadata, set_tid, set_tid_size);
90 TH_LOG("[%d] clone3() with CLONE_SET_TID %d says:%d", getpid(), set_tid[0], ret);
95 struct __user_cap_header_struct hdr;
96 struct __user_cap_data_struct data[2];
99 static int set_capability(void)
101 cap_value_t cap_values[] = { CAP_SETUID, CAP_SETGID };
106 caps = cap_get_proc();
108 perror("cap_get_proc");
112 /* Drop all capabilities */
113 if (cap_clear(caps)) {
118 cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_values, CAP_SET);
119 cap_set_flag(caps, CAP_PERMITTED, 2, cap_values, CAP_SET);
121 cap = (struct libcap *) caps;
123 /* 40 -> CAP_CHECKPOINT_RESTORE */
124 cap->data[1].effective |= 1 << (40 - 32);
125 cap->data[1].permitted |= 1 << (40 - 32);
127 if (cap_set_proc(caps)) {
128 perror("cap_set_proc");
138 TEST(clone3_cap_checkpoint_restore)
145 test_clone3_supported();
147 EXPECT_EQ(getuid(), 0)
148 XFAIL(return, "Skipping all tests as non-root\n");
150 memset(&set_tid, 0, sizeof(set_tid));
152 /* Find the current active PID */
155 TH_LOG("Child has PID %d", getpid());
156 child_exit(EXIT_SUCCESS);
158 ASSERT_GT(waitpid(pid, &status, 0), 0)
159 TH_LOG("Waiting for child %d failed", pid);
161 /* After the child has finished, its PID should be free. */
164 ASSERT_EQ(set_capability(), 0)
165 TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
167 ASSERT_EQ(prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0), 0);
169 EXPECT_EQ(setgid(65534), 0)
170 TH_LOG("Failed to setgid(65534)");
171 ASSERT_EQ(setuid(65534), 0);
174 /* This would fail without CAP_CHECKPOINT_RESTORE */
175 ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), -EPERM);
176 ASSERT_EQ(set_capability(), 0)
177 TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
178 /* This should work as we have CAP_CHECKPOINT_RESTORE as non-root */
179 ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), 0);