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