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