blob: 2b055e04b5825275802e10b6374326870d201749 [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 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -0700827 if (umount2(kProcPath, MNT_DETACH) && !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 */
Dylan Reidf682d472015-09-17 21:39:07 -0700872static int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -0400873{
Dylan Reidf682d472015-09-17 21:39:07 -0700874 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
875 FILE *fp = fopen(cap_file, "re");
876 int last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -0400877
Dylan Reidf682d472015-09-17 21:39:07 -0700878 if (fscanf(fp, "%u", &last_valid_cap) != 1)
879 pdie("fscanf(%s)", cap_file);
880 fclose(fp);
Mike Frysinger3adfef72013-05-09 17:19:08 -0400881
Dylan Reidf682d472015-09-17 21:39:07 -0700882 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -0400883}
884
Dylan Reidf682d472015-09-17 21:39:07 -0700885void drop_caps(const struct minijail *j, int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -0400886{
887 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800888 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800889 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400890 unsigned int i;
891 if (!caps)
892 die("can't get process caps");
893 if (cap_clear_flag(caps, CAP_INHERITABLE))
894 die("can't clear inheritable caps");
895 if (cap_clear_flag(caps, CAP_EFFECTIVE))
896 die("can't clear effective caps");
897 if (cap_clear_flag(caps, CAP_PERMITTED))
898 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -0700899 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800900 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800901 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400902 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800903 flag[0] = i;
904 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400905 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800906 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400907 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800908 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400909 die("can't add inheritable cap");
910 }
911 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800912 die("can't apply initial cleaned capset");
913
914 /*
915 * Instead of dropping bounding set first, do it here in case
916 * the caller had a more permissive bounding set which could
917 * have been used above to raise a capability that wasn't already
918 * present. This requires CAP_SETPCAP, so we raised/kept it above.
919 */
Dylan Reidf682d472015-09-17 21:39:07 -0700920 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800921 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400922 continue;
923 if (prctl(PR_CAPBSET_DROP, i))
924 pdie("prctl(PR_CAPBSET_DROP)");
925 }
Kees Cook323878a2013-02-05 15:35:24 -0800926
927 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800928 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800929 flag[0] = CAP_SETPCAP;
930 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
931 die("can't clear effective cap");
932 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
933 die("can't clear permitted cap");
934 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
935 die("can't clear inheritable cap");
936 }
937
938 if (cap_set_proc(caps))
939 die("can't apply final cleaned capset");
940
941 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400942}
943
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700944void set_seccomp_filter(const struct minijail *j)
945{
946 /*
947 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
948 * in the kernel source tree for an explanation of the parameters.
949 */
950 if (j->flags.no_new_privs) {
951 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
952 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
953 }
954
955 /*
956 * If we're logging seccomp filter failures,
957 * install the SIGSYS handler first.
958 */
959 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
960 if (install_sigsys_handler())
961 pdie("install SIGSYS handler");
962 warn("logging seccomp filter failures");
963 }
964
965 /*
966 * Install the syscall filter.
967 */
968 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700969 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
970 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
971 warn("seccomp not supported");
972 return;
973 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700974 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700975 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700976 }
977}
978
Will Drewry6ac91122011-10-21 16:38:58 -0500979void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400980{
Dylan Reidf682d472015-09-17 21:39:07 -0700981 /*
982 * Get the last valid cap from /proc, since /proc can be unmounted
983 * before drop_caps().
984 */
985 int last_valid_cap = get_last_valid_cap();
986
Elly Jonese1749eb2011-10-07 13:54:59 -0400987 if (j->flags.pids)
988 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700989 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400990
Elly Jonese1749eb2011-10-07 13:54:59 -0400991 if (j->flags.usergroups && !j->user)
992 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400993
Elly Jonesdd3e8512012-01-23 15:13:38 -0500994 /*
995 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400996 * so we don't even try. If any of our operations fail, we abort() the
997 * entire process.
998 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700999 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1000 pdie("setns(CLONE_NEWNS)");
1001
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001002 if (j->flags.vfs) {
1003 if (unshare(CLONE_NEWNS))
1004 pdie("unshare(vfs)");
1005 /*
1006 * Remount all filesystems as private. If they are shared
1007 * new bind mounts will creep out of our namespace.
1008 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1009 */
1010 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1011 pdie("mount(/, private)");
1012 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001013
1014 if (j->flags.net && unshare(CLONE_NEWNET))
1015 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -04001016
Elly Jones51a5b6c2011-10-12 19:09:26 -04001017 if (j->flags.chroot && enter_chroot(j))
1018 pdie("chroot");
1019
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001020 if (j->flags.pivot_root && enter_pivot_root(j))
1021 pdie("pivot_root");
1022
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001023 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001024 pdie("mount_tmp");
1025
Dylan Reid791f5772015-09-14 20:02:42 -07001026 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001027 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001028
Elly Jonese1749eb2011-10-07 13:54:59 -04001029 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001030 /*
1031 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001032 * capability to change uids, our attempt to use setuid()
1033 * below will fail. Hang on to root caps across setuid(), then
1034 * lock securebits.
1035 */
1036 if (prctl(PR_SET_KEEPCAPS, 1))
1037 pdie("prctl(PR_SET_KEEPCAPS)");
1038 if (prctl
1039 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1040 pdie("prctl(PR_SET_SECUREBITS)");
1041 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001042
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001043 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001044 * If we're setting no_new_privs, we can drop privileges
1045 * before setting seccomp filter. This way filter policies
1046 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001047 */
1048 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001049 drop_ugid(j);
1050 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001051 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001052
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001053 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001054 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001055 /*
1056 * If we're not setting no_new_privs,
1057 * we need to set seccomp filter *before* dropping privileges.
1058 * WARNING: this means that filter policies *must* allow
1059 * setgroups()/setresgid()/setresuid() for dropping root and
1060 * capget()/capset()/prctl() for dropping caps.
1061 */
1062 set_seccomp_filter(j);
1063
1064 drop_ugid(j);
1065 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001066 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001067 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001068
Elly Jonesdd3e8512012-01-23 15:13:38 -05001069 /*
1070 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001071 * privilege-dropping syscalls :)
1072 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001073 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1074 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1075 warn("seccomp not supported");
1076 return;
1077 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001078 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001079 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001080}
1081
Will Drewry6ac91122011-10-21 16:38:58 -05001082/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001083static int init_exitstatus = 0;
1084
Will Drewry6ac91122011-10-21 16:38:58 -05001085void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001086{
1087 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001088}
1089
Will Drewry6ac91122011-10-21 16:38:58 -05001090int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001091{
1092 pid_t pid;
1093 int status;
1094 /* so that we exit with the right status */
1095 signal(SIGTERM, init_term);
1096 /* TODO(wad) self jail with seccomp_filters here. */
1097 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001098 /*
1099 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001100 * left inside our pid namespace or we get a signal.
1101 */
1102 if (pid == rootpid)
1103 init_exitstatus = status;
1104 }
1105 if (!WIFEXITED(init_exitstatus))
1106 _exit(MINIJAIL_ERR_INIT);
1107 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001108}
1109
Will Drewry6ac91122011-10-21 16:38:58 -05001110int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001111{
1112 size_t sz = 0;
1113 size_t bytes = read(fd, &sz, sizeof(sz));
1114 char *buf;
1115 int r;
1116 if (sizeof(sz) != bytes)
1117 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001118 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001119 return -E2BIG;
1120 buf = malloc(sz);
1121 if (!buf)
1122 return -ENOMEM;
1123 bytes = read(fd, buf, sz);
1124 if (bytes != sz) {
1125 free(buf);
1126 return -EINVAL;
1127 }
1128 r = minijail_unmarshal(j, buf, sz);
1129 free(buf);
1130 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001131}
1132
Will Drewry6ac91122011-10-21 16:38:58 -05001133int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001134{
1135 char *buf;
1136 size_t sz = minijail_size(j);
1137 ssize_t written;
1138 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001139
Elly Jonese1749eb2011-10-07 13:54:59 -04001140 if (!sz)
1141 return -EINVAL;
1142 buf = malloc(sz);
1143 r = minijail_marshal(j, buf, sz);
1144 if (r) {
1145 free(buf);
1146 return r;
1147 }
1148 /* Sends [size][minijail]. */
1149 written = write(fd, &sz, sizeof(sz));
1150 if (written != sizeof(sz)) {
1151 free(buf);
1152 return -EFAULT;
1153 }
1154 written = write(fd, buf, sz);
1155 if (written < 0 || (size_t) written != sz) {
1156 free(buf);
1157 return -EFAULT;
1158 }
1159 free(buf);
1160 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001161}
Elly Jonescd7a9042011-07-22 13:56:51 -04001162
Will Drewry6ac91122011-10-21 16:38:58 -05001163int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001164{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001165#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001166 /* Don't use LDPRELOAD on Brillo. */
1167 return 0;
1168#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001169 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1170 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1171 if (!newenv)
1172 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001173
Elly Jonese1749eb2011-10-07 13:54:59 -04001174 /* Only insert a separating space if we have something to separate... */
1175 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1176 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001177
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001178 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001179 setenv(kLdPreloadEnvVar, newenv, 1);
1180 free(newenv);
1181 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001182#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001183}
1184
Will Drewry6ac91122011-10-21 16:38:58 -05001185int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001186{
1187 int r = pipe(fds);
1188 char fd_buf[11];
1189 if (r)
1190 return r;
1191 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1192 if (r <= 0)
1193 return -EINVAL;
1194 setenv(kFdEnvVar, fd_buf, 1);
1195 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001196}
1197
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001198int setup_pipe_end(int fds[2], size_t index)
1199{
1200 if (index > 1)
1201 return -1;
1202
1203 close(fds[1 - index]);
1204 return fds[index];
1205}
1206
1207int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1208{
1209 if (index > 1)
1210 return -1;
1211
1212 close(fds[1 - index]);
1213 /* dup2(2) the corresponding end of the pipe into |fd|. */
1214 return dup2(fds[index], fd);
1215}
1216
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001217int minijail_run_internal(struct minijail *j, const char *filename,
1218 char *const argv[], pid_t *pchild_pid,
1219 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1220 int use_preload);
1221
Will Drewry6ac91122011-10-21 16:38:58 -05001222int API minijail_run(struct minijail *j, const char *filename,
1223 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001224{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001225 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1226 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001227}
1228
1229int API minijail_run_pid(struct minijail *j, const char *filename,
1230 char *const argv[], pid_t *pchild_pid)
1231{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001232 return minijail_run_internal(j, filename, argv, pchild_pid,
1233 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001234}
1235
1236int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001237 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001238{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001239 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1240 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001241}
1242
1243int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001244 char *const argv[], pid_t *pchild_pid,
1245 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001246{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001247 return minijail_run_internal(j, filename, argv, pchild_pid, pstdin_fd,
1248 NULL, NULL, true);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001249}
1250
1251int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001252 char *const argv[], pid_t *pchild_pid,
1253 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001254{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001255 return minijail_run_internal(j, filename, argv, pchild_pid,
1256 pstdin_fd, pstdout_fd, pstderr_fd, true);
1257}
1258
1259int API minijail_run_no_preload(struct minijail *j, const char *filename,
1260 char *const argv[])
1261{
1262 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1263 false);
1264}
1265
1266int minijail_run_internal(struct minijail *j, const char *filename,
1267 char *const argv[], pid_t *pchild_pid,
1268 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1269 int use_preload)
1270{
Elly Jonese1749eb2011-10-07 13:54:59 -04001271 char *oldenv, *oldenv_copy = NULL;
1272 pid_t child_pid;
1273 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001274 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001275 int stdout_fds[2];
1276 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001277 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001278 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001279 /* We need to remember this across the minijail_preexec() call. */
1280 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001281 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001282
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001283 if (use_preload) {
1284 oldenv = getenv(kLdPreloadEnvVar);
1285 if (oldenv) {
1286 oldenv_copy = strdup(oldenv);
1287 if (!oldenv_copy)
1288 return -ENOMEM;
1289 }
1290
1291 if (setup_preload())
1292 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001293 }
Will Drewryf89aef52011-09-16 16:48:57 -05001294
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001295 if (!use_preload) {
1296 if (j->flags.caps)
1297 die("Capabilities are not supported without "
1298 "LD_PRELOAD");
1299 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001300
Elly Jonesdd3e8512012-01-23 15:13:38 -05001301 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001302 * Make the process group ID of this process equal to its PID, so that
1303 * both the Minijail process and the jailed process can be killed
1304 * together.
1305 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1306 * the process is already a process group leader.
1307 */
1308 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1309 if (errno != EPERM) {
1310 pdie("setpgid(0, 0)");
1311 }
1312 }
1313
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001314 if (use_preload) {
1315 /*
1316 * Before we fork(2) and execve(2) the child process, we need
1317 * to open a pipe(2) to send the minijail configuration over.
1318 */
1319 if (setup_pipe(pipe_fds))
1320 return -EFAULT;
1321 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001322
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001323 /*
1324 * If we want to write to the child process' standard input,
1325 * create the pipe(2) now.
1326 */
1327 if (pstdin_fd) {
1328 if (pipe(stdin_fds))
1329 return -EFAULT;
1330 }
1331
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001332 /*
1333 * If we want to read from the child process' standard output,
1334 * create the pipe(2) now.
1335 */
1336 if (pstdout_fd) {
1337 if (pipe(stdout_fds))
1338 return -EFAULT;
1339 }
1340
1341 /*
1342 * If we want to read from the child process' standard error,
1343 * create the pipe(2) now.
1344 */
1345 if (pstderr_fd) {
1346 if (pipe(stderr_fds))
1347 return -EFAULT;
1348 }
1349
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001350 /*
1351 * If we want to set up a new uid/gid mapping in the user namespace,
1352 * create the pipe(2) to sync between parent and child.
1353 */
1354 if (j->flags.userns) {
1355 if (pipe(userns_pipe_fds))
1356 return -EFAULT;
1357 }
1358
Elly Jones761b7412012-06-13 15:49:52 -04001359 /* Use sys_clone() if and only if we're creating a pid namespace.
1360 *
1361 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1362 *
1363 * In multithreaded programs, there are a bunch of locks inside libc,
1364 * some of which may be held by other threads at the time that we call
1365 * minijail_run_pid(). If we call fork(), glibc does its level best to
1366 * ensure that we hold all of these locks before it calls clone()
1367 * internally and drop them after clone() returns, but when we call
1368 * sys_clone(2) directly, all that gets bypassed and we end up with a
1369 * child address space where some of libc's important locks are held by
1370 * other threads (which did not get cloned, and hence will never release
1371 * those locks). This is okay so long as we call exec() immediately
1372 * after, but a bunch of seemingly-innocent libc functions like setenv()
1373 * take locks.
1374 *
1375 * Hence, only call sys_clone() if we need to, in order to get at pid
1376 * namespacing. If we follow this path, the child's address space might
1377 * have broken locks; you may only call functions that do not acquire
1378 * any locks.
1379 *
1380 * Unfortunately, fork() acquires every lock it can get its hands on, as
1381 * previously detailed, so this function is highly likely to deadlock
1382 * later on (see "deadlock here") if we're multithreaded.
1383 *
1384 * We might hack around this by having the clone()d child (init of the
1385 * pid namespace) return directly, rather than leaving the clone()d
1386 * process hanging around to be init for the new namespace (and having
1387 * its fork()ed child return in turn), but that process would be crippled
1388 * with its libc locks potentially broken. We might try fork()ing in the
1389 * parent before we clone() to ensure that we own all the locks, but
1390 * then we have to have the forked child hanging around consuming
1391 * resources (and possibly having file descriptors / shared memory
1392 * regions / etc attached). We'd need to keep the child around to avoid
1393 * having its children get reparented to init.
1394 *
1395 * TODO(ellyjones): figure out if the "forked child hanging around"
1396 * problem is fixable or not. It would be nice if we worked in this
1397 * case.
1398 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001399 if (pid_namespace) {
1400 int clone_flags = CLONE_NEWPID | SIGCHLD;
1401 if (j->flags.userns)
1402 clone_flags |= CLONE_NEWUSER;
1403 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001404 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001405 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001406 }
Elly Jones761b7412012-06-13 15:49:52 -04001407
Elly Jonese1749eb2011-10-07 13:54:59 -04001408 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001409 if (use_preload) {
1410 free(oldenv_copy);
1411 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001412 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001413 }
Will Drewryf89aef52011-09-16 16:48:57 -05001414
Elly Jonese1749eb2011-10-07 13:54:59 -04001415 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001416 if (use_preload) {
1417 /* Restore parent's LD_PRELOAD. */
1418 if (oldenv_copy) {
1419 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1420 free(oldenv_copy);
1421 } else {
1422 unsetenv(kLdPreloadEnvVar);
1423 }
1424 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001425 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001426
Elly Jonese1749eb2011-10-07 13:54:59 -04001427 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001428
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001429 if (j->flags.pid_file)
1430 write_pid_file(j);
1431
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001432 if (j->flags.userns)
1433 write_ugid_mappings(j, userns_pipe_fds);
1434
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001435 if (use_preload) {
1436 /* Send marshalled minijail. */
1437 close(pipe_fds[0]); /* read endpoint */
1438 ret = minijail_to_fd(j, pipe_fds[1]);
1439 close(pipe_fds[1]); /* write endpoint */
1440 if (ret) {
1441 kill(j->initpid, SIGKILL);
1442 die("failed to send marshalled minijail");
1443 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001444 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001445
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001446 if (pchild_pid)
1447 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001448
1449 /*
1450 * If we want to write to the child process' standard input,
1451 * set up the write end of the pipe.
1452 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001453 if (pstdin_fd)
1454 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001455 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001456
1457 /*
1458 * If we want to read from the child process' standard output,
1459 * set up the read end of the pipe.
1460 */
1461 if (pstdout_fd)
1462 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001463 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001464
1465 /*
1466 * If we want to read from the child process' standard error,
1467 * set up the read end of the pipe.
1468 */
1469 if (pstderr_fd)
1470 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001471 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001472
Elly Jonese1749eb2011-10-07 13:54:59 -04001473 return 0;
1474 }
1475 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001476
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001477 if (j->flags.userns)
1478 enter_user_namespace(j, userns_pipe_fds);
1479
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001480 /*
1481 * If we want to write to the jailed process' standard input,
1482 * set up the read end of the pipe.
1483 */
1484 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001485 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1486 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001487 die("failed to set up stdin pipe");
1488 }
1489
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001490 /*
1491 * If we want to read from the jailed process' standard output,
1492 * set up the write end of the pipe.
1493 */
1494 if (pstdout_fd) {
1495 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1496 STDOUT_FILENO) < 0)
1497 die("failed to set up stdout pipe");
1498 }
1499
1500 /*
1501 * If we want to read from the jailed process' standard error,
1502 * set up the write end of the pipe.
1503 */
1504 if (pstderr_fd) {
1505 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1506 STDERR_FILENO) < 0)
1507 die("failed to set up stderr pipe");
1508 }
1509
Dylan Reid791f5772015-09-14 20:02:42 -07001510 /* If running an init program, let it decide when/how to mount /proc. */
1511 if (pid_namespace && !do_init)
1512 j->flags.remount_proc_ro = 0;
1513
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001514 if (use_preload) {
1515 /* Strip out flags that cannot be inherited across execve(2). */
1516 minijail_preexec(j);
1517 } else {
1518 j->flags.pids = 0;
1519 }
1520 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001521 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001522
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001523 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001524 /*
1525 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001526 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001527 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001528 * a child to actually run the program. If |do_init == 0|, we
1529 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001530 *
1531 * If we're multithreaded, we'll probably deadlock here. See
1532 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001533 */
1534 child_pid = fork();
1535 if (child_pid < 0)
1536 _exit(child_pid);
1537 else if (child_pid > 0)
1538 init(child_pid); /* never returns */
1539 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001540
Elly Jonesdd3e8512012-01-23 15:13:38 -05001541 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001542 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001543 * calling process
1544 * -> execve()-ing process
1545 * If we are:
1546 * calling process
1547 * -> init()-ing process
1548 * -> execve()-ing process
1549 */
1550 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001551}
1552
Will Drewry6ac91122011-10-21 16:38:58 -05001553int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001554{
1555 int st;
1556 if (kill(j->initpid, SIGTERM))
1557 return -errno;
1558 if (waitpid(j->initpid, &st, 0) < 0)
1559 return -errno;
1560 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001561}
1562
Will Drewry6ac91122011-10-21 16:38:58 -05001563int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001564{
1565 int st;
1566 if (waitpid(j->initpid, &st, 0) < 0)
1567 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001568
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001569 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001570 int error_status = st;
1571 if (WIFSIGNALED(st)) {
1572 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001573 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001574 j->initpid, signum);
1575 /*
1576 * We return MINIJAIL_ERR_JAIL if the process received
1577 * SIGSYS, which happens when a syscall is blocked by
1578 * seccomp filters.
1579 * If not, we do what bash(1) does:
1580 * $? = 128 + signum
1581 */
1582 if (signum == SIGSYS) {
1583 error_status = MINIJAIL_ERR_JAIL;
1584 } else {
1585 error_status = 128 + signum;
1586 }
1587 }
1588 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001589 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001590
1591 int exit_status = WEXITSTATUS(st);
1592 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001593 info("child process %d exited with status %d",
1594 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001595
1596 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001597}
1598
Will Drewry6ac91122011-10-21 16:38:58 -05001599void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001600{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001601 if (j->flags.seccomp_filter && j->filter_prog) {
1602 free(j->filter_prog->filter);
1603 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001604 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001605 while (j->bindings_head) {
1606 struct binding *b = j->bindings_head;
1607 j->bindings_head = j->bindings_head->next;
1608 free(b->dest);
1609 free(b->src);
1610 free(b);
1611 }
1612 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001613 if (j->user)
1614 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001615 if (j->chrootdir)
1616 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001617 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001618}