blob: b934c349166ec627144b215aaf284ef6c03d8586 [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 Obes54714502015-09-30 10:08:45 -070021#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080022#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <syscall.h>
27#include <sys/capability.h>
28#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050029#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040030#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070031#include <sys/stat.h>
32#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080033#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <unistd.h>
36
37#include "libminijail.h"
38#include "libminijail-private.h"
39
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070040#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080041#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070042#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043
Lei Zhangeee31552012-10-17 21:27:10 -070044#ifdef HAVE_SECUREBITS_H
45#include <linux/securebits.h>
46#else
47#define SECURE_ALL_BITS 0x15
48#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
49#endif
50
Will Drewry32ac9f52011-08-18 21:36:27 -050051/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080052#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070053# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080054#endif
55
56/* For seccomp_filter using BPF. */
57#ifndef PR_SET_NO_NEW_PRIVS
58# define PR_SET_NO_NEW_PRIVS 38
59#endif
60#ifndef SECCOMP_MODE_FILTER
61# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050062#endif
63
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070064#ifdef USE_SECCOMP_SOFTFAIL
65# define SECCOMP_SOFTFAIL 1
66#else
67# define SECCOMP_SOFTFAIL 0
68#endif
69
Elly Jones51a5b6c2011-10-12 19:09:26 -040070struct binding {
71 char *src;
72 char *dest;
73 int writeable;
74 struct binding *next;
75};
76
Will Drewryf89aef52011-09-16 16:48:57 -050077struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070078 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070079 * WARNING: if you add a flag here you need to make sure it's
80 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070081 */
Elly Jonese1749eb2011-10-07 13:54:59 -040082 struct {
83 int uid:1;
84 int gid:1;
85 int caps:1;
86 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070087 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040088 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040089 int net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080090 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040091 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -070092 int remount_proc_ro:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040093 int usergroups:1;
94 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070095 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040096 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070097 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040098 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +080099 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700100 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800101 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800102 int pid_file:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400103 } flags;
104 uid_t uid;
105 gid_t gid;
106 gid_t usergid;
107 char *user;
108 uint64_t caps;
109 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700110 int mountns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800111 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400112 int binding_count;
113 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800114 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800115 char *uidmap;
116 char *gidmap;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800117 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400118 struct binding *bindings_head;
119 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500120};
121
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700122/*
123 * Strip out flags meant for the parent.
124 * We keep things that are not inherited across execve(2) (e.g. capabilities),
125 * or are easier to set after execve(2) (e.g. seccomp filters).
126 */
127void minijail_preenter(struct minijail *j)
128{
129 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700130 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700131 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700132 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800133 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800134 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700135}
136
137/*
138 * Strip out flags meant for the child.
139 * We keep things that are inherited across execve(2).
140 */
141void minijail_preexec(struct minijail *j)
142{
143 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700144 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700145 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800146 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700147 if (j->user)
148 free(j->user);
149 j->user = NULL;
150 memset(&j->flags, 0, sizeof(j->flags));
151 /* Now restore anything we meant to keep. */
152 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700153 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700154 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800155 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700156 /* Note, |pids| will already have been used before this call. */
157}
158
159/* Minijail API. */
160
Will Drewry6ac91122011-10-21 16:38:58 -0500161struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400162{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400163 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400164}
165
Will Drewry6ac91122011-10-21 16:38:58 -0500166void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400167{
168 if (uid == 0)
169 die("useless change to uid 0");
170 j->uid = uid;
171 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400172}
173
Will Drewry6ac91122011-10-21 16:38:58 -0500174void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400175{
176 if (gid == 0)
177 die("useless change to gid 0");
178 j->gid = gid;
179 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400180}
181
Will Drewry6ac91122011-10-21 16:38:58 -0500182int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400183{
184 char *buf = NULL;
185 struct passwd pw;
186 struct passwd *ppw = NULL;
187 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
188 if (sz == -1)
189 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400190
Elly Jonesdd3e8512012-01-23 15:13:38 -0500191 /*
192 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400193 * the maximum needed size of the buffer, so we don't have to search.
194 */
195 buf = malloc(sz);
196 if (!buf)
197 return -ENOMEM;
198 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500199 /*
200 * We're safe to free the buffer here. The strings inside pw point
201 * inside buf, but we don't use any of them; this leaves the pointers
202 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
203 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400204 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700205 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400206 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700207 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400208 minijail_change_uid(j, ppw->pw_uid);
209 j->user = strdup(user);
210 if (!j->user)
211 return -ENOMEM;
212 j->usergid = ppw->pw_gid;
213 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400214}
215
Will Drewry6ac91122011-10-21 16:38:58 -0500216int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400217{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700218 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700219 struct group gr;
220 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400221 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
222 if (sz == -1)
223 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400224
Elly Jonesdd3e8512012-01-23 15:13:38 -0500225 /*
226 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400227 * the maximum needed size of the buffer, so we don't have to search.
228 */
229 buf = malloc(sz);
230 if (!buf)
231 return -ENOMEM;
232 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500233 /*
234 * We're safe to free the buffer here. The strings inside gr point
235 * inside buf, but we don't use any of them; this leaves the pointers
236 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
237 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400238 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700239 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400240 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700241 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400242 minijail_change_gid(j, pgr->gr_gid);
243 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400244}
245
Will Drewry6ac91122011-10-21 16:38:58 -0500246void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400247{
248 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400249}
250
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700251void API minijail_no_new_privs(struct minijail *j)
252{
253 j->flags.no_new_privs = 1;
254}
255
Will Drewry6ac91122011-10-21 16:38:58 -0500256void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400257{
258 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500259}
260
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700261void API minijail_log_seccomp_filter_failures(struct minijail *j)
262{
263 j->flags.log_seccomp_filter = 1;
264}
265
Will Drewry6ac91122011-10-21 16:38:58 -0500266void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400267{
268 j->caps = capmask;
269 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400270}
271
Will Drewry6ac91122011-10-21 16:38:58 -0500272void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400273{
274 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400275}
276
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700277void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
278{
279 int ns_fd = open(ns_path, O_RDONLY);
280 if (ns_fd < 0) {
281 pdie("failed to open namespace '%s'", ns_path);
282 }
283 j->mountns_fd = ns_fd;
284 j->flags.enter_vfs = 1;
285}
286
Will Drewry6ac91122011-10-21 16:38:58 -0500287void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400288{
Elly Jonese58176c2012-01-23 11:46:17 -0500289 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700290 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400291 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800292 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400293}
294
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400295void API minijail_namespace_net(struct minijail *j)
296{
297 j->flags.net = 1;
298}
299
Dylan Reid791f5772015-09-14 20:02:42 -0700300void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400301{
302 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700303 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400304}
305
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800306void API minijail_namespace_user(struct minijail *j)
307{
308 j->flags.userns = 1;
309}
310
311int API minijail_uidmap(struct minijail *j, const char *uidmap)
312{
313 j->uidmap = strdup(uidmap);
314 if (!j->uidmap)
315 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800316 char *ch;
317 for (ch = j->uidmap; *ch; ch++) {
318 if (*ch == ',')
319 *ch = '\n';
320 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800321 return 0;
322}
323
324int API minijail_gidmap(struct minijail *j, const char *gidmap)
325{
326 j->gidmap = strdup(gidmap);
327 if (!j->gidmap)
328 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800329 char *ch;
330 for (ch = j->gidmap; *ch; ch++) {
331 if (*ch == ',')
332 *ch = '\n';
333 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800334 return 0;
335}
336
Will Drewry6ac91122011-10-21 16:38:58 -0500337void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400338{
339 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400340}
341
Will Drewry6ac91122011-10-21 16:38:58 -0500342void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400343{
344 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400345}
346
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800347void API minijail_run_as_init(struct minijail *j)
348{
349 /*
350 * Since the jailed program will become 'init' in the new PID namespace,
351 * Minijail does not need to fork an 'init' process.
352 */
353 j->flags.do_init = 0;
354}
355
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700356int API minijail_enter_chroot(struct minijail *j, const char *dir)
357{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400358 if (j->chrootdir)
359 return -EINVAL;
360 j->chrootdir = strdup(dir);
361 if (!j->chrootdir)
362 return -ENOMEM;
363 j->flags.chroot = 1;
364 return 0;
365}
366
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800367int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
368{
369 if (j->chrootdir)
370 return -EINVAL;
371 j->chrootdir = strdup(dir);
372 if (!j->chrootdir)
373 return -ENOMEM;
374 j->flags.pivot_root = 1;
375 return 0;
376}
377
Lee Campbell11af0622014-05-22 12:36:04 -0700378void API minijail_mount_tmp(struct minijail *j)
379{
380 j->flags.mount_tmp = 1;
381}
382
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800383int API minijail_write_pid_file(struct minijail *j, const char *path)
384{
385 j->pid_file_path = strdup(path);
386 if (!j->pid_file_path)
387 return -ENOMEM;
388 j->flags.pid_file = 1;
389 return 0;
390}
391
Will Drewry6ac91122011-10-21 16:38:58 -0500392int API minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700393 int writeable)
394{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400395 struct binding *b;
396
397 if (*dest != '/')
398 return -EINVAL;
399 b = calloc(1, sizeof(*b));
400 if (!b)
401 return -ENOMEM;
402 b->dest = strdup(dest);
403 if (!b->dest)
404 goto error;
405 b->src = strdup(src);
406 if (!b->src)
407 goto error;
408 b->writeable = writeable;
409
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700410 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400411
Elly Jonesdd3e8512012-01-23 15:13:38 -0500412 /*
413 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400414 * containing vfs namespace.
415 */
416 minijail_namespace_vfs(j);
417
418 if (j->bindings_tail)
419 j->bindings_tail->next = b;
420 else
421 j->bindings_head = b;
422 j->bindings_tail = b;
423 j->binding_count++;
424
425 return 0;
426
427error:
428 free(b->src);
429 free(b->dest);
430 free(b);
431 return -ENOMEM;
432}
433
Will Drewry6ac91122011-10-21 16:38:58 -0500434void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400435{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700436 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
437 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
438 warn("not loading seccomp filter, seccomp not supported");
439 return;
440 }
441 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400442 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800443 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700444 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400445 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800446
447 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700448 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
449 die("failed to compile seccomp filter BPF program in '%s'",
450 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800451 }
452
453 j->filter_len = fprog->len;
454 j->filter_prog = fprog;
455
Elly Jonese1749eb2011-10-07 13:54:59 -0400456 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500457}
458
Will Drewryf89aef52011-09-16 16:48:57 -0500459struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400460 size_t available;
461 size_t total;
462 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500463};
464
Will Drewry6ac91122011-10-21 16:38:58 -0500465void marshal_state_init(struct marshal_state *state,
466 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400467{
468 state->available = available;
469 state->buf = buf;
470 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500471}
472
Will Drewry6ac91122011-10-21 16:38:58 -0500473void marshal_append(struct marshal_state *state,
474 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400475{
476 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500477
Elly Jonese1749eb2011-10-07 13:54:59 -0400478 /* Up to |available| will be written. */
479 if (copy_len) {
480 memcpy(state->buf, src, copy_len);
481 state->buf += copy_len;
482 state->available -= copy_len;
483 }
484 /* |total| will contain the expected length. */
485 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500486}
487
Will Drewry6ac91122011-10-21 16:38:58 -0500488void minijail_marshal_helper(struct marshal_state *state,
489 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400490{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400491 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400492 marshal_append(state, (char *)j, sizeof(*j));
493 if (j->user)
494 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400495 if (j->chrootdir)
496 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800497 if (j->flags.seccomp_filter && j->filter_prog) {
498 struct sock_fprog *fp = j->filter_prog;
499 marshal_append(state, (char *)fp->filter,
500 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400501 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400502 for (b = j->bindings_head; b; b = b->next) {
503 marshal_append(state, b->src, strlen(b->src) + 1);
504 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700505 marshal_append(state, (char *)&b->writeable,
506 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400507 }
Will Drewryf89aef52011-09-16 16:48:57 -0500508}
509
Will Drewry6ac91122011-10-21 16:38:58 -0500510size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400511{
512 struct marshal_state state;
513 marshal_state_init(&state, NULL, 0);
514 minijail_marshal_helper(&state, j);
515 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500516}
517
Elly Jonese1749eb2011-10-07 13:54:59 -0400518int minijail_marshal(const struct minijail *j, char *buf, size_t available)
519{
520 struct marshal_state state;
521 marshal_state_init(&state, buf, available);
522 minijail_marshal_helper(&state, j);
523 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500524}
525
Elly Jones51a5b6c2011-10-12 19:09:26 -0400526/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
527 * @length Number of bytes to consume
528 * @buf Buffer to consume from
529 * @buflength Size of @buf
530 *
531 * Returns a pointer to the base of the bytes, or NULL for errors.
532 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700533void *consumebytes(size_t length, char **buf, size_t *buflength)
534{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400535 char *p = *buf;
536 if (length > *buflength)
537 return NULL;
538 *buf += length;
539 *buflength -= length;
540 return p;
541}
542
543/* consumestr: consumes a C string from a buffer @buf of length @length
544 * @buf Buffer to consume
545 * @length Length of buffer
546 *
547 * Returns a pointer to the base of the string, or NULL for errors.
548 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700549char *consumestr(char **buf, size_t *buflength)
550{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400551 size_t len = strnlen(*buf, *buflength);
552 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700553 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400554 return NULL;
555 return consumebytes(len + 1, buf, buflength);
556}
557
Elly Jonese1749eb2011-10-07 13:54:59 -0400558int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
559{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400560 int i;
561 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500562 int ret = -EINVAL;
563
Elly Jonese1749eb2011-10-07 13:54:59 -0400564 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500565 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400566 memcpy((void *)j, serialized, sizeof(*j));
567 serialized += sizeof(*j);
568 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500569
Will Drewrybee7ba72011-10-21 20:47:01 -0500570 /* Potentially stale pointers not used as signals. */
571 j->bindings_head = NULL;
572 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800573 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500574
Elly Jonese1749eb2011-10-07 13:54:59 -0400575 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400576 char *user = consumestr(&serialized, &length);
577 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500578 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400579 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500580 if (!j->user)
581 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400582 }
Will Drewryf89aef52011-09-16 16:48:57 -0500583
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400584 if (j->chrootdir) { /* stale pointer */
585 char *chrootdir = consumestr(&serialized, &length);
586 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500587 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400588 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500589 if (!j->chrootdir)
590 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400591 }
592
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800593 if (j->flags.seccomp_filter && j->filter_len > 0) {
594 size_t ninstrs = j->filter_len;
595 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
596 ninstrs > USHRT_MAX)
597 goto bad_filters;
598
599 size_t program_len = ninstrs * sizeof(struct sock_filter);
600 void *program = consumebytes(program_len, &serialized, &length);
601 if (!program)
602 goto bad_filters;
603
604 j->filter_prog = malloc(sizeof(struct sock_fprog));
605 j->filter_prog->len = ninstrs;
606 j->filter_prog->filter = malloc(program_len);
607 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400608 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400609
610 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400611 j->binding_count = 0;
612 for (i = 0; i < count; ++i) {
613 int *writeable;
614 const char *dest;
615 const char *src = consumestr(&serialized, &length);
616 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500617 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400618 dest = consumestr(&serialized, &length);
619 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500620 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400621 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
622 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500623 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400624 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500625 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400626 }
627
Elly Jonese1749eb2011-10-07 13:54:59 -0400628 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500629
630bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800631 if (j->flags.seccomp_filter && j->filter_len > 0) {
632 free(j->filter_prog->filter);
633 free(j->filter_prog);
634 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500635bad_filters:
636 if (j->chrootdir)
637 free(j->chrootdir);
638bad_chrootdir:
639 if (j->user)
640 free(j->user);
641clear_pointers:
642 j->user = NULL;
643 j->chrootdir = NULL;
644out:
645 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500646}
647
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800648static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
649{
650 int fd, ret, len;
651 size_t sz;
652 char fname[32];
653 close(pipe_fds[0]);
654
655 sz = sizeof(fname);
656 if (j->uidmap) {
657 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
658 if (ret < 0 || ret >= sz)
659 die("failed to write file name of uid_map");
660 fd = open(fname, O_WRONLY);
661 if (fd < 0)
662 pdie("failed to open '%s'", fname);
663 len = strlen(j->uidmap);
664 if (write(fd, j->uidmap, len) < len)
665 die("failed to set uid_map");
666 close(fd);
667 }
668 if (j->gidmap) {
669 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
670 if (ret < 0 || ret >= sz)
671 die("failed to write file name of gid_map");
672 fd = open(fname, O_WRONLY);
673 if (fd < 0)
674 pdie("failed to open '%s'", fname);
675 len = strlen(j->gidmap);
676 if (write(fd, j->gidmap, len) < len)
677 die("failed to set gid_map");
678 close(fd);
679 }
680
681 close(pipe_fds[1]);
682}
683
684static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
685{
686 char buf;
687
688 close(pipe_fds[1]);
689
690 /* Wait for parent to set up uid/gid mappings. */
691 if (read(pipe_fds[0], &buf, 1) != 0)
692 die("failed to sync with parent");
693 close(pipe_fds[0]);
694
695 if (j->uidmap && setresuid(0, 0, 0))
696 pdie("setresuid");
697 if (j->gidmap && setresgid(0, 0, 0))
698 pdie("setresgid");
699}
700
Elly Jones51a5b6c2011-10-12 19:09:26 -0400701/* bind_one: Applies bindings from @b for @j, recursing as needed.
702 * @j Minijail these bindings are for
703 * @b Head of list of bindings
704 *
705 * Returns 0 for success.
706 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700707int bind_one(const struct minijail *j, struct binding *b)
708{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400709 int ret = 0;
710 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400711 if (ret)
712 return ret;
713 /* dest has a leading "/" */
714 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
715 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500716 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400717 if (ret)
718 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500719 if (!b->writeable) {
720 ret = mount(b->src, dest, NULL,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700721 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
Elly Jonesa1059632011-12-15 15:17:07 -0500722 if (ret)
723 pdie("bind ro: %s -> %s", b->src, dest);
724 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400725 free(dest);
726 if (b->next)
727 return bind_one(j, b->next);
728 return ret;
729}
730
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700731int enter_chroot(const struct minijail *j)
732{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400733 int ret;
734 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
735 return ret;
736
737 if (chroot(j->chrootdir))
738 return -errno;
739
740 if (chdir("/"))
741 return -errno;
742
743 return 0;
744}
745
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800746int enter_pivot_root(const struct minijail *j)
747{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800748 int ret, oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800749 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
750 return ret;
751
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800752 /* Keep the fd for both old and new root. It will be used in fchdir later. */
753 oldroot = open("/", O_DIRECTORY | O_RDONLY);
754 if (oldroot < 0)
755 pdie("failed to open / for fchdir");
756 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
757 if (newroot < 0)
758 pdie("failed to open %s for fchdir", j->chrootdir);
759
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800760 /* To ensure chrootdir is the root of a file system, do a self bind mount. */
761 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
762 pdie("failed to bind mount '%s'", j->chrootdir);
763 if (chdir(j->chrootdir))
764 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800765 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800766 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800767
768 /*
769 * Now the old root is mounted on top of the new root. Use fchdir to
770 * change to the old root and unmount it.
771 */
772 if (fchdir(oldroot))
773 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800774 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800775 if (umount2(".", MNT_DETACH))
776 pdie("umount(/)");
777 /* Change back to the new root. */
778 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800779 return -errno;
780 if (chroot("/"))
781 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800782
783 return 0;
784}
785
Lee Campbell11af0622014-05-22 12:36:04 -0700786int mount_tmp(void)
787{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800788 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700789}
790
Dylan Reid791f5772015-09-14 20:02:42 -0700791int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400792{
793 const char *kProcPath = "/proc";
794 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500795 /*
796 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400797 * /proc in our namespace, which means using MS_REMOUNT here would
798 * mutate our parent's mount as well, even though we're in a VFS
799 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800800 * and make our own. However, if we are in a new user namespace, /proc
801 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400802 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800803 if (umount(kProcPath) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400804 return -errno;
805 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
806 return -errno;
807 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400808}
809
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800810static void write_pid_file(const struct minijail *j)
811{
812 FILE *fp = fopen(j->pid_file_path, "w");
813
814 if (!fp)
815 pdie("failed to open '%s'", j->pid_file_path);
816 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
817 pdie("fprintf(%s)", j->pid_file_path);
818 if (fclose(fp))
819 pdie("fclose(%s)", j->pid_file_path);
820}
821
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700822void drop_ugid(const struct minijail *j)
823{
824 if (j->flags.usergroups) {
825 if (initgroups(j->user, j->usergid))
826 pdie("initgroups");
827 } else {
828 /* Only attempt to clear supplemental groups if we are changing
829 * users. */
830 if ((j->uid || j->gid) && setgroups(0, NULL))
831 pdie("setgroups");
832 }
833
834 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
835 pdie("setresgid");
836
837 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
838 pdie("setresuid");
839}
840
Mike Frysinger3adfef72013-05-09 17:19:08 -0400841/*
842 * We specifically do not use cap_valid() as that only tells us the last
843 * valid cap we were *compiled* against (i.e. what the version of kernel
844 * headers says). If we run on a different kernel version, then it's not
845 * uncommon for that to be less (if an older kernel) or more (if a newer
846 * kernel). So suck up the answer via /proc.
847 */
848static int run_cap_valid(unsigned int cap)
849{
850 static unsigned int last_cap;
851
852 if (!last_cap) {
853 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
854 FILE *fp = fopen(cap_file, "re");
855 if (fscanf(fp, "%u", &last_cap) != 1)
856 pdie("fscanf(%s)", cap_file);
857 fclose(fp);
858 }
859
860 return cap <= last_cap;
861}
862
Will Drewry6ac91122011-10-21 16:38:58 -0500863void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400864{
865 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800866 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800867 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400868 unsigned int i;
869 if (!caps)
870 die("can't get process caps");
871 if (cap_clear_flag(caps, CAP_INHERITABLE))
872 die("can't clear inheritable caps");
873 if (cap_clear_flag(caps, CAP_EFFECTIVE))
874 die("can't clear effective caps");
875 if (cap_clear_flag(caps, CAP_PERMITTED))
876 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400877 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800878 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800879 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400880 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800881 flag[0] = i;
882 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400883 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800884 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400885 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800886 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400887 die("can't add inheritable cap");
888 }
889 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800890 die("can't apply initial cleaned capset");
891
892 /*
893 * Instead of dropping bounding set first, do it here in case
894 * the caller had a more permissive bounding set which could
895 * have been used above to raise a capability that wasn't already
896 * present. This requires CAP_SETPCAP, so we raised/kept it above.
897 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400898 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800899 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400900 continue;
901 if (prctl(PR_CAPBSET_DROP, i))
902 pdie("prctl(PR_CAPBSET_DROP)");
903 }
Kees Cook323878a2013-02-05 15:35:24 -0800904
905 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800906 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800907 flag[0] = CAP_SETPCAP;
908 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
909 die("can't clear effective cap");
910 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
911 die("can't clear permitted cap");
912 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
913 die("can't clear inheritable cap");
914 }
915
916 if (cap_set_proc(caps))
917 die("can't apply final cleaned capset");
918
919 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400920}
921
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700922void set_seccomp_filter(const struct minijail *j)
923{
924 /*
925 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
926 * in the kernel source tree for an explanation of the parameters.
927 */
928 if (j->flags.no_new_privs) {
929 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
930 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
931 }
932
933 /*
934 * If we're logging seccomp filter failures,
935 * install the SIGSYS handler first.
936 */
937 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
938 if (install_sigsys_handler())
939 pdie("install SIGSYS handler");
940 warn("logging seccomp filter failures");
941 }
942
943 /*
944 * Install the syscall filter.
945 */
946 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700947 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
948 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
949 warn("seccomp not supported");
950 return;
951 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700952 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700953 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700954 }
955}
956
Will Drewry6ac91122011-10-21 16:38:58 -0500957void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400958{
959 if (j->flags.pids)
960 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700961 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400962
Elly Jonese1749eb2011-10-07 13:54:59 -0400963 if (j->flags.usergroups && !j->user)
964 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400965
Elly Jonesdd3e8512012-01-23 15:13:38 -0500966 /*
967 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400968 * so we don't even try. If any of our operations fail, we abort() the
969 * entire process.
970 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700971 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
972 pdie("setns(CLONE_NEWNS)");
973
Elly Jonese1749eb2011-10-07 13:54:59 -0400974 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400975 pdie("unshare(vfs)");
976
977 if (j->flags.net && unshare(CLONE_NEWNET))
978 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400979
Elly Jones51a5b6c2011-10-12 19:09:26 -0400980 if (j->flags.chroot && enter_chroot(j))
981 pdie("chroot");
982
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800983 if (j->flags.pivot_root && enter_pivot_root(j))
984 pdie("pivot_root");
985
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800986 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -0700987 pdie("mount_tmp");
988
Dylan Reid791f5772015-09-14 20:02:42 -0700989 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -0400990 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400991
Elly Jonese1749eb2011-10-07 13:54:59 -0400992 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500993 /*
994 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400995 * capability to change uids, our attempt to use setuid()
996 * below will fail. Hang on to root caps across setuid(), then
997 * lock securebits.
998 */
999 if (prctl(PR_SET_KEEPCAPS, 1))
1000 pdie("prctl(PR_SET_KEEPCAPS)");
1001 if (prctl
1002 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1003 pdie("prctl(PR_SET_SECUREBITS)");
1004 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001005
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001006 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001007 * If we're setting no_new_privs, we can drop privileges
1008 * before setting seccomp filter. This way filter policies
1009 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001010 */
1011 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001012 drop_ugid(j);
1013 if (j->flags.caps)
1014 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001015
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001016 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001017 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001018 /*
1019 * If we're not setting no_new_privs,
1020 * we need to set seccomp filter *before* dropping privileges.
1021 * WARNING: this means that filter policies *must* allow
1022 * setgroups()/setresgid()/setresuid() for dropping root and
1023 * capget()/capset()/prctl() for dropping caps.
1024 */
1025 set_seccomp_filter(j);
1026
1027 drop_ugid(j);
1028 if (j->flags.caps)
1029 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001030 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001031
Elly Jonesdd3e8512012-01-23 15:13:38 -05001032 /*
1033 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001034 * privilege-dropping syscalls :)
1035 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001036 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1037 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1038 warn("seccomp not supported");
1039 return;
1040 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001041 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001042 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001043}
1044
Will Drewry6ac91122011-10-21 16:38:58 -05001045/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001046static int init_exitstatus = 0;
1047
Will Drewry6ac91122011-10-21 16:38:58 -05001048void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001049{
1050 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001051}
1052
Will Drewry6ac91122011-10-21 16:38:58 -05001053int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001054{
1055 pid_t pid;
1056 int status;
1057 /* so that we exit with the right status */
1058 signal(SIGTERM, init_term);
1059 /* TODO(wad) self jail with seccomp_filters here. */
1060 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001061 /*
1062 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001063 * left inside our pid namespace or we get a signal.
1064 */
1065 if (pid == rootpid)
1066 init_exitstatus = status;
1067 }
1068 if (!WIFEXITED(init_exitstatus))
1069 _exit(MINIJAIL_ERR_INIT);
1070 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001071}
1072
Will Drewry6ac91122011-10-21 16:38:58 -05001073int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001074{
1075 size_t sz = 0;
1076 size_t bytes = read(fd, &sz, sizeof(sz));
1077 char *buf;
1078 int r;
1079 if (sizeof(sz) != bytes)
1080 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001081 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001082 return -E2BIG;
1083 buf = malloc(sz);
1084 if (!buf)
1085 return -ENOMEM;
1086 bytes = read(fd, buf, sz);
1087 if (bytes != sz) {
1088 free(buf);
1089 return -EINVAL;
1090 }
1091 r = minijail_unmarshal(j, buf, sz);
1092 free(buf);
1093 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001094}
1095
Will Drewry6ac91122011-10-21 16:38:58 -05001096int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001097{
1098 char *buf;
1099 size_t sz = minijail_size(j);
1100 ssize_t written;
1101 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001102
Elly Jonese1749eb2011-10-07 13:54:59 -04001103 if (!sz)
1104 return -EINVAL;
1105 buf = malloc(sz);
1106 r = minijail_marshal(j, buf, sz);
1107 if (r) {
1108 free(buf);
1109 return r;
1110 }
1111 /* Sends [size][minijail]. */
1112 written = write(fd, &sz, sizeof(sz));
1113 if (written != sizeof(sz)) {
1114 free(buf);
1115 return -EFAULT;
1116 }
1117 written = write(fd, buf, sz);
1118 if (written < 0 || (size_t) written != sz) {
1119 free(buf);
1120 return -EFAULT;
1121 }
1122 free(buf);
1123 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001124}
Elly Jonescd7a9042011-07-22 13:56:51 -04001125
Will Drewry6ac91122011-10-21 16:38:58 -05001126int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001127{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001128#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001129 /* Don't use LDPRELOAD on Brillo. */
1130 return 0;
1131#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001132 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1133 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1134 if (!newenv)
1135 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001136
Elly Jonese1749eb2011-10-07 13:54:59 -04001137 /* Only insert a separating space if we have something to separate... */
1138 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1139 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001140
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001141 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001142 setenv(kLdPreloadEnvVar, newenv, 1);
1143 free(newenv);
1144 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001145#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001146}
1147
Will Drewry6ac91122011-10-21 16:38:58 -05001148int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001149{
1150 int r = pipe(fds);
1151 char fd_buf[11];
1152 if (r)
1153 return r;
1154 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1155 if (r <= 0)
1156 return -EINVAL;
1157 setenv(kFdEnvVar, fd_buf, 1);
1158 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001159}
1160
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001161int setup_pipe_end(int fds[2], size_t index)
1162{
1163 if (index > 1)
1164 return -1;
1165
1166 close(fds[1 - index]);
1167 return fds[index];
1168}
1169
1170int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1171{
1172 if (index > 1)
1173 return -1;
1174
1175 close(fds[1 - index]);
1176 /* dup2(2) the corresponding end of the pipe into |fd|. */
1177 return dup2(fds[index], fd);
1178}
1179
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001180int minijail_run_internal(struct minijail *j, const char *filename,
1181 char *const argv[], pid_t *pchild_pid,
1182 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1183 int use_preload);
1184
Will Drewry6ac91122011-10-21 16:38:58 -05001185int API minijail_run(struct minijail *j, const char *filename,
1186 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001187{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001188 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1189 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001190}
1191
1192int API minijail_run_pid(struct minijail *j, const char *filename,
1193 char *const argv[], pid_t *pchild_pid)
1194{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001195 return minijail_run_internal(j, filename, argv, pchild_pid,
1196 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001197}
1198
1199int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001200 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001201{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001202 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1203 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001204}
1205
1206int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001207 char *const argv[], pid_t *pchild_pid,
1208 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001209{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001210 return minijail_run_internal(j, filename, argv, pchild_pid, pstdin_fd,
1211 NULL, NULL, true);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001212}
1213
1214int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001215 char *const argv[], pid_t *pchild_pid,
1216 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001217{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001218 return minijail_run_internal(j, filename, argv, pchild_pid,
1219 pstdin_fd, pstdout_fd, pstderr_fd, true);
1220}
1221
1222int API minijail_run_no_preload(struct minijail *j, const char *filename,
1223 char *const argv[])
1224{
1225 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1226 false);
1227}
1228
1229int minijail_run_internal(struct minijail *j, const char *filename,
1230 char *const argv[], pid_t *pchild_pid,
1231 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1232 int use_preload)
1233{
Elly Jonese1749eb2011-10-07 13:54:59 -04001234 char *oldenv, *oldenv_copy = NULL;
1235 pid_t child_pid;
1236 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001237 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001238 int stdout_fds[2];
1239 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001240 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001241 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001242 /* We need to remember this across the minijail_preexec() call. */
1243 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001244 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001245
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001246 if (use_preload) {
1247 oldenv = getenv(kLdPreloadEnvVar);
1248 if (oldenv) {
1249 oldenv_copy = strdup(oldenv);
1250 if (!oldenv_copy)
1251 return -ENOMEM;
1252 }
1253
1254 if (setup_preload())
1255 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001256 }
Will Drewryf89aef52011-09-16 16:48:57 -05001257
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001258 if (!use_preload) {
1259 if (j->flags.caps)
1260 die("Capabilities are not supported without "
1261 "LD_PRELOAD");
1262 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001263
Elly Jonesdd3e8512012-01-23 15:13:38 -05001264 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001265 * Make the process group ID of this process equal to its PID, so that
1266 * both the Minijail process and the jailed process can be killed
1267 * together.
1268 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1269 * the process is already a process group leader.
1270 */
1271 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1272 if (errno != EPERM) {
1273 pdie("setpgid(0, 0)");
1274 }
1275 }
1276
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001277 if (use_preload) {
1278 /*
1279 * Before we fork(2) and execve(2) the child process, we need
1280 * to open a pipe(2) to send the minijail configuration over.
1281 */
1282 if (setup_pipe(pipe_fds))
1283 return -EFAULT;
1284 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001285
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001286 /*
1287 * If we want to write to the child process' standard input,
1288 * create the pipe(2) now.
1289 */
1290 if (pstdin_fd) {
1291 if (pipe(stdin_fds))
1292 return -EFAULT;
1293 }
1294
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001295 /*
1296 * If we want to read from the child process' standard output,
1297 * create the pipe(2) now.
1298 */
1299 if (pstdout_fd) {
1300 if (pipe(stdout_fds))
1301 return -EFAULT;
1302 }
1303
1304 /*
1305 * If we want to read from the child process' standard error,
1306 * create the pipe(2) now.
1307 */
1308 if (pstderr_fd) {
1309 if (pipe(stderr_fds))
1310 return -EFAULT;
1311 }
1312
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001313 /*
1314 * If we want to set up a new uid/gid mapping in the user namespace,
1315 * create the pipe(2) to sync between parent and child.
1316 */
1317 if (j->flags.userns) {
1318 if (pipe(userns_pipe_fds))
1319 return -EFAULT;
1320 }
1321
Elly Jones761b7412012-06-13 15:49:52 -04001322 /* Use sys_clone() if and only if we're creating a pid namespace.
1323 *
1324 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1325 *
1326 * In multithreaded programs, there are a bunch of locks inside libc,
1327 * some of which may be held by other threads at the time that we call
1328 * minijail_run_pid(). If we call fork(), glibc does its level best to
1329 * ensure that we hold all of these locks before it calls clone()
1330 * internally and drop them after clone() returns, but when we call
1331 * sys_clone(2) directly, all that gets bypassed and we end up with a
1332 * child address space where some of libc's important locks are held by
1333 * other threads (which did not get cloned, and hence will never release
1334 * those locks). This is okay so long as we call exec() immediately
1335 * after, but a bunch of seemingly-innocent libc functions like setenv()
1336 * take locks.
1337 *
1338 * Hence, only call sys_clone() if we need to, in order to get at pid
1339 * namespacing. If we follow this path, the child's address space might
1340 * have broken locks; you may only call functions that do not acquire
1341 * any locks.
1342 *
1343 * Unfortunately, fork() acquires every lock it can get its hands on, as
1344 * previously detailed, so this function is highly likely to deadlock
1345 * later on (see "deadlock here") if we're multithreaded.
1346 *
1347 * We might hack around this by having the clone()d child (init of the
1348 * pid namespace) return directly, rather than leaving the clone()d
1349 * process hanging around to be init for the new namespace (and having
1350 * its fork()ed child return in turn), but that process would be crippled
1351 * with its libc locks potentially broken. We might try fork()ing in the
1352 * parent before we clone() to ensure that we own all the locks, but
1353 * then we have to have the forked child hanging around consuming
1354 * resources (and possibly having file descriptors / shared memory
1355 * regions / etc attached). We'd need to keep the child around to avoid
1356 * having its children get reparented to init.
1357 *
1358 * TODO(ellyjones): figure out if the "forked child hanging around"
1359 * problem is fixable or not. It would be nice if we worked in this
1360 * case.
1361 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001362 if (pid_namespace) {
1363 int clone_flags = CLONE_NEWPID | SIGCHLD;
1364 if (j->flags.userns)
1365 clone_flags |= CLONE_NEWUSER;
1366 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001367 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001368 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001369 }
Elly Jones761b7412012-06-13 15:49:52 -04001370
Elly Jonese1749eb2011-10-07 13:54:59 -04001371 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001372 if (use_preload) {
1373 free(oldenv_copy);
1374 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001375 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001376 }
Will Drewryf89aef52011-09-16 16:48:57 -05001377
Elly Jonese1749eb2011-10-07 13:54:59 -04001378 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001379 if (use_preload) {
1380 /* Restore parent's LD_PRELOAD. */
1381 if (oldenv_copy) {
1382 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1383 free(oldenv_copy);
1384 } else {
1385 unsetenv(kLdPreloadEnvVar);
1386 }
1387 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001388 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001389
Elly Jonese1749eb2011-10-07 13:54:59 -04001390 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001391
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001392 if (j->flags.pid_file)
1393 write_pid_file(j);
1394
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001395 if (j->flags.userns)
1396 write_ugid_mappings(j, userns_pipe_fds);
1397
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001398 if (use_preload) {
1399 /* Send marshalled minijail. */
1400 close(pipe_fds[0]); /* read endpoint */
1401 ret = minijail_to_fd(j, pipe_fds[1]);
1402 close(pipe_fds[1]); /* write endpoint */
1403 if (ret) {
1404 kill(j->initpid, SIGKILL);
1405 die("failed to send marshalled minijail");
1406 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001407 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001408
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001409 if (pchild_pid)
1410 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001411
1412 /*
1413 * If we want to write to the child process' standard input,
1414 * set up the write end of the pipe.
1415 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001416 if (pstdin_fd)
1417 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001418 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001419
1420 /*
1421 * If we want to read from the child process' standard output,
1422 * set up the read end of the pipe.
1423 */
1424 if (pstdout_fd)
1425 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001426 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001427
1428 /*
1429 * If we want to read from the child process' standard error,
1430 * set up the read end of the pipe.
1431 */
1432 if (pstderr_fd)
1433 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001434 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001435
Elly Jonese1749eb2011-10-07 13:54:59 -04001436 return 0;
1437 }
1438 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001439
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001440 if (j->flags.userns)
1441 enter_user_namespace(j, userns_pipe_fds);
1442
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001443 /*
1444 * If we want to write to the jailed process' standard input,
1445 * set up the read end of the pipe.
1446 */
1447 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001448 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1449 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001450 die("failed to set up stdin pipe");
1451 }
1452
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001453 /*
1454 * If we want to read from the jailed process' standard output,
1455 * set up the write end of the pipe.
1456 */
1457 if (pstdout_fd) {
1458 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1459 STDOUT_FILENO) < 0)
1460 die("failed to set up stdout pipe");
1461 }
1462
1463 /*
1464 * If we want to read from the jailed process' standard error,
1465 * set up the write end of the pipe.
1466 */
1467 if (pstderr_fd) {
1468 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1469 STDERR_FILENO) < 0)
1470 die("failed to set up stderr pipe");
1471 }
1472
Dylan Reid791f5772015-09-14 20:02:42 -07001473 /* If running an init program, let it decide when/how to mount /proc. */
1474 if (pid_namespace && !do_init)
1475 j->flags.remount_proc_ro = 0;
1476
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001477 if (use_preload) {
1478 /* Strip out flags that cannot be inherited across execve(2). */
1479 minijail_preexec(j);
1480 } else {
1481 j->flags.pids = 0;
1482 }
1483 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001484 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001485
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001486 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001487 /*
1488 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001489 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001490 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001491 * a child to actually run the program. If |do_init == 0|, we
1492 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001493 *
1494 * If we're multithreaded, we'll probably deadlock here. See
1495 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001496 */
1497 child_pid = fork();
1498 if (child_pid < 0)
1499 _exit(child_pid);
1500 else if (child_pid > 0)
1501 init(child_pid); /* never returns */
1502 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001503
Elly Jonesdd3e8512012-01-23 15:13:38 -05001504 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001505 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001506 * calling process
1507 * -> execve()-ing process
1508 * If we are:
1509 * calling process
1510 * -> init()-ing process
1511 * -> execve()-ing process
1512 */
1513 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001514}
1515
Will Drewry6ac91122011-10-21 16:38:58 -05001516int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001517{
1518 int st;
1519 if (kill(j->initpid, SIGTERM))
1520 return -errno;
1521 if (waitpid(j->initpid, &st, 0) < 0)
1522 return -errno;
1523 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001524}
1525
Will Drewry6ac91122011-10-21 16:38:58 -05001526int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001527{
1528 int st;
1529 if (waitpid(j->initpid, &st, 0) < 0)
1530 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001531
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001532 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001533 int error_status = st;
1534 if (WIFSIGNALED(st)) {
1535 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001536 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001537 j->initpid, signum);
1538 /*
1539 * We return MINIJAIL_ERR_JAIL if the process received
1540 * SIGSYS, which happens when a syscall is blocked by
1541 * seccomp filters.
1542 * If not, we do what bash(1) does:
1543 * $? = 128 + signum
1544 */
1545 if (signum == SIGSYS) {
1546 error_status = MINIJAIL_ERR_JAIL;
1547 } else {
1548 error_status = 128 + signum;
1549 }
1550 }
1551 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001552 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001553
1554 int exit_status = WEXITSTATUS(st);
1555 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001556 info("child process %d exited with status %d",
1557 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001558
1559 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001560}
1561
Will Drewry6ac91122011-10-21 16:38:58 -05001562void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001563{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001564 if (j->flags.seccomp_filter && j->filter_prog) {
1565 free(j->filter_prog->filter);
1566 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001567 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001568 while (j->bindings_head) {
1569 struct binding *b = j->bindings_head;
1570 j->bindings_head = j->bindings_head->next;
1571 free(b->dest);
1572 free(b->src);
1573 free(b);
1574 }
1575 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001576 if (j->user)
1577 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001578 if (j->chrootdir)
1579 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001580 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001581}