blob: b5b041e53c92aaab1a43b8e47dfb434a69f503a2 [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;
Dylan Reid791f5772015-09-14 20:02:42 -0700169 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800170 int userns = j->flags.userns;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900171 int skip_remount_private = j->flags.skip_remount_private;
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;
Dylan Reid791f5772015-09-14 20:02:42 -0700182 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800183 j->flags.userns = userns;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900184 j->flags.skip_remount_private = skip_remount_private;
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
285 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3) succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500286 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400287 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700288 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400289 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700290 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400291 minijail_change_uid(j, ppw->pw_uid);
292 j->user = strdup(user);
293 if (!j->user)
294 return -ENOMEM;
295 j->usergid = ppw->pw_gid;
296 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400297}
298
Will Drewry6ac91122011-10-21 16:38:58 -0500299int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400300{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700301 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700302 struct group gr;
303 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400304 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
305 if (sz == -1)
306 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400307
Elly Jonesdd3e8512012-01-23 15:13:38 -0500308 /*
309 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400310 * the maximum needed size of the buffer, so we don't have to search.
311 */
312 buf = malloc(sz);
313 if (!buf)
314 return -ENOMEM;
315 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500316 /*
317 * We're safe to free the buffer here. The strings inside gr point
318 * inside buf, but we don't use any of them; this leaves the pointers
319 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
320 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400321 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700322 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400323 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700324 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400325 minijail_change_gid(j, pgr->gr_gid);
326 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400327}
328
Will Drewry6ac91122011-10-21 16:38:58 -0500329void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400330{
331 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400332}
333
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700334void API minijail_no_new_privs(struct minijail *j)
335{
336 j->flags.no_new_privs = 1;
337}
338
Will Drewry6ac91122011-10-21 16:38:58 -0500339void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400340{
341 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500342}
343
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700344void API minijail_log_seccomp_filter_failures(struct minijail *j)
345{
346 j->flags.log_seccomp_filter = 1;
347}
348
Will Drewry6ac91122011-10-21 16:38:58 -0500349void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400350{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800351 /*
352 * 'minijail_use_caps' configures a runtime-capabilities-only
353 * environment, including a bounding set matching the thread's runtime
354 * (permitted|inheritable|effective) sets.
355 * Therefore, it will override any existing bounding set configurations
356 * since the latter would allow gaining extra runtime capabilities from
357 * file capabilities.
358 */
359 if (j->flags.capbset_drop) {
360 warn("overriding bounding set configuration");
361 j->cap_bset = 0;
362 j->flags.capbset_drop = 0;
363 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400364 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800365 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400366}
367
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800368void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
369{
370 if (j->flags.use_caps) {
371 /*
372 * 'minijail_use_caps' will have already configured a capability
373 * bounding set matching the (permitted|inheritable|effective)
374 * sets. Abort if the user tries to configure a separate
375 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
376 * are mutually exclusive.
377 */
378 die("runtime capabilities already configured, can't drop "
379 "bounding set separately");
380 }
381 j->cap_bset = capmask;
382 j->flags.capbset_drop = 1;
383}
384
385void API minijail_reset_signal_mask(struct minijail *j)
386{
Peter Qiu2860c462015-12-16 15:13:06 -0800387 j->flags.reset_signal_mask = 1;
388}
389
Will Drewry6ac91122011-10-21 16:38:58 -0500390void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400391{
392 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400393}
394
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700395void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
396{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800397 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700398 if (ns_fd < 0) {
399 pdie("failed to open namespace '%s'", ns_path);
400 }
401 j->mountns_fd = ns_fd;
402 j->flags.enter_vfs = 1;
403}
404
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800405void API minijail_skip_remount_private(struct minijail *j)
406{
407 j->flags.skip_remount_private = 1;
408}
409
Will Drewry6ac91122011-10-21 16:38:58 -0500410void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400411{
Elly Jonese58176c2012-01-23 11:46:17 -0500412 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700413 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400414 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800415 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400416}
417
Dylan Reidf7942472015-11-18 17:55:26 -0800418void API minijail_namespace_ipc(struct minijail *j)
419{
420 j->flags.ipc = 1;
421}
422
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400423void API minijail_namespace_net(struct minijail *j)
424{
425 j->flags.net = 1;
426}
427
Dylan Reid1102f5a2015-09-15 11:52:20 -0700428void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
429{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800430 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700431 if (ns_fd < 0) {
432 pdie("failed to open namespace '%s'", ns_path);
433 }
434 j->netns_fd = ns_fd;
435 j->flags.enter_net = 1;
436}
437
Dylan Reid791f5772015-09-14 20:02:42 -0700438void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400439{
440 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700441 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400442}
443
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800444void API minijail_namespace_user(struct minijail *j)
445{
446 j->flags.userns = 1;
447}
448
449int API minijail_uidmap(struct minijail *j, const char *uidmap)
450{
451 j->uidmap = strdup(uidmap);
452 if (!j->uidmap)
453 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800454 char *ch;
455 for (ch = j->uidmap; *ch; ch++) {
456 if (*ch == ',')
457 *ch = '\n';
458 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800459 return 0;
460}
461
462int API minijail_gidmap(struct minijail *j, const char *gidmap)
463{
464 j->gidmap = strdup(gidmap);
465 if (!j->gidmap)
466 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800467 char *ch;
468 for (ch = j->gidmap; *ch; ch++) {
469 if (*ch == ',')
470 *ch = '\n';
471 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800472 return 0;
473}
474
Will Drewry6ac91122011-10-21 16:38:58 -0500475void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400476{
477 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400478}
479
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800480void API minijail_run_as_init(struct minijail *j)
481{
482 /*
483 * Since the jailed program will become 'init' in the new PID namespace,
484 * Minijail does not need to fork an 'init' process.
485 */
486 j->flags.do_init = 0;
487}
488
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700489int API minijail_enter_chroot(struct minijail *j, const char *dir)
490{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400491 if (j->chrootdir)
492 return -EINVAL;
493 j->chrootdir = strdup(dir);
494 if (!j->chrootdir)
495 return -ENOMEM;
496 j->flags.chroot = 1;
497 return 0;
498}
499
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800500int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
501{
502 if (j->chrootdir)
503 return -EINVAL;
504 j->chrootdir = strdup(dir);
505 if (!j->chrootdir)
506 return -ENOMEM;
507 j->flags.pivot_root = 1;
508 return 0;
509}
510
Dylan Reida14e08d2015-10-22 21:05:29 -0700511static char *append_external_path(const char *external_path,
512 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700513{
Dylan Reida14e08d2015-10-22 21:05:29 -0700514 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700515 size_t pathlen;
516
Dylan Reid08946cc2015-09-16 19:10:57 -0700517 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700518 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
519 path = malloc(pathlen);
520 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700521
Dylan Reida14e08d2015-10-22 21:05:29 -0700522 return path;
523}
524
525char API *minijail_get_original_path(struct minijail *j,
526 const char *path_inside_chroot)
527{
Dylan Reid648b2202015-10-23 00:50:00 -0700528 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700529
Dylan Reid648b2202015-10-23 00:50:00 -0700530 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700531 while (b) {
532 /*
533 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700534 * mount, then the original path is exactly the source of
535 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700536 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700537 * mount source = /some/path/exe, mount dest =
538 * /chroot/path/exe Then when getting the original path of
539 * "/chroot/path/exe", the source of that mount,
540 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700541 */
542 if (!strcmp(b->dest, path_inside_chroot))
543 return strdup(b->src);
544
545 /*
546 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700547 * mount, take the suffix of the chroot path relative to the
548 * mount destination path, and append it to the mount source
549 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700550 */
551 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
552 const char *relative_path =
553 path_inside_chroot + strlen(b->dest);
554 return append_external_path(b->src, relative_path);
555 }
556 b = b->next;
557 }
558
559 /* If there is a chroot path, append |path_inside_chroot| to that. */
560 if (j->chrootdir)
561 return append_external_path(j->chrootdir, path_inside_chroot);
562
563 /* No chroot, so the path outside is the same as it is inside. */
564 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700565}
566
Lee Campbell11af0622014-05-22 12:36:04 -0700567void API minijail_mount_tmp(struct minijail *j)
568{
569 j->flags.mount_tmp = 1;
570}
571
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800572int API minijail_write_pid_file(struct minijail *j, const char *path)
573{
574 j->pid_file_path = strdup(path);
575 if (!j->pid_file_path)
576 return -ENOMEM;
577 j->flags.pid_file = 1;
578 return 0;
579}
580
Dylan Reid605ce7f2016-01-19 19:21:00 -0800581int API minijail_add_to_cgroup(struct minijail *j, const char *path)
582{
583 if (j->cgroup_count >= MAX_CGROUPS)
584 return -ENOMEM;
585 j->cgroups[j->cgroup_count] = strdup(path);
586 if (!j->cgroups[j->cgroup_count])
587 return -ENOMEM;
588 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800589 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800590 return 0;
591}
592
Dylan Reid648b2202015-10-23 00:50:00 -0700593int API minijail_mount(struct minijail *j, const char *src, const char *dest,
594 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700595{
Dylan Reid648b2202015-10-23 00:50:00 -0700596 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400597
598 if (*dest != '/')
599 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700600 m = calloc(1, sizeof(*m));
601 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400602 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700603 m->dest = strdup(dest);
604 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400605 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700606 m->src = strdup(src);
607 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400608 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700609 m->type = strdup(type);
610 if (!m->type)
611 goto error;
612 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400613
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800614 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400615
Elly Jonesdd3e8512012-01-23 15:13:38 -0500616 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700617 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400618 * containing vfs namespace.
619 */
620 minijail_namespace_vfs(j);
621
Dylan Reid648b2202015-10-23 00:50:00 -0700622 if (j->mounts_tail)
623 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400624 else
Dylan Reid648b2202015-10-23 00:50:00 -0700625 j->mounts_head = m;
626 j->mounts_tail = m;
627 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400628
629 return 0;
630
631error:
Dylan Reid648b2202015-10-23 00:50:00 -0700632 free(m->src);
633 free(m->dest);
634 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400635 return -ENOMEM;
636}
637
Dylan Reid648b2202015-10-23 00:50:00 -0700638int API minijail_bind(struct minijail *j, const char *src, const char *dest,
639 int writeable)
640{
641 unsigned long flags = MS_BIND;
642
643 if (!writeable)
644 flags |= MS_RDONLY;
645
646 return minijail_mount(j, src, dest, "", flags);
647}
648
Will Drewry6ac91122011-10-21 16:38:58 -0500649void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400650{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700651 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800652 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800653 warn("not loading seccomp filter,"
654 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800655 j->flags.seccomp_filter = 0;
656 j->flags.log_seccomp_filter = 0;
657 j->filter_len = 0;
658 j->filter_prog = NULL;
659 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700660 }
661 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400662 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800663 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700664 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400665 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800666
667 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700668 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
669 die("failed to compile seccomp filter BPF program in '%s'",
670 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800671 }
672
673 j->filter_len = fprog->len;
674 j->filter_prog = fprog;
675
Elly Jonese1749eb2011-10-07 13:54:59 -0400676 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500677}
678
Andrew Brestickereac28942015-11-11 16:04:46 -0800679int API minijail_use_alt_syscall(struct minijail *j, const char *table)
680{
681 j->alt_syscall_table = strdup(table);
682 if (!j->alt_syscall_table)
683 return -ENOMEM;
684 j->flags.alt_syscall = 1;
685 return 0;
686}
687
Will Drewryf89aef52011-09-16 16:48:57 -0500688struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400689 size_t available;
690 size_t total;
691 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500692};
693
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800694void marshal_state_init(struct marshal_state *state, char *buf,
695 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400696{
697 state->available = available;
698 state->buf = buf;
699 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500700}
701
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800702void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400703{
704 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500705
Elly Jonese1749eb2011-10-07 13:54:59 -0400706 /* Up to |available| will be written. */
707 if (copy_len) {
708 memcpy(state->buf, src, copy_len);
709 state->buf += copy_len;
710 state->available -= copy_len;
711 }
712 /* |total| will contain the expected length. */
713 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500714}
715
Will Drewry6ac91122011-10-21 16:38:58 -0500716void minijail_marshal_helper(struct marshal_state *state,
717 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400718{
Dylan Reid648b2202015-10-23 00:50:00 -0700719 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800720 size_t i;
721
Elly Jonese1749eb2011-10-07 13:54:59 -0400722 marshal_append(state, (char *)j, sizeof(*j));
723 if (j->user)
724 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800725 if (j->suppl_gid_list) {
726 marshal_append(state, j->suppl_gid_list,
727 j->suppl_gid_count * sizeof(gid_t));
728 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400729 if (j->chrootdir)
730 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800731 if (j->alt_syscall_table) {
732 marshal_append(state, j->alt_syscall_table,
733 strlen(j->alt_syscall_table) + 1);
734 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800735 if (j->flags.seccomp_filter && j->filter_prog) {
736 struct sock_fprog *fp = j->filter_prog;
737 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800738 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400739 }
Dylan Reid648b2202015-10-23 00:50:00 -0700740 for (m = j->mounts_head; m; m = m->next) {
741 marshal_append(state, m->src, strlen(m->src) + 1);
742 marshal_append(state, m->dest, strlen(m->dest) + 1);
743 marshal_append(state, m->type, strlen(m->type) + 1);
744 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400745 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800746 for (i = 0; i < j->cgroup_count; ++i)
747 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500748}
749
Will Drewry6ac91122011-10-21 16:38:58 -0500750size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400751{
752 struct marshal_state state;
753 marshal_state_init(&state, NULL, 0);
754 minijail_marshal_helper(&state, j);
755 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500756}
757
Elly Jonese1749eb2011-10-07 13:54:59 -0400758int minijail_marshal(const struct minijail *j, char *buf, size_t available)
759{
760 struct marshal_state state;
761 marshal_state_init(&state, buf, available);
762 minijail_marshal_helper(&state, j);
763 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500764}
765
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800766/*
767 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400768 * @length Number of bytes to consume
769 * @buf Buffer to consume from
770 * @buflength Size of @buf
771 *
772 * Returns a pointer to the base of the bytes, or NULL for errors.
773 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700774void *consumebytes(size_t length, char **buf, size_t *buflength)
775{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400776 char *p = *buf;
777 if (length > *buflength)
778 return NULL;
779 *buf += length;
780 *buflength -= length;
781 return p;
782}
783
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800784/*
785 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400786 * @buf Buffer to consume
787 * @length Length of buffer
788 *
789 * Returns a pointer to the base of the string, or NULL for errors.
790 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700791char *consumestr(char **buf, size_t *buflength)
792{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400793 size_t len = strnlen(*buf, *buflength);
794 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700795 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400796 return NULL;
797 return consumebytes(len + 1, buf, buflength);
798}
799
Elly Jonese1749eb2011-10-07 13:54:59 -0400800int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
801{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800802 size_t i;
803 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500804 int ret = -EINVAL;
805
Elly Jonese1749eb2011-10-07 13:54:59 -0400806 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500807 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400808 memcpy((void *)j, serialized, sizeof(*j));
809 serialized += sizeof(*j);
810 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500811
Will Drewrybee7ba72011-10-21 20:47:01 -0500812 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700813 j->mounts_head = NULL;
814 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800815 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500816
Elly Jonese1749eb2011-10-07 13:54:59 -0400817 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400818 char *user = consumestr(&serialized, &length);
819 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500820 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400821 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500822 if (!j->user)
823 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400824 }
Will Drewryf89aef52011-09-16 16:48:57 -0500825
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800826 if (j->suppl_gid_list) { /* stale pointer */
827 if (j->suppl_gid_count > NGROUPS_MAX) {
828 goto bad_gid_list;
829 }
830 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
831 void *gid_list_bytes =
832 consumebytes(gid_list_size, &serialized, &length);
833 if (!gid_list_bytes)
834 goto bad_gid_list;
835
836 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
837 if (!j->suppl_gid_list)
838 goto bad_gid_list;
839
840 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
841 }
842
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400843 if (j->chrootdir) { /* stale pointer */
844 char *chrootdir = consumestr(&serialized, &length);
845 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500846 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400847 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500848 if (!j->chrootdir)
849 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400850 }
851
Andrew Brestickereac28942015-11-11 16:04:46 -0800852 if (j->alt_syscall_table) { /* stale pointer */
853 char *alt_syscall_table = consumestr(&serialized, &length);
854 if (!alt_syscall_table)
855 goto bad_syscall_table;
856 j->alt_syscall_table = strdup(alt_syscall_table);
857 if (!j->alt_syscall_table)
858 goto bad_syscall_table;
859 }
860
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800861 if (j->flags.seccomp_filter && j->filter_len > 0) {
862 size_t ninstrs = j->filter_len;
863 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
864 ninstrs > USHRT_MAX)
865 goto bad_filters;
866
867 size_t program_len = ninstrs * sizeof(struct sock_filter);
868 void *program = consumebytes(program_len, &serialized, &length);
869 if (!program)
870 goto bad_filters;
871
872 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800873 if (!j->filter_prog)
874 goto bad_filters;
875
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800876 j->filter_prog->len = ninstrs;
877 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800878 if (!j->filter_prog->filter)
879 goto bad_filter_prog_instrs;
880
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800881 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400882 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400883
Dylan Reid648b2202015-10-23 00:50:00 -0700884 count = j->mounts_count;
885 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400886 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700887 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400888 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700889 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400890 const char *src = consumestr(&serialized, &length);
891 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700892 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400893 dest = consumestr(&serialized, &length);
894 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700895 goto bad_mounts;
896 type = consumestr(&serialized, &length);
897 if (!type)
898 goto bad_mounts;
899 flags = consumebytes(sizeof(*flags), &serialized, &length);
900 if (!flags)
901 goto bad_mounts;
902 if (minijail_mount(j, src, dest, type, *flags))
903 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400904 }
905
Dylan Reid605ce7f2016-01-19 19:21:00 -0800906 count = j->cgroup_count;
907 j->cgroup_count = 0;
908 for (i = 0; i < count; ++i) {
909 char *cgroup = consumestr(&serialized, &length);
910 if (!cgroup)
911 goto bad_cgroups;
912 j->cgroups[i] = strdup(cgroup);
913 if (!j->cgroups[i])
914 goto bad_cgroups;
915 ++j->cgroup_count;
916 }
917
Elly Jonese1749eb2011-10-07 13:54:59 -0400918 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500919
Dylan Reid605ce7f2016-01-19 19:21:00 -0800920bad_cgroups:
921 while (j->mounts_head) {
922 struct mountpoint *m = j->mounts_head;
923 j->mounts_head = j->mounts_head->next;
924 free(m->type);
925 free(m->dest);
926 free(m->src);
927 free(m);
928 }
929 for (i = 0; i < j->cgroup_count; ++i)
930 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700931bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800932 if (j->flags.seccomp_filter && j->filter_len > 0) {
933 free(j->filter_prog->filter);
934 free(j->filter_prog);
935 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800936bad_filter_prog_instrs:
937 if (j->filter_prog)
938 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500939bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800940 if (j->alt_syscall_table)
941 free(j->alt_syscall_table);
942bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500943 if (j->chrootdir)
944 free(j->chrootdir);
945bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800946 if (j->suppl_gid_list)
947 free(j->suppl_gid_list);
948bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500949 if (j->user)
950 free(j->user);
951clear_pointers:
952 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800953 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500954 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800955 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800956 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500957out:
958 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500959}
960
Dylan Reidce5b55e2016-01-13 11:04:16 -0800961static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800962{
963 int fd, ret, len;
964 size_t sz;
965 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800966
967 sz = sizeof(fname);
968 if (j->uidmap) {
969 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700970 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800971 die("failed to write file name of uid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800972 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800973 if (fd < 0)
974 pdie("failed to open '%s'", fname);
975 len = strlen(j->uidmap);
976 if (write(fd, j->uidmap, len) < len)
977 die("failed to set uid_map");
978 close(fd);
979 }
980 if (j->gidmap) {
981 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700982 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800983 die("failed to write file name of gid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800984 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800985 if (fd < 0)
986 pdie("failed to open '%s'", fname);
987 len = strlen(j->gidmap);
988 if (write(fd, j->gidmap, len) < len)
989 die("failed to set gid_map");
990 close(fd);
991 }
Dylan Reidce5b55e2016-01-13 11:04:16 -0800992}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800993
Dylan Reidce5b55e2016-01-13 11:04:16 -0800994static void parent_setup_complete(int *pipe_fds)
995{
996 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800997 close(pipe_fds[1]);
998}
999
Dylan Reidce5b55e2016-01-13 11:04:16 -08001000/*
1001 * wait_for_parent_setup: Called by the child process to wait for any
1002 * further parent-side setup to complete before continuing.
1003 */
1004static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001005{
1006 char buf;
1007
1008 close(pipe_fds[1]);
1009
Dylan Reidce5b55e2016-01-13 11:04:16 -08001010 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001011 if (read(pipe_fds[0], &buf, 1) != 0)
1012 die("failed to sync with parent");
1013 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001014}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001015
Dylan Reidce5b55e2016-01-13 11:04:16 -08001016static void enter_user_namespace(const struct minijail *j)
1017{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001018 if (j->uidmap && setresuid(0, 0, 0))
1019 pdie("setresuid");
1020 if (j->gidmap && setresgid(0, 0, 0))
1021 pdie("setresgid");
1022}
1023
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001024/*
1025 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001026 * @j Minijail these mounts are for
1027 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001028 *
1029 * Returns 0 for success.
1030 */
Dylan Reid648b2202015-10-23 00:50:00 -07001031static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001032{
Dylan Reid648b2202015-10-23 00:50:00 -07001033 int ret;
1034 char *dest;
1035 int remount_ro = 0;
1036
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001037 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001038 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001039 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001040
1041 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001042 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1043 * can't both be specified in the original bind mount.
1044 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001045 */
1046 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1047 remount_ro = 1;
1048 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001049 }
Dylan Reid648b2202015-10-23 00:50:00 -07001050
1051 ret = mount(m->src, dest, m->type, m->flags, NULL);
1052 if (ret)
1053 pdie("mount: %s -> %s", m->src, dest);
1054
1055 if (remount_ro) {
1056 m->flags |= MS_RDONLY;
1057 ret = mount(m->src, dest, NULL,
1058 m->flags | MS_REMOUNT, NULL);
1059 if (ret)
1060 pdie("bind ro: %s -> %s", m->src, dest);
1061 }
1062
Elly Jones51a5b6c2011-10-12 19:09:26 -04001063 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001064 if (m->next)
1065 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001066 return ret;
1067}
1068
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001069int enter_chroot(const struct minijail *j)
1070{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001071 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001072
1073 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001074 return ret;
1075
1076 if (chroot(j->chrootdir))
1077 return -errno;
1078
1079 if (chdir("/"))
1080 return -errno;
1081
1082 return 0;
1083}
1084
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001085int enter_pivot_root(const struct minijail *j)
1086{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001087 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001088
1089 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001090 return ret;
1091
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001092 /*
1093 * Keep the fd for both old and new root.
1094 * It will be used in fchdir later.
1095 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001096 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001097 if (oldroot < 0)
1098 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001099 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001100 if (newroot < 0)
1101 pdie("failed to open %s for fchdir", j->chrootdir);
1102
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001103 /*
1104 * To ensure chrootdir is the root of a file system,
1105 * do a self bind mount.
1106 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001107 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1108 pdie("failed to bind mount '%s'", j->chrootdir);
1109 if (chdir(j->chrootdir))
1110 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001111 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001112 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001113
1114 /*
1115 * Now the old root is mounted on top of the new root. Use fchdir to
1116 * change to the old root and unmount it.
1117 */
1118 if (fchdir(oldroot))
1119 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001120 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001121 if (umount2(".", MNT_DETACH))
1122 pdie("umount(/)");
1123 /* Change back to the new root. */
1124 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001125 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001126 if (close(oldroot))
1127 return -errno;
1128 if (close(newroot))
1129 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001130 if (chroot("/"))
1131 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001132 /* Set correct CWD for getcwd(3). */
1133 if (chdir("/"))
1134 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001135
1136 return 0;
1137}
1138
Lee Campbell11af0622014-05-22 12:36:04 -07001139int mount_tmp(void)
1140{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001141 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001142}
1143
Dylan Reid791f5772015-09-14 20:02:42 -07001144int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001145{
1146 const char *kProcPath = "/proc";
1147 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001148 /*
1149 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001150 * /proc in our namespace, which means using MS_REMOUNT here would
1151 * mutate our parent's mount as well, even though we're in a VFS
1152 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001153 * and make our own. However, if we are in a new user namespace, /proc
1154 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001155 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001156 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001157 return -errno;
1158 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1159 return -errno;
1160 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001161}
1162
Dylan Reid605ce7f2016-01-19 19:21:00 -08001163static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001164{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001165 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001166
1167 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001168 pdie("failed to open '%s'", path);
1169 if (fprintf(fp, "%d\n", (int)pid) < 0)
1170 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001171 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001172 pdie("fclose(%s)", path);
1173}
1174
1175static void write_pid_file(const struct minijail *j)
1176{
1177 write_pid_to_path(j->initpid, j->pid_file_path);
1178}
1179
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001180static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001181{
1182 size_t i;
1183
1184 for (i = 0; i < j->cgroup_count; ++i)
1185 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001186}
1187
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001188void drop_ugid(const struct minijail *j)
1189{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001190 if (j->flags.usergroups && j->flags.suppl_gids) {
1191 die("tried to inherit *and* set supplementary groups;"
1192 " can only do one");
1193 }
1194
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001195 if (j->flags.usergroups) {
1196 if (initgroups(j->user, j->usergid))
1197 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001198 } else if (j->flags.suppl_gids) {
1199 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1200 pdie("setgroups");
1201 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001202 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001203 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001204 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001205 * users.
1206 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001207 if ((j->uid || j->gid) && setgroups(0, NULL))
1208 pdie("setgroups");
1209 }
1210
1211 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1212 pdie("setresgid");
1213
1214 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1215 pdie("setresuid");
1216}
1217
Mike Frysinger3adfef72013-05-09 17:19:08 -04001218/*
1219 * We specifically do not use cap_valid() as that only tells us the last
1220 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001221 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001222 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001223 * kernel).
1224 * Normally, we suck up the answer via /proc. On Android, not all processes are
1225 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1226 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001227 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001228static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001229{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001230 unsigned int last_valid_cap = 0;
1231 if (is_android()) {
1232 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1233 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001234
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001235 /* |last_valid_cap| will be the first failing value. */
1236 if (last_valid_cap > 0) {
1237 last_valid_cap--;
1238 }
1239 } else {
1240 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1241 FILE *fp = fopen(cap_file, "re");
1242 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1243 pdie("fscanf(%s)", cap_file);
1244 fclose(fp);
1245 }
Dylan Reidf682d472015-09-17 21:39:07 -07001246 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001247}
1248
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001249static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1250{
1251 const uint64_t one = 1;
1252 unsigned int i;
1253 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1254 if (keep_mask & (one << i))
1255 continue;
1256 if (prctl(PR_CAPBSET_DROP, i))
1257 pdie("could not drop capability from bounding set");
1258 }
1259}
1260
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001261void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001262{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001263 if (!j->flags.use_caps)
1264 return;
1265
Elly Jonese1749eb2011-10-07 13:54:59 -04001266 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001267 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001268 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001269 unsigned int i;
1270 if (!caps)
1271 die("can't get process caps");
1272 if (cap_clear_flag(caps, CAP_INHERITABLE))
1273 die("can't clear inheritable caps");
1274 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1275 die("can't clear effective caps");
1276 if (cap_clear_flag(caps, CAP_PERMITTED))
1277 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001278 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001279 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001280 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001281 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001282 flag[0] = i;
1283 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001284 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001285 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001286 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001287 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001288 die("can't add inheritable cap");
1289 }
1290 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001291 die("can't apply initial cleaned capset");
1292
1293 /*
1294 * Instead of dropping bounding set first, do it here in case
1295 * the caller had a more permissive bounding set which could
1296 * have been used above to raise a capability that wasn't already
1297 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1298 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001299 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001300
1301 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001302 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001303 flag[0] = CAP_SETPCAP;
1304 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1305 die("can't clear effective cap");
1306 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1307 die("can't clear permitted cap");
1308 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1309 die("can't clear inheritable cap");
1310 }
1311
1312 if (cap_set_proc(caps))
1313 die("can't apply final cleaned capset");
1314
1315 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001316}
1317
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001318void set_seccomp_filter(const struct minijail *j)
1319{
1320 /*
1321 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1322 * in the kernel source tree for an explanation of the parameters.
1323 */
1324 if (j->flags.no_new_privs) {
1325 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1326 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1327 }
1328
1329 /*
1330 * If we're logging seccomp filter failures,
1331 * install the SIGSYS handler first.
1332 */
1333 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1334 if (install_sigsys_handler())
1335 pdie("install SIGSYS handler");
1336 warn("logging seccomp filter failures");
1337 }
1338
1339 /*
1340 * Install the syscall filter.
1341 */
1342 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001343 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1344 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001345 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001346 warn("seccomp not supported");
1347 return;
1348 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001349 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001350 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001351 }
1352}
1353
Will Drewry6ac91122011-10-21 16:38:58 -05001354void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001355{
Dylan Reidf682d472015-09-17 21:39:07 -07001356 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001357 * If we're dropping caps, get the last valid cap from /proc now,
1358 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001359 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001360 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001361 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001362 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001363
Elly Jonese1749eb2011-10-07 13:54:59 -04001364 if (j->flags.pids)
1365 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001366 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001367
Elly Jonese1749eb2011-10-07 13:54:59 -04001368 if (j->flags.usergroups && !j->user)
1369 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001370
Elly Jonesdd3e8512012-01-23 15:13:38 -05001371 /*
1372 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001373 * so we don't even try. If any of our operations fail, we abort() the
1374 * entire process.
1375 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001376 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1377 pdie("setns(CLONE_NEWNS)");
1378
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001379 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001380 if (unshare(CLONE_NEWNS))
1381 pdie("unshare(vfs)");
1382 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001383 * Unless asked not to, remount all filesystems as private.
1384 * If they are shared, new bind mounts will creep out of our
1385 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001386 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1387 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001388 if (!j->flags.skip_remount_private) {
1389 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1390 pdie("mount(/, private)");
1391 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001392 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001393
Dylan Reidf7942472015-11-18 17:55:26 -08001394 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1395 pdie("unshare(ipc)");
1396 }
1397
Dylan Reid1102f5a2015-09-15 11:52:20 -07001398 if (j->flags.enter_net) {
1399 if (setns(j->netns_fd, CLONE_NEWNET))
1400 pdie("setns(CLONE_NEWNET)");
1401 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001402 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001403 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001404
Elly Jones51a5b6c2011-10-12 19:09:26 -04001405 if (j->flags.chroot && enter_chroot(j))
1406 pdie("chroot");
1407
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001408 if (j->flags.pivot_root && enter_pivot_root(j))
1409 pdie("pivot_root");
1410
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001411 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001412 pdie("mount_tmp");
1413
Dylan Reid791f5772015-09-14 20:02:42 -07001414 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001415 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001416
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001417 /*
1418 * If we're only dropping capabilities from the bounding set, but not
1419 * from the thread's (permitted|inheritable|effective) sets, do it now.
1420 */
1421 if (j->flags.capbset_drop) {
1422 drop_capbset(j->cap_bset, last_valid_cap);
1423 }
1424
1425 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001426 /*
1427 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001428 * capability to change uids, our attempt to use setuid()
1429 * below will fail. Hang on to root caps across setuid(), then
1430 * lock securebits.
1431 */
1432 if (prctl(PR_SET_KEEPCAPS, 1))
1433 pdie("prctl(PR_SET_KEEPCAPS)");
1434 if (prctl
1435 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1436 pdie("prctl(PR_SET_SECUREBITS)");
1437 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001438
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001439 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001440 /*
1441 * If we're setting no_new_privs, we can drop privileges
1442 * before setting seccomp filter. This way filter policies
1443 * don't need to allow privilege-dropping syscalls.
1444 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001445 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001446 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001447 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001448 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001449 /*
1450 * If we're not setting no_new_privs,
1451 * we need to set seccomp filter *before* dropping privileges.
1452 * WARNING: this means that filter policies *must* allow
1453 * setgroups()/setresgid()/setresuid() for dropping root and
1454 * capget()/capset()/prctl() for dropping caps.
1455 */
1456 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001457 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001458 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001459 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001460
Elly Jonesdd3e8512012-01-23 15:13:38 -05001461 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001462 * Select the specified alternate syscall table. The table must not
1463 * block prctl(2) if we're using seccomp as well.
1464 */
1465 if (j->flags.alt_syscall) {
1466 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1467 pdie("prctl(PR_ALT_SYSCALL)");
1468 }
1469
1470 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001471 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001472 * privilege-dropping syscalls :)
1473 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001474 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001475 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001476 warn("seccomp not supported");
1477 return;
1478 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001479 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001480 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001481}
1482
Will Drewry6ac91122011-10-21 16:38:58 -05001483/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001484static int init_exitstatus = 0;
1485
Will Drewry6ac91122011-10-21 16:38:58 -05001486void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001487{
1488 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001489}
1490
Will Drewry6ac91122011-10-21 16:38:58 -05001491int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001492{
1493 pid_t pid;
1494 int status;
1495 /* so that we exit with the right status */
1496 signal(SIGTERM, init_term);
1497 /* TODO(wad) self jail with seccomp_filters here. */
1498 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001499 /*
1500 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001501 * left inside our pid namespace or we get a signal.
1502 */
1503 if (pid == rootpid)
1504 init_exitstatus = status;
1505 }
1506 if (!WIFEXITED(init_exitstatus))
1507 _exit(MINIJAIL_ERR_INIT);
1508 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001509}
1510
Will Drewry6ac91122011-10-21 16:38:58 -05001511int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001512{
1513 size_t sz = 0;
1514 size_t bytes = read(fd, &sz, sizeof(sz));
1515 char *buf;
1516 int r;
1517 if (sizeof(sz) != bytes)
1518 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001519 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001520 return -E2BIG;
1521 buf = malloc(sz);
1522 if (!buf)
1523 return -ENOMEM;
1524 bytes = read(fd, buf, sz);
1525 if (bytes != sz) {
1526 free(buf);
1527 return -EINVAL;
1528 }
1529 r = minijail_unmarshal(j, buf, sz);
1530 free(buf);
1531 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001532}
1533
Will Drewry6ac91122011-10-21 16:38:58 -05001534int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001535{
1536 char *buf;
1537 size_t sz = minijail_size(j);
1538 ssize_t written;
1539 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001540
Elly Jonese1749eb2011-10-07 13:54:59 -04001541 if (!sz)
1542 return -EINVAL;
1543 buf = malloc(sz);
1544 r = minijail_marshal(j, buf, sz);
1545 if (r) {
1546 free(buf);
1547 return r;
1548 }
1549 /* Sends [size][minijail]. */
1550 written = write(fd, &sz, sizeof(sz));
1551 if (written != sizeof(sz)) {
1552 free(buf);
1553 return -EFAULT;
1554 }
1555 written = write(fd, buf, sz);
1556 if (written < 0 || (size_t) written != sz) {
1557 free(buf);
1558 return -EFAULT;
1559 }
1560 free(buf);
1561 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001562}
Elly Jonescd7a9042011-07-22 13:56:51 -04001563
Will Drewry6ac91122011-10-21 16:38:58 -05001564int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001565{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001566#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001567 /* Don't use LDPRELOAD on Brillo. */
1568 return 0;
1569#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001570 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1571 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1572 if (!newenv)
1573 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001574
Elly Jonese1749eb2011-10-07 13:54:59 -04001575 /* Only insert a separating space if we have something to separate... */
1576 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1577 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001578
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001579 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001580 setenv(kLdPreloadEnvVar, newenv, 1);
1581 free(newenv);
1582 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001583#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001584}
1585
Will Drewry6ac91122011-10-21 16:38:58 -05001586int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001587{
1588 int r = pipe(fds);
1589 char fd_buf[11];
1590 if (r)
1591 return r;
1592 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1593 if (r <= 0)
1594 return -EINVAL;
1595 setenv(kFdEnvVar, fd_buf, 1);
1596 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001597}
1598
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001599int setup_pipe_end(int fds[2], size_t index)
1600{
1601 if (index > 1)
1602 return -1;
1603
1604 close(fds[1 - index]);
1605 return fds[index];
1606}
1607
1608int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1609{
1610 if (index > 1)
1611 return -1;
1612
1613 close(fds[1 - index]);
1614 /* dup2(2) the corresponding end of the pipe into |fd|. */
1615 return dup2(fds[index], fd);
1616}
1617
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001618int minijail_run_internal(struct minijail *j, const char *filename,
1619 char *const argv[], pid_t *pchild_pid,
1620 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1621 int use_preload);
1622
Will Drewry6ac91122011-10-21 16:38:58 -05001623int API minijail_run(struct minijail *j, const char *filename,
1624 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001625{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001626 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1627 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001628}
1629
1630int API minijail_run_pid(struct minijail *j, const char *filename,
1631 char *const argv[], pid_t *pchild_pid)
1632{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001633 return minijail_run_internal(j, filename, argv, pchild_pid,
1634 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001635}
1636
1637int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001638 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001639{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001640 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1641 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001642}
1643
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001644int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001645 char *const argv[], pid_t *pchild_pid,
1646 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001647{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001648 return minijail_run_internal(j, filename, argv, pchild_pid,
1649 pstdin_fd, pstdout_fd, pstderr_fd, true);
1650}
1651
1652int API minijail_run_no_preload(struct minijail *j, const char *filename,
1653 char *const argv[])
1654{
1655 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1656 false);
1657}
1658
Samuel Tan63187f42015-10-16 13:01:53 -07001659int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001660 const char *filename,
1661 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001662 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001663 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001664 int *pstderr_fd)
1665{
Samuel Tan63187f42015-10-16 13:01:53 -07001666 return minijail_run_internal(j, filename, argv, pchild_pid,
1667 pstdin_fd, pstdout_fd, pstderr_fd, false);
1668}
1669
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001670int minijail_run_internal(struct minijail *j, const char *filename,
1671 char *const argv[], pid_t *pchild_pid,
1672 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1673 int use_preload)
1674{
Elly Jonese1749eb2011-10-07 13:54:59 -04001675 char *oldenv, *oldenv_copy = NULL;
1676 pid_t child_pid;
1677 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001678 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001679 int stdout_fds[2];
1680 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001681 int child_sync_pipe_fds[2];
1682 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001683 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001684 /* We need to remember this across the minijail_preexec() call. */
1685 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001686 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001687
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001688 if (use_preload) {
1689 oldenv = getenv(kLdPreloadEnvVar);
1690 if (oldenv) {
1691 oldenv_copy = strdup(oldenv);
1692 if (!oldenv_copy)
1693 return -ENOMEM;
1694 }
1695
1696 if (setup_preload())
1697 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001698 }
Will Drewryf89aef52011-09-16 16:48:57 -05001699
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001700 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001701 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001702 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001703 "LD_PRELOAD");
1704 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001705
Elly Jonesdd3e8512012-01-23 15:13:38 -05001706 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001707 * Make the process group ID of this process equal to its PID, so that
1708 * both the Minijail process and the jailed process can be killed
1709 * together.
1710 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1711 * the process is already a process group leader.
1712 */
1713 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1714 if (errno != EPERM) {
1715 pdie("setpgid(0, 0)");
1716 }
1717 }
1718
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001719 if (use_preload) {
1720 /*
1721 * Before we fork(2) and execve(2) the child process, we need
1722 * to open a pipe(2) to send the minijail configuration over.
1723 */
1724 if (setup_pipe(pipe_fds))
1725 return -EFAULT;
1726 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001727
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001728 /*
1729 * If we want to write to the child process' standard input,
1730 * create the pipe(2) now.
1731 */
1732 if (pstdin_fd) {
1733 if (pipe(stdin_fds))
1734 return -EFAULT;
1735 }
1736
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001737 /*
1738 * If we want to read from the child process' standard output,
1739 * create the pipe(2) now.
1740 */
1741 if (pstdout_fd) {
1742 if (pipe(stdout_fds))
1743 return -EFAULT;
1744 }
1745
1746 /*
1747 * If we want to read from the child process' standard error,
1748 * create the pipe(2) now.
1749 */
1750 if (pstderr_fd) {
1751 if (pipe(stderr_fds))
1752 return -EFAULT;
1753 }
1754
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001755 /*
1756 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001757 * or if we need to add the child process to cgroups, create the pipe(2)
1758 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001759 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001760 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001761 sync_child = 1;
1762 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001763 return -EFAULT;
1764 }
1765
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001766 /*
1767 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001768 *
1769 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1770 *
1771 * In multithreaded programs, there are a bunch of locks inside libc,
1772 * some of which may be held by other threads at the time that we call
1773 * minijail_run_pid(). If we call fork(), glibc does its level best to
1774 * ensure that we hold all of these locks before it calls clone()
1775 * internally and drop them after clone() returns, but when we call
1776 * sys_clone(2) directly, all that gets bypassed and we end up with a
1777 * child address space where some of libc's important locks are held by
1778 * other threads (which did not get cloned, and hence will never release
1779 * those locks). This is okay so long as we call exec() immediately
1780 * after, but a bunch of seemingly-innocent libc functions like setenv()
1781 * take locks.
1782 *
1783 * Hence, only call sys_clone() if we need to, in order to get at pid
1784 * namespacing. If we follow this path, the child's address space might
1785 * have broken locks; you may only call functions that do not acquire
1786 * any locks.
1787 *
1788 * Unfortunately, fork() acquires every lock it can get its hands on, as
1789 * previously detailed, so this function is highly likely to deadlock
1790 * later on (see "deadlock here") if we're multithreaded.
1791 *
1792 * We might hack around this by having the clone()d child (init of the
1793 * pid namespace) return directly, rather than leaving the clone()d
1794 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001795 * its fork()ed child return in turn), but that process would be
1796 * crippled with its libc locks potentially broken. We might try
1797 * fork()ing in the parent before we clone() to ensure that we own all
1798 * the locks, but then we have to have the forked child hanging around
1799 * consuming resources (and possibly having file descriptors / shared
1800 * memory regions / etc attached). We'd need to keep the child around to
1801 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001802 *
1803 * TODO(ellyjones): figure out if the "forked child hanging around"
1804 * problem is fixable or not. It would be nice if we worked in this
1805 * case.
1806 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001807 if (pid_namespace) {
1808 int clone_flags = CLONE_NEWPID | SIGCHLD;
1809 if (j->flags.userns)
1810 clone_flags |= CLONE_NEWUSER;
1811 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001812 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001813 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001814 }
Elly Jones761b7412012-06-13 15:49:52 -04001815
Elly Jonese1749eb2011-10-07 13:54:59 -04001816 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001817 if (use_preload) {
1818 free(oldenv_copy);
1819 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001820 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001821 }
Will Drewryf89aef52011-09-16 16:48:57 -05001822
Elly Jonese1749eb2011-10-07 13:54:59 -04001823 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001824 if (use_preload) {
1825 /* Restore parent's LD_PRELOAD. */
1826 if (oldenv_copy) {
1827 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1828 free(oldenv_copy);
1829 } else {
1830 unsetenv(kLdPreloadEnvVar);
1831 }
1832 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001833 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001834
Elly Jonese1749eb2011-10-07 13:54:59 -04001835 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001836
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001837 if (j->flags.pid_file)
1838 write_pid_file(j);
1839
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001840 if (j->flags.cgroups)
1841 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001842
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001843 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001844 write_ugid_mappings(j);
1845
1846 if (sync_child)
1847 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001848
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001849 if (use_preload) {
1850 /* Send marshalled minijail. */
1851 close(pipe_fds[0]); /* read endpoint */
1852 ret = minijail_to_fd(j, pipe_fds[1]);
1853 close(pipe_fds[1]); /* write endpoint */
1854 if (ret) {
1855 kill(j->initpid, SIGKILL);
1856 die("failed to send marshalled minijail");
1857 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001858 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001859
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001860 if (pchild_pid)
1861 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001862
1863 /*
1864 * If we want to write to the child process' standard input,
1865 * set up the write end of the pipe.
1866 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001867 if (pstdin_fd)
1868 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001869 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001870
1871 /*
1872 * If we want to read from the child process' standard output,
1873 * set up the read end of the pipe.
1874 */
1875 if (pstdout_fd)
1876 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001877 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001878
1879 /*
1880 * If we want to read from the child process' standard error,
1881 * set up the read end of the pipe.
1882 */
1883 if (pstderr_fd)
1884 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001885 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001886
Elly Jonese1749eb2011-10-07 13:54:59 -04001887 return 0;
1888 }
1889 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001890
Peter Qiu2860c462015-12-16 15:13:06 -08001891 if (j->flags.reset_signal_mask) {
1892 sigset_t signal_mask;
1893 if (sigemptyset(&signal_mask) != 0)
1894 pdie("sigemptyset failed");
1895 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1896 pdie("sigprocmask failed");
1897 }
1898
Dylan Reidce5b55e2016-01-13 11:04:16 -08001899 if (sync_child)
1900 wait_for_parent_setup(child_sync_pipe_fds);
1901
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001902 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001903 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001904
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001905 /*
1906 * If we want to write to the jailed process' standard input,
1907 * set up the read end of the pipe.
1908 */
1909 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001910 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1911 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001912 die("failed to set up stdin pipe");
1913 }
1914
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001915 /*
1916 * If we want to read from the jailed process' standard output,
1917 * set up the write end of the pipe.
1918 */
1919 if (pstdout_fd) {
1920 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1921 STDOUT_FILENO) < 0)
1922 die("failed to set up stdout pipe");
1923 }
1924
1925 /*
1926 * If we want to read from the jailed process' standard error,
1927 * set up the write end of the pipe.
1928 */
1929 if (pstderr_fd) {
1930 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1931 STDERR_FILENO) < 0)
1932 die("failed to set up stderr pipe");
1933 }
1934
Dylan Reid791f5772015-09-14 20:02:42 -07001935 /* If running an init program, let it decide when/how to mount /proc. */
1936 if (pid_namespace && !do_init)
1937 j->flags.remount_proc_ro = 0;
1938
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001939 if (use_preload) {
1940 /* Strip out flags that cannot be inherited across execve(2). */
1941 minijail_preexec(j);
1942 } else {
1943 j->flags.pids = 0;
1944 }
1945 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001946 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001947
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001948 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001949 /*
1950 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001951 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001952 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001953 * a child to actually run the program. If |do_init == 0|, we
1954 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001955 *
1956 * If we're multithreaded, we'll probably deadlock here. See
1957 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001958 */
1959 child_pid = fork();
1960 if (child_pid < 0)
1961 _exit(child_pid);
1962 else if (child_pid > 0)
1963 init(child_pid); /* never returns */
1964 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001965
Elly Jonesdd3e8512012-01-23 15:13:38 -05001966 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001967 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001968 * calling process
1969 * -> execve()-ing process
1970 * If we are:
1971 * calling process
1972 * -> init()-ing process
1973 * -> execve()-ing process
1974 */
1975 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001976}
1977
Will Drewry6ac91122011-10-21 16:38:58 -05001978int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001979{
1980 int st;
1981 if (kill(j->initpid, SIGTERM))
1982 return -errno;
1983 if (waitpid(j->initpid, &st, 0) < 0)
1984 return -errno;
1985 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001986}
1987
Will Drewry6ac91122011-10-21 16:38:58 -05001988int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001989{
1990 int st;
1991 if (waitpid(j->initpid, &st, 0) < 0)
1992 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001993
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001994 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001995 int error_status = st;
1996 if (WIFSIGNALED(st)) {
1997 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001998 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001999 j->initpid, signum);
2000 /*
2001 * We return MINIJAIL_ERR_JAIL if the process received
2002 * SIGSYS, which happens when a syscall is blocked by
2003 * seccomp filters.
2004 * If not, we do what bash(1) does:
2005 * $? = 128 + signum
2006 */
2007 if (signum == SIGSYS) {
2008 error_status = MINIJAIL_ERR_JAIL;
2009 } else {
2010 error_status = 128 + signum;
2011 }
2012 }
2013 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002014 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002015
2016 int exit_status = WEXITSTATUS(st);
2017 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002018 info("child process %d exited with status %d",
2019 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002020
2021 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002022}
2023
Will Drewry6ac91122011-10-21 16:38:58 -05002024void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002025{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002026 size_t i;
2027
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002028 if (j->flags.seccomp_filter && j->filter_prog) {
2029 free(j->filter_prog->filter);
2030 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002031 }
Dylan Reid648b2202015-10-23 00:50:00 -07002032 while (j->mounts_head) {
2033 struct mountpoint *m = j->mounts_head;
2034 j->mounts_head = j->mounts_head->next;
2035 free(m->type);
2036 free(m->dest);
2037 free(m->src);
2038 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002039 }
Dylan Reid648b2202015-10-23 00:50:00 -07002040 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002041 if (j->user)
2042 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002043 if (j->suppl_gid_list)
2044 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002045 if (j->chrootdir)
2046 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002047 if (j->alt_syscall_table)
2048 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002049 for (i = 0; i < j->cgroup_count; ++i)
2050 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002051 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002052}