blob: c764f8185a22425b29c385ad6894ac2a701c4405 [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 {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070069 /*
70 * WARNING: if you add a flag here you need to make sure it's accounted for
71 * in minijail_pre{enter|exec}() below.
72 */
Elly Jonese1749eb2011-10-07 13:54:59 -040073 struct {
74 int uid:1;
75 int gid:1;
76 int caps:1;
77 int vfs:1;
78 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040079 int net:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040080 int seccomp:1;
81 int readonly:1;
82 int usergroups:1;
83 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070084 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040085 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070086 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040087 int chroot:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040088 } flags;
89 uid_t uid;
90 gid_t gid;
91 gid_t usergid;
92 char *user;
93 uint64_t caps;
94 pid_t initpid;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080095 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -040096 int binding_count;
97 char *chrootdir;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080098 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -040099 struct binding *bindings_head;
100 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500101};
102
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700103/*
104 * Strip out flags meant for the parent.
105 * We keep things that are not inherited across execve(2) (e.g. capabilities),
106 * or are easier to set after execve(2) (e.g. seccomp filters).
107 */
108void minijail_preenter(struct minijail *j)
109{
110 j->flags.vfs = 0;
111 j->flags.readonly = 0;
112 j->flags.pids = 0;
113}
114
115/*
116 * Strip out flags meant for the child.
117 * We keep things that are inherited across execve(2).
118 */
119void minijail_preexec(struct minijail *j)
120{
121 int vfs = j->flags.vfs;
122 int readonly = j->flags.readonly;
123 if (j->user)
124 free(j->user);
125 j->user = NULL;
126 memset(&j->flags, 0, sizeof(j->flags));
127 /* Now restore anything we meant to keep. */
128 j->flags.vfs = vfs;
129 j->flags.readonly = readonly;
130 /* Note, |pids| will already have been used before this call. */
131}
132
133/* Minijail API. */
134
Will Drewry6ac91122011-10-21 16:38:58 -0500135struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400136{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400137 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400138}
139
Will Drewry6ac91122011-10-21 16:38:58 -0500140void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400141{
142 if (uid == 0)
143 die("useless change to uid 0");
144 j->uid = uid;
145 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400146}
147
Will Drewry6ac91122011-10-21 16:38:58 -0500148void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400149{
150 if (gid == 0)
151 die("useless change to gid 0");
152 j->gid = gid;
153 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400154}
155
Will Drewry6ac91122011-10-21 16:38:58 -0500156int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400157{
158 char *buf = NULL;
159 struct passwd pw;
160 struct passwd *ppw = NULL;
161 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
162 if (sz == -1)
163 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400164
Elly Jonesdd3e8512012-01-23 15:13:38 -0500165 /*
166 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400167 * the maximum needed size of the buffer, so we don't have to search.
168 */
169 buf = malloc(sz);
170 if (!buf)
171 return -ENOMEM;
172 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500173 /*
174 * We're safe to free the buffer here. The strings inside pw point
175 * inside buf, but we don't use any of them; this leaves the pointers
176 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
177 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400178 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700179 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400180 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700181 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400182 minijail_change_uid(j, ppw->pw_uid);
183 j->user = strdup(user);
184 if (!j->user)
185 return -ENOMEM;
186 j->usergid = ppw->pw_gid;
187 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400188}
189
Will Drewry6ac91122011-10-21 16:38:58 -0500190int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400191{
192 char *buf = NULL;
193 struct group gr;
194 struct group *pgr = NULL;
195 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
196 if (sz == -1)
197 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400198
Elly Jonesdd3e8512012-01-23 15:13:38 -0500199 /*
200 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400201 * the maximum needed size of the buffer, so we don't have to search.
202 */
203 buf = malloc(sz);
204 if (!buf)
205 return -ENOMEM;
206 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500207 /*
208 * We're safe to free the buffer here. The strings inside gr point
209 * inside buf, but we don't use any of them; this leaves the pointers
210 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
211 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400212 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700213 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400214 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700215 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400216 minijail_change_gid(j, pgr->gr_gid);
217 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400218}
219
Will Drewry6ac91122011-10-21 16:38:58 -0500220void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400221{
222 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400223}
224
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700225void API minijail_no_new_privs(struct minijail *j)
226{
227 j->flags.no_new_privs = 1;
228}
229
Will Drewry6ac91122011-10-21 16:38:58 -0500230void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400231{
232 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500233}
234
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700235void API minijail_log_seccomp_filter_failures(struct minijail *j)
236{
237 j->flags.log_seccomp_filter = 1;
238}
239
Will Drewry6ac91122011-10-21 16:38:58 -0500240void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400241{
242 j->caps = capmask;
243 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400244}
245
Will Drewry6ac91122011-10-21 16:38:58 -0500246void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400247{
248 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400249}
250
Will Drewry6ac91122011-10-21 16:38:58 -0500251void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400252{
Elly Jonese58176c2012-01-23 11:46:17 -0500253 j->flags.vfs = 1;
254 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400255 j->flags.pids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400256}
257
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400258void API minijail_namespace_net(struct minijail *j)
259{
260 j->flags.net = 1;
261}
262
Will Drewry6ac91122011-10-21 16:38:58 -0500263void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400264{
265 j->flags.vfs = 1;
266 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400267}
268
Will Drewry6ac91122011-10-21 16:38:58 -0500269void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400270{
271 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400272}
273
Will Drewry6ac91122011-10-21 16:38:58 -0500274void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400275{
276 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400277}
278
Will Drewry6ac91122011-10-21 16:38:58 -0500279int API minijail_enter_chroot(struct minijail *j, const char *dir) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400280 if (j->chrootdir)
281 return -EINVAL;
282 j->chrootdir = strdup(dir);
283 if (!j->chrootdir)
284 return -ENOMEM;
285 j->flags.chroot = 1;
286 return 0;
287}
288
Will Drewry6ac91122011-10-21 16:38:58 -0500289int API minijail_bind(struct minijail *j, const char *src, const char *dest,
290 int writeable) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400291 struct binding *b;
292
293 if (*dest != '/')
294 return -EINVAL;
295 b = calloc(1, sizeof(*b));
296 if (!b)
297 return -ENOMEM;
298 b->dest = strdup(dest);
299 if (!b->dest)
300 goto error;
301 b->src = strdup(src);
302 if (!b->src)
303 goto error;
304 b->writeable = writeable;
305
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700306 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400307
Elly Jonesdd3e8512012-01-23 15:13:38 -0500308 /*
309 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400310 * containing vfs namespace.
311 */
312 minijail_namespace_vfs(j);
313
314 if (j->bindings_tail)
315 j->bindings_tail->next = b;
316 else
317 j->bindings_head = b;
318 j->bindings_tail = b;
319 j->binding_count++;
320
321 return 0;
322
323error:
324 free(b->src);
325 free(b->dest);
326 free(b);
327 return -ENOMEM;
328}
329
Will Drewry6ac91122011-10-21 16:38:58 -0500330void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400331{
332 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800333 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700334 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400335 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800336
337 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700338 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
339 die("failed to compile seccomp filter BPF program in '%s'",
340 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800341 }
342
343 j->filter_len = fprog->len;
344 j->filter_prog = fprog;
345
Elly Jonese1749eb2011-10-07 13:54:59 -0400346 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500347}
348
Will Drewryf89aef52011-09-16 16:48:57 -0500349struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400350 size_t available;
351 size_t total;
352 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500353};
354
Will Drewry6ac91122011-10-21 16:38:58 -0500355void marshal_state_init(struct marshal_state *state,
356 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400357{
358 state->available = available;
359 state->buf = buf;
360 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500361}
362
Will Drewry6ac91122011-10-21 16:38:58 -0500363void marshal_append(struct marshal_state *state,
364 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400365{
366 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500367
Elly Jonese1749eb2011-10-07 13:54:59 -0400368 /* Up to |available| will be written. */
369 if (copy_len) {
370 memcpy(state->buf, src, copy_len);
371 state->buf += copy_len;
372 state->available -= copy_len;
373 }
374 /* |total| will contain the expected length. */
375 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500376}
377
Will Drewry6ac91122011-10-21 16:38:58 -0500378void minijail_marshal_helper(struct marshal_state *state,
379 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400380{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400381 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400382 marshal_append(state, (char *)j, sizeof(*j));
383 if (j->user)
384 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400385 if (j->chrootdir)
386 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800387 if (j->flags.seccomp_filter && j->filter_prog) {
388 struct sock_fprog *fp = j->filter_prog;
389 marshal_append(state, (char *)fp->filter,
390 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400391 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400392 for (b = j->bindings_head; b; b = b->next) {
393 marshal_append(state, b->src, strlen(b->src) + 1);
394 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700395 marshal_append(state, (char *)&b->writeable,
396 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400397 }
Will Drewryf89aef52011-09-16 16:48:57 -0500398}
399
Will Drewry6ac91122011-10-21 16:38:58 -0500400size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400401{
402 struct marshal_state state;
403 marshal_state_init(&state, NULL, 0);
404 minijail_marshal_helper(&state, j);
405 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500406}
407
Elly Jonese1749eb2011-10-07 13:54:59 -0400408int minijail_marshal(const struct minijail *j, char *buf, size_t available)
409{
410 struct marshal_state state;
411 marshal_state_init(&state, buf, available);
412 minijail_marshal_helper(&state, j);
413 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500414}
415
Elly Jones51a5b6c2011-10-12 19:09:26 -0400416/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
417 * @length Number of bytes to consume
418 * @buf Buffer to consume from
419 * @buflength Size of @buf
420 *
421 * Returns a pointer to the base of the bytes, or NULL for errors.
422 */
Will Drewry6ac91122011-10-21 16:38:58 -0500423void *consumebytes(size_t length, char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400424 char *p = *buf;
425 if (length > *buflength)
426 return NULL;
427 *buf += length;
428 *buflength -= length;
429 return p;
430}
431
432/* consumestr: consumes a C string from a buffer @buf of length @length
433 * @buf Buffer to consume
434 * @length Length of buffer
435 *
436 * Returns a pointer to the base of the string, or NULL for errors.
437 */
Will Drewry6ac91122011-10-21 16:38:58 -0500438char *consumestr(char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400439 size_t len = strnlen(*buf, *buflength);
440 if (len == *buflength)
441 /* There's no null-terminator */
442 return NULL;
443 return consumebytes(len + 1, buf, buflength);
444}
445
Elly Jonese1749eb2011-10-07 13:54:59 -0400446int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
447{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400448 int i;
449 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500450 int ret = -EINVAL;
451
Elly Jonese1749eb2011-10-07 13:54:59 -0400452 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500453 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400454 memcpy((void *)j, serialized, sizeof(*j));
455 serialized += sizeof(*j);
456 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500457
Will Drewrybee7ba72011-10-21 20:47:01 -0500458 /* Potentially stale pointers not used as signals. */
459 j->bindings_head = NULL;
460 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800461 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500462
Elly Jonese1749eb2011-10-07 13:54:59 -0400463 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400464 char *user = consumestr(&serialized, &length);
465 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500466 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400467 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500468 if (!j->user)
469 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400470 }
Will Drewryf89aef52011-09-16 16:48:57 -0500471
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400472 if (j->chrootdir) { /* stale pointer */
473 char *chrootdir = consumestr(&serialized, &length);
474 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500475 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400476 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500477 if (!j->chrootdir)
478 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400479 }
480
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800481 if (j->flags.seccomp_filter && j->filter_len > 0) {
482 size_t ninstrs = j->filter_len;
483 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
484 ninstrs > USHRT_MAX)
485 goto bad_filters;
486
487 size_t program_len = ninstrs * sizeof(struct sock_filter);
488 void *program = consumebytes(program_len, &serialized, &length);
489 if (!program)
490 goto bad_filters;
491
492 j->filter_prog = malloc(sizeof(struct sock_fprog));
493 j->filter_prog->len = ninstrs;
494 j->filter_prog->filter = malloc(program_len);
495 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400496 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400497
498 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400499 j->binding_count = 0;
500 for (i = 0; i < count; ++i) {
501 int *writeable;
502 const char *dest;
503 const char *src = consumestr(&serialized, &length);
504 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500505 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400506 dest = consumestr(&serialized, &length);
507 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500508 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400509 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
510 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500511 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400512 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500513 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400514 }
515
Elly Jonese1749eb2011-10-07 13:54:59 -0400516 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500517
518bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800519 if (j->flags.seccomp_filter && j->filter_len > 0) {
520 free(j->filter_prog->filter);
521 free(j->filter_prog);
522 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500523bad_filters:
524 if (j->chrootdir)
525 free(j->chrootdir);
526bad_chrootdir:
527 if (j->user)
528 free(j->user);
529clear_pointers:
530 j->user = NULL;
531 j->chrootdir = NULL;
532out:
533 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500534}
535
Elly Jones51a5b6c2011-10-12 19:09:26 -0400536/* bind_one: Applies bindings from @b for @j, recursing as needed.
537 * @j Minijail these bindings are for
538 * @b Head of list of bindings
539 *
540 * Returns 0 for success.
541 */
Will Drewry6ac91122011-10-21 16:38:58 -0500542int bind_one(const struct minijail *j, struct binding *b) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400543 int ret = 0;
544 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400545 if (ret)
546 return ret;
547 /* dest has a leading "/" */
548 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
549 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500550 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400551 if (ret)
552 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500553 if (!b->writeable) {
554 ret = mount(b->src, dest, NULL,
555 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
556 if (ret)
557 pdie("bind ro: %s -> %s", b->src, dest);
558 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400559 free(dest);
560 if (b->next)
561 return bind_one(j, b->next);
562 return ret;
563}
564
Will Drewry6ac91122011-10-21 16:38:58 -0500565int enter_chroot(const struct minijail *j) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400566 int ret;
567 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
568 return ret;
569
570 if (chroot(j->chrootdir))
571 return -errno;
572
573 if (chdir("/"))
574 return -errno;
575
576 return 0;
577}
578
Will Drewry6ac91122011-10-21 16:38:58 -0500579int remount_readonly(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400580{
581 const char *kProcPath = "/proc";
582 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500583 /*
584 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400585 * /proc in our namespace, which means using MS_REMOUNT here would
586 * mutate our parent's mount as well, even though we're in a VFS
587 * namespace (!). Instead, remove their mount from our namespace
588 * and make our own.
589 */
590 if (umount(kProcPath))
591 return -errno;
592 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
593 return -errno;
594 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400595}
596
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700597void drop_ugid(const struct minijail *j)
598{
599 if (j->flags.usergroups) {
600 if (initgroups(j->user, j->usergid))
601 pdie("initgroups");
602 } else {
603 /* Only attempt to clear supplemental groups if we are changing
604 * users. */
605 if ((j->uid || j->gid) && setgroups(0, NULL))
606 pdie("setgroups");
607 }
608
609 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
610 pdie("setresgid");
611
612 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
613 pdie("setresuid");
614}
615
Mike Frysinger3adfef72013-05-09 17:19:08 -0400616/*
617 * We specifically do not use cap_valid() as that only tells us the last
618 * valid cap we were *compiled* against (i.e. what the version of kernel
619 * headers says). If we run on a different kernel version, then it's not
620 * uncommon for that to be less (if an older kernel) or more (if a newer
621 * kernel). So suck up the answer via /proc.
622 */
623static int run_cap_valid(unsigned int cap)
624{
625 static unsigned int last_cap;
626
627 if (!last_cap) {
628 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
629 FILE *fp = fopen(cap_file, "re");
630 if (fscanf(fp, "%u", &last_cap) != 1)
631 pdie("fscanf(%s)", cap_file);
632 fclose(fp);
633 }
634
635 return cap <= last_cap;
636}
637
Will Drewry6ac91122011-10-21 16:38:58 -0500638void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400639{
640 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800641 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800642 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400643 unsigned int i;
644 if (!caps)
645 die("can't get process caps");
646 if (cap_clear_flag(caps, CAP_INHERITABLE))
647 die("can't clear inheritable caps");
648 if (cap_clear_flag(caps, CAP_EFFECTIVE))
649 die("can't clear effective caps");
650 if (cap_clear_flag(caps, CAP_PERMITTED))
651 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400652 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800653 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800654 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400655 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800656 flag[0] = i;
657 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400658 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800659 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400660 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800661 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400662 die("can't add inheritable cap");
663 }
664 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800665 die("can't apply initial cleaned capset");
666
667 /*
668 * Instead of dropping bounding set first, do it here in case
669 * the caller had a more permissive bounding set which could
670 * have been used above to raise a capability that wasn't already
671 * present. This requires CAP_SETPCAP, so we raised/kept it above.
672 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400673 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800674 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400675 continue;
676 if (prctl(PR_CAPBSET_DROP, i))
677 pdie("prctl(PR_CAPBSET_DROP)");
678 }
Kees Cook323878a2013-02-05 15:35:24 -0800679
680 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800681 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800682 flag[0] = CAP_SETPCAP;
683 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
684 die("can't clear effective cap");
685 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
686 die("can't clear permitted cap");
687 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
688 die("can't clear inheritable cap");
689 }
690
691 if (cap_set_proc(caps))
692 die("can't apply final cleaned capset");
693
694 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400695}
696
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700697void set_seccomp_filter(const struct minijail *j)
698{
699 /*
700 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
701 * in the kernel source tree for an explanation of the parameters.
702 */
703 if (j->flags.no_new_privs) {
704 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
705 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
706 }
707
708 /*
709 * If we're logging seccomp filter failures,
710 * install the SIGSYS handler first.
711 */
712 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
713 if (install_sigsys_handler())
714 pdie("install SIGSYS handler");
715 warn("logging seccomp filter failures");
716 }
717
718 /*
719 * Install the syscall filter.
720 */
721 if (j->flags.seccomp_filter) {
722 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog))
723 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
724 }
725}
726
Will Drewry6ac91122011-10-21 16:38:58 -0500727void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400728{
729 if (j->flags.pids)
730 die("tried to enter a pid-namespaced jail;"
731 "try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400732
Elly Jonese1749eb2011-10-07 13:54:59 -0400733 if (j->flags.usergroups && !j->user)
734 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400735
Elly Jonesdd3e8512012-01-23 15:13:38 -0500736 /*
737 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400738 * so we don't even try. If any of our operations fail, we abort() the
739 * entire process.
740 */
741 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400742 pdie("unshare(vfs)");
743
744 if (j->flags.net && unshare(CLONE_NEWNET))
745 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400746
Elly Jones51a5b6c2011-10-12 19:09:26 -0400747 if (j->flags.chroot && enter_chroot(j))
748 pdie("chroot");
749
Elly Jonese1749eb2011-10-07 13:54:59 -0400750 if (j->flags.readonly && remount_readonly())
751 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400752
Elly Jonese1749eb2011-10-07 13:54:59 -0400753 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500754 /*
755 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400756 * capability to change uids, our attempt to use setuid()
757 * below will fail. Hang on to root caps across setuid(), then
758 * lock securebits.
759 */
760 if (prctl(PR_SET_KEEPCAPS, 1))
761 pdie("prctl(PR_SET_KEEPCAPS)");
762 if (prctl
763 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
764 pdie("prctl(PR_SET_SECUREBITS)");
765 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400766
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700767 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700768 * If we're setting no_new_privs, we can drop privileges
769 * before setting seccomp filter. This way filter policies
770 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700771 */
772 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700773 drop_ugid(j);
774 if (j->flags.caps)
775 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700776
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700777 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400778 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700779 /*
780 * If we're not setting no_new_privs,
781 * we need to set seccomp filter *before* dropping privileges.
782 * WARNING: this means that filter policies *must* allow
783 * setgroups()/setresgid()/setresuid() for dropping root and
784 * capget()/capset()/prctl() for dropping caps.
785 */
786 set_seccomp_filter(j);
787
788 drop_ugid(j);
789 if (j->flags.caps)
790 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400791 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400792
Elly Jonesdd3e8512012-01-23 15:13:38 -0500793 /*
794 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400795 * privilege-dropping syscalls :)
796 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400797 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1))
798 pdie("prctl(PR_SET_SECCOMP)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400799}
800
Will Drewry6ac91122011-10-21 16:38:58 -0500801/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400802static int init_exitstatus = 0;
803
Will Drewry6ac91122011-10-21 16:38:58 -0500804void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400805{
806 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400807}
808
Will Drewry6ac91122011-10-21 16:38:58 -0500809int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400810{
811 pid_t pid;
812 int status;
813 /* so that we exit with the right status */
814 signal(SIGTERM, init_term);
815 /* TODO(wad) self jail with seccomp_filters here. */
816 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500817 /*
818 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -0400819 * left inside our pid namespace or we get a signal.
820 */
821 if (pid == rootpid)
822 init_exitstatus = status;
823 }
824 if (!WIFEXITED(init_exitstatus))
825 _exit(MINIJAIL_ERR_INIT);
826 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -0400827}
828
Will Drewry6ac91122011-10-21 16:38:58 -0500829int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400830{
831 size_t sz = 0;
832 size_t bytes = read(fd, &sz, sizeof(sz));
833 char *buf;
834 int r;
835 if (sizeof(sz) != bytes)
836 return -EINVAL;
837 if (sz > USHRT_MAX) /* Arbitrary sanity check */
838 return -E2BIG;
839 buf = malloc(sz);
840 if (!buf)
841 return -ENOMEM;
842 bytes = read(fd, buf, sz);
843 if (bytes != sz) {
844 free(buf);
845 return -EINVAL;
846 }
847 r = minijail_unmarshal(j, buf, sz);
848 free(buf);
849 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500850}
851
Will Drewry6ac91122011-10-21 16:38:58 -0500852int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400853{
854 char *buf;
855 size_t sz = minijail_size(j);
856 ssize_t written;
857 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400858
Elly Jonese1749eb2011-10-07 13:54:59 -0400859 if (!sz)
860 return -EINVAL;
861 buf = malloc(sz);
862 r = minijail_marshal(j, buf, sz);
863 if (r) {
864 free(buf);
865 return r;
866 }
867 /* Sends [size][minijail]. */
868 written = write(fd, &sz, sizeof(sz));
869 if (written != sizeof(sz)) {
870 free(buf);
871 return -EFAULT;
872 }
873 written = write(fd, buf, sz);
874 if (written < 0 || (size_t) written != sz) {
875 free(buf);
876 return -EFAULT;
877 }
878 free(buf);
879 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500880}
Elly Jonescd7a9042011-07-22 13:56:51 -0400881
Will Drewry6ac91122011-10-21 16:38:58 -0500882int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400883{
884 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
885 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
886 if (!newenv)
887 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400888
Elly Jonese1749eb2011-10-07 13:54:59 -0400889 /* Only insert a separating space if we have something to separate... */
890 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
891 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400892
Elly Jonese1749eb2011-10-07 13:54:59 -0400893 /* setenv() makes a copy of the string we give it */
894 setenv(kLdPreloadEnvVar, newenv, 1);
895 free(newenv);
896 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400897}
898
Will Drewry6ac91122011-10-21 16:38:58 -0500899int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400900{
901 int r = pipe(fds);
902 char fd_buf[11];
903 if (r)
904 return r;
905 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
906 if (r <= 0)
907 return -EINVAL;
908 setenv(kFdEnvVar, fd_buf, 1);
909 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500910}
911
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800912int setup_pipe_end(int fds[2], size_t index)
913{
914 if (index > 1)
915 return -1;
916
917 close(fds[1 - index]);
918 return fds[index];
919}
920
921int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
922{
923 if (index > 1)
924 return -1;
925
926 close(fds[1 - index]);
927 /* dup2(2) the corresponding end of the pipe into |fd|. */
928 return dup2(fds[index], fd);
929}
930
Will Drewry6ac91122011-10-21 16:38:58 -0500931int API minijail_run(struct minijail *j, const char *filename,
932 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -0400933{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800934 return minijail_run_pid_pipes(j, filename, argv,
935 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700936}
937
938int API minijail_run_pid(struct minijail *j, const char *filename,
939 char *const argv[], pid_t *pchild_pid)
940{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800941 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
942 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700943}
944
945int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -0700946 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700947{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800948 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
949 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700950}
951
952int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -0700953 char *const argv[], pid_t *pchild_pid,
954 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700955{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800956 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
957 NULL, NULL);
958}
959
960int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700961 char *const argv[], pid_t *pchild_pid,
962 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800963{
Elly Jonese1749eb2011-10-07 13:54:59 -0400964 char *oldenv, *oldenv_copy = NULL;
965 pid_t child_pid;
966 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700967 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800968 int stdout_fds[2];
969 int stderr_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -0400970 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -0400971 /* We need to remember this across the minijail_preexec() call. */
972 int pid_namespace = j->flags.pids;
Ben Chan541c7e52011-08-26 14:55:53 -0700973
Elly Jonese1749eb2011-10-07 13:54:59 -0400974 oldenv = getenv(kLdPreloadEnvVar);
975 if (oldenv) {
976 oldenv_copy = strdup(oldenv);
977 if (!oldenv_copy)
978 return -ENOMEM;
979 }
Will Drewryf89aef52011-09-16 16:48:57 -0500980
Elly Jonese1749eb2011-10-07 13:54:59 -0400981 if (setup_preload())
982 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500983
Elly Jonesdd3e8512012-01-23 15:13:38 -0500984 /*
985 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -0400986 * a pipe(2) to send the minijail configuration over.
987 */
988 if (setup_pipe(pipe_fds))
989 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -0400990
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700991 /*
992 * If we want to write to the child process' standard input,
993 * create the pipe(2) now.
994 */
995 if (pstdin_fd) {
996 if (pipe(stdin_fds))
997 return -EFAULT;
998 }
999
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001000 /*
1001 * If we want to read from the child process' standard output,
1002 * create the pipe(2) now.
1003 */
1004 if (pstdout_fd) {
1005 if (pipe(stdout_fds))
1006 return -EFAULT;
1007 }
1008
1009 /*
1010 * If we want to read from the child process' standard error,
1011 * create the pipe(2) now.
1012 */
1013 if (pstderr_fd) {
1014 if (pipe(stderr_fds))
1015 return -EFAULT;
1016 }
1017
Elly Jones761b7412012-06-13 15:49:52 -04001018 /* Use sys_clone() if and only if we're creating a pid namespace.
1019 *
1020 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1021 *
1022 * In multithreaded programs, there are a bunch of locks inside libc,
1023 * some of which may be held by other threads at the time that we call
1024 * minijail_run_pid(). If we call fork(), glibc does its level best to
1025 * ensure that we hold all of these locks before it calls clone()
1026 * internally and drop them after clone() returns, but when we call
1027 * sys_clone(2) directly, all that gets bypassed and we end up with a
1028 * child address space where some of libc's important locks are held by
1029 * other threads (which did not get cloned, and hence will never release
1030 * those locks). This is okay so long as we call exec() immediately
1031 * after, but a bunch of seemingly-innocent libc functions like setenv()
1032 * take locks.
1033 *
1034 * Hence, only call sys_clone() if we need to, in order to get at pid
1035 * namespacing. If we follow this path, the child's address space might
1036 * have broken locks; you may only call functions that do not acquire
1037 * any locks.
1038 *
1039 * Unfortunately, fork() acquires every lock it can get its hands on, as
1040 * previously detailed, so this function is highly likely to deadlock
1041 * later on (see "deadlock here") if we're multithreaded.
1042 *
1043 * We might hack around this by having the clone()d child (init of the
1044 * pid namespace) return directly, rather than leaving the clone()d
1045 * process hanging around to be init for the new namespace (and having
1046 * its fork()ed child return in turn), but that process would be crippled
1047 * with its libc locks potentially broken. We might try fork()ing in the
1048 * parent before we clone() to ensure that we own all the locks, but
1049 * then we have to have the forked child hanging around consuming
1050 * resources (and possibly having file descriptors / shared memory
1051 * regions / etc attached). We'd need to keep the child around to avoid
1052 * having its children get reparented to init.
1053 *
1054 * TODO(ellyjones): figure out if the "forked child hanging around"
1055 * problem is fixable or not. It would be nice if we worked in this
1056 * case.
1057 */
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001058 if (pid_namespace)
Elly Jones761b7412012-06-13 15:49:52 -04001059 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1060 else
1061 child_pid = fork();
1062
Elly Jonese1749eb2011-10-07 13:54:59 -04001063 if (child_pid < 0) {
1064 free(oldenv_copy);
1065 return child_pid;
1066 }
Will Drewryf89aef52011-09-16 16:48:57 -05001067
Elly Jonese1749eb2011-10-07 13:54:59 -04001068 if (child_pid) {
1069 /* Restore parent's LD_PRELOAD. */
1070 if (oldenv_copy) {
1071 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1072 free(oldenv_copy);
1073 } else {
1074 unsetenv(kLdPreloadEnvVar);
1075 }
1076 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001077
Elly Jonese1749eb2011-10-07 13:54:59 -04001078 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001079
1080 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001081 close(pipe_fds[0]); /* read endpoint */
1082 ret = minijail_to_fd(j, pipe_fds[1]);
1083 close(pipe_fds[1]); /* write endpoint */
1084 if (ret) {
1085 kill(j->initpid, SIGKILL);
1086 die("failed to send marshalled minijail");
1087 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001088
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001089 if (pchild_pid)
1090 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001091
1092 /*
1093 * If we want to write to the child process' standard input,
1094 * set up the write end of the pipe.
1095 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001096 if (pstdin_fd)
1097 *pstdin_fd = setup_pipe_end(stdin_fds,
1098 1 /* write end */);
1099
1100 /*
1101 * If we want to read from the child process' standard output,
1102 * set up the read end of the pipe.
1103 */
1104 if (pstdout_fd)
1105 *pstdout_fd = setup_pipe_end(stdout_fds,
1106 0 /* read end */);
1107
1108 /*
1109 * If we want to read from the child process' standard error,
1110 * set up the read end of the pipe.
1111 */
1112 if (pstderr_fd)
1113 *pstderr_fd = setup_pipe_end(stderr_fds,
1114 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001115
Elly Jonese1749eb2011-10-07 13:54:59 -04001116 return 0;
1117 }
1118 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001119
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001120 /*
1121 * If we want to write to the jailed process' standard input,
1122 * set up the read end of the pipe.
1123 */
1124 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001125 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1126 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001127 die("failed to set up stdin pipe");
1128 }
1129
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001130 /*
1131 * If we want to read from the jailed process' standard output,
1132 * set up the write end of the pipe.
1133 */
1134 if (pstdout_fd) {
1135 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1136 STDOUT_FILENO) < 0)
1137 die("failed to set up stdout pipe");
1138 }
1139
1140 /*
1141 * If we want to read from the jailed process' standard error,
1142 * set up the write end of the pipe.
1143 */
1144 if (pstderr_fd) {
1145 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1146 STDERR_FILENO) < 0)
1147 die("failed to set up stderr pipe");
1148 }
1149
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001150 /* Strip out flags that cannot be inherited across execve. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001151 minijail_preexec(j);
1152 /* Jail this process and its descendants... */
1153 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001154
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001155 if (pid_namespace) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001156 /*
1157 * pid namespace: this process will become init inside the new
Elly Jonese1749eb2011-10-07 13:54:59 -04001158 * namespace, so fork off a child to actually run the program
1159 * (we don't want all programs we might exec to have to know
1160 * how to be init).
Elly Jones761b7412012-06-13 15:49:52 -04001161 *
1162 * If we're multithreaded, we'll probably deadlock here. See
1163 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001164 */
1165 child_pid = fork();
1166 if (child_pid < 0)
1167 _exit(child_pid);
1168 else if (child_pid > 0)
1169 init(child_pid); /* never returns */
1170 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001171
Elly Jonesdd3e8512012-01-23 15:13:38 -05001172 /*
1173 * If we aren't pid-namespaced:
Elly Jonese1749eb2011-10-07 13:54:59 -04001174 * calling process
1175 * -> execve()-ing process
1176 * If we are:
1177 * calling process
1178 * -> init()-ing process
1179 * -> execve()-ing process
1180 */
1181 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001182}
1183
Will Drewry6ac91122011-10-21 16:38:58 -05001184int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001185{
1186 int st;
1187 if (kill(j->initpid, SIGTERM))
1188 return -errno;
1189 if (waitpid(j->initpid, &st, 0) < 0)
1190 return -errno;
1191 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001192}
1193
Will Drewry6ac91122011-10-21 16:38:58 -05001194int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001195{
1196 int st;
1197 if (waitpid(j->initpid, &st, 0) < 0)
1198 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001199
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001200 if (!WIFEXITED(st)) {
1201 if (WIFSIGNALED(st))
mukesh agrawalc420a262013-06-11 17:22:42 -07001202 warn("child process %d received signal %d",
1203 j->initpid, WTERMSIG(st));
Elly Jonese1749eb2011-10-07 13:54:59 -04001204 return MINIJAIL_ERR_JAIL;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001205 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001206
1207 int exit_status = WEXITSTATUS(st);
1208 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001209 info("child process %d exited with status %d",
1210 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001211
1212 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001213}
1214
Will Drewry6ac91122011-10-21 16:38:58 -05001215void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001216{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001217 if (j->flags.seccomp_filter && j->filter_prog) {
1218 free(j->filter_prog->filter);
1219 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001220 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001221 while (j->bindings_head) {
1222 struct binding *b = j->bindings_head;
1223 j->bindings_head = j->bindings_head->next;
1224 free(b->dest);
1225 free(b->src);
1226 free(b);
1227 }
1228 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001229 if (j->user)
1230 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001231 if (j->chrootdir)
1232 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001233 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001234}