blob: fc3368a99ac8e7fbe74c9ca8c64b9d526c1a497a [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>
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -080034#include <sys/utsname.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040036#include <unistd.h>
37
38#include "libminijail.h"
39#include "libminijail-private.h"
40
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070041#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044
Lei Zhangeee31552012-10-17 21:27:10 -070045#ifdef HAVE_SECUREBITS_H
46#include <linux/securebits.h>
47#else
48#define SECURE_ALL_BITS 0x15
49#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
50#endif
51
Will Drewry32ac9f52011-08-18 21:36:27 -050052/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080053#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070054# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080055#endif
56
Andrew Brestickereac28942015-11-11 16:04:46 -080057#ifndef PR_ALT_SYSCALL
58# define PR_ALT_SYSCALL 0x43724f53
59#endif
60
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080061/* For seccomp_filter using BPF. */
62#ifndef PR_SET_NO_NEW_PRIVS
63# define PR_SET_NO_NEW_PRIVS 38
64#endif
65#ifndef SECCOMP_MODE_FILTER
66# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050067#endif
68
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070069#ifdef USE_SECCOMP_SOFTFAIL
70# define SECCOMP_SOFTFAIL 1
71#else
72# define SECCOMP_SOFTFAIL 0
73#endif
74
Dylan Reid605ce7f2016-01-19 19:21:00 -080075#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
76
Dylan Reid648b2202015-10-23 00:50:00 -070077struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040078 char *src;
79 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070080 char *type;
81 unsigned long flags;
82 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040083};
84
Will Drewryf89aef52011-09-16 16:48:57 -050085struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070086 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070087 * WARNING: if you add a flag here you need to make sure it's
88 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070089 */
Elly Jonese1749eb2011-10-07 13:54:59 -040090 struct {
91 int uid:1;
92 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -080093 int usergroups:1;
94 int suppl_gids:1;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -080095 int use_caps:1;
96 int capbset_drop:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040097 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070098 int enter_vfs:1;
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -080099 int skip_remount_private:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400100 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -0800101 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400102 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700103 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800104 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400105 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700106 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700107 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400108 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700109 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400110 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800111 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700112 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800113 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800114 int pid_file:1;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800115 int cgroups:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800116 int alt_syscall:1;
Peter Qiu2860c462015-12-16 15:13:06 -0800117 int reset_signal_mask:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400118 } flags;
119 uid_t uid;
120 gid_t gid;
121 gid_t usergid;
122 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800123 size_t suppl_gid_count;
124 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400125 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800126 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400127 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700128 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700129 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400130 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800131 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800132 char *uidmap;
133 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800134 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800135 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800136 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700137 struct mountpoint *mounts_head;
138 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800139 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800140 char *cgroups[MAX_CGROUPS];
141 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500142};
143
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700144/*
145 * Strip out flags meant for the parent.
146 * We keep things that are not inherited across execve(2) (e.g. capabilities),
147 * or are easier to set after execve(2) (e.g. seccomp filters).
148 */
149void minijail_preenter(struct minijail *j)
150{
151 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700152 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900153 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700154 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700155 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800156 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800157 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800158 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700159}
160
161/*
162 * Strip out flags meant for the child.
163 * We keep things that are inherited across execve(2).
164 */
165void minijail_preexec(struct minijail *j)
166{
167 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700168 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800169 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700170 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800171 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700172 if (j->user)
173 free(j->user);
174 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800175 if (j->suppl_gid_list)
176 free(j->suppl_gid_list);
177 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700178 memset(&j->flags, 0, sizeof(j->flags));
179 /* Now restore anything we meant to keep. */
180 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700181 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800182 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700183 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800184 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700185 /* Note, |pids| will already have been used before this call. */
186}
187
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800188/* Returns true if the kernel version is less than 3.8. */
189int seccomp_kernel_support_not_required()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800190{
191 int major, minor;
192 struct utsname uts;
193 return (uname(&uts) != -1 &&
194 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
195 ((major < 3) || ((major == 3) && (minor < 8))));
196}
197
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800198/* Allow seccomp soft-fail on Android devices with kernel version < 3.8. */
199int can_softfail()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800200{
201#if SECCOMP_SOFTFAIL
202 if (is_android()) {
203 if (seccomp_kernel_support_not_required())
204 return 1;
205 else
206 return 0;
207 } else {
208 return 1;
209 }
210#endif
211 return 0;
212}
213
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700214/* Minijail API. */
215
Will Drewry6ac91122011-10-21 16:38:58 -0500216struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400217{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400218 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400219}
220
Will Drewry6ac91122011-10-21 16:38:58 -0500221void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400222{
223 if (uid == 0)
224 die("useless change to uid 0");
225 j->uid = uid;
226 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400227}
228
Will Drewry6ac91122011-10-21 16:38:58 -0500229void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400230{
231 if (gid == 0)
232 die("useless change to gid 0");
233 j->gid = gid;
234 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400235}
236
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800237void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
238 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800239{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800240 size_t i;
241
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800242 if (j->flags.usergroups)
243 die("cannot inherit *and* set supplementary groups");
244
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800245 if (size == 0) {
246 /* Clear supplementary groups. */
247 j->suppl_gid_list = NULL;
248 j->suppl_gid_count = 0;
249 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800250 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800251 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800252
253 /* Copy the gid_t array. */
254 j->suppl_gid_list = calloc(size, sizeof(gid_t));
255 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800256 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800257 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800258 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800259 j->suppl_gid_list[i] = list[i];
260 }
261 j->suppl_gid_count = size;
262 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800263}
264
Will Drewry6ac91122011-10-21 16:38:58 -0500265int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400266{
267 char *buf = NULL;
268 struct passwd pw;
269 struct passwd *ppw = NULL;
270 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
271 if (sz == -1)
272 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400273
Elly Jonesdd3e8512012-01-23 15:13:38 -0500274 /*
275 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400276 * the maximum needed size of the buffer, so we don't have to search.
277 */
278 buf = malloc(sz);
279 if (!buf)
280 return -ENOMEM;
281 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500282 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800283 * We're safe to free the buffer here. The strings inside |pw| point
284 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800285 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
286 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500287 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400288 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700289 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400290 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700291 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400292 minijail_change_uid(j, ppw->pw_uid);
293 j->user = strdup(user);
294 if (!j->user)
295 return -ENOMEM;
296 j->usergid = ppw->pw_gid;
297 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400298}
299
Will Drewry6ac91122011-10-21 16:38:58 -0500300int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400301{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700302 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700303 struct group gr;
304 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400305 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
306 if (sz == -1)
307 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400308
Elly Jonesdd3e8512012-01-23 15:13:38 -0500309 /*
310 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400311 * the maximum needed size of the buffer, so we don't have to search.
312 */
313 buf = malloc(sz);
314 if (!buf)
315 return -ENOMEM;
316 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500317 /*
318 * We're safe to free the buffer here. The strings inside gr point
319 * inside buf, but we don't use any of them; this leaves the pointers
320 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
321 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400322 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700323 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400324 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700325 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400326 minijail_change_gid(j, pgr->gr_gid);
327 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400328}
329
Will Drewry6ac91122011-10-21 16:38:58 -0500330void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400331{
332 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400333}
334
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700335void API minijail_no_new_privs(struct minijail *j)
336{
337 j->flags.no_new_privs = 1;
338}
339
Will Drewry6ac91122011-10-21 16:38:58 -0500340void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400341{
342 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500343}
344
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700345void API minijail_log_seccomp_filter_failures(struct minijail *j)
346{
347 j->flags.log_seccomp_filter = 1;
348}
349
Will Drewry6ac91122011-10-21 16:38:58 -0500350void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400351{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800352 /*
353 * 'minijail_use_caps' configures a runtime-capabilities-only
354 * environment, including a bounding set matching the thread's runtime
355 * (permitted|inheritable|effective) sets.
356 * Therefore, it will override any existing bounding set configurations
357 * since the latter would allow gaining extra runtime capabilities from
358 * file capabilities.
359 */
360 if (j->flags.capbset_drop) {
361 warn("overriding bounding set configuration");
362 j->cap_bset = 0;
363 j->flags.capbset_drop = 0;
364 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400365 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800366 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400367}
368
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800369void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
370{
371 if (j->flags.use_caps) {
372 /*
373 * 'minijail_use_caps' will have already configured a capability
374 * bounding set matching the (permitted|inheritable|effective)
375 * sets. Abort if the user tries to configure a separate
376 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
377 * are mutually exclusive.
378 */
379 die("runtime capabilities already configured, can't drop "
380 "bounding set separately");
381 }
382 j->cap_bset = capmask;
383 j->flags.capbset_drop = 1;
384}
385
386void API minijail_reset_signal_mask(struct minijail *j)
387{
Peter Qiu2860c462015-12-16 15:13:06 -0800388 j->flags.reset_signal_mask = 1;
389}
390
Will Drewry6ac91122011-10-21 16:38:58 -0500391void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400392{
393 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400394}
395
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700396void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
397{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800398 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700399 if (ns_fd < 0) {
400 pdie("failed to open namespace '%s'", ns_path);
401 }
402 j->mountns_fd = ns_fd;
403 j->flags.enter_vfs = 1;
404}
405
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800406void API minijail_skip_remount_private(struct minijail *j)
407{
408 j->flags.skip_remount_private = 1;
409}
410
Will Drewry6ac91122011-10-21 16:38:58 -0500411void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400412{
Elly Jonese58176c2012-01-23 11:46:17 -0500413 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700414 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400415 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800416 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400417}
418
Dylan Reidf7942472015-11-18 17:55:26 -0800419void API minijail_namespace_ipc(struct minijail *j)
420{
421 j->flags.ipc = 1;
422}
423
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400424void API minijail_namespace_net(struct minijail *j)
425{
426 j->flags.net = 1;
427}
428
Dylan Reid1102f5a2015-09-15 11:52:20 -0700429void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
430{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800431 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700432 if (ns_fd < 0) {
433 pdie("failed to open namespace '%s'", ns_path);
434 }
435 j->netns_fd = ns_fd;
436 j->flags.enter_net = 1;
437}
438
Dylan Reid791f5772015-09-14 20:02:42 -0700439void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400440{
441 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700442 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400443}
444
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800445void API minijail_namespace_user(struct minijail *j)
446{
447 j->flags.userns = 1;
448}
449
450int API minijail_uidmap(struct minijail *j, const char *uidmap)
451{
452 j->uidmap = strdup(uidmap);
453 if (!j->uidmap)
454 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800455 char *ch;
456 for (ch = j->uidmap; *ch; ch++) {
457 if (*ch == ',')
458 *ch = '\n';
459 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800460 return 0;
461}
462
463int API minijail_gidmap(struct minijail *j, const char *gidmap)
464{
465 j->gidmap = strdup(gidmap);
466 if (!j->gidmap)
467 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800468 char *ch;
469 for (ch = j->gidmap; *ch; ch++) {
470 if (*ch == ',')
471 *ch = '\n';
472 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800473 return 0;
474}
475
Will Drewry6ac91122011-10-21 16:38:58 -0500476void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400477{
478 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400479}
480
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800481void API minijail_run_as_init(struct minijail *j)
482{
483 /*
484 * Since the jailed program will become 'init' in the new PID namespace,
485 * Minijail does not need to fork an 'init' process.
486 */
487 j->flags.do_init = 0;
488}
489
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700490int API minijail_enter_chroot(struct minijail *j, const char *dir)
491{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400492 if (j->chrootdir)
493 return -EINVAL;
494 j->chrootdir = strdup(dir);
495 if (!j->chrootdir)
496 return -ENOMEM;
497 j->flags.chroot = 1;
498 return 0;
499}
500
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800501int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
502{
503 if (j->chrootdir)
504 return -EINVAL;
505 j->chrootdir = strdup(dir);
506 if (!j->chrootdir)
507 return -ENOMEM;
508 j->flags.pivot_root = 1;
509 return 0;
510}
511
Dylan Reida14e08d2015-10-22 21:05:29 -0700512static char *append_external_path(const char *external_path,
513 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700514{
Dylan Reida14e08d2015-10-22 21:05:29 -0700515 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700516 size_t pathlen;
517
Dylan Reid08946cc2015-09-16 19:10:57 -0700518 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700519 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
520 path = malloc(pathlen);
521 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700522
Dylan Reida14e08d2015-10-22 21:05:29 -0700523 return path;
524}
525
526char API *minijail_get_original_path(struct minijail *j,
527 const char *path_inside_chroot)
528{
Dylan Reid648b2202015-10-23 00:50:00 -0700529 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700530
Dylan Reid648b2202015-10-23 00:50:00 -0700531 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700532 while (b) {
533 /*
534 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700535 * mount, then the original path is exactly the source of
536 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700537 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700538 * mount source = /some/path/exe, mount dest =
539 * /chroot/path/exe Then when getting the original path of
540 * "/chroot/path/exe", the source of that mount,
541 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700542 */
543 if (!strcmp(b->dest, path_inside_chroot))
544 return strdup(b->src);
545
546 /*
547 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700548 * mount, take the suffix of the chroot path relative to the
549 * mount destination path, and append it to the mount source
550 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700551 */
552 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
553 const char *relative_path =
554 path_inside_chroot + strlen(b->dest);
555 return append_external_path(b->src, relative_path);
556 }
557 b = b->next;
558 }
559
560 /* If there is a chroot path, append |path_inside_chroot| to that. */
561 if (j->chrootdir)
562 return append_external_path(j->chrootdir, path_inside_chroot);
563
564 /* No chroot, so the path outside is the same as it is inside. */
565 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700566}
567
Lee Campbell11af0622014-05-22 12:36:04 -0700568void API minijail_mount_tmp(struct minijail *j)
569{
570 j->flags.mount_tmp = 1;
571}
572
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800573int API minijail_write_pid_file(struct minijail *j, const char *path)
574{
575 j->pid_file_path = strdup(path);
576 if (!j->pid_file_path)
577 return -ENOMEM;
578 j->flags.pid_file = 1;
579 return 0;
580}
581
Dylan Reid605ce7f2016-01-19 19:21:00 -0800582int API minijail_add_to_cgroup(struct minijail *j, const char *path)
583{
584 if (j->cgroup_count >= MAX_CGROUPS)
585 return -ENOMEM;
586 j->cgroups[j->cgroup_count] = strdup(path);
587 if (!j->cgroups[j->cgroup_count])
588 return -ENOMEM;
589 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800590 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800591 return 0;
592}
593
Dylan Reid648b2202015-10-23 00:50:00 -0700594int API minijail_mount(struct minijail *j, const char *src, const char *dest,
595 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700596{
Dylan Reid648b2202015-10-23 00:50:00 -0700597 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400598
599 if (*dest != '/')
600 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700601 m = calloc(1, sizeof(*m));
602 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400603 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700604 m->dest = strdup(dest);
605 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400606 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700607 m->src = strdup(src);
608 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400609 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700610 m->type = strdup(type);
611 if (!m->type)
612 goto error;
613 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400614
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800615 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400616
Elly Jonesdd3e8512012-01-23 15:13:38 -0500617 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700618 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400619 * containing vfs namespace.
620 */
621 minijail_namespace_vfs(j);
622
Dylan Reid648b2202015-10-23 00:50:00 -0700623 if (j->mounts_tail)
624 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400625 else
Dylan Reid648b2202015-10-23 00:50:00 -0700626 j->mounts_head = m;
627 j->mounts_tail = m;
628 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400629
630 return 0;
631
632error:
Dylan Reid648b2202015-10-23 00:50:00 -0700633 free(m->src);
634 free(m->dest);
635 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400636 return -ENOMEM;
637}
638
Dylan Reid648b2202015-10-23 00:50:00 -0700639int API minijail_bind(struct minijail *j, const char *src, const char *dest,
640 int writeable)
641{
642 unsigned long flags = MS_BIND;
643
644 if (!writeable)
645 flags |= MS_RDONLY;
646
647 return minijail_mount(j, src, dest, "", flags);
648}
649
Will Drewry6ac91122011-10-21 16:38:58 -0500650void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400651{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700652 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800653 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800654 warn("not loading seccomp filter,"
655 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800656 j->flags.seccomp_filter = 0;
657 j->flags.log_seccomp_filter = 0;
658 j->filter_len = 0;
659 j->filter_prog = NULL;
660 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700661 }
662 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400663 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800664 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700665 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400666 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800667
668 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700669 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
670 die("failed to compile seccomp filter BPF program in '%s'",
671 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800672 }
673
674 j->filter_len = fprog->len;
675 j->filter_prog = fprog;
676
Elly Jonese1749eb2011-10-07 13:54:59 -0400677 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500678}
679
Andrew Brestickereac28942015-11-11 16:04:46 -0800680int API minijail_use_alt_syscall(struct minijail *j, const char *table)
681{
682 j->alt_syscall_table = strdup(table);
683 if (!j->alt_syscall_table)
684 return -ENOMEM;
685 j->flags.alt_syscall = 1;
686 return 0;
687}
688
Will Drewryf89aef52011-09-16 16:48:57 -0500689struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400690 size_t available;
691 size_t total;
692 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500693};
694
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800695void marshal_state_init(struct marshal_state *state, char *buf,
696 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400697{
698 state->available = available;
699 state->buf = buf;
700 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500701}
702
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800703void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400704{
705 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500706
Elly Jonese1749eb2011-10-07 13:54:59 -0400707 /* Up to |available| will be written. */
708 if (copy_len) {
709 memcpy(state->buf, src, copy_len);
710 state->buf += copy_len;
711 state->available -= copy_len;
712 }
713 /* |total| will contain the expected length. */
714 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500715}
716
Will Drewry6ac91122011-10-21 16:38:58 -0500717void minijail_marshal_helper(struct marshal_state *state,
718 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400719{
Dylan Reid648b2202015-10-23 00:50:00 -0700720 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800721 size_t i;
722
Elly Jonese1749eb2011-10-07 13:54:59 -0400723 marshal_append(state, (char *)j, sizeof(*j));
724 if (j->user)
725 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800726 if (j->suppl_gid_list) {
727 marshal_append(state, j->suppl_gid_list,
728 j->suppl_gid_count * sizeof(gid_t));
729 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400730 if (j->chrootdir)
731 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800732 if (j->alt_syscall_table) {
733 marshal_append(state, j->alt_syscall_table,
734 strlen(j->alt_syscall_table) + 1);
735 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800736 if (j->flags.seccomp_filter && j->filter_prog) {
737 struct sock_fprog *fp = j->filter_prog;
738 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800739 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400740 }
Dylan Reid648b2202015-10-23 00:50:00 -0700741 for (m = j->mounts_head; m; m = m->next) {
742 marshal_append(state, m->src, strlen(m->src) + 1);
743 marshal_append(state, m->dest, strlen(m->dest) + 1);
744 marshal_append(state, m->type, strlen(m->type) + 1);
745 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400746 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800747 for (i = 0; i < j->cgroup_count; ++i)
748 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500749}
750
Will Drewry6ac91122011-10-21 16:38:58 -0500751size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400752{
753 struct marshal_state state;
754 marshal_state_init(&state, NULL, 0);
755 minijail_marshal_helper(&state, j);
756 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500757}
758
Elly Jonese1749eb2011-10-07 13:54:59 -0400759int minijail_marshal(const struct minijail *j, char *buf, size_t available)
760{
761 struct marshal_state state;
762 marshal_state_init(&state, buf, available);
763 minijail_marshal_helper(&state, j);
764 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500765}
766
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800767/*
768 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400769 * @length Number of bytes to consume
770 * @buf Buffer to consume from
771 * @buflength Size of @buf
772 *
773 * Returns a pointer to the base of the bytes, or NULL for errors.
774 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700775void *consumebytes(size_t length, char **buf, size_t *buflength)
776{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400777 char *p = *buf;
778 if (length > *buflength)
779 return NULL;
780 *buf += length;
781 *buflength -= length;
782 return p;
783}
784
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800785/*
786 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400787 * @buf Buffer to consume
788 * @length Length of buffer
789 *
790 * Returns a pointer to the base of the string, or NULL for errors.
791 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700792char *consumestr(char **buf, size_t *buflength)
793{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400794 size_t len = strnlen(*buf, *buflength);
795 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700796 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400797 return NULL;
798 return consumebytes(len + 1, buf, buflength);
799}
800
Elly Jonese1749eb2011-10-07 13:54:59 -0400801int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
802{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800803 size_t i;
804 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500805 int ret = -EINVAL;
806
Elly Jonese1749eb2011-10-07 13:54:59 -0400807 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500808 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400809 memcpy((void *)j, serialized, sizeof(*j));
810 serialized += sizeof(*j);
811 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500812
Will Drewrybee7ba72011-10-21 20:47:01 -0500813 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700814 j->mounts_head = NULL;
815 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800816 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500817
Elly Jonese1749eb2011-10-07 13:54:59 -0400818 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400819 char *user = consumestr(&serialized, &length);
820 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500821 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400822 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500823 if (!j->user)
824 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400825 }
Will Drewryf89aef52011-09-16 16:48:57 -0500826
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800827 if (j->suppl_gid_list) { /* stale pointer */
828 if (j->suppl_gid_count > NGROUPS_MAX) {
829 goto bad_gid_list;
830 }
831 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
832 void *gid_list_bytes =
833 consumebytes(gid_list_size, &serialized, &length);
834 if (!gid_list_bytes)
835 goto bad_gid_list;
836
837 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
838 if (!j->suppl_gid_list)
839 goto bad_gid_list;
840
841 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
842 }
843
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400844 if (j->chrootdir) { /* stale pointer */
845 char *chrootdir = consumestr(&serialized, &length);
846 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500847 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400848 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500849 if (!j->chrootdir)
850 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400851 }
852
Andrew Brestickereac28942015-11-11 16:04:46 -0800853 if (j->alt_syscall_table) { /* stale pointer */
854 char *alt_syscall_table = consumestr(&serialized, &length);
855 if (!alt_syscall_table)
856 goto bad_syscall_table;
857 j->alt_syscall_table = strdup(alt_syscall_table);
858 if (!j->alt_syscall_table)
859 goto bad_syscall_table;
860 }
861
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800862 if (j->flags.seccomp_filter && j->filter_len > 0) {
863 size_t ninstrs = j->filter_len;
864 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
865 ninstrs > USHRT_MAX)
866 goto bad_filters;
867
868 size_t program_len = ninstrs * sizeof(struct sock_filter);
869 void *program = consumebytes(program_len, &serialized, &length);
870 if (!program)
871 goto bad_filters;
872
873 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800874 if (!j->filter_prog)
875 goto bad_filters;
876
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800877 j->filter_prog->len = ninstrs;
878 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800879 if (!j->filter_prog->filter)
880 goto bad_filter_prog_instrs;
881
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800882 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400883 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400884
Dylan Reid648b2202015-10-23 00:50:00 -0700885 count = j->mounts_count;
886 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400887 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700888 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400889 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700890 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400891 const char *src = consumestr(&serialized, &length);
892 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700893 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400894 dest = consumestr(&serialized, &length);
895 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700896 goto bad_mounts;
897 type = consumestr(&serialized, &length);
898 if (!type)
899 goto bad_mounts;
900 flags = consumebytes(sizeof(*flags), &serialized, &length);
901 if (!flags)
902 goto bad_mounts;
903 if (minijail_mount(j, src, dest, type, *flags))
904 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400905 }
906
Dylan Reid605ce7f2016-01-19 19:21:00 -0800907 count = j->cgroup_count;
908 j->cgroup_count = 0;
909 for (i = 0; i < count; ++i) {
910 char *cgroup = consumestr(&serialized, &length);
911 if (!cgroup)
912 goto bad_cgroups;
913 j->cgroups[i] = strdup(cgroup);
914 if (!j->cgroups[i])
915 goto bad_cgroups;
916 ++j->cgroup_count;
917 }
918
Elly Jonese1749eb2011-10-07 13:54:59 -0400919 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500920
Dylan Reid605ce7f2016-01-19 19:21:00 -0800921bad_cgroups:
922 while (j->mounts_head) {
923 struct mountpoint *m = j->mounts_head;
924 j->mounts_head = j->mounts_head->next;
925 free(m->type);
926 free(m->dest);
927 free(m->src);
928 free(m);
929 }
930 for (i = 0; i < j->cgroup_count; ++i)
931 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700932bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800933 if (j->flags.seccomp_filter && j->filter_len > 0) {
934 free(j->filter_prog->filter);
935 free(j->filter_prog);
936 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800937bad_filter_prog_instrs:
938 if (j->filter_prog)
939 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500940bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800941 if (j->alt_syscall_table)
942 free(j->alt_syscall_table);
943bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500944 if (j->chrootdir)
945 free(j->chrootdir);
946bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800947 if (j->suppl_gid_list)
948 free(j->suppl_gid_list);
949bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500950 if (j->user)
951 free(j->user);
952clear_pointers:
953 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800954 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500955 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800956 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800957 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500958out:
959 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500960}
961
Dylan Reidce5b55e2016-01-13 11:04:16 -0800962static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800963{
964 int fd, ret, len;
965 size_t sz;
966 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800967
968 sz = sizeof(fname);
969 if (j->uidmap) {
970 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700971 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800972 die("failed to write file name of uid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800973 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800974 if (fd < 0)
975 pdie("failed to open '%s'", fname);
976 len = strlen(j->uidmap);
977 if (write(fd, j->uidmap, len) < len)
978 die("failed to set uid_map");
979 close(fd);
980 }
981 if (j->gidmap) {
982 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700983 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800984 die("failed to write file name of gid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800985 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800986 if (fd < 0)
987 pdie("failed to open '%s'", fname);
988 len = strlen(j->gidmap);
989 if (write(fd, j->gidmap, len) < len)
990 die("failed to set gid_map");
991 close(fd);
992 }
Dylan Reidce5b55e2016-01-13 11:04:16 -0800993}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800994
Dylan Reidce5b55e2016-01-13 11:04:16 -0800995static void parent_setup_complete(int *pipe_fds)
996{
997 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800998 close(pipe_fds[1]);
999}
1000
Dylan Reidce5b55e2016-01-13 11:04:16 -08001001/*
1002 * wait_for_parent_setup: Called by the child process to wait for any
1003 * further parent-side setup to complete before continuing.
1004 */
1005static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001006{
1007 char buf;
1008
1009 close(pipe_fds[1]);
1010
Dylan Reidce5b55e2016-01-13 11:04:16 -08001011 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001012 if (read(pipe_fds[0], &buf, 1) != 0)
1013 die("failed to sync with parent");
1014 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001015}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001016
Dylan Reidce5b55e2016-01-13 11:04:16 -08001017static void enter_user_namespace(const struct minijail *j)
1018{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001019 if (j->uidmap && setresuid(0, 0, 0))
1020 pdie("setresuid");
1021 if (j->gidmap && setresgid(0, 0, 0))
1022 pdie("setresgid");
1023}
1024
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001025/*
1026 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001027 * @j Minijail these mounts are for
1028 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001029 *
1030 * Returns 0 for success.
1031 */
Dylan Reid648b2202015-10-23 00:50:00 -07001032static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001033{
Dylan Reid648b2202015-10-23 00:50:00 -07001034 int ret;
1035 char *dest;
1036 int remount_ro = 0;
1037
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001038 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001039 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001040 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001041
1042 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001043 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1044 * can't both be specified in the original bind mount.
1045 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001046 */
1047 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1048 remount_ro = 1;
1049 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001050 }
Dylan Reid648b2202015-10-23 00:50:00 -07001051
1052 ret = mount(m->src, dest, m->type, m->flags, NULL);
1053 if (ret)
1054 pdie("mount: %s -> %s", m->src, dest);
1055
1056 if (remount_ro) {
1057 m->flags |= MS_RDONLY;
1058 ret = mount(m->src, dest, NULL,
1059 m->flags | MS_REMOUNT, NULL);
1060 if (ret)
1061 pdie("bind ro: %s -> %s", m->src, dest);
1062 }
1063
Elly Jones51a5b6c2011-10-12 19:09:26 -04001064 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001065 if (m->next)
1066 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001067 return ret;
1068}
1069
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001070int enter_chroot(const struct minijail *j)
1071{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001072 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001073
1074 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001075 return ret;
1076
1077 if (chroot(j->chrootdir))
1078 return -errno;
1079
1080 if (chdir("/"))
1081 return -errno;
1082
1083 return 0;
1084}
1085
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001086int enter_pivot_root(const struct minijail *j)
1087{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001088 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001089
1090 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001091 return ret;
1092
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001093 /*
1094 * Keep the fd for both old and new root.
1095 * It will be used in fchdir later.
1096 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001097 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001098 if (oldroot < 0)
1099 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001100 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001101 if (newroot < 0)
1102 pdie("failed to open %s for fchdir", j->chrootdir);
1103
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001104 /*
1105 * To ensure chrootdir is the root of a file system,
1106 * do a self bind mount.
1107 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001108 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1109 pdie("failed to bind mount '%s'", j->chrootdir);
1110 if (chdir(j->chrootdir))
1111 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001112 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001113 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001114
1115 /*
1116 * Now the old root is mounted on top of the new root. Use fchdir to
1117 * change to the old root and unmount it.
1118 */
1119 if (fchdir(oldroot))
1120 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001121
1122 /*
1123 * If j->flags.skip_remount_private is enabled, there could be a shared
1124 * mount point under |oldroot|. In such a case, mount points under the
1125 * shared mount point will be unmount(2)'ed below, so that it is
1126 * propagated to the original mount namespace. To prevent such
1127 * unexpected unmounting, remove them from peer groups by recursive
1128 * PRIVATE marking.
1129 */
1130 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
1131 pdie("failed to mount(/, private) for unmount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001132 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001133 if (umount2(".", MNT_DETACH))
1134 pdie("umount(/)");
1135 /* Change back to the new root. */
1136 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001137 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001138 if (close(oldroot))
1139 return -errno;
1140 if (close(newroot))
1141 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001142 if (chroot("/"))
1143 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001144 /* Set correct CWD for getcwd(3). */
1145 if (chdir("/"))
1146 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001147
1148 return 0;
1149}
1150
Lee Campbell11af0622014-05-22 12:36:04 -07001151int mount_tmp(void)
1152{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001153 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001154}
1155
Dylan Reid791f5772015-09-14 20:02:42 -07001156int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001157{
1158 const char *kProcPath = "/proc";
1159 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001160 /*
1161 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001162 * /proc in our namespace, which means using MS_REMOUNT here would
1163 * mutate our parent's mount as well, even though we're in a VFS
1164 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001165 * and make our own. However, if we are in a new user namespace, /proc
1166 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001167 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001168 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001169 return -errno;
1170 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1171 return -errno;
1172 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001173}
1174
Dylan Reid605ce7f2016-01-19 19:21:00 -08001175static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001176{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001177 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001178
1179 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001180 pdie("failed to open '%s'", path);
1181 if (fprintf(fp, "%d\n", (int)pid) < 0)
1182 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001183 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001184 pdie("fclose(%s)", path);
1185}
1186
1187static void write_pid_file(const struct minijail *j)
1188{
1189 write_pid_to_path(j->initpid, j->pid_file_path);
1190}
1191
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001192static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001193{
1194 size_t i;
1195
1196 for (i = 0; i < j->cgroup_count; ++i)
1197 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001198}
1199
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001200void drop_ugid(const struct minijail *j)
1201{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001202 if (j->flags.usergroups && j->flags.suppl_gids) {
1203 die("tried to inherit *and* set supplementary groups;"
1204 " can only do one");
1205 }
1206
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001207 if (j->flags.usergroups) {
1208 if (initgroups(j->user, j->usergid))
1209 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001210 } else if (j->flags.suppl_gids) {
1211 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1212 pdie("setgroups");
1213 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001214 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001215 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001216 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001217 * users.
1218 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001219 if ((j->uid || j->gid) && setgroups(0, NULL))
1220 pdie("setgroups");
1221 }
1222
1223 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1224 pdie("setresgid");
1225
1226 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1227 pdie("setresuid");
1228}
1229
Mike Frysinger3adfef72013-05-09 17:19:08 -04001230/*
1231 * We specifically do not use cap_valid() as that only tells us the last
1232 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001233 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001234 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001235 * kernel).
1236 * Normally, we suck up the answer via /proc. On Android, not all processes are
1237 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1238 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001239 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001240static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001241{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001242 unsigned int last_valid_cap = 0;
1243 if (is_android()) {
1244 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1245 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001246
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001247 /* |last_valid_cap| will be the first failing value. */
1248 if (last_valid_cap > 0) {
1249 last_valid_cap--;
1250 }
1251 } else {
1252 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1253 FILE *fp = fopen(cap_file, "re");
1254 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1255 pdie("fscanf(%s)", cap_file);
1256 fclose(fp);
1257 }
Dylan Reidf682d472015-09-17 21:39:07 -07001258 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001259}
1260
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001261static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1262{
1263 const uint64_t one = 1;
1264 unsigned int i;
1265 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1266 if (keep_mask & (one << i))
1267 continue;
1268 if (prctl(PR_CAPBSET_DROP, i))
1269 pdie("could not drop capability from bounding set");
1270 }
1271}
1272
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001273void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001274{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001275 if (!j->flags.use_caps)
1276 return;
1277
Elly Jonese1749eb2011-10-07 13:54:59 -04001278 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001279 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001280 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001281 unsigned int i;
1282 if (!caps)
1283 die("can't get process caps");
1284 if (cap_clear_flag(caps, CAP_INHERITABLE))
1285 die("can't clear inheritable caps");
1286 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1287 die("can't clear effective caps");
1288 if (cap_clear_flag(caps, CAP_PERMITTED))
1289 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001290 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001291 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001292 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001293 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001294 flag[0] = i;
1295 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001296 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001297 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001298 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001299 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001300 die("can't add inheritable cap");
1301 }
1302 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001303 die("can't apply initial cleaned capset");
1304
1305 /*
1306 * Instead of dropping bounding set first, do it here in case
1307 * the caller had a more permissive bounding set which could
1308 * have been used above to raise a capability that wasn't already
1309 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1310 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001311 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001312
1313 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001314 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001315 flag[0] = CAP_SETPCAP;
1316 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1317 die("can't clear effective cap");
1318 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1319 die("can't clear permitted cap");
1320 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1321 die("can't clear inheritable cap");
1322 }
1323
1324 if (cap_set_proc(caps))
1325 die("can't apply final cleaned capset");
1326
1327 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001328}
1329
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001330void set_seccomp_filter(const struct minijail *j)
1331{
1332 /*
1333 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1334 * in the kernel source tree for an explanation of the parameters.
1335 */
1336 if (j->flags.no_new_privs) {
1337 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1338 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1339 }
1340
1341 /*
1342 * If we're logging seccomp filter failures,
1343 * install the SIGSYS handler first.
1344 */
1345 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1346 if (install_sigsys_handler())
1347 pdie("install SIGSYS handler");
1348 warn("logging seccomp filter failures");
1349 }
1350
1351 /*
1352 * Install the syscall filter.
1353 */
1354 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001355 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1356 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001357 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001358 warn("seccomp not supported");
1359 return;
1360 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001361 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001362 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001363 }
1364}
1365
Will Drewry6ac91122011-10-21 16:38:58 -05001366void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001367{
Dylan Reidf682d472015-09-17 21:39:07 -07001368 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001369 * If we're dropping caps, get the last valid cap from /proc now,
1370 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001371 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001372 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001373 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001374 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001375
Elly Jonese1749eb2011-10-07 13:54:59 -04001376 if (j->flags.pids)
1377 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001378 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001379
Elly Jonese1749eb2011-10-07 13:54:59 -04001380 if (j->flags.usergroups && !j->user)
1381 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001382
Elly Jonesdd3e8512012-01-23 15:13:38 -05001383 /*
1384 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001385 * so we don't even try. If any of our operations fail, we abort() the
1386 * entire process.
1387 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001388 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1389 pdie("setns(CLONE_NEWNS)");
1390
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001391 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001392 if (unshare(CLONE_NEWNS))
1393 pdie("unshare(vfs)");
1394 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001395 * Unless asked not to, remount all filesystems as private.
1396 * If they are shared, new bind mounts will creep out of our
1397 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001398 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1399 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001400 if (!j->flags.skip_remount_private) {
1401 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1402 pdie("mount(/, private)");
1403 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001404 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001405
Dylan Reidf7942472015-11-18 17:55:26 -08001406 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1407 pdie("unshare(ipc)");
1408 }
1409
Dylan Reid1102f5a2015-09-15 11:52:20 -07001410 if (j->flags.enter_net) {
1411 if (setns(j->netns_fd, CLONE_NEWNET))
1412 pdie("setns(CLONE_NEWNET)");
1413 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001414 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001415 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001416
Elly Jones51a5b6c2011-10-12 19:09:26 -04001417 if (j->flags.chroot && enter_chroot(j))
1418 pdie("chroot");
1419
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001420 if (j->flags.pivot_root && enter_pivot_root(j))
1421 pdie("pivot_root");
1422
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001423 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001424 pdie("mount_tmp");
1425
Dylan Reid791f5772015-09-14 20:02:42 -07001426 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001427 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001428
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001429 /*
1430 * If we're only dropping capabilities from the bounding set, but not
1431 * from the thread's (permitted|inheritable|effective) sets, do it now.
1432 */
1433 if (j->flags.capbset_drop) {
1434 drop_capbset(j->cap_bset, last_valid_cap);
1435 }
1436
1437 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001438 /*
1439 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001440 * capability to change uids, our attempt to use setuid()
1441 * below will fail. Hang on to root caps across setuid(), then
1442 * lock securebits.
1443 */
1444 if (prctl(PR_SET_KEEPCAPS, 1))
1445 pdie("prctl(PR_SET_KEEPCAPS)");
1446 if (prctl
1447 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1448 pdie("prctl(PR_SET_SECUREBITS)");
1449 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001450
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001451 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001452 /*
1453 * If we're setting no_new_privs, we can drop privileges
1454 * before setting seccomp filter. This way filter policies
1455 * don't need to allow privilege-dropping syscalls.
1456 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001457 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001458 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001459 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001460 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001461 /*
1462 * If we're not setting no_new_privs,
1463 * we need to set seccomp filter *before* dropping privileges.
1464 * WARNING: this means that filter policies *must* allow
1465 * setgroups()/setresgid()/setresuid() for dropping root and
1466 * capget()/capset()/prctl() for dropping caps.
1467 */
1468 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001469 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001470 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001471 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001472
Elly Jonesdd3e8512012-01-23 15:13:38 -05001473 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001474 * Select the specified alternate syscall table. The table must not
1475 * block prctl(2) if we're using seccomp as well.
1476 */
1477 if (j->flags.alt_syscall) {
1478 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1479 pdie("prctl(PR_ALT_SYSCALL)");
1480 }
1481
1482 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001483 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001484 * privilege-dropping syscalls :)
1485 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001486 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001487 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001488 warn("seccomp not supported");
1489 return;
1490 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001491 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001492 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001493}
1494
Will Drewry6ac91122011-10-21 16:38:58 -05001495/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001496static int init_exitstatus = 0;
1497
Will Drewry6ac91122011-10-21 16:38:58 -05001498void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001499{
1500 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001501}
1502
Will Drewry6ac91122011-10-21 16:38:58 -05001503int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001504{
1505 pid_t pid;
1506 int status;
1507 /* so that we exit with the right status */
1508 signal(SIGTERM, init_term);
1509 /* TODO(wad) self jail with seccomp_filters here. */
1510 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001511 /*
1512 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001513 * left inside our pid namespace or we get a signal.
1514 */
1515 if (pid == rootpid)
1516 init_exitstatus = status;
1517 }
1518 if (!WIFEXITED(init_exitstatus))
1519 _exit(MINIJAIL_ERR_INIT);
1520 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001521}
1522
Will Drewry6ac91122011-10-21 16:38:58 -05001523int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001524{
1525 size_t sz = 0;
1526 size_t bytes = read(fd, &sz, sizeof(sz));
1527 char *buf;
1528 int r;
1529 if (sizeof(sz) != bytes)
1530 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001531 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001532 return -E2BIG;
1533 buf = malloc(sz);
1534 if (!buf)
1535 return -ENOMEM;
1536 bytes = read(fd, buf, sz);
1537 if (bytes != sz) {
1538 free(buf);
1539 return -EINVAL;
1540 }
1541 r = minijail_unmarshal(j, buf, sz);
1542 free(buf);
1543 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001544}
1545
Will Drewry6ac91122011-10-21 16:38:58 -05001546int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001547{
1548 char *buf;
1549 size_t sz = minijail_size(j);
1550 ssize_t written;
1551 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001552
Elly Jonese1749eb2011-10-07 13:54:59 -04001553 if (!sz)
1554 return -EINVAL;
1555 buf = malloc(sz);
1556 r = minijail_marshal(j, buf, sz);
1557 if (r) {
1558 free(buf);
1559 return r;
1560 }
1561 /* Sends [size][minijail]. */
1562 written = write(fd, &sz, sizeof(sz));
1563 if (written != sizeof(sz)) {
1564 free(buf);
1565 return -EFAULT;
1566 }
1567 written = write(fd, buf, sz);
1568 if (written < 0 || (size_t) written != sz) {
1569 free(buf);
1570 return -EFAULT;
1571 }
1572 free(buf);
1573 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001574}
Elly Jonescd7a9042011-07-22 13:56:51 -04001575
Will Drewry6ac91122011-10-21 16:38:58 -05001576int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001577{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001578#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001579 /* Don't use LDPRELOAD on Brillo. */
1580 return 0;
1581#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001582 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1583 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1584 if (!newenv)
1585 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001586
Elly Jonese1749eb2011-10-07 13:54:59 -04001587 /* Only insert a separating space if we have something to separate... */
1588 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1589 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001590
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001591 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001592 setenv(kLdPreloadEnvVar, newenv, 1);
1593 free(newenv);
1594 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001595#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001596}
1597
Will Drewry6ac91122011-10-21 16:38:58 -05001598int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001599{
1600 int r = pipe(fds);
1601 char fd_buf[11];
1602 if (r)
1603 return r;
1604 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1605 if (r <= 0)
1606 return -EINVAL;
1607 setenv(kFdEnvVar, fd_buf, 1);
1608 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001609}
1610
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001611int setup_pipe_end(int fds[2], size_t index)
1612{
1613 if (index > 1)
1614 return -1;
1615
1616 close(fds[1 - index]);
1617 return fds[index];
1618}
1619
1620int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1621{
1622 if (index > 1)
1623 return -1;
1624
1625 close(fds[1 - index]);
1626 /* dup2(2) the corresponding end of the pipe into |fd|. */
1627 return dup2(fds[index], fd);
1628}
1629
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001630int minijail_run_internal(struct minijail *j, const char *filename,
1631 char *const argv[], pid_t *pchild_pid,
1632 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1633 int use_preload);
1634
Will Drewry6ac91122011-10-21 16:38:58 -05001635int API minijail_run(struct minijail *j, const char *filename,
1636 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001637{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001638 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1639 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001640}
1641
1642int API minijail_run_pid(struct minijail *j, const char *filename,
1643 char *const argv[], pid_t *pchild_pid)
1644{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001645 return minijail_run_internal(j, filename, argv, pchild_pid,
1646 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001647}
1648
1649int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001650 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001651{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001652 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1653 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001654}
1655
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001656int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001657 char *const argv[], pid_t *pchild_pid,
1658 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001659{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001660 return minijail_run_internal(j, filename, argv, pchild_pid,
1661 pstdin_fd, pstdout_fd, pstderr_fd, true);
1662}
1663
1664int API minijail_run_no_preload(struct minijail *j, const char *filename,
1665 char *const argv[])
1666{
1667 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1668 false);
1669}
1670
Samuel Tan63187f42015-10-16 13:01:53 -07001671int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001672 const char *filename,
1673 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001674 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001675 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001676 int *pstderr_fd)
1677{
Samuel Tan63187f42015-10-16 13:01:53 -07001678 return minijail_run_internal(j, filename, argv, pchild_pid,
1679 pstdin_fd, pstdout_fd, pstderr_fd, false);
1680}
1681
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001682int minijail_run_internal(struct minijail *j, const char *filename,
1683 char *const argv[], pid_t *pchild_pid,
1684 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1685 int use_preload)
1686{
Elly Jonese1749eb2011-10-07 13:54:59 -04001687 char *oldenv, *oldenv_copy = NULL;
1688 pid_t child_pid;
1689 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001690 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001691 int stdout_fds[2];
1692 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001693 int child_sync_pipe_fds[2];
1694 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001695 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001696 /* We need to remember this across the minijail_preexec() call. */
1697 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001698 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001699
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001700 if (use_preload) {
1701 oldenv = getenv(kLdPreloadEnvVar);
1702 if (oldenv) {
1703 oldenv_copy = strdup(oldenv);
1704 if (!oldenv_copy)
1705 return -ENOMEM;
1706 }
1707
1708 if (setup_preload())
1709 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001710 }
Will Drewryf89aef52011-09-16 16:48:57 -05001711
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001712 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001713 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001714 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001715 "LD_PRELOAD");
1716 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001717
Elly Jonesdd3e8512012-01-23 15:13:38 -05001718 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001719 * Make the process group ID of this process equal to its PID, so that
1720 * both the Minijail process and the jailed process can be killed
1721 * together.
1722 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1723 * the process is already a process group leader.
1724 */
1725 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1726 if (errno != EPERM) {
1727 pdie("setpgid(0, 0)");
1728 }
1729 }
1730
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001731 if (use_preload) {
1732 /*
1733 * Before we fork(2) and execve(2) the child process, we need
1734 * to open a pipe(2) to send the minijail configuration over.
1735 */
1736 if (setup_pipe(pipe_fds))
1737 return -EFAULT;
1738 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001739
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001740 /*
1741 * If we want to write to the child process' standard input,
1742 * create the pipe(2) now.
1743 */
1744 if (pstdin_fd) {
1745 if (pipe(stdin_fds))
1746 return -EFAULT;
1747 }
1748
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001749 /*
1750 * If we want to read from the child process' standard output,
1751 * create the pipe(2) now.
1752 */
1753 if (pstdout_fd) {
1754 if (pipe(stdout_fds))
1755 return -EFAULT;
1756 }
1757
1758 /*
1759 * If we want to read from the child process' standard error,
1760 * create the pipe(2) now.
1761 */
1762 if (pstderr_fd) {
1763 if (pipe(stderr_fds))
1764 return -EFAULT;
1765 }
1766
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001767 /*
1768 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001769 * or if we need to add the child process to cgroups, create the pipe(2)
1770 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001771 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001772 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001773 sync_child = 1;
1774 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001775 return -EFAULT;
1776 }
1777
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001778 /*
1779 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001780 *
1781 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1782 *
1783 * In multithreaded programs, there are a bunch of locks inside libc,
1784 * some of which may be held by other threads at the time that we call
1785 * minijail_run_pid(). If we call fork(), glibc does its level best to
1786 * ensure that we hold all of these locks before it calls clone()
1787 * internally and drop them after clone() returns, but when we call
1788 * sys_clone(2) directly, all that gets bypassed and we end up with a
1789 * child address space where some of libc's important locks are held by
1790 * other threads (which did not get cloned, and hence will never release
1791 * those locks). This is okay so long as we call exec() immediately
1792 * after, but a bunch of seemingly-innocent libc functions like setenv()
1793 * take locks.
1794 *
1795 * Hence, only call sys_clone() if we need to, in order to get at pid
1796 * namespacing. If we follow this path, the child's address space might
1797 * have broken locks; you may only call functions that do not acquire
1798 * any locks.
1799 *
1800 * Unfortunately, fork() acquires every lock it can get its hands on, as
1801 * previously detailed, so this function is highly likely to deadlock
1802 * later on (see "deadlock here") if we're multithreaded.
1803 *
1804 * We might hack around this by having the clone()d child (init of the
1805 * pid namespace) return directly, rather than leaving the clone()d
1806 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001807 * its fork()ed child return in turn), but that process would be
1808 * crippled with its libc locks potentially broken. We might try
1809 * fork()ing in the parent before we clone() to ensure that we own all
1810 * the locks, but then we have to have the forked child hanging around
1811 * consuming resources (and possibly having file descriptors / shared
1812 * memory regions / etc attached). We'd need to keep the child around to
1813 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001814 *
1815 * TODO(ellyjones): figure out if the "forked child hanging around"
1816 * problem is fixable or not. It would be nice if we worked in this
1817 * case.
1818 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001819 if (pid_namespace) {
1820 int clone_flags = CLONE_NEWPID | SIGCHLD;
1821 if (j->flags.userns)
1822 clone_flags |= CLONE_NEWUSER;
1823 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001824 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001825 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001826 }
Elly Jones761b7412012-06-13 15:49:52 -04001827
Elly Jonese1749eb2011-10-07 13:54:59 -04001828 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001829 if (use_preload) {
1830 free(oldenv_copy);
1831 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001832 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001833 }
Will Drewryf89aef52011-09-16 16:48:57 -05001834
Elly Jonese1749eb2011-10-07 13:54:59 -04001835 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001836 if (use_preload) {
1837 /* Restore parent's LD_PRELOAD. */
1838 if (oldenv_copy) {
1839 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1840 free(oldenv_copy);
1841 } else {
1842 unsetenv(kLdPreloadEnvVar);
1843 }
1844 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001845 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001846
Elly Jonese1749eb2011-10-07 13:54:59 -04001847 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001848
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001849 if (j->flags.pid_file)
1850 write_pid_file(j);
1851
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001852 if (j->flags.cgroups)
1853 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001854
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001855 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001856 write_ugid_mappings(j);
1857
1858 if (sync_child)
1859 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001860
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001861 if (use_preload) {
1862 /* Send marshalled minijail. */
1863 close(pipe_fds[0]); /* read endpoint */
1864 ret = minijail_to_fd(j, pipe_fds[1]);
1865 close(pipe_fds[1]); /* write endpoint */
1866 if (ret) {
1867 kill(j->initpid, SIGKILL);
1868 die("failed to send marshalled minijail");
1869 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001870 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001871
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001872 if (pchild_pid)
1873 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001874
1875 /*
1876 * If we want to write to the child process' standard input,
1877 * set up the write end of the pipe.
1878 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001879 if (pstdin_fd)
1880 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001881 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001882
1883 /*
1884 * If we want to read from the child process' standard output,
1885 * set up the read end of the pipe.
1886 */
1887 if (pstdout_fd)
1888 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001889 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001890
1891 /*
1892 * If we want to read from the child process' standard error,
1893 * set up the read end of the pipe.
1894 */
1895 if (pstderr_fd)
1896 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001897 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001898
Elly Jonese1749eb2011-10-07 13:54:59 -04001899 return 0;
1900 }
1901 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001902
Peter Qiu2860c462015-12-16 15:13:06 -08001903 if (j->flags.reset_signal_mask) {
1904 sigset_t signal_mask;
1905 if (sigemptyset(&signal_mask) != 0)
1906 pdie("sigemptyset failed");
1907 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1908 pdie("sigprocmask failed");
1909 }
1910
Dylan Reidce5b55e2016-01-13 11:04:16 -08001911 if (sync_child)
1912 wait_for_parent_setup(child_sync_pipe_fds);
1913
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001914 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001915 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001916
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001917 /*
1918 * If we want to write to the jailed process' standard input,
1919 * set up the read end of the pipe.
1920 */
1921 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001922 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1923 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001924 die("failed to set up stdin pipe");
1925 }
1926
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001927 /*
1928 * If we want to read from the jailed process' standard output,
1929 * set up the write end of the pipe.
1930 */
1931 if (pstdout_fd) {
1932 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1933 STDOUT_FILENO) < 0)
1934 die("failed to set up stdout pipe");
1935 }
1936
1937 /*
1938 * If we want to read from the jailed process' standard error,
1939 * set up the write end of the pipe.
1940 */
1941 if (pstderr_fd) {
1942 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1943 STDERR_FILENO) < 0)
1944 die("failed to set up stderr pipe");
1945 }
1946
Dylan Reid791f5772015-09-14 20:02:42 -07001947 /* If running an init program, let it decide when/how to mount /proc. */
1948 if (pid_namespace && !do_init)
1949 j->flags.remount_proc_ro = 0;
1950
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001951 if (use_preload) {
1952 /* Strip out flags that cannot be inherited across execve(2). */
1953 minijail_preexec(j);
1954 } else {
1955 j->flags.pids = 0;
1956 }
1957 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001958 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001959
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001960 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001961 /*
1962 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001963 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001964 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001965 * a child to actually run the program. If |do_init == 0|, we
1966 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001967 *
1968 * If we're multithreaded, we'll probably deadlock here. See
1969 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001970 */
1971 child_pid = fork();
1972 if (child_pid < 0)
1973 _exit(child_pid);
1974 else if (child_pid > 0)
1975 init(child_pid); /* never returns */
1976 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001977
Elly Jonesdd3e8512012-01-23 15:13:38 -05001978 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001979 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001980 * calling process
1981 * -> execve()-ing process
1982 * If we are:
1983 * calling process
1984 * -> init()-ing process
1985 * -> execve()-ing process
1986 */
1987 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001988}
1989
Will Drewry6ac91122011-10-21 16:38:58 -05001990int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001991{
1992 int st;
1993 if (kill(j->initpid, SIGTERM))
1994 return -errno;
1995 if (waitpid(j->initpid, &st, 0) < 0)
1996 return -errno;
1997 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001998}
1999
Will Drewry6ac91122011-10-21 16:38:58 -05002000int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002001{
2002 int st;
2003 if (waitpid(j->initpid, &st, 0) < 0)
2004 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002005
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002006 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002007 int error_status = st;
2008 if (WIFSIGNALED(st)) {
2009 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002010 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002011 j->initpid, signum);
2012 /*
2013 * We return MINIJAIL_ERR_JAIL if the process received
2014 * SIGSYS, which happens when a syscall is blocked by
2015 * seccomp filters.
2016 * If not, we do what bash(1) does:
2017 * $? = 128 + signum
2018 */
2019 if (signum == SIGSYS) {
2020 error_status = MINIJAIL_ERR_JAIL;
2021 } else {
2022 error_status = 128 + signum;
2023 }
2024 }
2025 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002026 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002027
2028 int exit_status = WEXITSTATUS(st);
2029 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002030 info("child process %d exited with status %d",
2031 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002032
2033 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002034}
2035
Will Drewry6ac91122011-10-21 16:38:58 -05002036void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002037{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002038 size_t i;
2039
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002040 if (j->flags.seccomp_filter && j->filter_prog) {
2041 free(j->filter_prog->filter);
2042 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002043 }
Dylan Reid648b2202015-10-23 00:50:00 -07002044 while (j->mounts_head) {
2045 struct mountpoint *m = j->mounts_head;
2046 j->mounts_head = j->mounts_head->next;
2047 free(m->type);
2048 free(m->dest);
2049 free(m->src);
2050 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002051 }
Dylan Reid648b2202015-10-23 00:50:00 -07002052 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002053 if (j->user)
2054 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002055 if (j->suppl_gid_list)
2056 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002057 if (j->chrootdir)
2058 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002059 if (j->alt_syscall_table)
2060 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002061 for (i = 0; i < j->cgroup_count; ++i)
2062 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002063 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002064}