blob: d4b9d16ad99702eba2c193109aefde625553469e [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;
Lee Campbell11af0622014-05-22 12:36:04 -070088 int mount_tmp:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040089 } flags;
90 uid_t uid;
91 gid_t gid;
92 gid_t usergid;
93 char *user;
94 uint64_t caps;
95 pid_t initpid;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080096 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -040097 int binding_count;
98 char *chrootdir;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080099 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400100 struct binding *bindings_head;
101 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500102};
103
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700104/*
105 * Strip out flags meant for the parent.
106 * We keep things that are not inherited across execve(2) (e.g. capabilities),
107 * or are easier to set after execve(2) (e.g. seccomp filters).
108 */
109void minijail_preenter(struct minijail *j)
110{
111 j->flags.vfs = 0;
112 j->flags.readonly = 0;
113 j->flags.pids = 0;
114}
115
116/*
117 * Strip out flags meant for the child.
118 * We keep things that are inherited across execve(2).
119 */
120void minijail_preexec(struct minijail *j)
121{
122 int vfs = j->flags.vfs;
123 int readonly = j->flags.readonly;
124 if (j->user)
125 free(j->user);
126 j->user = NULL;
127 memset(&j->flags, 0, sizeof(j->flags));
128 /* Now restore anything we meant to keep. */
129 j->flags.vfs = vfs;
130 j->flags.readonly = readonly;
131 /* Note, |pids| will already have been used before this call. */
132}
133
134/* Minijail API. */
135
Will Drewry6ac91122011-10-21 16:38:58 -0500136struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400137{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400138 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400139}
140
Will Drewry6ac91122011-10-21 16:38:58 -0500141void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400142{
143 if (uid == 0)
144 die("useless change to uid 0");
145 j->uid = uid;
146 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400147}
148
Will Drewry6ac91122011-10-21 16:38:58 -0500149void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400150{
151 if (gid == 0)
152 die("useless change to gid 0");
153 j->gid = gid;
154 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400155}
156
Will Drewry6ac91122011-10-21 16:38:58 -0500157int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400158{
159 char *buf = NULL;
160 struct passwd pw;
161 struct passwd *ppw = NULL;
162 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
163 if (sz == -1)
164 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400165
Elly Jonesdd3e8512012-01-23 15:13:38 -0500166 /*
167 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400168 * the maximum needed size of the buffer, so we don't have to search.
169 */
170 buf = malloc(sz);
171 if (!buf)
172 return -ENOMEM;
173 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500174 /*
175 * We're safe to free the buffer here. The strings inside pw point
176 * inside buf, but we don't use any of them; this leaves the pointers
177 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
178 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400179 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700180 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400181 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700182 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400183 minijail_change_uid(j, ppw->pw_uid);
184 j->user = strdup(user);
185 if (!j->user)
186 return -ENOMEM;
187 j->usergid = ppw->pw_gid;
188 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400189}
190
Will Drewry6ac91122011-10-21 16:38:58 -0500191int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400192{
193 char *buf = NULL;
194 struct group gr;
195 struct group *pgr = NULL;
196 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
197 if (sz == -1)
198 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400199
Elly Jonesdd3e8512012-01-23 15:13:38 -0500200 /*
201 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400202 * the maximum needed size of the buffer, so we don't have to search.
203 */
204 buf = malloc(sz);
205 if (!buf)
206 return -ENOMEM;
207 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500208 /*
209 * We're safe to free the buffer here. The strings inside gr point
210 * inside buf, but we don't use any of them; this leaves the pointers
211 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
212 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400213 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700214 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400215 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700216 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400217 minijail_change_gid(j, pgr->gr_gid);
218 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400219}
220
Will Drewry6ac91122011-10-21 16:38:58 -0500221void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400222{
223 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400224}
225
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700226void API minijail_no_new_privs(struct minijail *j)
227{
228 j->flags.no_new_privs = 1;
229}
230
Will Drewry6ac91122011-10-21 16:38:58 -0500231void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400232{
233 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500234}
235
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700236void API minijail_log_seccomp_filter_failures(struct minijail *j)
237{
238 j->flags.log_seccomp_filter = 1;
239}
240
Will Drewry6ac91122011-10-21 16:38:58 -0500241void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400242{
243 j->caps = capmask;
244 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400245}
246
Will Drewry6ac91122011-10-21 16:38:58 -0500247void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400248{
249 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400250}
251
Will Drewry6ac91122011-10-21 16:38:58 -0500252void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400253{
Elly Jonese58176c2012-01-23 11:46:17 -0500254 j->flags.vfs = 1;
255 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400256 j->flags.pids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400257}
258
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400259void API minijail_namespace_net(struct minijail *j)
260{
261 j->flags.net = 1;
262}
263
Will Drewry6ac91122011-10-21 16:38:58 -0500264void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400265{
266 j->flags.vfs = 1;
267 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400268}
269
Will Drewry6ac91122011-10-21 16:38:58 -0500270void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400271{
272 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400273}
274
Will Drewry6ac91122011-10-21 16:38:58 -0500275void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400276{
277 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400278}
279
Will Drewry6ac91122011-10-21 16:38:58 -0500280int API minijail_enter_chroot(struct minijail *j, const char *dir) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400281 if (j->chrootdir)
282 return -EINVAL;
283 j->chrootdir = strdup(dir);
284 if (!j->chrootdir)
285 return -ENOMEM;
286 j->flags.chroot = 1;
287 return 0;
288}
289
Lee Campbell11af0622014-05-22 12:36:04 -0700290void API minijail_mount_tmp(struct minijail *j)
291{
292 j->flags.mount_tmp = 1;
293}
294
Will Drewry6ac91122011-10-21 16:38:58 -0500295int API minijail_bind(struct minijail *j, const char *src, const char *dest,
296 int writeable) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400297 struct binding *b;
298
299 if (*dest != '/')
300 return -EINVAL;
301 b = calloc(1, sizeof(*b));
302 if (!b)
303 return -ENOMEM;
304 b->dest = strdup(dest);
305 if (!b->dest)
306 goto error;
307 b->src = strdup(src);
308 if (!b->src)
309 goto error;
310 b->writeable = writeable;
311
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700312 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400313
Elly Jonesdd3e8512012-01-23 15:13:38 -0500314 /*
315 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400316 * containing vfs namespace.
317 */
318 minijail_namespace_vfs(j);
319
320 if (j->bindings_tail)
321 j->bindings_tail->next = b;
322 else
323 j->bindings_head = b;
324 j->bindings_tail = b;
325 j->binding_count++;
326
327 return 0;
328
329error:
330 free(b->src);
331 free(b->dest);
332 free(b);
333 return -ENOMEM;
334}
335
Will Drewry6ac91122011-10-21 16:38:58 -0500336void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400337{
338 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800339 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700340 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400341 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800342
343 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700344 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
345 die("failed to compile seccomp filter BPF program in '%s'",
346 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800347 }
348
349 j->filter_len = fprog->len;
350 j->filter_prog = fprog;
351
Elly Jonese1749eb2011-10-07 13:54:59 -0400352 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500353}
354
Will Drewryf89aef52011-09-16 16:48:57 -0500355struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400356 size_t available;
357 size_t total;
358 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500359};
360
Will Drewry6ac91122011-10-21 16:38:58 -0500361void marshal_state_init(struct marshal_state *state,
362 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400363{
364 state->available = available;
365 state->buf = buf;
366 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500367}
368
Will Drewry6ac91122011-10-21 16:38:58 -0500369void marshal_append(struct marshal_state *state,
370 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400371{
372 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500373
Elly Jonese1749eb2011-10-07 13:54:59 -0400374 /* Up to |available| will be written. */
375 if (copy_len) {
376 memcpy(state->buf, src, copy_len);
377 state->buf += copy_len;
378 state->available -= copy_len;
379 }
380 /* |total| will contain the expected length. */
381 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500382}
383
Will Drewry6ac91122011-10-21 16:38:58 -0500384void minijail_marshal_helper(struct marshal_state *state,
385 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400386{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400387 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400388 marshal_append(state, (char *)j, sizeof(*j));
389 if (j->user)
390 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400391 if (j->chrootdir)
392 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800393 if (j->flags.seccomp_filter && j->filter_prog) {
394 struct sock_fprog *fp = j->filter_prog;
395 marshal_append(state, (char *)fp->filter,
396 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400397 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400398 for (b = j->bindings_head; b; b = b->next) {
399 marshal_append(state, b->src, strlen(b->src) + 1);
400 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700401 marshal_append(state, (char *)&b->writeable,
402 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400403 }
Will Drewryf89aef52011-09-16 16:48:57 -0500404}
405
Will Drewry6ac91122011-10-21 16:38:58 -0500406size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400407{
408 struct marshal_state state;
409 marshal_state_init(&state, NULL, 0);
410 minijail_marshal_helper(&state, j);
411 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500412}
413
Elly Jonese1749eb2011-10-07 13:54:59 -0400414int minijail_marshal(const struct minijail *j, char *buf, size_t available)
415{
416 struct marshal_state state;
417 marshal_state_init(&state, buf, available);
418 minijail_marshal_helper(&state, j);
419 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500420}
421
Elly Jones51a5b6c2011-10-12 19:09:26 -0400422/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
423 * @length Number of bytes to consume
424 * @buf Buffer to consume from
425 * @buflength Size of @buf
426 *
427 * Returns a pointer to the base of the bytes, or NULL for errors.
428 */
Will Drewry6ac91122011-10-21 16:38:58 -0500429void *consumebytes(size_t length, char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400430 char *p = *buf;
431 if (length > *buflength)
432 return NULL;
433 *buf += length;
434 *buflength -= length;
435 return p;
436}
437
438/* consumestr: consumes a C string from a buffer @buf of length @length
439 * @buf Buffer to consume
440 * @length Length of buffer
441 *
442 * Returns a pointer to the base of the string, or NULL for errors.
443 */
Will Drewry6ac91122011-10-21 16:38:58 -0500444char *consumestr(char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400445 size_t len = strnlen(*buf, *buflength);
446 if (len == *buflength)
447 /* There's no null-terminator */
448 return NULL;
449 return consumebytes(len + 1, buf, buflength);
450}
451
Elly Jonese1749eb2011-10-07 13:54:59 -0400452int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
453{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400454 int i;
455 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500456 int ret = -EINVAL;
457
Elly Jonese1749eb2011-10-07 13:54:59 -0400458 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500459 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400460 memcpy((void *)j, serialized, sizeof(*j));
461 serialized += sizeof(*j);
462 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500463
Will Drewrybee7ba72011-10-21 20:47:01 -0500464 /* Potentially stale pointers not used as signals. */
465 j->bindings_head = NULL;
466 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800467 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500468
Elly Jonese1749eb2011-10-07 13:54:59 -0400469 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400470 char *user = consumestr(&serialized, &length);
471 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500472 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400473 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500474 if (!j->user)
475 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400476 }
Will Drewryf89aef52011-09-16 16:48:57 -0500477
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400478 if (j->chrootdir) { /* stale pointer */
479 char *chrootdir = consumestr(&serialized, &length);
480 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500481 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400482 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500483 if (!j->chrootdir)
484 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400485 }
486
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800487 if (j->flags.seccomp_filter && j->filter_len > 0) {
488 size_t ninstrs = j->filter_len;
489 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
490 ninstrs > USHRT_MAX)
491 goto bad_filters;
492
493 size_t program_len = ninstrs * sizeof(struct sock_filter);
494 void *program = consumebytes(program_len, &serialized, &length);
495 if (!program)
496 goto bad_filters;
497
498 j->filter_prog = malloc(sizeof(struct sock_fprog));
499 j->filter_prog->len = ninstrs;
500 j->filter_prog->filter = malloc(program_len);
501 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400502 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400503
504 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400505 j->binding_count = 0;
506 for (i = 0; i < count; ++i) {
507 int *writeable;
508 const char *dest;
509 const char *src = consumestr(&serialized, &length);
510 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500511 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400512 dest = consumestr(&serialized, &length);
513 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500514 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400515 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
516 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500517 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400518 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500519 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400520 }
521
Elly Jonese1749eb2011-10-07 13:54:59 -0400522 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500523
524bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800525 if (j->flags.seccomp_filter && j->filter_len > 0) {
526 free(j->filter_prog->filter);
527 free(j->filter_prog);
528 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500529bad_filters:
530 if (j->chrootdir)
531 free(j->chrootdir);
532bad_chrootdir:
533 if (j->user)
534 free(j->user);
535clear_pointers:
536 j->user = NULL;
537 j->chrootdir = NULL;
538out:
539 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500540}
541
Elly Jones51a5b6c2011-10-12 19:09:26 -0400542/* bind_one: Applies bindings from @b for @j, recursing as needed.
543 * @j Minijail these bindings are for
544 * @b Head of list of bindings
545 *
546 * Returns 0 for success.
547 */
Will Drewry6ac91122011-10-21 16:38:58 -0500548int bind_one(const struct minijail *j, struct binding *b) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400549 int ret = 0;
550 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400551 if (ret)
552 return ret;
553 /* dest has a leading "/" */
554 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
555 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500556 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400557 if (ret)
558 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500559 if (!b->writeable) {
560 ret = mount(b->src, dest, NULL,
561 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
562 if (ret)
563 pdie("bind ro: %s -> %s", b->src, dest);
564 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400565 free(dest);
566 if (b->next)
567 return bind_one(j, b->next);
568 return ret;
569}
570
Will Drewry6ac91122011-10-21 16:38:58 -0500571int enter_chroot(const struct minijail *j) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400572 int ret;
573 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
574 return ret;
575
576 if (chroot(j->chrootdir))
577 return -errno;
578
579 if (chdir("/"))
580 return -errno;
581
582 return 0;
583}
584
Lee Campbell11af0622014-05-22 12:36:04 -0700585int mount_tmp(void)
586{
587 return mount("none", "/tmp", "tmpfs", 0, "size=128M,mode=777");
588}
589
Will Drewry6ac91122011-10-21 16:38:58 -0500590int remount_readonly(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400591{
592 const char *kProcPath = "/proc";
593 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500594 /*
595 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400596 * /proc in our namespace, which means using MS_REMOUNT here would
597 * mutate our parent's mount as well, even though we're in a VFS
598 * namespace (!). Instead, remove their mount from our namespace
599 * and make our own.
600 */
601 if (umount(kProcPath))
602 return -errno;
603 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
604 return -errno;
605 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400606}
607
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700608void drop_ugid(const struct minijail *j)
609{
610 if (j->flags.usergroups) {
611 if (initgroups(j->user, j->usergid))
612 pdie("initgroups");
613 } else {
614 /* Only attempt to clear supplemental groups if we are changing
615 * users. */
616 if ((j->uid || j->gid) && setgroups(0, NULL))
617 pdie("setgroups");
618 }
619
620 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
621 pdie("setresgid");
622
623 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
624 pdie("setresuid");
625}
626
Mike Frysinger3adfef72013-05-09 17:19:08 -0400627/*
628 * We specifically do not use cap_valid() as that only tells us the last
629 * valid cap we were *compiled* against (i.e. what the version of kernel
630 * headers says). If we run on a different kernel version, then it's not
631 * uncommon for that to be less (if an older kernel) or more (if a newer
632 * kernel). So suck up the answer via /proc.
633 */
634static int run_cap_valid(unsigned int cap)
635{
636 static unsigned int last_cap;
637
638 if (!last_cap) {
639 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
640 FILE *fp = fopen(cap_file, "re");
641 if (fscanf(fp, "%u", &last_cap) != 1)
642 pdie("fscanf(%s)", cap_file);
643 fclose(fp);
644 }
645
646 return cap <= last_cap;
647}
648
Will Drewry6ac91122011-10-21 16:38:58 -0500649void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400650{
651 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800652 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800653 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400654 unsigned int i;
655 if (!caps)
656 die("can't get process caps");
657 if (cap_clear_flag(caps, CAP_INHERITABLE))
658 die("can't clear inheritable caps");
659 if (cap_clear_flag(caps, CAP_EFFECTIVE))
660 die("can't clear effective caps");
661 if (cap_clear_flag(caps, CAP_PERMITTED))
662 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400663 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800664 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800665 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400666 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800667 flag[0] = i;
668 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400669 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800670 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400671 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800672 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400673 die("can't add inheritable cap");
674 }
675 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800676 die("can't apply initial cleaned capset");
677
678 /*
679 * Instead of dropping bounding set first, do it here in case
680 * the caller had a more permissive bounding set which could
681 * have been used above to raise a capability that wasn't already
682 * present. This requires CAP_SETPCAP, so we raised/kept it above.
683 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400684 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800685 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400686 continue;
687 if (prctl(PR_CAPBSET_DROP, i))
688 pdie("prctl(PR_CAPBSET_DROP)");
689 }
Kees Cook323878a2013-02-05 15:35:24 -0800690
691 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800692 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800693 flag[0] = CAP_SETPCAP;
694 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
695 die("can't clear effective cap");
696 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
697 die("can't clear permitted cap");
698 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
699 die("can't clear inheritable cap");
700 }
701
702 if (cap_set_proc(caps))
703 die("can't apply final cleaned capset");
704
705 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400706}
707
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700708void set_seccomp_filter(const struct minijail *j)
709{
710 /*
711 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
712 * in the kernel source tree for an explanation of the parameters.
713 */
714 if (j->flags.no_new_privs) {
715 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
716 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
717 }
718
719 /*
720 * If we're logging seccomp filter failures,
721 * install the SIGSYS handler first.
722 */
723 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
724 if (install_sigsys_handler())
725 pdie("install SIGSYS handler");
726 warn("logging seccomp filter failures");
727 }
728
729 /*
730 * Install the syscall filter.
731 */
732 if (j->flags.seccomp_filter) {
733 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog))
734 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
735 }
736}
737
Will Drewry6ac91122011-10-21 16:38:58 -0500738void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400739{
740 if (j->flags.pids)
741 die("tried to enter a pid-namespaced jail;"
742 "try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400743
Elly Jonese1749eb2011-10-07 13:54:59 -0400744 if (j->flags.usergroups && !j->user)
745 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400746
Elly Jonesdd3e8512012-01-23 15:13:38 -0500747 /*
748 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400749 * so we don't even try. If any of our operations fail, we abort() the
750 * entire process.
751 */
752 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400753 pdie("unshare(vfs)");
754
755 if (j->flags.net && unshare(CLONE_NEWNET))
756 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400757
Elly Jones51a5b6c2011-10-12 19:09:26 -0400758 if (j->flags.chroot && enter_chroot(j))
759 pdie("chroot");
760
Lee Campbell11af0622014-05-22 12:36:04 -0700761 if (j->flags.chroot && j->flags.mount_tmp && mount_tmp())
762 pdie("mount_tmp");
763
Elly Jonese1749eb2011-10-07 13:54:59 -0400764 if (j->flags.readonly && remount_readonly())
765 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400766
Elly Jonese1749eb2011-10-07 13:54:59 -0400767 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500768 /*
769 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400770 * capability to change uids, our attempt to use setuid()
771 * below will fail. Hang on to root caps across setuid(), then
772 * lock securebits.
773 */
774 if (prctl(PR_SET_KEEPCAPS, 1))
775 pdie("prctl(PR_SET_KEEPCAPS)");
776 if (prctl
777 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
778 pdie("prctl(PR_SET_SECUREBITS)");
779 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400780
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700781 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700782 * If we're setting no_new_privs, we can drop privileges
783 * before setting seccomp filter. This way filter policies
784 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700785 */
786 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700787 drop_ugid(j);
788 if (j->flags.caps)
789 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700790
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700791 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400792 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700793 /*
794 * If we're not setting no_new_privs,
795 * we need to set seccomp filter *before* dropping privileges.
796 * WARNING: this means that filter policies *must* allow
797 * setgroups()/setresgid()/setresuid() for dropping root and
798 * capget()/capset()/prctl() for dropping caps.
799 */
800 set_seccomp_filter(j);
801
802 drop_ugid(j);
803 if (j->flags.caps)
804 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400805 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400806
Elly Jonesdd3e8512012-01-23 15:13:38 -0500807 /*
808 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400809 * privilege-dropping syscalls :)
810 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400811 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1))
812 pdie("prctl(PR_SET_SECCOMP)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400813}
814
Will Drewry6ac91122011-10-21 16:38:58 -0500815/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400816static int init_exitstatus = 0;
817
Will Drewry6ac91122011-10-21 16:38:58 -0500818void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400819{
820 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400821}
822
Will Drewry6ac91122011-10-21 16:38:58 -0500823int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400824{
825 pid_t pid;
826 int status;
827 /* so that we exit with the right status */
828 signal(SIGTERM, init_term);
829 /* TODO(wad) self jail with seccomp_filters here. */
830 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500831 /*
832 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -0400833 * left inside our pid namespace or we get a signal.
834 */
835 if (pid == rootpid)
836 init_exitstatus = status;
837 }
838 if (!WIFEXITED(init_exitstatus))
839 _exit(MINIJAIL_ERR_INIT);
840 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -0400841}
842
Will Drewry6ac91122011-10-21 16:38:58 -0500843int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400844{
845 size_t sz = 0;
846 size_t bytes = read(fd, &sz, sizeof(sz));
847 char *buf;
848 int r;
849 if (sizeof(sz) != bytes)
850 return -EINVAL;
851 if (sz > USHRT_MAX) /* Arbitrary sanity check */
852 return -E2BIG;
853 buf = malloc(sz);
854 if (!buf)
855 return -ENOMEM;
856 bytes = read(fd, buf, sz);
857 if (bytes != sz) {
858 free(buf);
859 return -EINVAL;
860 }
861 r = minijail_unmarshal(j, buf, sz);
862 free(buf);
863 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500864}
865
Will Drewry6ac91122011-10-21 16:38:58 -0500866int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400867{
868 char *buf;
869 size_t sz = minijail_size(j);
870 ssize_t written;
871 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400872
Elly Jonese1749eb2011-10-07 13:54:59 -0400873 if (!sz)
874 return -EINVAL;
875 buf = malloc(sz);
876 r = minijail_marshal(j, buf, sz);
877 if (r) {
878 free(buf);
879 return r;
880 }
881 /* Sends [size][minijail]. */
882 written = write(fd, &sz, sizeof(sz));
883 if (written != sizeof(sz)) {
884 free(buf);
885 return -EFAULT;
886 }
887 written = write(fd, buf, sz);
888 if (written < 0 || (size_t) written != sz) {
889 free(buf);
890 return -EFAULT;
891 }
892 free(buf);
893 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500894}
Elly Jonescd7a9042011-07-22 13:56:51 -0400895
Will Drewry6ac91122011-10-21 16:38:58 -0500896int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400897{
898 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
899 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
900 if (!newenv)
901 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400902
Elly Jonese1749eb2011-10-07 13:54:59 -0400903 /* Only insert a separating space if we have something to separate... */
904 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
905 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400906
Elly Jonese1749eb2011-10-07 13:54:59 -0400907 /* setenv() makes a copy of the string we give it */
908 setenv(kLdPreloadEnvVar, newenv, 1);
909 free(newenv);
910 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400911}
912
Will Drewry6ac91122011-10-21 16:38:58 -0500913int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400914{
915 int r = pipe(fds);
916 char fd_buf[11];
917 if (r)
918 return r;
919 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
920 if (r <= 0)
921 return -EINVAL;
922 setenv(kFdEnvVar, fd_buf, 1);
923 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500924}
925
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800926int setup_pipe_end(int fds[2], size_t index)
927{
928 if (index > 1)
929 return -1;
930
931 close(fds[1 - index]);
932 return fds[index];
933}
934
935int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
936{
937 if (index > 1)
938 return -1;
939
940 close(fds[1 - index]);
941 /* dup2(2) the corresponding end of the pipe into |fd|. */
942 return dup2(fds[index], fd);
943}
944
Will Drewry6ac91122011-10-21 16:38:58 -0500945int API minijail_run(struct minijail *j, const char *filename,
946 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -0400947{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800948 return minijail_run_pid_pipes(j, filename, argv,
949 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700950}
951
952int API minijail_run_pid(struct minijail *j, const char *filename,
953 char *const argv[], pid_t *pchild_pid)
954{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800955 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
956 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700957}
958
959int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -0700960 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700961{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800962 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
963 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700964}
965
966int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -0700967 char *const argv[], pid_t *pchild_pid,
968 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700969{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800970 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
971 NULL, NULL);
972}
973
974int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700975 char *const argv[], pid_t *pchild_pid,
976 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800977{
Elly Jonese1749eb2011-10-07 13:54:59 -0400978 char *oldenv, *oldenv_copy = NULL;
979 pid_t child_pid;
980 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700981 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800982 int stdout_fds[2];
983 int stderr_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -0400984 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -0400985 /* We need to remember this across the minijail_preexec() call. */
986 int pid_namespace = j->flags.pids;
Ben Chan541c7e52011-08-26 14:55:53 -0700987
Elly Jonese1749eb2011-10-07 13:54:59 -0400988 oldenv = getenv(kLdPreloadEnvVar);
989 if (oldenv) {
990 oldenv_copy = strdup(oldenv);
991 if (!oldenv_copy)
992 return -ENOMEM;
993 }
Will Drewryf89aef52011-09-16 16:48:57 -0500994
Elly Jonese1749eb2011-10-07 13:54:59 -0400995 if (setup_preload())
996 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500997
Elly Jonesdd3e8512012-01-23 15:13:38 -0500998 /*
999 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -04001000 * a pipe(2) to send the minijail configuration over.
1001 */
1002 if (setup_pipe(pipe_fds))
1003 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -04001004
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001005 /*
1006 * If we want to write to the child process' standard input,
1007 * create the pipe(2) now.
1008 */
1009 if (pstdin_fd) {
1010 if (pipe(stdin_fds))
1011 return -EFAULT;
1012 }
1013
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001014 /*
1015 * If we want to read from the child process' standard output,
1016 * create the pipe(2) now.
1017 */
1018 if (pstdout_fd) {
1019 if (pipe(stdout_fds))
1020 return -EFAULT;
1021 }
1022
1023 /*
1024 * If we want to read from the child process' standard error,
1025 * create the pipe(2) now.
1026 */
1027 if (pstderr_fd) {
1028 if (pipe(stderr_fds))
1029 return -EFAULT;
1030 }
1031
Elly Jones761b7412012-06-13 15:49:52 -04001032 /* Use sys_clone() if and only if we're creating a pid namespace.
1033 *
1034 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1035 *
1036 * In multithreaded programs, there are a bunch of locks inside libc,
1037 * some of which may be held by other threads at the time that we call
1038 * minijail_run_pid(). If we call fork(), glibc does its level best to
1039 * ensure that we hold all of these locks before it calls clone()
1040 * internally and drop them after clone() returns, but when we call
1041 * sys_clone(2) directly, all that gets bypassed and we end up with a
1042 * child address space where some of libc's important locks are held by
1043 * other threads (which did not get cloned, and hence will never release
1044 * those locks). This is okay so long as we call exec() immediately
1045 * after, but a bunch of seemingly-innocent libc functions like setenv()
1046 * take locks.
1047 *
1048 * Hence, only call sys_clone() if we need to, in order to get at pid
1049 * namespacing. If we follow this path, the child's address space might
1050 * have broken locks; you may only call functions that do not acquire
1051 * any locks.
1052 *
1053 * Unfortunately, fork() acquires every lock it can get its hands on, as
1054 * previously detailed, so this function is highly likely to deadlock
1055 * later on (see "deadlock here") if we're multithreaded.
1056 *
1057 * We might hack around this by having the clone()d child (init of the
1058 * pid namespace) return directly, rather than leaving the clone()d
1059 * process hanging around to be init for the new namespace (and having
1060 * its fork()ed child return in turn), but that process would be crippled
1061 * with its libc locks potentially broken. We might try fork()ing in the
1062 * parent before we clone() to ensure that we own all the locks, but
1063 * then we have to have the forked child hanging around consuming
1064 * resources (and possibly having file descriptors / shared memory
1065 * regions / etc attached). We'd need to keep the child around to avoid
1066 * having its children get reparented to init.
1067 *
1068 * TODO(ellyjones): figure out if the "forked child hanging around"
1069 * problem is fixable or not. It would be nice if we worked in this
1070 * case.
1071 */
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001072 if (pid_namespace)
Elly Jones761b7412012-06-13 15:49:52 -04001073 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1074 else
1075 child_pid = fork();
1076
Elly Jonese1749eb2011-10-07 13:54:59 -04001077 if (child_pid < 0) {
1078 free(oldenv_copy);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001079 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001080 }
Will Drewryf89aef52011-09-16 16:48:57 -05001081
Elly Jonese1749eb2011-10-07 13:54:59 -04001082 if (child_pid) {
1083 /* Restore parent's LD_PRELOAD. */
1084 if (oldenv_copy) {
1085 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1086 free(oldenv_copy);
1087 } else {
1088 unsetenv(kLdPreloadEnvVar);
1089 }
1090 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001091
Elly Jonese1749eb2011-10-07 13:54:59 -04001092 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001093
1094 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001095 close(pipe_fds[0]); /* read endpoint */
1096 ret = minijail_to_fd(j, pipe_fds[1]);
1097 close(pipe_fds[1]); /* write endpoint */
1098 if (ret) {
1099 kill(j->initpid, SIGKILL);
1100 die("failed to send marshalled minijail");
1101 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001102
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001103 if (pchild_pid)
1104 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001105
1106 /*
1107 * If we want to write to the child process' standard input,
1108 * set up the write end of the pipe.
1109 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001110 if (pstdin_fd)
1111 *pstdin_fd = setup_pipe_end(stdin_fds,
1112 1 /* write end */);
1113
1114 /*
1115 * If we want to read from the child process' standard output,
1116 * set up the read end of the pipe.
1117 */
1118 if (pstdout_fd)
1119 *pstdout_fd = setup_pipe_end(stdout_fds,
1120 0 /* read end */);
1121
1122 /*
1123 * If we want to read from the child process' standard error,
1124 * set up the read end of the pipe.
1125 */
1126 if (pstderr_fd)
1127 *pstderr_fd = setup_pipe_end(stderr_fds,
1128 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001129
Elly Jonese1749eb2011-10-07 13:54:59 -04001130 return 0;
1131 }
1132 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001133
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001134 /*
1135 * If we want to write to the jailed process' standard input,
1136 * set up the read end of the pipe.
1137 */
1138 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001139 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1140 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001141 die("failed to set up stdin pipe");
1142 }
1143
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001144 /*
1145 * If we want to read from the jailed process' standard output,
1146 * set up the write end of the pipe.
1147 */
1148 if (pstdout_fd) {
1149 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1150 STDOUT_FILENO) < 0)
1151 die("failed to set up stdout pipe");
1152 }
1153
1154 /*
1155 * If we want to read from the jailed process' standard error,
1156 * set up the write end of the pipe.
1157 */
1158 if (pstderr_fd) {
1159 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1160 STDERR_FILENO) < 0)
1161 die("failed to set up stderr pipe");
1162 }
1163
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001164 /* Strip out flags that cannot be inherited across execve. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001165 minijail_preexec(j);
1166 /* Jail this process and its descendants... */
1167 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001168
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001169 if (pid_namespace) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001170 /*
1171 * pid namespace: this process will become init inside the new
Elly Jonese1749eb2011-10-07 13:54:59 -04001172 * namespace, so fork off a child to actually run the program
1173 * (we don't want all programs we might exec to have to know
1174 * how to be init).
Elly Jones761b7412012-06-13 15:49:52 -04001175 *
1176 * If we're multithreaded, we'll probably deadlock here. See
1177 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001178 */
1179 child_pid = fork();
1180 if (child_pid < 0)
1181 _exit(child_pid);
1182 else if (child_pid > 0)
1183 init(child_pid); /* never returns */
1184 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001185
Elly Jonesdd3e8512012-01-23 15:13:38 -05001186 /*
1187 * If we aren't pid-namespaced:
Elly Jonese1749eb2011-10-07 13:54:59 -04001188 * calling process
1189 * -> execve()-ing process
1190 * If we are:
1191 * calling process
1192 * -> init()-ing process
1193 * -> execve()-ing process
1194 */
1195 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001196}
1197
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001198int API minijail_run_static(struct minijail *j, const char *filename,
1199 char *const argv[])
1200{
1201 pid_t child_pid;
1202 int pid_namespace = j->flags.pids;
1203
1204 if (j->flags.caps)
1205 die("caps not supported with static targets");
1206
1207 if (pid_namespace)
1208 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1209 else
1210 child_pid = fork();
1211
1212 if (child_pid < 0) {
1213 die("failed to fork child");
1214 }
1215 if (child_pid > 0 ) {
1216 j->initpid = child_pid;
1217 return 0;
1218 }
1219
1220 /*
1221 * We can now drop this child into the sandbox
1222 * then execve the target.
1223 */
1224
1225 j->flags.pids = 0;
1226 minijail_enter(j);
1227
1228 if (pid_namespace) {
1229 /*
1230 * pid namespace: this process will become init inside the new
1231 * namespace, so fork off a child to actually run the program
1232 * (we don't want all programs we might exec to have to know
1233 * how to be init).
1234 *
1235 * If we're multithreaded, we'll probably deadlock here. See
1236 * WARNING above.
1237 */
1238 child_pid = fork();
1239 if (child_pid < 0)
1240 _exit(child_pid);
1241 else if (child_pid > 0)
1242 init(child_pid); /* never returns */
1243 }
1244
1245 _exit(execve(filename, argv, environ));
1246}
1247
Will Drewry6ac91122011-10-21 16:38:58 -05001248int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001249{
1250 int st;
1251 if (kill(j->initpid, SIGTERM))
1252 return -errno;
1253 if (waitpid(j->initpid, &st, 0) < 0)
1254 return -errno;
1255 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001256}
1257
Will Drewry6ac91122011-10-21 16:38:58 -05001258int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001259{
1260 int st;
1261 if (waitpid(j->initpid, &st, 0) < 0)
1262 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001263
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001264 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001265 int error_status = st;
1266 if (WIFSIGNALED(st)) {
1267 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001268 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001269 j->initpid, signum);
1270 /*
1271 * We return MINIJAIL_ERR_JAIL if the process received
1272 * SIGSYS, which happens when a syscall is blocked by
1273 * seccomp filters.
1274 * If not, we do what bash(1) does:
1275 * $? = 128 + signum
1276 */
1277 if (signum == SIGSYS) {
1278 error_status = MINIJAIL_ERR_JAIL;
1279 } else {
1280 error_status = 128 + signum;
1281 }
1282 }
1283 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001284 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001285
1286 int exit_status = WEXITSTATUS(st);
1287 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001288 info("child process %d exited with status %d",
1289 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001290
1291 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001292}
1293
Will Drewry6ac91122011-10-21 16:38:58 -05001294void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001295{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001296 if (j->flags.seccomp_filter && j->filter_prog) {
1297 free(j->filter_prog->filter);
1298 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001299 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001300 while (j->bindings_head) {
1301 struct binding *b = j->bindings_head;
1302 j->bindings_head = j->bindings_head->next;
1303 free(b->dest);
1304 free(b->src);
1305 free(b);
1306 }
1307 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001308 if (j->user)
1309 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001310 if (j->chrootdir)
1311 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001312 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001313}