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