blob: 8a1300d94040b46d49385e266938cec1305fc8c1 [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);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700143 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400144 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700145 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400146 minijail_change_uid(j, ppw->pw_uid);
147 j->user = strdup(user);
148 if (!j->user)
149 return -ENOMEM;
150 j->usergid = ppw->pw_gid;
151 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400152}
153
Will Drewry6ac91122011-10-21 16:38:58 -0500154int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400155{
156 char *buf = NULL;
157 struct group gr;
158 struct group *pgr = NULL;
159 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
160 if (sz == -1)
161 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400162
Elly Jonesdd3e8512012-01-23 15:13:38 -0500163 /*
164 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400165 * the maximum needed size of the buffer, so we don't have to search.
166 */
167 buf = malloc(sz);
168 if (!buf)
169 return -ENOMEM;
170 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500171 /*
172 * We're safe to free the buffer here. The strings inside gr point
173 * inside buf, but we don't use any of them; this leaves the pointers
174 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
175 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400176 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700177 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400178 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700179 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400180 minijail_change_gid(j, pgr->gr_gid);
181 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400182}
183
Will Drewry6ac91122011-10-21 16:38:58 -0500184void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400185{
186 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400187}
188
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700189void API minijail_no_new_privs(struct minijail *j)
190{
191 j->flags.no_new_privs = 1;
192}
193
Will Drewry6ac91122011-10-21 16:38:58 -0500194void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400195{
196 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500197}
198
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700199void API minijail_log_seccomp_filter_failures(struct minijail *j)
200{
201 j->flags.log_seccomp_filter = 1;
202}
203
Will Drewry6ac91122011-10-21 16:38:58 -0500204void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400205{
206 j->caps = capmask;
207 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400208}
209
Will Drewry6ac91122011-10-21 16:38:58 -0500210void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400211{
212 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400213}
214
Will Drewry6ac91122011-10-21 16:38:58 -0500215void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400216{
Elly Jonese58176c2012-01-23 11:46:17 -0500217 j->flags.vfs = 1;
218 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400219 j->flags.pids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400220}
221
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400222void API minijail_namespace_net(struct minijail *j)
223{
224 j->flags.net = 1;
225}
226
Will Drewry6ac91122011-10-21 16:38:58 -0500227void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400228{
229 j->flags.vfs = 1;
230 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400231}
232
Will Drewry6ac91122011-10-21 16:38:58 -0500233void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400234{
235 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400236}
237
Will Drewry6ac91122011-10-21 16:38:58 -0500238void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400239{
240 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400241}
242
Will Drewry6ac91122011-10-21 16:38:58 -0500243int API minijail_enter_chroot(struct minijail *j, const char *dir) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400244 if (j->chrootdir)
245 return -EINVAL;
246 j->chrootdir = strdup(dir);
247 if (!j->chrootdir)
248 return -ENOMEM;
249 j->flags.chroot = 1;
250 return 0;
251}
252
Will Drewry6ac91122011-10-21 16:38:58 -0500253int API minijail_bind(struct minijail *j, const char *src, const char *dest,
254 int writeable) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400255 struct binding *b;
256
257 if (*dest != '/')
258 return -EINVAL;
259 b = calloc(1, sizeof(*b));
260 if (!b)
261 return -ENOMEM;
262 b->dest = strdup(dest);
263 if (!b->dest)
264 goto error;
265 b->src = strdup(src);
266 if (!b->src)
267 goto error;
268 b->writeable = writeable;
269
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700270 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400271
Elly Jonesdd3e8512012-01-23 15:13:38 -0500272 /*
273 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400274 * containing vfs namespace.
275 */
276 minijail_namespace_vfs(j);
277
278 if (j->bindings_tail)
279 j->bindings_tail->next = b;
280 else
281 j->bindings_head = b;
282 j->bindings_tail = b;
283 j->binding_count++;
284
285 return 0;
286
287error:
288 free(b->src);
289 free(b->dest);
290 free(b);
291 return -ENOMEM;
292}
293
Will Drewry6ac91122011-10-21 16:38:58 -0500294void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400295{
296 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800297 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700298 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400299 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800300
301 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700302 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
303 die("failed to compile seccomp filter BPF program in '%s'",
304 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800305 }
306
307 j->filter_len = fprog->len;
308 j->filter_prog = fprog;
309
Elly Jonese1749eb2011-10-07 13:54:59 -0400310 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500311}
312
Will Drewryf89aef52011-09-16 16:48:57 -0500313struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400314 size_t available;
315 size_t total;
316 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500317};
318
Will Drewry6ac91122011-10-21 16:38:58 -0500319void marshal_state_init(struct marshal_state *state,
320 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400321{
322 state->available = available;
323 state->buf = buf;
324 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500325}
326
Will Drewry6ac91122011-10-21 16:38:58 -0500327void marshal_append(struct marshal_state *state,
328 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400329{
330 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500331
Elly Jonese1749eb2011-10-07 13:54:59 -0400332 /* Up to |available| will be written. */
333 if (copy_len) {
334 memcpy(state->buf, src, copy_len);
335 state->buf += copy_len;
336 state->available -= copy_len;
337 }
338 /* |total| will contain the expected length. */
339 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500340}
341
Will Drewry6ac91122011-10-21 16:38:58 -0500342void minijail_marshal_helper(struct marshal_state *state,
343 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400344{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400345 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400346 marshal_append(state, (char *)j, sizeof(*j));
347 if (j->user)
348 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400349 if (j->chrootdir)
350 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800351 if (j->flags.seccomp_filter && j->filter_prog) {
352 struct sock_fprog *fp = j->filter_prog;
353 marshal_append(state, (char *)fp->filter,
354 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400355 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400356 for (b = j->bindings_head; b; b = b->next) {
357 marshal_append(state, b->src, strlen(b->src) + 1);
358 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700359 marshal_append(state, (char *)&b->writeable,
360 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400361 }
Will Drewryf89aef52011-09-16 16:48:57 -0500362}
363
Will Drewry6ac91122011-10-21 16:38:58 -0500364size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400365{
366 struct marshal_state state;
367 marshal_state_init(&state, NULL, 0);
368 minijail_marshal_helper(&state, j);
369 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500370}
371
Elly Jonese1749eb2011-10-07 13:54:59 -0400372int minijail_marshal(const struct minijail *j, char *buf, size_t available)
373{
374 struct marshal_state state;
375 marshal_state_init(&state, buf, available);
376 minijail_marshal_helper(&state, j);
377 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500378}
379
Elly Jones51a5b6c2011-10-12 19:09:26 -0400380/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
381 * @length Number of bytes to consume
382 * @buf Buffer to consume from
383 * @buflength Size of @buf
384 *
385 * Returns a pointer to the base of the bytes, or NULL for errors.
386 */
Will Drewry6ac91122011-10-21 16:38:58 -0500387void *consumebytes(size_t length, char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400388 char *p = *buf;
389 if (length > *buflength)
390 return NULL;
391 *buf += length;
392 *buflength -= length;
393 return p;
394}
395
396/* consumestr: consumes a C string from a buffer @buf of length @length
397 * @buf Buffer to consume
398 * @length Length of buffer
399 *
400 * Returns a pointer to the base of the string, or NULL for errors.
401 */
Will Drewry6ac91122011-10-21 16:38:58 -0500402char *consumestr(char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400403 size_t len = strnlen(*buf, *buflength);
404 if (len == *buflength)
405 /* There's no null-terminator */
406 return NULL;
407 return consumebytes(len + 1, buf, buflength);
408}
409
Elly Jonese1749eb2011-10-07 13:54:59 -0400410int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
411{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400412 int i;
413 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500414 int ret = -EINVAL;
415
Elly Jonese1749eb2011-10-07 13:54:59 -0400416 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500417 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400418 memcpy((void *)j, serialized, sizeof(*j));
419 serialized += sizeof(*j);
420 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500421
Will Drewrybee7ba72011-10-21 20:47:01 -0500422 /* Potentially stale pointers not used as signals. */
423 j->bindings_head = NULL;
424 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800425 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500426
Elly Jonese1749eb2011-10-07 13:54:59 -0400427 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400428 char *user = consumestr(&serialized, &length);
429 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500430 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400431 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500432 if (!j->user)
433 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400434 }
Will Drewryf89aef52011-09-16 16:48:57 -0500435
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400436 if (j->chrootdir) { /* stale pointer */
437 char *chrootdir = consumestr(&serialized, &length);
438 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500439 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400440 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500441 if (!j->chrootdir)
442 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400443 }
444
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800445 if (j->flags.seccomp_filter && j->filter_len > 0) {
446 size_t ninstrs = j->filter_len;
447 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
448 ninstrs > USHRT_MAX)
449 goto bad_filters;
450
451 size_t program_len = ninstrs * sizeof(struct sock_filter);
452 void *program = consumebytes(program_len, &serialized, &length);
453 if (!program)
454 goto bad_filters;
455
456 j->filter_prog = malloc(sizeof(struct sock_fprog));
457 j->filter_prog->len = ninstrs;
458 j->filter_prog->filter = malloc(program_len);
459 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400460 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400461
462 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400463 j->binding_count = 0;
464 for (i = 0; i < count; ++i) {
465 int *writeable;
466 const char *dest;
467 const char *src = consumestr(&serialized, &length);
468 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500469 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400470 dest = consumestr(&serialized, &length);
471 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500472 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400473 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
474 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500475 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400476 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500477 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400478 }
479
Elly Jonese1749eb2011-10-07 13:54:59 -0400480 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500481
482bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800483 if (j->flags.seccomp_filter && j->filter_len > 0) {
484 free(j->filter_prog->filter);
485 free(j->filter_prog);
486 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500487bad_filters:
488 if (j->chrootdir)
489 free(j->chrootdir);
490bad_chrootdir:
491 if (j->user)
492 free(j->user);
493clear_pointers:
494 j->user = NULL;
495 j->chrootdir = NULL;
496out:
497 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500498}
499
Elly Jonese1749eb2011-10-07 13:54:59 -0400500void minijail_preenter(struct minijail *j)
501{
502 /* Strip out options which are minijail_run() only. */
503 j->flags.vfs = 0;
504 j->flags.readonly = 0;
505 j->flags.pids = 0;
Will Drewryfe4a3722011-09-16 14:50:50 -0500506}
507
Elly Jonese1749eb2011-10-07 13:54:59 -0400508void minijail_preexec(struct minijail *j)
509{
510 int vfs = j->flags.vfs;
511 int readonly = j->flags.readonly;
512 if (j->user)
513 free(j->user);
514 j->user = NULL;
515 memset(&j->flags, 0, sizeof(j->flags));
516 /* Now restore anything we meant to keep. */
517 j->flags.vfs = vfs;
518 j->flags.readonly = readonly;
519 /* Note, pidns will already have been used before this call. */
Will Drewry2ddaad02011-09-16 11:36:08 -0500520}
521
Elly Jones51a5b6c2011-10-12 19:09:26 -0400522/* bind_one: Applies bindings from @b for @j, recursing as needed.
523 * @j Minijail these bindings are for
524 * @b Head of list of bindings
525 *
526 * Returns 0 for success.
527 */
Will Drewry6ac91122011-10-21 16:38:58 -0500528int bind_one(const struct minijail *j, struct binding *b) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400529 int ret = 0;
530 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400531 if (ret)
532 return ret;
533 /* dest has a leading "/" */
534 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
535 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500536 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400537 if (ret)
538 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500539 if (!b->writeable) {
540 ret = mount(b->src, dest, NULL,
541 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
542 if (ret)
543 pdie("bind ro: %s -> %s", b->src, dest);
544 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400545 free(dest);
546 if (b->next)
547 return bind_one(j, b->next);
548 return ret;
549}
550
Will Drewry6ac91122011-10-21 16:38:58 -0500551int enter_chroot(const struct minijail *j) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400552 int ret;
553 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
554 return ret;
555
556 if (chroot(j->chrootdir))
557 return -errno;
558
559 if (chdir("/"))
560 return -errno;
561
562 return 0;
563}
564
Will Drewry6ac91122011-10-21 16:38:58 -0500565int remount_readonly(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400566{
567 const char *kProcPath = "/proc";
568 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500569 /*
570 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400571 * /proc in our namespace, which means using MS_REMOUNT here would
572 * mutate our parent's mount as well, even though we're in a VFS
573 * namespace (!). Instead, remove their mount from our namespace
574 * and make our own.
575 */
576 if (umount(kProcPath))
577 return -errno;
578 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
579 return -errno;
580 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400581}
582
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700583void drop_ugid(const struct minijail *j)
584{
585 if (j->flags.usergroups) {
586 if (initgroups(j->user, j->usergid))
587 pdie("initgroups");
588 } else {
589 /* Only attempt to clear supplemental groups if we are changing
590 * users. */
591 if ((j->uid || j->gid) && setgroups(0, NULL))
592 pdie("setgroups");
593 }
594
595 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
596 pdie("setresgid");
597
598 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
599 pdie("setresuid");
600}
601
Mike Frysinger3adfef72013-05-09 17:19:08 -0400602/*
603 * We specifically do not use cap_valid() as that only tells us the last
604 * valid cap we were *compiled* against (i.e. what the version of kernel
605 * headers says). If we run on a different kernel version, then it's not
606 * uncommon for that to be less (if an older kernel) or more (if a newer
607 * kernel). So suck up the answer via /proc.
608 */
609static int run_cap_valid(unsigned int cap)
610{
611 static unsigned int last_cap;
612
613 if (!last_cap) {
614 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
615 FILE *fp = fopen(cap_file, "re");
616 if (fscanf(fp, "%u", &last_cap) != 1)
617 pdie("fscanf(%s)", cap_file);
618 fclose(fp);
619 }
620
621 return cap <= last_cap;
622}
623
Will Drewry6ac91122011-10-21 16:38:58 -0500624void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400625{
626 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800627 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800628 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400629 unsigned int i;
630 if (!caps)
631 die("can't get process caps");
632 if (cap_clear_flag(caps, CAP_INHERITABLE))
633 die("can't clear inheritable caps");
634 if (cap_clear_flag(caps, CAP_EFFECTIVE))
635 die("can't clear effective caps");
636 if (cap_clear_flag(caps, CAP_PERMITTED))
637 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400638 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800639 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800640 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400641 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800642 flag[0] = i;
643 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400644 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800645 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400646 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800647 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400648 die("can't add inheritable cap");
649 }
650 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800651 die("can't apply initial cleaned capset");
652
653 /*
654 * Instead of dropping bounding set first, do it here in case
655 * the caller had a more permissive bounding set which could
656 * have been used above to raise a capability that wasn't already
657 * present. This requires CAP_SETPCAP, so we raised/kept it above.
658 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400659 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800660 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400661 continue;
662 if (prctl(PR_CAPBSET_DROP, i))
663 pdie("prctl(PR_CAPBSET_DROP)");
664 }
Kees Cook323878a2013-02-05 15:35:24 -0800665
666 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800667 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800668 flag[0] = CAP_SETPCAP;
669 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
670 die("can't clear effective cap");
671 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
672 die("can't clear permitted cap");
673 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
674 die("can't clear inheritable cap");
675 }
676
677 if (cap_set_proc(caps))
678 die("can't apply final cleaned capset");
679
680 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400681}
682
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700683void set_seccomp_filter(const struct minijail *j)
684{
685 /*
686 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
687 * in the kernel source tree for an explanation of the parameters.
688 */
689 if (j->flags.no_new_privs) {
690 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
691 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
692 }
693
694 /*
695 * If we're logging seccomp filter failures,
696 * install the SIGSYS handler first.
697 */
698 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
699 if (install_sigsys_handler())
700 pdie("install SIGSYS handler");
701 warn("logging seccomp filter failures");
702 }
703
704 /*
705 * Install the syscall filter.
706 */
707 if (j->flags.seccomp_filter) {
708 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog))
709 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
710 }
711}
712
Will Drewry6ac91122011-10-21 16:38:58 -0500713void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400714{
715 if (j->flags.pids)
716 die("tried to enter a pid-namespaced jail;"
717 "try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400718
Elly Jonese1749eb2011-10-07 13:54:59 -0400719 if (j->flags.usergroups && !j->user)
720 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400721
Elly Jonesdd3e8512012-01-23 15:13:38 -0500722 /*
723 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400724 * so we don't even try. If any of our operations fail, we abort() the
725 * entire process.
726 */
727 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400728 pdie("unshare(vfs)");
729
730 if (j->flags.net && unshare(CLONE_NEWNET))
731 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400732
Elly Jones51a5b6c2011-10-12 19:09:26 -0400733 if (j->flags.chroot && enter_chroot(j))
734 pdie("chroot");
735
Elly Jonese1749eb2011-10-07 13:54:59 -0400736 if (j->flags.readonly && remount_readonly())
737 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400738
Elly Jonese1749eb2011-10-07 13:54:59 -0400739 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500740 /*
741 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400742 * capability to change uids, our attempt to use setuid()
743 * below will fail. Hang on to root caps across setuid(), then
744 * lock securebits.
745 */
746 if (prctl(PR_SET_KEEPCAPS, 1))
747 pdie("prctl(PR_SET_KEEPCAPS)");
748 if (prctl
749 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
750 pdie("prctl(PR_SET_SECUREBITS)");
751 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400752
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700753 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700754 * If we're setting no_new_privs, we can drop privileges
755 * before setting seccomp filter. This way filter policies
756 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700757 */
758 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700759 drop_ugid(j);
760 if (j->flags.caps)
761 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700762
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700763 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400764 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700765 /*
766 * If we're not setting no_new_privs,
767 * we need to set seccomp filter *before* dropping privileges.
768 * WARNING: this means that filter policies *must* allow
769 * setgroups()/setresgid()/setresuid() for dropping root and
770 * capget()/capset()/prctl() for dropping caps.
771 */
772 set_seccomp_filter(j);
773
774 drop_ugid(j);
775 if (j->flags.caps)
776 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400777 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400778
Elly Jonesdd3e8512012-01-23 15:13:38 -0500779 /*
780 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400781 * privilege-dropping syscalls :)
782 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400783 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1))
784 pdie("prctl(PR_SET_SECCOMP)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400785}
786
Will Drewry6ac91122011-10-21 16:38:58 -0500787/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400788static int init_exitstatus = 0;
789
Will Drewry6ac91122011-10-21 16:38:58 -0500790void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400791{
792 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400793}
794
Will Drewry6ac91122011-10-21 16:38:58 -0500795int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400796{
797 pid_t pid;
798 int status;
799 /* so that we exit with the right status */
800 signal(SIGTERM, init_term);
801 /* TODO(wad) self jail with seccomp_filters here. */
802 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500803 /*
804 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -0400805 * left inside our pid namespace or we get a signal.
806 */
807 if (pid == rootpid)
808 init_exitstatus = status;
809 }
810 if (!WIFEXITED(init_exitstatus))
811 _exit(MINIJAIL_ERR_INIT);
812 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -0400813}
814
Will Drewry6ac91122011-10-21 16:38:58 -0500815int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400816{
817 size_t sz = 0;
818 size_t bytes = read(fd, &sz, sizeof(sz));
819 char *buf;
820 int r;
821 if (sizeof(sz) != bytes)
822 return -EINVAL;
823 if (sz > USHRT_MAX) /* Arbitrary sanity check */
824 return -E2BIG;
825 buf = malloc(sz);
826 if (!buf)
827 return -ENOMEM;
828 bytes = read(fd, buf, sz);
829 if (bytes != sz) {
830 free(buf);
831 return -EINVAL;
832 }
833 r = minijail_unmarshal(j, buf, sz);
834 free(buf);
835 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500836}
837
Will Drewry6ac91122011-10-21 16:38:58 -0500838int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400839{
840 char *buf;
841 size_t sz = minijail_size(j);
842 ssize_t written;
843 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400844
Elly Jonese1749eb2011-10-07 13:54:59 -0400845 if (!sz)
846 return -EINVAL;
847 buf = malloc(sz);
848 r = minijail_marshal(j, buf, sz);
849 if (r) {
850 free(buf);
851 return r;
852 }
853 /* Sends [size][minijail]. */
854 written = write(fd, &sz, sizeof(sz));
855 if (written != sizeof(sz)) {
856 free(buf);
857 return -EFAULT;
858 }
859 written = write(fd, buf, sz);
860 if (written < 0 || (size_t) written != sz) {
861 free(buf);
862 return -EFAULT;
863 }
864 free(buf);
865 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500866}
Elly Jonescd7a9042011-07-22 13:56:51 -0400867
Will Drewry6ac91122011-10-21 16:38:58 -0500868int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400869{
870 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
871 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
872 if (!newenv)
873 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400874
Elly Jonese1749eb2011-10-07 13:54:59 -0400875 /* Only insert a separating space if we have something to separate... */
876 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
877 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400878
Elly Jonese1749eb2011-10-07 13:54:59 -0400879 /* setenv() makes a copy of the string we give it */
880 setenv(kLdPreloadEnvVar, newenv, 1);
881 free(newenv);
882 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400883}
884
Will Drewry6ac91122011-10-21 16:38:58 -0500885int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400886{
887 int r = pipe(fds);
888 char fd_buf[11];
889 if (r)
890 return r;
891 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
892 if (r <= 0)
893 return -EINVAL;
894 setenv(kFdEnvVar, fd_buf, 1);
895 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500896}
897
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800898int setup_pipe_end(int fds[2], size_t index)
899{
900 if (index > 1)
901 return -1;
902
903 close(fds[1 - index]);
904 return fds[index];
905}
906
907int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
908{
909 if (index > 1)
910 return -1;
911
912 close(fds[1 - index]);
913 /* dup2(2) the corresponding end of the pipe into |fd|. */
914 return dup2(fds[index], fd);
915}
916
Will Drewry6ac91122011-10-21 16:38:58 -0500917int API minijail_run(struct minijail *j, const char *filename,
918 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -0400919{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800920 return minijail_run_pid_pipes(j, filename, argv,
921 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700922}
923
924int API minijail_run_pid(struct minijail *j, const char *filename,
925 char *const argv[], pid_t *pchild_pid)
926{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800927 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
928 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700929}
930
931int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -0700932 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700933{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800934 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
935 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700936}
937
938int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -0700939 char *const argv[], pid_t *pchild_pid,
940 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700941{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800942 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
943 NULL, NULL);
944}
945
946int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
947 char *const argv[], pid_t *pchild_pid,
948 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
949{
Elly Jonese1749eb2011-10-07 13:54:59 -0400950 char *oldenv, *oldenv_copy = NULL;
951 pid_t child_pid;
952 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700953 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800954 int stdout_fds[2];
955 int stderr_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -0400956 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -0400957 /* We need to remember this across the minijail_preexec() call. */
958 int pid_namespace = j->flags.pids;
Ben Chan541c7e52011-08-26 14:55:53 -0700959
Elly Jonese1749eb2011-10-07 13:54:59 -0400960 oldenv = getenv(kLdPreloadEnvVar);
961 if (oldenv) {
962 oldenv_copy = strdup(oldenv);
963 if (!oldenv_copy)
964 return -ENOMEM;
965 }
Will Drewryf89aef52011-09-16 16:48:57 -0500966
Elly Jonese1749eb2011-10-07 13:54:59 -0400967 if (setup_preload())
968 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500969
Elly Jonesdd3e8512012-01-23 15:13:38 -0500970 /*
971 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -0400972 * a pipe(2) to send the minijail configuration over.
973 */
974 if (setup_pipe(pipe_fds))
975 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -0400976
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700977 /*
978 * If we want to write to the child process' standard input,
979 * create the pipe(2) now.
980 */
981 if (pstdin_fd) {
982 if (pipe(stdin_fds))
983 return -EFAULT;
984 }
985
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800986 /*
987 * If we want to read from the child process' standard output,
988 * create the pipe(2) now.
989 */
990 if (pstdout_fd) {
991 if (pipe(stdout_fds))
992 return -EFAULT;
993 }
994
995 /*
996 * If we want to read from the child process' standard error,
997 * create the pipe(2) now.
998 */
999 if (pstderr_fd) {
1000 if (pipe(stderr_fds))
1001 return -EFAULT;
1002 }
1003
Elly Jones761b7412012-06-13 15:49:52 -04001004 /* Use sys_clone() if and only if we're creating a pid namespace.
1005 *
1006 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1007 *
1008 * In multithreaded programs, there are a bunch of locks inside libc,
1009 * some of which may be held by other threads at the time that we call
1010 * minijail_run_pid(). If we call fork(), glibc does its level best to
1011 * ensure that we hold all of these locks before it calls clone()
1012 * internally and drop them after clone() returns, but when we call
1013 * sys_clone(2) directly, all that gets bypassed and we end up with a
1014 * child address space where some of libc's important locks are held by
1015 * other threads (which did not get cloned, and hence will never release
1016 * those locks). This is okay so long as we call exec() immediately
1017 * after, but a bunch of seemingly-innocent libc functions like setenv()
1018 * take locks.
1019 *
1020 * Hence, only call sys_clone() if we need to, in order to get at pid
1021 * namespacing. If we follow this path, the child's address space might
1022 * have broken locks; you may only call functions that do not acquire
1023 * any locks.
1024 *
1025 * Unfortunately, fork() acquires every lock it can get its hands on, as
1026 * previously detailed, so this function is highly likely to deadlock
1027 * later on (see "deadlock here") if we're multithreaded.
1028 *
1029 * We might hack around this by having the clone()d child (init of the
1030 * pid namespace) return directly, rather than leaving the clone()d
1031 * process hanging around to be init for the new namespace (and having
1032 * its fork()ed child return in turn), but that process would be crippled
1033 * with its libc locks potentially broken. We might try fork()ing in the
1034 * parent before we clone() to ensure that we own all the locks, but
1035 * then we have to have the forked child hanging around consuming
1036 * resources (and possibly having file descriptors / shared memory
1037 * regions / etc attached). We'd need to keep the child around to avoid
1038 * having its children get reparented to init.
1039 *
1040 * TODO(ellyjones): figure out if the "forked child hanging around"
1041 * problem is fixable or not. It would be nice if we worked in this
1042 * case.
1043 */
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001044 if (pid_namespace)
Elly Jones761b7412012-06-13 15:49:52 -04001045 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1046 else
1047 child_pid = fork();
1048
Elly Jonese1749eb2011-10-07 13:54:59 -04001049 if (child_pid < 0) {
1050 free(oldenv_copy);
1051 return child_pid;
1052 }
Will Drewryf89aef52011-09-16 16:48:57 -05001053
Elly Jonese1749eb2011-10-07 13:54:59 -04001054 if (child_pid) {
1055 /* Restore parent's LD_PRELOAD. */
1056 if (oldenv_copy) {
1057 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1058 free(oldenv_copy);
1059 } else {
1060 unsetenv(kLdPreloadEnvVar);
1061 }
1062 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001063
Elly Jonese1749eb2011-10-07 13:54:59 -04001064 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001065
1066 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001067 close(pipe_fds[0]); /* read endpoint */
1068 ret = minijail_to_fd(j, pipe_fds[1]);
1069 close(pipe_fds[1]); /* write endpoint */
1070 if (ret) {
1071 kill(j->initpid, SIGKILL);
1072 die("failed to send marshalled minijail");
1073 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001074
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001075 if (pchild_pid)
1076 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001077
1078 /*
1079 * If we want to write to the child process' standard input,
1080 * set up the write end of the pipe.
1081 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001082 if (pstdin_fd)
1083 *pstdin_fd = setup_pipe_end(stdin_fds,
1084 1 /* write end */);
1085
1086 /*
1087 * If we want to read from the child process' standard output,
1088 * set up the read end of the pipe.
1089 */
1090 if (pstdout_fd)
1091 *pstdout_fd = setup_pipe_end(stdout_fds,
1092 0 /* read end */);
1093
1094 /*
1095 * If we want to read from the child process' standard error,
1096 * set up the read end of the pipe.
1097 */
1098 if (pstderr_fd)
1099 *pstderr_fd = setup_pipe_end(stderr_fds,
1100 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001101
Elly Jonese1749eb2011-10-07 13:54:59 -04001102 return 0;
1103 }
1104 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001105
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001106 /*
1107 * If we want to write to the jailed process' standard input,
1108 * set up the read end of the pipe.
1109 */
1110 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001111 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1112 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001113 die("failed to set up stdin pipe");
1114 }
1115
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001116 /*
1117 * If we want to read from the jailed process' standard output,
1118 * set up the write end of the pipe.
1119 */
1120 if (pstdout_fd) {
1121 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1122 STDOUT_FILENO) < 0)
1123 die("failed to set up stdout pipe");
1124 }
1125
1126 /*
1127 * If we want to read from the jailed process' standard error,
1128 * set up the write end of the pipe.
1129 */
1130 if (pstderr_fd) {
1131 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1132 STDERR_FILENO) < 0)
1133 die("failed to set up stderr pipe");
1134 }
1135
Elly Jonese1749eb2011-10-07 13:54:59 -04001136 /* Drop everything that cannot be inherited across execve. */
1137 minijail_preexec(j);
1138 /* Jail this process and its descendants... */
1139 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001140
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001141 if (pid_namespace) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001142 /*
1143 * pid namespace: this process will become init inside the new
Elly Jonese1749eb2011-10-07 13:54:59 -04001144 * namespace, so fork off a child to actually run the program
1145 * (we don't want all programs we might exec to have to know
1146 * how to be init).
Elly Jones761b7412012-06-13 15:49:52 -04001147 *
1148 * If we're multithreaded, we'll probably deadlock here. See
1149 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001150 */
1151 child_pid = fork();
1152 if (child_pid < 0)
1153 _exit(child_pid);
1154 else if (child_pid > 0)
1155 init(child_pid); /* never returns */
1156 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001157
Elly Jonesdd3e8512012-01-23 15:13:38 -05001158 /*
1159 * If we aren't pid-namespaced:
Elly Jonese1749eb2011-10-07 13:54:59 -04001160 * calling process
1161 * -> execve()-ing process
1162 * If we are:
1163 * calling process
1164 * -> init()-ing process
1165 * -> execve()-ing process
1166 */
1167 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001168}
1169
Will Drewry6ac91122011-10-21 16:38:58 -05001170int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001171{
1172 int st;
1173 if (kill(j->initpid, SIGTERM))
1174 return -errno;
1175 if (waitpid(j->initpid, &st, 0) < 0)
1176 return -errno;
1177 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001178}
1179
Will Drewry6ac91122011-10-21 16:38:58 -05001180int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001181{
1182 int st;
1183 if (waitpid(j->initpid, &st, 0) < 0)
1184 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001185
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001186 if (!WIFEXITED(st)) {
1187 if (WIFSIGNALED(st))
mukesh agrawalc420a262013-06-11 17:22:42 -07001188 warn("child process %d received signal %d",
1189 j->initpid, WTERMSIG(st));
Elly Jonese1749eb2011-10-07 13:54:59 -04001190 return MINIJAIL_ERR_JAIL;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001191 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001192
1193 int exit_status = WEXITSTATUS(st);
1194 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001195 info("child process %d exited with status %d",
1196 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001197
1198 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001199}
1200
Will Drewry6ac91122011-10-21 16:38:58 -05001201void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001202{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001203 if (j->flags.seccomp_filter && j->filter_prog) {
1204 free(j->filter_prog->filter);
1205 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001206 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001207 while (j->bindings_head) {
1208 struct binding *b = j->bindings_head;
1209 j->bindings_head = j->bindings_head->next;
1210 free(b->dest);
1211 free(b->src);
1212 free(b);
1213 }
1214 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001215 if (j->user)
1216 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001217 if (j->chrootdir)
1218 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001219 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001220}