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> |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 11 | #include <ctype.h> |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 12 | #include <dirent.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 13 | #include <errno.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 14 | #include <fcntl.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 15 | #include <grp.h> |
| 16 | #include <inttypes.h> |
Will Drewry | fe4a372 | 2011-09-16 14:50:50 -0500 | [diff] [blame] | 17 | #include <limits.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 18 | #include <linux/capability.h> |
Mike Frysinger | 7559dfe | 2016-11-15 18:58:39 -0500 | [diff] [blame] | 19 | #include <net/if.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 20 | #include <pwd.h> |
| 21 | #include <sched.h> |
| 22 | #include <signal.h> |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 23 | #include <stdarg.h> |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 24 | #include <stdbool.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 25 | #include <stddef.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 29 | #include <sys/capability.h> |
| 30 | #include <sys/mount.h> |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 31 | #include <sys/param.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 32 | #include <sys/prctl.h> |
Mike Frysinger | 7559dfe | 2016-11-15 18:58:39 -0500 | [diff] [blame] | 33 | #include <sys/socket.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 34 | #include <sys/stat.h> |
| 35 | #include <sys/types.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 36 | #include <sys/user.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 37 | #include <sys/wait.h> |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 38 | #include <syscall.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 39 | #include <unistd.h> |
| 40 | |
| 41 | #include "libminijail.h" |
| 42 | #include "libminijail-private.h" |
| 43 | |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 44 | #include "signal_handler.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 45 | #include "syscall_filter.h" |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 46 | #include "syscall_wrapper.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 47 | #include "util.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 48 | |
Lei Zhang | eee3155 | 2012-10-17 21:27:10 -0700 | [diff] [blame] | 49 | #ifdef HAVE_SECUREBITS_H |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 50 | # include <linux/securebits.h> |
Lei Zhang | eee3155 | 2012-10-17 21:27:10 -0700 | [diff] [blame] | 51 | #else |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 52 | # define SECURE_ALL_BITS 0x55 |
| 53 | # define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) |
| 54 | #endif |
| 55 | /* For kernels < 4.3. */ |
| 56 | #define OLD_SECURE_ALL_BITS 0x15 |
| 57 | #define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1) |
| 58 | |
| 59 | /* |
| 60 | * Assert the value of SECURE_ALL_BITS at compile-time. |
| 61 | * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3 |
| 62 | * added a new securebit. |
| 63 | * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM |
| 64 | * when used on older kernels. The compile-time assert will catch this situation |
| 65 | * at compile time. |
| 66 | */ |
| 67 | #ifdef __BRILLO__ |
| 68 | _Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55."); |
Lei Zhang | eee3155 | 2012-10-17 21:27:10 -0700 | [diff] [blame] | 69 | #endif |
| 70 | |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 71 | /* Until these are reliably available in linux/prctl.h. */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 72 | #ifndef PR_SET_SECCOMP |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 73 | # define PR_SET_SECCOMP 22 |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 74 | #endif |
| 75 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 76 | #ifndef PR_ALT_SYSCALL |
| 77 | # define PR_ALT_SYSCALL 0x43724f53 |
| 78 | #endif |
| 79 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 80 | /* Seccomp filter related flags. */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 81 | #ifndef PR_SET_NO_NEW_PRIVS |
| 82 | # define PR_SET_NO_NEW_PRIVS 38 |
| 83 | #endif |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 84 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 85 | #ifndef SECCOMP_MODE_FILTER |
| 86 | # define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */ |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 87 | #endif |
| 88 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 89 | #ifndef SECCOMP_SET_MODE_STRICT |
| 90 | # define SECCOMP_SET_MODE_STRICT 0 |
| 91 | #endif |
| 92 | #ifndef SECCOMP_SET_MODE_FILTER |
| 93 | # define SECCOMP_SET_MODE_FILTER 1 |
| 94 | #endif |
| 95 | |
| 96 | #ifndef SECCOMP_FILTER_FLAG_TSYNC |
| 97 | # define SECCOMP_FILTER_FLAG_TSYNC 1 |
| 98 | #endif |
| 99 | /* End seccomp filter related flags. */ |
| 100 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 101 | /* New cgroup namespace might not be in linux-headers yet. */ |
| 102 | #ifndef CLONE_NEWCGROUP |
| 103 | # define CLONE_NEWCGROUP 0x02000000 |
| 104 | #endif |
| 105 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 106 | #define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */ |
| 107 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 108 | struct mountpoint { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 109 | char *src; |
| 110 | char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 111 | char *type; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 112 | char *data; |
| 113 | int has_data; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 114 | unsigned long flags; |
| 115 | struct mountpoint *next; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 116 | }; |
| 117 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 118 | struct minijail { |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 119 | /* |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 120 | * WARNING: if you add a flag here you need to make sure it's |
| 121 | * accounted for in minijail_pre{enter|exec}() below. |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 122 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 123 | struct { |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 124 | int uid : 1; |
| 125 | int gid : 1; |
| 126 | int usergroups : 1; |
| 127 | int suppl_gids : 1; |
| 128 | int use_caps : 1; |
| 129 | int capbset_drop : 1; |
| 130 | int vfs : 1; |
| 131 | int enter_vfs : 1; |
| 132 | int skip_remount_private : 1; |
| 133 | int pids : 1; |
| 134 | int ipc : 1; |
| 135 | int net : 1; |
| 136 | int enter_net : 1; |
| 137 | int ns_cgroups : 1; |
| 138 | int userns : 1; |
| 139 | int disable_setgroups : 1; |
| 140 | int seccomp : 1; |
| 141 | int remount_proc_ro : 1; |
| 142 | int no_new_privs : 1; |
| 143 | int seccomp_filter : 1; |
| 144 | int seccomp_filter_tsync : 1; |
| 145 | int seccomp_filter_logging : 1; |
| 146 | int chroot : 1; |
| 147 | int pivot_root : 1; |
| 148 | int mount_tmp : 1; |
| 149 | int do_init : 1; |
| 150 | int pid_file : 1; |
| 151 | int cgroups : 1; |
| 152 | int alt_syscall : 1; |
| 153 | int reset_signal_mask : 1; |
| 154 | int close_open_fds : 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 155 | } flags; |
| 156 | uid_t uid; |
| 157 | gid_t gid; |
| 158 | gid_t usergid; |
| 159 | char *user; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 160 | size_t suppl_gid_count; |
| 161 | gid_t *suppl_gid_list; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 162 | uint64_t caps; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 163 | uint64_t cap_bset; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 164 | pid_t initpid; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 165 | int mountns_fd; |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 166 | int netns_fd; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 167 | char *chrootdir; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 168 | char *pid_file_path; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 169 | char *uidmap; |
| 170 | char *gidmap; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 171 | size_t filter_len; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 172 | struct sock_fprog *filter_prog; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 173 | char *alt_syscall_table; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 174 | struct mountpoint *mounts_head; |
| 175 | struct mountpoint *mounts_tail; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 176 | size_t mounts_count; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 177 | char *cgroups[MAX_CGROUPS]; |
| 178 | size_t cgroup_count; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 179 | }; |
| 180 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 181 | /* |
| 182 | * Strip out flags meant for the parent. |
| 183 | * We keep things that are not inherited across execve(2) (e.g. capabilities), |
| 184 | * or are easier to set after execve(2) (e.g. seccomp filters). |
| 185 | */ |
| 186 | void minijail_preenter(struct minijail *j) |
| 187 | { |
| 188 | j->flags.vfs = 0; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 189 | j->flags.enter_vfs = 0; |
Shuhei Takahashi | 3da4031 | 2016-03-07 17:37:49 +0900 | [diff] [blame] | 190 | j->flags.skip_remount_private = 0; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 191 | j->flags.remount_proc_ro = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 192 | j->flags.pids = 0; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 193 | j->flags.do_init = 0; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 194 | j->flags.pid_file = 0; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 195 | j->flags.cgroups = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Strip out flags meant for the child. |
| 200 | * We keep things that are inherited across execve(2). |
| 201 | */ |
| 202 | void minijail_preexec(struct minijail *j) |
| 203 | { |
| 204 | int vfs = j->flags.vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 205 | int enter_vfs = j->flags.enter_vfs; |
Jorge Lucangeli Obes | 87bf01d | 2016-03-08 11:20:03 -0800 | [diff] [blame] | 206 | int skip_remount_private = j->flags.skip_remount_private; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 207 | int remount_proc_ro = j->flags.remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 208 | int userns = j->flags.userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 209 | if (j->user) |
| 210 | free(j->user); |
| 211 | j->user = NULL; |
Jorge Lucangeli Obes | e81a52f | 2015-12-04 16:05:23 -0800 | [diff] [blame] | 212 | if (j->suppl_gid_list) |
| 213 | free(j->suppl_gid_list); |
| 214 | j->suppl_gid_list = NULL; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 215 | memset(&j->flags, 0, sizeof(j->flags)); |
| 216 | /* Now restore anything we meant to keep. */ |
| 217 | j->flags.vfs = vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 218 | j->flags.enter_vfs = enter_vfs; |
Jorge Lucangeli Obes | 87bf01d | 2016-03-08 11:20:03 -0800 | [diff] [blame] | 219 | j->flags.skip_remount_private = skip_remount_private; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 220 | j->flags.remount_proc_ro = remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 221 | j->flags.userns = userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 222 | /* Note, |pids| will already have been used before this call. */ |
| 223 | } |
| 224 | |
| 225 | /* Minijail API. */ |
| 226 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 227 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 228 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 229 | return calloc(1, sizeof(struct minijail)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 232 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 233 | { |
| 234 | if (uid == 0) |
| 235 | die("useless change to uid 0"); |
| 236 | j->uid = uid; |
| 237 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 238 | } |
| 239 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 240 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 241 | { |
| 242 | if (gid == 0) |
| 243 | die("useless change to gid 0"); |
| 244 | j->gid = gid; |
| 245 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 246 | } |
| 247 | |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 248 | void API minijail_set_supplementary_gids(struct minijail *j, size_t size, |
| 249 | const gid_t *list) |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 250 | { |
Jorge Lucangeli Obes | 06940be | 2015-12-04 18:09:21 -0800 | [diff] [blame] | 251 | size_t i; |
| 252 | |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 253 | if (j->flags.usergroups) |
| 254 | die("cannot inherit *and* set supplementary groups"); |
| 255 | |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 256 | if (size == 0) { |
| 257 | /* Clear supplementary groups. */ |
| 258 | j->suppl_gid_list = NULL; |
| 259 | j->suppl_gid_count = 0; |
| 260 | j->flags.suppl_gids = 1; |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 261 | return; |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 262 | } |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 263 | |
| 264 | /* Copy the gid_t array. */ |
| 265 | j->suppl_gid_list = calloc(size, sizeof(gid_t)); |
| 266 | if (!j->suppl_gid_list) { |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 267 | die("failed to allocate internal supplementary group array"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 268 | } |
Jorge Lucangeli Obes | 06940be | 2015-12-04 18:09:21 -0800 | [diff] [blame] | 269 | for (i = 0; i < size; i++) { |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 270 | j->suppl_gid_list[i] = list[i]; |
| 271 | } |
| 272 | j->suppl_gid_count = size; |
| 273 | j->flags.suppl_gids = 1; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 276 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 277 | { |
| 278 | char *buf = NULL; |
| 279 | struct passwd pw; |
| 280 | struct passwd *ppw = NULL; |
| 281 | ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 282 | if (sz == -1) |
| 283 | sz = 65536; /* your guess is as good as mine... */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 284 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 285 | /* |
| 286 | * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 287 | * the maximum needed size of the buffer, so we don't have to search. |
| 288 | */ |
| 289 | buf = malloc(sz); |
| 290 | if (!buf) |
| 291 | return -ENOMEM; |
| 292 | getpwnam_r(user, &pw, buf, sz, &ppw); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 293 | /* |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 294 | * We're safe to free the buffer here. The strings inside |pw| point |
| 295 | * inside |buf|, but we don't use any of them; this leaves the pointers |
Jorge Lucangeli Obes | 87bf01d | 2016-03-08 11:20:03 -0800 | [diff] [blame] | 296 | * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3) |
| 297 | * succeeded. |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 298 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 299 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 300 | /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 301 | if (!ppw) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 302 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 303 | minijail_change_uid(j, ppw->pw_uid); |
| 304 | j->user = strdup(user); |
| 305 | if (!j->user) |
| 306 | return -ENOMEM; |
| 307 | j->usergid = ppw->pw_gid; |
| 308 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 311 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 312 | { |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 313 | char *buf = NULL; |
Yabin Cui | 1b21c8f | 2015-07-22 10:34:45 -0700 | [diff] [blame] | 314 | struct group gr; |
| 315 | struct group *pgr = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 316 | ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 317 | if (sz == -1) |
| 318 | sz = 65536; /* and mine is as good as yours, really */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 319 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 320 | /* |
| 321 | * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 322 | * the maximum needed size of the buffer, so we don't have to search. |
| 323 | */ |
| 324 | buf = malloc(sz); |
| 325 | if (!buf) |
| 326 | return -ENOMEM; |
| 327 | getgrnam_r(group, &gr, buf, sz, &pgr); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 328 | /* |
| 329 | * We're safe to free the buffer here. The strings inside gr point |
| 330 | * inside buf, but we don't use any of them; this leaves the pointers |
| 331 | * dangling but it's safe. pgr points at gr if getgrnam_r succeeded. |
| 332 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 333 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 334 | /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 335 | if (!pgr) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 336 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 337 | minijail_change_gid(j, pgr->gr_gid); |
| 338 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 341 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 342 | { |
| 343 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 346 | void API minijail_no_new_privs(struct minijail *j) |
| 347 | { |
| 348 | j->flags.no_new_privs = 1; |
| 349 | } |
| 350 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 351 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 352 | { |
| 353 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 356 | void API minijail_set_seccomp_filter_tsync(struct minijail *j) |
| 357 | { |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 358 | if (j->filter_len > 0 && j->filter_prog != NULL) { |
| 359 | die("minijail_set_seccomp_filter_tsync() must be called " |
| 360 | "before minijail_parse_seccomp_filters()"); |
| 361 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 362 | j->flags.seccomp_filter_tsync = 1; |
| 363 | } |
| 364 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 365 | void API minijail_log_seccomp_filter_failures(struct minijail *j) |
| 366 | { |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 367 | if (j->filter_len > 0 && j->filter_prog != NULL) { |
| 368 | die("minijail_log_seccomp_filter_failures() must be called " |
| 369 | "before minijail_parse_seccomp_filters()"); |
| 370 | } |
| 371 | j->flags.seccomp_filter_logging = 1; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 374 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 375 | { |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 376 | /* |
| 377 | * 'minijail_use_caps' configures a runtime-capabilities-only |
| 378 | * environment, including a bounding set matching the thread's runtime |
| 379 | * (permitted|inheritable|effective) sets. |
| 380 | * Therefore, it will override any existing bounding set configurations |
| 381 | * since the latter would allow gaining extra runtime capabilities from |
| 382 | * file capabilities. |
| 383 | */ |
| 384 | if (j->flags.capbset_drop) { |
| 385 | warn("overriding bounding set configuration"); |
| 386 | j->cap_bset = 0; |
| 387 | j->flags.capbset_drop = 0; |
| 388 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 389 | j->caps = capmask; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 390 | j->flags.use_caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 393 | void API minijail_capbset_drop(struct minijail *j, uint64_t capmask) |
| 394 | { |
| 395 | if (j->flags.use_caps) { |
| 396 | /* |
| 397 | * 'minijail_use_caps' will have already configured a capability |
| 398 | * bounding set matching the (permitted|inheritable|effective) |
| 399 | * sets. Abort if the user tries to configure a separate |
| 400 | * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps' |
| 401 | * are mutually exclusive. |
| 402 | */ |
| 403 | die("runtime capabilities already configured, can't drop " |
| 404 | "bounding set separately"); |
| 405 | } |
| 406 | j->cap_bset = capmask; |
| 407 | j->flags.capbset_drop = 1; |
| 408 | } |
| 409 | |
| 410 | void API minijail_reset_signal_mask(struct minijail *j) |
| 411 | { |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 412 | j->flags.reset_signal_mask = 1; |
| 413 | } |
| 414 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 415 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 416 | { |
| 417 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 420 | void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path) |
| 421 | { |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 422 | int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC); |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 423 | if (ns_fd < 0) { |
| 424 | pdie("failed to open namespace '%s'", ns_path); |
| 425 | } |
| 426 | j->mountns_fd = ns_fd; |
| 427 | j->flags.enter_vfs = 1; |
| 428 | } |
| 429 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 430 | void API minijail_skip_remount_private(struct minijail *j) |
| 431 | { |
| 432 | j->flags.skip_remount_private = 1; |
| 433 | } |
| 434 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 435 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 436 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 437 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 438 | j->flags.remount_proc_ro = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 439 | j->flags.pids = 1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 440 | j->flags.do_init = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 441 | } |
| 442 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 443 | void API minijail_namespace_ipc(struct minijail *j) |
| 444 | { |
| 445 | j->flags.ipc = 1; |
| 446 | } |
| 447 | |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 448 | void API minijail_namespace_net(struct minijail *j) |
| 449 | { |
| 450 | j->flags.net = 1; |
| 451 | } |
| 452 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 453 | void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path) |
| 454 | { |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 455 | int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 456 | if (ns_fd < 0) { |
| 457 | pdie("failed to open namespace '%s'", ns_path); |
| 458 | } |
| 459 | j->netns_fd = ns_fd; |
| 460 | j->flags.enter_net = 1; |
| 461 | } |
| 462 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 463 | void API minijail_namespace_cgroups(struct minijail *j) |
| 464 | { |
| 465 | j->flags.ns_cgroups = 1; |
| 466 | } |
| 467 | |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 468 | void API minijail_close_open_fds(struct minijail *j) |
| 469 | { |
| 470 | j->flags.close_open_fds = 1; |
| 471 | } |
| 472 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 473 | void API minijail_remount_proc_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 474 | { |
| 475 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 476 | j->flags.remount_proc_ro = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 479 | void API minijail_namespace_user(struct minijail *j) |
| 480 | { |
| 481 | j->flags.userns = 1; |
| 482 | } |
| 483 | |
Jorge Lucangeli Obes | 200299c | 2016-09-23 15:21:57 -0400 | [diff] [blame] | 484 | void API minijail_namespace_user_disable_setgroups(struct minijail *j) |
| 485 | { |
| 486 | j->flags.disable_setgroups = 1; |
| 487 | } |
| 488 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 489 | int API minijail_uidmap(struct minijail *j, const char *uidmap) |
| 490 | { |
| 491 | j->uidmap = strdup(uidmap); |
| 492 | if (!j->uidmap) |
| 493 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 494 | char *ch; |
| 495 | for (ch = j->uidmap; *ch; ch++) { |
| 496 | if (*ch == ',') |
| 497 | *ch = '\n'; |
| 498 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | int API minijail_gidmap(struct minijail *j, const char *gidmap) |
| 503 | { |
| 504 | j->gidmap = strdup(gidmap); |
| 505 | if (!j->gidmap) |
| 506 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 507 | char *ch; |
| 508 | for (ch = j->gidmap; *ch; ch++) { |
| 509 | if (*ch == ',') |
| 510 | *ch = '\n'; |
| 511 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 515 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 516 | { |
| 517 | j->flags.usergroups = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 520 | void API minijail_run_as_init(struct minijail *j) |
| 521 | { |
| 522 | /* |
| 523 | * Since the jailed program will become 'init' in the new PID namespace, |
| 524 | * Minijail does not need to fork an 'init' process. |
| 525 | */ |
| 526 | j->flags.do_init = 0; |
| 527 | } |
| 528 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 529 | int API minijail_enter_chroot(struct minijail *j, const char *dir) |
| 530 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 531 | if (j->chrootdir) |
| 532 | return -EINVAL; |
| 533 | j->chrootdir = strdup(dir); |
| 534 | if (!j->chrootdir) |
| 535 | return -ENOMEM; |
| 536 | j->flags.chroot = 1; |
| 537 | return 0; |
| 538 | } |
| 539 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 540 | int API minijail_enter_pivot_root(struct minijail *j, const char *dir) |
| 541 | { |
| 542 | if (j->chrootdir) |
| 543 | return -EINVAL; |
| 544 | j->chrootdir = strdup(dir); |
| 545 | if (!j->chrootdir) |
| 546 | return -ENOMEM; |
| 547 | j->flags.pivot_root = 1; |
| 548 | return 0; |
| 549 | } |
| 550 | |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 551 | char API *minijail_get_original_path(struct minijail *j, |
| 552 | const char *path_inside_chroot) |
| 553 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 554 | struct mountpoint *b; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 555 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 556 | b = j->mounts_head; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 557 | while (b) { |
| 558 | /* |
| 559 | * If |path_inside_chroot| is the exact destination of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 560 | * mount, then the original path is exactly the source of |
| 561 | * the mount. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 562 | * for example: "-b /some/path/exe,/chroot/path/exe" |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 563 | * mount source = /some/path/exe, mount dest = |
| 564 | * /chroot/path/exe Then when getting the original path of |
| 565 | * "/chroot/path/exe", the source of that mount, |
| 566 | * "/some/path/exe" is what should be returned. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 567 | */ |
| 568 | if (!strcmp(b->dest, path_inside_chroot)) |
| 569 | return strdup(b->src); |
| 570 | |
| 571 | /* |
| 572 | * If |path_inside_chroot| is within the destination path of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 573 | * mount, take the suffix of the chroot path relative to the |
| 574 | * mount destination path, and append it to the mount source |
| 575 | * path. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 576 | */ |
| 577 | if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) { |
| 578 | const char *relative_path = |
| 579 | path_inside_chroot + strlen(b->dest); |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 580 | return path_join(b->src, relative_path); |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 581 | } |
| 582 | b = b->next; |
| 583 | } |
| 584 | |
| 585 | /* If there is a chroot path, append |path_inside_chroot| to that. */ |
| 586 | if (j->chrootdir) |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 587 | return path_join(j->chrootdir, path_inside_chroot); |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 588 | |
| 589 | /* No chroot, so the path outside is the same as it is inside. */ |
| 590 | return strdup(path_inside_chroot); |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 593 | void API minijail_mount_tmp(struct minijail *j) |
| 594 | { |
| 595 | j->flags.mount_tmp = 1; |
| 596 | } |
| 597 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 598 | int API minijail_write_pid_file(struct minijail *j, const char *path) |
| 599 | { |
| 600 | j->pid_file_path = strdup(path); |
| 601 | if (!j->pid_file_path) |
| 602 | return -ENOMEM; |
| 603 | j->flags.pid_file = 1; |
| 604 | return 0; |
| 605 | } |
| 606 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 607 | int API minijail_add_to_cgroup(struct minijail *j, const char *path) |
| 608 | { |
| 609 | if (j->cgroup_count >= MAX_CGROUPS) |
| 610 | return -ENOMEM; |
| 611 | j->cgroups[j->cgroup_count] = strdup(path); |
| 612 | if (!j->cgroups[j->cgroup_count]) |
| 613 | return -ENOMEM; |
| 614 | j->cgroup_count++; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 615 | j->flags.cgroups = 1; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 619 | int API minijail_mount_with_data(struct minijail *j, const char *src, |
| 620 | const char *dest, const char *type, |
| 621 | unsigned long flags, const char *data) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 622 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 623 | struct mountpoint *m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 624 | |
| 625 | if (*dest != '/') |
| 626 | return -EINVAL; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 627 | m = calloc(1, sizeof(*m)); |
| 628 | if (!m) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 629 | return -ENOMEM; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 630 | m->dest = strdup(dest); |
| 631 | if (!m->dest) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 632 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 633 | m->src = strdup(src); |
| 634 | if (!m->src) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 635 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 636 | m->type = strdup(type); |
| 637 | if (!m->type) |
| 638 | goto error; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 639 | if (data) { |
| 640 | m->data = strdup(data); |
| 641 | if (!m->data) |
| 642 | goto error; |
| 643 | m->has_data = 1; |
| 644 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 645 | m->flags = flags; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 646 | |
Jorge Lucangeli Obes | 6c755d2 | 2016-01-28 15:24:40 -0800 | [diff] [blame] | 647 | info("mount %s -> %s type '%s'", src, dest, type); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 648 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 649 | /* |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 650 | * Force vfs namespacing so the mounts don't leak out into the |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 651 | * containing vfs namespace. |
| 652 | */ |
| 653 | minijail_namespace_vfs(j); |
| 654 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 655 | if (j->mounts_tail) |
| 656 | j->mounts_tail->next = m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 657 | else |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 658 | j->mounts_head = m; |
| 659 | j->mounts_tail = m; |
| 660 | j->mounts_count++; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 661 | |
| 662 | return 0; |
| 663 | |
| 664 | error: |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 665 | free(m->type); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 666 | free(m->src); |
| 667 | free(m->dest); |
| 668 | free(m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 669 | return -ENOMEM; |
| 670 | } |
| 671 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 672 | int API minijail_mount(struct minijail *j, const char *src, const char *dest, |
| 673 | const char *type, unsigned long flags) |
| 674 | { |
| 675 | return minijail_mount_with_data(j, src, dest, type, flags, NULL); |
| 676 | } |
| 677 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 678 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
| 679 | int writeable) |
| 680 | { |
| 681 | unsigned long flags = MS_BIND; |
| 682 | |
| 683 | if (!writeable) |
| 684 | flags |= MS_RDONLY; |
| 685 | |
| 686 | return minijail_mount(j, src, dest, "", flags); |
| 687 | } |
| 688 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 689 | static void clear_seccomp_options(struct minijail *j) |
| 690 | { |
| 691 | j->flags.seccomp_filter = 0; |
| 692 | j->flags.seccomp_filter_tsync = 0; |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 693 | j->flags.seccomp_filter_logging = 0; |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 694 | j->filter_len = 0; |
| 695 | j->filter_prog = NULL; |
| 696 | j->flags.no_new_privs = 0; |
| 697 | } |
| 698 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 699 | static int seccomp_should_parse_filters(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 700 | { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 701 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) { |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 702 | /* |
| 703 | * |errno| will be set to EINVAL when seccomp has not been |
| 704 | * compiled into the kernel. On certain platforms and kernel |
| 705 | * versions this is not a fatal failure. In that case, and only |
| 706 | * in that case, disable seccomp and skip loading the filters. |
| 707 | */ |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 708 | if ((errno == EINVAL) && seccomp_can_softfail()) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 709 | warn("not loading seccomp filters, seccomp filter not " |
| 710 | "supported"); |
| 711 | clear_seccomp_options(j); |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 712 | return 0; |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 713 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 714 | /* |
| 715 | * If |errno| != EINVAL or seccomp_can_softfail() is false, |
| 716 | * we can proceed. Worst case scenario minijail_enter() will |
| 717 | * abort() if seccomp fails. |
| 718 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 719 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 720 | if (j->flags.seccomp_filter_tsync) { |
| 721 | /* Are the seccomp(2) syscall and the TSYNC option supported? */ |
| 722 | if (sys_seccomp(SECCOMP_SET_MODE_FILTER, |
| 723 | SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) { |
| 724 | int saved_errno = errno; |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 725 | if (saved_errno == ENOSYS && seccomp_can_softfail()) { |
| 726 | warn("seccomp(2) syscall not supported"); |
| 727 | clear_seccomp_options(j); |
| 728 | return 0; |
| 729 | } else if (saved_errno == EINVAL && |
| 730 | seccomp_can_softfail()) { |
| 731 | warn( |
| 732 | "seccomp filter thread sync not supported"); |
| 733 | clear_seccomp_options(j); |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 734 | return 0; |
| 735 | } |
| 736 | /* |
| 737 | * Similar logic here. If seccomp_can_softfail() is |
| 738 | * false, or |errno| != ENOSYS, or |errno| != EINVAL, |
| 739 | * we can proceed. Worst case scenario minijail_enter() |
| 740 | * will abort() if seccomp or TSYNC fail. |
| 741 | */ |
| 742 | } |
| 743 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 744 | return 1; |
| 745 | } |
| 746 | |
| 747 | static int parse_seccomp_filters(struct minijail *j, FILE *policy_file) |
| 748 | { |
| 749 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 750 | int use_ret_trap = |
| 751 | j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging; |
| 752 | int allow_logging = j->flags.seccomp_filter_logging; |
| 753 | |
| 754 | if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) { |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 755 | free(fprog); |
| 756 | return -1; |
| 757 | } |
| 758 | |
| 759 | j->filter_len = fprog->len; |
| 760 | j->filter_prog = fprog; |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
| 765 | { |
| 766 | if (!seccomp_should_parse_filters(j)) |
| 767 | return; |
| 768 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 769 | FILE *file = fopen(path, "r"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 770 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 771 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 772 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 773 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 774 | if (parse_seccomp_filters(j, file) != 0) { |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 775 | die("failed to compile seccomp filter BPF program in '%s'", |
| 776 | path); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 777 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 778 | fclose(file); |
| 779 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 780 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 781 | void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd) |
| 782 | { |
| 783 | if (!seccomp_should_parse_filters(j)) |
| 784 | return; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 785 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 786 | FILE *file = fdopen(fd, "r"); |
| 787 | if (!file) { |
| 788 | pdie("failed to associate stream with fd %d", fd); |
| 789 | } |
| 790 | |
| 791 | if (parse_seccomp_filters(j, file) != 0) { |
| 792 | die("failed to compile seccomp filter BPF program from fd %d", |
| 793 | fd); |
| 794 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 795 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 796 | } |
| 797 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 798 | int API minijail_use_alt_syscall(struct minijail *j, const char *table) |
| 799 | { |
| 800 | j->alt_syscall_table = strdup(table); |
| 801 | if (!j->alt_syscall_table) |
| 802 | return -ENOMEM; |
| 803 | j->flags.alt_syscall = 1; |
| 804 | return 0; |
| 805 | } |
| 806 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 807 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 808 | size_t available; |
| 809 | size_t total; |
| 810 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 811 | }; |
| 812 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 813 | void marshal_state_init(struct marshal_state *state, char *buf, |
| 814 | size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 815 | { |
| 816 | state->available = available; |
| 817 | state->buf = buf; |
| 818 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 819 | } |
| 820 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 821 | void marshal_append(struct marshal_state *state, void *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 822 | { |
| 823 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 824 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 825 | /* Up to |available| will be written. */ |
| 826 | if (copy_len) { |
| 827 | memcpy(state->buf, src, copy_len); |
| 828 | state->buf += copy_len; |
| 829 | state->available -= copy_len; |
| 830 | } |
| 831 | /* |total| will contain the expected length. */ |
| 832 | state->total += length; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 833 | } |
| 834 | |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 835 | void marshal_mount(struct marshal_state *state, const struct mountpoint *m) |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 836 | { |
| 837 | marshal_append(state, m->src, strlen(m->src) + 1); |
| 838 | marshal_append(state, m->dest, strlen(m->dest) + 1); |
| 839 | marshal_append(state, m->type, strlen(m->type) + 1); |
| 840 | marshal_append(state, (char *)&m->has_data, sizeof(m->has_data)); |
| 841 | if (m->has_data) |
| 842 | marshal_append(state, m->data, strlen(m->data) + 1); |
| 843 | marshal_append(state, (char *)&m->flags, sizeof(m->flags)); |
| 844 | } |
| 845 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 846 | void minijail_marshal_helper(struct marshal_state *state, |
| 847 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 848 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 849 | struct mountpoint *m = NULL; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 850 | size_t i; |
| 851 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 852 | marshal_append(state, (char *)j, sizeof(*j)); |
| 853 | if (j->user) |
| 854 | marshal_append(state, j->user, strlen(j->user) + 1); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 855 | if (j->suppl_gid_list) { |
| 856 | marshal_append(state, j->suppl_gid_list, |
| 857 | j->suppl_gid_count * sizeof(gid_t)); |
| 858 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 859 | if (j->chrootdir) |
| 860 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 861 | if (j->alt_syscall_table) { |
| 862 | marshal_append(state, j->alt_syscall_table, |
| 863 | strlen(j->alt_syscall_table) + 1); |
| 864 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 865 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 866 | struct sock_fprog *fp = j->filter_prog; |
| 867 | marshal_append(state, (char *)fp->filter, |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 868 | fp->len * sizeof(struct sock_filter)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 869 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 870 | for (m = j->mounts_head; m; m = m->next) { |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 871 | marshal_mount(state, m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 872 | } |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 873 | for (i = 0; i < j->cgroup_count; ++i) |
| 874 | marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 875 | } |
| 876 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 877 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 878 | { |
| 879 | struct marshal_state state; |
| 880 | marshal_state_init(&state, NULL, 0); |
| 881 | minijail_marshal_helper(&state, j); |
| 882 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 883 | } |
| 884 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 885 | int minijail_marshal(const struct minijail *j, char *buf, size_t available) |
| 886 | { |
| 887 | struct marshal_state state; |
| 888 | marshal_state_init(&state, buf, available); |
| 889 | minijail_marshal_helper(&state, j); |
| 890 | return (state.total > available); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 891 | } |
| 892 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 893 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 894 | { |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 895 | size_t i; |
| 896 | size_t count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 897 | int ret = -EINVAL; |
| 898 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 899 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 900 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 901 | memcpy((void *)j, serialized, sizeof(*j)); |
| 902 | serialized += sizeof(*j); |
| 903 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 904 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 905 | /* Potentially stale pointers not used as signals. */ |
Jorge Lucangeli Obes | 3b2e6e4 | 2016-08-04 12:26:19 -0400 | [diff] [blame] | 906 | j->pid_file_path = NULL; |
| 907 | j->uidmap = NULL; |
| 908 | j->gidmap = NULL; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 909 | j->mounts_head = NULL; |
| 910 | j->mounts_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 911 | j->filter_prog = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 912 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 913 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 914 | char *user = consumestr(&serialized, &length); |
| 915 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 916 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 917 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 918 | if (!j->user) |
| 919 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 920 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 921 | |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 922 | if (j->suppl_gid_list) { /* stale pointer */ |
| 923 | if (j->suppl_gid_count > NGROUPS_MAX) { |
| 924 | goto bad_gid_list; |
| 925 | } |
| 926 | size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t); |
| 927 | void *gid_list_bytes = |
| 928 | consumebytes(gid_list_size, &serialized, &length); |
| 929 | if (!gid_list_bytes) |
| 930 | goto bad_gid_list; |
| 931 | |
| 932 | j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t)); |
| 933 | if (!j->suppl_gid_list) |
| 934 | goto bad_gid_list; |
| 935 | |
| 936 | memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size); |
| 937 | } |
| 938 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 939 | if (j->chrootdir) { /* stale pointer */ |
| 940 | char *chrootdir = consumestr(&serialized, &length); |
| 941 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 942 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 943 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 944 | if (!j->chrootdir) |
| 945 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 946 | } |
| 947 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 948 | if (j->alt_syscall_table) { /* stale pointer */ |
| 949 | char *alt_syscall_table = consumestr(&serialized, &length); |
| 950 | if (!alt_syscall_table) |
| 951 | goto bad_syscall_table; |
| 952 | j->alt_syscall_table = strdup(alt_syscall_table); |
| 953 | if (!j->alt_syscall_table) |
| 954 | goto bad_syscall_table; |
| 955 | } |
| 956 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 957 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 958 | size_t ninstrs = j->filter_len; |
| 959 | if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) || |
| 960 | ninstrs > USHRT_MAX) |
| 961 | goto bad_filters; |
| 962 | |
| 963 | size_t program_len = ninstrs * sizeof(struct sock_filter); |
| 964 | void *program = consumebytes(program_len, &serialized, &length); |
| 965 | if (!program) |
| 966 | goto bad_filters; |
| 967 | |
| 968 | j->filter_prog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 969 | if (!j->filter_prog) |
| 970 | goto bad_filters; |
| 971 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 972 | j->filter_prog->len = ninstrs; |
| 973 | j->filter_prog->filter = malloc(program_len); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 974 | if (!j->filter_prog->filter) |
| 975 | goto bad_filter_prog_instrs; |
| 976 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 977 | memcpy(j->filter_prog->filter, program, program_len); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 978 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 979 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 980 | count = j->mounts_count; |
| 981 | j->mounts_count = 0; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 982 | for (i = 0; i < count; ++i) { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 983 | unsigned long *flags; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 984 | int *has_data; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 985 | const char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 986 | const char *type; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 987 | const char *data = NULL; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 988 | const char *src = consumestr(&serialized, &length); |
| 989 | if (!src) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 990 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 991 | dest = consumestr(&serialized, &length); |
| 992 | if (!dest) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 993 | goto bad_mounts; |
| 994 | type = consumestr(&serialized, &length); |
| 995 | if (!type) |
| 996 | goto bad_mounts; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 997 | has_data = consumebytes(sizeof(*has_data), &serialized, |
| 998 | &length); |
| 999 | if (!has_data) |
| 1000 | goto bad_mounts; |
| 1001 | if (*has_data) { |
| 1002 | data = consumestr(&serialized, &length); |
| 1003 | if (!data) |
| 1004 | goto bad_mounts; |
| 1005 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1006 | flags = consumebytes(sizeof(*flags), &serialized, &length); |
| 1007 | if (!flags) |
| 1008 | goto bad_mounts; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1009 | if (minijail_mount_with_data(j, src, dest, type, *flags, data)) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1010 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1011 | } |
| 1012 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1013 | count = j->cgroup_count; |
| 1014 | j->cgroup_count = 0; |
| 1015 | for (i = 0; i < count; ++i) { |
| 1016 | char *cgroup = consumestr(&serialized, &length); |
| 1017 | if (!cgroup) |
| 1018 | goto bad_cgroups; |
| 1019 | j->cgroups[i] = strdup(cgroup); |
| 1020 | if (!j->cgroups[i]) |
| 1021 | goto bad_cgroups; |
| 1022 | ++j->cgroup_count; |
| 1023 | } |
| 1024 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1025 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1026 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1027 | bad_cgroups: |
| 1028 | while (j->mounts_head) { |
| 1029 | struct mountpoint *m = j->mounts_head; |
| 1030 | j->mounts_head = j->mounts_head->next; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1031 | free(m->data); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1032 | free(m->type); |
| 1033 | free(m->dest); |
| 1034 | free(m->src); |
| 1035 | free(m); |
| 1036 | } |
| 1037 | for (i = 0; i < j->cgroup_count; ++i) |
| 1038 | free(j->cgroups[i]); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1039 | bad_mounts: |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1040 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 1041 | free(j->filter_prog->filter); |
| 1042 | free(j->filter_prog); |
| 1043 | } |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1044 | bad_filter_prog_instrs: |
| 1045 | if (j->filter_prog) |
| 1046 | free(j->filter_prog); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1047 | bad_filters: |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1048 | if (j->alt_syscall_table) |
| 1049 | free(j->alt_syscall_table); |
| 1050 | bad_syscall_table: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1051 | if (j->chrootdir) |
| 1052 | free(j->chrootdir); |
| 1053 | bad_chrootdir: |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1054 | if (j->suppl_gid_list) |
| 1055 | free(j->suppl_gid_list); |
| 1056 | bad_gid_list: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1057 | if (j->user) |
| 1058 | free(j->user); |
| 1059 | clear_pointers: |
| 1060 | j->user = NULL; |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1061 | j->suppl_gid_list = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1062 | j->chrootdir = NULL; |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1063 | j->alt_syscall_table = NULL; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1064 | j->cgroup_count = 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1065 | out: |
| 1066 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 1067 | } |
| 1068 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1069 | /* |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 1070 | * setup_mount_destination: Ensures the mount target exists. |
| 1071 | * Creates it if needed and possible. |
Dylan Reid | eec7796 | 2016-06-30 19:35:10 -0700 | [diff] [blame] | 1072 | */ |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1073 | static int setup_mount_destination(const char *source, const char *dest, |
| 1074 | uid_t uid, uid_t gid) |
Dylan Reid | eec7796 | 2016-06-30 19:35:10 -0700 | [diff] [blame] | 1075 | { |
| 1076 | int rc; |
| 1077 | struct stat st_buf; |
| 1078 | |
| 1079 | rc = stat(dest, &st_buf); |
| 1080 | if (rc == 0) /* destination exists */ |
| 1081 | return 0; |
| 1082 | |
| 1083 | /* |
| 1084 | * Try to create the destination. |
| 1085 | * Either make a directory or touch a file depending on the source type. |
| 1086 | * If the source doesn't exist, assume it is a filesystem type such as |
| 1087 | * "tmpfs" and create a directory to mount it on. |
| 1088 | */ |
| 1089 | rc = stat(source, &st_buf); |
| 1090 | if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) { |
| 1091 | if (mkdir(dest, 0700)) |
| 1092 | return -errno; |
| 1093 | } else { |
| 1094 | int fd = open(dest, O_RDWR | O_CREAT, 0700); |
| 1095 | if (fd < 0) |
| 1096 | return -errno; |
| 1097 | close(fd); |
| 1098 | } |
| 1099 | return chown(dest, uid, gid); |
| 1100 | } |
| 1101 | |
| 1102 | /* |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1103 | * mount_one: Applies mounts from @m for @j, recursing as needed. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1104 | * @j Minijail these mounts are for |
| 1105 | * @m Head of list of mounts |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1106 | * |
| 1107 | * Returns 0 for success. |
| 1108 | */ |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1109 | static int mount_one(const struct minijail *j, struct mountpoint *m) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 1110 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1111 | int ret; |
| 1112 | char *dest; |
| 1113 | int remount_ro = 0; |
| 1114 | |
Jorge Lucangeli Obes | 2b12ba4 | 2016-01-26 10:37:51 -0800 | [diff] [blame] | 1115 | /* |dest| has a leading "/". */ |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1116 | if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1117 | return -ENOMEM; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1118 | |
Dylan Reid | eec7796 | 2016-06-30 19:35:10 -0700 | [diff] [blame] | 1119 | if (setup_mount_destination(m->src, dest, j->uid, j->gid)) |
| 1120 | pdie("creating mount target '%s' failed", dest); |
| 1121 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1122 | /* |
Jorge Lucangeli Obes | 2b12ba4 | 2016-01-26 10:37:51 -0800 | [diff] [blame] | 1123 | * R/O bind mounts have to be remounted since 'bind' and 'ro' |
| 1124 | * can't both be specified in the original bind mount. |
| 1125 | * Remount R/O after the initial mount. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1126 | */ |
| 1127 | if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) { |
| 1128 | remount_ro = 1; |
| 1129 | m->flags &= ~MS_RDONLY; |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 1130 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1131 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1132 | ret = mount(m->src, dest, m->type, m->flags, m->data); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1133 | if (ret) |
| 1134 | pdie("mount: %s -> %s", m->src, dest); |
| 1135 | |
| 1136 | if (remount_ro) { |
| 1137 | m->flags |= MS_RDONLY; |
| 1138 | ret = mount(m->src, dest, NULL, |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1139 | m->flags | MS_REMOUNT, m->data); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1140 | if (ret) |
| 1141 | pdie("bind ro: %s -> %s", m->src, dest); |
| 1142 | } |
| 1143 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1144 | free(dest); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1145 | if (m->next) |
| 1146 | return mount_one(j, m->next); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1147 | return ret; |
| 1148 | } |
| 1149 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1150 | static int enter_chroot(const struct minijail *j) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 1151 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1152 | int ret; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1153 | |
| 1154 | if (j->mounts_head && (ret = mount_one(j, j->mounts_head))) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1155 | return ret; |
| 1156 | |
| 1157 | if (chroot(j->chrootdir)) |
| 1158 | return -errno; |
| 1159 | |
| 1160 | if (chdir("/")) |
| 1161 | return -errno; |
| 1162 | |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1166 | static int enter_pivot_root(const struct minijail *j) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1167 | { |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1168 | int ret, oldroot, newroot; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1169 | |
| 1170 | if (j->mounts_head && (ret = mount_one(j, j->mounts_head))) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1171 | return ret; |
| 1172 | |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1173 | /* |
| 1174 | * Keep the fd for both old and new root. |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1175 | * It will be used in fchdir(2) later. |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1176 | */ |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1177 | oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1178 | if (oldroot < 0) |
| 1179 | pdie("failed to open / for fchdir"); |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1180 | newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1181 | if (newroot < 0) |
| 1182 | pdie("failed to open %s for fchdir", j->chrootdir); |
| 1183 | |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1184 | /* |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1185 | * To ensure j->chrootdir is the root of a filesystem, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1186 | * do a self bind mount. |
| 1187 | */ |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1188 | if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, "")) |
| 1189 | pdie("failed to bind mount '%s'", j->chrootdir); |
| 1190 | if (chdir(j->chrootdir)) |
| 1191 | return -errno; |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1192 | if (syscall(SYS_pivot_root, ".", ".")) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1193 | pdie("pivot_root"); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1194 | |
| 1195 | /* |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1196 | * 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] | 1197 | * change to the old root and unmount it. |
| 1198 | */ |
| 1199 | if (fchdir(oldroot)) |
| 1200 | pdie("failed to fchdir to old /"); |
Hidehiko Abe | 097b719 | 2016-03-16 18:00:36 +0900 | [diff] [blame] | 1201 | |
| 1202 | /* |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1203 | * If j->flags.skip_remount_private was enabled for minijail_enter(), |
| 1204 | * there could be a shared mount point under |oldroot|. In that case, |
| 1205 | * mounts under this shared mount point will be unmounted below, and |
| 1206 | * this unmounting will propagate to the original mount namespace |
| 1207 | * (because the mount point is shared). To prevent this unexpected |
| 1208 | * unmounting, remove these mounts from their peer groups by recursively |
| 1209 | * remounting them as MS_PRIVATE. |
Hidehiko Abe | 097b719 | 2016-03-16 18:00:36 +0900 | [diff] [blame] | 1210 | */ |
| 1211 | if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL)) |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1212 | pdie("failed to mount(/, private) before umount(/)"); |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1213 | /* The old root might be busy, so use lazy unmount. */ |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1214 | if (umount2(".", MNT_DETACH)) |
| 1215 | pdie("umount(/)"); |
| 1216 | /* Change back to the new root. */ |
| 1217 | if (fchdir(newroot)) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1218 | return -errno; |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1219 | if (close(oldroot)) |
| 1220 | return -errno; |
| 1221 | if (close(newroot)) |
| 1222 | return -errno; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1223 | if (chroot("/")) |
| 1224 | return -errno; |
Jorge Lucangeli Obes | 46a5509 | 2015-10-12 15:31:59 -0700 | [diff] [blame] | 1225 | /* Set correct CWD for getcwd(3). */ |
| 1226 | if (chdir("/")) |
| 1227 | return -errno; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1232 | static int mount_tmp(void) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1233 | { |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 1234 | return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777"); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1237 | static int remount_proc_readonly(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1238 | { |
| 1239 | const char *kProcPath = "/proc"; |
| 1240 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1241 | /* |
| 1242 | * 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] | 1243 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 1244 | * 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] | 1245 | * namespace (!). Instead, remove their mount from our namespace lazily |
| 1246 | * (MNT_DETACH) and make our own. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1247 | */ |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1248 | if (umount2(kProcPath, MNT_DETACH)) { |
| 1249 | /* |
| 1250 | * If we are in a new user namespace, umount(2) will fail. |
| 1251 | * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html |
| 1252 | */ |
| 1253 | if (j->flags.userns) { |
| 1254 | info("umount(/proc, MNT_DETACH) failed, " |
| 1255 | "this is expected when using user namespaces"); |
| 1256 | } else { |
| 1257 | return -errno; |
| 1258 | } |
| 1259 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1260 | if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
| 1261 | return -errno; |
| 1262 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1263 | } |
| 1264 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1265 | 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] | 1266 | { |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1267 | kill(j->initpid, SIGKILL); |
| 1268 | die("%s", msg); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1271 | static void write_pid_file_or_die(const struct minijail *j) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1272 | { |
Keshav Santhanam | db6dab4 | 2016-08-10 16:33:34 -0700 | [diff] [blame] | 1273 | if (write_pid_to_path(j->initpid, j->pid_file_path)) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1274 | kill_child_and_die(j, "failed to write pid file"); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1277 | static void add_to_cgroups_or_die(const struct minijail *j) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1278 | { |
| 1279 | size_t i; |
| 1280 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1281 | for (i = 0; i < j->cgroup_count; ++i) { |
Keshav Santhanam | db6dab4 | 2016-08-10 16:33:34 -0700 | [diff] [blame] | 1282 | if (write_pid_to_path(j->initpid, j->cgroups[i])) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1283 | kill_child_and_die(j, "failed to add to cgroups"); |
| 1284 | } |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1287 | static void write_ugid_maps_or_die(const struct minijail *j) |
| 1288 | { |
| 1289 | if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0) |
| 1290 | kill_child_and_die(j, "failed to write uid_map"); |
Jorge Lucangeli Obes | 200299c | 2016-09-23 15:21:57 -0400 | [diff] [blame] | 1291 | if (j->gidmap && j->flags.disable_setgroups && |
| 1292 | write_proc_file(j->initpid, "deny", "setgroups") != 0) |
| 1293 | kill_child_and_die(j, "failed to disable setgroups(2)"); |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1294 | if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0) |
| 1295 | kill_child_and_die(j, "failed to write gid_map"); |
| 1296 | } |
| 1297 | |
| 1298 | static void enter_user_namespace(const struct minijail *j) |
| 1299 | { |
| 1300 | if (j->uidmap && setresuid(0, 0, 0)) |
| 1301 | pdie("user_namespaces: setresuid(0, 0, 0) failed"); |
| 1302 | if (j->gidmap && setresgid(0, 0, 0)) |
| 1303 | pdie("user_namespaces: setresgid(0, 0, 0) failed"); |
| 1304 | } |
| 1305 | |
| 1306 | static void parent_setup_complete(int *pipe_fds) |
| 1307 | { |
| 1308 | close(pipe_fds[0]); |
| 1309 | close(pipe_fds[1]); |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * wait_for_parent_setup: Called by the child process to wait for any |
| 1314 | * further parent-side setup to complete before continuing. |
| 1315 | */ |
| 1316 | static void wait_for_parent_setup(int *pipe_fds) |
| 1317 | { |
| 1318 | char buf; |
| 1319 | |
| 1320 | close(pipe_fds[1]); |
| 1321 | |
| 1322 | /* Wait for parent to complete setup and close the pipe. */ |
| 1323 | if (read(pipe_fds[0], &buf, 1) != 0) |
| 1324 | die("failed to sync with parent"); |
| 1325 | close(pipe_fds[0]); |
| 1326 | } |
| 1327 | |
| 1328 | static void drop_ugid(const struct minijail *j) |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1329 | { |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1330 | if (j->flags.usergroups && j->flags.suppl_gids) { |
| 1331 | die("tried to inherit *and* set supplementary groups;" |
| 1332 | " can only do one"); |
| 1333 | } |
| 1334 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1335 | if (j->flags.usergroups) { |
| 1336 | if (initgroups(j->user, j->usergid)) |
| 1337 | pdie("initgroups"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1338 | } else if (j->flags.suppl_gids) { |
| 1339 | if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) { |
| 1340 | pdie("setgroups"); |
| 1341 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1342 | } else { |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1343 | /* |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1344 | * Only attempt to clear supplementary groups if we are changing |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1345 | * users. |
| 1346 | */ |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1347 | if ((j->uid || j->gid) && setgroups(0, NULL)) |
| 1348 | pdie("setgroups"); |
| 1349 | } |
| 1350 | |
| 1351 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
| 1352 | pdie("setresgid"); |
| 1353 | |
| 1354 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
| 1355 | pdie("setresuid"); |
| 1356 | } |
| 1357 | |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1358 | /* |
| 1359 | * We specifically do not use cap_valid() as that only tells us the last |
| 1360 | * valid cap we were *compiled* against (i.e. what the version of kernel |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 1361 | * headers says). If we run on a different kernel version, then it's not |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1362 | * uncommon for that to be less (if an older kernel) or more (if a newer |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 1363 | * kernel). |
| 1364 | * Normally, we suck up the answer via /proc. On Android, not all processes are |
| 1365 | * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we |
| 1366 | * programmatically find the value by calling prctl(PR_CAPBSET_READ). |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1367 | */ |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 1368 | static unsigned int get_last_valid_cap() |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1369 | { |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 1370 | unsigned int last_valid_cap = 0; |
| 1371 | if (is_android()) { |
| 1372 | for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0; |
| 1373 | ++last_valid_cap); |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1374 | |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 1375 | /* |last_valid_cap| will be the first failing value. */ |
| 1376 | if (last_valid_cap > 0) { |
| 1377 | last_valid_cap--; |
| 1378 | } |
| 1379 | } else { |
| 1380 | const char cap_file[] = "/proc/sys/kernel/cap_last_cap"; |
| 1381 | FILE *fp = fopen(cap_file, "re"); |
| 1382 | if (fscanf(fp, "%u", &last_valid_cap) != 1) |
| 1383 | pdie("fscanf(%s)", cap_file); |
| 1384 | fclose(fp); |
| 1385 | } |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1386 | return last_valid_cap; |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1387 | } |
| 1388 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1389 | static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap) |
| 1390 | { |
| 1391 | const uint64_t one = 1; |
| 1392 | unsigned int i; |
| 1393 | for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) { |
| 1394 | if (keep_mask & (one << i)) |
| 1395 | continue; |
| 1396 | if (prctl(PR_CAPBSET_DROP, i)) |
| 1397 | pdie("could not drop capability from bounding set"); |
| 1398 | } |
| 1399 | } |
| 1400 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1401 | 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] | 1402 | { |
Jorge Lucangeli Obes | 7ea269e | 2016-02-26 22:07:09 -0800 | [diff] [blame] | 1403 | if (!j->flags.use_caps) |
| 1404 | return; |
| 1405 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1406 | cap_t caps = cap_get_proc(); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1407 | cap_value_t flag[1]; |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1408 | const uint64_t one = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1409 | unsigned int i; |
| 1410 | if (!caps) |
| 1411 | die("can't get process caps"); |
| 1412 | if (cap_clear_flag(caps, CAP_INHERITABLE)) |
| 1413 | die("can't clear inheritable caps"); |
| 1414 | if (cap_clear_flag(caps, CAP_EFFECTIVE)) |
| 1415 | die("can't clear effective caps"); |
| 1416 | if (cap_clear_flag(caps, CAP_PERMITTED)) |
| 1417 | die("can't clear permitted caps"); |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1418 | for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1419 | /* Keep CAP_SETPCAP for dropping bounding set bits. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1420 | if (i != CAP_SETPCAP && !(j->caps & (one << i))) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1421 | continue; |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1422 | flag[0] = i; |
| 1423 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1424 | die("can't add effective cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1425 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1426 | die("can't add permitted cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1427 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1428 | die("can't add inheritable cap"); |
| 1429 | } |
| 1430 | if (cap_set_proc(caps)) |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1431 | die("can't apply initial cleaned capset"); |
| 1432 | |
| 1433 | /* |
| 1434 | * Instead of dropping bounding set first, do it here in case |
| 1435 | * the caller had a more permissive bounding set which could |
| 1436 | * have been used above to raise a capability that wasn't already |
| 1437 | * present. This requires CAP_SETPCAP, so we raised/kept it above. |
| 1438 | */ |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1439 | drop_capbset(j->caps, last_valid_cap); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1440 | |
| 1441 | /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1442 | if ((j->caps & (one << CAP_SETPCAP)) == 0) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1443 | flag[0] = CAP_SETPCAP; |
| 1444 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR)) |
| 1445 | die("can't clear effective cap"); |
| 1446 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR)) |
| 1447 | die("can't clear permitted cap"); |
| 1448 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR)) |
| 1449 | die("can't clear inheritable cap"); |
| 1450 | } |
| 1451 | |
| 1452 | if (cap_set_proc(caps)) |
| 1453 | die("can't apply final cleaned capset"); |
| 1454 | |
| 1455 | cap_free(caps); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1456 | } |
| 1457 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1458 | static void set_seccomp_filter(const struct minijail *j) |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1459 | { |
| 1460 | /* |
| 1461 | * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c> |
| 1462 | * in the kernel source tree for an explanation of the parameters. |
| 1463 | */ |
| 1464 | if (j->flags.no_new_privs) { |
| 1465 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 1466 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 1467 | } |
| 1468 | |
| 1469 | /* |
Jorge Lucangeli Obes | 2413f37 | 2016-04-06 18:43:10 -0700 | [diff] [blame] | 1470 | * Code running with ASan |
| 1471 | * (https://github.com/google/sanitizers/wiki/AddressSanitizer) |
| 1472 | * will make system calls not included in the syscall filter policy, |
| 1473 | * which will likely crash the program. Skip setting seccomp filter in |
| 1474 | * that case. |
| 1475 | * 'running_with_asan()' has no inputs and is completely defined at |
| 1476 | * build time, so this cannot be used by an attacker to skip setting |
| 1477 | * seccomp filter. |
| 1478 | */ |
| 1479 | if (j->flags.seccomp_filter && running_with_asan()) { |
| 1480 | warn("running with ASan, not setting seccomp filter"); |
| 1481 | return; |
| 1482 | } |
| 1483 | |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 1484 | if (j->flags.seccomp_filter) { |
| 1485 | if (j->flags.seccomp_filter_logging) { |
| 1486 | /* |
| 1487 | * If logging seccomp filter failures, |
| 1488 | * install the SIGSYS handler first. |
| 1489 | */ |
| 1490 | if (install_sigsys_handler()) |
| 1491 | pdie("failed to install SIGSYS handler"); |
| 1492 | warn("logging seccomp filter failures"); |
| 1493 | } else if (j->flags.seccomp_filter_tsync) { |
| 1494 | /* |
| 1495 | * If setting thread sync, |
| 1496 | * reset the SIGSYS signal handler so that |
| 1497 | * the entire thread group is killed. |
| 1498 | */ |
| 1499 | if (signal(SIGSYS, SIG_DFL) == SIG_ERR) |
| 1500 | pdie("failed to reset SIGSYS disposition"); |
| 1501 | info("reset SIGSYS disposition"); |
| 1502 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | /* |
| 1506 | * Install the syscall filter. |
| 1507 | */ |
| 1508 | if (j->flags.seccomp_filter) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 1509 | if (j->flags.seccomp_filter_tsync) { |
| 1510 | if (sys_seccomp(SECCOMP_SET_MODE_FILTER, |
| 1511 | SECCOMP_FILTER_FLAG_TSYNC, |
| 1512 | j->filter_prog)) { |
| 1513 | pdie("seccomp(tsync) failed"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1514 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 1515 | } else { |
| 1516 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, |
| 1517 | j->filter_prog)) { |
| 1518 | pdie("prctl(seccomp_filter) failed"); |
| 1519 | } |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1520 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1521 | } |
| 1522 | } |
| 1523 | |
Mike Frysinger | 7559dfe | 2016-11-15 18:58:39 -0500 | [diff] [blame] | 1524 | static void net_bring_up_loopback(void) |
| 1525 | { |
| 1526 | static const char ifname[] = "lo"; |
| 1527 | int sock; |
| 1528 | struct ifreq ifr; |
| 1529 | |
| 1530 | /* Make sure people don't try to add really long names. */ |
| 1531 | _Static_assert(sizeof(ifname) <= IFNAMSIZ, "interface name too long"); |
| 1532 | |
| 1533 | sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0); |
| 1534 | if (sock < 0) |
| 1535 | pdie("socket(AF_LOCAL) failed"); |
| 1536 | |
| 1537 | /* |
| 1538 | * Do the equiv of `ip link set up lo`. The kernel will assign |
| 1539 | * IPv4 (127.0.0.1) & IPv6 (::1) addresses automatically! |
| 1540 | */ |
| 1541 | strcpy(ifr.ifr_name, ifname); |
| 1542 | if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) |
| 1543 | pdie("ioctl(SIOCGIFFLAGS) failed"); |
| 1544 | |
| 1545 | /* The kernel preserves ifr.ifr_name for use. */ |
| 1546 | ifr.ifr_flags |= IFF_UP | IFF_RUNNING; |
| 1547 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) |
| 1548 | pdie("ioctl(SIOCSIFFLAGS) failed"); |
| 1549 | |
| 1550 | close(sock); |
| 1551 | } |
| 1552 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1553 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1554 | { |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1555 | /* |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1556 | * If we're dropping caps, get the last valid cap from /proc now, |
| 1557 | * since /proc can be unmounted before drop_caps() is called. |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1558 | */ |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1559 | unsigned int last_valid_cap = 0; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1560 | if (j->flags.capbset_drop || j->flags.use_caps) |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1561 | last_valid_cap = get_last_valid_cap(); |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1562 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1563 | if (j->flags.pids) |
| 1564 | die("tried to enter a pid-namespaced jail;" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1565 | " try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1566 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1567 | if (j->flags.usergroups && !j->user) |
| 1568 | die("usergroup inheritance without username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1569 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1570 | /* |
| 1571 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1572 | * so we don't even try. If any of our operations fail, we abort() the |
| 1573 | * entire process. |
| 1574 | */ |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1575 | if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS)) |
| 1576 | pdie("setns(CLONE_NEWNS)"); |
| 1577 | |
Jorge Lucangeli Obes | 805be39 | 2015-10-12 15:55:59 -0700 | [diff] [blame] | 1578 | if (j->flags.vfs) { |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1579 | if (unshare(CLONE_NEWNS)) |
| 1580 | pdie("unshare(vfs)"); |
| 1581 | /* |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1582 | * Unless asked not to, remount all filesystems as private. |
| 1583 | * If they are shared, new bind mounts will creep out of our |
| 1584 | * namespace. |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1585 | * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt |
| 1586 | */ |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1587 | if (!j->flags.skip_remount_private) { |
| 1588 | if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) |
| 1589 | pdie("mount(/, private)"); |
| 1590 | } |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1591 | } |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 1592 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 1593 | if (j->flags.ipc && unshare(CLONE_NEWIPC)) { |
| 1594 | pdie("unshare(ipc)"); |
| 1595 | } |
| 1596 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 1597 | if (j->flags.enter_net) { |
| 1598 | if (setns(j->netns_fd, CLONE_NEWNET)) |
| 1599 | pdie("setns(CLONE_NEWNET)"); |
Mike Frysinger | 7559dfe | 2016-11-15 18:58:39 -0500 | [diff] [blame] | 1600 | } else if (j->flags.net) { |
| 1601 | if (unshare(CLONE_NEWNET)) |
| 1602 | pdie("unshare(net)"); |
| 1603 | net_bring_up_loopback(); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 1604 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1605 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 1606 | if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP)) |
| 1607 | pdie("unshare(cgroups)"); |
| 1608 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1609 | if (j->flags.chroot && enter_chroot(j)) |
| 1610 | pdie("chroot"); |
| 1611 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1612 | if (j->flags.pivot_root && enter_pivot_root(j)) |
| 1613 | pdie("pivot_root"); |
| 1614 | |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 1615 | if (j->flags.mount_tmp && mount_tmp()) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1616 | pdie("mount_tmp"); |
| 1617 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 1618 | if (j->flags.remount_proc_ro && remount_proc_readonly(j)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1619 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1620 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1621 | /* |
| 1622 | * If we're only dropping capabilities from the bounding set, but not |
| 1623 | * from the thread's (permitted|inheritable|effective) sets, do it now. |
| 1624 | */ |
| 1625 | if (j->flags.capbset_drop) { |
| 1626 | drop_capbset(j->cap_bset, last_valid_cap); |
| 1627 | } |
| 1628 | |
| 1629 | if (j->flags.use_caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1630 | /* |
| 1631 | * POSIX capabilities are a bit tricky. If we drop our |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1632 | * capability to change uids, our attempt to use setuid() |
| 1633 | * below will fail. Hang on to root caps across setuid(), then |
| 1634 | * lock securebits. |
| 1635 | */ |
| 1636 | if (prctl(PR_SET_KEEPCAPS, 1)) |
| 1637 | pdie("prctl(PR_SET_KEEPCAPS)"); |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 1638 | |
| 1639 | /* |
| 1640 | * Kernels 4.3+ define a new securebit |
| 1641 | * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS |
| 1642 | * and SECURE_ALL_LOCKS masks from newer kernel headers will |
| 1643 | * return EPERM on older kernels. Detect this, and retry with |
| 1644 | * the right mask for older (2.6.26-4.2) kernels. |
| 1645 | */ |
| 1646 | int securebits_ret = prctl(PR_SET_SECUREBITS, |
| 1647 | SECURE_ALL_BITS | SECURE_ALL_LOCKS); |
| 1648 | if (securebits_ret < 0) { |
| 1649 | if (errno == EPERM) { |
| 1650 | /* Possibly running on kernel < 4.3. */ |
| 1651 | securebits_ret = prctl( |
| 1652 | PR_SET_SECUREBITS, |
| 1653 | OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS); |
| 1654 | } |
| 1655 | } |
| 1656 | if (securebits_ret < 0) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1657 | pdie("prctl(PR_SET_SECUREBITS)"); |
| 1658 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1659 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1660 | if (j->flags.no_new_privs) { |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 1661 | /* |
| 1662 | * If we're setting no_new_privs, we can drop privileges |
| 1663 | * before setting seccomp filter. This way filter policies |
| 1664 | * don't need to allow privilege-dropping syscalls. |
| 1665 | */ |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1666 | drop_ugid(j); |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 1667 | drop_caps(j, last_valid_cap); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1668 | set_seccomp_filter(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1669 | } else { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1670 | /* |
| 1671 | * If we're not setting no_new_privs, |
| 1672 | * we need to set seccomp filter *before* dropping privileges. |
| 1673 | * WARNING: this means that filter policies *must* allow |
| 1674 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 1675 | * capget()/capset()/prctl() for dropping caps. |
| 1676 | */ |
| 1677 | set_seccomp_filter(j); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1678 | drop_ugid(j); |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 1679 | drop_caps(j, last_valid_cap); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1680 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1681 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1682 | /* |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1683 | * Select the specified alternate syscall table. The table must not |
| 1684 | * block prctl(2) if we're using seccomp as well. |
| 1685 | */ |
| 1686 | if (j->flags.alt_syscall) { |
| 1687 | if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table)) |
| 1688 | pdie("prctl(PR_ALT_SYSCALL)"); |
| 1689 | } |
| 1690 | |
| 1691 | /* |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1692 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1693 | * privilege-dropping syscalls :) |
| 1694 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1695 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) { |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 1696 | if ((errno == EINVAL) && seccomp_can_softfail()) { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1697 | warn("seccomp not supported"); |
| 1698 | return; |
| 1699 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1700 | pdie("prctl(PR_SET_SECCOMP)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1701 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1702 | } |
| 1703 | |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 1704 | /* TODO(wad): will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1705 | static int init_exitstatus = 0; |
| 1706 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1707 | void init_term(int __attribute__ ((unused)) sig) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1708 | { |
| 1709 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1710 | } |
| 1711 | |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 1712 | void init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1713 | { |
| 1714 | pid_t pid; |
| 1715 | int status; |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 1716 | /* So that we exit with the right status. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1717 | signal(SIGTERM, init_term); |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 1718 | /* TODO(wad): self jail with seccomp filters here. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1719 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1720 | /* |
| 1721 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1722 | * left inside our pid namespace or we get a signal. |
| 1723 | */ |
| 1724 | if (pid == rootpid) |
| 1725 | init_exitstatus = status; |
| 1726 | } |
| 1727 | if (!WIFEXITED(init_exitstatus)) |
| 1728 | _exit(MINIJAIL_ERR_INIT); |
| 1729 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1730 | } |
| 1731 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1732 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1733 | { |
| 1734 | size_t sz = 0; |
| 1735 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 1736 | char *buf; |
| 1737 | int r; |
| 1738 | if (sizeof(sz) != bytes) |
| 1739 | return -EINVAL; |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1740 | if (sz > USHRT_MAX) /* arbitrary sanity check */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1741 | return -E2BIG; |
| 1742 | buf = malloc(sz); |
| 1743 | if (!buf) |
| 1744 | return -ENOMEM; |
| 1745 | bytes = read(fd, buf, sz); |
| 1746 | if (bytes != sz) { |
| 1747 | free(buf); |
| 1748 | return -EINVAL; |
| 1749 | } |
| 1750 | r = minijail_unmarshal(j, buf, sz); |
| 1751 | free(buf); |
| 1752 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1753 | } |
| 1754 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1755 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1756 | { |
| 1757 | char *buf; |
| 1758 | size_t sz = minijail_size(j); |
| 1759 | ssize_t written; |
| 1760 | int r; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1761 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1762 | if (!sz) |
| 1763 | return -EINVAL; |
| 1764 | buf = malloc(sz); |
| 1765 | r = minijail_marshal(j, buf, sz); |
| 1766 | if (r) { |
| 1767 | free(buf); |
| 1768 | return r; |
| 1769 | } |
| 1770 | /* Sends [size][minijail]. */ |
| 1771 | written = write(fd, &sz, sizeof(sz)); |
| 1772 | if (written != sizeof(sz)) { |
| 1773 | free(buf); |
| 1774 | return -EFAULT; |
| 1775 | } |
| 1776 | written = write(fd, buf, sz); |
| 1777 | if (written < 0 || (size_t) written != sz) { |
| 1778 | free(buf); |
| 1779 | return -EFAULT; |
| 1780 | } |
| 1781 | free(buf); |
| 1782 | return 0; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1783 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1784 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1785 | int setup_preload(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1786 | { |
Daniel Erat | 5b7a318 | 2015-08-19 16:06:22 -0600 | [diff] [blame] | 1787 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 1788 | /* Don't use LDPRELOAD on Brillo. */ |
| 1789 | return 0; |
| 1790 | #else |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1791 | char *oldenv = getenv(kLdPreloadEnvVar) ? : ""; |
| 1792 | char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH)); |
| 1793 | if (!newenv) |
| 1794 | return -ENOMEM; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1795 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1796 | /* Only insert a separating space if we have something to separate... */ |
| 1797 | sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "", |
| 1798 | PRELOADPATH); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1799 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1800 | /* setenv() makes a copy of the string we give it. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1801 | setenv(kLdPreloadEnvVar, newenv, 1); |
| 1802 | free(newenv); |
| 1803 | return 0; |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 1804 | #endif |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1805 | } |
| 1806 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1807 | int setup_pipe(int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1808 | { |
| 1809 | int r = pipe(fds); |
| 1810 | char fd_buf[11]; |
| 1811 | if (r) |
| 1812 | return r; |
| 1813 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 1814 | if (r <= 0) |
| 1815 | return -EINVAL; |
| 1816 | setenv(kFdEnvVar, fd_buf, 1); |
| 1817 | return 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1818 | } |
| 1819 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1820 | int setup_pipe_end(int fds[2], size_t index) |
| 1821 | { |
| 1822 | if (index > 1) |
| 1823 | return -1; |
| 1824 | |
| 1825 | close(fds[1 - index]); |
| 1826 | return fds[index]; |
| 1827 | } |
| 1828 | |
| 1829 | int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd) |
| 1830 | { |
| 1831 | if (index > 1) |
| 1832 | return -1; |
| 1833 | |
| 1834 | close(fds[1 - index]); |
| 1835 | /* dup2(2) the corresponding end of the pipe into |fd|. */ |
| 1836 | return dup2(fds[index], fd); |
| 1837 | } |
| 1838 | |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 1839 | int close_open_fds(int *inheritable_fds, size_t size) |
| 1840 | { |
| 1841 | const char *kFdPath = "/proc/self/fd"; |
| 1842 | |
| 1843 | DIR *d = opendir(kFdPath); |
| 1844 | struct dirent *dir_entry; |
| 1845 | |
| 1846 | if (d == NULL) |
| 1847 | return -1; |
| 1848 | int dir_fd = dirfd(d); |
| 1849 | while ((dir_entry = readdir(d)) != NULL) { |
| 1850 | size_t i; |
| 1851 | char *end; |
| 1852 | bool should_close = true; |
| 1853 | const int fd = strtol(dir_entry->d_name, &end, 10); |
| 1854 | |
| 1855 | if ((*end) != '\0') { |
| 1856 | continue; |
| 1857 | } |
| 1858 | /* |
| 1859 | * We might have set up some pipes that we want to share with |
| 1860 | * the parent process, and should not be closed. |
| 1861 | */ |
| 1862 | for (i = 0; i < size; ++i) { |
| 1863 | if (fd == inheritable_fds[i]) { |
| 1864 | should_close = false; |
| 1865 | break; |
| 1866 | } |
| 1867 | } |
| 1868 | /* Also avoid closing the directory fd. */ |
| 1869 | if (should_close && fd != dir_fd) |
| 1870 | close(fd); |
| 1871 | } |
| 1872 | closedir(d); |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1876 | int minijail_run_internal(struct minijail *j, const char *filename, |
| 1877 | char *const argv[], pid_t *pchild_pid, |
| 1878 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd, |
| 1879 | int use_preload); |
| 1880 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1881 | int API minijail_run(struct minijail *j, const char *filename, |
| 1882 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1883 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1884 | return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL, |
| 1885 | true); |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 1889 | char *const argv[], pid_t *pchild_pid) |
| 1890 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1891 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1892 | NULL, NULL, NULL, true); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | int API minijail_run_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 1896 | char *const argv[], int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1897 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1898 | return minijail_run_internal(j, filename, argv, NULL, pstdin_fd, |
| 1899 | NULL, NULL, true); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1902 | 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] | 1903 | char *const argv[], pid_t *pchild_pid, |
| 1904 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1905 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1906 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1907 | pstdin_fd, pstdout_fd, pstderr_fd, true); |
| 1908 | } |
| 1909 | |
| 1910 | int API minijail_run_no_preload(struct minijail *j, const char *filename, |
| 1911 | char *const argv[]) |
| 1912 | { |
| 1913 | return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL, |
| 1914 | false); |
| 1915 | } |
| 1916 | |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 1917 | int API minijail_run_pid_pipes_no_preload(struct minijail *j, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1918 | const char *filename, |
| 1919 | char *const argv[], |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 1920 | pid_t *pchild_pid, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1921 | int *pstdin_fd, int *pstdout_fd, |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1922 | int *pstderr_fd) |
| 1923 | { |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 1924 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1925 | pstdin_fd, pstdout_fd, pstderr_fd, false); |
| 1926 | } |
| 1927 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1928 | int minijail_run_internal(struct minijail *j, const char *filename, |
| 1929 | char *const argv[], pid_t *pchild_pid, |
| 1930 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd, |
| 1931 | int use_preload) |
| 1932 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1933 | char *oldenv, *oldenv_copy = NULL; |
| 1934 | pid_t child_pid; |
| 1935 | int pipe_fds[2]; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1936 | int stdin_fds[2]; |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1937 | int stdout_fds[2]; |
| 1938 | int stderr_fds[2]; |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 1939 | int child_sync_pipe_fds[2]; |
| 1940 | int sync_child = 0; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1941 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1942 | /* We need to remember this across the minijail_preexec() call. */ |
| 1943 | int pid_namespace = j->flags.pids; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1944 | int do_init = j->flags.do_init; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1945 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1946 | if (use_preload) { |
| 1947 | oldenv = getenv(kLdPreloadEnvVar); |
| 1948 | if (oldenv) { |
| 1949 | oldenv_copy = strdup(oldenv); |
| 1950 | if (!oldenv_copy) |
| 1951 | return -ENOMEM; |
| 1952 | } |
| 1953 | |
| 1954 | if (setup_preload()) |
| 1955 | return -EFAULT; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1956 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1957 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1958 | if (!use_preload) { |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 1959 | if (j->flags.use_caps && j->caps != 0) |
| 1960 | die("non-empty capabilities are not supported without LD_PRELOAD"); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1961 | } |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1962 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1963 | /* |
Jorge Lucangeli Obes | aa235b9 | 2016-11-23 13:48:15 -0500 | [diff] [blame] | 1964 | * Make the process group ID of this process equal to its PID. |
| 1965 | * In the non-interactive case (e.g. when the parent process is started |
| 1966 | * from init) this ensures the parent process and the jailed process |
| 1967 | * can be killed together. |
| 1968 | * When the parent process is started from the console this ensures |
| 1969 | * the call to setsid(2) in the jailed process succeeds. |
| 1970 | * |
Jorge Lucangeli Obes | 3c84df1 | 2015-05-14 17:37:58 -0700 | [diff] [blame] | 1971 | * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when |
| 1972 | * the process is already a process group leader. |
| 1973 | */ |
| 1974 | if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) { |
| 1975 | if (errno != EPERM) { |
Jorge Lucangeli Obes | aa235b9 | 2016-11-23 13:48:15 -0500 | [diff] [blame] | 1976 | pdie("setpgid(0, 0) failed"); |
Jorge Lucangeli Obes | 3c84df1 | 2015-05-14 17:37:58 -0700 | [diff] [blame] | 1977 | } |
| 1978 | } |
| 1979 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1980 | if (use_preload) { |
| 1981 | /* |
| 1982 | * Before we fork(2) and execve(2) the child process, we need |
| 1983 | * to open a pipe(2) to send the minijail configuration over. |
| 1984 | */ |
| 1985 | if (setup_pipe(pipe_fds)) |
| 1986 | return -EFAULT; |
| 1987 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1988 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1989 | /* |
| 1990 | * If we want to write to the child process' standard input, |
| 1991 | * create the pipe(2) now. |
| 1992 | */ |
| 1993 | if (pstdin_fd) { |
| 1994 | if (pipe(stdin_fds)) |
| 1995 | return -EFAULT; |
| 1996 | } |
| 1997 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1998 | /* |
| 1999 | * If we want to read from the child process' standard output, |
| 2000 | * create the pipe(2) now. |
| 2001 | */ |
| 2002 | if (pstdout_fd) { |
| 2003 | if (pipe(stdout_fds)) |
| 2004 | return -EFAULT; |
| 2005 | } |
| 2006 | |
| 2007 | /* |
| 2008 | * If we want to read from the child process' standard error, |
| 2009 | * create the pipe(2) now. |
| 2010 | */ |
| 2011 | if (pstderr_fd) { |
| 2012 | if (pipe(stderr_fds)) |
| 2013 | return -EFAULT; |
| 2014 | } |
| 2015 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2016 | /* |
Jorge Lucangeli Obes | ab6fa6f | 2016-08-04 15:42:48 -0400 | [diff] [blame] | 2017 | * If we want to set up a new uid/gid map in the user namespace, |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 2018 | * or if we need to add the child process to cgroups, create the pipe(2) |
| 2019 | * to sync between parent and child. |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2020 | */ |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 2021 | if (j->flags.userns || j->flags.cgroups) { |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2022 | sync_child = 1; |
| 2023 | if (pipe(child_sync_pipe_fds)) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2024 | return -EFAULT; |
| 2025 | } |
| 2026 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 2027 | /* |
| 2028 | * 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] | 2029 | * |
| 2030 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 2031 | * |
| 2032 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 2033 | * some of which may be held by other threads at the time that we call |
| 2034 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 2035 | * ensure that we hold all of these locks before it calls clone() |
| 2036 | * internally and drop them after clone() returns, but when we call |
| 2037 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 2038 | * child address space where some of libc's important locks are held by |
| 2039 | * other threads (which did not get cloned, and hence will never release |
| 2040 | * those locks). This is okay so long as we call exec() immediately |
| 2041 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 2042 | * take locks. |
| 2043 | * |
| 2044 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 2045 | * namespacing. If we follow this path, the child's address space might |
| 2046 | * have broken locks; you may only call functions that do not acquire |
| 2047 | * any locks. |
| 2048 | * |
| 2049 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 2050 | * previously detailed, so this function is highly likely to deadlock |
| 2051 | * later on (see "deadlock here") if we're multithreaded. |
| 2052 | * |
| 2053 | * We might hack around this by having the clone()d child (init of the |
| 2054 | * pid namespace) return directly, rather than leaving the clone()d |
| 2055 | * 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] | 2056 | * its fork()ed child return in turn), but that process would be |
| 2057 | * crippled with its libc locks potentially broken. We might try |
| 2058 | * fork()ing in the parent before we clone() to ensure that we own all |
| 2059 | * the locks, but then we have to have the forked child hanging around |
| 2060 | * consuming resources (and possibly having file descriptors / shared |
| 2061 | * memory regions / etc attached). We'd need to keep the child around to |
| 2062 | * avoid having its children get reparented to init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2063 | * |
| 2064 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 2065 | * problem is fixable or not. It would be nice if we worked in this |
| 2066 | * case. |
| 2067 | */ |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2068 | if (pid_namespace) { |
| 2069 | int clone_flags = CLONE_NEWPID | SIGCHLD; |
| 2070 | if (j->flags.userns) |
| 2071 | clone_flags |= CLONE_NEWUSER; |
| 2072 | child_pid = syscall(SYS_clone, clone_flags, NULL); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2073 | } else { |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2074 | child_pid = fork(); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2075 | } |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2076 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2077 | if (child_pid < 0) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2078 | if (use_preload) { |
| 2079 | free(oldenv_copy); |
| 2080 | } |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 2081 | die("failed to fork child"); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2082 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 2083 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2084 | if (child_pid) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2085 | if (use_preload) { |
| 2086 | /* Restore parent's LD_PRELOAD. */ |
| 2087 | if (oldenv_copy) { |
| 2088 | setenv(kLdPreloadEnvVar, oldenv_copy, 1); |
| 2089 | free(oldenv_copy); |
| 2090 | } else { |
| 2091 | unsetenv(kLdPreloadEnvVar); |
| 2092 | } |
| 2093 | unsetenv(kFdEnvVar); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2094 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2095 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2096 | j->initpid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2097 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 2098 | if (j->flags.pid_file) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2099 | write_pid_file_or_die(j); |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 2100 | |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 2101 | if (j->flags.cgroups) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2102 | add_to_cgroups_or_die(j); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 2103 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2104 | if (j->flags.userns) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2105 | write_ugid_maps_or_die(j); |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2106 | |
| 2107 | if (sync_child) |
| 2108 | parent_setup_complete(child_sync_pipe_fds); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2109 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2110 | if (use_preload) { |
| 2111 | /* Send marshalled minijail. */ |
| 2112 | close(pipe_fds[0]); /* read endpoint */ |
| 2113 | ret = minijail_to_fd(j, pipe_fds[1]); |
| 2114 | close(pipe_fds[1]); /* write endpoint */ |
| 2115 | if (ret) { |
| 2116 | kill(j->initpid, SIGKILL); |
| 2117 | die("failed to send marshalled minijail"); |
| 2118 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2119 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2120 | |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 2121 | if (pchild_pid) |
| 2122 | *pchild_pid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2123 | |
| 2124 | /* |
| 2125 | * If we want to write to the child process' standard input, |
| 2126 | * set up the write end of the pipe. |
| 2127 | */ |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2128 | if (pstdin_fd) |
| 2129 | *pstdin_fd = setup_pipe_end(stdin_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2130 | 1 /* write end */); |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2131 | |
| 2132 | /* |
| 2133 | * If we want to read from the child process' standard output, |
| 2134 | * set up the read end of the pipe. |
| 2135 | */ |
| 2136 | if (pstdout_fd) |
| 2137 | *pstdout_fd = setup_pipe_end(stdout_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2138 | 0 /* read end */); |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2139 | |
| 2140 | /* |
| 2141 | * If we want to read from the child process' standard error, |
| 2142 | * set up the read end of the pipe. |
| 2143 | */ |
| 2144 | if (pstderr_fd) |
| 2145 | *pstderr_fd = setup_pipe_end(stderr_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2146 | 0 /* read end */); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2147 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2148 | return 0; |
| 2149 | } |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2150 | /* Child process. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2151 | free(oldenv_copy); |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 2152 | |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 2153 | if (j->flags.reset_signal_mask) { |
| 2154 | sigset_t signal_mask; |
| 2155 | if (sigemptyset(&signal_mask) != 0) |
| 2156 | pdie("sigemptyset failed"); |
| 2157 | if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0) |
| 2158 | pdie("sigprocmask failed"); |
| 2159 | } |
| 2160 | |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2161 | if (j->flags.close_open_fds) { |
| 2162 | const size_t kMaxInheritableFdsSize = 10; |
| 2163 | int inheritable_fds[kMaxInheritableFdsSize]; |
| 2164 | size_t size = 0; |
| 2165 | if (use_preload) { |
| 2166 | inheritable_fds[size++] = pipe_fds[0]; |
| 2167 | inheritable_fds[size++] = pipe_fds[1]; |
| 2168 | } |
| 2169 | if (sync_child) { |
| 2170 | inheritable_fds[size++] = child_sync_pipe_fds[0]; |
| 2171 | inheritable_fds[size++] = child_sync_pipe_fds[1]; |
| 2172 | } |
| 2173 | if (pstdin_fd) { |
| 2174 | inheritable_fds[size++] = stdin_fds[0]; |
| 2175 | inheritable_fds[size++] = stdin_fds[1]; |
| 2176 | } |
| 2177 | if (pstdout_fd) { |
| 2178 | inheritable_fds[size++] = stdout_fds[0]; |
| 2179 | inheritable_fds[size++] = stdout_fds[1]; |
| 2180 | } |
| 2181 | if (pstderr_fd) { |
| 2182 | inheritable_fds[size++] = stderr_fds[0]; |
| 2183 | inheritable_fds[size++] = stderr_fds[1]; |
| 2184 | } |
| 2185 | |
| 2186 | if (close_open_fds(inheritable_fds, size) < 0) |
| 2187 | die("failed to close open file descriptors"); |
| 2188 | } |
| 2189 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2190 | if (sync_child) |
| 2191 | wait_for_parent_setup(child_sync_pipe_fds); |
| 2192 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2193 | if (j->flags.userns) |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2194 | enter_user_namespace(j); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2195 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2196 | /* |
| 2197 | * If we want to write to the jailed process' standard input, |
| 2198 | * set up the read end of the pipe. |
| 2199 | */ |
| 2200 | if (pstdin_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2201 | if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */, |
| 2202 | STDIN_FILENO) < 0) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2203 | die("failed to set up stdin pipe"); |
| 2204 | } |
| 2205 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2206 | /* |
| 2207 | * If we want to read from the jailed process' standard output, |
| 2208 | * set up the write end of the pipe. |
| 2209 | */ |
| 2210 | if (pstdout_fd) { |
| 2211 | if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */, |
| 2212 | STDOUT_FILENO) < 0) |
| 2213 | die("failed to set up stdout pipe"); |
| 2214 | } |
| 2215 | |
| 2216 | /* |
| 2217 | * If we want to read from the jailed process' standard error, |
| 2218 | * set up the write end of the pipe. |
| 2219 | */ |
| 2220 | if (pstderr_fd) { |
| 2221 | if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */, |
| 2222 | STDERR_FILENO) < 0) |
| 2223 | die("failed to set up stderr pipe"); |
| 2224 | } |
| 2225 | |
Jorge Lucangeli Obes | aa235b9 | 2016-11-23 13:48:15 -0500 | [diff] [blame] | 2226 | /* |
| 2227 | * If any of stdin, stdout, or stderr are TTYs, create a new session. |
| 2228 | * This prevents the jailed process from using the TIOCSTI ioctl |
| 2229 | * to push characters into the parent process terminal's input buffer, |
| 2230 | * therefore escaping the jail. |
| 2231 | */ |
| 2232 | if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || |
| 2233 | isatty(STDERR_FILENO)) { |
| 2234 | if (setsid() < 0) { |
| 2235 | pdie("setsid() failed"); |
| 2236 | } |
| 2237 | } |
| 2238 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 2239 | /* If running an init program, let it decide when/how to mount /proc. */ |
| 2240 | if (pid_namespace && !do_init) |
| 2241 | j->flags.remount_proc_ro = 0; |
| 2242 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2243 | if (use_preload) { |
| 2244 | /* Strip out flags that cannot be inherited across execve(2). */ |
| 2245 | minijail_preexec(j); |
| 2246 | } else { |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2247 | /* |
| 2248 | * If not using LD_PRELOAD, do all jailing before execve(2). |
| 2249 | * Note that PID namespaces can only be entered on fork(2), |
| 2250 | * so that flag is still cleared. |
| 2251 | */ |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2252 | j->flags.pids = 0; |
| 2253 | } |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2254 | /* Jail this process, then execve(2) the target. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2255 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2256 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 2257 | if (pid_namespace && do_init) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2258 | /* |
| 2259 | * pid namespace: this process will become init inside the new |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 2260 | * 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] | 2261 | * 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] | 2262 | * a child to actually run the program. If |do_init == 0|, we |
| 2263 | * let the program keep pid 1 and be init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2264 | * |
| 2265 | * If we're multithreaded, we'll probably deadlock here. See |
| 2266 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2267 | */ |
| 2268 | child_pid = fork(); |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2269 | if (child_pid < 0) { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2270 | _exit(child_pid); |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2271 | } else if (child_pid > 0) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 2272 | /* |
| 2273 | * Best effort. Don't bother checking the return value. |
| 2274 | */ |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2275 | prctl(PR_SET_NAME, "minijail-init"); |
| 2276 | init(child_pid); /* Never returns. */ |
| 2277 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2278 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2279 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2280 | /* |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2281 | * 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] | 2282 | * calling process |
| 2283 | * -> execve()-ing process |
| 2284 | * If we are: |
| 2285 | * calling process |
| 2286 | * -> init()-ing process |
| 2287 | * -> execve()-ing process |
| 2288 | */ |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2289 | ret = execve(filename, argv, environ); |
| 2290 | if (ret == -1) { |
| 2291 | pwarn("execve(%s) failed", filename); |
| 2292 | } |
| 2293 | _exit(ret); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2294 | } |
| 2295 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2296 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2297 | { |
| 2298 | int st; |
| 2299 | if (kill(j->initpid, SIGTERM)) |
| 2300 | return -errno; |
| 2301 | if (waitpid(j->initpid, &st, 0) < 0) |
| 2302 | return -errno; |
| 2303 | return st; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2304 | } |
| 2305 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2306 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2307 | { |
| 2308 | int st; |
| 2309 | if (waitpid(j->initpid, &st, 0) < 0) |
| 2310 | return -errno; |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 2311 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 2312 | if (!WIFEXITED(st)) { |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 2313 | int error_status = st; |
| 2314 | if (WIFSIGNALED(st)) { |
| 2315 | int signum = WTERMSIG(st); |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 2316 | warn("child process %d received signal %d", |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 2317 | j->initpid, signum); |
| 2318 | /* |
| 2319 | * We return MINIJAIL_ERR_JAIL if the process received |
| 2320 | * SIGSYS, which happens when a syscall is blocked by |
| 2321 | * seccomp filters. |
| 2322 | * If not, we do what bash(1) does: |
| 2323 | * $? = 128 + signum |
| 2324 | */ |
| 2325 | if (signum == SIGSYS) { |
| 2326 | error_status = MINIJAIL_ERR_JAIL; |
| 2327 | } else { |
| 2328 | error_status = 128 + signum; |
| 2329 | } |
| 2330 | } |
| 2331 | return error_status; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 2332 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 2333 | |
| 2334 | int exit_status = WEXITSTATUS(st); |
| 2335 | if (exit_status != 0) |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 2336 | info("child process %d exited with status %d", |
| 2337 | j->initpid, exit_status); |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 2338 | |
| 2339 | return exit_status; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2340 | } |
| 2341 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2342 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2343 | { |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 2344 | size_t i; |
| 2345 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 2346 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 2347 | free(j->filter_prog->filter); |
| 2348 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2349 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 2350 | while (j->mounts_head) { |
| 2351 | struct mountpoint *m = j->mounts_head; |
| 2352 | j->mounts_head = j->mounts_head->next; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 2353 | free(m->data); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 2354 | free(m->type); |
| 2355 | free(m->dest); |
| 2356 | free(m->src); |
| 2357 | free(m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 2358 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 2359 | j->mounts_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2360 | if (j->user) |
| 2361 | free(j->user); |
Jorge Lucangeli Obes | e81a52f | 2015-12-04 16:05:23 -0800 | [diff] [blame] | 2362 | if (j->suppl_gid_list) |
| 2363 | free(j->suppl_gid_list); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 2364 | if (j->chrootdir) |
| 2365 | free(j->chrootdir); |
Jorge Lucangeli Obes | 3b2e6e4 | 2016-08-04 12:26:19 -0400 | [diff] [blame] | 2366 | if (j->pid_file_path) |
| 2367 | free(j->pid_file_path); |
| 2368 | if (j->uidmap) |
| 2369 | free(j->uidmap); |
| 2370 | if (j->gidmap) |
| 2371 | free(j->gidmap); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 2372 | if (j->alt_syscall_table) |
| 2373 | free(j->alt_syscall_table); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 2374 | for (i = 0; i < j->cgroup_count; ++i) |
| 2375 | free(j->cgroups[i]); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2376 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2377 | } |