0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #define _GNU_SOURCE
0012 #include <errno.h>
0013 #include <linux/types.h>
0014 #include <linux/sched.h>
0015 #include <stdio.h>
0016 #include <stdlib.h>
0017 #include <stdbool.h>
0018 #include <sys/capability.h>
0019 #include <sys/prctl.h>
0020 #include <sys/syscall.h>
0021 #include <sys/types.h>
0022 #include <sys/un.h>
0023 #include <sys/wait.h>
0024 #include <unistd.h>
0025 #include <sched.h>
0026
0027 #include "../kselftest_harness.h"
0028 #include "clone3_selftests.h"
0029
0030 #ifndef MAX_PID_NS_LEVEL
0031 #define MAX_PID_NS_LEVEL 32
0032 #endif
0033
0034 static void child_exit(int ret)
0035 {
0036 fflush(stdout);
0037 fflush(stderr);
0038 _exit(ret);
0039 }
0040
0041 static int call_clone3_set_tid(struct __test_metadata *_metadata,
0042 pid_t *set_tid, size_t set_tid_size)
0043 {
0044 int status;
0045 pid_t pid = -1;
0046
0047 struct __clone_args args = {
0048 .exit_signal = SIGCHLD,
0049 .set_tid = ptr_to_u64(set_tid),
0050 .set_tid_size = set_tid_size,
0051 };
0052
0053 pid = sys_clone3(&args, sizeof(args));
0054 if (pid < 0) {
0055 TH_LOG("%s - Failed to create new process", strerror(errno));
0056 return -errno;
0057 }
0058
0059 if (pid == 0) {
0060 int ret;
0061 char tmp = 0;
0062
0063 TH_LOG("I am the child, my PID is %d (expected %d)", getpid(), set_tid[0]);
0064
0065 if (set_tid[0] != getpid())
0066 child_exit(EXIT_FAILURE);
0067 child_exit(EXIT_SUCCESS);
0068 }
0069
0070 TH_LOG("I am the parent (%d). My child's pid is %d", getpid(), pid);
0071
0072 if (waitpid(pid, &status, 0) < 0) {
0073 TH_LOG("Child returned %s", strerror(errno));
0074 return -errno;
0075 }
0076
0077 if (!WIFEXITED(status))
0078 return -1;
0079
0080 return WEXITSTATUS(status);
0081 }
0082
0083 static int test_clone3_set_tid(struct __test_metadata *_metadata,
0084 pid_t *set_tid, size_t set_tid_size)
0085 {
0086 int ret;
0087
0088 TH_LOG("[%d] Trying clone3() with CLONE_SET_TID to %d", getpid(), set_tid[0]);
0089 ret = call_clone3_set_tid(_metadata, set_tid, set_tid_size);
0090 TH_LOG("[%d] clone3() with CLONE_SET_TID %d says:%d", getpid(), set_tid[0], ret);
0091 return ret;
0092 }
0093
0094 struct libcap {
0095 struct __user_cap_header_struct hdr;
0096 struct __user_cap_data_struct data[2];
0097 };
0098
0099 static int set_capability(void)
0100 {
0101 cap_value_t cap_values[] = { CAP_SETUID, CAP_SETGID };
0102 struct libcap *cap;
0103 int ret = -1;
0104 cap_t caps;
0105
0106 caps = cap_get_proc();
0107 if (!caps) {
0108 perror("cap_get_proc");
0109 return -1;
0110 }
0111
0112
0113 if (cap_clear(caps)) {
0114 perror("cap_clear");
0115 goto out;
0116 }
0117
0118 cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_values, CAP_SET);
0119 cap_set_flag(caps, CAP_PERMITTED, 2, cap_values, CAP_SET);
0120
0121 cap = (struct libcap *) caps;
0122
0123
0124 cap->data[1].effective |= 1 << (40 - 32);
0125 cap->data[1].permitted |= 1 << (40 - 32);
0126
0127 if (cap_set_proc(caps)) {
0128 perror("cap_set_proc");
0129 goto out;
0130 }
0131 ret = 0;
0132 out:
0133 if (cap_free(caps))
0134 perror("cap_free");
0135 return ret;
0136 }
0137
0138 TEST(clone3_cap_checkpoint_restore)
0139 {
0140 pid_t pid;
0141 int status;
0142 int ret = 0;
0143 pid_t set_tid[1];
0144
0145 test_clone3_supported();
0146
0147 EXPECT_EQ(getuid(), 0)
0148 SKIP(return, "Skipping all tests as non-root");
0149
0150 memset(&set_tid, 0, sizeof(set_tid));
0151
0152
0153 pid = fork();
0154 if (pid == 0) {
0155 TH_LOG("Child has PID %d", getpid());
0156 child_exit(EXIT_SUCCESS);
0157 }
0158 ASSERT_GT(waitpid(pid, &status, 0), 0)
0159 TH_LOG("Waiting for child %d failed", pid);
0160
0161
0162 set_tid[0] = pid;
0163
0164 ASSERT_EQ(set_capability(), 0)
0165 TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
0166
0167 ASSERT_EQ(prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0), 0);
0168
0169 EXPECT_EQ(setgid(65534), 0)
0170 TH_LOG("Failed to setgid(65534)");
0171 ASSERT_EQ(setuid(65534), 0);
0172
0173 set_tid[0] = pid;
0174
0175 ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), -EPERM);
0176 ASSERT_EQ(set_capability(), 0)
0177 TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
0178
0179 ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), 0);
0180 }
0181
0182 TEST_HARNESS_MAIN