blob: 544651a8bbfb49e2d194f36fc597ee4823da1ec7 [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
Andrew Brestickereac28942015-11-11 16:04:46 -080056#ifndef PR_ALT_SYSCALL
57# define PR_ALT_SYSCALL 0x43724f53
58#endif
59
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080060/* For seccomp_filter using BPF. */
61#ifndef PR_SET_NO_NEW_PRIVS
62# define PR_SET_NO_NEW_PRIVS 38
63#endif
64#ifndef SECCOMP_MODE_FILTER
65# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050066#endif
67
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070068#ifdef USE_SECCOMP_SOFTFAIL
69# define SECCOMP_SOFTFAIL 1
70#else
71# define SECCOMP_SOFTFAIL 0
72#endif
73
Dylan Reid648b2202015-10-23 00:50:00 -070074struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040075 char *src;
76 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070077 char *type;
78 unsigned long flags;
79 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040080};
81
Will Drewryf89aef52011-09-16 16:48:57 -050082struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070083 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070084 * WARNING: if you add a flag here you need to make sure it's
85 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070086 */
Elly Jonese1749eb2011-10-07 13:54:59 -040087 struct {
88 int uid:1;
89 int gid:1;
90 int caps:1;
91 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070092 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040093 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040094 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -070095 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080096 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040097 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -070098 int remount_proc_ro:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040099 int usergroups:1;
100 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700101 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400102 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700103 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400104 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800105 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700106 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800107 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800108 int pid_file:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800109 int alt_syscall:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400110 } flags;
111 uid_t uid;
112 gid_t gid;
113 gid_t usergid;
114 char *user;
115 uint64_t caps;
116 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700117 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700118 int netns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800119 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400120 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800121 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800122 char *uidmap;
123 char *gidmap;
Andrew Brestickereac28942015-11-11 16:04:46 -0800124 char *alt_syscall_table;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800125 struct sock_fprog *filter_prog;
Dylan Reid648b2202015-10-23 00:50:00 -0700126 struct mountpoint *mounts_head;
127 struct mountpoint *mounts_tail;
128 int mounts_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500129};
130
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700131/*
132 * Strip out flags meant for the parent.
133 * We keep things that are not inherited across execve(2) (e.g. capabilities),
134 * or are easier to set after execve(2) (e.g. seccomp filters).
135 */
136void minijail_preenter(struct minijail *j)
137{
138 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700139 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700140 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700141 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800142 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800143 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700144}
145
146/*
147 * Strip out flags meant for the child.
148 * We keep things that are inherited across execve(2).
149 */
150void minijail_preexec(struct minijail *j)
151{
152 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700153 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700154 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800155 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700156 if (j->user)
157 free(j->user);
158 j->user = NULL;
159 memset(&j->flags, 0, sizeof(j->flags));
160 /* Now restore anything we meant to keep. */
161 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700162 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700163 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800164 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700165 /* Note, |pids| will already have been used before this call. */
166}
167
168/* Minijail API. */
169
Will Drewry6ac91122011-10-21 16:38:58 -0500170struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400171{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400172 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400173}
174
Will Drewry6ac91122011-10-21 16:38:58 -0500175void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400176{
177 if (uid == 0)
178 die("useless change to uid 0");
179 j->uid = uid;
180 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400181}
182
Will Drewry6ac91122011-10-21 16:38:58 -0500183void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400184{
185 if (gid == 0)
186 die("useless change to gid 0");
187 j->gid = gid;
188 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400189}
190
Will Drewry6ac91122011-10-21 16:38:58 -0500191int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400192{
193 char *buf = NULL;
194 struct passwd pw;
195 struct passwd *ppw = NULL;
196 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
197 if (sz == -1)
198 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400199
Elly Jonesdd3e8512012-01-23 15:13:38 -0500200 /*
201 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400202 * the maximum needed size of the buffer, so we don't have to search.
203 */
204 buf = malloc(sz);
205 if (!buf)
206 return -ENOMEM;
207 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500208 /*
209 * We're safe to free the buffer here. The strings inside pw point
210 * inside buf, but we don't use any of them; this leaves the pointers
211 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
212 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400213 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700214 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400215 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700216 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400217 minijail_change_uid(j, ppw->pw_uid);
218 j->user = strdup(user);
219 if (!j->user)
220 return -ENOMEM;
221 j->usergid = ppw->pw_gid;
222 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400223}
224
Will Drewry6ac91122011-10-21 16:38:58 -0500225int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400226{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700227 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700228 struct group gr;
229 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400230 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
231 if (sz == -1)
232 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400233
Elly Jonesdd3e8512012-01-23 15:13:38 -0500234 /*
235 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400236 * the maximum needed size of the buffer, so we don't have to search.
237 */
238 buf = malloc(sz);
239 if (!buf)
240 return -ENOMEM;
241 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500242 /*
243 * We're safe to free the buffer here. The strings inside gr point
244 * inside buf, but we don't use any of them; this leaves the pointers
245 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
246 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400247 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700248 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400249 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700250 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400251 minijail_change_gid(j, pgr->gr_gid);
252 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400253}
254
Will Drewry6ac91122011-10-21 16:38:58 -0500255void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400256{
257 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400258}
259
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700260void API minijail_no_new_privs(struct minijail *j)
261{
262 j->flags.no_new_privs = 1;
263}
264
Will Drewry6ac91122011-10-21 16:38:58 -0500265void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400266{
267 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500268}
269
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700270void API minijail_log_seccomp_filter_failures(struct minijail *j)
271{
272 j->flags.log_seccomp_filter = 1;
273}
274
Will Drewry6ac91122011-10-21 16:38:58 -0500275void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400276{
277 j->caps = capmask;
278 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400279}
280
Will Drewry6ac91122011-10-21 16:38:58 -0500281void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400282{
283 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400284}
285
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700286void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
287{
288 int ns_fd = open(ns_path, O_RDONLY);
289 if (ns_fd < 0) {
290 pdie("failed to open namespace '%s'", ns_path);
291 }
292 j->mountns_fd = ns_fd;
293 j->flags.enter_vfs = 1;
294}
295
Will Drewry6ac91122011-10-21 16:38:58 -0500296void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400297{
Elly Jonese58176c2012-01-23 11:46:17 -0500298 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700299 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400300 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800301 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400302}
303
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400304void API minijail_namespace_net(struct minijail *j)
305{
306 j->flags.net = 1;
307}
308
Dylan Reid1102f5a2015-09-15 11:52:20 -0700309void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
310{
311 int ns_fd = open(ns_path, O_RDONLY);
312 if (ns_fd < 0) {
313 pdie("failed to open namespace '%s'", ns_path);
314 }
315 j->netns_fd = ns_fd;
316 j->flags.enter_net = 1;
317}
318
Dylan Reid791f5772015-09-14 20:02:42 -0700319void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400320{
321 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700322 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400323}
324
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800325void API minijail_namespace_user(struct minijail *j)
326{
327 j->flags.userns = 1;
328}
329
330int API minijail_uidmap(struct minijail *j, const char *uidmap)
331{
332 j->uidmap = strdup(uidmap);
333 if (!j->uidmap)
334 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800335 char *ch;
336 for (ch = j->uidmap; *ch; ch++) {
337 if (*ch == ',')
338 *ch = '\n';
339 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800340 return 0;
341}
342
343int API minijail_gidmap(struct minijail *j, const char *gidmap)
344{
345 j->gidmap = strdup(gidmap);
346 if (!j->gidmap)
347 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800348 char *ch;
349 for (ch = j->gidmap; *ch; ch++) {
350 if (*ch == ',')
351 *ch = '\n';
352 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800353 return 0;
354}
355
Will Drewry6ac91122011-10-21 16:38:58 -0500356void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400357{
358 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400359}
360
Will Drewry6ac91122011-10-21 16:38:58 -0500361void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400362{
363 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400364}
365
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800366void API minijail_run_as_init(struct minijail *j)
367{
368 /*
369 * Since the jailed program will become 'init' in the new PID namespace,
370 * Minijail does not need to fork an 'init' process.
371 */
372 j->flags.do_init = 0;
373}
374
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700375int API minijail_enter_chroot(struct minijail *j, const char *dir)
376{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400377 if (j->chrootdir)
378 return -EINVAL;
379 j->chrootdir = strdup(dir);
380 if (!j->chrootdir)
381 return -ENOMEM;
382 j->flags.chroot = 1;
383 return 0;
384}
385
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800386int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
387{
388 if (j->chrootdir)
389 return -EINVAL;
390 j->chrootdir = strdup(dir);
391 if (!j->chrootdir)
392 return -ENOMEM;
393 j->flags.pivot_root = 1;
394 return 0;
395}
396
Dylan Reida14e08d2015-10-22 21:05:29 -0700397static char *append_external_path(const char *external_path,
398 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700399{
Dylan Reida14e08d2015-10-22 21:05:29 -0700400 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700401 size_t pathlen;
402
Dylan Reid08946cc2015-09-16 19:10:57 -0700403 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700404 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
405 path = malloc(pathlen);
406 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700407
Dylan Reida14e08d2015-10-22 21:05:29 -0700408 return path;
409}
410
411char API *minijail_get_original_path(struct minijail *j,
412 const char *path_inside_chroot)
413{
Dylan Reid648b2202015-10-23 00:50:00 -0700414 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700415
Dylan Reid648b2202015-10-23 00:50:00 -0700416 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700417 while (b) {
418 /*
419 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700420 * mount, then the original path is exactly the source of
421 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700422 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700423 * mount source = /some/path/exe, mount dest =
424 * /chroot/path/exe Then when getting the original path of
425 * "/chroot/path/exe", the source of that mount,
426 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700427 */
428 if (!strcmp(b->dest, path_inside_chroot))
429 return strdup(b->src);
430
431 /*
432 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700433 * mount, take the suffix of the chroot path relative to the
434 * mount destination path, and append it to the mount source
435 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700436 */
437 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
438 const char *relative_path =
439 path_inside_chroot + strlen(b->dest);
440 return append_external_path(b->src, relative_path);
441 }
442 b = b->next;
443 }
444
445 /* If there is a chroot path, append |path_inside_chroot| to that. */
446 if (j->chrootdir)
447 return append_external_path(j->chrootdir, path_inside_chroot);
448
449 /* No chroot, so the path outside is the same as it is inside. */
450 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700451}
452
Lee Campbell11af0622014-05-22 12:36:04 -0700453void API minijail_mount_tmp(struct minijail *j)
454{
455 j->flags.mount_tmp = 1;
456}
457
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800458int API minijail_write_pid_file(struct minijail *j, const char *path)
459{
460 j->pid_file_path = strdup(path);
461 if (!j->pid_file_path)
462 return -ENOMEM;
463 j->flags.pid_file = 1;
464 return 0;
465}
466
Dylan Reid648b2202015-10-23 00:50:00 -0700467int API minijail_mount(struct minijail *j, const char *src, const char *dest,
468 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700469{
Dylan Reid648b2202015-10-23 00:50:00 -0700470 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400471
472 if (*dest != '/')
473 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700474 m = calloc(1, sizeof(*m));
475 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400476 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700477 m->dest = strdup(dest);
478 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400479 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700480 m->src = strdup(src);
481 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400482 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700483 m->type = strdup(type);
484 if (!m->type)
485 goto error;
486 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400487
Dylan Reid648b2202015-10-23 00:50:00 -0700488 info("mount %s -> %s type %s", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400489
Elly Jonesdd3e8512012-01-23 15:13:38 -0500490 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700491 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400492 * containing vfs namespace.
493 */
494 minijail_namespace_vfs(j);
495
Dylan Reid648b2202015-10-23 00:50:00 -0700496 if (j->mounts_tail)
497 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400498 else
Dylan Reid648b2202015-10-23 00:50:00 -0700499 j->mounts_head = m;
500 j->mounts_tail = m;
501 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400502
503 return 0;
504
505error:
Dylan Reid648b2202015-10-23 00:50:00 -0700506 free(m->src);
507 free(m->dest);
508 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400509 return -ENOMEM;
510}
511
Dylan Reid648b2202015-10-23 00:50:00 -0700512int API minijail_bind(struct minijail *j, const char *src, const char *dest,
513 int writeable)
514{
515 unsigned long flags = MS_BIND;
516
517 if (!writeable)
518 flags |= MS_RDONLY;
519
520 return minijail_mount(j, src, dest, "", flags);
521}
522
Will Drewry6ac91122011-10-21 16:38:58 -0500523void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400524{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700525 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
526 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
527 warn("not loading seccomp filter, seccomp not supported");
528 return;
529 }
530 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400531 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800532 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700533 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400534 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800535
536 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700537 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
538 die("failed to compile seccomp filter BPF program in '%s'",
539 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800540 }
541
542 j->filter_len = fprog->len;
543 j->filter_prog = fprog;
544
Elly Jonese1749eb2011-10-07 13:54:59 -0400545 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500546}
547
Andrew Brestickereac28942015-11-11 16:04:46 -0800548int API minijail_use_alt_syscall(struct minijail *j, const char *table)
549{
550 j->alt_syscall_table = strdup(table);
551 if (!j->alt_syscall_table)
552 return -ENOMEM;
553 j->flags.alt_syscall = 1;
554 return 0;
555}
556
Will Drewryf89aef52011-09-16 16:48:57 -0500557struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400558 size_t available;
559 size_t total;
560 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500561};
562
Will Drewry6ac91122011-10-21 16:38:58 -0500563void marshal_state_init(struct marshal_state *state,
564 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400565{
566 state->available = available;
567 state->buf = buf;
568 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500569}
570
Will Drewry6ac91122011-10-21 16:38:58 -0500571void marshal_append(struct marshal_state *state,
572 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400573{
574 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500575
Elly Jonese1749eb2011-10-07 13:54:59 -0400576 /* Up to |available| will be written. */
577 if (copy_len) {
578 memcpy(state->buf, src, copy_len);
579 state->buf += copy_len;
580 state->available -= copy_len;
581 }
582 /* |total| will contain the expected length. */
583 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500584}
585
Will Drewry6ac91122011-10-21 16:38:58 -0500586void minijail_marshal_helper(struct marshal_state *state,
587 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400588{
Dylan Reid648b2202015-10-23 00:50:00 -0700589 struct mountpoint *m = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400590 marshal_append(state, (char *)j, sizeof(*j));
591 if (j->user)
592 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400593 if (j->chrootdir)
594 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800595 if (j->alt_syscall_table) {
596 marshal_append(state, j->alt_syscall_table,
597 strlen(j->alt_syscall_table) + 1);
598 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800599 if (j->flags.seccomp_filter && j->filter_prog) {
600 struct sock_fprog *fp = j->filter_prog;
601 marshal_append(state, (char *)fp->filter,
602 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400603 }
Dylan Reid648b2202015-10-23 00:50:00 -0700604 for (m = j->mounts_head; m; m = m->next) {
605 marshal_append(state, m->src, strlen(m->src) + 1);
606 marshal_append(state, m->dest, strlen(m->dest) + 1);
607 marshal_append(state, m->type, strlen(m->type) + 1);
608 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400609 }
Will Drewryf89aef52011-09-16 16:48:57 -0500610}
611
Will Drewry6ac91122011-10-21 16:38:58 -0500612size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400613{
614 struct marshal_state state;
615 marshal_state_init(&state, NULL, 0);
616 minijail_marshal_helper(&state, j);
617 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500618}
619
Elly Jonese1749eb2011-10-07 13:54:59 -0400620int minijail_marshal(const struct minijail *j, char *buf, size_t available)
621{
622 struct marshal_state state;
623 marshal_state_init(&state, buf, available);
624 minijail_marshal_helper(&state, j);
625 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500626}
627
Elly Jones51a5b6c2011-10-12 19:09:26 -0400628/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
629 * @length Number of bytes to consume
630 * @buf Buffer to consume from
631 * @buflength Size of @buf
632 *
633 * Returns a pointer to the base of the bytes, or NULL for errors.
634 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700635void *consumebytes(size_t length, char **buf, size_t *buflength)
636{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400637 char *p = *buf;
638 if (length > *buflength)
639 return NULL;
640 *buf += length;
641 *buflength -= length;
642 return p;
643}
644
645/* consumestr: consumes a C string from a buffer @buf of length @length
646 * @buf Buffer to consume
647 * @length Length of buffer
648 *
649 * Returns a pointer to the base of the string, or NULL for errors.
650 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700651char *consumestr(char **buf, size_t *buflength)
652{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400653 size_t len = strnlen(*buf, *buflength);
654 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700655 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400656 return NULL;
657 return consumebytes(len + 1, buf, buflength);
658}
659
Elly Jonese1749eb2011-10-07 13:54:59 -0400660int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
661{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400662 int i;
663 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500664 int ret = -EINVAL;
665
Elly Jonese1749eb2011-10-07 13:54:59 -0400666 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500667 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400668 memcpy((void *)j, serialized, sizeof(*j));
669 serialized += sizeof(*j);
670 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500671
Will Drewrybee7ba72011-10-21 20:47:01 -0500672 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700673 j->mounts_head = NULL;
674 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800675 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500676
Elly Jonese1749eb2011-10-07 13:54:59 -0400677 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400678 char *user = consumestr(&serialized, &length);
679 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500680 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400681 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500682 if (!j->user)
683 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400684 }
Will Drewryf89aef52011-09-16 16:48:57 -0500685
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400686 if (j->chrootdir) { /* stale pointer */
687 char *chrootdir = consumestr(&serialized, &length);
688 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500689 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400690 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500691 if (!j->chrootdir)
692 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400693 }
694
Andrew Brestickereac28942015-11-11 16:04:46 -0800695 if (j->alt_syscall_table) { /* stale pointer */
696 char *alt_syscall_table = consumestr(&serialized, &length);
697 if (!alt_syscall_table)
698 goto bad_syscall_table;
699 j->alt_syscall_table = strdup(alt_syscall_table);
700 if (!j->alt_syscall_table)
701 goto bad_syscall_table;
702 }
703
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800704 if (j->flags.seccomp_filter && j->filter_len > 0) {
705 size_t ninstrs = j->filter_len;
706 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
707 ninstrs > USHRT_MAX)
708 goto bad_filters;
709
710 size_t program_len = ninstrs * sizeof(struct sock_filter);
711 void *program = consumebytes(program_len, &serialized, &length);
712 if (!program)
713 goto bad_filters;
714
715 j->filter_prog = malloc(sizeof(struct sock_fprog));
716 j->filter_prog->len = ninstrs;
717 j->filter_prog->filter = malloc(program_len);
718 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400719 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400720
Dylan Reid648b2202015-10-23 00:50:00 -0700721 count = j->mounts_count;
722 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400723 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700724 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400725 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700726 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400727 const char *src = consumestr(&serialized, &length);
728 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700729 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400730 dest = consumestr(&serialized, &length);
731 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700732 goto bad_mounts;
733 type = consumestr(&serialized, &length);
734 if (!type)
735 goto bad_mounts;
736 flags = consumebytes(sizeof(*flags), &serialized, &length);
737 if (!flags)
738 goto bad_mounts;
739 if (minijail_mount(j, src, dest, type, *flags))
740 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400741 }
742
Elly Jonese1749eb2011-10-07 13:54:59 -0400743 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500744
Dylan Reid648b2202015-10-23 00:50:00 -0700745bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800746 if (j->flags.seccomp_filter && j->filter_len > 0) {
747 free(j->filter_prog->filter);
748 free(j->filter_prog);
749 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500750bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800751 if (j->alt_syscall_table)
752 free(j->alt_syscall_table);
753bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500754 if (j->chrootdir)
755 free(j->chrootdir);
756bad_chrootdir:
757 if (j->user)
758 free(j->user);
759clear_pointers:
760 j->user = NULL;
761 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800762 j->alt_syscall_table = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500763out:
764 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500765}
766
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800767static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
768{
769 int fd, ret, len;
770 size_t sz;
771 char fname[32];
772 close(pipe_fds[0]);
773
774 sz = sizeof(fname);
775 if (j->uidmap) {
776 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700777 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800778 die("failed to write file name of uid_map");
779 fd = open(fname, O_WRONLY);
780 if (fd < 0)
781 pdie("failed to open '%s'", fname);
782 len = strlen(j->uidmap);
783 if (write(fd, j->uidmap, len) < len)
784 die("failed to set uid_map");
785 close(fd);
786 }
787 if (j->gidmap) {
788 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700789 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800790 die("failed to write file name of gid_map");
791 fd = open(fname, O_WRONLY);
792 if (fd < 0)
793 pdie("failed to open '%s'", fname);
794 len = strlen(j->gidmap);
795 if (write(fd, j->gidmap, len) < len)
796 die("failed to set gid_map");
797 close(fd);
798 }
799
800 close(pipe_fds[1]);
801}
802
803static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
804{
805 char buf;
806
807 close(pipe_fds[1]);
808
809 /* Wait for parent to set up uid/gid mappings. */
810 if (read(pipe_fds[0], &buf, 1) != 0)
811 die("failed to sync with parent");
812 close(pipe_fds[0]);
813
814 if (j->uidmap && setresuid(0, 0, 0))
815 pdie("setresuid");
816 if (j->gidmap && setresgid(0, 0, 0))
817 pdie("setresgid");
818}
819
Dylan Reid648b2202015-10-23 00:50:00 -0700820/* mount_one: Applies mounts from @m for @j, recursing as needed.
821 * @j Minijail these mounts are for
822 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400823 *
824 * Returns 0 for success.
825 */
Dylan Reid648b2202015-10-23 00:50:00 -0700826static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700827{
Dylan Reid648b2202015-10-23 00:50:00 -0700828 int ret;
829 char *dest;
830 int remount_ro = 0;
831
Elly Jones51a5b6c2011-10-12 19:09:26 -0400832 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700833 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400834 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700835
836 /*
837 * R/O bind mounts have to be remounted since bind and ro can't both be
838 * specified in the original bind mount. Remount R/O after the initial
839 * mount.
840 */
841 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
842 remount_ro = 1;
843 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500844 }
Dylan Reid648b2202015-10-23 00:50:00 -0700845
846 ret = mount(m->src, dest, m->type, m->flags, NULL);
847 if (ret)
848 pdie("mount: %s -> %s", m->src, dest);
849
850 if (remount_ro) {
851 m->flags |= MS_RDONLY;
852 ret = mount(m->src, dest, NULL,
853 m->flags | MS_REMOUNT, NULL);
854 if (ret)
855 pdie("bind ro: %s -> %s", m->src, dest);
856 }
857
Elly Jones51a5b6c2011-10-12 19:09:26 -0400858 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700859 if (m->next)
860 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400861 return ret;
862}
863
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700864int enter_chroot(const struct minijail *j)
865{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400866 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700867
868 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400869 return ret;
870
871 if (chroot(j->chrootdir))
872 return -errno;
873
874 if (chdir("/"))
875 return -errno;
876
877 return 0;
878}
879
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800880int enter_pivot_root(const struct minijail *j)
881{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800882 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -0700883
884 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800885 return ret;
886
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800887 /* Keep the fd for both old and new root. It will be used in fchdir later. */
888 oldroot = open("/", O_DIRECTORY | O_RDONLY);
889 if (oldroot < 0)
890 pdie("failed to open / for fchdir");
891 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
892 if (newroot < 0)
893 pdie("failed to open %s for fchdir", j->chrootdir);
894
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800895 /* To ensure chrootdir is the root of a file system, do a self bind mount. */
896 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
897 pdie("failed to bind mount '%s'", j->chrootdir);
898 if (chdir(j->chrootdir))
899 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800900 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800901 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800902
903 /*
904 * Now the old root is mounted on top of the new root. Use fchdir to
905 * change to the old root and unmount it.
906 */
907 if (fchdir(oldroot))
908 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800909 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800910 if (umount2(".", MNT_DETACH))
911 pdie("umount(/)");
912 /* Change back to the new root. */
913 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800914 return -errno;
915 if (chroot("/"))
916 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -0700917 /* Set correct CWD for getcwd(3). */
918 if (chdir("/"))
919 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800920
921 return 0;
922}
923
Lee Campbell11af0622014-05-22 12:36:04 -0700924int mount_tmp(void)
925{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800926 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700927}
928
Dylan Reid791f5772015-09-14 20:02:42 -0700929int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400930{
931 const char *kProcPath = "/proc";
932 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500933 /*
934 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400935 * /proc in our namespace, which means using MS_REMOUNT here would
936 * mutate our parent's mount as well, even though we're in a VFS
937 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800938 * and make our own. However, if we are in a new user namespace, /proc
939 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400940 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -0700941 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400942 return -errno;
943 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
944 return -errno;
945 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400946}
947
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800948static void write_pid_file(const struct minijail *j)
949{
950 FILE *fp = fopen(j->pid_file_path, "w");
951
952 if (!fp)
953 pdie("failed to open '%s'", j->pid_file_path);
954 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
955 pdie("fprintf(%s)", j->pid_file_path);
956 if (fclose(fp))
957 pdie("fclose(%s)", j->pid_file_path);
958}
959
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700960void drop_ugid(const struct minijail *j)
961{
962 if (j->flags.usergroups) {
963 if (initgroups(j->user, j->usergid))
964 pdie("initgroups");
965 } else {
966 /* Only attempt to clear supplemental groups if we are changing
967 * users. */
968 if ((j->uid || j->gid) && setgroups(0, NULL))
969 pdie("setgroups");
970 }
971
972 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
973 pdie("setresgid");
974
975 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
976 pdie("setresuid");
977}
978
Mike Frysinger3adfef72013-05-09 17:19:08 -0400979/*
980 * We specifically do not use cap_valid() as that only tells us the last
981 * valid cap we were *compiled* against (i.e. what the version of kernel
982 * headers says). If we run on a different kernel version, then it's not
983 * uncommon for that to be less (if an older kernel) or more (if a newer
984 * kernel). So suck up the answer via /proc.
985 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700986static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -0400987{
Dylan Reidf682d472015-09-17 21:39:07 -0700988 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
989 FILE *fp = fopen(cap_file, "re");
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700990 unsigned int last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -0400991
Dylan Reidf682d472015-09-17 21:39:07 -0700992 if (fscanf(fp, "%u", &last_valid_cap) != 1)
993 pdie("fscanf(%s)", cap_file);
994 fclose(fp);
Mike Frysinger3adfef72013-05-09 17:19:08 -0400995
Dylan Reidf682d472015-09-17 21:39:07 -0700996 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -0400997}
998
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700999void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001000{
1001 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001002 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001003 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001004 unsigned int i;
1005 if (!caps)
1006 die("can't get process caps");
1007 if (cap_clear_flag(caps, CAP_INHERITABLE))
1008 die("can't clear inheritable caps");
1009 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1010 die("can't clear effective caps");
1011 if (cap_clear_flag(caps, CAP_PERMITTED))
1012 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001013 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001014 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001015 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001016 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001017 flag[0] = i;
1018 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001019 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001020 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001021 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001022 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001023 die("can't add inheritable cap");
1024 }
1025 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001026 die("can't apply initial cleaned capset");
1027
1028 /*
1029 * Instead of dropping bounding set first, do it here in case
1030 * the caller had a more permissive bounding set which could
1031 * have been used above to raise a capability that wasn't already
1032 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1033 */
Dylan Reidf682d472015-09-17 21:39:07 -07001034 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001035 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001036 continue;
1037 if (prctl(PR_CAPBSET_DROP, i))
1038 pdie("prctl(PR_CAPBSET_DROP)");
1039 }
Kees Cook323878a2013-02-05 15:35:24 -08001040
1041 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001042 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001043 flag[0] = CAP_SETPCAP;
1044 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1045 die("can't clear effective cap");
1046 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1047 die("can't clear permitted cap");
1048 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1049 die("can't clear inheritable cap");
1050 }
1051
1052 if (cap_set_proc(caps))
1053 die("can't apply final cleaned capset");
1054
1055 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001056}
1057
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001058void set_seccomp_filter(const struct minijail *j)
1059{
1060 /*
1061 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1062 * in the kernel source tree for an explanation of the parameters.
1063 */
1064 if (j->flags.no_new_privs) {
1065 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1066 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1067 }
1068
1069 /*
1070 * If we're logging seccomp filter failures,
1071 * install the SIGSYS handler first.
1072 */
1073 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1074 if (install_sigsys_handler())
1075 pdie("install SIGSYS handler");
1076 warn("logging seccomp filter failures");
1077 }
1078
1079 /*
1080 * Install the syscall filter.
1081 */
1082 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001083 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
1084 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1085 warn("seccomp not supported");
1086 return;
1087 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001088 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001089 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001090 }
1091}
1092
Will Drewry6ac91122011-10-21 16:38:58 -05001093void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001094{
Dylan Reidf682d472015-09-17 21:39:07 -07001095 /*
1096 * Get the last valid cap from /proc, since /proc can be unmounted
1097 * before drop_caps().
1098 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001099 unsigned int last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001100
Elly Jonese1749eb2011-10-07 13:54:59 -04001101 if (j->flags.pids)
1102 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001103 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001104
Elly Jonese1749eb2011-10-07 13:54:59 -04001105 if (j->flags.usergroups && !j->user)
1106 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001107
Elly Jonesdd3e8512012-01-23 15:13:38 -05001108 /*
1109 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001110 * so we don't even try. If any of our operations fail, we abort() the
1111 * entire process.
1112 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001113 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1114 pdie("setns(CLONE_NEWNS)");
1115
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001116 if (j->flags.vfs) {
1117 if (unshare(CLONE_NEWNS))
1118 pdie("unshare(vfs)");
1119 /*
1120 * Remount all filesystems as private. If they are shared
1121 * new bind mounts will creep out of our namespace.
1122 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1123 */
1124 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1125 pdie("mount(/, private)");
1126 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001127
Dylan Reid1102f5a2015-09-15 11:52:20 -07001128 if (j->flags.enter_net) {
1129 if (setns(j->netns_fd, CLONE_NEWNET))
1130 pdie("setns(CLONE_NEWNET)");
1131 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001132 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001133 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001134
Elly Jones51a5b6c2011-10-12 19:09:26 -04001135 if (j->flags.chroot && enter_chroot(j))
1136 pdie("chroot");
1137
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001138 if (j->flags.pivot_root && enter_pivot_root(j))
1139 pdie("pivot_root");
1140
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001141 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001142 pdie("mount_tmp");
1143
Dylan Reid791f5772015-09-14 20:02:42 -07001144 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001145 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001146
Elly Jonese1749eb2011-10-07 13:54:59 -04001147 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001148 /*
1149 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001150 * capability to change uids, our attempt to use setuid()
1151 * below will fail. Hang on to root caps across setuid(), then
1152 * lock securebits.
1153 */
1154 if (prctl(PR_SET_KEEPCAPS, 1))
1155 pdie("prctl(PR_SET_KEEPCAPS)");
1156 if (prctl
1157 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1158 pdie("prctl(PR_SET_SECUREBITS)");
1159 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001160
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001161 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001162 * If we're setting no_new_privs, we can drop privileges
1163 * before setting seccomp filter. This way filter policies
1164 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001165 */
1166 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001167 drop_ugid(j);
1168 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001169 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001170
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001171 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001172 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001173 /*
1174 * If we're not setting no_new_privs,
1175 * we need to set seccomp filter *before* dropping privileges.
1176 * WARNING: this means that filter policies *must* allow
1177 * setgroups()/setresgid()/setresuid() for dropping root and
1178 * capget()/capset()/prctl() for dropping caps.
1179 */
1180 set_seccomp_filter(j);
1181
1182 drop_ugid(j);
1183 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001184 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001185 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001186
Elly Jonesdd3e8512012-01-23 15:13:38 -05001187 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001188 * Select the specified alternate syscall table. The table must not
1189 * block prctl(2) if we're using seccomp as well.
1190 */
1191 if (j->flags.alt_syscall) {
1192 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1193 pdie("prctl(PR_ALT_SYSCALL)");
1194 }
1195
1196 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001197 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001198 * privilege-dropping syscalls :)
1199 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001200 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1201 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1202 warn("seccomp not supported");
1203 return;
1204 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001205 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001206 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001207}
1208
Will Drewry6ac91122011-10-21 16:38:58 -05001209/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001210static int init_exitstatus = 0;
1211
Will Drewry6ac91122011-10-21 16:38:58 -05001212void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001213{
1214 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001215}
1216
Will Drewry6ac91122011-10-21 16:38:58 -05001217int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001218{
1219 pid_t pid;
1220 int status;
1221 /* so that we exit with the right status */
1222 signal(SIGTERM, init_term);
1223 /* TODO(wad) self jail with seccomp_filters here. */
1224 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001225 /*
1226 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001227 * left inside our pid namespace or we get a signal.
1228 */
1229 if (pid == rootpid)
1230 init_exitstatus = status;
1231 }
1232 if (!WIFEXITED(init_exitstatus))
1233 _exit(MINIJAIL_ERR_INIT);
1234 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001235}
1236
Will Drewry6ac91122011-10-21 16:38:58 -05001237int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001238{
1239 size_t sz = 0;
1240 size_t bytes = read(fd, &sz, sizeof(sz));
1241 char *buf;
1242 int r;
1243 if (sizeof(sz) != bytes)
1244 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001245 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001246 return -E2BIG;
1247 buf = malloc(sz);
1248 if (!buf)
1249 return -ENOMEM;
1250 bytes = read(fd, buf, sz);
1251 if (bytes != sz) {
1252 free(buf);
1253 return -EINVAL;
1254 }
1255 r = minijail_unmarshal(j, buf, sz);
1256 free(buf);
1257 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001258}
1259
Will Drewry6ac91122011-10-21 16:38:58 -05001260int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001261{
1262 char *buf;
1263 size_t sz = minijail_size(j);
1264 ssize_t written;
1265 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001266
Elly Jonese1749eb2011-10-07 13:54:59 -04001267 if (!sz)
1268 return -EINVAL;
1269 buf = malloc(sz);
1270 r = minijail_marshal(j, buf, sz);
1271 if (r) {
1272 free(buf);
1273 return r;
1274 }
1275 /* Sends [size][minijail]. */
1276 written = write(fd, &sz, sizeof(sz));
1277 if (written != sizeof(sz)) {
1278 free(buf);
1279 return -EFAULT;
1280 }
1281 written = write(fd, buf, sz);
1282 if (written < 0 || (size_t) written != sz) {
1283 free(buf);
1284 return -EFAULT;
1285 }
1286 free(buf);
1287 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001288}
Elly Jonescd7a9042011-07-22 13:56:51 -04001289
Will Drewry6ac91122011-10-21 16:38:58 -05001290int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001291{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001292#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001293 /* Don't use LDPRELOAD on Brillo. */
1294 return 0;
1295#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001296 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1297 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1298 if (!newenv)
1299 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001300
Elly Jonese1749eb2011-10-07 13:54:59 -04001301 /* Only insert a separating space if we have something to separate... */
1302 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1303 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001304
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001305 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001306 setenv(kLdPreloadEnvVar, newenv, 1);
1307 free(newenv);
1308 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001309#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001310}
1311
Will Drewry6ac91122011-10-21 16:38:58 -05001312int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001313{
1314 int r = pipe(fds);
1315 char fd_buf[11];
1316 if (r)
1317 return r;
1318 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1319 if (r <= 0)
1320 return -EINVAL;
1321 setenv(kFdEnvVar, fd_buf, 1);
1322 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001323}
1324
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001325int setup_pipe_end(int fds[2], size_t index)
1326{
1327 if (index > 1)
1328 return -1;
1329
1330 close(fds[1 - index]);
1331 return fds[index];
1332}
1333
1334int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1335{
1336 if (index > 1)
1337 return -1;
1338
1339 close(fds[1 - index]);
1340 /* dup2(2) the corresponding end of the pipe into |fd|. */
1341 return dup2(fds[index], fd);
1342}
1343
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001344int minijail_run_internal(struct minijail *j, const char *filename,
1345 char *const argv[], pid_t *pchild_pid,
1346 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1347 int use_preload);
1348
Will Drewry6ac91122011-10-21 16:38:58 -05001349int API minijail_run(struct minijail *j, const char *filename,
1350 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001351{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001352 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1353 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001354}
1355
1356int API minijail_run_pid(struct minijail *j, const char *filename,
1357 char *const argv[], pid_t *pchild_pid)
1358{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001359 return minijail_run_internal(j, filename, argv, pchild_pid,
1360 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001361}
1362
1363int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001364 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001365{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001366 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1367 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001368}
1369
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001370int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001371 char *const argv[], pid_t *pchild_pid,
1372 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001373{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001374 return minijail_run_internal(j, filename, argv, pchild_pid,
1375 pstdin_fd, pstdout_fd, pstderr_fd, true);
1376}
1377
1378int API minijail_run_no_preload(struct minijail *j, const char *filename,
1379 char *const argv[])
1380{
1381 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1382 false);
1383}
1384
Samuel Tan63187f42015-10-16 13:01:53 -07001385int API minijail_run_pid_pipes_no_preload(struct minijail *j,
1386 const char *filename, char *const argv[],
1387 pid_t *pchild_pid,
1388 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) {
1389 return minijail_run_internal(j, filename, argv, pchild_pid,
1390 pstdin_fd, pstdout_fd, pstderr_fd, false);
1391}
1392
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001393int minijail_run_internal(struct minijail *j, const char *filename,
1394 char *const argv[], pid_t *pchild_pid,
1395 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1396 int use_preload)
1397{
Elly Jonese1749eb2011-10-07 13:54:59 -04001398 char *oldenv, *oldenv_copy = NULL;
1399 pid_t child_pid;
1400 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001401 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001402 int stdout_fds[2];
1403 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001404 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001405 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001406 /* We need to remember this across the minijail_preexec() call. */
1407 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001408 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001409
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001410 if (use_preload) {
1411 oldenv = getenv(kLdPreloadEnvVar);
1412 if (oldenv) {
1413 oldenv_copy = strdup(oldenv);
1414 if (!oldenv_copy)
1415 return -ENOMEM;
1416 }
1417
1418 if (setup_preload())
1419 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001420 }
Will Drewryf89aef52011-09-16 16:48:57 -05001421
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001422 if (!use_preload) {
1423 if (j->flags.caps)
1424 die("Capabilities are not supported without "
1425 "LD_PRELOAD");
1426 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001427
Elly Jonesdd3e8512012-01-23 15:13:38 -05001428 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001429 * Make the process group ID of this process equal to its PID, so that
1430 * both the Minijail process and the jailed process can be killed
1431 * together.
1432 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1433 * the process is already a process group leader.
1434 */
1435 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1436 if (errno != EPERM) {
1437 pdie("setpgid(0, 0)");
1438 }
1439 }
1440
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001441 if (use_preload) {
1442 /*
1443 * Before we fork(2) and execve(2) the child process, we need
1444 * to open a pipe(2) to send the minijail configuration over.
1445 */
1446 if (setup_pipe(pipe_fds))
1447 return -EFAULT;
1448 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001449
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001450 /*
1451 * If we want to write to the child process' standard input,
1452 * create the pipe(2) now.
1453 */
1454 if (pstdin_fd) {
1455 if (pipe(stdin_fds))
1456 return -EFAULT;
1457 }
1458
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001459 /*
1460 * If we want to read from the child process' standard output,
1461 * create the pipe(2) now.
1462 */
1463 if (pstdout_fd) {
1464 if (pipe(stdout_fds))
1465 return -EFAULT;
1466 }
1467
1468 /*
1469 * If we want to read from the child process' standard error,
1470 * create the pipe(2) now.
1471 */
1472 if (pstderr_fd) {
1473 if (pipe(stderr_fds))
1474 return -EFAULT;
1475 }
1476
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001477 /*
1478 * If we want to set up a new uid/gid mapping in the user namespace,
1479 * create the pipe(2) to sync between parent and child.
1480 */
1481 if (j->flags.userns) {
1482 if (pipe(userns_pipe_fds))
1483 return -EFAULT;
1484 }
1485
Elly Jones761b7412012-06-13 15:49:52 -04001486 /* Use sys_clone() if and only if we're creating a pid namespace.
1487 *
1488 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1489 *
1490 * In multithreaded programs, there are a bunch of locks inside libc,
1491 * some of which may be held by other threads at the time that we call
1492 * minijail_run_pid(). If we call fork(), glibc does its level best to
1493 * ensure that we hold all of these locks before it calls clone()
1494 * internally and drop them after clone() returns, but when we call
1495 * sys_clone(2) directly, all that gets bypassed and we end up with a
1496 * child address space where some of libc's important locks are held by
1497 * other threads (which did not get cloned, and hence will never release
1498 * those locks). This is okay so long as we call exec() immediately
1499 * after, but a bunch of seemingly-innocent libc functions like setenv()
1500 * take locks.
1501 *
1502 * Hence, only call sys_clone() if we need to, in order to get at pid
1503 * namespacing. If we follow this path, the child's address space might
1504 * have broken locks; you may only call functions that do not acquire
1505 * any locks.
1506 *
1507 * Unfortunately, fork() acquires every lock it can get its hands on, as
1508 * previously detailed, so this function is highly likely to deadlock
1509 * later on (see "deadlock here") if we're multithreaded.
1510 *
1511 * We might hack around this by having the clone()d child (init of the
1512 * pid namespace) return directly, rather than leaving the clone()d
1513 * process hanging around to be init for the new namespace (and having
1514 * its fork()ed child return in turn), but that process would be crippled
1515 * with its libc locks potentially broken. We might try fork()ing in the
1516 * parent before we clone() to ensure that we own all the locks, but
1517 * then we have to have the forked child hanging around consuming
1518 * resources (and possibly having file descriptors / shared memory
1519 * regions / etc attached). We'd need to keep the child around to avoid
1520 * having its children get reparented to init.
1521 *
1522 * TODO(ellyjones): figure out if the "forked child hanging around"
1523 * problem is fixable or not. It would be nice if we worked in this
1524 * case.
1525 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001526 if (pid_namespace) {
1527 int clone_flags = CLONE_NEWPID | SIGCHLD;
1528 if (j->flags.userns)
1529 clone_flags |= CLONE_NEWUSER;
1530 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001531 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001532 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001533 }
Elly Jones761b7412012-06-13 15:49:52 -04001534
Elly Jonese1749eb2011-10-07 13:54:59 -04001535 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001536 if (use_preload) {
1537 free(oldenv_copy);
1538 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001539 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001540 }
Will Drewryf89aef52011-09-16 16:48:57 -05001541
Elly Jonese1749eb2011-10-07 13:54:59 -04001542 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001543 if (use_preload) {
1544 /* Restore parent's LD_PRELOAD. */
1545 if (oldenv_copy) {
1546 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1547 free(oldenv_copy);
1548 } else {
1549 unsetenv(kLdPreloadEnvVar);
1550 }
1551 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001552 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001553
Elly Jonese1749eb2011-10-07 13:54:59 -04001554 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001555
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001556 if (j->flags.pid_file)
1557 write_pid_file(j);
1558
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001559 if (j->flags.userns)
1560 write_ugid_mappings(j, userns_pipe_fds);
1561
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001562 if (use_preload) {
1563 /* Send marshalled minijail. */
1564 close(pipe_fds[0]); /* read endpoint */
1565 ret = minijail_to_fd(j, pipe_fds[1]);
1566 close(pipe_fds[1]); /* write endpoint */
1567 if (ret) {
1568 kill(j->initpid, SIGKILL);
1569 die("failed to send marshalled minijail");
1570 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001571 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001572
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001573 if (pchild_pid)
1574 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001575
1576 /*
1577 * If we want to write to the child process' standard input,
1578 * set up the write end of the pipe.
1579 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001580 if (pstdin_fd)
1581 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001582 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001583
1584 /*
1585 * If we want to read from the child process' standard output,
1586 * set up the read end of the pipe.
1587 */
1588 if (pstdout_fd)
1589 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001590 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001591
1592 /*
1593 * If we want to read from the child process' standard error,
1594 * set up the read end of the pipe.
1595 */
1596 if (pstderr_fd)
1597 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001598 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001599
Elly Jonese1749eb2011-10-07 13:54:59 -04001600 return 0;
1601 }
1602 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001603
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001604 if (j->flags.userns)
1605 enter_user_namespace(j, userns_pipe_fds);
1606
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001607 /*
1608 * If we want to write to the jailed process' standard input,
1609 * set up the read end of the pipe.
1610 */
1611 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001612 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1613 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001614 die("failed to set up stdin pipe");
1615 }
1616
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001617 /*
1618 * If we want to read from the jailed process' standard output,
1619 * set up the write end of the pipe.
1620 */
1621 if (pstdout_fd) {
1622 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1623 STDOUT_FILENO) < 0)
1624 die("failed to set up stdout pipe");
1625 }
1626
1627 /*
1628 * If we want to read from the jailed process' standard error,
1629 * set up the write end of the pipe.
1630 */
1631 if (pstderr_fd) {
1632 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1633 STDERR_FILENO) < 0)
1634 die("failed to set up stderr pipe");
1635 }
1636
Dylan Reid791f5772015-09-14 20:02:42 -07001637 /* If running an init program, let it decide when/how to mount /proc. */
1638 if (pid_namespace && !do_init)
1639 j->flags.remount_proc_ro = 0;
1640
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001641 if (use_preload) {
1642 /* Strip out flags that cannot be inherited across execve(2). */
1643 minijail_preexec(j);
1644 } else {
1645 j->flags.pids = 0;
1646 }
1647 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001648 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001649
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001650 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001651 /*
1652 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001653 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001654 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001655 * a child to actually run the program. If |do_init == 0|, we
1656 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001657 *
1658 * If we're multithreaded, we'll probably deadlock here. See
1659 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001660 */
1661 child_pid = fork();
1662 if (child_pid < 0)
1663 _exit(child_pid);
1664 else if (child_pid > 0)
1665 init(child_pid); /* never returns */
1666 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001667
Elly Jonesdd3e8512012-01-23 15:13:38 -05001668 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001669 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001670 * calling process
1671 * -> execve()-ing process
1672 * If we are:
1673 * calling process
1674 * -> init()-ing process
1675 * -> execve()-ing process
1676 */
1677 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001678}
1679
Will Drewry6ac91122011-10-21 16:38:58 -05001680int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001681{
1682 int st;
1683 if (kill(j->initpid, SIGTERM))
1684 return -errno;
1685 if (waitpid(j->initpid, &st, 0) < 0)
1686 return -errno;
1687 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001688}
1689
Will Drewry6ac91122011-10-21 16:38:58 -05001690int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001691{
1692 int st;
1693 if (waitpid(j->initpid, &st, 0) < 0)
1694 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001695
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001696 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001697 int error_status = st;
1698 if (WIFSIGNALED(st)) {
1699 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001700 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001701 j->initpid, signum);
1702 /*
1703 * We return MINIJAIL_ERR_JAIL if the process received
1704 * SIGSYS, which happens when a syscall is blocked by
1705 * seccomp filters.
1706 * If not, we do what bash(1) does:
1707 * $? = 128 + signum
1708 */
1709 if (signum == SIGSYS) {
1710 error_status = MINIJAIL_ERR_JAIL;
1711 } else {
1712 error_status = 128 + signum;
1713 }
1714 }
1715 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001716 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001717
1718 int exit_status = WEXITSTATUS(st);
1719 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001720 info("child process %d exited with status %d",
1721 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001722
1723 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001724}
1725
Will Drewry6ac91122011-10-21 16:38:58 -05001726void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001727{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001728 if (j->flags.seccomp_filter && j->filter_prog) {
1729 free(j->filter_prog->filter);
1730 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001731 }
Dylan Reid648b2202015-10-23 00:50:00 -07001732 while (j->mounts_head) {
1733 struct mountpoint *m = j->mounts_head;
1734 j->mounts_head = j->mounts_head->next;
1735 free(m->type);
1736 free(m->dest);
1737 free(m->src);
1738 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001739 }
Dylan Reid648b2202015-10-23 00:50:00 -07001740 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001741 if (j->user)
1742 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001743 if (j->chrootdir)
1744 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08001745 if (j->alt_syscall_table)
1746 free(j->alt_syscall_table);
Elly Jonese1749eb2011-10-07 13:54:59 -04001747 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001748}