blob: 5d39f0fa654af1c118fe73985385eaed0ddb8c52 [file] [log] [blame]
Mike Frysinger50e31fa2018-01-19 18:59:49 -05001/* Copyright 2017 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04004 */
5
6#include "system.h"
7
8#include <errno.h>
9#include <fcntl.h>
Luis Hector Chavez71323552017-09-05 09:17:22 -070010#include <grp.h>
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040011#include <net/if.h>
Luis Hector Chavez71323552017-09-05 09:17:22 -070012#include <pwd.h>
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040013#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 Chavez0bacbf82018-07-10 20:06:55 -070020#include <sys/statvfs.h>
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040021#include <unistd.h>
22
Mattias Nisslere5200192018-10-18 12:29:40 +020023#include <linux/securebits.h>
24
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040025#include "util.h"
26
Mattias Nisslere5200192018-10-18 12:29:40 +020027/*
28 * SECBIT_NO_CAP_AMBIENT_RAISE was added in kernel 4.3, so fill in the
29 * definition if the securebits header doesn't provide it.
30 */
31#ifndef SECBIT_NO_CAP_AMBIENT_RAISE
32#define SECBIT_NO_CAP_AMBIENT_RAISE (issecure_mask(6))
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040033#endif
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040034
Mattias Nisslere5200192018-10-18 12:29:40 +020035#ifndef SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED
36#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED (issecure_mask(7))
37#endif
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040038
39/*
40 * Assert the value of SECURE_ALL_BITS at compile-time.
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040041 * Android devices are currently compiled against 4.4 kernel headers. Kernel 4.3
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040042 * added a new securebit.
43 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
44 * when used on older kernels. The compile-time assert will catch this situation
45 * at compile time.
46 */
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040047#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040048_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
49#endif
50
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -040051int secure_noroot_set_and_locked(uint64_t mask)
52{
53 return (mask & (SECBIT_NOROOT | SECBIT_NOROOT_LOCKED)) ==
54 (SECBIT_NOROOT | SECBIT_NOROOT_LOCKED);
55}
56
Mattias Nissler48b5ff12018-10-11 15:31:41 +020057int lock_securebits(uint64_t skip_mask, bool require_keep_caps)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040058{
Mattias Nissler48b5ff12018-10-11 15:31:41 +020059 /* The general idea is to set all bits, subject to exceptions below. */
60 unsigned long securebits = SECURE_ALL_BITS | SECURE_ALL_LOCKS;
61
62 /*
63 * SECBIT_KEEP_CAPS is special in that it is automatically cleared on
64 * execve(2). This implies that attempts to set SECBIT_KEEP_CAPS (as is
65 * the default) in processes that have it locked already (such as nested
66 * minijail usage) would fail. Thus, unless the caller requires it,
67 * allow it to remain off if it is already locked.
68 */
69 if (!require_keep_caps) {
70 int current_securebits = prctl(PR_GET_SECUREBITS);
71 if (current_securebits < 0) {
72 pwarn("prctl(PR_GET_SECUREBITS) failed");
73 return -1;
74 }
75
76 if ((current_securebits & SECBIT_KEEP_CAPS_LOCKED) != 0 &&
77 (current_securebits & SECBIT_KEEP_CAPS) == 0) {
78 securebits &= ~SECBIT_KEEP_CAPS;
79 }
80 }
81
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040082 /*
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040083 * Ambient capabilities can only be raised if they're already present
84 * in the permitted *and* inheritable set. Therefore, we don't really
85 * need to lock the NO_CAP_AMBIENT_RAISE securebit, since we are already
86 * configuring the permitted and inheritable set.
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040087 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +020088 securebits &=
89 ~(SECBIT_NO_CAP_AMBIENT_RAISE | SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED);
90
91 /* Don't set any bits that the user requested not to be touched. */
92 securebits &= ~skip_mask;
93
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -070094 if (!securebits) {
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -040095 warn("not locking any securebits");
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -070096 return 0;
97 }
98 int securebits_ret = prctl(PR_SET_SECUREBITS, securebits);
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040099 if (securebits_ret < 0) {
100 pwarn("prctl(PR_SET_SECUREBITS) failed");
101 return -1;
102 }
103
104 return 0;
105}
106
107int write_proc_file(pid_t pid, const char *content, const char *basename)
108{
109 int fd, ret;
110 size_t sz, len;
111 ssize_t written;
112 char filename[32];
113
114 sz = sizeof(filename);
115 ret = snprintf(filename, sz, "/proc/%d/%s", pid, basename);
116 if (ret < 0 || (size_t)ret >= sz) {
117 warn("failed to generate %s filename", basename);
118 return -1;
119 }
120
121 fd = open(filename, O_WRONLY | O_CLOEXEC);
122 if (fd < 0) {
123 pwarn("failed to open '%s'", filename);
124 return -errno;
125 }
126
127 len = strlen(content);
128 written = write(fd, content, len);
129 if (written < 0) {
130 pwarn("failed to write '%s'", filename);
Jorge Lucangeli Obes673c89d2018-10-04 16:08:10 -0400131 return -errno;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400132 }
133
134 if ((size_t)written < len) {
135 warn("failed to write %zu bytes to '%s'", len, filename);
136 return -1;
137 }
138 close(fd);
139 return 0;
140}
141
142/*
143 * We specifically do not use cap_valid() as that only tells us the last
144 * valid cap we were *compiled* against (i.e. what the version of kernel
145 * headers says). If we run on a different kernel version, then it's not
146 * uncommon for that to be less (if an older kernel) or more (if a newer
147 * kernel).
148 * Normally, we suck up the answer via /proc. On Android, not all processes are
149 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
150 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
151 */
152unsigned int get_last_valid_cap(void)
153{
154 unsigned int last_valid_cap = 0;
155 if (is_android()) {
156 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
157 ++last_valid_cap)
158 ;
159
160 /* |last_valid_cap| will be the first failing value. */
161 if (last_valid_cap > 0) {
162 last_valid_cap--;
163 }
164 } else {
165 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
166 FILE *fp = fopen(cap_file, "re");
167 if (fscanf(fp, "%u", &last_valid_cap) != 1)
168 pdie("fscanf(%s)", cap_file);
169 fclose(fp);
170 }
171 return last_valid_cap;
172}
173
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400174int cap_ambient_supported(void)
175{
176 return prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0) >=
177 0;
178}
179
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400180int config_net_loopback(void)
181{
182 const char ifname[] = "lo";
183 int sock;
184 struct ifreq ifr;
185
186 /* Make sure people don't try to add really long names. */
187 _Static_assert(sizeof(ifname) <= IFNAMSIZ, "interface name too long");
188
189 sock = socket(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
190 if (sock < 0) {
191 pwarn("socket(AF_LOCAL) failed");
192 return -1;
193 }
194
195 /*
196 * Do the equiv of `ip link set up lo`. The kernel will assign
197 * IPv4 (127.0.0.1) & IPv6 (::1) addresses automatically!
198 */
199 strcpy(ifr.ifr_name, ifname);
200 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
201 pwarn("ioctl(SIOCGIFFLAGS) failed");
202 return -1;
203 }
204
205 /* The kernel preserves ifr.ifr_name for use. */
206 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
207 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
208 pwarn("ioctl(SIOCSIFFLAGS) failed");
209 return -1;
210 }
211
212 close(sock);
213 return 0;
214}
215
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400216int write_pid_to_path(pid_t pid, const char *path)
217{
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400218 FILE *fp = fopen(path, "we");
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400219
220 if (!fp) {
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400221 pwarn("failed to open '%s'", path);
222 return -errno;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400223 }
224 if (fprintf(fp, "%d\n", (int)pid) < 0) {
225 /* fprintf(3) does not set errno on failure. */
226 warn("fprintf(%s) failed", path);
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400227 return -1;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400228 }
229 if (fclose(fp)) {
230 pwarn("fclose(%s) failed", path);
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400231 return -errno;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400232 }
233
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400234 return 0;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400235}
236
237/*
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500238 * Create the |path| directory and its parents (if need be) with |mode|.
239 * If not |isdir|, then |path| is actually a file, so the last component
240 * will not be created.
241 */
242int mkdir_p(const char *path, mode_t mode, bool isdir)
243{
yusukes059e0bd2018-03-05 10:22:16 -0800244 int rc;
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500245 char *dir = strdup(path);
yusukes059e0bd2018-03-05 10:22:16 -0800246 if (!dir) {
247 rc = errno;
248 pwarn("strdup(%s) failed", path);
249 return -rc;
250 }
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500251
252 /* Starting from the root, work our way out to the end. */
253 char *p = strchr(dir + 1, '/');
254 while (p) {
255 *p = '\0';
256 if (mkdir(dir, mode) && errno != EEXIST) {
yusukes059e0bd2018-03-05 10:22:16 -0800257 rc = errno;
258 pwarn("mkdir(%s, 0%o) failed", dir, mode);
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500259 free(dir);
yusukes059e0bd2018-03-05 10:22:16 -0800260 return -rc;
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500261 }
262 *p = '/';
263 p = strchr(p + 1, '/');
264 }
265
266 /*
267 * Create the last directory. We still check EEXIST here in case
268 * of trailing slashes.
269 */
270 free(dir);
yusukes059e0bd2018-03-05 10:22:16 -0800271 if (isdir && mkdir(path, mode) && errno != EEXIST) {
272 rc = errno;
273 pwarn("mkdir(%s, 0%o) failed", path, mode);
274 return -rc;
275 }
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500276 return 0;
277}
278
279/*
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400280 * setup_mount_destination: Ensures the mount target exists.
281 * Creates it if needed and possible.
282 */
283int setup_mount_destination(const char *source, const char *dest, uid_t uid,
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700284 uid_t gid, bool bind, unsigned long *mnt_flags)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400285{
286 int rc;
287 struct stat st_buf;
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400288 bool domkdir;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400289
290 rc = stat(dest, &st_buf);
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400291 if (rc == 0) /* destination exists */
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400292 return 0;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400293
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400294 /*
295 * Try to create the destination.
296 * Either make a directory or touch a file depending on the source type.
297 *
298 * If the source isn't an absolute path, assume it is a filesystem type
299 * such as "tmpfs" and create a directory to mount it on. The dest will
300 * be something like "none" or "proc" which we shouldn't be checking.
301 */
302 if (source[0] == '/') {
303 /* The source is an absolute path -- it better exist! */
304 rc = stat(source, &st_buf);
305 if (rc) {
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -0400306 rc = errno;
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400307 pwarn("stat(%s) failed", source);
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -0400308 return -rc;
309 }
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400310
311 /*
312 * If bind mounting, we only create a directory if the source
313 * is a directory, else we always bind mount it as a file to
314 * support device nodes, sockets, etc...
315 *
316 * For all other mounts, we assume a block/char source is
317 * going to want a directory to mount to. If the source is
318 * something else (e.g. a fifo or socket), this probably will
319 * not do the right thing, but we'll fail later on when we try
320 * to mount(), so shouldn't be a big deal.
321 */
322 domkdir = S_ISDIR(st_buf.st_mode) ||
323 (!bind && (S_ISBLK(st_buf.st_mode) ||
324 S_ISCHR(st_buf.st_mode)));
325
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400326 /* If bind mounting, also grab the mount flags of the source. */
327 if (bind && mnt_flags) {
328 struct statvfs stvfs_buf;
329 rc = statvfs(source, &stvfs_buf);
330 if (rc) {
331 rc = errno;
332 pwarn(
333 "failed to look up mount flags: source=%s",
334 source);
335 return -rc;
336 }
337 *mnt_flags = stvfs_buf.f_flag;
338 }
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400339 } else {
340 /* The source is a relative path -- assume it's a pseudo fs. */
341
342 /* Disallow relative bind mounts. */
343 if (bind) {
344 warn("relative bind-mounts are not allowed: source=%s",
345 source);
346 return -EINVAL;
347 }
348
349 domkdir = true;
Mike Frysingereaab4202017-08-14 14:57:21 -0400350 }
351
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500352 /*
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400353 * Now that we know what we want to do, do it!
354 * We always create the intermediate dirs and the final path with 0755
355 * perms and root/root ownership. This shouldn't be a problem because
356 * the actual mount will set those perms/ownership on the mount point
357 * which is all people should need to access it.
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500358 */
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400359 rc = mkdir_p(dest, 0755, domkdir);
360 if (rc)
361 return rc;
362 if (!domkdir) {
363 int fd = open(dest, O_RDWR | O_CREAT | O_CLOEXEC, 0700);
364 if (fd < 0) {
365 rc = errno;
366 pwarn("open(%s) failed", dest);
367 return -rc;
368 }
369 close(fd);
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400370 }
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400371 if (chown(dest, uid, gid)) {
372 rc = errno;
373 pwarn("chown(%s, %u, %u) failed", dest, uid, gid);
374 return -rc;
375 }
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400376 return 0;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400377}
Luis Hector Chavez71323552017-09-05 09:17:22 -0700378
379/*
380 * lookup_user: Gets the uid/gid for the given username.
381 */
382int lookup_user(const char *user, uid_t *uid, gid_t *gid)
383{
384 char *buf = NULL;
385 struct passwd pw;
386 struct passwd *ppw = NULL;
387 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
388 if (sz == -1)
389 sz = 65536; /* your guess is as good as mine... */
390
391 /*
392 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
393 * the maximum needed size of the buffer, so we don't have to search.
394 */
395 buf = malloc(sz);
396 if (!buf)
397 return -ENOMEM;
398 getpwnam_r(user, &pw, buf, sz, &ppw);
399 /*
400 * We're safe to free the buffer here. The strings inside |pw| point
401 * inside |buf|, but we don't use any of them; this leaves the pointers
402 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
403 * succeeded.
404 */
405 free(buf);
406 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
407 if (!ppw)
408 return -1;
409
410 *uid = ppw->pw_uid;
411 *gid = ppw->pw_gid;
412 return 0;
413}
414
415/*
416 * lookup_group: Gets the gid for the given group name.
417 */
418int lookup_group(const char *group, gid_t *gid)
419{
420 char *buf = NULL;
421 struct group gr;
422 struct group *pgr = NULL;
423 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
424 if (sz == -1)
425 sz = 65536; /* and mine is as good as yours, really */
426
427 /*
428 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
429 * the maximum needed size of the buffer, so we don't have to search.
430 */
431 buf = malloc(sz);
432 if (!buf)
433 return -ENOMEM;
434 getgrnam_r(group, &gr, buf, sz, &pgr);
435 /*
436 * We're safe to free the buffer here. The strings inside gr point
437 * inside buf, but we don't use any of them; this leaves the pointers
438 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
439 */
440 free(buf);
441 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
442 if (!pgr)
443 return -1;
444
445 *gid = pgr->gr_gid;
446 return 0;
447}
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400448
449static int seccomp_action_is_available(const char *wanted)
450{
451 if (is_android()) {
452 /*
453 * Accessing |actions_avail| is generating SELinux denials, so
454 * skip for now.
455 * TODO(crbug.com/978022, jorgelo): Remove once the denial is
456 * fixed.
457 */
458 return 0;
459 }
460 const char actions_avail_path[] =
461 "/proc/sys/kernel/seccomp/actions_avail";
462 FILE *f = fopen(actions_avail_path, "re");
463
464 if (!f) {
465 pwarn("fopen(%s) failed", actions_avail_path);
466 return 0;
467 }
468
469 char *actions_avail = NULL;
470 size_t buf_size = 0;
471 if (getline(&actions_avail, &buf_size, f) < 0) {
472 pwarn("getline() failed");
473 free(actions_avail);
474 return 0;
475 }
476
477 /*
478 * This is just substring search, which means that partial matches will
479 * match too (e.g. "action" would match "longaction"). There are no
480 * seccomp actions which include other actions though, so we're good for
481 * now. Eventually we might want to split the string by spaces.
482 */
483 return strstr(actions_avail, wanted) != NULL;
484}
485
486int seccomp_ret_log_available(void)
487{
488 static int ret_log_available = -1;
489
490 if (ret_log_available == -1)
491 ret_log_available = seccomp_action_is_available("log");
492
493 return ret_log_available;
494}
495
496int seccomp_ret_kill_process_available(void)
497{
498 static int ret_kill_process_available = -1;
499
500 if (ret_kill_process_available == -1)
501 ret_kill_process_available =
502 seccomp_action_is_available("kill_process");
503
504 return ret_kill_process_available;
505}