blob: 47b4b3f77c832d3ae4c042c7c6885f0f272ca666 [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
Elly Jones51a5b6c2011-10-12 19:09:26 -040070struct binding {
71 char *src;
72 char *dest;
73 int writeable;
74 struct binding *next;
75};
76
Will Drewryf89aef52011-09-16 16:48:57 -050077struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070078 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070079 * WARNING: if you add a flag here you need to make sure it's
80 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070081 */
Elly Jonese1749eb2011-10-07 13:54:59 -040082 struct {
83 int uid:1;
84 int gid:1;
85 int caps:1;
86 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070087 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040088 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040089 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -070090 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080091 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040092 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -070093 int remount_proc_ro:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040094 int usergroups:1;
95 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070096 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040097 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070098 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040099 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800100 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700101 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800102 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800103 int pid_file:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400104 } flags;
105 uid_t uid;
106 gid_t gid;
107 gid_t usergid;
108 char *user;
109 uint64_t caps;
110 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700111 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700112 int netns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800113 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400114 int binding_count;
115 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;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400120 struct binding *bindings_head;
121 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500122};
123
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700124/*
125 * Strip out flags meant for the parent.
126 * We keep things that are not inherited across execve(2) (e.g. capabilities),
127 * or are easier to set after execve(2) (e.g. seccomp filters).
128 */
129void minijail_preenter(struct minijail *j)
130{
131 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700132 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700133 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700134 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800135 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800136 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700137}
138
139/*
140 * Strip out flags meant for the child.
141 * We keep things that are inherited across execve(2).
142 */
143void minijail_preexec(struct minijail *j)
144{
145 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700146 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700147 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800148 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700149 if (j->user)
150 free(j->user);
151 j->user = NULL;
152 memset(&j->flags, 0, sizeof(j->flags));
153 /* Now restore anything we meant to keep. */
154 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700155 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700156 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800157 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700158 /* Note, |pids| will already have been used before this call. */
159}
160
161/* Minijail API. */
162
Will Drewry6ac91122011-10-21 16:38:58 -0500163struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400164{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400165 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400166}
167
Will Drewry6ac91122011-10-21 16:38:58 -0500168void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400169{
170 if (uid == 0)
171 die("useless change to uid 0");
172 j->uid = uid;
173 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400174}
175
Will Drewry6ac91122011-10-21 16:38:58 -0500176void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400177{
178 if (gid == 0)
179 die("useless change to gid 0");
180 j->gid = gid;
181 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400182}
183
Will Drewry6ac91122011-10-21 16:38:58 -0500184int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400185{
186 char *buf = NULL;
187 struct passwd pw;
188 struct passwd *ppw = NULL;
189 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
190 if (sz == -1)
191 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400192
Elly Jonesdd3e8512012-01-23 15:13:38 -0500193 /*
194 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400195 * the maximum needed size of the buffer, so we don't have to search.
196 */
197 buf = malloc(sz);
198 if (!buf)
199 return -ENOMEM;
200 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500201 /*
202 * We're safe to free the buffer here. The strings inside pw point
203 * inside buf, but we don't use any of them; this leaves the pointers
204 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
205 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400206 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700207 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400208 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700209 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400210 minijail_change_uid(j, ppw->pw_uid);
211 j->user = strdup(user);
212 if (!j->user)
213 return -ENOMEM;
214 j->usergid = ppw->pw_gid;
215 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400216}
217
Will Drewry6ac91122011-10-21 16:38:58 -0500218int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400219{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700220 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700221 struct group gr;
222 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400223 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
224 if (sz == -1)
225 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400226
Elly Jonesdd3e8512012-01-23 15:13:38 -0500227 /*
228 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400229 * the maximum needed size of the buffer, so we don't have to search.
230 */
231 buf = malloc(sz);
232 if (!buf)
233 return -ENOMEM;
234 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500235 /*
236 * We're safe to free the buffer here. The strings inside gr point
237 * inside buf, but we don't use any of them; this leaves the pointers
238 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
239 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400240 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700241 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400242 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700243 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400244 minijail_change_gid(j, pgr->gr_gid);
245 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400246}
247
Will Drewry6ac91122011-10-21 16:38:58 -0500248void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400249{
250 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400251}
252
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700253void API minijail_no_new_privs(struct minijail *j)
254{
255 j->flags.no_new_privs = 1;
256}
257
Will Drewry6ac91122011-10-21 16:38:58 -0500258void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400259{
260 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500261}
262
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700263void API minijail_log_seccomp_filter_failures(struct minijail *j)
264{
265 j->flags.log_seccomp_filter = 1;
266}
267
Will Drewry6ac91122011-10-21 16:38:58 -0500268void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400269{
270 j->caps = capmask;
271 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400272}
273
Will Drewry6ac91122011-10-21 16:38:58 -0500274void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400275{
276 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400277}
278
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700279void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
280{
281 int ns_fd = open(ns_path, O_RDONLY);
282 if (ns_fd < 0) {
283 pdie("failed to open namespace '%s'", ns_path);
284 }
285 j->mountns_fd = ns_fd;
286 j->flags.enter_vfs = 1;
287}
288
Will Drewry6ac91122011-10-21 16:38:58 -0500289void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400290{
Elly Jonese58176c2012-01-23 11:46:17 -0500291 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700292 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400293 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800294 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400295}
296
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400297void API minijail_namespace_net(struct minijail *j)
298{
299 j->flags.net = 1;
300}
301
Dylan Reid1102f5a2015-09-15 11:52:20 -0700302void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
303{
304 int ns_fd = open(ns_path, O_RDONLY);
305 if (ns_fd < 0) {
306 pdie("failed to open namespace '%s'", ns_path);
307 }
308 j->netns_fd = ns_fd;
309 j->flags.enter_net = 1;
310}
311
Dylan Reid791f5772015-09-14 20:02:42 -0700312void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400313{
314 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700315 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400316}
317
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800318void API minijail_namespace_user(struct minijail *j)
319{
320 j->flags.userns = 1;
321}
322
323int API minijail_uidmap(struct minijail *j, const char *uidmap)
324{
325 j->uidmap = strdup(uidmap);
326 if (!j->uidmap)
327 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800328 char *ch;
329 for (ch = j->uidmap; *ch; ch++) {
330 if (*ch == ',')
331 *ch = '\n';
332 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800333 return 0;
334}
335
336int API minijail_gidmap(struct minijail *j, const char *gidmap)
337{
338 j->gidmap = strdup(gidmap);
339 if (!j->gidmap)
340 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800341 char *ch;
342 for (ch = j->gidmap; *ch; ch++) {
343 if (*ch == ',')
344 *ch = '\n';
345 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800346 return 0;
347}
348
Will Drewry6ac91122011-10-21 16:38:58 -0500349void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400350{
351 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400352}
353
Will Drewry6ac91122011-10-21 16:38:58 -0500354void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400355{
356 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400357}
358
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800359void API minijail_run_as_init(struct minijail *j)
360{
361 /*
362 * Since the jailed program will become 'init' in the new PID namespace,
363 * Minijail does not need to fork an 'init' process.
364 */
365 j->flags.do_init = 0;
366}
367
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700368int API minijail_enter_chroot(struct minijail *j, const char *dir)
369{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400370 if (j->chrootdir)
371 return -EINVAL;
372 j->chrootdir = strdup(dir);
373 if (!j->chrootdir)
374 return -ENOMEM;
375 j->flags.chroot = 1;
376 return 0;
377}
378
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800379int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
380{
381 if (j->chrootdir)
382 return -EINVAL;
383 j->chrootdir = strdup(dir);
384 if (!j->chrootdir)
385 return -ENOMEM;
386 j->flags.pivot_root = 1;
387 return 0;
388}
389
Dylan Reida14e08d2015-10-22 21:05:29 -0700390static char *append_external_path(const char *external_path,
391 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700392{
Dylan Reida14e08d2015-10-22 21:05:29 -0700393 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700394 size_t pathlen;
395
Dylan Reid08946cc2015-09-16 19:10:57 -0700396 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700397 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
398 path = malloc(pathlen);
399 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700400
Dylan Reida14e08d2015-10-22 21:05:29 -0700401 return path;
402}
403
404char API *minijail_get_original_path(struct minijail *j,
405 const char *path_inside_chroot)
406{
407 struct binding *b;
408
409 b = j->bindings_head;
410 while (b) {
411 /*
412 * If |path_inside_chroot| is the exact destination of a
413 * bind mount, then the original path is exactly the source of
414 * the bind mount.
415 * for example: "-b /some/path/exe,/chroot/path/exe"
416 * bind source = /some/path/exe, bind dest = /chroot/path/exe
417 * Then when getting the original path of "/chroot/path/exe",
418 * the source of that bind mount, "/some/path/exe" is what
419 * should be returned.
420 */
421 if (!strcmp(b->dest, path_inside_chroot))
422 return strdup(b->src);
423
424 /*
425 * If |path_inside_chroot| is within the destination path of a
426 * bind mount, take the suffix of the chroot path relative to
427 * the bind mount destination path, and append it to the bind
428 * mount source path.
429 */
430 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
431 const char *relative_path =
432 path_inside_chroot + strlen(b->dest);
433 return append_external_path(b->src, relative_path);
434 }
435 b = b->next;
436 }
437
438 /* If there is a chroot path, append |path_inside_chroot| to that. */
439 if (j->chrootdir)
440 return append_external_path(j->chrootdir, path_inside_chroot);
441
442 /* No chroot, so the path outside is the same as it is inside. */
443 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700444}
445
Lee Campbell11af0622014-05-22 12:36:04 -0700446void API minijail_mount_tmp(struct minijail *j)
447{
448 j->flags.mount_tmp = 1;
449}
450
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800451int API minijail_write_pid_file(struct minijail *j, const char *path)
452{
453 j->pid_file_path = strdup(path);
454 if (!j->pid_file_path)
455 return -ENOMEM;
456 j->flags.pid_file = 1;
457 return 0;
458}
459
Will Drewry6ac91122011-10-21 16:38:58 -0500460int API minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700461 int writeable)
462{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400463 struct binding *b;
464
465 if (*dest != '/')
466 return -EINVAL;
467 b = calloc(1, sizeof(*b));
468 if (!b)
469 return -ENOMEM;
470 b->dest = strdup(dest);
471 if (!b->dest)
472 goto error;
473 b->src = strdup(src);
474 if (!b->src)
475 goto error;
476 b->writeable = writeable;
477
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700478 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400479
Elly Jonesdd3e8512012-01-23 15:13:38 -0500480 /*
481 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400482 * containing vfs namespace.
483 */
484 minijail_namespace_vfs(j);
485
486 if (j->bindings_tail)
487 j->bindings_tail->next = b;
488 else
489 j->bindings_head = b;
490 j->bindings_tail = b;
491 j->binding_count++;
492
493 return 0;
494
495error:
496 free(b->src);
497 free(b->dest);
498 free(b);
499 return -ENOMEM;
500}
501
Dylan Reid08946cc2015-09-16 19:10:57 -0700502int API minijail_has_bind_mounts(const struct minijail *j)
503{
504 return j->bindings_head != NULL;
505}
506
Will Drewry6ac91122011-10-21 16:38:58 -0500507void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400508{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700509 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
510 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
511 warn("not loading seccomp filter, seccomp not supported");
512 return;
513 }
514 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400515 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800516 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700517 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400518 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800519
520 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700521 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
522 die("failed to compile seccomp filter BPF program in '%s'",
523 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800524 }
525
526 j->filter_len = fprog->len;
527 j->filter_prog = fprog;
528
Elly Jonese1749eb2011-10-07 13:54:59 -0400529 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500530}
531
Will Drewryf89aef52011-09-16 16:48:57 -0500532struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400533 size_t available;
534 size_t total;
535 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500536};
537
Will Drewry6ac91122011-10-21 16:38:58 -0500538void marshal_state_init(struct marshal_state *state,
539 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400540{
541 state->available = available;
542 state->buf = buf;
543 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500544}
545
Will Drewry6ac91122011-10-21 16:38:58 -0500546void marshal_append(struct marshal_state *state,
547 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400548{
549 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500550
Elly Jonese1749eb2011-10-07 13:54:59 -0400551 /* Up to |available| will be written. */
552 if (copy_len) {
553 memcpy(state->buf, src, copy_len);
554 state->buf += copy_len;
555 state->available -= copy_len;
556 }
557 /* |total| will contain the expected length. */
558 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500559}
560
Will Drewry6ac91122011-10-21 16:38:58 -0500561void minijail_marshal_helper(struct marshal_state *state,
562 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400563{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400564 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400565 marshal_append(state, (char *)j, sizeof(*j));
566 if (j->user)
567 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400568 if (j->chrootdir)
569 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800570 if (j->flags.seccomp_filter && j->filter_prog) {
571 struct sock_fprog *fp = j->filter_prog;
572 marshal_append(state, (char *)fp->filter,
573 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400574 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400575 for (b = j->bindings_head; b; b = b->next) {
576 marshal_append(state, b->src, strlen(b->src) + 1);
577 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700578 marshal_append(state, (char *)&b->writeable,
579 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400580 }
Will Drewryf89aef52011-09-16 16:48:57 -0500581}
582
Will Drewry6ac91122011-10-21 16:38:58 -0500583size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400584{
585 struct marshal_state state;
586 marshal_state_init(&state, NULL, 0);
587 minijail_marshal_helper(&state, j);
588 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500589}
590
Elly Jonese1749eb2011-10-07 13:54:59 -0400591int minijail_marshal(const struct minijail *j, char *buf, size_t available)
592{
593 struct marshal_state state;
594 marshal_state_init(&state, buf, available);
595 minijail_marshal_helper(&state, j);
596 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500597}
598
Elly Jones51a5b6c2011-10-12 19:09:26 -0400599/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
600 * @length Number of bytes to consume
601 * @buf Buffer to consume from
602 * @buflength Size of @buf
603 *
604 * Returns a pointer to the base of the bytes, or NULL for errors.
605 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700606void *consumebytes(size_t length, char **buf, size_t *buflength)
607{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400608 char *p = *buf;
609 if (length > *buflength)
610 return NULL;
611 *buf += length;
612 *buflength -= length;
613 return p;
614}
615
616/* consumestr: consumes a C string from a buffer @buf of length @length
617 * @buf Buffer to consume
618 * @length Length of buffer
619 *
620 * Returns a pointer to the base of the string, or NULL for errors.
621 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700622char *consumestr(char **buf, size_t *buflength)
623{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400624 size_t len = strnlen(*buf, *buflength);
625 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700626 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400627 return NULL;
628 return consumebytes(len + 1, buf, buflength);
629}
630
Elly Jonese1749eb2011-10-07 13:54:59 -0400631int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
632{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400633 int i;
634 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500635 int ret = -EINVAL;
636
Elly Jonese1749eb2011-10-07 13:54:59 -0400637 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500638 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400639 memcpy((void *)j, serialized, sizeof(*j));
640 serialized += sizeof(*j);
641 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500642
Will Drewrybee7ba72011-10-21 20:47:01 -0500643 /* Potentially stale pointers not used as signals. */
644 j->bindings_head = NULL;
645 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800646 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500647
Elly Jonese1749eb2011-10-07 13:54:59 -0400648 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400649 char *user = consumestr(&serialized, &length);
650 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500651 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400652 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500653 if (!j->user)
654 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400655 }
Will Drewryf89aef52011-09-16 16:48:57 -0500656
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400657 if (j->chrootdir) { /* stale pointer */
658 char *chrootdir = consumestr(&serialized, &length);
659 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500660 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400661 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500662 if (!j->chrootdir)
663 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400664 }
665
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800666 if (j->flags.seccomp_filter && j->filter_len > 0) {
667 size_t ninstrs = j->filter_len;
668 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
669 ninstrs > USHRT_MAX)
670 goto bad_filters;
671
672 size_t program_len = ninstrs * sizeof(struct sock_filter);
673 void *program = consumebytes(program_len, &serialized, &length);
674 if (!program)
675 goto bad_filters;
676
677 j->filter_prog = malloc(sizeof(struct sock_fprog));
678 j->filter_prog->len = ninstrs;
679 j->filter_prog->filter = malloc(program_len);
680 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400681 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400682
683 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400684 j->binding_count = 0;
685 for (i = 0; i < count; ++i) {
686 int *writeable;
687 const char *dest;
688 const char *src = consumestr(&serialized, &length);
689 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500690 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400691 dest = consumestr(&serialized, &length);
692 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500693 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400694 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
695 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500696 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400697 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500698 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400699 }
700
Elly Jonese1749eb2011-10-07 13:54:59 -0400701 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500702
703bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800704 if (j->flags.seccomp_filter && j->filter_len > 0) {
705 free(j->filter_prog->filter);
706 free(j->filter_prog);
707 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500708bad_filters:
709 if (j->chrootdir)
710 free(j->chrootdir);
711bad_chrootdir:
712 if (j->user)
713 free(j->user);
714clear_pointers:
715 j->user = NULL;
716 j->chrootdir = NULL;
717out:
718 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500719}
720
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800721static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
722{
723 int fd, ret, len;
724 size_t sz;
725 char fname[32];
726 close(pipe_fds[0]);
727
728 sz = sizeof(fname);
729 if (j->uidmap) {
730 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700731 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800732 die("failed to write file name of uid_map");
733 fd = open(fname, O_WRONLY);
734 if (fd < 0)
735 pdie("failed to open '%s'", fname);
736 len = strlen(j->uidmap);
737 if (write(fd, j->uidmap, len) < len)
738 die("failed to set uid_map");
739 close(fd);
740 }
741 if (j->gidmap) {
742 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700743 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800744 die("failed to write file name of gid_map");
745 fd = open(fname, O_WRONLY);
746 if (fd < 0)
747 pdie("failed to open '%s'", fname);
748 len = strlen(j->gidmap);
749 if (write(fd, j->gidmap, len) < len)
750 die("failed to set gid_map");
751 close(fd);
752 }
753
754 close(pipe_fds[1]);
755}
756
757static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
758{
759 char buf;
760
761 close(pipe_fds[1]);
762
763 /* Wait for parent to set up uid/gid mappings. */
764 if (read(pipe_fds[0], &buf, 1) != 0)
765 die("failed to sync with parent");
766 close(pipe_fds[0]);
767
768 if (j->uidmap && setresuid(0, 0, 0))
769 pdie("setresuid");
770 if (j->gidmap && setresgid(0, 0, 0))
771 pdie("setresgid");
772}
773
Elly Jones51a5b6c2011-10-12 19:09:26 -0400774/* bind_one: Applies bindings from @b for @j, recursing as needed.
775 * @j Minijail these bindings are for
776 * @b Head of list of bindings
777 *
778 * Returns 0 for success.
779 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700780int bind_one(const struct minijail *j, struct binding *b)
781{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400782 int ret = 0;
783 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400784 if (ret)
785 return ret;
786 /* dest has a leading "/" */
787 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
788 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500789 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400790 if (ret)
791 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500792 if (!b->writeable) {
793 ret = mount(b->src, dest, NULL,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700794 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
Elly Jonesa1059632011-12-15 15:17:07 -0500795 if (ret)
796 pdie("bind ro: %s -> %s", b->src, dest);
797 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400798 free(dest);
799 if (b->next)
800 return bind_one(j, b->next);
801 return ret;
802}
803
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700804int enter_chroot(const struct minijail *j)
805{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400806 int ret;
807 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
808 return ret;
809
810 if (chroot(j->chrootdir))
811 return -errno;
812
813 if (chdir("/"))
814 return -errno;
815
816 return 0;
817}
818
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800819int enter_pivot_root(const struct minijail *j)
820{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800821 int ret, oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800822 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
823 return ret;
824
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800825 /* Keep the fd for both old and new root. It will be used in fchdir later. */
826 oldroot = open("/", O_DIRECTORY | O_RDONLY);
827 if (oldroot < 0)
828 pdie("failed to open / for fchdir");
829 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
830 if (newroot < 0)
831 pdie("failed to open %s for fchdir", j->chrootdir);
832
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800833 /* To ensure chrootdir is the root of a file system, do a self bind mount. */
834 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
835 pdie("failed to bind mount '%s'", j->chrootdir);
836 if (chdir(j->chrootdir))
837 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800838 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800839 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800840
841 /*
842 * Now the old root is mounted on top of the new root. Use fchdir to
843 * change to the old root and unmount it.
844 */
845 if (fchdir(oldroot))
846 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800847 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800848 if (umount2(".", MNT_DETACH))
849 pdie("umount(/)");
850 /* Change back to the new root. */
851 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800852 return -errno;
853 if (chroot("/"))
854 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -0700855 /* Set correct CWD for getcwd(3). */
856 if (chdir("/"))
857 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800858
859 return 0;
860}
861
Lee Campbell11af0622014-05-22 12:36:04 -0700862int mount_tmp(void)
863{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800864 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700865}
866
Dylan Reid791f5772015-09-14 20:02:42 -0700867int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400868{
869 const char *kProcPath = "/proc";
870 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500871 /*
872 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400873 * /proc in our namespace, which means using MS_REMOUNT here would
874 * mutate our parent's mount as well, even though we're in a VFS
875 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800876 * and make our own. However, if we are in a new user namespace, /proc
877 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400878 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -0700879 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400880 return -errno;
881 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
882 return -errno;
883 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400884}
885
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800886static void write_pid_file(const struct minijail *j)
887{
888 FILE *fp = fopen(j->pid_file_path, "w");
889
890 if (!fp)
891 pdie("failed to open '%s'", j->pid_file_path);
892 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
893 pdie("fprintf(%s)", j->pid_file_path);
894 if (fclose(fp))
895 pdie("fclose(%s)", j->pid_file_path);
896}
897
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700898void drop_ugid(const struct minijail *j)
899{
900 if (j->flags.usergroups) {
901 if (initgroups(j->user, j->usergid))
902 pdie("initgroups");
903 } else {
904 /* Only attempt to clear supplemental groups if we are changing
905 * users. */
906 if ((j->uid || j->gid) && setgroups(0, NULL))
907 pdie("setgroups");
908 }
909
910 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
911 pdie("setresgid");
912
913 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
914 pdie("setresuid");
915}
916
Mike Frysinger3adfef72013-05-09 17:19:08 -0400917/*
918 * We specifically do not use cap_valid() as that only tells us the last
919 * valid cap we were *compiled* against (i.e. what the version of kernel
920 * headers says). If we run on a different kernel version, then it's not
921 * uncommon for that to be less (if an older kernel) or more (if a newer
922 * kernel). So suck up the answer via /proc.
923 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700924static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -0400925{
Dylan Reidf682d472015-09-17 21:39:07 -0700926 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
927 FILE *fp = fopen(cap_file, "re");
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700928 unsigned int last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -0400929
Dylan Reidf682d472015-09-17 21:39:07 -0700930 if (fscanf(fp, "%u", &last_valid_cap) != 1)
931 pdie("fscanf(%s)", cap_file);
932 fclose(fp);
Mike Frysinger3adfef72013-05-09 17:19:08 -0400933
Dylan Reidf682d472015-09-17 21:39:07 -0700934 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -0400935}
936
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700937void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -0400938{
939 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800940 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800941 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400942 unsigned int i;
943 if (!caps)
944 die("can't get process caps");
945 if (cap_clear_flag(caps, CAP_INHERITABLE))
946 die("can't clear inheritable caps");
947 if (cap_clear_flag(caps, CAP_EFFECTIVE))
948 die("can't clear effective caps");
949 if (cap_clear_flag(caps, CAP_PERMITTED))
950 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -0700951 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800952 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800953 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400954 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800955 flag[0] = i;
956 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400957 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800958 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400959 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800960 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400961 die("can't add inheritable cap");
962 }
963 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800964 die("can't apply initial cleaned capset");
965
966 /*
967 * Instead of dropping bounding set first, do it here in case
968 * the caller had a more permissive bounding set which could
969 * have been used above to raise a capability that wasn't already
970 * present. This requires CAP_SETPCAP, so we raised/kept it above.
971 */
Dylan Reidf682d472015-09-17 21:39:07 -0700972 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800973 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400974 continue;
975 if (prctl(PR_CAPBSET_DROP, i))
976 pdie("prctl(PR_CAPBSET_DROP)");
977 }
Kees Cook323878a2013-02-05 15:35:24 -0800978
979 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800980 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800981 flag[0] = CAP_SETPCAP;
982 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
983 die("can't clear effective cap");
984 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
985 die("can't clear permitted cap");
986 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
987 die("can't clear inheritable cap");
988 }
989
990 if (cap_set_proc(caps))
991 die("can't apply final cleaned capset");
992
993 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400994}
995
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700996void set_seccomp_filter(const struct minijail *j)
997{
998 /*
999 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1000 * in the kernel source tree for an explanation of the parameters.
1001 */
1002 if (j->flags.no_new_privs) {
1003 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1004 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1005 }
1006
1007 /*
1008 * If we're logging seccomp filter failures,
1009 * install the SIGSYS handler first.
1010 */
1011 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1012 if (install_sigsys_handler())
1013 pdie("install SIGSYS handler");
1014 warn("logging seccomp filter failures");
1015 }
1016
1017 /*
1018 * Install the syscall filter.
1019 */
1020 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001021 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
1022 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1023 warn("seccomp not supported");
1024 return;
1025 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001026 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001027 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001028 }
1029}
1030
Will Drewry6ac91122011-10-21 16:38:58 -05001031void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001032{
Dylan Reidf682d472015-09-17 21:39:07 -07001033 /*
1034 * Get the last valid cap from /proc, since /proc can be unmounted
1035 * before drop_caps().
1036 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001037 unsigned int last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001038
Elly Jonese1749eb2011-10-07 13:54:59 -04001039 if (j->flags.pids)
1040 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001041 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001042
Elly Jonese1749eb2011-10-07 13:54:59 -04001043 if (j->flags.usergroups && !j->user)
1044 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001045
Elly Jonesdd3e8512012-01-23 15:13:38 -05001046 /*
1047 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001048 * so we don't even try. If any of our operations fail, we abort() the
1049 * entire process.
1050 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001051 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1052 pdie("setns(CLONE_NEWNS)");
1053
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001054 if (j->flags.vfs) {
1055 if (unshare(CLONE_NEWNS))
1056 pdie("unshare(vfs)");
1057 /*
1058 * Remount all filesystems as private. If they are shared
1059 * new bind mounts will creep out of our namespace.
1060 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1061 */
1062 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1063 pdie("mount(/, private)");
1064 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001065
Dylan Reid1102f5a2015-09-15 11:52:20 -07001066 if (j->flags.enter_net) {
1067 if (setns(j->netns_fd, CLONE_NEWNET))
1068 pdie("setns(CLONE_NEWNET)");
1069 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001070 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001071 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001072
Elly Jones51a5b6c2011-10-12 19:09:26 -04001073 if (j->flags.chroot && enter_chroot(j))
1074 pdie("chroot");
1075
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001076 if (j->flags.pivot_root && enter_pivot_root(j))
1077 pdie("pivot_root");
1078
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001079 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001080 pdie("mount_tmp");
1081
Dylan Reid791f5772015-09-14 20:02:42 -07001082 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001083 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001084
Elly Jonese1749eb2011-10-07 13:54:59 -04001085 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001086 /*
1087 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001088 * capability to change uids, our attempt to use setuid()
1089 * below will fail. Hang on to root caps across setuid(), then
1090 * lock securebits.
1091 */
1092 if (prctl(PR_SET_KEEPCAPS, 1))
1093 pdie("prctl(PR_SET_KEEPCAPS)");
1094 if (prctl
1095 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1096 pdie("prctl(PR_SET_SECUREBITS)");
1097 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001098
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001099 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001100 * If we're setting no_new_privs, we can drop privileges
1101 * before setting seccomp filter. This way filter policies
1102 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001103 */
1104 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001105 drop_ugid(j);
1106 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001107 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001108
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001109 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001110 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001111 /*
1112 * If we're not setting no_new_privs,
1113 * we need to set seccomp filter *before* dropping privileges.
1114 * WARNING: this means that filter policies *must* allow
1115 * setgroups()/setresgid()/setresuid() for dropping root and
1116 * capget()/capset()/prctl() for dropping caps.
1117 */
1118 set_seccomp_filter(j);
1119
1120 drop_ugid(j);
1121 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001122 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001123 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001124
Elly Jonesdd3e8512012-01-23 15:13:38 -05001125 /*
1126 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001127 * privilege-dropping syscalls :)
1128 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001129 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1130 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1131 warn("seccomp not supported");
1132 return;
1133 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001134 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001135 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001136}
1137
Will Drewry6ac91122011-10-21 16:38:58 -05001138/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001139static int init_exitstatus = 0;
1140
Will Drewry6ac91122011-10-21 16:38:58 -05001141void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001142{
1143 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001144}
1145
Will Drewry6ac91122011-10-21 16:38:58 -05001146int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001147{
1148 pid_t pid;
1149 int status;
1150 /* so that we exit with the right status */
1151 signal(SIGTERM, init_term);
1152 /* TODO(wad) self jail with seccomp_filters here. */
1153 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001154 /*
1155 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001156 * left inside our pid namespace or we get a signal.
1157 */
1158 if (pid == rootpid)
1159 init_exitstatus = status;
1160 }
1161 if (!WIFEXITED(init_exitstatus))
1162 _exit(MINIJAIL_ERR_INIT);
1163 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001164}
1165
Will Drewry6ac91122011-10-21 16:38:58 -05001166int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001167{
1168 size_t sz = 0;
1169 size_t bytes = read(fd, &sz, sizeof(sz));
1170 char *buf;
1171 int r;
1172 if (sizeof(sz) != bytes)
1173 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001174 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001175 return -E2BIG;
1176 buf = malloc(sz);
1177 if (!buf)
1178 return -ENOMEM;
1179 bytes = read(fd, buf, sz);
1180 if (bytes != sz) {
1181 free(buf);
1182 return -EINVAL;
1183 }
1184 r = minijail_unmarshal(j, buf, sz);
1185 free(buf);
1186 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001187}
1188
Will Drewry6ac91122011-10-21 16:38:58 -05001189int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001190{
1191 char *buf;
1192 size_t sz = minijail_size(j);
1193 ssize_t written;
1194 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001195
Elly Jonese1749eb2011-10-07 13:54:59 -04001196 if (!sz)
1197 return -EINVAL;
1198 buf = malloc(sz);
1199 r = minijail_marshal(j, buf, sz);
1200 if (r) {
1201 free(buf);
1202 return r;
1203 }
1204 /* Sends [size][minijail]. */
1205 written = write(fd, &sz, sizeof(sz));
1206 if (written != sizeof(sz)) {
1207 free(buf);
1208 return -EFAULT;
1209 }
1210 written = write(fd, buf, sz);
1211 if (written < 0 || (size_t) written != sz) {
1212 free(buf);
1213 return -EFAULT;
1214 }
1215 free(buf);
1216 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001217}
Elly Jonescd7a9042011-07-22 13:56:51 -04001218
Will Drewry6ac91122011-10-21 16:38:58 -05001219int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001220{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001221#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001222 /* Don't use LDPRELOAD on Brillo. */
1223 return 0;
1224#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001225 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1226 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1227 if (!newenv)
1228 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001229
Elly Jonese1749eb2011-10-07 13:54:59 -04001230 /* Only insert a separating space if we have something to separate... */
1231 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1232 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001233
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001234 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001235 setenv(kLdPreloadEnvVar, newenv, 1);
1236 free(newenv);
1237 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001238#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001239}
1240
Will Drewry6ac91122011-10-21 16:38:58 -05001241int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001242{
1243 int r = pipe(fds);
1244 char fd_buf[11];
1245 if (r)
1246 return r;
1247 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1248 if (r <= 0)
1249 return -EINVAL;
1250 setenv(kFdEnvVar, fd_buf, 1);
1251 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001252}
1253
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001254int setup_pipe_end(int fds[2], size_t index)
1255{
1256 if (index > 1)
1257 return -1;
1258
1259 close(fds[1 - index]);
1260 return fds[index];
1261}
1262
1263int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1264{
1265 if (index > 1)
1266 return -1;
1267
1268 close(fds[1 - index]);
1269 /* dup2(2) the corresponding end of the pipe into |fd|. */
1270 return dup2(fds[index], fd);
1271}
1272
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001273int minijail_run_internal(struct minijail *j, const char *filename,
1274 char *const argv[], pid_t *pchild_pid,
1275 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1276 int use_preload);
1277
Will Drewry6ac91122011-10-21 16:38:58 -05001278int API minijail_run(struct minijail *j, const char *filename,
1279 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001280{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001281 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1282 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001283}
1284
1285int API minijail_run_pid(struct minijail *j, const char *filename,
1286 char *const argv[], pid_t *pchild_pid)
1287{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001288 return minijail_run_internal(j, filename, argv, pchild_pid,
1289 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001290}
1291
1292int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001293 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001294{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001295 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1296 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001297}
1298
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001299int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001300 char *const argv[], pid_t *pchild_pid,
1301 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001302{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001303 return minijail_run_internal(j, filename, argv, pchild_pid,
1304 pstdin_fd, pstdout_fd, pstderr_fd, true);
1305}
1306
1307int API minijail_run_no_preload(struct minijail *j, const char *filename,
1308 char *const argv[])
1309{
1310 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1311 false);
1312}
1313
Samuel Tan63187f42015-10-16 13:01:53 -07001314int API minijail_run_pid_pipes_no_preload(struct minijail *j,
1315 const char *filename, char *const argv[],
1316 pid_t *pchild_pid,
1317 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) {
1318 return minijail_run_internal(j, filename, argv, pchild_pid,
1319 pstdin_fd, pstdout_fd, pstderr_fd, false);
1320}
1321
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001322int minijail_run_internal(struct minijail *j, const char *filename,
1323 char *const argv[], pid_t *pchild_pid,
1324 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1325 int use_preload)
1326{
Elly Jonese1749eb2011-10-07 13:54:59 -04001327 char *oldenv, *oldenv_copy = NULL;
1328 pid_t child_pid;
1329 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001330 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001331 int stdout_fds[2];
1332 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001333 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001334 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001335 /* We need to remember this across the minijail_preexec() call. */
1336 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001337 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001338
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001339 if (use_preload) {
1340 oldenv = getenv(kLdPreloadEnvVar);
1341 if (oldenv) {
1342 oldenv_copy = strdup(oldenv);
1343 if (!oldenv_copy)
1344 return -ENOMEM;
1345 }
1346
1347 if (setup_preload())
1348 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001349 }
Will Drewryf89aef52011-09-16 16:48:57 -05001350
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001351 if (!use_preload) {
1352 if (j->flags.caps)
1353 die("Capabilities are not supported without "
1354 "LD_PRELOAD");
1355 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001356
Elly Jonesdd3e8512012-01-23 15:13:38 -05001357 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001358 * Make the process group ID of this process equal to its PID, so that
1359 * both the Minijail process and the jailed process can be killed
1360 * together.
1361 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1362 * the process is already a process group leader.
1363 */
1364 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1365 if (errno != EPERM) {
1366 pdie("setpgid(0, 0)");
1367 }
1368 }
1369
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001370 if (use_preload) {
1371 /*
1372 * Before we fork(2) and execve(2) the child process, we need
1373 * to open a pipe(2) to send the minijail configuration over.
1374 */
1375 if (setup_pipe(pipe_fds))
1376 return -EFAULT;
1377 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001378
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001379 /*
1380 * If we want to write to the child process' standard input,
1381 * create the pipe(2) now.
1382 */
1383 if (pstdin_fd) {
1384 if (pipe(stdin_fds))
1385 return -EFAULT;
1386 }
1387
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001388 /*
1389 * If we want to read from the child process' standard output,
1390 * create the pipe(2) now.
1391 */
1392 if (pstdout_fd) {
1393 if (pipe(stdout_fds))
1394 return -EFAULT;
1395 }
1396
1397 /*
1398 * If we want to read from the child process' standard error,
1399 * create the pipe(2) now.
1400 */
1401 if (pstderr_fd) {
1402 if (pipe(stderr_fds))
1403 return -EFAULT;
1404 }
1405
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001406 /*
1407 * If we want to set up a new uid/gid mapping in the user namespace,
1408 * create the pipe(2) to sync between parent and child.
1409 */
1410 if (j->flags.userns) {
1411 if (pipe(userns_pipe_fds))
1412 return -EFAULT;
1413 }
1414
Elly Jones761b7412012-06-13 15:49:52 -04001415 /* Use sys_clone() if and only if we're creating a pid namespace.
1416 *
1417 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1418 *
1419 * In multithreaded programs, there are a bunch of locks inside libc,
1420 * some of which may be held by other threads at the time that we call
1421 * minijail_run_pid(). If we call fork(), glibc does its level best to
1422 * ensure that we hold all of these locks before it calls clone()
1423 * internally and drop them after clone() returns, but when we call
1424 * sys_clone(2) directly, all that gets bypassed and we end up with a
1425 * child address space where some of libc's important locks are held by
1426 * other threads (which did not get cloned, and hence will never release
1427 * those locks). This is okay so long as we call exec() immediately
1428 * after, but a bunch of seemingly-innocent libc functions like setenv()
1429 * take locks.
1430 *
1431 * Hence, only call sys_clone() if we need to, in order to get at pid
1432 * namespacing. If we follow this path, the child's address space might
1433 * have broken locks; you may only call functions that do not acquire
1434 * any locks.
1435 *
1436 * Unfortunately, fork() acquires every lock it can get its hands on, as
1437 * previously detailed, so this function is highly likely to deadlock
1438 * later on (see "deadlock here") if we're multithreaded.
1439 *
1440 * We might hack around this by having the clone()d child (init of the
1441 * pid namespace) return directly, rather than leaving the clone()d
1442 * process hanging around to be init for the new namespace (and having
1443 * its fork()ed child return in turn), but that process would be crippled
1444 * with its libc locks potentially broken. We might try fork()ing in the
1445 * parent before we clone() to ensure that we own all the locks, but
1446 * then we have to have the forked child hanging around consuming
1447 * resources (and possibly having file descriptors / shared memory
1448 * regions / etc attached). We'd need to keep the child around to avoid
1449 * having its children get reparented to init.
1450 *
1451 * TODO(ellyjones): figure out if the "forked child hanging around"
1452 * problem is fixable or not. It would be nice if we worked in this
1453 * case.
1454 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001455 if (pid_namespace) {
1456 int clone_flags = CLONE_NEWPID | SIGCHLD;
1457 if (j->flags.userns)
1458 clone_flags |= CLONE_NEWUSER;
1459 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001460 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001461 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001462 }
Elly Jones761b7412012-06-13 15:49:52 -04001463
Elly Jonese1749eb2011-10-07 13:54:59 -04001464 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001465 if (use_preload) {
1466 free(oldenv_copy);
1467 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001468 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001469 }
Will Drewryf89aef52011-09-16 16:48:57 -05001470
Elly Jonese1749eb2011-10-07 13:54:59 -04001471 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001472 if (use_preload) {
1473 /* Restore parent's LD_PRELOAD. */
1474 if (oldenv_copy) {
1475 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1476 free(oldenv_copy);
1477 } else {
1478 unsetenv(kLdPreloadEnvVar);
1479 }
1480 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001481 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001482
Elly Jonese1749eb2011-10-07 13:54:59 -04001483 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001484
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001485 if (j->flags.pid_file)
1486 write_pid_file(j);
1487
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001488 if (j->flags.userns)
1489 write_ugid_mappings(j, userns_pipe_fds);
1490
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001491 if (use_preload) {
1492 /* Send marshalled minijail. */
1493 close(pipe_fds[0]); /* read endpoint */
1494 ret = minijail_to_fd(j, pipe_fds[1]);
1495 close(pipe_fds[1]); /* write endpoint */
1496 if (ret) {
1497 kill(j->initpid, SIGKILL);
1498 die("failed to send marshalled minijail");
1499 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001500 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001501
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001502 if (pchild_pid)
1503 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001504
1505 /*
1506 * If we want to write to the child process' standard input,
1507 * set up the write end of the pipe.
1508 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001509 if (pstdin_fd)
1510 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001511 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001512
1513 /*
1514 * If we want to read from the child process' standard output,
1515 * set up the read end of the pipe.
1516 */
1517 if (pstdout_fd)
1518 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001519 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001520
1521 /*
1522 * If we want to read from the child process' standard error,
1523 * set up the read end of the pipe.
1524 */
1525 if (pstderr_fd)
1526 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001527 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001528
Elly Jonese1749eb2011-10-07 13:54:59 -04001529 return 0;
1530 }
1531 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001532
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001533 if (j->flags.userns)
1534 enter_user_namespace(j, userns_pipe_fds);
1535
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001536 /*
1537 * If we want to write to the jailed process' standard input,
1538 * set up the read end of the pipe.
1539 */
1540 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001541 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1542 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001543 die("failed to set up stdin pipe");
1544 }
1545
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001546 /*
1547 * If we want to read from the jailed process' standard output,
1548 * set up the write end of the pipe.
1549 */
1550 if (pstdout_fd) {
1551 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1552 STDOUT_FILENO) < 0)
1553 die("failed to set up stdout pipe");
1554 }
1555
1556 /*
1557 * If we want to read from the jailed process' standard error,
1558 * set up the write end of the pipe.
1559 */
1560 if (pstderr_fd) {
1561 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1562 STDERR_FILENO) < 0)
1563 die("failed to set up stderr pipe");
1564 }
1565
Dylan Reid791f5772015-09-14 20:02:42 -07001566 /* If running an init program, let it decide when/how to mount /proc. */
1567 if (pid_namespace && !do_init)
1568 j->flags.remount_proc_ro = 0;
1569
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001570 if (use_preload) {
1571 /* Strip out flags that cannot be inherited across execve(2). */
1572 minijail_preexec(j);
1573 } else {
1574 j->flags.pids = 0;
1575 }
1576 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001577 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001578
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001579 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001580 /*
1581 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001582 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001583 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001584 * a child to actually run the program. If |do_init == 0|, we
1585 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001586 *
1587 * If we're multithreaded, we'll probably deadlock here. See
1588 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001589 */
1590 child_pid = fork();
1591 if (child_pid < 0)
1592 _exit(child_pid);
1593 else if (child_pid > 0)
1594 init(child_pid); /* never returns */
1595 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001596
Elly Jonesdd3e8512012-01-23 15:13:38 -05001597 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001598 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001599 * calling process
1600 * -> execve()-ing process
1601 * If we are:
1602 * calling process
1603 * -> init()-ing process
1604 * -> execve()-ing process
1605 */
1606 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001607}
1608
Will Drewry6ac91122011-10-21 16:38:58 -05001609int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001610{
1611 int st;
1612 if (kill(j->initpid, SIGTERM))
1613 return -errno;
1614 if (waitpid(j->initpid, &st, 0) < 0)
1615 return -errno;
1616 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001617}
1618
Will Drewry6ac91122011-10-21 16:38:58 -05001619int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001620{
1621 int st;
1622 if (waitpid(j->initpid, &st, 0) < 0)
1623 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001624
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001625 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001626 int error_status = st;
1627 if (WIFSIGNALED(st)) {
1628 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001629 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001630 j->initpid, signum);
1631 /*
1632 * We return MINIJAIL_ERR_JAIL if the process received
1633 * SIGSYS, which happens when a syscall is blocked by
1634 * seccomp filters.
1635 * If not, we do what bash(1) does:
1636 * $? = 128 + signum
1637 */
1638 if (signum == SIGSYS) {
1639 error_status = MINIJAIL_ERR_JAIL;
1640 } else {
1641 error_status = 128 + signum;
1642 }
1643 }
1644 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001645 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001646
1647 int exit_status = WEXITSTATUS(st);
1648 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001649 info("child process %d exited with status %d",
1650 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001651
1652 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001653}
1654
Will Drewry6ac91122011-10-21 16:38:58 -05001655void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001656{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001657 if (j->flags.seccomp_filter && j->filter_prog) {
1658 free(j->filter_prog->filter);
1659 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001660 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001661 while (j->bindings_head) {
1662 struct binding *b = j->bindings_head;
1663 j->bindings_head = j->bindings_head->next;
1664 free(b->dest);
1665 free(b->src);
1666 free(b);
1667 }
1668 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001669 if (j->user)
1670 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001671 if (j->chrootdir)
1672 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001673 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001674}