blob: 6f75f607e1c23d429753126254420c4225685ca7 [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;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +080098 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -070099 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800100 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800101 int pid_file:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400102 } flags;
103 uid_t uid;
104 gid_t gid;
105 gid_t usergid;
106 char *user;
107 uint64_t caps;
108 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700109 int mountns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800110 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400111 int binding_count;
112 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800113 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800114 char *uidmap;
115 char *gidmap;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800116 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400117 struct binding *bindings_head;
118 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500119};
120
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700121/*
122 * Strip out flags meant for the parent.
123 * We keep things that are not inherited across execve(2) (e.g. capabilities),
124 * or are easier to set after execve(2) (e.g. seccomp filters).
125 */
126void minijail_preenter(struct minijail *j)
127{
128 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700129 j->flags.enter_vfs = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700130 j->flags.readonly = 0;
131 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800132 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800133 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700134}
135
136/*
137 * Strip out flags meant for the child.
138 * We keep things that are inherited across execve(2).
139 */
140void minijail_preexec(struct minijail *j)
141{
142 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700143 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700144 int readonly = j->flags.readonly;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800145 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700146 if (j->user)
147 free(j->user);
148 j->user = NULL;
149 memset(&j->flags, 0, sizeof(j->flags));
150 /* Now restore anything we meant to keep. */
151 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700152 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700153 j->flags.readonly = readonly;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800154 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700155 /* Note, |pids| will already have been used before this call. */
156}
157
158/* Minijail API. */
159
Will Drewry6ac91122011-10-21 16:38:58 -0500160struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400161{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400162 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400163}
164
Will Drewry6ac91122011-10-21 16:38:58 -0500165void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400166{
167 if (uid == 0)
168 die("useless change to uid 0");
169 j->uid = uid;
170 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400171}
172
Will Drewry6ac91122011-10-21 16:38:58 -0500173void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400174{
175 if (gid == 0)
176 die("useless change to gid 0");
177 j->gid = gid;
178 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400179}
180
Will Drewry6ac91122011-10-21 16:38:58 -0500181int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400182{
183 char *buf = NULL;
184 struct passwd pw;
185 struct passwd *ppw = NULL;
186 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
187 if (sz == -1)
188 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400189
Elly Jonesdd3e8512012-01-23 15:13:38 -0500190 /*
191 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400192 * the maximum needed size of the buffer, so we don't have to search.
193 */
194 buf = malloc(sz);
195 if (!buf)
196 return -ENOMEM;
197 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500198 /*
199 * We're safe to free the buffer here. The strings inside pw point
200 * inside buf, but we don't use any of them; this leaves the pointers
201 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
202 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400203 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700204 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400205 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700206 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400207 minijail_change_uid(j, ppw->pw_uid);
208 j->user = strdup(user);
209 if (!j->user)
210 return -ENOMEM;
211 j->usergid = ppw->pw_gid;
212 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400213}
214
Will Drewry6ac91122011-10-21 16:38:58 -0500215int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400216{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700217 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700218 struct group gr;
219 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400220 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
221 if (sz == -1)
222 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400223
Elly Jonesdd3e8512012-01-23 15:13:38 -0500224 /*
225 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400226 * the maximum needed size of the buffer, so we don't have to search.
227 */
228 buf = malloc(sz);
229 if (!buf)
230 return -ENOMEM;
231 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500232 /*
233 * We're safe to free the buffer here. The strings inside gr point
234 * inside buf, but we don't use any of them; this leaves the pointers
235 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
236 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400237 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700238 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400239 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700240 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400241 minijail_change_gid(j, pgr->gr_gid);
242 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400243}
244
Will Drewry6ac91122011-10-21 16:38:58 -0500245void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400246{
247 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400248}
249
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700250void API minijail_no_new_privs(struct minijail *j)
251{
252 j->flags.no_new_privs = 1;
253}
254
Will Drewry6ac91122011-10-21 16:38:58 -0500255void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400256{
257 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500258}
259
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700260void API minijail_log_seccomp_filter_failures(struct minijail *j)
261{
262 j->flags.log_seccomp_filter = 1;
263}
264
Will Drewry6ac91122011-10-21 16:38:58 -0500265void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400266{
267 j->caps = capmask;
268 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400269}
270
Will Drewry6ac91122011-10-21 16:38:58 -0500271void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400272{
273 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400274}
275
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700276void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
277{
278 int ns_fd = open(ns_path, O_RDONLY);
279 if (ns_fd < 0) {
280 pdie("failed to open namespace '%s'", ns_path);
281 }
282 j->mountns_fd = ns_fd;
283 j->flags.enter_vfs = 1;
284}
285
Will Drewry6ac91122011-10-21 16:38:58 -0500286void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400287{
Elly Jonese58176c2012-01-23 11:46:17 -0500288 j->flags.vfs = 1;
289 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400290 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800291 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400292}
293
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400294void API minijail_namespace_net(struct minijail *j)
295{
296 j->flags.net = 1;
297}
298
Will Drewry6ac91122011-10-21 16:38:58 -0500299void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400300{
301 j->flags.vfs = 1;
302 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400303}
304
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800305void API minijail_namespace_user(struct minijail *j)
306{
307 j->flags.userns = 1;
308}
309
310int API minijail_uidmap(struct minijail *j, const char *uidmap)
311{
312 j->uidmap = strdup(uidmap);
313 if (!j->uidmap)
314 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800315 char *ch;
316 for (ch = j->uidmap; *ch; ch++) {
317 if (*ch == ',')
318 *ch = '\n';
319 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800320 return 0;
321}
322
323int API minijail_gidmap(struct minijail *j, const char *gidmap)
324{
325 j->gidmap = strdup(gidmap);
326 if (!j->gidmap)
327 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800328 char *ch;
329 for (ch = j->gidmap; *ch; ch++) {
330 if (*ch == ',')
331 *ch = '\n';
332 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800333 return 0;
334}
335
Will Drewry6ac91122011-10-21 16:38:58 -0500336void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400337{
338 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400339}
340
Will Drewry6ac91122011-10-21 16:38:58 -0500341void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400342{
343 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400344}
345
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800346void API minijail_run_as_init(struct minijail *j)
347{
348 /*
349 * Since the jailed program will become 'init' in the new PID namespace,
350 * Minijail does not need to fork an 'init' process.
351 */
352 j->flags.do_init = 0;
353}
354
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700355int API minijail_enter_chroot(struct minijail *j, const char *dir)
356{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400357 if (j->chrootdir)
358 return -EINVAL;
359 j->chrootdir = strdup(dir);
360 if (!j->chrootdir)
361 return -ENOMEM;
362 j->flags.chroot = 1;
363 return 0;
364}
365
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800366int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
367{
368 if (j->chrootdir)
369 return -EINVAL;
370 j->chrootdir = strdup(dir);
371 if (!j->chrootdir)
372 return -ENOMEM;
373 j->flags.pivot_root = 1;
374 return 0;
375}
376
Lee Campbell11af0622014-05-22 12:36:04 -0700377void API minijail_mount_tmp(struct minijail *j)
378{
379 j->flags.mount_tmp = 1;
380}
381
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800382int API minijail_write_pid_file(struct minijail *j, const char *path)
383{
384 j->pid_file_path = strdup(path);
385 if (!j->pid_file_path)
386 return -ENOMEM;
387 j->flags.pid_file = 1;
388 return 0;
389}
390
Will Drewry6ac91122011-10-21 16:38:58 -0500391int API minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700392 int writeable)
393{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400394 struct binding *b;
395
396 if (*dest != '/')
397 return -EINVAL;
398 b = calloc(1, sizeof(*b));
399 if (!b)
400 return -ENOMEM;
401 b->dest = strdup(dest);
402 if (!b->dest)
403 goto error;
404 b->src = strdup(src);
405 if (!b->src)
406 goto error;
407 b->writeable = writeable;
408
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700409 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400410
Elly Jonesdd3e8512012-01-23 15:13:38 -0500411 /*
412 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400413 * containing vfs namespace.
414 */
415 minijail_namespace_vfs(j);
416
417 if (j->bindings_tail)
418 j->bindings_tail->next = b;
419 else
420 j->bindings_head = b;
421 j->bindings_tail = b;
422 j->binding_count++;
423
424 return 0;
425
426error:
427 free(b->src);
428 free(b->dest);
429 free(b);
430 return -ENOMEM;
431}
432
Will Drewry6ac91122011-10-21 16:38:58 -0500433void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400434{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700435 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
436 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
437 warn("not loading seccomp filter, seccomp not supported");
438 return;
439 }
440 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400441 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800442 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700443 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400444 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800445
446 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700447 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
448 die("failed to compile seccomp filter BPF program in '%s'",
449 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800450 }
451
452 j->filter_len = fprog->len;
453 j->filter_prog = fprog;
454
Elly Jonese1749eb2011-10-07 13:54:59 -0400455 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500456}
457
Will Drewryf89aef52011-09-16 16:48:57 -0500458struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400459 size_t available;
460 size_t total;
461 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500462};
463
Will Drewry6ac91122011-10-21 16:38:58 -0500464void marshal_state_init(struct marshal_state *state,
465 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400466{
467 state->available = available;
468 state->buf = buf;
469 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500470}
471
Will Drewry6ac91122011-10-21 16:38:58 -0500472void marshal_append(struct marshal_state *state,
473 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400474{
475 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500476
Elly Jonese1749eb2011-10-07 13:54:59 -0400477 /* Up to |available| will be written. */
478 if (copy_len) {
479 memcpy(state->buf, src, copy_len);
480 state->buf += copy_len;
481 state->available -= copy_len;
482 }
483 /* |total| will contain the expected length. */
484 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500485}
486
Will Drewry6ac91122011-10-21 16:38:58 -0500487void minijail_marshal_helper(struct marshal_state *state,
488 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400489{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400490 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400491 marshal_append(state, (char *)j, sizeof(*j));
492 if (j->user)
493 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400494 if (j->chrootdir)
495 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800496 if (j->flags.seccomp_filter && j->filter_prog) {
497 struct sock_fprog *fp = j->filter_prog;
498 marshal_append(state, (char *)fp->filter,
499 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400500 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400501 for (b = j->bindings_head; b; b = b->next) {
502 marshal_append(state, b->src, strlen(b->src) + 1);
503 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700504 marshal_append(state, (char *)&b->writeable,
505 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400506 }
Will Drewryf89aef52011-09-16 16:48:57 -0500507}
508
Will Drewry6ac91122011-10-21 16:38:58 -0500509size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400510{
511 struct marshal_state state;
512 marshal_state_init(&state, NULL, 0);
513 minijail_marshal_helper(&state, j);
514 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500515}
516
Elly Jonese1749eb2011-10-07 13:54:59 -0400517int minijail_marshal(const struct minijail *j, char *buf, size_t available)
518{
519 struct marshal_state state;
520 marshal_state_init(&state, buf, available);
521 minijail_marshal_helper(&state, j);
522 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500523}
524
Elly Jones51a5b6c2011-10-12 19:09:26 -0400525/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
526 * @length Number of bytes to consume
527 * @buf Buffer to consume from
528 * @buflength Size of @buf
529 *
530 * Returns a pointer to the base of the bytes, or NULL for errors.
531 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700532void *consumebytes(size_t length, char **buf, size_t *buflength)
533{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400534 char *p = *buf;
535 if (length > *buflength)
536 return NULL;
537 *buf += length;
538 *buflength -= length;
539 return p;
540}
541
542/* consumestr: consumes a C string from a buffer @buf of length @length
543 * @buf Buffer to consume
544 * @length Length of buffer
545 *
546 * Returns a pointer to the base of the string, or NULL for errors.
547 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700548char *consumestr(char **buf, size_t *buflength)
549{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400550 size_t len = strnlen(*buf, *buflength);
551 if (len == *buflength)
552 /* There's no null-terminator */
553 return NULL;
554 return consumebytes(len + 1, buf, buflength);
555}
556
Elly Jonese1749eb2011-10-07 13:54:59 -0400557int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
558{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400559 int i;
560 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500561 int ret = -EINVAL;
562
Elly Jonese1749eb2011-10-07 13:54:59 -0400563 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500564 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400565 memcpy((void *)j, serialized, sizeof(*j));
566 serialized += sizeof(*j);
567 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500568
Will Drewrybee7ba72011-10-21 20:47:01 -0500569 /* Potentially stale pointers not used as signals. */
570 j->bindings_head = NULL;
571 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800572 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500573
Elly Jonese1749eb2011-10-07 13:54:59 -0400574 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400575 char *user = consumestr(&serialized, &length);
576 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500577 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400578 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500579 if (!j->user)
580 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400581 }
Will Drewryf89aef52011-09-16 16:48:57 -0500582
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400583 if (j->chrootdir) { /* stale pointer */
584 char *chrootdir = consumestr(&serialized, &length);
585 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500586 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400587 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500588 if (!j->chrootdir)
589 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400590 }
591
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800592 if (j->flags.seccomp_filter && j->filter_len > 0) {
593 size_t ninstrs = j->filter_len;
594 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
595 ninstrs > USHRT_MAX)
596 goto bad_filters;
597
598 size_t program_len = ninstrs * sizeof(struct sock_filter);
599 void *program = consumebytes(program_len, &serialized, &length);
600 if (!program)
601 goto bad_filters;
602
603 j->filter_prog = malloc(sizeof(struct sock_fprog));
604 j->filter_prog->len = ninstrs;
605 j->filter_prog->filter = malloc(program_len);
606 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400607 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400608
609 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400610 j->binding_count = 0;
611 for (i = 0; i < count; ++i) {
612 int *writeable;
613 const char *dest;
614 const char *src = consumestr(&serialized, &length);
615 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500616 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400617 dest = consumestr(&serialized, &length);
618 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500619 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400620 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
621 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500622 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400623 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500624 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400625 }
626
Elly Jonese1749eb2011-10-07 13:54:59 -0400627 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500628
629bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800630 if (j->flags.seccomp_filter && j->filter_len > 0) {
631 free(j->filter_prog->filter);
632 free(j->filter_prog);
633 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500634bad_filters:
635 if (j->chrootdir)
636 free(j->chrootdir);
637bad_chrootdir:
638 if (j->user)
639 free(j->user);
640clear_pointers:
641 j->user = NULL;
642 j->chrootdir = NULL;
643out:
644 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500645}
646
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800647static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
648{
649 int fd, ret, len;
650 size_t sz;
651 char fname[32];
652 close(pipe_fds[0]);
653
654 sz = sizeof(fname);
655 if (j->uidmap) {
656 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
657 if (ret < 0 || ret >= sz)
658 die("failed to write file name of uid_map");
659 fd = open(fname, O_WRONLY);
660 if (fd < 0)
661 pdie("failed to open '%s'", fname);
662 len = strlen(j->uidmap);
663 if (write(fd, j->uidmap, len) < len)
664 die("failed to set uid_map");
665 close(fd);
666 }
667 if (j->gidmap) {
668 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
669 if (ret < 0 || ret >= sz)
670 die("failed to write file name of gid_map");
671 fd = open(fname, O_WRONLY);
672 if (fd < 0)
673 pdie("failed to open '%s'", fname);
674 len = strlen(j->gidmap);
675 if (write(fd, j->gidmap, len) < len)
676 die("failed to set gid_map");
677 close(fd);
678 }
679
680 close(pipe_fds[1]);
681}
682
683static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
684{
685 char buf;
686
687 close(pipe_fds[1]);
688
689 /* Wait for parent to set up uid/gid mappings. */
690 if (read(pipe_fds[0], &buf, 1) != 0)
691 die("failed to sync with parent");
692 close(pipe_fds[0]);
693
694 if (j->uidmap && setresuid(0, 0, 0))
695 pdie("setresuid");
696 if (j->gidmap && setresgid(0, 0, 0))
697 pdie("setresgid");
698}
699
Elly Jones51a5b6c2011-10-12 19:09:26 -0400700/* bind_one: Applies bindings from @b for @j, recursing as needed.
701 * @j Minijail these bindings are for
702 * @b Head of list of bindings
703 *
704 * Returns 0 for success.
705 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700706int bind_one(const struct minijail *j, struct binding *b)
707{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400708 int ret = 0;
709 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400710 if (ret)
711 return ret;
712 /* dest has a leading "/" */
713 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
714 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500715 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400716 if (ret)
717 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500718 if (!b->writeable) {
719 ret = mount(b->src, dest, NULL,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700720 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
Elly Jonesa1059632011-12-15 15:17:07 -0500721 if (ret)
722 pdie("bind ro: %s -> %s", b->src, dest);
723 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400724 free(dest);
725 if (b->next)
726 return bind_one(j, b->next);
727 return ret;
728}
729
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700730int enter_chroot(const struct minijail *j)
731{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400732 int ret;
733 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
734 return ret;
735
736 if (chroot(j->chrootdir))
737 return -errno;
738
739 if (chdir("/"))
740 return -errno;
741
742 return 0;
743}
744
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800745int enter_pivot_root(const struct minijail *j)
746{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800747 int ret, oldroot, newroot;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800748 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
749 return ret;
750
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800751 /* Keep the fd for both old and new root. It will be used in fchdir later. */
752 oldroot = open("/", O_DIRECTORY | O_RDONLY);
753 if (oldroot < 0)
754 pdie("failed to open / for fchdir");
755 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
756 if (newroot < 0)
757 pdie("failed to open %s for fchdir", j->chrootdir);
758
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800759 /* To ensure chrootdir is the root of a file system, do a self bind mount. */
760 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
761 pdie("failed to bind mount '%s'", j->chrootdir);
762 if (chdir(j->chrootdir))
763 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800764 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800765 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800766
767 /*
768 * Now the old root is mounted on top of the new root. Use fchdir to
769 * change to the old root and unmount it.
770 */
771 if (fchdir(oldroot))
772 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800773 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800774 if (umount2(".", MNT_DETACH))
775 pdie("umount(/)");
776 /* Change back to the new root. */
777 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800778 return -errno;
779 if (chroot("/"))
780 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800781
782 return 0;
783}
784
Lee Campbell11af0622014-05-22 12:36:04 -0700785int mount_tmp(void)
786{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800787 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700788}
789
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800790int remount_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400791{
792 const char *kProcPath = "/proc";
793 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500794 /*
795 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400796 * /proc in our namespace, which means using MS_REMOUNT here would
797 * mutate our parent's mount as well, even though we're in a VFS
798 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800799 * and make our own. However, if we are in a new user namespace, /proc
800 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400801 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800802 if (umount(kProcPath) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400803 return -errno;
804 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
805 return -errno;
806 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400807}
808
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800809static void write_pid_file(const struct minijail *j)
810{
811 FILE *fp = fopen(j->pid_file_path, "w");
812
813 if (!fp)
814 pdie("failed to open '%s'", j->pid_file_path);
815 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
816 pdie("fprintf(%s)", j->pid_file_path);
817 if (fclose(fp))
818 pdie("fclose(%s)", j->pid_file_path);
819}
820
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700821void drop_ugid(const struct minijail *j)
822{
823 if (j->flags.usergroups) {
824 if (initgroups(j->user, j->usergid))
825 pdie("initgroups");
826 } else {
827 /* Only attempt to clear supplemental groups if we are changing
828 * users. */
829 if ((j->uid || j->gid) && setgroups(0, NULL))
830 pdie("setgroups");
831 }
832
833 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
834 pdie("setresgid");
835
836 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
837 pdie("setresuid");
838}
839
Mike Frysinger3adfef72013-05-09 17:19:08 -0400840/*
841 * We specifically do not use cap_valid() as that only tells us the last
842 * valid cap we were *compiled* against (i.e. what the version of kernel
843 * headers says). If we run on a different kernel version, then it's not
844 * uncommon for that to be less (if an older kernel) or more (if a newer
845 * kernel). So suck up the answer via /proc.
846 */
847static int run_cap_valid(unsigned int cap)
848{
849 static unsigned int last_cap;
850
851 if (!last_cap) {
852 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
853 FILE *fp = fopen(cap_file, "re");
854 if (fscanf(fp, "%u", &last_cap) != 1)
855 pdie("fscanf(%s)", cap_file);
856 fclose(fp);
857 }
858
859 return cap <= last_cap;
860}
861
Will Drewry6ac91122011-10-21 16:38:58 -0500862void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400863{
864 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800865 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800866 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400867 unsigned int i;
868 if (!caps)
869 die("can't get process caps");
870 if (cap_clear_flag(caps, CAP_INHERITABLE))
871 die("can't clear inheritable caps");
872 if (cap_clear_flag(caps, CAP_EFFECTIVE))
873 die("can't clear effective caps");
874 if (cap_clear_flag(caps, CAP_PERMITTED))
875 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400876 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800877 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800878 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400879 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800880 flag[0] = i;
881 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400882 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800883 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400884 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800885 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400886 die("can't add inheritable cap");
887 }
888 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800889 die("can't apply initial cleaned capset");
890
891 /*
892 * Instead of dropping bounding set first, do it here in case
893 * the caller had a more permissive bounding set which could
894 * have been used above to raise a capability that wasn't already
895 * present. This requires CAP_SETPCAP, so we raised/kept it above.
896 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400897 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800898 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400899 continue;
900 if (prctl(PR_CAPBSET_DROP, i))
901 pdie("prctl(PR_CAPBSET_DROP)");
902 }
Kees Cook323878a2013-02-05 15:35:24 -0800903
904 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800905 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800906 flag[0] = CAP_SETPCAP;
907 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
908 die("can't clear effective cap");
909 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
910 die("can't clear permitted cap");
911 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
912 die("can't clear inheritable cap");
913 }
914
915 if (cap_set_proc(caps))
916 die("can't apply final cleaned capset");
917
918 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400919}
920
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700921void set_seccomp_filter(const struct minijail *j)
922{
923 /*
924 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
925 * in the kernel source tree for an explanation of the parameters.
926 */
927 if (j->flags.no_new_privs) {
928 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
929 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
930 }
931
932 /*
933 * If we're logging seccomp filter failures,
934 * install the SIGSYS handler first.
935 */
936 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
937 if (install_sigsys_handler())
938 pdie("install SIGSYS handler");
939 warn("logging seccomp filter failures");
940 }
941
942 /*
943 * Install the syscall filter.
944 */
945 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700946 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
947 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
948 warn("seccomp not supported");
949 return;
950 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700951 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700952 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700953 }
954}
955
Will Drewry6ac91122011-10-21 16:38:58 -0500956void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400957{
958 if (j->flags.pids)
959 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700960 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400961
Elly Jonese1749eb2011-10-07 13:54:59 -0400962 if (j->flags.usergroups && !j->user)
963 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400964
Elly Jonesdd3e8512012-01-23 15:13:38 -0500965 /*
966 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400967 * so we don't even try. If any of our operations fail, we abort() the
968 * entire process.
969 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700970 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
971 pdie("setns(CLONE_NEWNS)");
972
Elly Jonese1749eb2011-10-07 13:54:59 -0400973 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400974 pdie("unshare(vfs)");
975
976 if (j->flags.net && unshare(CLONE_NEWNET))
977 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400978
Elly Jones51a5b6c2011-10-12 19:09:26 -0400979 if (j->flags.chroot && enter_chroot(j))
980 pdie("chroot");
981
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800982 if (j->flags.pivot_root && enter_pivot_root(j))
983 pdie("pivot_root");
984
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800985 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -0700986 pdie("mount_tmp");
987
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800988 if (j->flags.readonly && remount_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -0400989 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400990
Elly Jonese1749eb2011-10-07 13:54:59 -0400991 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500992 /*
993 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400994 * capability to change uids, our attempt to use setuid()
995 * below will fail. Hang on to root caps across setuid(), then
996 * lock securebits.
997 */
998 if (prctl(PR_SET_KEEPCAPS, 1))
999 pdie("prctl(PR_SET_KEEPCAPS)");
1000 if (prctl
1001 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1002 pdie("prctl(PR_SET_SECUREBITS)");
1003 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001004
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001005 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001006 * If we're setting no_new_privs, we can drop privileges
1007 * before setting seccomp filter. This way filter policies
1008 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001009 */
1010 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001011 drop_ugid(j);
1012 if (j->flags.caps)
1013 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001014
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001015 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001016 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001017 /*
1018 * If we're not setting no_new_privs,
1019 * we need to set seccomp filter *before* dropping privileges.
1020 * WARNING: this means that filter policies *must* allow
1021 * setgroups()/setresgid()/setresuid() for dropping root and
1022 * capget()/capset()/prctl() for dropping caps.
1023 */
1024 set_seccomp_filter(j);
1025
1026 drop_ugid(j);
1027 if (j->flags.caps)
1028 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001029 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001030
Elly Jonesdd3e8512012-01-23 15:13:38 -05001031 /*
1032 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001033 * privilege-dropping syscalls :)
1034 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001035 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1036 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1037 warn("seccomp not supported");
1038 return;
1039 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001040 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001041 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001042}
1043
Will Drewry6ac91122011-10-21 16:38:58 -05001044/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001045static int init_exitstatus = 0;
1046
Will Drewry6ac91122011-10-21 16:38:58 -05001047void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001048{
1049 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001050}
1051
Will Drewry6ac91122011-10-21 16:38:58 -05001052int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001053{
1054 pid_t pid;
1055 int status;
1056 /* so that we exit with the right status */
1057 signal(SIGTERM, init_term);
1058 /* TODO(wad) self jail with seccomp_filters here. */
1059 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001060 /*
1061 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001062 * left inside our pid namespace or we get a signal.
1063 */
1064 if (pid == rootpid)
1065 init_exitstatus = status;
1066 }
1067 if (!WIFEXITED(init_exitstatus))
1068 _exit(MINIJAIL_ERR_INIT);
1069 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001070}
1071
Will Drewry6ac91122011-10-21 16:38:58 -05001072int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001073{
1074 size_t sz = 0;
1075 size_t bytes = read(fd, &sz, sizeof(sz));
1076 char *buf;
1077 int r;
1078 if (sizeof(sz) != bytes)
1079 return -EINVAL;
1080 if (sz > USHRT_MAX) /* Arbitrary sanity check */
1081 return -E2BIG;
1082 buf = malloc(sz);
1083 if (!buf)
1084 return -ENOMEM;
1085 bytes = read(fd, buf, sz);
1086 if (bytes != sz) {
1087 free(buf);
1088 return -EINVAL;
1089 }
1090 r = minijail_unmarshal(j, buf, sz);
1091 free(buf);
1092 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001093}
1094
Will Drewry6ac91122011-10-21 16:38:58 -05001095int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001096{
1097 char *buf;
1098 size_t sz = minijail_size(j);
1099 ssize_t written;
1100 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001101
Elly Jonese1749eb2011-10-07 13:54:59 -04001102 if (!sz)
1103 return -EINVAL;
1104 buf = malloc(sz);
1105 r = minijail_marshal(j, buf, sz);
1106 if (r) {
1107 free(buf);
1108 return r;
1109 }
1110 /* Sends [size][minijail]. */
1111 written = write(fd, &sz, sizeof(sz));
1112 if (written != sizeof(sz)) {
1113 free(buf);
1114 return -EFAULT;
1115 }
1116 written = write(fd, buf, sz);
1117 if (written < 0 || (size_t) written != sz) {
1118 free(buf);
1119 return -EFAULT;
1120 }
1121 free(buf);
1122 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001123}
Elly Jonescd7a9042011-07-22 13:56:51 -04001124
Will Drewry6ac91122011-10-21 16:38:58 -05001125int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001126{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001127#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001128 /* Don't use LDPRELOAD on Brillo. */
1129 return 0;
1130#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001131 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1132 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1133 if (!newenv)
1134 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001135
Elly Jonese1749eb2011-10-07 13:54:59 -04001136 /* Only insert a separating space if we have something to separate... */
1137 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1138 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001139
Elly Jonese1749eb2011-10-07 13:54:59 -04001140 /* setenv() makes a copy of the string we give it */
1141 setenv(kLdPreloadEnvVar, newenv, 1);
1142 free(newenv);
1143 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001144#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001145}
1146
Will Drewry6ac91122011-10-21 16:38:58 -05001147int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001148{
1149 int r = pipe(fds);
1150 char fd_buf[11];
1151 if (r)
1152 return r;
1153 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1154 if (r <= 0)
1155 return -EINVAL;
1156 setenv(kFdEnvVar, fd_buf, 1);
1157 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001158}
1159
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001160int setup_pipe_end(int fds[2], size_t index)
1161{
1162 if (index > 1)
1163 return -1;
1164
1165 close(fds[1 - index]);
1166 return fds[index];
1167}
1168
1169int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1170{
1171 if (index > 1)
1172 return -1;
1173
1174 close(fds[1 - index]);
1175 /* dup2(2) the corresponding end of the pipe into |fd|. */
1176 return dup2(fds[index], fd);
1177}
1178
Will Drewry6ac91122011-10-21 16:38:58 -05001179int API minijail_run(struct minijail *j, const char *filename,
1180 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001181{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001182 return minijail_run_pid_pipes(j, filename, argv,
1183 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001184}
1185
1186int API minijail_run_pid(struct minijail *j, const char *filename,
1187 char *const argv[], pid_t *pchild_pid)
1188{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001189 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
1190 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001191}
1192
1193int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001194 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001195{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001196 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
1197 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001198}
1199
1200int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001201 char *const argv[], pid_t *pchild_pid,
1202 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001203{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001204 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
1205 NULL, NULL);
1206}
1207
1208int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001209 char *const argv[], pid_t *pchild_pid,
1210 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001211{
Elly Jonese1749eb2011-10-07 13:54:59 -04001212 char *oldenv, *oldenv_copy = NULL;
1213 pid_t child_pid;
1214 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001215 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001216 int stdout_fds[2];
1217 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001218 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001219 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001220 /* We need to remember this across the minijail_preexec() call. */
1221 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001222 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001223
Elly Jonese1749eb2011-10-07 13:54:59 -04001224 oldenv = getenv(kLdPreloadEnvVar);
1225 if (oldenv) {
1226 oldenv_copy = strdup(oldenv);
1227 if (!oldenv_copy)
1228 return -ENOMEM;
1229 }
Will Drewryf89aef52011-09-16 16:48:57 -05001230
Elly Jonese1749eb2011-10-07 13:54:59 -04001231 if (setup_preload())
1232 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001233
Elly Jonesdd3e8512012-01-23 15:13:38 -05001234 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001235 * Make the process group ID of this process equal to its PID, so that
1236 * both the Minijail process and the jailed process can be killed
1237 * together.
1238 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1239 * the process is already a process group leader.
1240 */
1241 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1242 if (errno != EPERM) {
1243 pdie("setpgid(0, 0)");
1244 }
1245 }
1246
1247 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001248 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -04001249 * a pipe(2) to send the minijail configuration over.
1250 */
1251 if (setup_pipe(pipe_fds))
1252 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -04001253
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001254 /*
1255 * If we want to write to the child process' standard input,
1256 * create the pipe(2) now.
1257 */
1258 if (pstdin_fd) {
1259 if (pipe(stdin_fds))
1260 return -EFAULT;
1261 }
1262
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001263 /*
1264 * If we want to read from the child process' standard output,
1265 * create the pipe(2) now.
1266 */
1267 if (pstdout_fd) {
1268 if (pipe(stdout_fds))
1269 return -EFAULT;
1270 }
1271
1272 /*
1273 * If we want to read from the child process' standard error,
1274 * create the pipe(2) now.
1275 */
1276 if (pstderr_fd) {
1277 if (pipe(stderr_fds))
1278 return -EFAULT;
1279 }
1280
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001281 /*
1282 * If we want to set up a new uid/gid mapping in the user namespace,
1283 * create the pipe(2) to sync between parent and child.
1284 */
1285 if (j->flags.userns) {
1286 if (pipe(userns_pipe_fds))
1287 return -EFAULT;
1288 }
1289
Elly Jones761b7412012-06-13 15:49:52 -04001290 /* Use sys_clone() if and only if we're creating a pid namespace.
1291 *
1292 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1293 *
1294 * In multithreaded programs, there are a bunch of locks inside libc,
1295 * some of which may be held by other threads at the time that we call
1296 * minijail_run_pid(). If we call fork(), glibc does its level best to
1297 * ensure that we hold all of these locks before it calls clone()
1298 * internally and drop them after clone() returns, but when we call
1299 * sys_clone(2) directly, all that gets bypassed and we end up with a
1300 * child address space where some of libc's important locks are held by
1301 * other threads (which did not get cloned, and hence will never release
1302 * those locks). This is okay so long as we call exec() immediately
1303 * after, but a bunch of seemingly-innocent libc functions like setenv()
1304 * take locks.
1305 *
1306 * Hence, only call sys_clone() if we need to, in order to get at pid
1307 * namespacing. If we follow this path, the child's address space might
1308 * have broken locks; you may only call functions that do not acquire
1309 * any locks.
1310 *
1311 * Unfortunately, fork() acquires every lock it can get its hands on, as
1312 * previously detailed, so this function is highly likely to deadlock
1313 * later on (see "deadlock here") if we're multithreaded.
1314 *
1315 * We might hack around this by having the clone()d child (init of the
1316 * pid namespace) return directly, rather than leaving the clone()d
1317 * process hanging around to be init for the new namespace (and having
1318 * its fork()ed child return in turn), but that process would be crippled
1319 * with its libc locks potentially broken. We might try fork()ing in the
1320 * parent before we clone() to ensure that we own all the locks, but
1321 * then we have to have the forked child hanging around consuming
1322 * resources (and possibly having file descriptors / shared memory
1323 * regions / etc attached). We'd need to keep the child around to avoid
1324 * having its children get reparented to init.
1325 *
1326 * TODO(ellyjones): figure out if the "forked child hanging around"
1327 * problem is fixable or not. It would be nice if we worked in this
1328 * case.
1329 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001330 if (pid_namespace) {
1331 int clone_flags = CLONE_NEWPID | SIGCHLD;
1332 if (j->flags.userns)
1333 clone_flags |= CLONE_NEWUSER;
1334 child_pid = syscall(SYS_clone, clone_flags, NULL);
1335 }
Elly Jones761b7412012-06-13 15:49:52 -04001336 else
1337 child_pid = fork();
1338
Elly Jonese1749eb2011-10-07 13:54:59 -04001339 if (child_pid < 0) {
1340 free(oldenv_copy);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001341 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001342 }
Will Drewryf89aef52011-09-16 16:48:57 -05001343
Elly Jonese1749eb2011-10-07 13:54:59 -04001344 if (child_pid) {
1345 /* Restore parent's LD_PRELOAD. */
1346 if (oldenv_copy) {
1347 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1348 free(oldenv_copy);
1349 } else {
1350 unsetenv(kLdPreloadEnvVar);
1351 }
1352 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001353
Elly Jonese1749eb2011-10-07 13:54:59 -04001354 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001355
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001356 if (j->flags.pid_file)
1357 write_pid_file(j);
1358
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001359 if (j->flags.userns)
1360 write_ugid_mappings(j, userns_pipe_fds);
1361
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001362 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001363 close(pipe_fds[0]); /* read endpoint */
1364 ret = minijail_to_fd(j, pipe_fds[1]);
1365 close(pipe_fds[1]); /* write endpoint */
1366 if (ret) {
1367 kill(j->initpid, SIGKILL);
1368 die("failed to send marshalled minijail");
1369 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001370
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001371 if (pchild_pid)
1372 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001373
1374 /*
1375 * If we want to write to the child process' standard input,
1376 * set up the write end of the pipe.
1377 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001378 if (pstdin_fd)
1379 *pstdin_fd = setup_pipe_end(stdin_fds,
1380 1 /* write end */);
1381
1382 /*
1383 * If we want to read from the child process' standard output,
1384 * set up the read end of the pipe.
1385 */
1386 if (pstdout_fd)
1387 *pstdout_fd = setup_pipe_end(stdout_fds,
1388 0 /* read end */);
1389
1390 /*
1391 * If we want to read from the child process' standard error,
1392 * set up the read end of the pipe.
1393 */
1394 if (pstderr_fd)
1395 *pstderr_fd = setup_pipe_end(stderr_fds,
1396 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001397
Elly Jonese1749eb2011-10-07 13:54:59 -04001398 return 0;
1399 }
1400 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001401
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001402
1403 if (j->flags.userns)
1404 enter_user_namespace(j, userns_pipe_fds);
1405
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001406 /*
1407 * If we want to write to the jailed process' standard input,
1408 * set up the read end of the pipe.
1409 */
1410 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001411 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1412 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001413 die("failed to set up stdin pipe");
1414 }
1415
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001416 /*
1417 * If we want to read from the jailed process' standard output,
1418 * set up the write end of the pipe.
1419 */
1420 if (pstdout_fd) {
1421 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1422 STDOUT_FILENO) < 0)
1423 die("failed to set up stdout pipe");
1424 }
1425
1426 /*
1427 * If we want to read from the jailed process' standard error,
1428 * set up the write end of the pipe.
1429 */
1430 if (pstderr_fd) {
1431 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1432 STDERR_FILENO) < 0)
1433 die("failed to set up stderr pipe");
1434 }
1435
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001436 /* Strip out flags that cannot be inherited across execve. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001437 minijail_preexec(j);
1438 /* Jail this process and its descendants... */
1439 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001440
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001441 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001442 /*
1443 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001444 * namespace. We don't want all programs we might exec to have
1445 * to know how to be init. Normally |do_init == 1| we fork off
1446 * a child to actually run the program. If |do_init == 0|, we
1447 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001448 *
1449 * If we're multithreaded, we'll probably deadlock here. See
1450 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001451 */
1452 child_pid = fork();
1453 if (child_pid < 0)
1454 _exit(child_pid);
1455 else if (child_pid > 0)
1456 init(child_pid); /* never returns */
1457 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001458
Elly Jonesdd3e8512012-01-23 15:13:38 -05001459 /*
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001460 * If we aren't pid-namespaced, or jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001461 * calling process
1462 * -> execve()-ing process
1463 * If we are:
1464 * calling process
1465 * -> init()-ing process
1466 * -> execve()-ing process
1467 */
1468 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001469}
1470
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001471int API minijail_run_static(struct minijail *j, const char *filename,
1472 char *const argv[])
1473{
1474 pid_t child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001475 int userns_pipe_fds[2];
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001476 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001477 int do_init = j->flags.do_init;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001478
1479 if (j->flags.caps)
1480 die("caps not supported with static targets");
1481
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001482 /*
1483 * If we want to set up a new uid/gid mapping in the user namespace,
1484 * create the pipe(2) to sync between parent and child.
1485 */
1486 if (j->flags.userns) {
1487 if (pipe(userns_pipe_fds))
1488 return -EFAULT;
1489 }
1490
1491 if (pid_namespace) {
1492 int clone_flags = CLONE_NEWPID | SIGCHLD;
1493 if (j->flags.userns)
1494 clone_flags |= CLONE_NEWUSER;
1495 child_pid = syscall(SYS_clone, clone_flags, NULL);
1496 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001497 else
1498 child_pid = fork();
1499
1500 if (child_pid < 0) {
1501 die("failed to fork child");
1502 }
1503 if (child_pid > 0 ) {
1504 j->initpid = child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001505
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001506 if (j->flags.pid_file)
1507 write_pid_file(j);
1508
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001509 if (j->flags.userns)
1510 write_ugid_mappings(j, userns_pipe_fds);
1511
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001512 return 0;
1513 }
1514
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001515 if (j->flags.userns)
1516 enter_user_namespace(j, userns_pipe_fds);
1517
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001518 /*
1519 * We can now drop this child into the sandbox
1520 * then execve the target.
1521 */
1522
1523 j->flags.pids = 0;
1524 minijail_enter(j);
1525
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001526 if (pid_namespace && do_init) {
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001527 /*
1528 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001529 * namespace. We don't want all programs we might exec to have
1530 * to know how to be init. Normally |do_init == 1| we fork off
1531 * a child to actually run the program. If |do_init == 0|, we
1532 * let the program keep pid 1 and be init.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001533 *
1534 * If we're multithreaded, we'll probably deadlock here. See
1535 * WARNING above.
1536 */
1537 child_pid = fork();
1538 if (child_pid < 0)
1539 _exit(child_pid);
1540 else if (child_pid > 0)
1541 init(child_pid); /* never returns */
1542 }
1543
1544 _exit(execve(filename, argv, environ));
1545}
1546
Will Drewry6ac91122011-10-21 16:38:58 -05001547int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001548{
1549 int st;
1550 if (kill(j->initpid, SIGTERM))
1551 return -errno;
1552 if (waitpid(j->initpid, &st, 0) < 0)
1553 return -errno;
1554 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001555}
1556
Will Drewry6ac91122011-10-21 16:38:58 -05001557int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001558{
1559 int st;
1560 if (waitpid(j->initpid, &st, 0) < 0)
1561 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001562
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001563 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001564 int error_status = st;
1565 if (WIFSIGNALED(st)) {
1566 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001567 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001568 j->initpid, signum);
1569 /*
1570 * We return MINIJAIL_ERR_JAIL if the process received
1571 * SIGSYS, which happens when a syscall is blocked by
1572 * seccomp filters.
1573 * If not, we do what bash(1) does:
1574 * $? = 128 + signum
1575 */
1576 if (signum == SIGSYS) {
1577 error_status = MINIJAIL_ERR_JAIL;
1578 } else {
1579 error_status = 128 + signum;
1580 }
1581 }
1582 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001583 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001584
1585 int exit_status = WEXITSTATUS(st);
1586 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001587 info("child process %d exited with status %d",
1588 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001589
1590 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001591}
1592
Will Drewry6ac91122011-10-21 16:38:58 -05001593void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001594{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001595 if (j->flags.seccomp_filter && j->filter_prog) {
1596 free(j->filter_prog->filter);
1597 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001598 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001599 while (j->bindings_head) {
1600 struct binding *b = j->bindings_head;
1601 j->bindings_head = j->bindings_head->next;
1602 free(b->dest);
1603 free(b->src);
1604 free(b);
1605 }
1606 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001607 if (j->user)
1608 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001609 if (j->chrootdir)
1610 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001611 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001612}