Jorge Lucangeli Obes | d613ab2 | 2015-03-03 14:22:50 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 3 | * found in the LICENSE file. |
| 4 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 5 | |
| 6 | #define _BSD_SOURCE |
| 7 | #define _GNU_SOURCE |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 8 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 9 | #include <asm/unistd.h> |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 10 | #include <ctype.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 11 | #include <errno.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 12 | #include <fcntl.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 13 | #include <grp.h> |
| 14 | #include <inttypes.h> |
Will Drewry | fe4a372 | 2011-09-16 14:50:50 -0500 | [diff] [blame] | 15 | #include <limits.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 16 | #include <linux/capability.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 17 | #include <pwd.h> |
| 18 | #include <sched.h> |
| 19 | #include <signal.h> |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 20 | #include <stdarg.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 21 | #include <stddef.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 22 | #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 Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 28 | #include <sys/param.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 29 | #include <sys/prctl.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 32 | #include <sys/user.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 33 | #include <sys/wait.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
| 36 | #include "libminijail.h" |
| 37 | #include "libminijail-private.h" |
| 38 | |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 39 | #include "signal_handler.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 40 | #include "syscall_filter.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 41 | #include "util.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 42 | |
Lei Zhang | eee3155 | 2012-10-17 21:27:10 -0700 | [diff] [blame] | 43 | #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 Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 50 | /* Until these are reliably available in linux/prctl.h */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 51 | #ifndef PR_SET_SECCOMP |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 52 | # define PR_SET_SECCOMP 22 |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 53 | #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 Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 61 | #endif |
| 62 | |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 63 | #ifdef USE_SECCOMP_SOFTFAIL |
| 64 | # define SECCOMP_SOFTFAIL 1 |
| 65 | #else |
| 66 | # define SECCOMP_SOFTFAIL 0 |
| 67 | #endif |
| 68 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 69 | struct binding { |
| 70 | char *src; |
| 71 | char *dest; |
| 72 | int writeable; |
| 73 | struct binding *next; |
| 74 | }; |
| 75 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 76 | struct minijail { |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 77 | /* |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 78 | * 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 Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 80 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 81 | struct { |
| 82 | int uid:1; |
| 83 | int gid:1; |
| 84 | int caps:1; |
| 85 | int vfs:1; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 86 | int enter_vfs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 87 | int pids:1; |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 88 | int net:1; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 89 | int userns:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 90 | int seccomp:1; |
| 91 | int readonly:1; |
| 92 | int usergroups:1; |
| 93 | int ptrace:1; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 94 | int no_new_privs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 95 | int seccomp_filter:1; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 96 | int log_seccomp_filter:1; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 97 | int chroot:1; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame^] | 98 | int pivot_root:1; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 99 | int mount_tmp:1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 100 | int do_init:1; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 101 | int pid_file:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 102 | } 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 Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 109 | int mountns_fd; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 110 | int filter_len; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 111 | int binding_count; |
| 112 | char *chrootdir; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 113 | char *pid_file_path; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 114 | char *uidmap; |
| 115 | char *gidmap; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 116 | struct sock_fprog *filter_prog; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 117 | struct binding *bindings_head; |
| 118 | struct binding *bindings_tail; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 119 | }; |
| 120 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 121 | /* |
| 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 | */ |
| 126 | void minijail_preenter(struct minijail *j) |
| 127 | { |
| 128 | j->flags.vfs = 0; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 129 | j->flags.enter_vfs = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 130 | j->flags.readonly = 0; |
| 131 | j->flags.pids = 0; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 132 | j->flags.do_init = 0; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 133 | j->flags.pid_file = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Strip out flags meant for the child. |
| 138 | * We keep things that are inherited across execve(2). |
| 139 | */ |
| 140 | void minijail_preexec(struct minijail *j) |
| 141 | { |
| 142 | int vfs = j->flags.vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 143 | int enter_vfs = j->flags.enter_vfs; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 144 | int readonly = j->flags.readonly; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 145 | int userns = j->flags.userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 146 | 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 Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 152 | j->flags.enter_vfs = enter_vfs; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 153 | j->flags.readonly = readonly; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 154 | j->flags.userns = userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 155 | /* Note, |pids| will already have been used before this call. */ |
| 156 | } |
| 157 | |
| 158 | /* Minijail API. */ |
| 159 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 160 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 161 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 162 | return calloc(1, sizeof(struct minijail)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 165 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 166 | { |
| 167 | if (uid == 0) |
| 168 | die("useless change to uid 0"); |
| 169 | j->uid = uid; |
| 170 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 173 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 174 | { |
| 175 | if (gid == 0) |
| 176 | die("useless change to gid 0"); |
| 177 | j->gid = gid; |
| 178 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 181 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 182 | { |
| 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 Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 189 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 190 | /* |
| 191 | * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 192 | * 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 Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 198 | /* |
| 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 Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 203 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 204 | /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 205 | if (!ppw) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 206 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 207 | 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 Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 215 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 216 | { |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 217 | char *buf = NULL; |
Yabin Cui | 1b21c8f | 2015-07-22 10:34:45 -0700 | [diff] [blame] | 218 | struct group gr; |
| 219 | struct group *pgr = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 220 | 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 Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 223 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 224 | /* |
| 225 | * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 226 | * 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 Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 232 | /* |
| 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 Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 237 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 238 | /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 239 | if (!pgr) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 240 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 241 | minijail_change_gid(j, pgr->gr_gid); |
| 242 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 245 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 246 | { |
| 247 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 250 | void API minijail_no_new_privs(struct minijail *j) |
| 251 | { |
| 252 | j->flags.no_new_privs = 1; |
| 253 | } |
| 254 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 255 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 256 | { |
| 257 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 258 | } |
| 259 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 260 | void API minijail_log_seccomp_filter_failures(struct minijail *j) |
| 261 | { |
| 262 | j->flags.log_seccomp_filter = 1; |
| 263 | } |
| 264 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 265 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 266 | { |
| 267 | j->caps = capmask; |
| 268 | j->flags.caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 269 | } |
| 270 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 271 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 272 | { |
| 273 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 274 | } |
| 275 | |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 276 | void 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 Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 286 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 287 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 288 | j->flags.vfs = 1; |
| 289 | j->flags.readonly = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 290 | j->flags.pids = 1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 291 | j->flags.do_init = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 292 | } |
| 293 | |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 294 | void API minijail_namespace_net(struct minijail *j) |
| 295 | { |
| 296 | j->flags.net = 1; |
| 297 | } |
| 298 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 299 | void API minijail_remount_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 300 | { |
| 301 | j->flags.vfs = 1; |
| 302 | j->flags.readonly = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 303 | } |
| 304 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 305 | void API minijail_namespace_user(struct minijail *j) |
| 306 | { |
| 307 | j->flags.userns = 1; |
| 308 | } |
| 309 | |
| 310 | int API minijail_uidmap(struct minijail *j, const char *uidmap) |
| 311 | { |
| 312 | j->uidmap = strdup(uidmap); |
| 313 | if (!j->uidmap) |
| 314 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 315 | char *ch; |
| 316 | for (ch = j->uidmap; *ch; ch++) { |
| 317 | if (*ch == ',') |
| 318 | *ch = '\n'; |
| 319 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | int API minijail_gidmap(struct minijail *j, const char *gidmap) |
| 324 | { |
| 325 | j->gidmap = strdup(gidmap); |
| 326 | if (!j->gidmap) |
| 327 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 328 | char *ch; |
| 329 | for (ch = j->gidmap; *ch; ch++) { |
| 330 | if (*ch == ',') |
| 331 | *ch = '\n'; |
| 332 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 336 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 337 | { |
| 338 | j->flags.usergroups = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 341 | void API minijail_disable_ptrace(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 342 | { |
| 343 | j->flags.ptrace = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 346 | void 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 Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 355 | int API minijail_enter_chroot(struct minijail *j, const char *dir) |
| 356 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 357 | 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 Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame^] | 366 | int 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 Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 377 | void API minijail_mount_tmp(struct minijail *j) |
| 378 | { |
| 379 | j->flags.mount_tmp = 1; |
| 380 | } |
| 381 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 382 | int 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 Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 391 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 392 | int writeable) |
| 393 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 394 | 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 Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 409 | info("bind %s -> %s", src, dest); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 410 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 411 | /* |
| 412 | * Force vfs namespacing so the bind mounts don't leak out into the |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 413 | * 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 | |
| 426 | error: |
| 427 | free(b->src); |
| 428 | free(b->dest); |
| 429 | free(b); |
| 430 | return -ENOMEM; |
| 431 | } |
| 432 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 433 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 434 | { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 435 | 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 Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 441 | FILE *file = fopen(path, "r"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 442 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 443 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 444 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 445 | |
| 446 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 447 | 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 Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | j->filter_len = fprog->len; |
| 453 | j->filter_prog = fprog; |
| 454 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 455 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 456 | } |
| 457 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 458 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 459 | size_t available; |
| 460 | size_t total; |
| 461 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 462 | }; |
| 463 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 464 | void marshal_state_init(struct marshal_state *state, |
| 465 | char *buf, size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 466 | { |
| 467 | state->available = available; |
| 468 | state->buf = buf; |
| 469 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 470 | } |
| 471 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 472 | void marshal_append(struct marshal_state *state, |
| 473 | char *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 474 | { |
| 475 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 476 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 477 | /* 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 Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 485 | } |
| 486 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 487 | void minijail_marshal_helper(struct marshal_state *state, |
| 488 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 489 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 490 | struct binding *b = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 491 | marshal_append(state, (char *)j, sizeof(*j)); |
| 492 | if (j->user) |
| 493 | marshal_append(state, j->user, strlen(j->user) + 1); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 494 | if (j->chrootdir) |
| 495 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 496 | 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 Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 500 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 501 | 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 Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 504 | marshal_append(state, (char *)&b->writeable, |
| 505 | sizeof(b->writeable)); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 506 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 507 | } |
| 508 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 509 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 510 | { |
| 511 | struct marshal_state state; |
| 512 | marshal_state_init(&state, NULL, 0); |
| 513 | minijail_marshal_helper(&state, j); |
| 514 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 515 | } |
| 516 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 517 | int 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 Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 525 | /* 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 Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 532 | void *consumebytes(size_t length, char **buf, size_t *buflength) |
| 533 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 534 | 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 Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 548 | char *consumestr(char **buf, size_t *buflength) |
| 549 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 550 | 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 Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 557 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 558 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 559 | int i; |
| 560 | int count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 561 | int ret = -EINVAL; |
| 562 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 563 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 564 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 565 | memcpy((void *)j, serialized, sizeof(*j)); |
| 566 | serialized += sizeof(*j); |
| 567 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 568 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 569 | /* Potentially stale pointers not used as signals. */ |
| 570 | j->bindings_head = NULL; |
| 571 | j->bindings_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 572 | j->filter_prog = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 573 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 574 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 575 | char *user = consumestr(&serialized, &length); |
| 576 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 577 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 578 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 579 | if (!j->user) |
| 580 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 581 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 582 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 583 | if (j->chrootdir) { /* stale pointer */ |
| 584 | char *chrootdir = consumestr(&serialized, &length); |
| 585 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 586 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 587 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 588 | if (!j->chrootdir) |
| 589 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 590 | } |
| 591 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 592 | 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 Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 607 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 608 | |
| 609 | count = j->binding_count; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 610 | 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 Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 616 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 617 | dest = consumestr(&serialized, &length); |
| 618 | if (!dest) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 619 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 620 | writeable = consumebytes(sizeof(*writeable), &serialized, &length); |
| 621 | if (!writeable) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 622 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 623 | if (minijail_bind(j, src, dest, *writeable)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 624 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 625 | } |
| 626 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 627 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 628 | |
| 629 | bad_bindings: |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 630 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 631 | free(j->filter_prog->filter); |
| 632 | free(j->filter_prog); |
| 633 | } |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 634 | bad_filters: |
| 635 | if (j->chrootdir) |
| 636 | free(j->chrootdir); |
| 637 | bad_chrootdir: |
| 638 | if (j->user) |
| 639 | free(j->user); |
| 640 | clear_pointers: |
| 641 | j->user = NULL; |
| 642 | j->chrootdir = NULL; |
| 643 | out: |
| 644 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 645 | } |
| 646 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 647 | static 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 | |
| 683 | static 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 Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 700 | /* 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 Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 706 | int bind_one(const struct minijail *j, struct binding *b) |
| 707 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 708 | int ret = 0; |
| 709 | char *dest = NULL; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 710 | 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 Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 715 | ret = mount(b->src, dest, NULL, MS_BIND, NULL); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 716 | if (ret) |
| 717 | pdie("bind: %s -> %s", b->src, dest); |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 718 | if (!b->writeable) { |
| 719 | ret = mount(b->src, dest, NULL, |
Jorge Lucangeli Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 720 | MS_BIND | MS_REMOUNT | MS_RDONLY, NULL); |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 721 | if (ret) |
| 722 | pdie("bind ro: %s -> %s", b->src, dest); |
| 723 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 724 | free(dest); |
| 725 | if (b->next) |
| 726 | return bind_one(j, b->next); |
| 727 | return ret; |
| 728 | } |
| 729 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 730 | int enter_chroot(const struct minijail *j) |
| 731 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 732 | 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 Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame^] | 745 | int enter_pivot_root(const struct minijail *j) |
| 746 | { |
| 747 | int ret; |
| 748 | if (j->bindings_head && (ret = bind_one(j, j->bindings_head))) |
| 749 | return ret; |
| 750 | |
| 751 | /* To ensure chrootdir is the root of a file system, do a self bind mount. */ |
| 752 | if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, "")) |
| 753 | pdie("failed to bind mount '%s'", j->chrootdir); |
| 754 | if (chdir(j->chrootdir)) |
| 755 | return -errno; |
| 756 | if (mkdir(".minijail_pivot", 0755)) |
| 757 | pdie("mkdir(.minijail_pivot)"); |
| 758 | if (syscall(SYS_pivot_root, ".", ".minijail_pivot")) { |
| 759 | remove(".minijail_pivot"); |
| 760 | pdie("pivot_root"); |
| 761 | } |
| 762 | /* The old root might be busy, so use lazy unmount. */ |
| 763 | if (umount2(".minijail_pivot", MNT_DETACH)) |
| 764 | pdie("umount(.minijail_pivot"); |
| 765 | if (chdir("/")) |
| 766 | return -errno; |
| 767 | if (chroot("/")) |
| 768 | return -errno; |
| 769 | if (remove(".minijail_pivot")) |
| 770 | return -errno; |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 775 | int mount_tmp(void) |
| 776 | { |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 777 | return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777"); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 780 | int remount_readonly(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 781 | { |
| 782 | const char *kProcPath = "/proc"; |
| 783 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 784 | /* |
| 785 | * Right now, we're holding a reference to our parent's old mount of |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 786 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 787 | * mutate our parent's mount as well, even though we're in a VFS |
| 788 | * namespace (!). Instead, remove their mount from our namespace |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 789 | * and make our own. However, if we are in a new user namespace, /proc |
| 790 | * is not seen as mounted, so don't return error if umount() fails. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 791 | */ |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 792 | if (umount(kProcPath) && !j->flags.userns) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 793 | return -errno; |
| 794 | if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
| 795 | return -errno; |
| 796 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 797 | } |
| 798 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 799 | static void write_pid_file(const struct minijail *j) |
| 800 | { |
| 801 | FILE *fp = fopen(j->pid_file_path, "w"); |
| 802 | |
| 803 | if (!fp) |
| 804 | pdie("failed to open '%s'", j->pid_file_path); |
| 805 | if (fprintf(fp, "%d\n", (int)j->initpid) < 0) |
| 806 | pdie("fprintf(%s)", j->pid_file_path); |
| 807 | if (fclose(fp)) |
| 808 | pdie("fclose(%s)", j->pid_file_path); |
| 809 | } |
| 810 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 811 | void drop_ugid(const struct minijail *j) |
| 812 | { |
| 813 | if (j->flags.usergroups) { |
| 814 | if (initgroups(j->user, j->usergid)) |
| 815 | pdie("initgroups"); |
| 816 | } else { |
| 817 | /* Only attempt to clear supplemental groups if we are changing |
| 818 | * users. */ |
| 819 | if ((j->uid || j->gid) && setgroups(0, NULL)) |
| 820 | pdie("setgroups"); |
| 821 | } |
| 822 | |
| 823 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
| 824 | pdie("setresgid"); |
| 825 | |
| 826 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
| 827 | pdie("setresuid"); |
| 828 | } |
| 829 | |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 830 | /* |
| 831 | * We specifically do not use cap_valid() as that only tells us the last |
| 832 | * valid cap we were *compiled* against (i.e. what the version of kernel |
| 833 | * headers says). If we run on a different kernel version, then it's not |
| 834 | * uncommon for that to be less (if an older kernel) or more (if a newer |
| 835 | * kernel). So suck up the answer via /proc. |
| 836 | */ |
| 837 | static int run_cap_valid(unsigned int cap) |
| 838 | { |
| 839 | static unsigned int last_cap; |
| 840 | |
| 841 | if (!last_cap) { |
| 842 | const char cap_file[] = "/proc/sys/kernel/cap_last_cap"; |
| 843 | FILE *fp = fopen(cap_file, "re"); |
| 844 | if (fscanf(fp, "%u", &last_cap) != 1) |
| 845 | pdie("fscanf(%s)", cap_file); |
| 846 | fclose(fp); |
| 847 | } |
| 848 | |
| 849 | return cap <= last_cap; |
| 850 | } |
| 851 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 852 | void drop_caps(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 853 | { |
| 854 | cap_t caps = cap_get_proc(); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 855 | cap_value_t flag[1]; |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 856 | const uint64_t one = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 857 | unsigned int i; |
| 858 | if (!caps) |
| 859 | die("can't get process caps"); |
| 860 | if (cap_clear_flag(caps, CAP_INHERITABLE)) |
| 861 | die("can't clear inheritable caps"); |
| 862 | if (cap_clear_flag(caps, CAP_EFFECTIVE)) |
| 863 | die("can't clear effective caps"); |
| 864 | if (cap_clear_flag(caps, CAP_PERMITTED)) |
| 865 | die("can't clear permitted caps"); |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 866 | for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 867 | /* Keep CAP_SETPCAP for dropping bounding set bits. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 868 | if (i != CAP_SETPCAP && !(j->caps & (one << i))) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 869 | continue; |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 870 | flag[0] = i; |
| 871 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 872 | die("can't add effective cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 873 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 874 | die("can't add permitted cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 875 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 876 | die("can't add inheritable cap"); |
| 877 | } |
| 878 | if (cap_set_proc(caps)) |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 879 | die("can't apply initial cleaned capset"); |
| 880 | |
| 881 | /* |
| 882 | * Instead of dropping bounding set first, do it here in case |
| 883 | * the caller had a more permissive bounding set which could |
| 884 | * have been used above to raise a capability that wasn't already |
| 885 | * present. This requires CAP_SETPCAP, so we raised/kept it above. |
| 886 | */ |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 887 | for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) { |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 888 | if (j->caps & (one << i)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 889 | continue; |
| 890 | if (prctl(PR_CAPBSET_DROP, i)) |
| 891 | pdie("prctl(PR_CAPBSET_DROP)"); |
| 892 | } |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 893 | |
| 894 | /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 895 | if ((j->caps & (one << CAP_SETPCAP)) == 0) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 896 | flag[0] = CAP_SETPCAP; |
| 897 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR)) |
| 898 | die("can't clear effective cap"); |
| 899 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR)) |
| 900 | die("can't clear permitted cap"); |
| 901 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR)) |
| 902 | die("can't clear inheritable cap"); |
| 903 | } |
| 904 | |
| 905 | if (cap_set_proc(caps)) |
| 906 | die("can't apply final cleaned capset"); |
| 907 | |
| 908 | cap_free(caps); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 909 | } |
| 910 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 911 | void set_seccomp_filter(const struct minijail *j) |
| 912 | { |
| 913 | /* |
| 914 | * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c> |
| 915 | * in the kernel source tree for an explanation of the parameters. |
| 916 | */ |
| 917 | if (j->flags.no_new_privs) { |
| 918 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 919 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * If we're logging seccomp filter failures, |
| 924 | * install the SIGSYS handler first. |
| 925 | */ |
| 926 | if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) { |
| 927 | if (install_sigsys_handler()) |
| 928 | pdie("install SIGSYS handler"); |
| 929 | warn("logging seccomp filter failures"); |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Install the syscall filter. |
| 934 | */ |
| 935 | if (j->flags.seccomp_filter) { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 936 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) { |
| 937 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 938 | warn("seccomp not supported"); |
| 939 | return; |
| 940 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 941 | pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 942 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 946 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 947 | { |
| 948 | if (j->flags.pids) |
| 949 | die("tried to enter a pid-namespaced jail;" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 950 | " try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 951 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 952 | if (j->flags.usergroups && !j->user) |
| 953 | die("usergroup inheritance without username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 954 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 955 | /* |
| 956 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 957 | * so we don't even try. If any of our operations fail, we abort() the |
| 958 | * entire process. |
| 959 | */ |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 960 | if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS)) |
| 961 | pdie("setns(CLONE_NEWNS)"); |
| 962 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 963 | if (j->flags.vfs && unshare(CLONE_NEWNS)) |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 964 | pdie("unshare(vfs)"); |
| 965 | |
| 966 | if (j->flags.net && unshare(CLONE_NEWNET)) |
| 967 | pdie("unshare(net)"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 968 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 969 | if (j->flags.chroot && enter_chroot(j)) |
| 970 | pdie("chroot"); |
| 971 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame^] | 972 | if (j->flags.pivot_root && enter_pivot_root(j)) |
| 973 | pdie("pivot_root"); |
| 974 | |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 975 | if (j->flags.mount_tmp && mount_tmp()) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 976 | pdie("mount_tmp"); |
| 977 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 978 | if (j->flags.readonly && remount_readonly(j)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 979 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 980 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 981 | if (j->flags.caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 982 | /* |
| 983 | * POSIX capabilities are a bit tricky. If we drop our |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 984 | * capability to change uids, our attempt to use setuid() |
| 985 | * below will fail. Hang on to root caps across setuid(), then |
| 986 | * lock securebits. |
| 987 | */ |
| 988 | if (prctl(PR_SET_KEEPCAPS, 1)) |
| 989 | pdie("prctl(PR_SET_KEEPCAPS)"); |
| 990 | if (prctl |
| 991 | (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS)) |
| 992 | pdie("prctl(PR_SET_SECUREBITS)"); |
| 993 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 994 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 995 | /* |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 996 | * If we're setting no_new_privs, we can drop privileges |
| 997 | * before setting seccomp filter. This way filter policies |
| 998 | * don't need to allow privilege-dropping syscalls. |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 999 | */ |
| 1000 | if (j->flags.no_new_privs) { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1001 | drop_ugid(j); |
| 1002 | if (j->flags.caps) |
| 1003 | drop_caps(j); |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1004 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1005 | set_seccomp_filter(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1006 | } else { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1007 | /* |
| 1008 | * If we're not setting no_new_privs, |
| 1009 | * we need to set seccomp filter *before* dropping privileges. |
| 1010 | * WARNING: this means that filter policies *must* allow |
| 1011 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 1012 | * capget()/capset()/prctl() for dropping caps. |
| 1013 | */ |
| 1014 | set_seccomp_filter(j); |
| 1015 | |
| 1016 | drop_ugid(j); |
| 1017 | if (j->flags.caps) |
| 1018 | drop_caps(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1019 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1020 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1021 | /* |
| 1022 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1023 | * privilege-dropping syscalls :) |
| 1024 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1025 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) { |
| 1026 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 1027 | warn("seccomp not supported"); |
| 1028 | return; |
| 1029 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1030 | pdie("prctl(PR_SET_SECCOMP)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1031 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1032 | } |
| 1033 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1034 | /* TODO(wad) will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1035 | static int init_exitstatus = 0; |
| 1036 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1037 | void init_term(int __attribute__ ((unused)) sig) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1038 | { |
| 1039 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1040 | } |
| 1041 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1042 | int init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1043 | { |
| 1044 | pid_t pid; |
| 1045 | int status; |
| 1046 | /* so that we exit with the right status */ |
| 1047 | signal(SIGTERM, init_term); |
| 1048 | /* TODO(wad) self jail with seccomp_filters here. */ |
| 1049 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1050 | /* |
| 1051 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1052 | * left inside our pid namespace or we get a signal. |
| 1053 | */ |
| 1054 | if (pid == rootpid) |
| 1055 | init_exitstatus = status; |
| 1056 | } |
| 1057 | if (!WIFEXITED(init_exitstatus)) |
| 1058 | _exit(MINIJAIL_ERR_INIT); |
| 1059 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1060 | } |
| 1061 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1062 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1063 | { |
| 1064 | size_t sz = 0; |
| 1065 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 1066 | char *buf; |
| 1067 | int r; |
| 1068 | if (sizeof(sz) != bytes) |
| 1069 | return -EINVAL; |
| 1070 | if (sz > USHRT_MAX) /* Arbitrary sanity check */ |
| 1071 | return -E2BIG; |
| 1072 | buf = malloc(sz); |
| 1073 | if (!buf) |
| 1074 | return -ENOMEM; |
| 1075 | bytes = read(fd, buf, sz); |
| 1076 | if (bytes != sz) { |
| 1077 | free(buf); |
| 1078 | return -EINVAL; |
| 1079 | } |
| 1080 | r = minijail_unmarshal(j, buf, sz); |
| 1081 | free(buf); |
| 1082 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1083 | } |
| 1084 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1085 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1086 | { |
| 1087 | char *buf; |
| 1088 | size_t sz = minijail_size(j); |
| 1089 | ssize_t written; |
| 1090 | int r; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1091 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1092 | if (!sz) |
| 1093 | return -EINVAL; |
| 1094 | buf = malloc(sz); |
| 1095 | r = minijail_marshal(j, buf, sz); |
| 1096 | if (r) { |
| 1097 | free(buf); |
| 1098 | return r; |
| 1099 | } |
| 1100 | /* Sends [size][minijail]. */ |
| 1101 | written = write(fd, &sz, sizeof(sz)); |
| 1102 | if (written != sizeof(sz)) { |
| 1103 | free(buf); |
| 1104 | return -EFAULT; |
| 1105 | } |
| 1106 | written = write(fd, buf, sz); |
| 1107 | if (written < 0 || (size_t) written != sz) { |
| 1108 | free(buf); |
| 1109 | return -EFAULT; |
| 1110 | } |
| 1111 | free(buf); |
| 1112 | return 0; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1113 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1114 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1115 | int setup_preload(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1116 | { |
Daniel Erat | 5b7a318 | 2015-08-19 16:06:22 -0600 | [diff] [blame] | 1117 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 1118 | /* Don't use LDPRELOAD on Brillo. */ |
| 1119 | return 0; |
| 1120 | #else |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1121 | char *oldenv = getenv(kLdPreloadEnvVar) ? : ""; |
| 1122 | char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH)); |
| 1123 | if (!newenv) |
| 1124 | return -ENOMEM; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1125 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1126 | /* Only insert a separating space if we have something to separate... */ |
| 1127 | sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "", |
| 1128 | PRELOADPATH); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1129 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1130 | /* setenv() makes a copy of the string we give it */ |
| 1131 | setenv(kLdPreloadEnvVar, newenv, 1); |
| 1132 | free(newenv); |
| 1133 | return 0; |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 1134 | #endif |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1135 | } |
| 1136 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1137 | int setup_pipe(int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1138 | { |
| 1139 | int r = pipe(fds); |
| 1140 | char fd_buf[11]; |
| 1141 | if (r) |
| 1142 | return r; |
| 1143 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 1144 | if (r <= 0) |
| 1145 | return -EINVAL; |
| 1146 | setenv(kFdEnvVar, fd_buf, 1); |
| 1147 | return 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1148 | } |
| 1149 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1150 | int setup_pipe_end(int fds[2], size_t index) |
| 1151 | { |
| 1152 | if (index > 1) |
| 1153 | return -1; |
| 1154 | |
| 1155 | close(fds[1 - index]); |
| 1156 | return fds[index]; |
| 1157 | } |
| 1158 | |
| 1159 | int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd) |
| 1160 | { |
| 1161 | if (index > 1) |
| 1162 | return -1; |
| 1163 | |
| 1164 | close(fds[1 - index]); |
| 1165 | /* dup2(2) the corresponding end of the pipe into |fd|. */ |
| 1166 | return dup2(fds[index], fd); |
| 1167 | } |
| 1168 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1169 | int API minijail_run(struct minijail *j, const char *filename, |
| 1170 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1171 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1172 | return minijail_run_pid_pipes(j, filename, argv, |
| 1173 | NULL, NULL, NULL, NULL); |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 1177 | char *const argv[], pid_t *pchild_pid) |
| 1178 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1179 | return minijail_run_pid_pipes(j, filename, argv, pchild_pid, |
| 1180 | NULL, NULL, NULL); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | int API minijail_run_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 1184 | char *const argv[], int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1185 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1186 | return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd, |
| 1187 | NULL, NULL); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | int API minijail_run_pid_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 1191 | char *const argv[], pid_t *pchild_pid, |
| 1192 | int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1193 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1194 | return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd, |
| 1195 | NULL, NULL); |
| 1196 | } |
| 1197 | |
| 1198 | int API minijail_run_pid_pipes(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 1199 | char *const argv[], pid_t *pchild_pid, |
| 1200 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1201 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1202 | char *oldenv, *oldenv_copy = NULL; |
| 1203 | pid_t child_pid; |
| 1204 | int pipe_fds[2]; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1205 | int stdin_fds[2]; |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1206 | int stdout_fds[2]; |
| 1207 | int stderr_fds[2]; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1208 | int userns_pipe_fds[2]; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1209 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1210 | /* We need to remember this across the minijail_preexec() call. */ |
| 1211 | int pid_namespace = j->flags.pids; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1212 | int do_init = j->flags.do_init; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1213 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1214 | oldenv = getenv(kLdPreloadEnvVar); |
| 1215 | if (oldenv) { |
| 1216 | oldenv_copy = strdup(oldenv); |
| 1217 | if (!oldenv_copy) |
| 1218 | return -ENOMEM; |
| 1219 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1220 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1221 | if (setup_preload()) |
| 1222 | return -EFAULT; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1223 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1224 | /* |
Jorge Lucangeli Obes | 3c84df1 | 2015-05-14 17:37:58 -0700 | [diff] [blame] | 1225 | * Make the process group ID of this process equal to its PID, so that |
| 1226 | * both the Minijail process and the jailed process can be killed |
| 1227 | * together. |
| 1228 | * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when |
| 1229 | * the process is already a process group leader. |
| 1230 | */ |
| 1231 | if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) { |
| 1232 | if (errno != EPERM) { |
| 1233 | pdie("setpgid(0, 0)"); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | /* |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1238 | * Before we fork(2) and execve(2) the child process, we need to open |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1239 | * a pipe(2) to send the minijail configuration over. |
| 1240 | */ |
| 1241 | if (setup_pipe(pipe_fds)) |
| 1242 | return -EFAULT; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1243 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1244 | /* |
| 1245 | * If we want to write to the child process' standard input, |
| 1246 | * create the pipe(2) now. |
| 1247 | */ |
| 1248 | if (pstdin_fd) { |
| 1249 | if (pipe(stdin_fds)) |
| 1250 | return -EFAULT; |
| 1251 | } |
| 1252 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1253 | /* |
| 1254 | * If we want to read from the child process' standard output, |
| 1255 | * create the pipe(2) now. |
| 1256 | */ |
| 1257 | if (pstdout_fd) { |
| 1258 | if (pipe(stdout_fds)) |
| 1259 | return -EFAULT; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * If we want to read from the child process' standard error, |
| 1264 | * create the pipe(2) now. |
| 1265 | */ |
| 1266 | if (pstderr_fd) { |
| 1267 | if (pipe(stderr_fds)) |
| 1268 | return -EFAULT; |
| 1269 | } |
| 1270 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1271 | /* |
| 1272 | * If we want to set up a new uid/gid mapping in the user namespace, |
| 1273 | * create the pipe(2) to sync between parent and child. |
| 1274 | */ |
| 1275 | if (j->flags.userns) { |
| 1276 | if (pipe(userns_pipe_fds)) |
| 1277 | return -EFAULT; |
| 1278 | } |
| 1279 | |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1280 | /* Use sys_clone() if and only if we're creating a pid namespace. |
| 1281 | * |
| 1282 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 1283 | * |
| 1284 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 1285 | * some of which may be held by other threads at the time that we call |
| 1286 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 1287 | * ensure that we hold all of these locks before it calls clone() |
| 1288 | * internally and drop them after clone() returns, but when we call |
| 1289 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 1290 | * child address space where some of libc's important locks are held by |
| 1291 | * other threads (which did not get cloned, and hence will never release |
| 1292 | * those locks). This is okay so long as we call exec() immediately |
| 1293 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 1294 | * take locks. |
| 1295 | * |
| 1296 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 1297 | * namespacing. If we follow this path, the child's address space might |
| 1298 | * have broken locks; you may only call functions that do not acquire |
| 1299 | * any locks. |
| 1300 | * |
| 1301 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 1302 | * previously detailed, so this function is highly likely to deadlock |
| 1303 | * later on (see "deadlock here") if we're multithreaded. |
| 1304 | * |
| 1305 | * We might hack around this by having the clone()d child (init of the |
| 1306 | * pid namespace) return directly, rather than leaving the clone()d |
| 1307 | * process hanging around to be init for the new namespace (and having |
| 1308 | * its fork()ed child return in turn), but that process would be crippled |
| 1309 | * with its libc locks potentially broken. We might try fork()ing in the |
| 1310 | * parent before we clone() to ensure that we own all the locks, but |
| 1311 | * then we have to have the forked child hanging around consuming |
| 1312 | * resources (and possibly having file descriptors / shared memory |
| 1313 | * regions / etc attached). We'd need to keep the child around to avoid |
| 1314 | * having its children get reparented to init. |
| 1315 | * |
| 1316 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 1317 | * problem is fixable or not. It would be nice if we worked in this |
| 1318 | * case. |
| 1319 | */ |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1320 | if (pid_namespace) { |
| 1321 | int clone_flags = CLONE_NEWPID | SIGCHLD; |
| 1322 | if (j->flags.userns) |
| 1323 | clone_flags |= CLONE_NEWUSER; |
| 1324 | child_pid = syscall(SYS_clone, clone_flags, NULL); |
| 1325 | } |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1326 | else |
| 1327 | child_pid = fork(); |
| 1328 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1329 | if (child_pid < 0) { |
| 1330 | free(oldenv_copy); |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1331 | die("failed to fork child"); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1332 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1333 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1334 | if (child_pid) { |
| 1335 | /* Restore parent's LD_PRELOAD. */ |
| 1336 | if (oldenv_copy) { |
| 1337 | setenv(kLdPreloadEnvVar, oldenv_copy, 1); |
| 1338 | free(oldenv_copy); |
| 1339 | } else { |
| 1340 | unsetenv(kLdPreloadEnvVar); |
| 1341 | } |
| 1342 | unsetenv(kFdEnvVar); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1343 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1344 | j->initpid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1345 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1346 | if (j->flags.pid_file) |
| 1347 | write_pid_file(j); |
| 1348 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1349 | if (j->flags.userns) |
| 1350 | write_ugid_mappings(j, userns_pipe_fds); |
| 1351 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1352 | /* Send marshalled minijail. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1353 | close(pipe_fds[0]); /* read endpoint */ |
| 1354 | ret = minijail_to_fd(j, pipe_fds[1]); |
| 1355 | close(pipe_fds[1]); /* write endpoint */ |
| 1356 | if (ret) { |
| 1357 | kill(j->initpid, SIGKILL); |
| 1358 | die("failed to send marshalled minijail"); |
| 1359 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1360 | |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1361 | if (pchild_pid) |
| 1362 | *pchild_pid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1363 | |
| 1364 | /* |
| 1365 | * If we want to write to the child process' standard input, |
| 1366 | * set up the write end of the pipe. |
| 1367 | */ |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1368 | if (pstdin_fd) |
| 1369 | *pstdin_fd = setup_pipe_end(stdin_fds, |
| 1370 | 1 /* write end */); |
| 1371 | |
| 1372 | /* |
| 1373 | * If we want to read from the child process' standard output, |
| 1374 | * set up the read end of the pipe. |
| 1375 | */ |
| 1376 | if (pstdout_fd) |
| 1377 | *pstdout_fd = setup_pipe_end(stdout_fds, |
| 1378 | 0 /* read end */); |
| 1379 | |
| 1380 | /* |
| 1381 | * If we want to read from the child process' standard error, |
| 1382 | * set up the read end of the pipe. |
| 1383 | */ |
| 1384 | if (pstderr_fd) |
| 1385 | *pstderr_fd = setup_pipe_end(stderr_fds, |
| 1386 | 0 /* read end */); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1387 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1388 | return 0; |
| 1389 | } |
| 1390 | free(oldenv_copy); |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1391 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1392 | |
| 1393 | if (j->flags.userns) |
| 1394 | enter_user_namespace(j, userns_pipe_fds); |
| 1395 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1396 | /* |
| 1397 | * If we want to write to the jailed process' standard input, |
| 1398 | * set up the read end of the pipe. |
| 1399 | */ |
| 1400 | if (pstdin_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1401 | if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */, |
| 1402 | STDIN_FILENO) < 0) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1403 | die("failed to set up stdin pipe"); |
| 1404 | } |
| 1405 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1406 | /* |
| 1407 | * If we want to read from the jailed process' standard output, |
| 1408 | * set up the write end of the pipe. |
| 1409 | */ |
| 1410 | if (pstdout_fd) { |
| 1411 | if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */, |
| 1412 | STDOUT_FILENO) < 0) |
| 1413 | die("failed to set up stdout pipe"); |
| 1414 | } |
| 1415 | |
| 1416 | /* |
| 1417 | * If we want to read from the jailed process' standard error, |
| 1418 | * set up the write end of the pipe. |
| 1419 | */ |
| 1420 | if (pstderr_fd) { |
| 1421 | if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */, |
| 1422 | STDERR_FILENO) < 0) |
| 1423 | die("failed to set up stderr pipe"); |
| 1424 | } |
| 1425 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 1426 | /* Strip out flags that cannot be inherited across execve. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1427 | minijail_preexec(j); |
| 1428 | /* Jail this process and its descendants... */ |
| 1429 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1430 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1431 | if (pid_namespace && do_init) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1432 | /* |
| 1433 | * pid namespace: this process will become init inside the new |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1434 | * namespace. We don't want all programs we might exec to have |
| 1435 | * to know how to be init. Normally |do_init == 1| we fork off |
| 1436 | * a child to actually run the program. If |do_init == 0|, we |
| 1437 | * let the program keep pid 1 and be init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1438 | * |
| 1439 | * If we're multithreaded, we'll probably deadlock here. See |
| 1440 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1441 | */ |
| 1442 | child_pid = fork(); |
| 1443 | if (child_pid < 0) |
| 1444 | _exit(child_pid); |
| 1445 | else if (child_pid > 0) |
| 1446 | init(child_pid); /* never returns */ |
| 1447 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1448 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1449 | /* |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1450 | * If we aren't pid-namespaced, or jailed program asked to be init: |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1451 | * calling process |
| 1452 | * -> execve()-ing process |
| 1453 | * If we are: |
| 1454 | * calling process |
| 1455 | * -> init()-ing process |
| 1456 | * -> execve()-ing process |
| 1457 | */ |
| 1458 | _exit(execve(filename, argv, environ)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1459 | } |
| 1460 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1461 | int API minijail_run_static(struct minijail *j, const char *filename, |
| 1462 | char *const argv[]) |
| 1463 | { |
| 1464 | pid_t child_pid; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1465 | int userns_pipe_fds[2]; |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1466 | int pid_namespace = j->flags.pids; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1467 | int do_init = j->flags.do_init; |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1468 | |
| 1469 | if (j->flags.caps) |
| 1470 | die("caps not supported with static targets"); |
| 1471 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1472 | /* |
| 1473 | * If we want to set up a new uid/gid mapping in the user namespace, |
| 1474 | * create the pipe(2) to sync between parent and child. |
| 1475 | */ |
| 1476 | if (j->flags.userns) { |
| 1477 | if (pipe(userns_pipe_fds)) |
| 1478 | return -EFAULT; |
| 1479 | } |
| 1480 | |
| 1481 | if (pid_namespace) { |
| 1482 | int clone_flags = CLONE_NEWPID | SIGCHLD; |
| 1483 | if (j->flags.userns) |
| 1484 | clone_flags |= CLONE_NEWUSER; |
| 1485 | child_pid = syscall(SYS_clone, clone_flags, NULL); |
| 1486 | } |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1487 | else |
| 1488 | child_pid = fork(); |
| 1489 | |
| 1490 | if (child_pid < 0) { |
| 1491 | die("failed to fork child"); |
| 1492 | } |
| 1493 | if (child_pid > 0 ) { |
| 1494 | j->initpid = child_pid; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1495 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1496 | if (j->flags.pid_file) |
| 1497 | write_pid_file(j); |
| 1498 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1499 | if (j->flags.userns) |
| 1500 | write_ugid_mappings(j, userns_pipe_fds); |
| 1501 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1502 | return 0; |
| 1503 | } |
| 1504 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1505 | if (j->flags.userns) |
| 1506 | enter_user_namespace(j, userns_pipe_fds); |
| 1507 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1508 | /* |
| 1509 | * We can now drop this child into the sandbox |
| 1510 | * then execve the target. |
| 1511 | */ |
| 1512 | |
| 1513 | j->flags.pids = 0; |
| 1514 | minijail_enter(j); |
| 1515 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1516 | if (pid_namespace && do_init) { |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1517 | /* |
| 1518 | * pid namespace: this process will become init inside the new |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1519 | * namespace. We don't want all programs we might exec to have |
| 1520 | * to know how to be init. Normally |do_init == 1| we fork off |
| 1521 | * a child to actually run the program. If |do_init == 0|, we |
| 1522 | * let the program keep pid 1 and be init. |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1523 | * |
| 1524 | * If we're multithreaded, we'll probably deadlock here. See |
| 1525 | * WARNING above. |
| 1526 | */ |
| 1527 | child_pid = fork(); |
| 1528 | if (child_pid < 0) |
| 1529 | _exit(child_pid); |
| 1530 | else if (child_pid > 0) |
| 1531 | init(child_pid); /* never returns */ |
| 1532 | } |
| 1533 | |
| 1534 | _exit(execve(filename, argv, environ)); |
| 1535 | } |
| 1536 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1537 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1538 | { |
| 1539 | int st; |
| 1540 | if (kill(j->initpid, SIGTERM)) |
| 1541 | return -errno; |
| 1542 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1543 | return -errno; |
| 1544 | return st; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1545 | } |
| 1546 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1547 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1548 | { |
| 1549 | int st; |
| 1550 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1551 | return -errno; |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1552 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1553 | if (!WIFEXITED(st)) { |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1554 | int error_status = st; |
| 1555 | if (WIFSIGNALED(st)) { |
| 1556 | int signum = WTERMSIG(st); |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1557 | warn("child process %d received signal %d", |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1558 | j->initpid, signum); |
| 1559 | /* |
| 1560 | * We return MINIJAIL_ERR_JAIL if the process received |
| 1561 | * SIGSYS, which happens when a syscall is blocked by |
| 1562 | * seccomp filters. |
| 1563 | * If not, we do what bash(1) does: |
| 1564 | * $? = 128 + signum |
| 1565 | */ |
| 1566 | if (signum == SIGSYS) { |
| 1567 | error_status = MINIJAIL_ERR_JAIL; |
| 1568 | } else { |
| 1569 | error_status = 128 + signum; |
| 1570 | } |
| 1571 | } |
| 1572 | return error_status; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1573 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1574 | |
| 1575 | int exit_status = WEXITSTATUS(st); |
| 1576 | if (exit_status != 0) |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1577 | info("child process %d exited with status %d", |
| 1578 | j->initpid, exit_status); |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1579 | |
| 1580 | return exit_status; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1581 | } |
| 1582 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1583 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1584 | { |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1585 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 1586 | free(j->filter_prog->filter); |
| 1587 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1588 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1589 | while (j->bindings_head) { |
| 1590 | struct binding *b = j->bindings_head; |
| 1591 | j->bindings_head = j->bindings_head->next; |
| 1592 | free(b->dest); |
| 1593 | free(b->src); |
| 1594 | free(b); |
| 1595 | } |
| 1596 | j->bindings_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1597 | if (j->user) |
| 1598 | free(j->user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1599 | if (j->chrootdir) |
| 1600 | free(j->chrootdir); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1601 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1602 | } |