blob: 0a429c94a2adfc9c7babbdf7a51a8d49662c8e07 [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 Obesa21c8fc2015-07-15 16:22:34 -070039#include "signal_handler.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;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080089 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040090 int seccomp:1;
91 int readonly:1;
92 int usergroups:1;
93 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070094 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040095 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070096 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040097 int chroot:1;
Lee Campbell11af0622014-05-22 12:36:04 -070098 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +080099 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800100 int pid_file:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400101 } flags;
102 uid_t uid;
103 gid_t gid;
104 gid_t usergid;
105 char *user;
106 uint64_t caps;
107 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700108 int mountns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800109 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400110 int binding_count;
111 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800112 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800113 char *uidmap;
114 char *gidmap;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800115 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400116 struct binding *bindings_head;
117 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500118};
119
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700120/*
121 * Strip out flags meant for the parent.
122 * We keep things that are not inherited across execve(2) (e.g. capabilities),
123 * or are easier to set after execve(2) (e.g. seccomp filters).
124 */
125void minijail_preenter(struct minijail *j)
126{
127 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700128 j->flags.enter_vfs = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700129 j->flags.readonly = 0;
130 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800131 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800132 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700133}
134
135/*
136 * Strip out flags meant for the child.
137 * We keep things that are inherited across execve(2).
138 */
139void minijail_preexec(struct minijail *j)
140{
141 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700142 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700143 int readonly = j->flags.readonly;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800144 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700145 if (j->user)
146 free(j->user);
147 j->user = NULL;
148 memset(&j->flags, 0, sizeof(j->flags));
149 /* Now restore anything we meant to keep. */
150 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700151 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700152 j->flags.readonly = readonly;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800153 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700154 /* Note, |pids| will already have been used before this call. */
155}
156
157/* Minijail API. */
158
Will Drewry6ac91122011-10-21 16:38:58 -0500159struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400160{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400161 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400162}
163
Will Drewry6ac91122011-10-21 16:38:58 -0500164void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400165{
166 if (uid == 0)
167 die("useless change to uid 0");
168 j->uid = uid;
169 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400170}
171
Will Drewry6ac91122011-10-21 16:38:58 -0500172void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400173{
174 if (gid == 0)
175 die("useless change to gid 0");
176 j->gid = gid;
177 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400178}
179
Will Drewry6ac91122011-10-21 16:38:58 -0500180int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400181{
182 char *buf = NULL;
183 struct passwd pw;
184 struct passwd *ppw = NULL;
185 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
186 if (sz == -1)
187 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400188
Elly Jonesdd3e8512012-01-23 15:13:38 -0500189 /*
190 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400191 * the maximum needed size of the buffer, so we don't have to search.
192 */
193 buf = malloc(sz);
194 if (!buf)
195 return -ENOMEM;
196 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500197 /*
198 * We're safe to free the buffer here. The strings inside pw point
199 * inside buf, but we don't use any of them; this leaves the pointers
200 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
201 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400202 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700203 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400204 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700205 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400206 minijail_change_uid(j, ppw->pw_uid);
207 j->user = strdup(user);
208 if (!j->user)
209 return -ENOMEM;
210 j->usergid = ppw->pw_gid;
211 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400212}
213
Will Drewry6ac91122011-10-21 16:38:58 -0500214int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400215{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700216 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700217 struct group gr;
218 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400219 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
220 if (sz == -1)
221 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400222
Elly Jonesdd3e8512012-01-23 15:13:38 -0500223 /*
224 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400225 * the maximum needed size of the buffer, so we don't have to search.
226 */
227 buf = malloc(sz);
228 if (!buf)
229 return -ENOMEM;
230 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500231 /*
232 * We're safe to free the buffer here. The strings inside gr point
233 * inside buf, but we don't use any of them; this leaves the pointers
234 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
235 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400236 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700237 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400238 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700239 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400240 minijail_change_gid(j, pgr->gr_gid);
241 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400242}
243
Will Drewry6ac91122011-10-21 16:38:58 -0500244void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400245{
246 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400247}
248
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700249void API minijail_no_new_privs(struct minijail *j)
250{
251 j->flags.no_new_privs = 1;
252}
253
Will Drewry6ac91122011-10-21 16:38:58 -0500254void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400255{
256 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500257}
258
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700259void API minijail_log_seccomp_filter_failures(struct minijail *j)
260{
261 j->flags.log_seccomp_filter = 1;
262}
263
Will Drewry6ac91122011-10-21 16:38:58 -0500264void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400265{
266 j->caps = capmask;
267 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400268}
269
Will Drewry6ac91122011-10-21 16:38:58 -0500270void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400271{
272 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400273}
274
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700275void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
276{
277 int ns_fd = open(ns_path, O_RDONLY);
278 if (ns_fd < 0) {
279 pdie("failed to open namespace '%s'", ns_path);
280 }
281 j->mountns_fd = ns_fd;
282 j->flags.enter_vfs = 1;
283}
284
Will Drewry6ac91122011-10-21 16:38:58 -0500285void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400286{
Elly Jonese58176c2012-01-23 11:46:17 -0500287 j->flags.vfs = 1;
288 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400289 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800290 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400291}
292
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400293void API minijail_namespace_net(struct minijail *j)
294{
295 j->flags.net = 1;
296}
297
Will Drewry6ac91122011-10-21 16:38:58 -0500298void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400299{
300 j->flags.vfs = 1;
301 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400302}
303
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800304void API minijail_namespace_user(struct minijail *j)
305{
306 j->flags.userns = 1;
307}
308
309int API minijail_uidmap(struct minijail *j, const char *uidmap)
310{
311 j->uidmap = strdup(uidmap);
312 if (!j->uidmap)
313 return -ENOMEM;
314 return 0;
315}
316
317int API minijail_gidmap(struct minijail *j, const char *gidmap)
318{
319 j->gidmap = strdup(gidmap);
320 if (!j->gidmap)
321 return -ENOMEM;
322 return 0;
323}
324
Will Drewry6ac91122011-10-21 16:38:58 -0500325void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400326{
327 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400328}
329
Will Drewry6ac91122011-10-21 16:38:58 -0500330void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400331{
332 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400333}
334
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800335void API minijail_run_as_init(struct minijail *j)
336{
337 /*
338 * Since the jailed program will become 'init' in the new PID namespace,
339 * Minijail does not need to fork an 'init' process.
340 */
341 j->flags.do_init = 0;
342}
343
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700344int API minijail_enter_chroot(struct minijail *j, const char *dir)
345{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400346 if (j->chrootdir)
347 return -EINVAL;
348 j->chrootdir = strdup(dir);
349 if (!j->chrootdir)
350 return -ENOMEM;
351 j->flags.chroot = 1;
352 return 0;
353}
354
Lee Campbell11af0622014-05-22 12:36:04 -0700355void API minijail_mount_tmp(struct minijail *j)
356{
357 j->flags.mount_tmp = 1;
358}
359
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800360int API minijail_write_pid_file(struct minijail *j, const char *path)
361{
362 j->pid_file_path = strdup(path);
363 if (!j->pid_file_path)
364 return -ENOMEM;
365 j->flags.pid_file = 1;
366 return 0;
367}
368
Will Drewry6ac91122011-10-21 16:38:58 -0500369int API minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700370 int writeable)
371{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400372 struct binding *b;
373
374 if (*dest != '/')
375 return -EINVAL;
376 b = calloc(1, sizeof(*b));
377 if (!b)
378 return -ENOMEM;
379 b->dest = strdup(dest);
380 if (!b->dest)
381 goto error;
382 b->src = strdup(src);
383 if (!b->src)
384 goto error;
385 b->writeable = writeable;
386
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700387 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400388
Elly Jonesdd3e8512012-01-23 15:13:38 -0500389 /*
390 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400391 * containing vfs namespace.
392 */
393 minijail_namespace_vfs(j);
394
395 if (j->bindings_tail)
396 j->bindings_tail->next = b;
397 else
398 j->bindings_head = b;
399 j->bindings_tail = b;
400 j->binding_count++;
401
402 return 0;
403
404error:
405 free(b->src);
406 free(b->dest);
407 free(b);
408 return -ENOMEM;
409}
410
Will Drewry6ac91122011-10-21 16:38:58 -0500411void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400412{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700413 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
414 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
415 warn("not loading seccomp filter, seccomp not supported");
416 return;
417 }
418 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400419 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800420 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700421 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400422 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800423
424 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700425 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
426 die("failed to compile seccomp filter BPF program in '%s'",
427 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800428 }
429
430 j->filter_len = fprog->len;
431 j->filter_prog = fprog;
432
Elly Jonese1749eb2011-10-07 13:54:59 -0400433 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500434}
435
Will Drewryf89aef52011-09-16 16:48:57 -0500436struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400437 size_t available;
438 size_t total;
439 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500440};
441
Will Drewry6ac91122011-10-21 16:38:58 -0500442void marshal_state_init(struct marshal_state *state,
443 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400444{
445 state->available = available;
446 state->buf = buf;
447 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500448}
449
Will Drewry6ac91122011-10-21 16:38:58 -0500450void marshal_append(struct marshal_state *state,
451 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400452{
453 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500454
Elly Jonese1749eb2011-10-07 13:54:59 -0400455 /* Up to |available| will be written. */
456 if (copy_len) {
457 memcpy(state->buf, src, copy_len);
458 state->buf += copy_len;
459 state->available -= copy_len;
460 }
461 /* |total| will contain the expected length. */
462 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500463}
464
Will Drewry6ac91122011-10-21 16:38:58 -0500465void minijail_marshal_helper(struct marshal_state *state,
466 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400467{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400468 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400469 marshal_append(state, (char *)j, sizeof(*j));
470 if (j->user)
471 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400472 if (j->chrootdir)
473 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800474 if (j->flags.seccomp_filter && j->filter_prog) {
475 struct sock_fprog *fp = j->filter_prog;
476 marshal_append(state, (char *)fp->filter,
477 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400478 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400479 for (b = j->bindings_head; b; b = b->next) {
480 marshal_append(state, b->src, strlen(b->src) + 1);
481 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700482 marshal_append(state, (char *)&b->writeable,
483 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400484 }
Will Drewryf89aef52011-09-16 16:48:57 -0500485}
486
Will Drewry6ac91122011-10-21 16:38:58 -0500487size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400488{
489 struct marshal_state state;
490 marshal_state_init(&state, NULL, 0);
491 minijail_marshal_helper(&state, j);
492 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500493}
494
Elly Jonese1749eb2011-10-07 13:54:59 -0400495int minijail_marshal(const struct minijail *j, char *buf, size_t available)
496{
497 struct marshal_state state;
498 marshal_state_init(&state, buf, available);
499 minijail_marshal_helper(&state, j);
500 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500501}
502
Elly Jones51a5b6c2011-10-12 19:09:26 -0400503/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
504 * @length Number of bytes to consume
505 * @buf Buffer to consume from
506 * @buflength Size of @buf
507 *
508 * Returns a pointer to the base of the bytes, or NULL for errors.
509 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700510void *consumebytes(size_t length, char **buf, size_t *buflength)
511{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400512 char *p = *buf;
513 if (length > *buflength)
514 return NULL;
515 *buf += length;
516 *buflength -= length;
517 return p;
518}
519
520/* consumestr: consumes a C string from a buffer @buf of length @length
521 * @buf Buffer to consume
522 * @length Length of buffer
523 *
524 * Returns a pointer to the base of the string, or NULL for errors.
525 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700526char *consumestr(char **buf, size_t *buflength)
527{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400528 size_t len = strnlen(*buf, *buflength);
529 if (len == *buflength)
530 /* There's no null-terminator */
531 return NULL;
532 return consumebytes(len + 1, buf, buflength);
533}
534
Elly Jonese1749eb2011-10-07 13:54:59 -0400535int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
536{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400537 int i;
538 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500539 int ret = -EINVAL;
540
Elly Jonese1749eb2011-10-07 13:54:59 -0400541 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500542 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400543 memcpy((void *)j, serialized, sizeof(*j));
544 serialized += sizeof(*j);
545 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500546
Will Drewrybee7ba72011-10-21 20:47:01 -0500547 /* Potentially stale pointers not used as signals. */
548 j->bindings_head = NULL;
549 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800550 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500551
Elly Jonese1749eb2011-10-07 13:54:59 -0400552 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400553 char *user = consumestr(&serialized, &length);
554 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500555 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400556 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500557 if (!j->user)
558 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400559 }
Will Drewryf89aef52011-09-16 16:48:57 -0500560
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400561 if (j->chrootdir) { /* stale pointer */
562 char *chrootdir = consumestr(&serialized, &length);
563 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500564 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400565 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500566 if (!j->chrootdir)
567 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400568 }
569
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800570 if (j->flags.seccomp_filter && j->filter_len > 0) {
571 size_t ninstrs = j->filter_len;
572 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
573 ninstrs > USHRT_MAX)
574 goto bad_filters;
575
576 size_t program_len = ninstrs * sizeof(struct sock_filter);
577 void *program = consumebytes(program_len, &serialized, &length);
578 if (!program)
579 goto bad_filters;
580
581 j->filter_prog = malloc(sizeof(struct sock_fprog));
582 j->filter_prog->len = ninstrs;
583 j->filter_prog->filter = malloc(program_len);
584 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400585 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400586
587 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400588 j->binding_count = 0;
589 for (i = 0; i < count; ++i) {
590 int *writeable;
591 const char *dest;
592 const char *src = consumestr(&serialized, &length);
593 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500594 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400595 dest = consumestr(&serialized, &length);
596 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500597 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400598 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
599 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500600 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400601 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500602 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400603 }
604
Elly Jonese1749eb2011-10-07 13:54:59 -0400605 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500606
607bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800608 if (j->flags.seccomp_filter && j->filter_len > 0) {
609 free(j->filter_prog->filter);
610 free(j->filter_prog);
611 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500612bad_filters:
613 if (j->chrootdir)
614 free(j->chrootdir);
615bad_chrootdir:
616 if (j->user)
617 free(j->user);
618clear_pointers:
619 j->user = NULL;
620 j->chrootdir = NULL;
621out:
622 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500623}
624
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800625static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
626{
627 int fd, ret, len;
628 size_t sz;
629 char fname[32];
630 close(pipe_fds[0]);
631
632 sz = sizeof(fname);
633 if (j->uidmap) {
634 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
635 if (ret < 0 || ret >= sz)
636 die("failed to write file name of uid_map");
637 fd = open(fname, O_WRONLY);
638 if (fd < 0)
639 pdie("failed to open '%s'", fname);
640 len = strlen(j->uidmap);
641 if (write(fd, j->uidmap, len) < len)
642 die("failed to set uid_map");
643 close(fd);
644 }
645 if (j->gidmap) {
646 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
647 if (ret < 0 || ret >= sz)
648 die("failed to write file name of gid_map");
649 fd = open(fname, O_WRONLY);
650 if (fd < 0)
651 pdie("failed to open '%s'", fname);
652 len = strlen(j->gidmap);
653 if (write(fd, j->gidmap, len) < len)
654 die("failed to set gid_map");
655 close(fd);
656 }
657
658 close(pipe_fds[1]);
659}
660
661static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
662{
663 char buf;
664
665 close(pipe_fds[1]);
666
667 /* Wait for parent to set up uid/gid mappings. */
668 if (read(pipe_fds[0], &buf, 1) != 0)
669 die("failed to sync with parent");
670 close(pipe_fds[0]);
671
672 if (j->uidmap && setresuid(0, 0, 0))
673 pdie("setresuid");
674 if (j->gidmap && setresgid(0, 0, 0))
675 pdie("setresgid");
676}
677
Elly Jones51a5b6c2011-10-12 19:09:26 -0400678/* bind_one: Applies bindings from @b for @j, recursing as needed.
679 * @j Minijail these bindings are for
680 * @b Head of list of bindings
681 *
682 * Returns 0 for success.
683 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700684int bind_one(const struct minijail *j, struct binding *b)
685{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400686 int ret = 0;
687 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400688 if (ret)
689 return ret;
690 /* dest has a leading "/" */
691 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
692 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500693 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400694 if (ret)
695 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500696 if (!b->writeable) {
697 ret = mount(b->src, dest, NULL,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700698 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
Elly Jonesa1059632011-12-15 15:17:07 -0500699 if (ret)
700 pdie("bind ro: %s -> %s", b->src, dest);
701 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400702 free(dest);
703 if (b->next)
704 return bind_one(j, b->next);
705 return ret;
706}
707
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700708int enter_chroot(const struct minijail *j)
709{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400710 int ret;
711 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
712 return ret;
713
714 if (chroot(j->chrootdir))
715 return -errno;
716
717 if (chdir("/"))
718 return -errno;
719
720 return 0;
721}
722
Lee Campbell11af0622014-05-22 12:36:04 -0700723int mount_tmp(void)
724{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800725 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700726}
727
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800728int remount_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400729{
730 const char *kProcPath = "/proc";
731 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500732 /*
733 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400734 * /proc in our namespace, which means using MS_REMOUNT here would
735 * mutate our parent's mount as well, even though we're in a VFS
736 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800737 * and make our own. However, if we are in a new user namespace, /proc
738 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400739 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800740 if (umount(kProcPath) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400741 return -errno;
742 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
743 return -errno;
744 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400745}
746
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800747static void write_pid_file(const struct minijail *j)
748{
749 FILE *fp = fopen(j->pid_file_path, "w");
750
751 if (!fp)
752 pdie("failed to open '%s'", j->pid_file_path);
753 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
754 pdie("fprintf(%s)", j->pid_file_path);
755 if (fclose(fp))
756 pdie("fclose(%s)", j->pid_file_path);
757}
758
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700759void drop_ugid(const struct minijail *j)
760{
761 if (j->flags.usergroups) {
762 if (initgroups(j->user, j->usergid))
763 pdie("initgroups");
764 } else {
765 /* Only attempt to clear supplemental groups if we are changing
766 * users. */
767 if ((j->uid || j->gid) && setgroups(0, NULL))
768 pdie("setgroups");
769 }
770
771 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
772 pdie("setresgid");
773
774 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
775 pdie("setresuid");
776}
777
Mike Frysinger3adfef72013-05-09 17:19:08 -0400778/*
779 * We specifically do not use cap_valid() as that only tells us the last
780 * valid cap we were *compiled* against (i.e. what the version of kernel
781 * headers says). If we run on a different kernel version, then it's not
782 * uncommon for that to be less (if an older kernel) or more (if a newer
783 * kernel). So suck up the answer via /proc.
784 */
785static int run_cap_valid(unsigned int cap)
786{
787 static unsigned int last_cap;
788
789 if (!last_cap) {
790 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
791 FILE *fp = fopen(cap_file, "re");
792 if (fscanf(fp, "%u", &last_cap) != 1)
793 pdie("fscanf(%s)", cap_file);
794 fclose(fp);
795 }
796
797 return cap <= last_cap;
798}
799
Will Drewry6ac91122011-10-21 16:38:58 -0500800void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400801{
802 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800803 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800804 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400805 unsigned int i;
806 if (!caps)
807 die("can't get process caps");
808 if (cap_clear_flag(caps, CAP_INHERITABLE))
809 die("can't clear inheritable caps");
810 if (cap_clear_flag(caps, CAP_EFFECTIVE))
811 die("can't clear effective caps");
812 if (cap_clear_flag(caps, CAP_PERMITTED))
813 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400814 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800815 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800816 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400817 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800818 flag[0] = i;
819 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400820 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800821 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400822 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800823 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400824 die("can't add inheritable cap");
825 }
826 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800827 die("can't apply initial cleaned capset");
828
829 /*
830 * Instead of dropping bounding set first, do it here in case
831 * the caller had a more permissive bounding set which could
832 * have been used above to raise a capability that wasn't already
833 * present. This requires CAP_SETPCAP, so we raised/kept it above.
834 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400835 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800836 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400837 continue;
838 if (prctl(PR_CAPBSET_DROP, i))
839 pdie("prctl(PR_CAPBSET_DROP)");
840 }
Kees Cook323878a2013-02-05 15:35:24 -0800841
842 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800843 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800844 flag[0] = CAP_SETPCAP;
845 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
846 die("can't clear effective cap");
847 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
848 die("can't clear permitted cap");
849 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
850 die("can't clear inheritable cap");
851 }
852
853 if (cap_set_proc(caps))
854 die("can't apply final cleaned capset");
855
856 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400857}
858
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700859void set_seccomp_filter(const struct minijail *j)
860{
861 /*
862 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
863 * in the kernel source tree for an explanation of the parameters.
864 */
865 if (j->flags.no_new_privs) {
866 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
867 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
868 }
869
870 /*
871 * If we're logging seccomp filter failures,
872 * install the SIGSYS handler first.
873 */
874 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
875 if (install_sigsys_handler())
876 pdie("install SIGSYS handler");
877 warn("logging seccomp filter failures");
878 }
879
880 /*
881 * Install the syscall filter.
882 */
883 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700884 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
885 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
886 warn("seccomp not supported");
887 return;
888 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700889 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700890 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700891 }
892}
893
Will Drewry6ac91122011-10-21 16:38:58 -0500894void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400895{
896 if (j->flags.pids)
897 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700898 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400899
Elly Jonese1749eb2011-10-07 13:54:59 -0400900 if (j->flags.usergroups && !j->user)
901 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400902
Elly Jonesdd3e8512012-01-23 15:13:38 -0500903 /*
904 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400905 * so we don't even try. If any of our operations fail, we abort() the
906 * entire process.
907 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700908 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
909 pdie("setns(CLONE_NEWNS)");
910
Elly Jonese1749eb2011-10-07 13:54:59 -0400911 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400912 pdie("unshare(vfs)");
913
914 if (j->flags.net && unshare(CLONE_NEWNET))
915 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400916
Elly Jones51a5b6c2011-10-12 19:09:26 -0400917 if (j->flags.chroot && enter_chroot(j))
918 pdie("chroot");
919
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800920 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -0700921 pdie("mount_tmp");
922
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800923 if (j->flags.readonly && remount_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -0400924 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400925
Elly Jonese1749eb2011-10-07 13:54:59 -0400926 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500927 /*
928 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400929 * capability to change uids, our attempt to use setuid()
930 * below will fail. Hang on to root caps across setuid(), then
931 * lock securebits.
932 */
933 if (prctl(PR_SET_KEEPCAPS, 1))
934 pdie("prctl(PR_SET_KEEPCAPS)");
935 if (prctl
936 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
937 pdie("prctl(PR_SET_SECUREBITS)");
938 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400939
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700940 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700941 * If we're setting no_new_privs, we can drop privileges
942 * before setting seccomp filter. This way filter policies
943 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700944 */
945 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700946 drop_ugid(j);
947 if (j->flags.caps)
948 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700949
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700950 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400951 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700952 /*
953 * If we're not setting no_new_privs,
954 * we need to set seccomp filter *before* dropping privileges.
955 * WARNING: this means that filter policies *must* allow
956 * setgroups()/setresgid()/setresuid() for dropping root and
957 * capget()/capset()/prctl() for dropping caps.
958 */
959 set_seccomp_filter(j);
960
961 drop_ugid(j);
962 if (j->flags.caps)
963 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400964 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400965
Elly Jonesdd3e8512012-01-23 15:13:38 -0500966 /*
967 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400968 * privilege-dropping syscalls :)
969 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700970 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
971 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
972 warn("seccomp not supported");
973 return;
974 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400975 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700976 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400977}
978
Will Drewry6ac91122011-10-21 16:38:58 -0500979/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400980static int init_exitstatus = 0;
981
Will Drewry6ac91122011-10-21 16:38:58 -0500982void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400983{
984 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400985}
986
Will Drewry6ac91122011-10-21 16:38:58 -0500987int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400988{
989 pid_t pid;
990 int status;
991 /* so that we exit with the right status */
992 signal(SIGTERM, init_term);
993 /* TODO(wad) self jail with seccomp_filters here. */
994 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500995 /*
996 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -0400997 * left inside our pid namespace or we get a signal.
998 */
999 if (pid == rootpid)
1000 init_exitstatus = status;
1001 }
1002 if (!WIFEXITED(init_exitstatus))
1003 _exit(MINIJAIL_ERR_INIT);
1004 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001005}
1006
Will Drewry6ac91122011-10-21 16:38:58 -05001007int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001008{
1009 size_t sz = 0;
1010 size_t bytes = read(fd, &sz, sizeof(sz));
1011 char *buf;
1012 int r;
1013 if (sizeof(sz) != bytes)
1014 return -EINVAL;
1015 if (sz > USHRT_MAX) /* Arbitrary sanity check */
1016 return -E2BIG;
1017 buf = malloc(sz);
1018 if (!buf)
1019 return -ENOMEM;
1020 bytes = read(fd, buf, sz);
1021 if (bytes != sz) {
1022 free(buf);
1023 return -EINVAL;
1024 }
1025 r = minijail_unmarshal(j, buf, sz);
1026 free(buf);
1027 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001028}
1029
Will Drewry6ac91122011-10-21 16:38:58 -05001030int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001031{
1032 char *buf;
1033 size_t sz = minijail_size(j);
1034 ssize_t written;
1035 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001036
Elly Jonese1749eb2011-10-07 13:54:59 -04001037 if (!sz)
1038 return -EINVAL;
1039 buf = malloc(sz);
1040 r = minijail_marshal(j, buf, sz);
1041 if (r) {
1042 free(buf);
1043 return r;
1044 }
1045 /* Sends [size][minijail]. */
1046 written = write(fd, &sz, sizeof(sz));
1047 if (written != sizeof(sz)) {
1048 free(buf);
1049 return -EFAULT;
1050 }
1051 written = write(fd, buf, sz);
1052 if (written < 0 || (size_t) written != sz) {
1053 free(buf);
1054 return -EFAULT;
1055 }
1056 free(buf);
1057 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001058}
Elly Jonescd7a9042011-07-22 13:56:51 -04001059
Will Drewry6ac91122011-10-21 16:38:58 -05001060int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001061{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001062#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001063 /* Don't use LDPRELOAD on Brillo. */
1064 return 0;
1065#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001066 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1067 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1068 if (!newenv)
1069 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001070
Elly Jonese1749eb2011-10-07 13:54:59 -04001071 /* Only insert a separating space if we have something to separate... */
1072 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1073 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001074
Elly Jonese1749eb2011-10-07 13:54:59 -04001075 /* setenv() makes a copy of the string we give it */
1076 setenv(kLdPreloadEnvVar, newenv, 1);
1077 free(newenv);
1078 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001079#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001080}
1081
Will Drewry6ac91122011-10-21 16:38:58 -05001082int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001083{
1084 int r = pipe(fds);
1085 char fd_buf[11];
1086 if (r)
1087 return r;
1088 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1089 if (r <= 0)
1090 return -EINVAL;
1091 setenv(kFdEnvVar, fd_buf, 1);
1092 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001093}
1094
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001095int setup_pipe_end(int fds[2], size_t index)
1096{
1097 if (index > 1)
1098 return -1;
1099
1100 close(fds[1 - index]);
1101 return fds[index];
1102}
1103
1104int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1105{
1106 if (index > 1)
1107 return -1;
1108
1109 close(fds[1 - index]);
1110 /* dup2(2) the corresponding end of the pipe into |fd|. */
1111 return dup2(fds[index], fd);
1112}
1113
Will Drewry6ac91122011-10-21 16:38:58 -05001114int API minijail_run(struct minijail *j, const char *filename,
1115 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001116{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001117 return minijail_run_pid_pipes(j, filename, argv,
1118 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001119}
1120
1121int API minijail_run_pid(struct minijail *j, const char *filename,
1122 char *const argv[], pid_t *pchild_pid)
1123{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001124 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
1125 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001126}
1127
1128int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001129 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001130{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001131 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
1132 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001133}
1134
1135int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001136 char *const argv[], pid_t *pchild_pid,
1137 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001138{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001139 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
1140 NULL, NULL);
1141}
1142
1143int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001144 char *const argv[], pid_t *pchild_pid,
1145 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001146{
Elly Jonese1749eb2011-10-07 13:54:59 -04001147 char *oldenv, *oldenv_copy = NULL;
1148 pid_t child_pid;
1149 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001150 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001151 int stdout_fds[2];
1152 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001153 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001154 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001155 /* We need to remember this across the minijail_preexec() call. */
1156 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001157 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001158
Elly Jonese1749eb2011-10-07 13:54:59 -04001159 oldenv = getenv(kLdPreloadEnvVar);
1160 if (oldenv) {
1161 oldenv_copy = strdup(oldenv);
1162 if (!oldenv_copy)
1163 return -ENOMEM;
1164 }
Will Drewryf89aef52011-09-16 16:48:57 -05001165
Elly Jonese1749eb2011-10-07 13:54:59 -04001166 if (setup_preload())
1167 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001168
Elly Jonesdd3e8512012-01-23 15:13:38 -05001169 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001170 * Make the process group ID of this process equal to its PID, so that
1171 * both the Minijail process and the jailed process can be killed
1172 * together.
1173 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1174 * the process is already a process group leader.
1175 */
1176 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1177 if (errno != EPERM) {
1178 pdie("setpgid(0, 0)");
1179 }
1180 }
1181
1182 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001183 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -04001184 * a pipe(2) to send the minijail configuration over.
1185 */
1186 if (setup_pipe(pipe_fds))
1187 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -04001188
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001189 /*
1190 * If we want to write to the child process' standard input,
1191 * create the pipe(2) now.
1192 */
1193 if (pstdin_fd) {
1194 if (pipe(stdin_fds))
1195 return -EFAULT;
1196 }
1197
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001198 /*
1199 * If we want to read from the child process' standard output,
1200 * create the pipe(2) now.
1201 */
1202 if (pstdout_fd) {
1203 if (pipe(stdout_fds))
1204 return -EFAULT;
1205 }
1206
1207 /*
1208 * If we want to read from the child process' standard error,
1209 * create the pipe(2) now.
1210 */
1211 if (pstderr_fd) {
1212 if (pipe(stderr_fds))
1213 return -EFAULT;
1214 }
1215
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001216 /*
1217 * If we want to set up a new uid/gid mapping in the user namespace,
1218 * create the pipe(2) to sync between parent and child.
1219 */
1220 if (j->flags.userns) {
1221 if (pipe(userns_pipe_fds))
1222 return -EFAULT;
1223 }
1224
Elly Jones761b7412012-06-13 15:49:52 -04001225 /* Use sys_clone() if and only if we're creating a pid namespace.
1226 *
1227 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1228 *
1229 * In multithreaded programs, there are a bunch of locks inside libc,
1230 * some of which may be held by other threads at the time that we call
1231 * minijail_run_pid(). If we call fork(), glibc does its level best to
1232 * ensure that we hold all of these locks before it calls clone()
1233 * internally and drop them after clone() returns, but when we call
1234 * sys_clone(2) directly, all that gets bypassed and we end up with a
1235 * child address space where some of libc's important locks are held by
1236 * other threads (which did not get cloned, and hence will never release
1237 * those locks). This is okay so long as we call exec() immediately
1238 * after, but a bunch of seemingly-innocent libc functions like setenv()
1239 * take locks.
1240 *
1241 * Hence, only call sys_clone() if we need to, in order to get at pid
1242 * namespacing. If we follow this path, the child's address space might
1243 * have broken locks; you may only call functions that do not acquire
1244 * any locks.
1245 *
1246 * Unfortunately, fork() acquires every lock it can get its hands on, as
1247 * previously detailed, so this function is highly likely to deadlock
1248 * later on (see "deadlock here") if we're multithreaded.
1249 *
1250 * We might hack around this by having the clone()d child (init of the
1251 * pid namespace) return directly, rather than leaving the clone()d
1252 * process hanging around to be init for the new namespace (and having
1253 * its fork()ed child return in turn), but that process would be crippled
1254 * with its libc locks potentially broken. We might try fork()ing in the
1255 * parent before we clone() to ensure that we own all the locks, but
1256 * then we have to have the forked child hanging around consuming
1257 * resources (and possibly having file descriptors / shared memory
1258 * regions / etc attached). We'd need to keep the child around to avoid
1259 * having its children get reparented to init.
1260 *
1261 * TODO(ellyjones): figure out if the "forked child hanging around"
1262 * problem is fixable or not. It would be nice if we worked in this
1263 * case.
1264 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001265 if (pid_namespace) {
1266 int clone_flags = CLONE_NEWPID | SIGCHLD;
1267 if (j->flags.userns)
1268 clone_flags |= CLONE_NEWUSER;
1269 child_pid = syscall(SYS_clone, clone_flags, NULL);
1270 }
Elly Jones761b7412012-06-13 15:49:52 -04001271 else
1272 child_pid = fork();
1273
Elly Jonese1749eb2011-10-07 13:54:59 -04001274 if (child_pid < 0) {
1275 free(oldenv_copy);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001276 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001277 }
Will Drewryf89aef52011-09-16 16:48:57 -05001278
Elly Jonese1749eb2011-10-07 13:54:59 -04001279 if (child_pid) {
1280 /* Restore parent's LD_PRELOAD. */
1281 if (oldenv_copy) {
1282 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1283 free(oldenv_copy);
1284 } else {
1285 unsetenv(kLdPreloadEnvVar);
1286 }
1287 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001288
Elly Jonese1749eb2011-10-07 13:54:59 -04001289 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001290
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001291 if (j->flags.pid_file)
1292 write_pid_file(j);
1293
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001294 if (j->flags.userns)
1295 write_ugid_mappings(j, userns_pipe_fds);
1296
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001297 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001298 close(pipe_fds[0]); /* read endpoint */
1299 ret = minijail_to_fd(j, pipe_fds[1]);
1300 close(pipe_fds[1]); /* write endpoint */
1301 if (ret) {
1302 kill(j->initpid, SIGKILL);
1303 die("failed to send marshalled minijail");
1304 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001305
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001306 if (pchild_pid)
1307 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001308
1309 /*
1310 * If we want to write to the child process' standard input,
1311 * set up the write end of the pipe.
1312 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001313 if (pstdin_fd)
1314 *pstdin_fd = setup_pipe_end(stdin_fds,
1315 1 /* write end */);
1316
1317 /*
1318 * If we want to read from the child process' standard output,
1319 * set up the read end of the pipe.
1320 */
1321 if (pstdout_fd)
1322 *pstdout_fd = setup_pipe_end(stdout_fds,
1323 0 /* read end */);
1324
1325 /*
1326 * If we want to read from the child process' standard error,
1327 * set up the read end of the pipe.
1328 */
1329 if (pstderr_fd)
1330 *pstderr_fd = setup_pipe_end(stderr_fds,
1331 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001332
Elly Jonese1749eb2011-10-07 13:54:59 -04001333 return 0;
1334 }
1335 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001336
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001337
1338 if (j->flags.userns)
1339 enter_user_namespace(j, userns_pipe_fds);
1340
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001341 /*
1342 * If we want to write to the jailed process' standard input,
1343 * set up the read end of the pipe.
1344 */
1345 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001346 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1347 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001348 die("failed to set up stdin pipe");
1349 }
1350
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001351 /*
1352 * If we want to read from the jailed process' standard output,
1353 * set up the write end of the pipe.
1354 */
1355 if (pstdout_fd) {
1356 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1357 STDOUT_FILENO) < 0)
1358 die("failed to set up stdout pipe");
1359 }
1360
1361 /*
1362 * If we want to read from the jailed process' standard error,
1363 * set up the write end of the pipe.
1364 */
1365 if (pstderr_fd) {
1366 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1367 STDERR_FILENO) < 0)
1368 die("failed to set up stderr pipe");
1369 }
1370
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001371 /* Strip out flags that cannot be inherited across execve. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001372 minijail_preexec(j);
1373 /* Jail this process and its descendants... */
1374 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001375
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001376 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001377 /*
1378 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001379 * namespace. We don't want all programs we might exec to have
1380 * to know how to be init. Normally |do_init == 1| we fork off
1381 * a child to actually run the program. If |do_init == 0|, we
1382 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001383 *
1384 * If we're multithreaded, we'll probably deadlock here. See
1385 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001386 */
1387 child_pid = fork();
1388 if (child_pid < 0)
1389 _exit(child_pid);
1390 else if (child_pid > 0)
1391 init(child_pid); /* never returns */
1392 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001393
Elly Jonesdd3e8512012-01-23 15:13:38 -05001394 /*
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001395 * If we aren't pid-namespaced, or jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001396 * calling process
1397 * -> execve()-ing process
1398 * If we are:
1399 * calling process
1400 * -> init()-ing process
1401 * -> execve()-ing process
1402 */
1403 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001404}
1405
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001406int API minijail_run_static(struct minijail *j, const char *filename,
1407 char *const argv[])
1408{
1409 pid_t child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001410 int userns_pipe_fds[2];
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001411 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001412 int do_init = j->flags.do_init;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001413
1414 if (j->flags.caps)
1415 die("caps not supported with static targets");
1416
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001417 /*
1418 * If we want to set up a new uid/gid mapping in the user namespace,
1419 * create the pipe(2) to sync between parent and child.
1420 */
1421 if (j->flags.userns) {
1422 if (pipe(userns_pipe_fds))
1423 return -EFAULT;
1424 }
1425
1426 if (pid_namespace) {
1427 int clone_flags = CLONE_NEWPID | SIGCHLD;
1428 if (j->flags.userns)
1429 clone_flags |= CLONE_NEWUSER;
1430 child_pid = syscall(SYS_clone, clone_flags, NULL);
1431 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001432 else
1433 child_pid = fork();
1434
1435 if (child_pid < 0) {
1436 die("failed to fork child");
1437 }
1438 if (child_pid > 0 ) {
1439 j->initpid = child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001440
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001441 if (j->flags.pid_file)
1442 write_pid_file(j);
1443
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001444 if (j->flags.userns)
1445 write_ugid_mappings(j, userns_pipe_fds);
1446
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001447 return 0;
1448 }
1449
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001450 if (j->flags.userns)
1451 enter_user_namespace(j, userns_pipe_fds);
1452
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001453 /*
1454 * We can now drop this child into the sandbox
1455 * then execve the target.
1456 */
1457
1458 j->flags.pids = 0;
1459 minijail_enter(j);
1460
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001461 if (pid_namespace && do_init) {
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001462 /*
1463 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001464 * namespace. We don't want all programs we might exec to have
1465 * to know how to be init. Normally |do_init == 1| we fork off
1466 * a child to actually run the program. If |do_init == 0|, we
1467 * let the program keep pid 1 and be init.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001468 *
1469 * If we're multithreaded, we'll probably deadlock here. See
1470 * WARNING above.
1471 */
1472 child_pid = fork();
1473 if (child_pid < 0)
1474 _exit(child_pid);
1475 else if (child_pid > 0)
1476 init(child_pid); /* never returns */
1477 }
1478
1479 _exit(execve(filename, argv, environ));
1480}
1481
Will Drewry6ac91122011-10-21 16:38:58 -05001482int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001483{
1484 int st;
1485 if (kill(j->initpid, SIGTERM))
1486 return -errno;
1487 if (waitpid(j->initpid, &st, 0) < 0)
1488 return -errno;
1489 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001490}
1491
Will Drewry6ac91122011-10-21 16:38:58 -05001492int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001493{
1494 int st;
1495 if (waitpid(j->initpid, &st, 0) < 0)
1496 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001497
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001498 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001499 int error_status = st;
1500 if (WIFSIGNALED(st)) {
1501 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001502 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001503 j->initpid, signum);
1504 /*
1505 * We return MINIJAIL_ERR_JAIL if the process received
1506 * SIGSYS, which happens when a syscall is blocked by
1507 * seccomp filters.
1508 * If not, we do what bash(1) does:
1509 * $? = 128 + signum
1510 */
1511 if (signum == SIGSYS) {
1512 error_status = MINIJAIL_ERR_JAIL;
1513 } else {
1514 error_status = 128 + signum;
1515 }
1516 }
1517 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001518 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001519
1520 int exit_status = WEXITSTATUS(st);
1521 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001522 info("child process %d exited with status %d",
1523 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001524
1525 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001526}
1527
Will Drewry6ac91122011-10-21 16:38:58 -05001528void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001529{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001530 if (j->flags.seccomp_filter && j->filter_prog) {
1531 free(j->filter_prog->filter);
1532 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001533 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001534 while (j->bindings_head) {
1535 struct binding *b = j->bindings_head;
1536 j->bindings_head = j->bindings_head->next;
1537 free(b->dest);
1538 free(b->src);
1539 free(b);
1540 }
1541 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001542 if (j->user)
1543 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001544 if (j->chrootdir)
1545 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001546 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001547}