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 | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 21 | #include <stddef.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <syscall.h> |
| 26 | #include <sys/capability.h> |
| 27 | #include <sys/mount.h> |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 28 | #include <sys/param.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 29 | #include <sys/prctl.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 32 | #include <sys/user.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 33 | #include <sys/wait.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
| 36 | #include "libminijail.h" |
| 37 | #include "libminijail-private.h" |
| 38 | |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 39 | #include "signal_handler.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 40 | #include "syscall_filter.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 41 | #include "util.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 42 | |
Lei Zhang | eee3155 | 2012-10-17 21:27:10 -0700 | [diff] [blame] | 43 | #ifdef HAVE_SECUREBITS_H |
| 44 | #include <linux/securebits.h> |
| 45 | #else |
| 46 | #define SECURE_ALL_BITS 0x15 |
| 47 | #define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) |
| 48 | #endif |
| 49 | |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 50 | /* Until these are reliably available in linux/prctl.h */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 51 | #ifndef PR_SET_SECCOMP |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 52 | # define PR_SET_SECCOMP 22 |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 53 | #endif |
| 54 | |
| 55 | /* For seccomp_filter using BPF. */ |
| 56 | #ifndef PR_SET_NO_NEW_PRIVS |
| 57 | # define PR_SET_NO_NEW_PRIVS 38 |
| 58 | #endif |
| 59 | #ifndef SECCOMP_MODE_FILTER |
| 60 | # define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */ |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 61 | #endif |
| 62 | |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 63 | #ifdef USE_SECCOMP_SOFTFAIL |
| 64 | # define SECCOMP_SOFTFAIL 1 |
| 65 | #else |
| 66 | # define SECCOMP_SOFTFAIL 0 |
| 67 | #endif |
| 68 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 69 | struct binding { |
| 70 | char *src; |
| 71 | char *dest; |
| 72 | int writeable; |
| 73 | struct binding *next; |
| 74 | }; |
| 75 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 76 | struct minijail { |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 77 | /* |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 78 | * WARNING: if you add a flag here you need to make sure it's |
| 79 | * accounted for in minijail_pre{enter|exec}() below. |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 80 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 81 | struct { |
| 82 | int uid:1; |
| 83 | int gid:1; |
| 84 | int caps:1; |
| 85 | int vfs:1; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 86 | int enter_vfs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 87 | int pids:1; |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 88 | int net:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 89 | int seccomp:1; |
| 90 | int readonly:1; |
| 91 | int usergroups:1; |
| 92 | int ptrace:1; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 93 | int no_new_privs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 94 | int seccomp_filter:1; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 95 | int log_seccomp_filter:1; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 96 | int chroot:1; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 97 | int mount_tmp:1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 98 | int do_init:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 99 | } flags; |
| 100 | uid_t uid; |
| 101 | gid_t gid; |
| 102 | gid_t usergid; |
| 103 | char *user; |
| 104 | uint64_t caps; |
| 105 | pid_t initpid; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 106 | int mountns_fd; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 107 | int filter_len; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 108 | int binding_count; |
| 109 | char *chrootdir; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 110 | struct sock_fprog *filter_prog; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 111 | struct binding *bindings_head; |
| 112 | struct binding *bindings_tail; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 113 | }; |
| 114 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 115 | /* |
| 116 | * Strip out flags meant for the parent. |
| 117 | * We keep things that are not inherited across execve(2) (e.g. capabilities), |
| 118 | * or are easier to set after execve(2) (e.g. seccomp filters). |
| 119 | */ |
| 120 | void minijail_preenter(struct minijail *j) |
| 121 | { |
| 122 | j->flags.vfs = 0; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 123 | j->flags.enter_vfs = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 124 | j->flags.readonly = 0; |
| 125 | j->flags.pids = 0; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 126 | j->flags.do_init = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Strip out flags meant for the child. |
| 131 | * We keep things that are inherited across execve(2). |
| 132 | */ |
| 133 | void minijail_preexec(struct minijail *j) |
| 134 | { |
| 135 | int vfs = j->flags.vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 136 | int enter_vfs = j->flags.enter_vfs; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 137 | int readonly = j->flags.readonly; |
| 138 | if (j->user) |
| 139 | free(j->user); |
| 140 | j->user = NULL; |
| 141 | memset(&j->flags, 0, sizeof(j->flags)); |
| 142 | /* Now restore anything we meant to keep. */ |
| 143 | j->flags.vfs = vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 144 | j->flags.enter_vfs = enter_vfs; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 145 | j->flags.readonly = readonly; |
| 146 | /* Note, |pids| will already have been used before this call. */ |
| 147 | } |
| 148 | |
| 149 | /* Minijail API. */ |
| 150 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 151 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 152 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 153 | return calloc(1, sizeof(struct minijail)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 156 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 157 | { |
| 158 | if (uid == 0) |
| 159 | die("useless change to uid 0"); |
| 160 | j->uid = uid; |
| 161 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 164 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 165 | { |
| 166 | if (gid == 0) |
| 167 | die("useless change to gid 0"); |
| 168 | j->gid = gid; |
| 169 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 172 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 173 | { |
| 174 | char *buf = NULL; |
| 175 | struct passwd pw; |
| 176 | struct passwd *ppw = NULL; |
| 177 | ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 178 | if (sz == -1) |
| 179 | sz = 65536; /* your guess is as good as mine... */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 180 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 181 | /* |
| 182 | * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 183 | * the maximum needed size of the buffer, so we don't have to search. |
| 184 | */ |
| 185 | buf = malloc(sz); |
| 186 | if (!buf) |
| 187 | return -ENOMEM; |
| 188 | getpwnam_r(user, &pw, buf, sz, &ppw); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 189 | /* |
| 190 | * We're safe to free the buffer here. The strings inside pw point |
| 191 | * inside buf, but we don't use any of them; this leaves the pointers |
| 192 | * dangling but it's safe. ppw points at pw if getpwnam_r succeeded. |
| 193 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 194 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 195 | /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 196 | if (!ppw) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 197 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 198 | minijail_change_uid(j, ppw->pw_uid); |
| 199 | j->user = strdup(user); |
| 200 | if (!j->user) |
| 201 | return -ENOMEM; |
| 202 | j->usergid = ppw->pw_gid; |
| 203 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 206 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 207 | { |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 208 | char *buf = NULL; |
Yabin Cui | 1b21c8f | 2015-07-22 10:34:45 -0700 | [diff] [blame] | 209 | struct group gr; |
| 210 | struct group *pgr = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 211 | ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 212 | if (sz == -1) |
| 213 | sz = 65536; /* and mine is as good as yours, really */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 214 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 215 | /* |
| 216 | * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 217 | * the maximum needed size of the buffer, so we don't have to search. |
| 218 | */ |
| 219 | buf = malloc(sz); |
| 220 | if (!buf) |
| 221 | return -ENOMEM; |
| 222 | getgrnam_r(group, &gr, buf, sz, &pgr); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 223 | /* |
| 224 | * We're safe to free the buffer here. The strings inside gr point |
| 225 | * inside buf, but we don't use any of them; this leaves the pointers |
| 226 | * dangling but it's safe. pgr points at gr if getgrnam_r succeeded. |
| 227 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 228 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 229 | /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 230 | if (!pgr) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 231 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 232 | minijail_change_gid(j, pgr->gr_gid); |
| 233 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 236 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 237 | { |
| 238 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 241 | void API minijail_no_new_privs(struct minijail *j) |
| 242 | { |
| 243 | j->flags.no_new_privs = 1; |
| 244 | } |
| 245 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 246 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 247 | { |
| 248 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 251 | void API minijail_log_seccomp_filter_failures(struct minijail *j) |
| 252 | { |
| 253 | j->flags.log_seccomp_filter = 1; |
| 254 | } |
| 255 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 256 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 257 | { |
| 258 | j->caps = capmask; |
| 259 | j->flags.caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 260 | } |
| 261 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 262 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 263 | { |
| 264 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 265 | } |
| 266 | |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 267 | void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path) |
| 268 | { |
| 269 | int ns_fd = open(ns_path, O_RDONLY); |
| 270 | if (ns_fd < 0) { |
| 271 | pdie("failed to open namespace '%s'", ns_path); |
| 272 | } |
| 273 | j->mountns_fd = ns_fd; |
| 274 | j->flags.enter_vfs = 1; |
| 275 | } |
| 276 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 277 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 278 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 279 | j->flags.vfs = 1; |
| 280 | j->flags.readonly = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 281 | j->flags.pids = 1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 282 | j->flags.do_init = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 285 | void API minijail_namespace_net(struct minijail *j) |
| 286 | { |
| 287 | j->flags.net = 1; |
| 288 | } |
| 289 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 290 | void API minijail_remount_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 291 | { |
| 292 | j->flags.vfs = 1; |
| 293 | j->flags.readonly = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 294 | } |
| 295 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 296 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 297 | { |
| 298 | j->flags.usergroups = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 301 | void API minijail_disable_ptrace(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 302 | { |
| 303 | j->flags.ptrace = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 304 | } |
| 305 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 306 | void API minijail_run_as_init(struct minijail *j) |
| 307 | { |
| 308 | /* |
| 309 | * Since the jailed program will become 'init' in the new PID namespace, |
| 310 | * Minijail does not need to fork an 'init' process. |
| 311 | */ |
| 312 | j->flags.do_init = 0; |
| 313 | } |
| 314 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 315 | int API minijail_enter_chroot(struct minijail *j, const char *dir) |
| 316 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 317 | if (j->chrootdir) |
| 318 | return -EINVAL; |
| 319 | j->chrootdir = strdup(dir); |
| 320 | if (!j->chrootdir) |
| 321 | return -ENOMEM; |
| 322 | j->flags.chroot = 1; |
| 323 | return 0; |
| 324 | } |
| 325 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 326 | void API minijail_mount_tmp(struct minijail *j) |
| 327 | { |
| 328 | j->flags.mount_tmp = 1; |
| 329 | } |
| 330 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 331 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 332 | int writeable) |
| 333 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 334 | struct binding *b; |
| 335 | |
| 336 | if (*dest != '/') |
| 337 | return -EINVAL; |
| 338 | b = calloc(1, sizeof(*b)); |
| 339 | if (!b) |
| 340 | return -ENOMEM; |
| 341 | b->dest = strdup(dest); |
| 342 | if (!b->dest) |
| 343 | goto error; |
| 344 | b->src = strdup(src); |
| 345 | if (!b->src) |
| 346 | goto error; |
| 347 | b->writeable = writeable; |
| 348 | |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 349 | info("bind %s -> %s", src, dest); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 350 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 351 | /* |
| 352 | * Force vfs namespacing so the bind mounts don't leak out into the |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 353 | * containing vfs namespace. |
| 354 | */ |
| 355 | minijail_namespace_vfs(j); |
| 356 | |
| 357 | if (j->bindings_tail) |
| 358 | j->bindings_tail->next = b; |
| 359 | else |
| 360 | j->bindings_head = b; |
| 361 | j->bindings_tail = b; |
| 362 | j->binding_count++; |
| 363 | |
| 364 | return 0; |
| 365 | |
| 366 | error: |
| 367 | free(b->src); |
| 368 | free(b->dest); |
| 369 | free(b); |
| 370 | return -ENOMEM; |
| 371 | } |
| 372 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 373 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 374 | { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 375 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) { |
| 376 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 377 | warn("not loading seccomp filter, seccomp not supported"); |
| 378 | return; |
| 379 | } |
| 380 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 381 | FILE *file = fopen(path, "r"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 382 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 383 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 384 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 385 | |
| 386 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 387 | if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) { |
| 388 | die("failed to compile seccomp filter BPF program in '%s'", |
| 389 | path); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | j->filter_len = fprog->len; |
| 393 | j->filter_prog = fprog; |
| 394 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 395 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 398 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 399 | size_t available; |
| 400 | size_t total; |
| 401 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 402 | }; |
| 403 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 404 | void marshal_state_init(struct marshal_state *state, |
| 405 | char *buf, size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 406 | { |
| 407 | state->available = available; |
| 408 | state->buf = buf; |
| 409 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 412 | void marshal_append(struct marshal_state *state, |
| 413 | char *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 414 | { |
| 415 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 416 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 417 | /* Up to |available| will be written. */ |
| 418 | if (copy_len) { |
| 419 | memcpy(state->buf, src, copy_len); |
| 420 | state->buf += copy_len; |
| 421 | state->available -= copy_len; |
| 422 | } |
| 423 | /* |total| will contain the expected length. */ |
| 424 | state->total += length; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 425 | } |
| 426 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 427 | void minijail_marshal_helper(struct marshal_state *state, |
| 428 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 429 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 430 | struct binding *b = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 431 | marshal_append(state, (char *)j, sizeof(*j)); |
| 432 | if (j->user) |
| 433 | marshal_append(state, j->user, strlen(j->user) + 1); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 434 | if (j->chrootdir) |
| 435 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 436 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 437 | struct sock_fprog *fp = j->filter_prog; |
| 438 | marshal_append(state, (char *)fp->filter, |
| 439 | fp->len * sizeof(struct sock_filter)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 440 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 441 | for (b = j->bindings_head; b; b = b->next) { |
| 442 | marshal_append(state, b->src, strlen(b->src) + 1); |
| 443 | marshal_append(state, b->dest, strlen(b->dest) + 1); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 444 | marshal_append(state, (char *)&b->writeable, |
| 445 | sizeof(b->writeable)); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 446 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 449 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 450 | { |
| 451 | struct marshal_state state; |
| 452 | marshal_state_init(&state, NULL, 0); |
| 453 | minijail_marshal_helper(&state, j); |
| 454 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 455 | } |
| 456 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 457 | int minijail_marshal(const struct minijail *j, char *buf, size_t available) |
| 458 | { |
| 459 | struct marshal_state state; |
| 460 | marshal_state_init(&state, buf, available); |
| 461 | minijail_marshal_helper(&state, j); |
| 462 | return (state.total > available); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 463 | } |
| 464 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 465 | /* consumebytes: consumes @length bytes from a buffer @buf of length @buflength |
| 466 | * @length Number of bytes to consume |
| 467 | * @buf Buffer to consume from |
| 468 | * @buflength Size of @buf |
| 469 | * |
| 470 | * Returns a pointer to the base of the bytes, or NULL for errors. |
| 471 | */ |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 472 | void *consumebytes(size_t length, char **buf, size_t *buflength) |
| 473 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 474 | char *p = *buf; |
| 475 | if (length > *buflength) |
| 476 | return NULL; |
| 477 | *buf += length; |
| 478 | *buflength -= length; |
| 479 | return p; |
| 480 | } |
| 481 | |
| 482 | /* consumestr: consumes a C string from a buffer @buf of length @length |
| 483 | * @buf Buffer to consume |
| 484 | * @length Length of buffer |
| 485 | * |
| 486 | * Returns a pointer to the base of the string, or NULL for errors. |
| 487 | */ |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 488 | char *consumestr(char **buf, size_t *buflength) |
| 489 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 490 | size_t len = strnlen(*buf, *buflength); |
| 491 | if (len == *buflength) |
| 492 | /* There's no null-terminator */ |
| 493 | return NULL; |
| 494 | return consumebytes(len + 1, buf, buflength); |
| 495 | } |
| 496 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 497 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 498 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 499 | int i; |
| 500 | int count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 501 | int ret = -EINVAL; |
| 502 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 503 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 504 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 505 | memcpy((void *)j, serialized, sizeof(*j)); |
| 506 | serialized += sizeof(*j); |
| 507 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 508 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 509 | /* Potentially stale pointers not used as signals. */ |
| 510 | j->bindings_head = NULL; |
| 511 | j->bindings_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 512 | j->filter_prog = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 513 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 514 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 515 | char *user = consumestr(&serialized, &length); |
| 516 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 517 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 518 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 519 | if (!j->user) |
| 520 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 521 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 522 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 523 | if (j->chrootdir) { /* stale pointer */ |
| 524 | char *chrootdir = consumestr(&serialized, &length); |
| 525 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 526 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 527 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 528 | if (!j->chrootdir) |
| 529 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 530 | } |
| 531 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 532 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 533 | size_t ninstrs = j->filter_len; |
| 534 | if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) || |
| 535 | ninstrs > USHRT_MAX) |
| 536 | goto bad_filters; |
| 537 | |
| 538 | size_t program_len = ninstrs * sizeof(struct sock_filter); |
| 539 | void *program = consumebytes(program_len, &serialized, &length); |
| 540 | if (!program) |
| 541 | goto bad_filters; |
| 542 | |
| 543 | j->filter_prog = malloc(sizeof(struct sock_fprog)); |
| 544 | j->filter_prog->len = ninstrs; |
| 545 | j->filter_prog->filter = malloc(program_len); |
| 546 | memcpy(j->filter_prog->filter, program, program_len); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 547 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 548 | |
| 549 | count = j->binding_count; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 550 | j->binding_count = 0; |
| 551 | for (i = 0; i < count; ++i) { |
| 552 | int *writeable; |
| 553 | const char *dest; |
| 554 | const char *src = consumestr(&serialized, &length); |
| 555 | if (!src) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 556 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 557 | dest = consumestr(&serialized, &length); |
| 558 | if (!dest) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 559 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 560 | writeable = consumebytes(sizeof(*writeable), &serialized, &length); |
| 561 | if (!writeable) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 562 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 563 | if (minijail_bind(j, src, dest, *writeable)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 564 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 565 | } |
| 566 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 567 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 568 | |
| 569 | bad_bindings: |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 570 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 571 | free(j->filter_prog->filter); |
| 572 | free(j->filter_prog); |
| 573 | } |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 574 | bad_filters: |
| 575 | if (j->chrootdir) |
| 576 | free(j->chrootdir); |
| 577 | bad_chrootdir: |
| 578 | if (j->user) |
| 579 | free(j->user); |
| 580 | clear_pointers: |
| 581 | j->user = NULL; |
| 582 | j->chrootdir = NULL; |
| 583 | out: |
| 584 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 585 | } |
| 586 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 587 | /* bind_one: Applies bindings from @b for @j, recursing as needed. |
| 588 | * @j Minijail these bindings are for |
| 589 | * @b Head of list of bindings |
| 590 | * |
| 591 | * Returns 0 for success. |
| 592 | */ |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 593 | int bind_one(const struct minijail *j, struct binding *b) |
| 594 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 595 | int ret = 0; |
| 596 | char *dest = NULL; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 597 | if (ret) |
| 598 | return ret; |
| 599 | /* dest has a leading "/" */ |
| 600 | if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0) |
| 601 | return -ENOMEM; |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 602 | ret = mount(b->src, dest, NULL, MS_BIND, NULL); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 603 | if (ret) |
| 604 | pdie("bind: %s -> %s", b->src, dest); |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 605 | if (!b->writeable) { |
| 606 | ret = mount(b->src, dest, NULL, |
Jorge Lucangeli Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 607 | MS_BIND | MS_REMOUNT | MS_RDONLY, NULL); |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 608 | if (ret) |
| 609 | pdie("bind ro: %s -> %s", b->src, dest); |
| 610 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 611 | free(dest); |
| 612 | if (b->next) |
| 613 | return bind_one(j, b->next); |
| 614 | return ret; |
| 615 | } |
| 616 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 617 | int enter_chroot(const struct minijail *j) |
| 618 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 619 | int ret; |
| 620 | if (j->bindings_head && (ret = bind_one(j, j->bindings_head))) |
| 621 | return ret; |
| 622 | |
| 623 | if (chroot(j->chrootdir)) |
| 624 | return -errno; |
| 625 | |
| 626 | if (chdir("/")) |
| 627 | return -errno; |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 632 | int mount_tmp(void) |
| 633 | { |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 634 | return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777"); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 637 | int remount_readonly(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 638 | { |
| 639 | const char *kProcPath = "/proc"; |
| 640 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 641 | /* |
| 642 | * 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] | 643 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 644 | * mutate our parent's mount as well, even though we're in a VFS |
| 645 | * namespace (!). Instead, remove their mount from our namespace |
| 646 | * and make our own. |
| 647 | */ |
| 648 | if (umount(kProcPath)) |
| 649 | return -errno; |
| 650 | if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
| 651 | return -errno; |
| 652 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 653 | } |
| 654 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 655 | void drop_ugid(const struct minijail *j) |
| 656 | { |
| 657 | if (j->flags.usergroups) { |
| 658 | if (initgroups(j->user, j->usergid)) |
| 659 | pdie("initgroups"); |
| 660 | } else { |
| 661 | /* Only attempt to clear supplemental groups if we are changing |
| 662 | * users. */ |
| 663 | if ((j->uid || j->gid) && setgroups(0, NULL)) |
| 664 | pdie("setgroups"); |
| 665 | } |
| 666 | |
| 667 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
| 668 | pdie("setresgid"); |
| 669 | |
| 670 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
| 671 | pdie("setresuid"); |
| 672 | } |
| 673 | |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 674 | /* |
| 675 | * We specifically do not use cap_valid() as that only tells us the last |
| 676 | * valid cap we were *compiled* against (i.e. what the version of kernel |
| 677 | * headers says). If we run on a different kernel version, then it's not |
| 678 | * uncommon for that to be less (if an older kernel) or more (if a newer |
| 679 | * kernel). So suck up the answer via /proc. |
| 680 | */ |
| 681 | static int run_cap_valid(unsigned int cap) |
| 682 | { |
| 683 | static unsigned int last_cap; |
| 684 | |
| 685 | if (!last_cap) { |
| 686 | const char cap_file[] = "/proc/sys/kernel/cap_last_cap"; |
| 687 | FILE *fp = fopen(cap_file, "re"); |
| 688 | if (fscanf(fp, "%u", &last_cap) != 1) |
| 689 | pdie("fscanf(%s)", cap_file); |
| 690 | fclose(fp); |
| 691 | } |
| 692 | |
| 693 | return cap <= last_cap; |
| 694 | } |
| 695 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 696 | void drop_caps(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 697 | { |
| 698 | cap_t caps = cap_get_proc(); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 699 | cap_value_t flag[1]; |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 700 | const uint64_t one = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 701 | unsigned int i; |
| 702 | if (!caps) |
| 703 | die("can't get process caps"); |
| 704 | if (cap_clear_flag(caps, CAP_INHERITABLE)) |
| 705 | die("can't clear inheritable caps"); |
| 706 | if (cap_clear_flag(caps, CAP_EFFECTIVE)) |
| 707 | die("can't clear effective caps"); |
| 708 | if (cap_clear_flag(caps, CAP_PERMITTED)) |
| 709 | die("can't clear permitted caps"); |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 710 | for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 711 | /* Keep CAP_SETPCAP for dropping bounding set bits. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 712 | if (i != CAP_SETPCAP && !(j->caps & (one << i))) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 713 | continue; |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 714 | flag[0] = i; |
| 715 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 716 | die("can't add effective cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 717 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 718 | die("can't add permitted cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 719 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 720 | die("can't add inheritable cap"); |
| 721 | } |
| 722 | if (cap_set_proc(caps)) |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 723 | die("can't apply initial cleaned capset"); |
| 724 | |
| 725 | /* |
| 726 | * Instead of dropping bounding set first, do it here in case |
| 727 | * the caller had a more permissive bounding set which could |
| 728 | * have been used above to raise a capability that wasn't already |
| 729 | * present. This requires CAP_SETPCAP, so we raised/kept it above. |
| 730 | */ |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 731 | for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) { |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 732 | if (j->caps & (one << i)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 733 | continue; |
| 734 | if (prctl(PR_CAPBSET_DROP, i)) |
| 735 | pdie("prctl(PR_CAPBSET_DROP)"); |
| 736 | } |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 737 | |
| 738 | /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 739 | if ((j->caps & (one << CAP_SETPCAP)) == 0) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 740 | flag[0] = CAP_SETPCAP; |
| 741 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR)) |
| 742 | die("can't clear effective cap"); |
| 743 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR)) |
| 744 | die("can't clear permitted cap"); |
| 745 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR)) |
| 746 | die("can't clear inheritable cap"); |
| 747 | } |
| 748 | |
| 749 | if (cap_set_proc(caps)) |
| 750 | die("can't apply final cleaned capset"); |
| 751 | |
| 752 | cap_free(caps); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 753 | } |
| 754 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 755 | void set_seccomp_filter(const struct minijail *j) |
| 756 | { |
| 757 | /* |
| 758 | * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c> |
| 759 | * in the kernel source tree for an explanation of the parameters. |
| 760 | */ |
| 761 | if (j->flags.no_new_privs) { |
| 762 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 763 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * If we're logging seccomp filter failures, |
| 768 | * install the SIGSYS handler first. |
| 769 | */ |
| 770 | if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) { |
| 771 | if (install_sigsys_handler()) |
| 772 | pdie("install SIGSYS handler"); |
| 773 | warn("logging seccomp filter failures"); |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Install the syscall filter. |
| 778 | */ |
| 779 | if (j->flags.seccomp_filter) { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 780 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) { |
| 781 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 782 | warn("seccomp not supported"); |
| 783 | return; |
| 784 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 785 | pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 786 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 790 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 791 | { |
| 792 | if (j->flags.pids) |
| 793 | die("tried to enter a pid-namespaced jail;" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 794 | " try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 795 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 796 | if (j->flags.usergroups && !j->user) |
| 797 | die("usergroup inheritance without username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 798 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 799 | /* |
| 800 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 801 | * so we don't even try. If any of our operations fail, we abort() the |
| 802 | * entire process. |
| 803 | */ |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 804 | if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS)) |
| 805 | pdie("setns(CLONE_NEWNS)"); |
| 806 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 807 | if (j->flags.vfs && unshare(CLONE_NEWNS)) |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 808 | pdie("unshare(vfs)"); |
| 809 | |
| 810 | if (j->flags.net && unshare(CLONE_NEWNET)) |
| 811 | pdie("unshare(net)"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 812 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 813 | if (j->flags.chroot && enter_chroot(j)) |
| 814 | pdie("chroot"); |
| 815 | |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 816 | if (j->flags.mount_tmp && mount_tmp()) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 817 | pdie("mount_tmp"); |
| 818 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 819 | if (j->flags.readonly && remount_readonly()) |
| 820 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 821 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 822 | if (j->flags.caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 823 | /* |
| 824 | * POSIX capabilities are a bit tricky. If we drop our |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 825 | * capability to change uids, our attempt to use setuid() |
| 826 | * below will fail. Hang on to root caps across setuid(), then |
| 827 | * lock securebits. |
| 828 | */ |
| 829 | if (prctl(PR_SET_KEEPCAPS, 1)) |
| 830 | pdie("prctl(PR_SET_KEEPCAPS)"); |
| 831 | if (prctl |
| 832 | (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS)) |
| 833 | pdie("prctl(PR_SET_SECUREBITS)"); |
| 834 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 835 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 836 | /* |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 837 | * If we're setting no_new_privs, we can drop privileges |
| 838 | * before setting seccomp filter. This way filter policies |
| 839 | * don't need to allow privilege-dropping syscalls. |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 840 | */ |
| 841 | if (j->flags.no_new_privs) { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 842 | drop_ugid(j); |
| 843 | if (j->flags.caps) |
| 844 | drop_caps(j); |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 845 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 846 | set_seccomp_filter(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 847 | } else { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 848 | /* |
| 849 | * If we're not setting no_new_privs, |
| 850 | * we need to set seccomp filter *before* dropping privileges. |
| 851 | * WARNING: this means that filter policies *must* allow |
| 852 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 853 | * capget()/capset()/prctl() for dropping caps. |
| 854 | */ |
| 855 | set_seccomp_filter(j); |
| 856 | |
| 857 | drop_ugid(j); |
| 858 | if (j->flags.caps) |
| 859 | drop_caps(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 860 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 861 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 862 | /* |
| 863 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 864 | * privilege-dropping syscalls :) |
| 865 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 866 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) { |
| 867 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 868 | warn("seccomp not supported"); |
| 869 | return; |
| 870 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 871 | pdie("prctl(PR_SET_SECCOMP)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 872 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 873 | } |
| 874 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 875 | /* TODO(wad) will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 876 | static int init_exitstatus = 0; |
| 877 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 878 | void init_term(int __attribute__ ((unused)) sig) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 879 | { |
| 880 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 881 | } |
| 882 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 883 | int init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 884 | { |
| 885 | pid_t pid; |
| 886 | int status; |
| 887 | /* so that we exit with the right status */ |
| 888 | signal(SIGTERM, init_term); |
| 889 | /* TODO(wad) self jail with seccomp_filters here. */ |
| 890 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 891 | /* |
| 892 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 893 | * left inside our pid namespace or we get a signal. |
| 894 | */ |
| 895 | if (pid == rootpid) |
| 896 | init_exitstatus = status; |
| 897 | } |
| 898 | if (!WIFEXITED(init_exitstatus)) |
| 899 | _exit(MINIJAIL_ERR_INIT); |
| 900 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 901 | } |
| 902 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 903 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 904 | { |
| 905 | size_t sz = 0; |
| 906 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 907 | char *buf; |
| 908 | int r; |
| 909 | if (sizeof(sz) != bytes) |
| 910 | return -EINVAL; |
| 911 | if (sz > USHRT_MAX) /* Arbitrary sanity check */ |
| 912 | return -E2BIG; |
| 913 | buf = malloc(sz); |
| 914 | if (!buf) |
| 915 | return -ENOMEM; |
| 916 | bytes = read(fd, buf, sz); |
| 917 | if (bytes != sz) { |
| 918 | free(buf); |
| 919 | return -EINVAL; |
| 920 | } |
| 921 | r = minijail_unmarshal(j, buf, sz); |
| 922 | free(buf); |
| 923 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 924 | } |
| 925 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 926 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 927 | { |
| 928 | char *buf; |
| 929 | size_t sz = minijail_size(j); |
| 930 | ssize_t written; |
| 931 | int r; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 932 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 933 | if (!sz) |
| 934 | return -EINVAL; |
| 935 | buf = malloc(sz); |
| 936 | r = minijail_marshal(j, buf, sz); |
| 937 | if (r) { |
| 938 | free(buf); |
| 939 | return r; |
| 940 | } |
| 941 | /* Sends [size][minijail]. */ |
| 942 | written = write(fd, &sz, sizeof(sz)); |
| 943 | if (written != sizeof(sz)) { |
| 944 | free(buf); |
| 945 | return -EFAULT; |
| 946 | } |
| 947 | written = write(fd, buf, sz); |
| 948 | if (written < 0 || (size_t) written != sz) { |
| 949 | free(buf); |
| 950 | return -EFAULT; |
| 951 | } |
| 952 | free(buf); |
| 953 | return 0; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 954 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 955 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 956 | int setup_preload(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 957 | { |
Daniel Erat | 5b7a318 | 2015-08-19 16:06:22 -0600 | [diff] [blame] | 958 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 959 | /* Don't use LDPRELOAD on Brillo. */ |
| 960 | return 0; |
| 961 | #else |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 962 | char *oldenv = getenv(kLdPreloadEnvVar) ? : ""; |
| 963 | char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH)); |
| 964 | if (!newenv) |
| 965 | return -ENOMEM; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 966 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 967 | /* Only insert a separating space if we have something to separate... */ |
| 968 | sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "", |
| 969 | PRELOADPATH); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 970 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 971 | /* setenv() makes a copy of the string we give it */ |
| 972 | setenv(kLdPreloadEnvVar, newenv, 1); |
| 973 | free(newenv); |
| 974 | return 0; |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 975 | #endif |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 976 | } |
| 977 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 978 | int setup_pipe(int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 979 | { |
| 980 | int r = pipe(fds); |
| 981 | char fd_buf[11]; |
| 982 | if (r) |
| 983 | return r; |
| 984 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 985 | if (r <= 0) |
| 986 | return -EINVAL; |
| 987 | setenv(kFdEnvVar, fd_buf, 1); |
| 988 | return 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 989 | } |
| 990 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 991 | int setup_pipe_end(int fds[2], size_t index) |
| 992 | { |
| 993 | if (index > 1) |
| 994 | return -1; |
| 995 | |
| 996 | close(fds[1 - index]); |
| 997 | return fds[index]; |
| 998 | } |
| 999 | |
| 1000 | int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd) |
| 1001 | { |
| 1002 | if (index > 1) |
| 1003 | return -1; |
| 1004 | |
| 1005 | close(fds[1 - index]); |
| 1006 | /* dup2(2) the corresponding end of the pipe into |fd|. */ |
| 1007 | return dup2(fds[index], fd); |
| 1008 | } |
| 1009 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1010 | int API minijail_run(struct minijail *j, const char *filename, |
| 1011 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1012 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1013 | return minijail_run_pid_pipes(j, filename, argv, |
| 1014 | NULL, NULL, NULL, NULL); |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 1018 | char *const argv[], pid_t *pchild_pid) |
| 1019 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1020 | return minijail_run_pid_pipes(j, filename, argv, pchild_pid, |
| 1021 | NULL, NULL, NULL); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | int API minijail_run_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 1025 | char *const argv[], int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1026 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1027 | return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd, |
| 1028 | NULL, NULL); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | int API minijail_run_pid_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 1032 | char *const argv[], pid_t *pchild_pid, |
| 1033 | int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1034 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1035 | return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd, |
| 1036 | NULL, NULL); |
| 1037 | } |
| 1038 | |
| 1039 | 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] | 1040 | char *const argv[], pid_t *pchild_pid, |
| 1041 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1042 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1043 | char *oldenv, *oldenv_copy = NULL; |
| 1044 | pid_t child_pid; |
| 1045 | int pipe_fds[2]; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1046 | int stdin_fds[2]; |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1047 | int stdout_fds[2]; |
| 1048 | int stderr_fds[2]; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1049 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1050 | /* We need to remember this across the minijail_preexec() call. */ |
| 1051 | int pid_namespace = j->flags.pids; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1052 | int do_init = j->flags.do_init; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1053 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1054 | oldenv = getenv(kLdPreloadEnvVar); |
| 1055 | if (oldenv) { |
| 1056 | oldenv_copy = strdup(oldenv); |
| 1057 | if (!oldenv_copy) |
| 1058 | return -ENOMEM; |
| 1059 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1060 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1061 | if (setup_preload()) |
| 1062 | return -EFAULT; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1063 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1064 | /* |
Jorge Lucangeli Obes | 3c84df1 | 2015-05-14 17:37:58 -0700 | [diff] [blame] | 1065 | * Make the process group ID of this process equal to its PID, so that |
| 1066 | * both the Minijail process and the jailed process can be killed |
| 1067 | * together. |
| 1068 | * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when |
| 1069 | * the process is already a process group leader. |
| 1070 | */ |
| 1071 | if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) { |
| 1072 | if (errno != EPERM) { |
| 1073 | pdie("setpgid(0, 0)"); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | /* |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1078 | * Before we fork(2) and execve(2) the child process, we need to open |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1079 | * a pipe(2) to send the minijail configuration over. |
| 1080 | */ |
| 1081 | if (setup_pipe(pipe_fds)) |
| 1082 | return -EFAULT; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1083 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1084 | /* |
| 1085 | * If we want to write to the child process' standard input, |
| 1086 | * create the pipe(2) now. |
| 1087 | */ |
| 1088 | if (pstdin_fd) { |
| 1089 | if (pipe(stdin_fds)) |
| 1090 | return -EFAULT; |
| 1091 | } |
| 1092 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1093 | /* |
| 1094 | * If we want to read from the child process' standard output, |
| 1095 | * create the pipe(2) now. |
| 1096 | */ |
| 1097 | if (pstdout_fd) { |
| 1098 | if (pipe(stdout_fds)) |
| 1099 | return -EFAULT; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * If we want to read from the child process' standard error, |
| 1104 | * create the pipe(2) now. |
| 1105 | */ |
| 1106 | if (pstderr_fd) { |
| 1107 | if (pipe(stderr_fds)) |
| 1108 | return -EFAULT; |
| 1109 | } |
| 1110 | |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1111 | /* Use sys_clone() if and only if we're creating a pid namespace. |
| 1112 | * |
| 1113 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 1114 | * |
| 1115 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 1116 | * some of which may be held by other threads at the time that we call |
| 1117 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 1118 | * ensure that we hold all of these locks before it calls clone() |
| 1119 | * internally and drop them after clone() returns, but when we call |
| 1120 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 1121 | * child address space where some of libc's important locks are held by |
| 1122 | * other threads (which did not get cloned, and hence will never release |
| 1123 | * those locks). This is okay so long as we call exec() immediately |
| 1124 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 1125 | * take locks. |
| 1126 | * |
| 1127 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 1128 | * namespacing. If we follow this path, the child's address space might |
| 1129 | * have broken locks; you may only call functions that do not acquire |
| 1130 | * any locks. |
| 1131 | * |
| 1132 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 1133 | * previously detailed, so this function is highly likely to deadlock |
| 1134 | * later on (see "deadlock here") if we're multithreaded. |
| 1135 | * |
| 1136 | * We might hack around this by having the clone()d child (init of the |
| 1137 | * pid namespace) return directly, rather than leaving the clone()d |
| 1138 | * process hanging around to be init for the new namespace (and having |
| 1139 | * its fork()ed child return in turn), but that process would be crippled |
| 1140 | * with its libc locks potentially broken. We might try fork()ing in the |
| 1141 | * parent before we clone() to ensure that we own all the locks, but |
| 1142 | * then we have to have the forked child hanging around consuming |
| 1143 | * resources (and possibly having file descriptors / shared memory |
| 1144 | * regions / etc attached). We'd need to keep the child around to avoid |
| 1145 | * having its children get reparented to init. |
| 1146 | * |
| 1147 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 1148 | * problem is fixable or not. It would be nice if we worked in this |
| 1149 | * case. |
| 1150 | */ |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1151 | if (pid_namespace) |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1152 | child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL); |
| 1153 | else |
| 1154 | child_pid = fork(); |
| 1155 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1156 | if (child_pid < 0) { |
| 1157 | free(oldenv_copy); |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1158 | die("failed to fork child"); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1159 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1160 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1161 | if (child_pid) { |
| 1162 | /* Restore parent's LD_PRELOAD. */ |
| 1163 | if (oldenv_copy) { |
| 1164 | setenv(kLdPreloadEnvVar, oldenv_copy, 1); |
| 1165 | free(oldenv_copy); |
| 1166 | } else { |
| 1167 | unsetenv(kLdPreloadEnvVar); |
| 1168 | } |
| 1169 | unsetenv(kFdEnvVar); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1170 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1171 | j->initpid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1172 | |
| 1173 | /* Send marshalled minijail. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1174 | close(pipe_fds[0]); /* read endpoint */ |
| 1175 | ret = minijail_to_fd(j, pipe_fds[1]); |
| 1176 | close(pipe_fds[1]); /* write endpoint */ |
| 1177 | if (ret) { |
| 1178 | kill(j->initpid, SIGKILL); |
| 1179 | die("failed to send marshalled minijail"); |
| 1180 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1181 | |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1182 | if (pchild_pid) |
| 1183 | *pchild_pid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1184 | |
| 1185 | /* |
| 1186 | * If we want to write to the child process' standard input, |
| 1187 | * set up the write end of the pipe. |
| 1188 | */ |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1189 | if (pstdin_fd) |
| 1190 | *pstdin_fd = setup_pipe_end(stdin_fds, |
| 1191 | 1 /* write end */); |
| 1192 | |
| 1193 | /* |
| 1194 | * If we want to read from the child process' standard output, |
| 1195 | * set up the read end of the pipe. |
| 1196 | */ |
| 1197 | if (pstdout_fd) |
| 1198 | *pstdout_fd = setup_pipe_end(stdout_fds, |
| 1199 | 0 /* read end */); |
| 1200 | |
| 1201 | /* |
| 1202 | * If we want to read from the child process' standard error, |
| 1203 | * set up the read end of the pipe. |
| 1204 | */ |
| 1205 | if (pstderr_fd) |
| 1206 | *pstderr_fd = setup_pipe_end(stderr_fds, |
| 1207 | 0 /* read end */); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1208 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1209 | return 0; |
| 1210 | } |
| 1211 | free(oldenv_copy); |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1212 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1213 | /* |
| 1214 | * If we want to write to the jailed process' standard input, |
| 1215 | * set up the read end of the pipe. |
| 1216 | */ |
| 1217 | if (pstdin_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1218 | if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */, |
| 1219 | STDIN_FILENO) < 0) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1220 | die("failed to set up stdin pipe"); |
| 1221 | } |
| 1222 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1223 | /* |
| 1224 | * If we want to read from the jailed process' standard output, |
| 1225 | * set up the write end of the pipe. |
| 1226 | */ |
| 1227 | if (pstdout_fd) { |
| 1228 | if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */, |
| 1229 | STDOUT_FILENO) < 0) |
| 1230 | die("failed to set up stdout pipe"); |
| 1231 | } |
| 1232 | |
| 1233 | /* |
| 1234 | * If we want to read from the jailed process' standard error, |
| 1235 | * set up the write end of the pipe. |
| 1236 | */ |
| 1237 | if (pstderr_fd) { |
| 1238 | if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */, |
| 1239 | STDERR_FILENO) < 0) |
| 1240 | die("failed to set up stderr pipe"); |
| 1241 | } |
| 1242 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 1243 | /* Strip out flags that cannot be inherited across execve. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1244 | minijail_preexec(j); |
| 1245 | /* Jail this process and its descendants... */ |
| 1246 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1247 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1248 | if (pid_namespace && do_init) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1249 | /* |
| 1250 | * pid namespace: this process will become init inside the new |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1251 | * namespace. We don't want all programs we might exec to have |
| 1252 | * to know how to be init. Normally |do_init == 1| we fork off |
| 1253 | * a child to actually run the program. If |do_init == 0|, we |
| 1254 | * let the program keep pid 1 and be init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1255 | * |
| 1256 | * If we're multithreaded, we'll probably deadlock here. See |
| 1257 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1258 | */ |
| 1259 | child_pid = fork(); |
| 1260 | if (child_pid < 0) |
| 1261 | _exit(child_pid); |
| 1262 | else if (child_pid > 0) |
| 1263 | init(child_pid); /* never returns */ |
| 1264 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1265 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1266 | /* |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1267 | * If we aren't pid-namespaced, or jailed program asked to be init: |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1268 | * calling process |
| 1269 | * -> execve()-ing process |
| 1270 | * If we are: |
| 1271 | * calling process |
| 1272 | * -> init()-ing process |
| 1273 | * -> execve()-ing process |
| 1274 | */ |
| 1275 | _exit(execve(filename, argv, environ)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1276 | } |
| 1277 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1278 | int API minijail_run_static(struct minijail *j, const char *filename, |
| 1279 | char *const argv[]) |
| 1280 | { |
| 1281 | pid_t child_pid; |
| 1282 | int pid_namespace = j->flags.pids; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1283 | int do_init = j->flags.do_init; |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1284 | |
| 1285 | if (j->flags.caps) |
| 1286 | die("caps not supported with static targets"); |
| 1287 | |
| 1288 | if (pid_namespace) |
| 1289 | child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL); |
| 1290 | else |
| 1291 | child_pid = fork(); |
| 1292 | |
| 1293 | if (child_pid < 0) { |
| 1294 | die("failed to fork child"); |
| 1295 | } |
| 1296 | if (child_pid > 0 ) { |
| 1297 | j->initpid = child_pid; |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | * We can now drop this child into the sandbox |
| 1303 | * then execve the target. |
| 1304 | */ |
| 1305 | |
| 1306 | j->flags.pids = 0; |
| 1307 | minijail_enter(j); |
| 1308 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1309 | if (pid_namespace && do_init) { |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * pid namespace: this process will become init inside the new |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1312 | * namespace. We don't want all programs we might exec to have |
| 1313 | * to know how to be init. Normally |do_init == 1| we fork off |
| 1314 | * a child to actually run the program. If |do_init == 0|, we |
| 1315 | * let the program keep pid 1 and be init. |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1316 | * |
| 1317 | * If we're multithreaded, we'll probably deadlock here. See |
| 1318 | * WARNING above. |
| 1319 | */ |
| 1320 | child_pid = fork(); |
| 1321 | if (child_pid < 0) |
| 1322 | _exit(child_pid); |
| 1323 | else if (child_pid > 0) |
| 1324 | init(child_pid); /* never returns */ |
| 1325 | } |
| 1326 | |
| 1327 | _exit(execve(filename, argv, environ)); |
| 1328 | } |
| 1329 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1330 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1331 | { |
| 1332 | int st; |
| 1333 | if (kill(j->initpid, SIGTERM)) |
| 1334 | return -errno; |
| 1335 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1336 | return -errno; |
| 1337 | return st; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1338 | } |
| 1339 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1340 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1341 | { |
| 1342 | int st; |
| 1343 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1344 | return -errno; |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1345 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1346 | if (!WIFEXITED(st)) { |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1347 | int error_status = st; |
| 1348 | if (WIFSIGNALED(st)) { |
| 1349 | int signum = WTERMSIG(st); |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1350 | warn("child process %d received signal %d", |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1351 | j->initpid, signum); |
| 1352 | /* |
| 1353 | * We return MINIJAIL_ERR_JAIL if the process received |
| 1354 | * SIGSYS, which happens when a syscall is blocked by |
| 1355 | * seccomp filters. |
| 1356 | * If not, we do what bash(1) does: |
| 1357 | * $? = 128 + signum |
| 1358 | */ |
| 1359 | if (signum == SIGSYS) { |
| 1360 | error_status = MINIJAIL_ERR_JAIL; |
| 1361 | } else { |
| 1362 | error_status = 128 + signum; |
| 1363 | } |
| 1364 | } |
| 1365 | return error_status; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1366 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1367 | |
| 1368 | int exit_status = WEXITSTATUS(st); |
| 1369 | if (exit_status != 0) |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1370 | info("child process %d exited with status %d", |
| 1371 | j->initpid, exit_status); |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1372 | |
| 1373 | return exit_status; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1374 | } |
| 1375 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1376 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1377 | { |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1378 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 1379 | free(j->filter_prog->filter); |
| 1380 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1381 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1382 | while (j->bindings_head) { |
| 1383 | struct binding *b = j->bindings_head; |
| 1384 | j->bindings_head = j->bindings_head->next; |
| 1385 | free(b->dest); |
| 1386 | free(b->src); |
| 1387 | free(b); |
| 1388 | } |
| 1389 | j->bindings_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1390 | if (j->user) |
| 1391 | free(j->user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1392 | if (j->chrootdir) |
| 1393 | free(j->chrootdir); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1394 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1395 | } |