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