blob: c31af06f8fe219c22cf441a7035ad96c8fe5f059 [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
Luis Hector Chavez1617f632017-08-01 18:32:30 -070080#define MAX_PRESERVED_FDS 10
81
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080082/* Keyctl commands. */
83#define KEYCTL_JOIN_SESSION_KEYRING 1
84
Dylan Reid0f72ef42017-06-06 15:42:49 -070085struct minijail_rlimit {
86 int type;
87 uint32_t cur;
88 uint32_t max;
89};
90
Dylan Reid648b2202015-10-23 00:50:00 -070091struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040092 char *src;
93 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070094 char *type;
Dylan Reid81e23972016-05-18 14:06:35 -070095 char *data;
96 int has_data;
Dylan Reid648b2202015-10-23 00:50:00 -070097 unsigned long flags;
98 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040099};
100
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700101struct hook {
102 minijail_hook_t hook;
103 void *payload;
104 minijail_hook_event_t event;
105 struct hook *next;
106};
107
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700108struct preserved_fd {
109 int parent_fd;
110 int child_fd;
111};
112
Will Drewryf89aef52011-09-16 16:48:57 -0500113struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700114 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700115 * WARNING: if you add a flag here you need to make sure it's
116 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700117 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400118 struct {
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700119 int uid : 1;
120 int gid : 1;
Lutz Justen13807cb2017-01-03 17:11:55 +0100121 int inherit_suppl_gids : 1;
122 int set_suppl_gids : 1;
123 int keep_suppl_gids : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700124 int use_caps : 1;
125 int capbset_drop : 1;
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400126 int set_ambient_caps : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700127 int vfs : 1;
128 int enter_vfs : 1;
129 int skip_remount_private : 1;
130 int pids : 1;
131 int ipc : 1;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400132 int uts : 1;
Luis Hector Chavezfb449ab2016-10-14 09:49:22 -0700133 int net : 1;
134 int enter_net : 1;
135 int ns_cgroups : 1;
136 int userns : 1;
137 int disable_setgroups : 1;
138 int seccomp : 1;
139 int remount_proc_ro : 1;
140 int no_new_privs : 1;
141 int seccomp_filter : 1;
142 int seccomp_filter_tsync : 1;
143 int seccomp_filter_logging : 1;
144 int chroot : 1;
145 int pivot_root : 1;
146 int mount_tmp : 1;
147 int do_init : 1;
148 int pid_file : 1;
149 int cgroups : 1;
150 int alt_syscall : 1;
151 int reset_signal_mask : 1;
152 int close_open_fds : 1;
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800153 int new_session_keyring : 1;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400154 int forward_signals : 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400155 } flags;
156 uid_t uid;
157 gid_t gid;
158 gid_t usergid;
159 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800160 size_t suppl_gid_count;
161 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400162 uint64_t caps;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800163 uint64_t cap_bset;
Elly Jonese1749eb2011-10-07 13:54:59 -0400164 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700165 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700166 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400167 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800168 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800169 char *uidmap;
170 char *gidmap;
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400171 char *hostname;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800172 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800173 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800174 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700175 struct mountpoint *mounts_head;
176 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800177 size_t mounts_count;
Martin Pelikánab9eb442017-01-25 11:53:58 +1100178 size_t tmpfs_size;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800179 char *cgroups[MAX_CGROUPS];
180 size_t cgroup_count;
Dylan Reid0f72ef42017-06-06 15:42:49 -0700181 struct minijail_rlimit rlimits[MAX_RLIMITS];
182 size_t rlimit_count;
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700183 uint64_t securebits_skip_mask;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700184 struct hook *hooks_head;
185 struct hook *hooks_tail;
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700186 struct preserved_fd preserved_fds[MAX_PRESERVED_FDS];
187 size_t preserved_fd_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500188};
189
Luis Hector Chavez64730af2017-09-13 13:18:59 -0700190static void run_hooks_or_die(const struct minijail *j,
191 minijail_hook_event_t event);
192
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700193/*
194 * Strip out flags meant for the parent.
195 * We keep things that are not inherited across execve(2) (e.g. capabilities),
196 * or are easier to set after execve(2) (e.g. seccomp filters).
197 */
198void minijail_preenter(struct minijail *j)
199{
200 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700201 j->flags.enter_vfs = 0;
Shuhei Takahashi3da40312016-03-07 17:37:49 +0900202 j->flags.skip_remount_private = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700203 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700204 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800205 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800206 j->flags.pid_file = 0;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800207 j->flags.cgroups = 0;
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400208 j->flags.forward_signals = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700209}
210
211/*
212 * Strip out flags meant for the child.
213 * We keep things that are inherited across execve(2).
214 */
215void minijail_preexec(struct minijail *j)
216{
217 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700218 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800219 int skip_remount_private = j->flags.skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700220 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800221 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700222 if (j->user)
223 free(j->user);
224 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800225 if (j->suppl_gid_list)
226 free(j->suppl_gid_list);
227 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700228 memset(&j->flags, 0, sizeof(j->flags));
229 /* Now restore anything we meant to keep. */
230 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700231 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800232 j->flags.skip_remount_private = skip_remount_private;
Dylan Reid791f5772015-09-14 20:02:42 -0700233 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800234 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700235 /* Note, |pids| will already have been used before this call. */
236}
237
238/* Minijail API. */
239
Will Drewry6ac91122011-10-21 16:38:58 -0500240struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400241{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400242 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400243}
244
Will Drewry6ac91122011-10-21 16:38:58 -0500245void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400246{
247 if (uid == 0)
248 die("useless change to uid 0");
249 j->uid = uid;
250 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400251}
252
Will Drewry6ac91122011-10-21 16:38:58 -0500253void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400254{
255 if (gid == 0)
256 die("useless change to gid 0");
257 j->gid = gid;
258 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400259}
260
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800261void API minijail_set_supplementary_gids(struct minijail *j, size_t size,
262 const gid_t *list)
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800263{
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800264 size_t i;
265
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -0500266 if (j->flags.inherit_suppl_gids)
267 die("cannot inherit *and* set supplementary groups");
268 if (j->flags.keep_suppl_gids)
269 die("cannot keep *and* set supplementary groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800270
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800271 if (size == 0) {
272 /* Clear supplementary groups. */
273 j->suppl_gid_list = NULL;
274 j->suppl_gid_count = 0;
Lutz Justen13807cb2017-01-03 17:11:55 +0100275 j->flags.set_suppl_gids = 1;
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -0800276 return;
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800277 }
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800278
279 /* Copy the gid_t array. */
280 j->suppl_gid_list = calloc(size, sizeof(gid_t));
281 if (!j->suppl_gid_list) {
Jorge Lucangeli Obesfd5fc562016-01-08 10:29:27 -0800282 die("failed to allocate internal supplementary group array");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800283 }
Jorge Lucangeli Obes06940be2015-12-04 18:09:21 -0800284 for (i = 0; i < size; i++) {
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800285 j->suppl_gid_list[i] = list[i];
286 }
287 j->suppl_gid_count = size;
Lutz Justen13807cb2017-01-03 17:11:55 +0100288 j->flags.set_suppl_gids = 1;
289}
290
291void API minijail_keep_supplementary_gids(struct minijail *j) {
292 j->flags.keep_suppl_gids = 1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800293}
294
Will Drewry6ac91122011-10-21 16:38:58 -0500295int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400296{
297 char *buf = NULL;
298 struct passwd pw;
299 struct passwd *ppw = NULL;
300 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
301 if (sz == -1)
302 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400303
Elly Jonesdd3e8512012-01-23 15:13:38 -0500304 /*
305 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400306 * the maximum needed size of the buffer, so we don't have to search.
307 */
308 buf = malloc(sz);
309 if (!buf)
310 return -ENOMEM;
311 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500312 /*
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800313 * We're safe to free the buffer here. The strings inside |pw| point
314 * inside |buf|, but we don't use any of them; this leaves the pointers
Jorge Lucangeli Obes87bf01d2016-03-08 11:20:03 -0800315 * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3)
316 * succeeded.
Elly Jonesdd3e8512012-01-23 15:13:38 -0500317 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400318 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700319 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400320 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700321 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400322 minijail_change_uid(j, ppw->pw_uid);
323 j->user = strdup(user);
324 if (!j->user)
325 return -ENOMEM;
326 j->usergid = ppw->pw_gid;
327 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400328}
329
Will Drewry6ac91122011-10-21 16:38:58 -0500330int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400331{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700332 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700333 struct group gr;
334 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400335 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
336 if (sz == -1)
337 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400338
Elly Jonesdd3e8512012-01-23 15:13:38 -0500339 /*
340 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400341 * the maximum needed size of the buffer, so we don't have to search.
342 */
343 buf = malloc(sz);
344 if (!buf)
345 return -ENOMEM;
346 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500347 /*
348 * We're safe to free the buffer here. The strings inside gr point
349 * inside buf, but we don't use any of them; this leaves the pointers
350 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
351 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400352 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700353 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400354 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700355 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400356 minijail_change_gid(j, pgr->gr_gid);
357 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400358}
359
Will Drewry6ac91122011-10-21 16:38:58 -0500360void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400361{
362 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400363}
364
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700365void API minijail_no_new_privs(struct minijail *j)
366{
367 j->flags.no_new_privs = 1;
368}
369
Will Drewry6ac91122011-10-21 16:38:58 -0500370void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400371{
372 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500373}
374
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400375void API minijail_set_seccomp_filter_tsync(struct minijail *j)
376{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400377 if (j->filter_len > 0 && j->filter_prog != NULL) {
378 die("minijail_set_seccomp_filter_tsync() must be called "
379 "before minijail_parse_seccomp_filters()");
380 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400381 j->flags.seccomp_filter_tsync = 1;
382}
383
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700384void API minijail_log_seccomp_filter_failures(struct minijail *j)
385{
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400386 if (j->filter_len > 0 && j->filter_prog != NULL) {
387 die("minijail_log_seccomp_filter_failures() must be called "
388 "before minijail_parse_seccomp_filters()");
389 }
390 j->flags.seccomp_filter_logging = 1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700391}
392
Will Drewry6ac91122011-10-21 16:38:58 -0500393void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400394{
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800395 /*
396 * 'minijail_use_caps' configures a runtime-capabilities-only
397 * environment, including a bounding set matching the thread's runtime
398 * (permitted|inheritable|effective) sets.
399 * Therefore, it will override any existing bounding set configurations
400 * since the latter would allow gaining extra runtime capabilities from
401 * file capabilities.
402 */
403 if (j->flags.capbset_drop) {
404 warn("overriding bounding set configuration");
405 j->cap_bset = 0;
406 j->flags.capbset_drop = 0;
407 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400408 j->caps = capmask;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800409 j->flags.use_caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400410}
411
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800412void API minijail_capbset_drop(struct minijail *j, uint64_t capmask)
413{
414 if (j->flags.use_caps) {
415 /*
416 * 'minijail_use_caps' will have already configured a capability
417 * bounding set matching the (permitted|inheritable|effective)
418 * sets. Abort if the user tries to configure a separate
419 * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps'
420 * are mutually exclusive.
421 */
422 die("runtime capabilities already configured, can't drop "
423 "bounding set separately");
424 }
425 j->cap_bset = capmask;
426 j->flags.capbset_drop = 1;
427}
428
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400429void API minijail_set_ambient_caps(struct minijail *j)
430{
431 j->flags.set_ambient_caps = 1;
432}
433
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800434void API minijail_reset_signal_mask(struct minijail *j)
435{
Peter Qiu2860c462015-12-16 15:13:06 -0800436 j->flags.reset_signal_mask = 1;
437}
438
Will Drewry6ac91122011-10-21 16:38:58 -0500439void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400440{
441 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400442}
443
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700444void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
445{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800446 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700447 if (ns_fd < 0) {
448 pdie("failed to open namespace '%s'", ns_path);
449 }
450 j->mountns_fd = ns_fd;
451 j->flags.enter_vfs = 1;
452}
453
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800454void API minijail_new_session_keyring(struct minijail *j)
455{
456 j->flags.new_session_keyring = 1;
457}
458
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700459void API minijail_skip_setting_securebits(struct minijail *j,
460 uint64_t securebits_skip_mask)
461{
462 j->securebits_skip_mask = securebits_skip_mask;
463}
464
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800465void API minijail_skip_remount_private(struct minijail *j)
466{
467 j->flags.skip_remount_private = 1;
468}
469
Will Drewry6ac91122011-10-21 16:38:58 -0500470void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400471{
Elly Jonese58176c2012-01-23 11:46:17 -0500472 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700473 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400474 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800475 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400476}
477
Dylan Reidf7942472015-11-18 17:55:26 -0800478void API minijail_namespace_ipc(struct minijail *j)
479{
480 j->flags.ipc = 1;
481}
482
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400483void API minijail_namespace_uts(struct minijail *j)
484{
485 j->flags.uts = 1;
486}
487
488int API minijail_namespace_set_hostname(struct minijail *j, const char *name)
489{
490 if (j->hostname)
491 return -EINVAL;
492 minijail_namespace_uts(j);
493 j->hostname = strdup(name);
494 if (!j->hostname)
495 return -ENOMEM;
496 return 0;
497}
498
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400499void API minijail_namespace_net(struct minijail *j)
500{
501 j->flags.net = 1;
502}
503
Dylan Reid1102f5a2015-09-15 11:52:20 -0700504void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
505{
Ricky Zhoubce609d2016-03-02 21:47:56 -0800506 int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700507 if (ns_fd < 0) {
508 pdie("failed to open namespace '%s'", ns_path);
509 }
510 j->netns_fd = ns_fd;
511 j->flags.enter_net = 1;
512}
513
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700514void API minijail_namespace_cgroups(struct minijail *j)
515{
516 j->flags.ns_cgroups = 1;
517}
518
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700519void API minijail_close_open_fds(struct minijail *j)
520{
521 j->flags.close_open_fds = 1;
522}
523
Dylan Reid791f5772015-09-14 20:02:42 -0700524void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400525{
526 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700527 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400528}
529
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800530void API minijail_namespace_user(struct minijail *j)
531{
532 j->flags.userns = 1;
533}
534
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400535void API minijail_namespace_user_disable_setgroups(struct minijail *j)
536{
537 j->flags.disable_setgroups = 1;
538}
539
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800540int API minijail_uidmap(struct minijail *j, const char *uidmap)
541{
542 j->uidmap = strdup(uidmap);
543 if (!j->uidmap)
544 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800545 char *ch;
546 for (ch = j->uidmap; *ch; ch++) {
547 if (*ch == ',')
548 *ch = '\n';
549 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800550 return 0;
551}
552
553int API minijail_gidmap(struct minijail *j, const char *gidmap)
554{
555 j->gidmap = strdup(gidmap);
556 if (!j->gidmap)
557 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800558 char *ch;
559 for (ch = j->gidmap; *ch; ch++) {
560 if (*ch == ',')
561 *ch = '\n';
562 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800563 return 0;
564}
565
Will Drewry6ac91122011-10-21 16:38:58 -0500566void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400567{
Lutz Justen13807cb2017-01-03 17:11:55 +0100568 j->flags.inherit_suppl_gids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400569}
570
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800571void API minijail_run_as_init(struct minijail *j)
572{
573 /*
574 * Since the jailed program will become 'init' in the new PID namespace,
575 * Minijail does not need to fork an 'init' process.
576 */
577 j->flags.do_init = 0;
578}
579
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700580int API minijail_enter_chroot(struct minijail *j, const char *dir)
581{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400582 if (j->chrootdir)
583 return -EINVAL;
584 j->chrootdir = strdup(dir);
585 if (!j->chrootdir)
586 return -ENOMEM;
587 j->flags.chroot = 1;
588 return 0;
589}
590
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800591int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
592{
593 if (j->chrootdir)
594 return -EINVAL;
595 j->chrootdir = strdup(dir);
596 if (!j->chrootdir)
597 return -ENOMEM;
598 j->flags.pivot_root = 1;
599 return 0;
600}
601
Dylan Reida14e08d2015-10-22 21:05:29 -0700602char API *minijail_get_original_path(struct minijail *j,
603 const char *path_inside_chroot)
604{
Dylan Reid648b2202015-10-23 00:50:00 -0700605 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700606
Dylan Reid648b2202015-10-23 00:50:00 -0700607 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700608 while (b) {
609 /*
610 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700611 * mount, then the original path is exactly the source of
612 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700613 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700614 * mount source = /some/path/exe, mount dest =
615 * /chroot/path/exe Then when getting the original path of
616 * "/chroot/path/exe", the source of that mount,
617 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700618 */
619 if (!strcmp(b->dest, path_inside_chroot))
620 return strdup(b->src);
621
622 /*
623 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700624 * mount, take the suffix of the chroot path relative to the
625 * mount destination path, and append it to the mount source
626 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700627 */
628 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
629 const char *relative_path =
630 path_inside_chroot + strlen(b->dest);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400631 return path_join(b->src, relative_path);
Dylan Reida14e08d2015-10-22 21:05:29 -0700632 }
633 b = b->next;
634 }
635
636 /* If there is a chroot path, append |path_inside_chroot| to that. */
637 if (j->chrootdir)
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400638 return path_join(j->chrootdir, path_inside_chroot);
Dylan Reida14e08d2015-10-22 21:05:29 -0700639
640 /* No chroot, so the path outside is the same as it is inside. */
641 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700642}
643
Martin Pelikánab9eb442017-01-25 11:53:58 +1100644size_t minijail_get_tmpfs_size(const struct minijail *j)
645{
646 return j->tmpfs_size;
647}
648
Lee Campbell11af0622014-05-22 12:36:04 -0700649void API minijail_mount_tmp(struct minijail *j)
650{
Martin Pelikánab9eb442017-01-25 11:53:58 +1100651 minijail_mount_tmp_size(j, 64 * 1024 * 1024);
652}
653
654void API minijail_mount_tmp_size(struct minijail *j, size_t size)
655{
656 j->tmpfs_size = size;
Lee Campbell11af0622014-05-22 12:36:04 -0700657 j->flags.mount_tmp = 1;
658}
659
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800660int API minijail_write_pid_file(struct minijail *j, const char *path)
661{
662 j->pid_file_path = strdup(path);
663 if (!j->pid_file_path)
664 return -ENOMEM;
665 j->flags.pid_file = 1;
666 return 0;
667}
668
Dylan Reid605ce7f2016-01-19 19:21:00 -0800669int API minijail_add_to_cgroup(struct minijail *j, const char *path)
670{
671 if (j->cgroup_count >= MAX_CGROUPS)
672 return -ENOMEM;
673 j->cgroups[j->cgroup_count] = strdup(path);
674 if (!j->cgroups[j->cgroup_count])
675 return -ENOMEM;
676 j->cgroup_count++;
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -0800677 j->flags.cgroups = 1;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800678 return 0;
679}
680
Dylan Reid0f72ef42017-06-06 15:42:49 -0700681int API minijail_rlimit(struct minijail *j, int type, uint32_t cur,
682 uint32_t max)
683{
684 size_t i;
685
686 if (j->rlimit_count >= MAX_RLIMITS)
687 return -ENOMEM;
688 /* It's an error if the caller sets the same rlimit multiple times. */
689 for (i = 0; i < j->rlimit_count; i++) {
690 if (j->rlimits[i].type == type)
691 return -EEXIST;
692 }
693
694 j->rlimits[j->rlimit_count].type = type;
695 j->rlimits[j->rlimit_count].cur = cur;
696 j->rlimits[j->rlimit_count].max = max;
697 j->rlimit_count++;
698 return 0;
699}
700
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400701int API minijail_forward_signals(struct minijail *j)
702{
703 j->flags.forward_signals = 1;
704 return 0;
705}
706
Dylan Reid81e23972016-05-18 14:06:35 -0700707int API minijail_mount_with_data(struct minijail *j, const char *src,
708 const char *dest, const char *type,
709 unsigned long flags, const char *data)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700710{
Dylan Reid648b2202015-10-23 00:50:00 -0700711 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400712
713 if (*dest != '/')
714 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700715 m = calloc(1, sizeof(*m));
716 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400717 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700718 m->dest = strdup(dest);
719 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400720 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700721 m->src = strdup(src);
722 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400723 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700724 m->type = strdup(type);
725 if (!m->type)
726 goto error;
Dylan Reid81e23972016-05-18 14:06:35 -0700727 if (data) {
728 m->data = strdup(data);
729 if (!m->data)
730 goto error;
731 m->has_data = 1;
732 }
Dylan Reid648b2202015-10-23 00:50:00 -0700733 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400734
Jorge Lucangeli Obes6c755d22016-01-28 15:24:40 -0800735 info("mount %s -> %s type '%s'", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400736
Elly Jonesdd3e8512012-01-23 15:13:38 -0500737 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700738 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400739 * containing vfs namespace.
740 */
741 minijail_namespace_vfs(j);
742
Dylan Reid648b2202015-10-23 00:50:00 -0700743 if (j->mounts_tail)
744 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400745 else
Dylan Reid648b2202015-10-23 00:50:00 -0700746 j->mounts_head = m;
747 j->mounts_tail = m;
748 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400749
750 return 0;
751
752error:
Dylan Reid81e23972016-05-18 14:06:35 -0700753 free(m->type);
Dylan Reid648b2202015-10-23 00:50:00 -0700754 free(m->src);
755 free(m->dest);
756 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400757 return -ENOMEM;
758}
759
Dylan Reid81e23972016-05-18 14:06:35 -0700760int API minijail_mount(struct minijail *j, const char *src, const char *dest,
761 const char *type, unsigned long flags)
762{
763 return minijail_mount_with_data(j, src, dest, type, flags, NULL);
764}
765
Dylan Reid648b2202015-10-23 00:50:00 -0700766int API minijail_bind(struct minijail *j, const char *src, const char *dest,
767 int writeable)
768{
769 unsigned long flags = MS_BIND;
770
771 if (!writeable)
772 flags |= MS_RDONLY;
773
774 return minijail_mount(j, src, dest, "", flags);
775}
776
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700777int API minijail_add_hook(struct minijail *j, minijail_hook_t hook,
778 void *payload, minijail_hook_event_t event)
779{
780 struct hook *c;
781
782 if (hook == NULL)
783 return -EINVAL;
784 if (event >= MINIJAIL_HOOK_EVENT_MAX)
785 return -EINVAL;
786 c = calloc(1, sizeof(*c));
787 if (!c)
788 return -ENOMEM;
789
790 c->hook = hook;
791 c->payload = payload;
792 c->event = event;
793
794 if (j->hooks_tail)
795 j->hooks_tail->next = c;
796 else
797 j->hooks_head = c;
798 j->hooks_tail = c;
799
800 return 0;
801}
802
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700803int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd)
804{
805 if (parent_fd < 0 || child_fd < 0)
806 return -EINVAL;
807 if (j->preserved_fd_count >= MAX_PRESERVED_FDS)
808 return -ENOMEM;
809 j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd;
810 j->preserved_fds[j->preserved_fd_count].child_fd = child_fd;
811 j->preserved_fd_count++;
812 return 0;
813}
814
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400815static void clear_seccomp_options(struct minijail *j)
816{
817 j->flags.seccomp_filter = 0;
818 j->flags.seccomp_filter_tsync = 0;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400819 j->flags.seccomp_filter_logging = 0;
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400820 j->filter_len = 0;
821 j->filter_prog = NULL;
822 j->flags.no_new_privs = 0;
823}
824
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400825static int seccomp_should_parse_filters(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400826{
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400827 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400828 /*
829 * |errno| will be set to EINVAL when seccomp has not been
830 * compiled into the kernel. On certain platforms and kernel
831 * versions this is not a fatal failure. In that case, and only
832 * in that case, disable seccomp and skip loading the filters.
833 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400834 if ((errno == EINVAL) && seccomp_can_softfail()) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400835 warn("not loading seccomp filters, seccomp filter not "
836 "supported");
837 clear_seccomp_options(j);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400838 return 0;
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700839 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400840 /*
841 * If |errno| != EINVAL or seccomp_can_softfail() is false,
842 * we can proceed. Worst case scenario minijail_enter() will
843 * abort() if seccomp fails.
844 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700845 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400846 if (j->flags.seccomp_filter_tsync) {
847 /* Are the seccomp(2) syscall and the TSYNC option supported? */
848 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
849 SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) {
850 int saved_errno = errno;
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400851 if (saved_errno == ENOSYS && seccomp_can_softfail()) {
852 warn("seccomp(2) syscall not supported");
853 clear_seccomp_options(j);
854 return 0;
855 } else if (saved_errno == EINVAL &&
856 seccomp_can_softfail()) {
857 warn(
858 "seccomp filter thread sync not supported");
859 clear_seccomp_options(j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400860 return 0;
861 }
862 /*
863 * Similar logic here. If seccomp_can_softfail() is
864 * false, or |errno| != ENOSYS, or |errno| != EINVAL,
865 * we can proceed. Worst case scenario minijail_enter()
866 * will abort() if seccomp or TSYNC fail.
867 */
868 }
869 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400870 return 1;
871}
872
873static int parse_seccomp_filters(struct minijail *j, FILE *policy_file)
874{
875 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -0400876 int use_ret_trap =
877 j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging;
878 int allow_logging = j->flags.seccomp_filter_logging;
879
880 if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) {
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400881 free(fprog);
882 return -1;
883 }
884
885 j->filter_len = fprog->len;
886 j->filter_prog = fprog;
887 return 0;
888}
889
890void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
891{
892 if (!seccomp_should_parse_filters(j))
893 return;
894
Elly Jonese1749eb2011-10-07 13:54:59 -0400895 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800896 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700897 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400898 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800899
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400900 if (parse_seccomp_filters(j, file) != 0) {
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700901 die("failed to compile seccomp filter BPF program in '%s'",
902 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800903 }
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400904 fclose(file);
905}
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800906
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400907void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
908{
909 if (!seccomp_should_parse_filters(j))
910 return;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800911
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400912 FILE *file = fdopen(fd, "r");
913 if (!file) {
914 pdie("failed to associate stream with fd %d", fd);
915 }
916
917 if (parse_seccomp_filters(j, file) != 0) {
918 die("failed to compile seccomp filter BPF program from fd %d",
919 fd);
920 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400921 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500922}
923
Andrew Brestickereac28942015-11-11 16:04:46 -0800924int API minijail_use_alt_syscall(struct minijail *j, const char *table)
925{
926 j->alt_syscall_table = strdup(table);
927 if (!j->alt_syscall_table)
928 return -ENOMEM;
929 j->flags.alt_syscall = 1;
930 return 0;
931}
932
Will Drewryf89aef52011-09-16 16:48:57 -0500933struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400934 size_t available;
935 size_t total;
936 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500937};
938
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800939void marshal_state_init(struct marshal_state *state, char *buf,
940 size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400941{
942 state->available = available;
943 state->buf = buf;
944 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500945}
946
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800947void marshal_append(struct marshal_state *state, void *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400948{
949 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500950
Elly Jonese1749eb2011-10-07 13:54:59 -0400951 /* Up to |available| will be written. */
952 if (copy_len) {
953 memcpy(state->buf, src, copy_len);
954 state->buf += copy_len;
955 state->available -= copy_len;
956 }
957 /* |total| will contain the expected length. */
958 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500959}
960
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400961void marshal_mount(struct marshal_state *state, const struct mountpoint *m)
Dylan Reid81e23972016-05-18 14:06:35 -0700962{
963 marshal_append(state, m->src, strlen(m->src) + 1);
964 marshal_append(state, m->dest, strlen(m->dest) + 1);
965 marshal_append(state, m->type, strlen(m->type) + 1);
966 marshal_append(state, (char *)&m->has_data, sizeof(m->has_data));
967 if (m->has_data)
968 marshal_append(state, m->data, strlen(m->data) + 1);
969 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
970}
971
Will Drewry6ac91122011-10-21 16:38:58 -0500972void minijail_marshal_helper(struct marshal_state *state,
973 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400974{
Dylan Reid648b2202015-10-23 00:50:00 -0700975 struct mountpoint *m = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -0800976 size_t i;
977
Elly Jonese1749eb2011-10-07 13:54:59 -0400978 marshal_append(state, (char *)j, sizeof(*j));
979 if (j->user)
980 marshal_append(state, j->user, strlen(j->user) + 1);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -0800981 if (j->suppl_gid_list) {
982 marshal_append(state, j->suppl_gid_list,
983 j->suppl_gid_count * sizeof(gid_t));
984 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400985 if (j->chrootdir)
986 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400987 if (j->hostname)
988 marshal_append(state, j->hostname, strlen(j->hostname) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800989 if (j->alt_syscall_table) {
990 marshal_append(state, j->alt_syscall_table,
991 strlen(j->alt_syscall_table) + 1);
992 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800993 if (j->flags.seccomp_filter && j->filter_prog) {
994 struct sock_fprog *fp = j->filter_prog;
995 marshal_append(state, (char *)fp->filter,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800996 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400997 }
Dylan Reid648b2202015-10-23 00:50:00 -0700998 for (m = j->mounts_head; m; m = m->next) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400999 marshal_mount(state, m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001000 }
Dylan Reid605ce7f2016-01-19 19:21:00 -08001001 for (i = 0; i < j->cgroup_count; ++i)
1002 marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1);
Will Drewryf89aef52011-09-16 16:48:57 -05001003}
1004
Will Drewry6ac91122011-10-21 16:38:58 -05001005size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001006{
1007 struct marshal_state state;
1008 marshal_state_init(&state, NULL, 0);
1009 minijail_marshal_helper(&state, j);
1010 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -05001011}
1012
Elly Jonese1749eb2011-10-07 13:54:59 -04001013int minijail_marshal(const struct minijail *j, char *buf, size_t available)
1014{
1015 struct marshal_state state;
1016 marshal_state_init(&state, buf, available);
1017 minijail_marshal_helper(&state, j);
1018 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -05001019}
1020
Elly Jonese1749eb2011-10-07 13:54:59 -04001021int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
1022{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -08001023 size_t i;
1024 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -05001025 int ret = -EINVAL;
1026
Elly Jonese1749eb2011-10-07 13:54:59 -04001027 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -05001028 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -04001029 memcpy((void *)j, serialized, sizeof(*j));
1030 serialized += sizeof(*j);
1031 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -05001032
Will Drewrybee7ba72011-10-21 20:47:01 -05001033 /* Potentially stale pointers not used as signals. */
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04001034 j->pid_file_path = NULL;
1035 j->uidmap = NULL;
1036 j->gidmap = NULL;
Dylan Reid648b2202015-10-23 00:50:00 -07001037 j->mounts_head = NULL;
1038 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001039 j->filter_prog = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001040 j->hooks_head = NULL;
1041 j->hooks_tail = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001042
Elly Jonese1749eb2011-10-07 13:54:59 -04001043 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -04001044 char *user = consumestr(&serialized, &length);
1045 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -05001046 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001047 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001048 if (!j->user)
1049 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -04001050 }
Will Drewryf89aef52011-09-16 16:48:57 -05001051
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001052 if (j->suppl_gid_list) { /* stale pointer */
1053 if (j->suppl_gid_count > NGROUPS_MAX) {
1054 goto bad_gid_list;
1055 }
1056 size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t);
1057 void *gid_list_bytes =
1058 consumebytes(gid_list_size, &serialized, &length);
1059 if (!gid_list_bytes)
1060 goto bad_gid_list;
1061
1062 j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t));
1063 if (!j->suppl_gid_list)
1064 goto bad_gid_list;
1065
1066 memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size);
1067 }
1068
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001069 if (j->chrootdir) { /* stale pointer */
1070 char *chrootdir = consumestr(&serialized, &length);
1071 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -05001072 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001073 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -05001074 if (!j->chrootdir)
1075 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -04001076 }
1077
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001078 if (j->hostname) { /* stale pointer */
1079 char *hostname = consumestr(&serialized, &length);
1080 if (!hostname)
1081 goto bad_hostname;
1082 j->hostname = strdup(hostname);
1083 if (!j->hostname)
1084 goto bad_hostname;
1085 }
1086
Andrew Brestickereac28942015-11-11 16:04:46 -08001087 if (j->alt_syscall_table) { /* stale pointer */
1088 char *alt_syscall_table = consumestr(&serialized, &length);
1089 if (!alt_syscall_table)
1090 goto bad_syscall_table;
1091 j->alt_syscall_table = strdup(alt_syscall_table);
1092 if (!j->alt_syscall_table)
1093 goto bad_syscall_table;
1094 }
1095
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001096 if (j->flags.seccomp_filter && j->filter_len > 0) {
1097 size_t ninstrs = j->filter_len;
1098 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
1099 ninstrs > USHRT_MAX)
1100 goto bad_filters;
1101
1102 size_t program_len = ninstrs * sizeof(struct sock_filter);
1103 void *program = consumebytes(program_len, &serialized, &length);
1104 if (!program)
1105 goto bad_filters;
1106
1107 j->filter_prog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001108 if (!j->filter_prog)
1109 goto bad_filters;
1110
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001111 j->filter_prog->len = ninstrs;
1112 j->filter_prog->filter = malloc(program_len);
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001113 if (!j->filter_prog->filter)
1114 goto bad_filter_prog_instrs;
1115
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001116 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -04001117 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001118
Dylan Reid648b2202015-10-23 00:50:00 -07001119 count = j->mounts_count;
1120 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001121 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -07001122 unsigned long *flags;
Dylan Reid81e23972016-05-18 14:06:35 -07001123 int *has_data;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001124 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -07001125 const char *type;
Dylan Reid81e23972016-05-18 14:06:35 -07001126 const char *data = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001127 const char *src = consumestr(&serialized, &length);
1128 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -07001129 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001130 dest = consumestr(&serialized, &length);
1131 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -07001132 goto bad_mounts;
1133 type = consumestr(&serialized, &length);
1134 if (!type)
1135 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001136 has_data = consumebytes(sizeof(*has_data), &serialized,
1137 &length);
1138 if (!has_data)
1139 goto bad_mounts;
1140 if (*has_data) {
1141 data = consumestr(&serialized, &length);
1142 if (!data)
1143 goto bad_mounts;
1144 }
Dylan Reid648b2202015-10-23 00:50:00 -07001145 flags = consumebytes(sizeof(*flags), &serialized, &length);
1146 if (!flags)
1147 goto bad_mounts;
Dylan Reid81e23972016-05-18 14:06:35 -07001148 if (minijail_mount_with_data(j, src, dest, type, *flags, data))
Dylan Reid648b2202015-10-23 00:50:00 -07001149 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -04001150 }
1151
Dylan Reid605ce7f2016-01-19 19:21:00 -08001152 count = j->cgroup_count;
1153 j->cgroup_count = 0;
1154 for (i = 0; i < count; ++i) {
1155 char *cgroup = consumestr(&serialized, &length);
1156 if (!cgroup)
1157 goto bad_cgroups;
1158 j->cgroups[i] = strdup(cgroup);
1159 if (!j->cgroups[i])
1160 goto bad_cgroups;
1161 ++j->cgroup_count;
1162 }
1163
Elly Jonese1749eb2011-10-07 13:54:59 -04001164 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001165
Dylan Reid605ce7f2016-01-19 19:21:00 -08001166bad_cgroups:
1167 while (j->mounts_head) {
1168 struct mountpoint *m = j->mounts_head;
1169 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07001170 free(m->data);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001171 free(m->type);
1172 free(m->dest);
1173 free(m->src);
1174 free(m);
1175 }
1176 for (i = 0; i < j->cgroup_count; ++i)
1177 free(j->cgroups[i]);
Dylan Reid648b2202015-10-23 00:50:00 -07001178bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001179 if (j->flags.seccomp_filter && j->filter_len > 0) {
1180 free(j->filter_prog->filter);
1181 free(j->filter_prog);
1182 }
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001183bad_filter_prog_instrs:
1184 if (j->filter_prog)
1185 free(j->filter_prog);
Will Drewrybee7ba72011-10-21 20:47:01 -05001186bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -08001187 if (j->alt_syscall_table)
1188 free(j->alt_syscall_table);
1189bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -05001190 if (j->chrootdir)
1191 free(j->chrootdir);
1192bad_chrootdir:
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001193 if (j->hostname)
1194 free(j->hostname);
1195bad_hostname:
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001196 if (j->suppl_gid_list)
1197 free(j->suppl_gid_list);
1198bad_gid_list:
Will Drewrybee7ba72011-10-21 20:47:01 -05001199 if (j->user)
1200 free(j->user);
1201clear_pointers:
1202 j->user = NULL;
Jorge Lucangeli Obesde02a5b2015-12-11 15:28:52 -08001203 j->suppl_gid_list = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -05001204 j->chrootdir = NULL;
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001205 j->hostname = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -08001206 j->alt_syscall_table = NULL;
Dylan Reid605ce7f2016-01-19 19:21:00 -08001207 j->cgroup_count = 0;
Will Drewrybee7ba72011-10-21 20:47:01 -05001208out:
1209 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -05001210}
1211
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001212/*
1213 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -07001214 * @j Minijail these mounts are for
1215 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -04001216 *
1217 * Returns 0 for success.
1218 */
Dylan Reid648b2202015-10-23 00:50:00 -07001219static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001220{
Dylan Reid648b2202015-10-23 00:50:00 -07001221 int ret;
1222 char *dest;
1223 int remount_ro = 0;
1224
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001225 /* |dest| has a leading "/". */
Dylan Reid648b2202015-10-23 00:50:00 -07001226 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -04001227 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -07001228
Mike Frysingereaab4202017-08-14 14:57:21 -04001229 if (setup_mount_destination(m->src, dest, j->uid, j->gid,
1230 (m->flags & MS_BIND)))
Dylan Reideec77962016-06-30 19:35:10 -07001231 pdie("creating mount target '%s' failed", dest);
1232
Dylan Reid648b2202015-10-23 00:50:00 -07001233 /*
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -08001234 * R/O bind mounts have to be remounted since 'bind' and 'ro'
1235 * can't both be specified in the original bind mount.
1236 * Remount R/O after the initial mount.
Dylan Reid648b2202015-10-23 00:50:00 -07001237 */
1238 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
1239 remount_ro = 1;
1240 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -05001241 }
Dylan Reid648b2202015-10-23 00:50:00 -07001242
Dylan Reid81e23972016-05-18 14:06:35 -07001243 ret = mount(m->src, dest, m->type, m->flags, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001244 if (ret)
1245 pdie("mount: %s -> %s", m->src, dest);
1246
1247 if (remount_ro) {
1248 m->flags |= MS_RDONLY;
1249 ret = mount(m->src, dest, NULL,
Dylan Reid81e23972016-05-18 14:06:35 -07001250 m->flags | MS_REMOUNT, m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07001251 if (ret)
1252 pdie("bind ro: %s -> %s", m->src, dest);
1253 }
1254
Elly Jones51a5b6c2011-10-12 19:09:26 -04001255 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -07001256 if (m->next)
1257 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001258 return ret;
1259}
1260
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001261static int enter_chroot(const struct minijail *j)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -07001262{
Elly Jones51a5b6c2011-10-12 19:09:26 -04001263 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -07001264
1265 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -04001266 return ret;
1267
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001268 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1269
Elly Jones51a5b6c2011-10-12 19:09:26 -04001270 if (chroot(j->chrootdir))
1271 return -errno;
1272
1273 if (chdir("/"))
1274 return -errno;
1275
1276 return 0;
1277}
1278
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001279static int enter_pivot_root(const struct minijail *j)
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001280{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001281 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -07001282
1283 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001284 return ret;
1285
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001286 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT);
1287
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001288 /*
1289 * Keep the fd for both old and new root.
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001290 * It will be used in fchdir(2) later.
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001291 */
Ricky Zhoubce609d2016-03-02 21:47:56 -08001292 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001293 if (oldroot < 0)
1294 pdie("failed to open / for fchdir");
Ricky Zhoubce609d2016-03-02 21:47:56 -08001295 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001296 if (newroot < 0)
1297 pdie("failed to open %s for fchdir", j->chrootdir);
1298
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001299 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001300 * To ensure j->chrootdir is the root of a filesystem,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001301 * do a self bind mount.
1302 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001303 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
1304 pdie("failed to bind mount '%s'", j->chrootdir);
1305 if (chdir(j->chrootdir))
1306 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001307 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001308 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001309
1310 /*
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001311 * Now the old root is mounted on top of the new root. Use fchdir(2) to
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001312 * change to the old root and unmount it.
1313 */
1314 if (fchdir(oldroot))
1315 pdie("failed to fchdir to old /");
Hidehiko Abe097b7192016-03-16 18:00:36 +09001316
1317 /*
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001318 * If j->flags.skip_remount_private was enabled for minijail_enter(),
1319 * there could be a shared mount point under |oldroot|. In that case,
1320 * mounts under this shared mount point will be unmounted below, and
1321 * this unmounting will propagate to the original mount namespace
1322 * (because the mount point is shared). To prevent this unexpected
1323 * unmounting, remove these mounts from their peer groups by recursively
1324 * remounting them as MS_PRIVATE.
Hidehiko Abe097b7192016-03-16 18:00:36 +09001325 */
1326 if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes6b0de9b2016-03-16 22:41:34 -07001327 pdie("failed to mount(/, private) before umount(/)");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001328 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +08001329 if (umount2(".", MNT_DETACH))
1330 pdie("umount(/)");
1331 /* Change back to the new root. */
1332 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001333 return -errno;
Ricky Zhoubce609d2016-03-02 21:47:56 -08001334 if (close(oldroot))
1335 return -errno;
1336 if (close(newroot))
1337 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001338 if (chroot("/"))
1339 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -07001340 /* Set correct CWD for getcwd(3). */
1341 if (chdir("/"))
1342 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001343
1344 return 0;
1345}
1346
Martin Pelikánab9eb442017-01-25 11:53:58 +11001347static int mount_tmp(const struct minijail *j)
Lee Campbell11af0622014-05-22 12:36:04 -07001348{
Martin Pelikánab9eb442017-01-25 11:53:58 +11001349 const char fmt[] = "size=%zu,mode=1777";
1350 /* Count for the user storing ULLONG_MAX literally + extra space. */
1351 char data[sizeof(fmt) + sizeof("18446744073709551615ULL")];
1352 int ret;
1353
1354 ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size);
1355
1356 if (ret <= 0)
1357 pdie("tmpfs size spec error");
1358 else if ((size_t)ret >= sizeof(data))
1359 pdie("tmpfs size spec too large");
Mike Frysingerb91d4042017-01-13 19:03:34 -05001360 return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID,
Martin Pelikánab9eb442017-01-25 11:53:58 +11001361 data);
Lee Campbell11af0622014-05-22 12:36:04 -07001362}
1363
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001364static int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001365{
1366 const char *kProcPath = "/proc";
1367 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -05001368 /*
1369 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -04001370 * /proc in our namespace, which means using MS_REMOUNT here would
1371 * mutate our parent's mount as well, even though we're in a VFS
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001372 * namespace (!). Instead, remove their mount from our namespace lazily
1373 * (MNT_DETACH) and make our own.
Elly Jonese1749eb2011-10-07 13:54:59 -04001374 */
Jorge Lucangeli Obesdf7fab12016-06-01 17:15:31 -07001375 if (umount2(kProcPath, MNT_DETACH)) {
1376 /*
1377 * If we are in a new user namespace, umount(2) will fail.
1378 * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html
1379 */
1380 if (j->flags.userns) {
1381 info("umount(/proc, MNT_DETACH) failed, "
1382 "this is expected when using user namespaces");
1383 } else {
1384 return -errno;
1385 }
1386 }
Mike Frysinger3ba81572017-01-17 23:33:28 -05001387 if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
Elly Jonese1749eb2011-10-07 13:54:59 -04001388 return -errno;
1389 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -04001390}
1391
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001392static void kill_child_and_die(const struct minijail *j, const char *msg)
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001393{
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001394 kill(j->initpid, SIGKILL);
1395 die("%s", msg);
Dylan Reid605ce7f2016-01-19 19:21:00 -08001396}
1397
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001398static void write_pid_file_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001399{
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001400 if (write_pid_to_path(j->initpid, j->pid_file_path))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001401 kill_child_and_die(j, "failed to write pid file");
Dylan Reid605ce7f2016-01-19 19:21:00 -08001402}
1403
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001404static void add_to_cgroups_or_die(const struct minijail *j)
Dylan Reid605ce7f2016-01-19 19:21:00 -08001405{
1406 size_t i;
1407
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001408 for (i = 0; i < j->cgroup_count; ++i) {
Keshav Santhanamdb6dab42016-08-10 16:33:34 -07001409 if (write_pid_to_path(j->initpid, j->cgroups[i]))
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001410 kill_child_and_die(j, "failed to add to cgroups");
1411 }
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001412}
1413
Dylan Reid0f72ef42017-06-06 15:42:49 -07001414static void set_rlimits_or_die(const struct minijail *j)
1415{
1416 size_t i;
1417
1418 for (i = 0; i < j->rlimit_count; ++i) {
1419 struct rlimit limit;
1420 limit.rlim_cur = j->rlimits[i].cur;
1421 limit.rlim_max = j->rlimits[i].max;
1422 if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL))
1423 kill_child_and_die(j, "failed to set rlimit");
1424 }
1425}
1426
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001427static void write_ugid_maps_or_die(const struct minijail *j)
1428{
1429 if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0)
1430 kill_child_and_die(j, "failed to write uid_map");
Mike Frysinger6b190c02017-01-04 17:18:42 -05001431 if (j->gidmap && j->flags.disable_setgroups) {
1432 /* Older kernels might not have the /proc/<pid>/setgroups files. */
1433 int ret = write_proc_file(j->initpid, "deny", "setgroups");
Mike Frysingereea841b2017-01-13 18:11:57 -05001434 if (ret != 0) {
Mike Frysinger6b190c02017-01-04 17:18:42 -05001435 if (ret == -ENOENT) {
1436 /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */
1437 warn("could not disable setgroups(2)");
1438 } else
1439 kill_child_and_die(j, "failed to disable setgroups(2)");
1440 }
1441 }
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001442 if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0)
1443 kill_child_and_die(j, "failed to write gid_map");
1444}
1445
1446static void enter_user_namespace(const struct minijail *j)
1447{
1448 if (j->uidmap && setresuid(0, 0, 0))
1449 pdie("user_namespaces: setresuid(0, 0, 0) failed");
1450 if (j->gidmap && setresgid(0, 0, 0))
1451 pdie("user_namespaces: setresgid(0, 0, 0) failed");
1452}
1453
1454static void parent_setup_complete(int *pipe_fds)
1455{
1456 close(pipe_fds[0]);
1457 close(pipe_fds[1]);
1458}
1459
1460/*
1461 * wait_for_parent_setup: Called by the child process to wait for any
1462 * further parent-side setup to complete before continuing.
1463 */
1464static void wait_for_parent_setup(int *pipe_fds)
1465{
1466 char buf;
1467
1468 close(pipe_fds[1]);
1469
1470 /* Wait for parent to complete setup and close the pipe. */
1471 if (read(pipe_fds[0], &buf, 1) != 0)
1472 die("failed to sync with parent");
1473 close(pipe_fds[0]);
1474}
1475
1476static void drop_ugid(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001477{
Lutz Justen13807cb2017-01-03 17:11:55 +01001478 if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids +
1479 j->flags.set_suppl_gids > 1) {
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001480 die("can only do one of inherit, keep, or set supplementary "
1481 "groups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001482 }
1483
Lutz Justen13807cb2017-01-03 17:11:55 +01001484 if (j->flags.inherit_suppl_gids) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001485 if (initgroups(j->user, j->usergid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001486 pdie("initgroups(%s, %d) failed", j->user, j->usergid);
Lutz Justen13807cb2017-01-03 17:11:55 +01001487 } else if (j->flags.set_suppl_gids) {
1488 if (setgroups(j->suppl_gid_count, j->suppl_gid_list))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001489 pdie("setgroups(suppl_gids) failed");
Lutz Justen13807cb2017-01-03 17:11:55 +01001490 } else if (!j->flags.keep_suppl_gids) {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001491 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001492 * Only attempt to clear supplementary groups if we are changing
Lutz Justen13807cb2017-01-03 17:11:55 +01001493 * users or groups.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001494 */
Jorge Lucangeli Obes24499562016-12-01 11:59:27 -05001495 if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001496 pdie("setgroups(0, NULL) failed");
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001497 }
1498
1499 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001500 pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001501
1502 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001503 pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001504}
1505
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001506static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap)
1507{
1508 const uint64_t one = 1;
1509 unsigned int i;
1510 for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) {
1511 if (keep_mask & (one << i))
1512 continue;
1513 if (prctl(PR_CAPBSET_DROP, i))
1514 pdie("could not drop capability from bounding set");
1515 }
1516}
1517
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001518static void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001519{
Jorge Lucangeli Obes7ea269e2016-02-26 22:07:09 -08001520 if (!j->flags.use_caps)
1521 return;
1522
Elly Jonese1749eb2011-10-07 13:54:59 -04001523 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001524 cap_value_t flag[1];
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001525 const size_t ncaps = sizeof(j->caps) * 8;
Kees Cooke5609ac2013-02-06 14:12:41 -08001526 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001527 unsigned int i;
1528 if (!caps)
1529 die("can't get process caps");
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001530 if (cap_clear(caps))
1531 die("can't clear caps");
1532
1533 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001534 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001535 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001536 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001537 flag[0] = i;
1538 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001539 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001540 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001541 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001542 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001543 die("can't add inheritable cap");
1544 }
1545 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001546 die("can't apply initial cleaned capset");
1547
1548 /*
1549 * Instead of dropping bounding set first, do it here in case
1550 * the caller had a more permissive bounding set which could
1551 * have been used above to raise a capability that wasn't already
1552 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1553 */
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001554 drop_capbset(j->caps, last_valid_cap);
Kees Cook323878a2013-02-05 15:35:24 -08001555
1556 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001557 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001558 flag[0] = CAP_SETPCAP;
1559 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1560 die("can't clear effective cap");
1561 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1562 die("can't clear permitted cap");
1563 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1564 die("can't clear inheritable cap");
1565 }
1566
1567 if (cap_set_proc(caps))
1568 die("can't apply final cleaned capset");
1569
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001570 /*
1571 * If ambient capabilities are supported, clear all capabilities first,
1572 * then raise the requested ones.
1573 */
1574 if (j->flags.set_ambient_caps) {
1575 if (!cap_ambient_supported()) {
1576 pdie("ambient capabilities not supported");
1577 }
Jorge Lucangeli Obesf6058c32017-04-26 10:26:59 -04001578 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) !=
1579 0) {
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -04001580 pdie("can't clear ambient capabilities");
1581 }
1582
1583 for (i = 0; i < ncaps && i <= last_valid_cap; ++i) {
1584 if (!(j->caps & (one << i)))
1585 continue;
1586
1587 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0,
1588 0) != 0) {
1589 pdie("prctl(PR_CAP_AMBIENT, "
1590 "PR_CAP_AMBIENT_RAISE, %u) failed",
1591 i);
1592 }
1593 }
1594 }
1595
Kees Cook323878a2013-02-05 15:35:24 -08001596 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001597}
1598
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04001599static void set_seccomp_filter(const struct minijail *j)
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001600{
1601 /*
1602 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1603 * in the kernel source tree for an explanation of the parameters.
1604 */
1605 if (j->flags.no_new_privs) {
1606 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1607 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1608 }
1609
1610 /*
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -07001611 * Code running with ASan
1612 * (https://github.com/google/sanitizers/wiki/AddressSanitizer)
1613 * will make system calls not included in the syscall filter policy,
1614 * which will likely crash the program. Skip setting seccomp filter in
1615 * that case.
1616 * 'running_with_asan()' has no inputs and is completely defined at
1617 * build time, so this cannot be used by an attacker to skip setting
1618 * seccomp filter.
1619 */
1620 if (j->flags.seccomp_filter && running_with_asan()) {
1621 warn("running with ASan, not setting seccomp filter");
1622 return;
1623 }
1624
Jorge Lucangeli Obes713f6fb2016-10-03 13:03:25 -04001625 if (j->flags.seccomp_filter) {
1626 if (j->flags.seccomp_filter_logging) {
1627 /*
1628 * If logging seccomp filter failures,
1629 * install the SIGSYS handler first.
1630 */
1631 if (install_sigsys_handler())
1632 pdie("failed to install SIGSYS handler");
1633 warn("logging seccomp filter failures");
1634 } else if (j->flags.seccomp_filter_tsync) {
1635 /*
1636 * If setting thread sync,
1637 * reset the SIGSYS signal handler so that
1638 * the entire thread group is killed.
1639 */
1640 if (signal(SIGSYS, SIG_DFL) == SIG_ERR)
1641 pdie("failed to reset SIGSYS disposition");
1642 info("reset SIGSYS disposition");
1643 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001644 }
1645
1646 /*
1647 * Install the syscall filter.
1648 */
1649 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001650 if (j->flags.seccomp_filter_tsync) {
1651 if (sys_seccomp(SECCOMP_SET_MODE_FILTER,
1652 SECCOMP_FILTER_FLAG_TSYNC,
1653 j->filter_prog)) {
1654 pdie("seccomp(tsync) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001655 }
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04001656 } else {
1657 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1658 j->filter_prog)) {
1659 pdie("prctl(seccomp_filter) failed");
1660 }
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001661 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001662 }
1663}
1664
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04001665static pid_t forward_pid = -1;
1666
1667static void forward_signal(__attribute__((unused)) int nr,
1668 __attribute__((unused)) siginfo_t *siginfo,
1669 __attribute__((unused)) void *void_context)
1670{
1671 if (forward_pid != -1) {
1672 kill(forward_pid, nr);
1673 }
1674}
1675
1676static void install_signal_handlers(void)
1677{
1678 struct sigaction act;
1679
1680 memset(&act, 0, sizeof(act));
1681 act.sa_sigaction = &forward_signal;
1682 act.sa_flags = SA_SIGINFO | SA_RESTART;
1683
1684 /* Handle all signals, except SIGCHLD. */
1685 for (int nr = 1; nr < NSIG; nr++) {
1686 /*
1687 * We don't care if we get EINVAL: that just means that we
1688 * can't handle this signal, so let's skip it and continue.
1689 */
1690 sigaction(nr, &act, NULL);
1691 }
1692 /* Reset SIGCHLD's handler. */
1693 signal(SIGCHLD, SIG_DFL);
1694
1695 /* Handle real-time signals. */
1696 for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) {
1697 sigaction(nr, &act, NULL);
1698 }
1699}
1700
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001701static const char *lookup_hook_name(minijail_hook_event_t event)
1702{
1703 switch (event) {
1704 case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS:
1705 return "pre-drop-caps";
1706 case MINIJAIL_HOOK_EVENT_PRE_EXECVE:
1707 return "pre-execve";
Luis Hector Chavez64730af2017-09-13 13:18:59 -07001708 case MINIJAIL_HOOK_EVENT_PRE_CHROOT:
1709 return "pre-chroot";
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001710 case MINIJAIL_HOOK_EVENT_MAX:
1711 /*
1712 * Adding this in favor of a default case to force the
1713 * compiler to error out if a new enum value is added.
1714 */
1715 break;
1716 }
1717 return "unknown";
1718}
1719
1720static void run_hooks_or_die(const struct minijail *j,
1721 minijail_hook_event_t event)
1722{
1723 int rc;
1724 int hook_index = 0;
1725 for (struct hook *c = j->hooks_head; c; c = c->next) {
1726 if (c->event != event)
1727 continue;
1728 rc = c->hook(c->payload);
1729 if (rc != 0) {
1730 errno = -rc;
1731 pdie("%s hook (index %d) failed",
1732 lookup_hook_name(event), hook_index);
1733 }
1734 /* Only increase the index within the same hook event type. */
1735 ++hook_index;
1736 }
1737}
1738
Will Drewry6ac91122011-10-21 16:38:58 -05001739void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001740{
Dylan Reidf682d472015-09-17 21:39:07 -07001741 /*
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001742 * If we're dropping caps, get the last valid cap from /proc now,
1743 * since /proc can be unmounted before drop_caps() is called.
Dylan Reidf682d472015-09-17 21:39:07 -07001744 */
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001745 unsigned int last_valid_cap = 0;
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001746 if (j->flags.capbset_drop || j->flags.use_caps)
Jorge Lucangeli Obes43e29b32015-12-08 21:07:14 -08001747 last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001748
Elly Jonese1749eb2011-10-07 13:54:59 -04001749 if (j->flags.pids)
1750 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001751 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001752
Lutz Justen13807cb2017-01-03 17:11:55 +01001753 if (j->flags.inherit_suppl_gids && !j->user)
Jorge Lucangeli Obes34543192017-01-11 16:07:57 -05001754 die("cannot inherit supplementary groups without setting a "
1755 "username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001756
Elly Jonesdd3e8512012-01-23 15:13:38 -05001757 /*
1758 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001759 * so we don't even try. If any of our operations fail, we abort() the
1760 * entire process.
1761 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001762 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001763 pdie("setns(CLONE_NEWNS) failed");
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001764
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001765 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001766 if (unshare(CLONE_NEWNS))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001767 pdie("unshare(CLONE_NEWNS) failed");
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001768 /*
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001769 * Unless asked not to, remount all filesystems as private.
1770 * If they are shared, new bind mounts will creep out of our
1771 * namespace.
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001772 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1773 */
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001774 if (!j->flags.skip_remount_private) {
1775 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001776 pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE,"
1777 " NULL) failed");
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08001778 }
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001779 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001780
Dylan Reidf7942472015-11-18 17:55:26 -08001781 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001782 pdie("unshare(CLONE_NEWIPC) failed");
Dylan Reidf7942472015-11-18 17:55:26 -08001783 }
1784
Mike Frysingerb9a7b162017-05-30 15:25:49 -04001785 if (j->flags.uts) {
1786 if (unshare(CLONE_NEWUTS))
1787 pdie("unshare(CLONE_NEWUTS) failed");
1788
1789 if (j->hostname && sethostname(j->hostname, strlen(j->hostname)))
1790 pdie("sethostname(%s) failed", j->hostname);
1791 }
1792
Dylan Reid1102f5a2015-09-15 11:52:20 -07001793 if (j->flags.enter_net) {
1794 if (setns(j->netns_fd, CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001795 pdie("setns(CLONE_NEWNET) failed");
Mike Frysinger7559dfe2016-11-15 18:58:39 -05001796 } else if (j->flags.net) {
1797 if (unshare(CLONE_NEWNET))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001798 pdie("unshare(CLONE_NEWNET) failed");
1799 config_net_loopback();
Dylan Reid1102f5a2015-09-15 11:52:20 -07001800 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001801
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001802 if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001803 pdie("unshare(CLONE_NEWCGROUP) failed");
Dylan Reid4cbc2a52016-06-17 19:06:07 -07001804
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -08001805 if (j->flags.new_session_keyring) {
1806 if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0)
1807 pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed");
1808 }
1809
Elly Jones51a5b6c2011-10-12 19:09:26 -04001810 if (j->flags.chroot && enter_chroot(j))
1811 pdie("chroot");
1812
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001813 if (j->flags.pivot_root && enter_pivot_root(j))
1814 pdie("pivot_root");
1815
Martin Pelikánab9eb442017-01-25 11:53:58 +11001816 if (j->flags.mount_tmp && mount_tmp(j))
Lee Campbell11af0622014-05-22 12:36:04 -07001817 pdie("mount_tmp");
1818
Dylan Reid791f5772015-09-14 20:02:42 -07001819 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001820 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001821
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07001822 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS);
1823
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -08001824 /*
1825 * If we're only dropping capabilities from the bounding set, but not
1826 * from the thread's (permitted|inheritable|effective) sets, do it now.
1827 */
1828 if (j->flags.capbset_drop) {
1829 drop_capbset(j->cap_bset, last_valid_cap);
1830 }
1831
1832 if (j->flags.use_caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001833 /*
1834 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001835 * capability to change uids, our attempt to use setuid()
1836 * below will fail. Hang on to root caps across setuid(), then
1837 * lock securebits.
1838 */
1839 if (prctl(PR_SET_KEEPCAPS, 1))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001840 pdie("prctl(PR_SET_KEEPCAPS) failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001841
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -07001842 if (lock_securebits(j->securebits_skip_mask) < 0) {
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001843 pdie("locking securebits failed");
Jorge Lucangeli Obesf783b522016-03-14 14:34:10 -07001844 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001845 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001846
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001847 if (j->flags.no_new_privs) {
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001848 /*
1849 * If we're setting no_new_privs, we can drop privileges
1850 * before setting seccomp filter. This way filter policies
1851 * don't need to allow privilege-dropping syscalls.
1852 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001853 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001854 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001855 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001856 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001857 /*
1858 * If we're not setting no_new_privs,
1859 * we need to set seccomp filter *before* dropping privileges.
1860 * WARNING: this means that filter policies *must* allow
1861 * setgroups()/setresgid()/setresuid() for dropping root and
1862 * capget()/capset()/prctl() for dropping caps.
1863 */
1864 set_seccomp_filter(j);
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001865 drop_ugid(j);
Jorge Lucangeli Obesd8c82052016-02-25 16:00:32 -08001866 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001867 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001868
Elly Jonesdd3e8512012-01-23 15:13:38 -05001869 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001870 * Select the specified alternate syscall table. The table must not
1871 * block prctl(2) if we're using seccomp as well.
1872 */
1873 if (j->flags.alt_syscall) {
1874 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001875 pdie("prctl(PR_ALT_SYSCALL) failed");
Andrew Brestickereac28942015-11-11 16:04:46 -08001876 }
1877
1878 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001879 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001880 * privilege-dropping syscalls :)
1881 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001882 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -04001883 if ((errno == EINVAL) && seccomp_can_softfail()) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001884 warn("seccomp not supported");
1885 return;
1886 }
Jorge Lucangeli Obes457a5e32016-11-23 15:18:56 -05001887 pdie("prctl(PR_SET_SECCOMP) failed");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001888 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001889}
1890
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001891/* TODO(wad): will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001892static int init_exitstatus = 0;
1893
Will Drewry6ac91122011-10-21 16:38:58 -05001894void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001895{
1896 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001897}
1898
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04001899void init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001900{
1901 pid_t pid;
1902 int status;
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001903 /* So that we exit with the right status. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001904 signal(SIGTERM, init_term);
Jorge Lucangeli Obesdb0bc672016-08-03 10:45:21 -04001905 /* TODO(wad): self jail with seccomp filters here. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001906 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001907 /*
1908 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001909 * left inside our pid namespace or we get a signal.
1910 */
1911 if (pid == rootpid)
1912 init_exitstatus = status;
1913 }
1914 if (!WIFEXITED(init_exitstatus))
1915 _exit(MINIJAIL_ERR_INIT);
1916 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001917}
1918
Will Drewry6ac91122011-10-21 16:38:58 -05001919int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001920{
1921 size_t sz = 0;
1922 size_t bytes = read(fd, &sz, sizeof(sz));
1923 char *buf;
1924 int r;
1925 if (sizeof(sz) != bytes)
1926 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001927 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001928 return -E2BIG;
1929 buf = malloc(sz);
1930 if (!buf)
1931 return -ENOMEM;
1932 bytes = read(fd, buf, sz);
1933 if (bytes != sz) {
1934 free(buf);
1935 return -EINVAL;
1936 }
1937 r = minijail_unmarshal(j, buf, sz);
1938 free(buf);
1939 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001940}
1941
Will Drewry6ac91122011-10-21 16:38:58 -05001942int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001943{
1944 char *buf;
1945 size_t sz = minijail_size(j);
1946 ssize_t written;
1947 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001948
Elly Jonese1749eb2011-10-07 13:54:59 -04001949 if (!sz)
1950 return -EINVAL;
1951 buf = malloc(sz);
1952 r = minijail_marshal(j, buf, sz);
1953 if (r) {
1954 free(buf);
1955 return r;
1956 }
1957 /* Sends [size][minijail]. */
1958 written = write(fd, &sz, sizeof(sz));
1959 if (written != sizeof(sz)) {
1960 free(buf);
1961 return -EFAULT;
1962 }
1963 written = write(fd, buf, sz);
1964 if (written < 0 || (size_t) written != sz) {
1965 free(buf);
1966 return -EFAULT;
1967 }
1968 free(buf);
1969 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001970}
Elly Jonescd7a9042011-07-22 13:56:51 -04001971
Will Drewry6ac91122011-10-21 16:38:58 -05001972int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001973{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001974#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001975 /* Don't use LDPRELOAD on Android. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001976 return 0;
1977#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001978 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1979 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1980 if (!newenv)
1981 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001982
Elly Jonese1749eb2011-10-07 13:54:59 -04001983 /* Only insert a separating space if we have something to separate... */
1984 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1985 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001986
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001987 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001988 setenv(kLdPreloadEnvVar, newenv, 1);
1989 free(newenv);
1990 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001991#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001992}
1993
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04001994static int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001995{
1996 int r = pipe(fds);
1997 char fd_buf[11];
1998 if (r)
1999 return r;
2000 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
2001 if (r <= 0)
2002 return -EINVAL;
2003 setenv(kFdEnvVar, fd_buf, 1);
2004 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05002005}
2006
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04002007static int close_open_fds(int *inheritable_fds, size_t size)
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002008{
2009 const char *kFdPath = "/proc/self/fd";
2010
2011 DIR *d = opendir(kFdPath);
2012 struct dirent *dir_entry;
2013
2014 if (d == NULL)
2015 return -1;
2016 int dir_fd = dirfd(d);
2017 while ((dir_entry = readdir(d)) != NULL) {
2018 size_t i;
2019 char *end;
2020 bool should_close = true;
2021 const int fd = strtol(dir_entry->d_name, &end, 10);
2022
2023 if ((*end) != '\0') {
2024 continue;
2025 }
2026 /*
2027 * We might have set up some pipes that we want to share with
2028 * the parent process, and should not be closed.
2029 */
2030 for (i = 0; i < size; ++i) {
2031 if (fd == inheritable_fds[i]) {
2032 should_close = false;
2033 break;
2034 }
2035 }
2036 /* Also avoid closing the directory fd. */
2037 if (should_close && fd != dir_fd)
2038 close(fd);
2039 }
2040 closedir(d);
2041 return 0;
2042}
2043
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002044static int redirect_fds(struct minijail *j)
2045{
2046 size_t i, i2;
2047 int closeable;
2048 for (i = 0; i < j->preserved_fd_count; i++) {
2049 if (dup2(j->preserved_fds[i].parent_fd,
2050 j->preserved_fds[i].child_fd) == -1) {
2051 return -1;
2052 }
2053 }
2054 /*
2055 * After all fds have been duped, we are now free to close all parent
2056 * fds that are *not* child fds.
2057 */
2058 for (i = 0; i < j->preserved_fd_count; i++) {
2059 closeable = true;
2060 for (i2 = 0; i2 < j->preserved_fd_count; i2++) {
2061 closeable &= j->preserved_fds[i].parent_fd !=
2062 j->preserved_fds[i2].child_fd;
2063 }
2064 if (closeable)
2065 close(j->preserved_fds[i].parent_fd);
2066 }
2067 return 0;
2068}
2069
Dylan Reidacfb8be2017-08-25 12:56:51 -07002070/*
2071 * Structure that specifies how to start a minijail.
2072 *
2073 * filename - The program to exec in the child.
2074 * argv - Arguments for the child program.
2075 * use_preload - If true use LD_PRELOAD.
2076 */
2077struct minijail_run_config {
2078 const char *filename;
2079 char *const *argv;
2080 int use_preload;
2081};
2082
2083/*
2084 * Set of pointers to fill with values from minijail_run.
2085 * All arguments are allowed to be NULL if unused.
2086 *
2087 * pstdin_fd - Filled with stdin pipe if non-NULL.
2088 * pstdout_fd - Filled with stdout pipe if non-NULL.
2089 * pstderr_fd - Filled with stderr pipe if non-NULL.
2090 * pchild_pid - Filled with the pid of the child process if non-NULL.
2091 */
2092struct minijail_run_status {
2093 int *pstdin_fd;
2094 int *pstdout_fd;
2095 int *pstderr_fd;
2096 pid_t *pchild_pid;
2097};
2098
Dylan Reid18c49c82017-08-25 14:52:27 -07002099static int minijail_run_internal(struct minijail *j,
2100 const struct minijail_run_config *config,
2101 struct minijail_run_status *status_out);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002102
Will Drewry6ac91122011-10-21 16:38:58 -05002103int API minijail_run(struct minijail *j, const char *filename,
2104 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04002105{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002106 struct minijail_run_config config = {
2107 .filename = filename,
2108 .argv = argv,
2109 .use_preload = true,
2110 };
2111 struct minijail_run_status status = {};
2112 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07002113}
2114
2115int API minijail_run_pid(struct minijail *j, const char *filename,
2116 char *const argv[], pid_t *pchild_pid)
2117{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002118 struct minijail_run_config config = {
2119 .filename = filename,
2120 .argv = argv,
2121 .use_preload = true,
2122 };
2123 struct minijail_run_status status = {
2124 .pchild_pid = pchild_pid,
2125 };
2126 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002127}
2128
2129int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07002130 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002131{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002132 struct minijail_run_config config = {
2133 .filename = filename,
2134 .argv = argv,
2135 .use_preload = true,
2136 };
2137 struct minijail_run_status status = {
2138 .pstdin_fd = pstdin_fd,
2139 };
2140 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002141}
2142
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002143int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07002144 char *const argv[], pid_t *pchild_pid,
2145 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002146{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002147 struct minijail_run_config config = {
2148 .filename = filename,
2149 .argv = argv,
2150 .use_preload = true,
2151 };
2152 struct minijail_run_status status = {
2153 .pstdin_fd = pstdin_fd,
2154 .pstdout_fd = pstdout_fd,
2155 .pstderr_fd = pstderr_fd,
2156 .pchild_pid = pchild_pid,
2157 };
2158 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002159}
2160
2161int API minijail_run_no_preload(struct minijail *j, const char *filename,
2162 char *const argv[])
2163{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002164 struct minijail_run_config config = {
2165 .filename = filename,
2166 .argv = argv,
2167 .use_preload = false,
2168 };
2169 struct minijail_run_status status = {};
2170 return minijail_run_internal(j, &config, &status);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002171}
2172
Samuel Tan63187f42015-10-16 13:01:53 -07002173int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08002174 const char *filename,
2175 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07002176 pid_t *pchild_pid,
Dylan Reidacfb8be2017-08-25 12:56:51 -07002177 int *pstdin_fd,
2178 int *pstdout_fd,
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002179 int *pstderr_fd)
2180{
Dylan Reidacfb8be2017-08-25 12:56:51 -07002181 struct minijail_run_config config = {
2182 .filename = filename,
2183 .argv = argv,
2184 .use_preload = false,
2185 };
2186 struct minijail_run_status status = {
2187 .pstdin_fd = pstdin_fd,
2188 .pstdout_fd = pstdout_fd,
2189 .pstderr_fd = pstderr_fd,
2190 .pchild_pid = pchild_pid,
2191 };
2192 return minijail_run_internal(j, &config, &status);
Samuel Tan63187f42015-10-16 13:01:53 -07002193}
2194
Dylan Reid18c49c82017-08-25 14:52:27 -07002195static int minijail_run_internal(struct minijail *j,
2196 const struct minijail_run_config *config,
2197 struct minijail_run_status *status_out)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002198{
Elly Jonese1749eb2011-10-07 13:54:59 -04002199 char *oldenv, *oldenv_copy = NULL;
2200 pid_t child_pid;
2201 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002202 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002203 int stdout_fds[2];
2204 int stderr_fds[2];
Dylan Reidce5b55e2016-01-13 11:04:16 -08002205 int child_sync_pipe_fds[2];
2206 int sync_child = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -04002207 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04002208 /* We need to remember this across the minijail_preexec() call. */
2209 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002210 int do_init = j->flags.do_init;
Dylan Reidacfb8be2017-08-25 12:56:51 -07002211 int use_preload = config->use_preload;
Ben Chan541c7e52011-08-26 14:55:53 -07002212
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002213 if (use_preload) {
2214 oldenv = getenv(kLdPreloadEnvVar);
2215 if (oldenv) {
2216 oldenv_copy = strdup(oldenv);
2217 if (!oldenv_copy)
2218 return -ENOMEM;
2219 }
2220
2221 if (setup_preload())
2222 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04002223 }
Will Drewryf89aef52011-09-16 16:48:57 -05002224
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002225 if (!use_preload) {
Luis Hector Chavezfe5fb8e2017-06-29 10:41:27 -07002226 if (j->flags.use_caps && j->caps != 0 &&
2227 !j->flags.set_ambient_caps) {
2228 die("non-empty, non-ambient capabilities are not "
2229 "supported without LD_PRELOAD");
2230 }
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002231 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05002232
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002233 if (use_preload && j->hooks_head != NULL) {
2234 die("Minijail hooks are not supported with LD_PRELOAD");
2235 }
2236
Elly Jonesdd3e8512012-01-23 15:13:38 -05002237 /*
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002238 * Make the process group ID of this process equal to its PID.
2239 * In the non-interactive case (e.g. when the parent process is started
2240 * from init) this ensures the parent process and the jailed process
2241 * can be killed together.
2242 * When the parent process is started from the console this ensures
2243 * the call to setsid(2) in the jailed process succeeds.
2244 *
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002245 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
2246 * the process is already a process group leader.
2247 */
2248 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
2249 if (errno != EPERM) {
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002250 pdie("setpgid(0, 0) failed");
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07002251 }
2252 }
2253
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002254 if (use_preload) {
2255 /*
2256 * Before we fork(2) and execve(2) the child process, we need
2257 * to open a pipe(2) to send the minijail configuration over.
2258 */
2259 if (setup_pipe(pipe_fds))
2260 return -EFAULT;
2261 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002262
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002263 /*
2264 * If we want to write to the child process' standard input,
2265 * create the pipe(2) now.
2266 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002267 if (status_out->pstdin_fd) {
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002268 if (pipe(stdin_fds))
2269 return -EFAULT;
2270 }
2271
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002272 /*
2273 * If we want to read from the child process' standard output,
2274 * create the pipe(2) now.
2275 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002276 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002277 if (pipe(stdout_fds))
2278 return -EFAULT;
2279 }
2280
2281 /*
2282 * If we want to read from the child process' standard error,
2283 * create the pipe(2) now.
2284 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002285 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002286 if (pipe(stderr_fds))
2287 return -EFAULT;
2288 }
2289
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002290 /*
Jorge Lucangeli Obesab6fa6f2016-08-04 15:42:48 -04002291 * If we want to set up a new uid/gid map in the user namespace,
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002292 * or if we need to add the child process to cgroups, create the pipe(2)
2293 * to sync between parent and child.
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002294 */
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002295 if (j->flags.userns || j->flags.cgroups) {
Dylan Reidce5b55e2016-01-13 11:04:16 -08002296 sync_child = 1;
2297 if (pipe(child_sync_pipe_fds))
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002298 return -EFAULT;
2299 }
2300
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08002301 /*
2302 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04002303 *
2304 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
2305 *
2306 * In multithreaded programs, there are a bunch of locks inside libc,
2307 * some of which may be held by other threads at the time that we call
2308 * minijail_run_pid(). If we call fork(), glibc does its level best to
2309 * ensure that we hold all of these locks before it calls clone()
2310 * internally and drop them after clone() returns, but when we call
2311 * sys_clone(2) directly, all that gets bypassed and we end up with a
2312 * child address space where some of libc's important locks are held by
2313 * other threads (which did not get cloned, and hence will never release
2314 * those locks). This is okay so long as we call exec() immediately
2315 * after, but a bunch of seemingly-innocent libc functions like setenv()
2316 * take locks.
2317 *
2318 * Hence, only call sys_clone() if we need to, in order to get at pid
2319 * namespacing. If we follow this path, the child's address space might
2320 * have broken locks; you may only call functions that do not acquire
2321 * any locks.
2322 *
2323 * Unfortunately, fork() acquires every lock it can get its hands on, as
2324 * previously detailed, so this function is highly likely to deadlock
2325 * later on (see "deadlock here") if we're multithreaded.
2326 *
2327 * We might hack around this by having the clone()d child (init of the
2328 * pid namespace) return directly, rather than leaving the clone()d
2329 * process hanging around to be init for the new namespace (and having
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -08002330 * its fork()ed child return in turn), but that process would be
2331 * crippled with its libc locks potentially broken. We might try
2332 * fork()ing in the parent before we clone() to ensure that we own all
2333 * the locks, but then we have to have the forked child hanging around
2334 * consuming resources (and possibly having file descriptors / shared
2335 * memory regions / etc attached). We'd need to keep the child around to
2336 * avoid having its children get reparented to init.
Elly Jones761b7412012-06-13 15:49:52 -04002337 *
2338 * TODO(ellyjones): figure out if the "forked child hanging around"
2339 * problem is fixable or not. It would be nice if we worked in this
2340 * case.
2341 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002342 if (pid_namespace) {
2343 int clone_flags = CLONE_NEWPID | SIGCHLD;
2344 if (j->flags.userns)
2345 clone_flags |= CLONE_NEWUSER;
2346 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002347 } else {
Elly Jones761b7412012-06-13 15:49:52 -04002348 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002349 }
Elly Jones761b7412012-06-13 15:49:52 -04002350
Elly Jonese1749eb2011-10-07 13:54:59 -04002351 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002352 if (use_preload) {
2353 free(oldenv_copy);
2354 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07002355 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04002356 }
Will Drewryf89aef52011-09-16 16:48:57 -05002357
Elly Jonese1749eb2011-10-07 13:54:59 -04002358 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002359 if (use_preload) {
2360 /* Restore parent's LD_PRELOAD. */
2361 if (oldenv_copy) {
2362 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
2363 free(oldenv_copy);
2364 } else {
2365 unsetenv(kLdPreloadEnvVar);
2366 }
2367 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04002368 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002369
Elly Jonese1749eb2011-10-07 13:54:59 -04002370 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002371
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -04002372 if (j->flags.forward_signals) {
2373 forward_pid = child_pid;
2374 install_signal_handlers();
2375 }
2376
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002377 if (j->flags.pid_file)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002378 write_pid_file_or_die(j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08002379
Jorge Lucangeli Obesb8a51382016-01-25 20:08:22 -08002380 if (j->flags.cgroups)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002381 add_to_cgroups_or_die(j);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002382
Dylan Reid0f72ef42017-06-06 15:42:49 -07002383 if (j->rlimit_count)
2384 set_rlimits_or_die(j);
2385
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002386 if (j->flags.userns)
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04002387 write_ugid_maps_or_die(j);
Dylan Reidce5b55e2016-01-13 11:04:16 -08002388
2389 if (sync_child)
2390 parent_setup_complete(child_sync_pipe_fds);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002391
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002392 if (use_preload) {
2393 /* Send marshalled minijail. */
2394 close(pipe_fds[0]); /* read endpoint */
2395 ret = minijail_to_fd(j, pipe_fds[1]);
2396 close(pipe_fds[1]); /* write endpoint */
2397 if (ret) {
2398 kill(j->initpid, SIGKILL);
2399 die("failed to send marshalled minijail");
2400 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002401 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002402
Dylan Reidacfb8be2017-08-25 12:56:51 -07002403 if (status_out->pchild_pid)
2404 *status_out->pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002405
2406 /*
2407 * If we want to write to the child process' standard input,
2408 * set up the write end of the pipe.
2409 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002410 if (status_out->pstdin_fd)
2411 *status_out->pstdin_fd =
2412 setup_pipe_end(stdin_fds, 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002413
2414 /*
2415 * If we want to read from the child process' standard output,
2416 * set up the read end of the pipe.
2417 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002418 if (status_out->pstdout_fd)
2419 *status_out->pstdout_fd =
2420 setup_pipe_end(stdout_fds, 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002421
2422 /*
2423 * If we want to read from the child process' standard error,
2424 * set up the read end of the pipe.
2425 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002426 if (status_out->pstderr_fd)
2427 *status_out->pstderr_fd =
2428 setup_pipe_end(stderr_fds, 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002429
Elly Jonese1749eb2011-10-07 13:54:59 -04002430 return 0;
2431 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002432 /* Child process. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002433 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07002434
Peter Qiu2860c462015-12-16 15:13:06 -08002435 if (j->flags.reset_signal_mask) {
2436 sigset_t signal_mask;
2437 if (sigemptyset(&signal_mask) != 0)
2438 pdie("sigemptyset failed");
2439 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0)
2440 pdie("sigprocmask failed");
2441 }
2442
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002443 if (j->flags.close_open_fds) {
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002444 const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002445 int inheritable_fds[kMaxInheritableFdsSize];
2446 size_t size = 0;
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002447 size_t i;
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002448 if (use_preload) {
2449 inheritable_fds[size++] = pipe_fds[0];
2450 inheritable_fds[size++] = pipe_fds[1];
2451 }
2452 if (sync_child) {
2453 inheritable_fds[size++] = child_sync_pipe_fds[0];
2454 inheritable_fds[size++] = child_sync_pipe_fds[1];
2455 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002456 if (status_out->pstdin_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002457 inheritable_fds[size++] = stdin_fds[0];
2458 inheritable_fds[size++] = stdin_fds[1];
2459 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002460 if (status_out->pstdout_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002461 inheritable_fds[size++] = stdout_fds[0];
2462 inheritable_fds[size++] = stdout_fds[1];
2463 }
Dylan Reidacfb8be2017-08-25 12:56:51 -07002464 if (status_out->pstderr_fd) {
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002465 inheritable_fds[size++] = stderr_fds[0];
2466 inheritable_fds[size++] = stderr_fds[1];
2467 }
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002468 for (i = 0; i < j->preserved_fd_count; i++) {
2469 /*
2470 * Preserve all parent_fds. They will be dup2(2)-ed in
2471 * the child later.
2472 */
2473 inheritable_fds[size++] = j->preserved_fds[i].parent_fd;
2474 }
Luis Hector Chavez43ff0802016-10-07 12:21:07 -07002475
2476 if (close_open_fds(inheritable_fds, size) < 0)
2477 die("failed to close open file descriptors");
2478 }
2479
Luis Hector Chavez1617f632017-08-01 18:32:30 -07002480 if (redirect_fds(j))
2481 die("failed to set up fd redirections");
2482
Dylan Reidce5b55e2016-01-13 11:04:16 -08002483 if (sync_child)
2484 wait_for_parent_setup(child_sync_pipe_fds);
2485
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002486 if (j->flags.userns)
Dylan Reidce5b55e2016-01-13 11:04:16 -08002487 enter_user_namespace(j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08002488
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002489 /*
2490 * If we want to write to the jailed process' standard input,
2491 * set up the read end of the pipe.
2492 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002493 if (status_out->pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002494 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
2495 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07002496 die("failed to set up stdin pipe");
2497 }
2498
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002499 /*
2500 * If we want to read from the jailed process' standard output,
2501 * set up the write end of the pipe.
2502 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002503 if (status_out->pstdout_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002504 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
2505 STDOUT_FILENO) < 0)
2506 die("failed to set up stdout pipe");
2507 }
2508
2509 /*
2510 * If we want to read from the jailed process' standard error,
2511 * set up the write end of the pipe.
2512 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002513 if (status_out->pstderr_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08002514 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
2515 STDERR_FILENO) < 0)
2516 die("failed to set up stderr pipe");
2517 }
2518
Jorge Lucangeli Obesaa235b92016-11-23 13:48:15 -05002519 /*
2520 * If any of stdin, stdout, or stderr are TTYs, create a new session.
2521 * This prevents the jailed process from using the TIOCSTI ioctl
2522 * to push characters into the parent process terminal's input buffer,
2523 * therefore escaping the jail.
2524 */
2525 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) ||
2526 isatty(STDERR_FILENO)) {
2527 if (setsid() < 0) {
2528 pdie("setsid() failed");
2529 }
2530 }
2531
Dylan Reid791f5772015-09-14 20:02:42 -07002532 /* If running an init program, let it decide when/how to mount /proc. */
2533 if (pid_namespace && !do_init)
2534 j->flags.remount_proc_ro = 0;
2535
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002536 if (use_preload) {
2537 /* Strip out flags that cannot be inherited across execve(2). */
2538 minijail_preexec(j);
2539 } else {
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002540 /*
2541 * If not using LD_PRELOAD, do all jailing before execve(2).
2542 * Note that PID namespaces can only be entered on fork(2),
2543 * so that flag is still cleared.
2544 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002545 j->flags.pids = 0;
2546 }
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002547 /* Jail this process, then execve(2) the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04002548 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002549
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002550 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05002551 /*
2552 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002553 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002554 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08002555 * a child to actually run the program. If |do_init == 0|, we
2556 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04002557 *
2558 * If we're multithreaded, we'll probably deadlock here. See
2559 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04002560 */
2561 child_pid = fork();
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002562 if (child_pid < 0) {
Elly Jonese1749eb2011-10-07 13:54:59 -04002563 _exit(child_pid);
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002564 } else if (child_pid > 0) {
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -04002565 /*
2566 * Best effort. Don't bother checking the return value.
2567 */
Jorge Lucangeli Obes963eeec2016-08-10 16:02:43 -04002568 prctl(PR_SET_NAME, "minijail-init");
2569 init(child_pid); /* Never returns. */
2570 }
Elly Jonese1749eb2011-10-07 13:54:59 -04002571 }
Elly Jonescd7a9042011-07-22 13:56:51 -04002572
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002573 run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE);
2574
Elly Jonesdd3e8512012-01-23 15:13:38 -05002575 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07002576 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04002577 * calling process
2578 * -> execve()-ing process
2579 * If we are:
2580 * calling process
2581 * -> init()-ing process
2582 * -> execve()-ing process
2583 */
Dylan Reidacfb8be2017-08-25 12:56:51 -07002584 ret = execve(config->filename, config->argv, environ);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002585 if (ret == -1) {
Dylan Reidacfb8be2017-08-25 12:56:51 -07002586 pwarn("execve(%s) failed", config->filename);
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -04002587 }
2588 _exit(ret);
Elly Jonescd7a9042011-07-22 13:56:51 -04002589}
2590
Will Drewry6ac91122011-10-21 16:38:58 -05002591int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002592{
2593 int st;
2594 if (kill(j->initpid, SIGTERM))
2595 return -errno;
2596 if (waitpid(j->initpid, &st, 0) < 0)
2597 return -errno;
2598 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04002599}
2600
Will Drewry6ac91122011-10-21 16:38:58 -05002601int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002602{
2603 int st;
2604 if (waitpid(j->initpid, &st, 0) < 0)
2605 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002606
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002607 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002608 int error_status = st;
2609 if (WIFSIGNALED(st)) {
2610 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07002611 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07002612 j->initpid, signum);
2613 /*
2614 * We return MINIJAIL_ERR_JAIL if the process received
2615 * SIGSYS, which happens when a syscall is blocked by
2616 * seccomp filters.
2617 * If not, we do what bash(1) does:
2618 * $? = 128 + signum
2619 */
2620 if (signum == SIGSYS) {
2621 error_status = MINIJAIL_ERR_JAIL;
2622 } else {
2623 error_status = 128 + signum;
2624 }
2625 }
2626 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07002627 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002628
2629 int exit_status = WEXITSTATUS(st);
2630 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07002631 info("child process %d exited with status %d",
2632 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08002633
2634 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04002635}
2636
Will Drewry6ac91122011-10-21 16:38:58 -05002637void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04002638{
Dylan Reid605ce7f2016-01-19 19:21:00 -08002639 size_t i;
2640
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08002641 if (j->flags.seccomp_filter && j->filter_prog) {
2642 free(j->filter_prog->filter);
2643 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04002644 }
Dylan Reid648b2202015-10-23 00:50:00 -07002645 while (j->mounts_head) {
2646 struct mountpoint *m = j->mounts_head;
2647 j->mounts_head = j->mounts_head->next;
Dylan Reid81e23972016-05-18 14:06:35 -07002648 free(m->data);
Dylan Reid648b2202015-10-23 00:50:00 -07002649 free(m->type);
2650 free(m->dest);
2651 free(m->src);
2652 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04002653 }
Dylan Reid648b2202015-10-23 00:50:00 -07002654 j->mounts_tail = NULL;
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -07002655 while (j->hooks_head) {
2656 struct hook *c = j->hooks_head;
2657 j->hooks_head = c->next;
2658 free(c);
2659 }
2660 j->hooks_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04002661 if (j->user)
2662 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08002663 if (j->suppl_gid_list)
2664 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05002665 if (j->chrootdir)
2666 free(j->chrootdir);
Jorge Lucangeli Obes3b2e6e42016-08-04 12:26:19 -04002667 if (j->pid_file_path)
2668 free(j->pid_file_path);
2669 if (j->uidmap)
2670 free(j->uidmap);
2671 if (j->gidmap)
2672 free(j->gidmap);
Mike Frysingerb9a7b162017-05-30 15:25:49 -04002673 if (j->hostname)
2674 free(j->hostname);
Andrew Brestickereac28942015-11-11 16:04:46 -08002675 if (j->alt_syscall_table)
2676 free(j->alt_syscall_table);
Dylan Reid605ce7f2016-01-19 19:21:00 -08002677 for (i = 0; i < j->cgroup_count; ++i)
2678 free(j->cgroups[i]);
Elly Jonese1749eb2011-10-07 13:54:59 -04002679 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04002680}