blob: 4690c6bc2306aafb7735f0a2efdc8e0475231b9a [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;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100178 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800179 char *cgroups[MAX_CGROUPS];
180 size_t cgroup_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500181};
182
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700183/*
184 * Strip out flags meant for the parent.
185 * We keep things that are not inherited across execve(2) (e.g. capabilities),
186 * or are easier to set after execve(2) (e.g. seccomp filters).
187 */
188void minijail_preenter(struct minijail *j)
189{
190 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700191 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900192 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700193 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700194 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800195 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800196 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800197 j->flags.cgroups = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700198}
199
200/*
201 * Strip out flags meant for the child.
202 * We keep things that are inherited across execve(2).
203 */
204void minijail_preexec(struct minijail *j)
205{
206 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700207 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800208 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700209 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800210 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700211 if (j->user)
212 free(j->user);
213 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800214 if (j->suppl_gid_list)
215 free(j->suppl_gid_list);
216 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700217 memset(&j->flags, 0, sizeof(j->flags));
218 /* Now restore anything we meant to keep. */
219 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700220 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800221 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700222 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800223 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700224 /* Note, |pids| will already have been used before this call. */
225}
226
227/* Minijail API. */
228
Will Drewry6ac91122011-10-21 16:38:58 -0500229struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400230{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400231 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400232}
233
Will Drewry6ac91122011-10-21 16:38:58 -0500234void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400235{
236 if (uid == 0)
237 die("useless change to uid 0");
238 j->uid = uid;
239 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400240}
241
Will Drewry6ac91122011-10-21 16:38:58 -0500242void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400243{
244 if (gid == 0)
245 die("useless change to gid 0");
246 j->gid = gid;
247 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400248}
249
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800250void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
251 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800252{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800253 size_t i;
254
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500255 if (j->flags.inherit_suppl_gids)
256 die("cannot inherit *and* set supplementary groups");
257 if (j->flags.keep_suppl_gids)
258 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800259
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800260 if (size == 0) {
261 /* Clear supplementary groups. */
262 j->suppl_gid_list = NULL;
263 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100264 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800265 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800266 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800267
268 /* Copy the gid_t array. */
269 j->suppl_gid_list = calloc(size, sizeof(gid_t));
270 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800271 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800272 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800273 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800274 j->suppl_gid_list[i] = list[i];
275 }
276 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100277 j->flags.set_suppl_gids = 1;
278}
279
280void API minijail_keep_supplementary_gids(struct minijail *j) {
281 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800282}
283
Will Drewry6ac91122011-10-21 16:38:58 -0500284int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400285{
286 char *buf = NULL;
287 struct passwd pw;
288 struct passwd *ppw = NULL;
289 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
290 if (sz == -1)
291 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400292
Elly Jonesdd3e8512012-01-23 15:13:38 -0500293 /*
294 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400295 * the maximum needed size of the buffer, so we don't have to search.
296 */
297 buf = malloc(sz);
298 if (!buf)
299 return -ENOMEM;
300 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500301 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800302 * We're safe to free the buffer here. The strings inside |pw| point
303 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800304 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
305 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500306 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400307 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700308 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400309 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700310 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400311 minijail_change_uid(j, ppw->pw_uid);
312 j->user = strdup(user);
313 if (!j->user)
314 return -ENOMEM;
315 j->usergid = ppw->pw_gid;
316 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400317}
318
Will Drewry6ac91122011-10-21 16:38:58 -0500319int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400320{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700321 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700322 struct group gr;
323 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400324 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
325 if (sz == -1)
326 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400327
Elly Jonesdd3e8512012-01-23 15:13:38 -0500328 /*
329 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400330 * the maximum needed size of the buffer, so we don't have to search.
331 */
332 buf = malloc(sz);
333 if (!buf)
334 return -ENOMEM;
335 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500336 /*
337 * We're safe to free the buffer here. The strings inside gr point
338 * inside buf, but we don't use any of them; this leaves the pointers
339 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
340 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400341 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700342 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400343 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700344 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400345 minijail_change_gid(j, pgr->gr_gid);
346 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400347}
348
Will Drewry6ac91122011-10-21 16:38:58 -0500349void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400350{
351 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400352}
353
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700354void API minijail_no_new_privs(struct minijail *j)
355{
356 j->flags.no_new_privs = 1;
357}
358
Will Drewry6ac91122011-10-21 16:38:58 -0500359void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400360{
361 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500362}
363
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400364void API minijail_set_seccomp_filter_tsync(struct minijail *j)
365{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400366 if (j->filter_len > 0 && j->filter_prog != NULL) {
367 die("minijail_set_seccomp_filter_tsync() must be called "
368 "before minijail_parse_seccomp_filters()");
369 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400370 j->flags.seccomp_filter_tsync = 1;
371}
372
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700373void API minijail_log_seccomp_filter_failures(struct minijail *j)
374{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400375 if (j->filter_len > 0 && j->filter_prog != NULL) {
376 die("minijail_log_seccomp_filter_failures() must be called "
377 "before minijail_parse_seccomp_filters()");
378 }
379 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700380}
381
Will Drewry6ac91122011-10-21 16:38:58 -0500382void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400383{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800384 /*
385 * 'minijail_use_caps' configures a runtime-capabilities-only
386 * environment, including a bounding set matching the thread's runtime
387 * (permitted|inheritable|effective) sets.
388 * Therefore, it will override any existing bounding set configurations
389 * since the latter would allow gaining extra runtime capabilities from
390 * file capabilities.
391 */
392 if (j->flags.capbset_drop) {
393 warn("overriding bounding set configuration");
394 j->cap_bset = 0;
395 j->flags.capbset_drop = 0;
396 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400397 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800398 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400399}
400
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800401void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
402{
403 if (j->flags.use_caps) {
404 /*
405 * 'minijail_use_caps' will have already configured a capability
406 * bounding set matching the (permitted|inheritable|effective)
407 * sets. Abort if the user tries to configure a separate
408 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
409 * are mutually exclusive.
410 */
411 die("runtime capabilities already configured, can't drop "
412 "bounding set separately");
413 }
414 j->cap_bset = capmask;
415 j->flags.capbset_drop = 1;
416}
417
418void API minijail_reset_signal_mask(struct minijail *j)
419{
Peter Qiu2860c462015-12-16 15:13:06 -0800420 j->flags.reset_signal_mask = 1;
421}
422
Will Drewry6ac91122011-10-21 16:38:58 -0500423void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400424{
425 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400426}
427
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700428void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
429{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800430 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700431 if (ns_fd < 0) {
432 pdie("failed to open namespace '%s'", ns_path);
433 }
434 j->mountns_fd = ns_fd;
435 j->flags.enter_vfs = 1;
436}
437
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800438void API minijail_skip_remount_private(struct minijail *j)
439{
440 j->flags.skip_remount_private = 1;
441}
442
Will Drewry6ac91122011-10-21 16:38:58 -0500443void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400444{
Elly Jonese58176c2012-01-23 11:46:17 -0500445 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700446 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400447 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800448 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400449}
450
Dylan Reidf7942472015-11-18 17:55:26 -0800451void API minijail_namespace_ipc(struct minijail *j)
452{
453 j->flags.ipc = 1;
454}
455
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400456void API minijail_namespace_net(struct minijail *j)
457{
458 j->flags.net = 1;
459}
460
Dylan Reid1102f5a2015-09-15 11:52:20 -0700461void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
462{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800463 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700464 if (ns_fd < 0) {
465 pdie("failed to open namespace '%s'", ns_path);
466 }
467 j->netns_fd = ns_fd;
468 j->flags.enter_net = 1;
469}
470
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700471void API minijail_namespace_cgroups(struct minijail *j)
472{
473 j->flags.ns_cgroups = 1;
474}
475
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700476void API minijail_close_open_fds(struct minijail *j)
477{
478 j->flags.close_open_fds = 1;
479}
480
Dylan Reid791f5772015-09-14 20:02:42 -0700481void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400482{
483 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700484 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400485}
486
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800487void API minijail_namespace_user(struct minijail *j)
488{
489 j->flags.userns = 1;
490}
491
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400492void API minijail_namespace_user_disable_setgroups(struct minijail *j)
493{
494 j->flags.disable_setgroups = 1;
495}
496
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800497int API minijail_uidmap(struct minijail *j, const char *uidmap)
498{
499 j->uidmap = strdup(uidmap);
500 if (!j->uidmap)
501 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800502 char *ch;
503 for (ch = j->uidmap; *ch; ch++) {
504 if (*ch == ',')
505 *ch = '\n';
506 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800507 return 0;
508}
509
510int API minijail_gidmap(struct minijail *j, const char *gidmap)
511{
512 j->gidmap = strdup(gidmap);
513 if (!j->gidmap)
514 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800515 char *ch;
516 for (ch = j->gidmap; *ch; ch++) {
517 if (*ch == ',')
518 *ch = '\n';
519 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800520 return 0;
521}
522
Will Drewry6ac91122011-10-21 16:38:58 -0500523void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400524{
Lutz Justen13807cb2017-01-03 17:11:55 +0100525 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400526}
527
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800528void API minijail_run_as_init(struct minijail *j)
529{
530 /*
531 * Since the jailed program will become 'init' in the new PID namespace,
532 * Minijail does not need to fork an 'init' process.
533 */
534 j->flags.do_init = 0;
535}
536
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700537int API minijail_enter_chroot(struct minijail *j, const char *dir)
538{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400539 if (j->chrootdir)
540 return -EINVAL;
541 j->chrootdir = strdup(dir);
542 if (!j->chrootdir)
543 return -ENOMEM;
544 j->flags.chroot = 1;
545 return 0;
546}
547
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800548int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
549{
550 if (j->chrootdir)
551 return -EINVAL;
552 j->chrootdir = strdup(dir);
553 if (!j->chrootdir)
554 return -ENOMEM;
555 j->flags.pivot_root = 1;
556 return 0;
557}
558
Dylan Reida14e08d2015-10-22 21:05:29 -0700559char API *minijail_get_original_path(struct minijail *j,
560 const char *path_inside_chroot)
561{
Dylan Reid648b2202015-10-23 00:50:00 -0700562 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700563
Dylan Reid648b2202015-10-23 00:50:00 -0700564 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700565 while (b) {
566 /*
567 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700568 * mount, then the original path is exactly the source of
569 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700570 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700571 * mount source = /some/path/exe, mount dest =
572 * /chroot/path/exe Then when getting the original path of
573 * "/chroot/path/exe", the source of that mount,
574 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700575 */
576 if (!strcmp(b->dest, path_inside_chroot))
577 return strdup(b->src);
578
579 /*
580 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700581 * mount, take the suffix of the chroot path relative to the
582 * mount destination path, and append it to the mount source
583 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700584 */
585 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
586 const char *relative_path =
587 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400588 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700589 }
590 b = b->next;
591 }
592
593 /* If there is a chroot path, append |path_inside_chroot| to that. */
594 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400595 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700596
597 /* No chroot, so the path outside is the same as it is inside. */
598 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700599}
600
Martin Pelikánab9eb442017-01-25 11:53:58 +1100601size_t minijail_get_tmpfs_size(const struct minijail *j)
602{
603 return j->tmpfs_size;
604}
605
Lee Campbell11af0622014-05-22 12:36:04 -0700606void API minijail_mount_tmp(struct minijail *j)
607{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100608 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
609}
610
611void API minijail_mount_tmp_size(struct minijail *j, size_t size)
612{
613 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700614 j->flags.mount_tmp = 1;
615}
616
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800617int API minijail_write_pid_file(struct minijail *j, const char *path)
618{
619 j->pid_file_path = strdup(path);
620 if (!j->pid_file_path)
621 return -ENOMEM;
622 j->flags.pid_file = 1;
623 return 0;
624}
625
Dylan Reid605ce7f2016-01-19 19:21:00 -0800626int API minijail_add_to_cgroup(struct minijail *j, const char *path)
627{
628 if (j->cgroup_count >= MAX_CGROUPS)
629 return -ENOMEM;
630 j->cgroups[j->cgroup_count] = strdup(path);
631 if (!j->cgroups[j->cgroup_count])
632 return -ENOMEM;
633 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800634 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800635 return 0;
636}
637
Dylan Reid81e23972016-05-18 14:06:35 -0700638int API minijail_mount_with_data(struct minijail *j, const char *src,
639 const char *dest, const char *type,
640 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700641{
Dylan Reid648b2202015-10-23 00:50:00 -0700642 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400643
644 if (*dest != '/')
645 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700646 m = calloc(1, sizeof(*m));
647 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400648 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700649 m->dest = strdup(dest);
650 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400651 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700652 m->src = strdup(src);
653 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400654 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700655 m->type = strdup(type);
656 if (!m->type)
657 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700658 if (data) {
659 m->data = strdup(data);
660 if (!m->data)
661 goto error;
662 m->has_data = 1;
663 }
Dylan Reid648b2202015-10-23 00:50:00 -0700664 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400665
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800666 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400667
Elly Jonesdd3e8512012-01-23 15:13:38 -0500668 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700669 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400670 * containing vfs namespace.
671 */
672 minijail_namespace_vfs(j);
673
Dylan Reid648b2202015-10-23 00:50:00 -0700674 if (j->mounts_tail)
675 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400676 else
Dylan Reid648b2202015-10-23 00:50:00 -0700677 j->mounts_head = m;
678 j->mounts_tail = m;
679 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400680
681 return 0;
682
683error:
Dylan Reid81e23972016-05-18 14:06:35 -0700684 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700685 free(m->src);
686 free(m->dest);
687 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400688 return -ENOMEM;
689}
690
Dylan Reid81e23972016-05-18 14:06:35 -0700691int API minijail_mount(struct minijail *j, const char *src, const char *dest,
692 const char *type, unsigned long flags)
693{
694 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
695}
696
Dylan Reid648b2202015-10-23 00:50:00 -0700697int API minijail_bind(struct minijail *j, const char *src, const char *dest,
698 int writeable)
699{
700 unsigned long flags = MS_BIND;
701
702 if (!writeable)
703 flags |= MS_RDONLY;
704
705 return minijail_mount(j, src, dest, "", flags);
706}
707
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400708static void clear_seccomp_options(struct minijail *j)
709{
710 j->flags.seccomp_filter = 0;
711 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400712 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400713 j->filter_len = 0;
714 j->filter_prog = NULL;
715 j->flags.no_new_privs = 0;
716}
717
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400718static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400719{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400720 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400721 /*
722 * |errno| will be set to EINVAL when seccomp has not been
723 * compiled into the kernel. On certain platforms and kernel
724 * versions this is not a fatal failure. In that case, and only
725 * in that case, disable seccomp and skip loading the filters.
726 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400727 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400728 warn("not loading seccomp filters, seccomp filter not "
729 "supported");
730 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400731 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700732 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400733 /*
734 * If |errno| != EINVAL or seccomp_can_softfail() is false,
735 * we can proceed. Worst case scenario minijail_enter() will
736 * abort() if seccomp fails.
737 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700738 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400739 if (j->flags.seccomp_filter_tsync) {
740 /* Are the seccomp(2) syscall and the TSYNC option supported? */
741 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
742 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
743 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400744 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
745 warn("seccomp(2) syscall not supported");
746 clear_seccomp_options(j);
747 return 0;
748 } else if (saved_errno == EINVAL &&
749 seccomp_can_softfail()) {
750 warn(
751 "seccomp filter thread sync not supported");
752 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400753 return 0;
754 }
755 /*
756 * Similar logic here. If seccomp_can_softfail() is
757 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
758 * we can proceed. Worst case scenario minijail_enter()
759 * will abort() if seccomp or TSYNC fail.
760 */
761 }
762 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400763 return 1;
764}
765
766static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
767{
768 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400769 int use_ret_trap =
770 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
771 int allow_logging = j->flags.seccomp_filter_logging;
772
773 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400774 free(fprog);
775 return -1;
776 }
777
778 j->filter_len = fprog->len;
779 j->filter_prog = fprog;
780 return 0;
781}
782
783void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
784{
785 if (!seccomp_should_parse_filters(j))
786 return;
787
Elly Jonese1749eb2011-10-07 13:54:59 -0400788 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800789 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700790 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400791 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800792
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400793 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700794 die("failed to compile seccomp filter BPF program in '%s'",
795 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800796 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400797 fclose(file);
798}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800799
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400800void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
801{
802 if (!seccomp_should_parse_filters(j))
803 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800804
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400805 FILE *file = fdopen(fd, "r");
806 if (!file) {
807 pdie("failed to associate stream with fd %d", fd);
808 }
809
810 if (parse_seccomp_filters(j, file) != 0) {
811 die("failed to compile seccomp filter BPF program from fd %d",
812 fd);
813 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400814 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500815}
816
Andrew Brestickereac28942015-11-11 16:04:46 -0800817int API minijail_use_alt_syscall(struct minijail *j, const char *table)
818{
819 j->alt_syscall_table = strdup(table);
820 if (!j->alt_syscall_table)
821 return -ENOMEM;
822 j->flags.alt_syscall = 1;
823 return 0;
824}
825
Will Drewryf89aef52011-09-16 16:48:57 -0500826struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400827 size_t available;
828 size_t total;
829 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500830};
831
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800832void marshal_state_init(struct marshal_state *state, char *buf,
833 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400834{
835 state->available = available;
836 state->buf = buf;
837 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500838}
839
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800840void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400841{
842 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500843
Elly Jonese1749eb2011-10-07 13:54:59 -0400844 /* Up to |available| will be written. */
845 if (copy_len) {
846 memcpy(state->buf, src, copy_len);
847 state->buf += copy_len;
848 state->available -= copy_len;
849 }
850 /* |total| will contain the expected length. */
851 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500852}
853
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400854void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700855{
856 marshal_append(state, m->src, strlen(m->src) + 1);
857 marshal_append(state, m->dest, strlen(m->dest) + 1);
858 marshal_append(state, m->type, strlen(m->type) + 1);
859 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
860 if (m->has_data)
861 marshal_append(state, m->data, strlen(m->data) + 1);
862 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
863}
864
Will Drewry6ac91122011-10-21 16:38:58 -0500865void minijail_marshal_helper(struct marshal_state *state,
866 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400867{
Dylan Reid648b2202015-10-23 00:50:00 -0700868 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800869 size_t i;
870
Elly Jonese1749eb2011-10-07 13:54:59 -0400871 marshal_append(state, (char *)j, sizeof(*j));
872 if (j->user)
873 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800874 if (j->suppl_gid_list) {
875 marshal_append(state, j->suppl_gid_list,
876 j->suppl_gid_count * sizeof(gid_t));
877 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400878 if (j->chrootdir)
879 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800880 if (j->alt_syscall_table) {
881 marshal_append(state, j->alt_syscall_table,
882 strlen(j->alt_syscall_table) + 1);
883 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800884 if (j->flags.seccomp_filter && j->filter_prog) {
885 struct sock_fprog *fp = j->filter_prog;
886 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800887 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400888 }
Dylan Reid648b2202015-10-23 00:50:00 -0700889 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400890 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400891 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800892 for (i = 0; i < j->cgroup_count; ++i)
893 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500894}
895
Will Drewry6ac91122011-10-21 16:38:58 -0500896size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400897{
898 struct marshal_state state;
899 marshal_state_init(&state, NULL, 0);
900 minijail_marshal_helper(&state, j);
901 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500902}
903
Elly Jonese1749eb2011-10-07 13:54:59 -0400904int minijail_marshal(const struct minijail *j, char *buf, size_t available)
905{
906 struct marshal_state state;
907 marshal_state_init(&state, buf, available);
908 minijail_marshal_helper(&state, j);
909 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500910}
911
Elly Jonese1749eb2011-10-07 13:54:59 -0400912int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
913{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800914 size_t i;
915 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500916 int ret = -EINVAL;
917
Elly Jonese1749eb2011-10-07 13:54:59 -0400918 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500919 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400920 memcpy((void *)j, serialized, sizeof(*j));
921 serialized += sizeof(*j);
922 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500923
Will Drewrybee7ba72011-10-21 20:47:01 -0500924 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400925 j->pid_file_path = NULL;
926 j->uidmap = NULL;
927 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700928 j->mounts_head = NULL;
929 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800930 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500931
Elly Jonese1749eb2011-10-07 13:54:59 -0400932 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400933 char *user = consumestr(&serialized, &length);
934 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500935 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400936 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500937 if (!j->user)
938 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400939 }
Will Drewryf89aef52011-09-16 16:48:57 -0500940
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800941 if (j->suppl_gid_list) { /* stale pointer */
942 if (j->suppl_gid_count > NGROUPS_MAX) {
943 goto bad_gid_list;
944 }
945 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
946 void *gid_list_bytes =
947 consumebytes(gid_list_size, &serialized, &length);
948 if (!gid_list_bytes)
949 goto bad_gid_list;
950
951 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
952 if (!j->suppl_gid_list)
953 goto bad_gid_list;
954
955 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
956 }
957
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400958 if (j->chrootdir) { /* stale pointer */
959 char *chrootdir = consumestr(&serialized, &length);
960 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500961 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400962 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500963 if (!j->chrootdir)
964 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400965 }
966
Andrew Brestickereac28942015-11-11 16:04:46 -0800967 if (j->alt_syscall_table) { /* stale pointer */
968 char *alt_syscall_table = consumestr(&serialized, &length);
969 if (!alt_syscall_table)
970 goto bad_syscall_table;
971 j->alt_syscall_table = strdup(alt_syscall_table);
972 if (!j->alt_syscall_table)
973 goto bad_syscall_table;
974 }
975
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800976 if (j->flags.seccomp_filter && j->filter_len > 0) {
977 size_t ninstrs = j->filter_len;
978 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
979 ninstrs > USHRT_MAX)
980 goto bad_filters;
981
982 size_t program_len = ninstrs * sizeof(struct sock_filter);
983 void *program = consumebytes(program_len, &serialized, &length);
984 if (!program)
985 goto bad_filters;
986
987 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800988 if (!j->filter_prog)
989 goto bad_filters;
990
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800991 j->filter_prog->len = ninstrs;
992 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800993 if (!j->filter_prog->filter)
994 goto bad_filter_prog_instrs;
995
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800996 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400997 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400998
Dylan Reid648b2202015-10-23 00:50:00 -0700999 count = j->mounts_count;
1000 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001001 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001002 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001003 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001004 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001005 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001006 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001007 const char *src = consumestr(&serialized, &length);
1008 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001009 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001010 dest = consumestr(&serialized, &length);
1011 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001012 goto bad_mounts;
1013 type = consumestr(&serialized, &length);
1014 if (!type)
1015 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001016 has_data = consumebytes(sizeof(*has_data), &serialized,
1017 &length);
1018 if (!has_data)
1019 goto bad_mounts;
1020 if (*has_data) {
1021 data = consumestr(&serialized, &length);
1022 if (!data)
1023 goto bad_mounts;
1024 }
Dylan Reid648b2202015-10-23 00:50:00 -07001025 flags = consumebytes(sizeof(*flags), &serialized, &length);
1026 if (!flags)
1027 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001028 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001029 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001030 }
1031
Dylan Reid605ce7f2016-01-19 19:21:00 -08001032 count = j->cgroup_count;
1033 j->cgroup_count = 0;
1034 for (i = 0; i < count; ++i) {
1035 char *cgroup = consumestr(&serialized, &length);
1036 if (!cgroup)
1037 goto bad_cgroups;
1038 j->cgroups[i] = strdup(cgroup);
1039 if (!j->cgroups[i])
1040 goto bad_cgroups;
1041 ++j->cgroup_count;
1042 }
1043
Elly Jonese1749eb2011-10-07 13:54:59 -04001044 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001045
Dylan Reid605ce7f2016-01-19 19:21:00 -08001046bad_cgroups:
1047 while (j->mounts_head) {
1048 struct mountpoint *m = j->mounts_head;
1049 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001050 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001051 free(m->type);
1052 free(m->dest);
1053 free(m->src);
1054 free(m);
1055 }
1056 for (i = 0; i < j->cgroup_count; ++i)
1057 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001058bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001059 if (j->flags.seccomp_filter && j->filter_len > 0) {
1060 free(j->filter_prog->filter);
1061 free(j->filter_prog);
1062 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001063bad_filter_prog_instrs:
1064 if (j->filter_prog)
1065 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001066bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001067 if (j->alt_syscall_table)
1068 free(j->alt_syscall_table);
1069bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001070 if (j->chrootdir)
1071 free(j->chrootdir);
1072bad_chrootdir:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001073 if (j->suppl_gid_list)
1074 free(j->suppl_gid_list);
1075bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001076 if (j->user)
1077 free(j->user);
1078clear_pointers:
1079 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001080 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001081 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001082 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001083 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001084out:
1085 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001086}
1087
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001088/*
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001089 * setup_mount_destination: Ensures the mount target exists.
1090 * Creates it if needed and possible.
Dylan Reideec77962016-06-30 19:35:10 -07001091 */
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001092static int setup_mount_destination(const char *source, const char *dest,
1093 uid_t uid, uid_t gid)
Dylan Reideec77962016-06-30 19:35:10 -07001094{
1095 int rc;
1096 struct stat st_buf;
1097
1098 rc = stat(dest, &st_buf);
1099 if (rc == 0) /* destination exists */
1100 return 0;
1101
1102 /*
1103 * Try to create the destination.
1104 * Either make a directory or touch a file depending on the source type.
1105 * If the source doesn't exist, assume it is a filesystem type such as
1106 * "tmpfs" and create a directory to mount it on.
1107 */
1108 rc = stat(source, &st_buf);
1109 if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) {
1110 if (mkdir(dest, 0700))
1111 return -errno;
1112 } else {
1113 int fd = open(dest, O_RDWR | O_CREAT, 0700);
1114 if (fd < 0)
1115 return -errno;
1116 close(fd);
1117 }
1118 return chown(dest, uid, gid);
1119}
1120
1121/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001122 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001123 * @j Minijail these mounts are for
1124 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001125 *
1126 * Returns 0 for success.
1127 */
Dylan Reid648b2202015-10-23 00:50:00 -07001128static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001129{
Dylan Reid648b2202015-10-23 00:50:00 -07001130 int ret;
1131 char *dest;
1132 int remount_ro = 0;
1133
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001134 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001135 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001136 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001137
Dylan Reideec77962016-06-30 19:35:10 -07001138 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1139 pdie("creating mount target '%s' failed", dest);
1140
Dylan Reid648b2202015-10-23 00:50:00 -07001141 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001142 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1143 * can't both be specified in the original bind mount.
1144 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001145 */
1146 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1147 remount_ro = 1;
1148 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001149 }
Dylan Reid648b2202015-10-23 00:50:00 -07001150
Dylan Reid81e23972016-05-18 14:06:35 -07001151 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001152 if (ret)
1153 pdie("mount: %s -> %s", m->src, dest);
1154
1155 if (remount_ro) {
1156 m->flags |= MS_RDONLY;
1157 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001158 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001159 if (ret)
1160 pdie("bind ro: %s -> %s", m->src, dest);
1161 }
1162
Elly Jones51a5b6c2011-10-12 19:09:26 -04001163 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001164 if (m->next)
1165 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001166 return ret;
1167}
1168
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001169static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001170{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001171 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001172
1173 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001174 return ret;
1175
1176 if (chroot(j->chrootdir))
1177 return -errno;
1178
1179 if (chdir("/"))
1180 return -errno;
1181
1182 return 0;
1183}
1184
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001185static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001186{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001187 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001188
1189 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001190 return ret;
1191
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001192 /*
1193 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001194 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001195 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001196 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001197 if (oldroot < 0)
1198 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001199 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001200 if (newroot < 0)
1201 pdie("failed to open %s for fchdir", j->chrootdir);
1202
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001203 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001204 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001205 * do a self bind mount.
1206 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001207 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1208 pdie("failed to bind mount '%s'", j->chrootdir);
1209 if (chdir(j->chrootdir))
1210 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001211 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001212 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001213
1214 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001215 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001216 * change to the old root and unmount it.
1217 */
1218 if (fchdir(oldroot))
1219 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001220
1221 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001222 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1223 * there could be a shared mount point under |oldroot|. In that case,
1224 * mounts under this shared mount point will be unmounted below, and
1225 * this unmounting will propagate to the original mount namespace
1226 * (because the mount point is shared). To prevent this unexpected
1227 * unmounting, remove these mounts from their peer groups by recursively
1228 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001229 */
1230 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001231 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001232 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001233 if (umount2(".", MNT_DETACH))
1234 pdie("umount(/)");
1235 /* Change back to the new root. */
1236 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001237 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001238 if (close(oldroot))
1239 return -errno;
1240 if (close(newroot))
1241 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001242 if (chroot("/"))
1243 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001244 /* Set correct CWD for getcwd(3). */
1245 if (chdir("/"))
1246 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001247
1248 return 0;
1249}
1250
Martin Pelikánab9eb442017-01-25 11:53:58 +11001251static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001252{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001253 const char fmt[] = "size=%zu,mode=1777";
1254 /* Count for the user storing ULLONG_MAX literally + extra space. */
1255 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1256 int ret;
1257
1258 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1259
1260 if (ret <= 0)
1261 pdie("tmpfs size spec error");
1262 else if ((size_t)ret >= sizeof(data))
1263 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001264 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001265 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001266}
1267
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001268static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001269{
1270 const char *kProcPath = "/proc";
1271 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001272 /*
1273 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001274 * /proc in our namespace, which means using MS_REMOUNT here would
1275 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001276 * namespace (!). Instead, remove their mount from our namespace lazily
1277 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001278 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001279 if (umount2(kProcPath, MNT_DETACH)) {
1280 /*
1281 * If we are in a new user namespace, umount(2) will fail.
1282 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1283 */
1284 if (j->flags.userns) {
1285 info("umount(/proc, MNT_DETACH) failed, "
1286 "this is expected when using user namespaces");
1287 } else {
1288 return -errno;
1289 }
1290 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001291 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001292 return -errno;
1293 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001294}
1295
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001296static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001297{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001298 kill(j->initpid, SIGKILL);
1299 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001300}
1301
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001302static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001303{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001304 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001305 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001306}
1307
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001308static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001309{
1310 size_t i;
1311
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001312 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001313 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001314 kill_child_and_die(j, "failed to add to cgroups");
1315 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001316}
1317
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001318static void write_ugid_maps_or_die(const struct minijail *j)
1319{
1320 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1321 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001322 if (j->gidmap && j->flags.disable_setgroups) {
1323 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1324 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001325 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001326 if (ret == -ENOENT) {
1327 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1328 warn("could not disable setgroups(2)");
1329 } else
1330 kill_child_and_die(j, "failed to disable setgroups(2)");
1331 }
1332 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001333 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1334 kill_child_and_die(j, "failed to write gid_map");
1335}
1336
1337static void enter_user_namespace(const struct minijail *j)
1338{
1339 if (j->uidmap && setresuid(0, 0, 0))
1340 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1341 if (j->gidmap && setresgid(0, 0, 0))
1342 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1343}
1344
1345static void parent_setup_complete(int *pipe_fds)
1346{
1347 close(pipe_fds[0]);
1348 close(pipe_fds[1]);
1349}
1350
1351/*
1352 * wait_for_parent_setup: Called by the child process to wait for any
1353 * further parent-side setup to complete before continuing.
1354 */
1355static void wait_for_parent_setup(int *pipe_fds)
1356{
1357 char buf;
1358
1359 close(pipe_fds[1]);
1360
1361 /* Wait for parent to complete setup and close the pipe. */
1362 if (read(pipe_fds[0], &buf, 1) != 0)
1363 die("failed to sync with parent");
1364 close(pipe_fds[0]);
1365}
1366
1367static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001368{
Lutz Justen13807cb2017-01-03 17:11:55 +01001369 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1370 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001371 die("can only do one of inherit, keep, or set supplementary "
1372 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001373 }
1374
Lutz Justen13807cb2017-01-03 17:11:55 +01001375 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001376 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001377 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001378 } else if (j->flags.set_suppl_gids) {
1379 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001380 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001381 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001382 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001383 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001384 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001385 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001386 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001387 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001388 }
1389
1390 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001391 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001392
1393 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001394 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001395}
1396
Mike Frysinger3adfef72013-05-09 17:19:08 -04001397/*
1398 * We specifically do not use cap_valid() as that only tells us the last
1399 * valid cap we were *compiled* against (i.e. what the version of kernel
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001400 * headers says). If we run on a different kernel version, then it's not
Mike Frysinger3adfef72013-05-09 17:19:08 -04001401 * uncommon for that to be less (if an older kernel) or more (if a newer
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001402 * kernel).
1403 * Normally, we suck up the answer via /proc. On Android, not all processes are
1404 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
1405 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
Mike Frysinger3adfef72013-05-09 17:19:08 -04001406 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001407static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001408{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001409 unsigned int last_valid_cap = 0;
1410 if (is_android()) {
1411 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
1412 ++last_valid_cap);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001413
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -08001414 /* |last_valid_cap| will be the first failing value. */
1415 if (last_valid_cap > 0) {
1416 last_valid_cap--;
1417 }
1418 } else {
1419 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1420 FILE *fp = fopen(cap_file, "re");
1421 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1422 pdie("fscanf(%s)", cap_file);
1423 fclose(fp);
1424 }
Dylan Reidf682d472015-09-17 21:39:07 -07001425 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001426}
1427
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001428static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1429{
1430 const uint64_t one = 1;
1431 unsigned int i;
1432 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1433 if (keep_mask & (one << i))
1434 continue;
1435 if (prctl(PR_CAPBSET_DROP, i))
1436 pdie("could not drop capability from bounding set");
1437 }
1438}
1439
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001440static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001441{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001442 if (!j->flags.use_caps)
1443 return;
1444
Elly Jonese1749eb2011-10-07 13:54:59 -04001445 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001446 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001447 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001448 unsigned int i;
1449 if (!caps)
1450 die("can't get process caps");
1451 if (cap_clear_flag(caps, CAP_INHERITABLE))
1452 die("can't clear inheritable caps");
1453 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1454 die("can't clear effective caps");
1455 if (cap_clear_flag(caps, CAP_PERMITTED))
1456 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001457 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001458 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001459 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001460 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001461 flag[0] = i;
1462 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001463 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001464 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001465 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001466 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001467 die("can't add inheritable cap");
1468 }
1469 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001470 die("can't apply initial cleaned capset");
1471
1472 /*
1473 * Instead of dropping bounding set first, do it here in case
1474 * the caller had a more permissive bounding set which could
1475 * have been used above to raise a capability that wasn't already
1476 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1477 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001478 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001479
1480 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001481 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001482 flag[0] = CAP_SETPCAP;
1483 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1484 die("can't clear effective cap");
1485 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1486 die("can't clear permitted cap");
1487 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1488 die("can't clear inheritable cap");
1489 }
1490
1491 if (cap_set_proc(caps))
1492 die("can't apply final cleaned capset");
1493
1494 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001495}
1496
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001497static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001498{
1499 /*
1500 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1501 * in the kernel source tree for an explanation of the parameters.
1502 */
1503 if (j->flags.no_new_privs) {
1504 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1505 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1506 }
1507
1508 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001509 * Code running with ASan
1510 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1511 * will make system calls not included in the syscall filter policy,
1512 * which will likely crash the program. Skip setting seccomp filter in
1513 * that case.
1514 * 'running_with_asan()' has no inputs and is completely defined at
1515 * build time, so this cannot be used by an attacker to skip setting
1516 * seccomp filter.
1517 */
1518 if (j->flags.seccomp_filter && running_with_asan()) {
1519 warn("running with ASan, not setting seccomp filter");
1520 return;
1521 }
1522
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001523 if (j->flags.seccomp_filter) {
1524 if (j->flags.seccomp_filter_logging) {
1525 /*
1526 * If logging seccomp filter failures,
1527 * install the SIGSYS handler first.
1528 */
1529 if (install_sigsys_handler())
1530 pdie("failed to install SIGSYS handler");
1531 warn("logging seccomp filter failures");
1532 } else if (j->flags.seccomp_filter_tsync) {
1533 /*
1534 * If setting thread sync,
1535 * reset the SIGSYS signal handler so that
1536 * the entire thread group is killed.
1537 */
1538 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1539 pdie("failed to reset SIGSYS disposition");
1540 info("reset SIGSYS disposition");
1541 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001542 }
1543
1544 /*
1545 * Install the syscall filter.
1546 */
1547 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001548 if (j->flags.seccomp_filter_tsync) {
1549 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1550 SECCOMP_FILTER_FLAG_TSYNC,
1551 j->filter_prog)) {
1552 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001553 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001554 } else {
1555 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1556 j->filter_prog)) {
1557 pdie("prctl(seccomp_filter) failed");
1558 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001559 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001560 }
1561}
1562
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001563static void config_net_loopback(void)
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001564{
1565 static const char ifname[] = "lo";
1566 int sock;
1567 struct ifreq ifr;
1568
1569 /* Make sure people don't try to add really long names. */
1570 _Static_assert(sizeof(ifname) <= IFNAMSIZ, "interface name too long");
1571
1572 sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0);
1573 if (sock < 0)
1574 pdie("socket(AF_LOCAL) failed");
1575
1576 /*
1577 * Do the equiv of `ip link set up lo`. The kernel will assign
1578 * IPv4 (127.0.0.1) & IPv6 (::1) addresses automatically!
1579 */
1580 strcpy(ifr.ifr_name, ifname);
1581 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0)
1582 pdie("ioctl(SIOCGIFFLAGS) failed");
1583
1584 /* The kernel preserves ifr.ifr_name for use. */
1585 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
1586 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0)
1587 pdie("ioctl(SIOCSIFFLAGS) failed");
1588
1589 close(sock);
1590}
1591
Will Drewry6ac91122011-10-21 16:38:58 -05001592void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001593{
Dylan Reidf682d472015-09-17 21:39:07 -07001594 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001595 * If we're dropping caps, get the last valid cap from /proc now,
1596 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001597 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001598 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001599 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001600 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001601
Elly Jonese1749eb2011-10-07 13:54:59 -04001602 if (j->flags.pids)
1603 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001604 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001605
Lutz Justen13807cb2017-01-03 17:11:55 +01001606 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001607 die("cannot inherit supplementary groups without setting a "
1608 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001609
Elly Jonesdd3e8512012-01-23 15:13:38 -05001610 /*
1611 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001612 * so we don't even try. If any of our operations fail, we abort() the
1613 * entire process.
1614 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001615 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001616 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001617
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001618 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001619 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001620 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001621 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001622 * Unless asked not to, remount all filesystems as private.
1623 * If they are shared, new bind mounts will creep out of our
1624 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001625 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1626 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001627 if (!j->flags.skip_remount_private) {
1628 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001629 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1630 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001631 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001632 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001633
Dylan Reidf7942472015-11-18 17:55:26 -08001634 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001635 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001636 }
1637
Dylan Reid1102f5a2015-09-15 11:52:20 -07001638 if (j->flags.enter_net) {
1639 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001640 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001641 } else if (j->flags.net) {
1642 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001643 pdie("unshare(CLONE_NEWNET) failed");
1644 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001645 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001646
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001647 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001648 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001649
Elly Jones51a5b6c2011-10-12 19:09:26 -04001650 if (j->flags.chroot && enter_chroot(j))
1651 pdie("chroot");
1652
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001653 if (j->flags.pivot_root && enter_pivot_root(j))
1654 pdie("pivot_root");
1655
Martin Pelikánab9eb442017-01-25 11:53:58 +11001656 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001657 pdie("mount_tmp");
1658
Dylan Reid791f5772015-09-14 20:02:42 -07001659 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001660 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001661
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001662 /*
1663 * If we're only dropping capabilities from the bounding set, but not
1664 * from the thread's (permitted|inheritable|effective) sets, do it now.
1665 */
1666 if (j->flags.capbset_drop) {
1667 drop_capbset(j->cap_bset, last_valid_cap);
1668 }
1669
1670 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001671 /*
1672 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001673 * capability to change uids, our attempt to use setuid()
1674 * below will fail. Hang on to root caps across setuid(), then
1675 * lock securebits.
1676 */
1677 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001678 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001679
1680 /*
1681 * Kernels 4.3+ define a new securebit
1682 * (SECURE_NO_CAP_AMBIENT_RAISE), so using the SECURE_ALL_BITS
1683 * and SECURE_ALL_LOCKS masks from newer kernel headers will
1684 * return EPERM on older kernels. Detect this, and retry with
1685 * the right mask for older (2.6.26-4.2) kernels.
1686 */
1687 int securebits_ret = prctl(PR_SET_SECUREBITS,
1688 SECURE_ALL_BITS | SECURE_ALL_LOCKS);
1689 if (securebits_ret < 0) {
1690 if (errno == EPERM) {
1691 /* Possibly running on kernel < 4.3. */
1692 securebits_ret = prctl(
1693 PR_SET_SECUREBITS,
1694 OLD_SECURE_ALL_BITS | OLD_SECURE_ALL_LOCKS);
1695 }
1696 }
1697 if (securebits_ret < 0)
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001698 pdie("prctl(PR_SET_SECUREBITS) failed");
Elly Jonese1749eb2011-10-07 13:54:59 -04001699 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001700
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001701 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001702 /*
1703 * If we're setting no_new_privs, we can drop privileges
1704 * before setting seccomp filter. This way filter policies
1705 * don't need to allow privilege-dropping syscalls.
1706 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001707 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001708 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001709 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001710 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001711 /*
1712 * If we're not setting no_new_privs,
1713 * we need to set seccomp filter *before* dropping privileges.
1714 * WARNING: this means that filter policies *must* allow
1715 * setgroups()/setresgid()/setresuid() for dropping root and
1716 * capget()/capset()/prctl() for dropping caps.
1717 */
1718 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001719 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001720 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001721 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001722
Elly Jonesdd3e8512012-01-23 15:13:38 -05001723 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001724 * Select the specified alternate syscall table. The table must not
1725 * block prctl(2) if we're using seccomp as well.
1726 */
1727 if (j->flags.alt_syscall) {
1728 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001729 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001730 }
1731
1732 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001733 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001734 * privilege-dropping syscalls :)
1735 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001736 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001737 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001738 warn("seccomp not supported");
1739 return;
1740 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001741 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001742 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001743}
1744
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001745/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001746static int init_exitstatus = 0;
1747
Will Drewry6ac91122011-10-21 16:38:58 -05001748void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001749{
1750 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001751}
1752
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001753void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001754{
1755 pid_t pid;
1756 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001757 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001758 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001759 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001760 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001761 /*
1762 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001763 * left inside our pid namespace or we get a signal.
1764 */
1765 if (pid == rootpid)
1766 init_exitstatus = status;
1767 }
1768 if (!WIFEXITED(init_exitstatus))
1769 _exit(MINIJAIL_ERR_INIT);
1770 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001771}
1772
Will Drewry6ac91122011-10-21 16:38:58 -05001773int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001774{
1775 size_t sz = 0;
1776 size_t bytes = read(fd, &sz, sizeof(sz));
1777 char *buf;
1778 int r;
1779 if (sizeof(sz) != bytes)
1780 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001781 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001782 return -E2BIG;
1783 buf = malloc(sz);
1784 if (!buf)
1785 return -ENOMEM;
1786 bytes = read(fd, buf, sz);
1787 if (bytes != sz) {
1788 free(buf);
1789 return -EINVAL;
1790 }
1791 r = minijail_unmarshal(j, buf, sz);
1792 free(buf);
1793 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001794}
1795
Will Drewry6ac91122011-10-21 16:38:58 -05001796int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001797{
1798 char *buf;
1799 size_t sz = minijail_size(j);
1800 ssize_t written;
1801 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001802
Elly Jonese1749eb2011-10-07 13:54:59 -04001803 if (!sz)
1804 return -EINVAL;
1805 buf = malloc(sz);
1806 r = minijail_marshal(j, buf, sz);
1807 if (r) {
1808 free(buf);
1809 return r;
1810 }
1811 /* Sends [size][minijail]. */
1812 written = write(fd, &sz, sizeof(sz));
1813 if (written != sizeof(sz)) {
1814 free(buf);
1815 return -EFAULT;
1816 }
1817 written = write(fd, buf, sz);
1818 if (written < 0 || (size_t) written != sz) {
1819 free(buf);
1820 return -EFAULT;
1821 }
1822 free(buf);
1823 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001824}
Elly Jonescd7a9042011-07-22 13:56:51 -04001825
Will Drewry6ac91122011-10-21 16:38:58 -05001826int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001827{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001828#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001829 /* Don't use LDPRELOAD on Brillo. */
1830 return 0;
1831#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001832 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1833 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1834 if (!newenv)
1835 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001836
Elly Jonese1749eb2011-10-07 13:54:59 -04001837 /* Only insert a separating space if we have something to separate... */
1838 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1839 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001840
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001841 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001842 setenv(kLdPreloadEnvVar, newenv, 1);
1843 free(newenv);
1844 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001845#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001846}
1847
Will Drewry6ac91122011-10-21 16:38:58 -05001848int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001849{
1850 int r = pipe(fds);
1851 char fd_buf[11];
1852 if (r)
1853 return r;
1854 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1855 if (r <= 0)
1856 return -EINVAL;
1857 setenv(kFdEnvVar, fd_buf, 1);
1858 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001859}
1860
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001861int setup_pipe_end(int fds[2], size_t index)
1862{
1863 if (index > 1)
1864 return -1;
1865
1866 close(fds[1 - index]);
1867 return fds[index];
1868}
1869
1870int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1871{
1872 if (index > 1)
1873 return -1;
1874
1875 close(fds[1 - index]);
1876 /* dup2(2) the corresponding end of the pipe into |fd|. */
1877 return dup2(fds[index], fd);
1878}
1879
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001880int close_open_fds(int *inheritable_fds, size_t size)
1881{
1882 const char *kFdPath = "/proc/self/fd";
1883
1884 DIR *d = opendir(kFdPath);
1885 struct dirent *dir_entry;
1886
1887 if (d == NULL)
1888 return -1;
1889 int dir_fd = dirfd(d);
1890 while ((dir_entry = readdir(d)) != NULL) {
1891 size_t i;
1892 char *end;
1893 bool should_close = true;
1894 const int fd = strtol(dir_entry->d_name, &end, 10);
1895
1896 if ((*end) != '\0') {
1897 continue;
1898 }
1899 /*
1900 * We might have set up some pipes that we want to share with
1901 * the parent process, and should not be closed.
1902 */
1903 for (i = 0; i < size; ++i) {
1904 if (fd == inheritable_fds[i]) {
1905 should_close = false;
1906 break;
1907 }
1908 }
1909 /* Also avoid closing the directory fd. */
1910 if (should_close && fd != dir_fd)
1911 close(fd);
1912 }
1913 closedir(d);
1914 return 0;
1915}
1916
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001917int minijail_run_internal(struct minijail *j, const char *filename,
1918 char *const argv[], pid_t *pchild_pid,
1919 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1920 int use_preload);
1921
Will Drewry6ac91122011-10-21 16:38:58 -05001922int API minijail_run(struct minijail *j, const char *filename,
1923 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001924{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001925 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1926 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001927}
1928
1929int API minijail_run_pid(struct minijail *j, const char *filename,
1930 char *const argv[], pid_t *pchild_pid)
1931{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001932 return minijail_run_internal(j, filename, argv, pchild_pid,
1933 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001934}
1935
1936int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001937 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001938{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001939 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1940 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001941}
1942
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001943int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001944 char *const argv[], pid_t *pchild_pid,
1945 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001946{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001947 return minijail_run_internal(j, filename, argv, pchild_pid,
1948 pstdin_fd, pstdout_fd, pstderr_fd, true);
1949}
1950
1951int API minijail_run_no_preload(struct minijail *j, const char *filename,
1952 char *const argv[])
1953{
1954 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1955 false);
1956}
1957
Samuel Tan63187f42015-10-16 13:01:53 -07001958int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001959 const char *filename,
1960 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001961 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001962 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001963 int *pstderr_fd)
1964{
Samuel Tan63187f42015-10-16 13:01:53 -07001965 return minijail_run_internal(j, filename, argv, pchild_pid,
1966 pstdin_fd, pstdout_fd, pstderr_fd, false);
1967}
1968
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001969int minijail_run_internal(struct minijail *j, const char *filename,
1970 char *const argv[], pid_t *pchild_pid,
1971 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1972 int use_preload)
1973{
Elly Jonese1749eb2011-10-07 13:54:59 -04001974 char *oldenv, *oldenv_copy = NULL;
1975 pid_t child_pid;
1976 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001977 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001978 int stdout_fds[2];
1979 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001980 int child_sync_pipe_fds[2];
1981 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001982 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001983 /* We need to remember this across the minijail_preexec() call. */
1984 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001985 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001986
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001987 if (use_preload) {
1988 oldenv = getenv(kLdPreloadEnvVar);
1989 if (oldenv) {
1990 oldenv_copy = strdup(oldenv);
1991 if (!oldenv_copy)
1992 return -ENOMEM;
1993 }
1994
1995 if (setup_preload())
1996 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001997 }
Will Drewryf89aef52011-09-16 16:48:57 -05001998
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001999 if (!use_preload) {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002000 if (j->flags.use_caps && j->caps != 0)
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05002001 die("non-empty capabilities are not supported without "
2002 "LD_PRELOAD");
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002003 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002004
Elly Jonesdd3e8512012-01-23 15:13:38 -05002005 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002006 * Make the process group ID of this process equal to its PID.
2007 * In the non-interactive case (e.g. when the parent process is started
2008 * from init) this ensures the parent process and the jailed process
2009 * can be killed together.
2010 * When the parent process is started from the console this ensures
2011 * the call to setsid(2) in the jailed process succeeds.
2012 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002013 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2014 * the process is already a process group leader.
2015 */
2016 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2017 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002018 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002019 }
2020 }
2021
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002022 if (use_preload) {
2023 /*
2024 * Before we fork(2) and execve(2) the child process, we need
2025 * to open a pipe(2) to send the minijail configuration over.
2026 */
2027 if (setup_pipe(pipe_fds))
2028 return -EFAULT;
2029 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002030
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002031 /*
2032 * If we want to write to the child process' standard input,
2033 * create the pipe(2) now.
2034 */
2035 if (pstdin_fd) {
2036 if (pipe(stdin_fds))
2037 return -EFAULT;
2038 }
2039
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002040 /*
2041 * If we want to read from the child process' standard output,
2042 * create the pipe(2) now.
2043 */
2044 if (pstdout_fd) {
2045 if (pipe(stdout_fds))
2046 return -EFAULT;
2047 }
2048
2049 /*
2050 * If we want to read from the child process' standard error,
2051 * create the pipe(2) now.
2052 */
2053 if (pstderr_fd) {
2054 if (pipe(stderr_fds))
2055 return -EFAULT;
2056 }
2057
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002058 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002059 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002060 * or if we need to add the child process to cgroups, create the pipe(2)
2061 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002062 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002063 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002064 sync_child = 1;
2065 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002066 return -EFAULT;
2067 }
2068
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002069 /*
2070 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002071 *
2072 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2073 *
2074 * In multithreaded programs, there are a bunch of locks inside libc,
2075 * some of which may be held by other threads at the time that we call
2076 * minijail_run_pid(). If we call fork(), glibc does its level best to
2077 * ensure that we hold all of these locks before it calls clone()
2078 * internally and drop them after clone() returns, but when we call
2079 * sys_clone(2) directly, all that gets bypassed and we end up with a
2080 * child address space where some of libc's important locks are held by
2081 * other threads (which did not get cloned, and hence will never release
2082 * those locks). This is okay so long as we call exec() immediately
2083 * after, but a bunch of seemingly-innocent libc functions like setenv()
2084 * take locks.
2085 *
2086 * Hence, only call sys_clone() if we need to, in order to get at pid
2087 * namespacing. If we follow this path, the child's address space might
2088 * have broken locks; you may only call functions that do not acquire
2089 * any locks.
2090 *
2091 * Unfortunately, fork() acquires every lock it can get its hands on, as
2092 * previously detailed, so this function is highly likely to deadlock
2093 * later on (see "deadlock here") if we're multithreaded.
2094 *
2095 * We might hack around this by having the clone()d child (init of the
2096 * pid namespace) return directly, rather than leaving the clone()d
2097 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002098 * its fork()ed child return in turn), but that process would be
2099 * crippled with its libc locks potentially broken. We might try
2100 * fork()ing in the parent before we clone() to ensure that we own all
2101 * the locks, but then we have to have the forked child hanging around
2102 * consuming resources (and possibly having file descriptors / shared
2103 * memory regions / etc attached). We'd need to keep the child around to
2104 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002105 *
2106 * TODO(ellyjones): figure out if the "forked child hanging around"
2107 * problem is fixable or not. It would be nice if we worked in this
2108 * case.
2109 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002110 if (pid_namespace) {
2111 int clone_flags = CLONE_NEWPID | SIGCHLD;
2112 if (j->flags.userns)
2113 clone_flags |= CLONE_NEWUSER;
2114 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002115 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002116 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002117 }
Elly Jones761b7412012-06-13 15:49:52 -04002118
Elly Jonese1749eb2011-10-07 13:54:59 -04002119 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002120 if (use_preload) {
2121 free(oldenv_copy);
2122 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002123 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002124 }
Will Drewryf89aef52011-09-16 16:48:57 -05002125
Elly Jonese1749eb2011-10-07 13:54:59 -04002126 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002127 if (use_preload) {
2128 /* Restore parent's LD_PRELOAD. */
2129 if (oldenv_copy) {
2130 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2131 free(oldenv_copy);
2132 } else {
2133 unsetenv(kLdPreloadEnvVar);
2134 }
2135 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002136 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002137
Elly Jonese1749eb2011-10-07 13:54:59 -04002138 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002139
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002140 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002141 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002142
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002143 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002144 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002145
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002146 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002147 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002148
2149 if (sync_child)
2150 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002151
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002152 if (use_preload) {
2153 /* Send marshalled minijail. */
2154 close(pipe_fds[0]); /* read endpoint */
2155 ret = minijail_to_fd(j, pipe_fds[1]);
2156 close(pipe_fds[1]); /* write endpoint */
2157 if (ret) {
2158 kill(j->initpid, SIGKILL);
2159 die("failed to send marshalled minijail");
2160 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002161 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002162
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002163 if (pchild_pid)
2164 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002165
2166 /*
2167 * If we want to write to the child process' standard input,
2168 * set up the write end of the pipe.
2169 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002170 if (pstdin_fd)
2171 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002172 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002173
2174 /*
2175 * If we want to read from the child process' standard output,
2176 * set up the read end of the pipe.
2177 */
2178 if (pstdout_fd)
2179 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002180 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002181
2182 /*
2183 * If we want to read from the child process' standard error,
2184 * set up the read end of the pipe.
2185 */
2186 if (pstderr_fd)
2187 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002188 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002189
Elly Jonese1749eb2011-10-07 13:54:59 -04002190 return 0;
2191 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002192 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002193 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002194
Peter Qiu2860c462015-12-16 15:13:06 -08002195 if (j->flags.reset_signal_mask) {
2196 sigset_t signal_mask;
2197 if (sigemptyset(&signal_mask) != 0)
2198 pdie("sigemptyset failed");
2199 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2200 pdie("sigprocmask failed");
2201 }
2202
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002203 if (j->flags.close_open_fds) {
2204 const size_t kMaxInheritableFdsSize = 10;
2205 int inheritable_fds[kMaxInheritableFdsSize];
2206 size_t size = 0;
2207 if (use_preload) {
2208 inheritable_fds[size++] = pipe_fds[0];
2209 inheritable_fds[size++] = pipe_fds[1];
2210 }
2211 if (sync_child) {
2212 inheritable_fds[size++] = child_sync_pipe_fds[0];
2213 inheritable_fds[size++] = child_sync_pipe_fds[1];
2214 }
2215 if (pstdin_fd) {
2216 inheritable_fds[size++] = stdin_fds[0];
2217 inheritable_fds[size++] = stdin_fds[1];
2218 }
2219 if (pstdout_fd) {
2220 inheritable_fds[size++] = stdout_fds[0];
2221 inheritable_fds[size++] = stdout_fds[1];
2222 }
2223 if (pstderr_fd) {
2224 inheritable_fds[size++] = stderr_fds[0];
2225 inheritable_fds[size++] = stderr_fds[1];
2226 }
2227
2228 if (close_open_fds(inheritable_fds, size) < 0)
2229 die("failed to close open file descriptors");
2230 }
2231
Dylan Reidce5b55e2016-01-13 11:04:16 -08002232 if (sync_child)
2233 wait_for_parent_setup(child_sync_pipe_fds);
2234
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002235 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002236 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002237
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002238 /*
2239 * If we want to write to the jailed process' standard input,
2240 * set up the read end of the pipe.
2241 */
2242 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002243 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2244 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002245 die("failed to set up stdin pipe");
2246 }
2247
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002248 /*
2249 * If we want to read from the jailed process' standard output,
2250 * set up the write end of the pipe.
2251 */
2252 if (pstdout_fd) {
2253 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2254 STDOUT_FILENO) < 0)
2255 die("failed to set up stdout pipe");
2256 }
2257
2258 /*
2259 * If we want to read from the jailed process' standard error,
2260 * set up the write end of the pipe.
2261 */
2262 if (pstderr_fd) {
2263 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2264 STDERR_FILENO) < 0)
2265 die("failed to set up stderr pipe");
2266 }
2267
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002268 /*
2269 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2270 * This prevents the jailed process from using the TIOCSTI ioctl
2271 * to push characters into the parent process terminal's input buffer,
2272 * therefore escaping the jail.
2273 */
2274 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2275 isatty(STDERR_FILENO)) {
2276 if (setsid() < 0) {
2277 pdie("setsid() failed");
2278 }
2279 }
2280
Dylan Reid791f5772015-09-14 20:02:42 -07002281 /* If running an init program, let it decide when/how to mount /proc. */
2282 if (pid_namespace && !do_init)
2283 j->flags.remount_proc_ro = 0;
2284
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002285 if (use_preload) {
2286 /* Strip out flags that cannot be inherited across execve(2). */
2287 minijail_preexec(j);
2288 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002289 /*
2290 * If not using LD_PRELOAD, do all jailing before execve(2).
2291 * Note that PID namespaces can only be entered on fork(2),
2292 * so that flag is still cleared.
2293 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002294 j->flags.pids = 0;
2295 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002296 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002297 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002298
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002299 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002300 /*
2301 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002302 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002303 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002304 * a child to actually run the program. If |do_init == 0|, we
2305 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002306 *
2307 * If we're multithreaded, we'll probably deadlock here. See
2308 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002309 */
2310 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002311 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002312 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002313 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002314 /*
2315 * Best effort. Don't bother checking the return value.
2316 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002317 prctl(PR_SET_NAME, "minijail-init");
2318 init(child_pid); /* Never returns. */
2319 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002320 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002321
Elly Jonesdd3e8512012-01-23 15:13:38 -05002322 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002323 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002324 * calling process
2325 * -> execve()-ing process
2326 * If we are:
2327 * calling process
2328 * -> init()-ing process
2329 * -> execve()-ing process
2330 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002331 ret = execve(filename, argv, environ);
2332 if (ret == -1) {
2333 pwarn("execve(%s) failed", filename);
2334 }
2335 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002336}
2337
Will Drewry6ac91122011-10-21 16:38:58 -05002338int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002339{
2340 int st;
2341 if (kill(j->initpid, SIGTERM))
2342 return -errno;
2343 if (waitpid(j->initpid, &st, 0) < 0)
2344 return -errno;
2345 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002346}
2347
Will Drewry6ac91122011-10-21 16:38:58 -05002348int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002349{
2350 int st;
2351 if (waitpid(j->initpid, &st, 0) < 0)
2352 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002353
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002354 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002355 int error_status = st;
2356 if (WIFSIGNALED(st)) {
2357 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002358 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002359 j->initpid, signum);
2360 /*
2361 * We return MINIJAIL_ERR_JAIL if the process received
2362 * SIGSYS, which happens when a syscall is blocked by
2363 * seccomp filters.
2364 * If not, we do what bash(1) does:
2365 * $? = 128 + signum
2366 */
2367 if (signum == SIGSYS) {
2368 error_status = MINIJAIL_ERR_JAIL;
2369 } else {
2370 error_status = 128 + signum;
2371 }
2372 }
2373 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002374 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002375
2376 int exit_status = WEXITSTATUS(st);
2377 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002378 info("child process %d exited with status %d",
2379 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002380
2381 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002382}
2383
Will Drewry6ac91122011-10-21 16:38:58 -05002384void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002385{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002386 size_t i;
2387
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002388 if (j->flags.seccomp_filter && j->filter_prog) {
2389 free(j->filter_prog->filter);
2390 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002391 }
Dylan Reid648b2202015-10-23 00:50:00 -07002392 while (j->mounts_head) {
2393 struct mountpoint *m = j->mounts_head;
2394 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002395 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002396 free(m->type);
2397 free(m->dest);
2398 free(m->src);
2399 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002400 }
Dylan Reid648b2202015-10-23 00:50:00 -07002401 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002402 if (j->user)
2403 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002404 if (j->suppl_gid_list)
2405 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002406 if (j->chrootdir)
2407 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002408 if (j->pid_file_path)
2409 free(j->pid_file_path);
2410 if (j->uidmap)
2411 free(j->uidmap);
2412 if (j->gidmap)
2413 free(j->gidmap);
Andrew Brestickereac28942015-11-11 16:04:46 -08002414 if (j->alt_syscall_table)
2415 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002416 for (i = 0; i < j->cgroup_count; ++i)
2417 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002418 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002419}