blob: 92a6e705223c09b5065ccc7f75767f2de91a2dcf [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
7#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07008
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08009#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050010#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040011#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070012#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#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 Obes1563b5b2014-07-10 07:01:53 -070030#include <sys/stat.h>
31#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080032#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <unistd.h>
35
36#include "libminijail.h"
37#include "libminijail-private.h"
38
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070039#include "signal.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070041#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042
Lei Zhangeee31552012-10-17 21:27:10 -070043#ifdef HAVE_SECUREBITS_H
44#include <linux/securebits.h>
45#else
46#define SECURE_ALL_BITS 0x15
47#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
48#endif
49
Will Drewry32ac9f52011-08-18 21:36:27 -050050/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070052# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080053#endif
54
55/* For seccomp_filter using BPF. */
56#ifndef PR_SET_NO_NEW_PRIVS
57# define PR_SET_NO_NEW_PRIVS 38
58#endif
59#ifndef SECCOMP_MODE_FILTER
60# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050061#endif
62
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070063#ifdef USE_SECCOMP_SOFTFAIL
64# define SECCOMP_SOFTFAIL 1
65#else
66# define SECCOMP_SOFTFAIL 0
67#endif
68
Elly Jones51a5b6c2011-10-12 19:09:26 -040069struct binding {
70 char *src;
71 char *dest;
72 int writeable;
73 struct binding *next;
74};
75
Will Drewryf89aef52011-09-16 16:48:57 -050076struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070077 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070078 * WARNING: if you add a flag here you need to make sure it's
79 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070080 */
Elly Jonese1749eb2011-10-07 13:54:59 -040081 struct {
82 int uid:1;
83 int gid:1;
84 int caps:1;
85 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070086 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040087 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040088 int net:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040089 int seccomp:1;
90 int readonly:1;
91 int usergroups:1;
92 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070093 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040094 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070095 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040096 int chroot:1;
Lee Campbell11af0622014-05-22 12:36:04 -070097 int mount_tmp:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040098 } flags;
99 uid_t uid;
100 gid_t gid;
101 gid_t usergid;
102 char *user;
103 uint64_t caps;
104 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700105 int mountns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800106 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400107 int binding_count;
108 char *chrootdir;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800109 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400110 struct binding *bindings_head;
111 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500112};
113
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700114/*
115 * Strip out flags meant for the parent.
116 * We keep things that are not inherited across execve(2) (e.g. capabilities),
117 * or are easier to set after execve(2) (e.g. seccomp filters).
118 */
119void minijail_preenter(struct minijail *j)
120{
121 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700122 j->flags.enter_vfs = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700123 j->flags.readonly = 0;
124 j->flags.pids = 0;
125}
126
127/*
128 * Strip out flags meant for the child.
129 * We keep things that are inherited across execve(2).
130 */
131void minijail_preexec(struct minijail *j)
132{
133 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700134 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700135 int readonly = j->flags.readonly;
136 if (j->user)
137 free(j->user);
138 j->user = NULL;
139 memset(&j->flags, 0, sizeof(j->flags));
140 /* Now restore anything we meant to keep. */
141 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700142 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700143 j->flags.readonly = readonly;
144 /* Note, |pids| will already have been used before this call. */
145}
146
147/* Minijail API. */
148
Will Drewry6ac91122011-10-21 16:38:58 -0500149struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400150{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400151 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400152}
153
Will Drewry6ac91122011-10-21 16:38:58 -0500154void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400155{
156 if (uid == 0)
157 die("useless change to uid 0");
158 j->uid = uid;
159 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400160}
161
Will Drewry6ac91122011-10-21 16:38:58 -0500162void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400163{
164 if (gid == 0)
165 die("useless change to gid 0");
166 j->gid = gid;
167 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400168}
169
Will Drewry6ac91122011-10-21 16:38:58 -0500170int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400171{
172 char *buf = NULL;
173 struct passwd pw;
174 struct passwd *ppw = NULL;
175 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
176 if (sz == -1)
177 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400178
Elly Jonesdd3e8512012-01-23 15:13:38 -0500179 /*
180 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400181 * the maximum needed size of the buffer, so we don't have to search.
182 */
183 buf = malloc(sz);
184 if (!buf)
185 return -ENOMEM;
186 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500187 /*
188 * We're safe to free the buffer here. The strings inside pw point
189 * inside buf, but we don't use any of them; this leaves the pointers
190 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
191 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400192 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700193 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400194 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700195 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400196 minijail_change_uid(j, ppw->pw_uid);
197 j->user = strdup(user);
198 if (!j->user)
199 return -ENOMEM;
200 j->usergid = ppw->pw_gid;
201 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400202}
203
Will Drewry6ac91122011-10-21 16:38:58 -0500204int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400205{
206 char *buf = NULL;
207 struct group gr;
208 struct group *pgr = NULL;
209 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
210 if (sz == -1)
211 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400212
Elly Jonesdd3e8512012-01-23 15:13:38 -0500213 /*
214 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400215 * the maximum needed size of the buffer, so we don't have to search.
216 */
217 buf = malloc(sz);
218 if (!buf)
219 return -ENOMEM;
220 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500221 /*
222 * We're safe to free the buffer here. The strings inside gr point
223 * inside buf, but we don't use any of them; this leaves the pointers
224 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
225 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400226 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700227 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400228 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700229 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400230 minijail_change_gid(j, pgr->gr_gid);
231 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400232}
233
Will Drewry6ac91122011-10-21 16:38:58 -0500234void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400235{
236 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400237}
238
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700239void API minijail_no_new_privs(struct minijail *j)
240{
241 j->flags.no_new_privs = 1;
242}
243
Will Drewry6ac91122011-10-21 16:38:58 -0500244void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400245{
246 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500247}
248
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700249void API minijail_log_seccomp_filter_failures(struct minijail *j)
250{
251 j->flags.log_seccomp_filter = 1;
252}
253
Will Drewry6ac91122011-10-21 16:38:58 -0500254void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400255{
256 j->caps = capmask;
257 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400258}
259
Will Drewry6ac91122011-10-21 16:38:58 -0500260void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400261{
262 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400263}
264
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700265void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
266{
267 int ns_fd = open(ns_path, O_RDONLY);
268 if (ns_fd < 0) {
269 pdie("failed to open namespace '%s'", ns_path);
270 }
271 j->mountns_fd = ns_fd;
272 j->flags.enter_vfs = 1;
273}
274
Will Drewry6ac91122011-10-21 16:38:58 -0500275void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400276{
Elly Jonese58176c2012-01-23 11:46:17 -0500277 j->flags.vfs = 1;
278 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400279 j->flags.pids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400280}
281
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400282void API minijail_namespace_net(struct minijail *j)
283{
284 j->flags.net = 1;
285}
286
Will Drewry6ac91122011-10-21 16:38:58 -0500287void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400288{
289 j->flags.vfs = 1;
290 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400291}
292
Will Drewry6ac91122011-10-21 16:38:58 -0500293void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400294{
295 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400296}
297
Will Drewry6ac91122011-10-21 16:38:58 -0500298void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400299{
300 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400301}
302
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700303int API minijail_enter_chroot(struct minijail *j, const char *dir)
304{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400305 if (j->chrootdir)
306 return -EINVAL;
307 j->chrootdir = strdup(dir);
308 if (!j->chrootdir)
309 return -ENOMEM;
310 j->flags.chroot = 1;
311 return 0;
312}
313
Lee Campbell11af0622014-05-22 12:36:04 -0700314void API minijail_mount_tmp(struct minijail *j)
315{
316 j->flags.mount_tmp = 1;
317}
318
Will Drewry6ac91122011-10-21 16:38:58 -0500319int API minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700320 int writeable)
321{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400322 struct binding *b;
323
324 if (*dest != '/')
325 return -EINVAL;
326 b = calloc(1, sizeof(*b));
327 if (!b)
328 return -ENOMEM;
329 b->dest = strdup(dest);
330 if (!b->dest)
331 goto error;
332 b->src = strdup(src);
333 if (!b->src)
334 goto error;
335 b->writeable = writeable;
336
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700337 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400338
Elly Jonesdd3e8512012-01-23 15:13:38 -0500339 /*
340 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400341 * containing vfs namespace.
342 */
343 minijail_namespace_vfs(j);
344
345 if (j->bindings_tail)
346 j->bindings_tail->next = b;
347 else
348 j->bindings_head = b;
349 j->bindings_tail = b;
350 j->binding_count++;
351
352 return 0;
353
354error:
355 free(b->src);
356 free(b->dest);
357 free(b);
358 return -ENOMEM;
359}
360
Will Drewry6ac91122011-10-21 16:38:58 -0500361void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400362{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700363 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
364 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
365 warn("not loading seccomp filter, seccomp not supported");
366 return;
367 }
368 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400369 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800370 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700371 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400372 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800373
374 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700375 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
376 die("failed to compile seccomp filter BPF program in '%s'",
377 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800378 }
379
380 j->filter_len = fprog->len;
381 j->filter_prog = fprog;
382
Elly Jonese1749eb2011-10-07 13:54:59 -0400383 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500384}
385
Will Drewryf89aef52011-09-16 16:48:57 -0500386struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400387 size_t available;
388 size_t total;
389 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500390};
391
Will Drewry6ac91122011-10-21 16:38:58 -0500392void marshal_state_init(struct marshal_state *state,
393 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400394{
395 state->available = available;
396 state->buf = buf;
397 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500398}
399
Will Drewry6ac91122011-10-21 16:38:58 -0500400void marshal_append(struct marshal_state *state,
401 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400402{
403 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500404
Elly Jonese1749eb2011-10-07 13:54:59 -0400405 /* Up to |available| will be written. */
406 if (copy_len) {
407 memcpy(state->buf, src, copy_len);
408 state->buf += copy_len;
409 state->available -= copy_len;
410 }
411 /* |total| will contain the expected length. */
412 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500413}
414
Will Drewry6ac91122011-10-21 16:38:58 -0500415void minijail_marshal_helper(struct marshal_state *state,
416 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400417{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400418 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400419 marshal_append(state, (char *)j, sizeof(*j));
420 if (j->user)
421 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400422 if (j->chrootdir)
423 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800424 if (j->flags.seccomp_filter && j->filter_prog) {
425 struct sock_fprog *fp = j->filter_prog;
426 marshal_append(state, (char *)fp->filter,
427 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400428 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400429 for (b = j->bindings_head; b; b = b->next) {
430 marshal_append(state, b->src, strlen(b->src) + 1);
431 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700432 marshal_append(state, (char *)&b->writeable,
433 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400434 }
Will Drewryf89aef52011-09-16 16:48:57 -0500435}
436
Will Drewry6ac91122011-10-21 16:38:58 -0500437size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400438{
439 struct marshal_state state;
440 marshal_state_init(&state, NULL, 0);
441 minijail_marshal_helper(&state, j);
442 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500443}
444
Elly Jonese1749eb2011-10-07 13:54:59 -0400445int minijail_marshal(const struct minijail *j, char *buf, size_t available)
446{
447 struct marshal_state state;
448 marshal_state_init(&state, buf, available);
449 minijail_marshal_helper(&state, j);
450 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500451}
452
Elly Jones51a5b6c2011-10-12 19:09:26 -0400453/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
454 * @length Number of bytes to consume
455 * @buf Buffer to consume from
456 * @buflength Size of @buf
457 *
458 * Returns a pointer to the base of the bytes, or NULL for errors.
459 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700460void *consumebytes(size_t length, char **buf, size_t *buflength)
461{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400462 char *p = *buf;
463 if (length > *buflength)
464 return NULL;
465 *buf += length;
466 *buflength -= length;
467 return p;
468}
469
470/* consumestr: consumes a C string from a buffer @buf of length @length
471 * @buf Buffer to consume
472 * @length Length of buffer
473 *
474 * Returns a pointer to the base of the string, or NULL for errors.
475 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700476char *consumestr(char **buf, size_t *buflength)
477{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400478 size_t len = strnlen(*buf, *buflength);
479 if (len == *buflength)
480 /* There's no null-terminator */
481 return NULL;
482 return consumebytes(len + 1, buf, buflength);
483}
484
Elly Jonese1749eb2011-10-07 13:54:59 -0400485int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
486{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400487 int i;
488 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500489 int ret = -EINVAL;
490
Elly Jonese1749eb2011-10-07 13:54:59 -0400491 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500492 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400493 memcpy((void *)j, serialized, sizeof(*j));
494 serialized += sizeof(*j);
495 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500496
Will Drewrybee7ba72011-10-21 20:47:01 -0500497 /* Potentially stale pointers not used as signals. */
498 j->bindings_head = NULL;
499 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800500 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500501
Elly Jonese1749eb2011-10-07 13:54:59 -0400502 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400503 char *user = consumestr(&serialized, &length);
504 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500505 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400506 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500507 if (!j->user)
508 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400509 }
Will Drewryf89aef52011-09-16 16:48:57 -0500510
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400511 if (j->chrootdir) { /* stale pointer */
512 char *chrootdir = consumestr(&serialized, &length);
513 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500514 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400515 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500516 if (!j->chrootdir)
517 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400518 }
519
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800520 if (j->flags.seccomp_filter && j->filter_len > 0) {
521 size_t ninstrs = j->filter_len;
522 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
523 ninstrs > USHRT_MAX)
524 goto bad_filters;
525
526 size_t program_len = ninstrs * sizeof(struct sock_filter);
527 void *program = consumebytes(program_len, &serialized, &length);
528 if (!program)
529 goto bad_filters;
530
531 j->filter_prog = malloc(sizeof(struct sock_fprog));
532 j->filter_prog->len = ninstrs;
533 j->filter_prog->filter = malloc(program_len);
534 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400535 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400536
537 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400538 j->binding_count = 0;
539 for (i = 0; i < count; ++i) {
540 int *writeable;
541 const char *dest;
542 const char *src = consumestr(&serialized, &length);
543 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500544 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400545 dest = consumestr(&serialized, &length);
546 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500547 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400548 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
549 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500550 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400551 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500552 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400553 }
554
Elly Jonese1749eb2011-10-07 13:54:59 -0400555 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500556
557bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800558 if (j->flags.seccomp_filter && j->filter_len > 0) {
559 free(j->filter_prog->filter);
560 free(j->filter_prog);
561 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500562bad_filters:
563 if (j->chrootdir)
564 free(j->chrootdir);
565bad_chrootdir:
566 if (j->user)
567 free(j->user);
568clear_pointers:
569 j->user = NULL;
570 j->chrootdir = NULL;
571out:
572 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500573}
574
Elly Jones51a5b6c2011-10-12 19:09:26 -0400575/* bind_one: Applies bindings from @b for @j, recursing as needed.
576 * @j Minijail these bindings are for
577 * @b Head of list of bindings
578 *
579 * Returns 0 for success.
580 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700581int bind_one(const struct minijail *j, struct binding *b)
582{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400583 int ret = 0;
584 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400585 if (ret)
586 return ret;
587 /* dest has a leading "/" */
588 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
589 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500590 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400591 if (ret)
592 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500593 if (!b->writeable) {
594 ret = mount(b->src, dest, NULL,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700595 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
Elly Jonesa1059632011-12-15 15:17:07 -0500596 if (ret)
597 pdie("bind ro: %s -> %s", b->src, dest);
598 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400599 free(dest);
600 if (b->next)
601 return bind_one(j, b->next);
602 return ret;
603}
604
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700605int enter_chroot(const struct minijail *j)
606{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400607 int ret;
608 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
609 return ret;
610
611 if (chroot(j->chrootdir))
612 return -errno;
613
614 if (chdir("/"))
615 return -errno;
616
617 return 0;
618}
619
Lee Campbell11af0622014-05-22 12:36:04 -0700620int mount_tmp(void)
621{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800622 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700623}
624
Will Drewry6ac91122011-10-21 16:38:58 -0500625int remount_readonly(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400626{
627 const char *kProcPath = "/proc";
628 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500629 /*
630 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400631 * /proc in our namespace, which means using MS_REMOUNT here would
632 * mutate our parent's mount as well, even though we're in a VFS
633 * namespace (!). Instead, remove their mount from our namespace
634 * and make our own.
635 */
636 if (umount(kProcPath))
637 return -errno;
638 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
639 return -errno;
640 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400641}
642
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700643void drop_ugid(const struct minijail *j)
644{
645 if (j->flags.usergroups) {
646 if (initgroups(j->user, j->usergid))
647 pdie("initgroups");
648 } else {
649 /* Only attempt to clear supplemental groups if we are changing
650 * users. */
651 if ((j->uid || j->gid) && setgroups(0, NULL))
652 pdie("setgroups");
653 }
654
655 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
656 pdie("setresgid");
657
658 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
659 pdie("setresuid");
660}
661
Mike Frysinger3adfef72013-05-09 17:19:08 -0400662/*
663 * We specifically do not use cap_valid() as that only tells us the last
664 * valid cap we were *compiled* against (i.e. what the version of kernel
665 * headers says). If we run on a different kernel version, then it's not
666 * uncommon for that to be less (if an older kernel) or more (if a newer
667 * kernel). So suck up the answer via /proc.
668 */
669static int run_cap_valid(unsigned int cap)
670{
671 static unsigned int last_cap;
672
673 if (!last_cap) {
674 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
675 FILE *fp = fopen(cap_file, "re");
676 if (fscanf(fp, "%u", &last_cap) != 1)
677 pdie("fscanf(%s)", cap_file);
678 fclose(fp);
679 }
680
681 return cap <= last_cap;
682}
683
Will Drewry6ac91122011-10-21 16:38:58 -0500684void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400685{
686 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800687 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800688 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400689 unsigned int i;
690 if (!caps)
691 die("can't get process caps");
692 if (cap_clear_flag(caps, CAP_INHERITABLE))
693 die("can't clear inheritable caps");
694 if (cap_clear_flag(caps, CAP_EFFECTIVE))
695 die("can't clear effective caps");
696 if (cap_clear_flag(caps, CAP_PERMITTED))
697 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400698 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800699 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800700 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400701 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800702 flag[0] = i;
703 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400704 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800705 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400706 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800707 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400708 die("can't add inheritable cap");
709 }
710 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800711 die("can't apply initial cleaned capset");
712
713 /*
714 * Instead of dropping bounding set first, do it here in case
715 * the caller had a more permissive bounding set which could
716 * have been used above to raise a capability that wasn't already
717 * present. This requires CAP_SETPCAP, so we raised/kept it above.
718 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400719 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800720 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400721 continue;
722 if (prctl(PR_CAPBSET_DROP, i))
723 pdie("prctl(PR_CAPBSET_DROP)");
724 }
Kees Cook323878a2013-02-05 15:35:24 -0800725
726 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800727 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800728 flag[0] = CAP_SETPCAP;
729 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
730 die("can't clear effective cap");
731 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
732 die("can't clear permitted cap");
733 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
734 die("can't clear inheritable cap");
735 }
736
737 if (cap_set_proc(caps))
738 die("can't apply final cleaned capset");
739
740 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400741}
742
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700743void set_seccomp_filter(const struct minijail *j)
744{
745 /*
746 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
747 * in the kernel source tree for an explanation of the parameters.
748 */
749 if (j->flags.no_new_privs) {
750 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
751 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
752 }
753
754 /*
755 * If we're logging seccomp filter failures,
756 * install the SIGSYS handler first.
757 */
758 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
759 if (install_sigsys_handler())
760 pdie("install SIGSYS handler");
761 warn("logging seccomp filter failures");
762 }
763
764 /*
765 * Install the syscall filter.
766 */
767 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700768 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
769 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
770 warn("seccomp not supported");
771 return;
772 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700773 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700774 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700775 }
776}
777
Will Drewry6ac91122011-10-21 16:38:58 -0500778void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400779{
780 if (j->flags.pids)
781 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700782 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400783
Elly Jonese1749eb2011-10-07 13:54:59 -0400784 if (j->flags.usergroups && !j->user)
785 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400786
Elly Jonesdd3e8512012-01-23 15:13:38 -0500787 /*
788 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400789 * so we don't even try. If any of our operations fail, we abort() the
790 * entire process.
791 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700792 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
793 pdie("setns(CLONE_NEWNS)");
794
Elly Jonese1749eb2011-10-07 13:54:59 -0400795 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400796 pdie("unshare(vfs)");
797
798 if (j->flags.net && unshare(CLONE_NEWNET))
799 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400800
Elly Jones51a5b6c2011-10-12 19:09:26 -0400801 if (j->flags.chroot && enter_chroot(j))
802 pdie("chroot");
803
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800804 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -0700805 pdie("mount_tmp");
806
Elly Jonese1749eb2011-10-07 13:54:59 -0400807 if (j->flags.readonly && remount_readonly())
808 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400809
Elly Jonese1749eb2011-10-07 13:54:59 -0400810 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500811 /*
812 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400813 * capability to change uids, our attempt to use setuid()
814 * below will fail. Hang on to root caps across setuid(), then
815 * lock securebits.
816 */
817 if (prctl(PR_SET_KEEPCAPS, 1))
818 pdie("prctl(PR_SET_KEEPCAPS)");
819 if (prctl
820 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
821 pdie("prctl(PR_SET_SECUREBITS)");
822 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400823
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700824 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700825 * If we're setting no_new_privs, we can drop privileges
826 * before setting seccomp filter. This way filter policies
827 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700828 */
829 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700830 drop_ugid(j);
831 if (j->flags.caps)
832 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700833
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700834 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400835 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700836 /*
837 * If we're not setting no_new_privs,
838 * we need to set seccomp filter *before* dropping privileges.
839 * WARNING: this means that filter policies *must* allow
840 * setgroups()/setresgid()/setresuid() for dropping root and
841 * capget()/capset()/prctl() for dropping caps.
842 */
843 set_seccomp_filter(j);
844
845 drop_ugid(j);
846 if (j->flags.caps)
847 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400848 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400849
Elly Jonesdd3e8512012-01-23 15:13:38 -0500850 /*
851 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400852 * privilege-dropping syscalls :)
853 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700854 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
855 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
856 warn("seccomp not supported");
857 return;
858 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400859 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700860 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400861}
862
Will Drewry6ac91122011-10-21 16:38:58 -0500863/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400864static int init_exitstatus = 0;
865
Will Drewry6ac91122011-10-21 16:38:58 -0500866void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400867{
868 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400869}
870
Will Drewry6ac91122011-10-21 16:38:58 -0500871int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400872{
873 pid_t pid;
874 int status;
875 /* so that we exit with the right status */
876 signal(SIGTERM, init_term);
877 /* TODO(wad) self jail with seccomp_filters here. */
878 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500879 /*
880 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -0400881 * left inside our pid namespace or we get a signal.
882 */
883 if (pid == rootpid)
884 init_exitstatus = status;
885 }
886 if (!WIFEXITED(init_exitstatus))
887 _exit(MINIJAIL_ERR_INIT);
888 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -0400889}
890
Will Drewry6ac91122011-10-21 16:38:58 -0500891int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400892{
893 size_t sz = 0;
894 size_t bytes = read(fd, &sz, sizeof(sz));
895 char *buf;
896 int r;
897 if (sizeof(sz) != bytes)
898 return -EINVAL;
899 if (sz > USHRT_MAX) /* Arbitrary sanity check */
900 return -E2BIG;
901 buf = malloc(sz);
902 if (!buf)
903 return -ENOMEM;
904 bytes = read(fd, buf, sz);
905 if (bytes != sz) {
906 free(buf);
907 return -EINVAL;
908 }
909 r = minijail_unmarshal(j, buf, sz);
910 free(buf);
911 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500912}
913
Will Drewry6ac91122011-10-21 16:38:58 -0500914int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400915{
916 char *buf;
917 size_t sz = minijail_size(j);
918 ssize_t written;
919 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400920
Elly Jonese1749eb2011-10-07 13:54:59 -0400921 if (!sz)
922 return -EINVAL;
923 buf = malloc(sz);
924 r = minijail_marshal(j, buf, sz);
925 if (r) {
926 free(buf);
927 return r;
928 }
929 /* Sends [size][minijail]. */
930 written = write(fd, &sz, sizeof(sz));
931 if (written != sizeof(sz)) {
932 free(buf);
933 return -EFAULT;
934 }
935 written = write(fd, buf, sz);
936 if (written < 0 || (size_t) written != sz) {
937 free(buf);
938 return -EFAULT;
939 }
940 free(buf);
941 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500942}
Elly Jonescd7a9042011-07-22 13:56:51 -0400943
Will Drewry6ac91122011-10-21 16:38:58 -0500944int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400945{
946 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
947 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
948 if (!newenv)
949 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400950
Elly Jonese1749eb2011-10-07 13:54:59 -0400951 /* Only insert a separating space if we have something to separate... */
952 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
953 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400954
Elly Jonese1749eb2011-10-07 13:54:59 -0400955 /* setenv() makes a copy of the string we give it */
956 setenv(kLdPreloadEnvVar, newenv, 1);
957 free(newenv);
958 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400959}
960
Will Drewry6ac91122011-10-21 16:38:58 -0500961int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400962{
963 int r = pipe(fds);
964 char fd_buf[11];
965 if (r)
966 return r;
967 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
968 if (r <= 0)
969 return -EINVAL;
970 setenv(kFdEnvVar, fd_buf, 1);
971 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500972}
973
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800974int setup_pipe_end(int fds[2], size_t index)
975{
976 if (index > 1)
977 return -1;
978
979 close(fds[1 - index]);
980 return fds[index];
981}
982
983int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
984{
985 if (index > 1)
986 return -1;
987
988 close(fds[1 - index]);
989 /* dup2(2) the corresponding end of the pipe into |fd|. */
990 return dup2(fds[index], fd);
991}
992
Will Drewry6ac91122011-10-21 16:38:58 -0500993int API minijail_run(struct minijail *j, const char *filename,
994 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -0400995{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800996 return minijail_run_pid_pipes(j, filename, argv,
997 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700998}
999
1000int API minijail_run_pid(struct minijail *j, const char *filename,
1001 char *const argv[], pid_t *pchild_pid)
1002{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001003 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
1004 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001005}
1006
1007int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001008 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001009{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001010 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
1011 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001012}
1013
1014int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001015 char *const argv[], pid_t *pchild_pid,
1016 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001017{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001018 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
1019 NULL, NULL);
1020}
1021
1022int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001023 char *const argv[], pid_t *pchild_pid,
1024 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001025{
Elly Jonese1749eb2011-10-07 13:54:59 -04001026 char *oldenv, *oldenv_copy = NULL;
1027 pid_t child_pid;
1028 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001029 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001030 int stdout_fds[2];
1031 int stderr_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001032 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001033 /* We need to remember this across the minijail_preexec() call. */
1034 int pid_namespace = j->flags.pids;
Ben Chan541c7e52011-08-26 14:55:53 -07001035
Elly Jonese1749eb2011-10-07 13:54:59 -04001036 oldenv = getenv(kLdPreloadEnvVar);
1037 if (oldenv) {
1038 oldenv_copy = strdup(oldenv);
1039 if (!oldenv_copy)
1040 return -ENOMEM;
1041 }
Will Drewryf89aef52011-09-16 16:48:57 -05001042
Elly Jonese1749eb2011-10-07 13:54:59 -04001043 if (setup_preload())
1044 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001045
Elly Jonesdd3e8512012-01-23 15:13:38 -05001046 /*
1047 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -04001048 * a pipe(2) to send the minijail configuration over.
1049 */
1050 if (setup_pipe(pipe_fds))
1051 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -04001052
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001053 /*
1054 * If we want to write to the child process' standard input,
1055 * create the pipe(2) now.
1056 */
1057 if (pstdin_fd) {
1058 if (pipe(stdin_fds))
1059 return -EFAULT;
1060 }
1061
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001062 /*
1063 * If we want to read from the child process' standard output,
1064 * create the pipe(2) now.
1065 */
1066 if (pstdout_fd) {
1067 if (pipe(stdout_fds))
1068 return -EFAULT;
1069 }
1070
1071 /*
1072 * If we want to read from the child process' standard error,
1073 * create the pipe(2) now.
1074 */
1075 if (pstderr_fd) {
1076 if (pipe(stderr_fds))
1077 return -EFAULT;
1078 }
1079
Elly Jones761b7412012-06-13 15:49:52 -04001080 /* Use sys_clone() if and only if we're creating a pid namespace.
1081 *
1082 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1083 *
1084 * In multithreaded programs, there are a bunch of locks inside libc,
1085 * some of which may be held by other threads at the time that we call
1086 * minijail_run_pid(). If we call fork(), glibc does its level best to
1087 * ensure that we hold all of these locks before it calls clone()
1088 * internally and drop them after clone() returns, but when we call
1089 * sys_clone(2) directly, all that gets bypassed and we end up with a
1090 * child address space where some of libc's important locks are held by
1091 * other threads (which did not get cloned, and hence will never release
1092 * those locks). This is okay so long as we call exec() immediately
1093 * after, but a bunch of seemingly-innocent libc functions like setenv()
1094 * take locks.
1095 *
1096 * Hence, only call sys_clone() if we need to, in order to get at pid
1097 * namespacing. If we follow this path, the child's address space might
1098 * have broken locks; you may only call functions that do not acquire
1099 * any locks.
1100 *
1101 * Unfortunately, fork() acquires every lock it can get its hands on, as
1102 * previously detailed, so this function is highly likely to deadlock
1103 * later on (see "deadlock here") if we're multithreaded.
1104 *
1105 * We might hack around this by having the clone()d child (init of the
1106 * pid namespace) return directly, rather than leaving the clone()d
1107 * process hanging around to be init for the new namespace (and having
1108 * its fork()ed child return in turn), but that process would be crippled
1109 * with its libc locks potentially broken. We might try fork()ing in the
1110 * parent before we clone() to ensure that we own all the locks, but
1111 * then we have to have the forked child hanging around consuming
1112 * resources (and possibly having file descriptors / shared memory
1113 * regions / etc attached). We'd need to keep the child around to avoid
1114 * having its children get reparented to init.
1115 *
1116 * TODO(ellyjones): figure out if the "forked child hanging around"
1117 * problem is fixable or not. It would be nice if we worked in this
1118 * case.
1119 */
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001120 if (pid_namespace)
Elly Jones761b7412012-06-13 15:49:52 -04001121 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1122 else
1123 child_pid = fork();
1124
Elly Jonese1749eb2011-10-07 13:54:59 -04001125 if (child_pid < 0) {
1126 free(oldenv_copy);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001127 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001128 }
Will Drewryf89aef52011-09-16 16:48:57 -05001129
Elly Jonese1749eb2011-10-07 13:54:59 -04001130 if (child_pid) {
1131 /* Restore parent's LD_PRELOAD. */
1132 if (oldenv_copy) {
1133 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1134 free(oldenv_copy);
1135 } else {
1136 unsetenv(kLdPreloadEnvVar);
1137 }
1138 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001139
Elly Jonese1749eb2011-10-07 13:54:59 -04001140 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001141
1142 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001143 close(pipe_fds[0]); /* read endpoint */
1144 ret = minijail_to_fd(j, pipe_fds[1]);
1145 close(pipe_fds[1]); /* write endpoint */
1146 if (ret) {
1147 kill(j->initpid, SIGKILL);
1148 die("failed to send marshalled minijail");
1149 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001150
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001151 if (pchild_pid)
1152 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001153
1154 /*
1155 * If we want to write to the child process' standard input,
1156 * set up the write end of the pipe.
1157 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001158 if (pstdin_fd)
1159 *pstdin_fd = setup_pipe_end(stdin_fds,
1160 1 /* write end */);
1161
1162 /*
1163 * If we want to read from the child process' standard output,
1164 * set up the read end of the pipe.
1165 */
1166 if (pstdout_fd)
1167 *pstdout_fd = setup_pipe_end(stdout_fds,
1168 0 /* read end */);
1169
1170 /*
1171 * If we want to read from the child process' standard error,
1172 * set up the read end of the pipe.
1173 */
1174 if (pstderr_fd)
1175 *pstderr_fd = setup_pipe_end(stderr_fds,
1176 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001177
Elly Jonese1749eb2011-10-07 13:54:59 -04001178 return 0;
1179 }
1180 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001181
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001182 /*
1183 * If we want to write to the jailed process' standard input,
1184 * set up the read end of the pipe.
1185 */
1186 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001187 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1188 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001189 die("failed to set up stdin pipe");
1190 }
1191
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001192 /*
1193 * If we want to read from the jailed process' standard output,
1194 * set up the write end of the pipe.
1195 */
1196 if (pstdout_fd) {
1197 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1198 STDOUT_FILENO) < 0)
1199 die("failed to set up stdout pipe");
1200 }
1201
1202 /*
1203 * If we want to read from the jailed process' standard error,
1204 * set up the write end of the pipe.
1205 */
1206 if (pstderr_fd) {
1207 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1208 STDERR_FILENO) < 0)
1209 die("failed to set up stderr pipe");
1210 }
1211
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001212 /* Strip out flags that cannot be inherited across execve. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001213 minijail_preexec(j);
1214 /* Jail this process and its descendants... */
1215 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001216
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001217 if (pid_namespace) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001218 /*
1219 * pid namespace: this process will become init inside the new
Elly Jonese1749eb2011-10-07 13:54:59 -04001220 * namespace, so fork off a child to actually run the program
1221 * (we don't want all programs we might exec to have to know
1222 * how to be init).
Elly Jones761b7412012-06-13 15:49:52 -04001223 *
1224 * If we're multithreaded, we'll probably deadlock here. See
1225 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001226 */
1227 child_pid = fork();
1228 if (child_pid < 0)
1229 _exit(child_pid);
1230 else if (child_pid > 0)
1231 init(child_pid); /* never returns */
1232 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001233
Elly Jonesdd3e8512012-01-23 15:13:38 -05001234 /*
1235 * If we aren't pid-namespaced:
Elly Jonese1749eb2011-10-07 13:54:59 -04001236 * calling process
1237 * -> execve()-ing process
1238 * If we are:
1239 * calling process
1240 * -> init()-ing process
1241 * -> execve()-ing process
1242 */
1243 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001244}
1245
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001246int API minijail_run_static(struct minijail *j, const char *filename,
1247 char *const argv[])
1248{
1249 pid_t child_pid;
1250 int pid_namespace = j->flags.pids;
1251
1252 if (j->flags.caps)
1253 die("caps not supported with static targets");
1254
1255 if (pid_namespace)
1256 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1257 else
1258 child_pid = fork();
1259
1260 if (child_pid < 0) {
1261 die("failed to fork child");
1262 }
1263 if (child_pid > 0 ) {
1264 j->initpid = child_pid;
1265 return 0;
1266 }
1267
1268 /*
1269 * We can now drop this child into the sandbox
1270 * then execve the target.
1271 */
1272
1273 j->flags.pids = 0;
1274 minijail_enter(j);
1275
1276 if (pid_namespace) {
1277 /*
1278 * pid namespace: this process will become init inside the new
1279 * namespace, so fork off a child to actually run the program
1280 * (we don't want all programs we might exec to have to know
1281 * how to be init).
1282 *
1283 * If we're multithreaded, we'll probably deadlock here. See
1284 * WARNING above.
1285 */
1286 child_pid = fork();
1287 if (child_pid < 0)
1288 _exit(child_pid);
1289 else if (child_pid > 0)
1290 init(child_pid); /* never returns */
1291 }
1292
1293 _exit(execve(filename, argv, environ));
1294}
1295
Will Drewry6ac91122011-10-21 16:38:58 -05001296int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001297{
1298 int st;
1299 if (kill(j->initpid, SIGTERM))
1300 return -errno;
1301 if (waitpid(j->initpid, &st, 0) < 0)
1302 return -errno;
1303 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001304}
1305
Will Drewry6ac91122011-10-21 16:38:58 -05001306int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001307{
1308 int st;
1309 if (waitpid(j->initpid, &st, 0) < 0)
1310 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001311
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001312 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001313 int error_status = st;
1314 if (WIFSIGNALED(st)) {
1315 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001316 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001317 j->initpid, signum);
1318 /*
1319 * We return MINIJAIL_ERR_JAIL if the process received
1320 * SIGSYS, which happens when a syscall is blocked by
1321 * seccomp filters.
1322 * If not, we do what bash(1) does:
1323 * $? = 128 + signum
1324 */
1325 if (signum == SIGSYS) {
1326 error_status = MINIJAIL_ERR_JAIL;
1327 } else {
1328 error_status = 128 + signum;
1329 }
1330 }
1331 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001332 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001333
1334 int exit_status = WEXITSTATUS(st);
1335 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001336 info("child process %d exited with status %d",
1337 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001338
1339 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001340}
1341
Will Drewry6ac91122011-10-21 16:38:58 -05001342void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001343{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001344 if (j->flags.seccomp_filter && j->filter_prog) {
1345 free(j->filter_prog->filter);
1346 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001347 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001348 while (j->bindings_head) {
1349 struct binding *b = j->bindings_head;
1350 j->bindings_head = j->bindings_head->next;
1351 free(b->dest);
1352 free(b->src);
1353 free(b);
1354 }
1355 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001356 if (j->user)
1357 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001358 if (j->chrootdir)
1359 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001360 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001361}