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