Jorge Lucangeli Obes | d613ab2 | 2015-03-03 14:22:50 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 3 | * found in the LICENSE file. |
| 4 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 5 | |
| 6 | #define _BSD_SOURCE |
Arthur Gautier | 7a56907 | 2016-04-23 17:25:20 +0000 | [diff] [blame] | 7 | #define _DEFAULT_SOURCE |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 8 | #define _GNU_SOURCE |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 9 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 10 | #include <asm/unistd.h> |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 11 | #include <dirent.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 12 | #include <errno.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 13 | #include <fcntl.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 14 | #include <grp.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 15 | #include <linux/capability.h> |
Luis Hector Chavez | c3e1772 | 2018-10-16 20:43:12 -0700 | [diff] [blame] | 16 | #include <linux/filter.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 17 | #include <sched.h> |
| 18 | #include <signal.h> |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 19 | #include <stdbool.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 20 | #include <stddef.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 24 | #include <sys/capability.h> |
| 25 | #include <sys/mount.h> |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 26 | #include <sys/param.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 27 | #include <sys/prctl.h> |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 28 | #include <sys/resource.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 29 | #include <sys/stat.h> |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 30 | #include <sys/sysmacros.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 31 | #include <sys/types.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 32 | #include <sys/user.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 33 | #include <sys/wait.h> |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 34 | #include <syscall.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 35 | #include <unistd.h> |
| 36 | |
| 37 | #include "libminijail.h" |
| 38 | #include "libminijail-private.h" |
| 39 | |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 40 | #include "signal_handler.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 41 | #include "syscall_filter.h" |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 42 | #include "syscall_wrapper.h" |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 43 | #include "system.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 44 | #include "util.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 45 | |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 46 | /* Until these are reliably available in linux/prctl.h. */ |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 47 | #ifndef PR_ALT_SYSCALL |
| 48 | # define PR_ALT_SYSCALL 0x43724f53 |
| 49 | #endif |
| 50 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 51 | /* Seccomp filter related flags. */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 52 | #ifndef PR_SET_NO_NEW_PRIVS |
| 53 | # define PR_SET_NO_NEW_PRIVS 38 |
| 54 | #endif |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 55 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 56 | #ifndef SECCOMP_MODE_FILTER |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 57 | #define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */ |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 58 | #endif |
| 59 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 60 | #ifndef SECCOMP_SET_MODE_STRICT |
| 61 | # define SECCOMP_SET_MODE_STRICT 0 |
| 62 | #endif |
| 63 | #ifndef SECCOMP_SET_MODE_FILTER |
| 64 | # define SECCOMP_SET_MODE_FILTER 1 |
| 65 | #endif |
| 66 | |
| 67 | #ifndef SECCOMP_FILTER_FLAG_TSYNC |
| 68 | # define SECCOMP_FILTER_FLAG_TSYNC 1 |
| 69 | #endif |
| 70 | /* End seccomp filter related flags. */ |
| 71 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 72 | /* New cgroup namespace might not be in linux-headers yet. */ |
| 73 | #ifndef CLONE_NEWCGROUP |
| 74 | # define CLONE_NEWCGROUP 0x02000000 |
| 75 | #endif |
| 76 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 77 | #define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */ |
| 78 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 79 | #define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */ |
| 80 | |
Stephen Barber | 0d1cbf6 | 2017-10-16 22:19:38 -0700 | [diff] [blame] | 81 | #define MAX_PRESERVED_FDS 32U |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 82 | |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 83 | /* Keyctl commands. */ |
| 84 | #define KEYCTL_JOIN_SESSION_KEYRING 1 |
| 85 | |
Luis Hector Chavez | 0bacbf8 | 2018-07-10 20:06:55 -0700 | [diff] [blame] | 86 | /* |
| 87 | * The userspace equivalent of MNT_USER_SETTABLE_MASK, which is the mask of all |
| 88 | * flags that can be modified by MS_REMOUNT. |
| 89 | */ |
| 90 | #define MS_USER_SETTABLE_MASK \ |
| 91 | (MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_NODIRATIME | \ |
| 92 | MS_RELATIME | MS_RDONLY) |
| 93 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 94 | struct minijail_rlimit { |
| 95 | int type; |
Luis Hector Chavez | 7058a2d | 2018-01-29 08:41:34 -0800 | [diff] [blame] | 96 | rlim_t cur; |
| 97 | rlim_t max; |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 100 | struct mountpoint { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 101 | char *src; |
| 102 | char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 103 | char *type; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 104 | char *data; |
| 105 | int has_data; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 106 | unsigned long flags; |
| 107 | struct mountpoint *next; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 108 | }; |
| 109 | |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 110 | struct hook { |
| 111 | minijail_hook_t hook; |
| 112 | void *payload; |
| 113 | minijail_hook_event_t event; |
| 114 | struct hook *next; |
| 115 | }; |
| 116 | |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 117 | struct preserved_fd { |
| 118 | int parent_fd; |
| 119 | int child_fd; |
| 120 | }; |
| 121 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 122 | struct minijail { |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 123 | /* |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 124 | * WARNING: if you add a flag here you need to make sure it's |
| 125 | * accounted for in minijail_pre{enter|exec}() below. |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 126 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 127 | struct { |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 128 | int uid : 1; |
| 129 | int gid : 1; |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 130 | int inherit_suppl_gids : 1; |
| 131 | int set_suppl_gids : 1; |
| 132 | int keep_suppl_gids : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 133 | int use_caps : 1; |
| 134 | int capbset_drop : 1; |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 135 | int set_ambient_caps : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 136 | int vfs : 1; |
| 137 | int enter_vfs : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 138 | int pids : 1; |
| 139 | int ipc : 1; |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 140 | int uts : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 141 | int net : 1; |
| 142 | int enter_net : 1; |
| 143 | int ns_cgroups : 1; |
| 144 | int userns : 1; |
| 145 | int disable_setgroups : 1; |
| 146 | int seccomp : 1; |
| 147 | int remount_proc_ro : 1; |
| 148 | int no_new_privs : 1; |
| 149 | int seccomp_filter : 1; |
| 150 | int seccomp_filter_tsync : 1; |
| 151 | int seccomp_filter_logging : 1; |
| 152 | int chroot : 1; |
| 153 | int pivot_root : 1; |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 154 | int mount_dev : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 155 | int mount_tmp : 1; |
| 156 | int do_init : 1; |
Luis Hector Chavez | ac981fc | 2017-09-18 15:52:38 -0700 | [diff] [blame] | 157 | int run_as_init : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 158 | int pid_file : 1; |
| 159 | int cgroups : 1; |
| 160 | int alt_syscall : 1; |
| 161 | int reset_signal_mask : 1; |
Luis Hector Chavez | a27118a | 2018-04-04 08:18:01 -0700 | [diff] [blame] | 162 | int reset_signal_handlers : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 163 | int close_open_fds : 1; |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 164 | int new_session_keyring : 1; |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 165 | int forward_signals : 1; |
Xiyuan Xia | 9b41e65 | 2019-05-23 11:03:04 -0700 | [diff] [blame] | 166 | int setsid : 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 167 | } flags; |
| 168 | uid_t uid; |
| 169 | gid_t gid; |
| 170 | gid_t usergid; |
| 171 | char *user; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 172 | size_t suppl_gid_count; |
| 173 | gid_t *suppl_gid_list; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 174 | uint64_t caps; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 175 | uint64_t cap_bset; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 176 | pid_t initpid; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 177 | int mountns_fd; |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 178 | int netns_fd; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 179 | char *chrootdir; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 180 | char *pid_file_path; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 181 | char *uidmap; |
| 182 | char *gidmap; |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 183 | char *hostname; |
Luis Hector Chavez | 9acba45 | 2018-10-11 10:13:25 -0700 | [diff] [blame] | 184 | char *preload_path; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 185 | size_t filter_len; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 186 | struct sock_fprog *filter_prog; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 187 | char *alt_syscall_table; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 188 | struct mountpoint *mounts_head; |
| 189 | struct mountpoint *mounts_tail; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 190 | size_t mounts_count; |
Mike Frysinger | 785b1c3 | 2018-02-23 15:47:24 -0500 | [diff] [blame] | 191 | unsigned long remount_mode; |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 192 | size_t tmpfs_size; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 193 | char *cgroups[MAX_CGROUPS]; |
| 194 | size_t cgroup_count; |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 195 | struct minijail_rlimit rlimits[MAX_RLIMITS]; |
| 196 | size_t rlimit_count; |
Luis Hector Chavez | ec0a2c1 | 2017-06-29 20:29:57 -0700 | [diff] [blame] | 197 | uint64_t securebits_skip_mask; |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 198 | struct hook *hooks_head; |
| 199 | struct hook *hooks_tail; |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 200 | struct preserved_fd preserved_fds[MAX_PRESERVED_FDS]; |
| 201 | size_t preserved_fd_count; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 202 | }; |
| 203 | |
Luis Hector Chavez | 64730af | 2017-09-13 13:18:59 -0700 | [diff] [blame] | 204 | static void run_hooks_or_die(const struct minijail *j, |
| 205 | minijail_hook_event_t event); |
| 206 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 207 | static void free_mounts_list(struct minijail *j) |
| 208 | { |
| 209 | while (j->mounts_head) { |
| 210 | struct mountpoint *m = j->mounts_head; |
| 211 | j->mounts_head = j->mounts_head->next; |
| 212 | free(m->data); |
| 213 | free(m->type); |
| 214 | free(m->dest); |
| 215 | free(m->src); |
| 216 | free(m); |
| 217 | } |
| 218 | // No need to clear mounts_head as we know it's NULL after the loop. |
| 219 | j->mounts_tail = NULL; |
| 220 | } |
| 221 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 222 | /* |
François Degros | 664eba7 | 2019-11-05 13:18:24 +1100 | [diff] [blame] | 223 | * Writes exactly n bytes from buf to file descriptor fd. |
| 224 | * Returns 0 on success or a negative error code on error. |
| 225 | */ |
| 226 | static int write_exactly(int fd, const void *buf, size_t n) |
| 227 | { |
| 228 | const char *p = buf; |
| 229 | while (n > 0) { |
| 230 | const ssize_t written = write(fd, p, n); |
| 231 | if (written < 0) { |
| 232 | if (errno == EINTR) |
| 233 | continue; |
| 234 | |
| 235 | return -errno; |
| 236 | } |
| 237 | |
| 238 | p += written; |
| 239 | n -= written; |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 245 | /* Closes *pfd and sets it to -1. */ |
| 246 | static void close_and_reset(int *pfd) |
| 247 | { |
| 248 | if (*pfd != -1) |
| 249 | close(*pfd); |
| 250 | *pfd = -1; |
| 251 | } |
| 252 | |
François Degros | 664eba7 | 2019-11-05 13:18:24 +1100 | [diff] [blame] | 253 | /* |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 254 | * Strip out flags meant for the parent. |
| 255 | * We keep things that are not inherited across execve(2) (e.g. capabilities), |
| 256 | * or are easier to set after execve(2) (e.g. seccomp filters). |
| 257 | */ |
| 258 | void minijail_preenter(struct minijail *j) |
| 259 | { |
| 260 | j->flags.vfs = 0; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 261 | j->flags.enter_vfs = 0; |
Luis Hector Chavez | 83a4489 | 2018-10-12 08:56:20 -0700 | [diff] [blame] | 262 | j->flags.ns_cgroups = 0; |
| 263 | j->flags.net = 0; |
| 264 | j->flags.uts = 0; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 265 | j->flags.remount_proc_ro = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 266 | j->flags.pids = 0; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 267 | j->flags.do_init = 0; |
Luis Hector Chavez | ac981fc | 2017-09-18 15:52:38 -0700 | [diff] [blame] | 268 | j->flags.run_as_init = 0; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 269 | j->flags.pid_file = 0; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 270 | j->flags.cgroups = 0; |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 271 | j->flags.forward_signals = 0; |
Xiyuan Xia | 9b41e65 | 2019-05-23 11:03:04 -0700 | [diff] [blame] | 272 | j->flags.setsid = 0; |
Mike Frysinger | 785b1c3 | 2018-02-23 15:47:24 -0500 | [diff] [blame] | 273 | j->remount_mode = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Strip out flags meant for the child. |
| 278 | * We keep things that are inherited across execve(2). |
| 279 | */ |
| 280 | void minijail_preexec(struct minijail *j) |
| 281 | { |
| 282 | int vfs = j->flags.vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 283 | int enter_vfs = j->flags.enter_vfs; |
Luis Hector Chavez | 83a4489 | 2018-10-12 08:56:20 -0700 | [diff] [blame] | 284 | int ns_cgroups = j->flags.ns_cgroups; |
| 285 | int net = j->flags.net; |
| 286 | int uts = j->flags.uts; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 287 | int remount_proc_ro = j->flags.remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 288 | int userns = j->flags.userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 289 | if (j->user) |
| 290 | free(j->user); |
| 291 | j->user = NULL; |
Jorge Lucangeli Obes | e81a52f | 2015-12-04 16:05:23 -0800 | [diff] [blame] | 292 | if (j->suppl_gid_list) |
| 293 | free(j->suppl_gid_list); |
| 294 | j->suppl_gid_list = NULL; |
Luis Hector Chavez | 9acba45 | 2018-10-11 10:13:25 -0700 | [diff] [blame] | 295 | if (j->preload_path) |
| 296 | free(j->preload_path); |
| 297 | j->preload_path = NULL; |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 298 | free_mounts_list(j); |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 299 | memset(&j->flags, 0, sizeof(j->flags)); |
| 300 | /* Now restore anything we meant to keep. */ |
| 301 | j->flags.vfs = vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 302 | j->flags.enter_vfs = enter_vfs; |
Luis Hector Chavez | 83a4489 | 2018-10-12 08:56:20 -0700 | [diff] [blame] | 303 | j->flags.ns_cgroups = ns_cgroups; |
| 304 | j->flags.net = net; |
| 305 | j->flags.uts = uts; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 306 | j->flags.remount_proc_ro = remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 307 | j->flags.userns = userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 308 | /* Note, |pids| will already have been used before this call. */ |
| 309 | } |
| 310 | |
| 311 | /* Minijail API. */ |
| 312 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 313 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 314 | { |
Mike Frysinger | 785b1c3 | 2018-02-23 15:47:24 -0500 | [diff] [blame] | 315 | struct minijail *j = calloc(1, sizeof(struct minijail)); |
| 316 | j->remount_mode = MS_PRIVATE; |
| 317 | return j; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 320 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 321 | { |
| 322 | if (uid == 0) |
| 323 | die("useless change to uid 0"); |
| 324 | j->uid = uid; |
| 325 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 328 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 329 | { |
| 330 | if (gid == 0) |
| 331 | die("useless change to gid 0"); |
| 332 | j->gid = gid; |
| 333 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 336 | void API minijail_set_supplementary_gids(struct minijail *j, size_t size, |
| 337 | const gid_t *list) |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 338 | { |
Jorge Lucangeli Obes | 06940be | 2015-12-04 18:09:21 -0800 | [diff] [blame] | 339 | size_t i; |
| 340 | |
Jorge Lucangeli Obes | 3454319 | 2017-01-11 16:07:57 -0500 | [diff] [blame] | 341 | if (j->flags.inherit_suppl_gids) |
| 342 | die("cannot inherit *and* set supplementary groups"); |
| 343 | if (j->flags.keep_suppl_gids) |
| 344 | die("cannot keep *and* set supplementary groups"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 345 | |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 346 | if (size == 0) { |
| 347 | /* Clear supplementary groups. */ |
| 348 | j->suppl_gid_list = NULL; |
| 349 | j->suppl_gid_count = 0; |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 350 | j->flags.set_suppl_gids = 1; |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 351 | return; |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 352 | } |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 353 | |
| 354 | /* Copy the gid_t array. */ |
| 355 | j->suppl_gid_list = calloc(size, sizeof(gid_t)); |
| 356 | if (!j->suppl_gid_list) { |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 357 | die("failed to allocate internal supplementary group array"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 358 | } |
Jorge Lucangeli Obes | 06940be | 2015-12-04 18:09:21 -0800 | [diff] [blame] | 359 | for (i = 0; i < size; i++) { |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 360 | j->suppl_gid_list[i] = list[i]; |
| 361 | } |
| 362 | j->suppl_gid_count = size; |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 363 | j->flags.set_suppl_gids = 1; |
| 364 | } |
| 365 | |
| 366 | void API minijail_keep_supplementary_gids(struct minijail *j) { |
| 367 | j->flags.keep_suppl_gids = 1; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 370 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 371 | { |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 372 | uid_t uid; |
| 373 | gid_t gid; |
| 374 | int rc = lookup_user(user, &uid, &gid); |
| 375 | if (rc) |
| 376 | return rc; |
| 377 | minijail_change_uid(j, uid); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 378 | j->user = strdup(user); |
| 379 | if (!j->user) |
| 380 | return -ENOMEM; |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 381 | j->usergid = gid; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 382 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 383 | } |
| 384 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 385 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 386 | { |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 387 | gid_t gid; |
| 388 | int rc = lookup_group(group, &gid); |
| 389 | if (rc) |
| 390 | return rc; |
| 391 | minijail_change_gid(j, gid); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 392 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 395 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 396 | { |
| 397 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 398 | } |
| 399 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 400 | void API minijail_no_new_privs(struct minijail *j) |
| 401 | { |
| 402 | j->flags.no_new_privs = 1; |
| 403 | } |
| 404 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 405 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 406 | { |
| 407 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 410 | void API minijail_set_seccomp_filter_tsync(struct minijail *j) |
| 411 | { |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 412 | if (j->filter_len > 0 && j->filter_prog != NULL) { |
| 413 | die("minijail_set_seccomp_filter_tsync() must be called " |
| 414 | "before minijail_parse_seccomp_filters()"); |
| 415 | } |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 416 | |
| 417 | if (j->flags.seccomp_filter_logging && !seccomp_ret_log_available()) { |
| 418 | /* |
| 419 | * If SECCOMP_RET_LOG is not available, we don't want to use |
| 420 | * SECCOMP_RET_TRAP to both kill the entire process and report |
| 421 | * failing syscalls, since it will be brittle. Just bail. |
| 422 | */ |
Mike Frysinger | 52f6ada | 2019-06-26 16:59:36 -0400 | [diff] [blame] | 423 | die("SECCOMP_RET_LOG not available, cannot use logging with " |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 424 | "thread sync at the same time"); |
| 425 | } |
| 426 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 427 | j->flags.seccomp_filter_tsync = 1; |
| 428 | } |
| 429 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 430 | void API minijail_log_seccomp_filter_failures(struct minijail *j) |
| 431 | { |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 432 | if (j->filter_len > 0 && j->filter_prog != NULL) { |
| 433 | die("minijail_log_seccomp_filter_failures() must be called " |
| 434 | "before minijail_parse_seccomp_filters()"); |
| 435 | } |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 436 | |
| 437 | if (j->flags.seccomp_filter_tsync && !seccomp_ret_log_available()) { |
| 438 | /* |
| 439 | * If SECCOMP_RET_LOG is not available, we don't want to use |
| 440 | * SECCOMP_RET_TRAP to both kill the entire process and report |
| 441 | * failing syscalls, since it will be brittle. Just bail. |
| 442 | */ |
Mike Frysinger | 52f6ada | 2019-06-26 16:59:36 -0400 | [diff] [blame] | 443 | die("SECCOMP_RET_LOG not available, cannot use thread sync with " |
| 444 | "logging at the same time"); |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | if (debug_logging_allowed()) { |
| 448 | j->flags.seccomp_filter_logging = 1; |
| 449 | } else { |
| 450 | warn("non-debug build: ignoring request to enable seccomp " |
| 451 | "logging"); |
| 452 | } |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 455 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 456 | { |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 457 | /* |
| 458 | * 'minijail_use_caps' configures a runtime-capabilities-only |
| 459 | * environment, including a bounding set matching the thread's runtime |
| 460 | * (permitted|inheritable|effective) sets. |
| 461 | * Therefore, it will override any existing bounding set configurations |
| 462 | * since the latter would allow gaining extra runtime capabilities from |
| 463 | * file capabilities. |
| 464 | */ |
| 465 | if (j->flags.capbset_drop) { |
| 466 | warn("overriding bounding set configuration"); |
| 467 | j->cap_bset = 0; |
| 468 | j->flags.capbset_drop = 0; |
| 469 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 470 | j->caps = capmask; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 471 | j->flags.use_caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 474 | void API minijail_capbset_drop(struct minijail *j, uint64_t capmask) |
| 475 | { |
| 476 | if (j->flags.use_caps) { |
| 477 | /* |
| 478 | * 'minijail_use_caps' will have already configured a capability |
| 479 | * bounding set matching the (permitted|inheritable|effective) |
| 480 | * sets. Abort if the user tries to configure a separate |
| 481 | * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps' |
| 482 | * are mutually exclusive. |
| 483 | */ |
| 484 | die("runtime capabilities already configured, can't drop " |
| 485 | "bounding set separately"); |
| 486 | } |
| 487 | j->cap_bset = capmask; |
| 488 | j->flags.capbset_drop = 1; |
| 489 | } |
| 490 | |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 491 | void API minijail_set_ambient_caps(struct minijail *j) |
| 492 | { |
| 493 | j->flags.set_ambient_caps = 1; |
| 494 | } |
| 495 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 496 | void API minijail_reset_signal_mask(struct minijail *j) |
| 497 | { |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 498 | j->flags.reset_signal_mask = 1; |
| 499 | } |
| 500 | |
Luis Hector Chavez | a27118a | 2018-04-04 08:18:01 -0700 | [diff] [blame] | 501 | void API minijail_reset_signal_handlers(struct minijail *j) |
| 502 | { |
| 503 | j->flags.reset_signal_handlers = 1; |
| 504 | } |
| 505 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 506 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 507 | { |
| 508 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 509 | } |
| 510 | |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 511 | void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path) |
| 512 | { |
Mike Frysinger | 902a449 | 2018-12-27 05:22:56 -0500 | [diff] [blame] | 513 | /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */ |
| 514 | int ns_fd = open(ns_path, O_RDONLY); |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 515 | if (ns_fd < 0) { |
| 516 | pdie("failed to open namespace '%s'", ns_path); |
| 517 | } |
| 518 | j->mountns_fd = ns_fd; |
| 519 | j->flags.enter_vfs = 1; |
| 520 | } |
| 521 | |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 522 | void API minijail_new_session_keyring(struct minijail *j) |
| 523 | { |
| 524 | j->flags.new_session_keyring = 1; |
| 525 | } |
| 526 | |
Luis Hector Chavez | ec0a2c1 | 2017-06-29 20:29:57 -0700 | [diff] [blame] | 527 | void API minijail_skip_setting_securebits(struct minijail *j, |
| 528 | uint64_t securebits_skip_mask) |
| 529 | { |
| 530 | j->securebits_skip_mask = securebits_skip_mask; |
| 531 | } |
| 532 | |
Mike Frysinger | 785b1c3 | 2018-02-23 15:47:24 -0500 | [diff] [blame] | 533 | void API minijail_remount_mode(struct minijail *j, unsigned long mode) |
| 534 | { |
| 535 | j->remount_mode = mode; |
| 536 | } |
| 537 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 538 | void API minijail_skip_remount_private(struct minijail *j) |
| 539 | { |
Mike Frysinger | 785b1c3 | 2018-02-23 15:47:24 -0500 | [diff] [blame] | 540 | j->remount_mode = 0; |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 543 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 544 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 545 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 546 | j->flags.remount_proc_ro = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 547 | j->flags.pids = 1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 548 | j->flags.do_init = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 549 | } |
| 550 | |
Jorge Lucangeli Obes | 2fa96d1 | 2019-02-05 10:51:57 -0500 | [diff] [blame] | 551 | void API minijail_namespace_pids_rw_proc(struct minijail *j) |
| 552 | { |
| 553 | j->flags.vfs = 1; |
| 554 | j->flags.pids = 1; |
| 555 | j->flags.do_init = 1; |
| 556 | } |
| 557 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 558 | void API minijail_namespace_ipc(struct minijail *j) |
| 559 | { |
| 560 | j->flags.ipc = 1; |
| 561 | } |
| 562 | |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 563 | void API minijail_namespace_uts(struct minijail *j) |
| 564 | { |
| 565 | j->flags.uts = 1; |
| 566 | } |
| 567 | |
| 568 | int API minijail_namespace_set_hostname(struct minijail *j, const char *name) |
| 569 | { |
| 570 | if (j->hostname) |
| 571 | return -EINVAL; |
| 572 | minijail_namespace_uts(j); |
| 573 | j->hostname = strdup(name); |
| 574 | if (!j->hostname) |
| 575 | return -ENOMEM; |
| 576 | return 0; |
| 577 | } |
| 578 | |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 579 | void API minijail_namespace_net(struct minijail *j) |
| 580 | { |
| 581 | j->flags.net = 1; |
| 582 | } |
| 583 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 584 | void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path) |
| 585 | { |
Mike Frysinger | 902a449 | 2018-12-27 05:22:56 -0500 | [diff] [blame] | 586 | /* Note: Do not use O_CLOEXEC here. We'll close it after we use it. */ |
| 587 | int ns_fd = open(ns_path, O_RDONLY); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 588 | if (ns_fd < 0) { |
| 589 | pdie("failed to open namespace '%s'", ns_path); |
| 590 | } |
| 591 | j->netns_fd = ns_fd; |
| 592 | j->flags.enter_net = 1; |
| 593 | } |
| 594 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 595 | void API minijail_namespace_cgroups(struct minijail *j) |
| 596 | { |
| 597 | j->flags.ns_cgroups = 1; |
| 598 | } |
| 599 | |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 600 | void API minijail_close_open_fds(struct minijail *j) |
| 601 | { |
| 602 | j->flags.close_open_fds = 1; |
| 603 | } |
| 604 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 605 | void API minijail_remount_proc_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 606 | { |
| 607 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 608 | j->flags.remount_proc_ro = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 609 | } |
| 610 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 611 | void API minijail_namespace_user(struct minijail *j) |
| 612 | { |
| 613 | j->flags.userns = 1; |
| 614 | } |
| 615 | |
Jorge Lucangeli Obes | 200299c | 2016-09-23 15:21:57 -0400 | [diff] [blame] | 616 | void API minijail_namespace_user_disable_setgroups(struct minijail *j) |
| 617 | { |
| 618 | j->flags.disable_setgroups = 1; |
| 619 | } |
| 620 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 621 | int API minijail_uidmap(struct minijail *j, const char *uidmap) |
| 622 | { |
| 623 | j->uidmap = strdup(uidmap); |
| 624 | if (!j->uidmap) |
| 625 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 626 | char *ch; |
| 627 | for (ch = j->uidmap; *ch; ch++) { |
| 628 | if (*ch == ',') |
| 629 | *ch = '\n'; |
| 630 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | int API minijail_gidmap(struct minijail *j, const char *gidmap) |
| 635 | { |
| 636 | j->gidmap = strdup(gidmap); |
| 637 | if (!j->gidmap) |
| 638 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 639 | char *ch; |
| 640 | for (ch = j->gidmap; *ch; ch++) { |
| 641 | if (*ch == ',') |
| 642 | *ch = '\n'; |
| 643 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 647 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 648 | { |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 649 | j->flags.inherit_suppl_gids = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 650 | } |
| 651 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 652 | void API minijail_run_as_init(struct minijail *j) |
| 653 | { |
| 654 | /* |
| 655 | * Since the jailed program will become 'init' in the new PID namespace, |
| 656 | * Minijail does not need to fork an 'init' process. |
| 657 | */ |
Luis Hector Chavez | ac981fc | 2017-09-18 15:52:38 -0700 | [diff] [blame] | 658 | j->flags.run_as_init = 1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 659 | } |
| 660 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 661 | int API minijail_enter_chroot(struct minijail *j, const char *dir) |
| 662 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 663 | if (j->chrootdir) |
| 664 | return -EINVAL; |
| 665 | j->chrootdir = strdup(dir); |
| 666 | if (!j->chrootdir) |
| 667 | return -ENOMEM; |
| 668 | j->flags.chroot = 1; |
| 669 | return 0; |
| 670 | } |
| 671 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 672 | int API minijail_enter_pivot_root(struct minijail *j, const char *dir) |
| 673 | { |
| 674 | if (j->chrootdir) |
| 675 | return -EINVAL; |
| 676 | j->chrootdir = strdup(dir); |
| 677 | if (!j->chrootdir) |
| 678 | return -ENOMEM; |
| 679 | j->flags.pivot_root = 1; |
| 680 | return 0; |
| 681 | } |
| 682 | |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 683 | char API *minijail_get_original_path(struct minijail *j, |
| 684 | const char *path_inside_chroot) |
| 685 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 686 | struct mountpoint *b; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 687 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 688 | b = j->mounts_head; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 689 | while (b) { |
| 690 | /* |
| 691 | * If |path_inside_chroot| is the exact destination of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 692 | * mount, then the original path is exactly the source of |
| 693 | * the mount. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 694 | * for example: "-b /some/path/exe,/chroot/path/exe" |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 695 | * mount source = /some/path/exe, mount dest = |
| 696 | * /chroot/path/exe Then when getting the original path of |
| 697 | * "/chroot/path/exe", the source of that mount, |
| 698 | * "/some/path/exe" is what should be returned. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 699 | */ |
| 700 | if (!strcmp(b->dest, path_inside_chroot)) |
| 701 | return strdup(b->src); |
| 702 | |
| 703 | /* |
| 704 | * If |path_inside_chroot| is within the destination path of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 705 | * mount, take the suffix of the chroot path relative to the |
| 706 | * mount destination path, and append it to the mount source |
| 707 | * path. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 708 | */ |
| 709 | if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) { |
| 710 | const char *relative_path = |
| 711 | path_inside_chroot + strlen(b->dest); |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 712 | return path_join(b->src, relative_path); |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 713 | } |
| 714 | b = b->next; |
| 715 | } |
| 716 | |
| 717 | /* If there is a chroot path, append |path_inside_chroot| to that. */ |
| 718 | if (j->chrootdir) |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 719 | return path_join(j->chrootdir, path_inside_chroot); |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 720 | |
| 721 | /* No chroot, so the path outside is the same as it is inside. */ |
| 722 | return strdup(path_inside_chroot); |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 725 | size_t minijail_get_tmpfs_size(const struct minijail *j) |
| 726 | { |
| 727 | return j->tmpfs_size; |
| 728 | } |
| 729 | |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 730 | void API minijail_mount_dev(struct minijail *j) |
| 731 | { |
| 732 | j->flags.mount_dev = 1; |
| 733 | } |
| 734 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 735 | void API minijail_mount_tmp(struct minijail *j) |
| 736 | { |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 737 | minijail_mount_tmp_size(j, 64 * 1024 * 1024); |
| 738 | } |
| 739 | |
| 740 | void API minijail_mount_tmp_size(struct minijail *j, size_t size) |
| 741 | { |
| 742 | j->tmpfs_size = size; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 743 | j->flags.mount_tmp = 1; |
| 744 | } |
| 745 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 746 | int API minijail_write_pid_file(struct minijail *j, const char *path) |
| 747 | { |
| 748 | j->pid_file_path = strdup(path); |
| 749 | if (!j->pid_file_path) |
| 750 | return -ENOMEM; |
| 751 | j->flags.pid_file = 1; |
| 752 | return 0; |
| 753 | } |
| 754 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 755 | int API minijail_add_to_cgroup(struct minijail *j, const char *path) |
| 756 | { |
| 757 | if (j->cgroup_count >= MAX_CGROUPS) |
| 758 | return -ENOMEM; |
| 759 | j->cgroups[j->cgroup_count] = strdup(path); |
| 760 | if (!j->cgroups[j->cgroup_count]) |
| 761 | return -ENOMEM; |
| 762 | j->cgroup_count++; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 763 | j->flags.cgroups = 1; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 764 | return 0; |
| 765 | } |
| 766 | |
Luis Hector Chavez | 7058a2d | 2018-01-29 08:41:34 -0800 | [diff] [blame] | 767 | int API minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max) |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 768 | { |
| 769 | size_t i; |
| 770 | |
| 771 | if (j->rlimit_count >= MAX_RLIMITS) |
| 772 | return -ENOMEM; |
| 773 | /* It's an error if the caller sets the same rlimit multiple times. */ |
| 774 | for (i = 0; i < j->rlimit_count; i++) { |
| 775 | if (j->rlimits[i].type == type) |
| 776 | return -EEXIST; |
| 777 | } |
| 778 | |
| 779 | j->rlimits[j->rlimit_count].type = type; |
| 780 | j->rlimits[j->rlimit_count].cur = cur; |
| 781 | j->rlimits[j->rlimit_count].max = max; |
| 782 | j->rlimit_count++; |
| 783 | return 0; |
| 784 | } |
| 785 | |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 786 | int API minijail_forward_signals(struct minijail *j) |
| 787 | { |
| 788 | j->flags.forward_signals = 1; |
| 789 | return 0; |
| 790 | } |
| 791 | |
Xiyuan Xia | 9b41e65 | 2019-05-23 11:03:04 -0700 | [diff] [blame] | 792 | int API minijail_create_session(struct minijail *j) { |
| 793 | j->flags.setsid = 1; |
| 794 | return 0; |
| 795 | } |
| 796 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 797 | int API minijail_mount_with_data(struct minijail *j, const char *src, |
| 798 | const char *dest, const char *type, |
| 799 | unsigned long flags, const char *data) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 800 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 801 | struct mountpoint *m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 802 | |
| 803 | if (*dest != '/') |
| 804 | return -EINVAL; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 805 | m = calloc(1, sizeof(*m)); |
| 806 | if (!m) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 807 | return -ENOMEM; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 808 | m->dest = strdup(dest); |
| 809 | if (!m->dest) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 810 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 811 | m->src = strdup(src); |
| 812 | if (!m->src) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 813 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 814 | m->type = strdup(type); |
| 815 | if (!m->type) |
| 816 | goto error; |
Mike Frysinger | b7803c8 | 2018-08-23 15:43:15 -0400 | [diff] [blame] | 817 | |
| 818 | if (!data || !data[0]) { |
| 819 | /* |
| 820 | * Set up secure defaults for certain filesystems. Adding this |
| 821 | * fs-specific logic here kind of sucks, but considering how |
| 822 | * people use these in practice, it's probably OK. If they want |
| 823 | * the kernel defaults, they can pass data="" instead of NULL. |
| 824 | */ |
| 825 | if (!strcmp(type, "tmpfs")) { |
| 826 | /* tmpfs defaults to mode=1777 and size=50%. */ |
| 827 | data = "mode=0755,size=10M"; |
| 828 | } |
| 829 | } |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 830 | if (data) { |
| 831 | m->data = strdup(data); |
| 832 | if (!m->data) |
| 833 | goto error; |
| 834 | m->has_data = 1; |
| 835 | } |
Mike Frysinger | cb8674d | 2018-08-12 00:53:35 -0400 | [diff] [blame] | 836 | |
| 837 | /* If they don't specify any flags, default to secure ones. */ |
| 838 | if (flags == 0) |
| 839 | flags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 840 | m->flags = flags; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 841 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 842 | /* |
Jorge Lucangeli Obes | 0a0514c | 2020-01-03 11:18:32 -0500 | [diff] [blame] | 843 | * Unless asked to enter an existing namespace, force vfs namespacing |
| 844 | * so the mounts don't leak out into the containing vfs namespace. |
| 845 | * If Minijail is being asked to enter the root vfs namespace this will |
| 846 | * leak mounts, but it's unlikely that the user would ask to do that by |
| 847 | * mistake. |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 848 | */ |
Jorge Lucangeli Obes | 0a0514c | 2020-01-03 11:18:32 -0500 | [diff] [blame] | 849 | if (!j->flags.enter_vfs) |
| 850 | minijail_namespace_vfs(j); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 851 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 852 | if (j->mounts_tail) |
| 853 | j->mounts_tail->next = m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 854 | else |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 855 | j->mounts_head = m; |
| 856 | j->mounts_tail = m; |
| 857 | j->mounts_count++; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 858 | |
| 859 | return 0; |
| 860 | |
| 861 | error: |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 862 | free(m->type); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 863 | free(m->src); |
| 864 | free(m->dest); |
| 865 | free(m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 866 | return -ENOMEM; |
| 867 | } |
| 868 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 869 | int API minijail_mount(struct minijail *j, const char *src, const char *dest, |
| 870 | const char *type, unsigned long flags) |
| 871 | { |
| 872 | return minijail_mount_with_data(j, src, dest, type, flags, NULL); |
| 873 | } |
| 874 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 875 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
| 876 | int writeable) |
| 877 | { |
| 878 | unsigned long flags = MS_BIND; |
| 879 | |
| 880 | if (!writeable) |
| 881 | flags |= MS_RDONLY; |
| 882 | |
| 883 | return minijail_mount(j, src, dest, "", flags); |
| 884 | } |
| 885 | |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 886 | int API minijail_add_hook(struct minijail *j, minijail_hook_t hook, |
| 887 | void *payload, minijail_hook_event_t event) |
| 888 | { |
| 889 | struct hook *c; |
| 890 | |
| 891 | if (hook == NULL) |
| 892 | return -EINVAL; |
| 893 | if (event >= MINIJAIL_HOOK_EVENT_MAX) |
| 894 | return -EINVAL; |
| 895 | c = calloc(1, sizeof(*c)); |
| 896 | if (!c) |
| 897 | return -ENOMEM; |
| 898 | |
| 899 | c->hook = hook; |
| 900 | c->payload = payload; |
| 901 | c->event = event; |
| 902 | |
| 903 | if (j->hooks_tail) |
| 904 | j->hooks_tail->next = c; |
| 905 | else |
| 906 | j->hooks_head = c; |
| 907 | j->hooks_tail = c; |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 912 | int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd) |
| 913 | { |
| 914 | if (parent_fd < 0 || child_fd < 0) |
| 915 | return -EINVAL; |
| 916 | if (j->preserved_fd_count >= MAX_PRESERVED_FDS) |
| 917 | return -ENOMEM; |
| 918 | j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd; |
| 919 | j->preserved_fds[j->preserved_fd_count].child_fd = child_fd; |
| 920 | j->preserved_fd_count++; |
| 921 | return 0; |
| 922 | } |
| 923 | |
Luis Hector Chavez | 9acba45 | 2018-10-11 10:13:25 -0700 | [diff] [blame] | 924 | int API minijail_set_preload_path(struct minijail *j, const char *preload_path) |
| 925 | { |
| 926 | if (j->preload_path) |
| 927 | return -EINVAL; |
| 928 | j->preload_path = strdup(preload_path); |
| 929 | if (!j->preload_path) |
| 930 | return -ENOMEM; |
| 931 | return 0; |
| 932 | } |
| 933 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 934 | static void clear_seccomp_options(struct minijail *j) |
| 935 | { |
| 936 | j->flags.seccomp_filter = 0; |
| 937 | j->flags.seccomp_filter_tsync = 0; |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 938 | j->flags.seccomp_filter_logging = 0; |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 939 | j->filter_len = 0; |
| 940 | j->filter_prog = NULL; |
| 941 | j->flags.no_new_privs = 0; |
| 942 | } |
| 943 | |
Luis Hector Chavez | c3e1772 | 2018-10-16 20:43:12 -0700 | [diff] [blame] | 944 | static int seccomp_should_use_filters(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 945 | { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 946 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) { |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 947 | /* |
| 948 | * |errno| will be set to EINVAL when seccomp has not been |
| 949 | * compiled into the kernel. On certain platforms and kernel |
| 950 | * versions this is not a fatal failure. In that case, and only |
| 951 | * in that case, disable seccomp and skip loading the filters. |
| 952 | */ |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 953 | if ((errno == EINVAL) && seccomp_can_softfail()) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 954 | warn("not loading seccomp filters, seccomp filter not " |
| 955 | "supported"); |
| 956 | clear_seccomp_options(j); |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 957 | return 0; |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 958 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 959 | /* |
| 960 | * If |errno| != EINVAL or seccomp_can_softfail() is false, |
| 961 | * we can proceed. Worst case scenario minijail_enter() will |
| 962 | * abort() if seccomp fails. |
| 963 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 964 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 965 | if (j->flags.seccomp_filter_tsync) { |
| 966 | /* Are the seccomp(2) syscall and the TSYNC option supported? */ |
| 967 | if (sys_seccomp(SECCOMP_SET_MODE_FILTER, |
| 968 | SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) { |
| 969 | int saved_errno = errno; |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 970 | if (saved_errno == ENOSYS && seccomp_can_softfail()) { |
| 971 | warn("seccomp(2) syscall not supported"); |
| 972 | clear_seccomp_options(j); |
| 973 | return 0; |
| 974 | } else if (saved_errno == EINVAL && |
| 975 | seccomp_can_softfail()) { |
| 976 | warn( |
| 977 | "seccomp filter thread sync not supported"); |
| 978 | clear_seccomp_options(j); |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 979 | return 0; |
| 980 | } |
| 981 | /* |
| 982 | * Similar logic here. If seccomp_can_softfail() is |
| 983 | * false, or |errno| != ENOSYS, or |errno| != EINVAL, |
| 984 | * we can proceed. Worst case scenario minijail_enter() |
| 985 | * will abort() if seccomp or TSYNC fail. |
| 986 | */ |
| 987 | } |
| 988 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 989 | return 1; |
| 990 | } |
| 991 | |
Luis Hector Chavez | c3e1772 | 2018-10-16 20:43:12 -0700 | [diff] [blame] | 992 | static int set_seccomp_filters_internal(struct minijail *j, |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 993 | const struct sock_fprog *filter, |
| 994 | bool owned) |
Luis Hector Chavez | c3e1772 | 2018-10-16 20:43:12 -0700 | [diff] [blame] | 995 | { |
| 996 | struct sock_fprog *fprog; |
| 997 | |
| 998 | if (owned) { |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 999 | /* |
| 1000 | * If |owned| is true, it's OK to cast away the const-ness since |
| 1001 | * we'll own the pointer going forward. |
| 1002 | */ |
| 1003 | fprog = (struct sock_fprog *)filter; |
Luis Hector Chavez | c3e1772 | 2018-10-16 20:43:12 -0700 | [diff] [blame] | 1004 | } else { |
| 1005 | fprog = malloc(sizeof(struct sock_fprog)); |
| 1006 | if (!fprog) |
| 1007 | return -ENOMEM; |
| 1008 | fprog->len = filter->len; |
| 1009 | fprog->filter = malloc(sizeof(struct sock_filter) * fprog->len); |
| 1010 | if (!fprog->filter) { |
| 1011 | free(fprog); |
| 1012 | return -ENOMEM; |
| 1013 | } |
| 1014 | memcpy(fprog->filter, filter->filter, |
| 1015 | sizeof(struct sock_filter) * fprog->len); |
| 1016 | } |
| 1017 | |
| 1018 | if (j->filter_prog) { |
| 1019 | free(j->filter_prog->filter); |
| 1020 | free(j->filter_prog); |
| 1021 | } |
| 1022 | |
| 1023 | j->filter_len = fprog->len; |
| 1024 | j->filter_prog = fprog; |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 1028 | static int parse_seccomp_filters(struct minijail *j, const char *filename, |
| 1029 | FILE *policy_file) |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 1030 | { |
| 1031 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
Luis Hector Chavez | c3e1772 | 2018-10-16 20:43:12 -0700 | [diff] [blame] | 1032 | if (!fprog) |
| 1033 | return -ENOMEM; |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 1034 | |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 1035 | struct filter_options filteropts; |
| 1036 | |
| 1037 | /* |
| 1038 | * Figure out filter options. |
| 1039 | * Allow logging? |
| 1040 | */ |
| 1041 | filteropts.allow_logging = |
| 1042 | debug_logging_allowed() && j->flags.seccomp_filter_logging; |
| 1043 | |
| 1044 | /* What to do on a blocked system call? */ |
| 1045 | if (filteropts.allow_logging) { |
| 1046 | if (seccomp_ret_log_available()) |
| 1047 | filteropts.action = ACTION_RET_LOG; |
| 1048 | else |
| 1049 | filteropts.action = ACTION_RET_TRAP; |
| 1050 | } else { |
| 1051 | if (j->flags.seccomp_filter_tsync) |
| 1052 | filteropts.action = ACTION_RET_TRAP; |
| 1053 | else |
| 1054 | filteropts.action = ACTION_RET_KILL; |
| 1055 | } |
| 1056 | |
| 1057 | /* |
| 1058 | * If SECCOMP_RET_LOG is not available, need to allow extra syscalls |
| 1059 | * for logging. |
| 1060 | */ |
| 1061 | filteropts.allow_syscalls_for_logging = |
| 1062 | filteropts.allow_logging && !seccomp_ret_log_available(); |
| 1063 | |
Jorge Lucangeli Obes | e1a8689 | 2019-06-10 16:17:03 -0400 | [diff] [blame] | 1064 | if (compile_filter(filename, policy_file, fprog, &filteropts)) { |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 1065 | free(fprog); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 1069 | return set_seccomp_filters_internal(j, fprog, true /* owned */); |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
| 1073 | { |
Luis Hector Chavez | c3e1772 | 2018-10-16 20:43:12 -0700 | [diff] [blame] | 1074 | if (!seccomp_should_use_filters(j)) |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 1075 | return; |
| 1076 | |
Luis Hector Chavez | a30a206 | 2018-07-12 21:10:33 -0700 | [diff] [blame] | 1077 | FILE *file = fopen(path, "re"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1078 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 1079 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1080 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1081 | |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 1082 | if (parse_seccomp_filters(j, path, file) != 0) { |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 1083 | die("failed to compile seccomp filter BPF program in '%s'", |
| 1084 | path); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1085 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 1086 | fclose(file); |
| 1087 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1088 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 1089 | void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd) |
| 1090 | { |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 1091 | char *fd_path, *path; |
| 1092 | FILE *file; |
| 1093 | |
Luis Hector Chavez | c3e1772 | 2018-10-16 20:43:12 -0700 | [diff] [blame] | 1094 | if (!seccomp_should_use_filters(j)) |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 1095 | return; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1096 | |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 1097 | file = fdopen(fd, "r"); |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 1098 | if (!file) { |
| 1099 | pdie("failed to associate stream with fd %d", fd); |
| 1100 | } |
| 1101 | |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 1102 | if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1) |
| 1103 | pdie("failed to create path for fd %d", fd); |
| 1104 | path = realpath(fd_path, NULL); |
| 1105 | if (path == NULL) |
| 1106 | pwarn("failed to get path of fd %d", fd); |
| 1107 | free(fd_path); |
| 1108 | |
| 1109 | if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) { |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 1110 | die("failed to compile seccomp filter BPF program from fd %d", |
| 1111 | fd); |
| 1112 | } |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 1113 | free(path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1114 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 1115 | } |
| 1116 | |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 1117 | void API minijail_set_seccomp_filters(struct minijail *j, |
| 1118 | const struct sock_fprog *filter) |
| 1119 | { |
| 1120 | if (!seccomp_should_use_filters(j)) |
| 1121 | return; |
| 1122 | |
| 1123 | if (j->flags.seccomp_filter_logging) { |
| 1124 | die("minijail_log_seccomp_filter_failures() is incompatible " |
| 1125 | "with minijail_set_seccomp_filters()"); |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * set_seccomp_filters_internal() can only fail with ENOMEM. |
| 1130 | * Furthermore, since we won't own the incoming filter, it will not be |
| 1131 | * modified. |
| 1132 | */ |
| 1133 | if (set_seccomp_filters_internal(j, filter, false /* owned */) < 0) { |
| 1134 | die("failed to set seccomp filter"); |
| 1135 | } |
| 1136 | } |
| 1137 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1138 | int API minijail_use_alt_syscall(struct minijail *j, const char *table) |
| 1139 | { |
| 1140 | j->alt_syscall_table = strdup(table); |
| 1141 | if (!j->alt_syscall_table) |
| 1142 | return -ENOMEM; |
| 1143 | j->flags.alt_syscall = 1; |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1147 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1148 | size_t available; |
| 1149 | size_t total; |
| 1150 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1151 | }; |
| 1152 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1153 | void marshal_state_init(struct marshal_state *state, char *buf, |
| 1154 | size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1155 | { |
| 1156 | state->available = available; |
| 1157 | state->buf = buf; |
| 1158 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1159 | } |
| 1160 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1161 | void marshal_append(struct marshal_state *state, void *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1162 | { |
| 1163 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1164 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1165 | /* Up to |available| will be written. */ |
| 1166 | if (copy_len) { |
| 1167 | memcpy(state->buf, src, copy_len); |
| 1168 | state->buf += copy_len; |
| 1169 | state->available -= copy_len; |
| 1170 | } |
| 1171 | /* |total| will contain the expected length. */ |
| 1172 | state->total += length; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1173 | } |
| 1174 | |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 1175 | void marshal_mount(struct marshal_state *state, const struct mountpoint *m) |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1176 | { |
| 1177 | marshal_append(state, m->src, strlen(m->src) + 1); |
| 1178 | marshal_append(state, m->dest, strlen(m->dest) + 1); |
| 1179 | marshal_append(state, m->type, strlen(m->type) + 1); |
| 1180 | marshal_append(state, (char *)&m->has_data, sizeof(m->has_data)); |
| 1181 | if (m->has_data) |
| 1182 | marshal_append(state, m->data, strlen(m->data) + 1); |
| 1183 | marshal_append(state, (char *)&m->flags, sizeof(m->flags)); |
| 1184 | } |
| 1185 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1186 | void minijail_marshal_helper(struct marshal_state *state, |
| 1187 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1188 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1189 | struct mountpoint *m = NULL; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1190 | size_t i; |
| 1191 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1192 | marshal_append(state, (char *)j, sizeof(*j)); |
| 1193 | if (j->user) |
| 1194 | marshal_append(state, j->user, strlen(j->user) + 1); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1195 | if (j->suppl_gid_list) { |
| 1196 | marshal_append(state, j->suppl_gid_list, |
| 1197 | j->suppl_gid_count * sizeof(gid_t)); |
| 1198 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1199 | if (j->chrootdir) |
| 1200 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1201 | if (j->hostname) |
| 1202 | marshal_append(state, j->hostname, strlen(j->hostname) + 1); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1203 | if (j->alt_syscall_table) { |
| 1204 | marshal_append(state, j->alt_syscall_table, |
| 1205 | strlen(j->alt_syscall_table) + 1); |
| 1206 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1207 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 1208 | struct sock_fprog *fp = j->filter_prog; |
| 1209 | marshal_append(state, (char *)fp->filter, |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1210 | fp->len * sizeof(struct sock_filter)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1211 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1212 | for (m = j->mounts_head; m; m = m->next) { |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 1213 | marshal_mount(state, m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1214 | } |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1215 | for (i = 0; i < j->cgroup_count; ++i) |
| 1216 | marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1217 | } |
| 1218 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1219 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1220 | { |
| 1221 | struct marshal_state state; |
| 1222 | marshal_state_init(&state, NULL, 0); |
| 1223 | minijail_marshal_helper(&state, j); |
| 1224 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 1225 | } |
| 1226 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1227 | int minijail_marshal(const struct minijail *j, char *buf, size_t available) |
| 1228 | { |
| 1229 | struct marshal_state state; |
| 1230 | marshal_state_init(&state, buf, available); |
| 1231 | minijail_marshal_helper(&state, j); |
| 1232 | return (state.total > available); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 1233 | } |
| 1234 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1235 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 1236 | { |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 1237 | size_t i; |
| 1238 | size_t count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1239 | int ret = -EINVAL; |
| 1240 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1241 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1242 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1243 | memcpy((void *)j, serialized, sizeof(*j)); |
| 1244 | serialized += sizeof(*j); |
| 1245 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1246 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1247 | /* Potentially stale pointers not used as signals. */ |
Luis Hector Chavez | 9acba45 | 2018-10-11 10:13:25 -0700 | [diff] [blame] | 1248 | j->preload_path = NULL; |
Jorge Lucangeli Obes | 3b2e6e4 | 2016-08-04 12:26:19 -0400 | [diff] [blame] | 1249 | j->pid_file_path = NULL; |
| 1250 | j->uidmap = NULL; |
| 1251 | j->gidmap = NULL; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1252 | j->mounts_head = NULL; |
| 1253 | j->mounts_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1254 | j->filter_prog = NULL; |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 1255 | j->hooks_head = NULL; |
| 1256 | j->hooks_tail = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1257 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1258 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1259 | char *user = consumestr(&serialized, &length); |
| 1260 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1261 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1262 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1263 | if (!j->user) |
| 1264 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1265 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1266 | |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1267 | if (j->suppl_gid_list) { /* stale pointer */ |
| 1268 | if (j->suppl_gid_count > NGROUPS_MAX) { |
| 1269 | goto bad_gid_list; |
| 1270 | } |
| 1271 | size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t); |
| 1272 | void *gid_list_bytes = |
| 1273 | consumebytes(gid_list_size, &serialized, &length); |
| 1274 | if (!gid_list_bytes) |
| 1275 | goto bad_gid_list; |
| 1276 | |
| 1277 | j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t)); |
| 1278 | if (!j->suppl_gid_list) |
| 1279 | goto bad_gid_list; |
| 1280 | |
| 1281 | memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size); |
| 1282 | } |
| 1283 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 1284 | if (j->chrootdir) { /* stale pointer */ |
| 1285 | char *chrootdir = consumestr(&serialized, &length); |
| 1286 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1287 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 1288 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1289 | if (!j->chrootdir) |
| 1290 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 1291 | } |
| 1292 | |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1293 | if (j->hostname) { /* stale pointer */ |
| 1294 | char *hostname = consumestr(&serialized, &length); |
| 1295 | if (!hostname) |
| 1296 | goto bad_hostname; |
| 1297 | j->hostname = strdup(hostname); |
| 1298 | if (!j->hostname) |
| 1299 | goto bad_hostname; |
| 1300 | } |
| 1301 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1302 | if (j->alt_syscall_table) { /* stale pointer */ |
| 1303 | char *alt_syscall_table = consumestr(&serialized, &length); |
| 1304 | if (!alt_syscall_table) |
| 1305 | goto bad_syscall_table; |
| 1306 | j->alt_syscall_table = strdup(alt_syscall_table); |
| 1307 | if (!j->alt_syscall_table) |
| 1308 | goto bad_syscall_table; |
| 1309 | } |
| 1310 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1311 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 1312 | size_t ninstrs = j->filter_len; |
| 1313 | if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) || |
| 1314 | ninstrs > USHRT_MAX) |
| 1315 | goto bad_filters; |
| 1316 | |
| 1317 | size_t program_len = ninstrs * sizeof(struct sock_filter); |
| 1318 | void *program = consumebytes(program_len, &serialized, &length); |
| 1319 | if (!program) |
| 1320 | goto bad_filters; |
| 1321 | |
| 1322 | j->filter_prog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1323 | if (!j->filter_prog) |
| 1324 | goto bad_filters; |
| 1325 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1326 | j->filter_prog->len = ninstrs; |
| 1327 | j->filter_prog->filter = malloc(program_len); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1328 | if (!j->filter_prog->filter) |
| 1329 | goto bad_filter_prog_instrs; |
| 1330 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1331 | memcpy(j->filter_prog->filter, program, program_len); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1332 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1333 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1334 | count = j->mounts_count; |
| 1335 | j->mounts_count = 0; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1336 | for (i = 0; i < count; ++i) { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1337 | unsigned long *flags; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1338 | int *has_data; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1339 | const char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1340 | const char *type; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1341 | const char *data = NULL; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1342 | const char *src = consumestr(&serialized, &length); |
| 1343 | if (!src) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1344 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1345 | dest = consumestr(&serialized, &length); |
| 1346 | if (!dest) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1347 | goto bad_mounts; |
| 1348 | type = consumestr(&serialized, &length); |
| 1349 | if (!type) |
| 1350 | goto bad_mounts; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1351 | has_data = consumebytes(sizeof(*has_data), &serialized, |
| 1352 | &length); |
| 1353 | if (!has_data) |
| 1354 | goto bad_mounts; |
| 1355 | if (*has_data) { |
| 1356 | data = consumestr(&serialized, &length); |
| 1357 | if (!data) |
| 1358 | goto bad_mounts; |
| 1359 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1360 | flags = consumebytes(sizeof(*flags), &serialized, &length); |
| 1361 | if (!flags) |
| 1362 | goto bad_mounts; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1363 | if (minijail_mount_with_data(j, src, dest, type, *flags, data)) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1364 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1365 | } |
| 1366 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1367 | count = j->cgroup_count; |
| 1368 | j->cgroup_count = 0; |
| 1369 | for (i = 0; i < count; ++i) { |
| 1370 | char *cgroup = consumestr(&serialized, &length); |
| 1371 | if (!cgroup) |
| 1372 | goto bad_cgroups; |
| 1373 | j->cgroups[i] = strdup(cgroup); |
| 1374 | if (!j->cgroups[i]) |
| 1375 | goto bad_cgroups; |
| 1376 | ++j->cgroup_count; |
| 1377 | } |
| 1378 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1379 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1380 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1381 | bad_cgroups: |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1382 | free_mounts_list(j); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1383 | for (i = 0; i < j->cgroup_count; ++i) |
| 1384 | free(j->cgroups[i]); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1385 | bad_mounts: |
Luis Hector Chavez | c3e1772 | 2018-10-16 20:43:12 -0700 | [diff] [blame] | 1386 | if (j->filter_prog && j->filter_prog->filter) |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1387 | free(j->filter_prog->filter); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1388 | bad_filter_prog_instrs: |
| 1389 | if (j->filter_prog) |
| 1390 | free(j->filter_prog); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1391 | bad_filters: |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1392 | if (j->alt_syscall_table) |
| 1393 | free(j->alt_syscall_table); |
| 1394 | bad_syscall_table: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1395 | if (j->chrootdir) |
| 1396 | free(j->chrootdir); |
| 1397 | bad_chrootdir: |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1398 | if (j->hostname) |
| 1399 | free(j->hostname); |
| 1400 | bad_hostname: |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1401 | if (j->suppl_gid_list) |
| 1402 | free(j->suppl_gid_list); |
| 1403 | bad_gid_list: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1404 | if (j->user) |
| 1405 | free(j->user); |
| 1406 | clear_pointers: |
| 1407 | j->user = NULL; |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1408 | j->suppl_gid_list = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1409 | j->chrootdir = NULL; |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1410 | j->hostname = NULL; |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1411 | j->alt_syscall_table = NULL; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1412 | j->cgroup_count = 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1413 | out: |
| 1414 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 1415 | } |
| 1416 | |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1417 | struct dev_spec { |
| 1418 | const char *name; |
| 1419 | mode_t mode; |
| 1420 | dev_t major, minor; |
| 1421 | }; |
| 1422 | |
| 1423 | static const struct dev_spec device_nodes[] = { |
| 1424 | { |
| 1425 | "null", |
| 1426 | S_IFCHR | 0666, 1, 3, |
| 1427 | }, |
| 1428 | { |
| 1429 | "zero", |
| 1430 | S_IFCHR | 0666, 1, 5, |
| 1431 | }, |
| 1432 | { |
| 1433 | "full", |
| 1434 | S_IFCHR | 0666, 1, 7, |
| 1435 | }, |
| 1436 | { |
| 1437 | "urandom", |
| 1438 | S_IFCHR | 0444, 1, 9, |
| 1439 | }, |
| 1440 | { |
| 1441 | "tty", |
| 1442 | S_IFCHR | 0666, 5, 0, |
| 1443 | }, |
| 1444 | }; |
| 1445 | |
| 1446 | struct dev_sym_spec { |
| 1447 | const char *source, *dest; |
| 1448 | }; |
| 1449 | |
| 1450 | static const struct dev_sym_spec device_symlinks[] = { |
| 1451 | { "ptmx", "pts/ptmx", }, |
| 1452 | { "fd", "/proc/self/fd", }, |
| 1453 | { "stdin", "fd/0", }, |
| 1454 | { "stdout", "fd/1", }, |
| 1455 | { "stderr", "fd/2", }, |
| 1456 | }; |
| 1457 | |
| 1458 | /* |
| 1459 | * Clean up the temporary dev path we had setup previously. In case of errors, |
| 1460 | * we don't want to go leaking empty tempdirs. |
| 1461 | */ |
| 1462 | static void mount_dev_cleanup(char *dev_path) |
| 1463 | { |
| 1464 | umount2(dev_path, MNT_DETACH); |
| 1465 | rmdir(dev_path); |
| 1466 | free(dev_path); |
| 1467 | } |
| 1468 | |
| 1469 | /* |
| 1470 | * Set up the pseudo /dev path at the temporary location. |
| 1471 | * See mount_dev_finalize for more details. |
| 1472 | */ |
| 1473 | static int mount_dev(char **dev_path_ret) |
| 1474 | { |
| 1475 | int ret; |
| 1476 | int dev_fd; |
| 1477 | size_t i; |
| 1478 | mode_t mask; |
| 1479 | char *dev_path; |
| 1480 | |
| 1481 | /* |
| 1482 | * Create a temp path for the /dev init. We'll relocate this to the |
| 1483 | * final location later on in the startup process. |
| 1484 | */ |
| 1485 | dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX"); |
| 1486 | if (dev_path == NULL || mkdtemp(dev_path) == NULL) |
| 1487 | pdie("could not create temp path for /dev"); |
| 1488 | |
| 1489 | /* Set up the empty /dev mount point first. */ |
| 1490 | ret = mount("minijail-devfs", dev_path, "tmpfs", |
| 1491 | MS_NOEXEC | MS_NOSUID, "size=5M,mode=755"); |
| 1492 | if (ret) { |
| 1493 | rmdir(dev_path); |
| 1494 | return ret; |
| 1495 | } |
| 1496 | |
| 1497 | /* We want to set the mode directly from the spec. */ |
| 1498 | mask = umask(0); |
| 1499 | |
| 1500 | /* Get a handle to the temp dev path for *at funcs below. */ |
| 1501 | dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC); |
| 1502 | if (dev_fd < 0) { |
| 1503 | ret = 1; |
| 1504 | goto done; |
| 1505 | } |
| 1506 | |
| 1507 | /* Create all the nodes in /dev. */ |
| 1508 | for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) { |
| 1509 | const struct dev_spec *ds = &device_nodes[i]; |
| 1510 | ret = mknodat(dev_fd, ds->name, ds->mode, |
| 1511 | makedev(ds->major, ds->minor)); |
| 1512 | if (ret) |
| 1513 | goto done; |
| 1514 | } |
| 1515 | |
| 1516 | /* Create all the symlinks in /dev. */ |
| 1517 | for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) { |
| 1518 | const struct dev_sym_spec *ds = &device_symlinks[i]; |
| 1519 | ret = symlinkat(ds->dest, dev_fd, ds->source); |
| 1520 | if (ret) |
| 1521 | goto done; |
| 1522 | } |
| 1523 | |
| 1524 | /* Restore old mask. */ |
| 1525 | done: |
| 1526 | close(dev_fd); |
| 1527 | umask(mask); |
| 1528 | |
| 1529 | if (ret) |
| 1530 | mount_dev_cleanup(dev_path); |
| 1531 | |
| 1532 | return ret; |
| 1533 | } |
| 1534 | |
| 1535 | /* |
| 1536 | * Relocate the temporary /dev mount to its final /dev place. |
| 1537 | * We have to do this two step process so people can bind mount extra |
| 1538 | * /dev paths like /dev/log. |
| 1539 | */ |
| 1540 | static int mount_dev_finalize(const struct minijail *j, char *dev_path) |
| 1541 | { |
| 1542 | int ret = -1; |
| 1543 | char *dest = NULL; |
| 1544 | |
| 1545 | /* Unmount the /dev mount if possible. */ |
| 1546 | if (umount2("/dev", MNT_DETACH)) |
| 1547 | goto done; |
| 1548 | |
| 1549 | if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0) |
| 1550 | goto done; |
| 1551 | |
| 1552 | if (mount(dev_path, dest, NULL, MS_MOVE, NULL)) |
| 1553 | goto done; |
| 1554 | |
| 1555 | ret = 0; |
| 1556 | done: |
| 1557 | free(dest); |
| 1558 | mount_dev_cleanup(dev_path); |
| 1559 | |
| 1560 | return ret; |
| 1561 | } |
| 1562 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1563 | /* |
| 1564 | * mount_one: Applies mounts from @m for @j, recursing as needed. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1565 | * @j Minijail these mounts are for |
| 1566 | * @m Head of list of mounts |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1567 | * |
| 1568 | * Returns 0 for success. |
| 1569 | */ |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1570 | static int mount_one(const struct minijail *j, struct mountpoint *m, |
| 1571 | const char *dev_path) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 1572 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1573 | int ret; |
| 1574 | char *dest; |
Luis Hector Chavez | 0bacbf8 | 2018-07-10 20:06:55 -0700 | [diff] [blame] | 1575 | int remount = 0; |
| 1576 | unsigned long original_mnt_flags = 0; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1577 | |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 1578 | /* We assume |dest| has a leading "/". */ |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1579 | if (dev_path && strncmp("/dev/", m->dest, 5) == 0) { |
Jorge Lucangeli Obes | 9299cae | 2019-08-23 11:28:39 -0400 | [diff] [blame] | 1580 | /* |
Jorge Lucangeli Obes | 7654c6e | 2019-09-09 10:45:38 -0400 | [diff] [blame] | 1581 | * Since the temp path is rooted at /dev, skip that dest part. |
Jorge Lucangeli Obes | 9299cae | 2019-08-23 11:28:39 -0400 | [diff] [blame] | 1582 | */ |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1583 | if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0) |
| 1584 | return -ENOMEM; |
| 1585 | } else { |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1586 | if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0) |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1587 | return -ENOMEM; |
| 1588 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1589 | |
Luis Hector Chavez | 0bacbf8 | 2018-07-10 20:06:55 -0700 | [diff] [blame] | 1590 | ret = |
| 1591 | setup_mount_destination(m->src, dest, j->uid, j->gid, |
| 1592 | (m->flags & MS_BIND), &original_mnt_flags); |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1593 | if (ret) { |
yusukes | 1b32f85 | 2018-03-05 10:24:58 -0800 | [diff] [blame] | 1594 | warn("creating mount target '%s' failed", dest); |
Luis Hector Chavez | 8c3acbc | 2017-10-24 16:45:00 -0700 | [diff] [blame] | 1595 | goto error; |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1596 | } |
Dylan Reid | eec7796 | 2016-06-30 19:35:10 -0700 | [diff] [blame] | 1597 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1598 | /* |
Luis Hector Chavez | 0bacbf8 | 2018-07-10 20:06:55 -0700 | [diff] [blame] | 1599 | * Bind mounts that change the 'ro' flag have to be remounted since |
| 1600 | * 'bind' and other flags can't both be specified in the same command. |
| 1601 | * Remount after the initial mount. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1602 | */ |
Luis Hector Chavez | 0bacbf8 | 2018-07-10 20:06:55 -0700 | [diff] [blame] | 1603 | if ((m->flags & MS_BIND) && |
| 1604 | ((m->flags & MS_RDONLY) != (original_mnt_flags & MS_RDONLY))) { |
| 1605 | remount = 1; |
| 1606 | /* |
| 1607 | * Restrict the mount flags to those that are user-settable in a |
| 1608 | * MS_REMOUNT request, but excluding MS_RDONLY. The |
| 1609 | * user-requested mount flags will dictate whether the remount |
| 1610 | * will have that flag or not. |
| 1611 | */ |
| 1612 | original_mnt_flags &= (MS_USER_SETTABLE_MASK & ~MS_RDONLY); |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 1613 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1614 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1615 | ret = mount(m->src, dest, m->type, m->flags, m->data); |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1616 | if (ret) { |
Luis Hector Chavez | 0bacbf8 | 2018-07-10 20:06:55 -0700 | [diff] [blame] | 1617 | pwarn("bind: %s -> %s flags=%#lx", m->src, dest, m->flags); |
Luis Hector Chavez | 8c3acbc | 2017-10-24 16:45:00 -0700 | [diff] [blame] | 1618 | goto error; |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1619 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1620 | |
Luis Hector Chavez | 0bacbf8 | 2018-07-10 20:06:55 -0700 | [diff] [blame] | 1621 | if (remount) { |
| 1622 | ret = |
| 1623 | mount(m->src, dest, NULL, |
| 1624 | m->flags | original_mnt_flags | MS_REMOUNT, m->data); |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1625 | if (ret) { |
Luis Hector Chavez | 0bacbf8 | 2018-07-10 20:06:55 -0700 | [diff] [blame] | 1626 | pwarn("bind remount: %s -> %s flags=%#lx", m->src, dest, |
| 1627 | m->flags | original_mnt_flags | MS_REMOUNT); |
Luis Hector Chavez | 8c3acbc | 2017-10-24 16:45:00 -0700 | [diff] [blame] | 1628 | goto error; |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1629 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1632 | free(dest); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1633 | if (m->next) |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1634 | return mount_one(j, m->next, dev_path); |
Luis Hector Chavez | 8c3acbc | 2017-10-24 16:45:00 -0700 | [diff] [blame] | 1635 | return 0; |
| 1636 | |
| 1637 | error: |
| 1638 | free(dest); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1639 | return ret; |
| 1640 | } |
| 1641 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1642 | static void process_mounts_or_die(const struct minijail *j) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 1643 | { |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1644 | /* |
| 1645 | * We have to mount /dev first in case there are bind mounts from |
| 1646 | * the original /dev into the new unique tmpfs one. |
| 1647 | */ |
| 1648 | char *dev_path = NULL; |
| 1649 | if (j->flags.mount_dev && mount_dev(&dev_path)) |
| 1650 | pdie("mount_dev failed"); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1651 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1652 | if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) { |
| 1653 | if (dev_path) { |
| 1654 | int saved_errno = errno; |
| 1655 | mount_dev_cleanup(dev_path); |
| 1656 | errno = saved_errno; |
| 1657 | } |
| 1658 | pdie("mount_one failed"); |
| 1659 | } |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1660 | |
| 1661 | /* |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1662 | * Once all bind mounts have been processed, move the temp dev to |
| 1663 | * its final /dev home. |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1664 | */ |
| 1665 | if (j->flags.mount_dev && mount_dev_finalize(j, dev_path)) |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1666 | pdie("mount_dev_finalize failed"); |
| 1667 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1668 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1669 | static int enter_chroot(const struct minijail *j) |
| 1670 | { |
Luis Hector Chavez | 64730af | 2017-09-13 13:18:59 -0700 | [diff] [blame] | 1671 | run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT); |
| 1672 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1673 | if (chroot(j->chrootdir)) |
| 1674 | return -errno; |
| 1675 | |
| 1676 | if (chdir("/")) |
| 1677 | return -errno; |
| 1678 | |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1682 | static int enter_pivot_root(const struct minijail *j) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1683 | { |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1684 | int oldroot, newroot; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1685 | |
Luis Hector Chavez | 64730af | 2017-09-13 13:18:59 -0700 | [diff] [blame] | 1686 | run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT); |
| 1687 | |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1688 | /* |
| 1689 | * Keep the fd for both old and new root. |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1690 | * It will be used in fchdir(2) later. |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1691 | */ |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1692 | oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1693 | if (oldroot < 0) |
| 1694 | pdie("failed to open / for fchdir"); |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1695 | newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1696 | if (newroot < 0) |
| 1697 | pdie("failed to open %s for fchdir", j->chrootdir); |
| 1698 | |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1699 | /* |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1700 | * To ensure j->chrootdir is the root of a filesystem, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1701 | * do a self bind mount. |
| 1702 | */ |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1703 | if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, "")) |
| 1704 | pdie("failed to bind mount '%s'", j->chrootdir); |
| 1705 | if (chdir(j->chrootdir)) |
| 1706 | return -errno; |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1707 | if (syscall(SYS_pivot_root, ".", ".")) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1708 | pdie("pivot_root"); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1709 | |
| 1710 | /* |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1711 | * Now the old root is mounted on top of the new root. Use fchdir(2) to |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1712 | * change to the old root and unmount it. |
| 1713 | */ |
| 1714 | if (fchdir(oldroot)) |
| 1715 | pdie("failed to fchdir to old /"); |
Hidehiko Abe | 097b719 | 2016-03-16 18:00:36 +0900 | [diff] [blame] | 1716 | |
| 1717 | /* |
Mike Frysinger | 785b1c3 | 2018-02-23 15:47:24 -0500 | [diff] [blame] | 1718 | * If skip_remount_private was enabled for minijail_enter(), |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1719 | * there could be a shared mount point under |oldroot|. In that case, |
| 1720 | * mounts under this shared mount point will be unmounted below, and |
| 1721 | * this unmounting will propagate to the original mount namespace |
| 1722 | * (because the mount point is shared). To prevent this unexpected |
| 1723 | * unmounting, remove these mounts from their peer groups by recursively |
| 1724 | * remounting them as MS_PRIVATE. |
Hidehiko Abe | 097b719 | 2016-03-16 18:00:36 +0900 | [diff] [blame] | 1725 | */ |
| 1726 | if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL)) |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1727 | pdie("failed to mount(/, private) before umount(/)"); |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1728 | /* The old root might be busy, so use lazy unmount. */ |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1729 | if (umount2(".", MNT_DETACH)) |
| 1730 | pdie("umount(/)"); |
| 1731 | /* Change back to the new root. */ |
| 1732 | if (fchdir(newroot)) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1733 | return -errno; |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1734 | if (close(oldroot)) |
| 1735 | return -errno; |
| 1736 | if (close(newroot)) |
| 1737 | return -errno; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1738 | if (chroot("/")) |
| 1739 | return -errno; |
Jorge Lucangeli Obes | 46a5509 | 2015-10-12 15:31:59 -0700 | [diff] [blame] | 1740 | /* Set correct CWD for getcwd(3). */ |
| 1741 | if (chdir("/")) |
| 1742 | return -errno; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1743 | |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1747 | static int mount_tmp(const struct minijail *j) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1748 | { |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1749 | const char fmt[] = "size=%zu,mode=1777"; |
| 1750 | /* Count for the user storing ULLONG_MAX literally + extra space. */ |
| 1751 | char data[sizeof(fmt) + sizeof("18446744073709551615ULL")]; |
| 1752 | int ret; |
| 1753 | |
| 1754 | ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size); |
| 1755 | |
| 1756 | if (ret <= 0) |
| 1757 | pdie("tmpfs size spec error"); |
| 1758 | else if ((size_t)ret >= sizeof(data)) |
| 1759 | pdie("tmpfs size spec too large"); |
Mike Frysinger | b91d404 | 2017-01-13 19:03:34 -0500 | [diff] [blame] | 1760 | return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1761 | data); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1764 | static int remount_proc_readonly(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1765 | { |
| 1766 | const char *kProcPath = "/proc"; |
| 1767 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1768 | /* |
| 1769 | * Right now, we're holding a reference to our parent's old mount of |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1770 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 1771 | * mutate our parent's mount as well, even though we're in a VFS |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1772 | * namespace (!). Instead, remove their mount from our namespace lazily |
| 1773 | * (MNT_DETACH) and make our own. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1774 | */ |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1775 | if (umount2(kProcPath, MNT_DETACH)) { |
| 1776 | /* |
| 1777 | * If we are in a new user namespace, umount(2) will fail. |
| 1778 | * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html |
| 1779 | */ |
| 1780 | if (j->flags.userns) { |
| 1781 | info("umount(/proc, MNT_DETACH) failed, " |
| 1782 | "this is expected when using user namespaces"); |
| 1783 | } else { |
| 1784 | return -errno; |
| 1785 | } |
| 1786 | } |
Mike Frysinger | 3ba8157 | 2017-01-17 23:33:28 -0500 | [diff] [blame] | 1787 | if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1788 | return -errno; |
| 1789 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1790 | } |
| 1791 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1792 | static void kill_child_and_die(const struct minijail *j, const char *msg) |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1793 | { |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1794 | kill(j->initpid, SIGKILL); |
| 1795 | die("%s", msg); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1796 | } |
| 1797 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1798 | static void write_pid_file_or_die(const struct minijail *j) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1799 | { |
Keshav Santhanam | db6dab4 | 2016-08-10 16:33:34 -0700 | [diff] [blame] | 1800 | if (write_pid_to_path(j->initpid, j->pid_file_path)) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1801 | kill_child_and_die(j, "failed to write pid file"); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1802 | } |
| 1803 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1804 | static void add_to_cgroups_or_die(const struct minijail *j) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1805 | { |
| 1806 | size_t i; |
| 1807 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1808 | for (i = 0; i < j->cgroup_count; ++i) { |
Keshav Santhanam | db6dab4 | 2016-08-10 16:33:34 -0700 | [diff] [blame] | 1809 | if (write_pid_to_path(j->initpid, j->cgroups[i])) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1810 | kill_child_and_die(j, "failed to add to cgroups"); |
| 1811 | } |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1812 | } |
| 1813 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 1814 | static void set_rlimits_or_die(const struct minijail *j) |
| 1815 | { |
| 1816 | size_t i; |
| 1817 | |
| 1818 | for (i = 0; i < j->rlimit_count; ++i) { |
| 1819 | struct rlimit limit; |
| 1820 | limit.rlim_cur = j->rlimits[i].cur; |
| 1821 | limit.rlim_max = j->rlimits[i].max; |
| 1822 | if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL)) |
| 1823 | kill_child_and_die(j, "failed to set rlimit"); |
| 1824 | } |
| 1825 | } |
| 1826 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1827 | static void write_ugid_maps_or_die(const struct minijail *j) |
| 1828 | { |
| 1829 | if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0) |
| 1830 | kill_child_and_die(j, "failed to write uid_map"); |
Mike Frysinger | 6b190c0 | 2017-01-04 17:18:42 -0500 | [diff] [blame] | 1831 | if (j->gidmap && j->flags.disable_setgroups) { |
Jorge Lucangeli Obes | 9341806 | 2019-09-27 10:59:45 -0400 | [diff] [blame] | 1832 | /* |
| 1833 | * Older kernels might not have the /proc/<pid>/setgroups files. |
| 1834 | */ |
Mike Frysinger | 6b190c0 | 2017-01-04 17:18:42 -0500 | [diff] [blame] | 1835 | int ret = write_proc_file(j->initpid, "deny", "setgroups"); |
Mike Frysinger | eea841b | 2017-01-13 18:11:57 -0500 | [diff] [blame] | 1836 | if (ret != 0) { |
Mike Frysinger | 6b190c0 | 2017-01-04 17:18:42 -0500 | [diff] [blame] | 1837 | if (ret == -ENOENT) { |
| 1838 | /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */ |
| 1839 | warn("could not disable setgroups(2)"); |
| 1840 | } else |
Jorge Lucangeli Obes | 9341806 | 2019-09-27 10:59:45 -0400 | [diff] [blame] | 1841 | kill_child_and_die( |
| 1842 | j, "failed to disable setgroups(2)"); |
Mike Frysinger | 6b190c0 | 2017-01-04 17:18:42 -0500 | [diff] [blame] | 1843 | } |
| 1844 | } |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1845 | if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0) |
| 1846 | kill_child_and_die(j, "failed to write gid_map"); |
| 1847 | } |
| 1848 | |
| 1849 | static void enter_user_namespace(const struct minijail *j) |
| 1850 | { |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 1851 | int uid = j->flags.uid ? j->uid : 0; |
| 1852 | int gid = j->flags.gid ? j->gid : 0; |
| 1853 | if (j->gidmap && setresgid(gid, gid, gid)) { |
| 1854 | pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid, |
| 1855 | gid); |
| 1856 | } |
| 1857 | if (j->uidmap && setresuid(uid, uid, uid)) { |
| 1858 | pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid, |
| 1859 | uid); |
| 1860 | } |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | static void parent_setup_complete(int *pipe_fds) |
| 1864 | { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 1865 | close_and_reset(&pipe_fds[0]); |
| 1866 | close_and_reset(&pipe_fds[1]); |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | /* |
| 1870 | * wait_for_parent_setup: Called by the child process to wait for any |
| 1871 | * further parent-side setup to complete before continuing. |
| 1872 | */ |
| 1873 | static void wait_for_parent_setup(int *pipe_fds) |
| 1874 | { |
| 1875 | char buf; |
| 1876 | |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 1877 | close_and_reset(&pipe_fds[1]); |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1878 | |
| 1879 | /* Wait for parent to complete setup and close the pipe. */ |
| 1880 | if (read(pipe_fds[0], &buf, 1) != 0) |
| 1881 | die("failed to sync with parent"); |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 1882 | close_and_reset(&pipe_fds[0]); |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | static void drop_ugid(const struct minijail *j) |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1886 | { |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1887 | if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids + |
| 1888 | j->flags.set_suppl_gids > 1) { |
Jorge Lucangeli Obes | 3454319 | 2017-01-11 16:07:57 -0500 | [diff] [blame] | 1889 | die("can only do one of inherit, keep, or set supplementary " |
| 1890 | "groups"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1891 | } |
| 1892 | |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1893 | if (j->flags.inherit_suppl_gids) { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1894 | if (initgroups(j->user, j->usergid)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1895 | pdie("initgroups(%s, %d) failed", j->user, j->usergid); |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1896 | } else if (j->flags.set_suppl_gids) { |
| 1897 | if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1898 | pdie("setgroups(suppl_gids) failed"); |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 1899 | } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) { |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1900 | /* |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1901 | * Only attempt to clear supplementary groups if we are changing |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 1902 | * users or groups, and if the caller did not request to disable |
| 1903 | * setgroups (used when entering a user namespace as a |
| 1904 | * non-privileged user). |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1905 | */ |
Jorge Lucangeli Obes | 2449956 | 2016-12-01 11:59:27 -0500 | [diff] [blame] | 1906 | if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1907 | pdie("setgroups(0, NULL) failed"); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1911 | pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1912 | |
| 1913 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1914 | pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1917 | static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap) |
| 1918 | { |
| 1919 | const uint64_t one = 1; |
| 1920 | unsigned int i; |
| 1921 | for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) { |
| 1922 | if (keep_mask & (one << i)) |
| 1923 | continue; |
| 1924 | if (prctl(PR_CAPBSET_DROP, i)) |
| 1925 | pdie("could not drop capability from bounding set"); |
| 1926 | } |
| 1927 | } |
| 1928 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1929 | static void drop_caps(const struct minijail *j, unsigned int last_valid_cap) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1930 | { |
Jorge Lucangeli Obes | 7ea269e | 2016-02-26 22:07:09 -0800 | [diff] [blame] | 1931 | if (!j->flags.use_caps) |
| 1932 | return; |
| 1933 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1934 | cap_t caps = cap_get_proc(); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1935 | cap_value_t flag[1]; |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1936 | const size_t ncaps = sizeof(j->caps) * 8; |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1937 | const uint64_t one = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1938 | unsigned int i; |
| 1939 | if (!caps) |
| 1940 | die("can't get process caps"); |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1941 | if (cap_clear(caps)) |
| 1942 | die("can't clear caps"); |
| 1943 | |
| 1944 | for (i = 0; i < ncaps && i <= last_valid_cap; ++i) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1945 | /* Keep CAP_SETPCAP for dropping bounding set bits. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1946 | if (i != CAP_SETPCAP && !(j->caps & (one << i))) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1947 | continue; |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1948 | flag[0] = i; |
| 1949 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1950 | die("can't add effective cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1951 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1952 | die("can't add permitted cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1953 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1954 | die("can't add inheritable cap"); |
| 1955 | } |
| 1956 | if (cap_set_proc(caps)) |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1957 | die("can't apply initial cleaned capset"); |
| 1958 | |
| 1959 | /* |
Jorge Lucangeli Obes | 5423421 | 2018-04-26 11:52:15 -0400 | [diff] [blame] | 1960 | * Instead of dropping the bounding set first, do it here in case |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1961 | * the caller had a more permissive bounding set which could |
| 1962 | * have been used above to raise a capability that wasn't already |
| 1963 | * present. This requires CAP_SETPCAP, so we raised/kept it above. |
Jorge Lucangeli Obes | 5423421 | 2018-04-26 11:52:15 -0400 | [diff] [blame] | 1964 | * |
| 1965 | * However, if we're asked to skip setting *and* locking the |
| 1966 | * SECURE_NOROOT securebit, also skip dropping the bounding set. |
| 1967 | * If the caller wants to regain all capabilities when executing a |
| 1968 | * set-user-ID-root program, allow them to do so. The default behavior |
| 1969 | * (i.e. the behavior without |securebits_skip_mask| set) will still put |
| 1970 | * the jailed process tree in a capabilities-only environment. |
| 1971 | * |
| 1972 | * We check the negated skip mask for SECURE_NOROOT and |
| 1973 | * SECURE_NOROOT_LOCKED. If the bits are set in the negated mask they |
| 1974 | * will *not* be skipped in lock_securebits(), and therefore we should |
| 1975 | * drop the bounding set. |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1976 | */ |
Jorge Lucangeli Obes | 5423421 | 2018-04-26 11:52:15 -0400 | [diff] [blame] | 1977 | if (secure_noroot_set_and_locked(~j->securebits_skip_mask)) { |
| 1978 | drop_capbset(j->caps, last_valid_cap); |
| 1979 | } else { |
| 1980 | warn("SECURE_NOROOT not set, not dropping bounding set"); |
| 1981 | } |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1982 | |
| 1983 | /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1984 | if ((j->caps & (one << CAP_SETPCAP)) == 0) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1985 | flag[0] = CAP_SETPCAP; |
| 1986 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR)) |
| 1987 | die("can't clear effective cap"); |
| 1988 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR)) |
| 1989 | die("can't clear permitted cap"); |
| 1990 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR)) |
| 1991 | die("can't clear inheritable cap"); |
| 1992 | } |
| 1993 | |
| 1994 | if (cap_set_proc(caps)) |
| 1995 | die("can't apply final cleaned capset"); |
| 1996 | |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1997 | /* |
| 1998 | * If ambient capabilities are supported, clear all capabilities first, |
| 1999 | * then raise the requested ones. |
| 2000 | */ |
| 2001 | if (j->flags.set_ambient_caps) { |
| 2002 | if (!cap_ambient_supported()) { |
| 2003 | pdie("ambient capabilities not supported"); |
| 2004 | } |
Jorge Lucangeli Obes | f6058c3 | 2017-04-26 10:26:59 -0400 | [diff] [blame] | 2005 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != |
| 2006 | 0) { |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 2007 | pdie("can't clear ambient capabilities"); |
| 2008 | } |
| 2009 | |
| 2010 | for (i = 0; i < ncaps && i <= last_valid_cap; ++i) { |
| 2011 | if (!(j->caps & (one << i))) |
| 2012 | continue; |
| 2013 | |
| 2014 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, |
| 2015 | 0) != 0) { |
| 2016 | pdie("prctl(PR_CAP_AMBIENT, " |
| 2017 | "PR_CAP_AMBIENT_RAISE, %u) failed", |
| 2018 | i); |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 2023 | cap_free(caps); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2024 | } |
| 2025 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2026 | static void set_seccomp_filter(const struct minijail *j) |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2027 | { |
| 2028 | /* |
| 2029 | * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c> |
| 2030 | * in the kernel source tree for an explanation of the parameters. |
| 2031 | */ |
| 2032 | if (j->flags.no_new_privs) { |
| 2033 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 2034 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 2035 | } |
| 2036 | |
| 2037 | /* |
Jorge Lucangeli Obes | 2413f37 | 2016-04-06 18:43:10 -0700 | [diff] [blame] | 2038 | * Code running with ASan |
| 2039 | * (https://github.com/google/sanitizers/wiki/AddressSanitizer) |
| 2040 | * will make system calls not included in the syscall filter policy, |
| 2041 | * which will likely crash the program. Skip setting seccomp filter in |
| 2042 | * that case. |
| 2043 | * 'running_with_asan()' has no inputs and is completely defined at |
| 2044 | * build time, so this cannot be used by an attacker to skip setting |
| 2045 | * seccomp filter. |
| 2046 | */ |
Evgenii Stepanov | 3d98f3c | 2018-08-23 15:06:50 -0700 | [diff] [blame] | 2047 | if (j->flags.seccomp_filter && running_with_asan()) { |
Evgenii Stepanov | 825828c | 2018-07-27 11:57:07 -0700 | [diff] [blame] | 2048 | warn("running with (HW)ASan, not setting seccomp filter"); |
Jorge Lucangeli Obes | 2413f37 | 2016-04-06 18:43:10 -0700 | [diff] [blame] | 2049 | return; |
| 2050 | } |
| 2051 | |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 2052 | if (j->flags.seccomp_filter) { |
| 2053 | if (j->flags.seccomp_filter_logging) { |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 2054 | warn("logging seccomp filter failures"); |
Jorge Lucangeli Obes | 32201f8 | 2019-06-12 14:45:06 -0400 | [diff] [blame] | 2055 | if (!seccomp_ret_log_available()) { |
| 2056 | /* |
| 2057 | * If SECCOMP_RET_LOG is not available, |
| 2058 | * install the SIGSYS handler first. |
| 2059 | */ |
| 2060 | if (install_sigsys_handler()) |
| 2061 | pdie( |
| 2062 | "failed to install SIGSYS handler"); |
| 2063 | } |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 2064 | } else if (j->flags.seccomp_filter_tsync) { |
| 2065 | /* |
| 2066 | * If setting thread sync, |
| 2067 | * reset the SIGSYS signal handler so that |
| 2068 | * the entire thread group is killed. |
| 2069 | */ |
| 2070 | if (signal(SIGSYS, SIG_DFL) == SIG_ERR) |
| 2071 | pdie("failed to reset SIGSYS disposition"); |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 2072 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | /* |
| 2076 | * Install the syscall filter. |
| 2077 | */ |
| 2078 | if (j->flags.seccomp_filter) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 2079 | if (j->flags.seccomp_filter_tsync) { |
| 2080 | if (sys_seccomp(SECCOMP_SET_MODE_FILTER, |
| 2081 | SECCOMP_FILTER_FLAG_TSYNC, |
| 2082 | j->filter_prog)) { |
| 2083 | pdie("seccomp(tsync) failed"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 2084 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 2085 | } else { |
| 2086 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, |
| 2087 | j->filter_prog)) { |
| 2088 | pdie("prctl(seccomp_filter) failed"); |
| 2089 | } |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 2090 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2091 | } |
| 2092 | } |
| 2093 | |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 2094 | static pid_t forward_pid = -1; |
| 2095 | |
Mike Frysinger | 33d051a | 2018-05-30 16:41:10 -0400 | [diff] [blame] | 2096 | static void forward_signal(int sig, |
Mike Frysinger | d9ef07c | 2018-05-30 16:51:36 -0400 | [diff] [blame] | 2097 | siginfo_t *siginfo attribute_unused, |
| 2098 | void *void_context attribute_unused) |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 2099 | { |
| 2100 | if (forward_pid != -1) { |
Mike Frysinger | 33d051a | 2018-05-30 16:41:10 -0400 | [diff] [blame] | 2101 | kill(forward_pid, sig); |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | static void install_signal_handlers(void) |
| 2106 | { |
| 2107 | struct sigaction act; |
| 2108 | |
| 2109 | memset(&act, 0, sizeof(act)); |
| 2110 | act.sa_sigaction = &forward_signal; |
| 2111 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
| 2112 | |
| 2113 | /* Handle all signals, except SIGCHLD. */ |
Mike Frysinger | 33d051a | 2018-05-30 16:41:10 -0400 | [diff] [blame] | 2114 | for (int sig = 1; sig < NSIG; sig++) { |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 2115 | /* |
| 2116 | * We don't care if we get EINVAL: that just means that we |
| 2117 | * can't handle this signal, so let's skip it and continue. |
| 2118 | */ |
Mike Frysinger | 33d051a | 2018-05-30 16:41:10 -0400 | [diff] [blame] | 2119 | sigaction(sig, &act, NULL); |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 2120 | } |
| 2121 | /* Reset SIGCHLD's handler. */ |
| 2122 | signal(SIGCHLD, SIG_DFL); |
| 2123 | |
| 2124 | /* Handle real-time signals. */ |
Mike Frysinger | 33d051a | 2018-05-30 16:41:10 -0400 | [diff] [blame] | 2125 | for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) { |
| 2126 | sigaction(sig, &act, NULL); |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 2127 | } |
| 2128 | } |
| 2129 | |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 2130 | static const char *lookup_hook_name(minijail_hook_event_t event) |
| 2131 | { |
| 2132 | switch (event) { |
| 2133 | case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS: |
| 2134 | return "pre-drop-caps"; |
| 2135 | case MINIJAIL_HOOK_EVENT_PRE_EXECVE: |
| 2136 | return "pre-execve"; |
Luis Hector Chavez | 64730af | 2017-09-13 13:18:59 -0700 | [diff] [blame] | 2137 | case MINIJAIL_HOOK_EVENT_PRE_CHROOT: |
| 2138 | return "pre-chroot"; |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 2139 | case MINIJAIL_HOOK_EVENT_MAX: |
| 2140 | /* |
| 2141 | * Adding this in favor of a default case to force the |
| 2142 | * compiler to error out if a new enum value is added. |
| 2143 | */ |
| 2144 | break; |
| 2145 | } |
| 2146 | return "unknown"; |
| 2147 | } |
| 2148 | |
| 2149 | static void run_hooks_or_die(const struct minijail *j, |
| 2150 | minijail_hook_event_t event) |
| 2151 | { |
| 2152 | int rc; |
| 2153 | int hook_index = 0; |
| 2154 | for (struct hook *c = j->hooks_head; c; c = c->next) { |
| 2155 | if (c->event != event) |
| 2156 | continue; |
| 2157 | rc = c->hook(c->payload); |
| 2158 | if (rc != 0) { |
| 2159 | errno = -rc; |
| 2160 | pdie("%s hook (index %d) failed", |
| 2161 | lookup_hook_name(event), hook_index); |
| 2162 | } |
| 2163 | /* Only increase the index within the same hook event type. */ |
| 2164 | ++hook_index; |
| 2165 | } |
| 2166 | } |
| 2167 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2168 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2169 | { |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 2170 | /* |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 2171 | * If we're dropping caps, get the last valid cap from /proc now, |
| 2172 | * since /proc can be unmounted before drop_caps() is called. |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 2173 | */ |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 2174 | unsigned int last_valid_cap = 0; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 2175 | if (j->flags.capbset_drop || j->flags.use_caps) |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 2176 | last_valid_cap = get_last_valid_cap(); |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 2177 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2178 | if (j->flags.pids) |
| 2179 | die("tried to enter a pid-namespaced jail;" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 2180 | " try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2181 | |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 2182 | if (j->flags.inherit_suppl_gids && !j->user) |
Jorge Lucangeli Obes | 3454319 | 2017-01-11 16:07:57 -0500 | [diff] [blame] | 2183 | die("cannot inherit supplementary groups without setting a " |
| 2184 | "username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2185 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2186 | /* |
| 2187 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2188 | * so we don't even try. If any of our operations fail, we abort() the |
| 2189 | * entire process. |
| 2190 | */ |
Mike Frysinger | 902a449 | 2018-12-27 05:22:56 -0500 | [diff] [blame] | 2191 | if (j->flags.enter_vfs) { |
| 2192 | if (setns(j->mountns_fd, CLONE_NEWNS)) |
| 2193 | pdie("setns(CLONE_NEWNS) failed"); |
| 2194 | close(j->mountns_fd); |
| 2195 | } |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 2196 | |
Jorge Lucangeli Obes | 805be39 | 2015-10-12 15:55:59 -0700 | [diff] [blame] | 2197 | if (j->flags.vfs) { |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 2198 | if (unshare(CLONE_NEWNS)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2199 | pdie("unshare(CLONE_NEWNS) failed"); |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 2200 | /* |
Mike Frysinger | 785b1c3 | 2018-02-23 15:47:24 -0500 | [diff] [blame] | 2201 | * By default, remount all filesystems as private, unless |
Jorge Lucangeli Obes | 9341806 | 2019-09-27 10:59:45 -0400 | [diff] [blame] | 2202 | * - Passed a specific remount mode, in which case remount with |
| 2203 | * that, |
| 2204 | * - Asked not to remount at all, in which case skip the |
| 2205 | * mount(2) call. |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 2206 | * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt |
| 2207 | */ |
Mike Frysinger | 785b1c3 | 2018-02-23 15:47:24 -0500 | [diff] [blame] | 2208 | if (j->remount_mode) { |
Jorge Lucangeli Obes | 9341806 | 2019-09-27 10:59:45 -0400 | [diff] [blame] | 2209 | if (mount(NULL, "/", NULL, MS_REC | j->remount_mode, |
| 2210 | NULL)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2211 | pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE," |
| 2212 | " NULL) failed"); |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 2213 | } |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 2214 | } |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 2215 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 2216 | if (j->flags.ipc && unshare(CLONE_NEWIPC)) { |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2217 | pdie("unshare(CLONE_NEWIPC) failed"); |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 2218 | } |
| 2219 | |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 2220 | if (j->flags.uts) { |
| 2221 | if (unshare(CLONE_NEWUTS)) |
| 2222 | pdie("unshare(CLONE_NEWUTS) failed"); |
| 2223 | |
| 2224 | if (j->hostname && sethostname(j->hostname, strlen(j->hostname))) |
| 2225 | pdie("sethostname(%s) failed", j->hostname); |
| 2226 | } |
| 2227 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 2228 | if (j->flags.enter_net) { |
| 2229 | if (setns(j->netns_fd, CLONE_NEWNET)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2230 | pdie("setns(CLONE_NEWNET) failed"); |
Mike Frysinger | 902a449 | 2018-12-27 05:22:56 -0500 | [diff] [blame] | 2231 | close(j->netns_fd); |
Mike Frysinger | 7559dfe | 2016-11-15 18:58:39 -0500 | [diff] [blame] | 2232 | } else if (j->flags.net) { |
| 2233 | if (unshare(CLONE_NEWNET)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2234 | pdie("unshare(CLONE_NEWNET) failed"); |
| 2235 | config_net_loopback(); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 2236 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2237 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 2238 | if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2239 | pdie("unshare(CLONE_NEWCGROUP) failed"); |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 2240 | |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 2241 | if (j->flags.new_session_keyring) { |
| 2242 | if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0) |
| 2243 | pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed"); |
| 2244 | } |
| 2245 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 2246 | /* We have to process all the mounts before we chroot/pivot_root. */ |
| 2247 | process_mounts_or_die(j); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 2248 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 2249 | if (j->flags.chroot && enter_chroot(j)) |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 2250 | pdie("chroot"); |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 2251 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 2252 | if (j->flags.pivot_root && enter_pivot_root(j)) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 2253 | pdie("pivot_root"); |
| 2254 | |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 2255 | if (j->flags.mount_tmp && mount_tmp(j)) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 2256 | pdie("mount_tmp"); |
| 2257 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 2258 | if (j->flags.remount_proc_ro && remount_proc_readonly(j)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2259 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2260 | |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 2261 | run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS); |
| 2262 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 2263 | /* |
| 2264 | * If we're only dropping capabilities from the bounding set, but not |
| 2265 | * from the thread's (permitted|inheritable|effective) sets, do it now. |
| 2266 | */ |
| 2267 | if (j->flags.capbset_drop) { |
| 2268 | drop_capbset(j->cap_bset, last_valid_cap); |
| 2269 | } |
| 2270 | |
Luis Hector Chavez | 89cbc32 | 2018-08-06 11:31:15 -0700 | [diff] [blame] | 2271 | /* |
Mattias Nissler | 48b5ff1 | 2018-10-11 15:31:41 +0200 | [diff] [blame] | 2272 | * POSIX capabilities are a bit tricky. We must set SECBIT_KEEP_CAPS |
| 2273 | * before drop_ugid() below as the latter would otherwise drop all |
| 2274 | * capabilities. |
Luis Hector Chavez | 89cbc32 | 2018-08-06 11:31:15 -0700 | [diff] [blame] | 2275 | */ |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 2276 | if (j->flags.use_caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2277 | /* |
Mattias Nissler | 48b5ff1 | 2018-10-11 15:31:41 +0200 | [diff] [blame] | 2278 | * When using ambient capabilities, CAP_SET{GID,UID} can be |
| 2279 | * inherited across execve(2), so SECBIT_KEEP_CAPS is not |
| 2280 | * strictly needed. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2281 | */ |
Mattias Nissler | 48b5ff1 | 2018-10-11 15:31:41 +0200 | [diff] [blame] | 2282 | bool require_keep_caps = !j->flags.set_ambient_caps; |
| 2283 | if (lock_securebits(j->securebits_skip_mask, |
| 2284 | require_keep_caps) < 0) { |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 2285 | pdie("locking securebits failed"); |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 2286 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2287 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2288 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 2289 | if (j->flags.no_new_privs) { |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 2290 | /* |
| 2291 | * If we're setting no_new_privs, we can drop privileges |
| 2292 | * before setting seccomp filter. This way filter policies |
| 2293 | * don't need to allow privilege-dropping syscalls. |
| 2294 | */ |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2295 | drop_ugid(j); |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 2296 | drop_caps(j, last_valid_cap); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2297 | set_seccomp_filter(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2298 | } else { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2299 | /* |
| 2300 | * If we're not setting no_new_privs, |
| 2301 | * we need to set seccomp filter *before* dropping privileges. |
| 2302 | * WARNING: this means that filter policies *must* allow |
| 2303 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 2304 | * capget()/capset()/prctl() for dropping caps. |
| 2305 | */ |
| 2306 | set_seccomp_filter(j); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2307 | drop_ugid(j); |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 2308 | drop_caps(j, last_valid_cap); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2309 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2310 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2311 | /* |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 2312 | * Select the specified alternate syscall table. The table must not |
| 2313 | * block prctl(2) if we're using seccomp as well. |
| 2314 | */ |
| 2315 | if (j->flags.alt_syscall) { |
| 2316 | if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2317 | pdie("prctl(PR_ALT_SYSCALL) failed"); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | /* |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2321 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2322 | * privilege-dropping syscalls :) |
| 2323 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 2324 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) { |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 2325 | if ((errno == EINVAL) && seccomp_can_softfail()) { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 2326 | warn("seccomp not supported"); |
| 2327 | return; |
| 2328 | } |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2329 | pdie("prctl(PR_SET_SECCOMP) failed"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 2330 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2331 | } |
| 2332 | |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 2333 | /* TODO(wad): will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2334 | static int init_exitstatus = 0; |
| 2335 | |
Mike Frysinger | d9ef07c | 2018-05-30 16:51:36 -0400 | [diff] [blame] | 2336 | void init_term(int sig attribute_unused) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2337 | { |
| 2338 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2339 | } |
| 2340 | |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2341 | void init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2342 | { |
| 2343 | pid_t pid; |
| 2344 | int status; |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 2345 | /* So that we exit with the right status. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2346 | signal(SIGTERM, init_term); |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 2347 | /* TODO(wad): self jail with seccomp filters here. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2348 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2349 | /* |
| 2350 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2351 | * left inside our pid namespace or we get a signal. |
| 2352 | */ |
| 2353 | if (pid == rootpid) |
| 2354 | init_exitstatus = status; |
| 2355 | } |
| 2356 | if (!WIFEXITED(init_exitstatus)) |
| 2357 | _exit(MINIJAIL_ERR_INIT); |
| 2358 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2359 | } |
| 2360 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2361 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2362 | { |
| 2363 | size_t sz = 0; |
| 2364 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 2365 | char *buf; |
| 2366 | int r; |
| 2367 | if (sizeof(sz) != bytes) |
| 2368 | return -EINVAL; |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2369 | if (sz > USHRT_MAX) /* arbitrary sanity check */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2370 | return -E2BIG; |
| 2371 | buf = malloc(sz); |
| 2372 | if (!buf) |
| 2373 | return -ENOMEM; |
| 2374 | bytes = read(fd, buf, sz); |
| 2375 | if (bytes != sz) { |
| 2376 | free(buf); |
| 2377 | return -EINVAL; |
| 2378 | } |
| 2379 | r = minijail_unmarshal(j, buf, sz); |
| 2380 | free(buf); |
| 2381 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 2382 | } |
| 2383 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2384 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2385 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2386 | size_t sz = minijail_size(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2387 | if (!sz) |
| 2388 | return -EINVAL; |
François Degros | 664eba7 | 2019-11-05 13:18:24 +1100 | [diff] [blame] | 2389 | |
| 2390 | char *buf = malloc(sz); |
| 2391 | if (!buf) |
| 2392 | return -ENOMEM; |
| 2393 | |
| 2394 | int err = minijail_marshal(j, buf, sz); |
| 2395 | if (err) |
| 2396 | goto error; |
| 2397 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2398 | /* Sends [size][minijail]. */ |
François Degros | 664eba7 | 2019-11-05 13:18:24 +1100 | [diff] [blame] | 2399 | err = write_exactly(fd, &sz, sizeof(sz)); |
| 2400 | if (err) |
| 2401 | goto error; |
| 2402 | |
| 2403 | err = write_exactly(fd, buf, sz); |
| 2404 | |
| 2405 | error: |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2406 | free(buf); |
François Degros | 664eba7 | 2019-11-05 13:18:24 +1100 | [diff] [blame] | 2407 | return err; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 2408 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2409 | |
Luis Hector Chavez | 9acba45 | 2018-10-11 10:13:25 -0700 | [diff] [blame] | 2410 | static int setup_preload(const struct minijail *j attribute_unused, |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 2411 | char ***child_env attribute_unused) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2412 | { |
Daniel Erat | 5b7a318 | 2015-08-19 16:06:22 -0600 | [diff] [blame] | 2413 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 2414 | /* Don't use LDPRELOAD on Android. */ |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 2415 | return 0; |
| 2416 | #else |
Luis Hector Chavez | 9acba45 | 2018-10-11 10:13:25 -0700 | [diff] [blame] | 2417 | const char *preload_path = j->preload_path ?: PRELOADPATH; |
| 2418 | char *newenv = NULL; |
Luis Hector Chavez | d1d24d2 | 2019-02-11 17:59:21 -0800 | [diff] [blame] | 2419 | int ret = 0; |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 2420 | const char *oldenv = getenv(kLdPreloadEnvVar); |
Luis Hector Chavez | 9acba45 | 2018-10-11 10:13:25 -0700 | [diff] [blame] | 2421 | |
| 2422 | if (!oldenv) |
| 2423 | oldenv = ""; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2424 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2425 | /* Only insert a separating space if we have something to separate... */ |
Luis Hector Chavez | d1d24d2 | 2019-02-11 17:59:21 -0800 | [diff] [blame] | 2426 | if (asprintf(&newenv, "%s%s%s", oldenv, oldenv[0] != '\0' ? " " : "", |
| 2427 | preload_path) < 0) { |
| 2428 | return -1; |
Luis Hector Chavez | 9acba45 | 2018-10-11 10:13:25 -0700 | [diff] [blame] | 2429 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2430 | |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 2431 | ret = minijail_setenv(child_env, kLdPreloadEnvVar, newenv, 1); |
Luis Hector Chavez | d1d24d2 | 2019-02-11 17:59:21 -0800 | [diff] [blame] | 2432 | free(newenv); |
| 2433 | return ret; |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 2434 | #endif |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2435 | } |
| 2436 | |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 2437 | static int setup_pipe(char ***child_env, int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2438 | { |
| 2439 | int r = pipe(fds); |
| 2440 | char fd_buf[11]; |
| 2441 | if (r) |
| 2442 | return r; |
| 2443 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 2444 | if (r <= 0) |
| 2445 | return -EINVAL; |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 2446 | return minijail_setenv(child_env, kFdEnvVar, fd_buf, 1); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 2447 | } |
| 2448 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 2449 | static int close_open_fds(int *inheritable_fds, size_t size) |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2450 | { |
| 2451 | const char *kFdPath = "/proc/self/fd"; |
| 2452 | |
| 2453 | DIR *d = opendir(kFdPath); |
| 2454 | struct dirent *dir_entry; |
| 2455 | |
| 2456 | if (d == NULL) |
| 2457 | return -1; |
| 2458 | int dir_fd = dirfd(d); |
| 2459 | while ((dir_entry = readdir(d)) != NULL) { |
| 2460 | size_t i; |
| 2461 | char *end; |
| 2462 | bool should_close = true; |
| 2463 | const int fd = strtol(dir_entry->d_name, &end, 10); |
| 2464 | |
| 2465 | if ((*end) != '\0') { |
| 2466 | continue; |
| 2467 | } |
| 2468 | /* |
| 2469 | * We might have set up some pipes that we want to share with |
| 2470 | * the parent process, and should not be closed. |
| 2471 | */ |
| 2472 | for (i = 0; i < size; ++i) { |
| 2473 | if (fd == inheritable_fds[i]) { |
| 2474 | should_close = false; |
| 2475 | break; |
| 2476 | } |
| 2477 | } |
| 2478 | /* Also avoid closing the directory fd. */ |
| 2479 | if (should_close && fd != dir_fd) |
| 2480 | close(fd); |
| 2481 | } |
| 2482 | closedir(d); |
| 2483 | return 0; |
| 2484 | } |
| 2485 | |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 2486 | static int redirect_fds(struct minijail *j) |
| 2487 | { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2488 | for (size_t i = 0; i < j->preserved_fd_count; i++) { |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 2489 | if (dup2(j->preserved_fds[i].parent_fd, |
| 2490 | j->preserved_fds[i].child_fd) == -1) { |
| 2491 | return -1; |
| 2492 | } |
| 2493 | } |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 2494 | return 0; |
| 2495 | } |
| 2496 | |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2497 | /* |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2498 | * Structure holding resources and state created when running a minijail. |
| 2499 | */ |
| 2500 | struct minijail_run_state { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2501 | pid_t child_pid; |
| 2502 | int pipe_fds[2]; |
| 2503 | int stdin_fds[2]; |
| 2504 | int stdout_fds[2]; |
| 2505 | int stderr_fds[2]; |
| 2506 | int child_sync_pipe_fds[2]; |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 2507 | char **child_env; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2508 | }; |
| 2509 | |
| 2510 | static void minijail_free_run_state(struct minijail_run_state *state) |
| 2511 | { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2512 | state->child_pid = -1; |
| 2513 | |
| 2514 | int *fd_pairs[] = {state->pipe_fds, state->stdin_fds, state->stdout_fds, |
| 2515 | state->stderr_fds, state->child_sync_pipe_fds}; |
| 2516 | for (size_t i = 0; i < ARRAY_SIZE(fd_pairs); ++i) { |
| 2517 | close_and_reset(&fd_pairs[i][0]); |
| 2518 | close_and_reset(&fd_pairs[i][1]); |
| 2519 | } |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 2520 | |
| 2521 | minijail_free_env(state->child_env); |
| 2522 | state->child_env = NULL; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | /* Set up stdin/stdout/stderr file descriptors in the child. */ |
| 2526 | static void setup_child_std_fds(struct minijail *j, |
| 2527 | struct minijail_run_state *state) |
| 2528 | { |
| 2529 | struct { |
| 2530 | const char *name; |
| 2531 | int from; |
| 2532 | int to; |
| 2533 | } fd_map[] = { |
| 2534 | {"stdin", state->stdin_fds[0], STDIN_FILENO}, |
| 2535 | {"stdout", state->stdout_fds[1], STDOUT_FILENO}, |
| 2536 | {"stderr", state->stderr_fds[1], STDERR_FILENO}, |
| 2537 | }; |
| 2538 | |
| 2539 | for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) { |
| 2540 | if (fd_map[i].from != -1) { |
| 2541 | if (dup2(fd_map[i].from, fd_map[i].to) == -1) |
| 2542 | die("failed to set up %s pipe", fd_map[i].name); |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | /* Close temporary pipe file descriptors. */ |
| 2547 | int *std_pipes[] = {state->stdin_fds, state->stdout_fds, |
| 2548 | state->stderr_fds}; |
| 2549 | for (size_t i = 0; i < ARRAY_SIZE(std_pipes); ++i) { |
| 2550 | close_and_reset(&std_pipes[i][0]); |
| 2551 | close_and_reset(&std_pipes[i][1]); |
| 2552 | } |
| 2553 | |
| 2554 | /* |
| 2555 | * If any of stdin, stdout, or stderr are TTYs, or setsid flag is |
| 2556 | * set, create a new session. This prevents the jailed process from |
| 2557 | * using the TIOCSTI ioctl to push characters into the parent process |
| 2558 | * terminal's input buffer, therefore escaping the jail. |
| 2559 | * |
| 2560 | * Since it has just forked, the child will not be a process group |
| 2561 | * leader, and this call to setsid() should always succeed. |
| 2562 | */ |
| 2563 | if (j->flags.setsid || isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || |
| 2564 | isatty(STDERR_FILENO)) { |
| 2565 | if (setsid() < 0) { |
| 2566 | pdie("setsid() failed"); |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | /* |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2572 | * Structure that specifies how to start a minijail. |
| 2573 | * |
Jorge Lucangeli Obes | d2c951d | 2019-02-01 15:43:36 -0500 | [diff] [blame] | 2574 | * filename - The program to exec in the child. Required if |exec_in_child| = 1. |
| 2575 | * argv - Arguments for the child program. Required if |exec_in_child| = 1. |
| 2576 | * envp - Environment for the child program. Available if |exec_in_child| = 1. |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2577 | * use_preload - If true use LD_PRELOAD. |
Jorge Lucangeli Obes | d2c951d | 2019-02-01 15:43:36 -0500 | [diff] [blame] | 2578 | * exec_in_child - If true, run |filename|. Otherwise, the child will return to |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2579 | * the caller. |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2580 | * pstdin_fd - Filled with stdin pipe if non-NULL. |
| 2581 | * pstdout_fd - Filled with stdout pipe if non-NULL. |
| 2582 | * pstderr_fd - Filled with stderr pipe if non-NULL. |
| 2583 | * pchild_pid - Filled with the pid of the child process if non-NULL. |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2584 | */ |
| 2585 | struct minijail_run_config { |
| 2586 | const char *filename; |
| 2587 | char *const *argv; |
Jorge Lucangeli Obes | d2c951d | 2019-02-01 15:43:36 -0500 | [diff] [blame] | 2588 | char *const *envp; |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2589 | int use_preload; |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2590 | int exec_in_child; |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2591 | int *pstdin_fd; |
| 2592 | int *pstdout_fd; |
| 2593 | int *pstderr_fd; |
| 2594 | pid_t *pchild_pid; |
| 2595 | }; |
| 2596 | |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2597 | static int |
| 2598 | minijail_run_config_internal(struct minijail *j, |
| 2599 | const struct minijail_run_config *config); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2600 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2601 | int API minijail_run(struct minijail *j, const char *filename, |
| 2602 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2603 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2604 | struct minijail_run_config config = { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2605 | .filename = filename, |
| 2606 | .argv = argv, |
| 2607 | .envp = NULL, |
| 2608 | .use_preload = true, |
| 2609 | .exec_in_child = true, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2610 | }; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2611 | return minijail_run_config_internal(j, &config); |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 2615 | char *const argv[], pid_t *pchild_pid) |
| 2616 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2617 | struct minijail_run_config config = { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2618 | .filename = filename, |
| 2619 | .argv = argv, |
| 2620 | .envp = NULL, |
| 2621 | .use_preload = true, |
| 2622 | .exec_in_child = true, |
| 2623 | .pchild_pid = pchild_pid, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2624 | }; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2625 | return minijail_run_config_internal(j, &config); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | int API minijail_run_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 2629 | char *const argv[], int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2630 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2631 | struct minijail_run_config config = { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2632 | .filename = filename, |
| 2633 | .argv = argv, |
| 2634 | .envp = NULL, |
| 2635 | .use_preload = true, |
| 2636 | .exec_in_child = true, |
| 2637 | .pstdin_fd = pstdin_fd, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2638 | }; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2639 | return minijail_run_config_internal(j, &config); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2640 | } |
| 2641 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2642 | int API minijail_run_pid_pipes(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 2643 | char *const argv[], pid_t *pchild_pid, |
| 2644 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2645 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2646 | struct minijail_run_config config = { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2647 | .filename = filename, |
| 2648 | .argv = argv, |
| 2649 | .envp = NULL, |
| 2650 | .use_preload = true, |
| 2651 | .exec_in_child = true, |
| 2652 | .pstdin_fd = pstdin_fd, |
| 2653 | .pstdout_fd = pstdout_fd, |
| 2654 | .pstderr_fd = pstderr_fd, |
| 2655 | .pchild_pid = pchild_pid, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2656 | }; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2657 | return minijail_run_config_internal(j, &config); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2658 | } |
| 2659 | |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 2660 | int API minijail_run_env_pid_pipes(struct minijail *j, const char *filename, |
| 2661 | char *const argv[], char *const envp[], |
| 2662 | pid_t *pchild_pid, int *pstdin_fd, |
| 2663 | int *pstdout_fd, int *pstderr_fd) |
| 2664 | { |
| 2665 | struct minijail_run_config config = { |
| 2666 | .filename = filename, |
| 2667 | .argv = argv, |
| 2668 | .envp = envp, |
| 2669 | .use_preload = true, |
| 2670 | .exec_in_child = true, |
| 2671 | .pstdin_fd = pstdin_fd, |
| 2672 | .pstdout_fd = pstdout_fd, |
| 2673 | .pstderr_fd = pstderr_fd, |
| 2674 | .pchild_pid = pchild_pid, |
| 2675 | }; |
| 2676 | return minijail_run_config_internal(j, &config); |
| 2677 | } |
| 2678 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2679 | int API minijail_run_no_preload(struct minijail *j, const char *filename, |
| 2680 | char *const argv[]) |
| 2681 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2682 | struct minijail_run_config config = { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2683 | .filename = filename, |
| 2684 | .argv = argv, |
| 2685 | .envp = NULL, |
| 2686 | .use_preload = false, |
| 2687 | .exec_in_child = true, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2688 | }; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2689 | return minijail_run_config_internal(j, &config); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2690 | } |
| 2691 | |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 2692 | int API minijail_run_pid_pipes_no_preload(struct minijail *j, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 2693 | const char *filename, |
| 2694 | char *const argv[], |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 2695 | pid_t *pchild_pid, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2696 | int *pstdin_fd, |
| 2697 | int *pstdout_fd, |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 2698 | int *pstderr_fd) |
| 2699 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2700 | struct minijail_run_config config = { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2701 | .filename = filename, |
| 2702 | .argv = argv, |
| 2703 | .envp = NULL, |
| 2704 | .use_preload = false, |
| 2705 | .exec_in_child = true, |
| 2706 | .pstdin_fd = pstdin_fd, |
| 2707 | .pstdout_fd = pstdout_fd, |
| 2708 | .pstderr_fd = pstderr_fd, |
| 2709 | .pchild_pid = pchild_pid, |
Jorge Lucangeli Obes | d2c951d | 2019-02-01 15:43:36 -0500 | [diff] [blame] | 2710 | }; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2711 | return minijail_run_config_internal(j, &config); |
Jorge Lucangeli Obes | d2c951d | 2019-02-01 15:43:36 -0500 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | int API minijail_run_env_pid_pipes_no_preload(struct minijail *j, |
| 2715 | const char *filename, |
| 2716 | char *const argv[], |
| 2717 | char *const envp[], |
| 2718 | pid_t *pchild_pid, int *pstdin_fd, |
| 2719 | int *pstdout_fd, int *pstderr_fd) |
| 2720 | { |
| 2721 | struct minijail_run_config config = { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2722 | .filename = filename, |
| 2723 | .argv = argv, |
| 2724 | .envp = envp, |
| 2725 | .use_preload = false, |
| 2726 | .exec_in_child = true, |
| 2727 | .pstdin_fd = pstdin_fd, |
| 2728 | .pstdout_fd = pstdout_fd, |
| 2729 | .pstderr_fd = pstderr_fd, |
| 2730 | .pchild_pid = pchild_pid, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2731 | }; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2732 | return minijail_run_config_internal(j, &config); |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 2733 | } |
| 2734 | |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2735 | pid_t API minijail_fork(struct minijail *j) |
| 2736 | { |
| 2737 | struct minijail_run_config config = {}; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2738 | return minijail_run_config_internal(j, &config); |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2739 | } |
| 2740 | |
Dylan Reid | 18c49c8 | 2017-08-25 14:52:27 -0700 | [diff] [blame] | 2741 | static int minijail_run_internal(struct minijail *j, |
| 2742 | const struct minijail_run_config *config, |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2743 | struct minijail_run_state *state_out) |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2744 | { |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2745 | int sync_child = 0; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2746 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 2747 | /* We need to remember this across the minijail_preexec() call. */ |
| 2748 | int pid_namespace = j->flags.pids; |
Luis Hector Chavez | ac981fc | 2017-09-18 15:52:38 -0700 | [diff] [blame] | 2749 | /* |
| 2750 | * Create an init process if we are entering a pid namespace, unless the |
| 2751 | * user has explicitly opted out by calling minijail_run_as_init(). |
| 2752 | */ |
| 2753 | int do_init = j->flags.do_init && !j->flags.run_as_init; |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2754 | int use_preload = config->use_preload; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 2755 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2756 | if (use_preload) { |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2757 | if (j->hooks_head != NULL) |
| 2758 | die("Minijail hooks are not supported with LD_PRELOAD"); |
| 2759 | if (!config->exec_in_child) |
| 2760 | die("minijail_fork is not supported with LD_PRELOAD"); |
| 2761 | |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 2762 | /* |
| 2763 | * Before we fork(2) and execve(2) the child process, we need |
| 2764 | * to open a pipe(2) to send the minijail configuration over. |
| 2765 | */ |
| 2766 | state_out->child_env = |
| 2767 | minijail_copy_env(config->envp ? config->envp : environ); |
| 2768 | if (!state_out->child_env) |
| 2769 | return ENOMEM; |
| 2770 | if (setup_preload(j, &state_out->child_env) || |
| 2771 | setup_pipe(&state_out->child_env, state_out->pipe_fds)) |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2772 | return -EFAULT; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2773 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 2774 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2775 | if (!use_preload) { |
Luis Hector Chavez | fe5fb8e | 2017-06-29 10:41:27 -0700 | [diff] [blame] | 2776 | if (j->flags.use_caps && j->caps != 0 && |
| 2777 | !j->flags.set_ambient_caps) { |
| 2778 | die("non-empty, non-ambient capabilities are not " |
| 2779 | "supported without LD_PRELOAD"); |
| 2780 | } |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2781 | } |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 2782 | |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2783 | /* Create pipes for stdin/stdout/stderr as requested by caller. */ |
| 2784 | struct { |
| 2785 | bool requested; |
| 2786 | int *pipe_fds; |
| 2787 | } pipe_fd_req[] = { |
| 2788 | {config->pstdin_fd != NULL, state_out->stdin_fds}, |
| 2789 | {config->pstdout_fd != NULL, state_out->stdout_fds}, |
| 2790 | {config->pstderr_fd != NULL, state_out->stderr_fds}, |
| 2791 | }; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2792 | |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2793 | for (size_t i = 0; i < ARRAY_SIZE(pipe_fd_req); ++i) { |
| 2794 | if (pipe_fd_req[i].requested && |
| 2795 | pipe(pipe_fd_req[i].pipe_fds) == -1) |
| 2796 | return EFAULT; |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2797 | } |
| 2798 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2799 | /* |
François Degros | 664eba7 | 2019-11-05 13:18:24 +1100 | [diff] [blame] | 2800 | * If the parent process needs to configure the child's runtime |
| 2801 | * environment after forking, create a pipe(2) to block the child until |
| 2802 | * configuration is done. |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2803 | */ |
Daniel Erat | 2d69add | 2019-04-23 20:58:53 -0700 | [diff] [blame] | 2804 | if (j->flags.forward_signals || j->flags.pid_file || j->flags.cgroups || |
| 2805 | j->rlimit_count || j->flags.userns) { |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2806 | sync_child = 1; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2807 | if (pipe(state_out->child_sync_pipe_fds)) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2808 | return -EFAULT; |
| 2809 | } |
| 2810 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 2811 | /* |
| 2812 | * Use sys_clone() if and only if we're creating a pid namespace. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2813 | * |
| 2814 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 2815 | * |
| 2816 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 2817 | * some of which may be held by other threads at the time that we call |
| 2818 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 2819 | * ensure that we hold all of these locks before it calls clone() |
| 2820 | * internally and drop them after clone() returns, but when we call |
| 2821 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 2822 | * child address space where some of libc's important locks are held by |
| 2823 | * other threads (which did not get cloned, and hence will never release |
| 2824 | * those locks). This is okay so long as we call exec() immediately |
| 2825 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 2826 | * take locks. |
| 2827 | * |
| 2828 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 2829 | * namespacing. If we follow this path, the child's address space might |
| 2830 | * have broken locks; you may only call functions that do not acquire |
| 2831 | * any locks. |
| 2832 | * |
| 2833 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 2834 | * previously detailed, so this function is highly likely to deadlock |
| 2835 | * later on (see "deadlock here") if we're multithreaded. |
| 2836 | * |
| 2837 | * We might hack around this by having the clone()d child (init of the |
| 2838 | * pid namespace) return directly, rather than leaving the clone()d |
| 2839 | * process hanging around to be init for the new namespace (and having |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 2840 | * its fork()ed child return in turn), but that process would be |
| 2841 | * crippled with its libc locks potentially broken. We might try |
| 2842 | * fork()ing in the parent before we clone() to ensure that we own all |
| 2843 | * the locks, but then we have to have the forked child hanging around |
| 2844 | * consuming resources (and possibly having file descriptors / shared |
| 2845 | * memory regions / etc attached). We'd need to keep the child around to |
| 2846 | * avoid having its children get reparented to init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2847 | * |
| 2848 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 2849 | * problem is fixable or not. It would be nice if we worked in this |
| 2850 | * case. |
| 2851 | */ |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2852 | pid_t child_pid; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2853 | if (pid_namespace) { |
François Degros | 9461984 | 2019-11-08 16:37:55 +1100 | [diff] [blame] | 2854 | unsigned long clone_flags = CLONE_NEWPID | SIGCHLD; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2855 | if (j->flags.userns) |
| 2856 | clone_flags |= CLONE_NEWUSER; |
François Degros | 9461984 | 2019-11-08 16:37:55 +1100 | [diff] [blame] | 2857 | |
| 2858 | child_pid = syscall(SYS_clone, clone_flags, NULL, 0L, 0L, 0L); |
| 2859 | |
| 2860 | if (child_pid < 0) { |
| 2861 | if (errno == EPERM) |
| 2862 | pdie("clone(CLONE_NEWPID | ...) failed with EPERM; " |
| 2863 | "is this process missing CAP_SYS_ADMIN?"); |
| 2864 | pdie("clone(CLONE_NEWPID | ...) failed"); |
| 2865 | } |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2866 | } else { |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2867 | child_pid = fork(); |
| 2868 | |
François Degros | 9461984 | 2019-11-08 16:37:55 +1100 | [diff] [blame] | 2869 | if (child_pid < 0) |
| 2870 | pdie("fork failed"); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2871 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 2872 | |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2873 | state_out->child_pid = child_pid; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2874 | if (child_pid) { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2875 | j->initpid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2876 | |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 2877 | if (j->flags.forward_signals) { |
| 2878 | forward_pid = child_pid; |
| 2879 | install_signal_handlers(); |
| 2880 | } |
| 2881 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 2882 | if (j->flags.pid_file) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2883 | write_pid_file_or_die(j); |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 2884 | |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 2885 | if (j->flags.cgroups) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2886 | add_to_cgroups_or_die(j); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 2887 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 2888 | if (j->rlimit_count) |
| 2889 | set_rlimits_or_die(j); |
| 2890 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2891 | if (j->flags.userns) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2892 | write_ugid_maps_or_die(j); |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2893 | |
Mike Frysinger | 902a449 | 2018-12-27 05:22:56 -0500 | [diff] [blame] | 2894 | if (j->flags.enter_vfs) |
| 2895 | close(j->mountns_fd); |
| 2896 | |
| 2897 | if (j->flags.enter_net) |
| 2898 | close(j->netns_fd); |
| 2899 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2900 | if (sync_child) |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2901 | parent_setup_complete(state_out->child_sync_pipe_fds); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2902 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2903 | if (use_preload) { |
François Degros | 664eba7 | 2019-11-05 13:18:24 +1100 | [diff] [blame] | 2904 | /* |
| 2905 | * Add SIGPIPE to the signal mask to avoid getting |
| 2906 | * killed if the child process finishes or closes its |
| 2907 | * end of the pipe prematurely. |
| 2908 | * |
| 2909 | * TODO(crbug.com/1022170): Use pthread_sigmask instead |
| 2910 | * of sigprocmask if Minijail is used in multithreaded |
| 2911 | * programs. |
| 2912 | */ |
| 2913 | sigset_t to_block, to_restore; |
| 2914 | if (sigemptyset(&to_block) < 0) |
| 2915 | pdie("sigemptyset failed"); |
| 2916 | if (sigaddset(&to_block, SIGPIPE) < 0) |
| 2917 | pdie("sigaddset failed"); |
| 2918 | if (sigprocmask(SIG_BLOCK, &to_block, &to_restore) < 0) |
| 2919 | pdie("sigprocmask failed"); |
| 2920 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2921 | /* Send marshalled minijail. */ |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2922 | close_and_reset(&state_out->pipe_fds[0]); |
| 2923 | ret = minijail_to_fd(j, state_out->pipe_fds[1]); |
| 2924 | close_and_reset(&state_out->pipe_fds[1]); |
François Degros | 664eba7 | 2019-11-05 13:18:24 +1100 | [diff] [blame] | 2925 | |
| 2926 | /* Accept any pending SIGPIPE. */ |
| 2927 | while (true) { |
| 2928 | const struct timespec zero_time = {0, 0}; |
| 2929 | const int sig = sigtimedwait(&to_block, NULL, &zero_time); |
| 2930 | if (sig < 0) { |
| 2931 | if (errno != EINTR) |
| 2932 | break; |
| 2933 | } else { |
| 2934 | if (sig != SIGPIPE) |
| 2935 | die("unexpected signal %d", sig); |
| 2936 | } |
| 2937 | } |
| 2938 | |
| 2939 | /* Restore the signal mask to its original state. */ |
| 2940 | if (sigprocmask(SIG_SETMASK, &to_restore, NULL) < 0) |
| 2941 | pdie("sigprocmask failed"); |
| 2942 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2943 | if (ret) { |
François Degros | 664eba7 | 2019-11-05 13:18:24 +1100 | [diff] [blame] | 2944 | warn("failed to send marshalled minijail: %s", |
| 2945 | strerror(-ret)); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2946 | kill(j->initpid, SIGKILL); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2947 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2948 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2949 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2950 | return 0; |
| 2951 | } |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 2952 | |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2953 | /* Child process. */ |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 2954 | if (j->flags.reset_signal_mask) { |
| 2955 | sigset_t signal_mask; |
| 2956 | if (sigemptyset(&signal_mask) != 0) |
| 2957 | pdie("sigemptyset failed"); |
| 2958 | if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0) |
| 2959 | pdie("sigprocmask failed"); |
| 2960 | } |
| 2961 | |
Luis Hector Chavez | a27118a | 2018-04-04 08:18:01 -0700 | [diff] [blame] | 2962 | if (j->flags.reset_signal_handlers) { |
| 2963 | int signum; |
| 2964 | for (signum = 0; signum <= SIGRTMAX; signum++) { |
| 2965 | /* |
| 2966 | * Ignore EINVAL since some signal numbers in the range |
| 2967 | * might not be valid. |
| 2968 | */ |
| 2969 | if (signal(signum, SIG_DFL) == SIG_ERR && |
| 2970 | errno != EINVAL) { |
| 2971 | pdie("failed to reset signal %d disposition", |
| 2972 | signum); |
| 2973 | } |
| 2974 | } |
| 2975 | } |
| 2976 | |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2977 | if (j->flags.close_open_fds) { |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 2978 | const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS; |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2979 | int inheritable_fds[kMaxInheritableFdsSize]; |
| 2980 | size_t size = 0; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 2981 | |
| 2982 | int *pipe_fds[] = { |
| 2983 | state_out->pipe_fds, state_out->child_sync_pipe_fds, |
| 2984 | state_out->stdin_fds, state_out->stdout_fds, |
| 2985 | state_out->stderr_fds, |
| 2986 | }; |
| 2987 | |
| 2988 | for (size_t i = 0; i < ARRAY_SIZE(pipe_fds); ++i) { |
| 2989 | if (pipe_fds[i][0] != -1) { |
| 2990 | inheritable_fds[size++] = pipe_fds[i][0]; |
| 2991 | } |
| 2992 | if (pipe_fds[i][1] != -1) { |
| 2993 | inheritable_fds[size++] = pipe_fds[i][1]; |
| 2994 | } |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2995 | } |
Jorge Lucangeli Obes | 2337f80 | 2019-07-18 14:46:03 -0400 | [diff] [blame] | 2996 | |
Jorge Lucangeli Obes | cf3bbea | 2019-07-24 09:06:40 -0400 | [diff] [blame] | 2997 | /* |
| 2998 | * Preserve namespace file descriptors over the close_open_fds() |
| 2999 | * call. These are closed in minijail_enter() so they won't leak |
| 3000 | * into the child process. |
| 3001 | */ |
Jorge Lucangeli Obes | 2337f80 | 2019-07-18 14:46:03 -0400 | [diff] [blame] | 3002 | if (j->flags.enter_vfs) |
| 3003 | minijail_preserve_fd(j, j->mountns_fd, j->mountns_fd); |
| 3004 | if (j->flags.enter_net) |
| 3005 | minijail_preserve_fd(j, j->netns_fd, j->netns_fd); |
| 3006 | |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3007 | for (size_t i = 0; i < j->preserved_fd_count; i++) { |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 3008 | /* |
| 3009 | * Preserve all parent_fds. They will be dup2(2)-ed in |
| 3010 | * the child later. |
| 3011 | */ |
| 3012 | inheritable_fds[size++] = j->preserved_fds[i].parent_fd; |
| 3013 | } |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 3014 | |
| 3015 | if (close_open_fds(inheritable_fds, size) < 0) |
| 3016 | die("failed to close open file descriptors"); |
| 3017 | } |
| 3018 | |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 3019 | if (redirect_fds(j)) |
| 3020 | die("failed to set up fd redirections"); |
| 3021 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 3022 | if (sync_child) |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3023 | wait_for_parent_setup(state_out->child_sync_pipe_fds); |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 3024 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 3025 | if (j->flags.userns) |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 3026 | enter_user_namespace(j); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 3027 | |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3028 | setup_child_std_fds(j, state_out); |
Jorge Lucangeli Obes | aa235b9 | 2016-11-23 13:48:15 -0500 | [diff] [blame] | 3029 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 3030 | /* If running an init program, let it decide when/how to mount /proc. */ |
| 3031 | if (pid_namespace && !do_init) |
| 3032 | j->flags.remount_proc_ro = 0; |
| 3033 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 3034 | if (use_preload) { |
| 3035 | /* Strip out flags that cannot be inherited across execve(2). */ |
| 3036 | minijail_preexec(j); |
| 3037 | } else { |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 3038 | /* |
| 3039 | * If not using LD_PRELOAD, do all jailing before execve(2). |
| 3040 | * Note that PID namespaces can only be entered on fork(2), |
| 3041 | * so that flag is still cleared. |
| 3042 | */ |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 3043 | j->flags.pids = 0; |
| 3044 | } |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 3045 | |
| 3046 | /* |
| 3047 | * Jail this process. |
| 3048 | * If forking, return. |
| 3049 | * If not, execve(2) the target. |
| 3050 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3051 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 3052 | |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 3053 | if (config->exec_in_child && pid_namespace && do_init) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 3054 | /* |
| 3055 | * pid namespace: this process will become init inside the new |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 3056 | * namespace. We don't want all programs we might exec to have |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 3057 | * to know how to be init. Normally (do_init == 1) we fork off |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 3058 | * a child to actually run the program. If |do_init == 0|, we |
| 3059 | * let the program keep pid 1 and be init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 3060 | * |
| 3061 | * If we're multithreaded, we'll probably deadlock here. See |
| 3062 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3063 | */ |
| 3064 | child_pid = fork(); |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 3065 | if (child_pid < 0) { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3066 | _exit(child_pid); |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 3067 | } else if (child_pid > 0) { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3068 | minijail_free_run_state(state_out); |
| 3069 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 3070 | /* |
| 3071 | * Best effort. Don't bother checking the return value. |
| 3072 | */ |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 3073 | prctl(PR_SET_NAME, "minijail-init"); |
| 3074 | init(child_pid); /* Never returns. */ |
| 3075 | } |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3076 | state_out->child_pid = child_pid; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3077 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 3078 | |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 3079 | run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE); |
| 3080 | |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 3081 | if (!config->exec_in_child) |
| 3082 | return 0; |
| 3083 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 3084 | /* |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3085 | * We're going to execve(), so make sure any remaining resources are |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 3086 | * freed. Exceptions are: |
| 3087 | * 1. The child environment. No need to worry about freeing it since |
| 3088 | * execve reinitializes the heap anyways. |
| 3089 | * 2. The read side of the LD_PRELOAD pipe, which we need to hand down |
| 3090 | * into the target in which the preloaded code will read from it and |
| 3091 | * then close it. |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3092 | */ |
| 3093 | state_out->pipe_fds[0] = -1; |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 3094 | char *const *child_env = state_out->child_env; |
| 3095 | state_out->child_env = NULL; |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3096 | minijail_free_run_state(state_out); |
| 3097 | |
| 3098 | /* |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 3099 | * If we aren't pid-namespaced, or the jailed program asked to be init: |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3100 | * calling process |
| 3101 | * -> execve()-ing process |
| 3102 | * If we are: |
| 3103 | * calling process |
| 3104 | * -> init()-ing process |
| 3105 | * -> execve()-ing process |
| 3106 | */ |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 3107 | if (!child_env) |
| 3108 | child_env = config->envp ? config->envp : environ; |
François Degros | 08b10f7 | 2019-10-09 12:44:05 +1100 | [diff] [blame] | 3109 | execve(config->filename, config->argv, child_env); |
| 3110 | |
| 3111 | ret = (errno == ENOENT ? MINIJAIL_ERR_NO_COMMAND : MINIJAIL_ERR_NO_ACCESS); |
| 3112 | pwarn("execve(%s) failed", config->filename); |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 3113 | _exit(ret); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 3114 | } |
| 3115 | |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3116 | static int |
| 3117 | minijail_run_config_internal(struct minijail *j, |
| 3118 | const struct minijail_run_config *config) |
| 3119 | { |
| 3120 | struct minijail_run_state state = { |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3121 | .child_pid = -1, |
| 3122 | .pipe_fds = {-1, -1}, |
| 3123 | .stdin_fds = {-1, -1}, |
| 3124 | .stdout_fds = {-1, -1}, |
| 3125 | .stderr_fds = {-1, -1}, |
| 3126 | .child_sync_pipe_fds = {-1, -1}, |
Mattias Nissler | b35f2c1 | 2020-02-07 13:37:36 +0100 | [diff] [blame] | 3127 | .child_env = NULL, |
Mattias Nissler | 6123e5a | 2020-02-11 13:38:03 +0100 | [diff] [blame] | 3128 | }; |
| 3129 | int ret = minijail_run_internal(j, config, &state); |
| 3130 | |
| 3131 | if (ret == 0) { |
| 3132 | if (config->pchild_pid) |
| 3133 | *config->pchild_pid = state.child_pid; |
| 3134 | |
| 3135 | /* Grab stdin/stdout/stderr descriptors requested by caller. */ |
| 3136 | struct { |
| 3137 | int *pfd; |
| 3138 | int *psrc; |
| 3139 | } fd_map[] = { |
| 3140 | {config->pstdin_fd, &state.stdin_fds[1]}, |
| 3141 | {config->pstdout_fd, &state.stdout_fds[0]}, |
| 3142 | {config->pstderr_fd, &state.stderr_fds[0]}, |
| 3143 | }; |
| 3144 | |
| 3145 | for (size_t i = 0; i < ARRAY_SIZE(fd_map); ++i) { |
| 3146 | if (fd_map[i].pfd) { |
| 3147 | *fd_map[i].pfd = *fd_map[i].psrc; |
| 3148 | *fd_map[i].psrc = -1; |
| 3149 | } |
| 3150 | } |
| 3151 | |
| 3152 | if (!config->exec_in_child) |
| 3153 | ret = state.child_pid; |
| 3154 | } |
| 3155 | |
| 3156 | minijail_free_run_state(&state); |
| 3157 | |
| 3158 | return ret; |
| 3159 | } |
| 3160 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 3161 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3162 | { |
François Degros | 47e6335 | 2019-10-01 13:01:53 +1000 | [diff] [blame] | 3163 | if (j->initpid <= 0) |
| 3164 | return -ECHILD; |
| 3165 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3166 | if (kill(j->initpid, SIGTERM)) |
| 3167 | return -errno; |
François Degros | 47e6335 | 2019-10-01 13:01:53 +1000 | [diff] [blame] | 3168 | |
| 3169 | return minijail_wait(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 3170 | } |
| 3171 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 3172 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3173 | { |
François Degros | 08b10f7 | 2019-10-09 12:44:05 +1100 | [diff] [blame] | 3174 | if (j->initpid <= 0) |
| 3175 | return -ECHILD; |
| 3176 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3177 | int st; |
François Degros | 627deba | 2019-10-01 12:48:25 +1000 | [diff] [blame] | 3178 | while (true) { |
| 3179 | const int ret = waitpid(j->initpid, &st, 0); |
| 3180 | if (ret >= 0) |
| 3181 | break; |
| 3182 | if (errno != EINTR) |
| 3183 | return -errno; |
| 3184 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 3185 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 3186 | if (!WIFEXITED(st)) { |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 3187 | int error_status = st; |
| 3188 | if (WIFSIGNALED(st)) { |
| 3189 | int signum = WTERMSIG(st); |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 3190 | warn("child process %d received signal %d", |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 3191 | j->initpid, signum); |
| 3192 | /* |
| 3193 | * We return MINIJAIL_ERR_JAIL if the process received |
| 3194 | * SIGSYS, which happens when a syscall is blocked by |
| 3195 | * seccomp filters. |
| 3196 | * If not, we do what bash(1) does: |
| 3197 | * $? = 128 + signum |
| 3198 | */ |
| 3199 | if (signum == SIGSYS) { |
| 3200 | error_status = MINIJAIL_ERR_JAIL; |
| 3201 | } else { |
François Degros | 08b10f7 | 2019-10-09 12:44:05 +1100 | [diff] [blame] | 3202 | error_status = MINIJAIL_ERR_SIG_BASE + signum; |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 3203 | } |
| 3204 | } |
| 3205 | return error_status; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 3206 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 3207 | |
| 3208 | int exit_status = WEXITSTATUS(st); |
| 3209 | if (exit_status != 0) |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 3210 | info("child process %d exited with status %d", |
| 3211 | j->initpid, exit_status); |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 3212 | |
| 3213 | return exit_status; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 3214 | } |
| 3215 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 3216 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3217 | { |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 3218 | size_t i; |
| 3219 | |
Luis Hector Chavez | c3e1772 | 2018-10-16 20:43:12 -0700 | [diff] [blame] | 3220 | if (j->filter_prog) { |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 3221 | free(j->filter_prog->filter); |
| 3222 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3223 | } |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 3224 | free_mounts_list(j); |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 3225 | while (j->hooks_head) { |
| 3226 | struct hook *c = j->hooks_head; |
| 3227 | j->hooks_head = c->next; |
| 3228 | free(c); |
| 3229 | } |
| 3230 | j->hooks_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3231 | if (j->user) |
| 3232 | free(j->user); |
Jorge Lucangeli Obes | e81a52f | 2015-12-04 16:05:23 -0800 | [diff] [blame] | 3233 | if (j->suppl_gid_list) |
| 3234 | free(j->suppl_gid_list); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 3235 | if (j->chrootdir) |
| 3236 | free(j->chrootdir); |
Jorge Lucangeli Obes | 3b2e6e4 | 2016-08-04 12:26:19 -0400 | [diff] [blame] | 3237 | if (j->pid_file_path) |
| 3238 | free(j->pid_file_path); |
| 3239 | if (j->uidmap) |
| 3240 | free(j->uidmap); |
| 3241 | if (j->gidmap) |
| 3242 | free(j->gidmap); |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 3243 | if (j->hostname) |
| 3244 | free(j->hostname); |
Luis Hector Chavez | 9acba45 | 2018-10-11 10:13:25 -0700 | [diff] [blame] | 3245 | if (j->preload_path) |
| 3246 | free(j->preload_path); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 3247 | if (j->alt_syscall_table) |
| 3248 | free(j->alt_syscall_table); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 3249 | for (i = 0; i < j->cgroup_count; ++i) |
| 3250 | free(j->cgroups[i]); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 3251 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 3252 | } |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 3253 | |
| 3254 | void API minijail_log_to_fd(int fd, int min_priority) |
| 3255 | { |
| 3256 | init_logging(LOG_TO_FD, fd, min_priority); |
| 3257 | } |