blob: cea3088f5adae2cd9a90a7861a568a465809df24 [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
Mike Frysinger3adfef72013-05-09 17:19:08 -0400600/*
601 * We specifically do not use cap_valid() as that only tells us the last
602 * valid cap we were *compiled* against (i.e. what the version of kernel
603 * headers says). If we run on a different kernel version, then it's not
604 * uncommon for that to be less (if an older kernel) or more (if a newer
605 * kernel). So suck up the answer via /proc.
606 */
607static int run_cap_valid(unsigned int cap)
608{
609 static unsigned int last_cap;
610
611 if (!last_cap) {
612 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
613 FILE *fp = fopen(cap_file, "re");
614 if (fscanf(fp, "%u", &last_cap) != 1)
615 pdie("fscanf(%s)", cap_file);
616 fclose(fp);
617 }
618
619 return cap <= last_cap;
620}
621
Will Drewry6ac91122011-10-21 16:38:58 -0500622void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400623{
624 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800625 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800626 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400627 unsigned int i;
628 if (!caps)
629 die("can't get process caps");
630 if (cap_clear_flag(caps, CAP_INHERITABLE))
631 die("can't clear inheritable caps");
632 if (cap_clear_flag(caps, CAP_EFFECTIVE))
633 die("can't clear effective caps");
634 if (cap_clear_flag(caps, CAP_PERMITTED))
635 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400636 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800637 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800638 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400639 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800640 flag[0] = i;
641 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400642 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800643 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400644 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800645 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400646 die("can't add inheritable cap");
647 }
648 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800649 die("can't apply initial cleaned capset");
650
651 /*
652 * Instead of dropping bounding set first, do it here in case
653 * the caller had a more permissive bounding set which could
654 * have been used above to raise a capability that wasn't already
655 * present. This requires CAP_SETPCAP, so we raised/kept it above.
656 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400657 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800658 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400659 continue;
660 if (prctl(PR_CAPBSET_DROP, i))
661 pdie("prctl(PR_CAPBSET_DROP)");
662 }
Kees Cook323878a2013-02-05 15:35:24 -0800663
664 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800665 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800666 flag[0] = CAP_SETPCAP;
667 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
668 die("can't clear effective cap");
669 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
670 die("can't clear permitted cap");
671 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
672 die("can't clear inheritable cap");
673 }
674
675 if (cap_set_proc(caps))
676 die("can't apply final cleaned capset");
677
678 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400679}
680
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700681void set_seccomp_filter(const struct minijail *j)
682{
683 /*
684 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
685 * in the kernel source tree for an explanation of the parameters.
686 */
687 if (j->flags.no_new_privs) {
688 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
689 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
690 }
691
692 /*
693 * If we're logging seccomp filter failures,
694 * install the SIGSYS handler first.
695 */
696 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
697 if (install_sigsys_handler())
698 pdie("install SIGSYS handler");
699 warn("logging seccomp filter failures");
700 }
701
702 /*
703 * Install the syscall filter.
704 */
705 if (j->flags.seccomp_filter) {
706 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog))
707 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
708 }
709}
710
Will Drewry6ac91122011-10-21 16:38:58 -0500711void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400712{
713 if (j->flags.pids)
714 die("tried to enter a pid-namespaced jail;"
715 "try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400716
Elly Jonese1749eb2011-10-07 13:54:59 -0400717 if (j->flags.usergroups && !j->user)
718 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400719
Elly Jonesdd3e8512012-01-23 15:13:38 -0500720 /*
721 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400722 * so we don't even try. If any of our operations fail, we abort() the
723 * entire process.
724 */
725 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400726 pdie("unshare(vfs)");
727
728 if (j->flags.net && unshare(CLONE_NEWNET))
729 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400730
Elly Jones51a5b6c2011-10-12 19:09:26 -0400731 if (j->flags.chroot && enter_chroot(j))
732 pdie("chroot");
733
Elly Jonese1749eb2011-10-07 13:54:59 -0400734 if (j->flags.readonly && remount_readonly())
735 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400736
Elly Jonese1749eb2011-10-07 13:54:59 -0400737 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500738 /*
739 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400740 * capability to change uids, our attempt to use setuid()
741 * below will fail. Hang on to root caps across setuid(), then
742 * lock securebits.
743 */
744 if (prctl(PR_SET_KEEPCAPS, 1))
745 pdie("prctl(PR_SET_KEEPCAPS)");
746 if (prctl
747 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
748 pdie("prctl(PR_SET_SECUREBITS)");
749 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400750
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700751 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700752 * If we're setting no_new_privs, we can drop privileges
753 * before setting seccomp filter. This way filter policies
754 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700755 */
756 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700757 drop_ugid(j);
758 if (j->flags.caps)
759 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700760
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700761 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400762 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700763 /*
764 * If we're not setting no_new_privs,
765 * we need to set seccomp filter *before* dropping privileges.
766 * WARNING: this means that filter policies *must* allow
767 * setgroups()/setresgid()/setresuid() for dropping root and
768 * capget()/capset()/prctl() for dropping caps.
769 */
770 set_seccomp_filter(j);
771
772 drop_ugid(j);
773 if (j->flags.caps)
774 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400775 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400776
Elly Jonesdd3e8512012-01-23 15:13:38 -0500777 /*
778 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400779 * privilege-dropping syscalls :)
780 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400781 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1))
782 pdie("prctl(PR_SET_SECCOMP)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400783}
784
Will Drewry6ac91122011-10-21 16:38:58 -0500785/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400786static int init_exitstatus = 0;
787
Will Drewry6ac91122011-10-21 16:38:58 -0500788void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400789{
790 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400791}
792
Will Drewry6ac91122011-10-21 16:38:58 -0500793int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400794{
795 pid_t pid;
796 int status;
797 /* so that we exit with the right status */
798 signal(SIGTERM, init_term);
799 /* TODO(wad) self jail with seccomp_filters here. */
800 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500801 /*
802 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -0400803 * left inside our pid namespace or we get a signal.
804 */
805 if (pid == rootpid)
806 init_exitstatus = status;
807 }
808 if (!WIFEXITED(init_exitstatus))
809 _exit(MINIJAIL_ERR_INIT);
810 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -0400811}
812
Will Drewry6ac91122011-10-21 16:38:58 -0500813int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400814{
815 size_t sz = 0;
816 size_t bytes = read(fd, &sz, sizeof(sz));
817 char *buf;
818 int r;
819 if (sizeof(sz) != bytes)
820 return -EINVAL;
821 if (sz > USHRT_MAX) /* Arbitrary sanity check */
822 return -E2BIG;
823 buf = malloc(sz);
824 if (!buf)
825 return -ENOMEM;
826 bytes = read(fd, buf, sz);
827 if (bytes != sz) {
828 free(buf);
829 return -EINVAL;
830 }
831 r = minijail_unmarshal(j, buf, sz);
832 free(buf);
833 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500834}
835
Will Drewry6ac91122011-10-21 16:38:58 -0500836int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400837{
838 char *buf;
839 size_t sz = minijail_size(j);
840 ssize_t written;
841 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400842
Elly Jonese1749eb2011-10-07 13:54:59 -0400843 if (!sz)
844 return -EINVAL;
845 buf = malloc(sz);
846 r = minijail_marshal(j, buf, sz);
847 if (r) {
848 free(buf);
849 return r;
850 }
851 /* Sends [size][minijail]. */
852 written = write(fd, &sz, sizeof(sz));
853 if (written != sizeof(sz)) {
854 free(buf);
855 return -EFAULT;
856 }
857 written = write(fd, buf, sz);
858 if (written < 0 || (size_t) written != sz) {
859 free(buf);
860 return -EFAULT;
861 }
862 free(buf);
863 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500864}
Elly Jonescd7a9042011-07-22 13:56:51 -0400865
Will Drewry6ac91122011-10-21 16:38:58 -0500866int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400867{
868 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
869 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
870 if (!newenv)
871 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400872
Elly Jonese1749eb2011-10-07 13:54:59 -0400873 /* Only insert a separating space if we have something to separate... */
874 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
875 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400876
Elly Jonese1749eb2011-10-07 13:54:59 -0400877 /* setenv() makes a copy of the string we give it */
878 setenv(kLdPreloadEnvVar, newenv, 1);
879 free(newenv);
880 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400881}
882
Will Drewry6ac91122011-10-21 16:38:58 -0500883int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400884{
885 int r = pipe(fds);
886 char fd_buf[11];
887 if (r)
888 return r;
889 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
890 if (r <= 0)
891 return -EINVAL;
892 setenv(kFdEnvVar, fd_buf, 1);
893 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500894}
895
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800896int setup_pipe_end(int fds[2], size_t index)
897{
898 if (index > 1)
899 return -1;
900
901 close(fds[1 - index]);
902 return fds[index];
903}
904
905int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
906{
907 if (index > 1)
908 return -1;
909
910 close(fds[1 - index]);
911 /* dup2(2) the corresponding end of the pipe into |fd|. */
912 return dup2(fds[index], fd);
913}
914
Will Drewry6ac91122011-10-21 16:38:58 -0500915int API minijail_run(struct minijail *j, const char *filename,
916 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -0400917{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800918 return minijail_run_pid_pipes(j, filename, argv,
919 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700920}
921
922int API minijail_run_pid(struct minijail *j, const char *filename,
923 char *const argv[], pid_t *pchild_pid)
924{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800925 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
926 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700927}
928
929int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -0700930 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700931{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800932 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
933 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700934}
935
936int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -0700937 char *const argv[], pid_t *pchild_pid,
938 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700939{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800940 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
941 NULL, NULL);
942}
943
944int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
945 char *const argv[], pid_t *pchild_pid,
946 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
947{
Elly Jonese1749eb2011-10-07 13:54:59 -0400948 char *oldenv, *oldenv_copy = NULL;
949 pid_t child_pid;
950 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700951 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800952 int stdout_fds[2];
953 int stderr_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -0400954 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -0400955 /* We need to remember this across the minijail_preexec() call. */
956 int pid_namespace = j->flags.pids;
Ben Chan541c7e52011-08-26 14:55:53 -0700957
Elly Jonese1749eb2011-10-07 13:54:59 -0400958 oldenv = getenv(kLdPreloadEnvVar);
959 if (oldenv) {
960 oldenv_copy = strdup(oldenv);
961 if (!oldenv_copy)
962 return -ENOMEM;
963 }
Will Drewryf89aef52011-09-16 16:48:57 -0500964
Elly Jonese1749eb2011-10-07 13:54:59 -0400965 if (setup_preload())
966 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500967
Elly Jonesdd3e8512012-01-23 15:13:38 -0500968 /*
969 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -0400970 * a pipe(2) to send the minijail configuration over.
971 */
972 if (setup_pipe(pipe_fds))
973 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -0400974
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700975 /*
976 * If we want to write to the child process' standard input,
977 * create the pipe(2) now.
978 */
979 if (pstdin_fd) {
980 if (pipe(stdin_fds))
981 return -EFAULT;
982 }
983
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800984 /*
985 * If we want to read from the child process' standard output,
986 * create the pipe(2) now.
987 */
988 if (pstdout_fd) {
989 if (pipe(stdout_fds))
990 return -EFAULT;
991 }
992
993 /*
994 * If we want to read from the child process' standard error,
995 * create the pipe(2) now.
996 */
997 if (pstderr_fd) {
998 if (pipe(stderr_fds))
999 return -EFAULT;
1000 }
1001
Elly Jones761b7412012-06-13 15:49:52 -04001002 /* Use sys_clone() if and only if we're creating a pid namespace.
1003 *
1004 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1005 *
1006 * In multithreaded programs, there are a bunch of locks inside libc,
1007 * some of which may be held by other threads at the time that we call
1008 * minijail_run_pid(). If we call fork(), glibc does its level best to
1009 * ensure that we hold all of these locks before it calls clone()
1010 * internally and drop them after clone() returns, but when we call
1011 * sys_clone(2) directly, all that gets bypassed and we end up with a
1012 * child address space where some of libc's important locks are held by
1013 * other threads (which did not get cloned, and hence will never release
1014 * those locks). This is okay so long as we call exec() immediately
1015 * after, but a bunch of seemingly-innocent libc functions like setenv()
1016 * take locks.
1017 *
1018 * Hence, only call sys_clone() if we need to, in order to get at pid
1019 * namespacing. If we follow this path, the child's address space might
1020 * have broken locks; you may only call functions that do not acquire
1021 * any locks.
1022 *
1023 * Unfortunately, fork() acquires every lock it can get its hands on, as
1024 * previously detailed, so this function is highly likely to deadlock
1025 * later on (see "deadlock here") if we're multithreaded.
1026 *
1027 * We might hack around this by having the clone()d child (init of the
1028 * pid namespace) return directly, rather than leaving the clone()d
1029 * process hanging around to be init for the new namespace (and having
1030 * its fork()ed child return in turn), but that process would be crippled
1031 * with its libc locks potentially broken. We might try fork()ing in the
1032 * parent before we clone() to ensure that we own all the locks, but
1033 * then we have to have the forked child hanging around consuming
1034 * resources (and possibly having file descriptors / shared memory
1035 * regions / etc attached). We'd need to keep the child around to avoid
1036 * having its children get reparented to init.
1037 *
1038 * TODO(ellyjones): figure out if the "forked child hanging around"
1039 * problem is fixable or not. It would be nice if we worked in this
1040 * case.
1041 */
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001042 if (pid_namespace)
Elly Jones761b7412012-06-13 15:49:52 -04001043 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1044 else
1045 child_pid = fork();
1046
Elly Jonese1749eb2011-10-07 13:54:59 -04001047 if (child_pid < 0) {
1048 free(oldenv_copy);
1049 return child_pid;
1050 }
Will Drewryf89aef52011-09-16 16:48:57 -05001051
Elly Jonese1749eb2011-10-07 13:54:59 -04001052 if (child_pid) {
1053 /* Restore parent's LD_PRELOAD. */
1054 if (oldenv_copy) {
1055 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1056 free(oldenv_copy);
1057 } else {
1058 unsetenv(kLdPreloadEnvVar);
1059 }
1060 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001061
Elly Jonese1749eb2011-10-07 13:54:59 -04001062 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001063
1064 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001065 close(pipe_fds[0]); /* read endpoint */
1066 ret = minijail_to_fd(j, pipe_fds[1]);
1067 close(pipe_fds[1]); /* write endpoint */
1068 if (ret) {
1069 kill(j->initpid, SIGKILL);
1070 die("failed to send marshalled minijail");
1071 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001072
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001073 if (pchild_pid)
1074 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001075
1076 /*
1077 * If we want to write to the child process' standard input,
1078 * set up the write end of the pipe.
1079 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001080 if (pstdin_fd)
1081 *pstdin_fd = setup_pipe_end(stdin_fds,
1082 1 /* write end */);
1083
1084 /*
1085 * If we want to read from the child process' standard output,
1086 * set up the read end of the pipe.
1087 */
1088 if (pstdout_fd)
1089 *pstdout_fd = setup_pipe_end(stdout_fds,
1090 0 /* read end */);
1091
1092 /*
1093 * If we want to read from the child process' standard error,
1094 * set up the read end of the pipe.
1095 */
1096 if (pstderr_fd)
1097 *pstderr_fd = setup_pipe_end(stderr_fds,
1098 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001099
Elly Jonese1749eb2011-10-07 13:54:59 -04001100 return 0;
1101 }
1102 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001103
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001104 /*
1105 * If we want to write to the jailed process' standard input,
1106 * set up the read end of the pipe.
1107 */
1108 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001109 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1110 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001111 die("failed to set up stdin pipe");
1112 }
1113
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001114 /*
1115 * If we want to read from the jailed process' standard output,
1116 * set up the write end of the pipe.
1117 */
1118 if (pstdout_fd) {
1119 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1120 STDOUT_FILENO) < 0)
1121 die("failed to set up stdout pipe");
1122 }
1123
1124 /*
1125 * If we want to read from the jailed process' standard error,
1126 * set up the write end of the pipe.
1127 */
1128 if (pstderr_fd) {
1129 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1130 STDERR_FILENO) < 0)
1131 die("failed to set up stderr pipe");
1132 }
1133
Elly Jonese1749eb2011-10-07 13:54:59 -04001134 /* Drop everything that cannot be inherited across execve. */
1135 minijail_preexec(j);
1136 /* Jail this process and its descendants... */
1137 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001138
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001139 if (pid_namespace) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001140 /*
1141 * pid namespace: this process will become init inside the new
Elly Jonese1749eb2011-10-07 13:54:59 -04001142 * namespace, so fork off a child to actually run the program
1143 * (we don't want all programs we might exec to have to know
1144 * how to be init).
Elly Jones761b7412012-06-13 15:49:52 -04001145 *
1146 * If we're multithreaded, we'll probably deadlock here. See
1147 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001148 */
1149 child_pid = fork();
1150 if (child_pid < 0)
1151 _exit(child_pid);
1152 else if (child_pid > 0)
1153 init(child_pid); /* never returns */
1154 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001155
Elly Jonesdd3e8512012-01-23 15:13:38 -05001156 /*
1157 * If we aren't pid-namespaced:
Elly Jonese1749eb2011-10-07 13:54:59 -04001158 * calling process
1159 * -> execve()-ing process
1160 * If we are:
1161 * calling process
1162 * -> init()-ing process
1163 * -> execve()-ing process
1164 */
1165 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001166}
1167
Will Drewry6ac91122011-10-21 16:38:58 -05001168int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001169{
1170 int st;
1171 if (kill(j->initpid, SIGTERM))
1172 return -errno;
1173 if (waitpid(j->initpid, &st, 0) < 0)
1174 return -errno;
1175 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001176}
1177
Will Drewry6ac91122011-10-21 16:38:58 -05001178int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001179{
1180 int st;
1181 if (waitpid(j->initpid, &st, 0) < 0)
1182 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001183
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001184 if (!WIFEXITED(st)) {
1185 if (WIFSIGNALED(st))
mukesh agrawalc420a262013-06-11 17:22:42 -07001186 warn("child process %d received signal %d",
1187 j->initpid, WTERMSIG(st));
Elly Jonese1749eb2011-10-07 13:54:59 -04001188 return MINIJAIL_ERR_JAIL;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001189 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001190
1191 int exit_status = WEXITSTATUS(st);
1192 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001193 info("child process %d exited with status %d",
1194 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001195
1196 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001197}
1198
Will Drewry6ac91122011-10-21 16:38:58 -05001199void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001200{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001201 if (j->flags.seccomp_filter && j->filter_prog) {
1202 free(j->filter_prog->filter);
1203 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001204 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001205 while (j->bindings_head) {
1206 struct binding *b = j->bindings_head;
1207 j->bindings_head = j->bindings_head->next;
1208 free(b->dest);
1209 free(b->src);
1210 free(b);
1211 }
1212 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001213 if (j->user)
1214 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001215 if (j->chrootdir)
1216 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001217 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001218}