blob: c7b573842df516a9e3cffbc3d65c06125294664c [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 Obesd16ac492015-12-03 14:44:35 -0800198int API minijail_set_supplementary_gids(struct minijail *j, size_t size,
199 const gid_t *list)
200{
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;
211 return 0;
212 }
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;
224 return 0;
225}
226
Will Drewry6ac91122011-10-21 16:38:58 -0500227int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400228{
229 char *buf = NULL;
230 struct passwd pw;
231 struct passwd *ppw = NULL;
232 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
233 if (sz == -1)
234 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400235
Elly Jonesdd3e8512012-01-23 15:13:38 -0500236 /*
237 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400238 * the maximum needed size of the buffer, so we don't have to search.
239 */
240 buf = malloc(sz);
241 if (!buf)
242 return -ENOMEM;
243 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500244 /*
245 * We're safe to free the buffer here. The strings inside pw point
246 * inside buf, but we don't use any of them; this leaves the pointers
247 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
248 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400249 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700250 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400251 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700252 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400253 minijail_change_uid(j, ppw->pw_uid);
254 j->user = strdup(user);
255 if (!j->user)
256 return -ENOMEM;
257 j->usergid = ppw->pw_gid;
258 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400259}
260
Will Drewry6ac91122011-10-21 16:38:58 -0500261int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400262{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700263 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700264 struct group gr;
265 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400266 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
267 if (sz == -1)
268 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400269
Elly Jonesdd3e8512012-01-23 15:13:38 -0500270 /*
271 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400272 * the maximum needed size of the buffer, so we don't have to search.
273 */
274 buf = malloc(sz);
275 if (!buf)
276 return -ENOMEM;
277 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500278 /*
279 * We're safe to free the buffer here. The strings inside gr point
280 * inside buf, but we don't use any of them; this leaves the pointers
281 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
282 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400283 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700284 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400285 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700286 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400287 minijail_change_gid(j, pgr->gr_gid);
288 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400289}
290
Will Drewry6ac91122011-10-21 16:38:58 -0500291void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400292{
293 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400294}
295
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700296void API minijail_no_new_privs(struct minijail *j)
297{
298 j->flags.no_new_privs = 1;
299}
300
Will Drewry6ac91122011-10-21 16:38:58 -0500301void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400302{
303 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500304}
305
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700306void API minijail_log_seccomp_filter_failures(struct minijail *j)
307{
308 j->flags.log_seccomp_filter = 1;
309}
310
Will Drewry6ac91122011-10-21 16:38:58 -0500311void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400312{
313 j->caps = capmask;
314 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400315}
316
Peter Qiu2860c462015-12-16 15:13:06 -0800317void API minijail_reset_signal_mask(struct minijail* j) {
318 j->flags.reset_signal_mask = 1;
319}
320
Will Drewry6ac91122011-10-21 16:38:58 -0500321void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400322{
323 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400324}
325
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700326void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
327{
328 int ns_fd = open(ns_path, O_RDONLY);
329 if (ns_fd < 0) {
330 pdie("failed to open namespace '%s'", ns_path);
331 }
332 j->mountns_fd = ns_fd;
333 j->flags.enter_vfs = 1;
334}
335
Will Drewry6ac91122011-10-21 16:38:58 -0500336void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400337{
Elly Jonese58176c2012-01-23 11:46:17 -0500338 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700339 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400340 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800341 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400342}
343
Dylan Reidf7942472015-11-18 17:55:26 -0800344void API minijail_namespace_ipc(struct minijail *j)
345{
346 j->flags.ipc = 1;
347}
348
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400349void API minijail_namespace_net(struct minijail *j)
350{
351 j->flags.net = 1;
352}
353
Dylan Reid1102f5a2015-09-15 11:52:20 -0700354void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
355{
356 int ns_fd = open(ns_path, O_RDONLY);
357 if (ns_fd < 0) {
358 pdie("failed to open namespace '%s'", ns_path);
359 }
360 j->netns_fd = ns_fd;
361 j->flags.enter_net = 1;
362}
363
Dylan Reid791f5772015-09-14 20:02:42 -0700364void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400365{
366 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700367 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400368}
369
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800370void API minijail_namespace_user(struct minijail *j)
371{
372 j->flags.userns = 1;
373}
374
375int API minijail_uidmap(struct minijail *j, const char *uidmap)
376{
377 j->uidmap = strdup(uidmap);
378 if (!j->uidmap)
379 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800380 char *ch;
381 for (ch = j->uidmap; *ch; ch++) {
382 if (*ch == ',')
383 *ch = '\n';
384 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800385 return 0;
386}
387
388int API minijail_gidmap(struct minijail *j, const char *gidmap)
389{
390 j->gidmap = strdup(gidmap);
391 if (!j->gidmap)
392 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800393 char *ch;
394 for (ch = j->gidmap; *ch; ch++) {
395 if (*ch == ',')
396 *ch = '\n';
397 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800398 return 0;
399}
400
Will Drewry6ac91122011-10-21 16:38:58 -0500401void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400402{
403 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400404}
405
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800406void API minijail_run_as_init(struct minijail *j)
407{
408 /*
409 * Since the jailed program will become 'init' in the new PID namespace,
410 * Minijail does not need to fork an 'init' process.
411 */
412 j->flags.do_init = 0;
413}
414
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700415int API minijail_enter_chroot(struct minijail *j, const char *dir)
416{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400417 if (j->chrootdir)
418 return -EINVAL;
419 j->chrootdir = strdup(dir);
420 if (!j->chrootdir)
421 return -ENOMEM;
422 j->flags.chroot = 1;
423 return 0;
424}
425
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800426int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
427{
428 if (j->chrootdir)
429 return -EINVAL;
430 j->chrootdir = strdup(dir);
431 if (!j->chrootdir)
432 return -ENOMEM;
433 j->flags.pivot_root = 1;
434 return 0;
435}
436
Dylan Reida14e08d2015-10-22 21:05:29 -0700437static char *append_external_path(const char *external_path,
438 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700439{
Dylan Reida14e08d2015-10-22 21:05:29 -0700440 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700441 size_t pathlen;
442
Dylan Reid08946cc2015-09-16 19:10:57 -0700443 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700444 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
445 path = malloc(pathlen);
446 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700447
Dylan Reida14e08d2015-10-22 21:05:29 -0700448 return path;
449}
450
451char API *minijail_get_original_path(struct minijail *j,
452 const char *path_inside_chroot)
453{
Dylan Reid648b2202015-10-23 00:50:00 -0700454 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700455
Dylan Reid648b2202015-10-23 00:50:00 -0700456 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700457 while (b) {
458 /*
459 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700460 * mount, then the original path is exactly the source of
461 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700462 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700463 * mount source = /some/path/exe, mount dest =
464 * /chroot/path/exe Then when getting the original path of
465 * "/chroot/path/exe", the source of that mount,
466 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700467 */
468 if (!strcmp(b->dest, path_inside_chroot))
469 return strdup(b->src);
470
471 /*
472 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700473 * mount, take the suffix of the chroot path relative to the
474 * mount destination path, and append it to the mount source
475 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700476 */
477 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
478 const char *relative_path =
479 path_inside_chroot + strlen(b->dest);
480 return append_external_path(b->src, relative_path);
481 }
482 b = b->next;
483 }
484
485 /* If there is a chroot path, append |path_inside_chroot| to that. */
486 if (j->chrootdir)
487 return append_external_path(j->chrootdir, path_inside_chroot);
488
489 /* No chroot, so the path outside is the same as it is inside. */
490 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700491}
492
Lee Campbell11af0622014-05-22 12:36:04 -0700493void API minijail_mount_tmp(struct minijail *j)
494{
495 j->flags.mount_tmp = 1;
496}
497
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800498int API minijail_write_pid_file(struct minijail *j, const char *path)
499{
500 j->pid_file_path = strdup(path);
501 if (!j->pid_file_path)
502 return -ENOMEM;
503 j->flags.pid_file = 1;
504 return 0;
505}
506
Dylan Reid648b2202015-10-23 00:50:00 -0700507int API minijail_mount(struct minijail *j, const char *src, const char *dest,
508 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700509{
Dylan Reid648b2202015-10-23 00:50:00 -0700510 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400511
512 if (*dest != '/')
513 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700514 m = calloc(1, sizeof(*m));
515 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400516 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700517 m->dest = strdup(dest);
518 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400519 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700520 m->src = strdup(src);
521 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400522 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700523 m->type = strdup(type);
524 if (!m->type)
525 goto error;
526 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400527
Dylan Reid648b2202015-10-23 00:50:00 -0700528 info("mount %s -> %s type %s", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400529
Elly Jonesdd3e8512012-01-23 15:13:38 -0500530 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700531 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400532 * containing vfs namespace.
533 */
534 minijail_namespace_vfs(j);
535
Dylan Reid648b2202015-10-23 00:50:00 -0700536 if (j->mounts_tail)
537 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400538 else
Dylan Reid648b2202015-10-23 00:50:00 -0700539 j->mounts_head = m;
540 j->mounts_tail = m;
541 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400542
543 return 0;
544
545error:
Dylan Reid648b2202015-10-23 00:50:00 -0700546 free(m->src);
547 free(m->dest);
548 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400549 return -ENOMEM;
550}
551
Dylan Reid648b2202015-10-23 00:50:00 -0700552int API minijail_bind(struct minijail *j, const char *src, const char *dest,
553 int writeable)
554{
555 unsigned long flags = MS_BIND;
556
557 if (!writeable)
558 flags |= MS_RDONLY;
559
560 return minijail_mount(j, src, dest, "", flags);
561}
562
Will Drewry6ac91122011-10-21 16:38:58 -0500563void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400564{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700565 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
566 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800567 warn("not loading seccomp filter,"
568 " seccomp not supported");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700569 return;
570 }
571 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400572 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800573 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700574 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400575 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800576
577 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700578 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
579 die("failed to compile seccomp filter BPF program in '%s'",
580 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800581 }
582
583 j->filter_len = fprog->len;
584 j->filter_prog = fprog;
585
Elly Jonese1749eb2011-10-07 13:54:59 -0400586 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500587}
588
Andrew Brestickereac28942015-11-11 16:04:46 -0800589int API minijail_use_alt_syscall(struct minijail *j, const char *table)
590{
591 j->alt_syscall_table = strdup(table);
592 if (!j->alt_syscall_table)
593 return -ENOMEM;
594 j->flags.alt_syscall = 1;
595 return 0;
596}
597
Will Drewryf89aef52011-09-16 16:48:57 -0500598struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400599 size_t available;
600 size_t total;
601 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500602};
603
Will Drewry6ac91122011-10-21 16:38:58 -0500604void marshal_state_init(struct marshal_state *state,
605 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400606{
607 state->available = available;
608 state->buf = buf;
609 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500610}
611
Will Drewry6ac91122011-10-21 16:38:58 -0500612void marshal_append(struct marshal_state *state,
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800613 void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400614{
615 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500616
Elly Jonese1749eb2011-10-07 13:54:59 -0400617 /* Up to |available| will be written. */
618 if (copy_len) {
619 memcpy(state->buf, src, copy_len);
620 state->buf += copy_len;
621 state->available -= copy_len;
622 }
623 /* |total| will contain the expected length. */
624 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500625}
626
Will Drewry6ac91122011-10-21 16:38:58 -0500627void minijail_marshal_helper(struct marshal_state *state,
628 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400629{
Dylan Reid648b2202015-10-23 00:50:00 -0700630 struct mountpoint *m = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400631 marshal_append(state, (char *)j, sizeof(*j));
632 if (j->user)
633 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800634 if (j->suppl_gid_list) {
635 marshal_append(state, j->suppl_gid_list,
636 j->suppl_gid_count * sizeof(gid_t));
637 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400638 if (j->chrootdir)
639 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800640 if (j->alt_syscall_table) {
641 marshal_append(state, j->alt_syscall_table,
642 strlen(j->alt_syscall_table) + 1);
643 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800644 if (j->flags.seccomp_filter && j->filter_prog) {
645 struct sock_fprog *fp = j->filter_prog;
646 marshal_append(state, (char *)fp->filter,
647 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400648 }
Dylan Reid648b2202015-10-23 00:50:00 -0700649 for (m = j->mounts_head; m; m = m->next) {
650 marshal_append(state, m->src, strlen(m->src) + 1);
651 marshal_append(state, m->dest, strlen(m->dest) + 1);
652 marshal_append(state, m->type, strlen(m->type) + 1);
653 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400654 }
Will Drewryf89aef52011-09-16 16:48:57 -0500655}
656
Will Drewry6ac91122011-10-21 16:38:58 -0500657size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400658{
659 struct marshal_state state;
660 marshal_state_init(&state, NULL, 0);
661 minijail_marshal_helper(&state, j);
662 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500663}
664
Elly Jonese1749eb2011-10-07 13:54:59 -0400665int minijail_marshal(const struct minijail *j, char *buf, size_t available)
666{
667 struct marshal_state state;
668 marshal_state_init(&state, buf, available);
669 minijail_marshal_helper(&state, j);
670 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500671}
672
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800673/*
674 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400675 * @length Number of bytes to consume
676 * @buf Buffer to consume from
677 * @buflength Size of @buf
678 *
679 * Returns a pointer to the base of the bytes, or NULL for errors.
680 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700681void *consumebytes(size_t length, char **buf, size_t *buflength)
682{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400683 char *p = *buf;
684 if (length > *buflength)
685 return NULL;
686 *buf += length;
687 *buflength -= length;
688 return p;
689}
690
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800691/*
692 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400693 * @buf Buffer to consume
694 * @length Length of buffer
695 *
696 * Returns a pointer to the base of the string, or NULL for errors.
697 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700698char *consumestr(char **buf, size_t *buflength)
699{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400700 size_t len = strnlen(*buf, *buflength);
701 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700702 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400703 return NULL;
704 return consumebytes(len + 1, buf, buflength);
705}
706
Elly Jonese1749eb2011-10-07 13:54:59 -0400707int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
708{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800709 size_t i;
710 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500711 int ret = -EINVAL;
712
Elly Jonese1749eb2011-10-07 13:54:59 -0400713 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500714 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400715 memcpy((void *)j, serialized, sizeof(*j));
716 serialized += sizeof(*j);
717 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500718
Will Drewrybee7ba72011-10-21 20:47:01 -0500719 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700720 j->mounts_head = NULL;
721 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800722 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500723
Elly Jonese1749eb2011-10-07 13:54:59 -0400724 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400725 char *user = consumestr(&serialized, &length);
726 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500727 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400728 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500729 if (!j->user)
730 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400731 }
Will Drewryf89aef52011-09-16 16:48:57 -0500732
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800733 if (j->suppl_gid_list) { /* stale pointer */
734 if (j->suppl_gid_count > NGROUPS_MAX) {
735 goto bad_gid_list;
736 }
737 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
738 void *gid_list_bytes =
739 consumebytes(gid_list_size, &serialized, &length);
740 if (!gid_list_bytes)
741 goto bad_gid_list;
742
743 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
744 if (!j->suppl_gid_list)
745 goto bad_gid_list;
746
747 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
748 }
749
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400750 if (j->chrootdir) { /* stale pointer */
751 char *chrootdir = consumestr(&serialized, &length);
752 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500753 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400754 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500755 if (!j->chrootdir)
756 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400757 }
758
Andrew Brestickereac28942015-11-11 16:04:46 -0800759 if (j->alt_syscall_table) { /* stale pointer */
760 char *alt_syscall_table = consumestr(&serialized, &length);
761 if (!alt_syscall_table)
762 goto bad_syscall_table;
763 j->alt_syscall_table = strdup(alt_syscall_table);
764 if (!j->alt_syscall_table)
765 goto bad_syscall_table;
766 }
767
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800768 if (j->flags.seccomp_filter && j->filter_len > 0) {
769 size_t ninstrs = j->filter_len;
770 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
771 ninstrs > USHRT_MAX)
772 goto bad_filters;
773
774 size_t program_len = ninstrs * sizeof(struct sock_filter);
775 void *program = consumebytes(program_len, &serialized, &length);
776 if (!program)
777 goto bad_filters;
778
779 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800780 if (!j->filter_prog)
781 goto bad_filters;
782
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800783 j->filter_prog->len = ninstrs;
784 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800785 if (!j->filter_prog->filter)
786 goto bad_filter_prog_instrs;
787
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800788 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400789 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400790
Dylan Reid648b2202015-10-23 00:50:00 -0700791 count = j->mounts_count;
792 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400793 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700794 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400795 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700796 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400797 const char *src = consumestr(&serialized, &length);
798 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700799 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400800 dest = consumestr(&serialized, &length);
801 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700802 goto bad_mounts;
803 type = consumestr(&serialized, &length);
804 if (!type)
805 goto bad_mounts;
806 flags = consumebytes(sizeof(*flags), &serialized, &length);
807 if (!flags)
808 goto bad_mounts;
809 if (minijail_mount(j, src, dest, type, *flags))
810 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400811 }
812
Elly Jonese1749eb2011-10-07 13:54:59 -0400813 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500814
Dylan Reid648b2202015-10-23 00:50:00 -0700815bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800816 if (j->flags.seccomp_filter && j->filter_len > 0) {
817 free(j->filter_prog->filter);
818 free(j->filter_prog);
819 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800820bad_filter_prog_instrs:
821 if (j->filter_prog)
822 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500823bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800824 if (j->alt_syscall_table)
825 free(j->alt_syscall_table);
826bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500827 if (j->chrootdir)
828 free(j->chrootdir);
829bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800830 if (j->suppl_gid_list)
831 free(j->suppl_gid_list);
832bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500833 if (j->user)
834 free(j->user);
835clear_pointers:
836 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800837 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500838 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800839 j->alt_syscall_table = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500840out:
841 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500842}
843
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800844static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
845{
846 int fd, ret, len;
847 size_t sz;
848 char fname[32];
849 close(pipe_fds[0]);
850
851 sz = sizeof(fname);
852 if (j->uidmap) {
853 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700854 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800855 die("failed to write file name of uid_map");
856 fd = open(fname, O_WRONLY);
857 if (fd < 0)
858 pdie("failed to open '%s'", fname);
859 len = strlen(j->uidmap);
860 if (write(fd, j->uidmap, len) < len)
861 die("failed to set uid_map");
862 close(fd);
863 }
864 if (j->gidmap) {
865 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700866 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800867 die("failed to write file name of gid_map");
868 fd = open(fname, O_WRONLY);
869 if (fd < 0)
870 pdie("failed to open '%s'", fname);
871 len = strlen(j->gidmap);
872 if (write(fd, j->gidmap, len) < len)
873 die("failed to set gid_map");
874 close(fd);
875 }
876
877 close(pipe_fds[1]);
878}
879
880static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
881{
882 char buf;
883
884 close(pipe_fds[1]);
885
886 /* Wait for parent to set up uid/gid mappings. */
887 if (read(pipe_fds[0], &buf, 1) != 0)
888 die("failed to sync with parent");
889 close(pipe_fds[0]);
890
891 if (j->uidmap && setresuid(0, 0, 0))
892 pdie("setresuid");
893 if (j->gidmap && setresgid(0, 0, 0))
894 pdie("setresgid");
895}
896
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800897/*
898 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -0700899 * @j Minijail these mounts are for
900 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400901 *
902 * Returns 0 for success.
903 */
Dylan Reid648b2202015-10-23 00:50:00 -0700904static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700905{
Dylan Reid648b2202015-10-23 00:50:00 -0700906 int ret;
907 char *dest;
908 int remount_ro = 0;
909
Elly Jones51a5b6c2011-10-12 19:09:26 -0400910 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700911 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400912 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700913
914 /*
915 * R/O bind mounts have to be remounted since bind and ro can't both be
916 * specified in the original bind mount. Remount R/O after the initial
917 * mount.
918 */
919 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
920 remount_ro = 1;
921 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500922 }
Dylan Reid648b2202015-10-23 00:50:00 -0700923
924 ret = mount(m->src, dest, m->type, m->flags, NULL);
925 if (ret)
926 pdie("mount: %s -> %s", m->src, dest);
927
928 if (remount_ro) {
929 m->flags |= MS_RDONLY;
930 ret = mount(m->src, dest, NULL,
931 m->flags | MS_REMOUNT, NULL);
932 if (ret)
933 pdie("bind ro: %s -> %s", m->src, dest);
934 }
935
Elly Jones51a5b6c2011-10-12 19:09:26 -0400936 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700937 if (m->next)
938 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400939 return ret;
940}
941
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700942int enter_chroot(const struct minijail *j)
943{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400944 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700945
946 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400947 return ret;
948
949 if (chroot(j->chrootdir))
950 return -errno;
951
952 if (chdir("/"))
953 return -errno;
954
955 return 0;
956}
957
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800958int enter_pivot_root(const struct minijail *j)
959{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800960 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -0700961
962 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800963 return ret;
964
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800965 /*
966 * Keep the fd for both old and new root.
967 * It will be used in fchdir later.
968 */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800969 oldroot = open("/", O_DIRECTORY | O_RDONLY);
970 if (oldroot < 0)
971 pdie("failed to open / for fchdir");
972 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
973 if (newroot < 0)
974 pdie("failed to open %s for fchdir", j->chrootdir);
975
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800976 /*
977 * To ensure chrootdir is the root of a file system,
978 * do a self bind mount.
979 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800980 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
981 pdie("failed to bind mount '%s'", j->chrootdir);
982 if (chdir(j->chrootdir))
983 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800984 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800985 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800986
987 /*
988 * Now the old root is mounted on top of the new root. Use fchdir to
989 * change to the old root and unmount it.
990 */
991 if (fchdir(oldroot))
992 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800993 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800994 if (umount2(".", MNT_DETACH))
995 pdie("umount(/)");
996 /* Change back to the new root. */
997 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800998 return -errno;
999 if (chroot("/"))
1000 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001001 /* Set correct CWD for getcwd(3). */
1002 if (chdir("/"))
1003 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001004
1005 return 0;
1006}
1007
Lee Campbell11af0622014-05-22 12:36:04 -07001008int mount_tmp(void)
1009{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001010 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001011}
1012
Dylan Reid791f5772015-09-14 20:02:42 -07001013int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001014{
1015 const char *kProcPath = "/proc";
1016 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001017 /*
1018 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001019 * /proc in our namespace, which means using MS_REMOUNT here would
1020 * mutate our parent's mount as well, even though we're in a VFS
1021 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001022 * and make our own. However, if we are in a new user namespace, /proc
1023 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001024 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001025 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001026 return -errno;
1027 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1028 return -errno;
1029 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001030}
1031
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001032static void write_pid_file(const struct minijail *j)
1033{
1034 FILE *fp = fopen(j->pid_file_path, "w");
1035
1036 if (!fp)
1037 pdie("failed to open '%s'", j->pid_file_path);
1038 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
1039 pdie("fprintf(%s)", j->pid_file_path);
1040 if (fclose(fp))
1041 pdie("fclose(%s)", j->pid_file_path);
1042}
1043
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001044void drop_ugid(const struct minijail *j)
1045{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001046 if (j->flags.usergroups && j->flags.suppl_gids) {
1047 die("tried to inherit *and* set supplementary groups;"
1048 " can only do one");
1049 }
1050
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001051 if (j->flags.usergroups) {
1052 if (initgroups(j->user, j->usergid))
1053 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001054 } else if (j->flags.suppl_gids) {
1055 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1056 pdie("setgroups");
1057 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001058 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001059 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001060 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001061 * users.
1062 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001063 if ((j->uid || j->gid) && setgroups(0, NULL))
1064 pdie("setgroups");
1065 }
1066
1067 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1068 pdie("setresgid");
1069
1070 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1071 pdie("setresuid");
1072}
1073
Mike Frysinger3adfef72013-05-09 17:19:08 -04001074/*
1075 * We specifically do not use cap_valid() as that only tells us the last
1076 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001077 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001078 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001079 * kernel).
1080 * Normally, we suck up the answer via /proc. On Android, not all processes are
1081 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1082 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001083 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001084static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001085{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001086 unsigned int last_valid_cap = 0;
1087 if (is_android()) {
1088 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1089 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001090
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001091 /* |last_valid_cap| will be the first failing value. */
1092 if (last_valid_cap > 0) {
1093 last_valid_cap--;
1094 }
1095 } else {
1096 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1097 FILE *fp = fopen(cap_file, "re");
1098 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1099 pdie("fscanf(%s)", cap_file);
1100 fclose(fp);
1101 }
Dylan Reidf682d472015-09-17 21:39:07 -07001102 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001103}
1104
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001105void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001106{
1107 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001108 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001109 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001110 unsigned int i;
1111 if (!caps)
1112 die("can't get process caps");
1113 if (cap_clear_flag(caps, CAP_INHERITABLE))
1114 die("can't clear inheritable caps");
1115 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1116 die("can't clear effective caps");
1117 if (cap_clear_flag(caps, CAP_PERMITTED))
1118 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001119 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001120 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001121 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001122 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001123 flag[0] = i;
1124 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001125 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001126 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001127 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001128 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001129 die("can't add inheritable cap");
1130 }
1131 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001132 die("can't apply initial cleaned capset");
1133
1134 /*
1135 * Instead of dropping bounding set first, do it here in case
1136 * the caller had a more permissive bounding set which could
1137 * have been used above to raise a capability that wasn't already
1138 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1139 */
Dylan Reidf682d472015-09-17 21:39:07 -07001140 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001141 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001142 continue;
1143 if (prctl(PR_CAPBSET_DROP, i))
1144 pdie("prctl(PR_CAPBSET_DROP)");
1145 }
Kees Cook323878a2013-02-05 15:35:24 -08001146
1147 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001148 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001149 flag[0] = CAP_SETPCAP;
1150 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1151 die("can't clear effective cap");
1152 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1153 die("can't clear permitted cap");
1154 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1155 die("can't clear inheritable cap");
1156 }
1157
1158 if (cap_set_proc(caps))
1159 die("can't apply final cleaned capset");
1160
1161 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001162}
1163
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001164void set_seccomp_filter(const struct minijail *j)
1165{
1166 /*
1167 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1168 * in the kernel source tree for an explanation of the parameters.
1169 */
1170 if (j->flags.no_new_privs) {
1171 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1172 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1173 }
1174
1175 /*
1176 * If we're logging seccomp filter failures,
1177 * install the SIGSYS handler first.
1178 */
1179 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1180 if (install_sigsys_handler())
1181 pdie("install SIGSYS handler");
1182 warn("logging seccomp filter failures");
1183 }
1184
1185 /*
1186 * Install the syscall filter.
1187 */
1188 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001189 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1190 j->filter_prog)) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001191 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1192 warn("seccomp not supported");
1193 return;
1194 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001195 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001196 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001197 }
1198}
1199
Will Drewry6ac91122011-10-21 16:38:58 -05001200void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001201{
Dylan Reidf682d472015-09-17 21:39:07 -07001202 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001203 * If we're dropping caps, get the last valid cap from /proc now,
1204 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001205 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001206 unsigned int last_valid_cap = 0;
1207 if (j->flags.caps)
1208 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001209
Elly Jonese1749eb2011-10-07 13:54:59 -04001210 if (j->flags.pids)
1211 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001212 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001213
Elly Jonese1749eb2011-10-07 13:54:59 -04001214 if (j->flags.usergroups && !j->user)
1215 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001216
Elly Jonesdd3e8512012-01-23 15:13:38 -05001217 /*
1218 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001219 * so we don't even try. If any of our operations fail, we abort() the
1220 * entire process.
1221 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001222 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1223 pdie("setns(CLONE_NEWNS)");
1224
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001225 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001226 if (unshare(CLONE_NEWNS))
1227 pdie("unshare(vfs)");
1228 /*
1229 * Remount all filesystems as private. If they are shared
1230 * new bind mounts will creep out of our namespace.
1231 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1232 */
1233 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1234 pdie("mount(/, private)");
1235 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001236
Dylan Reidf7942472015-11-18 17:55:26 -08001237 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1238 pdie("unshare(ipc)");
1239 }
1240
Dylan Reid1102f5a2015-09-15 11:52:20 -07001241 if (j->flags.enter_net) {
1242 if (setns(j->netns_fd, CLONE_NEWNET))
1243 pdie("setns(CLONE_NEWNET)");
1244 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001245 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001246 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001247
Elly Jones51a5b6c2011-10-12 19:09:26 -04001248 if (j->flags.chroot && enter_chroot(j))
1249 pdie("chroot");
1250
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001251 if (j->flags.pivot_root && enter_pivot_root(j))
1252 pdie("pivot_root");
1253
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001254 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001255 pdie("mount_tmp");
1256
Dylan Reid791f5772015-09-14 20:02:42 -07001257 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001258 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001259
Elly Jonese1749eb2011-10-07 13:54:59 -04001260 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001261 /*
1262 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001263 * capability to change uids, our attempt to use setuid()
1264 * below will fail. Hang on to root caps across setuid(), then
1265 * lock securebits.
1266 */
1267 if (prctl(PR_SET_KEEPCAPS, 1))
1268 pdie("prctl(PR_SET_KEEPCAPS)");
1269 if (prctl
1270 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1271 pdie("prctl(PR_SET_SECUREBITS)");
1272 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001273
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001274 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001275 * If we're setting no_new_privs, we can drop privileges
1276 * before setting seccomp filter. This way filter policies
1277 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001278 */
1279 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001280 drop_ugid(j);
1281 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001282 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001283
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001284 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001285 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001286 /*
1287 * If we're not setting no_new_privs,
1288 * we need to set seccomp filter *before* dropping privileges.
1289 * WARNING: this means that filter policies *must* allow
1290 * setgroups()/setresgid()/setresuid() for dropping root and
1291 * capget()/capset()/prctl() for dropping caps.
1292 */
1293 set_seccomp_filter(j);
1294
1295 drop_ugid(j);
1296 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001297 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001298 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001299
Elly Jonesdd3e8512012-01-23 15:13:38 -05001300 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001301 * Select the specified alternate syscall table. The table must not
1302 * block prctl(2) if we're using seccomp as well.
1303 */
1304 if (j->flags.alt_syscall) {
1305 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1306 pdie("prctl(PR_ALT_SYSCALL)");
1307 }
1308
1309 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001310 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001311 * privilege-dropping syscalls :)
1312 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001313 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1314 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1315 warn("seccomp not supported");
1316 return;
1317 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001318 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001319 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001320}
1321
Will Drewry6ac91122011-10-21 16:38:58 -05001322/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001323static int init_exitstatus = 0;
1324
Will Drewry6ac91122011-10-21 16:38:58 -05001325void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001326{
1327 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001328}
1329
Will Drewry6ac91122011-10-21 16:38:58 -05001330int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001331{
1332 pid_t pid;
1333 int status;
1334 /* so that we exit with the right status */
1335 signal(SIGTERM, init_term);
1336 /* TODO(wad) self jail with seccomp_filters here. */
1337 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001338 /*
1339 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001340 * left inside our pid namespace or we get a signal.
1341 */
1342 if (pid == rootpid)
1343 init_exitstatus = status;
1344 }
1345 if (!WIFEXITED(init_exitstatus))
1346 _exit(MINIJAIL_ERR_INIT);
1347 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001348}
1349
Will Drewry6ac91122011-10-21 16:38:58 -05001350int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001351{
1352 size_t sz = 0;
1353 size_t bytes = read(fd, &sz, sizeof(sz));
1354 char *buf;
1355 int r;
1356 if (sizeof(sz) != bytes)
1357 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001358 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001359 return -E2BIG;
1360 buf = malloc(sz);
1361 if (!buf)
1362 return -ENOMEM;
1363 bytes = read(fd, buf, sz);
1364 if (bytes != sz) {
1365 free(buf);
1366 return -EINVAL;
1367 }
1368 r = minijail_unmarshal(j, buf, sz);
1369 free(buf);
1370 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001371}
1372
Will Drewry6ac91122011-10-21 16:38:58 -05001373int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001374{
1375 char *buf;
1376 size_t sz = minijail_size(j);
1377 ssize_t written;
1378 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001379
Elly Jonese1749eb2011-10-07 13:54:59 -04001380 if (!sz)
1381 return -EINVAL;
1382 buf = malloc(sz);
1383 r = minijail_marshal(j, buf, sz);
1384 if (r) {
1385 free(buf);
1386 return r;
1387 }
1388 /* Sends [size][minijail]. */
1389 written = write(fd, &sz, sizeof(sz));
1390 if (written != sizeof(sz)) {
1391 free(buf);
1392 return -EFAULT;
1393 }
1394 written = write(fd, buf, sz);
1395 if (written < 0 || (size_t) written != sz) {
1396 free(buf);
1397 return -EFAULT;
1398 }
1399 free(buf);
1400 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001401}
Elly Jonescd7a9042011-07-22 13:56:51 -04001402
Will Drewry6ac91122011-10-21 16:38:58 -05001403int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001404{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001405#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001406 /* Don't use LDPRELOAD on Brillo. */
1407 return 0;
1408#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001409 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1410 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1411 if (!newenv)
1412 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001413
Elly Jonese1749eb2011-10-07 13:54:59 -04001414 /* Only insert a separating space if we have something to separate... */
1415 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1416 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001417
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001418 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001419 setenv(kLdPreloadEnvVar, newenv, 1);
1420 free(newenv);
1421 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001422#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001423}
1424
Will Drewry6ac91122011-10-21 16:38:58 -05001425int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001426{
1427 int r = pipe(fds);
1428 char fd_buf[11];
1429 if (r)
1430 return r;
1431 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1432 if (r <= 0)
1433 return -EINVAL;
1434 setenv(kFdEnvVar, fd_buf, 1);
1435 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001436}
1437
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001438int setup_pipe_end(int fds[2], size_t index)
1439{
1440 if (index > 1)
1441 return -1;
1442
1443 close(fds[1 - index]);
1444 return fds[index];
1445}
1446
1447int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1448{
1449 if (index > 1)
1450 return -1;
1451
1452 close(fds[1 - index]);
1453 /* dup2(2) the corresponding end of the pipe into |fd|. */
1454 return dup2(fds[index], fd);
1455}
1456
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001457int minijail_run_internal(struct minijail *j, const char *filename,
1458 char *const argv[], pid_t *pchild_pid,
1459 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1460 int use_preload);
1461
Will Drewry6ac91122011-10-21 16:38:58 -05001462int API minijail_run(struct minijail *j, const char *filename,
1463 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001464{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001465 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1466 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001467}
1468
1469int API minijail_run_pid(struct minijail *j, const char *filename,
1470 char *const argv[], pid_t *pchild_pid)
1471{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001472 return minijail_run_internal(j, filename, argv, pchild_pid,
1473 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001474}
1475
1476int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001477 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001478{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001479 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1480 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001481}
1482
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001483int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001484 char *const argv[], pid_t *pchild_pid,
1485 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001486{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001487 return minijail_run_internal(j, filename, argv, pchild_pid,
1488 pstdin_fd, pstdout_fd, pstderr_fd, true);
1489}
1490
1491int API minijail_run_no_preload(struct minijail *j, const char *filename,
1492 char *const argv[])
1493{
1494 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1495 false);
1496}
1497
Samuel Tan63187f42015-10-16 13:01:53 -07001498int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001499 const char *filename,
1500 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001501 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001502 int *pstdin_fd, int *pstdout_fd,
1503 int *pstderr_fd) {
Samuel Tan63187f42015-10-16 13:01:53 -07001504 return minijail_run_internal(j, filename, argv, pchild_pid,
1505 pstdin_fd, pstdout_fd, pstderr_fd, false);
1506}
1507
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001508int minijail_run_internal(struct minijail *j, const char *filename,
1509 char *const argv[], pid_t *pchild_pid,
1510 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1511 int use_preload)
1512{
Elly Jonese1749eb2011-10-07 13:54:59 -04001513 char *oldenv, *oldenv_copy = NULL;
1514 pid_t child_pid;
1515 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001516 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001517 int stdout_fds[2];
1518 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001519 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001520 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001521 /* We need to remember this across the minijail_preexec() call. */
1522 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001523 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001524
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001525 if (use_preload) {
1526 oldenv = getenv(kLdPreloadEnvVar);
1527 if (oldenv) {
1528 oldenv_copy = strdup(oldenv);
1529 if (!oldenv_copy)
1530 return -ENOMEM;
1531 }
1532
1533 if (setup_preload())
1534 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001535 }
Will Drewryf89aef52011-09-16 16:48:57 -05001536
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001537 if (!use_preload) {
1538 if (j->flags.caps)
1539 die("Capabilities are not supported without "
1540 "LD_PRELOAD");
1541 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001542
Elly Jonesdd3e8512012-01-23 15:13:38 -05001543 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001544 * Make the process group ID of this process equal to its PID, so that
1545 * both the Minijail process and the jailed process can be killed
1546 * together.
1547 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1548 * the process is already a process group leader.
1549 */
1550 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1551 if (errno != EPERM) {
1552 pdie("setpgid(0, 0)");
1553 }
1554 }
1555
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001556 if (use_preload) {
1557 /*
1558 * Before we fork(2) and execve(2) the child process, we need
1559 * to open a pipe(2) to send the minijail configuration over.
1560 */
1561 if (setup_pipe(pipe_fds))
1562 return -EFAULT;
1563 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001564
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001565 /*
1566 * If we want to write to the child process' standard input,
1567 * create the pipe(2) now.
1568 */
1569 if (pstdin_fd) {
1570 if (pipe(stdin_fds))
1571 return -EFAULT;
1572 }
1573
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001574 /*
1575 * If we want to read from the child process' standard output,
1576 * create the pipe(2) now.
1577 */
1578 if (pstdout_fd) {
1579 if (pipe(stdout_fds))
1580 return -EFAULT;
1581 }
1582
1583 /*
1584 * If we want to read from the child process' standard error,
1585 * create the pipe(2) now.
1586 */
1587 if (pstderr_fd) {
1588 if (pipe(stderr_fds))
1589 return -EFAULT;
1590 }
1591
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001592 /*
1593 * If we want to set up a new uid/gid mapping in the user namespace,
1594 * create the pipe(2) to sync between parent and child.
1595 */
1596 if (j->flags.userns) {
1597 if (pipe(userns_pipe_fds))
1598 return -EFAULT;
1599 }
1600
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001601 /*
1602 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001603 *
1604 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1605 *
1606 * In multithreaded programs, there are a bunch of locks inside libc,
1607 * some of which may be held by other threads at the time that we call
1608 * minijail_run_pid(). If we call fork(), glibc does its level best to
1609 * ensure that we hold all of these locks before it calls clone()
1610 * internally and drop them after clone() returns, but when we call
1611 * sys_clone(2) directly, all that gets bypassed and we end up with a
1612 * child address space where some of libc's important locks are held by
1613 * other threads (which did not get cloned, and hence will never release
1614 * those locks). This is okay so long as we call exec() immediately
1615 * after, but a bunch of seemingly-innocent libc functions like setenv()
1616 * take locks.
1617 *
1618 * Hence, only call sys_clone() if we need to, in order to get at pid
1619 * namespacing. If we follow this path, the child's address space might
1620 * have broken locks; you may only call functions that do not acquire
1621 * any locks.
1622 *
1623 * Unfortunately, fork() acquires every lock it can get its hands on, as
1624 * previously detailed, so this function is highly likely to deadlock
1625 * later on (see "deadlock here") if we're multithreaded.
1626 *
1627 * We might hack around this by having the clone()d child (init of the
1628 * pid namespace) return directly, rather than leaving the clone()d
1629 * process hanging around to be init for the new namespace (and having
1630 * its fork()ed child return in turn), but that process would be crippled
1631 * with its libc locks potentially broken. We might try fork()ing in the
1632 * parent before we clone() to ensure that we own all the locks, but
1633 * then we have to have the forked child hanging around consuming
1634 * resources (and possibly having file descriptors / shared memory
1635 * regions / etc attached). We'd need to keep the child around to avoid
1636 * having its children get reparented to init.
1637 *
1638 * TODO(ellyjones): figure out if the "forked child hanging around"
1639 * problem is fixable or not. It would be nice if we worked in this
1640 * case.
1641 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001642 if (pid_namespace) {
1643 int clone_flags = CLONE_NEWPID | SIGCHLD;
1644 if (j->flags.userns)
1645 clone_flags |= CLONE_NEWUSER;
1646 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001647 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001648 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001649 }
Elly Jones761b7412012-06-13 15:49:52 -04001650
Elly Jonese1749eb2011-10-07 13:54:59 -04001651 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001652 if (use_preload) {
1653 free(oldenv_copy);
1654 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001655 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001656 }
Will Drewryf89aef52011-09-16 16:48:57 -05001657
Elly Jonese1749eb2011-10-07 13:54:59 -04001658 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001659 if (use_preload) {
1660 /* Restore parent's LD_PRELOAD. */
1661 if (oldenv_copy) {
1662 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1663 free(oldenv_copy);
1664 } else {
1665 unsetenv(kLdPreloadEnvVar);
1666 }
1667 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001668 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001669
Elly Jonese1749eb2011-10-07 13:54:59 -04001670 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001671
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001672 if (j->flags.pid_file)
1673 write_pid_file(j);
1674
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001675 if (j->flags.userns)
1676 write_ugid_mappings(j, userns_pipe_fds);
1677
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001678 if (use_preload) {
1679 /* Send marshalled minijail. */
1680 close(pipe_fds[0]); /* read endpoint */
1681 ret = minijail_to_fd(j, pipe_fds[1]);
1682 close(pipe_fds[1]); /* write endpoint */
1683 if (ret) {
1684 kill(j->initpid, SIGKILL);
1685 die("failed to send marshalled minijail");
1686 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001687 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001688
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001689 if (pchild_pid)
1690 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001691
1692 /*
1693 * If we want to write to the child process' standard input,
1694 * set up the write end of the pipe.
1695 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001696 if (pstdin_fd)
1697 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001698 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001699
1700 /*
1701 * If we want to read from the child process' standard output,
1702 * set up the read end of the pipe.
1703 */
1704 if (pstdout_fd)
1705 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001706 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001707
1708 /*
1709 * If we want to read from the child process' standard error,
1710 * set up the read end of the pipe.
1711 */
1712 if (pstderr_fd)
1713 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001714 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001715
Elly Jonese1749eb2011-10-07 13:54:59 -04001716 return 0;
1717 }
1718 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001719
Peter Qiu2860c462015-12-16 15:13:06 -08001720 if (j->flags.reset_signal_mask) {
1721 sigset_t signal_mask;
1722 if (sigemptyset(&signal_mask) != 0)
1723 pdie("sigemptyset failed");
1724 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1725 pdie("sigprocmask failed");
1726 }
1727
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001728 if (j->flags.userns)
1729 enter_user_namespace(j, userns_pipe_fds);
1730
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001731 /*
1732 * If we want to write to the jailed process' standard input,
1733 * set up the read end of the pipe.
1734 */
1735 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001736 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1737 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001738 die("failed to set up stdin pipe");
1739 }
1740
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001741 /*
1742 * If we want to read from the jailed process' standard output,
1743 * set up the write end of the pipe.
1744 */
1745 if (pstdout_fd) {
1746 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1747 STDOUT_FILENO) < 0)
1748 die("failed to set up stdout pipe");
1749 }
1750
1751 /*
1752 * If we want to read from the jailed process' standard error,
1753 * set up the write end of the pipe.
1754 */
1755 if (pstderr_fd) {
1756 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1757 STDERR_FILENO) < 0)
1758 die("failed to set up stderr pipe");
1759 }
1760
Dylan Reid791f5772015-09-14 20:02:42 -07001761 /* If running an init program, let it decide when/how to mount /proc. */
1762 if (pid_namespace && !do_init)
1763 j->flags.remount_proc_ro = 0;
1764
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001765 if (use_preload) {
1766 /* Strip out flags that cannot be inherited across execve(2). */
1767 minijail_preexec(j);
1768 } else {
1769 j->flags.pids = 0;
1770 }
1771 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001772 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001773
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001774 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001775 /*
1776 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001777 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001778 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001779 * a child to actually run the program. If |do_init == 0|, we
1780 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001781 *
1782 * If we're multithreaded, we'll probably deadlock here. See
1783 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001784 */
1785 child_pid = fork();
1786 if (child_pid < 0)
1787 _exit(child_pid);
1788 else if (child_pid > 0)
1789 init(child_pid); /* never returns */
1790 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001791
Elly Jonesdd3e8512012-01-23 15:13:38 -05001792 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001793 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001794 * calling process
1795 * -> execve()-ing process
1796 * If we are:
1797 * calling process
1798 * -> init()-ing process
1799 * -> execve()-ing process
1800 */
1801 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001802}
1803
Will Drewry6ac91122011-10-21 16:38:58 -05001804int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001805{
1806 int st;
1807 if (kill(j->initpid, SIGTERM))
1808 return -errno;
1809 if (waitpid(j->initpid, &st, 0) < 0)
1810 return -errno;
1811 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001812}
1813
Will Drewry6ac91122011-10-21 16:38:58 -05001814int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001815{
1816 int st;
1817 if (waitpid(j->initpid, &st, 0) < 0)
1818 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001819
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001820 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001821 int error_status = st;
1822 if (WIFSIGNALED(st)) {
1823 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001824 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001825 j->initpid, signum);
1826 /*
1827 * We return MINIJAIL_ERR_JAIL if the process received
1828 * SIGSYS, which happens when a syscall is blocked by
1829 * seccomp filters.
1830 * If not, we do what bash(1) does:
1831 * $? = 128 + signum
1832 */
1833 if (signum == SIGSYS) {
1834 error_status = MINIJAIL_ERR_JAIL;
1835 } else {
1836 error_status = 128 + signum;
1837 }
1838 }
1839 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001840 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001841
1842 int exit_status = WEXITSTATUS(st);
1843 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001844 info("child process %d exited with status %d",
1845 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001846
1847 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001848}
1849
Will Drewry6ac91122011-10-21 16:38:58 -05001850void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001851{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001852 if (j->flags.seccomp_filter && j->filter_prog) {
1853 free(j->filter_prog->filter);
1854 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001855 }
Dylan Reid648b2202015-10-23 00:50:00 -07001856 while (j->mounts_head) {
1857 struct mountpoint *m = j->mounts_head;
1858 j->mounts_head = j->mounts_head->next;
1859 free(m->type);
1860 free(m->dest);
1861 free(m->src);
1862 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001863 }
Dylan Reid648b2202015-10-23 00:50:00 -07001864 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001865 if (j->user)
1866 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08001867 if (j->suppl_gid_list)
1868 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05001869 if (j->chrootdir)
1870 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08001871 if (j->alt_syscall_table)
1872 free(j->alt_syscall_table);
Elly Jonese1749eb2011-10-07 13:54:59 -04001873 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001874}