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