Mike Frysinger | 4c33189 | 2022-09-13 05:17:08 -0400 | [diff] [blame] | 1 | /* Copyright 2017 The ChromiumOS Authors |
Mike Frysinger | 50e31fa | 2018-01-19 18:59:49 -0500 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include "system.h" |
| 7 | |
| 8 | #include <errno.h> |
| 9 | #include <fcntl.h> |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 10 | #include <grp.h> |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 11 | #include <net/if.h> |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 12 | #include <pwd.h> |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 13 | #include <stdbool.h> |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/prctl.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/stat.h> |
Luis Hector Chavez | 0bacbf8 | 2018-07-10 20:06:55 -0700 | [diff] [blame] | 20 | #include <sys/statvfs.h> |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
Mattias Nissler | e520019 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 23 | #include <linux/securebits.h> |
| 24 | |
Luis Héctor Chávez | 01b628c | 2021-01-03 05:46:57 -0800 | [diff] [blame] | 25 | #include "syscall_wrapper.h" |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 26 | #include "util.h" |
| 27 | |
Mattias Nissler | e520019 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 28 | /* |
| 29 | * SECBIT_NO_CAP_AMBIENT_RAISE was added in kernel 4.3, so fill in the |
| 30 | * definition if the securebits header doesn't provide it. |
| 31 | */ |
| 32 | #ifndef SECBIT_NO_CAP_AMBIENT_RAISE |
| 33 | #define SECBIT_NO_CAP_AMBIENT_RAISE (issecure_mask(6)) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 34 | #endif |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 35 | |
Mattias Nissler | e520019 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 36 | #ifndef SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED |
| 37 | #define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED (issecure_mask(7)) |
| 38 | #endif |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Assert the value of SECURE_ALL_BITS at compile-time. |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 42 | * Android devices are currently compiled against 4.4 kernel headers. Kernel 4.3 |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 43 | * added a new securebit. |
| 44 | * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM |
| 45 | * when used on older kernels. The compile-time assert will catch this situation |
| 46 | * at compile time. |
| 47 | */ |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 48 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 49 | _Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55."); |
| 50 | #endif |
| 51 | |
Stéphane Lesimple | 84ffabc | 2020-02-14 20:33:34 +0100 | [diff] [blame] | 52 | /* Used by lookup_(user|group) functions. */ |
| 53 | #define MAX_PWENT_SZ (1 << 20) |
| 54 | #define MAX_GRENT_SZ (1 << 20) |
| 55 | |
Jorge Lucangeli Obes | 5423421 | 2018-04-26 11:52:15 -0400 | [diff] [blame] | 56 | int secure_noroot_set_and_locked(uint64_t mask) |
| 57 | { |
| 58 | return (mask & (SECBIT_NOROOT | SECBIT_NOROOT_LOCKED)) == |
| 59 | (SECBIT_NOROOT | SECBIT_NOROOT_LOCKED); |
| 60 | } |
| 61 | |
Mattias Nissler | 48b5ff1 | 2018-10-11 15:31:41 +0200 | [diff] [blame] | 62 | int lock_securebits(uint64_t skip_mask, bool require_keep_caps) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 63 | { |
Mattias Nissler | 48b5ff1 | 2018-10-11 15:31:41 +0200 | [diff] [blame] | 64 | /* The general idea is to set all bits, subject to exceptions below. */ |
| 65 | unsigned long securebits = SECURE_ALL_BITS | SECURE_ALL_LOCKS; |
| 66 | |
| 67 | /* |
| 68 | * SECBIT_KEEP_CAPS is special in that it is automatically cleared on |
| 69 | * execve(2). This implies that attempts to set SECBIT_KEEP_CAPS (as is |
| 70 | * the default) in processes that have it locked already (such as nested |
| 71 | * minijail usage) would fail. Thus, unless the caller requires it, |
| 72 | * allow it to remain off if it is already locked. |
| 73 | */ |
| 74 | if (!require_keep_caps) { |
| 75 | int current_securebits = prctl(PR_GET_SECUREBITS); |
| 76 | if (current_securebits < 0) { |
| 77 | pwarn("prctl(PR_GET_SECUREBITS) failed"); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | if ((current_securebits & SECBIT_KEEP_CAPS_LOCKED) != 0 && |
| 82 | (current_securebits & SECBIT_KEEP_CAPS) == 0) { |
| 83 | securebits &= ~SECBIT_KEEP_CAPS; |
| 84 | } |
| 85 | } |
| 86 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 87 | /* |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 88 | * Ambient capabilities can only be raised if they're already present |
| 89 | * in the permitted *and* inheritable set. Therefore, we don't really |
| 90 | * need to lock the NO_CAP_AMBIENT_RAISE securebit, since we are already |
| 91 | * configuring the permitted and inheritable set. |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 92 | */ |
Mattias Nissler | 48b5ff1 | 2018-10-11 15:31:41 +0200 | [diff] [blame] | 93 | securebits &= |
| 94 | ~(SECBIT_NO_CAP_AMBIENT_RAISE | SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED); |
| 95 | |
| 96 | /* Don't set any bits that the user requested not to be touched. */ |
| 97 | securebits &= ~skip_mask; |
| 98 | |
Luis Hector Chavez | ec0a2c1 | 2017-06-29 20:29:57 -0700 | [diff] [blame] | 99 | if (!securebits) { |
Jorge Lucangeli Obes | 5423421 | 2018-04-26 11:52:15 -0400 | [diff] [blame] | 100 | warn("not locking any securebits"); |
Luis Hector Chavez | ec0a2c1 | 2017-06-29 20:29:57 -0700 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | int securebits_ret = prctl(PR_SET_SECUREBITS, securebits); |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 104 | if (securebits_ret < 0) { |
| 105 | pwarn("prctl(PR_SET_SECUREBITS) failed"); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int write_proc_file(pid_t pid, const char *content, const char *basename) |
| 113 | { |
Mike Frysinger | 94cff17 | 2021-07-16 02:59:04 -0400 | [diff] [blame] | 114 | attribute_cleanup_fd int fd = -1; |
| 115 | int ret; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 116 | size_t sz, len; |
| 117 | ssize_t written; |
| 118 | char filename[32]; |
| 119 | |
| 120 | sz = sizeof(filename); |
| 121 | ret = snprintf(filename, sz, "/proc/%d/%s", pid, basename); |
| 122 | if (ret < 0 || (size_t)ret >= sz) { |
| 123 | warn("failed to generate %s filename", basename); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | fd = open(filename, O_WRONLY | O_CLOEXEC); |
| 128 | if (fd < 0) { |
| 129 | pwarn("failed to open '%s'", filename); |
| 130 | return -errno; |
| 131 | } |
| 132 | |
| 133 | len = strlen(content); |
| 134 | written = write(fd, content, len); |
| 135 | if (written < 0) { |
| 136 | pwarn("failed to write '%s'", filename); |
Jorge Lucangeli Obes | 673c89d | 2018-10-04 16:08:10 -0400 | [diff] [blame] | 137 | return -errno; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if ((size_t)written < len) { |
| 141 | warn("failed to write %zu bytes to '%s'", len, filename); |
| 142 | return -1; |
| 143 | } |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * We specifically do not use cap_valid() as that only tells us the last |
| 149 | * valid cap we were *compiled* against (i.e. what the version of kernel |
| 150 | * headers says). If we run on a different kernel version, then it's not |
| 151 | * uncommon for that to be less (if an older kernel) or more (if a newer |
| 152 | * kernel). |
| 153 | * Normally, we suck up the answer via /proc. On Android, not all processes are |
| 154 | * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we |
| 155 | * programmatically find the value by calling prctl(PR_CAPBSET_READ). |
| 156 | */ |
| 157 | unsigned int get_last_valid_cap(void) |
| 158 | { |
| 159 | unsigned int last_valid_cap = 0; |
| 160 | if (is_android()) { |
| 161 | for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0; |
| 162 | ++last_valid_cap) |
| 163 | ; |
| 164 | |
| 165 | /* |last_valid_cap| will be the first failing value. */ |
| 166 | if (last_valid_cap > 0) { |
| 167 | last_valid_cap--; |
| 168 | } |
| 169 | } else { |
Mike Frysinger | 28c64b6 | 2021-10-05 00:09:29 -0400 | [diff] [blame] | 170 | static const char cap_file[] = "/proc/sys/kernel/cap_last_cap"; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 171 | FILE *fp = fopen(cap_file, "re"); |
Mike Frysinger | 28c64b6 | 2021-10-05 00:09:29 -0400 | [diff] [blame] | 172 | if (!fp) |
| 173 | pdie("fopen(%s)", cap_file); |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 174 | if (fscanf(fp, "%u", &last_valid_cap) != 1) |
| 175 | pdie("fscanf(%s)", cap_file); |
| 176 | fclose(fp); |
| 177 | } |
| 178 | return last_valid_cap; |
| 179 | } |
| 180 | |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 181 | int cap_ambient_supported(void) |
| 182 | { |
| 183 | return prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0) >= |
| 184 | 0; |
| 185 | } |
| 186 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 187 | int config_net_loopback(void) |
| 188 | { |
| 189 | const char ifname[] = "lo"; |
Mike Frysinger | 94cff17 | 2021-07-16 02:59:04 -0400 | [diff] [blame] | 190 | attribute_cleanup_fd int sock = -1; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 191 | struct ifreq ifr; |
| 192 | |
| 193 | /* Make sure people don't try to add really long names. */ |
| 194 | _Static_assert(sizeof(ifname) <= IFNAMSIZ, "interface name too long"); |
| 195 | |
| 196 | sock = socket(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 197 | if (sock < 0) { |
| 198 | pwarn("socket(AF_LOCAL) failed"); |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Do the equiv of `ip link set up lo`. The kernel will assign |
| 204 | * IPv4 (127.0.0.1) & IPv6 (::1) addresses automatically! |
| 205 | */ |
| 206 | strcpy(ifr.ifr_name, ifname); |
| 207 | if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
| 208 | pwarn("ioctl(SIOCGIFFLAGS) failed"); |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | /* The kernel preserves ifr.ifr_name for use. */ |
| 213 | ifr.ifr_flags |= IFF_UP | IFF_RUNNING; |
| 214 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) { |
| 215 | pwarn("ioctl(SIOCSIFFLAGS) failed"); |
| 216 | return -1; |
| 217 | } |
| 218 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 222 | int write_pid_to_path(pid_t pid, const char *path) |
| 223 | { |
Jorge Lucangeli Obes | 1f5d095 | 2019-06-04 09:18:26 -0400 | [diff] [blame] | 224 | FILE *fp = fopen(path, "we"); |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 225 | |
| 226 | if (!fp) { |
Jorge Lucangeli Obes | 1f5d095 | 2019-06-04 09:18:26 -0400 | [diff] [blame] | 227 | pwarn("failed to open '%s'", path); |
| 228 | return -errno; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 229 | } |
| 230 | if (fprintf(fp, "%d\n", (int)pid) < 0) { |
| 231 | /* fprintf(3) does not set errno on failure. */ |
| 232 | warn("fprintf(%s) failed", path); |
Jorge Lucangeli Obes | 005c903 | 2021-06-18 11:26:48 -0400 | [diff] [blame] | 233 | fclose(fp); |
Jorge Lucangeli Obes | 1f5d095 | 2019-06-04 09:18:26 -0400 | [diff] [blame] | 234 | return -1; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 235 | } |
| 236 | if (fclose(fp)) { |
| 237 | pwarn("fclose(%s) failed", path); |
Jorge Lucangeli Obes | 1f5d095 | 2019-06-04 09:18:26 -0400 | [diff] [blame] | 238 | return -errno; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Jorge Lucangeli Obes | 1f5d095 | 2019-06-04 09:18:26 -0400 | [diff] [blame] | 241 | return 0; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* |
Mike Frysinger | 5fdba4e | 2018-01-17 15:39:48 -0500 | [diff] [blame] | 245 | * Create the |path| directory and its parents (if need be) with |mode|. |
| 246 | * If not |isdir|, then |path| is actually a file, so the last component |
| 247 | * will not be created. |
| 248 | */ |
| 249 | int mkdir_p(const char *path, mode_t mode, bool isdir) |
| 250 | { |
yusukes | 059e0bd | 2018-03-05 10:22:16 -0800 | [diff] [blame] | 251 | int rc; |
Mike Frysinger | 5fdba4e | 2018-01-17 15:39:48 -0500 | [diff] [blame] | 252 | char *dir = strdup(path); |
yusukes | 059e0bd | 2018-03-05 10:22:16 -0800 | [diff] [blame] | 253 | if (!dir) { |
| 254 | rc = errno; |
| 255 | pwarn("strdup(%s) failed", path); |
| 256 | return -rc; |
| 257 | } |
Mike Frysinger | 5fdba4e | 2018-01-17 15:39:48 -0500 | [diff] [blame] | 258 | |
| 259 | /* Starting from the root, work our way out to the end. */ |
| 260 | char *p = strchr(dir + 1, '/'); |
| 261 | while (p) { |
| 262 | *p = '\0'; |
| 263 | if (mkdir(dir, mode) && errno != EEXIST) { |
yusukes | 059e0bd | 2018-03-05 10:22:16 -0800 | [diff] [blame] | 264 | rc = errno; |
| 265 | pwarn("mkdir(%s, 0%o) failed", dir, mode); |
Mike Frysinger | 5fdba4e | 2018-01-17 15:39:48 -0500 | [diff] [blame] | 266 | free(dir); |
yusukes | 059e0bd | 2018-03-05 10:22:16 -0800 | [diff] [blame] | 267 | return -rc; |
Mike Frysinger | 5fdba4e | 2018-01-17 15:39:48 -0500 | [diff] [blame] | 268 | } |
| 269 | *p = '/'; |
| 270 | p = strchr(p + 1, '/'); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Create the last directory. We still check EEXIST here in case |
| 275 | * of trailing slashes. |
| 276 | */ |
| 277 | free(dir); |
yusukes | 059e0bd | 2018-03-05 10:22:16 -0800 | [diff] [blame] | 278 | if (isdir && mkdir(path, mode) && errno != EEXIST) { |
| 279 | rc = errno; |
| 280 | pwarn("mkdir(%s, 0%o) failed", path, mode); |
| 281 | return -rc; |
| 282 | } |
Mike Frysinger | 5fdba4e | 2018-01-17 15:39:48 -0500 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* |
Jorge Lucangeli Obes | 3ce72e0 | 2022-06-07 19:41:11 -0400 | [diff] [blame] | 287 | * get_mount_flags: Obtain the mount flags of the mount where |source| lives. |
| 288 | */ |
| 289 | int get_mount_flags(const char *source, unsigned long *mnt_flags) |
| 290 | { |
| 291 | if (mnt_flags) { |
| 292 | struct statvfs stvfs_buf; |
| 293 | int rc = statvfs(source, &stvfs_buf); |
| 294 | if (rc) { |
| 295 | rc = errno; |
| 296 | pwarn("failed to look up mount flags: source=%s", |
| 297 | source); |
| 298 | return -rc; |
| 299 | } |
| 300 | *mnt_flags = stvfs_buf.f_flag; |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /* |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 306 | * setup_mount_destination: Ensures the mount target exists. |
| 307 | * Creates it if needed and possible. |
| 308 | */ |
| 309 | int setup_mount_destination(const char *source, const char *dest, uid_t uid, |
Jorge Lucangeli Obes | 3ce72e0 | 2022-06-07 19:41:11 -0400 | [diff] [blame] | 310 | uid_t gid, bool bind) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 311 | { |
| 312 | int rc; |
| 313 | struct stat st_buf; |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 314 | bool domkdir; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 315 | |
| 316 | rc = stat(dest, &st_buf); |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 317 | if (rc == 0) /* destination exists */ |
Jorge Lucangeli Obes | b4b7c5a | 2019-09-09 10:47:36 -0400 | [diff] [blame] | 318 | return 0; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 319 | |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 320 | /* |
| 321 | * Try to create the destination. |
| 322 | * Either make a directory or touch a file depending on the source type. |
| 323 | * |
| 324 | * If the source isn't an absolute path, assume it is a filesystem type |
| 325 | * such as "tmpfs" and create a directory to mount it on. The dest will |
| 326 | * be something like "none" or "proc" which we shouldn't be checking. |
| 327 | */ |
| 328 | if (source[0] == '/') { |
| 329 | /* The source is an absolute path -- it better exist! */ |
| 330 | rc = stat(source, &st_buf); |
| 331 | if (rc) { |
Jorge Lucangeli Obes | 9299cae | 2019-08-23 11:28:39 -0400 | [diff] [blame] | 332 | rc = errno; |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 333 | pwarn("stat(%s) failed", source); |
Jorge Lucangeli Obes | 9299cae | 2019-08-23 11:28:39 -0400 | [diff] [blame] | 334 | return -rc; |
| 335 | } |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 336 | |
| 337 | /* |
| 338 | * If bind mounting, we only create a directory if the source |
| 339 | * is a directory, else we always bind mount it as a file to |
| 340 | * support device nodes, sockets, etc... |
| 341 | * |
| 342 | * For all other mounts, we assume a block/char source is |
| 343 | * going to want a directory to mount to. If the source is |
| 344 | * something else (e.g. a fifo or socket), this probably will |
| 345 | * not do the right thing, but we'll fail later on when we try |
| 346 | * to mount(), so shouldn't be a big deal. |
| 347 | */ |
| 348 | domkdir = S_ISDIR(st_buf.st_mode) || |
| 349 | (!bind && (S_ISBLK(st_buf.st_mode) || |
| 350 | S_ISCHR(st_buf.st_mode))); |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 351 | } else { |
| 352 | /* The source is a relative path -- assume it's a pseudo fs. */ |
| 353 | |
| 354 | /* Disallow relative bind mounts. */ |
| 355 | if (bind) { |
| 356 | warn("relative bind-mounts are not allowed: source=%s", |
| 357 | source); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | |
| 361 | domkdir = true; |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Mike Frysinger | 5fdba4e | 2018-01-17 15:39:48 -0500 | [diff] [blame] | 364 | /* |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 365 | * Now that we know what we want to do, do it! |
| 366 | * We always create the intermediate dirs and the final path with 0755 |
| 367 | * perms and root/root ownership. This shouldn't be a problem because |
| 368 | * the actual mount will set those perms/ownership on the mount point |
| 369 | * which is all people should need to access it. |
Mike Frysinger | 5fdba4e | 2018-01-17 15:39:48 -0500 | [diff] [blame] | 370 | */ |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 371 | rc = mkdir_p(dest, 0755, domkdir); |
| 372 | if (rc) |
| 373 | return rc; |
| 374 | if (!domkdir) { |
Jorge Lucangeli Obes | 3ce72e0 | 2022-06-07 19:41:11 -0400 | [diff] [blame] | 375 | attribute_cleanup_fd int fd = |
| 376 | open(dest, O_RDWR | O_CREAT | O_CLOEXEC, 0700); |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 377 | if (fd < 0) { |
| 378 | rc = errno; |
| 379 | pwarn("open(%s) failed", dest); |
| 380 | return -rc; |
| 381 | } |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 382 | } |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 383 | if (chown(dest, uid, gid)) { |
| 384 | rc = errno; |
| 385 | pwarn("chown(%s, %u, %u) failed", dest, uid, gid); |
| 386 | return -rc; |
| 387 | } |
Jorge Lucangeli Obes | b4b7c5a | 2019-09-09 10:47:36 -0400 | [diff] [blame] | 388 | return 0; |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 389 | } |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 390 | |
| 391 | /* |
| 392 | * lookup_user: Gets the uid/gid for the given username. |
| 393 | */ |
| 394 | int lookup_user(const char *user, uid_t *uid, gid_t *gid) |
| 395 | { |
| 396 | char *buf = NULL; |
| 397 | struct passwd pw; |
| 398 | struct passwd *ppw = NULL; |
Stéphane Lesimple | 84ffabc | 2020-02-14 20:33:34 +0100 | [diff] [blame] | 399 | /* |
| 400 | * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return |
| 401 | * a suggested starting size for the buffer, so let's try getting this |
| 402 | * size first, and fallback to a default othersise. |
| 403 | */ |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 404 | ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 405 | if (sz == -1) |
| 406 | sz = 65536; /* your guess is as good as mine... */ |
| 407 | |
Stéphane Lesimple | 84ffabc | 2020-02-14 20:33:34 +0100 | [diff] [blame] | 408 | do { |
| 409 | buf = malloc(sz); |
| 410 | if (!buf) |
| 411 | return -ENOMEM; |
| 412 | int err = getpwnam_r(user, &pw, buf, sz, &ppw); |
| 413 | /* |
| 414 | * We're safe to free the buffer here. The strings inside |pw| |
| 415 | * point inside |buf|, but we don't use any of them; this leaves |
| 416 | * the pointers dangling but it's safe. |
| 417 | * |ppw| points at |pw| if getpwnam_r(3) succeeded. |
| 418 | */ |
| 419 | free(buf); |
| 420 | if (err == ERANGE) { |
| 421 | /* |buf| was too small, retry with a bigger one. */ |
| 422 | sz <<= 1; |
| 423 | } else if (err != 0) { |
| 424 | /* We got an error not related to the size of |buf|. */ |
| 425 | return -err; |
| 426 | } else if (!ppw) { |
| 427 | /* Not found. */ |
| 428 | return -ENOENT; |
| 429 | } else { |
| 430 | *uid = ppw->pw_uid; |
| 431 | *gid = ppw->pw_gid; |
| 432 | return 0; |
| 433 | } |
| 434 | } while (sz <= MAX_PWENT_SZ); |
Mattias Nissler | 160d58f | 2020-02-25 11:01:30 +0100 | [diff] [blame] | 435 | |
Stéphane Lesimple | 84ffabc | 2020-02-14 20:33:34 +0100 | [diff] [blame] | 436 | /* A buffer of size MAX_PWENT_SZ is still too small, return an error. */ |
| 437 | return -ERANGE; |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* |
| 441 | * lookup_group: Gets the gid for the given group name. |
| 442 | */ |
| 443 | int lookup_group(const char *group, gid_t *gid) |
| 444 | { |
| 445 | char *buf = NULL; |
| 446 | struct group gr; |
| 447 | struct group *pgr = NULL; |
Stéphane Lesimple | 84ffabc | 2020-02-14 20:33:34 +0100 | [diff] [blame] | 448 | /* |
| 449 | * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return |
| 450 | * a suggested starting size for the buffer, so let's try getting this |
| 451 | * size first, and fallback to a default otherwise. |
| 452 | */ |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 453 | ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 454 | if (sz == -1) |
| 455 | sz = 65536; /* and mine is as good as yours, really */ |
| 456 | |
Stéphane Lesimple | 84ffabc | 2020-02-14 20:33:34 +0100 | [diff] [blame] | 457 | do { |
| 458 | buf = malloc(sz); |
| 459 | if (!buf) |
| 460 | return -ENOMEM; |
| 461 | int err = getgrnam_r(group, &gr, buf, sz, &pgr); |
| 462 | /* |
| 463 | * We're safe to free the buffer here. The strings inside |gr| |
| 464 | * point inside |buf|, but we don't use any of them; this leaves |
| 465 | * the pointers dangling but it's safe. |
| 466 | * |pgr| points at |gr| if getgrnam_r(3) succeeded. |
| 467 | */ |
| 468 | free(buf); |
| 469 | if (err == ERANGE) { |
| 470 | /* |buf| was too small, retry with a bigger one. */ |
| 471 | sz <<= 1; |
| 472 | } else if (err != 0) { |
| 473 | /* We got an error not related to the size of |buf|. */ |
| 474 | return -err; |
| 475 | } else if (!pgr) { |
| 476 | /* Not found. */ |
| 477 | return -ENOENT; |
| 478 | } else { |
| 479 | *gid = pgr->gr_gid; |
| 480 | return 0; |
| 481 | } |
| 482 | } while (sz <= MAX_GRENT_SZ); |
Mattias Nissler | 160d58f | 2020-02-25 11:01:30 +0100 | [diff] [blame] | 483 | |
Stéphane Lesimple | 84ffabc | 2020-02-14 20:33:34 +0100 | [diff] [blame] | 484 | /* A buffer of size MAX_GRENT_SZ is still too small, return an error. */ |
| 485 | return -ERANGE; |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 486 | } |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 487 | |
Luis Héctor Chávez | 4baeafa | 2021-01-03 05:47:13 -0800 | [diff] [blame] | 488 | static bool seccomp_action_is_available(const char *wanted) |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 489 | { |
| 490 | if (is_android()) { |
| 491 | /* |
| 492 | * Accessing |actions_avail| is generating SELinux denials, so |
| 493 | * skip for now. |
| 494 | * TODO(crbug.com/978022, jorgelo): Remove once the denial is |
| 495 | * fixed. |
| 496 | */ |
Luis Héctor Chávez | 4baeafa | 2021-01-03 05:47:13 -0800 | [diff] [blame] | 497 | return false; |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 498 | } |
| 499 | const char actions_avail_path[] = |
| 500 | "/proc/sys/kernel/seccomp/actions_avail"; |
| 501 | FILE *f = fopen(actions_avail_path, "re"); |
| 502 | |
| 503 | if (!f) { |
| 504 | pwarn("fopen(%s) failed", actions_avail_path); |
Luis Héctor Chávez | 4baeafa | 2021-01-03 05:47:13 -0800 | [diff] [blame] | 505 | return false; |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Mike Frysinger | e933fce | 2021-10-02 01:28:06 -0400 | [diff] [blame] | 508 | attribute_cleanup_str char *actions_avail = NULL; |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 509 | size_t buf_size = 0; |
| 510 | if (getline(&actions_avail, &buf_size, f) < 0) { |
| 511 | pwarn("getline() failed"); |
Luis Héctor Chávez | 4baeafa | 2021-01-03 05:47:13 -0800 | [diff] [blame] | 512 | return false; |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* |
| 516 | * This is just substring search, which means that partial matches will |
| 517 | * match too (e.g. "action" would match "longaction"). There are no |
| 518 | * seccomp actions which include other actions though, so we're good for |
| 519 | * now. Eventually we might want to split the string by spaces. |
| 520 | */ |
Mike Frysinger | e933fce | 2021-10-02 01:28:06 -0400 | [diff] [blame] | 521 | return strstr(actions_avail, wanted) != NULL; |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | int seccomp_ret_log_available(void) |
| 525 | { |
| 526 | static int ret_log_available = -1; |
| 527 | |
| 528 | if (ret_log_available == -1) |
| 529 | ret_log_available = seccomp_action_is_available("log"); |
| 530 | |
| 531 | return ret_log_available; |
| 532 | } |
| 533 | |
| 534 | int seccomp_ret_kill_process_available(void) |
| 535 | { |
| 536 | static int ret_kill_process_available = -1; |
| 537 | |
| 538 | if (ret_kill_process_available == -1) |
| 539 | ret_kill_process_available = |
| 540 | seccomp_action_is_available("kill_process"); |
| 541 | |
| 542 | return ret_kill_process_available; |
| 543 | } |
Luis Héctor Chávez | 01b628c | 2021-01-03 05:46:57 -0800 | [diff] [blame] | 544 | |
| 545 | bool seccomp_filter_flags_available(unsigned int flags) |
| 546 | { |
| 547 | return sys_seccomp(SECCOMP_SET_MODE_FILTER, flags, NULL) != -1 || |
| 548 | errno != EINVAL; |
| 549 | } |
Jorge Lucangeli Obes | a8eef8b | 2022-07-20 19:20:06 -0400 | [diff] [blame] | 550 | |
| 551 | bool is_canonical_path(const char *path) |
| 552 | { |
| 553 | attribute_cleanup_str char *rp = realpath(path, NULL); |
Jorge Lucangeli Obes | b5464b7 | 2022-08-09 21:22:40 +0000 | [diff] [blame] | 554 | if (!rp) { |
| 555 | return false; |
| 556 | } |
| 557 | |
| 558 | if (streq(path, rp)) { |
| 559 | return true; |
| 560 | } |
| 561 | |
| 562 | size_t path_len = strlen(path); |
| 563 | size_t rp_len = strlen(rp); |
| 564 | /* If |path| has a single trailing slash, that's OK. */ |
| 565 | return path_len == rp_len + 1 && strncmp(path, rp, rp_len) == 0 && |
| 566 | path[path_len - 1] == '/'; |
Jorge Lucangeli Obes | a8eef8b | 2022-07-20 19:20:06 -0400 | [diff] [blame] | 567 | } |