blob: 7f41b33c5768b38f9bb62f0a66c762b79162b71c [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
Arthur Gautier7a569072016-04-23 17:25:20 +00007#define _DEFAULT_SOURCE
Elly Jonescd7a9042011-07-22 13:56:51 -04008#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07009
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080010#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050011#include <ctype.h>
Luis Hector Chavez43ff0802016-10-07 12:21:07 -070012#include <dirent.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070014#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040015#include <grp.h>
16#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050017#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040018#include <linux/capability.h>
Mike Frysinger7559dfe2016-11-15 18:58:39 -050019#include <net/if.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040020#include <pwd.h>
21#include <sched.h>
22#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050023#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070024#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080025#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040029#include <sys/capability.h>
30#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050031#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040032#include <sys/prctl.h>
Mike Frysinger7559dfe2016-11-15 18:58:39 -050033#include <sys/socket.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070034#include <sys/stat.h>
35#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080036#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040037#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070038#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040039#include <unistd.h>
40
41#include "libminijail.h"
42#include "libminijail-private.h"
43
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070044#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080045#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040046#include "syscall_wrapper.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070047#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080048
Lei Zhangeee31552012-10-17 21:27:10 -070049#ifdef HAVE_SECUREBITS_H
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070050# include <linux/securebits.h>
Lei Zhangeee31552012-10-17 21:27:10 -070051#else
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070052# define SECURE_ALL_BITS 0x55
53# define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
54#endif
55/* For kernels < 4.3. */
56#define OLD_SECURE_ALL_BITS 0x15
57#define OLD_SECURE_ALL_LOCKS (OLD_SECURE_ALL_BITS << 1)
58
59/*
60 * Assert the value of SECURE_ALL_BITS at compile-time.
61 * Brillo devices are currently compiled against 4.4 kernel headers. Kernel 4.3
62 * added a new securebit.
63 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
64 * when used on older kernels. The compile-time assert will catch this situation
65 * at compile time.
66 */
67#ifdef __BRILLO__
68_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
Lei Zhangeee31552012-10-17 21:27:10 -070069#endif
70
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070071/* Until these are reliably available in linux/prctl.h. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080072#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070073# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080074#endif
75
Andrew Brestickereac28942015-11-11 16:04:46 -080076#ifndef PR_ALT_SYSCALL
77# define PR_ALT_SYSCALL 0x43724f53
78#endif
79
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040080/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080081#ifndef PR_SET_NO_NEW_PRIVS
82# define PR_SET_NO_NEW_PRIVS 38
83#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040084
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080085#ifndef SECCOMP_MODE_FILTER
86# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050087#endif
88
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040089#ifndef SECCOMP_SET_MODE_STRICT
90# define SECCOMP_SET_MODE_STRICT 0
91#endif
92#ifndef SECCOMP_SET_MODE_FILTER
93# define SECCOMP_SET_MODE_FILTER 1
94#endif
95
96#ifndef SECCOMP_FILTER_FLAG_TSYNC
97# define SECCOMP_FILTER_FLAG_TSYNC 1
98#endif
99/* End seccomp filter related flags. */
100
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700101/* New cgroup namespace might not be in linux-headers yet. */
102#ifndef CLONE_NEWCGROUP
103# define CLONE_NEWCGROUP 0x02000000
104#endif
105
Dylan Reid605ce7f2016-01-19 19:21:00 -0800106#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
107
Dylan Reid648b2202015-10-23 00:50:00 -0700108struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400109 char *src;
110 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700111 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700112 char *data;
113 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -0700114 unsigned long flags;
115 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400116};
117
Will Drewryf89aef52011-09-16 16:48:57 -0500118struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700119 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700120 * WARNING: if you add a flag here you need to make sure it's
121 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700122 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400123 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700124 int uid : 1;
125 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100126 int inherit_suppl_gids : 1;
127 int set_suppl_gids : 1;
128 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700129 int use_caps : 1;
130 int capbset_drop : 1;
131 int vfs : 1;
132 int enter_vfs : 1;
133 int skip_remount_private : 1;
134 int pids : 1;
135 int ipc : 1;
136 int net : 1;
137 int enter_net : 1;
138 int ns_cgroups : 1;
139 int userns : 1;
140 int disable_setgroups : 1;
141 int seccomp : 1;
142 int remount_proc_ro : 1;
143 int no_new_privs : 1;
144 int seccomp_filter : 1;
145 int seccomp_filter_tsync : 1;
146 int seccomp_filter_logging : 1;
147 int chroot : 1;
148 int pivot_root : 1;
149 int mount_tmp : 1;
150 int do_init : 1;
151 int pid_file : 1;
152 int cgroups : 1;
153 int alt_syscall : 1;
154 int reset_signal_mask : 1;
155 int close_open_fds : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400156 } flags;
157 uid_t uid;
158 gid_t gid;
159 gid_t usergid;
160 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800161 size_t suppl_gid_count;
162 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400163 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800164 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400165 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700166 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700167 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400168 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800169 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800170 char *uidmap;
171 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800172 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800173 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800174 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700175 struct mountpoint *mounts_head;
176 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800177 size_t mounts_count;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800178 char *cgroups[MAX_CGROUPS];
179 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500180};
181
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700182/*
183 * Strip out flags meant for the parent.
184 * We keep things that are not inherited across execve(2) (e.g. capabilities),
185 * or are easier to set after execve(2) (e.g. seccomp filters).
186 */
187void minijail_preenter(struct minijail *j)
188{
189 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700190 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900191 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700192 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700193 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800194 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800195 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800196 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700197}
198
199/*
200 * Strip out flags meant for the child.
201 * We keep things that are inherited across execve(2).
202 */
203void minijail_preexec(struct minijail *j)
204{
205 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700206 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800207 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700208 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800209 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700210 if (j->user)
211 free(j->user);
212 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800213 if (j->suppl_gid_list)
214 free(j->suppl_gid_list);
215 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700216 memset(&j->flags, 0, sizeof(j->flags));
217 /* Now restore anything we meant to keep. */
218 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700219 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800220 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700221 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800222 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700223 /* Note, |pids| will already have been used before this call. */
224}
225
226/* Minijail API. */
227
Will Drewry6ac91122011-10-21 16:38:58 -0500228struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400229{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400230 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400231}
232
Will Drewry6ac91122011-10-21 16:38:58 -0500233void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400234{
235 if (uid == 0)
236 die("useless change to uid 0");
237 j->uid = uid;
238 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400239}
240
Will Drewry6ac91122011-10-21 16:38:58 -0500241void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400242{
243 if (gid == 0)
244 die("useless change to gid 0");
245 j->gid = gid;
246 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400247}
248
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800249void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
250 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800251{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800252 size_t i;
253
Lutz Justen13807cb2017-01-03 17:11:55 +0100254 if (j->flags.inherit_suppl_gids || j->flags.keep_suppl_gids)
255 die("cannot inherit *and* set or keep supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800256
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800257 if (size == 0) {
258 /* Clear supplementary groups. */
259 j->suppl_gid_list = NULL;
260 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100261 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800262 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800263 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800264
265 /* Copy the gid_t array. */
266 j->suppl_gid_list = calloc(size, sizeof(gid_t));
267 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800268 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800269 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800270 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800271 j->suppl_gid_list[i] = list[i];
272 }
273 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100274 j->flags.set_suppl_gids = 1;
275}
276
277void API minijail_keep_supplementary_gids(struct minijail *j) {
278 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800279}
280
Will Drewry6ac91122011-10-21 16:38:58 -0500281int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400282{
283 char *buf = NULL;
284 struct passwd pw;
285 struct passwd *ppw = NULL;
286 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
287 if (sz == -1)
288 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400289
Elly Jonesdd3e8512012-01-23 15:13:38 -0500290 /*
291 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400292 * the maximum needed size of the buffer, so we don't have to search.
293 */
294 buf = malloc(sz);
295 if (!buf)
296 return -ENOMEM;
297 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500298 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800299 * We're safe to free the buffer here. The strings inside |pw| point
300 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800301 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
302 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500303 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400304 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700305 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400306 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700307 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400308 minijail_change_uid(j, ppw->pw_uid);
309 j->user = strdup(user);
310 if (!j->user)
311 return -ENOMEM;
312 j->usergid = ppw->pw_gid;
313 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400314}
315
Will Drewry6ac91122011-10-21 16:38:58 -0500316int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400317{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700318 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700319 struct group gr;
320 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400321 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
322 if (sz == -1)
323 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400324
Elly Jonesdd3e8512012-01-23 15:13:38 -0500325 /*
326 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400327 * the maximum needed size of the buffer, so we don't have to search.
328 */
329 buf = malloc(sz);
330 if (!buf)
331 return -ENOMEM;
332 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500333 /*
334 * We're safe to free the buffer here. The strings inside gr point
335 * inside buf, but we don't use any of them; this leaves the pointers
336 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
337 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400338 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700339 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400340 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700341 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400342 minijail_change_gid(j, pgr->gr_gid);
343 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400344}
345
Will Drewry6ac91122011-10-21 16:38:58 -0500346void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400347{
348 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400349}
350
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700351void API minijail_no_new_privs(struct minijail *j)
352{
353 j->flags.no_new_privs = 1;
354}
355
Will Drewry6ac91122011-10-21 16:38:58 -0500356void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400357{
358 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500359}
360
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400361void API minijail_set_seccomp_filter_tsync(struct minijail *j)
362{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400363 if (j->filter_len > 0 && j->filter_prog != NULL) {
364 die("minijail_set_seccomp_filter_tsync() must be called "
365 "before minijail_parse_seccomp_filters()");
366 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400367 j->flags.seccomp_filter_tsync = 1;
368}
369
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700370void API minijail_log_seccomp_filter_failures(struct minijail *j)
371{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400372 if (j->filter_len > 0 && j->filter_prog != NULL) {
373 die("minijail_log_seccomp_filter_failures() must be called "
374 "before minijail_parse_seccomp_filters()");
375 }
376 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700377}
378
Will Drewry6ac91122011-10-21 16:38:58 -0500379void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400380{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800381 /*
382 * 'minijail_use_caps' configures a runtime-capabilities-only
383 * environment, including a bounding set matching the thread's runtime
384 * (permitted|inheritable|effective) sets.
385 * Therefore, it will override any existing bounding set configurations
386 * since the latter would allow gaining extra runtime capabilities from
387 * file capabilities.
388 */
389 if (j->flags.capbset_drop) {
390 warn("overriding bounding set configuration");
391 j->cap_bset = 0;
392 j->flags.capbset_drop = 0;
393 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400394 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800395 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400396}
397
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800398void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
399{
400 if (j->flags.use_caps) {
401 /*
402 * 'minijail_use_caps' will have already configured a capability
403 * bounding set matching the (permitted|inheritable|effective)
404 * sets. Abort if the user tries to configure a separate
405 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
406 * are mutually exclusive.
407 */
408 die("runtime capabilities already configured, can't drop "
409 "bounding set separately");
410 }
411 j->cap_bset = capmask;
412 j->flags.capbset_drop = 1;
413}
414
415void API minijail_reset_signal_mask(struct minijail *j)
416{
Peter Qiu2860c462015-12-16 15:13:06 -0800417 j->flags.reset_signal_mask = 1;
418}
419
Will Drewry6ac91122011-10-21 16:38:58 -0500420void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400421{
422 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400423}
424
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700425void API minijail_namespace_enter_vfs(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);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700428 if (ns_fd < 0) {
429 pdie("failed to open namespace '%s'", ns_path);
430 }
431 j->mountns_fd = ns_fd;
432 j->flags.enter_vfs = 1;
433}
434
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800435void API minijail_skip_remount_private(struct minijail *j)
436{
437 j->flags.skip_remount_private = 1;
438}
439
Will Drewry6ac91122011-10-21 16:38:58 -0500440void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400441{
Elly Jonese58176c2012-01-23 11:46:17 -0500442 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700443 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400444 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800445 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400446}
447
Dylan Reidf7942472015-11-18 17:55:26 -0800448void API minijail_namespace_ipc(struct minijail *j)
449{
450 j->flags.ipc = 1;
451}
452
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400453void API minijail_namespace_net(struct minijail *j)
454{
455 j->flags.net = 1;
456}
457
Dylan Reid1102f5a2015-09-15 11:52:20 -0700458void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
459{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800460 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700461 if (ns_fd < 0) {
462 pdie("failed to open namespace '%s'", ns_path);
463 }
464 j->netns_fd = ns_fd;
465 j->flags.enter_net = 1;
466}
467
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700468void API minijail_namespace_cgroups(struct minijail *j)
469{
470 j->flags.ns_cgroups = 1;
471}
472
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700473void API minijail_close_open_fds(struct minijail *j)
474{
475 j->flags.close_open_fds = 1;
476}
477
Dylan Reid791f5772015-09-14 20:02:42 -0700478void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400479{
480 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700481 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400482}
483
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800484void API minijail_namespace_user(struct minijail *j)
485{
486 j->flags.userns = 1;
487}
488
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400489void API minijail_namespace_user_disable_setgroups(struct minijail *j)
490{
491 j->flags.disable_setgroups = 1;
492}
493
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800494int API minijail_uidmap(struct minijail *j, const char *uidmap)
495{
496 j->uidmap = strdup(uidmap);
497 if (!j->uidmap)
498 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800499 char *ch;
500 for (ch = j->uidmap; *ch; ch++) {
501 if (*ch == ',')
502 *ch = '\n';
503 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800504 return 0;
505}
506
507int API minijail_gidmap(struct minijail *j, const char *gidmap)
508{
509 j->gidmap = strdup(gidmap);
510 if (!j->gidmap)
511 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800512 char *ch;
513 for (ch = j->gidmap; *ch; ch++) {
514 if (*ch == ',')
515 *ch = '\n';
516 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800517 return 0;
518}
519
Will Drewry6ac91122011-10-21 16:38:58 -0500520void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400521{
Lutz Justen13807cb2017-01-03 17:11:55 +0100522 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400523}
524
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800525void API minijail_run_as_init(struct minijail *j)
526{
527 /*
528 * Since the jailed program will become 'init' in the new PID namespace,
529 * Minijail does not need to fork an 'init' process.
530 */
531 j->flags.do_init = 0;
532}
533
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700534int API minijail_enter_chroot(struct minijail *j, const char *dir)
535{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400536 if (j->chrootdir)
537 return -EINVAL;
538 j->chrootdir = strdup(dir);
539 if (!j->chrootdir)
540 return -ENOMEM;
541 j->flags.chroot = 1;
542 return 0;
543}
544
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800545int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
546{
547 if (j->chrootdir)
548 return -EINVAL;
549 j->chrootdir = strdup(dir);
550 if (!j->chrootdir)
551 return -ENOMEM;
552 j->flags.pivot_root = 1;
553 return 0;
554}
555
Dylan Reida14e08d2015-10-22 21:05:29 -0700556char API *minijail_get_original_path(struct minijail *j,
557 const char *path_inside_chroot)
558{
Dylan Reid648b2202015-10-23 00:50:00 -0700559 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700560
Dylan Reid648b2202015-10-23 00:50:00 -0700561 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700562 while (b) {
563 /*
564 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700565 * mount, then the original path is exactly the source of
566 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700567 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700568 * mount source = /some/path/exe, mount dest =
569 * /chroot/path/exe Then when getting the original path of
570 * "/chroot/path/exe", the source of that mount,
571 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700572 */
573 if (!strcmp(b->dest, path_inside_chroot))
574 return strdup(b->src);
575
576 /*
577 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700578 * mount, take the suffix of the chroot path relative to the
579 * mount destination path, and append it to the mount source
580 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700581 */
582 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
583 const char *relative_path =
584 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400585 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700586 }
587 b = b->next;
588 }
589
590 /* If there is a chroot path, append |path_inside_chroot| to that. */
591 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400592 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700593
594 /* No chroot, so the path outside is the same as it is inside. */
595 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700596}
597
Lee Campbell11af0622014-05-22 12:36:04 -0700598void API minijail_mount_tmp(struct minijail *j)
599{
600 j->flags.mount_tmp = 1;
601}
602
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800603int API minijail_write_pid_file(struct minijail *j, const char *path)
604{
605 j->pid_file_path = strdup(path);
606 if (!j->pid_file_path)
607 return -ENOMEM;
608 j->flags.pid_file = 1;
609 return 0;
610}
611
Dylan Reid605ce7f2016-01-19 19:21:00 -0800612int API minijail_add_to_cgroup(struct minijail *j, const char *path)
613{
614 if (j->cgroup_count >= MAX_CGROUPS)
615 return -ENOMEM;
616 j->cgroups[j->cgroup_count] = strdup(path);
617 if (!j->cgroups[j->cgroup_count])
618 return -ENOMEM;
619 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800620 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800621 return 0;
622}
623
Dylan Reid81e23972016-05-18 14:06:35 -0700624int API minijail_mount_with_data(struct minijail *j, const char *src,
625 const char *dest, const char *type,
626 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700627{
Dylan Reid648b2202015-10-23 00:50:00 -0700628 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400629
630 if (*dest != '/')
631 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700632 m = calloc(1, sizeof(*m));
633 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400634 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700635 m->dest = strdup(dest);
636 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400637 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700638 m->src = strdup(src);
639 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400640 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700641 m->type = strdup(type);
642 if (!m->type)
643 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700644 if (data) {
645 m->data = strdup(data);
646 if (!m->data)
647 goto error;
648 m->has_data = 1;
649 }
Dylan Reid648b2202015-10-23 00:50:00 -0700650 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400651
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800652 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400653
Elly Jonesdd3e8512012-01-23 15:13:38 -0500654 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700655 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400656 * containing vfs namespace.
657 */
658 minijail_namespace_vfs(j);
659
Dylan Reid648b2202015-10-23 00:50:00 -0700660 if (j->mounts_tail)
661 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400662 else
Dylan Reid648b2202015-10-23 00:50:00 -0700663 j->mounts_head = m;
664 j->mounts_tail = m;
665 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400666
667 return 0;
668
669error:
Dylan Reid81e23972016-05-18 14:06:35 -0700670 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700671 free(m->src);
672 free(m->dest);
673 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400674 return -ENOMEM;
675}
676
Dylan Reid81e23972016-05-18 14:06:35 -0700677int API minijail_mount(struct minijail *j, const char *src, const char *dest,
678 const char *type, unsigned long flags)
679{
680 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
681}
682
Dylan Reid648b2202015-10-23 00:50:00 -0700683int API minijail_bind(struct minijail *j, const char *src, const char *dest,
684 int writeable)
685{
686 unsigned long flags = MS_BIND;
687
688 if (!writeable)
689 flags |= MS_RDONLY;
690
691 return minijail_mount(j, src, dest, "", flags);
692}
693
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400694static void clear_seccomp_options(struct minijail *j)
695{
696 j->flags.seccomp_filter = 0;
697 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400698 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400699 j->filter_len = 0;
700 j->filter_prog = NULL;
701 j->flags.no_new_privs = 0;
702}
703
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400704static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400705{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400706 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400707 /*
708 * |errno| will be set to EINVAL when seccomp has not been
709 * compiled into the kernel. On certain platforms and kernel
710 * versions this is not a fatal failure. In that case, and only
711 * in that case, disable seccomp and skip loading the filters.
712 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400713 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400714 warn("not loading seccomp filters, seccomp filter not "
715 "supported");
716 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400717 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700718 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400719 /*
720 * If |errno| != EINVAL or seccomp_can_softfail() is false,
721 * we can proceed. Worst case scenario minijail_enter() will
722 * abort() if seccomp fails.
723 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700724 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400725 if (j->flags.seccomp_filter_tsync) {
726 /* Are the seccomp(2) syscall and the TSYNC option supported? */
727 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
728 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
729 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400730 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
731 warn("seccomp(2) syscall not supported");
732 clear_seccomp_options(j);
733 return 0;
734 } else if (saved_errno == EINVAL &&
735 seccomp_can_softfail()) {
736 warn(
737 "seccomp filter thread sync not supported");
738 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400739 return 0;
740 }
741 /*
742 * Similar logic here. If seccomp_can_softfail() is
743 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
744 * we can proceed. Worst case scenario minijail_enter()
745 * will abort() if seccomp or TSYNC fail.
746 */
747 }
748 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400749 return 1;
750}
751
752static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
753{
754 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400755 int use_ret_trap =
756 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
757 int allow_logging = j->flags.seccomp_filter_logging;
758
759 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400760 free(fprog);
761 return -1;
762 }
763
764 j->filter_len = fprog->len;
765 j->filter_prog = fprog;
766 return 0;
767}
768
769void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
770{
771 if (!seccomp_should_parse_filters(j))
772 return;
773
Elly Jonese1749eb2011-10-07 13:54:59 -0400774 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800775 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700776 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400777 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800778
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400779 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700780 die("failed to compile seccomp filter BPF program in '%s'",
781 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800782 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400783 fclose(file);
784}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800785
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400786void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
787{
788 if (!seccomp_should_parse_filters(j))
789 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800790
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400791 FILE *file = fdopen(fd, "r");
792 if (!file) {
793 pdie("failed to associate stream with fd %d", fd);
794 }
795
796 if (parse_seccomp_filters(j, file) != 0) {
797 die("failed to compile seccomp filter BPF program from fd %d",
798 fd);
799 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400800 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500801}
802
Andrew Brestickereac28942015-11-11 16:04:46 -0800803int API minijail_use_alt_syscall(struct minijail *j, const char *table)
804{
805 j->alt_syscall_table = strdup(table);
806 if (!j->alt_syscall_table)
807 return -ENOMEM;
808 j->flags.alt_syscall = 1;
809 return 0;
810}
811
Will Drewryf89aef52011-09-16 16:48:57 -0500812struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400813 size_t available;
814 size_t total;
815 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500816};
817
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800818void marshal_state_init(struct marshal_state *state, char *buf,
819 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400820{
821 state->available = available;
822 state->buf = buf;
823 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500824}
825
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800826void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400827{
828 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500829
Elly Jonese1749eb2011-10-07 13:54:59 -0400830 /* Up to |available| will be written. */
831 if (copy_len) {
832 memcpy(state->buf, src, copy_len);
833 state->buf += copy_len;
834 state->available -= copy_len;
835 }
836 /* |total| will contain the expected length. */
837 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500838}
839
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400840void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700841{
842 marshal_append(state, m->src, strlen(m->src) + 1);
843 marshal_append(state, m->dest, strlen(m->dest) + 1);
844 marshal_append(state, m->type, strlen(m->type) + 1);
845 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
846 if (m->has_data)
847 marshal_append(state, m->data, strlen(m->data) + 1);
848 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
849}
850
Will Drewry6ac91122011-10-21 16:38:58 -0500851void minijail_marshal_helper(struct marshal_state *state,
852 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400853{
Dylan Reid648b2202015-10-23 00:50:00 -0700854 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800855 size_t i;
856
Elly Jonese1749eb2011-10-07 13:54:59 -0400857 marshal_append(state, (char *)j, sizeof(*j));
858 if (j->user)
859 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800860 if (j->suppl_gid_list) {
861 marshal_append(state, j->suppl_gid_list,
862 j->suppl_gid_count * sizeof(gid_t));
863 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400864 if (j->chrootdir)
865 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800866 if (j->alt_syscall_table) {
867 marshal_append(state, j->alt_syscall_table,
868 strlen(j->alt_syscall_table) + 1);
869 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800870 if (j->flags.seccomp_filter && j->filter_prog) {
871 struct sock_fprog *fp = j->filter_prog;
872 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800873 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400874 }
Dylan Reid648b2202015-10-23 00:50:00 -0700875 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400876 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400877 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800878 for (i = 0; i < j->cgroup_count; ++i)
879 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500880}
881
Will Drewry6ac91122011-10-21 16:38:58 -0500882size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400883{
884 struct marshal_state state;
885 marshal_state_init(&state, NULL, 0);
886 minijail_marshal_helper(&state, j);
887 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500888}
889
Elly Jonese1749eb2011-10-07 13:54:59 -0400890int minijail_marshal(const struct minijail *j, char *buf, size_t available)
891{
892 struct marshal_state state;
893 marshal_state_init(&state, buf, available);
894 minijail_marshal_helper(&state, j);
895 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500896}
897
Elly Jonese1749eb2011-10-07 13:54:59 -0400898int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
899{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800900 size_t i;
901 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500902 int ret = -EINVAL;
903
Elly Jonese1749eb2011-10-07 13:54:59 -0400904 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500905 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400906 memcpy((void *)j, serialized, sizeof(*j));
907 serialized += sizeof(*j);
908 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500909
Will Drewrybee7ba72011-10-21 20:47:01 -0500910 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400911 j->pid_file_path = NULL;
912 j->uidmap = NULL;
913 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700914 j->mounts_head = NULL;
915 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800916 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500917
Elly Jonese1749eb2011-10-07 13:54:59 -0400918 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400919 char *user = consumestr(&serialized, &length);
920 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500921 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400922 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500923 if (!j->user)
924 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400925 }
Will Drewryf89aef52011-09-16 16:48:57 -0500926
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800927 if (j->suppl_gid_list) { /* stale pointer */
928 if (j->suppl_gid_count > NGROUPS_MAX) {
929 goto bad_gid_list;
930 }
931 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
932 void *gid_list_bytes =
933 consumebytes(gid_list_size, &serialized, &length);
934 if (!gid_list_bytes)
935 goto bad_gid_list;
936
937 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
938 if (!j->suppl_gid_list)
939 goto bad_gid_list;
940
941 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
942 }
943
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400944 if (j->chrootdir) { /* stale pointer */
945 char *chrootdir = consumestr(&serialized, &length);
946 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500947 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400948 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500949 if (!j->chrootdir)
950 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400951 }
952
Andrew Brestickereac28942015-11-11 16:04:46 -0800953 if (j->alt_syscall_table) { /* stale pointer */
954 char *alt_syscall_table = consumestr(&serialized, &length);
955 if (!alt_syscall_table)
956 goto bad_syscall_table;
957 j->alt_syscall_table = strdup(alt_syscall_table);
958 if (!j->alt_syscall_table)
959 goto bad_syscall_table;
960 }
961
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800962 if (j->flags.seccomp_filter && j->filter_len > 0) {
963 size_t ninstrs = j->filter_len;
964 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
965 ninstrs > USHRT_MAX)
966 goto bad_filters;
967
968 size_t program_len = ninstrs * sizeof(struct sock_filter);
969 void *program = consumebytes(program_len, &serialized, &length);
970 if (!program)
971 goto bad_filters;
972
973 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800974 if (!j->filter_prog)
975 goto bad_filters;
976
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800977 j->filter_prog->len = ninstrs;
978 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800979 if (!j->filter_prog->filter)
980 goto bad_filter_prog_instrs;
981
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800982 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400983 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400984
Dylan Reid648b2202015-10-23 00:50:00 -0700985 count = j->mounts_count;
986 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400987 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700988 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -0700989 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400990 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700991 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -0700992 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400993 const char *src = consumestr(&serialized, &length);
994 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700995 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400996 dest = consumestr(&serialized, &length);
997 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700998 goto bad_mounts;
999 type = consumestr(&serialized, &length);
1000 if (!type)
1001 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001002 has_data = consumebytes(sizeof(*has_data), &serialized,
1003 &length);
1004 if (!has_data)
1005 goto bad_mounts;
1006 if (*has_data) {
1007 data = consumestr(&serialized, &length);
1008 if (!data)
1009 goto bad_mounts;
1010 }
Dylan Reid648b2202015-10-23 00:50:00 -07001011 flags = consumebytes(sizeof(*flags), &serialized, &length);
1012 if (!flags)
1013 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001014 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001015 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001016 }
1017
Dylan Reid605ce7f2016-01-19 19:21:00 -08001018 count = j->cgroup_count;
1019 j->cgroup_count = 0;
1020 for (i = 0; i < count; ++i) {
1021 char *cgroup = consumestr(&serialized, &length);
1022 if (!cgroup)
1023 goto bad_cgroups;
1024 j->cgroups[i] = strdup(cgroup);
1025 if (!j->cgroups[i])
1026 goto bad_cgroups;
1027 ++j->cgroup_count;
1028 }
1029
Elly Jonese1749eb2011-10-07 13:54:59 -04001030 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001031
Dylan Reid605ce7f2016-01-19 19:21:00 -08001032bad_cgroups:
1033 while (j->mounts_head) {
1034 struct mountpoint *m = j->mounts_head;
1035 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001036 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001037 free(m->type);
1038 free(m->dest);
1039 free(m->src);
1040 free(m);
1041 }
1042 for (i = 0; i < j->cgroup_count; ++i)
1043 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001044bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001045 if (j->flags.seccomp_filter && j->filter_len > 0) {
1046 free(j->filter_prog->filter);
1047 free(j->filter_prog);
1048 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001049bad_filter_prog_instrs:
1050 if (j->filter_prog)
1051 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001052bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001053 if (j->alt_syscall_table)
1054 free(j->alt_syscall_table);
1055bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001056 if (j->chrootdir)
1057 free(j->chrootdir);
1058bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001059 if (j->suppl_gid_list)
1060 free(j->suppl_gid_list);
1061bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001062 if (j->user)
1063 free(j->user);
1064clear_pointers:
1065 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001066 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001067 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001068 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001069 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001070out:
1071 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001072}
1073
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001074/*
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001075 * setup_mount_destination: Ensures the mount target exists.
1076 * Creates it if needed and possible.
Dylan Reideec77962016-06-30 19:35:10 -07001077 */
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001078static int setup_mount_destination(const char *source, const char *dest,
1079 uid_t uid, uid_t gid)
Dylan Reideec77962016-06-30 19:35:10 -07001080{
1081 int rc;
1082 struct stat st_buf;
1083
1084 rc = stat(dest, &st_buf);
1085 if (rc == 0) /* destination exists */
1086 return 0;
1087
1088 /*
1089 * Try to create the destination.
1090 * Either make a directory or touch a file depending on the source type.
1091 * If the source doesn't exist, assume it is a filesystem type such as
1092 * "tmpfs" and create a directory to mount it on.
1093 */
1094 rc = stat(source, &st_buf);
1095 if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) {
1096 if (mkdir(dest, 0700))
1097 return -errno;
1098 } else {
1099 int fd = open(dest, O_RDWR | O_CREAT, 0700);
1100 if (fd < 0)
1101 return -errno;
1102 close(fd);
1103 }
1104 return chown(dest, uid, gid);
1105}
1106
1107/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001108 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001109 * @j Minijail these mounts are for
1110 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001111 *
1112 * Returns 0 for success.
1113 */
Dylan Reid648b2202015-10-23 00:50:00 -07001114static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001115{
Dylan Reid648b2202015-10-23 00:50:00 -07001116 int ret;
1117 char *dest;
1118 int remount_ro = 0;
1119
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001120 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001121 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001122 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001123
Dylan Reideec77962016-06-30 19:35:10 -07001124 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1125 pdie("creating mount target '%s' failed", dest);
1126
Dylan Reid648b2202015-10-23 00:50:00 -07001127 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001128 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1129 * can't both be specified in the original bind mount.
1130 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001131 */
1132 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1133 remount_ro = 1;
1134 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001135 }
Dylan Reid648b2202015-10-23 00:50:00 -07001136
Dylan Reid81e23972016-05-18 14:06:35 -07001137 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001138 if (ret)
1139 pdie("mount: %s -> %s", m->src, dest);
1140
1141 if (remount_ro) {
1142 m->flags |= MS_RDONLY;
1143 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001144 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001145 if (ret)
1146 pdie("bind ro: %s -> %s", m->src, dest);
1147 }
1148
Elly Jones51a5b6c2011-10-12 19:09:26 -04001149 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001150 if (m->next)
1151 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001152 return ret;
1153}
1154
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001155static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001156{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001157 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001158
1159 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001160 return ret;
1161
1162 if (chroot(j->chrootdir))
1163 return -errno;
1164
1165 if (chdir("/"))
1166 return -errno;
1167
1168 return 0;
1169}
1170
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001171static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001172{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001173 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001174
1175 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001176 return ret;
1177
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001178 /*
1179 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001180 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001181 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001182 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001183 if (oldroot < 0)
1184 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001185 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001186 if (newroot < 0)
1187 pdie("failed to open %s for fchdir", j->chrootdir);
1188
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001189 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001190 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001191 * do a self bind mount.
1192 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001193 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1194 pdie("failed to bind mount '%s'", j->chrootdir);
1195 if (chdir(j->chrootdir))
1196 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001197 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001198 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001199
1200 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001201 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001202 * change to the old root and unmount it.
1203 */
1204 if (fchdir(oldroot))
1205 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001206
1207 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001208 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1209 * there could be a shared mount point under |oldroot|. In that case,
1210 * mounts under this shared mount point will be unmounted below, and
1211 * this unmounting will propagate to the original mount namespace
1212 * (because the mount point is shared). To prevent this unexpected
1213 * unmounting, remove these mounts from their peer groups by recursively
1214 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001215 */
1216 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001217 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001218 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001219 if (umount2(".", MNT_DETACH))
1220 pdie("umount(/)");
1221 /* Change back to the new root. */
1222 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001223 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001224 if (close(oldroot))
1225 return -errno;
1226 if (close(newroot))
1227 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001228 if (chroot("/"))
1229 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001230 /* Set correct CWD for getcwd(3). */
1231 if (chdir("/"))
1232 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001233
1234 return 0;
1235}
1236
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001237static int mount_tmp(void)
Lee Campbell11af0622014-05-22 12:36:04 -07001238{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001239 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -07001240}
1241
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001242static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001243{
1244 const char *kProcPath = "/proc";
1245 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001246 /*
1247 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001248 * /proc in our namespace, which means using MS_REMOUNT here would
1249 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001250 * namespace (!). Instead, remove their mount from our namespace lazily
1251 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001252 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001253 if (umount2(kProcPath, MNT_DETACH)) {
1254 /*
1255 * If we are in a new user namespace, umount(2) will fail.
1256 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1257 */
1258 if (j->flags.userns) {
1259 info("umount(/proc, MNT_DETACH) failed, "
1260 "this is expected when using user namespaces");
1261 } else {
1262 return -errno;
1263 }
1264 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001265 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
1266 return -errno;
1267 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001268}
1269
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001270static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001271{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001272 kill(j->initpid, SIGKILL);
1273 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001274}
1275
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001276static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001277{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001278 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001279 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001280}
1281
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001282static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001283{
1284 size_t i;
1285
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001286 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001287 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001288 kill_child_and_die(j, "failed to add to cgroups");
1289 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001290}
1291
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001292static void write_ugid_maps_or_die(const struct minijail *j)
1293{
1294 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1295 kill_child_and_die(j, "failed to write uid_map");
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -04001296 if (j->gidmap && j->flags.disable_setgroups &&
1297 write_proc_file(j->initpid, "deny", "setgroups") != 0)
1298 kill_child_and_die(j, "failed to disable setgroups(2)");
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001299 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1300 kill_child_and_die(j, "failed to write gid_map");
1301}
1302
1303static void enter_user_namespace(const struct minijail *j)
1304{
1305 if (j->uidmap && setresuid(0, 0, 0))
1306 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1307 if (j->gidmap && setresgid(0, 0, 0))
1308 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1309}
1310
1311static void parent_setup_complete(int *pipe_fds)
1312{
1313 close(pipe_fds[0]);
1314 close(pipe_fds[1]);
1315}
1316
1317/*
1318 * wait_for_parent_setup: Called by the child process to wait for any
1319 * further parent-side setup to complete before continuing.
1320 */
1321static void wait_for_parent_setup(int *pipe_fds)
1322{
1323 char buf;
1324
1325 close(pipe_fds[1]);
1326
1327 /* Wait for parent to complete setup and close the pipe. */
1328 if (read(pipe_fds[0], &buf, 1) != 0)
1329 die("failed to sync with parent");
1330 close(pipe_fds[0]);
1331}
1332
1333static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001334{
Lutz Justen13807cb2017-01-03 17:11:55 +01001335 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1336 j->flags.set_suppl_gids > 1) {
1337 die("can only either inherit, keep or set supplementary groups;"
1338 " tried to do two or more");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001339 }
1340
Lutz Justen13807cb2017-01-03 17:11:55 +01001341 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001342 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001343 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001344 } else if (j->flags.set_suppl_gids) {
1345 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001346 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001347 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001348 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001349 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001350 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001351 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001352 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001353 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001354 }
1355
1356 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001357 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001358
1359 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001360 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001361}
1362
Mike Frysinger3adfef72013-05-09 17:19:08 -04001363/*
1364 * We specifically do not use cap_valid() as that only tells us the last
1365 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001366 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001367 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001368 * kernel).
1369 * Normally, we suck up the answer via /proc. On Android, not all processes are
1370 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1371 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001372 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001373static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001374{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001375 unsigned int last_valid_cap = 0;
1376 if (is_android()) {
1377 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1378 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001379
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001380 /* |last_valid_cap| will be the first failing value. */
1381 if (last_valid_cap > 0) {
1382 last_valid_cap--;
1383 }
1384 } else {
1385 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1386 FILE *fp = fopen(cap_file, "re");
1387 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1388 pdie("fscanf(%s)", cap_file);
1389 fclose(fp);
1390 }
Dylan Reidf682d472015-09-17 21:39:07 -07001391 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001392}
1393
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001394static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1395{
1396 const uint64_t one = 1;
1397 unsigned int i;
1398 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1399 if (keep_mask & (one << i))
1400 continue;
1401 if (prctl(PR_CAPBSET_DROP, i))
1402 pdie("could not drop capability from bounding set");
1403 }
1404}
1405
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001406static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001407{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001408 if (!j->flags.use_caps)
1409 return;
1410
Elly Jonese1749eb2011-10-07 13:54:59 -04001411 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001412 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001413 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001414 unsigned int i;
1415 if (!caps)
1416 die("can't get process caps");
1417 if (cap_clear_flag(caps, CAP_INHERITABLE))
1418 die("can't clear inheritable caps");
1419 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1420 die("can't clear effective caps");
1421 if (cap_clear_flag(caps, CAP_PERMITTED))
1422 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001423 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001424 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001425 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001426 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001427 flag[0] = i;
1428 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001429 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001430 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001431 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001432 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001433 die("can't add inheritable cap");
1434 }
1435 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001436 die("can't apply initial cleaned capset");
1437
1438 /*
1439 * Instead of dropping bounding set first, do it here in case
1440 * the caller had a more permissive bounding set which could
1441 * have been used above to raise a capability that wasn't already
1442 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1443 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001444 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001445
1446 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001447 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001448 flag[0] = CAP_SETPCAP;
1449 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1450 die("can't clear effective cap");
1451 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1452 die("can't clear permitted cap");
1453 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1454 die("can't clear inheritable cap");
1455 }
1456
1457 if (cap_set_proc(caps))
1458 die("can't apply final cleaned capset");
1459
1460 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001461}
1462
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001463static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001464{
1465 /*
1466 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1467 * in the kernel source tree for an explanation of the parameters.
1468 */
1469 if (j->flags.no_new_privs) {
1470 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1471 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1472 }
1473
1474 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001475 * Code running with ASan
1476 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1477 * will make system calls not included in the syscall filter policy,
1478 * which will likely crash the program. Skip setting seccomp filter in
1479 * that case.
1480 * 'running_with_asan()' has no inputs and is completely defined at
1481 * build time, so this cannot be used by an attacker to skip setting
1482 * seccomp filter.
1483 */
1484 if (j->flags.seccomp_filter && running_with_asan()) {
1485 warn("running with ASan, not setting seccomp filter");
1486 return;
1487 }
1488
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001489 if (j->flags.seccomp_filter) {
1490 if (j->flags.seccomp_filter_logging) {
1491 /*
1492 * If logging seccomp filter failures,
1493 * install the SIGSYS handler first.
1494 */
1495 if (install_sigsys_handler())
1496 pdie("failed to install SIGSYS handler");
1497 warn("logging seccomp filter failures");
1498 } else if (j->flags.seccomp_filter_tsync) {
1499 /*
1500 * If setting thread sync,
1501 * reset the SIGSYS signal handler so that
1502 * the entire thread group is killed.
1503 */
1504 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1505 pdie("failed to reset SIGSYS disposition");
1506 info("reset SIGSYS disposition");
1507 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001508 }
1509
1510 /*
1511 * Install the syscall filter.
1512 */
1513 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001514 if (j->flags.seccomp_filter_tsync) {
1515 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1516 SECCOMP_FILTER_FLAG_TSYNC,
1517 j->filter_prog)) {
1518 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001519 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001520 } else {
1521 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1522 j->filter_prog)) {
1523 pdie("prctl(seccomp_filter) failed");
1524 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001525 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001526 }
1527}
1528
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001529static void config_net_loopback(void)
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001530{
1531 static const char ifname[] = "lo";
1532 int sock;
1533 struct ifreq ifr;
1534
1535 /* Make sure people don't try to add really long names. */
1536 _Static_assert(sizeof(ifname) <= IFNAMSIZ, "interface name too long");
1537
1538 sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0);
1539 if (sock < 0)
1540 pdie("socket(AF_LOCAL) failed");
1541
1542 /*
1543 * Do the equiv of `ip link set up lo`. The kernel will assign
1544 * IPv4 (127.0.0.1) & IPv6 (::1) addresses automatically!
1545 */
1546 strcpy(ifr.ifr_name, ifname);
1547 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0)
1548 pdie("ioctl(SIOCGIFFLAGS) failed");
1549
1550 /* The kernel preserves ifr.ifr_name for use. */
1551 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
1552 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0)
1553 pdie("ioctl(SIOCSIFFLAGS) failed");
1554
1555 close(sock);
1556}
1557
Will Drewry6ac91122011-10-21 16:38:58 -05001558void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001559{
Dylan Reidf682d472015-09-17 21:39:07 -07001560 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001561 * If we're dropping caps, get the last valid cap from /proc now,
1562 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001563 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001564 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001565 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001566 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001567
Elly Jonese1749eb2011-10-07 13:54:59 -04001568 if (j->flags.pids)
1569 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001570 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001571
Lutz Justen13807cb2017-01-03 17:11:55 +01001572 if (j->flags.inherit_suppl_gids && !j->user)
Elly Jonese1749eb2011-10-07 13:54:59 -04001573 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001574
Elly Jonesdd3e8512012-01-23 15:13:38 -05001575 /*
1576 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001577 * so we don't even try. If any of our operations fail, we abort() the
1578 * entire process.
1579 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001580 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001581 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001582
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001583 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001584 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001585 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001586 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001587 * Unless asked not to, remount all filesystems as private.
1588 * If they are shared, new bind mounts will creep out of our
1589 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001590 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1591 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001592 if (!j->flags.skip_remount_private) {
1593 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001594 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1595 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001596 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001597 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001598
Dylan Reidf7942472015-11-18 17:55:26 -08001599 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001600 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001601 }
1602
Dylan Reid1102f5a2015-09-15 11:52:20 -07001603 if (j->flags.enter_net) {
1604 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001605 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001606 } else if (j->flags.net) {
1607 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001608 pdie("unshare(CLONE_NEWNET) failed");
1609 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001610 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001611
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001612 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001613 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001614
Elly Jones51a5b6c2011-10-12 19:09:26 -04001615 if (j->flags.chroot && enter_chroot(j))
1616 pdie("chroot");
1617
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001618 if (j->flags.pivot_root && enter_pivot_root(j))
1619 pdie("pivot_root");
1620
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001621 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001622 pdie("mount_tmp");
1623
Dylan Reid791f5772015-09-14 20:02:42 -07001624 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001625 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001626
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001627 /*
1628 * If we're only dropping capabilities from the bounding set, but not
1629 * from the thread's (permitted|inheritable|effective) sets, do it now.
1630 */
1631 if (j->flags.capbset_drop) {
1632 drop_capbset(j->cap_bset, last_valid_cap);
1633 }
1634
1635 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001636 /*
1637 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001638 * capability to change uids, our attempt to use setuid()
1639 * below will fail. Hang on to root caps across setuid(), then
1640 * lock securebits.
1641 */
1642 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001643 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001644
1645 /*
1646 * Kernels 4.3+ define a new securebit
1647 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1648 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1649 * return EPERM on older kernels. Detect this, and retry with
1650 * the right mask for older (2.6.26-4.2) kernels.
1651 */
1652 int securebits_ret = prctl(PR_SET_SECUREBITS,
1653 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1654 if (securebits_ret < 0) {
1655 if (errno == EPERM) {
1656 /* Possibly running on kernel < 4.3. */
1657 securebits_ret = prctl(
1658 PR_SET_SECUREBITS,
1659 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1660 }
1661 }
1662 if (securebits_ret < 0)
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001663 pdie("prctl(PR_SET_SECUREBITS) failed");
Elly Jonese1749eb2011-10-07 13:54:59 -04001664 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001665
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001666 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001667 /*
1668 * If we're setting no_new_privs, we can drop privileges
1669 * before setting seccomp filter. This way filter policies
1670 * don't need to allow privilege-dropping syscalls.
1671 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001672 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001673 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001674 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001675 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001676 /*
1677 * If we're not setting no_new_privs,
1678 * we need to set seccomp filter *before* dropping privileges.
1679 * WARNING: this means that filter policies *must* allow
1680 * setgroups()/setresgid()/setresuid() for dropping root and
1681 * capget()/capset()/prctl() for dropping caps.
1682 */
1683 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001684 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001685 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001686 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001687
Elly Jonesdd3e8512012-01-23 15:13:38 -05001688 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001689 * Select the specified alternate syscall table. The table must not
1690 * block prctl(2) if we're using seccomp as well.
1691 */
1692 if (j->flags.alt_syscall) {
1693 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001694 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001695 }
1696
1697 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001698 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001699 * privilege-dropping syscalls :)
1700 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001701 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001702 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001703 warn("seccomp not supported");
1704 return;
1705 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001706 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001707 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001708}
1709
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001710/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001711static int init_exitstatus = 0;
1712
Will Drewry6ac91122011-10-21 16:38:58 -05001713void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001714{
1715 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001716}
1717
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001718void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001719{
1720 pid_t pid;
1721 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001722 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001723 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001724 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001725 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001726 /*
1727 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001728 * left inside our pid namespace or we get a signal.
1729 */
1730 if (pid == rootpid)
1731 init_exitstatus = status;
1732 }
1733 if (!WIFEXITED(init_exitstatus))
1734 _exit(MINIJAIL_ERR_INIT);
1735 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001736}
1737
Will Drewry6ac91122011-10-21 16:38:58 -05001738int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001739{
1740 size_t sz = 0;
1741 size_t bytes = read(fd, &sz, sizeof(sz));
1742 char *buf;
1743 int r;
1744 if (sizeof(sz) != bytes)
1745 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001746 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001747 return -E2BIG;
1748 buf = malloc(sz);
1749 if (!buf)
1750 return -ENOMEM;
1751 bytes = read(fd, buf, sz);
1752 if (bytes != sz) {
1753 free(buf);
1754 return -EINVAL;
1755 }
1756 r = minijail_unmarshal(j, buf, sz);
1757 free(buf);
1758 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001759}
1760
Will Drewry6ac91122011-10-21 16:38:58 -05001761int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001762{
1763 char *buf;
1764 size_t sz = minijail_size(j);
1765 ssize_t written;
1766 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001767
Elly Jonese1749eb2011-10-07 13:54:59 -04001768 if (!sz)
1769 return -EINVAL;
1770 buf = malloc(sz);
1771 r = minijail_marshal(j, buf, sz);
1772 if (r) {
1773 free(buf);
1774 return r;
1775 }
1776 /* Sends [size][minijail]. */
1777 written = write(fd, &sz, sizeof(sz));
1778 if (written != sizeof(sz)) {
1779 free(buf);
1780 return -EFAULT;
1781 }
1782 written = write(fd, buf, sz);
1783 if (written < 0 || (size_t) written != sz) {
1784 free(buf);
1785 return -EFAULT;
1786 }
1787 free(buf);
1788 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001789}
Elly Jonescd7a9042011-07-22 13:56:51 -04001790
Will Drewry6ac91122011-10-21 16:38:58 -05001791int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001792{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001793#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001794 /* Don't use LDPRELOAD on Brillo. */
1795 return 0;
1796#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001797 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1798 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1799 if (!newenv)
1800 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001801
Elly Jonese1749eb2011-10-07 13:54:59 -04001802 /* Only insert a separating space if we have something to separate... */
1803 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1804 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001805
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001806 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001807 setenv(kLdPreloadEnvVar, newenv, 1);
1808 free(newenv);
1809 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001810#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001811}
1812
Will Drewry6ac91122011-10-21 16:38:58 -05001813int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001814{
1815 int r = pipe(fds);
1816 char fd_buf[11];
1817 if (r)
1818 return r;
1819 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1820 if (r <= 0)
1821 return -EINVAL;
1822 setenv(kFdEnvVar, fd_buf, 1);
1823 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001824}
1825
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001826int setup_pipe_end(int fds[2], size_t index)
1827{
1828 if (index > 1)
1829 return -1;
1830
1831 close(fds[1 - index]);
1832 return fds[index];
1833}
1834
1835int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1836{
1837 if (index > 1)
1838 return -1;
1839
1840 close(fds[1 - index]);
1841 /* dup2(2) the corresponding end of the pipe into |fd|. */
1842 return dup2(fds[index], fd);
1843}
1844
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001845int close_open_fds(int *inheritable_fds, size_t size)
1846{
1847 const char *kFdPath = "/proc/self/fd";
1848
1849 DIR *d = opendir(kFdPath);
1850 struct dirent *dir_entry;
1851
1852 if (d == NULL)
1853 return -1;
1854 int dir_fd = dirfd(d);
1855 while ((dir_entry = readdir(d)) != NULL) {
1856 size_t i;
1857 char *end;
1858 bool should_close = true;
1859 const int fd = strtol(dir_entry->d_name, &end, 10);
1860
1861 if ((*end) != '\0') {
1862 continue;
1863 }
1864 /*
1865 * We might have set up some pipes that we want to share with
1866 * the parent process, and should not be closed.
1867 */
1868 for (i = 0; i < size; ++i) {
1869 if (fd == inheritable_fds[i]) {
1870 should_close = false;
1871 break;
1872 }
1873 }
1874 /* Also avoid closing the directory fd. */
1875 if (should_close && fd != dir_fd)
1876 close(fd);
1877 }
1878 closedir(d);
1879 return 0;
1880}
1881
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001882int minijail_run_internal(struct minijail *j, const char *filename,
1883 char *const argv[], pid_t *pchild_pid,
1884 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1885 int use_preload);
1886
Will Drewry6ac91122011-10-21 16:38:58 -05001887int API minijail_run(struct minijail *j, const char *filename,
1888 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001889{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001890 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1891 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001892}
1893
1894int API minijail_run_pid(struct minijail *j, const char *filename,
1895 char *const argv[], pid_t *pchild_pid)
1896{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001897 return minijail_run_internal(j, filename, argv, pchild_pid,
1898 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001899}
1900
1901int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001902 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001903{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001904 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1905 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001906}
1907
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001908int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001909 char *const argv[], pid_t *pchild_pid,
1910 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001911{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001912 return minijail_run_internal(j, filename, argv, pchild_pid,
1913 pstdin_fd, pstdout_fd, pstderr_fd, true);
1914}
1915
1916int API minijail_run_no_preload(struct minijail *j, const char *filename,
1917 char *const argv[])
1918{
1919 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1920 false);
1921}
1922
Samuel Tan63187f42015-10-16 13:01:53 -07001923int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001924 const char *filename,
1925 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001926 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001927 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001928 int *pstderr_fd)
1929{
Samuel Tan63187f42015-10-16 13:01:53 -07001930 return minijail_run_internal(j, filename, argv, pchild_pid,
1931 pstdin_fd, pstdout_fd, pstderr_fd, false);
1932}
1933
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001934int minijail_run_internal(struct minijail *j, const char *filename,
1935 char *const argv[], pid_t *pchild_pid,
1936 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1937 int use_preload)
1938{
Elly Jonese1749eb2011-10-07 13:54:59 -04001939 char *oldenv, *oldenv_copy = NULL;
1940 pid_t child_pid;
1941 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001942 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001943 int stdout_fds[2];
1944 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001945 int child_sync_pipe_fds[2];
1946 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001947 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001948 /* We need to remember this across the minijail_preexec() call. */
1949 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001950 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001951
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001952 if (use_preload) {
1953 oldenv = getenv(kLdPreloadEnvVar);
1954 if (oldenv) {
1955 oldenv_copy = strdup(oldenv);
1956 if (!oldenv_copy)
1957 return -ENOMEM;
1958 }
1959
1960 if (setup_preload())
1961 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001962 }
Will Drewryf89aef52011-09-16 16:48:57 -05001963
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001964 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04001965 if (j->flags.use_caps && j->caps != 0)
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001966 die("non-empty capabilities are not supported without "
1967 "LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001968 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001969
Elly Jonesdd3e8512012-01-23 15:13:38 -05001970 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05001971 * Make the process group ID of this process equal to its PID.
1972 * In the non-interactive case (e.g. when the parent process is started
1973 * from init) this ensures the parent process and the jailed process
1974 * can be killed together.
1975 * When the parent process is started from the console this ensures
1976 * the call to setsid(2) in the jailed process succeeds.
1977 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001978 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1979 * the process is already a process group leader.
1980 */
1981 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1982 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05001983 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001984 }
1985 }
1986
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001987 if (use_preload) {
1988 /*
1989 * Before we fork(2) and execve(2) the child process, we need
1990 * to open a pipe(2) to send the minijail configuration over.
1991 */
1992 if (setup_pipe(pipe_fds))
1993 return -EFAULT;
1994 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001995
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001996 /*
1997 * If we want to write to the child process' standard input,
1998 * create the pipe(2) now.
1999 */
2000 if (pstdin_fd) {
2001 if (pipe(stdin_fds))
2002 return -EFAULT;
2003 }
2004
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002005 /*
2006 * If we want to read from the child process' standard output,
2007 * create the pipe(2) now.
2008 */
2009 if (pstdout_fd) {
2010 if (pipe(stdout_fds))
2011 return -EFAULT;
2012 }
2013
2014 /*
2015 * If we want to read from the child process' standard error,
2016 * create the pipe(2) now.
2017 */
2018 if (pstderr_fd) {
2019 if (pipe(stderr_fds))
2020 return -EFAULT;
2021 }
2022
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002023 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002024 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002025 * or if we need to add the child process to cgroups, create the pipe(2)
2026 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002027 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002028 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002029 sync_child = 1;
2030 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002031 return -EFAULT;
2032 }
2033
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002034 /*
2035 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002036 *
2037 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2038 *
2039 * In multithreaded programs, there are a bunch of locks inside libc,
2040 * some of which may be held by other threads at the time that we call
2041 * minijail_run_pid(). If we call fork(), glibc does its level best to
2042 * ensure that we hold all of these locks before it calls clone()
2043 * internally and drop them after clone() returns, but when we call
2044 * sys_clone(2) directly, all that gets bypassed and we end up with a
2045 * child address space where some of libc's important locks are held by
2046 * other threads (which did not get cloned, and hence will never release
2047 * those locks). This is okay so long as we call exec() immediately
2048 * after, but a bunch of seemingly-innocent libc functions like setenv()
2049 * take locks.
2050 *
2051 * Hence, only call sys_clone() if we need to, in order to get at pid
2052 * namespacing. If we follow this path, the child's address space might
2053 * have broken locks; you may only call functions that do not acquire
2054 * any locks.
2055 *
2056 * Unfortunately, fork() acquires every lock it can get its hands on, as
2057 * previously detailed, so this function is highly likely to deadlock
2058 * later on (see "deadlock here") if we're multithreaded.
2059 *
2060 * We might hack around this by having the clone()d child (init of the
2061 * pid namespace) return directly, rather than leaving the clone()d
2062 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002063 * its fork()ed child return in turn), but that process would be
2064 * crippled with its libc locks potentially broken. We might try
2065 * fork()ing in the parent before we clone() to ensure that we own all
2066 * the locks, but then we have to have the forked child hanging around
2067 * consuming resources (and possibly having file descriptors / shared
2068 * memory regions / etc attached). We'd need to keep the child around to
2069 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002070 *
2071 * TODO(ellyjones): figure out if the "forked child hanging around"
2072 * problem is fixable or not. It would be nice if we worked in this
2073 * case.
2074 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002075 if (pid_namespace) {
2076 int clone_flags = CLONE_NEWPID | SIGCHLD;
2077 if (j->flags.userns)
2078 clone_flags |= CLONE_NEWUSER;
2079 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002080 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002081 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002082 }
Elly Jones761b7412012-06-13 15:49:52 -04002083
Elly Jonese1749eb2011-10-07 13:54:59 -04002084 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002085 if (use_preload) {
2086 free(oldenv_copy);
2087 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002088 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002089 }
Will Drewryf89aef52011-09-16 16:48:57 -05002090
Elly Jonese1749eb2011-10-07 13:54:59 -04002091 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002092 if (use_preload) {
2093 /* Restore parent's LD_PRELOAD. */
2094 if (oldenv_copy) {
2095 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2096 free(oldenv_copy);
2097 } else {
2098 unsetenv(kLdPreloadEnvVar);
2099 }
2100 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002101 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002102
Elly Jonese1749eb2011-10-07 13:54:59 -04002103 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002104
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002105 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002106 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002107
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002108 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002109 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002110
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002111 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002112 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002113
2114 if (sync_child)
2115 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002116
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002117 if (use_preload) {
2118 /* Send marshalled minijail. */
2119 close(pipe_fds[0]); /* read endpoint */
2120 ret = minijail_to_fd(j, pipe_fds[1]);
2121 close(pipe_fds[1]); /* write endpoint */
2122 if (ret) {
2123 kill(j->initpid, SIGKILL);
2124 die("failed to send marshalled minijail");
2125 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002126 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002127
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002128 if (pchild_pid)
2129 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002130
2131 /*
2132 * If we want to write to the child process' standard input,
2133 * set up the write end of the pipe.
2134 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002135 if (pstdin_fd)
2136 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002137 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002138
2139 /*
2140 * If we want to read from the child process' standard output,
2141 * set up the read end of the pipe.
2142 */
2143 if (pstdout_fd)
2144 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002145 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002146
2147 /*
2148 * If we want to read from the child process' standard error,
2149 * set up the read end of the pipe.
2150 */
2151 if (pstderr_fd)
2152 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002153 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002154
Elly Jonese1749eb2011-10-07 13:54:59 -04002155 return 0;
2156 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002157 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002158 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002159
Peter Qiu2860c462015-12-16 15:13:06 -08002160 if (j->flags.reset_signal_mask) {
2161 sigset_t signal_mask;
2162 if (sigemptyset(&signal_mask) != 0)
2163 pdie("sigemptyset failed");
2164 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2165 pdie("sigprocmask failed");
2166 }
2167
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002168 if (j->flags.close_open_fds) {
2169 const size_t kMaxInheritableFdsSize = 10;
2170 int inheritable_fds[kMaxInheritableFdsSize];
2171 size_t size = 0;
2172 if (use_preload) {
2173 inheritable_fds[size++] = pipe_fds[0];
2174 inheritable_fds[size++] = pipe_fds[1];
2175 }
2176 if (sync_child) {
2177 inheritable_fds[size++] = child_sync_pipe_fds[0];
2178 inheritable_fds[size++] = child_sync_pipe_fds[1];
2179 }
2180 if (pstdin_fd) {
2181 inheritable_fds[size++] = stdin_fds[0];
2182 inheritable_fds[size++] = stdin_fds[1];
2183 }
2184 if (pstdout_fd) {
2185 inheritable_fds[size++] = stdout_fds[0];
2186 inheritable_fds[size++] = stdout_fds[1];
2187 }
2188 if (pstderr_fd) {
2189 inheritable_fds[size++] = stderr_fds[0];
2190 inheritable_fds[size++] = stderr_fds[1];
2191 }
2192
2193 if (close_open_fds(inheritable_fds, size) < 0)
2194 die("failed to close open file descriptors");
2195 }
2196
Dylan Reidce5b55e2016-01-13 11:04:16 -08002197 if (sync_child)
2198 wait_for_parent_setup(child_sync_pipe_fds);
2199
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002200 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002201 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002202
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002203 /*
2204 * If we want to write to the jailed process' standard input,
2205 * set up the read end of the pipe.
2206 */
2207 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002208 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2209 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002210 die("failed to set up stdin pipe");
2211 }
2212
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002213 /*
2214 * If we want to read from the jailed process' standard output,
2215 * set up the write end of the pipe.
2216 */
2217 if (pstdout_fd) {
2218 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2219 STDOUT_FILENO) < 0)
2220 die("failed to set up stdout pipe");
2221 }
2222
2223 /*
2224 * If we want to read from the jailed process' standard error,
2225 * set up the write end of the pipe.
2226 */
2227 if (pstderr_fd) {
2228 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2229 STDERR_FILENO) < 0)
2230 die("failed to set up stderr pipe");
2231 }
2232
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002233 /*
2234 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2235 * This prevents the jailed process from using the TIOCSTI ioctl
2236 * to push characters into the parent process terminal's input buffer,
2237 * therefore escaping the jail.
2238 */
2239 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2240 isatty(STDERR_FILENO)) {
2241 if (setsid() < 0) {
2242 pdie("setsid() failed");
2243 }
2244 }
2245
Dylan Reid791f5772015-09-14 20:02:42 -07002246 /* If running an init program, let it decide when/how to mount /proc. */
2247 if (pid_namespace && !do_init)
2248 j->flags.remount_proc_ro = 0;
2249
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002250 if (use_preload) {
2251 /* Strip out flags that cannot be inherited across execve(2). */
2252 minijail_preexec(j);
2253 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002254 /*
2255 * If not using LD_PRELOAD, do all jailing before execve(2).
2256 * Note that PID namespaces can only be entered on fork(2),
2257 * so that flag is still cleared.
2258 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002259 j->flags.pids = 0;
2260 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002261 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002262 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002263
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002264 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002265 /*
2266 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002267 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002268 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002269 * a child to actually run the program. If |do_init == 0|, we
2270 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002271 *
2272 * If we're multithreaded, we'll probably deadlock here. See
2273 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002274 */
2275 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002276 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002277 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002278 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002279 /*
2280 * Best effort. Don't bother checking the return value.
2281 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002282 prctl(PR_SET_NAME, "minijail-init");
2283 init(child_pid); /* Never returns. */
2284 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002285 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002286
Elly Jonesdd3e8512012-01-23 15:13:38 -05002287 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002288 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002289 * calling process
2290 * -> execve()-ing process
2291 * If we are:
2292 * calling process
2293 * -> init()-ing process
2294 * -> execve()-ing process
2295 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002296 ret = execve(filename, argv, environ);
2297 if (ret == -1) {
2298 pwarn("execve(%s) failed", filename);
2299 }
2300 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002301}
2302
Will Drewry6ac91122011-10-21 16:38:58 -05002303int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002304{
2305 int st;
2306 if (kill(j->initpid, SIGTERM))
2307 return -errno;
2308 if (waitpid(j->initpid, &st, 0) < 0)
2309 return -errno;
2310 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002311}
2312
Will Drewry6ac91122011-10-21 16:38:58 -05002313int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002314{
2315 int st;
2316 if (waitpid(j->initpid, &st, 0) < 0)
2317 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002318
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002319 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002320 int error_status = st;
2321 if (WIFSIGNALED(st)) {
2322 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002323 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002324 j->initpid, signum);
2325 /*
2326 * We return MINIJAIL_ERR_JAIL if the process received
2327 * SIGSYS, which happens when a syscall is blocked by
2328 * seccomp filters.
2329 * If not, we do what bash(1) does:
2330 * $? = 128 + signum
2331 */
2332 if (signum == SIGSYS) {
2333 error_status = MINIJAIL_ERR_JAIL;
2334 } else {
2335 error_status = 128 + signum;
2336 }
2337 }
2338 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002339 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002340
2341 int exit_status = WEXITSTATUS(st);
2342 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002343 info("child process %d exited with status %d",
2344 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002345
2346 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002347}
2348
Will Drewry6ac91122011-10-21 16:38:58 -05002349void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002350{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002351 size_t i;
2352
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002353 if (j->flags.seccomp_filter && j->filter_prog) {
2354 free(j->filter_prog->filter);
2355 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002356 }
Dylan Reid648b2202015-10-23 00:50:00 -07002357 while (j->mounts_head) {
2358 struct mountpoint *m = j->mounts_head;
2359 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002360 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002361 free(m->type);
2362 free(m->dest);
2363 free(m->src);
2364 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002365 }
Dylan Reid648b2202015-10-23 00:50:00 -07002366 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002367 if (j->user)
2368 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002369 if (j->suppl_gid_list)
2370 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002371 if (j->chrootdir)
2372 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002373 if (j->pid_file_path)
2374 free(j->pid_file_path);
2375 if (j->uidmap)
2376 free(j->uidmap);
2377 if (j->gidmap)
2378 free(j->gidmap);
Andrew Brestickereac28942015-11-11 16:04:46 -08002379 if (j->alt_syscall_table)
2380 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002381 for (i = 0; i < j->cgroup_count; ++i)
2382 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002383 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002384}