blob: 5378d8464e65e8fcca745e388630a45d51e965d6 [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 Obes524c0402012-01-17 11:30:23 -080021#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <syscall.h>
26#include <sys/capability.h>
27#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050028#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040029#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070030#include <sys/stat.h>
31#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080032#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <unistd.h>
35
36#include "libminijail.h"
37#include "libminijail-private.h"
38
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070039#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070041#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042
Lei Zhangeee31552012-10-17 21:27:10 -070043#ifdef HAVE_SECUREBITS_H
44#include <linux/securebits.h>
45#else
46#define SECURE_ALL_BITS 0x15
47#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
48#endif
49
Will Drewry32ac9f52011-08-18 21:36:27 -050050/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070052# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080053#endif
54
55/* For seccomp_filter using BPF. */
56#ifndef PR_SET_NO_NEW_PRIVS
57# define PR_SET_NO_NEW_PRIVS 38
58#endif
59#ifndef SECCOMP_MODE_FILTER
60# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050061#endif
62
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070063#ifdef USE_SECCOMP_SOFTFAIL
64# define SECCOMP_SOFTFAIL 1
65#else
66# define SECCOMP_SOFTFAIL 0
67#endif
68
Elly Jones51a5b6c2011-10-12 19:09:26 -040069struct binding {
70 char *src;
71 char *dest;
72 int writeable;
73 struct binding *next;
74};
75
Will Drewryf89aef52011-09-16 16:48:57 -050076struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070077 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070078 * WARNING: if you add a flag here you need to make sure it's
79 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070080 */
Elly Jonese1749eb2011-10-07 13:54:59 -040081 struct {
82 int uid:1;
83 int gid:1;
84 int caps:1;
85 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070086 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040087 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040088 int net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080089 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040090 int seccomp:1;
91 int readonly:1;
92 int usergroups:1;
93 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070094 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040095 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070096 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040097 int chroot:1;
Lee Campbell11af0622014-05-22 12:36:04 -070098 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +080099 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800100 int pid_file:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400101 } flags;
102 uid_t uid;
103 gid_t gid;
104 gid_t usergid;
105 char *user;
106 uint64_t caps;
107 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700108 int mountns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800109 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400110 int binding_count;
111 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800112 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800113 char *uidmap;
114 char *gidmap;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800115 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400116 struct binding *bindings_head;
117 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500118};
119
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700120/*
121 * Strip out flags meant for the parent.
122 * We keep things that are not inherited across execve(2) (e.g. capabilities),
123 * or are easier to set after execve(2) (e.g. seccomp filters).
124 */
125void minijail_preenter(struct minijail *j)
126{
127 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700128 j->flags.enter_vfs = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700129 j->flags.readonly = 0;
130 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800131 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800132 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700133}
134
135/*
136 * Strip out flags meant for the child.
137 * We keep things that are inherited across execve(2).
138 */
139void minijail_preexec(struct minijail *j)
140{
141 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700142 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700143 int readonly = j->flags.readonly;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800144 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700145 if (j->user)
146 free(j->user);
147 j->user = NULL;
148 memset(&j->flags, 0, sizeof(j->flags));
149 /* Now restore anything we meant to keep. */
150 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700151 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700152 j->flags.readonly = readonly;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800153 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700154 /* Note, |pids| will already have been used before this call. */
155}
156
157/* Minijail API. */
158
Will Drewry6ac91122011-10-21 16:38:58 -0500159struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400160{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400161 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400162}
163
Will Drewry6ac91122011-10-21 16:38:58 -0500164void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400165{
166 if (uid == 0)
167 die("useless change to uid 0");
168 j->uid = uid;
169 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400170}
171
Will Drewry6ac91122011-10-21 16:38:58 -0500172void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400173{
174 if (gid == 0)
175 die("useless change to gid 0");
176 j->gid = gid;
177 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400178}
179
Will Drewry6ac91122011-10-21 16:38:58 -0500180int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400181{
182 char *buf = NULL;
183 struct passwd pw;
184 struct passwd *ppw = NULL;
185 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
186 if (sz == -1)
187 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400188
Elly Jonesdd3e8512012-01-23 15:13:38 -0500189 /*
190 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400191 * the maximum needed size of the buffer, so we don't have to search.
192 */
193 buf = malloc(sz);
194 if (!buf)
195 return -ENOMEM;
196 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500197 /*
198 * We're safe to free the buffer here. The strings inside pw point
199 * inside buf, but we don't use any of them; this leaves the pointers
200 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
201 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400202 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700203 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400204 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700205 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400206 minijail_change_uid(j, ppw->pw_uid);
207 j->user = strdup(user);
208 if (!j->user)
209 return -ENOMEM;
210 j->usergid = ppw->pw_gid;
211 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400212}
213
Will Drewry6ac91122011-10-21 16:38:58 -0500214int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400215{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700216 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700217 struct group gr;
218 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400219 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
220 if (sz == -1)
221 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400222
Elly Jonesdd3e8512012-01-23 15:13:38 -0500223 /*
224 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400225 * the maximum needed size of the buffer, so we don't have to search.
226 */
227 buf = malloc(sz);
228 if (!buf)
229 return -ENOMEM;
230 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500231 /*
232 * We're safe to free the buffer here. The strings inside gr point
233 * inside buf, but we don't use any of them; this leaves the pointers
234 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
235 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400236 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700237 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400238 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700239 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400240 minijail_change_gid(j, pgr->gr_gid);
241 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400242}
243
Will Drewry6ac91122011-10-21 16:38:58 -0500244void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400245{
246 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400247}
248
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700249void API minijail_no_new_privs(struct minijail *j)
250{
251 j->flags.no_new_privs = 1;
252}
253
Will Drewry6ac91122011-10-21 16:38:58 -0500254void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400255{
256 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500257}
258
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700259void API minijail_log_seccomp_filter_failures(struct minijail *j)
260{
261 j->flags.log_seccomp_filter = 1;
262}
263
Will Drewry6ac91122011-10-21 16:38:58 -0500264void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400265{
266 j->caps = capmask;
267 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400268}
269
Will Drewry6ac91122011-10-21 16:38:58 -0500270void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400271{
272 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400273}
274
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700275void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
276{
277 int ns_fd = open(ns_path, O_RDONLY);
278 if (ns_fd < 0) {
279 pdie("failed to open namespace '%s'", ns_path);
280 }
281 j->mountns_fd = ns_fd;
282 j->flags.enter_vfs = 1;
283}
284
Will Drewry6ac91122011-10-21 16:38:58 -0500285void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400286{
Elly Jonese58176c2012-01-23 11:46:17 -0500287 j->flags.vfs = 1;
288 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400289 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800290 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400291}
292
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400293void API minijail_namespace_net(struct minijail *j)
294{
295 j->flags.net = 1;
296}
297
Will Drewry6ac91122011-10-21 16:38:58 -0500298void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400299{
300 j->flags.vfs = 1;
301 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400302}
303
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800304void API minijail_namespace_user(struct minijail *j)
305{
306 j->flags.userns = 1;
307}
308
309int API minijail_uidmap(struct minijail *j, const char *uidmap)
310{
311 j->uidmap = strdup(uidmap);
312 if (!j->uidmap)
313 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800314 char *ch;
315 for (ch = j->uidmap; *ch; ch++) {
316 if (*ch == ',')
317 *ch = '\n';
318 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800319 return 0;
320}
321
322int API minijail_gidmap(struct minijail *j, const char *gidmap)
323{
324 j->gidmap = strdup(gidmap);
325 if (!j->gidmap)
326 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800327 char *ch;
328 for (ch = j->gidmap; *ch; ch++) {
329 if (*ch == ',')
330 *ch = '\n';
331 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800332 return 0;
333}
334
Will Drewry6ac91122011-10-21 16:38:58 -0500335void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400336{
337 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400338}
339
Will Drewry6ac91122011-10-21 16:38:58 -0500340void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400341{
342 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400343}
344
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800345void API minijail_run_as_init(struct minijail *j)
346{
347 /*
348 * Since the jailed program will become 'init' in the new PID namespace,
349 * Minijail does not need to fork an 'init' process.
350 */
351 j->flags.do_init = 0;
352}
353
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700354int API minijail_enter_chroot(struct minijail *j, const char *dir)
355{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400356 if (j->chrootdir)
357 return -EINVAL;
358 j->chrootdir = strdup(dir);
359 if (!j->chrootdir)
360 return -ENOMEM;
361 j->flags.chroot = 1;
362 return 0;
363}
364
Lee Campbell11af0622014-05-22 12:36:04 -0700365void API minijail_mount_tmp(struct minijail *j)
366{
367 j->flags.mount_tmp = 1;
368}
369
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800370int API minijail_write_pid_file(struct minijail *j, const char *path)
371{
372 j->pid_file_path = strdup(path);
373 if (!j->pid_file_path)
374 return -ENOMEM;
375 j->flags.pid_file = 1;
376 return 0;
377}
378
Will Drewry6ac91122011-10-21 16:38:58 -0500379int API minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700380 int writeable)
381{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400382 struct binding *b;
383
384 if (*dest != '/')
385 return -EINVAL;
386 b = calloc(1, sizeof(*b));
387 if (!b)
388 return -ENOMEM;
389 b->dest = strdup(dest);
390 if (!b->dest)
391 goto error;
392 b->src = strdup(src);
393 if (!b->src)
394 goto error;
395 b->writeable = writeable;
396
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700397 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400398
Elly Jonesdd3e8512012-01-23 15:13:38 -0500399 /*
400 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400401 * containing vfs namespace.
402 */
403 minijail_namespace_vfs(j);
404
405 if (j->bindings_tail)
406 j->bindings_tail->next = b;
407 else
408 j->bindings_head = b;
409 j->bindings_tail = b;
410 j->binding_count++;
411
412 return 0;
413
414error:
415 free(b->src);
416 free(b->dest);
417 free(b);
418 return -ENOMEM;
419}
420
Will Drewry6ac91122011-10-21 16:38:58 -0500421void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400422{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700423 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
424 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
425 warn("not loading seccomp filter, seccomp not supported");
426 return;
427 }
428 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400429 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800430 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700431 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400432 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800433
434 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700435 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
436 die("failed to compile seccomp filter BPF program in '%s'",
437 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800438 }
439
440 j->filter_len = fprog->len;
441 j->filter_prog = fprog;
442
Elly Jonese1749eb2011-10-07 13:54:59 -0400443 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500444}
445
Will Drewryf89aef52011-09-16 16:48:57 -0500446struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400447 size_t available;
448 size_t total;
449 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500450};
451
Will Drewry6ac91122011-10-21 16:38:58 -0500452void marshal_state_init(struct marshal_state *state,
453 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400454{
455 state->available = available;
456 state->buf = buf;
457 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500458}
459
Will Drewry6ac91122011-10-21 16:38:58 -0500460void marshal_append(struct marshal_state *state,
461 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400462{
463 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500464
Elly Jonese1749eb2011-10-07 13:54:59 -0400465 /* Up to |available| will be written. */
466 if (copy_len) {
467 memcpy(state->buf, src, copy_len);
468 state->buf += copy_len;
469 state->available -= copy_len;
470 }
471 /* |total| will contain the expected length. */
472 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500473}
474
Will Drewry6ac91122011-10-21 16:38:58 -0500475void minijail_marshal_helper(struct marshal_state *state,
476 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400477{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400478 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400479 marshal_append(state, (char *)j, sizeof(*j));
480 if (j->user)
481 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400482 if (j->chrootdir)
483 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800484 if (j->flags.seccomp_filter && j->filter_prog) {
485 struct sock_fprog *fp = j->filter_prog;
486 marshal_append(state, (char *)fp->filter,
487 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400488 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400489 for (b = j->bindings_head; b; b = b->next) {
490 marshal_append(state, b->src, strlen(b->src) + 1);
491 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700492 marshal_append(state, (char *)&b->writeable,
493 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400494 }
Will Drewryf89aef52011-09-16 16:48:57 -0500495}
496
Will Drewry6ac91122011-10-21 16:38:58 -0500497size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400498{
499 struct marshal_state state;
500 marshal_state_init(&state, NULL, 0);
501 minijail_marshal_helper(&state, j);
502 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500503}
504
Elly Jonese1749eb2011-10-07 13:54:59 -0400505int minijail_marshal(const struct minijail *j, char *buf, size_t available)
506{
507 struct marshal_state state;
508 marshal_state_init(&state, buf, available);
509 minijail_marshal_helper(&state, j);
510 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500511}
512
Elly Jones51a5b6c2011-10-12 19:09:26 -0400513/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
514 * @length Number of bytes to consume
515 * @buf Buffer to consume from
516 * @buflength Size of @buf
517 *
518 * Returns a pointer to the base of the bytes, or NULL for errors.
519 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700520void *consumebytes(size_t length, char **buf, size_t *buflength)
521{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400522 char *p = *buf;
523 if (length > *buflength)
524 return NULL;
525 *buf += length;
526 *buflength -= length;
527 return p;
528}
529
530/* consumestr: consumes a C string from a buffer @buf of length @length
531 * @buf Buffer to consume
532 * @length Length of buffer
533 *
534 * Returns a pointer to the base of the string, or NULL for errors.
535 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700536char *consumestr(char **buf, size_t *buflength)
537{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400538 size_t len = strnlen(*buf, *buflength);
539 if (len == *buflength)
540 /* There's no null-terminator */
541 return NULL;
542 return consumebytes(len + 1, buf, buflength);
543}
544
Elly Jonese1749eb2011-10-07 13:54:59 -0400545int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
546{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400547 int i;
548 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500549 int ret = -EINVAL;
550
Elly Jonese1749eb2011-10-07 13:54:59 -0400551 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500552 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400553 memcpy((void *)j, serialized, sizeof(*j));
554 serialized += sizeof(*j);
555 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500556
Will Drewrybee7ba72011-10-21 20:47:01 -0500557 /* Potentially stale pointers not used as signals. */
558 j->bindings_head = NULL;
559 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800560 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500561
Elly Jonese1749eb2011-10-07 13:54:59 -0400562 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400563 char *user = consumestr(&serialized, &length);
564 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500565 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400566 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500567 if (!j->user)
568 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400569 }
Will Drewryf89aef52011-09-16 16:48:57 -0500570
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400571 if (j->chrootdir) { /* stale pointer */
572 char *chrootdir = consumestr(&serialized, &length);
573 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500574 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400575 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500576 if (!j->chrootdir)
577 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400578 }
579
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800580 if (j->flags.seccomp_filter && j->filter_len > 0) {
581 size_t ninstrs = j->filter_len;
582 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
583 ninstrs > USHRT_MAX)
584 goto bad_filters;
585
586 size_t program_len = ninstrs * sizeof(struct sock_filter);
587 void *program = consumebytes(program_len, &serialized, &length);
588 if (!program)
589 goto bad_filters;
590
591 j->filter_prog = malloc(sizeof(struct sock_fprog));
592 j->filter_prog->len = ninstrs;
593 j->filter_prog->filter = malloc(program_len);
594 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400595 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400596
597 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400598 j->binding_count = 0;
599 for (i = 0; i < count; ++i) {
600 int *writeable;
601 const char *dest;
602 const char *src = consumestr(&serialized, &length);
603 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500604 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400605 dest = consumestr(&serialized, &length);
606 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500607 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400608 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
609 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500610 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400611 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500612 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400613 }
614
Elly Jonese1749eb2011-10-07 13:54:59 -0400615 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500616
617bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800618 if (j->flags.seccomp_filter && j->filter_len > 0) {
619 free(j->filter_prog->filter);
620 free(j->filter_prog);
621 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500622bad_filters:
623 if (j->chrootdir)
624 free(j->chrootdir);
625bad_chrootdir:
626 if (j->user)
627 free(j->user);
628clear_pointers:
629 j->user = NULL;
630 j->chrootdir = NULL;
631out:
632 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500633}
634
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800635static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
636{
637 int fd, ret, len;
638 size_t sz;
639 char fname[32];
640 close(pipe_fds[0]);
641
642 sz = sizeof(fname);
643 if (j->uidmap) {
644 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
645 if (ret < 0 || ret >= sz)
646 die("failed to write file name of uid_map");
647 fd = open(fname, O_WRONLY);
648 if (fd < 0)
649 pdie("failed to open '%s'", fname);
650 len = strlen(j->uidmap);
651 if (write(fd, j->uidmap, len) < len)
652 die("failed to set uid_map");
653 close(fd);
654 }
655 if (j->gidmap) {
656 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
657 if (ret < 0 || ret >= sz)
658 die("failed to write file name of gid_map");
659 fd = open(fname, O_WRONLY);
660 if (fd < 0)
661 pdie("failed to open '%s'", fname);
662 len = strlen(j->gidmap);
663 if (write(fd, j->gidmap, len) < len)
664 die("failed to set gid_map");
665 close(fd);
666 }
667
668 close(pipe_fds[1]);
669}
670
671static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
672{
673 char buf;
674
675 close(pipe_fds[1]);
676
677 /* Wait for parent to set up uid/gid mappings. */
678 if (read(pipe_fds[0], &buf, 1) != 0)
679 die("failed to sync with parent");
680 close(pipe_fds[0]);
681
682 if (j->uidmap && setresuid(0, 0, 0))
683 pdie("setresuid");
684 if (j->gidmap && setresgid(0, 0, 0))
685 pdie("setresgid");
686}
687
Elly Jones51a5b6c2011-10-12 19:09:26 -0400688/* bind_one: Applies bindings from @b for @j, recursing as needed.
689 * @j Minijail these bindings are for
690 * @b Head of list of bindings
691 *
692 * Returns 0 for success.
693 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700694int bind_one(const struct minijail *j, struct binding *b)
695{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400696 int ret = 0;
697 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400698 if (ret)
699 return ret;
700 /* dest has a leading "/" */
701 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
702 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500703 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400704 if (ret)
705 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500706 if (!b->writeable) {
707 ret = mount(b->src, dest, NULL,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700708 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
Elly Jonesa1059632011-12-15 15:17:07 -0500709 if (ret)
710 pdie("bind ro: %s -> %s", b->src, dest);
711 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400712 free(dest);
713 if (b->next)
714 return bind_one(j, b->next);
715 return ret;
716}
717
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700718int enter_chroot(const struct minijail *j)
719{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400720 int ret;
721 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
722 return ret;
723
724 if (chroot(j->chrootdir))
725 return -errno;
726
727 if (chdir("/"))
728 return -errno;
729
730 return 0;
731}
732
Lee Campbell11af0622014-05-22 12:36:04 -0700733int mount_tmp(void)
734{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800735 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700736}
737
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800738int remount_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400739{
740 const char *kProcPath = "/proc";
741 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500742 /*
743 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400744 * /proc in our namespace, which means using MS_REMOUNT here would
745 * mutate our parent's mount as well, even though we're in a VFS
746 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800747 * and make our own. However, if we are in a new user namespace, /proc
748 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400749 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800750 if (umount(kProcPath) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400751 return -errno;
752 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
753 return -errno;
754 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400755}
756
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800757static void write_pid_file(const struct minijail *j)
758{
759 FILE *fp = fopen(j->pid_file_path, "w");
760
761 if (!fp)
762 pdie("failed to open '%s'", j->pid_file_path);
763 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
764 pdie("fprintf(%s)", j->pid_file_path);
765 if (fclose(fp))
766 pdie("fclose(%s)", j->pid_file_path);
767}
768
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700769void drop_ugid(const struct minijail *j)
770{
771 if (j->flags.usergroups) {
772 if (initgroups(j->user, j->usergid))
773 pdie("initgroups");
774 } else {
775 /* Only attempt to clear supplemental groups if we are changing
776 * users. */
777 if ((j->uid || j->gid) && setgroups(0, NULL))
778 pdie("setgroups");
779 }
780
781 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
782 pdie("setresgid");
783
784 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
785 pdie("setresuid");
786}
787
Mike Frysinger3adfef72013-05-09 17:19:08 -0400788/*
789 * We specifically do not use cap_valid() as that only tells us the last
790 * valid cap we were *compiled* against (i.e. what the version of kernel
791 * headers says). If we run on a different kernel version, then it's not
792 * uncommon for that to be less (if an older kernel) or more (if a newer
793 * kernel). So suck up the answer via /proc.
794 */
795static int run_cap_valid(unsigned int cap)
796{
797 static unsigned int last_cap;
798
799 if (!last_cap) {
800 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
801 FILE *fp = fopen(cap_file, "re");
802 if (fscanf(fp, "%u", &last_cap) != 1)
803 pdie("fscanf(%s)", cap_file);
804 fclose(fp);
805 }
806
807 return cap <= last_cap;
808}
809
Will Drewry6ac91122011-10-21 16:38:58 -0500810void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400811{
812 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800813 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800814 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400815 unsigned int i;
816 if (!caps)
817 die("can't get process caps");
818 if (cap_clear_flag(caps, CAP_INHERITABLE))
819 die("can't clear inheritable caps");
820 if (cap_clear_flag(caps, CAP_EFFECTIVE))
821 die("can't clear effective caps");
822 if (cap_clear_flag(caps, CAP_PERMITTED))
823 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400824 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800825 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800826 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400827 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800828 flag[0] = i;
829 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400830 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800831 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400832 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800833 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400834 die("can't add inheritable cap");
835 }
836 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800837 die("can't apply initial cleaned capset");
838
839 /*
840 * Instead of dropping bounding set first, do it here in case
841 * the caller had a more permissive bounding set which could
842 * have been used above to raise a capability that wasn't already
843 * present. This requires CAP_SETPCAP, so we raised/kept it above.
844 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400845 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800846 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400847 continue;
848 if (prctl(PR_CAPBSET_DROP, i))
849 pdie("prctl(PR_CAPBSET_DROP)");
850 }
Kees Cook323878a2013-02-05 15:35:24 -0800851
852 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800853 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800854 flag[0] = CAP_SETPCAP;
855 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
856 die("can't clear effective cap");
857 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
858 die("can't clear permitted cap");
859 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
860 die("can't clear inheritable cap");
861 }
862
863 if (cap_set_proc(caps))
864 die("can't apply final cleaned capset");
865
866 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400867}
868
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700869void set_seccomp_filter(const struct minijail *j)
870{
871 /*
872 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
873 * in the kernel source tree for an explanation of the parameters.
874 */
875 if (j->flags.no_new_privs) {
876 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
877 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
878 }
879
880 /*
881 * If we're logging seccomp filter failures,
882 * install the SIGSYS handler first.
883 */
884 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
885 if (install_sigsys_handler())
886 pdie("install SIGSYS handler");
887 warn("logging seccomp filter failures");
888 }
889
890 /*
891 * Install the syscall filter.
892 */
893 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700894 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
895 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
896 warn("seccomp not supported");
897 return;
898 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700899 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700900 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700901 }
902}
903
Will Drewry6ac91122011-10-21 16:38:58 -0500904void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400905{
906 if (j->flags.pids)
907 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700908 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400909
Elly Jonese1749eb2011-10-07 13:54:59 -0400910 if (j->flags.usergroups && !j->user)
911 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400912
Elly Jonesdd3e8512012-01-23 15:13:38 -0500913 /*
914 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400915 * so we don't even try. If any of our operations fail, we abort() the
916 * entire process.
917 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700918 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
919 pdie("setns(CLONE_NEWNS)");
920
Elly Jonese1749eb2011-10-07 13:54:59 -0400921 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400922 pdie("unshare(vfs)");
923
924 if (j->flags.net && unshare(CLONE_NEWNET))
925 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400926
Elly Jones51a5b6c2011-10-12 19:09:26 -0400927 if (j->flags.chroot && enter_chroot(j))
928 pdie("chroot");
929
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800930 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -0700931 pdie("mount_tmp");
932
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800933 if (j->flags.readonly && remount_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -0400934 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400935
Elly Jonese1749eb2011-10-07 13:54:59 -0400936 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500937 /*
938 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400939 * capability to change uids, our attempt to use setuid()
940 * below will fail. Hang on to root caps across setuid(), then
941 * lock securebits.
942 */
943 if (prctl(PR_SET_KEEPCAPS, 1))
944 pdie("prctl(PR_SET_KEEPCAPS)");
945 if (prctl
946 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
947 pdie("prctl(PR_SET_SECUREBITS)");
948 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400949
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700950 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700951 * If we're setting no_new_privs, we can drop privileges
952 * before setting seccomp filter. This way filter policies
953 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700954 */
955 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700956 drop_ugid(j);
957 if (j->flags.caps)
958 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700959
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700960 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400961 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700962 /*
963 * If we're not setting no_new_privs,
964 * we need to set seccomp filter *before* dropping privileges.
965 * WARNING: this means that filter policies *must* allow
966 * setgroups()/setresgid()/setresuid() for dropping root and
967 * capget()/capset()/prctl() for dropping caps.
968 */
969 set_seccomp_filter(j);
970
971 drop_ugid(j);
972 if (j->flags.caps)
973 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400974 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400975
Elly Jonesdd3e8512012-01-23 15:13:38 -0500976 /*
977 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400978 * privilege-dropping syscalls :)
979 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700980 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
981 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
982 warn("seccomp not supported");
983 return;
984 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400985 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700986 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400987}
988
Will Drewry6ac91122011-10-21 16:38:58 -0500989/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400990static int init_exitstatus = 0;
991
Will Drewry6ac91122011-10-21 16:38:58 -0500992void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400993{
994 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400995}
996
Will Drewry6ac91122011-10-21 16:38:58 -0500997int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400998{
999 pid_t pid;
1000 int status;
1001 /* so that we exit with the right status */
1002 signal(SIGTERM, init_term);
1003 /* TODO(wad) self jail with seccomp_filters here. */
1004 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001005 /*
1006 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001007 * left inside our pid namespace or we get a signal.
1008 */
1009 if (pid == rootpid)
1010 init_exitstatus = status;
1011 }
1012 if (!WIFEXITED(init_exitstatus))
1013 _exit(MINIJAIL_ERR_INIT);
1014 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001015}
1016
Will Drewry6ac91122011-10-21 16:38:58 -05001017int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001018{
1019 size_t sz = 0;
1020 size_t bytes = read(fd, &sz, sizeof(sz));
1021 char *buf;
1022 int r;
1023 if (sizeof(sz) != bytes)
1024 return -EINVAL;
1025 if (sz > USHRT_MAX) /* Arbitrary sanity check */
1026 return -E2BIG;
1027 buf = malloc(sz);
1028 if (!buf)
1029 return -ENOMEM;
1030 bytes = read(fd, buf, sz);
1031 if (bytes != sz) {
1032 free(buf);
1033 return -EINVAL;
1034 }
1035 r = minijail_unmarshal(j, buf, sz);
1036 free(buf);
1037 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001038}
1039
Will Drewry6ac91122011-10-21 16:38:58 -05001040int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001041{
1042 char *buf;
1043 size_t sz = minijail_size(j);
1044 ssize_t written;
1045 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001046
Elly Jonese1749eb2011-10-07 13:54:59 -04001047 if (!sz)
1048 return -EINVAL;
1049 buf = malloc(sz);
1050 r = minijail_marshal(j, buf, sz);
1051 if (r) {
1052 free(buf);
1053 return r;
1054 }
1055 /* Sends [size][minijail]. */
1056 written = write(fd, &sz, sizeof(sz));
1057 if (written != sizeof(sz)) {
1058 free(buf);
1059 return -EFAULT;
1060 }
1061 written = write(fd, buf, sz);
1062 if (written < 0 || (size_t) written != sz) {
1063 free(buf);
1064 return -EFAULT;
1065 }
1066 free(buf);
1067 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001068}
Elly Jonescd7a9042011-07-22 13:56:51 -04001069
Will Drewry6ac91122011-10-21 16:38:58 -05001070int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001071{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001072#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001073 /* Don't use LDPRELOAD on Brillo. */
1074 return 0;
1075#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001076 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1077 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1078 if (!newenv)
1079 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001080
Elly Jonese1749eb2011-10-07 13:54:59 -04001081 /* Only insert a separating space if we have something to separate... */
1082 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1083 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001084
Elly Jonese1749eb2011-10-07 13:54:59 -04001085 /* setenv() makes a copy of the string we give it */
1086 setenv(kLdPreloadEnvVar, newenv, 1);
1087 free(newenv);
1088 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001089#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001090}
1091
Will Drewry6ac91122011-10-21 16:38:58 -05001092int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001093{
1094 int r = pipe(fds);
1095 char fd_buf[11];
1096 if (r)
1097 return r;
1098 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1099 if (r <= 0)
1100 return -EINVAL;
1101 setenv(kFdEnvVar, fd_buf, 1);
1102 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001103}
1104
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001105int setup_pipe_end(int fds[2], size_t index)
1106{
1107 if (index > 1)
1108 return -1;
1109
1110 close(fds[1 - index]);
1111 return fds[index];
1112}
1113
1114int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1115{
1116 if (index > 1)
1117 return -1;
1118
1119 close(fds[1 - index]);
1120 /* dup2(2) the corresponding end of the pipe into |fd|. */
1121 return dup2(fds[index], fd);
1122}
1123
Will Drewry6ac91122011-10-21 16:38:58 -05001124int API minijail_run(struct minijail *j, const char *filename,
1125 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001126{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001127 return minijail_run_pid_pipes(j, filename, argv,
1128 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001129}
1130
1131int API minijail_run_pid(struct minijail *j, const char *filename,
1132 char *const argv[], pid_t *pchild_pid)
1133{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001134 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
1135 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001136}
1137
1138int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001139 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001140{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001141 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
1142 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001143}
1144
1145int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001146 char *const argv[], pid_t *pchild_pid,
1147 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001148{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001149 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
1150 NULL, NULL);
1151}
1152
1153int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001154 char *const argv[], pid_t *pchild_pid,
1155 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001156{
Elly Jonese1749eb2011-10-07 13:54:59 -04001157 char *oldenv, *oldenv_copy = NULL;
1158 pid_t child_pid;
1159 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001160 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001161 int stdout_fds[2];
1162 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001163 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001164 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001165 /* We need to remember this across the minijail_preexec() call. */
1166 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001167 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001168
Elly Jonese1749eb2011-10-07 13:54:59 -04001169 oldenv = getenv(kLdPreloadEnvVar);
1170 if (oldenv) {
1171 oldenv_copy = strdup(oldenv);
1172 if (!oldenv_copy)
1173 return -ENOMEM;
1174 }
Will Drewryf89aef52011-09-16 16:48:57 -05001175
Elly Jonese1749eb2011-10-07 13:54:59 -04001176 if (setup_preload())
1177 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001178
Elly Jonesdd3e8512012-01-23 15:13:38 -05001179 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001180 * Make the process group ID of this process equal to its PID, so that
1181 * both the Minijail process and the jailed process can be killed
1182 * together.
1183 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1184 * the process is already a process group leader.
1185 */
1186 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1187 if (errno != EPERM) {
1188 pdie("setpgid(0, 0)");
1189 }
1190 }
1191
1192 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001193 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -04001194 * a pipe(2) to send the minijail configuration over.
1195 */
1196 if (setup_pipe(pipe_fds))
1197 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -04001198
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001199 /*
1200 * If we want to write to the child process' standard input,
1201 * create the pipe(2) now.
1202 */
1203 if (pstdin_fd) {
1204 if (pipe(stdin_fds))
1205 return -EFAULT;
1206 }
1207
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001208 /*
1209 * If we want to read from the child process' standard output,
1210 * create the pipe(2) now.
1211 */
1212 if (pstdout_fd) {
1213 if (pipe(stdout_fds))
1214 return -EFAULT;
1215 }
1216
1217 /*
1218 * If we want to read from the child process' standard error,
1219 * create the pipe(2) now.
1220 */
1221 if (pstderr_fd) {
1222 if (pipe(stderr_fds))
1223 return -EFAULT;
1224 }
1225
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001226 /*
1227 * If we want to set up a new uid/gid mapping in the user namespace,
1228 * create the pipe(2) to sync between parent and child.
1229 */
1230 if (j->flags.userns) {
1231 if (pipe(userns_pipe_fds))
1232 return -EFAULT;
1233 }
1234
Elly Jones761b7412012-06-13 15:49:52 -04001235 /* Use sys_clone() if and only if we're creating a pid namespace.
1236 *
1237 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1238 *
1239 * In multithreaded programs, there are a bunch of locks inside libc,
1240 * some of which may be held by other threads at the time that we call
1241 * minijail_run_pid(). If we call fork(), glibc does its level best to
1242 * ensure that we hold all of these locks before it calls clone()
1243 * internally and drop them after clone() returns, but when we call
1244 * sys_clone(2) directly, all that gets bypassed and we end up with a
1245 * child address space where some of libc's important locks are held by
1246 * other threads (which did not get cloned, and hence will never release
1247 * those locks). This is okay so long as we call exec() immediately
1248 * after, but a bunch of seemingly-innocent libc functions like setenv()
1249 * take locks.
1250 *
1251 * Hence, only call sys_clone() if we need to, in order to get at pid
1252 * namespacing. If we follow this path, the child's address space might
1253 * have broken locks; you may only call functions that do not acquire
1254 * any locks.
1255 *
1256 * Unfortunately, fork() acquires every lock it can get its hands on, as
1257 * previously detailed, so this function is highly likely to deadlock
1258 * later on (see "deadlock here") if we're multithreaded.
1259 *
1260 * We might hack around this by having the clone()d child (init of the
1261 * pid namespace) return directly, rather than leaving the clone()d
1262 * process hanging around to be init for the new namespace (and having
1263 * its fork()ed child return in turn), but that process would be crippled
1264 * with its libc locks potentially broken. We might try fork()ing in the
1265 * parent before we clone() to ensure that we own all the locks, but
1266 * then we have to have the forked child hanging around consuming
1267 * resources (and possibly having file descriptors / shared memory
1268 * regions / etc attached). We'd need to keep the child around to avoid
1269 * having its children get reparented to init.
1270 *
1271 * TODO(ellyjones): figure out if the "forked child hanging around"
1272 * problem is fixable or not. It would be nice if we worked in this
1273 * case.
1274 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001275 if (pid_namespace) {
1276 int clone_flags = CLONE_NEWPID | SIGCHLD;
1277 if (j->flags.userns)
1278 clone_flags |= CLONE_NEWUSER;
1279 child_pid = syscall(SYS_clone, clone_flags, NULL);
1280 }
Elly Jones761b7412012-06-13 15:49:52 -04001281 else
1282 child_pid = fork();
1283
Elly Jonese1749eb2011-10-07 13:54:59 -04001284 if (child_pid < 0) {
1285 free(oldenv_copy);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001286 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001287 }
Will Drewryf89aef52011-09-16 16:48:57 -05001288
Elly Jonese1749eb2011-10-07 13:54:59 -04001289 if (child_pid) {
1290 /* Restore parent's LD_PRELOAD. */
1291 if (oldenv_copy) {
1292 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1293 free(oldenv_copy);
1294 } else {
1295 unsetenv(kLdPreloadEnvVar);
1296 }
1297 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001298
Elly Jonese1749eb2011-10-07 13:54:59 -04001299 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001300
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001301 if (j->flags.pid_file)
1302 write_pid_file(j);
1303
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001304 if (j->flags.userns)
1305 write_ugid_mappings(j, userns_pipe_fds);
1306
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001307 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001308 close(pipe_fds[0]); /* read endpoint */
1309 ret = minijail_to_fd(j, pipe_fds[1]);
1310 close(pipe_fds[1]); /* write endpoint */
1311 if (ret) {
1312 kill(j->initpid, SIGKILL);
1313 die("failed to send marshalled minijail");
1314 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001315
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001316 if (pchild_pid)
1317 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001318
1319 /*
1320 * If we want to write to the child process' standard input,
1321 * set up the write end of the pipe.
1322 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001323 if (pstdin_fd)
1324 *pstdin_fd = setup_pipe_end(stdin_fds,
1325 1 /* write end */);
1326
1327 /*
1328 * If we want to read from the child process' standard output,
1329 * set up the read end of the pipe.
1330 */
1331 if (pstdout_fd)
1332 *pstdout_fd = setup_pipe_end(stdout_fds,
1333 0 /* read end */);
1334
1335 /*
1336 * If we want to read from the child process' standard error,
1337 * set up the read end of the pipe.
1338 */
1339 if (pstderr_fd)
1340 *pstderr_fd = setup_pipe_end(stderr_fds,
1341 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001342
Elly Jonese1749eb2011-10-07 13:54:59 -04001343 return 0;
1344 }
1345 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001346
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001347
1348 if (j->flags.userns)
1349 enter_user_namespace(j, userns_pipe_fds);
1350
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001351 /*
1352 * If we want to write to the jailed process' standard input,
1353 * set up the read end of the pipe.
1354 */
1355 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001356 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1357 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001358 die("failed to set up stdin pipe");
1359 }
1360
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001361 /*
1362 * If we want to read from the jailed process' standard output,
1363 * set up the write end of the pipe.
1364 */
1365 if (pstdout_fd) {
1366 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1367 STDOUT_FILENO) < 0)
1368 die("failed to set up stdout pipe");
1369 }
1370
1371 /*
1372 * If we want to read from the jailed process' standard error,
1373 * set up the write end of the pipe.
1374 */
1375 if (pstderr_fd) {
1376 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1377 STDERR_FILENO) < 0)
1378 die("failed to set up stderr pipe");
1379 }
1380
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001381 /* Strip out flags that cannot be inherited across execve. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001382 minijail_preexec(j);
1383 /* Jail this process and its descendants... */
1384 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001385
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001386 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001387 /*
1388 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001389 * namespace. We don't want all programs we might exec to have
1390 * to know how to be init. Normally |do_init == 1| we fork off
1391 * a child to actually run the program. If |do_init == 0|, we
1392 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001393 *
1394 * If we're multithreaded, we'll probably deadlock here. See
1395 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001396 */
1397 child_pid = fork();
1398 if (child_pid < 0)
1399 _exit(child_pid);
1400 else if (child_pid > 0)
1401 init(child_pid); /* never returns */
1402 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001403
Elly Jonesdd3e8512012-01-23 15:13:38 -05001404 /*
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001405 * If we aren't pid-namespaced, or jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001406 * calling process
1407 * -> execve()-ing process
1408 * If we are:
1409 * calling process
1410 * -> init()-ing process
1411 * -> execve()-ing process
1412 */
1413 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001414}
1415
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001416int API minijail_run_static(struct minijail *j, const char *filename,
1417 char *const argv[])
1418{
1419 pid_t child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001420 int userns_pipe_fds[2];
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001421 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001422 int do_init = j->flags.do_init;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001423
1424 if (j->flags.caps)
1425 die("caps not supported with static targets");
1426
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001427 /*
1428 * If we want to set up a new uid/gid mapping in the user namespace,
1429 * create the pipe(2) to sync between parent and child.
1430 */
1431 if (j->flags.userns) {
1432 if (pipe(userns_pipe_fds))
1433 return -EFAULT;
1434 }
1435
1436 if (pid_namespace) {
1437 int clone_flags = CLONE_NEWPID | SIGCHLD;
1438 if (j->flags.userns)
1439 clone_flags |= CLONE_NEWUSER;
1440 child_pid = syscall(SYS_clone, clone_flags, NULL);
1441 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001442 else
1443 child_pid = fork();
1444
1445 if (child_pid < 0) {
1446 die("failed to fork child");
1447 }
1448 if (child_pid > 0 ) {
1449 j->initpid = child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001450
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001451 if (j->flags.pid_file)
1452 write_pid_file(j);
1453
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001454 if (j->flags.userns)
1455 write_ugid_mappings(j, userns_pipe_fds);
1456
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001457 return 0;
1458 }
1459
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001460 if (j->flags.userns)
1461 enter_user_namespace(j, userns_pipe_fds);
1462
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001463 /*
1464 * We can now drop this child into the sandbox
1465 * then execve the target.
1466 */
1467
1468 j->flags.pids = 0;
1469 minijail_enter(j);
1470
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001471 if (pid_namespace && do_init) {
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001472 /*
1473 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001474 * namespace. We don't want all programs we might exec to have
1475 * to know how to be init. Normally |do_init == 1| we fork off
1476 * a child to actually run the program. If |do_init == 0|, we
1477 * let the program keep pid 1 and be init.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001478 *
1479 * If we're multithreaded, we'll probably deadlock here. See
1480 * WARNING above.
1481 */
1482 child_pid = fork();
1483 if (child_pid < 0)
1484 _exit(child_pid);
1485 else if (child_pid > 0)
1486 init(child_pid); /* never returns */
1487 }
1488
1489 _exit(execve(filename, argv, environ));
1490}
1491
Will Drewry6ac91122011-10-21 16:38:58 -05001492int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001493{
1494 int st;
1495 if (kill(j->initpid, SIGTERM))
1496 return -errno;
1497 if (waitpid(j->initpid, &st, 0) < 0)
1498 return -errno;
1499 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001500}
1501
Will Drewry6ac91122011-10-21 16:38:58 -05001502int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001503{
1504 int st;
1505 if (waitpid(j->initpid, &st, 0) < 0)
1506 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001507
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001508 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001509 int error_status = st;
1510 if (WIFSIGNALED(st)) {
1511 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001512 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001513 j->initpid, signum);
1514 /*
1515 * We return MINIJAIL_ERR_JAIL if the process received
1516 * SIGSYS, which happens when a syscall is blocked by
1517 * seccomp filters.
1518 * If not, we do what bash(1) does:
1519 * $? = 128 + signum
1520 */
1521 if (signum == SIGSYS) {
1522 error_status = MINIJAIL_ERR_JAIL;
1523 } else {
1524 error_status = 128 + signum;
1525 }
1526 }
1527 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001528 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001529
1530 int exit_status = WEXITSTATUS(st);
1531 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001532 info("child process %d exited with status %d",
1533 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001534
1535 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001536}
1537
Will Drewry6ac91122011-10-21 16:38:58 -05001538void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001539{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001540 if (j->flags.seccomp_filter && j->filter_prog) {
1541 free(j->filter_prog->filter);
1542 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001543 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001544 while (j->bindings_head) {
1545 struct binding *b = j->bindings_head;
1546 j->bindings_head = j->bindings_head->next;
1547 free(b->dest);
1548 free(b->src);
1549 free(b);
1550 }
1551 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001552 if (j->user)
1553 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001554 if (j->chrootdir)
1555 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001556 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001557}