1 // SPDX-License-Identifier: GPL-2.0 2 3 /* 4 * Based on Christian Brauner's clone3() example. 5 * These tests are assuming to be running in the host's 6 * PID namespace. 7 */ 8 9 /* capabilities related code based on selftests/bpf/test_verifier.c */ 10 11 #define _GNU_SOURCE 12 #include <errno.h> 13 #include <linux/types.h> 14 #include <linux/sched.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <stdbool.h> 18 #include <sys/capability.h> 19 #include <sys/prctl.h> 20 #include <sys/syscall.h> 21 #include <sys/types.h> 22 #include <sys/un.h> 23 #include <sys/wait.h> 24 #include <unistd.h> 25 #include <sched.h> 26 27 #include "kselftest_harness.h" 28 #include "clone3_selftests.h" 29 30 static void child_exit(int ret) 31 { 32 fflush(stdout); 33 fflush(stderr); 34 _exit(ret); 35 } 36 37 static int call_clone3_set_tid(struct __test_metadata *_metadata, 38 pid_t *set_tid, size_t set_tid_size) 39 { 40 int status; 41 pid_t pid = -1; 42 43 struct __clone_args args = { 44 .exit_signal = SIGCHLD, 45 .set_tid = ptr_to_u64(set_tid), 46 .set_tid_size = set_tid_size, 47 }; 48 49 pid = sys_clone3(&args, sizeof(args)); 50 if (pid < 0) { 51 TH_LOG("%s - Failed to create new process", strerror(errno)); 52 return -errno; 53 } 54 55 if (pid == 0) { 56 TH_LOG("I am the child, my PID is %d (expected %d)", getpid(), set_tid[0]); 57 58 if (set_tid[0] != getpid()) 59 child_exit(EXIT_FAILURE); 60 child_exit(EXIT_SUCCESS); 61 } 62 63 TH_LOG("I am the parent (%d). My child's pid is %d", getpid(), pid); 64 65 if (waitpid(pid, &status, 0) < 0) { 66 TH_LOG("Child returned %s", strerror(errno)); 67 return -errno; 68 } 69 70 if (!WIFEXITED(status)) 71 return -1; 72 73 return WEXITSTATUS(status); 74 } 75 76 static int test_clone3_set_tid(struct __test_metadata *_metadata, 77 pid_t *set_tid, size_t set_tid_size) 78 { 79 int ret; 80 81 TH_LOG("[%d] Trying clone3() with CLONE_SET_TID to %d", getpid(), set_tid[0]); 82 ret = call_clone3_set_tid(_metadata, set_tid, set_tid_size); 83 TH_LOG("[%d] clone3() with CLONE_SET_TID %d says:%d", getpid(), set_tid[0], ret); 84 return ret; 85 } 86 87 static int set_capability(void) 88 { 89 cap_value_t cap_values[] = { 90 CAP_SETUID, CAP_SETGID, CAP_CHECKPOINT_RESTORE 91 }; 92 int ret = -1; 93 cap_t caps; 94 95 caps = cap_get_proc(); 96 if (!caps) { 97 perror("cap_get_proc"); 98 return -1; 99 } 100 101 /* Drop all capabilities */ 102 if (cap_clear(caps)) { 103 perror("cap_clear"); 104 goto out; 105 } 106 107 cap_set_flag(caps, CAP_EFFECTIVE, 3, cap_values, CAP_SET); 108 cap_set_flag(caps, CAP_PERMITTED, 3, cap_values, CAP_SET); 109 110 if (cap_set_proc(caps)) { 111 perror("cap_set_proc"); 112 goto out; 113 } 114 ret = 0; 115 out: 116 if (cap_free(caps)) 117 perror("cap_free"); 118 return ret; 119 } 120 121 TEST(clone3_cap_checkpoint_restore) 122 { 123 pid_t pid; 124 int status; 125 pid_t set_tid[1]; 126 127 test_clone3_supported(); 128 129 EXPECT_EQ(getuid(), 0) 130 SKIP(return, "Skipping all tests as non-root"); 131 132 memset(&set_tid, 0, sizeof(set_tid)); 133 134 /* Find the current active PID */ 135 pid = fork(); 136 if (pid == 0) { 137 TH_LOG("Child has PID %d", getpid()); 138 child_exit(EXIT_SUCCESS); 139 } 140 ASSERT_GT(waitpid(pid, &status, 0), 0) 141 TH_LOG("Waiting for child %d failed", pid); 142 143 /* After the child has finished, its PID should be free. */ 144 set_tid[0] = pid; 145 146 ASSERT_EQ(set_capability(), 0) 147 TH_LOG("Could not set CAP_CHECKPOINT_RESTORE"); 148 149 ASSERT_EQ(prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0), 0); 150 151 EXPECT_EQ(setgid(65534), 0) 152 TH_LOG("Failed to setgid(65534)"); 153 ASSERT_EQ(setuid(65534), 0); 154 155 set_tid[0] = pid; 156 /* This would fail without CAP_CHECKPOINT_RESTORE */ 157 ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), -EPERM); 158 ASSERT_EQ(set_capability(), 0) 159 TH_LOG("Could not set CAP_CHECKPOINT_RESTORE"); 160 /* This should work as we have CAP_CHECKPOINT_RESTORE as non-root */ 161 ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), 0); 162 } 163 164 TEST_HARNESS_MAIN 165