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