blob: e9dfc89865546ce6bf699a63b8ab851ce145ccb6 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
7#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07008
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08009#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050010#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040011#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070012#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <grp.h>
14#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050015#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <pwd.h>
18#include <sched.h>
19#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050020#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070021#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080022#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <syscall.h>
27#include <sys/capability.h>
28#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050029#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040030#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070031#include <sys/stat.h>
32#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080033#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <unistd.h>
36
37#include "libminijail.h"
38#include "libminijail-private.h"
39
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070040#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080041#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070042#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043
Lei Zhangeee31552012-10-17 21:27:10 -070044#ifdef HAVE_SECUREBITS_H
45#include <linux/securebits.h>
46#else
47#define SECURE_ALL_BITS 0x15
48#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
49#endif
50
Will Drewry32ac9f52011-08-18 21:36:27 -050051/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080052#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070053# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080054#endif
55
56/* For seccomp_filter using BPF. */
57#ifndef PR_SET_NO_NEW_PRIVS
58# define PR_SET_NO_NEW_PRIVS 38
59#endif
60#ifndef SECCOMP_MODE_FILTER
61# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050062#endif
63
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070064#ifdef USE_SECCOMP_SOFTFAIL
65# define SECCOMP_SOFTFAIL 1
66#else
67# define SECCOMP_SOFTFAIL 0
68#endif
69
Dylan Reid648b2202015-10-23 00:50:00 -070070struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040071 char *src;
72 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070073 char *type;
74 unsigned long flags;
75 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040076};
77
Will Drewryf89aef52011-09-16 16:48:57 -050078struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070079 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070080 * WARNING: if you add a flag here you need to make sure it's
81 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070082 */
Elly Jonese1749eb2011-10-07 13:54:59 -040083 struct {
84 int uid:1;
85 int gid:1;
86 int caps:1;
87 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070088 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040089 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040090 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -070091 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080092 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040093 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -070094 int remount_proc_ro:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040095 int usergroups:1;
96 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070097 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040098 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070099 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400100 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800101 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700102 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800103 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800104 int pid_file:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400105 } flags;
106 uid_t uid;
107 gid_t gid;
108 gid_t usergid;
109 char *user;
110 uint64_t caps;
111 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700112 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700113 int netns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800114 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400115 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800116 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800117 char *uidmap;
118 char *gidmap;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800119 struct sock_fprog *filter_prog;
Dylan Reid648b2202015-10-23 00:50:00 -0700120 struct mountpoint *mounts_head;
121 struct mountpoint *mounts_tail;
122 int mounts_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500123};
124
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700125/*
126 * Strip out flags meant for the parent.
127 * We keep things that are not inherited across execve(2) (e.g. capabilities),
128 * or are easier to set after execve(2) (e.g. seccomp filters).
129 */
130void minijail_preenter(struct minijail *j)
131{
132 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700133 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700134 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700135 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800136 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800137 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700138}
139
140/*
141 * Strip out flags meant for the child.
142 * We keep things that are inherited across execve(2).
143 */
144void minijail_preexec(struct minijail *j)
145{
146 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700147 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700148 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800149 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700150 if (j->user)
151 free(j->user);
152 j->user = NULL;
153 memset(&j->flags, 0, sizeof(j->flags));
154 /* Now restore anything we meant to keep. */
155 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700156 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700157 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800158 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700159 /* Note, |pids| will already have been used before this call. */
160}
161
162/* Minijail API. */
163
Will Drewry6ac91122011-10-21 16:38:58 -0500164struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400165{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400166 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400167}
168
Will Drewry6ac91122011-10-21 16:38:58 -0500169void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400170{
171 if (uid == 0)
172 die("useless change to uid 0");
173 j->uid = uid;
174 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400175}
176
Will Drewry6ac91122011-10-21 16:38:58 -0500177void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400178{
179 if (gid == 0)
180 die("useless change to gid 0");
181 j->gid = gid;
182 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400183}
184
Will Drewry6ac91122011-10-21 16:38:58 -0500185int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400186{
187 char *buf = NULL;
188 struct passwd pw;
189 struct passwd *ppw = NULL;
190 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
191 if (sz == -1)
192 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400193
Elly Jonesdd3e8512012-01-23 15:13:38 -0500194 /*
195 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400196 * the maximum needed size of the buffer, so we don't have to search.
197 */
198 buf = malloc(sz);
199 if (!buf)
200 return -ENOMEM;
201 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500202 /*
203 * We're safe to free the buffer here. The strings inside pw point
204 * inside buf, but we don't use any of them; this leaves the pointers
205 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
206 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400207 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700208 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400209 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700210 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400211 minijail_change_uid(j, ppw->pw_uid);
212 j->user = strdup(user);
213 if (!j->user)
214 return -ENOMEM;
215 j->usergid = ppw->pw_gid;
216 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400217}
218
Will Drewry6ac91122011-10-21 16:38:58 -0500219int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400220{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700221 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700222 struct group gr;
223 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400224 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
225 if (sz == -1)
226 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400227
Elly Jonesdd3e8512012-01-23 15:13:38 -0500228 /*
229 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400230 * the maximum needed size of the buffer, so we don't have to search.
231 */
232 buf = malloc(sz);
233 if (!buf)
234 return -ENOMEM;
235 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500236 /*
237 * We're safe to free the buffer here. The strings inside gr point
238 * inside buf, but we don't use any of them; this leaves the pointers
239 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
240 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400241 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700242 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400243 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700244 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400245 minijail_change_gid(j, pgr->gr_gid);
246 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400247}
248
Will Drewry6ac91122011-10-21 16:38:58 -0500249void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400250{
251 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400252}
253
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700254void API minijail_no_new_privs(struct minijail *j)
255{
256 j->flags.no_new_privs = 1;
257}
258
Will Drewry6ac91122011-10-21 16:38:58 -0500259void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400260{
261 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500262}
263
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700264void API minijail_log_seccomp_filter_failures(struct minijail *j)
265{
266 j->flags.log_seccomp_filter = 1;
267}
268
Will Drewry6ac91122011-10-21 16:38:58 -0500269void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400270{
271 j->caps = capmask;
272 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400273}
274
Will Drewry6ac91122011-10-21 16:38:58 -0500275void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400276{
277 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400278}
279
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700280void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
281{
282 int ns_fd = open(ns_path, O_RDONLY);
283 if (ns_fd < 0) {
284 pdie("failed to open namespace '%s'", ns_path);
285 }
286 j->mountns_fd = ns_fd;
287 j->flags.enter_vfs = 1;
288}
289
Will Drewry6ac91122011-10-21 16:38:58 -0500290void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400291{
Elly Jonese58176c2012-01-23 11:46:17 -0500292 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700293 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400294 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800295 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400296}
297
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400298void API minijail_namespace_net(struct minijail *j)
299{
300 j->flags.net = 1;
301}
302
Dylan Reid1102f5a2015-09-15 11:52:20 -0700303void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
304{
305 int ns_fd = open(ns_path, O_RDONLY);
306 if (ns_fd < 0) {
307 pdie("failed to open namespace '%s'", ns_path);
308 }
309 j->netns_fd = ns_fd;
310 j->flags.enter_net = 1;
311}
312
Dylan Reid791f5772015-09-14 20:02:42 -0700313void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400314{
315 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700316 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400317}
318
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800319void API minijail_namespace_user(struct minijail *j)
320{
321 j->flags.userns = 1;
322}
323
324int API minijail_uidmap(struct minijail *j, const char *uidmap)
325{
326 j->uidmap = strdup(uidmap);
327 if (!j->uidmap)
328 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800329 char *ch;
330 for (ch = j->uidmap; *ch; ch++) {
331 if (*ch == ',')
332 *ch = '\n';
333 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800334 return 0;
335}
336
337int API minijail_gidmap(struct minijail *j, const char *gidmap)
338{
339 j->gidmap = strdup(gidmap);
340 if (!j->gidmap)
341 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800342 char *ch;
343 for (ch = j->gidmap; *ch; ch++) {
344 if (*ch == ',')
345 *ch = '\n';
346 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800347 return 0;
348}
349
Will Drewry6ac91122011-10-21 16:38:58 -0500350void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400351{
352 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400353}
354
Will Drewry6ac91122011-10-21 16:38:58 -0500355void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400356{
357 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400358}
359
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800360void API minijail_run_as_init(struct minijail *j)
361{
362 /*
363 * Since the jailed program will become 'init' in the new PID namespace,
364 * Minijail does not need to fork an 'init' process.
365 */
366 j->flags.do_init = 0;
367}
368
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700369int API minijail_enter_chroot(struct minijail *j, const char *dir)
370{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400371 if (j->chrootdir)
372 return -EINVAL;
373 j->chrootdir = strdup(dir);
374 if (!j->chrootdir)
375 return -ENOMEM;
376 j->flags.chroot = 1;
377 return 0;
378}
379
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800380int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
381{
382 if (j->chrootdir)
383 return -EINVAL;
384 j->chrootdir = strdup(dir);
385 if (!j->chrootdir)
386 return -ENOMEM;
387 j->flags.pivot_root = 1;
388 return 0;
389}
390
Dylan Reida14e08d2015-10-22 21:05:29 -0700391static char *append_external_path(const char *external_path,
392 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700393{
Dylan Reida14e08d2015-10-22 21:05:29 -0700394 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700395 size_t pathlen;
396
Dylan Reid08946cc2015-09-16 19:10:57 -0700397 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700398 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
399 path = malloc(pathlen);
400 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700401
Dylan Reida14e08d2015-10-22 21:05:29 -0700402 return path;
403}
404
405char API *minijail_get_original_path(struct minijail *j,
406 const char *path_inside_chroot)
407{
Dylan Reid648b2202015-10-23 00:50:00 -0700408 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700409
Dylan Reid648b2202015-10-23 00:50:00 -0700410 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700411 while (b) {
412 /*
413 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700414 * mount, then the original path is exactly the source of
415 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700416 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700417 * mount source = /some/path/exe, mount dest =
418 * /chroot/path/exe Then when getting the original path of
419 * "/chroot/path/exe", the source of that mount,
420 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700421 */
422 if (!strcmp(b->dest, path_inside_chroot))
423 return strdup(b->src);
424
425 /*
426 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700427 * mount, take the suffix of the chroot path relative to the
428 * mount destination path, and append it to the mount source
429 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700430 */
431 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
432 const char *relative_path =
433 path_inside_chroot + strlen(b->dest);
434 return append_external_path(b->src, relative_path);
435 }
436 b = b->next;
437 }
438
439 /* If there is a chroot path, append |path_inside_chroot| to that. */
440 if (j->chrootdir)
441 return append_external_path(j->chrootdir, path_inside_chroot);
442
443 /* No chroot, so the path outside is the same as it is inside. */
444 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700445}
446
Lee Campbell11af0622014-05-22 12:36:04 -0700447void API minijail_mount_tmp(struct minijail *j)
448{
449 j->flags.mount_tmp = 1;
450}
451
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800452int API minijail_write_pid_file(struct minijail *j, const char *path)
453{
454 j->pid_file_path = strdup(path);
455 if (!j->pid_file_path)
456 return -ENOMEM;
457 j->flags.pid_file = 1;
458 return 0;
459}
460
Dylan Reid648b2202015-10-23 00:50:00 -0700461int API minijail_mount(struct minijail *j, const char *src, const char *dest,
462 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700463{
Dylan Reid648b2202015-10-23 00:50:00 -0700464 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400465
466 if (*dest != '/')
467 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700468 m = calloc(1, sizeof(*m));
469 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400470 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700471 m->dest = strdup(dest);
472 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400473 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700474 m->src = strdup(src);
475 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400476 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700477 m->type = strdup(type);
478 if (!m->type)
479 goto error;
480 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400481
Dylan Reid648b2202015-10-23 00:50:00 -0700482 info("mount %s -> %s type %s", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400483
Elly Jonesdd3e8512012-01-23 15:13:38 -0500484 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700485 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400486 * containing vfs namespace.
487 */
488 minijail_namespace_vfs(j);
489
Dylan Reid648b2202015-10-23 00:50:00 -0700490 if (j->mounts_tail)
491 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400492 else
Dylan Reid648b2202015-10-23 00:50:00 -0700493 j->mounts_head = m;
494 j->mounts_tail = m;
495 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400496
497 return 0;
498
499error:
Dylan Reid648b2202015-10-23 00:50:00 -0700500 free(m->src);
501 free(m->dest);
502 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400503 return -ENOMEM;
504}
505
Dylan Reid648b2202015-10-23 00:50:00 -0700506int API minijail_bind(struct minijail *j, const char *src, const char *dest,
507 int writeable)
508{
509 unsigned long flags = MS_BIND;
510
511 if (!writeable)
512 flags |= MS_RDONLY;
513
514 return minijail_mount(j, src, dest, "", flags);
515}
516
Will Drewry6ac91122011-10-21 16:38:58 -0500517void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400518{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700519 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
520 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
521 warn("not loading seccomp filter, seccomp not supported");
522 return;
523 }
524 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400525 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800526 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700527 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400528 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800529
530 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700531 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
532 die("failed to compile seccomp filter BPF program in '%s'",
533 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800534 }
535
536 j->filter_len = fprog->len;
537 j->filter_prog = fprog;
538
Elly Jonese1749eb2011-10-07 13:54:59 -0400539 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500540}
541
Will Drewryf89aef52011-09-16 16:48:57 -0500542struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400543 size_t available;
544 size_t total;
545 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500546};
547
Will Drewry6ac91122011-10-21 16:38:58 -0500548void marshal_state_init(struct marshal_state *state,
549 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400550{
551 state->available = available;
552 state->buf = buf;
553 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500554}
555
Will Drewry6ac91122011-10-21 16:38:58 -0500556void marshal_append(struct marshal_state *state,
557 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400558{
559 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500560
Elly Jonese1749eb2011-10-07 13:54:59 -0400561 /* Up to |available| will be written. */
562 if (copy_len) {
563 memcpy(state->buf, src, copy_len);
564 state->buf += copy_len;
565 state->available -= copy_len;
566 }
567 /* |total| will contain the expected length. */
568 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500569}
570
Will Drewry6ac91122011-10-21 16:38:58 -0500571void minijail_marshal_helper(struct marshal_state *state,
572 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400573{
Dylan Reid648b2202015-10-23 00:50:00 -0700574 struct mountpoint *m = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400575 marshal_append(state, (char *)j, sizeof(*j));
576 if (j->user)
577 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400578 if (j->chrootdir)
579 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800580 if (j->flags.seccomp_filter && j->filter_prog) {
581 struct sock_fprog *fp = j->filter_prog;
582 marshal_append(state, (char *)fp->filter,
583 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400584 }
Dylan Reid648b2202015-10-23 00:50:00 -0700585 for (m = j->mounts_head; m; m = m->next) {
586 marshal_append(state, m->src, strlen(m->src) + 1);
587 marshal_append(state, m->dest, strlen(m->dest) + 1);
588 marshal_append(state, m->type, strlen(m->type) + 1);
589 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400590 }
Will Drewryf89aef52011-09-16 16:48:57 -0500591}
592
Will Drewry6ac91122011-10-21 16:38:58 -0500593size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400594{
595 struct marshal_state state;
596 marshal_state_init(&state, NULL, 0);
597 minijail_marshal_helper(&state, j);
598 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500599}
600
Elly Jonese1749eb2011-10-07 13:54:59 -0400601int minijail_marshal(const struct minijail *j, char *buf, size_t available)
602{
603 struct marshal_state state;
604 marshal_state_init(&state, buf, available);
605 minijail_marshal_helper(&state, j);
606 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500607}
608
Elly Jones51a5b6c2011-10-12 19:09:26 -0400609/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
610 * @length Number of bytes to consume
611 * @buf Buffer to consume from
612 * @buflength Size of @buf
613 *
614 * Returns a pointer to the base of the bytes, or NULL for errors.
615 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700616void *consumebytes(size_t length, char **buf, size_t *buflength)
617{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400618 char *p = *buf;
619 if (length > *buflength)
620 return NULL;
621 *buf += length;
622 *buflength -= length;
623 return p;
624}
625
626/* consumestr: consumes a C string from a buffer @buf of length @length
627 * @buf Buffer to consume
628 * @length Length of buffer
629 *
630 * Returns a pointer to the base of the string, or NULL for errors.
631 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700632char *consumestr(char **buf, size_t *buflength)
633{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400634 size_t len = strnlen(*buf, *buflength);
635 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700636 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400637 return NULL;
638 return consumebytes(len + 1, buf, buflength);
639}
640
Elly Jonese1749eb2011-10-07 13:54:59 -0400641int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
642{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400643 int i;
644 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500645 int ret = -EINVAL;
646
Elly Jonese1749eb2011-10-07 13:54:59 -0400647 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500648 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400649 memcpy((void *)j, serialized, sizeof(*j));
650 serialized += sizeof(*j);
651 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500652
Will Drewrybee7ba72011-10-21 20:47:01 -0500653 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700654 j->mounts_head = NULL;
655 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800656 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500657
Elly Jonese1749eb2011-10-07 13:54:59 -0400658 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400659 char *user = consumestr(&serialized, &length);
660 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500661 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400662 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500663 if (!j->user)
664 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400665 }
Will Drewryf89aef52011-09-16 16:48:57 -0500666
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400667 if (j->chrootdir) { /* stale pointer */
668 char *chrootdir = consumestr(&serialized, &length);
669 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500670 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400671 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500672 if (!j->chrootdir)
673 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400674 }
675
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800676 if (j->flags.seccomp_filter && j->filter_len > 0) {
677 size_t ninstrs = j->filter_len;
678 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
679 ninstrs > USHRT_MAX)
680 goto bad_filters;
681
682 size_t program_len = ninstrs * sizeof(struct sock_filter);
683 void *program = consumebytes(program_len, &serialized, &length);
684 if (!program)
685 goto bad_filters;
686
687 j->filter_prog = malloc(sizeof(struct sock_fprog));
688 j->filter_prog->len = ninstrs;
689 j->filter_prog->filter = malloc(program_len);
690 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400691 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400692
Dylan Reid648b2202015-10-23 00:50:00 -0700693 count = j->mounts_count;
694 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400695 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700696 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400697 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700698 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400699 const char *src = consumestr(&serialized, &length);
700 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700701 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400702 dest = consumestr(&serialized, &length);
703 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700704 goto bad_mounts;
705 type = consumestr(&serialized, &length);
706 if (!type)
707 goto bad_mounts;
708 flags = consumebytes(sizeof(*flags), &serialized, &length);
709 if (!flags)
710 goto bad_mounts;
711 if (minijail_mount(j, src, dest, type, *flags))
712 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400713 }
714
Elly Jonese1749eb2011-10-07 13:54:59 -0400715 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500716
Dylan Reid648b2202015-10-23 00:50:00 -0700717bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800718 if (j->flags.seccomp_filter && j->filter_len > 0) {
719 free(j->filter_prog->filter);
720 free(j->filter_prog);
721 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500722bad_filters:
723 if (j->chrootdir)
724 free(j->chrootdir);
725bad_chrootdir:
726 if (j->user)
727 free(j->user);
728clear_pointers:
729 j->user = NULL;
730 j->chrootdir = NULL;
731out:
732 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500733}
734
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800735static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
736{
737 int fd, ret, len;
738 size_t sz;
739 char fname[32];
740 close(pipe_fds[0]);
741
742 sz = sizeof(fname);
743 if (j->uidmap) {
744 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700745 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800746 die("failed to write file name of uid_map");
747 fd = open(fname, O_WRONLY);
748 if (fd < 0)
749 pdie("failed to open '%s'", fname);
750 len = strlen(j->uidmap);
751 if (write(fd, j->uidmap, len) < len)
752 die("failed to set uid_map");
753 close(fd);
754 }
755 if (j->gidmap) {
756 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700757 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800758 die("failed to write file name of gid_map");
759 fd = open(fname, O_WRONLY);
760 if (fd < 0)
761 pdie("failed to open '%s'", fname);
762 len = strlen(j->gidmap);
763 if (write(fd, j->gidmap, len) < len)
764 die("failed to set gid_map");
765 close(fd);
766 }
767
768 close(pipe_fds[1]);
769}
770
771static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
772{
773 char buf;
774
775 close(pipe_fds[1]);
776
777 /* Wait for parent to set up uid/gid mappings. */
778 if (read(pipe_fds[0], &buf, 1) != 0)
779 die("failed to sync with parent");
780 close(pipe_fds[0]);
781
782 if (j->uidmap && setresuid(0, 0, 0))
783 pdie("setresuid");
784 if (j->gidmap && setresgid(0, 0, 0))
785 pdie("setresgid");
786}
787
Dylan Reid648b2202015-10-23 00:50:00 -0700788/* mount_one: Applies mounts from @m for @j, recursing as needed.
789 * @j Minijail these mounts are for
790 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400791 *
792 * Returns 0 for success.
793 */
Dylan Reid648b2202015-10-23 00:50:00 -0700794static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700795{
Dylan Reid648b2202015-10-23 00:50:00 -0700796 int ret;
797 char *dest;
798 int remount_ro = 0;
799
Elly Jones51a5b6c2011-10-12 19:09:26 -0400800 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700801 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400802 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700803
804 /*
805 * R/O bind mounts have to be remounted since bind and ro can't both be
806 * specified in the original bind mount. Remount R/O after the initial
807 * mount.
808 */
809 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
810 remount_ro = 1;
811 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500812 }
Dylan Reid648b2202015-10-23 00:50:00 -0700813
814 ret = mount(m->src, dest, m->type, m->flags, NULL);
815 if (ret)
816 pdie("mount: %s -> %s", m->src, dest);
817
818 if (remount_ro) {
819 m->flags |= MS_RDONLY;
820 ret = mount(m->src, dest, NULL,
821 m->flags | MS_REMOUNT, NULL);
822 if (ret)
823 pdie("bind ro: %s -> %s", m->src, dest);
824 }
825
Elly Jones51a5b6c2011-10-12 19:09:26 -0400826 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700827 if (m->next)
828 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400829 return ret;
830}
831
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700832int enter_chroot(const struct minijail *j)
833{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400834 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700835
836 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400837 return ret;
838
839 if (chroot(j->chrootdir))
840 return -errno;
841
842 if (chdir("/"))
843 return -errno;
844
845 return 0;
846}
847
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800848int enter_pivot_root(const struct minijail *j)
849{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800850 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -0700851
852 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800853 return ret;
854
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800855 /* Keep the fd for both old and new root. It will be used in fchdir later. */
856 oldroot = open("/", O_DIRECTORY | O_RDONLY);
857 if (oldroot < 0)
858 pdie("failed to open / for fchdir");
859 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
860 if (newroot < 0)
861 pdie("failed to open %s for fchdir", j->chrootdir);
862
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800863 /* To ensure chrootdir is the root of a file system, do a self bind mount. */
864 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
865 pdie("failed to bind mount '%s'", j->chrootdir);
866 if (chdir(j->chrootdir))
867 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800868 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800869 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800870
871 /*
872 * Now the old root is mounted on top of the new root. Use fchdir to
873 * change to the old root and unmount it.
874 */
875 if (fchdir(oldroot))
876 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800877 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800878 if (umount2(".", MNT_DETACH))
879 pdie("umount(/)");
880 /* Change back to the new root. */
881 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800882 return -errno;
883 if (chroot("/"))
884 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -0700885 /* Set correct CWD for getcwd(3). */
886 if (chdir("/"))
887 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800888
889 return 0;
890}
891
Lee Campbell11af0622014-05-22 12:36:04 -0700892int mount_tmp(void)
893{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800894 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700895}
896
Dylan Reid791f5772015-09-14 20:02:42 -0700897int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400898{
899 const char *kProcPath = "/proc";
900 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500901 /*
902 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400903 * /proc in our namespace, which means using MS_REMOUNT here would
904 * mutate our parent's mount as well, even though we're in a VFS
905 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800906 * and make our own. However, if we are in a new user namespace, /proc
907 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400908 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -0700909 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400910 return -errno;
911 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
912 return -errno;
913 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400914}
915
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800916static void write_pid_file(const struct minijail *j)
917{
918 FILE *fp = fopen(j->pid_file_path, "w");
919
920 if (!fp)
921 pdie("failed to open '%s'", j->pid_file_path);
922 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
923 pdie("fprintf(%s)", j->pid_file_path);
924 if (fclose(fp))
925 pdie("fclose(%s)", j->pid_file_path);
926}
927
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700928void drop_ugid(const struct minijail *j)
929{
930 if (j->flags.usergroups) {
931 if (initgroups(j->user, j->usergid))
932 pdie("initgroups");
933 } else {
934 /* Only attempt to clear supplemental groups if we are changing
935 * users. */
936 if ((j->uid || j->gid) && setgroups(0, NULL))
937 pdie("setgroups");
938 }
939
940 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
941 pdie("setresgid");
942
943 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
944 pdie("setresuid");
945}
946
Mike Frysinger3adfef72013-05-09 17:19:08 -0400947/*
948 * We specifically do not use cap_valid() as that only tells us the last
949 * valid cap we were *compiled* against (i.e. what the version of kernel
950 * headers says). If we run on a different kernel version, then it's not
951 * uncommon for that to be less (if an older kernel) or more (if a newer
952 * kernel). So suck up the answer via /proc.
953 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700954static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -0400955{
Dylan Reidf682d472015-09-17 21:39:07 -0700956 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
957 FILE *fp = fopen(cap_file, "re");
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700958 unsigned int last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -0400959
Dylan Reidf682d472015-09-17 21:39:07 -0700960 if (fscanf(fp, "%u", &last_valid_cap) != 1)
961 pdie("fscanf(%s)", cap_file);
962 fclose(fp);
Mike Frysinger3adfef72013-05-09 17:19:08 -0400963
Dylan Reidf682d472015-09-17 21:39:07 -0700964 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -0400965}
966
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700967void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -0400968{
969 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800970 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800971 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400972 unsigned int i;
973 if (!caps)
974 die("can't get process caps");
975 if (cap_clear_flag(caps, CAP_INHERITABLE))
976 die("can't clear inheritable caps");
977 if (cap_clear_flag(caps, CAP_EFFECTIVE))
978 die("can't clear effective caps");
979 if (cap_clear_flag(caps, CAP_PERMITTED))
980 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -0700981 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800982 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800983 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400984 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800985 flag[0] = i;
986 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400987 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800988 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400989 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800990 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400991 die("can't add inheritable cap");
992 }
993 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800994 die("can't apply initial cleaned capset");
995
996 /*
997 * Instead of dropping bounding set first, do it here in case
998 * the caller had a more permissive bounding set which could
999 * have been used above to raise a capability that wasn't already
1000 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1001 */
Dylan Reidf682d472015-09-17 21:39:07 -07001002 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001003 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001004 continue;
1005 if (prctl(PR_CAPBSET_DROP, i))
1006 pdie("prctl(PR_CAPBSET_DROP)");
1007 }
Kees Cook323878a2013-02-05 15:35:24 -08001008
1009 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001010 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001011 flag[0] = CAP_SETPCAP;
1012 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1013 die("can't clear effective cap");
1014 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1015 die("can't clear permitted cap");
1016 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1017 die("can't clear inheritable cap");
1018 }
1019
1020 if (cap_set_proc(caps))
1021 die("can't apply final cleaned capset");
1022
1023 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001024}
1025
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001026void set_seccomp_filter(const struct minijail *j)
1027{
1028 /*
1029 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1030 * in the kernel source tree for an explanation of the parameters.
1031 */
1032 if (j->flags.no_new_privs) {
1033 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1034 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1035 }
1036
1037 /*
1038 * If we're logging seccomp filter failures,
1039 * install the SIGSYS handler first.
1040 */
1041 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1042 if (install_sigsys_handler())
1043 pdie("install SIGSYS handler");
1044 warn("logging seccomp filter failures");
1045 }
1046
1047 /*
1048 * Install the syscall filter.
1049 */
1050 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001051 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
1052 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1053 warn("seccomp not supported");
1054 return;
1055 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001056 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001057 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001058 }
1059}
1060
Will Drewry6ac91122011-10-21 16:38:58 -05001061void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001062{
Dylan Reidf682d472015-09-17 21:39:07 -07001063 /*
1064 * Get the last valid cap from /proc, since /proc can be unmounted
1065 * before drop_caps().
1066 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001067 unsigned int last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001068
Elly Jonese1749eb2011-10-07 13:54:59 -04001069 if (j->flags.pids)
1070 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001071 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001072
Elly Jonese1749eb2011-10-07 13:54:59 -04001073 if (j->flags.usergroups && !j->user)
1074 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001075
Elly Jonesdd3e8512012-01-23 15:13:38 -05001076 /*
1077 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001078 * so we don't even try. If any of our operations fail, we abort() the
1079 * entire process.
1080 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001081 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1082 pdie("setns(CLONE_NEWNS)");
1083
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001084 if (j->flags.vfs) {
1085 if (unshare(CLONE_NEWNS))
1086 pdie("unshare(vfs)");
1087 /*
1088 * Remount all filesystems as private. If they are shared
1089 * new bind mounts will creep out of our namespace.
1090 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1091 */
1092 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1093 pdie("mount(/, private)");
1094 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001095
Dylan Reid1102f5a2015-09-15 11:52:20 -07001096 if (j->flags.enter_net) {
1097 if (setns(j->netns_fd, CLONE_NEWNET))
1098 pdie("setns(CLONE_NEWNET)");
1099 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001100 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001101 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001102
Elly Jones51a5b6c2011-10-12 19:09:26 -04001103 if (j->flags.chroot && enter_chroot(j))
1104 pdie("chroot");
1105
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001106 if (j->flags.pivot_root && enter_pivot_root(j))
1107 pdie("pivot_root");
1108
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001109 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001110 pdie("mount_tmp");
1111
Dylan Reid791f5772015-09-14 20:02:42 -07001112 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001113 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001114
Elly Jonese1749eb2011-10-07 13:54:59 -04001115 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001116 /*
1117 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001118 * capability to change uids, our attempt to use setuid()
1119 * below will fail. Hang on to root caps across setuid(), then
1120 * lock securebits.
1121 */
1122 if (prctl(PR_SET_KEEPCAPS, 1))
1123 pdie("prctl(PR_SET_KEEPCAPS)");
1124 if (prctl
1125 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1126 pdie("prctl(PR_SET_SECUREBITS)");
1127 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001128
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001129 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001130 * If we're setting no_new_privs, we can drop privileges
1131 * before setting seccomp filter. This way filter policies
1132 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001133 */
1134 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001135 drop_ugid(j);
1136 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001137 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001138
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001139 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001140 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001141 /*
1142 * If we're not setting no_new_privs,
1143 * we need to set seccomp filter *before* dropping privileges.
1144 * WARNING: this means that filter policies *must* allow
1145 * setgroups()/setresgid()/setresuid() for dropping root and
1146 * capget()/capset()/prctl() for dropping caps.
1147 */
1148 set_seccomp_filter(j);
1149
1150 drop_ugid(j);
1151 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001152 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001153 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001154
Elly Jonesdd3e8512012-01-23 15:13:38 -05001155 /*
1156 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001157 * privilege-dropping syscalls :)
1158 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001159 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1160 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1161 warn("seccomp not supported");
1162 return;
1163 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001164 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001165 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001166}
1167
Will Drewry6ac91122011-10-21 16:38:58 -05001168/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001169static int init_exitstatus = 0;
1170
Will Drewry6ac91122011-10-21 16:38:58 -05001171void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001172{
1173 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001174}
1175
Will Drewry6ac91122011-10-21 16:38:58 -05001176int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001177{
1178 pid_t pid;
1179 int status;
1180 /* so that we exit with the right status */
1181 signal(SIGTERM, init_term);
1182 /* TODO(wad) self jail with seccomp_filters here. */
1183 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001184 /*
1185 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001186 * left inside our pid namespace or we get a signal.
1187 */
1188 if (pid == rootpid)
1189 init_exitstatus = status;
1190 }
1191 if (!WIFEXITED(init_exitstatus))
1192 _exit(MINIJAIL_ERR_INIT);
1193 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001194}
1195
Will Drewry6ac91122011-10-21 16:38:58 -05001196int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001197{
1198 size_t sz = 0;
1199 size_t bytes = read(fd, &sz, sizeof(sz));
1200 char *buf;
1201 int r;
1202 if (sizeof(sz) != bytes)
1203 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001204 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001205 return -E2BIG;
1206 buf = malloc(sz);
1207 if (!buf)
1208 return -ENOMEM;
1209 bytes = read(fd, buf, sz);
1210 if (bytes != sz) {
1211 free(buf);
1212 return -EINVAL;
1213 }
1214 r = minijail_unmarshal(j, buf, sz);
1215 free(buf);
1216 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001217}
1218
Will Drewry6ac91122011-10-21 16:38:58 -05001219int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001220{
1221 char *buf;
1222 size_t sz = minijail_size(j);
1223 ssize_t written;
1224 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001225
Elly Jonese1749eb2011-10-07 13:54:59 -04001226 if (!sz)
1227 return -EINVAL;
1228 buf = malloc(sz);
1229 r = minijail_marshal(j, buf, sz);
1230 if (r) {
1231 free(buf);
1232 return r;
1233 }
1234 /* Sends [size][minijail]. */
1235 written = write(fd, &sz, sizeof(sz));
1236 if (written != sizeof(sz)) {
1237 free(buf);
1238 return -EFAULT;
1239 }
1240 written = write(fd, buf, sz);
1241 if (written < 0 || (size_t) written != sz) {
1242 free(buf);
1243 return -EFAULT;
1244 }
1245 free(buf);
1246 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001247}
Elly Jonescd7a9042011-07-22 13:56:51 -04001248
Will Drewry6ac91122011-10-21 16:38:58 -05001249int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001250{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001251#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001252 /* Don't use LDPRELOAD on Brillo. */
1253 return 0;
1254#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001255 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1256 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1257 if (!newenv)
1258 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001259
Elly Jonese1749eb2011-10-07 13:54:59 -04001260 /* Only insert a separating space if we have something to separate... */
1261 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1262 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001263
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001264 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001265 setenv(kLdPreloadEnvVar, newenv, 1);
1266 free(newenv);
1267 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001268#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001269}
1270
Will Drewry6ac91122011-10-21 16:38:58 -05001271int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001272{
1273 int r = pipe(fds);
1274 char fd_buf[11];
1275 if (r)
1276 return r;
1277 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1278 if (r <= 0)
1279 return -EINVAL;
1280 setenv(kFdEnvVar, fd_buf, 1);
1281 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001282}
1283
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001284int setup_pipe_end(int fds[2], size_t index)
1285{
1286 if (index > 1)
1287 return -1;
1288
1289 close(fds[1 - index]);
1290 return fds[index];
1291}
1292
1293int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1294{
1295 if (index > 1)
1296 return -1;
1297
1298 close(fds[1 - index]);
1299 /* dup2(2) the corresponding end of the pipe into |fd|. */
1300 return dup2(fds[index], fd);
1301}
1302
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001303int minijail_run_internal(struct minijail *j, const char *filename,
1304 char *const argv[], pid_t *pchild_pid,
1305 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1306 int use_preload);
1307
Will Drewry6ac91122011-10-21 16:38:58 -05001308int API minijail_run(struct minijail *j, const char *filename,
1309 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001310{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001311 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1312 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001313}
1314
1315int API minijail_run_pid(struct minijail *j, const char *filename,
1316 char *const argv[], pid_t *pchild_pid)
1317{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001318 return minijail_run_internal(j, filename, argv, pchild_pid,
1319 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001320}
1321
1322int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001323 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001324{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001325 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1326 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001327}
1328
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001329int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001330 char *const argv[], pid_t *pchild_pid,
1331 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001332{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001333 return minijail_run_internal(j, filename, argv, pchild_pid,
1334 pstdin_fd, pstdout_fd, pstderr_fd, true);
1335}
1336
1337int API minijail_run_no_preload(struct minijail *j, const char *filename,
1338 char *const argv[])
1339{
1340 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1341 false);
1342}
1343
Samuel Tan63187f42015-10-16 13:01:53 -07001344int API minijail_run_pid_pipes_no_preload(struct minijail *j,
1345 const char *filename, char *const argv[],
1346 pid_t *pchild_pid,
1347 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) {
1348 return minijail_run_internal(j, filename, argv, pchild_pid,
1349 pstdin_fd, pstdout_fd, pstderr_fd, false);
1350}
1351
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001352int minijail_run_internal(struct minijail *j, const char *filename,
1353 char *const argv[], pid_t *pchild_pid,
1354 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1355 int use_preload)
1356{
Elly Jonese1749eb2011-10-07 13:54:59 -04001357 char *oldenv, *oldenv_copy = NULL;
1358 pid_t child_pid;
1359 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001360 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001361 int stdout_fds[2];
1362 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001363 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001364 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001365 /* We need to remember this across the minijail_preexec() call. */
1366 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001367 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001368
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001369 if (use_preload) {
1370 oldenv = getenv(kLdPreloadEnvVar);
1371 if (oldenv) {
1372 oldenv_copy = strdup(oldenv);
1373 if (!oldenv_copy)
1374 return -ENOMEM;
1375 }
1376
1377 if (setup_preload())
1378 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001379 }
Will Drewryf89aef52011-09-16 16:48:57 -05001380
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001381 if (!use_preload) {
1382 if (j->flags.caps)
1383 die("Capabilities are not supported without "
1384 "LD_PRELOAD");
1385 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001386
Elly Jonesdd3e8512012-01-23 15:13:38 -05001387 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001388 * Make the process group ID of this process equal to its PID, so that
1389 * both the Minijail process and the jailed process can be killed
1390 * together.
1391 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1392 * the process is already a process group leader.
1393 */
1394 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1395 if (errno != EPERM) {
1396 pdie("setpgid(0, 0)");
1397 }
1398 }
1399
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001400 if (use_preload) {
1401 /*
1402 * Before we fork(2) and execve(2) the child process, we need
1403 * to open a pipe(2) to send the minijail configuration over.
1404 */
1405 if (setup_pipe(pipe_fds))
1406 return -EFAULT;
1407 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001408
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001409 /*
1410 * If we want to write to the child process' standard input,
1411 * create the pipe(2) now.
1412 */
1413 if (pstdin_fd) {
1414 if (pipe(stdin_fds))
1415 return -EFAULT;
1416 }
1417
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001418 /*
1419 * If we want to read from the child process' standard output,
1420 * create the pipe(2) now.
1421 */
1422 if (pstdout_fd) {
1423 if (pipe(stdout_fds))
1424 return -EFAULT;
1425 }
1426
1427 /*
1428 * If we want to read from the child process' standard error,
1429 * create the pipe(2) now.
1430 */
1431 if (pstderr_fd) {
1432 if (pipe(stderr_fds))
1433 return -EFAULT;
1434 }
1435
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001436 /*
1437 * If we want to set up a new uid/gid mapping in the user namespace,
1438 * create the pipe(2) to sync between parent and child.
1439 */
1440 if (j->flags.userns) {
1441 if (pipe(userns_pipe_fds))
1442 return -EFAULT;
1443 }
1444
Elly Jones761b7412012-06-13 15:49:52 -04001445 /* Use sys_clone() if and only if we're creating a pid namespace.
1446 *
1447 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1448 *
1449 * In multithreaded programs, there are a bunch of locks inside libc,
1450 * some of which may be held by other threads at the time that we call
1451 * minijail_run_pid(). If we call fork(), glibc does its level best to
1452 * ensure that we hold all of these locks before it calls clone()
1453 * internally and drop them after clone() returns, but when we call
1454 * sys_clone(2) directly, all that gets bypassed and we end up with a
1455 * child address space where some of libc's important locks are held by
1456 * other threads (which did not get cloned, and hence will never release
1457 * those locks). This is okay so long as we call exec() immediately
1458 * after, but a bunch of seemingly-innocent libc functions like setenv()
1459 * take locks.
1460 *
1461 * Hence, only call sys_clone() if we need to, in order to get at pid
1462 * namespacing. If we follow this path, the child's address space might
1463 * have broken locks; you may only call functions that do not acquire
1464 * any locks.
1465 *
1466 * Unfortunately, fork() acquires every lock it can get its hands on, as
1467 * previously detailed, so this function is highly likely to deadlock
1468 * later on (see "deadlock here") if we're multithreaded.
1469 *
1470 * We might hack around this by having the clone()d child (init of the
1471 * pid namespace) return directly, rather than leaving the clone()d
1472 * process hanging around to be init for the new namespace (and having
1473 * its fork()ed child return in turn), but that process would be crippled
1474 * with its libc locks potentially broken. We might try fork()ing in the
1475 * parent before we clone() to ensure that we own all the locks, but
1476 * then we have to have the forked child hanging around consuming
1477 * resources (and possibly having file descriptors / shared memory
1478 * regions / etc attached). We'd need to keep the child around to avoid
1479 * having its children get reparented to init.
1480 *
1481 * TODO(ellyjones): figure out if the "forked child hanging around"
1482 * problem is fixable or not. It would be nice if we worked in this
1483 * case.
1484 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001485 if (pid_namespace) {
1486 int clone_flags = CLONE_NEWPID | SIGCHLD;
1487 if (j->flags.userns)
1488 clone_flags |= CLONE_NEWUSER;
1489 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001490 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001491 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001492 }
Elly Jones761b7412012-06-13 15:49:52 -04001493
Elly Jonese1749eb2011-10-07 13:54:59 -04001494 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001495 if (use_preload) {
1496 free(oldenv_copy);
1497 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001498 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001499 }
Will Drewryf89aef52011-09-16 16:48:57 -05001500
Elly Jonese1749eb2011-10-07 13:54:59 -04001501 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001502 if (use_preload) {
1503 /* Restore parent's LD_PRELOAD. */
1504 if (oldenv_copy) {
1505 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1506 free(oldenv_copy);
1507 } else {
1508 unsetenv(kLdPreloadEnvVar);
1509 }
1510 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001511 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001512
Elly Jonese1749eb2011-10-07 13:54:59 -04001513 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001514
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001515 if (j->flags.pid_file)
1516 write_pid_file(j);
1517
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001518 if (j->flags.userns)
1519 write_ugid_mappings(j, userns_pipe_fds);
1520
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001521 if (use_preload) {
1522 /* Send marshalled minijail. */
1523 close(pipe_fds[0]); /* read endpoint */
1524 ret = minijail_to_fd(j, pipe_fds[1]);
1525 close(pipe_fds[1]); /* write endpoint */
1526 if (ret) {
1527 kill(j->initpid, SIGKILL);
1528 die("failed to send marshalled minijail");
1529 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001530 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001531
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001532 if (pchild_pid)
1533 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001534
1535 /*
1536 * If we want to write to the child process' standard input,
1537 * set up the write end of the pipe.
1538 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001539 if (pstdin_fd)
1540 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001541 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001542
1543 /*
1544 * If we want to read from the child process' standard output,
1545 * set up the read end of the pipe.
1546 */
1547 if (pstdout_fd)
1548 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001549 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001550
1551 /*
1552 * If we want to read from the child process' standard error,
1553 * set up the read end of the pipe.
1554 */
1555 if (pstderr_fd)
1556 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001557 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001558
Elly Jonese1749eb2011-10-07 13:54:59 -04001559 return 0;
1560 }
1561 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001562
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001563 if (j->flags.userns)
1564 enter_user_namespace(j, userns_pipe_fds);
1565
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001566 /*
1567 * If we want to write to the jailed process' standard input,
1568 * set up the read end of the pipe.
1569 */
1570 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001571 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1572 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001573 die("failed to set up stdin pipe");
1574 }
1575
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001576 /*
1577 * If we want to read from the jailed process' standard output,
1578 * set up the write end of the pipe.
1579 */
1580 if (pstdout_fd) {
1581 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1582 STDOUT_FILENO) < 0)
1583 die("failed to set up stdout pipe");
1584 }
1585
1586 /*
1587 * If we want to read from the jailed process' standard error,
1588 * set up the write end of the pipe.
1589 */
1590 if (pstderr_fd) {
1591 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1592 STDERR_FILENO) < 0)
1593 die("failed to set up stderr pipe");
1594 }
1595
Dylan Reid791f5772015-09-14 20:02:42 -07001596 /* If running an init program, let it decide when/how to mount /proc. */
1597 if (pid_namespace && !do_init)
1598 j->flags.remount_proc_ro = 0;
1599
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001600 if (use_preload) {
1601 /* Strip out flags that cannot be inherited across execve(2). */
1602 minijail_preexec(j);
1603 } else {
1604 j->flags.pids = 0;
1605 }
1606 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001607 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001608
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001609 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001610 /*
1611 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001612 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001613 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001614 * a child to actually run the program. If |do_init == 0|, we
1615 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001616 *
1617 * If we're multithreaded, we'll probably deadlock here. See
1618 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001619 */
1620 child_pid = fork();
1621 if (child_pid < 0)
1622 _exit(child_pid);
1623 else if (child_pid > 0)
1624 init(child_pid); /* never returns */
1625 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001626
Elly Jonesdd3e8512012-01-23 15:13:38 -05001627 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001628 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001629 * calling process
1630 * -> execve()-ing process
1631 * If we are:
1632 * calling process
1633 * -> init()-ing process
1634 * -> execve()-ing process
1635 */
1636 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001637}
1638
Will Drewry6ac91122011-10-21 16:38:58 -05001639int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001640{
1641 int st;
1642 if (kill(j->initpid, SIGTERM))
1643 return -errno;
1644 if (waitpid(j->initpid, &st, 0) < 0)
1645 return -errno;
1646 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001647}
1648
Will Drewry6ac91122011-10-21 16:38:58 -05001649int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001650{
1651 int st;
1652 if (waitpid(j->initpid, &st, 0) < 0)
1653 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001654
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001655 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001656 int error_status = st;
1657 if (WIFSIGNALED(st)) {
1658 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001659 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001660 j->initpid, signum);
1661 /*
1662 * We return MINIJAIL_ERR_JAIL if the process received
1663 * SIGSYS, which happens when a syscall is blocked by
1664 * seccomp filters.
1665 * If not, we do what bash(1) does:
1666 * $? = 128 + signum
1667 */
1668 if (signum == SIGSYS) {
1669 error_status = MINIJAIL_ERR_JAIL;
1670 } else {
1671 error_status = 128 + signum;
1672 }
1673 }
1674 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001675 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001676
1677 int exit_status = WEXITSTATUS(st);
1678 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001679 info("child process %d exited with status %d",
1680 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001681
1682 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001683}
1684
Will Drewry6ac91122011-10-21 16:38:58 -05001685void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001686{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001687 if (j->flags.seccomp_filter && j->filter_prog) {
1688 free(j->filter_prog->filter);
1689 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001690 }
Dylan Reid648b2202015-10-23 00:50:00 -07001691 while (j->mounts_head) {
1692 struct mountpoint *m = j->mounts_head;
1693 j->mounts_head = j->mounts_head->next;
1694 free(m->type);
1695 free(m->dest);
1696 free(m->src);
1697 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001698 }
Dylan Reid648b2202015-10-23 00:50:00 -07001699 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001700 if (j->user)
1701 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001702 if (j->chrootdir)
1703 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001704 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001705}