blob: 103aa5d0cd85875f71d7bd1c77e763add8d78c95 [file] [log] [blame]
Elly Jonesdd3e8512012-01-23 15:13:38 -05001/*
2 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04003 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05004 * found in the LICENSE file.
5 */
Elly Jonescd7a9042011-07-22 13:56:51 -04006
7#define _BSD_SOURCE
8#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07009
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080010#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050011#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
13#include <grp.h>
14#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050015#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <pwd.h>
18#include <sched.h>
19#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050020#include <stdarg.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080021#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040022#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 Drewryf89aef52011-09-16 16:48:57 -050028#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040029#include <sys/prctl.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080030#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040031#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040032#include <unistd.h>
33
34#include "libminijail.h"
35#include "libminijail-private.h"
36
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070037#include "signal.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080038#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070039#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040
Lei Zhangeee31552012-10-17 21:27:10 -070041#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 Drewry32ac9f52011-08-18 21:36:27 -050048/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080049#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070050# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#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 Drewry32ac9f52011-08-18 21:36:27 -050059#endif
60
Elly Jones51a5b6c2011-10-12 19:09:26 -040061struct binding {
62 char *src;
63 char *dest;
64 int writeable;
65 struct binding *next;
66};
67
Will Drewryf89aef52011-09-16 16:48:57 -050068struct minijail {
Elly Jonese1749eb2011-10-07 13:54:59 -040069 struct {
70 int uid:1;
71 int gid:1;
72 int caps:1;
73 int vfs:1;
74 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040075 int net:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040076 int seccomp:1;
77 int readonly:1;
78 int usergroups:1;
79 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070080 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040081 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070082 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040083 int chroot:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040084 } 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 Obes524c0402012-01-17 11:30:23 -080091 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -040092 int binding_count;
93 char *chrootdir;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080094 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -040095 struct binding *bindings_head;
96 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -050097};
98
Will Drewry6ac91122011-10-21 16:38:58 -050099struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400100{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400101 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400102}
103
Will Drewry6ac91122011-10-21 16:38:58 -0500104void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400105{
106 if (uid == 0)
107 die("useless change to uid 0");
108 j->uid = uid;
109 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400110}
111
Will Drewry6ac91122011-10-21 16:38:58 -0500112void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400113{
114 if (gid == 0)
115 die("useless change to gid 0");
116 j->gid = gid;
117 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400118}
119
Will Drewry6ac91122011-10-21 16:38:58 -0500120int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400121{
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 Joneseb300c52011-09-22 14:35:43 -0400128
Elly Jonesdd3e8512012-01-23 15:13:38 -0500129 /*
130 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400131 * 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 Jonesdd3e8512012-01-23 15:13:38 -0500137 /*
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 Jonese1749eb2011-10-07 13:54:59 -0400142 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 Jonescd7a9042011-07-22 13:56:51 -0400151}
152
Will Drewry6ac91122011-10-21 16:38:58 -0500153int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400154{
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 Joneseb300c52011-09-22 14:35:43 -0400161
Elly Jonesdd3e8512012-01-23 15:13:38 -0500162 /*
163 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400164 * 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 Jonesdd3e8512012-01-23 15:13:38 -0500170 /*
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 Jonese1749eb2011-10-07 13:54:59 -0400175 free(buf);
176 if (!pgr)
177 return -errno;
178 minijail_change_gid(j, pgr->gr_gid);
179 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400180}
181
Will Drewry6ac91122011-10-21 16:38:58 -0500182void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400183{
184 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400185}
186
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700187void API minijail_no_new_privs(struct minijail *j)
188{
189 j->flags.no_new_privs = 1;
190}
191
Will Drewry6ac91122011-10-21 16:38:58 -0500192void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400193{
194 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500195}
196
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700197void API minijail_log_seccomp_filter_failures(struct minijail *j)
198{
199 j->flags.log_seccomp_filter = 1;
200}
201
Will Drewry6ac91122011-10-21 16:38:58 -0500202void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400203{
204 j->caps = capmask;
205 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400206}
207
Will Drewry6ac91122011-10-21 16:38:58 -0500208void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400209{
210 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400211}
212
Will Drewry6ac91122011-10-21 16:38:58 -0500213void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400214{
Elly Jonese58176c2012-01-23 11:46:17 -0500215 j->flags.vfs = 1;
216 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400217 j->flags.pids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400218}
219
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400220void API minijail_namespace_net(struct minijail *j)
221{
222 j->flags.net = 1;
223}
224
Will Drewry6ac91122011-10-21 16:38:58 -0500225void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400226{
227 j->flags.vfs = 1;
228 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400229}
230
Will Drewry6ac91122011-10-21 16:38:58 -0500231void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400232{
233 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400234}
235
Will Drewry6ac91122011-10-21 16:38:58 -0500236void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400237{
238 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400239}
240
Will Drewry6ac91122011-10-21 16:38:58 -0500241int API minijail_enter_chroot(struct minijail *j, const char *dir) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400242 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 Drewry6ac91122011-10-21 16:38:58 -0500251int API minijail_bind(struct minijail *j, const char *src, const char *dest,
252 int writeable) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400253 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 Obes224e4272012-08-02 14:31:39 -0700268 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400269
Elly Jonesdd3e8512012-01-23 15:13:38 -0500270 /*
271 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400272 * 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
285error:
286 free(b->src);
287 free(b->dest);
288 free(b);
289 return -ENOMEM;
290}
291
Will Drewry6ac91122011-10-21 16:38:58 -0500292void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400293{
294 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800295 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700296 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400297 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800298
299 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700300 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 Obes524c0402012-01-17 11:30:23 -0800303 }
304
305 j->filter_len = fprog->len;
306 j->filter_prog = fprog;
307
Elly Jonese1749eb2011-10-07 13:54:59 -0400308 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500309}
310
Will Drewryf89aef52011-09-16 16:48:57 -0500311struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400312 size_t available;
313 size_t total;
314 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500315};
316
Will Drewry6ac91122011-10-21 16:38:58 -0500317void marshal_state_init(struct marshal_state *state,
318 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400319{
320 state->available = available;
321 state->buf = buf;
322 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500323}
324
Will Drewry6ac91122011-10-21 16:38:58 -0500325void marshal_append(struct marshal_state *state,
326 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400327{
328 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500329
Elly Jonese1749eb2011-10-07 13:54:59 -0400330 /* 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 Drewryf89aef52011-09-16 16:48:57 -0500338}
339
Will Drewry6ac91122011-10-21 16:38:58 -0500340void minijail_marshal_helper(struct marshal_state *state,
341 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400342{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400343 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400344 marshal_append(state, (char *)j, sizeof(*j));
345 if (j->user)
346 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400347 if (j->chrootdir)
348 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800349 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 Jonese1749eb2011-10-07 13:54:59 -0400353 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400354 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 Obesbda833c2012-07-31 16:25:56 -0700357 marshal_append(state, (char *)&b->writeable,
358 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400359 }
Will Drewryf89aef52011-09-16 16:48:57 -0500360}
361
Will Drewry6ac91122011-10-21 16:38:58 -0500362size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400363{
364 struct marshal_state state;
365 marshal_state_init(&state, NULL, 0);
366 minijail_marshal_helper(&state, j);
367 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500368}
369
Elly Jonese1749eb2011-10-07 13:54:59 -0400370int 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 Drewry2ddaad02011-09-16 11:36:08 -0500376}
377
Elly Jones51a5b6c2011-10-12 19:09:26 -0400378/* 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 Drewry6ac91122011-10-21 16:38:58 -0500385void *consumebytes(size_t length, char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400386 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 Drewry6ac91122011-10-21 16:38:58 -0500400char *consumestr(char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400401 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 Jonese1749eb2011-10-07 13:54:59 -0400408int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
409{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400410 int i;
411 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500412 int ret = -EINVAL;
413
Elly Jonese1749eb2011-10-07 13:54:59 -0400414 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500415 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400416 memcpy((void *)j, serialized, sizeof(*j));
417 serialized += sizeof(*j);
418 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500419
Will Drewrybee7ba72011-10-21 20:47:01 -0500420 /* Potentially stale pointers not used as signals. */
421 j->bindings_head = NULL;
422 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800423 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500424
Elly Jonese1749eb2011-10-07 13:54:59 -0400425 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400426 char *user = consumestr(&serialized, &length);
427 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500428 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400429 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500430 if (!j->user)
431 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400432 }
Will Drewryf89aef52011-09-16 16:48:57 -0500433
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400434 if (j->chrootdir) { /* stale pointer */
435 char *chrootdir = consumestr(&serialized, &length);
436 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500437 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400438 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500439 if (!j->chrootdir)
440 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400441 }
442
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800443 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 Jonese1749eb2011-10-07 13:54:59 -0400458 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400459
460 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400461 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 Drewrybee7ba72011-10-21 20:47:01 -0500467 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400468 dest = consumestr(&serialized, &length);
469 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500470 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400471 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
472 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500473 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400474 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500475 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400476 }
477
Elly Jonese1749eb2011-10-07 13:54:59 -0400478 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500479
480bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800481 if (j->flags.seccomp_filter && j->filter_len > 0) {
482 free(j->filter_prog->filter);
483 free(j->filter_prog);
484 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500485bad_filters:
486 if (j->chrootdir)
487 free(j->chrootdir);
488bad_chrootdir:
489 if (j->user)
490 free(j->user);
491clear_pointers:
492 j->user = NULL;
493 j->chrootdir = NULL;
494out:
495 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500496}
497
Elly Jonese1749eb2011-10-07 13:54:59 -0400498void 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 Drewryfe4a3722011-09-16 14:50:50 -0500504}
505
Elly Jonese1749eb2011-10-07 13:54:59 -0400506void 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 Drewry2ddaad02011-09-16 11:36:08 -0500518}
519
Elly Jones51a5b6c2011-10-12 19:09:26 -0400520/* 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 Drewry6ac91122011-10-21 16:38:58 -0500526int bind_one(const struct minijail *j, struct binding *b) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400527 int ret = 0;
528 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400529 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 Jonesa1059632011-12-15 15:17:07 -0500534 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400535 if (ret)
536 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500537 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 Jones51a5b6c2011-10-12 19:09:26 -0400543 free(dest);
544 if (b->next)
545 return bind_one(j, b->next);
546 return ret;
547}
548
Will Drewry6ac91122011-10-21 16:38:58 -0500549int enter_chroot(const struct minijail *j) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400550 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 Drewry6ac91122011-10-21 16:38:58 -0500563int remount_readonly(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400564{
565 const char *kProcPath = "/proc";
566 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500567 /*
568 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400569 * /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 Jonescd7a9042011-07-22 13:56:51 -0400579}
580
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700581void 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 Drewry6ac91122011-10-21 16:38:58 -0500600void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400601{
602 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800603 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800604 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400605 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 Cook323878a2013-02-05 15:35:24 -0800615 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800616 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400617 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800618 flag[0] = i;
619 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400620 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800621 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400622 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800623 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400624 die("can't add inheritable cap");
625 }
626 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800627 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 Jonese1749eb2011-10-07 13:54:59 -0400635 for (i = 0; i < sizeof(j->caps) * 8 && cap_valid((int)i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800636 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400637 continue;
638 if (prctl(PR_CAPBSET_DROP, i))
639 pdie("prctl(PR_CAPBSET_DROP)");
640 }
Kees Cook323878a2013-02-05 15:35:24 -0800641
642 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800643 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800644 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 Jonescd7a9042011-07-22 13:56:51 -0400657}
658
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700659void 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 Drewry6ac91122011-10-21 16:38:58 -0500689void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400690{
691 if (j->flags.pids)
692 die("tried to enter a pid-namespaced jail;"
693 "try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400694
Elly Jonese1749eb2011-10-07 13:54:59 -0400695 if (j->flags.usergroups && !j->user)
696 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400697
Elly Jonesdd3e8512012-01-23 15:13:38 -0500698 /*
699 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400700 * 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-Jones6c086302013-03-20 17:15:28 -0400704 pdie("unshare(vfs)");
705
706 if (j->flags.net && unshare(CLONE_NEWNET))
707 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400708
Elly Jones51a5b6c2011-10-12 19:09:26 -0400709 if (j->flags.chroot && enter_chroot(j))
710 pdie("chroot");
711
Elly Jonese1749eb2011-10-07 13:54:59 -0400712 if (j->flags.readonly && remount_readonly())
713 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400714
Elly Jonese1749eb2011-10-07 13:54:59 -0400715 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500716 /*
717 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400718 * 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 Jonescd7a9042011-07-22 13:56:51 -0400728
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700729 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700730 * 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 Obesc2c9bcc2012-05-01 09:30:24 -0700733 */
734 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700735 drop_ugid(j);
736 if (j->flags.caps)
737 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700738
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700739 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400740 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700741 /*
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 Jonese1749eb2011-10-07 13:54:59 -0400753 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400754
Elly Jonesdd3e8512012-01-23 15:13:38 -0500755 /*
756 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400757 * privilege-dropping syscalls :)
758 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400759 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1))
760 pdie("prctl(PR_SET_SECCOMP)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400761}
762
Will Drewry6ac91122011-10-21 16:38:58 -0500763/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400764static int init_exitstatus = 0;
765
Will Drewry6ac91122011-10-21 16:38:58 -0500766void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400767{
768 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400769}
770
Will Drewry6ac91122011-10-21 16:38:58 -0500771int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400772{
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 Jonesdd3e8512012-01-23 15:13:38 -0500779 /*
780 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -0400781 * 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 Jonescd7a9042011-07-22 13:56:51 -0400789}
790
Will Drewry6ac91122011-10-21 16:38:58 -0500791int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400792{
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 Drewry2f54b6a2011-09-16 13:45:31 -0500812}
813
Will Drewry6ac91122011-10-21 16:38:58 -0500814int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400815{
816 char *buf;
817 size_t sz = minijail_size(j);
818 ssize_t written;
819 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400820
Elly Jonese1749eb2011-10-07 13:54:59 -0400821 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 Drewry2f54b6a2011-09-16 13:45:31 -0500842}
Elly Jonescd7a9042011-07-22 13:56:51 -0400843
Will Drewry6ac91122011-10-21 16:38:58 -0500844int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400845{
846 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
847 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
848 if (!newenv)
849 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400850
Elly Jonese1749eb2011-10-07 13:54:59 -0400851 /* Only insert a separating space if we have something to separate... */
852 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
853 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400854
Elly Jonese1749eb2011-10-07 13:54:59 -0400855 /* setenv() makes a copy of the string we give it */
856 setenv(kLdPreloadEnvVar, newenv, 1);
857 free(newenv);
858 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400859}
860
Will Drewry6ac91122011-10-21 16:38:58 -0500861int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400862{
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 Drewryf89aef52011-09-16 16:48:57 -0500872}
873
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800874int 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
883int 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 Drewry6ac91122011-10-21 16:38:58 -0500893int API minijail_run(struct minijail *j, const char *filename,
894 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -0400895{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800896 return minijail_run_pid_pipes(j, filename, argv,
897 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700898}
899
900int API minijail_run_pid(struct minijail *j, const char *filename,
901 char *const argv[], pid_t *pchild_pid)
902{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800903 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
904 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700905}
906
907int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -0700908 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700909{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800910 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
911 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700912}
913
914int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -0700915 char *const argv[], pid_t *pchild_pid,
916 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700917{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800918 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
919 NULL, NULL);
920}
921
922int 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 Jonese1749eb2011-10-07 13:54:59 -0400926 char *oldenv, *oldenv_copy = NULL;
927 pid_t child_pid;
928 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700929 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800930 int stdout_fds[2];
931 int stderr_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -0400932 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -0400933 /* We need to remember this across the minijail_preexec() call. */
934 int pid_namespace = j->flags.pids;
Ben Chan541c7e52011-08-26 14:55:53 -0700935
Elly Jonese1749eb2011-10-07 13:54:59 -0400936 oldenv = getenv(kLdPreloadEnvVar);
937 if (oldenv) {
938 oldenv_copy = strdup(oldenv);
939 if (!oldenv_copy)
940 return -ENOMEM;
941 }
Will Drewryf89aef52011-09-16 16:48:57 -0500942
Elly Jonese1749eb2011-10-07 13:54:59 -0400943 if (setup_preload())
944 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500945
Elly Jonesdd3e8512012-01-23 15:13:38 -0500946 /*
947 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -0400948 * a pipe(2) to send the minijail configuration over.
949 */
950 if (setup_pipe(pipe_fds))
951 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -0400952
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700953 /*
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 Obes339a1132013-02-15 16:53:47 -0800962 /*
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 Jones761b7412012-06-13 15:49:52 -0400980 /* 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 Jonesa05d7bb2012-06-14 14:09:27 -04001020 if (pid_namespace)
Elly Jones761b7412012-06-13 15:49:52 -04001021 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1022 else
1023 child_pid = fork();
1024
Elly Jonese1749eb2011-10-07 13:54:59 -04001025 if (child_pid < 0) {
1026 free(oldenv_copy);
1027 return child_pid;
1028 }
Will Drewryf89aef52011-09-16 16:48:57 -05001029
Elly Jonese1749eb2011-10-07 13:54:59 -04001030 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 Obesdf4bd352012-08-29 19:12:28 -07001039
Elly Jonese1749eb2011-10-07 13:54:59 -04001040 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001041
1042 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001043 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 Obesdf4bd352012-08-29 19:12:28 -07001050
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001051 if (pchild_pid)
1052 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001053
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 Obes339a1132013-02-15 16:53:47 -08001058 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 Obesdf4bd352012-08-29 19:12:28 -07001077
Elly Jonese1749eb2011-10-07 13:54:59 -04001078 return 0;
1079 }
1080 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001081
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001082 /*
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 Obes339a1132013-02-15 16:53:47 -08001087 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1088 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001089 die("failed to set up stdin pipe");
1090 }
1091
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001092 /*
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 Jonese1749eb2011-10-07 13:54:59 -04001112 /* Drop everything that cannot be inherited across execve. */
1113 minijail_preexec(j);
1114 /* Jail this process and its descendants... */
1115 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001116
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001117 if (pid_namespace) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001118 /*
1119 * pid namespace: this process will become init inside the new
Elly Jonese1749eb2011-10-07 13:54:59 -04001120 * 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 Jones761b7412012-06-13 15:49:52 -04001123 *
1124 * If we're multithreaded, we'll probably deadlock here. See
1125 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001126 */
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 Jonescd7a9042011-07-22 13:56:51 -04001133
Elly Jonesdd3e8512012-01-23 15:13:38 -05001134 /*
1135 * If we aren't pid-namespaced:
Elly Jonese1749eb2011-10-07 13:54:59 -04001136 * 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 Jonescd7a9042011-07-22 13:56:51 -04001144}
1145
Will Drewry6ac91122011-10-21 16:38:58 -05001146int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001147{
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 Jonescd7a9042011-07-22 13:56:51 -04001154}
1155
Will Drewry6ac91122011-10-21 16:38:58 -05001156int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001157{
1158 int st;
1159 if (waitpid(j->initpid, &st, 0) < 0)
1160 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001161
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001162 if (!WIFEXITED(st)) {
1163 if (WIFSIGNALED(st))
1164 warn("child process received signal %d", WTERMSIG(st));
Elly Jonese1749eb2011-10-07 13:54:59 -04001165 return MINIJAIL_ERR_JAIL;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001166 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001167
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 Jonescd7a9042011-07-22 13:56:51 -04001173}
1174
Will Drewry6ac91122011-10-21 16:38:58 -05001175void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001176{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001177 if (j->flags.seccomp_filter && j->filter_prog) {
1178 free(j->filter_prog->filter);
1179 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001180 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001181 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 Jonese1749eb2011-10-07 13:54:59 -04001189 if (j->user)
1190 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001191 if (j->chrootdir)
1192 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001193 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001194}