blob: d492522a5f19a877f751595273eff56f57e3f833 [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
Andrew Brestickereac28942015-11-11 16:04:46 -080056#ifndef PR_ALT_SYSCALL
57# define PR_ALT_SYSCALL 0x43724f53
58#endif
59
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080060/* For seccomp_filter using BPF. */
61#ifndef PR_SET_NO_NEW_PRIVS
62# define PR_SET_NO_NEW_PRIVS 38
63#endif
64#ifndef SECCOMP_MODE_FILTER
65# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050066#endif
67
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070068#ifdef USE_SECCOMP_SOFTFAIL
69# define SECCOMP_SOFTFAIL 1
70#else
71# define SECCOMP_SOFTFAIL 0
72#endif
73
Dylan Reid648b2202015-10-23 00:50:00 -070074struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040075 char *src;
76 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070077 char *type;
78 unsigned long flags;
79 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040080};
81
Will Drewryf89aef52011-09-16 16:48:57 -050082struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070083 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070084 * WARNING: if you add a flag here you need to make sure it's
85 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070086 */
Elly Jonese1749eb2011-10-07 13:54:59 -040087 struct {
88 int uid:1;
89 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -080090 int usergroups:1;
91 int suppl_gids:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040092 int caps:1;
93 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070094 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040095 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -080096 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040097 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -070098 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080099 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400100 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700101 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700102 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400103 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700104 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400105 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800106 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700107 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800108 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800109 int pid_file:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800110 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800111 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400112 } flags;
113 uid_t uid;
114 gid_t gid;
115 gid_t usergid;
116 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800117 size_t suppl_gid_count;
118 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400119 uint64_t caps;
120 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700121 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700122 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400123 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800124 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800125 char *uidmap;
126 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800127 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800128 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800129 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700130 struct mountpoint *mounts_head;
131 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800132 size_t mounts_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500133};
134
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700135/*
136 * Strip out flags meant for the parent.
137 * We keep things that are not inherited across execve(2) (e.g. capabilities),
138 * or are easier to set after execve(2) (e.g. seccomp filters).
139 */
140void minijail_preenter(struct minijail *j)
141{
142 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700143 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700144 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700145 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800146 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800147 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700148}
149
150/*
151 * Strip out flags meant for the child.
152 * We keep things that are inherited across execve(2).
153 */
154void minijail_preexec(struct minijail *j)
155{
156 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700157 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700158 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800159 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700160 if (j->user)
161 free(j->user);
162 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800163 if (j->suppl_gid_list)
164 free(j->suppl_gid_list);
165 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700166 memset(&j->flags, 0, sizeof(j->flags));
167 /* Now restore anything we meant to keep. */
168 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700169 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700170 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800171 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700172 /* Note, |pids| will already have been used before this call. */
173}
174
175/* Minijail API. */
176
Will Drewry6ac91122011-10-21 16:38:58 -0500177struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400178{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400179 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400180}
181
Will Drewry6ac91122011-10-21 16:38:58 -0500182void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400183{
184 if (uid == 0)
185 die("useless change to uid 0");
186 j->uid = uid;
187 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400188}
189
Will Drewry6ac91122011-10-21 16:38:58 -0500190void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400191{
192 if (gid == 0)
193 die("useless change to gid 0");
194 j->gid = gid;
195 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400196}
197
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800198void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
199 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800200{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800201 size_t i;
202
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800203 if (j->flags.usergroups)
204 die("cannot inherit *and* set supplementary groups");
205
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800206 if (size == 0) {
207 /* Clear supplementary groups. */
208 j->suppl_gid_list = NULL;
209 j->suppl_gid_count = 0;
210 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800211 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800212 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800213
214 /* Copy the gid_t array. */
215 j->suppl_gid_list = calloc(size, sizeof(gid_t));
216 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800217 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800218 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800219 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800220 j->suppl_gid_list[i] = list[i];
221 }
222 j->suppl_gid_count = size;
223 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800224}
225
Will Drewry6ac91122011-10-21 16:38:58 -0500226int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400227{
228 char *buf = NULL;
229 struct passwd pw;
230 struct passwd *ppw = NULL;
231 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
232 if (sz == -1)
233 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400234
Elly Jonesdd3e8512012-01-23 15:13:38 -0500235 /*
236 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400237 * the maximum needed size of the buffer, so we don't have to search.
238 */
239 buf = malloc(sz);
240 if (!buf)
241 return -ENOMEM;
242 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500243 /*
244 * We're safe to free the buffer here. The strings inside pw point
245 * inside buf, but we don't use any of them; this leaves the pointers
246 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
247 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400248 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700249 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400250 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700251 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400252 minijail_change_uid(j, ppw->pw_uid);
253 j->user = strdup(user);
254 if (!j->user)
255 return -ENOMEM;
256 j->usergid = ppw->pw_gid;
257 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400258}
259
Will Drewry6ac91122011-10-21 16:38:58 -0500260int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400261{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700262 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700263 struct group gr;
264 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400265 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
266 if (sz == -1)
267 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400268
Elly Jonesdd3e8512012-01-23 15:13:38 -0500269 /*
270 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400271 * the maximum needed size of the buffer, so we don't have to search.
272 */
273 buf = malloc(sz);
274 if (!buf)
275 return -ENOMEM;
276 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500277 /*
278 * We're safe to free the buffer here. The strings inside gr point
279 * inside buf, but we don't use any of them; this leaves the pointers
280 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
281 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400282 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700283 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400284 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700285 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400286 minijail_change_gid(j, pgr->gr_gid);
287 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400288}
289
Will Drewry6ac91122011-10-21 16:38:58 -0500290void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400291{
292 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400293}
294
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700295void API minijail_no_new_privs(struct minijail *j)
296{
297 j->flags.no_new_privs = 1;
298}
299
Will Drewry6ac91122011-10-21 16:38:58 -0500300void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400301{
302 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500303}
304
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700305void API minijail_log_seccomp_filter_failures(struct minijail *j)
306{
307 j->flags.log_seccomp_filter = 1;
308}
309
Will Drewry6ac91122011-10-21 16:38:58 -0500310void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400311{
312 j->caps = capmask;
313 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400314}
315
Peter Qiu2860c462015-12-16 15:13:06 -0800316void API minijail_reset_signal_mask(struct minijail* j) {
317 j->flags.reset_signal_mask = 1;
318}
319
Will Drewry6ac91122011-10-21 16:38:58 -0500320void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400321{
322 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400323}
324
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700325void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
326{
327 int ns_fd = open(ns_path, O_RDONLY);
328 if (ns_fd < 0) {
329 pdie("failed to open namespace '%s'", ns_path);
330 }
331 j->mountns_fd = ns_fd;
332 j->flags.enter_vfs = 1;
333}
334
Will Drewry6ac91122011-10-21 16:38:58 -0500335void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400336{
Elly Jonese58176c2012-01-23 11:46:17 -0500337 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700338 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400339 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800340 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400341}
342
Dylan Reidf7942472015-11-18 17:55:26 -0800343void API minijail_namespace_ipc(struct minijail *j)
344{
345 j->flags.ipc = 1;
346}
347
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400348void API minijail_namespace_net(struct minijail *j)
349{
350 j->flags.net = 1;
351}
352
Dylan Reid1102f5a2015-09-15 11:52:20 -0700353void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
354{
355 int ns_fd = open(ns_path, O_RDONLY);
356 if (ns_fd < 0) {
357 pdie("failed to open namespace '%s'", ns_path);
358 }
359 j->netns_fd = ns_fd;
360 j->flags.enter_net = 1;
361}
362
Dylan Reid791f5772015-09-14 20:02:42 -0700363void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400364{
365 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700366 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400367}
368
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800369void API minijail_namespace_user(struct minijail *j)
370{
371 j->flags.userns = 1;
372}
373
374int API minijail_uidmap(struct minijail *j, const char *uidmap)
375{
376 j->uidmap = strdup(uidmap);
377 if (!j->uidmap)
378 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800379 char *ch;
380 for (ch = j->uidmap; *ch; ch++) {
381 if (*ch == ',')
382 *ch = '\n';
383 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800384 return 0;
385}
386
387int API minijail_gidmap(struct minijail *j, const char *gidmap)
388{
389 j->gidmap = strdup(gidmap);
390 if (!j->gidmap)
391 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800392 char *ch;
393 for (ch = j->gidmap; *ch; ch++) {
394 if (*ch == ',')
395 *ch = '\n';
396 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800397 return 0;
398}
399
Will Drewry6ac91122011-10-21 16:38:58 -0500400void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400401{
402 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400403}
404
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800405void API minijail_run_as_init(struct minijail *j)
406{
407 /*
408 * Since the jailed program will become 'init' in the new PID namespace,
409 * Minijail does not need to fork an 'init' process.
410 */
411 j->flags.do_init = 0;
412}
413
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700414int API minijail_enter_chroot(struct minijail *j, const char *dir)
415{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400416 if (j->chrootdir)
417 return -EINVAL;
418 j->chrootdir = strdup(dir);
419 if (!j->chrootdir)
420 return -ENOMEM;
421 j->flags.chroot = 1;
422 return 0;
423}
424
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800425int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
426{
427 if (j->chrootdir)
428 return -EINVAL;
429 j->chrootdir = strdup(dir);
430 if (!j->chrootdir)
431 return -ENOMEM;
432 j->flags.pivot_root = 1;
433 return 0;
434}
435
Dylan Reida14e08d2015-10-22 21:05:29 -0700436static char *append_external_path(const char *external_path,
437 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700438{
Dylan Reida14e08d2015-10-22 21:05:29 -0700439 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700440 size_t pathlen;
441
Dylan Reid08946cc2015-09-16 19:10:57 -0700442 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700443 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
444 path = malloc(pathlen);
445 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700446
Dylan Reida14e08d2015-10-22 21:05:29 -0700447 return path;
448}
449
450char API *minijail_get_original_path(struct minijail *j,
451 const char *path_inside_chroot)
452{
Dylan Reid648b2202015-10-23 00:50:00 -0700453 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700454
Dylan Reid648b2202015-10-23 00:50:00 -0700455 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700456 while (b) {
457 /*
458 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700459 * mount, then the original path is exactly the source of
460 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700461 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700462 * mount source = /some/path/exe, mount dest =
463 * /chroot/path/exe Then when getting the original path of
464 * "/chroot/path/exe", the source of that mount,
465 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700466 */
467 if (!strcmp(b->dest, path_inside_chroot))
468 return strdup(b->src);
469
470 /*
471 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700472 * mount, take the suffix of the chroot path relative to the
473 * mount destination path, and append it to the mount source
474 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700475 */
476 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
477 const char *relative_path =
478 path_inside_chroot + strlen(b->dest);
479 return append_external_path(b->src, relative_path);
480 }
481 b = b->next;
482 }
483
484 /* If there is a chroot path, append |path_inside_chroot| to that. */
485 if (j->chrootdir)
486 return append_external_path(j->chrootdir, path_inside_chroot);
487
488 /* No chroot, so the path outside is the same as it is inside. */
489 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700490}
491
Lee Campbell11af0622014-05-22 12:36:04 -0700492void API minijail_mount_tmp(struct minijail *j)
493{
494 j->flags.mount_tmp = 1;
495}
496
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800497int API minijail_write_pid_file(struct minijail *j, const char *path)
498{
499 j->pid_file_path = strdup(path);
500 if (!j->pid_file_path)
501 return -ENOMEM;
502 j->flags.pid_file = 1;
503 return 0;
504}
505
Dylan Reid648b2202015-10-23 00:50:00 -0700506int API minijail_mount(struct minijail *j, const char *src, const char *dest,
507 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700508{
Dylan Reid648b2202015-10-23 00:50:00 -0700509 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400510
511 if (*dest != '/')
512 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700513 m = calloc(1, sizeof(*m));
514 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400515 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700516 m->dest = strdup(dest);
517 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400518 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700519 m->src = strdup(src);
520 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400521 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700522 m->type = strdup(type);
523 if (!m->type)
524 goto error;
525 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400526
Dylan Reid648b2202015-10-23 00:50:00 -0700527 info("mount %s -> %s type %s", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400528
Elly Jonesdd3e8512012-01-23 15:13:38 -0500529 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700530 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400531 * containing vfs namespace.
532 */
533 minijail_namespace_vfs(j);
534
Dylan Reid648b2202015-10-23 00:50:00 -0700535 if (j->mounts_tail)
536 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400537 else
Dylan Reid648b2202015-10-23 00:50:00 -0700538 j->mounts_head = m;
539 j->mounts_tail = m;
540 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400541
542 return 0;
543
544error:
Dylan Reid648b2202015-10-23 00:50:00 -0700545 free(m->src);
546 free(m->dest);
547 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400548 return -ENOMEM;
549}
550
Dylan Reid648b2202015-10-23 00:50:00 -0700551int API minijail_bind(struct minijail *j, const char *src, const char *dest,
552 int writeable)
553{
554 unsigned long flags = MS_BIND;
555
556 if (!writeable)
557 flags |= MS_RDONLY;
558
559 return minijail_mount(j, src, dest, "", flags);
560}
561
Will Drewry6ac91122011-10-21 16:38:58 -0500562void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400563{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700564 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
565 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800566 warn("not loading seccomp filter,"
567 " seccomp not supported");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700568 return;
569 }
570 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400571 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800572 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700573 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400574 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800575
576 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700577 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
578 die("failed to compile seccomp filter BPF program in '%s'",
579 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800580 }
581
582 j->filter_len = fprog->len;
583 j->filter_prog = fprog;
584
Elly Jonese1749eb2011-10-07 13:54:59 -0400585 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500586}
587
Andrew Brestickereac28942015-11-11 16:04:46 -0800588int API minijail_use_alt_syscall(struct minijail *j, const char *table)
589{
590 j->alt_syscall_table = strdup(table);
591 if (!j->alt_syscall_table)
592 return -ENOMEM;
593 j->flags.alt_syscall = 1;
594 return 0;
595}
596
Will Drewryf89aef52011-09-16 16:48:57 -0500597struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400598 size_t available;
599 size_t total;
600 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500601};
602
Will Drewry6ac91122011-10-21 16:38:58 -0500603void marshal_state_init(struct marshal_state *state,
604 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400605{
606 state->available = available;
607 state->buf = buf;
608 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500609}
610
Will Drewry6ac91122011-10-21 16:38:58 -0500611void marshal_append(struct marshal_state *state,
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800612 void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400613{
614 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500615
Elly Jonese1749eb2011-10-07 13:54:59 -0400616 /* Up to |available| will be written. */
617 if (copy_len) {
618 memcpy(state->buf, src, copy_len);
619 state->buf += copy_len;
620 state->available -= copy_len;
621 }
622 /* |total| will contain the expected length. */
623 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500624}
625
Will Drewry6ac91122011-10-21 16:38:58 -0500626void minijail_marshal_helper(struct marshal_state *state,
627 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400628{
Dylan Reid648b2202015-10-23 00:50:00 -0700629 struct mountpoint *m = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400630 marshal_append(state, (char *)j, sizeof(*j));
631 if (j->user)
632 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800633 if (j->suppl_gid_list) {
634 marshal_append(state, j->suppl_gid_list,
635 j->suppl_gid_count * sizeof(gid_t));
636 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400637 if (j->chrootdir)
638 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800639 if (j->alt_syscall_table) {
640 marshal_append(state, j->alt_syscall_table,
641 strlen(j->alt_syscall_table) + 1);
642 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800643 if (j->flags.seccomp_filter && j->filter_prog) {
644 struct sock_fprog *fp = j->filter_prog;
645 marshal_append(state, (char *)fp->filter,
646 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400647 }
Dylan Reid648b2202015-10-23 00:50:00 -0700648 for (m = j->mounts_head; m; m = m->next) {
649 marshal_append(state, m->src, strlen(m->src) + 1);
650 marshal_append(state, m->dest, strlen(m->dest) + 1);
651 marshal_append(state, m->type, strlen(m->type) + 1);
652 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400653 }
Will Drewryf89aef52011-09-16 16:48:57 -0500654}
655
Will Drewry6ac91122011-10-21 16:38:58 -0500656size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400657{
658 struct marshal_state state;
659 marshal_state_init(&state, NULL, 0);
660 minijail_marshal_helper(&state, j);
661 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500662}
663
Elly Jonese1749eb2011-10-07 13:54:59 -0400664int minijail_marshal(const struct minijail *j, char *buf, size_t available)
665{
666 struct marshal_state state;
667 marshal_state_init(&state, buf, available);
668 minijail_marshal_helper(&state, j);
669 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500670}
671
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800672/*
673 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400674 * @length Number of bytes to consume
675 * @buf Buffer to consume from
676 * @buflength Size of @buf
677 *
678 * Returns a pointer to the base of the bytes, or NULL for errors.
679 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700680void *consumebytes(size_t length, char **buf, size_t *buflength)
681{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400682 char *p = *buf;
683 if (length > *buflength)
684 return NULL;
685 *buf += length;
686 *buflength -= length;
687 return p;
688}
689
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800690/*
691 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400692 * @buf Buffer to consume
693 * @length Length of buffer
694 *
695 * Returns a pointer to the base of the string, or NULL for errors.
696 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700697char *consumestr(char **buf, size_t *buflength)
698{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400699 size_t len = strnlen(*buf, *buflength);
700 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700701 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400702 return NULL;
703 return consumebytes(len + 1, buf, buflength);
704}
705
Elly Jonese1749eb2011-10-07 13:54:59 -0400706int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
707{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800708 size_t i;
709 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500710 int ret = -EINVAL;
711
Elly Jonese1749eb2011-10-07 13:54:59 -0400712 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500713 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400714 memcpy((void *)j, serialized, sizeof(*j));
715 serialized += sizeof(*j);
716 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500717
Will Drewrybee7ba72011-10-21 20:47:01 -0500718 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700719 j->mounts_head = NULL;
720 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800721 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500722
Elly Jonese1749eb2011-10-07 13:54:59 -0400723 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400724 char *user = consumestr(&serialized, &length);
725 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500726 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400727 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500728 if (!j->user)
729 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400730 }
Will Drewryf89aef52011-09-16 16:48:57 -0500731
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800732 if (j->suppl_gid_list) { /* stale pointer */
733 if (j->suppl_gid_count > NGROUPS_MAX) {
734 goto bad_gid_list;
735 }
736 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
737 void *gid_list_bytes =
738 consumebytes(gid_list_size, &serialized, &length);
739 if (!gid_list_bytes)
740 goto bad_gid_list;
741
742 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
743 if (!j->suppl_gid_list)
744 goto bad_gid_list;
745
746 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
747 }
748
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400749 if (j->chrootdir) { /* stale pointer */
750 char *chrootdir = consumestr(&serialized, &length);
751 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500752 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400753 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500754 if (!j->chrootdir)
755 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400756 }
757
Andrew Brestickereac28942015-11-11 16:04:46 -0800758 if (j->alt_syscall_table) { /* stale pointer */
759 char *alt_syscall_table = consumestr(&serialized, &length);
760 if (!alt_syscall_table)
761 goto bad_syscall_table;
762 j->alt_syscall_table = strdup(alt_syscall_table);
763 if (!j->alt_syscall_table)
764 goto bad_syscall_table;
765 }
766
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800767 if (j->flags.seccomp_filter && j->filter_len > 0) {
768 size_t ninstrs = j->filter_len;
769 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
770 ninstrs > USHRT_MAX)
771 goto bad_filters;
772
773 size_t program_len = ninstrs * sizeof(struct sock_filter);
774 void *program = consumebytes(program_len, &serialized, &length);
775 if (!program)
776 goto bad_filters;
777
778 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800779 if (!j->filter_prog)
780 goto bad_filters;
781
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800782 j->filter_prog->len = ninstrs;
783 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800784 if (!j->filter_prog->filter)
785 goto bad_filter_prog_instrs;
786
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800787 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400788 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400789
Dylan Reid648b2202015-10-23 00:50:00 -0700790 count = j->mounts_count;
791 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400792 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700793 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400794 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700795 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400796 const char *src = consumestr(&serialized, &length);
797 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700798 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400799 dest = consumestr(&serialized, &length);
800 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700801 goto bad_mounts;
802 type = consumestr(&serialized, &length);
803 if (!type)
804 goto bad_mounts;
805 flags = consumebytes(sizeof(*flags), &serialized, &length);
806 if (!flags)
807 goto bad_mounts;
808 if (minijail_mount(j, src, dest, type, *flags))
809 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400810 }
811
Elly Jonese1749eb2011-10-07 13:54:59 -0400812 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500813
Dylan Reid648b2202015-10-23 00:50:00 -0700814bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800815 if (j->flags.seccomp_filter && j->filter_len > 0) {
816 free(j->filter_prog->filter);
817 free(j->filter_prog);
818 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800819bad_filter_prog_instrs:
820 if (j->filter_prog)
821 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500822bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800823 if (j->alt_syscall_table)
824 free(j->alt_syscall_table);
825bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500826 if (j->chrootdir)
827 free(j->chrootdir);
828bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800829 if (j->suppl_gid_list)
830 free(j->suppl_gid_list);
831bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500832 if (j->user)
833 free(j->user);
834clear_pointers:
835 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800836 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500837 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800838 j->alt_syscall_table = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500839out:
840 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500841}
842
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800843static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
844{
845 int fd, ret, len;
846 size_t sz;
847 char fname[32];
848 close(pipe_fds[0]);
849
850 sz = sizeof(fname);
851 if (j->uidmap) {
852 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700853 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800854 die("failed to write file name of uid_map");
855 fd = open(fname, O_WRONLY);
856 if (fd < 0)
857 pdie("failed to open '%s'", fname);
858 len = strlen(j->uidmap);
859 if (write(fd, j->uidmap, len) < len)
860 die("failed to set uid_map");
861 close(fd);
862 }
863 if (j->gidmap) {
864 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700865 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800866 die("failed to write file name of gid_map");
867 fd = open(fname, O_WRONLY);
868 if (fd < 0)
869 pdie("failed to open '%s'", fname);
870 len = strlen(j->gidmap);
871 if (write(fd, j->gidmap, len) < len)
872 die("failed to set gid_map");
873 close(fd);
874 }
875
876 close(pipe_fds[1]);
877}
878
879static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
880{
881 char buf;
882
883 close(pipe_fds[1]);
884
885 /* Wait for parent to set up uid/gid mappings. */
886 if (read(pipe_fds[0], &buf, 1) != 0)
887 die("failed to sync with parent");
888 close(pipe_fds[0]);
889
890 if (j->uidmap && setresuid(0, 0, 0))
891 pdie("setresuid");
892 if (j->gidmap && setresgid(0, 0, 0))
893 pdie("setresgid");
894}
895
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800896/*
897 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -0700898 * @j Minijail these mounts are for
899 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400900 *
901 * Returns 0 for success.
902 */
Dylan Reid648b2202015-10-23 00:50:00 -0700903static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700904{
Dylan Reid648b2202015-10-23 00:50:00 -0700905 int ret;
906 char *dest;
907 int remount_ro = 0;
908
Elly Jones51a5b6c2011-10-12 19:09:26 -0400909 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700910 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400911 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700912
913 /*
914 * R/O bind mounts have to be remounted since bind and ro can't both be
915 * specified in the original bind mount. Remount R/O after the initial
916 * mount.
917 */
918 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
919 remount_ro = 1;
920 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500921 }
Dylan Reid648b2202015-10-23 00:50:00 -0700922
923 ret = mount(m->src, dest, m->type, m->flags, NULL);
924 if (ret)
925 pdie("mount: %s -> %s", m->src, dest);
926
927 if (remount_ro) {
928 m->flags |= MS_RDONLY;
929 ret = mount(m->src, dest, NULL,
930 m->flags | MS_REMOUNT, NULL);
931 if (ret)
932 pdie("bind ro: %s -> %s", m->src, dest);
933 }
934
Elly Jones51a5b6c2011-10-12 19:09:26 -0400935 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700936 if (m->next)
937 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400938 return ret;
939}
940
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700941int enter_chroot(const struct minijail *j)
942{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400943 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700944
945 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400946 return ret;
947
948 if (chroot(j->chrootdir))
949 return -errno;
950
951 if (chdir("/"))
952 return -errno;
953
954 return 0;
955}
956
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800957int enter_pivot_root(const struct minijail *j)
958{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800959 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -0700960
961 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800962 return ret;
963
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800964 /*
965 * Keep the fd for both old and new root.
966 * It will be used in fchdir later.
967 */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800968 oldroot = open("/", O_DIRECTORY | O_RDONLY);
969 if (oldroot < 0)
970 pdie("failed to open / for fchdir");
971 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
972 if (newroot < 0)
973 pdie("failed to open %s for fchdir", j->chrootdir);
974
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800975 /*
976 * To ensure chrootdir is the root of a file system,
977 * do a self bind mount.
978 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800979 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
980 pdie("failed to bind mount '%s'", j->chrootdir);
981 if (chdir(j->chrootdir))
982 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800983 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800984 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800985
986 /*
987 * Now the old root is mounted on top of the new root. Use fchdir to
988 * change to the old root and unmount it.
989 */
990 if (fchdir(oldroot))
991 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800992 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800993 if (umount2(".", MNT_DETACH))
994 pdie("umount(/)");
995 /* Change back to the new root. */
996 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800997 return -errno;
998 if (chroot("/"))
999 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001000 /* Set correct CWD for getcwd(3). */
1001 if (chdir("/"))
1002 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001003
1004 return 0;
1005}
1006
Lee Campbell11af0622014-05-22 12:36:04 -07001007int mount_tmp(void)
1008{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001009 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001010}
1011
Dylan Reid791f5772015-09-14 20:02:42 -07001012int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001013{
1014 const char *kProcPath = "/proc";
1015 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001016 /*
1017 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001018 * /proc in our namespace, which means using MS_REMOUNT here would
1019 * mutate our parent's mount as well, even though we're in a VFS
1020 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001021 * and make our own. However, if we are in a new user namespace, /proc
1022 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001023 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001024 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001025 return -errno;
1026 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1027 return -errno;
1028 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001029}
1030
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001031static void write_pid_file(const struct minijail *j)
1032{
1033 FILE *fp = fopen(j->pid_file_path, "w");
1034
1035 if (!fp)
1036 pdie("failed to open '%s'", j->pid_file_path);
1037 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
1038 pdie("fprintf(%s)", j->pid_file_path);
1039 if (fclose(fp))
1040 pdie("fclose(%s)", j->pid_file_path);
1041}
1042
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001043void drop_ugid(const struct minijail *j)
1044{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001045 if (j->flags.usergroups && j->flags.suppl_gids) {
1046 die("tried to inherit *and* set supplementary groups;"
1047 " can only do one");
1048 }
1049
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001050 if (j->flags.usergroups) {
1051 if (initgroups(j->user, j->usergid))
1052 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001053 } else if (j->flags.suppl_gids) {
1054 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1055 pdie("setgroups");
1056 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001057 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001058 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001059 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001060 * users.
1061 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001062 if ((j->uid || j->gid) && setgroups(0, NULL))
1063 pdie("setgroups");
1064 }
1065
1066 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1067 pdie("setresgid");
1068
1069 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1070 pdie("setresuid");
1071}
1072
Mike Frysinger3adfef72013-05-09 17:19:08 -04001073/*
1074 * We specifically do not use cap_valid() as that only tells us the last
1075 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001076 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001077 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001078 * kernel).
1079 * Normally, we suck up the answer via /proc. On Android, not all processes are
1080 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1081 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001082 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001083static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001084{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001085 unsigned int last_valid_cap = 0;
1086 if (is_android()) {
1087 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1088 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001089
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001090 /* |last_valid_cap| will be the first failing value. */
1091 if (last_valid_cap > 0) {
1092 last_valid_cap--;
1093 }
1094 } else {
1095 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1096 FILE *fp = fopen(cap_file, "re");
1097 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1098 pdie("fscanf(%s)", cap_file);
1099 fclose(fp);
1100 }
Dylan Reidf682d472015-09-17 21:39:07 -07001101 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001102}
1103
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001104void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001105{
1106 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001107 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001108 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001109 unsigned int i;
1110 if (!caps)
1111 die("can't get process caps");
1112 if (cap_clear_flag(caps, CAP_INHERITABLE))
1113 die("can't clear inheritable caps");
1114 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1115 die("can't clear effective caps");
1116 if (cap_clear_flag(caps, CAP_PERMITTED))
1117 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001118 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001119 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001120 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001121 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001122 flag[0] = i;
1123 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001124 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001125 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001126 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001127 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001128 die("can't add inheritable cap");
1129 }
1130 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001131 die("can't apply initial cleaned capset");
1132
1133 /*
1134 * Instead of dropping bounding set first, do it here in case
1135 * the caller had a more permissive bounding set which could
1136 * have been used above to raise a capability that wasn't already
1137 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1138 */
Dylan Reidf682d472015-09-17 21:39:07 -07001139 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001140 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001141 continue;
1142 if (prctl(PR_CAPBSET_DROP, i))
1143 pdie("prctl(PR_CAPBSET_DROP)");
1144 }
Kees Cook323878a2013-02-05 15:35:24 -08001145
1146 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001147 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001148 flag[0] = CAP_SETPCAP;
1149 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1150 die("can't clear effective cap");
1151 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1152 die("can't clear permitted cap");
1153 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1154 die("can't clear inheritable cap");
1155 }
1156
1157 if (cap_set_proc(caps))
1158 die("can't apply final cleaned capset");
1159
1160 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001161}
1162
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001163void set_seccomp_filter(const struct minijail *j)
1164{
1165 /*
1166 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1167 * in the kernel source tree for an explanation of the parameters.
1168 */
1169 if (j->flags.no_new_privs) {
1170 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1171 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1172 }
1173
1174 /*
1175 * If we're logging seccomp filter failures,
1176 * install the SIGSYS handler first.
1177 */
1178 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1179 if (install_sigsys_handler())
1180 pdie("install SIGSYS handler");
1181 warn("logging seccomp filter failures");
1182 }
1183
1184 /*
1185 * Install the syscall filter.
1186 */
1187 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001188 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1189 j->filter_prog)) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001190 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1191 warn("seccomp not supported");
1192 return;
1193 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001194 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001195 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001196 }
1197}
1198
Will Drewry6ac91122011-10-21 16:38:58 -05001199void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001200{
Dylan Reidf682d472015-09-17 21:39:07 -07001201 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001202 * If we're dropping caps, get the last valid cap from /proc now,
1203 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001204 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001205 unsigned int last_valid_cap = 0;
1206 if (j->flags.caps)
1207 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001208
Elly Jonese1749eb2011-10-07 13:54:59 -04001209 if (j->flags.pids)
1210 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001211 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001212
Elly Jonese1749eb2011-10-07 13:54:59 -04001213 if (j->flags.usergroups && !j->user)
1214 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001215
Elly Jonesdd3e8512012-01-23 15:13:38 -05001216 /*
1217 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001218 * so we don't even try. If any of our operations fail, we abort() the
1219 * entire process.
1220 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001221 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1222 pdie("setns(CLONE_NEWNS)");
1223
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001224 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001225 if (unshare(CLONE_NEWNS))
1226 pdie("unshare(vfs)");
1227 /*
1228 * Remount all filesystems as private. If they are shared
1229 * new bind mounts will creep out of our namespace.
1230 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1231 */
1232 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1233 pdie("mount(/, private)");
1234 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001235
Dylan Reidf7942472015-11-18 17:55:26 -08001236 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1237 pdie("unshare(ipc)");
1238 }
1239
Dylan Reid1102f5a2015-09-15 11:52:20 -07001240 if (j->flags.enter_net) {
1241 if (setns(j->netns_fd, CLONE_NEWNET))
1242 pdie("setns(CLONE_NEWNET)");
1243 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001244 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001245 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001246
Elly Jones51a5b6c2011-10-12 19:09:26 -04001247 if (j->flags.chroot && enter_chroot(j))
1248 pdie("chroot");
1249
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001250 if (j->flags.pivot_root && enter_pivot_root(j))
1251 pdie("pivot_root");
1252
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001253 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001254 pdie("mount_tmp");
1255
Dylan Reid791f5772015-09-14 20:02:42 -07001256 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001257 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001258
Elly Jonese1749eb2011-10-07 13:54:59 -04001259 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001260 /*
1261 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001262 * capability to change uids, our attempt to use setuid()
1263 * below will fail. Hang on to root caps across setuid(), then
1264 * lock securebits.
1265 */
1266 if (prctl(PR_SET_KEEPCAPS, 1))
1267 pdie("prctl(PR_SET_KEEPCAPS)");
1268 if (prctl
1269 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1270 pdie("prctl(PR_SET_SECUREBITS)");
1271 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001272
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001273 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001274 * If we're setting no_new_privs, we can drop privileges
1275 * before setting seccomp filter. This way filter policies
1276 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001277 */
1278 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001279 drop_ugid(j);
1280 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001281 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001282
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001283 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001284 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001285 /*
1286 * If we're not setting no_new_privs,
1287 * we need to set seccomp filter *before* dropping privileges.
1288 * WARNING: this means that filter policies *must* allow
1289 * setgroups()/setresgid()/setresuid() for dropping root and
1290 * capget()/capset()/prctl() for dropping caps.
1291 */
1292 set_seccomp_filter(j);
1293
1294 drop_ugid(j);
1295 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001296 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001297 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001298
Elly Jonesdd3e8512012-01-23 15:13:38 -05001299 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001300 * Select the specified alternate syscall table. The table must not
1301 * block prctl(2) if we're using seccomp as well.
1302 */
1303 if (j->flags.alt_syscall) {
1304 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1305 pdie("prctl(PR_ALT_SYSCALL)");
1306 }
1307
1308 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001309 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001310 * privilege-dropping syscalls :)
1311 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001312 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1313 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1314 warn("seccomp not supported");
1315 return;
1316 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001317 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001318 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001319}
1320
Will Drewry6ac91122011-10-21 16:38:58 -05001321/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001322static int init_exitstatus = 0;
1323
Will Drewry6ac91122011-10-21 16:38:58 -05001324void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001325{
1326 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001327}
1328
Will Drewry6ac91122011-10-21 16:38:58 -05001329int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001330{
1331 pid_t pid;
1332 int status;
1333 /* so that we exit with the right status */
1334 signal(SIGTERM, init_term);
1335 /* TODO(wad) self jail with seccomp_filters here. */
1336 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001337 /*
1338 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001339 * left inside our pid namespace or we get a signal.
1340 */
1341 if (pid == rootpid)
1342 init_exitstatus = status;
1343 }
1344 if (!WIFEXITED(init_exitstatus))
1345 _exit(MINIJAIL_ERR_INIT);
1346 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001347}
1348
Will Drewry6ac91122011-10-21 16:38:58 -05001349int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001350{
1351 size_t sz = 0;
1352 size_t bytes = read(fd, &sz, sizeof(sz));
1353 char *buf;
1354 int r;
1355 if (sizeof(sz) != bytes)
1356 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001357 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001358 return -E2BIG;
1359 buf = malloc(sz);
1360 if (!buf)
1361 return -ENOMEM;
1362 bytes = read(fd, buf, sz);
1363 if (bytes != sz) {
1364 free(buf);
1365 return -EINVAL;
1366 }
1367 r = minijail_unmarshal(j, buf, sz);
1368 free(buf);
1369 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001370}
1371
Will Drewry6ac91122011-10-21 16:38:58 -05001372int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001373{
1374 char *buf;
1375 size_t sz = minijail_size(j);
1376 ssize_t written;
1377 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001378
Elly Jonese1749eb2011-10-07 13:54:59 -04001379 if (!sz)
1380 return -EINVAL;
1381 buf = malloc(sz);
1382 r = minijail_marshal(j, buf, sz);
1383 if (r) {
1384 free(buf);
1385 return r;
1386 }
1387 /* Sends [size][minijail]. */
1388 written = write(fd, &sz, sizeof(sz));
1389 if (written != sizeof(sz)) {
1390 free(buf);
1391 return -EFAULT;
1392 }
1393 written = write(fd, buf, sz);
1394 if (written < 0 || (size_t) written != sz) {
1395 free(buf);
1396 return -EFAULT;
1397 }
1398 free(buf);
1399 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001400}
Elly Jonescd7a9042011-07-22 13:56:51 -04001401
Will Drewry6ac91122011-10-21 16:38:58 -05001402int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001403{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001404#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001405 /* Don't use LDPRELOAD on Brillo. */
1406 return 0;
1407#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001408 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1409 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1410 if (!newenv)
1411 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001412
Elly Jonese1749eb2011-10-07 13:54:59 -04001413 /* Only insert a separating space if we have something to separate... */
1414 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1415 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001416
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001417 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001418 setenv(kLdPreloadEnvVar, newenv, 1);
1419 free(newenv);
1420 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001421#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001422}
1423
Will Drewry6ac91122011-10-21 16:38:58 -05001424int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001425{
1426 int r = pipe(fds);
1427 char fd_buf[11];
1428 if (r)
1429 return r;
1430 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1431 if (r <= 0)
1432 return -EINVAL;
1433 setenv(kFdEnvVar, fd_buf, 1);
1434 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001435}
1436
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001437int setup_pipe_end(int fds[2], size_t index)
1438{
1439 if (index > 1)
1440 return -1;
1441
1442 close(fds[1 - index]);
1443 return fds[index];
1444}
1445
1446int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1447{
1448 if (index > 1)
1449 return -1;
1450
1451 close(fds[1 - index]);
1452 /* dup2(2) the corresponding end of the pipe into |fd|. */
1453 return dup2(fds[index], fd);
1454}
1455
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001456int minijail_run_internal(struct minijail *j, const char *filename,
1457 char *const argv[], pid_t *pchild_pid,
1458 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1459 int use_preload);
1460
Will Drewry6ac91122011-10-21 16:38:58 -05001461int API minijail_run(struct minijail *j, const char *filename,
1462 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001463{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001464 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1465 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001466}
1467
1468int API minijail_run_pid(struct minijail *j, const char *filename,
1469 char *const argv[], pid_t *pchild_pid)
1470{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001471 return minijail_run_internal(j, filename, argv, pchild_pid,
1472 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001473}
1474
1475int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001476 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001477{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001478 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1479 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001480}
1481
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001482int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001483 char *const argv[], pid_t *pchild_pid,
1484 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001485{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001486 return minijail_run_internal(j, filename, argv, pchild_pid,
1487 pstdin_fd, pstdout_fd, pstderr_fd, true);
1488}
1489
1490int API minijail_run_no_preload(struct minijail *j, const char *filename,
1491 char *const argv[])
1492{
1493 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1494 false);
1495}
1496
Samuel Tan63187f42015-10-16 13:01:53 -07001497int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001498 const char *filename,
1499 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001500 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001501 int *pstdin_fd, int *pstdout_fd,
1502 int *pstderr_fd) {
Samuel Tan63187f42015-10-16 13:01:53 -07001503 return minijail_run_internal(j, filename, argv, pchild_pid,
1504 pstdin_fd, pstdout_fd, pstderr_fd, false);
1505}
1506
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001507int minijail_run_internal(struct minijail *j, const char *filename,
1508 char *const argv[], pid_t *pchild_pid,
1509 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1510 int use_preload)
1511{
Elly Jonese1749eb2011-10-07 13:54:59 -04001512 char *oldenv, *oldenv_copy = NULL;
1513 pid_t child_pid;
1514 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001515 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001516 int stdout_fds[2];
1517 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001518 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001519 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001520 /* We need to remember this across the minijail_preexec() call. */
1521 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001522 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001523
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001524 if (use_preload) {
1525 oldenv = getenv(kLdPreloadEnvVar);
1526 if (oldenv) {
1527 oldenv_copy = strdup(oldenv);
1528 if (!oldenv_copy)
1529 return -ENOMEM;
1530 }
1531
1532 if (setup_preload())
1533 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001534 }
Will Drewryf89aef52011-09-16 16:48:57 -05001535
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001536 if (!use_preload) {
1537 if (j->flags.caps)
1538 die("Capabilities are not supported without "
1539 "LD_PRELOAD");
1540 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001541
Elly Jonesdd3e8512012-01-23 15:13:38 -05001542 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001543 * Make the process group ID of this process equal to its PID, so that
1544 * both the Minijail process and the jailed process can be killed
1545 * together.
1546 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1547 * the process is already a process group leader.
1548 */
1549 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1550 if (errno != EPERM) {
1551 pdie("setpgid(0, 0)");
1552 }
1553 }
1554
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001555 if (use_preload) {
1556 /*
1557 * Before we fork(2) and execve(2) the child process, we need
1558 * to open a pipe(2) to send the minijail configuration over.
1559 */
1560 if (setup_pipe(pipe_fds))
1561 return -EFAULT;
1562 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001563
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001564 /*
1565 * If we want to write to the child process' standard input,
1566 * create the pipe(2) now.
1567 */
1568 if (pstdin_fd) {
1569 if (pipe(stdin_fds))
1570 return -EFAULT;
1571 }
1572
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001573 /*
1574 * If we want to read from the child process' standard output,
1575 * create the pipe(2) now.
1576 */
1577 if (pstdout_fd) {
1578 if (pipe(stdout_fds))
1579 return -EFAULT;
1580 }
1581
1582 /*
1583 * If we want to read from the child process' standard error,
1584 * create the pipe(2) now.
1585 */
1586 if (pstderr_fd) {
1587 if (pipe(stderr_fds))
1588 return -EFAULT;
1589 }
1590
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001591 /*
1592 * If we want to set up a new uid/gid mapping in the user namespace,
1593 * create the pipe(2) to sync between parent and child.
1594 */
1595 if (j->flags.userns) {
1596 if (pipe(userns_pipe_fds))
1597 return -EFAULT;
1598 }
1599
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001600 /*
1601 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001602 *
1603 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1604 *
1605 * In multithreaded programs, there are a bunch of locks inside libc,
1606 * some of which may be held by other threads at the time that we call
1607 * minijail_run_pid(). If we call fork(), glibc does its level best to
1608 * ensure that we hold all of these locks before it calls clone()
1609 * internally and drop them after clone() returns, but when we call
1610 * sys_clone(2) directly, all that gets bypassed and we end up with a
1611 * child address space where some of libc's important locks are held by
1612 * other threads (which did not get cloned, and hence will never release
1613 * those locks). This is okay so long as we call exec() immediately
1614 * after, but a bunch of seemingly-innocent libc functions like setenv()
1615 * take locks.
1616 *
1617 * Hence, only call sys_clone() if we need to, in order to get at pid
1618 * namespacing. If we follow this path, the child's address space might
1619 * have broken locks; you may only call functions that do not acquire
1620 * any locks.
1621 *
1622 * Unfortunately, fork() acquires every lock it can get its hands on, as
1623 * previously detailed, so this function is highly likely to deadlock
1624 * later on (see "deadlock here") if we're multithreaded.
1625 *
1626 * We might hack around this by having the clone()d child (init of the
1627 * pid namespace) return directly, rather than leaving the clone()d
1628 * process hanging around to be init for the new namespace (and having
1629 * its fork()ed child return in turn), but that process would be crippled
1630 * with its libc locks potentially broken. We might try fork()ing in the
1631 * parent before we clone() to ensure that we own all the locks, but
1632 * then we have to have the forked child hanging around consuming
1633 * resources (and possibly having file descriptors / shared memory
1634 * regions / etc attached). We'd need to keep the child around to avoid
1635 * having its children get reparented to init.
1636 *
1637 * TODO(ellyjones): figure out if the "forked child hanging around"
1638 * problem is fixable or not. It would be nice if we worked in this
1639 * case.
1640 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001641 if (pid_namespace) {
1642 int clone_flags = CLONE_NEWPID | SIGCHLD;
1643 if (j->flags.userns)
1644 clone_flags |= CLONE_NEWUSER;
1645 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001646 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001647 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001648 }
Elly Jones761b7412012-06-13 15:49:52 -04001649
Elly Jonese1749eb2011-10-07 13:54:59 -04001650 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001651 if (use_preload) {
1652 free(oldenv_copy);
1653 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001654 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001655 }
Will Drewryf89aef52011-09-16 16:48:57 -05001656
Elly Jonese1749eb2011-10-07 13:54:59 -04001657 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001658 if (use_preload) {
1659 /* Restore parent's LD_PRELOAD. */
1660 if (oldenv_copy) {
1661 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1662 free(oldenv_copy);
1663 } else {
1664 unsetenv(kLdPreloadEnvVar);
1665 }
1666 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001667 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001668
Elly Jonese1749eb2011-10-07 13:54:59 -04001669 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001670
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001671 if (j->flags.pid_file)
1672 write_pid_file(j);
1673
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001674 if (j->flags.userns)
1675 write_ugid_mappings(j, userns_pipe_fds);
1676
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001677 if (use_preload) {
1678 /* Send marshalled minijail. */
1679 close(pipe_fds[0]); /* read endpoint */
1680 ret = minijail_to_fd(j, pipe_fds[1]);
1681 close(pipe_fds[1]); /* write endpoint */
1682 if (ret) {
1683 kill(j->initpid, SIGKILL);
1684 die("failed to send marshalled minijail");
1685 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001686 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001687
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001688 if (pchild_pid)
1689 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001690
1691 /*
1692 * If we want to write to the child process' standard input,
1693 * set up the write end of the pipe.
1694 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001695 if (pstdin_fd)
1696 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001697 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001698
1699 /*
1700 * If we want to read from the child process' standard output,
1701 * set up the read end of the pipe.
1702 */
1703 if (pstdout_fd)
1704 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001705 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001706
1707 /*
1708 * If we want to read from the child process' standard error,
1709 * set up the read end of the pipe.
1710 */
1711 if (pstderr_fd)
1712 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001713 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001714
Elly Jonese1749eb2011-10-07 13:54:59 -04001715 return 0;
1716 }
1717 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001718
Peter Qiu2860c462015-12-16 15:13:06 -08001719 if (j->flags.reset_signal_mask) {
1720 sigset_t signal_mask;
1721 if (sigemptyset(&signal_mask) != 0)
1722 pdie("sigemptyset failed");
1723 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1724 pdie("sigprocmask failed");
1725 }
1726
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001727 if (j->flags.userns)
1728 enter_user_namespace(j, userns_pipe_fds);
1729
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001730 /*
1731 * If we want to write to the jailed process' standard input,
1732 * set up the read end of the pipe.
1733 */
1734 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001735 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1736 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001737 die("failed to set up stdin pipe");
1738 }
1739
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001740 /*
1741 * If we want to read from the jailed process' standard output,
1742 * set up the write end of the pipe.
1743 */
1744 if (pstdout_fd) {
1745 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1746 STDOUT_FILENO) < 0)
1747 die("failed to set up stdout pipe");
1748 }
1749
1750 /*
1751 * If we want to read from the jailed process' standard error,
1752 * set up the write end of the pipe.
1753 */
1754 if (pstderr_fd) {
1755 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1756 STDERR_FILENO) < 0)
1757 die("failed to set up stderr pipe");
1758 }
1759
Dylan Reid791f5772015-09-14 20:02:42 -07001760 /* If running an init program, let it decide when/how to mount /proc. */
1761 if (pid_namespace && !do_init)
1762 j->flags.remount_proc_ro = 0;
1763
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001764 if (use_preload) {
1765 /* Strip out flags that cannot be inherited across execve(2). */
1766 minijail_preexec(j);
1767 } else {
1768 j->flags.pids = 0;
1769 }
1770 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001771 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001772
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001773 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001774 /*
1775 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001776 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001777 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001778 * a child to actually run the program. If |do_init == 0|, we
1779 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001780 *
1781 * If we're multithreaded, we'll probably deadlock here. See
1782 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001783 */
1784 child_pid = fork();
1785 if (child_pid < 0)
1786 _exit(child_pid);
1787 else if (child_pid > 0)
1788 init(child_pid); /* never returns */
1789 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001790
Elly Jonesdd3e8512012-01-23 15:13:38 -05001791 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001792 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001793 * calling process
1794 * -> execve()-ing process
1795 * If we are:
1796 * calling process
1797 * -> init()-ing process
1798 * -> execve()-ing process
1799 */
1800 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001801}
1802
Will Drewry6ac91122011-10-21 16:38:58 -05001803int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001804{
1805 int st;
1806 if (kill(j->initpid, SIGTERM))
1807 return -errno;
1808 if (waitpid(j->initpid, &st, 0) < 0)
1809 return -errno;
1810 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001811}
1812
Will Drewry6ac91122011-10-21 16:38:58 -05001813int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001814{
1815 int st;
1816 if (waitpid(j->initpid, &st, 0) < 0)
1817 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001818
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001819 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001820 int error_status = st;
1821 if (WIFSIGNALED(st)) {
1822 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001823 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001824 j->initpid, signum);
1825 /*
1826 * We return MINIJAIL_ERR_JAIL if the process received
1827 * SIGSYS, which happens when a syscall is blocked by
1828 * seccomp filters.
1829 * If not, we do what bash(1) does:
1830 * $? = 128 + signum
1831 */
1832 if (signum == SIGSYS) {
1833 error_status = MINIJAIL_ERR_JAIL;
1834 } else {
1835 error_status = 128 + signum;
1836 }
1837 }
1838 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001839 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001840
1841 int exit_status = WEXITSTATUS(st);
1842 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001843 info("child process %d exited with status %d",
1844 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001845
1846 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001847}
1848
Will Drewry6ac91122011-10-21 16:38:58 -05001849void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001850{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001851 if (j->flags.seccomp_filter && j->filter_prog) {
1852 free(j->filter_prog->filter);
1853 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001854 }
Dylan Reid648b2202015-10-23 00:50:00 -07001855 while (j->mounts_head) {
1856 struct mountpoint *m = j->mounts_head;
1857 j->mounts_head = j->mounts_head->next;
1858 free(m->type);
1859 free(m->dest);
1860 free(m->src);
1861 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001862 }
Dylan Reid648b2202015-10-23 00:50:00 -07001863 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001864 if (j->user)
1865 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08001866 if (j->suppl_gid_list)
1867 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05001868 if (j->chrootdir)
1869 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08001870 if (j->alt_syscall_table)
1871 free(j->alt_syscall_table);
Elly Jonese1749eb2011-10-07 13:54:59 -04001872 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001873}