blob: c66060ca7bedb902a4aa7de92f2401f700ca6508 [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
Dylan Reid08946cc2015-09-16 19:10:57 -0700378char *minijail_get_original_path(struct minijail *j, const char *chroot_path)
379{
380 char *external_path;
381 size_t pathlen;
382
383 if (!j->chrootdir)
384 return strdup(chroot_path);
385
386 /* One extra char for '/' and one for '\0', hence + 2. */
387 pathlen = strlen(chroot_path) + strlen(j->chrootdir) + 2;
388 external_path = malloc(pathlen);
389 snprintf(external_path, pathlen, "%s/%s", j->chrootdir, chroot_path);
390
391 return external_path;
392}
393
Lee Campbell11af0622014-05-22 12:36:04 -0700394void API minijail_mount_tmp(struct minijail *j)
395{
396 j->flags.mount_tmp = 1;
397}
398
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800399int API minijail_write_pid_file(struct minijail *j, const char *path)
400{
401 j->pid_file_path = strdup(path);
402 if (!j->pid_file_path)
403 return -ENOMEM;
404 j->flags.pid_file = 1;
405 return 0;
406}
407
Will Drewry6ac91122011-10-21 16:38:58 -0500408int API minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700409 int writeable)
410{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400411 struct binding *b;
412
413 if (*dest != '/')
414 return -EINVAL;
415 b = calloc(1, sizeof(*b));
416 if (!b)
417 return -ENOMEM;
418 b->dest = strdup(dest);
419 if (!b->dest)
420 goto error;
421 b->src = strdup(src);
422 if (!b->src)
423 goto error;
424 b->writeable = writeable;
425
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700426 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400427
Elly Jonesdd3e8512012-01-23 15:13:38 -0500428 /*
429 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400430 * containing vfs namespace.
431 */
432 minijail_namespace_vfs(j);
433
434 if (j->bindings_tail)
435 j->bindings_tail->next = b;
436 else
437 j->bindings_head = b;
438 j->bindings_tail = b;
439 j->binding_count++;
440
441 return 0;
442
443error:
444 free(b->src);
445 free(b->dest);
446 free(b);
447 return -ENOMEM;
448}
449
Dylan Reid08946cc2015-09-16 19:10:57 -0700450int API minijail_has_bind_mounts(const struct minijail *j)
451{
452 return j->bindings_head != NULL;
453}
454
Will Drewry6ac91122011-10-21 16:38:58 -0500455void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400456{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700457 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
458 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
459 warn("not loading seccomp filter, seccomp not supported");
460 return;
461 }
462 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400463 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800464 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700465 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400466 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800467
468 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700469 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
470 die("failed to compile seccomp filter BPF program in '%s'",
471 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800472 }
473
474 j->filter_len = fprog->len;
475 j->filter_prog = fprog;
476
Elly Jonese1749eb2011-10-07 13:54:59 -0400477 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500478}
479
Will Drewryf89aef52011-09-16 16:48:57 -0500480struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400481 size_t available;
482 size_t total;
483 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500484};
485
Will Drewry6ac91122011-10-21 16:38:58 -0500486void marshal_state_init(struct marshal_state *state,
487 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400488{
489 state->available = available;
490 state->buf = buf;
491 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500492}
493
Will Drewry6ac91122011-10-21 16:38:58 -0500494void marshal_append(struct marshal_state *state,
495 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400496{
497 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500498
Elly Jonese1749eb2011-10-07 13:54:59 -0400499 /* Up to |available| will be written. */
500 if (copy_len) {
501 memcpy(state->buf, src, copy_len);
502 state->buf += copy_len;
503 state->available -= copy_len;
504 }
505 /* |total| will contain the expected length. */
506 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500507}
508
Will Drewry6ac91122011-10-21 16:38:58 -0500509void minijail_marshal_helper(struct marshal_state *state,
510 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400511{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400512 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400513 marshal_append(state, (char *)j, sizeof(*j));
514 if (j->user)
515 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400516 if (j->chrootdir)
517 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800518 if (j->flags.seccomp_filter && j->filter_prog) {
519 struct sock_fprog *fp = j->filter_prog;
520 marshal_append(state, (char *)fp->filter,
521 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400522 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400523 for (b = j->bindings_head; b; b = b->next) {
524 marshal_append(state, b->src, strlen(b->src) + 1);
525 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700526 marshal_append(state, (char *)&b->writeable,
527 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400528 }
Will Drewryf89aef52011-09-16 16:48:57 -0500529}
530
Will Drewry6ac91122011-10-21 16:38:58 -0500531size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400532{
533 struct marshal_state state;
534 marshal_state_init(&state, NULL, 0);
535 minijail_marshal_helper(&state, j);
536 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500537}
538
Elly Jonese1749eb2011-10-07 13:54:59 -0400539int minijail_marshal(const struct minijail *j, char *buf, size_t available)
540{
541 struct marshal_state state;
542 marshal_state_init(&state, buf, available);
543 minijail_marshal_helper(&state, j);
544 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500545}
546
Elly Jones51a5b6c2011-10-12 19:09:26 -0400547/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
548 * @length Number of bytes to consume
549 * @buf Buffer to consume from
550 * @buflength Size of @buf
551 *
552 * Returns a pointer to the base of the bytes, or NULL for errors.
553 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700554void *consumebytes(size_t length, char **buf, size_t *buflength)
555{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400556 char *p = *buf;
557 if (length > *buflength)
558 return NULL;
559 *buf += length;
560 *buflength -= length;
561 return p;
562}
563
564/* consumestr: consumes a C string from a buffer @buf of length @length
565 * @buf Buffer to consume
566 * @length Length of buffer
567 *
568 * Returns a pointer to the base of the string, or NULL for errors.
569 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700570char *consumestr(char **buf, size_t *buflength)
571{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400572 size_t len = strnlen(*buf, *buflength);
573 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700574 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400575 return NULL;
576 return consumebytes(len + 1, buf, buflength);
577}
578
Elly Jonese1749eb2011-10-07 13:54:59 -0400579int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
580{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400581 int i;
582 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500583 int ret = -EINVAL;
584
Elly Jonese1749eb2011-10-07 13:54:59 -0400585 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500586 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400587 memcpy((void *)j, serialized, sizeof(*j));
588 serialized += sizeof(*j);
589 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500590
Will Drewrybee7ba72011-10-21 20:47:01 -0500591 /* Potentially stale pointers not used as signals. */
592 j->bindings_head = NULL;
593 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800594 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500595
Elly Jonese1749eb2011-10-07 13:54:59 -0400596 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400597 char *user = consumestr(&serialized, &length);
598 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500599 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400600 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500601 if (!j->user)
602 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400603 }
Will Drewryf89aef52011-09-16 16:48:57 -0500604
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400605 if (j->chrootdir) { /* stale pointer */
606 char *chrootdir = consumestr(&serialized, &length);
607 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500608 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400609 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500610 if (!j->chrootdir)
611 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400612 }
613
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800614 if (j->flags.seccomp_filter && j->filter_len > 0) {
615 size_t ninstrs = j->filter_len;
616 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
617 ninstrs > USHRT_MAX)
618 goto bad_filters;
619
620 size_t program_len = ninstrs * sizeof(struct sock_filter);
621 void *program = consumebytes(program_len, &serialized, &length);
622 if (!program)
623 goto bad_filters;
624
625 j->filter_prog = malloc(sizeof(struct sock_fprog));
626 j->filter_prog->len = ninstrs;
627 j->filter_prog->filter = malloc(program_len);
628 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400629 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400630
631 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400632 j->binding_count = 0;
633 for (i = 0; i < count; ++i) {
634 int *writeable;
635 const char *dest;
636 const char *src = consumestr(&serialized, &length);
637 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500638 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400639 dest = consumestr(&serialized, &length);
640 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500641 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400642 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
643 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500644 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400645 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500646 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400647 }
648
Elly Jonese1749eb2011-10-07 13:54:59 -0400649 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500650
651bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800652 if (j->flags.seccomp_filter && j->filter_len > 0) {
653 free(j->filter_prog->filter);
654 free(j->filter_prog);
655 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500656bad_filters:
657 if (j->chrootdir)
658 free(j->chrootdir);
659bad_chrootdir:
660 if (j->user)
661 free(j->user);
662clear_pointers:
663 j->user = NULL;
664 j->chrootdir = NULL;
665out:
666 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500667}
668
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800669static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
670{
671 int fd, ret, len;
672 size_t sz;
673 char fname[32];
674 close(pipe_fds[0]);
675
676 sz = sizeof(fname);
677 if (j->uidmap) {
678 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
679 if (ret < 0 || ret >= sz)
680 die("failed to write file name of uid_map");
681 fd = open(fname, O_WRONLY);
682 if (fd < 0)
683 pdie("failed to open '%s'", fname);
684 len = strlen(j->uidmap);
685 if (write(fd, j->uidmap, len) < len)
686 die("failed to set uid_map");
687 close(fd);
688 }
689 if (j->gidmap) {
690 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
691 if (ret < 0 || ret >= sz)
692 die("failed to write file name of gid_map");
693 fd = open(fname, O_WRONLY);
694 if (fd < 0)
695 pdie("failed to open '%s'", fname);
696 len = strlen(j->gidmap);
697 if (write(fd, j->gidmap, len) < len)
698 die("failed to set gid_map");
699 close(fd);
700 }
701
702 close(pipe_fds[1]);
703}
704
705static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
706{
707 char buf;
708
709 close(pipe_fds[1]);
710
711 /* Wait for parent to set up uid/gid mappings. */
712 if (read(pipe_fds[0], &buf, 1) != 0)
713 die("failed to sync with parent");
714 close(pipe_fds[0]);
715
716 if (j->uidmap && setresuid(0, 0, 0))
717 pdie("setresuid");
718 if (j->gidmap && setresgid(0, 0, 0))
719 pdie("setresgid");
720}
721
Elly Jones51a5b6c2011-10-12 19:09:26 -0400722/* bind_one: Applies bindings from @b for @j, recursing as needed.
723 * @j Minijail these bindings are for
724 * @b Head of list of bindings
725 *
726 * Returns 0 for success.
727 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700728int bind_one(const struct minijail *j, struct binding *b)
729{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400730 int ret = 0;
731 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400732 if (ret)
733 return ret;
734 /* dest has a leading "/" */
735 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
736 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500737 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400738 if (ret)
739 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500740 if (!b->writeable) {
741 ret = mount(b->src, dest, NULL,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700742 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
Elly Jonesa1059632011-12-15 15:17:07 -0500743 if (ret)
744 pdie("bind ro: %s -> %s", b->src, dest);
745 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400746 free(dest);
747 if (b->next)
748 return bind_one(j, b->next);
749 return ret;
750}
751
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700752int enter_chroot(const struct minijail *j)
753{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400754 int ret;
755 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
756 return ret;
757
758 if (chroot(j->chrootdir))
759 return -errno;
760
761 if (chdir("/"))
762 return -errno;
763
764 return 0;
765}
766
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800767int enter_pivot_root(const struct minijail *j)
768{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800769 int ret, oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800770 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
771 return ret;
772
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800773 /* Keep the fd for both old and new root. It will be used in fchdir later. */
774 oldroot = open("/", O_DIRECTORY | O_RDONLY);
775 if (oldroot < 0)
776 pdie("failed to open / for fchdir");
777 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
778 if (newroot < 0)
779 pdie("failed to open %s for fchdir", j->chrootdir);
780
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800781 /* To ensure chrootdir is the root of a file system, do a self bind mount. */
782 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
783 pdie("failed to bind mount '%s'", j->chrootdir);
784 if (chdir(j->chrootdir))
785 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800786 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800787 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800788
789 /*
790 * Now the old root is mounted on top of the new root. Use fchdir to
791 * change to the old root and unmount it.
792 */
793 if (fchdir(oldroot))
794 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800795 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800796 if (umount2(".", MNT_DETACH))
797 pdie("umount(/)");
798 /* Change back to the new root. */
799 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800800 return -errno;
801 if (chroot("/"))
802 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -0700803 /* Set correct CWD for getcwd(3). */
804 if (chdir("/"))
805 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800806
807 return 0;
808}
809
Lee Campbell11af0622014-05-22 12:36:04 -0700810int mount_tmp(void)
811{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800812 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700813}
814
Dylan Reid791f5772015-09-14 20:02:42 -0700815int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400816{
817 const char *kProcPath = "/proc";
818 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500819 /*
820 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400821 * /proc in our namespace, which means using MS_REMOUNT here would
822 * mutate our parent's mount as well, even though we're in a VFS
823 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800824 * and make our own. However, if we are in a new user namespace, /proc
825 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400826 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800827 if (umount(kProcPath) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400828 return -errno;
829 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
830 return -errno;
831 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400832}
833
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800834static void write_pid_file(const struct minijail *j)
835{
836 FILE *fp = fopen(j->pid_file_path, "w");
837
838 if (!fp)
839 pdie("failed to open '%s'", j->pid_file_path);
840 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
841 pdie("fprintf(%s)", j->pid_file_path);
842 if (fclose(fp))
843 pdie("fclose(%s)", j->pid_file_path);
844}
845
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700846void drop_ugid(const struct minijail *j)
847{
848 if (j->flags.usergroups) {
849 if (initgroups(j->user, j->usergid))
850 pdie("initgroups");
851 } else {
852 /* Only attempt to clear supplemental groups if we are changing
853 * users. */
854 if ((j->uid || j->gid) && setgroups(0, NULL))
855 pdie("setgroups");
856 }
857
858 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
859 pdie("setresgid");
860
861 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
862 pdie("setresuid");
863}
864
Mike Frysinger3adfef72013-05-09 17:19:08 -0400865/*
866 * We specifically do not use cap_valid() as that only tells us the last
867 * valid cap we were *compiled* against (i.e. what the version of kernel
868 * headers says). If we run on a different kernel version, then it's not
869 * uncommon for that to be less (if an older kernel) or more (if a newer
870 * kernel). So suck up the answer via /proc.
871 */
872static int run_cap_valid(unsigned int cap)
873{
874 static unsigned int last_cap;
875
876 if (!last_cap) {
877 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
878 FILE *fp = fopen(cap_file, "re");
879 if (fscanf(fp, "%u", &last_cap) != 1)
880 pdie("fscanf(%s)", cap_file);
881 fclose(fp);
882 }
883
884 return cap <= last_cap;
885}
886
Will Drewry6ac91122011-10-21 16:38:58 -0500887void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400888{
889 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800890 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800891 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400892 unsigned int i;
893 if (!caps)
894 die("can't get process caps");
895 if (cap_clear_flag(caps, CAP_INHERITABLE))
896 die("can't clear inheritable caps");
897 if (cap_clear_flag(caps, CAP_EFFECTIVE))
898 die("can't clear effective caps");
899 if (cap_clear_flag(caps, CAP_PERMITTED))
900 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400901 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800902 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800903 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400904 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800905 flag[0] = i;
906 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400907 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800908 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400909 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800910 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400911 die("can't add inheritable cap");
912 }
913 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800914 die("can't apply initial cleaned capset");
915
916 /*
917 * Instead of dropping bounding set first, do it here in case
918 * the caller had a more permissive bounding set which could
919 * have been used above to raise a capability that wasn't already
920 * present. This requires CAP_SETPCAP, so we raised/kept it above.
921 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400922 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800923 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400924 continue;
925 if (prctl(PR_CAPBSET_DROP, i))
926 pdie("prctl(PR_CAPBSET_DROP)");
927 }
Kees Cook323878a2013-02-05 15:35:24 -0800928
929 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800930 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800931 flag[0] = CAP_SETPCAP;
932 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
933 die("can't clear effective cap");
934 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
935 die("can't clear permitted cap");
936 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
937 die("can't clear inheritable cap");
938 }
939
940 if (cap_set_proc(caps))
941 die("can't apply final cleaned capset");
942
943 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400944}
945
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700946void set_seccomp_filter(const struct minijail *j)
947{
948 /*
949 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
950 * in the kernel source tree for an explanation of the parameters.
951 */
952 if (j->flags.no_new_privs) {
953 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
954 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
955 }
956
957 /*
958 * If we're logging seccomp filter failures,
959 * install the SIGSYS handler first.
960 */
961 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
962 if (install_sigsys_handler())
963 pdie("install SIGSYS handler");
964 warn("logging seccomp filter failures");
965 }
966
967 /*
968 * Install the syscall filter.
969 */
970 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700971 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
972 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
973 warn("seccomp not supported");
974 return;
975 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700976 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700977 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700978 }
979}
980
Will Drewry6ac91122011-10-21 16:38:58 -0500981void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400982{
983 if (j->flags.pids)
984 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700985 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400986
Elly Jonese1749eb2011-10-07 13:54:59 -0400987 if (j->flags.usergroups && !j->user)
988 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400989
Elly Jonesdd3e8512012-01-23 15:13:38 -0500990 /*
991 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400992 * so we don't even try. If any of our operations fail, we abort() the
993 * entire process.
994 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700995 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
996 pdie("setns(CLONE_NEWNS)");
997
Elly Jonese1749eb2011-10-07 13:54:59 -0400998 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400999 pdie("unshare(vfs)");
1000
1001 if (j->flags.net && unshare(CLONE_NEWNET))
1002 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -04001003
Elly Jones51a5b6c2011-10-12 19:09:26 -04001004 if (j->flags.chroot && enter_chroot(j))
1005 pdie("chroot");
1006
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001007 if (j->flags.pivot_root && enter_pivot_root(j))
1008 pdie("pivot_root");
1009
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001010 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001011 pdie("mount_tmp");
1012
Dylan Reid791f5772015-09-14 20:02:42 -07001013 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001014 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001015
Elly Jonese1749eb2011-10-07 13:54:59 -04001016 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001017 /*
1018 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001019 * capability to change uids, our attempt to use setuid()
1020 * below will fail. Hang on to root caps across setuid(), then
1021 * lock securebits.
1022 */
1023 if (prctl(PR_SET_KEEPCAPS, 1))
1024 pdie("prctl(PR_SET_KEEPCAPS)");
1025 if (prctl
1026 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1027 pdie("prctl(PR_SET_SECUREBITS)");
1028 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001029
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001030 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001031 * If we're setting no_new_privs, we can drop privileges
1032 * before setting seccomp filter. This way filter policies
1033 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001034 */
1035 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001036 drop_ugid(j);
1037 if (j->flags.caps)
1038 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001039
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001040 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001041 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001042 /*
1043 * If we're not setting no_new_privs,
1044 * we need to set seccomp filter *before* dropping privileges.
1045 * WARNING: this means that filter policies *must* allow
1046 * setgroups()/setresgid()/setresuid() for dropping root and
1047 * capget()/capset()/prctl() for dropping caps.
1048 */
1049 set_seccomp_filter(j);
1050
1051 drop_ugid(j);
1052 if (j->flags.caps)
1053 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001054 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001055
Elly Jonesdd3e8512012-01-23 15:13:38 -05001056 /*
1057 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001058 * privilege-dropping syscalls :)
1059 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001060 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1061 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1062 warn("seccomp not supported");
1063 return;
1064 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001065 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001066 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001067}
1068
Will Drewry6ac91122011-10-21 16:38:58 -05001069/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001070static int init_exitstatus = 0;
1071
Will Drewry6ac91122011-10-21 16:38:58 -05001072void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001073{
1074 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001075}
1076
Will Drewry6ac91122011-10-21 16:38:58 -05001077int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001078{
1079 pid_t pid;
1080 int status;
1081 /* so that we exit with the right status */
1082 signal(SIGTERM, init_term);
1083 /* TODO(wad) self jail with seccomp_filters here. */
1084 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001085 /*
1086 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001087 * left inside our pid namespace or we get a signal.
1088 */
1089 if (pid == rootpid)
1090 init_exitstatus = status;
1091 }
1092 if (!WIFEXITED(init_exitstatus))
1093 _exit(MINIJAIL_ERR_INIT);
1094 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001095}
1096
Will Drewry6ac91122011-10-21 16:38:58 -05001097int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001098{
1099 size_t sz = 0;
1100 size_t bytes = read(fd, &sz, sizeof(sz));
1101 char *buf;
1102 int r;
1103 if (sizeof(sz) != bytes)
1104 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001105 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001106 return -E2BIG;
1107 buf = malloc(sz);
1108 if (!buf)
1109 return -ENOMEM;
1110 bytes = read(fd, buf, sz);
1111 if (bytes != sz) {
1112 free(buf);
1113 return -EINVAL;
1114 }
1115 r = minijail_unmarshal(j, buf, sz);
1116 free(buf);
1117 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001118}
1119
Will Drewry6ac91122011-10-21 16:38:58 -05001120int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001121{
1122 char *buf;
1123 size_t sz = minijail_size(j);
1124 ssize_t written;
1125 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001126
Elly Jonese1749eb2011-10-07 13:54:59 -04001127 if (!sz)
1128 return -EINVAL;
1129 buf = malloc(sz);
1130 r = minijail_marshal(j, buf, sz);
1131 if (r) {
1132 free(buf);
1133 return r;
1134 }
1135 /* Sends [size][minijail]. */
1136 written = write(fd, &sz, sizeof(sz));
1137 if (written != sizeof(sz)) {
1138 free(buf);
1139 return -EFAULT;
1140 }
1141 written = write(fd, buf, sz);
1142 if (written < 0 || (size_t) written != sz) {
1143 free(buf);
1144 return -EFAULT;
1145 }
1146 free(buf);
1147 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001148}
Elly Jonescd7a9042011-07-22 13:56:51 -04001149
Will Drewry6ac91122011-10-21 16:38:58 -05001150int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001151{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001152#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001153 /* Don't use LDPRELOAD on Brillo. */
1154 return 0;
1155#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001156 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1157 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1158 if (!newenv)
1159 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001160
Elly Jonese1749eb2011-10-07 13:54:59 -04001161 /* Only insert a separating space if we have something to separate... */
1162 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1163 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001164
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001165 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001166 setenv(kLdPreloadEnvVar, newenv, 1);
1167 free(newenv);
1168 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001169#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001170}
1171
Will Drewry6ac91122011-10-21 16:38:58 -05001172int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001173{
1174 int r = pipe(fds);
1175 char fd_buf[11];
1176 if (r)
1177 return r;
1178 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1179 if (r <= 0)
1180 return -EINVAL;
1181 setenv(kFdEnvVar, fd_buf, 1);
1182 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001183}
1184
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001185int setup_pipe_end(int fds[2], size_t index)
1186{
1187 if (index > 1)
1188 return -1;
1189
1190 close(fds[1 - index]);
1191 return fds[index];
1192}
1193
1194int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1195{
1196 if (index > 1)
1197 return -1;
1198
1199 close(fds[1 - index]);
1200 /* dup2(2) the corresponding end of the pipe into |fd|. */
1201 return dup2(fds[index], fd);
1202}
1203
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001204int minijail_run_internal(struct minijail *j, const char *filename,
1205 char *const argv[], pid_t *pchild_pid,
1206 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1207 int use_preload);
1208
Will Drewry6ac91122011-10-21 16:38:58 -05001209int API minijail_run(struct minijail *j, const char *filename,
1210 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001211{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001212 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1213 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001214}
1215
1216int API minijail_run_pid(struct minijail *j, const char *filename,
1217 char *const argv[], pid_t *pchild_pid)
1218{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001219 return minijail_run_internal(j, filename, argv, pchild_pid,
1220 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001221}
1222
1223int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001224 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001225{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001226 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1227 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001228}
1229
1230int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001231 char *const argv[], pid_t *pchild_pid,
1232 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001233{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001234 return minijail_run_internal(j, filename, argv, pchild_pid, pstdin_fd,
1235 NULL, NULL, true);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001236}
1237
1238int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001239 char *const argv[], pid_t *pchild_pid,
1240 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001241{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001242 return minijail_run_internal(j, filename, argv, pchild_pid,
1243 pstdin_fd, pstdout_fd, pstderr_fd, true);
1244}
1245
1246int API minijail_run_no_preload(struct minijail *j, const char *filename,
1247 char *const argv[])
1248{
1249 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1250 false);
1251}
1252
1253int minijail_run_internal(struct minijail *j, const char *filename,
1254 char *const argv[], pid_t *pchild_pid,
1255 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1256 int use_preload)
1257{
Elly Jonese1749eb2011-10-07 13:54:59 -04001258 char *oldenv, *oldenv_copy = NULL;
1259 pid_t child_pid;
1260 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001261 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001262 int stdout_fds[2];
1263 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001264 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001265 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001266 /* We need to remember this across the minijail_preexec() call. */
1267 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001268 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001269
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001270 if (use_preload) {
1271 oldenv = getenv(kLdPreloadEnvVar);
1272 if (oldenv) {
1273 oldenv_copy = strdup(oldenv);
1274 if (!oldenv_copy)
1275 return -ENOMEM;
1276 }
1277
1278 if (setup_preload())
1279 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001280 }
Will Drewryf89aef52011-09-16 16:48:57 -05001281
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001282 if (!use_preload) {
1283 if (j->flags.caps)
1284 die("Capabilities are not supported without "
1285 "LD_PRELOAD");
1286 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001287
Elly Jonesdd3e8512012-01-23 15:13:38 -05001288 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001289 * Make the process group ID of this process equal to its PID, so that
1290 * both the Minijail process and the jailed process can be killed
1291 * together.
1292 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1293 * the process is already a process group leader.
1294 */
1295 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1296 if (errno != EPERM) {
1297 pdie("setpgid(0, 0)");
1298 }
1299 }
1300
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001301 if (use_preload) {
1302 /*
1303 * Before we fork(2) and execve(2) the child process, we need
1304 * to open a pipe(2) to send the minijail configuration over.
1305 */
1306 if (setup_pipe(pipe_fds))
1307 return -EFAULT;
1308 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001309
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001310 /*
1311 * If we want to write to the child process' standard input,
1312 * create the pipe(2) now.
1313 */
1314 if (pstdin_fd) {
1315 if (pipe(stdin_fds))
1316 return -EFAULT;
1317 }
1318
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001319 /*
1320 * If we want to read from the child process' standard output,
1321 * create the pipe(2) now.
1322 */
1323 if (pstdout_fd) {
1324 if (pipe(stdout_fds))
1325 return -EFAULT;
1326 }
1327
1328 /*
1329 * If we want to read from the child process' standard error,
1330 * create the pipe(2) now.
1331 */
1332 if (pstderr_fd) {
1333 if (pipe(stderr_fds))
1334 return -EFAULT;
1335 }
1336
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001337 /*
1338 * If we want to set up a new uid/gid mapping in the user namespace,
1339 * create the pipe(2) to sync between parent and child.
1340 */
1341 if (j->flags.userns) {
1342 if (pipe(userns_pipe_fds))
1343 return -EFAULT;
1344 }
1345
Elly Jones761b7412012-06-13 15:49:52 -04001346 /* Use sys_clone() if and only if we're creating a pid namespace.
1347 *
1348 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1349 *
1350 * In multithreaded programs, there are a bunch of locks inside libc,
1351 * some of which may be held by other threads at the time that we call
1352 * minijail_run_pid(). If we call fork(), glibc does its level best to
1353 * ensure that we hold all of these locks before it calls clone()
1354 * internally and drop them after clone() returns, but when we call
1355 * sys_clone(2) directly, all that gets bypassed and we end up with a
1356 * child address space where some of libc's important locks are held by
1357 * other threads (which did not get cloned, and hence will never release
1358 * those locks). This is okay so long as we call exec() immediately
1359 * after, but a bunch of seemingly-innocent libc functions like setenv()
1360 * take locks.
1361 *
1362 * Hence, only call sys_clone() if we need to, in order to get at pid
1363 * namespacing. If we follow this path, the child's address space might
1364 * have broken locks; you may only call functions that do not acquire
1365 * any locks.
1366 *
1367 * Unfortunately, fork() acquires every lock it can get its hands on, as
1368 * previously detailed, so this function is highly likely to deadlock
1369 * later on (see "deadlock here") if we're multithreaded.
1370 *
1371 * We might hack around this by having the clone()d child (init of the
1372 * pid namespace) return directly, rather than leaving the clone()d
1373 * process hanging around to be init for the new namespace (and having
1374 * its fork()ed child return in turn), but that process would be crippled
1375 * with its libc locks potentially broken. We might try fork()ing in the
1376 * parent before we clone() to ensure that we own all the locks, but
1377 * then we have to have the forked child hanging around consuming
1378 * resources (and possibly having file descriptors / shared memory
1379 * regions / etc attached). We'd need to keep the child around to avoid
1380 * having its children get reparented to init.
1381 *
1382 * TODO(ellyjones): figure out if the "forked child hanging around"
1383 * problem is fixable or not. It would be nice if we worked in this
1384 * case.
1385 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001386 if (pid_namespace) {
1387 int clone_flags = CLONE_NEWPID | SIGCHLD;
1388 if (j->flags.userns)
1389 clone_flags |= CLONE_NEWUSER;
1390 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001391 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001392 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001393 }
Elly Jones761b7412012-06-13 15:49:52 -04001394
Elly Jonese1749eb2011-10-07 13:54:59 -04001395 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001396 if (use_preload) {
1397 free(oldenv_copy);
1398 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001399 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001400 }
Will Drewryf89aef52011-09-16 16:48:57 -05001401
Elly Jonese1749eb2011-10-07 13:54:59 -04001402 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001403 if (use_preload) {
1404 /* Restore parent's LD_PRELOAD. */
1405 if (oldenv_copy) {
1406 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1407 free(oldenv_copy);
1408 } else {
1409 unsetenv(kLdPreloadEnvVar);
1410 }
1411 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001412 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001413
Elly Jonese1749eb2011-10-07 13:54:59 -04001414 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001415
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001416 if (j->flags.pid_file)
1417 write_pid_file(j);
1418
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001419 if (j->flags.userns)
1420 write_ugid_mappings(j, userns_pipe_fds);
1421
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001422 if (use_preload) {
1423 /* Send marshalled minijail. */
1424 close(pipe_fds[0]); /* read endpoint */
1425 ret = minijail_to_fd(j, pipe_fds[1]);
1426 close(pipe_fds[1]); /* write endpoint */
1427 if (ret) {
1428 kill(j->initpid, SIGKILL);
1429 die("failed to send marshalled minijail");
1430 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001431 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001432
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001433 if (pchild_pid)
1434 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001435
1436 /*
1437 * If we want to write to the child process' standard input,
1438 * set up the write end of the pipe.
1439 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001440 if (pstdin_fd)
1441 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001442 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001443
1444 /*
1445 * If we want to read from the child process' standard output,
1446 * set up the read end of the pipe.
1447 */
1448 if (pstdout_fd)
1449 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001450 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001451
1452 /*
1453 * If we want to read from the child process' standard error,
1454 * set up the read end of the pipe.
1455 */
1456 if (pstderr_fd)
1457 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001458 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001459
Elly Jonese1749eb2011-10-07 13:54:59 -04001460 return 0;
1461 }
1462 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001463
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001464 if (j->flags.userns)
1465 enter_user_namespace(j, userns_pipe_fds);
1466
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001467 /*
1468 * If we want to write to the jailed process' standard input,
1469 * set up the read end of the pipe.
1470 */
1471 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001472 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1473 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001474 die("failed to set up stdin pipe");
1475 }
1476
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001477 /*
1478 * If we want to read from the jailed process' standard output,
1479 * set up the write end of the pipe.
1480 */
1481 if (pstdout_fd) {
1482 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1483 STDOUT_FILENO) < 0)
1484 die("failed to set up stdout pipe");
1485 }
1486
1487 /*
1488 * If we want to read from the jailed process' standard error,
1489 * set up the write end of the pipe.
1490 */
1491 if (pstderr_fd) {
1492 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1493 STDERR_FILENO) < 0)
1494 die("failed to set up stderr pipe");
1495 }
1496
Dylan Reid791f5772015-09-14 20:02:42 -07001497 /* If running an init program, let it decide when/how to mount /proc. */
1498 if (pid_namespace && !do_init)
1499 j->flags.remount_proc_ro = 0;
1500
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001501 if (use_preload) {
1502 /* Strip out flags that cannot be inherited across execve(2). */
1503 minijail_preexec(j);
1504 } else {
1505 j->flags.pids = 0;
1506 }
1507 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001508 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001509
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001510 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001511 /*
1512 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001513 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001514 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001515 * a child to actually run the program. If |do_init == 0|, we
1516 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001517 *
1518 * If we're multithreaded, we'll probably deadlock here. See
1519 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001520 */
1521 child_pid = fork();
1522 if (child_pid < 0)
1523 _exit(child_pid);
1524 else if (child_pid > 0)
1525 init(child_pid); /* never returns */
1526 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001527
Elly Jonesdd3e8512012-01-23 15:13:38 -05001528 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001529 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001530 * calling process
1531 * -> execve()-ing process
1532 * If we are:
1533 * calling process
1534 * -> init()-ing process
1535 * -> execve()-ing process
1536 */
1537 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001538}
1539
Will Drewry6ac91122011-10-21 16:38:58 -05001540int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001541{
1542 int st;
1543 if (kill(j->initpid, SIGTERM))
1544 return -errno;
1545 if (waitpid(j->initpid, &st, 0) < 0)
1546 return -errno;
1547 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001548}
1549
Will Drewry6ac91122011-10-21 16:38:58 -05001550int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001551{
1552 int st;
1553 if (waitpid(j->initpid, &st, 0) < 0)
1554 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001555
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001556 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001557 int error_status = st;
1558 if (WIFSIGNALED(st)) {
1559 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001560 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001561 j->initpid, signum);
1562 /*
1563 * We return MINIJAIL_ERR_JAIL if the process received
1564 * SIGSYS, which happens when a syscall is blocked by
1565 * seccomp filters.
1566 * If not, we do what bash(1) does:
1567 * $? = 128 + signum
1568 */
1569 if (signum == SIGSYS) {
1570 error_status = MINIJAIL_ERR_JAIL;
1571 } else {
1572 error_status = 128 + signum;
1573 }
1574 }
1575 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001576 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001577
1578 int exit_status = WEXITSTATUS(st);
1579 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001580 info("child process %d exited with status %d",
1581 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001582
1583 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001584}
1585
Will Drewry6ac91122011-10-21 16:38:58 -05001586void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001587{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001588 if (j->flags.seccomp_filter && j->filter_prog) {
1589 free(j->filter_prog->filter);
1590 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001591 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001592 while (j->bindings_head) {
1593 struct binding *b = j->bindings_head;
1594 j->bindings_head = j->bindings_head->next;
1595 free(b->dest);
1596 free(b->src);
1597 free(b);
1598 }
1599 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001600 if (j->user)
1601 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001602 if (j->chrootdir)
1603 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001604 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001605}