blob: 434980aa35821cb389b10873996393ce86ad01c4 [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
23#include "util.h"
24
25#ifdef HAVE_SECUREBITS_H
26#include <linux/securebits.h>
27#else
28#define SECURE_ALL_BITS 0x55
29#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
30#endif
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040031
32#define SECURE_BITS_NO_AMBIENT 0x15
33#define SECURE_LOCKS_NO_AMBIENT (SECURE_BITS_NO_AMBIENT << 1)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040034
35/*
36 * Assert the value of SECURE_ALL_BITS at compile-time.
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040037 * Android devices are currently compiled against 4.4 kernel headers. Kernel 4.3
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040038 * added a new securebit.
39 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
40 * when used on older kernels. The compile-time assert will catch this situation
41 * at compile time.
42 */
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040043#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040044_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
45#endif
46
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -040047int secure_noroot_set_and_locked(uint64_t mask)
48{
49 return (mask & (SECBIT_NOROOT | SECBIT_NOROOT_LOCKED)) ==
50 (SECBIT_NOROOT | SECBIT_NOROOT_LOCKED);
51}
52
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -070053int lock_securebits(uint64_t skip_mask)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040054{
55 /*
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040056 * Ambient capabilities can only be raised if they're already present
57 * in the permitted *and* inheritable set. Therefore, we don't really
58 * need to lock the NO_CAP_AMBIENT_RAISE securebit, since we are already
59 * configuring the permitted and inheritable set.
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040060 */
Dylan Reida7f4fc92017-07-13 18:45:23 -070061 unsigned long securebits =
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -070062 (SECURE_BITS_NO_AMBIENT | SECURE_LOCKS_NO_AMBIENT) & ~skip_mask;
63 if (!securebits) {
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -040064 warn("not locking any securebits");
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -070065 return 0;
66 }
67 int securebits_ret = prctl(PR_SET_SECUREBITS, securebits);
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040068 if (securebits_ret < 0) {
69 pwarn("prctl(PR_SET_SECUREBITS) failed");
70 return -1;
71 }
72
73 return 0;
74}
75
76int write_proc_file(pid_t pid, const char *content, const char *basename)
77{
78 int fd, ret;
79 size_t sz, len;
80 ssize_t written;
81 char filename[32];
82
83 sz = sizeof(filename);
84 ret = snprintf(filename, sz, "/proc/%d/%s", pid, basename);
85 if (ret < 0 || (size_t)ret >= sz) {
86 warn("failed to generate %s filename", basename);
87 return -1;
88 }
89
90 fd = open(filename, O_WRONLY | O_CLOEXEC);
91 if (fd < 0) {
92 pwarn("failed to open '%s'", filename);
93 return -errno;
94 }
95
96 len = strlen(content);
97 written = write(fd, content, len);
98 if (written < 0) {
99 pwarn("failed to write '%s'", filename);
100 return -1;
101 }
102
103 if ((size_t)written < len) {
104 warn("failed to write %zu bytes to '%s'", len, filename);
105 return -1;
106 }
107 close(fd);
108 return 0;
109}
110
111/*
112 * We specifically do not use cap_valid() as that only tells us the last
113 * valid cap we were *compiled* against (i.e. what the version of kernel
114 * headers says). If we run on a different kernel version, then it's not
115 * uncommon for that to be less (if an older kernel) or more (if a newer
116 * kernel).
117 * Normally, we suck up the answer via /proc. On Android, not all processes are
118 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
119 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
120 */
121unsigned int get_last_valid_cap(void)
122{
123 unsigned int last_valid_cap = 0;
124 if (is_android()) {
125 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
126 ++last_valid_cap)
127 ;
128
129 /* |last_valid_cap| will be the first failing value. */
130 if (last_valid_cap > 0) {
131 last_valid_cap--;
132 }
133 } else {
134 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
135 FILE *fp = fopen(cap_file, "re");
136 if (fscanf(fp, "%u", &last_valid_cap) != 1)
137 pdie("fscanf(%s)", cap_file);
138 fclose(fp);
139 }
140 return last_valid_cap;
141}
142
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400143int cap_ambient_supported(void)
144{
145 return prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0) >=
146 0;
147}
148
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400149int config_net_loopback(void)
150{
151 const char ifname[] = "lo";
152 int sock;
153 struct ifreq ifr;
154
155 /* Make sure people don't try to add really long names. */
156 _Static_assert(sizeof(ifname) <= IFNAMSIZ, "interface name too long");
157
158 sock = socket(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
159 if (sock < 0) {
160 pwarn("socket(AF_LOCAL) failed");
161 return -1;
162 }
163
164 /*
165 * Do the equiv of `ip link set up lo`. The kernel will assign
166 * IPv4 (127.0.0.1) & IPv6 (::1) addresses automatically!
167 */
168 strcpy(ifr.ifr_name, ifname);
169 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
170 pwarn("ioctl(SIOCGIFFLAGS) failed");
171 return -1;
172 }
173
174 /* The kernel preserves ifr.ifr_name for use. */
175 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
176 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
177 pwarn("ioctl(SIOCSIFFLAGS) failed");
178 return -1;
179 }
180
181 close(sock);
182 return 0;
183}
184
185int setup_pipe_end(int fds[2], size_t index)
186{
187 if (index > 1)
188 return -1;
189
190 close(fds[1 - index]);
191 return fds[index];
192}
193
194int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
195{
196 if (index > 1)
197 return -1;
198
199 close(fds[1 - index]);
200 /* dup2(2) the corresponding end of the pipe into |fd|. */
201 return dup2(fds[index], fd);
202}
203
204int write_pid_to_path(pid_t pid, const char *path)
205{
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400206 FILE *fp = fopen(path, "we");
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400207
208 if (!fp) {
209 pwarn("failed to open '%s'", path);
210 return -errno;
211 }
212 if (fprintf(fp, "%d\n", (int)pid) < 0) {
213 /* fprintf(3) does not set errno on failure. */
214 warn("fprintf(%s) failed", path);
215 return -1;
216 }
217 if (fclose(fp)) {
218 pwarn("fclose(%s) failed", path);
219 return -errno;
220 }
221
222 return 0;
223}
224
225/*
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500226 * Create the |path| directory and its parents (if need be) with |mode|.
227 * If not |isdir|, then |path| is actually a file, so the last component
228 * will not be created.
229 */
230int mkdir_p(const char *path, mode_t mode, bool isdir)
231{
yusukes059e0bd2018-03-05 10:22:16 -0800232 int rc;
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500233 char *dir = strdup(path);
yusukes059e0bd2018-03-05 10:22:16 -0800234 if (!dir) {
235 rc = errno;
236 pwarn("strdup(%s) failed", path);
237 return -rc;
238 }
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500239
240 /* Starting from the root, work our way out to the end. */
241 char *p = strchr(dir + 1, '/');
242 while (p) {
243 *p = '\0';
244 if (mkdir(dir, mode) && errno != EEXIST) {
yusukes059e0bd2018-03-05 10:22:16 -0800245 rc = errno;
246 pwarn("mkdir(%s, 0%o) failed", dir, mode);
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500247 free(dir);
yusukes059e0bd2018-03-05 10:22:16 -0800248 return -rc;
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500249 }
250 *p = '/';
251 p = strchr(p + 1, '/');
252 }
253
254 /*
255 * Create the last directory. We still check EEXIST here in case
256 * of trailing slashes.
257 */
258 free(dir);
yusukes059e0bd2018-03-05 10:22:16 -0800259 if (isdir && mkdir(path, mode) && errno != EEXIST) {
260 rc = errno;
261 pwarn("mkdir(%s, 0%o) failed", path, mode);
262 return -rc;
263 }
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500264 return 0;
265}
266
267/*
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400268 * setup_mount_destination: Ensures the mount target exists.
269 * Creates it if needed and possible.
270 */
271int setup_mount_destination(const char *source, const char *dest, uid_t uid,
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700272 uid_t gid, bool bind, unsigned long *mnt_flags)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400273{
274 int rc;
275 struct stat st_buf;
Mike Frysingereaab4202017-08-14 14:57:21 -0400276 bool domkdir;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400277
278 rc = stat(dest, &st_buf);
279 if (rc == 0) /* destination exists */
280 return 0;
281
282 /*
283 * Try to create the destination.
284 * Either make a directory or touch a file depending on the source type.
Mike Frysingereaab4202017-08-14 14:57:21 -0400285 *
286 * If the source isn't an absolute path, assume it is a filesystem type
287 * such as "tmpfs" and create a directory to mount it on. The dest will
288 * be something like "none" or "proc" which we shouldn't be checking.
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400289 */
Mike Frysingereaab4202017-08-14 14:57:21 -0400290 if (source[0] == '/') {
291 /* The source is an absolute path -- it better exist! */
292 rc = stat(source, &st_buf);
yusukes059e0bd2018-03-05 10:22:16 -0800293 if (rc) {
294 rc = errno;
295 pwarn("stat(%s) failed", source);
296 return -rc;
297 }
Mike Frysingereaab4202017-08-14 14:57:21 -0400298
299 /*
300 * If bind mounting, we only create a directory if the source
301 * is a directory, else we always bind mount it as a file to
302 * support device nodes, sockets, etc...
303 *
304 * For all other mounts, we assume a block/char source is
305 * going to want a directory to mount to. If the source is
306 * something else (e.g. a fifo or socket), this probably will
307 * not do the right thing, but we'll fail later on when we try
308 * to mount(), so shouldn't be a big deal.
309 */
310 domkdir = S_ISDIR(st_buf.st_mode) ||
311 (!bind && (S_ISBLK(st_buf.st_mode) ||
312 S_ISCHR(st_buf.st_mode)));
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700313
314 /* If bind mounting, also grab the mount flags of the source. */
315 if (bind && mnt_flags) {
316 struct statvfs stvfs_buf;
317 rc = statvfs(source, &stvfs_buf);
318 if (rc) {
319 rc = errno;
320 pwarn(
321 "failed to look up mount flags: source=%s",
322 source);
323 return -rc;
324 }
325 *mnt_flags = stvfs_buf.f_flag;
326 }
Mike Frysingereaab4202017-08-14 14:57:21 -0400327 } else {
328 /* The source is a relative path -- assume it's a pseudo fs. */
329
330 /* Disallow relative bind mounts. */
yusukes059e0bd2018-03-05 10:22:16 -0800331 if (bind) {
332 warn("relative bind-mounts are not allowed: source=%s",
333 source);
Mike Frysingereaab4202017-08-14 14:57:21 -0400334 return -EINVAL;
yusukes059e0bd2018-03-05 10:22:16 -0800335 }
Mike Frysingereaab4202017-08-14 14:57:21 -0400336
337 domkdir = true;
338 }
339
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500340 /*
341 * Now that we know what we want to do, do it!
342 * We always create the intermediate dirs and the final path with 0755
343 * perms and root/root ownership. This shouldn't be a problem because
344 * the actual mount will set those perms/ownership on the mount point
345 * which is all people should need to access it.
346 */
yusukes059e0bd2018-03-05 10:22:16 -0800347 rc = mkdir_p(dest, 0755, domkdir);
348 if (rc)
349 return rc;
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500350 if (!domkdir) {
Mike Frysingereaab4202017-08-14 14:57:21 -0400351 int fd = open(dest, O_RDWR | O_CREAT | O_CLOEXEC, 0700);
yusukes059e0bd2018-03-05 10:22:16 -0800352 if (fd < 0) {
353 rc = errno;
354 pwarn("open(%s) failed", dest);
355 return -rc;
356 }
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400357 close(fd);
358 }
yusukes059e0bd2018-03-05 10:22:16 -0800359 if (chown(dest, uid, gid)) {
360 rc = errno;
361 pwarn("chown(%s, %u, %u) failed", dest, uid, gid);
362 return -rc;
363 }
yusukes76a9d742018-03-05 10:20:22 -0800364 return 0;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400365}
Luis Hector Chavez71323552017-09-05 09:17:22 -0700366
367/*
368 * lookup_user: Gets the uid/gid for the given username.
369 */
370int lookup_user(const char *user, uid_t *uid, gid_t *gid)
371{
372 char *buf = NULL;
373 struct passwd pw;
374 struct passwd *ppw = NULL;
375 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
376 if (sz == -1)
377 sz = 65536; /* your guess is as good as mine... */
378
379 /*
380 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
381 * the maximum needed size of the buffer, so we don't have to search.
382 */
383 buf = malloc(sz);
384 if (!buf)
385 return -ENOMEM;
386 getpwnam_r(user, &pw, buf, sz, &ppw);
387 /*
388 * We're safe to free the buffer here. The strings inside |pw| point
389 * inside |buf|, but we don't use any of them; this leaves the pointers
390 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
391 * succeeded.
392 */
393 free(buf);
394 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
395 if (!ppw)
396 return -1;
397
398 *uid = ppw->pw_uid;
399 *gid = ppw->pw_gid;
400 return 0;
401}
402
403/*
404 * lookup_group: Gets the gid for the given group name.
405 */
406int lookup_group(const char *group, gid_t *gid)
407{
408 char *buf = NULL;
409 struct group gr;
410 struct group *pgr = NULL;
411 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
412 if (sz == -1)
413 sz = 65536; /* and mine is as good as yours, really */
414
415 /*
416 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
417 * the maximum needed size of the buffer, so we don't have to search.
418 */
419 buf = malloc(sz);
420 if (!buf)
421 return -ENOMEM;
422 getgrnam_r(group, &gr, buf, sz, &pgr);
423 /*
424 * We're safe to free the buffer here. The strings inside gr point
425 * inside buf, but we don't use any of them; this leaves the pointers
426 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
427 */
428 free(buf);
429 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
430 if (!pgr)
431 return -1;
432
433 *gid = pgr->gr_gid;
434 return 0;
435}