blob: 118e61f1a97f74e859cfc21efc93d66973fbf77e [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;
Dylan Reid791f5772015-09-14 20:02:42 -0700153 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700154 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800155 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800156 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800157 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700158}
159
160/*
161 * Strip out flags meant for the child.
162 * We keep things that are inherited across execve(2).
163 */
164void minijail_preexec(struct minijail *j)
165{
166 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700167 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700168 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800169 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700170 if (j->user)
171 free(j->user);
172 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800173 if (j->suppl_gid_list)
174 free(j->suppl_gid_list);
175 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700176 memset(&j->flags, 0, sizeof(j->flags));
177 /* Now restore anything we meant to keep. */
178 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700179 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700180 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800181 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700182 /* Note, |pids| will already have been used before this call. */
183}
184
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800185/* Returns true if the kernel version is less than 3.8. */
186int seccomp_kernel_support_not_required()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800187{
188 int major, minor;
189 struct utsname uts;
190 return (uname(&uts) != -1 &&
191 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
192 ((major < 3) || ((major == 3) && (minor < 8))));
193}
194
Jorge Lucangeli Obes272e3ab2016-01-12 21:18:59 -0800195/* Allow seccomp soft-fail on Android devices with kernel version < 3.8. */
196int can_softfail()
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800197{
198#if SECCOMP_SOFTFAIL
199 if (is_android()) {
200 if (seccomp_kernel_support_not_required())
201 return 1;
202 else
203 return 0;
204 } else {
205 return 1;
206 }
207#endif
208 return 0;
209}
210
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700211/* Minijail API. */
212
Will Drewry6ac91122011-10-21 16:38:58 -0500213struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400214{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400215 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400216}
217
Will Drewry6ac91122011-10-21 16:38:58 -0500218void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400219{
220 if (uid == 0)
221 die("useless change to uid 0");
222 j->uid = uid;
223 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400224}
225
Will Drewry6ac91122011-10-21 16:38:58 -0500226void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400227{
228 if (gid == 0)
229 die("useless change to gid 0");
230 j->gid = gid;
231 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400232}
233
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800234void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
235 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800236{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800237 size_t i;
238
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800239 if (j->flags.usergroups)
240 die("cannot inherit *and* set supplementary groups");
241
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800242 if (size == 0) {
243 /* Clear supplementary groups. */
244 j->suppl_gid_list = NULL;
245 j->suppl_gid_count = 0;
246 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800247 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800248 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800249
250 /* Copy the gid_t array. */
251 j->suppl_gid_list = calloc(size, sizeof(gid_t));
252 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800253 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800254 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800255 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800256 j->suppl_gid_list[i] = list[i];
257 }
258 j->suppl_gid_count = size;
259 j->flags.suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800260}
261
Will Drewry6ac91122011-10-21 16:38:58 -0500262int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400263{
264 char *buf = NULL;
265 struct passwd pw;
266 struct passwd *ppw = NULL;
267 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
268 if (sz == -1)
269 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400270
Elly Jonesdd3e8512012-01-23 15:13:38 -0500271 /*
272 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400273 * the maximum needed size of the buffer, so we don't have to search.
274 */
275 buf = malloc(sz);
276 if (!buf)
277 return -ENOMEM;
278 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500279 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800280 * We're safe to free the buffer here. The strings inside |pw| point
281 * inside |buf|, but we don't use any of them; this leaves the pointers
282 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3) succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500283 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400284 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700285 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400286 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700287 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400288 minijail_change_uid(j, ppw->pw_uid);
289 j->user = strdup(user);
290 if (!j->user)
291 return -ENOMEM;
292 j->usergid = ppw->pw_gid;
293 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400294}
295
Will Drewry6ac91122011-10-21 16:38:58 -0500296int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400297{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700298 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700299 struct group gr;
300 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400301 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
302 if (sz == -1)
303 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400304
Elly Jonesdd3e8512012-01-23 15:13:38 -0500305 /*
306 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400307 * the maximum needed size of the buffer, so we don't have to search.
308 */
309 buf = malloc(sz);
310 if (!buf)
311 return -ENOMEM;
312 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500313 /*
314 * We're safe to free the buffer here. The strings inside gr point
315 * inside buf, but we don't use any of them; this leaves the pointers
316 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
317 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400318 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700319 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400320 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700321 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400322 minijail_change_gid(j, pgr->gr_gid);
323 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400324}
325
Will Drewry6ac91122011-10-21 16:38:58 -0500326void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400327{
328 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400329}
330
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700331void API minijail_no_new_privs(struct minijail *j)
332{
333 j->flags.no_new_privs = 1;
334}
335
Will Drewry6ac91122011-10-21 16:38:58 -0500336void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400337{
338 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500339}
340
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700341void API minijail_log_seccomp_filter_failures(struct minijail *j)
342{
343 j->flags.log_seccomp_filter = 1;
344}
345
Will Drewry6ac91122011-10-21 16:38:58 -0500346void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400347{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800348 /*
349 * 'minijail_use_caps' configures a runtime-capabilities-only
350 * environment, including a bounding set matching the thread's runtime
351 * (permitted|inheritable|effective) sets.
352 * Therefore, it will override any existing bounding set configurations
353 * since the latter would allow gaining extra runtime capabilities from
354 * file capabilities.
355 */
356 if (j->flags.capbset_drop) {
357 warn("overriding bounding set configuration");
358 j->cap_bset = 0;
359 j->flags.capbset_drop = 0;
360 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400361 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800362 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400363}
364
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800365void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
366{
367 if (j->flags.use_caps) {
368 /*
369 * 'minijail_use_caps' will have already configured a capability
370 * bounding set matching the (permitted|inheritable|effective)
371 * sets. Abort if the user tries to configure a separate
372 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
373 * are mutually exclusive.
374 */
375 die("runtime capabilities already configured, can't drop "
376 "bounding set separately");
377 }
378 j->cap_bset = capmask;
379 j->flags.capbset_drop = 1;
380}
381
382void API minijail_reset_signal_mask(struct minijail *j)
383{
Peter Qiu2860c462015-12-16 15:13:06 -0800384 j->flags.reset_signal_mask = 1;
385}
386
Will Drewry6ac91122011-10-21 16:38:58 -0500387void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400388{
389 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400390}
391
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700392void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
393{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800394 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700395 if (ns_fd < 0) {
396 pdie("failed to open namespace '%s'", ns_path);
397 }
398 j->mountns_fd = ns_fd;
399 j->flags.enter_vfs = 1;
400}
401
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800402void API minijail_skip_remount_private(struct minijail *j)
403{
404 j->flags.skip_remount_private = 1;
405}
406
Will Drewry6ac91122011-10-21 16:38:58 -0500407void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400408{
Elly Jonese58176c2012-01-23 11:46:17 -0500409 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700410 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400411 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800412 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400413}
414
Dylan Reidf7942472015-11-18 17:55:26 -0800415void API minijail_namespace_ipc(struct minijail *j)
416{
417 j->flags.ipc = 1;
418}
419
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400420void API minijail_namespace_net(struct minijail *j)
421{
422 j->flags.net = 1;
423}
424
Dylan Reid1102f5a2015-09-15 11:52:20 -0700425void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
426{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800427 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700428 if (ns_fd < 0) {
429 pdie("failed to open namespace '%s'", ns_path);
430 }
431 j->netns_fd = ns_fd;
432 j->flags.enter_net = 1;
433}
434
Dylan Reid791f5772015-09-14 20:02:42 -0700435void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400436{
437 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700438 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400439}
440
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800441void API minijail_namespace_user(struct minijail *j)
442{
443 j->flags.userns = 1;
444}
445
446int API minijail_uidmap(struct minijail *j, const char *uidmap)
447{
448 j->uidmap = strdup(uidmap);
449 if (!j->uidmap)
450 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800451 char *ch;
452 for (ch = j->uidmap; *ch; ch++) {
453 if (*ch == ',')
454 *ch = '\n';
455 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800456 return 0;
457}
458
459int API minijail_gidmap(struct minijail *j, const char *gidmap)
460{
461 j->gidmap = strdup(gidmap);
462 if (!j->gidmap)
463 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800464 char *ch;
465 for (ch = j->gidmap; *ch; ch++) {
466 if (*ch == ',')
467 *ch = '\n';
468 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800469 return 0;
470}
471
Will Drewry6ac91122011-10-21 16:38:58 -0500472void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400473{
474 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400475}
476
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800477void API minijail_run_as_init(struct minijail *j)
478{
479 /*
480 * Since the jailed program will become 'init' in the new PID namespace,
481 * Minijail does not need to fork an 'init' process.
482 */
483 j->flags.do_init = 0;
484}
485
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700486int API minijail_enter_chroot(struct minijail *j, const char *dir)
487{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400488 if (j->chrootdir)
489 return -EINVAL;
490 j->chrootdir = strdup(dir);
491 if (!j->chrootdir)
492 return -ENOMEM;
493 j->flags.chroot = 1;
494 return 0;
495}
496
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800497int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
498{
499 if (j->chrootdir)
500 return -EINVAL;
501 j->chrootdir = strdup(dir);
502 if (!j->chrootdir)
503 return -ENOMEM;
504 j->flags.pivot_root = 1;
505 return 0;
506}
507
Dylan Reida14e08d2015-10-22 21:05:29 -0700508static char *append_external_path(const char *external_path,
509 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700510{
Dylan Reida14e08d2015-10-22 21:05:29 -0700511 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700512 size_t pathlen;
513
Dylan Reid08946cc2015-09-16 19:10:57 -0700514 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700515 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
516 path = malloc(pathlen);
517 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700518
Dylan Reida14e08d2015-10-22 21:05:29 -0700519 return path;
520}
521
522char API *minijail_get_original_path(struct minijail *j,
523 const char *path_inside_chroot)
524{
Dylan Reid648b2202015-10-23 00:50:00 -0700525 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700526
Dylan Reid648b2202015-10-23 00:50:00 -0700527 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700528 while (b) {
529 /*
530 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700531 * mount, then the original path is exactly the source of
532 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700533 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700534 * mount source = /some/path/exe, mount dest =
535 * /chroot/path/exe Then when getting the original path of
536 * "/chroot/path/exe", the source of that mount,
537 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700538 */
539 if (!strcmp(b->dest, path_inside_chroot))
540 return strdup(b->src);
541
542 /*
543 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700544 * mount, take the suffix of the chroot path relative to the
545 * mount destination path, and append it to the mount source
546 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700547 */
548 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
549 const char *relative_path =
550 path_inside_chroot + strlen(b->dest);
551 return append_external_path(b->src, relative_path);
552 }
553 b = b->next;
554 }
555
556 /* If there is a chroot path, append |path_inside_chroot| to that. */
557 if (j->chrootdir)
558 return append_external_path(j->chrootdir, path_inside_chroot);
559
560 /* No chroot, so the path outside is the same as it is inside. */
561 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700562}
563
Lee Campbell11af0622014-05-22 12:36:04 -0700564void API minijail_mount_tmp(struct minijail *j)
565{
566 j->flags.mount_tmp = 1;
567}
568
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800569int API minijail_write_pid_file(struct minijail *j, const char *path)
570{
571 j->pid_file_path = strdup(path);
572 if (!j->pid_file_path)
573 return -ENOMEM;
574 j->flags.pid_file = 1;
575 return 0;
576}
577
Dylan Reid605ce7f2016-01-19 19:21:00 -0800578int API minijail_add_to_cgroup(struct minijail *j, const char *path)
579{
580 if (j->cgroup_count >= MAX_CGROUPS)
581 return -ENOMEM;
582 j->cgroups[j->cgroup_count] = strdup(path);
583 if (!j->cgroups[j->cgroup_count])
584 return -ENOMEM;
585 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800586 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800587 return 0;
588}
589
Dylan Reid648b2202015-10-23 00:50:00 -0700590int API minijail_mount(struct minijail *j, const char *src, const char *dest,
591 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700592{
Dylan Reid648b2202015-10-23 00:50:00 -0700593 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400594
595 if (*dest != '/')
596 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700597 m = calloc(1, sizeof(*m));
598 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400599 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700600 m->dest = strdup(dest);
601 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400602 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700603 m->src = strdup(src);
604 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400605 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700606 m->type = strdup(type);
607 if (!m->type)
608 goto error;
609 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400610
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800611 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400612
Elly Jonesdd3e8512012-01-23 15:13:38 -0500613 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700614 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400615 * containing vfs namespace.
616 */
617 minijail_namespace_vfs(j);
618
Dylan Reid648b2202015-10-23 00:50:00 -0700619 if (j->mounts_tail)
620 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400621 else
Dylan Reid648b2202015-10-23 00:50:00 -0700622 j->mounts_head = m;
623 j->mounts_tail = m;
624 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400625
626 return 0;
627
628error:
Dylan Reid648b2202015-10-23 00:50:00 -0700629 free(m->src);
630 free(m->dest);
631 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400632 return -ENOMEM;
633}
634
Dylan Reid648b2202015-10-23 00:50:00 -0700635int API minijail_bind(struct minijail *j, const char *src, const char *dest,
636 int writeable)
637{
638 unsigned long flags = MS_BIND;
639
640 if (!writeable)
641 flags |= MS_RDONLY;
642
643 return minijail_mount(j, src, dest, "", flags);
644}
645
Will Drewry6ac91122011-10-21 16:38:58 -0500646void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400647{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700648 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800649 if ((errno == EINVAL) && can_softfail()) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800650 warn("not loading seccomp filter,"
651 " seccomp not supported");
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -0800652 j->flags.seccomp_filter = 0;
653 j->flags.log_seccomp_filter = 0;
654 j->filter_len = 0;
655 j->filter_prog = NULL;
656 j->flags.no_new_privs = 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700657 }
658 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400659 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800660 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700661 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400662 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800663
664 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700665 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
666 die("failed to compile seccomp filter BPF program in '%s'",
667 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800668 }
669
670 j->filter_len = fprog->len;
671 j->filter_prog = fprog;
672
Elly Jonese1749eb2011-10-07 13:54:59 -0400673 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500674}
675
Andrew Brestickereac28942015-11-11 16:04:46 -0800676int API minijail_use_alt_syscall(struct minijail *j, const char *table)
677{
678 j->alt_syscall_table = strdup(table);
679 if (!j->alt_syscall_table)
680 return -ENOMEM;
681 j->flags.alt_syscall = 1;
682 return 0;
683}
684
Will Drewryf89aef52011-09-16 16:48:57 -0500685struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400686 size_t available;
687 size_t total;
688 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500689};
690
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800691void marshal_state_init(struct marshal_state *state, char *buf,
692 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400693{
694 state->available = available;
695 state->buf = buf;
696 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500697}
698
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800699void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400700{
701 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500702
Elly Jonese1749eb2011-10-07 13:54:59 -0400703 /* Up to |available| will be written. */
704 if (copy_len) {
705 memcpy(state->buf, src, copy_len);
706 state->buf += copy_len;
707 state->available -= copy_len;
708 }
709 /* |total| will contain the expected length. */
710 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500711}
712
Will Drewry6ac91122011-10-21 16:38:58 -0500713void minijail_marshal_helper(struct marshal_state *state,
714 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400715{
Dylan Reid648b2202015-10-23 00:50:00 -0700716 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800717 size_t i;
718
Elly Jonese1749eb2011-10-07 13:54:59 -0400719 marshal_append(state, (char *)j, sizeof(*j));
720 if (j->user)
721 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800722 if (j->suppl_gid_list) {
723 marshal_append(state, j->suppl_gid_list,
724 j->suppl_gid_count * sizeof(gid_t));
725 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400726 if (j->chrootdir)
727 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800728 if (j->alt_syscall_table) {
729 marshal_append(state, j->alt_syscall_table,
730 strlen(j->alt_syscall_table) + 1);
731 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800732 if (j->flags.seccomp_filter && j->filter_prog) {
733 struct sock_fprog *fp = j->filter_prog;
734 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800735 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400736 }
Dylan Reid648b2202015-10-23 00:50:00 -0700737 for (m = j->mounts_head; m; m = m->next) {
738 marshal_append(state, m->src, strlen(m->src) + 1);
739 marshal_append(state, m->dest, strlen(m->dest) + 1);
740 marshal_append(state, m->type, strlen(m->type) + 1);
741 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400742 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800743 for (i = 0; i < j->cgroup_count; ++i)
744 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500745}
746
Will Drewry6ac91122011-10-21 16:38:58 -0500747size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400748{
749 struct marshal_state state;
750 marshal_state_init(&state, NULL, 0);
751 minijail_marshal_helper(&state, j);
752 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500753}
754
Elly Jonese1749eb2011-10-07 13:54:59 -0400755int minijail_marshal(const struct minijail *j, char *buf, size_t available)
756{
757 struct marshal_state state;
758 marshal_state_init(&state, buf, available);
759 minijail_marshal_helper(&state, j);
760 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500761}
762
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800763/*
764 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400765 * @length Number of bytes to consume
766 * @buf Buffer to consume from
767 * @buflength Size of @buf
768 *
769 * Returns a pointer to the base of the bytes, or NULL for errors.
770 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700771void *consumebytes(size_t length, char **buf, size_t *buflength)
772{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400773 char *p = *buf;
774 if (length > *buflength)
775 return NULL;
776 *buf += length;
777 *buflength -= length;
778 return p;
779}
780
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800781/*
782 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400783 * @buf Buffer to consume
784 * @length Length of buffer
785 *
786 * Returns a pointer to the base of the string, or NULL for errors.
787 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700788char *consumestr(char **buf, size_t *buflength)
789{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400790 size_t len = strnlen(*buf, *buflength);
791 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700792 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400793 return NULL;
794 return consumebytes(len + 1, buf, buflength);
795}
796
Elly Jonese1749eb2011-10-07 13:54:59 -0400797int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
798{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800799 size_t i;
800 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500801 int ret = -EINVAL;
802
Elly Jonese1749eb2011-10-07 13:54:59 -0400803 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500804 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400805 memcpy((void *)j, serialized, sizeof(*j));
806 serialized += sizeof(*j);
807 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500808
Will Drewrybee7ba72011-10-21 20:47:01 -0500809 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700810 j->mounts_head = NULL;
811 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800812 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500813
Elly Jonese1749eb2011-10-07 13:54:59 -0400814 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400815 char *user = consumestr(&serialized, &length);
816 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500817 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400818 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500819 if (!j->user)
820 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400821 }
Will Drewryf89aef52011-09-16 16:48:57 -0500822
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800823 if (j->suppl_gid_list) { /* stale pointer */
824 if (j->suppl_gid_count > NGROUPS_MAX) {
825 goto bad_gid_list;
826 }
827 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
828 void *gid_list_bytes =
829 consumebytes(gid_list_size, &serialized, &length);
830 if (!gid_list_bytes)
831 goto bad_gid_list;
832
833 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
834 if (!j->suppl_gid_list)
835 goto bad_gid_list;
836
837 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
838 }
839
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400840 if (j->chrootdir) { /* stale pointer */
841 char *chrootdir = consumestr(&serialized, &length);
842 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500843 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400844 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500845 if (!j->chrootdir)
846 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400847 }
848
Andrew Brestickereac28942015-11-11 16:04:46 -0800849 if (j->alt_syscall_table) { /* stale pointer */
850 char *alt_syscall_table = consumestr(&serialized, &length);
851 if (!alt_syscall_table)
852 goto bad_syscall_table;
853 j->alt_syscall_table = strdup(alt_syscall_table);
854 if (!j->alt_syscall_table)
855 goto bad_syscall_table;
856 }
857
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800858 if (j->flags.seccomp_filter && j->filter_len > 0) {
859 size_t ninstrs = j->filter_len;
860 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
861 ninstrs > USHRT_MAX)
862 goto bad_filters;
863
864 size_t program_len = ninstrs * sizeof(struct sock_filter);
865 void *program = consumebytes(program_len, &serialized, &length);
866 if (!program)
867 goto bad_filters;
868
869 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800870 if (!j->filter_prog)
871 goto bad_filters;
872
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800873 j->filter_prog->len = ninstrs;
874 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800875 if (!j->filter_prog->filter)
876 goto bad_filter_prog_instrs;
877
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800878 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400879 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400880
Dylan Reid648b2202015-10-23 00:50:00 -0700881 count = j->mounts_count;
882 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400883 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700884 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400885 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700886 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400887 const char *src = consumestr(&serialized, &length);
888 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700889 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400890 dest = consumestr(&serialized, &length);
891 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700892 goto bad_mounts;
893 type = consumestr(&serialized, &length);
894 if (!type)
895 goto bad_mounts;
896 flags = consumebytes(sizeof(*flags), &serialized, &length);
897 if (!flags)
898 goto bad_mounts;
899 if (minijail_mount(j, src, dest, type, *flags))
900 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400901 }
902
Dylan Reid605ce7f2016-01-19 19:21:00 -0800903 count = j->cgroup_count;
904 j->cgroup_count = 0;
905 for (i = 0; i < count; ++i) {
906 char *cgroup = consumestr(&serialized, &length);
907 if (!cgroup)
908 goto bad_cgroups;
909 j->cgroups[i] = strdup(cgroup);
910 if (!j->cgroups[i])
911 goto bad_cgroups;
912 ++j->cgroup_count;
913 }
914
Elly Jonese1749eb2011-10-07 13:54:59 -0400915 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500916
Dylan Reid605ce7f2016-01-19 19:21:00 -0800917bad_cgroups:
918 while (j->mounts_head) {
919 struct mountpoint *m = j->mounts_head;
920 j->mounts_head = j->mounts_head->next;
921 free(m->type);
922 free(m->dest);
923 free(m->src);
924 free(m);
925 }
926 for (i = 0; i < j->cgroup_count; ++i)
927 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -0700928bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800929 if (j->flags.seccomp_filter && j->filter_len > 0) {
930 free(j->filter_prog->filter);
931 free(j->filter_prog);
932 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800933bad_filter_prog_instrs:
934 if (j->filter_prog)
935 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -0500936bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800937 if (j->alt_syscall_table)
938 free(j->alt_syscall_table);
939bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500940 if (j->chrootdir)
941 free(j->chrootdir);
942bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800943 if (j->suppl_gid_list)
944 free(j->suppl_gid_list);
945bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -0500946 if (j->user)
947 free(j->user);
948clear_pointers:
949 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800950 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500951 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800952 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800953 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500954out:
955 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500956}
957
Dylan Reidce5b55e2016-01-13 11:04:16 -0800958static void write_ugid_mappings(const struct minijail *j)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800959{
960 int fd, ret, len;
961 size_t sz;
962 char fname[32];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800963
964 sz = sizeof(fname);
965 if (j->uidmap) {
966 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700967 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800968 die("failed to write file name of uid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800969 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800970 if (fd < 0)
971 pdie("failed to open '%s'", fname);
972 len = strlen(j->uidmap);
973 if (write(fd, j->uidmap, len) < len)
974 die("failed to set uid_map");
975 close(fd);
976 }
977 if (j->gidmap) {
978 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700979 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800980 die("failed to write file name of gid_map");
Ricky Zhoubce609d2016-03-02 21:47:56 -0800981 fd = open(fname, O_WRONLY | O_CLOEXEC);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800982 if (fd < 0)
983 pdie("failed to open '%s'", fname);
984 len = strlen(j->gidmap);
985 if (write(fd, j->gidmap, len) < len)
986 die("failed to set gid_map");
987 close(fd);
988 }
Dylan Reidce5b55e2016-01-13 11:04:16 -0800989}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800990
Dylan Reidce5b55e2016-01-13 11:04:16 -0800991static void parent_setup_complete(int *pipe_fds)
992{
993 close(pipe_fds[0]);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800994 close(pipe_fds[1]);
995}
996
Dylan Reidce5b55e2016-01-13 11:04:16 -0800997/*
998 * wait_for_parent_setup: Called by the child process to wait for any
999 * further parent-side setup to complete before continuing.
1000 */
1001static void wait_for_parent_setup(int *pipe_fds)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001002{
1003 char buf;
1004
1005 close(pipe_fds[1]);
1006
Dylan Reidce5b55e2016-01-13 11:04:16 -08001007 /* Wait for parent to complete setup and close the pipe. */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001008 if (read(pipe_fds[0], &buf, 1) != 0)
1009 die("failed to sync with parent");
1010 close(pipe_fds[0]);
Dylan Reidce5b55e2016-01-13 11:04:16 -08001011}
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001012
Dylan Reidce5b55e2016-01-13 11:04:16 -08001013static void enter_user_namespace(const struct minijail *j)
1014{
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001015 if (j->uidmap && setresuid(0, 0, 0))
1016 pdie("setresuid");
1017 if (j->gidmap && setresgid(0, 0, 0))
1018 pdie("setresgid");
1019}
1020
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001021/*
1022 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001023 * @j Minijail these mounts are for
1024 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001025 *
1026 * Returns 0 for success.
1027 */
Dylan Reid648b2202015-10-23 00:50:00 -07001028static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001029{
Dylan Reid648b2202015-10-23 00:50:00 -07001030 int ret;
1031 char *dest;
1032 int remount_ro = 0;
1033
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001034 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001035 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001036 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001037
1038 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001039 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1040 * can't both be specified in the original bind mount.
1041 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001042 */
1043 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1044 remount_ro = 1;
1045 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001046 }
Dylan Reid648b2202015-10-23 00:50:00 -07001047
1048 ret = mount(m->src, dest, m->type, m->flags, NULL);
1049 if (ret)
1050 pdie("mount: %s -> %s", m->src, dest);
1051
1052 if (remount_ro) {
1053 m->flags |= MS_RDONLY;
1054 ret = mount(m->src, dest, NULL,
1055 m->flags | MS_REMOUNT, NULL);
1056 if (ret)
1057 pdie("bind ro: %s -> %s", m->src, dest);
1058 }
1059
Elly Jones51a5b6c2011-10-12 19:09:26 -04001060 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001061 if (m->next)
1062 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001063 return ret;
1064}
1065
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001066int enter_chroot(const struct minijail *j)
1067{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001068 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001069
1070 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001071 return ret;
1072
1073 if (chroot(j->chrootdir))
1074 return -errno;
1075
1076 if (chdir("/"))
1077 return -errno;
1078
1079 return 0;
1080}
1081
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001082int enter_pivot_root(const struct minijail *j)
1083{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001084 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001085
1086 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001087 return ret;
1088
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001089 /*
1090 * Keep the fd for both old and new root.
1091 * It will be used in fchdir later.
1092 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001093 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001094 if (oldroot < 0)
1095 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001096 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001097 if (newroot < 0)
1098 pdie("failed to open %s for fchdir", j->chrootdir);
1099
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001100 /*
1101 * To ensure chrootdir is the root of a file system,
1102 * do a self bind mount.
1103 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001104 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1105 pdie("failed to bind mount '%s'", j->chrootdir);
1106 if (chdir(j->chrootdir))
1107 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001108 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001109 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001110
1111 /*
1112 * Now the old root is mounted on top of the new root. Use fchdir to
1113 * change to the old root and unmount it.
1114 */
1115 if (fchdir(oldroot))
1116 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001117 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001118 if (umount2(".", MNT_DETACH))
1119 pdie("umount(/)");
1120 /* Change back to the new root. */
1121 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001122 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001123 if (close(oldroot))
1124 return -errno;
1125 if (close(newroot))
1126 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001127 if (chroot("/"))
1128 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001129 /* Set correct CWD for getcwd(3). */
1130 if (chdir("/"))
1131 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001132
1133 return 0;
1134}
1135
Lee Campbell11af0622014-05-22 12:36:04 -07001136int mount_tmp(void)
1137{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001138 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001139}
1140
Dylan Reid791f5772015-09-14 20:02:42 -07001141int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001142{
1143 const char *kProcPath = "/proc";
1144 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001145 /*
1146 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001147 * /proc in our namespace, which means using MS_REMOUNT here would
1148 * mutate our parent's mount as well, even though we're in a VFS
1149 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001150 * and make our own. However, if we are in a new user namespace, /proc
1151 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -04001152 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001153 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -04001154 return -errno;
1155 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1156 return -errno;
1157 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001158}
1159
Dylan Reid605ce7f2016-01-19 19:21:00 -08001160static void write_pid_to_path(pid_t pid, const char *path)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001161{
Dylan Reid605ce7f2016-01-19 19:21:00 -08001162 FILE *fp = fopen(path, "w");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001163
1164 if (!fp)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001165 pdie("failed to open '%s'", path);
1166 if (fprintf(fp, "%d\n", (int)pid) < 0)
1167 pdie("fprintf(%s)", path);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001168 if (fclose(fp))
Dylan Reid605ce7f2016-01-19 19:21:00 -08001169 pdie("fclose(%s)", path);
1170}
1171
1172static void write_pid_file(const struct minijail *j)
1173{
1174 write_pid_to_path(j->initpid, j->pid_file_path);
1175}
1176
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001177static void add_to_cgroups(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001178{
1179 size_t i;
1180
1181 for (i = 0; i < j->cgroup_count; ++i)
1182 write_pid_to_path(j->initpid, j->cgroups[i]);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001183}
1184
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001185void drop_ugid(const struct minijail *j)
1186{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001187 if (j->flags.usergroups && j->flags.suppl_gids) {
1188 die("tried to inherit *and* set supplementary groups;"
1189 " can only do one");
1190 }
1191
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001192 if (j->flags.usergroups) {
1193 if (initgroups(j->user, j->usergid))
1194 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001195 } else if (j->flags.suppl_gids) {
1196 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1197 pdie("setgroups");
1198 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001199 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001200 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001201 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001202 * users.
1203 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001204 if ((j->uid || j->gid) && setgroups(0, NULL))
1205 pdie("setgroups");
1206 }
1207
1208 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1209 pdie("setresgid");
1210
1211 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1212 pdie("setresuid");
1213}
1214
Mike Frysinger3adfef72013-05-09 17:19:08 -04001215/*
1216 * We specifically do not use cap_valid() as that only tells us the last
1217 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001218 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001219 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001220 * kernel).
1221 * Normally, we suck up the answer via /proc. On Android, not all processes are
1222 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1223 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001224 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001225static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001226{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001227 unsigned int last_valid_cap = 0;
1228 if (is_android()) {
1229 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1230 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001231
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001232 /* |last_valid_cap| will be the first failing value. */
1233 if (last_valid_cap > 0) {
1234 last_valid_cap--;
1235 }
1236 } else {
1237 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1238 FILE *fp = fopen(cap_file, "re");
1239 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1240 pdie("fscanf(%s)", cap_file);
1241 fclose(fp);
1242 }
Dylan Reidf682d472015-09-17 21:39:07 -07001243 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001244}
1245
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001246static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1247{
1248 const uint64_t one = 1;
1249 unsigned int i;
1250 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1251 if (keep_mask & (one << i))
1252 continue;
1253 if (prctl(PR_CAPBSET_DROP, i))
1254 pdie("could not drop capability from bounding set");
1255 }
1256}
1257
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001258void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001259{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001260 if (!j->flags.use_caps)
1261 return;
1262
Elly Jonese1749eb2011-10-07 13:54:59 -04001263 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001264 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001265 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001266 unsigned int i;
1267 if (!caps)
1268 die("can't get process caps");
1269 if (cap_clear_flag(caps, CAP_INHERITABLE))
1270 die("can't clear inheritable caps");
1271 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1272 die("can't clear effective caps");
1273 if (cap_clear_flag(caps, CAP_PERMITTED))
1274 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001275 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001276 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001277 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001278 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001279 flag[0] = i;
1280 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001281 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001282 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001283 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001284 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001285 die("can't add inheritable cap");
1286 }
1287 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001288 die("can't apply initial cleaned capset");
1289
1290 /*
1291 * Instead of dropping bounding set first, do it here in case
1292 * the caller had a more permissive bounding set which could
1293 * have been used above to raise a capability that wasn't already
1294 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1295 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001296 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001297
1298 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001299 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001300 flag[0] = CAP_SETPCAP;
1301 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1302 die("can't clear effective cap");
1303 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1304 die("can't clear permitted cap");
1305 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1306 die("can't clear inheritable cap");
1307 }
1308
1309 if (cap_set_proc(caps))
1310 die("can't apply final cleaned capset");
1311
1312 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001313}
1314
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001315void set_seccomp_filter(const struct minijail *j)
1316{
1317 /*
1318 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1319 * in the kernel source tree for an explanation of the parameters.
1320 */
1321 if (j->flags.no_new_privs) {
1322 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1323 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1324 }
1325
1326 /*
1327 * If we're logging seccomp filter failures,
1328 * install the SIGSYS handler first.
1329 */
1330 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1331 if (install_sigsys_handler())
1332 pdie("install SIGSYS handler");
1333 warn("logging seccomp filter failures");
1334 }
1335
1336 /*
1337 * Install the syscall filter.
1338 */
1339 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001340 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1341 j->filter_prog)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001342 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001343 warn("seccomp not supported");
1344 return;
1345 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001346 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001347 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001348 }
1349}
1350
Will Drewry6ac91122011-10-21 16:38:58 -05001351void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001352{
Dylan Reidf682d472015-09-17 21:39:07 -07001353 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001354 * If we're dropping caps, get the last valid cap from /proc now,
1355 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001356 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001357 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001358 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001359 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001360
Elly Jonese1749eb2011-10-07 13:54:59 -04001361 if (j->flags.pids)
1362 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001363 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001364
Elly Jonese1749eb2011-10-07 13:54:59 -04001365 if (j->flags.usergroups && !j->user)
1366 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001367
Elly Jonesdd3e8512012-01-23 15:13:38 -05001368 /*
1369 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001370 * so we don't even try. If any of our operations fail, we abort() the
1371 * entire process.
1372 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001373 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1374 pdie("setns(CLONE_NEWNS)");
1375
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001376 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001377 if (unshare(CLONE_NEWNS))
1378 pdie("unshare(vfs)");
1379 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001380 * Unless asked not to, remount all filesystems as private.
1381 * If they are shared, new bind mounts will creep out of our
1382 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001383 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1384 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001385 if (!j->flags.skip_remount_private) {
1386 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1387 pdie("mount(/, private)");
1388 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001389 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001390
Dylan Reidf7942472015-11-18 17:55:26 -08001391 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1392 pdie("unshare(ipc)");
1393 }
1394
Dylan Reid1102f5a2015-09-15 11:52:20 -07001395 if (j->flags.enter_net) {
1396 if (setns(j->netns_fd, CLONE_NEWNET))
1397 pdie("setns(CLONE_NEWNET)");
1398 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001399 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001400 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001401
Elly Jones51a5b6c2011-10-12 19:09:26 -04001402 if (j->flags.chroot && enter_chroot(j))
1403 pdie("chroot");
1404
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001405 if (j->flags.pivot_root && enter_pivot_root(j))
1406 pdie("pivot_root");
1407
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001408 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001409 pdie("mount_tmp");
1410
Dylan Reid791f5772015-09-14 20:02:42 -07001411 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001412 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001413
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001414 /*
1415 * If we're only dropping capabilities from the bounding set, but not
1416 * from the thread's (permitted|inheritable|effective) sets, do it now.
1417 */
1418 if (j->flags.capbset_drop) {
1419 drop_capbset(j->cap_bset, last_valid_cap);
1420 }
1421
1422 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001423 /*
1424 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001425 * capability to change uids, our attempt to use setuid()
1426 * below will fail. Hang on to root caps across setuid(), then
1427 * lock securebits.
1428 */
1429 if (prctl(PR_SET_KEEPCAPS, 1))
1430 pdie("prctl(PR_SET_KEEPCAPS)");
1431 if (prctl
1432 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1433 pdie("prctl(PR_SET_SECUREBITS)");
1434 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001435
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001436 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001437 /*
1438 * If we're setting no_new_privs, we can drop privileges
1439 * before setting seccomp filter. This way filter policies
1440 * don't need to allow privilege-dropping syscalls.
1441 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001442 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001443 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001444 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001445 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001446 /*
1447 * If we're not setting no_new_privs,
1448 * we need to set seccomp filter *before* dropping privileges.
1449 * WARNING: this means that filter policies *must* allow
1450 * setgroups()/setresgid()/setresuid() for dropping root and
1451 * capget()/capset()/prctl() for dropping caps.
1452 */
1453 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001454 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001455 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001456 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001457
Elly Jonesdd3e8512012-01-23 15:13:38 -05001458 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001459 * Select the specified alternate syscall table. The table must not
1460 * block prctl(2) if we're using seccomp as well.
1461 */
1462 if (j->flags.alt_syscall) {
1463 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1464 pdie("prctl(PR_ALT_SYSCALL)");
1465 }
1466
1467 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001468 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001469 * privilege-dropping syscalls :)
1470 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001471 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jeff Vander Stoep2885bef2016-01-11 15:22:42 -08001472 if ((errno == EINVAL) && can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001473 warn("seccomp not supported");
1474 return;
1475 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001476 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001477 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001478}
1479
Will Drewry6ac91122011-10-21 16:38:58 -05001480/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001481static int init_exitstatus = 0;
1482
Will Drewry6ac91122011-10-21 16:38:58 -05001483void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001484{
1485 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001486}
1487
Will Drewry6ac91122011-10-21 16:38:58 -05001488int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001489{
1490 pid_t pid;
1491 int status;
1492 /* so that we exit with the right status */
1493 signal(SIGTERM, init_term);
1494 /* TODO(wad) self jail with seccomp_filters here. */
1495 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001496 /*
1497 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001498 * left inside our pid namespace or we get a signal.
1499 */
1500 if (pid == rootpid)
1501 init_exitstatus = status;
1502 }
1503 if (!WIFEXITED(init_exitstatus))
1504 _exit(MINIJAIL_ERR_INIT);
1505 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001506}
1507
Will Drewry6ac91122011-10-21 16:38:58 -05001508int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001509{
1510 size_t sz = 0;
1511 size_t bytes = read(fd, &sz, sizeof(sz));
1512 char *buf;
1513 int r;
1514 if (sizeof(sz) != bytes)
1515 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001516 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001517 return -E2BIG;
1518 buf = malloc(sz);
1519 if (!buf)
1520 return -ENOMEM;
1521 bytes = read(fd, buf, sz);
1522 if (bytes != sz) {
1523 free(buf);
1524 return -EINVAL;
1525 }
1526 r = minijail_unmarshal(j, buf, sz);
1527 free(buf);
1528 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001529}
1530
Will Drewry6ac91122011-10-21 16:38:58 -05001531int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001532{
1533 char *buf;
1534 size_t sz = minijail_size(j);
1535 ssize_t written;
1536 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001537
Elly Jonese1749eb2011-10-07 13:54:59 -04001538 if (!sz)
1539 return -EINVAL;
1540 buf = malloc(sz);
1541 r = minijail_marshal(j, buf, sz);
1542 if (r) {
1543 free(buf);
1544 return r;
1545 }
1546 /* Sends [size][minijail]. */
1547 written = write(fd, &sz, sizeof(sz));
1548 if (written != sizeof(sz)) {
1549 free(buf);
1550 return -EFAULT;
1551 }
1552 written = write(fd, buf, sz);
1553 if (written < 0 || (size_t) written != sz) {
1554 free(buf);
1555 return -EFAULT;
1556 }
1557 free(buf);
1558 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001559}
Elly Jonescd7a9042011-07-22 13:56:51 -04001560
Will Drewry6ac91122011-10-21 16:38:58 -05001561int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001562{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001563#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001564 /* Don't use LDPRELOAD on Brillo. */
1565 return 0;
1566#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001567 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1568 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1569 if (!newenv)
1570 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001571
Elly Jonese1749eb2011-10-07 13:54:59 -04001572 /* Only insert a separating space if we have something to separate... */
1573 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1574 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001575
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001576 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001577 setenv(kLdPreloadEnvVar, newenv, 1);
1578 free(newenv);
1579 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001580#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001581}
1582
Will Drewry6ac91122011-10-21 16:38:58 -05001583int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001584{
1585 int r = pipe(fds);
1586 char fd_buf[11];
1587 if (r)
1588 return r;
1589 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1590 if (r <= 0)
1591 return -EINVAL;
1592 setenv(kFdEnvVar, fd_buf, 1);
1593 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001594}
1595
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001596int setup_pipe_end(int fds[2], size_t index)
1597{
1598 if (index > 1)
1599 return -1;
1600
1601 close(fds[1 - index]);
1602 return fds[index];
1603}
1604
1605int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1606{
1607 if (index > 1)
1608 return -1;
1609
1610 close(fds[1 - index]);
1611 /* dup2(2) the corresponding end of the pipe into |fd|. */
1612 return dup2(fds[index], fd);
1613}
1614
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001615int minijail_run_internal(struct minijail *j, const char *filename,
1616 char *const argv[], pid_t *pchild_pid,
1617 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1618 int use_preload);
1619
Will Drewry6ac91122011-10-21 16:38:58 -05001620int API minijail_run(struct minijail *j, const char *filename,
1621 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001622{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001623 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1624 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001625}
1626
1627int API minijail_run_pid(struct minijail *j, const char *filename,
1628 char *const argv[], pid_t *pchild_pid)
1629{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001630 return minijail_run_internal(j, filename, argv, pchild_pid,
1631 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001632}
1633
1634int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001635 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001636{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001637 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1638 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001639}
1640
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001641int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001642 char *const argv[], pid_t *pchild_pid,
1643 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001644{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001645 return minijail_run_internal(j, filename, argv, pchild_pid,
1646 pstdin_fd, pstdout_fd, pstderr_fd, true);
1647}
1648
1649int API minijail_run_no_preload(struct minijail *j, const char *filename,
1650 char *const argv[])
1651{
1652 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1653 false);
1654}
1655
Samuel Tan63187f42015-10-16 13:01:53 -07001656int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001657 const char *filename,
1658 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001659 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001660 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001661 int *pstderr_fd)
1662{
Samuel Tan63187f42015-10-16 13:01:53 -07001663 return minijail_run_internal(j, filename, argv, pchild_pid,
1664 pstdin_fd, pstdout_fd, pstderr_fd, false);
1665}
1666
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001667int minijail_run_internal(struct minijail *j, const char *filename,
1668 char *const argv[], pid_t *pchild_pid,
1669 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1670 int use_preload)
1671{
Elly Jonese1749eb2011-10-07 13:54:59 -04001672 char *oldenv, *oldenv_copy = NULL;
1673 pid_t child_pid;
1674 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001675 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001676 int stdout_fds[2];
1677 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001678 int child_sync_pipe_fds[2];
1679 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001680 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001681 /* We need to remember this across the minijail_preexec() call. */
1682 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001683 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001684
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001685 if (use_preload) {
1686 oldenv = getenv(kLdPreloadEnvVar);
1687 if (oldenv) {
1688 oldenv_copy = strdup(oldenv);
1689 if (!oldenv_copy)
1690 return -ENOMEM;
1691 }
1692
1693 if (setup_preload())
1694 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001695 }
Will Drewryf89aef52011-09-16 16:48:57 -05001696
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001697 if (!use_preload) {
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001698 if (j->flags.use_caps)
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001699 die("capabilities are not supported without "
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001700 "LD_PRELOAD");
1701 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001702
Elly Jonesdd3e8512012-01-23 15:13:38 -05001703 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001704 * Make the process group ID of this process equal to its PID, so that
1705 * both the Minijail process and the jailed process can be killed
1706 * together.
1707 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1708 * the process is already a process group leader.
1709 */
1710 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1711 if (errno != EPERM) {
1712 pdie("setpgid(0, 0)");
1713 }
1714 }
1715
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001716 if (use_preload) {
1717 /*
1718 * Before we fork(2) and execve(2) the child process, we need
1719 * to open a pipe(2) to send the minijail configuration over.
1720 */
1721 if (setup_pipe(pipe_fds))
1722 return -EFAULT;
1723 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001724
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001725 /*
1726 * If we want to write to the child process' standard input,
1727 * create the pipe(2) now.
1728 */
1729 if (pstdin_fd) {
1730 if (pipe(stdin_fds))
1731 return -EFAULT;
1732 }
1733
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001734 /*
1735 * If we want to read from the child process' standard output,
1736 * create the pipe(2) now.
1737 */
1738 if (pstdout_fd) {
1739 if (pipe(stdout_fds))
1740 return -EFAULT;
1741 }
1742
1743 /*
1744 * If we want to read from the child process' standard error,
1745 * create the pipe(2) now.
1746 */
1747 if (pstderr_fd) {
1748 if (pipe(stderr_fds))
1749 return -EFAULT;
1750 }
1751
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001752 /*
1753 * If we want to set up a new uid/gid mapping in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001754 * or if we need to add the child process to cgroups, create the pipe(2)
1755 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001756 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001757 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08001758 sync_child = 1;
1759 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001760 return -EFAULT;
1761 }
1762
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001763 /*
1764 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001765 *
1766 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1767 *
1768 * In multithreaded programs, there are a bunch of locks inside libc,
1769 * some of which may be held by other threads at the time that we call
1770 * minijail_run_pid(). If we call fork(), glibc does its level best to
1771 * ensure that we hold all of these locks before it calls clone()
1772 * internally and drop them after clone() returns, but when we call
1773 * sys_clone(2) directly, all that gets bypassed and we end up with a
1774 * child address space where some of libc's important locks are held by
1775 * other threads (which did not get cloned, and hence will never release
1776 * those locks). This is okay so long as we call exec() immediately
1777 * after, but a bunch of seemingly-innocent libc functions like setenv()
1778 * take locks.
1779 *
1780 * Hence, only call sys_clone() if we need to, in order to get at pid
1781 * namespacing. If we follow this path, the child's address space might
1782 * have broken locks; you may only call functions that do not acquire
1783 * any locks.
1784 *
1785 * Unfortunately, fork() acquires every lock it can get its hands on, as
1786 * previously detailed, so this function is highly likely to deadlock
1787 * later on (see "deadlock here") if we're multithreaded.
1788 *
1789 * We might hack around this by having the clone()d child (init of the
1790 * pid namespace) return directly, rather than leaving the clone()d
1791 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001792 * its fork()ed child return in turn), but that process would be
1793 * crippled with its libc locks potentially broken. We might try
1794 * fork()ing in the parent before we clone() to ensure that we own all
1795 * the locks, but then we have to have the forked child hanging around
1796 * consuming resources (and possibly having file descriptors / shared
1797 * memory regions / etc attached). We'd need to keep the child around to
1798 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04001799 *
1800 * TODO(ellyjones): figure out if the "forked child hanging around"
1801 * problem is fixable or not. It would be nice if we worked in this
1802 * case.
1803 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001804 if (pid_namespace) {
1805 int clone_flags = CLONE_NEWPID | SIGCHLD;
1806 if (j->flags.userns)
1807 clone_flags |= CLONE_NEWUSER;
1808 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001809 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001810 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001811 }
Elly Jones761b7412012-06-13 15:49:52 -04001812
Elly Jonese1749eb2011-10-07 13:54:59 -04001813 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001814 if (use_preload) {
1815 free(oldenv_copy);
1816 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001817 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001818 }
Will Drewryf89aef52011-09-16 16:48:57 -05001819
Elly Jonese1749eb2011-10-07 13:54:59 -04001820 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001821 if (use_preload) {
1822 /* Restore parent's LD_PRELOAD. */
1823 if (oldenv_copy) {
1824 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1825 free(oldenv_copy);
1826 } else {
1827 unsetenv(kLdPreloadEnvVar);
1828 }
1829 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001830 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001831
Elly Jonese1749eb2011-10-07 13:54:59 -04001832 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001833
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001834 if (j->flags.pid_file)
1835 write_pid_file(j);
1836
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08001837 if (j->flags.cgroups)
1838 add_to_cgroups(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001839
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001840 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001841 write_ugid_mappings(j);
1842
1843 if (sync_child)
1844 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001845
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001846 if (use_preload) {
1847 /* Send marshalled minijail. */
1848 close(pipe_fds[0]); /* read endpoint */
1849 ret = minijail_to_fd(j, pipe_fds[1]);
1850 close(pipe_fds[1]); /* write endpoint */
1851 if (ret) {
1852 kill(j->initpid, SIGKILL);
1853 die("failed to send marshalled minijail");
1854 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001855 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001856
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001857 if (pchild_pid)
1858 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001859
1860 /*
1861 * If we want to write to the child process' standard input,
1862 * set up the write end of the pipe.
1863 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001864 if (pstdin_fd)
1865 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001866 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001867
1868 /*
1869 * If we want to read from the child process' standard output,
1870 * set up the read end of the pipe.
1871 */
1872 if (pstdout_fd)
1873 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001874 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001875
1876 /*
1877 * If we want to read from the child process' standard error,
1878 * set up the read end of the pipe.
1879 */
1880 if (pstderr_fd)
1881 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001882 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001883
Elly Jonese1749eb2011-10-07 13:54:59 -04001884 return 0;
1885 }
1886 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001887
Peter Qiu2860c462015-12-16 15:13:06 -08001888 if (j->flags.reset_signal_mask) {
1889 sigset_t signal_mask;
1890 if (sigemptyset(&signal_mask) != 0)
1891 pdie("sigemptyset failed");
1892 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
1893 pdie("sigprocmask failed");
1894 }
1895
Dylan Reidce5b55e2016-01-13 11:04:16 -08001896 if (sync_child)
1897 wait_for_parent_setup(child_sync_pipe_fds);
1898
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001899 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08001900 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001901
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001902 /*
1903 * If we want to write to the jailed process' standard input,
1904 * set up the read end of the pipe.
1905 */
1906 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001907 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1908 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001909 die("failed to set up stdin pipe");
1910 }
1911
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001912 /*
1913 * If we want to read from the jailed process' standard output,
1914 * set up the write end of the pipe.
1915 */
1916 if (pstdout_fd) {
1917 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1918 STDOUT_FILENO) < 0)
1919 die("failed to set up stdout pipe");
1920 }
1921
1922 /*
1923 * If we want to read from the jailed process' standard error,
1924 * set up the write end of the pipe.
1925 */
1926 if (pstderr_fd) {
1927 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1928 STDERR_FILENO) < 0)
1929 die("failed to set up stderr pipe");
1930 }
1931
Dylan Reid791f5772015-09-14 20:02:42 -07001932 /* If running an init program, let it decide when/how to mount /proc. */
1933 if (pid_namespace && !do_init)
1934 j->flags.remount_proc_ro = 0;
1935
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001936 if (use_preload) {
1937 /* Strip out flags that cannot be inherited across execve(2). */
1938 minijail_preexec(j);
1939 } else {
1940 j->flags.pids = 0;
1941 }
1942 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001943 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001944
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001945 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001946 /*
1947 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001948 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001949 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001950 * a child to actually run the program. If |do_init == 0|, we
1951 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001952 *
1953 * If we're multithreaded, we'll probably deadlock here. See
1954 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001955 */
1956 child_pid = fork();
1957 if (child_pid < 0)
1958 _exit(child_pid);
1959 else if (child_pid > 0)
1960 init(child_pid); /* never returns */
1961 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001962
Elly Jonesdd3e8512012-01-23 15:13:38 -05001963 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001964 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001965 * calling process
1966 * -> execve()-ing process
1967 * If we are:
1968 * calling process
1969 * -> init()-ing process
1970 * -> execve()-ing process
1971 */
1972 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001973}
1974
Will Drewry6ac91122011-10-21 16:38:58 -05001975int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001976{
1977 int st;
1978 if (kill(j->initpid, SIGTERM))
1979 return -errno;
1980 if (waitpid(j->initpid, &st, 0) < 0)
1981 return -errno;
1982 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001983}
1984
Will Drewry6ac91122011-10-21 16:38:58 -05001985int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001986{
1987 int st;
1988 if (waitpid(j->initpid, &st, 0) < 0)
1989 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001990
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001991 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001992 int error_status = st;
1993 if (WIFSIGNALED(st)) {
1994 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001995 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001996 j->initpid, signum);
1997 /*
1998 * We return MINIJAIL_ERR_JAIL if the process received
1999 * SIGSYS, which happens when a syscall is blocked by
2000 * seccomp filters.
2001 * If not, we do what bash(1) does:
2002 * $? = 128 + signum
2003 */
2004 if (signum == SIGSYS) {
2005 error_status = MINIJAIL_ERR_JAIL;
2006 } else {
2007 error_status = 128 + signum;
2008 }
2009 }
2010 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002011 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002012
2013 int exit_status = WEXITSTATUS(st);
2014 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002015 info("child process %d exited with status %d",
2016 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002017
2018 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002019}
2020
Will Drewry6ac91122011-10-21 16:38:58 -05002021void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002022{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002023 size_t i;
2024
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002025 if (j->flags.seccomp_filter && j->filter_prog) {
2026 free(j->filter_prog->filter);
2027 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002028 }
Dylan Reid648b2202015-10-23 00:50:00 -07002029 while (j->mounts_head) {
2030 struct mountpoint *m = j->mounts_head;
2031 j->mounts_head = j->mounts_head->next;
2032 free(m->type);
2033 free(m->dest);
2034 free(m->src);
2035 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002036 }
Dylan Reid648b2202015-10-23 00:50:00 -07002037 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002038 if (j->user)
2039 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002040 if (j->suppl_gid_list)
2041 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002042 if (j->chrootdir)
2043 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08002044 if (j->alt_syscall_table)
2045 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002046 for (i = 0; i < j->cgroup_count; ++i)
2047 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002048 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002049}