blob: 2927b62a5485ff5ee839b72ab46d6724ec7cb969 [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.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001095 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001096 */
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 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001105 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001106 * 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 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001116 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001117 * 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 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001123 * If j->flags.skip_remount_private was enabled for minijail_enter(), there
1124 * could be a shared mount point under |oldroot|. In that case, mounts
1125 * under this shared mount point will be unmounted below, and this
1126 * unmounting will propagate to the original mount namespace (because the
1127 * mount point is shared). To prevent this unexpected unmounting, remove
1128 * these mounts from their peer groups by recursively remounting them as
1129 * MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001130 */
1131 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001132 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001133 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001134 if (umount2(".", MNT_DETACH))
1135 pdie("umount(/)");
1136 /* Change back to the new root. */
1137 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001138 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001139 if (close(oldroot))
1140 return -errno;
1141 if (close(newroot))
1142 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001143 if (chroot("/"))
1144 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001145 /* Set correct CWD for getcwd(3). */
1146 if (chdir("/"))
1147 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001148
1149 return 0;
1150}
1151
Lee Campbell11af0622014-05-22 12:36:04 -07001152int mount_tmp(void)
1153{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001154 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001155}
1156
Dylan Reid791f5772015-09-14 20:02:42 -07001157int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001158{
1159 const char *kProcPath = "/proc";
1160 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001161 /*
1162 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001163 * /proc in our namespace, which means using MS_REMOUNT here would
1164 * mutate our parent's mount as well, even though we're in a VFS
1165 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001166 * and make our own. However, if we are in a new user namespace, /proc
1167 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001168 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001169 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001170 return -errno;
1171 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1172 return -errno;
1173 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001174}
1175
Dylan Reid605ce7f2016-01-19 19:21:00 -08001176static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001177{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001178 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001179
1180 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001181 pdie("failed to open '%s'", path);
1182 if (fprintf(fp, "%d\n", (int)pid) < 0)
1183 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001184 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001185 pdie("fclose(%s)", path);
1186}
1187
1188static void write_pid_file(const struct minijail *j)
1189{
1190 write_pid_to_path(j->initpid, j->pid_file_path);
1191}
1192
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001193static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001194{
1195 size_t i;
1196
1197 for (i = 0; i < j->cgroup_count; ++i)
1198 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001199}
1200
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001201void drop_ugid(const struct minijail *j)
1202{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001203 if (j->flags.usergroups && j->flags.suppl_gids) {
1204 die("tried to inherit *and* set supplementary groups;"
1205 " can only do one");
1206 }
1207
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001208 if (j->flags.usergroups) {
1209 if (initgroups(j->user, j->usergid))
1210 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001211 } else if (j->flags.suppl_gids) {
1212 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1213 pdie("setgroups");
1214 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001215 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001216 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001217 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001218 * users.
1219 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001220 if ((j->uid || j->gid) && setgroups(0, NULL))
1221 pdie("setgroups");
1222 }
1223
1224 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1225 pdie("setresgid");
1226
1227 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1228 pdie("setresuid");
1229}
1230
Mike Frysinger3adfef72013-05-09 17:19:08 -04001231/*
1232 * We specifically do not use cap_valid() as that only tells us the last
1233 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001234 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001235 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001236 * kernel).
1237 * Normally, we suck up the answer via /proc. On Android, not all processes are
1238 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1239 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001240 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001241static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001242{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001243 unsigned int last_valid_cap = 0;
1244 if (is_android()) {
1245 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1246 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001247
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001248 /* |last_valid_cap| will be the first failing value. */
1249 if (last_valid_cap > 0) {
1250 last_valid_cap--;
1251 }
1252 } else {
1253 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1254 FILE *fp = fopen(cap_file, "re");
1255 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1256 pdie("fscanf(%s)", cap_file);
1257 fclose(fp);
1258 }
Dylan Reidf682d472015-09-17 21:39:07 -07001259 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001260}
1261
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001262static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1263{
1264 const uint64_t one = 1;
1265 unsigned int i;
1266 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1267 if (keep_mask & (one << i))
1268 continue;
1269 if (prctl(PR_CAPBSET_DROP, i))
1270 pdie("could not drop capability from bounding set");
1271 }
1272}
1273
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001274void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001275{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001276 if (!j->flags.use_caps)
1277 return;
1278
Elly Jonese1749eb2011-10-07 13:54:59 -04001279 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001280 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001281 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001282 unsigned int i;
1283 if (!caps)
1284 die("can't get process caps");
1285 if (cap_clear_flag(caps, CAP_INHERITABLE))
1286 die("can't clear inheritable caps");
1287 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1288 die("can't clear effective caps");
1289 if (cap_clear_flag(caps, CAP_PERMITTED))
1290 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001291 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001292 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001293 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001294 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001295 flag[0] = i;
1296 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001297 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001298 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001299 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001300 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001301 die("can't add inheritable cap");
1302 }
1303 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001304 die("can't apply initial cleaned capset");
1305
1306 /*
1307 * Instead of dropping bounding set first, do it here in case
1308 * the caller had a more permissive bounding set which could
1309 * have been used above to raise a capability that wasn't already
1310 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1311 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001312 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001313
1314 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001315 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001316 flag[0] = CAP_SETPCAP;
1317 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1318 die("can't clear effective cap");
1319 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1320 die("can't clear permitted cap");
1321 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1322 die("can't clear inheritable cap");
1323 }
1324
1325 if (cap_set_proc(caps))
1326 die("can't apply final cleaned capset");
1327
1328 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001329}
1330
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001331void set_seccomp_filter(const struct minijail *j)
1332{
1333 /*
1334 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1335 * in the kernel source tree for an explanation of the parameters.
1336 */
1337 if (j->flags.no_new_privs) {
1338 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1339 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1340 }
1341
1342 /*
1343 * If we're logging seccomp filter failures,
1344 * install the SIGSYS handler first.
1345 */
1346 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1347 if (install_sigsys_handler())
1348 pdie("install SIGSYS handler");
1349 warn("logging seccomp filter failures");
1350 }
1351
1352 /*
1353 * Install the syscall filter.
1354 */
1355 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001356 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1357 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001358 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001359 warn("seccomp not supported");
1360 return;
1361 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001362 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001363 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001364 }
1365}
1366
Will Drewry6ac91122011-10-21 16:38:58 -05001367void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001368{
Dylan Reidf682d472015-09-17 21:39:07 -07001369 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001370 * If we're dropping caps, get the last valid cap from /proc now,
1371 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001372 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001373 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001374 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001375 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001376
Elly Jonese1749eb2011-10-07 13:54:59 -04001377 if (j->flags.pids)
1378 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001379 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001380
Elly Jonese1749eb2011-10-07 13:54:59 -04001381 if (j->flags.usergroups && !j->user)
1382 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001383
Elly Jonesdd3e8512012-01-23 15:13:38 -05001384 /*
1385 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001386 * so we don't even try. If any of our operations fail, we abort() the
1387 * entire process.
1388 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001389 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1390 pdie("setns(CLONE_NEWNS)");
1391
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001392 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001393 if (unshare(CLONE_NEWNS))
1394 pdie("unshare(vfs)");
1395 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001396 * Unless asked not to, remount all filesystems as private.
1397 * If they are shared, new bind mounts will creep out of our
1398 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001399 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1400 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001401 if (!j->flags.skip_remount_private) {
1402 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1403 pdie("mount(/, private)");
1404 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001405 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001406
Dylan Reidf7942472015-11-18 17:55:26 -08001407 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1408 pdie("unshare(ipc)");
1409 }
1410
Dylan Reid1102f5a2015-09-15 11:52:20 -07001411 if (j->flags.enter_net) {
1412 if (setns(j->netns_fd, CLONE_NEWNET))
1413 pdie("setns(CLONE_NEWNET)");
1414 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001415 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001416 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001417
Elly Jones51a5b6c2011-10-12 19:09:26 -04001418 if (j->flags.chroot && enter_chroot(j))
1419 pdie("chroot");
1420
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001421 if (j->flags.pivot_root && enter_pivot_root(j))
1422 pdie("pivot_root");
1423
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001424 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001425 pdie("mount_tmp");
1426
Dylan Reid791f5772015-09-14 20:02:42 -07001427 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001428 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001429
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001430 /*
1431 * If we're only dropping capabilities from the bounding set, but not
1432 * from the thread's (permitted|inheritable|effective) sets, do it now.
1433 */
1434 if (j->flags.capbset_drop) {
1435 drop_capbset(j->cap_bset, last_valid_cap);
1436 }
1437
1438 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001439 /*
1440 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001441 * capability to change uids, our attempt to use setuid()
1442 * below will fail. Hang on to root caps across setuid(), then
1443 * lock securebits.
1444 */
1445 if (prctl(PR_SET_KEEPCAPS, 1))
1446 pdie("prctl(PR_SET_KEEPCAPS)");
1447 if (prctl
1448 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1449 pdie("prctl(PR_SET_SECUREBITS)");
1450 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001451
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001452 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001453 /*
1454 * If we're setting no_new_privs, we can drop privileges
1455 * before setting seccomp filter. This way filter policies
1456 * don't need to allow privilege-dropping syscalls.
1457 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001458 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001459 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001460 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001461 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001462 /*
1463 * If we're not setting no_new_privs,
1464 * we need to set seccomp filter *before* dropping privileges.
1465 * WARNING: this means that filter policies *must* allow
1466 * setgroups()/setresgid()/setresuid() for dropping root and
1467 * capget()/capset()/prctl() for dropping caps.
1468 */
1469 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001470 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001471 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001472 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001473
Elly Jonesdd3e8512012-01-23 15:13:38 -05001474 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001475 * Select the specified alternate syscall table. The table must not
1476 * block prctl(2) if we're using seccomp as well.
1477 */
1478 if (j->flags.alt_syscall) {
1479 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1480 pdie("prctl(PR_ALT_SYSCALL)");
1481 }
1482
1483 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001484 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001485 * privilege-dropping syscalls :)
1486 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001487 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001488 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001489 warn("seccomp not supported");
1490 return;
1491 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001492 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001493 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001494}
1495
Will Drewry6ac91122011-10-21 16:38:58 -05001496/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001497static int init_exitstatus = 0;
1498
Will Drewry6ac91122011-10-21 16:38:58 -05001499void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001500{
1501 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001502}
1503
Will Drewry6ac91122011-10-21 16:38:58 -05001504int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001505{
1506 pid_t pid;
1507 int status;
1508 /* so that we exit with the right status */
1509 signal(SIGTERM, init_term);
1510 /* TODO(wad) self jail with seccomp_filters here. */
1511 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001512 /*
1513 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001514 * left inside our pid namespace or we get a signal.
1515 */
1516 if (pid == rootpid)
1517 init_exitstatus = status;
1518 }
1519 if (!WIFEXITED(init_exitstatus))
1520 _exit(MINIJAIL_ERR_INIT);
1521 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001522}
1523
Will Drewry6ac91122011-10-21 16:38:58 -05001524int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001525{
1526 size_t sz = 0;
1527 size_t bytes = read(fd, &sz, sizeof(sz));
1528 char *buf;
1529 int r;
1530 if (sizeof(sz) != bytes)
1531 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001532 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001533 return -E2BIG;
1534 buf = malloc(sz);
1535 if (!buf)
1536 return -ENOMEM;
1537 bytes = read(fd, buf, sz);
1538 if (bytes != sz) {
1539 free(buf);
1540 return -EINVAL;
1541 }
1542 r = minijail_unmarshal(j, buf, sz);
1543 free(buf);
1544 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001545}
1546
Will Drewry6ac91122011-10-21 16:38:58 -05001547int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001548{
1549 char *buf;
1550 size_t sz = minijail_size(j);
1551 ssize_t written;
1552 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001553
Elly Jonese1749eb2011-10-07 13:54:59 -04001554 if (!sz)
1555 return -EINVAL;
1556 buf = malloc(sz);
1557 r = minijail_marshal(j, buf, sz);
1558 if (r) {
1559 free(buf);
1560 return r;
1561 }
1562 /* Sends [size][minijail]. */
1563 written = write(fd, &sz, sizeof(sz));
1564 if (written != sizeof(sz)) {
1565 free(buf);
1566 return -EFAULT;
1567 }
1568 written = write(fd, buf, sz);
1569 if (written < 0 || (size_t) written != sz) {
1570 free(buf);
1571 return -EFAULT;
1572 }
1573 free(buf);
1574 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001575}
Elly Jonescd7a9042011-07-22 13:56:51 -04001576
Will Drewry6ac91122011-10-21 16:38:58 -05001577int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001578{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001579#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001580 /* Don't use LDPRELOAD on Brillo. */
1581 return 0;
1582#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001583 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1584 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1585 if (!newenv)
1586 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001587
Elly Jonese1749eb2011-10-07 13:54:59 -04001588 /* Only insert a separating space if we have something to separate... */
1589 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1590 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001591
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001592 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001593 setenv(kLdPreloadEnvVar, newenv, 1);
1594 free(newenv);
1595 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001596#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001597}
1598
Will Drewry6ac91122011-10-21 16:38:58 -05001599int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001600{
1601 int r = pipe(fds);
1602 char fd_buf[11];
1603 if (r)
1604 return r;
1605 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1606 if (r <= 0)
1607 return -EINVAL;
1608 setenv(kFdEnvVar, fd_buf, 1);
1609 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001610}
1611
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001612int setup_pipe_end(int fds[2], size_t index)
1613{
1614 if (index > 1)
1615 return -1;
1616
1617 close(fds[1 - index]);
1618 return fds[index];
1619}
1620
1621int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1622{
1623 if (index > 1)
1624 return -1;
1625
1626 close(fds[1 - index]);
1627 /* dup2(2) the corresponding end of the pipe into |fd|. */
1628 return dup2(fds[index], fd);
1629}
1630
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001631int minijail_run_internal(struct minijail *j, const char *filename,
1632 char *const argv[], pid_t *pchild_pid,
1633 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1634 int use_preload);
1635
Will Drewry6ac91122011-10-21 16:38:58 -05001636int API minijail_run(struct minijail *j, const char *filename,
1637 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001638{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001639 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1640 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001641}
1642
1643int API minijail_run_pid(struct minijail *j, const char *filename,
1644 char *const argv[], pid_t *pchild_pid)
1645{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001646 return minijail_run_internal(j, filename, argv, pchild_pid,
1647 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001648}
1649
1650int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001651 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001652{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001653 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1654 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001655}
1656
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001657int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001658 char *const argv[], pid_t *pchild_pid,
1659 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001660{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001661 return minijail_run_internal(j, filename, argv, pchild_pid,
1662 pstdin_fd, pstdout_fd, pstderr_fd, true);
1663}
1664
1665int API minijail_run_no_preload(struct minijail *j, const char *filename,
1666 char *const argv[])
1667{
1668 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1669 false);
1670}
1671
Samuel Tan63187f42015-10-16 13:01:53 -07001672int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001673 const char *filename,
1674 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001675 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001676 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001677 int *pstderr_fd)
1678{
Samuel Tan63187f42015-10-16 13:01:53 -07001679 return minijail_run_internal(j, filename, argv, pchild_pid,
1680 pstdin_fd, pstdout_fd, pstderr_fd, false);
1681}
1682
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001683int minijail_run_internal(struct minijail *j, const char *filename,
1684 char *const argv[], pid_t *pchild_pid,
1685 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1686 int use_preload)
1687{
Elly Jonese1749eb2011-10-07 13:54:59 -04001688 char *oldenv, *oldenv_copy = NULL;
1689 pid_t child_pid;
1690 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001691 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001692 int stdout_fds[2];
1693 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001694 int child_sync_pipe_fds[2];
1695 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001696 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001697 /* We need to remember this across the minijail_preexec() call. */
1698 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001699 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001700
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001701 if (use_preload) {
1702 oldenv = getenv(kLdPreloadEnvVar);
1703 if (oldenv) {
1704 oldenv_copy = strdup(oldenv);
1705 if (!oldenv_copy)
1706 return -ENOMEM;
1707 }
1708
1709 if (setup_preload())
1710 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001711 }
Will Drewryf89aef52011-09-16 16:48:57 -05001712
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001713 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001714 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001715 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001716 "LD_PRELOAD");
1717 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001718
Elly Jonesdd3e8512012-01-23 15:13:38 -05001719 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001720 * Make the process group ID of this process equal to its PID, so that
1721 * both the Minijail process and the jailed process can be killed
1722 * together.
1723 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1724 * the process is already a process group leader.
1725 */
1726 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1727 if (errno != EPERM) {
1728 pdie("setpgid(0, 0)");
1729 }
1730 }
1731
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001732 if (use_preload) {
1733 /*
1734 * Before we fork(2) and execve(2) the child process, we need
1735 * to open a pipe(2) to send the minijail configuration over.
1736 */
1737 if (setup_pipe(pipe_fds))
1738 return -EFAULT;
1739 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001740
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001741 /*
1742 * If we want to write to the child process' standard input,
1743 * create the pipe(2) now.
1744 */
1745 if (pstdin_fd) {
1746 if (pipe(stdin_fds))
1747 return -EFAULT;
1748 }
1749
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001750 /*
1751 * If we want to read from the child process' standard output,
1752 * create the pipe(2) now.
1753 */
1754 if (pstdout_fd) {
1755 if (pipe(stdout_fds))
1756 return -EFAULT;
1757 }
1758
1759 /*
1760 * If we want to read from the child process' standard error,
1761 * create the pipe(2) now.
1762 */
1763 if (pstderr_fd) {
1764 if (pipe(stderr_fds))
1765 return -EFAULT;
1766 }
1767
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001768 /*
1769 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001770 * or if we need to add the child process to cgroups, create the pipe(2)
1771 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001772 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001773 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001774 sync_child = 1;
1775 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001776 return -EFAULT;
1777 }
1778
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001779 /*
1780 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001781 *
1782 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1783 *
1784 * In multithreaded programs, there are a bunch of locks inside libc,
1785 * some of which may be held by other threads at the time that we call
1786 * minijail_run_pid(). If we call fork(), glibc does its level best to
1787 * ensure that we hold all of these locks before it calls clone()
1788 * internally and drop them after clone() returns, but when we call
1789 * sys_clone(2) directly, all that gets bypassed and we end up with a
1790 * child address space where some of libc's important locks are held by
1791 * other threads (which did not get cloned, and hence will never release
1792 * those locks). This is okay so long as we call exec() immediately
1793 * after, but a bunch of seemingly-innocent libc functions like setenv()
1794 * take locks.
1795 *
1796 * Hence, only call sys_clone() if we need to, in order to get at pid
1797 * namespacing. If we follow this path, the child's address space might
1798 * have broken locks; you may only call functions that do not acquire
1799 * any locks.
1800 *
1801 * Unfortunately, fork() acquires every lock it can get its hands on, as
1802 * previously detailed, so this function is highly likely to deadlock
1803 * later on (see "deadlock here") if we're multithreaded.
1804 *
1805 * We might hack around this by having the clone()d child (init of the
1806 * pid namespace) return directly, rather than leaving the clone()d
1807 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001808 * its fork()ed child return in turn), but that process would be
1809 * crippled with its libc locks potentially broken. We might try
1810 * fork()ing in the parent before we clone() to ensure that we own all
1811 * the locks, but then we have to have the forked child hanging around
1812 * consuming resources (and possibly having file descriptors / shared
1813 * memory regions / etc attached). We'd need to keep the child around to
1814 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001815 *
1816 * TODO(ellyjones): figure out if the "forked child hanging around"
1817 * problem is fixable or not. It would be nice if we worked in this
1818 * case.
1819 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001820 if (pid_namespace) {
1821 int clone_flags = CLONE_NEWPID | SIGCHLD;
1822 if (j->flags.userns)
1823 clone_flags |= CLONE_NEWUSER;
1824 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001825 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001826 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001827 }
Elly Jones761b7412012-06-13 15:49:52 -04001828
Elly Jonese1749eb2011-10-07 13:54:59 -04001829 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001830 if (use_preload) {
1831 free(oldenv_copy);
1832 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001833 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001834 }
Will Drewryf89aef52011-09-16 16:48:57 -05001835
Elly Jonese1749eb2011-10-07 13:54:59 -04001836 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001837 if (use_preload) {
1838 /* Restore parent's LD_PRELOAD. */
1839 if (oldenv_copy) {
1840 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1841 free(oldenv_copy);
1842 } else {
1843 unsetenv(kLdPreloadEnvVar);
1844 }
1845 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001846 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001847
Elly Jonese1749eb2011-10-07 13:54:59 -04001848 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001849
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001850 if (j->flags.pid_file)
1851 write_pid_file(j);
1852
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001853 if (j->flags.cgroups)
1854 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001855
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001856 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001857 write_ugid_mappings(j);
1858
1859 if (sync_child)
1860 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001861
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001862 if (use_preload) {
1863 /* Send marshalled minijail. */
1864 close(pipe_fds[0]); /* read endpoint */
1865 ret = minijail_to_fd(j, pipe_fds[1]);
1866 close(pipe_fds[1]); /* write endpoint */
1867 if (ret) {
1868 kill(j->initpid, SIGKILL);
1869 die("failed to send marshalled minijail");
1870 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001871 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001872
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001873 if (pchild_pid)
1874 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001875
1876 /*
1877 * If we want to write to the child process' standard input,
1878 * set up the write end of the pipe.
1879 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001880 if (pstdin_fd)
1881 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001882 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001883
1884 /*
1885 * If we want to read from the child process' standard output,
1886 * set up the read end of the pipe.
1887 */
1888 if (pstdout_fd)
1889 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001890 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001891
1892 /*
1893 * If we want to read from the child process' standard error,
1894 * set up the read end of the pipe.
1895 */
1896 if (pstderr_fd)
1897 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001898 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001899
Elly Jonese1749eb2011-10-07 13:54:59 -04001900 return 0;
1901 }
1902 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001903
Peter Qiu2860c462015-12-16 15:13:06 -08001904 if (j->flags.reset_signal_mask) {
1905 sigset_t signal_mask;
1906 if (sigemptyset(&signal_mask) != 0)
1907 pdie("sigemptyset failed");
1908 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1909 pdie("sigprocmask failed");
1910 }
1911
Dylan Reidce5b55e2016-01-13 11:04:16 -08001912 if (sync_child)
1913 wait_for_parent_setup(child_sync_pipe_fds);
1914
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001915 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001916 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001917
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001918 /*
1919 * If we want to write to the jailed process' standard input,
1920 * set up the read end of the pipe.
1921 */
1922 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001923 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1924 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001925 die("failed to set up stdin pipe");
1926 }
1927
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001928 /*
1929 * If we want to read from the jailed process' standard output,
1930 * set up the write end of the pipe.
1931 */
1932 if (pstdout_fd) {
1933 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1934 STDOUT_FILENO) < 0)
1935 die("failed to set up stdout pipe");
1936 }
1937
1938 /*
1939 * If we want to read from the jailed process' standard error,
1940 * set up the write end of the pipe.
1941 */
1942 if (pstderr_fd) {
1943 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1944 STDERR_FILENO) < 0)
1945 die("failed to set up stderr pipe");
1946 }
1947
Dylan Reid791f5772015-09-14 20:02:42 -07001948 /* If running an init program, let it decide when/how to mount /proc. */
1949 if (pid_namespace && !do_init)
1950 j->flags.remount_proc_ro = 0;
1951
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001952 if (use_preload) {
1953 /* Strip out flags that cannot be inherited across execve(2). */
1954 minijail_preexec(j);
1955 } else {
1956 j->flags.pids = 0;
1957 }
1958 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001959 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001960
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001961 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001962 /*
1963 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001964 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001965 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001966 * a child to actually run the program. If |do_init == 0|, we
1967 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001968 *
1969 * If we're multithreaded, we'll probably deadlock here. See
1970 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001971 */
1972 child_pid = fork();
1973 if (child_pid < 0)
1974 _exit(child_pid);
1975 else if (child_pid > 0)
1976 init(child_pid); /* never returns */
1977 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001978
Elly Jonesdd3e8512012-01-23 15:13:38 -05001979 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001980 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001981 * calling process
1982 * -> execve()-ing process
1983 * If we are:
1984 * calling process
1985 * -> init()-ing process
1986 * -> execve()-ing process
1987 */
1988 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001989}
1990
Will Drewry6ac91122011-10-21 16:38:58 -05001991int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001992{
1993 int st;
1994 if (kill(j->initpid, SIGTERM))
1995 return -errno;
1996 if (waitpid(j->initpid, &st, 0) < 0)
1997 return -errno;
1998 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001999}
2000
Will Drewry6ac91122011-10-21 16:38:58 -05002001int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002002{
2003 int st;
2004 if (waitpid(j->initpid, &st, 0) < 0)
2005 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002006
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002007 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002008 int error_status = st;
2009 if (WIFSIGNALED(st)) {
2010 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002011 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002012 j->initpid, signum);
2013 /*
2014 * We return MINIJAIL_ERR_JAIL if the process received
2015 * SIGSYS, which happens when a syscall is blocked by
2016 * seccomp filters.
2017 * If not, we do what bash(1) does:
2018 * $? = 128 + signum
2019 */
2020 if (signum == SIGSYS) {
2021 error_status = MINIJAIL_ERR_JAIL;
2022 } else {
2023 error_status = 128 + signum;
2024 }
2025 }
2026 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002027 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002028
2029 int exit_status = WEXITSTATUS(st);
2030 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002031 info("child process %d exited with status %d",
2032 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002033
2034 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002035}
2036
Will Drewry6ac91122011-10-21 16:38:58 -05002037void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002038{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002039 size_t i;
2040
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002041 if (j->flags.seccomp_filter && j->filter_prog) {
2042 free(j->filter_prog->filter);
2043 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002044 }
Dylan Reid648b2202015-10-23 00:50:00 -07002045 while (j->mounts_head) {
2046 struct mountpoint *m = j->mounts_head;
2047 j->mounts_head = j->mounts_head->next;
2048 free(m->type);
2049 free(m->dest);
2050 free(m->src);
2051 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002052 }
Dylan Reid648b2202015-10-23 00:50:00 -07002053 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002054 if (j->user)
2055 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002056 if (j->suppl_gid_list)
2057 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002058 if (j->chrootdir)
2059 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002060 if (j->alt_syscall_table)
2061 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002062 for (i = 0; i < j->cgroup_count; ++i)
2063 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002064 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002065}