1 // SPDX-License-Identifier: GPL-2.0 2 #define _GNU_SOURCE 3 #include <stdio.h> 4 #include <errno.h> 5 #include <pwd.h> 6 #include <string.h> 7 #include <syscall.h> 8 #include <sys/capability.h> 9 #include <sys/types.h> 10 #include <sys/mount.h> 11 #include <sys/prctl.h> 12 #include <sys/wait.h> 13 #include <stdlib.h> 14 #include <unistd.h> 15 #include <fcntl.h> 16 #include <stdbool.h> 17 #include <stdarg.h> 18 19 #ifndef CLONE_NEWUSER 20 # define CLONE_NEWUSER 0x10000000 21 #endif 22 23 #define ROOT_USER 0 24 #define RESTRICTED_PARENT 1 25 #define ALLOWED_CHILD1 2 26 #define ALLOWED_CHILD2 3 27 #define NO_POLICY_USER 4 28 29 char* add_whitelist_policy_file = "/sys/kernel/security/safesetid/add_whitelist_policy"; 30 31 static void die(char *fmt, ...) 32 { 33 va_list ap; 34 va_start(ap, fmt); 35 vfprintf(stderr, fmt, ap); 36 va_end(ap); 37 exit(EXIT_FAILURE); 38 } 39 40 static bool vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap) 41 { 42 char buf[4096]; 43 int fd; 44 ssize_t written; 45 int buf_len; 46 47 buf_len = vsnprintf(buf, sizeof(buf), fmt, ap); 48 if (buf_len < 0) { 49 printf("vsnprintf failed: %s\n", 50 strerror(errno)); 51 return false; 52 } 53 if (buf_len >= sizeof(buf)) { 54 printf("vsnprintf output truncated\n"); 55 return false; 56 } 57 58 fd = open(filename, O_WRONLY); 59 if (fd < 0) { 60 if ((errno == ENOENT) && enoent_ok) 61 return true; 62 return false; 63 } 64 written = write(fd, buf, buf_len); 65 if (written != buf_len) { 66 if (written >= 0) { 67 printf("short write to %s\n", filename); 68 return false; 69 } else { 70 printf("write to %s failed: %s\n", 71 filename, strerror(errno)); 72 return false; 73 } 74 } 75 if (close(fd) != 0) { 76 printf("close of %s failed: %s\n", 77 filename, strerror(errno)); 78 return false; 79 } 80 return true; 81 } 82 83 static bool write_file(char *filename, char *fmt, ...) 84 { 85 va_list ap; 86 bool ret; 87 88 va_start(ap, fmt); 89 ret = vmaybe_write_file(false, filename, fmt, ap); 90 va_end(ap); 91 92 return ret; 93 } 94 95 static void ensure_user_exists(uid_t uid) 96 { 97 struct passwd p; 98 99 FILE *fd; 100 char name_str[10]; 101 102 if (getpwuid(uid) == NULL) { 103 memset(&p,0x00,sizeof(p)); 104 fd=fopen("/etc/passwd","a"); 105 if (fd == NULL) 106 die("couldn't open file\n"); 107 if (fseek(fd, 0, SEEK_END)) 108 die("couldn't fseek\n"); 109 snprintf(name_str, 10, "%d", uid); 110 p.pw_name=name_str; 111 p.pw_uid=uid; 112 p.pw_gecos="Test account"; 113 p.pw_dir="/dev/null"; 114 p.pw_shell="/bin/false"; 115 int value = putpwent(&p,fd); 116 if (value != 0) 117 die("putpwent failed\n"); 118 if (fclose(fd)) 119 die("fclose failed\n"); 120 } 121 } 122 123 static void ensure_securityfs_mounted(void) 124 { 125 int fd = open(add_whitelist_policy_file, O_WRONLY); 126 if (fd < 0) { 127 if (errno == ENOENT) { 128 // Need to mount securityfs 129 if (mount("securityfs", "/sys/kernel/security", 130 "securityfs", 0, NULL) < 0) 131 die("mounting securityfs failed\n"); 132 } else { 133 die("couldn't find securityfs for unknown reason\n"); 134 } 135 } else { 136 if (close(fd) != 0) { 137 die("close of %s failed: %s\n", 138 add_whitelist_policy_file, strerror(errno)); 139 } 140 } 141 } 142 143 static void write_policies(void) 144 { 145 ssize_t written; 146 int fd; 147 148 fd = open(add_whitelist_policy_file, O_WRONLY); 149 if (fd < 0) 150 die("cant open add_whitelist_policy file\n"); 151 written = write(fd, "1:2", strlen("1:2")); 152 if (written != strlen("1:2")) { 153 if (written >= 0) { 154 die("short write to %s\n", add_whitelist_policy_file); 155 } else { 156 die("write to %s failed: %s\n", 157 add_whitelist_policy_file, strerror(errno)); 158 } 159 } 160 written = write(fd, "1:3", strlen("1:3")); 161 if (written != strlen("1:3")) { 162 if (written >= 0) { 163 die("short write to %s\n", add_whitelist_policy_file); 164 } else { 165 die("write to %s failed: %s\n", 166 add_whitelist_policy_file, strerror(errno)); 167 } 168 } 169 if (close(fd) != 0) { 170 die("close of %s failed: %s\n", 171 add_whitelist_policy_file, strerror(errno)); 172 } 173 } 174 175 static bool test_userns(bool expect_success) 176 { 177 uid_t uid; 178 char map_file_name[32]; 179 size_t sz = sizeof(map_file_name); 180 pid_t cpid; 181 bool success; 182 183 uid = getuid(); 184 185 int clone_flags = CLONE_NEWUSER; 186 cpid = syscall(SYS_clone, clone_flags, NULL); 187 if (cpid == -1) { 188 printf("clone failed"); 189 return false; 190 } 191 192 if (cpid == 0) { /* Code executed by child */ 193 // Give parent 1 second to write map file 194 sleep(1); 195 exit(EXIT_SUCCESS); 196 } else { /* Code executed by parent */ 197 if(snprintf(map_file_name, sz, "/proc/%d/uid_map", cpid) < 0) { 198 printf("preparing file name string failed"); 199 return false; 200 } 201 success = write_file(map_file_name, "0 0 1", uid); 202 return success == expect_success; 203 } 204 205 printf("should not reach here"); 206 return false; 207 } 208 209 static void test_setuid(uid_t child_uid, bool expect_success) 210 { 211 pid_t cpid, w; 212 int wstatus; 213 214 cpid = fork(); 215 if (cpid == -1) { 216 die("fork\n"); 217 } 218 219 if (cpid == 0) { /* Code executed by child */ 220 setuid(child_uid); 221 if (getuid() == child_uid) 222 exit(EXIT_SUCCESS); 223 else 224 exit(EXIT_FAILURE); 225 } else { /* Code executed by parent */ 226 do { 227 w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED); 228 if (w == -1) { 229 die("waitpid\n"); 230 } 231 232 if (WIFEXITED(wstatus)) { 233 if (WEXITSTATUS(wstatus) == EXIT_SUCCESS) { 234 if (expect_success) { 235 return; 236 } else { 237 die("unexpected success\n"); 238 } 239 } else { 240 if (expect_success) { 241 die("unexpected failure\n"); 242 } else { 243 return; 244 } 245 } 246 } else if (WIFSIGNALED(wstatus)) { 247 if (WTERMSIG(wstatus) == 9) { 248 if (expect_success) 249 die("killed unexpectedly\n"); 250 else 251 return; 252 } else { 253 die("unexpected signal: %d\n", wstatus); 254 } 255 } else { 256 die("unexpected status: %d\n", wstatus); 257 } 258 } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus)); 259 } 260 261 die("should not reach here\n"); 262 } 263 264 static void ensure_users_exist(void) 265 { 266 ensure_user_exists(ROOT_USER); 267 ensure_user_exists(RESTRICTED_PARENT); 268 ensure_user_exists(ALLOWED_CHILD1); 269 ensure_user_exists(ALLOWED_CHILD2); 270 ensure_user_exists(NO_POLICY_USER); 271 } 272 273 static void drop_caps(bool setid_retained) 274 { 275 cap_value_t cap_values[] = {CAP_SETUID, CAP_SETGID}; 276 cap_t caps; 277 278 caps = cap_get_proc(); 279 if (setid_retained) 280 cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_values, CAP_SET); 281 else 282 cap_clear(caps); 283 cap_set_proc(caps); 284 cap_free(caps); 285 } 286 287 int main(int argc, char **argv) 288 { 289 ensure_users_exist(); 290 ensure_securityfs_mounted(); 291 write_policies(); 292 293 if (prctl(PR_SET_KEEPCAPS, 1L)) 294 die("Error with set keepcaps\n"); 295 296 // First test to make sure we can write userns mappings from a user 297 // that doesn't have any restrictions (as long as it has CAP_SETUID); 298 setuid(NO_POLICY_USER); 299 setgid(NO_POLICY_USER); 300 301 // Take away all but setid caps 302 drop_caps(true); 303 304 // Need PR_SET_DUMPABLE flag set so we can write /proc/[pid]/uid_map 305 // from non-root parent process. 306 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)) 307 die("Error with set dumpable\n"); 308 309 if (!test_userns(true)) { 310 die("test_userns failed when it should work\n"); 311 } 312 313 setuid(RESTRICTED_PARENT); 314 setgid(RESTRICTED_PARENT); 315 316 test_setuid(ROOT_USER, false); 317 test_setuid(ALLOWED_CHILD1, true); 318 test_setuid(ALLOWED_CHILD2, true); 319 test_setuid(NO_POLICY_USER, false); 320 321 if (!test_userns(false)) { 322 die("test_userns worked when it should fail\n"); 323 } 324 325 // Now take away all caps 326 drop_caps(false); 327 test_setuid(2, false); 328 test_setuid(3, false); 329 test_setuid(4, false); 330 331 // NOTE: this test doesn't clean up users that were created in 332 // /etc/passwd or flush policies that were added to the LSM. 333 return EXIT_SUCCESS; 334 } 335