blob: 6381c305ccd001c5cece200afa93618aa7c7d214 [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>
Luis Hector Chavez43ff0802016-10-07 12:21:07 -070011#include <dirent.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040012#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070013#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040014#include <grp.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040015#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <pwd.h>
17#include <sched.h>
18#include <signal.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070019#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080020#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040024#include <sys/capability.h>
25#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050026#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040027#include <sys/prctl.h>
Dylan Reid0f72ef42017-06-06 15:42:49 -070028#include <sys/resource.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070029#include <sys/stat.h>
30#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080031#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040032#include <sys/wait.h>
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -070033#include <syscall.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <unistd.h>
35
36#include "libminijail.h"
37#include "libminijail-private.h"
38
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070039#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040#include "syscall_filter.h"
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040041#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040042#include "system.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080044
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -070045/* Until these are reliably available in linux/prctl.h. */
Andrew Brestickereac28942015-11-11 16:04:46 -080046#ifndef PR_ALT_SYSCALL
47# define PR_ALT_SYSCALL 0x43724f53
48#endif
49
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040050/* Seccomp filter related flags. */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#ifndef PR_SET_NO_NEW_PRIVS
52# define PR_SET_NO_NEW_PRIVS 38
53#endif
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040054
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080055#ifndef SECCOMP_MODE_FILTER
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040056#define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050057#endif
58
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040059#ifndef SECCOMP_SET_MODE_STRICT
60# define SECCOMP_SET_MODE_STRICT 0
61#endif
62#ifndef SECCOMP_SET_MODE_FILTER
63# define SECCOMP_SET_MODE_FILTER 1
64#endif
65
66#ifndef SECCOMP_FILTER_FLAG_TSYNC
67# define SECCOMP_FILTER_FLAG_TSYNC 1
68#endif
69/* End seccomp filter related flags. */
70
Dylan Reid4cbc2a52016-06-17 19:06:07 -070071/* New cgroup namespace might not be in linux-headers yet. */
72#ifndef CLONE_NEWCGROUP
73# define CLONE_NEWCGROUP 0x02000000
74#endif
75
Dylan Reid605ce7f2016-01-19 19:21:00 -080076#define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */
77
Dylan Reid0f72ef42017-06-06 15:42:49 -070078#define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */
79
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080080/* Keyctl commands. */
81#define KEYCTL_JOIN_SESSION_KEYRING 1
82
Dylan Reid0f72ef42017-06-06 15:42:49 -070083struct minijail_rlimit {
84 int type;
85 uint32_t cur;
86 uint32_t max;
87};
88
Dylan Reid648b2202015-10-23 00:50:00 -070089struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040090 char *src;
91 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070092 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070093 char *data;
94 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070095 unsigned long flags;
96 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040097};
98
Will Drewryf89aef52011-09-16 16:48:57 -050099struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700100 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700101 * WARNING: if you add a flag here you need to make sure it's
102 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700103 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400104 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700105 int uid : 1;
106 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100107 int inherit_suppl_gids : 1;
108 int set_suppl_gids : 1;
109 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700110 int use_caps : 1;
111 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400112 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700113 int vfs : 1;
114 int enter_vfs : 1;
115 int skip_remount_private : 1;
116 int pids : 1;
117 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400118 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700119 int net : 1;
120 int enter_net : 1;
121 int ns_cgroups : 1;
122 int userns : 1;
123 int disable_setgroups : 1;
124 int seccomp : 1;
125 int remount_proc_ro : 1;
126 int no_new_privs : 1;
127 int seccomp_filter : 1;
128 int seccomp_filter_tsync : 1;
129 int seccomp_filter_logging : 1;
130 int chroot : 1;
131 int pivot_root : 1;
132 int mount_tmp : 1;
133 int do_init : 1;
134 int pid_file : 1;
135 int cgroups : 1;
136 int alt_syscall : 1;
137 int reset_signal_mask : 1;
138 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800139 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400140 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400141 } flags;
142 uid_t uid;
143 gid_t gid;
144 gid_t usergid;
145 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800146 size_t suppl_gid_count;
147 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400148 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800149 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400150 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700151 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700152 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400153 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800154 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800155 char *uidmap;
156 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400157 char *hostname;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800158 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800159 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800160 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700161 struct mountpoint *mounts_head;
162 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800163 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100164 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800165 char *cgroups[MAX_CGROUPS];
166 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700167 struct minijail_rlimit rlimits[MAX_RLIMITS];
168 size_t rlimit_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500169};
170
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700171/*
172 * Strip out flags meant for the parent.
173 * We keep things that are not inherited across execve(2) (e.g. capabilities),
174 * or are easier to set after execve(2) (e.g. seccomp filters).
175 */
176void minijail_preenter(struct minijail *j)
177{
178 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700179 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900180 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700181 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700182 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800183 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800184 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800185 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400186 j->flags.forward_signals = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700187}
188
189/*
190 * Strip out flags meant for the child.
191 * We keep things that are inherited across execve(2).
192 */
193void minijail_preexec(struct minijail *j)
194{
195 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700196 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800197 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700198 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800199 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700200 if (j->user)
201 free(j->user);
202 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800203 if (j->suppl_gid_list)
204 free(j->suppl_gid_list);
205 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700206 memset(&j->flags, 0, sizeof(j->flags));
207 /* Now restore anything we meant to keep. */
208 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700209 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800210 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700211 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800212 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700213 /* Note, |pids| will already have been used before this call. */
214}
215
216/* Minijail API. */
217
Will Drewry6ac91122011-10-21 16:38:58 -0500218struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400219{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400220 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400221}
222
Will Drewry6ac91122011-10-21 16:38:58 -0500223void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400224{
225 if (uid == 0)
226 die("useless change to uid 0");
227 j->uid = uid;
228 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400229}
230
Will Drewry6ac91122011-10-21 16:38:58 -0500231void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400232{
233 if (gid == 0)
234 die("useless change to gid 0");
235 j->gid = gid;
236 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400237}
238
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800239void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
240 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800241{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800242 size_t i;
243
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500244 if (j->flags.inherit_suppl_gids)
245 die("cannot inherit *and* set supplementary groups");
246 if (j->flags.keep_suppl_gids)
247 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800248
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800249 if (size == 0) {
250 /* Clear supplementary groups. */
251 j->suppl_gid_list = NULL;
252 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100253 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800254 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800255 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800256
257 /* Copy the gid_t array. */
258 j->suppl_gid_list = calloc(size, sizeof(gid_t));
259 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800260 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800261 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800262 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800263 j->suppl_gid_list[i] = list[i];
264 }
265 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100266 j->flags.set_suppl_gids = 1;
267}
268
269void API minijail_keep_supplementary_gids(struct minijail *j) {
270 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800271}
272
Will Drewry6ac91122011-10-21 16:38:58 -0500273int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400274{
275 char *buf = NULL;
276 struct passwd pw;
277 struct passwd *ppw = NULL;
278 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
279 if (sz == -1)
280 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400281
Elly Jonesdd3e8512012-01-23 15:13:38 -0500282 /*
283 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400284 * the maximum needed size of the buffer, so we don't have to search.
285 */
286 buf = malloc(sz);
287 if (!buf)
288 return -ENOMEM;
289 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500290 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800291 * We're safe to free the buffer here. The strings inside |pw| point
292 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800293 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
294 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500295 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400296 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700297 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400298 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700299 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400300 minijail_change_uid(j, ppw->pw_uid);
301 j->user = strdup(user);
302 if (!j->user)
303 return -ENOMEM;
304 j->usergid = ppw->pw_gid;
305 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400306}
307
Will Drewry6ac91122011-10-21 16:38:58 -0500308int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400309{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700310 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700311 struct group gr;
312 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400313 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
314 if (sz == -1)
315 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400316
Elly Jonesdd3e8512012-01-23 15:13:38 -0500317 /*
318 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400319 * the maximum needed size of the buffer, so we don't have to search.
320 */
321 buf = malloc(sz);
322 if (!buf)
323 return -ENOMEM;
324 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500325 /*
326 * We're safe to free the buffer here. The strings inside gr point
327 * inside buf, but we don't use any of them; this leaves the pointers
328 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
329 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400330 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700331 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400332 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700333 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400334 minijail_change_gid(j, pgr->gr_gid);
335 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400336}
337
Will Drewry6ac91122011-10-21 16:38:58 -0500338void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400339{
340 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400341}
342
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700343void API minijail_no_new_privs(struct minijail *j)
344{
345 j->flags.no_new_privs = 1;
346}
347
Will Drewry6ac91122011-10-21 16:38:58 -0500348void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400349{
350 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500351}
352
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400353void API minijail_set_seccomp_filter_tsync(struct minijail *j)
354{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400355 if (j->filter_len > 0 && j->filter_prog != NULL) {
356 die("minijail_set_seccomp_filter_tsync() must be called "
357 "before minijail_parse_seccomp_filters()");
358 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400359 j->flags.seccomp_filter_tsync = 1;
360}
361
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700362void API minijail_log_seccomp_filter_failures(struct minijail *j)
363{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400364 if (j->filter_len > 0 && j->filter_prog != NULL) {
365 die("minijail_log_seccomp_filter_failures() must be called "
366 "before minijail_parse_seccomp_filters()");
367 }
368 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700369}
370
Will Drewry6ac91122011-10-21 16:38:58 -0500371void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400372{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800373 /*
374 * 'minijail_use_caps' configures a runtime-capabilities-only
375 * environment, including a bounding set matching the thread's runtime
376 * (permitted|inheritable|effective) sets.
377 * Therefore, it will override any existing bounding set configurations
378 * since the latter would allow gaining extra runtime capabilities from
379 * file capabilities.
380 */
381 if (j->flags.capbset_drop) {
382 warn("overriding bounding set configuration");
383 j->cap_bset = 0;
384 j->flags.capbset_drop = 0;
385 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400386 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800387 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400388}
389
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800390void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
391{
392 if (j->flags.use_caps) {
393 /*
394 * 'minijail_use_caps' will have already configured a capability
395 * bounding set matching the (permitted|inheritable|effective)
396 * sets. Abort if the user tries to configure a separate
397 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
398 * are mutually exclusive.
399 */
400 die("runtime capabilities already configured, can't drop "
401 "bounding set separately");
402 }
403 j->cap_bset = capmask;
404 j->flags.capbset_drop = 1;
405}
406
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400407void API minijail_set_ambient_caps(struct minijail *j)
408{
409 j->flags.set_ambient_caps = 1;
410}
411
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800412void API minijail_reset_signal_mask(struct minijail *j)
413{
Peter Qiu2860c462015-12-16 15:13:06 -0800414 j->flags.reset_signal_mask = 1;
415}
416
Will Drewry6ac91122011-10-21 16:38:58 -0500417void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400418{
419 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400420}
421
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700422void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
423{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800424 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700425 if (ns_fd < 0) {
426 pdie("failed to open namespace '%s'", ns_path);
427 }
428 j->mountns_fd = ns_fd;
429 j->flags.enter_vfs = 1;
430}
431
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800432void API minijail_new_session_keyring(struct minijail *j)
433{
434 j->flags.new_session_keyring = 1;
435}
436
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800437void API minijail_skip_remount_private(struct minijail *j)
438{
439 j->flags.skip_remount_private = 1;
440}
441
Will Drewry6ac91122011-10-21 16:38:58 -0500442void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400443{
Elly Jonese58176c2012-01-23 11:46:17 -0500444 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700445 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400446 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800447 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400448}
449
Dylan Reidf7942472015-11-18 17:55:26 -0800450void API minijail_namespace_ipc(struct minijail *j)
451{
452 j->flags.ipc = 1;
453}
454
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400455void API minijail_namespace_uts(struct minijail *j)
456{
457 j->flags.uts = 1;
458}
459
460int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
461{
462 if (j->hostname)
463 return -EINVAL;
464 minijail_namespace_uts(j);
465 j->hostname = strdup(name);
466 if (!j->hostname)
467 return -ENOMEM;
468 return 0;
469}
470
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400471void API minijail_namespace_net(struct minijail *j)
472{
473 j->flags.net = 1;
474}
475
Dylan Reid1102f5a2015-09-15 11:52:20 -0700476void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
477{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800478 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700479 if (ns_fd < 0) {
480 pdie("failed to open namespace '%s'", ns_path);
481 }
482 j->netns_fd = ns_fd;
483 j->flags.enter_net = 1;
484}
485
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700486void API minijail_namespace_cgroups(struct minijail *j)
487{
488 j->flags.ns_cgroups = 1;
489}
490
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700491void API minijail_close_open_fds(struct minijail *j)
492{
493 j->flags.close_open_fds = 1;
494}
495
Dylan Reid791f5772015-09-14 20:02:42 -0700496void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400497{
498 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700499 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400500}
501
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800502void API minijail_namespace_user(struct minijail *j)
503{
504 j->flags.userns = 1;
505}
506
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400507void API minijail_namespace_user_disable_setgroups(struct minijail *j)
508{
509 j->flags.disable_setgroups = 1;
510}
511
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800512int API minijail_uidmap(struct minijail *j, const char *uidmap)
513{
514 j->uidmap = strdup(uidmap);
515 if (!j->uidmap)
516 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800517 char *ch;
518 for (ch = j->uidmap; *ch; ch++) {
519 if (*ch == ',')
520 *ch = '\n';
521 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800522 return 0;
523}
524
525int API minijail_gidmap(struct minijail *j, const char *gidmap)
526{
527 j->gidmap = strdup(gidmap);
528 if (!j->gidmap)
529 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800530 char *ch;
531 for (ch = j->gidmap; *ch; ch++) {
532 if (*ch == ',')
533 *ch = '\n';
534 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800535 return 0;
536}
537
Will Drewry6ac91122011-10-21 16:38:58 -0500538void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400539{
Lutz Justen13807cb2017-01-03 17:11:55 +0100540 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400541}
542
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800543void API minijail_run_as_init(struct minijail *j)
544{
545 /*
546 * Since the jailed program will become 'init' in the new PID namespace,
547 * Minijail does not need to fork an 'init' process.
548 */
549 j->flags.do_init = 0;
550}
551
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700552int API minijail_enter_chroot(struct minijail *j, const char *dir)
553{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400554 if (j->chrootdir)
555 return -EINVAL;
556 j->chrootdir = strdup(dir);
557 if (!j->chrootdir)
558 return -ENOMEM;
559 j->flags.chroot = 1;
560 return 0;
561}
562
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800563int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
564{
565 if (j->chrootdir)
566 return -EINVAL;
567 j->chrootdir = strdup(dir);
568 if (!j->chrootdir)
569 return -ENOMEM;
570 j->flags.pivot_root = 1;
571 return 0;
572}
573
Dylan Reida14e08d2015-10-22 21:05:29 -0700574char API *minijail_get_original_path(struct minijail *j,
575 const char *path_inside_chroot)
576{
Dylan Reid648b2202015-10-23 00:50:00 -0700577 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700578
Dylan Reid648b2202015-10-23 00:50:00 -0700579 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700580 while (b) {
581 /*
582 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700583 * mount, then the original path is exactly the source of
584 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700585 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700586 * mount source = /some/path/exe, mount dest =
587 * /chroot/path/exe Then when getting the original path of
588 * "/chroot/path/exe", the source of that mount,
589 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700590 */
591 if (!strcmp(b->dest, path_inside_chroot))
592 return strdup(b->src);
593
594 /*
595 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700596 * mount, take the suffix of the chroot path relative to the
597 * mount destination path, and append it to the mount source
598 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700599 */
600 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
601 const char *relative_path =
602 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400603 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700604 }
605 b = b->next;
606 }
607
608 /* If there is a chroot path, append |path_inside_chroot| to that. */
609 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400610 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700611
612 /* No chroot, so the path outside is the same as it is inside. */
613 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700614}
615
Martin Pelikánab9eb442017-01-25 11:53:58 +1100616size_t minijail_get_tmpfs_size(const struct minijail *j)
617{
618 return j->tmpfs_size;
619}
620
Lee Campbell11af0622014-05-22 12:36:04 -0700621void API minijail_mount_tmp(struct minijail *j)
622{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100623 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
624}
625
626void API minijail_mount_tmp_size(struct minijail *j, size_t size)
627{
628 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700629 j->flags.mount_tmp = 1;
630}
631
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800632int API minijail_write_pid_file(struct minijail *j, const char *path)
633{
634 j->pid_file_path = strdup(path);
635 if (!j->pid_file_path)
636 return -ENOMEM;
637 j->flags.pid_file = 1;
638 return 0;
639}
640
Dylan Reid605ce7f2016-01-19 19:21:00 -0800641int API minijail_add_to_cgroup(struct minijail *j, const char *path)
642{
643 if (j->cgroup_count >= MAX_CGROUPS)
644 return -ENOMEM;
645 j->cgroups[j->cgroup_count] = strdup(path);
646 if (!j->cgroups[j->cgroup_count])
647 return -ENOMEM;
648 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800649 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800650 return 0;
651}
652
Dylan Reid0f72ef42017-06-06 15:42:49 -0700653int API minijail_rlimit(struct minijail *j, int type, uint32_t cur,
654 uint32_t max)
655{
656 size_t i;
657
658 if (j->rlimit_count >= MAX_RLIMITS)
659 return -ENOMEM;
660 /* It's an error if the caller sets the same rlimit multiple times. */
661 for (i = 0; i < j->rlimit_count; i++) {
662 if (j->rlimits[i].type == type)
663 return -EEXIST;
664 }
665
666 j->rlimits[j->rlimit_count].type = type;
667 j->rlimits[j->rlimit_count].cur = cur;
668 j->rlimits[j->rlimit_count].max = max;
669 j->rlimit_count++;
670 return 0;
671}
672
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400673int API minijail_forward_signals(struct minijail *j)
674{
675 j->flags.forward_signals = 1;
676 return 0;
677}
678
Dylan Reid81e23972016-05-18 14:06:35 -0700679int API minijail_mount_with_data(struct minijail *j, const char *src,
680 const char *dest, const char *type,
681 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700682{
Dylan Reid648b2202015-10-23 00:50:00 -0700683 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400684
685 if (*dest != '/')
686 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700687 m = calloc(1, sizeof(*m));
688 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400689 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700690 m->dest = strdup(dest);
691 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400692 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700693 m->src = strdup(src);
694 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400695 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700696 m->type = strdup(type);
697 if (!m->type)
698 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700699 if (data) {
700 m->data = strdup(data);
701 if (!m->data)
702 goto error;
703 m->has_data = 1;
704 }
Dylan Reid648b2202015-10-23 00:50:00 -0700705 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400706
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800707 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400708
Elly Jonesdd3e8512012-01-23 15:13:38 -0500709 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700710 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400711 * containing vfs namespace.
712 */
713 minijail_namespace_vfs(j);
714
Dylan Reid648b2202015-10-23 00:50:00 -0700715 if (j->mounts_tail)
716 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400717 else
Dylan Reid648b2202015-10-23 00:50:00 -0700718 j->mounts_head = m;
719 j->mounts_tail = m;
720 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400721
722 return 0;
723
724error:
Dylan Reid81e23972016-05-18 14:06:35 -0700725 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700726 free(m->src);
727 free(m->dest);
728 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400729 return -ENOMEM;
730}
731
Dylan Reid81e23972016-05-18 14:06:35 -0700732int API minijail_mount(struct minijail *j, const char *src, const char *dest,
733 const char *type, unsigned long flags)
734{
735 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
736}
737
Dylan Reid648b2202015-10-23 00:50:00 -0700738int API minijail_bind(struct minijail *j, const char *src, const char *dest,
739 int writeable)
740{
741 unsigned long flags = MS_BIND;
742
743 if (!writeable)
744 flags |= MS_RDONLY;
745
746 return minijail_mount(j, src, dest, "", flags);
747}
748
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400749static void clear_seccomp_options(struct minijail *j)
750{
751 j->flags.seccomp_filter = 0;
752 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400753 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400754 j->filter_len = 0;
755 j->filter_prog = NULL;
756 j->flags.no_new_privs = 0;
757}
758
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400759static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400760{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400761 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400762 /*
763 * |errno| will be set to EINVAL when seccomp has not been
764 * compiled into the kernel. On certain platforms and kernel
765 * versions this is not a fatal failure. In that case, and only
766 * in that case, disable seccomp and skip loading the filters.
767 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400768 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400769 warn("not loading seccomp filters, seccomp filter not "
770 "supported");
771 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400772 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700773 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400774 /*
775 * If |errno| != EINVAL or seccomp_can_softfail() is false,
776 * we can proceed. Worst case scenario minijail_enter() will
777 * abort() if seccomp fails.
778 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700779 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400780 if (j->flags.seccomp_filter_tsync) {
781 /* Are the seccomp(2) syscall and the TSYNC option supported? */
782 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
783 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
784 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400785 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
786 warn("seccomp(2) syscall not supported");
787 clear_seccomp_options(j);
788 return 0;
789 } else if (saved_errno == EINVAL &&
790 seccomp_can_softfail()) {
791 warn(
792 "seccomp filter thread sync not supported");
793 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400794 return 0;
795 }
796 /*
797 * Similar logic here. If seccomp_can_softfail() is
798 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
799 * we can proceed. Worst case scenario minijail_enter()
800 * will abort() if seccomp or TSYNC fail.
801 */
802 }
803 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400804 return 1;
805}
806
807static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
808{
809 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400810 int use_ret_trap =
811 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
812 int allow_logging = j->flags.seccomp_filter_logging;
813
814 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400815 free(fprog);
816 return -1;
817 }
818
819 j->filter_len = fprog->len;
820 j->filter_prog = fprog;
821 return 0;
822}
823
824void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
825{
826 if (!seccomp_should_parse_filters(j))
827 return;
828
Elly Jonese1749eb2011-10-07 13:54:59 -0400829 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800830 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700831 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400832 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800833
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400834 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700835 die("failed to compile seccomp filter BPF program in '%s'",
836 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800837 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400838 fclose(file);
839}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800840
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400841void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
842{
843 if (!seccomp_should_parse_filters(j))
844 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800845
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400846 FILE *file = fdopen(fd, "r");
847 if (!file) {
848 pdie("failed to associate stream with fd %d", fd);
849 }
850
851 if (parse_seccomp_filters(j, file) != 0) {
852 die("failed to compile seccomp filter BPF program from fd %d",
853 fd);
854 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400855 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500856}
857
Andrew Brestickereac28942015-11-11 16:04:46 -0800858int API minijail_use_alt_syscall(struct minijail *j, const char *table)
859{
860 j->alt_syscall_table = strdup(table);
861 if (!j->alt_syscall_table)
862 return -ENOMEM;
863 j->flags.alt_syscall = 1;
864 return 0;
865}
866
Will Drewryf89aef52011-09-16 16:48:57 -0500867struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400868 size_t available;
869 size_t total;
870 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500871};
872
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800873void marshal_state_init(struct marshal_state *state, char *buf,
874 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400875{
876 state->available = available;
877 state->buf = buf;
878 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500879}
880
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800881void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400882{
883 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500884
Elly Jonese1749eb2011-10-07 13:54:59 -0400885 /* Up to |available| will be written. */
886 if (copy_len) {
887 memcpy(state->buf, src, copy_len);
888 state->buf += copy_len;
889 state->available -= copy_len;
890 }
891 /* |total| will contain the expected length. */
892 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500893}
894
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400895void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700896{
897 marshal_append(state, m->src, strlen(m->src) + 1);
898 marshal_append(state, m->dest, strlen(m->dest) + 1);
899 marshal_append(state, m->type, strlen(m->type) + 1);
900 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
901 if (m->has_data)
902 marshal_append(state, m->data, strlen(m->data) + 1);
903 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
904}
905
Will Drewry6ac91122011-10-21 16:38:58 -0500906void minijail_marshal_helper(struct marshal_state *state,
907 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400908{
Dylan Reid648b2202015-10-23 00:50:00 -0700909 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800910 size_t i;
911
Elly Jonese1749eb2011-10-07 13:54:59 -0400912 marshal_append(state, (char *)j, sizeof(*j));
913 if (j->user)
914 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800915 if (j->suppl_gid_list) {
916 marshal_append(state, j->suppl_gid_list,
917 j->suppl_gid_count * sizeof(gid_t));
918 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400919 if (j->chrootdir)
920 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400921 if (j->hostname)
922 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800923 if (j->alt_syscall_table) {
924 marshal_append(state, j->alt_syscall_table,
925 strlen(j->alt_syscall_table) + 1);
926 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800927 if (j->flags.seccomp_filter && j->filter_prog) {
928 struct sock_fprog *fp = j->filter_prog;
929 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800930 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400931 }
Dylan Reid648b2202015-10-23 00:50:00 -0700932 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400933 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400934 }
Dylan Reid605ce7f2016-01-19 19:21:00 -0800935 for (i = 0; i < j->cgroup_count; ++i)
936 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -0500937}
938
Will Drewry6ac91122011-10-21 16:38:58 -0500939size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400940{
941 struct marshal_state state;
942 marshal_state_init(&state, NULL, 0);
943 minijail_marshal_helper(&state, j);
944 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500945}
946
Elly Jonese1749eb2011-10-07 13:54:59 -0400947int minijail_marshal(const struct minijail *j, char *buf, size_t available)
948{
949 struct marshal_state state;
950 marshal_state_init(&state, buf, available);
951 minijail_marshal_helper(&state, j);
952 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500953}
954
Elly Jonese1749eb2011-10-07 13:54:59 -0400955int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
956{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800957 size_t i;
958 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500959 int ret = -EINVAL;
960
Elly Jonese1749eb2011-10-07 13:54:59 -0400961 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500962 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400963 memcpy((void *)j, serialized, sizeof(*j));
964 serialized += sizeof(*j);
965 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500966
Will Drewrybee7ba72011-10-21 20:47:01 -0500967 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -0400968 j->pid_file_path = NULL;
969 j->uidmap = NULL;
970 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -0700971 j->mounts_head = NULL;
972 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800973 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500974
Elly Jonese1749eb2011-10-07 13:54:59 -0400975 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400976 char *user = consumestr(&serialized, &length);
977 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500978 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400979 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500980 if (!j->user)
981 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400982 }
Will Drewryf89aef52011-09-16 16:48:57 -0500983
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800984 if (j->suppl_gid_list) { /* stale pointer */
985 if (j->suppl_gid_count > NGROUPS_MAX) {
986 goto bad_gid_list;
987 }
988 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
989 void *gid_list_bytes =
990 consumebytes(gid_list_size, &serialized, &length);
991 if (!gid_list_bytes)
992 goto bad_gid_list;
993
994 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
995 if (!j->suppl_gid_list)
996 goto bad_gid_list;
997
998 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
999 }
1000
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001001 if (j->chrootdir) { /* stale pointer */
1002 char *chrootdir = consumestr(&serialized, &length);
1003 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001004 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001005 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001006 if (!j->chrootdir)
1007 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001008 }
1009
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001010 if (j->hostname) { /* stale pointer */
1011 char *hostname = consumestr(&serialized, &length);
1012 if (!hostname)
1013 goto bad_hostname;
1014 j->hostname = strdup(hostname);
1015 if (!j->hostname)
1016 goto bad_hostname;
1017 }
1018
Andrew Brestickereac28942015-11-11 16:04:46 -08001019 if (j->alt_syscall_table) { /* stale pointer */
1020 char *alt_syscall_table = consumestr(&serialized, &length);
1021 if (!alt_syscall_table)
1022 goto bad_syscall_table;
1023 j->alt_syscall_table = strdup(alt_syscall_table);
1024 if (!j->alt_syscall_table)
1025 goto bad_syscall_table;
1026 }
1027
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001028 if (j->flags.seccomp_filter && j->filter_len > 0) {
1029 size_t ninstrs = j->filter_len;
1030 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1031 ninstrs > USHRT_MAX)
1032 goto bad_filters;
1033
1034 size_t program_len = ninstrs * sizeof(struct sock_filter);
1035 void *program = consumebytes(program_len, &serialized, &length);
1036 if (!program)
1037 goto bad_filters;
1038
1039 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001040 if (!j->filter_prog)
1041 goto bad_filters;
1042
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001043 j->filter_prog->len = ninstrs;
1044 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001045 if (!j->filter_prog->filter)
1046 goto bad_filter_prog_instrs;
1047
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001048 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001049 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001050
Dylan Reid648b2202015-10-23 00:50:00 -07001051 count = j->mounts_count;
1052 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001053 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001054 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001055 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001056 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001057 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001058 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001059 const char *src = consumestr(&serialized, &length);
1060 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001061 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001062 dest = consumestr(&serialized, &length);
1063 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001064 goto bad_mounts;
1065 type = consumestr(&serialized, &length);
1066 if (!type)
1067 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001068 has_data = consumebytes(sizeof(*has_data), &serialized,
1069 &length);
1070 if (!has_data)
1071 goto bad_mounts;
1072 if (*has_data) {
1073 data = consumestr(&serialized, &length);
1074 if (!data)
1075 goto bad_mounts;
1076 }
Dylan Reid648b2202015-10-23 00:50:00 -07001077 flags = consumebytes(sizeof(*flags), &serialized, &length);
1078 if (!flags)
1079 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001080 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001081 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001082 }
1083
Dylan Reid605ce7f2016-01-19 19:21:00 -08001084 count = j->cgroup_count;
1085 j->cgroup_count = 0;
1086 for (i = 0; i < count; ++i) {
1087 char *cgroup = consumestr(&serialized, &length);
1088 if (!cgroup)
1089 goto bad_cgroups;
1090 j->cgroups[i] = strdup(cgroup);
1091 if (!j->cgroups[i])
1092 goto bad_cgroups;
1093 ++j->cgroup_count;
1094 }
1095
Elly Jonese1749eb2011-10-07 13:54:59 -04001096 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001097
Dylan Reid605ce7f2016-01-19 19:21:00 -08001098bad_cgroups:
1099 while (j->mounts_head) {
1100 struct mountpoint *m = j->mounts_head;
1101 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001102 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001103 free(m->type);
1104 free(m->dest);
1105 free(m->src);
1106 free(m);
1107 }
1108 for (i = 0; i < j->cgroup_count; ++i)
1109 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001110bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001111 if (j->flags.seccomp_filter && j->filter_len > 0) {
1112 free(j->filter_prog->filter);
1113 free(j->filter_prog);
1114 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001115bad_filter_prog_instrs:
1116 if (j->filter_prog)
1117 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001118bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001119 if (j->alt_syscall_table)
1120 free(j->alt_syscall_table);
1121bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001122 if (j->chrootdir)
1123 free(j->chrootdir);
1124bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001125 if (j->hostname)
1126 free(j->hostname);
1127bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001128 if (j->suppl_gid_list)
1129 free(j->suppl_gid_list);
1130bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001131 if (j->user)
1132 free(j->user);
1133clear_pointers:
1134 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001135 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001136 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001137 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001138 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001139 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001140out:
1141 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001142}
1143
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001144/*
1145 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001146 * @j Minijail these mounts are for
1147 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001148 *
1149 * Returns 0 for success.
1150 */
Dylan Reid648b2202015-10-23 00:50:00 -07001151static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001152{
Dylan Reid648b2202015-10-23 00:50:00 -07001153 int ret;
1154 char *dest;
1155 int remount_ro = 0;
1156
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001157 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001158 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001159 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001160
Dylan Reideec77962016-06-30 19:35:10 -07001161 if (setup_mount_destination(m->src, dest, j->uid, j->gid))
1162 pdie("creating mount target '%s' failed", dest);
1163
Dylan Reid648b2202015-10-23 00:50:00 -07001164 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001165 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1166 * can't both be specified in the original bind mount.
1167 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001168 */
1169 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1170 remount_ro = 1;
1171 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001172 }
Dylan Reid648b2202015-10-23 00:50:00 -07001173
Dylan Reid81e23972016-05-18 14:06:35 -07001174 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001175 if (ret)
1176 pdie("mount: %s -> %s", m->src, dest);
1177
1178 if (remount_ro) {
1179 m->flags |= MS_RDONLY;
1180 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001181 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001182 if (ret)
1183 pdie("bind ro: %s -> %s", m->src, dest);
1184 }
1185
Elly Jones51a5b6c2011-10-12 19:09:26 -04001186 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001187 if (m->next)
1188 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001189 return ret;
1190}
1191
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001192static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001193{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001194 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001195
1196 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001197 return ret;
1198
1199 if (chroot(j->chrootdir))
1200 return -errno;
1201
1202 if (chdir("/"))
1203 return -errno;
1204
1205 return 0;
1206}
1207
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001208static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001209{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001210 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001211
1212 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001213 return ret;
1214
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001215 /*
1216 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001217 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001218 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001219 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001220 if (oldroot < 0)
1221 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001222 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001223 if (newroot < 0)
1224 pdie("failed to open %s for fchdir", j->chrootdir);
1225
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001226 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001227 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001228 * do a self bind mount.
1229 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001230 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1231 pdie("failed to bind mount '%s'", j->chrootdir);
1232 if (chdir(j->chrootdir))
1233 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001234 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001235 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001236
1237 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001238 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001239 * change to the old root and unmount it.
1240 */
1241 if (fchdir(oldroot))
1242 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001243
1244 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001245 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1246 * there could be a shared mount point under |oldroot|. In that case,
1247 * mounts under this shared mount point will be unmounted below, and
1248 * this unmounting will propagate to the original mount namespace
1249 * (because the mount point is shared). To prevent this unexpected
1250 * unmounting, remove these mounts from their peer groups by recursively
1251 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001252 */
1253 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001254 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001255 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001256 if (umount2(".", MNT_DETACH))
1257 pdie("umount(/)");
1258 /* Change back to the new root. */
1259 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001260 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001261 if (close(oldroot))
1262 return -errno;
1263 if (close(newroot))
1264 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001265 if (chroot("/"))
1266 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001267 /* Set correct CWD for getcwd(3). */
1268 if (chdir("/"))
1269 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001270
1271 return 0;
1272}
1273
Martin Pelikánab9eb442017-01-25 11:53:58 +11001274static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001275{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001276 const char fmt[] = "size=%zu,mode=1777";
1277 /* Count for the user storing ULLONG_MAX literally + extra space. */
1278 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1279 int ret;
1280
1281 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1282
1283 if (ret <= 0)
1284 pdie("tmpfs size spec error");
1285 else if ((size_t)ret >= sizeof(data))
1286 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001287 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001288 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001289}
1290
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001291static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001292{
1293 const char *kProcPath = "/proc";
1294 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001295 /*
1296 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001297 * /proc in our namespace, which means using MS_REMOUNT here would
1298 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001299 * namespace (!). Instead, remove their mount from our namespace lazily
1300 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001301 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001302 if (umount2(kProcPath, MNT_DETACH)) {
1303 /*
1304 * If we are in a new user namespace, umount(2) will fail.
1305 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1306 */
1307 if (j->flags.userns) {
1308 info("umount(/proc, MNT_DETACH) failed, "
1309 "this is expected when using user namespaces");
1310 } else {
1311 return -errno;
1312 }
1313 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001314 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001315 return -errno;
1316 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001317}
1318
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001319static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001320{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001321 kill(j->initpid, SIGKILL);
1322 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001323}
1324
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001325static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001326{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001327 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001328 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001329}
1330
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001331static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001332{
1333 size_t i;
1334
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001335 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001336 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001337 kill_child_and_die(j, "failed to add to cgroups");
1338 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001339}
1340
Dylan Reid0f72ef42017-06-06 15:42:49 -07001341static void set_rlimits_or_die(const struct minijail *j)
1342{
1343 size_t i;
1344
1345 for (i = 0; i < j->rlimit_count; ++i) {
1346 struct rlimit limit;
1347 limit.rlim_cur = j->rlimits[i].cur;
1348 limit.rlim_max = j->rlimits[i].max;
1349 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1350 kill_child_and_die(j, "failed to set rlimit");
1351 }
1352}
1353
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001354static void write_ugid_maps_or_die(const struct minijail *j)
1355{
1356 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1357 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001358 if (j->gidmap && j->flags.disable_setgroups) {
1359 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1360 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001361 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001362 if (ret == -ENOENT) {
1363 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1364 warn("could not disable setgroups(2)");
1365 } else
1366 kill_child_and_die(j, "failed to disable setgroups(2)");
1367 }
1368 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001369 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1370 kill_child_and_die(j, "failed to write gid_map");
1371}
1372
1373static void enter_user_namespace(const struct minijail *j)
1374{
1375 if (j->uidmap && setresuid(0, 0, 0))
1376 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1377 if (j->gidmap && setresgid(0, 0, 0))
1378 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1379}
1380
1381static void parent_setup_complete(int *pipe_fds)
1382{
1383 close(pipe_fds[0]);
1384 close(pipe_fds[1]);
1385}
1386
1387/*
1388 * wait_for_parent_setup: Called by the child process to wait for any
1389 * further parent-side setup to complete before continuing.
1390 */
1391static void wait_for_parent_setup(int *pipe_fds)
1392{
1393 char buf;
1394
1395 close(pipe_fds[1]);
1396
1397 /* Wait for parent to complete setup and close the pipe. */
1398 if (read(pipe_fds[0], &buf, 1) != 0)
1399 die("failed to sync with parent");
1400 close(pipe_fds[0]);
1401}
1402
1403static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001404{
Lutz Justen13807cb2017-01-03 17:11:55 +01001405 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1406 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001407 die("can only do one of inherit, keep, or set supplementary "
1408 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001409 }
1410
Lutz Justen13807cb2017-01-03 17:11:55 +01001411 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001412 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001413 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001414 } else if (j->flags.set_suppl_gids) {
1415 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001416 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001417 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001418 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001419 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001420 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001421 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001422 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001423 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001424 }
1425
1426 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001427 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001428
1429 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001430 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001431}
1432
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001433static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1434{
1435 const uint64_t one = 1;
1436 unsigned int i;
1437 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1438 if (keep_mask & (one << i))
1439 continue;
1440 if (prctl(PR_CAPBSET_DROP, i))
1441 pdie("could not drop capability from bounding set");
1442 }
1443}
1444
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001445static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001446{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001447 if (!j->flags.use_caps)
1448 return;
1449
Elly Jonese1749eb2011-10-07 13:54:59 -04001450 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001451 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001452 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001453 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001454 unsigned int i;
1455 if (!caps)
1456 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001457 if (cap_clear(caps))
1458 die("can't clear caps");
1459
1460 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001461 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001462 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001463 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001464 flag[0] = i;
1465 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001466 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001467 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001468 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001469 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001470 die("can't add inheritable cap");
1471 }
1472 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001473 die("can't apply initial cleaned capset");
1474
1475 /*
1476 * Instead of dropping bounding set first, do it here in case
1477 * the caller had a more permissive bounding set which could
1478 * have been used above to raise a capability that wasn't already
1479 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1480 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001481 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001482
1483 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001484 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001485 flag[0] = CAP_SETPCAP;
1486 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1487 die("can't clear effective cap");
1488 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1489 die("can't clear permitted cap");
1490 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1491 die("can't clear inheritable cap");
1492 }
1493
1494 if (cap_set_proc(caps))
1495 die("can't apply final cleaned capset");
1496
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001497 /*
1498 * If ambient capabilities are supported, clear all capabilities first,
1499 * then raise the requested ones.
1500 */
1501 if (j->flags.set_ambient_caps) {
1502 if (!cap_ambient_supported()) {
1503 pdie("ambient capabilities not supported");
1504 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001505 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1506 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001507 pdie("can't clear ambient capabilities");
1508 }
1509
1510 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1511 if (!(j->caps & (one << i)))
1512 continue;
1513
1514 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1515 0) != 0) {
1516 pdie("prctl(PR_CAP_AMBIENT, "
1517 "PR_CAP_AMBIENT_RAISE, %u) failed",
1518 i);
1519 }
1520 }
1521 }
1522
Kees Cook323878a2013-02-05 15:35:24 -08001523 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001524}
1525
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001526static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001527{
1528 /*
1529 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1530 * in the kernel source tree for an explanation of the parameters.
1531 */
1532 if (j->flags.no_new_privs) {
1533 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1534 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1535 }
1536
1537 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001538 * Code running with ASan
1539 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1540 * will make system calls not included in the syscall filter policy,
1541 * which will likely crash the program. Skip setting seccomp filter in
1542 * that case.
1543 * 'running_with_asan()' has no inputs and is completely defined at
1544 * build time, so this cannot be used by an attacker to skip setting
1545 * seccomp filter.
1546 */
1547 if (j->flags.seccomp_filter && running_with_asan()) {
1548 warn("running with ASan, not setting seccomp filter");
1549 return;
1550 }
1551
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001552 if (j->flags.seccomp_filter) {
1553 if (j->flags.seccomp_filter_logging) {
1554 /*
1555 * If logging seccomp filter failures,
1556 * install the SIGSYS handler first.
1557 */
1558 if (install_sigsys_handler())
1559 pdie("failed to install SIGSYS handler");
1560 warn("logging seccomp filter failures");
1561 } else if (j->flags.seccomp_filter_tsync) {
1562 /*
1563 * If setting thread sync,
1564 * reset the SIGSYS signal handler so that
1565 * the entire thread group is killed.
1566 */
1567 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1568 pdie("failed to reset SIGSYS disposition");
1569 info("reset SIGSYS disposition");
1570 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001571 }
1572
1573 /*
1574 * Install the syscall filter.
1575 */
1576 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001577 if (j->flags.seccomp_filter_tsync) {
1578 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1579 SECCOMP_FILTER_FLAG_TSYNC,
1580 j->filter_prog)) {
1581 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001582 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001583 } else {
1584 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1585 j->filter_prog)) {
1586 pdie("prctl(seccomp_filter) failed");
1587 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001588 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001589 }
1590}
1591
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001592static pid_t forward_pid = -1;
1593
1594static void forward_signal(__attribute__((unused)) int nr,
1595 __attribute__((unused)) siginfo_t *siginfo,
1596 __attribute__((unused)) void *void_context)
1597{
1598 if (forward_pid != -1) {
1599 kill(forward_pid, nr);
1600 }
1601}
1602
1603static void install_signal_handlers(void)
1604{
1605 struct sigaction act;
1606
1607 memset(&act, 0, sizeof(act));
1608 act.sa_sigaction = &forward_signal;
1609 act.sa_flags = SA_SIGINFO | SA_RESTART;
1610
1611 /* Handle all signals, except SIGCHLD. */
1612 for (int nr = 1; nr < NSIG; nr++) {
1613 /*
1614 * We don't care if we get EINVAL: that just means that we
1615 * can't handle this signal, so let's skip it and continue.
1616 */
1617 sigaction(nr, &act, NULL);
1618 }
1619 /* Reset SIGCHLD's handler. */
1620 signal(SIGCHLD, SIG_DFL);
1621
1622 /* Handle real-time signals. */
1623 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1624 sigaction(nr, &act, NULL);
1625 }
1626}
1627
Will Drewry6ac91122011-10-21 16:38:58 -05001628void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001629{
Dylan Reidf682d472015-09-17 21:39:07 -07001630 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001631 * If we're dropping caps, get the last valid cap from /proc now,
1632 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001633 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001634 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001635 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001636 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001637
Elly Jonese1749eb2011-10-07 13:54:59 -04001638 if (j->flags.pids)
1639 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001640 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001641
Lutz Justen13807cb2017-01-03 17:11:55 +01001642 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001643 die("cannot inherit supplementary groups without setting a "
1644 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001645
Elly Jonesdd3e8512012-01-23 15:13:38 -05001646 /*
1647 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001648 * so we don't even try. If any of our operations fail, we abort() the
1649 * entire process.
1650 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001651 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001652 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001653
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001654 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001655 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001656 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001657 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001658 * Unless asked not to, remount all filesystems as private.
1659 * If they are shared, new bind mounts will creep out of our
1660 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001661 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1662 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001663 if (!j->flags.skip_remount_private) {
1664 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001665 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1666 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001667 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001668 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001669
Dylan Reidf7942472015-11-18 17:55:26 -08001670 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001671 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001672 }
1673
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001674 if (j->flags.uts) {
1675 if (unshare(CLONE_NEWUTS))
1676 pdie("unshare(CLONE_NEWUTS) failed");
1677
1678 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1679 pdie("sethostname(%s) failed", j->hostname);
1680 }
1681
Dylan Reid1102f5a2015-09-15 11:52:20 -07001682 if (j->flags.enter_net) {
1683 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001684 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001685 } else if (j->flags.net) {
1686 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001687 pdie("unshare(CLONE_NEWNET) failed");
1688 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001689 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001690
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001691 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001692 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001693
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001694 if (j->flags.new_session_keyring) {
1695 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1696 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1697 }
1698
Elly Jones51a5b6c2011-10-12 19:09:26 -04001699 if (j->flags.chroot && enter_chroot(j))
1700 pdie("chroot");
1701
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001702 if (j->flags.pivot_root && enter_pivot_root(j))
1703 pdie("pivot_root");
1704
Martin Pelikánab9eb442017-01-25 11:53:58 +11001705 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001706 pdie("mount_tmp");
1707
Dylan Reid791f5772015-09-14 20:02:42 -07001708 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001709 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001710
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001711 /*
1712 * If we're only dropping capabilities from the bounding set, but not
1713 * from the thread's (permitted|inheritable|effective) sets, do it now.
1714 */
1715 if (j->flags.capbset_drop) {
1716 drop_capbset(j->cap_bset, last_valid_cap);
1717 }
1718
1719 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001720 /*
1721 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001722 * capability to change uids, our attempt to use setuid()
1723 * below will fail. Hang on to root caps across setuid(), then
1724 * lock securebits.
1725 */
1726 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001727 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001728
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001729 if (lock_securebits() < 0) {
1730 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001731 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001732 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001733
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001734 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001735 /*
1736 * If we're setting no_new_privs, we can drop privileges
1737 * before setting seccomp filter. This way filter policies
1738 * don't need to allow privilege-dropping syscalls.
1739 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001740 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001741 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001742 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001743 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001744 /*
1745 * If we're not setting no_new_privs,
1746 * we need to set seccomp filter *before* dropping privileges.
1747 * WARNING: this means that filter policies *must* allow
1748 * setgroups()/setresgid()/setresuid() for dropping root and
1749 * capget()/capset()/prctl() for dropping caps.
1750 */
1751 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001752 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001753 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001754 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001755
Elly Jonesdd3e8512012-01-23 15:13:38 -05001756 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001757 * Select the specified alternate syscall table. The table must not
1758 * block prctl(2) if we're using seccomp as well.
1759 */
1760 if (j->flags.alt_syscall) {
1761 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001762 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001763 }
1764
1765 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001766 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001767 * privilege-dropping syscalls :)
1768 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001769 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001770 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001771 warn("seccomp not supported");
1772 return;
1773 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001774 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001775 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001776}
1777
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001778/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001779static int init_exitstatus = 0;
1780
Will Drewry6ac91122011-10-21 16:38:58 -05001781void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001782{
1783 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001784}
1785
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001786void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001787{
1788 pid_t pid;
1789 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001790 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001791 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001792 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001793 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001794 /*
1795 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001796 * left inside our pid namespace or we get a signal.
1797 */
1798 if (pid == rootpid)
1799 init_exitstatus = status;
1800 }
1801 if (!WIFEXITED(init_exitstatus))
1802 _exit(MINIJAIL_ERR_INIT);
1803 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001804}
1805
Will Drewry6ac91122011-10-21 16:38:58 -05001806int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001807{
1808 size_t sz = 0;
1809 size_t bytes = read(fd, &sz, sizeof(sz));
1810 char *buf;
1811 int r;
1812 if (sizeof(sz) != bytes)
1813 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001814 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001815 return -E2BIG;
1816 buf = malloc(sz);
1817 if (!buf)
1818 return -ENOMEM;
1819 bytes = read(fd, buf, sz);
1820 if (bytes != sz) {
1821 free(buf);
1822 return -EINVAL;
1823 }
1824 r = minijail_unmarshal(j, buf, sz);
1825 free(buf);
1826 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001827}
1828
Will Drewry6ac91122011-10-21 16:38:58 -05001829int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001830{
1831 char *buf;
1832 size_t sz = minijail_size(j);
1833 ssize_t written;
1834 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001835
Elly Jonese1749eb2011-10-07 13:54:59 -04001836 if (!sz)
1837 return -EINVAL;
1838 buf = malloc(sz);
1839 r = minijail_marshal(j, buf, sz);
1840 if (r) {
1841 free(buf);
1842 return r;
1843 }
1844 /* Sends [size][minijail]. */
1845 written = write(fd, &sz, sizeof(sz));
1846 if (written != sizeof(sz)) {
1847 free(buf);
1848 return -EFAULT;
1849 }
1850 written = write(fd, buf, sz);
1851 if (written < 0 || (size_t) written != sz) {
1852 free(buf);
1853 return -EFAULT;
1854 }
1855 free(buf);
1856 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001857}
Elly Jonescd7a9042011-07-22 13:56:51 -04001858
Will Drewry6ac91122011-10-21 16:38:58 -05001859int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001860{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001861#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001862 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001863 return 0;
1864#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001865 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1866 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1867 if (!newenv)
1868 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001869
Elly Jonese1749eb2011-10-07 13:54:59 -04001870 /* Only insert a separating space if we have something to separate... */
1871 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1872 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001873
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001874 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001875 setenv(kLdPreloadEnvVar, newenv, 1);
1876 free(newenv);
1877 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001878#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001879}
1880
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001881static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001882{
1883 int r = pipe(fds);
1884 char fd_buf[11];
1885 if (r)
1886 return r;
1887 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1888 if (r <= 0)
1889 return -EINVAL;
1890 setenv(kFdEnvVar, fd_buf, 1);
1891 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001892}
1893
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001894static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07001895{
1896 const char *kFdPath = "/proc/self/fd";
1897
1898 DIR *d = opendir(kFdPath);
1899 struct dirent *dir_entry;
1900
1901 if (d == NULL)
1902 return -1;
1903 int dir_fd = dirfd(d);
1904 while ((dir_entry = readdir(d)) != NULL) {
1905 size_t i;
1906 char *end;
1907 bool should_close = true;
1908 const int fd = strtol(dir_entry->d_name, &end, 10);
1909
1910 if ((*end) != '\0') {
1911 continue;
1912 }
1913 /*
1914 * We might have set up some pipes that we want to share with
1915 * the parent process, and should not be closed.
1916 */
1917 for (i = 0; i < size; ++i) {
1918 if (fd == inheritable_fds[i]) {
1919 should_close = false;
1920 break;
1921 }
1922 }
1923 /* Also avoid closing the directory fd. */
1924 if (should_close && fd != dir_fd)
1925 close(fd);
1926 }
1927 closedir(d);
1928 return 0;
1929}
1930
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001931int minijail_run_internal(struct minijail *j, const char *filename,
1932 char *const argv[], pid_t *pchild_pid,
1933 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1934 int use_preload);
1935
Will Drewry6ac91122011-10-21 16:38:58 -05001936int API minijail_run(struct minijail *j, const char *filename,
1937 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001938{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001939 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1940 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001941}
1942
1943int API minijail_run_pid(struct minijail *j, const char *filename,
1944 char *const argv[], pid_t *pchild_pid)
1945{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001946 return minijail_run_internal(j, filename, argv, pchild_pid,
1947 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001948}
1949
1950int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001951 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001952{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001953 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1954 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001955}
1956
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001957int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001958 char *const argv[], pid_t *pchild_pid,
1959 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001960{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001961 return minijail_run_internal(j, filename, argv, pchild_pid,
1962 pstdin_fd, pstdout_fd, pstderr_fd, true);
1963}
1964
1965int API minijail_run_no_preload(struct minijail *j, const char *filename,
1966 char *const argv[])
1967{
1968 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1969 false);
1970}
1971
Samuel Tan63187f42015-10-16 13:01:53 -07001972int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001973 const char *filename,
1974 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001975 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001976 int *pstdin_fd, int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001977 int *pstderr_fd)
1978{
Samuel Tan63187f42015-10-16 13:01:53 -07001979 return minijail_run_internal(j, filename, argv, pchild_pid,
1980 pstdin_fd, pstdout_fd, pstderr_fd, false);
1981}
1982
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001983int minijail_run_internal(struct minijail *j, const char *filename,
1984 char *const argv[], pid_t *pchild_pid,
1985 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1986 int use_preload)
1987{
Elly Jonese1749eb2011-10-07 13:54:59 -04001988 char *oldenv, *oldenv_copy = NULL;
1989 pid_t child_pid;
1990 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001991 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001992 int stdout_fds[2];
1993 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08001994 int child_sync_pipe_fds[2];
1995 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04001996 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001997 /* We need to remember this across the minijail_preexec() call. */
1998 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001999 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07002000
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002001 if (use_preload) {
2002 oldenv = getenv(kLdPreloadEnvVar);
2003 if (oldenv) {
2004 oldenv_copy = strdup(oldenv);
2005 if (!oldenv_copy)
2006 return -ENOMEM;
2007 }
2008
2009 if (setup_preload())
2010 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002011 }
Will Drewryf89aef52011-09-16 16:48:57 -05002012
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002013 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002014 if (j->flags.use_caps && j->caps != 0 &&
2015 !j->flags.set_ambient_caps) {
2016 die("non-empty, non-ambient capabilities are not "
2017 "supported without LD_PRELOAD");
2018 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002019 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002020
Elly Jonesdd3e8512012-01-23 15:13:38 -05002021 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002022 * Make the process group ID of this process equal to its PID.
2023 * In the non-interactive case (e.g. when the parent process is started
2024 * from init) this ensures the parent process and the jailed process
2025 * can be killed together.
2026 * When the parent process is started from the console this ensures
2027 * the call to setsid(2) in the jailed process succeeds.
2028 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002029 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2030 * the process is already a process group leader.
2031 */
2032 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2033 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002034 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002035 }
2036 }
2037
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002038 if (use_preload) {
2039 /*
2040 * Before we fork(2) and execve(2) the child process, we need
2041 * to open a pipe(2) to send the minijail configuration over.
2042 */
2043 if (setup_pipe(pipe_fds))
2044 return -EFAULT;
2045 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002046
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002047 /*
2048 * If we want to write to the child process' standard input,
2049 * create the pipe(2) now.
2050 */
2051 if (pstdin_fd) {
2052 if (pipe(stdin_fds))
2053 return -EFAULT;
2054 }
2055
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002056 /*
2057 * If we want to read from the child process' standard output,
2058 * create the pipe(2) now.
2059 */
2060 if (pstdout_fd) {
2061 if (pipe(stdout_fds))
2062 return -EFAULT;
2063 }
2064
2065 /*
2066 * If we want to read from the child process' standard error,
2067 * create the pipe(2) now.
2068 */
2069 if (pstderr_fd) {
2070 if (pipe(stderr_fds))
2071 return -EFAULT;
2072 }
2073
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002074 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002075 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002076 * or if we need to add the child process to cgroups, create the pipe(2)
2077 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002078 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002079 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002080 sync_child = 1;
2081 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002082 return -EFAULT;
2083 }
2084
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002085 /*
2086 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002087 *
2088 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2089 *
2090 * In multithreaded programs, there are a bunch of locks inside libc,
2091 * some of which may be held by other threads at the time that we call
2092 * minijail_run_pid(). If we call fork(), glibc does its level best to
2093 * ensure that we hold all of these locks before it calls clone()
2094 * internally and drop them after clone() returns, but when we call
2095 * sys_clone(2) directly, all that gets bypassed and we end up with a
2096 * child address space where some of libc's important locks are held by
2097 * other threads (which did not get cloned, and hence will never release
2098 * those locks). This is okay so long as we call exec() immediately
2099 * after, but a bunch of seemingly-innocent libc functions like setenv()
2100 * take locks.
2101 *
2102 * Hence, only call sys_clone() if we need to, in order to get at pid
2103 * namespacing. If we follow this path, the child's address space might
2104 * have broken locks; you may only call functions that do not acquire
2105 * any locks.
2106 *
2107 * Unfortunately, fork() acquires every lock it can get its hands on, as
2108 * previously detailed, so this function is highly likely to deadlock
2109 * later on (see "deadlock here") if we're multithreaded.
2110 *
2111 * We might hack around this by having the clone()d child (init of the
2112 * pid namespace) return directly, rather than leaving the clone()d
2113 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002114 * its fork()ed child return in turn), but that process would be
2115 * crippled with its libc locks potentially broken. We might try
2116 * fork()ing in the parent before we clone() to ensure that we own all
2117 * the locks, but then we have to have the forked child hanging around
2118 * consuming resources (and possibly having file descriptors / shared
2119 * memory regions / etc attached). We'd need to keep the child around to
2120 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002121 *
2122 * TODO(ellyjones): figure out if the "forked child hanging around"
2123 * problem is fixable or not. It would be nice if we worked in this
2124 * case.
2125 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002126 if (pid_namespace) {
2127 int clone_flags = CLONE_NEWPID | SIGCHLD;
2128 if (j->flags.userns)
2129 clone_flags |= CLONE_NEWUSER;
2130 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002131 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002132 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002133 }
Elly Jones761b7412012-06-13 15:49:52 -04002134
Elly Jonese1749eb2011-10-07 13:54:59 -04002135 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002136 if (use_preload) {
2137 free(oldenv_copy);
2138 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002139 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002140 }
Will Drewryf89aef52011-09-16 16:48:57 -05002141
Elly Jonese1749eb2011-10-07 13:54:59 -04002142 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002143 if (use_preload) {
2144 /* Restore parent's LD_PRELOAD. */
2145 if (oldenv_copy) {
2146 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2147 free(oldenv_copy);
2148 } else {
2149 unsetenv(kLdPreloadEnvVar);
2150 }
2151 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002152 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002153
Elly Jonese1749eb2011-10-07 13:54:59 -04002154 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002155
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002156 if (j->flags.forward_signals) {
2157 forward_pid = child_pid;
2158 install_signal_handlers();
2159 }
2160
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002161 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002162 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002163
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002164 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002165 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002166
Dylan Reid0f72ef42017-06-06 15:42:49 -07002167 if (j->rlimit_count)
2168 set_rlimits_or_die(j);
2169
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002170 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002171 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002172
2173 if (sync_child)
2174 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002175
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002176 if (use_preload) {
2177 /* Send marshalled minijail. */
2178 close(pipe_fds[0]); /* read endpoint */
2179 ret = minijail_to_fd(j, pipe_fds[1]);
2180 close(pipe_fds[1]); /* write endpoint */
2181 if (ret) {
2182 kill(j->initpid, SIGKILL);
2183 die("failed to send marshalled minijail");
2184 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002185 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002186
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002187 if (pchild_pid)
2188 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002189
2190 /*
2191 * If we want to write to the child process' standard input,
2192 * set up the write end of the pipe.
2193 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002194 if (pstdin_fd)
2195 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002196 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002197
2198 /*
2199 * If we want to read from the child process' standard output,
2200 * set up the read end of the pipe.
2201 */
2202 if (pstdout_fd)
2203 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002204 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002205
2206 /*
2207 * If we want to read from the child process' standard error,
2208 * set up the read end of the pipe.
2209 */
2210 if (pstderr_fd)
2211 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002212 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002213
Elly Jonese1749eb2011-10-07 13:54:59 -04002214 return 0;
2215 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002216 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002217 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002218
Peter Qiu2860c462015-12-16 15:13:06 -08002219 if (j->flags.reset_signal_mask) {
2220 sigset_t signal_mask;
2221 if (sigemptyset(&signal_mask) != 0)
2222 pdie("sigemptyset failed");
2223 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2224 pdie("sigprocmask failed");
2225 }
2226
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002227 if (j->flags.close_open_fds) {
2228 const size_t kMaxInheritableFdsSize = 10;
2229 int inheritable_fds[kMaxInheritableFdsSize];
2230 size_t size = 0;
2231 if (use_preload) {
2232 inheritable_fds[size++] = pipe_fds[0];
2233 inheritable_fds[size++] = pipe_fds[1];
2234 }
2235 if (sync_child) {
2236 inheritable_fds[size++] = child_sync_pipe_fds[0];
2237 inheritable_fds[size++] = child_sync_pipe_fds[1];
2238 }
2239 if (pstdin_fd) {
2240 inheritable_fds[size++] = stdin_fds[0];
2241 inheritable_fds[size++] = stdin_fds[1];
2242 }
2243 if (pstdout_fd) {
2244 inheritable_fds[size++] = stdout_fds[0];
2245 inheritable_fds[size++] = stdout_fds[1];
2246 }
2247 if (pstderr_fd) {
2248 inheritable_fds[size++] = stderr_fds[0];
2249 inheritable_fds[size++] = stderr_fds[1];
2250 }
2251
2252 if (close_open_fds(inheritable_fds, size) < 0)
2253 die("failed to close open file descriptors");
2254 }
2255
Dylan Reidce5b55e2016-01-13 11:04:16 -08002256 if (sync_child)
2257 wait_for_parent_setup(child_sync_pipe_fds);
2258
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002259 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002260 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002261
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002262 /*
2263 * If we want to write to the jailed process' standard input,
2264 * set up the read end of the pipe.
2265 */
2266 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002267 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2268 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002269 die("failed to set up stdin pipe");
2270 }
2271
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002272 /*
2273 * If we want to read from the jailed process' standard output,
2274 * set up the write end of the pipe.
2275 */
2276 if (pstdout_fd) {
2277 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2278 STDOUT_FILENO) < 0)
2279 die("failed to set up stdout pipe");
2280 }
2281
2282 /*
2283 * If we want to read from the jailed process' standard error,
2284 * set up the write end of the pipe.
2285 */
2286 if (pstderr_fd) {
2287 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2288 STDERR_FILENO) < 0)
2289 die("failed to set up stderr pipe");
2290 }
2291
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002292 /*
2293 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2294 * This prevents the jailed process from using the TIOCSTI ioctl
2295 * to push characters into the parent process terminal's input buffer,
2296 * therefore escaping the jail.
2297 */
2298 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2299 isatty(STDERR_FILENO)) {
2300 if (setsid() < 0) {
2301 pdie("setsid() failed");
2302 }
2303 }
2304
Dylan Reid791f5772015-09-14 20:02:42 -07002305 /* If running an init program, let it decide when/how to mount /proc. */
2306 if (pid_namespace && !do_init)
2307 j->flags.remount_proc_ro = 0;
2308
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002309 if (use_preload) {
2310 /* Strip out flags that cannot be inherited across execve(2). */
2311 minijail_preexec(j);
2312 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002313 /*
2314 * If not using LD_PRELOAD, do all jailing before execve(2).
2315 * Note that PID namespaces can only be entered on fork(2),
2316 * so that flag is still cleared.
2317 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002318 j->flags.pids = 0;
2319 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002320 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002321 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002322
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002323 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002324 /*
2325 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002326 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002327 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002328 * a child to actually run the program. If |do_init == 0|, we
2329 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002330 *
2331 * If we're multithreaded, we'll probably deadlock here. See
2332 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002333 */
2334 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002335 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002336 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002337 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002338 /*
2339 * Best effort. Don't bother checking the return value.
2340 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002341 prctl(PR_SET_NAME, "minijail-init");
2342 init(child_pid); /* Never returns. */
2343 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002344 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002345
Elly Jonesdd3e8512012-01-23 15:13:38 -05002346 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002347 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002348 * calling process
2349 * -> execve()-ing process
2350 * If we are:
2351 * calling process
2352 * -> init()-ing process
2353 * -> execve()-ing process
2354 */
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002355 ret = execve(filename, argv, environ);
2356 if (ret == -1) {
2357 pwarn("execve(%s) failed", filename);
2358 }
2359 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002360}
2361
Will Drewry6ac91122011-10-21 16:38:58 -05002362int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002363{
2364 int st;
2365 if (kill(j->initpid, SIGTERM))
2366 return -errno;
2367 if (waitpid(j->initpid, &st, 0) < 0)
2368 return -errno;
2369 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002370}
2371
Will Drewry6ac91122011-10-21 16:38:58 -05002372int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002373{
2374 int st;
2375 if (waitpid(j->initpid, &st, 0) < 0)
2376 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002377
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002378 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002379 int error_status = st;
2380 if (WIFSIGNALED(st)) {
2381 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002382 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002383 j->initpid, signum);
2384 /*
2385 * We return MINIJAIL_ERR_JAIL if the process received
2386 * SIGSYS, which happens when a syscall is blocked by
2387 * seccomp filters.
2388 * If not, we do what bash(1) does:
2389 * $? = 128 + signum
2390 */
2391 if (signum == SIGSYS) {
2392 error_status = MINIJAIL_ERR_JAIL;
2393 } else {
2394 error_status = 128 + signum;
2395 }
2396 }
2397 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002398 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002399
2400 int exit_status = WEXITSTATUS(st);
2401 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002402 info("child process %d exited with status %d",
2403 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002404
2405 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002406}
2407
Will Drewry6ac91122011-10-21 16:38:58 -05002408void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002409{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002410 size_t i;
2411
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002412 if (j->flags.seccomp_filter && j->filter_prog) {
2413 free(j->filter_prog->filter);
2414 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002415 }
Dylan Reid648b2202015-10-23 00:50:00 -07002416 while (j->mounts_head) {
2417 struct mountpoint *m = j->mounts_head;
2418 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002419 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002420 free(m->type);
2421 free(m->dest);
2422 free(m->src);
2423 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002424 }
Dylan Reid648b2202015-10-23 00:50:00 -07002425 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002426 if (j->user)
2427 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002428 if (j->suppl_gid_list)
2429 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002430 if (j->chrootdir)
2431 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002432 if (j->pid_file_path)
2433 free(j->pid_file_path);
2434 if (j->uidmap)
2435 free(j->uidmap);
2436 if (j->gidmap)
2437 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002438 if (j->hostname)
2439 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002440 if (j->alt_syscall_table)
2441 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002442 for (i = 0; i < j->cgroup_count; ++i)
2443 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002444 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002445}