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