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