Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 3 | * 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] | 4 | * found in the LICENSE file. |
| 5 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 6 | |
| 7 | #define _BSD_SOURCE |
| 8 | #define _GNU_SOURCE |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 9 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 10 | #include <asm/unistd.h> |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 11 | #include <ctype.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 12 | #include <errno.h> |
| 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 | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 30 | #include <sys/user.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 31 | #include <sys/wait.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | |
| 34 | #include "libminijail.h" |
| 35 | #include "libminijail-private.h" |
| 36 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 37 | #include "signal.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 38 | #include "syscall_filter.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 39 | #include "util.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 40 | |
Lei Zhang | eee3155 | 2012-10-17 21:27:10 -0700 | [diff] [blame] | 41 | #ifdef HAVE_SECUREBITS_H |
| 42 | #include <linux/securebits.h> |
| 43 | #else |
| 44 | #define SECURE_ALL_BITS 0x15 |
| 45 | #define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) |
| 46 | #endif |
| 47 | |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 48 | /* Until these are reliably available in linux/prctl.h */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 49 | #ifndef PR_SET_SECCOMP |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 50 | # define PR_SET_SECCOMP 22 |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 51 | #endif |
| 52 | |
| 53 | /* For seccomp_filter using BPF. */ |
| 54 | #ifndef PR_SET_NO_NEW_PRIVS |
| 55 | # define PR_SET_NO_NEW_PRIVS 38 |
| 56 | #endif |
| 57 | #ifndef SECCOMP_MODE_FILTER |
| 58 | # define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */ |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 59 | #endif |
| 60 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 61 | struct binding { |
| 62 | char *src; |
| 63 | char *dest; |
| 64 | int writeable; |
| 65 | struct binding *next; |
| 66 | }; |
| 67 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 68 | struct minijail { |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 69 | /* |
| 70 | * WARNING: if you add a flag here you need to make sure it's accounted for |
| 71 | * in minijail_pre{enter|exec}() below. |
| 72 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 73 | struct { |
| 74 | int uid:1; |
| 75 | int gid:1; |
| 76 | int caps:1; |
| 77 | int vfs:1; |
| 78 | int pids:1; |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 79 | int net:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 80 | int seccomp:1; |
| 81 | int readonly:1; |
| 82 | int usergroups:1; |
| 83 | int ptrace:1; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 84 | int no_new_privs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 85 | int seccomp_filter:1; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 86 | int log_seccomp_filter:1; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 87 | int chroot:1; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame^] | 88 | int mount_tmp:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 89 | } flags; |
| 90 | uid_t uid; |
| 91 | gid_t gid; |
| 92 | gid_t usergid; |
| 93 | char *user; |
| 94 | uint64_t caps; |
| 95 | pid_t initpid; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 96 | int filter_len; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 97 | int binding_count; |
| 98 | char *chrootdir; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 99 | struct sock_fprog *filter_prog; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 100 | struct binding *bindings_head; |
| 101 | struct binding *bindings_tail; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 102 | }; |
| 103 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 104 | /* |
| 105 | * Strip out flags meant for the parent. |
| 106 | * We keep things that are not inherited across execve(2) (e.g. capabilities), |
| 107 | * or are easier to set after execve(2) (e.g. seccomp filters). |
| 108 | */ |
| 109 | void minijail_preenter(struct minijail *j) |
| 110 | { |
| 111 | j->flags.vfs = 0; |
| 112 | j->flags.readonly = 0; |
| 113 | j->flags.pids = 0; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Strip out flags meant for the child. |
| 118 | * We keep things that are inherited across execve(2). |
| 119 | */ |
| 120 | void minijail_preexec(struct minijail *j) |
| 121 | { |
| 122 | int vfs = j->flags.vfs; |
| 123 | int readonly = j->flags.readonly; |
| 124 | if (j->user) |
| 125 | free(j->user); |
| 126 | j->user = NULL; |
| 127 | memset(&j->flags, 0, sizeof(j->flags)); |
| 128 | /* Now restore anything we meant to keep. */ |
| 129 | j->flags.vfs = vfs; |
| 130 | j->flags.readonly = readonly; |
| 131 | /* Note, |pids| will already have been used before this call. */ |
| 132 | } |
| 133 | |
| 134 | /* Minijail API. */ |
| 135 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 136 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 137 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 138 | return calloc(1, sizeof(struct minijail)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 141 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 142 | { |
| 143 | if (uid == 0) |
| 144 | die("useless change to uid 0"); |
| 145 | j->uid = uid; |
| 146 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 149 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 150 | { |
| 151 | if (gid == 0) |
| 152 | die("useless change to gid 0"); |
| 153 | j->gid = gid; |
| 154 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 157 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 158 | { |
| 159 | char *buf = NULL; |
| 160 | struct passwd pw; |
| 161 | struct passwd *ppw = NULL; |
| 162 | ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 163 | if (sz == -1) |
| 164 | sz = 65536; /* your guess is as good as mine... */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 165 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 166 | /* |
| 167 | * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 168 | * the maximum needed size of the buffer, so we don't have to search. |
| 169 | */ |
| 170 | buf = malloc(sz); |
| 171 | if (!buf) |
| 172 | return -ENOMEM; |
| 173 | getpwnam_r(user, &pw, buf, sz, &ppw); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 174 | /* |
| 175 | * We're safe to free the buffer here. The strings inside pw point |
| 176 | * inside buf, but we don't use any of them; this leaves the pointers |
| 177 | * dangling but it's safe. ppw points at pw if getpwnam_r succeeded. |
| 178 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 179 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 180 | /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 181 | if (!ppw) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 182 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 183 | minijail_change_uid(j, ppw->pw_uid); |
| 184 | j->user = strdup(user); |
| 185 | if (!j->user) |
| 186 | return -ENOMEM; |
| 187 | j->usergid = ppw->pw_gid; |
| 188 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 191 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 192 | { |
| 193 | char *buf = NULL; |
| 194 | struct group gr; |
| 195 | struct group *pgr = NULL; |
| 196 | ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 197 | if (sz == -1) |
| 198 | sz = 65536; /* and mine is as good as yours, really */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 199 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 200 | /* |
| 201 | * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 202 | * the maximum needed size of the buffer, so we don't have to search. |
| 203 | */ |
| 204 | buf = malloc(sz); |
| 205 | if (!buf) |
| 206 | return -ENOMEM; |
| 207 | getgrnam_r(group, &gr, buf, sz, &pgr); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 208 | /* |
| 209 | * We're safe to free the buffer here. The strings inside gr point |
| 210 | * inside buf, but we don't use any of them; this leaves the pointers |
| 211 | * dangling but it's safe. pgr points at gr if getgrnam_r succeeded. |
| 212 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 213 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 214 | /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 215 | if (!pgr) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 216 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 217 | minijail_change_gid(j, pgr->gr_gid); |
| 218 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 221 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 222 | { |
| 223 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 226 | void API minijail_no_new_privs(struct minijail *j) |
| 227 | { |
| 228 | j->flags.no_new_privs = 1; |
| 229 | } |
| 230 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 231 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 232 | { |
| 233 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 236 | void API minijail_log_seccomp_filter_failures(struct minijail *j) |
| 237 | { |
| 238 | j->flags.log_seccomp_filter = 1; |
| 239 | } |
| 240 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 241 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 242 | { |
| 243 | j->caps = capmask; |
| 244 | j->flags.caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 247 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 248 | { |
| 249 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 252 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 253 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 254 | j->flags.vfs = 1; |
| 255 | j->flags.readonly = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 256 | j->flags.pids = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 259 | void API minijail_namespace_net(struct minijail *j) |
| 260 | { |
| 261 | j->flags.net = 1; |
| 262 | } |
| 263 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 264 | void API minijail_remount_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 265 | { |
| 266 | j->flags.vfs = 1; |
| 267 | j->flags.readonly = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 270 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 271 | { |
| 272 | j->flags.usergroups = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 275 | void API minijail_disable_ptrace(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 276 | { |
| 277 | j->flags.ptrace = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 278 | } |
| 279 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 280 | int API minijail_enter_chroot(struct minijail *j, const char *dir) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 281 | if (j->chrootdir) |
| 282 | return -EINVAL; |
| 283 | j->chrootdir = strdup(dir); |
| 284 | if (!j->chrootdir) |
| 285 | return -ENOMEM; |
| 286 | j->flags.chroot = 1; |
| 287 | return 0; |
| 288 | } |
| 289 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame^] | 290 | void API minijail_mount_tmp(struct minijail *j) |
| 291 | { |
| 292 | j->flags.mount_tmp = 1; |
| 293 | } |
| 294 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 295 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
| 296 | int writeable) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 297 | struct binding *b; |
| 298 | |
| 299 | if (*dest != '/') |
| 300 | return -EINVAL; |
| 301 | b = calloc(1, sizeof(*b)); |
| 302 | if (!b) |
| 303 | return -ENOMEM; |
| 304 | b->dest = strdup(dest); |
| 305 | if (!b->dest) |
| 306 | goto error; |
| 307 | b->src = strdup(src); |
| 308 | if (!b->src) |
| 309 | goto error; |
| 310 | b->writeable = writeable; |
| 311 | |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 312 | info("bind %s -> %s", src, dest); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 313 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 314 | /* |
| 315 | * 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] | 316 | * containing vfs namespace. |
| 317 | */ |
| 318 | minijail_namespace_vfs(j); |
| 319 | |
| 320 | if (j->bindings_tail) |
| 321 | j->bindings_tail->next = b; |
| 322 | else |
| 323 | j->bindings_head = b; |
| 324 | j->bindings_tail = b; |
| 325 | j->binding_count++; |
| 326 | |
| 327 | return 0; |
| 328 | |
| 329 | error: |
| 330 | free(b->src); |
| 331 | free(b->dest); |
| 332 | free(b); |
| 333 | return -ENOMEM; |
| 334 | } |
| 335 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 336 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 337 | { |
| 338 | FILE *file = fopen(path, "r"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 339 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 340 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 341 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 342 | |
| 343 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 344 | if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) { |
| 345 | die("failed to compile seccomp filter BPF program in '%s'", |
| 346 | path); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | j->filter_len = fprog->len; |
| 350 | j->filter_prog = fprog; |
| 351 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 352 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 353 | } |
| 354 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 355 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 356 | size_t available; |
| 357 | size_t total; |
| 358 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 359 | }; |
| 360 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 361 | void marshal_state_init(struct marshal_state *state, |
| 362 | char *buf, size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 363 | { |
| 364 | state->available = available; |
| 365 | state->buf = buf; |
| 366 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 367 | } |
| 368 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 369 | void marshal_append(struct marshal_state *state, |
| 370 | char *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 371 | { |
| 372 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 373 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 374 | /* Up to |available| will be written. */ |
| 375 | if (copy_len) { |
| 376 | memcpy(state->buf, src, copy_len); |
| 377 | state->buf += copy_len; |
| 378 | state->available -= copy_len; |
| 379 | } |
| 380 | /* |total| will contain the expected length. */ |
| 381 | state->total += length; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 384 | void minijail_marshal_helper(struct marshal_state *state, |
| 385 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 386 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 387 | struct binding *b = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 388 | marshal_append(state, (char *)j, sizeof(*j)); |
| 389 | if (j->user) |
| 390 | marshal_append(state, j->user, strlen(j->user) + 1); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 391 | if (j->chrootdir) |
| 392 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 393 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 394 | struct sock_fprog *fp = j->filter_prog; |
| 395 | marshal_append(state, (char *)fp->filter, |
| 396 | fp->len * sizeof(struct sock_filter)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 397 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 398 | for (b = j->bindings_head; b; b = b->next) { |
| 399 | marshal_append(state, b->src, strlen(b->src) + 1); |
| 400 | marshal_append(state, b->dest, strlen(b->dest) + 1); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 401 | marshal_append(state, (char *)&b->writeable, |
| 402 | sizeof(b->writeable)); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 403 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 404 | } |
| 405 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 406 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 407 | { |
| 408 | struct marshal_state state; |
| 409 | marshal_state_init(&state, NULL, 0); |
| 410 | minijail_marshal_helper(&state, j); |
| 411 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 412 | } |
| 413 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 414 | int minijail_marshal(const struct minijail *j, char *buf, size_t available) |
| 415 | { |
| 416 | struct marshal_state state; |
| 417 | marshal_state_init(&state, buf, available); |
| 418 | minijail_marshal_helper(&state, j); |
| 419 | return (state.total > available); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 420 | } |
| 421 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 422 | /* consumebytes: consumes @length bytes from a buffer @buf of length @buflength |
| 423 | * @length Number of bytes to consume |
| 424 | * @buf Buffer to consume from |
| 425 | * @buflength Size of @buf |
| 426 | * |
| 427 | * Returns a pointer to the base of the bytes, or NULL for errors. |
| 428 | */ |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 429 | void *consumebytes(size_t length, char **buf, size_t *buflength) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 430 | char *p = *buf; |
| 431 | if (length > *buflength) |
| 432 | return NULL; |
| 433 | *buf += length; |
| 434 | *buflength -= length; |
| 435 | return p; |
| 436 | } |
| 437 | |
| 438 | /* consumestr: consumes a C string from a buffer @buf of length @length |
| 439 | * @buf Buffer to consume |
| 440 | * @length Length of buffer |
| 441 | * |
| 442 | * Returns a pointer to the base of the string, or NULL for errors. |
| 443 | */ |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 444 | char *consumestr(char **buf, size_t *buflength) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 445 | size_t len = strnlen(*buf, *buflength); |
| 446 | if (len == *buflength) |
| 447 | /* There's no null-terminator */ |
| 448 | return NULL; |
| 449 | return consumebytes(len + 1, buf, buflength); |
| 450 | } |
| 451 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 452 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 453 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 454 | int i; |
| 455 | int count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 456 | int ret = -EINVAL; |
| 457 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 458 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 459 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 460 | memcpy((void *)j, serialized, sizeof(*j)); |
| 461 | serialized += sizeof(*j); |
| 462 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 463 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 464 | /* Potentially stale pointers not used as signals. */ |
| 465 | j->bindings_head = NULL; |
| 466 | j->bindings_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 467 | j->filter_prog = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 468 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 469 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 470 | char *user = consumestr(&serialized, &length); |
| 471 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 472 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 473 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 474 | if (!j->user) |
| 475 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 476 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 477 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 478 | if (j->chrootdir) { /* stale pointer */ |
| 479 | char *chrootdir = consumestr(&serialized, &length); |
| 480 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 481 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 482 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 483 | if (!j->chrootdir) |
| 484 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 485 | } |
| 486 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 487 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 488 | size_t ninstrs = j->filter_len; |
| 489 | if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) || |
| 490 | ninstrs > USHRT_MAX) |
| 491 | goto bad_filters; |
| 492 | |
| 493 | size_t program_len = ninstrs * sizeof(struct sock_filter); |
| 494 | void *program = consumebytes(program_len, &serialized, &length); |
| 495 | if (!program) |
| 496 | goto bad_filters; |
| 497 | |
| 498 | j->filter_prog = malloc(sizeof(struct sock_fprog)); |
| 499 | j->filter_prog->len = ninstrs; |
| 500 | j->filter_prog->filter = malloc(program_len); |
| 501 | memcpy(j->filter_prog->filter, program, program_len); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 502 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 503 | |
| 504 | count = j->binding_count; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 505 | j->binding_count = 0; |
| 506 | for (i = 0; i < count; ++i) { |
| 507 | int *writeable; |
| 508 | const char *dest; |
| 509 | const char *src = consumestr(&serialized, &length); |
| 510 | if (!src) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 511 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 512 | dest = consumestr(&serialized, &length); |
| 513 | if (!dest) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 514 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 515 | writeable = consumebytes(sizeof(*writeable), &serialized, &length); |
| 516 | if (!writeable) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 517 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 518 | if (minijail_bind(j, src, dest, *writeable)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 519 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 520 | } |
| 521 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 522 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 523 | |
| 524 | bad_bindings: |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 525 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 526 | free(j->filter_prog->filter); |
| 527 | free(j->filter_prog); |
| 528 | } |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 529 | bad_filters: |
| 530 | if (j->chrootdir) |
| 531 | free(j->chrootdir); |
| 532 | bad_chrootdir: |
| 533 | if (j->user) |
| 534 | free(j->user); |
| 535 | clear_pointers: |
| 536 | j->user = NULL; |
| 537 | j->chrootdir = NULL; |
| 538 | out: |
| 539 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 540 | } |
| 541 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 542 | /* bind_one: Applies bindings from @b for @j, recursing as needed. |
| 543 | * @j Minijail these bindings are for |
| 544 | * @b Head of list of bindings |
| 545 | * |
| 546 | * Returns 0 for success. |
| 547 | */ |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 548 | int bind_one(const struct minijail *j, struct binding *b) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 549 | int ret = 0; |
| 550 | char *dest = NULL; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 551 | if (ret) |
| 552 | return ret; |
| 553 | /* dest has a leading "/" */ |
| 554 | if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0) |
| 555 | return -ENOMEM; |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 556 | ret = mount(b->src, dest, NULL, MS_BIND, NULL); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 557 | if (ret) |
| 558 | pdie("bind: %s -> %s", b->src, dest); |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 559 | if (!b->writeable) { |
| 560 | ret = mount(b->src, dest, NULL, |
| 561 | MS_BIND | MS_REMOUNT | MS_RDONLY, NULL); |
| 562 | if (ret) |
| 563 | pdie("bind ro: %s -> %s", b->src, dest); |
| 564 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 565 | free(dest); |
| 566 | if (b->next) |
| 567 | return bind_one(j, b->next); |
| 568 | return ret; |
| 569 | } |
| 570 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 571 | int enter_chroot(const struct minijail *j) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 572 | int ret; |
| 573 | if (j->bindings_head && (ret = bind_one(j, j->bindings_head))) |
| 574 | return ret; |
| 575 | |
| 576 | if (chroot(j->chrootdir)) |
| 577 | return -errno; |
| 578 | |
| 579 | if (chdir("/")) |
| 580 | return -errno; |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame^] | 585 | int mount_tmp(void) |
| 586 | { |
| 587 | return mount("none", "/tmp", "tmpfs", 0, "size=128M,mode=777"); |
| 588 | } |
| 589 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 590 | int remount_readonly(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 591 | { |
| 592 | const char *kProcPath = "/proc"; |
| 593 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 594 | /* |
| 595 | * 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] | 596 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 597 | * mutate our parent's mount as well, even though we're in a VFS |
| 598 | * namespace (!). Instead, remove their mount from our namespace |
| 599 | * and make our own. |
| 600 | */ |
| 601 | if (umount(kProcPath)) |
| 602 | return -errno; |
| 603 | if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
| 604 | return -errno; |
| 605 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 606 | } |
| 607 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 608 | void drop_ugid(const struct minijail *j) |
| 609 | { |
| 610 | if (j->flags.usergroups) { |
| 611 | if (initgroups(j->user, j->usergid)) |
| 612 | pdie("initgroups"); |
| 613 | } else { |
| 614 | /* Only attempt to clear supplemental groups if we are changing |
| 615 | * users. */ |
| 616 | if ((j->uid || j->gid) && setgroups(0, NULL)) |
| 617 | pdie("setgroups"); |
| 618 | } |
| 619 | |
| 620 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
| 621 | pdie("setresgid"); |
| 622 | |
| 623 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
| 624 | pdie("setresuid"); |
| 625 | } |
| 626 | |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 627 | /* |
| 628 | * We specifically do not use cap_valid() as that only tells us the last |
| 629 | * valid cap we were *compiled* against (i.e. what the version of kernel |
| 630 | * headers says). If we run on a different kernel version, then it's not |
| 631 | * uncommon for that to be less (if an older kernel) or more (if a newer |
| 632 | * kernel). So suck up the answer via /proc. |
| 633 | */ |
| 634 | static int run_cap_valid(unsigned int cap) |
| 635 | { |
| 636 | static unsigned int last_cap; |
| 637 | |
| 638 | if (!last_cap) { |
| 639 | const char cap_file[] = "/proc/sys/kernel/cap_last_cap"; |
| 640 | FILE *fp = fopen(cap_file, "re"); |
| 641 | if (fscanf(fp, "%u", &last_cap) != 1) |
| 642 | pdie("fscanf(%s)", cap_file); |
| 643 | fclose(fp); |
| 644 | } |
| 645 | |
| 646 | return cap <= last_cap; |
| 647 | } |
| 648 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 649 | void drop_caps(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 650 | { |
| 651 | cap_t caps = cap_get_proc(); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 652 | cap_value_t flag[1]; |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 653 | const uint64_t one = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 654 | unsigned int i; |
| 655 | if (!caps) |
| 656 | die("can't get process caps"); |
| 657 | if (cap_clear_flag(caps, CAP_INHERITABLE)) |
| 658 | die("can't clear inheritable caps"); |
| 659 | if (cap_clear_flag(caps, CAP_EFFECTIVE)) |
| 660 | die("can't clear effective caps"); |
| 661 | if (cap_clear_flag(caps, CAP_PERMITTED)) |
| 662 | die("can't clear permitted caps"); |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 663 | 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] | 664 | /* Keep CAP_SETPCAP for dropping bounding set bits. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 665 | if (i != CAP_SETPCAP && !(j->caps & (one << i))) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 666 | continue; |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 667 | flag[0] = i; |
| 668 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 669 | die("can't add effective cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 670 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 671 | die("can't add permitted cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 672 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 673 | die("can't add inheritable cap"); |
| 674 | } |
| 675 | if (cap_set_proc(caps)) |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 676 | die("can't apply initial cleaned capset"); |
| 677 | |
| 678 | /* |
| 679 | * Instead of dropping bounding set first, do it here in case |
| 680 | * the caller had a more permissive bounding set which could |
| 681 | * have been used above to raise a capability that wasn't already |
| 682 | * present. This requires CAP_SETPCAP, so we raised/kept it above. |
| 683 | */ |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 684 | 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] | 685 | if (j->caps & (one << i)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 686 | continue; |
| 687 | if (prctl(PR_CAPBSET_DROP, i)) |
| 688 | pdie("prctl(PR_CAPBSET_DROP)"); |
| 689 | } |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 690 | |
| 691 | /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 692 | if ((j->caps & (one << CAP_SETPCAP)) == 0) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 693 | flag[0] = CAP_SETPCAP; |
| 694 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR)) |
| 695 | die("can't clear effective cap"); |
| 696 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR)) |
| 697 | die("can't clear permitted cap"); |
| 698 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR)) |
| 699 | die("can't clear inheritable cap"); |
| 700 | } |
| 701 | |
| 702 | if (cap_set_proc(caps)) |
| 703 | die("can't apply final cleaned capset"); |
| 704 | |
| 705 | cap_free(caps); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 708 | void set_seccomp_filter(const struct minijail *j) |
| 709 | { |
| 710 | /* |
| 711 | * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c> |
| 712 | * in the kernel source tree for an explanation of the parameters. |
| 713 | */ |
| 714 | if (j->flags.no_new_privs) { |
| 715 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 716 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * If we're logging seccomp filter failures, |
| 721 | * install the SIGSYS handler first. |
| 722 | */ |
| 723 | if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) { |
| 724 | if (install_sigsys_handler()) |
| 725 | pdie("install SIGSYS handler"); |
| 726 | warn("logging seccomp filter failures"); |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * Install the syscall filter. |
| 731 | */ |
| 732 | if (j->flags.seccomp_filter) { |
| 733 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) |
| 734 | pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)"); |
| 735 | } |
| 736 | } |
| 737 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 738 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 739 | { |
| 740 | if (j->flags.pids) |
| 741 | die("tried to enter a pid-namespaced jail;" |
| 742 | "try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 743 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 744 | if (j->flags.usergroups && !j->user) |
| 745 | die("usergroup inheritance without username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 746 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 747 | /* |
| 748 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 749 | * so we don't even try. If any of our operations fail, we abort() the |
| 750 | * entire process. |
| 751 | */ |
| 752 | if (j->flags.vfs && unshare(CLONE_NEWNS)) |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 753 | pdie("unshare(vfs)"); |
| 754 | |
| 755 | if (j->flags.net && unshare(CLONE_NEWNET)) |
| 756 | pdie("unshare(net)"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 757 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 758 | if (j->flags.chroot && enter_chroot(j)) |
| 759 | pdie("chroot"); |
| 760 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame^] | 761 | if (j->flags.chroot && j->flags.mount_tmp && mount_tmp()) |
| 762 | pdie("mount_tmp"); |
| 763 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 764 | if (j->flags.readonly && remount_readonly()) |
| 765 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 766 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 767 | if (j->flags.caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 768 | /* |
| 769 | * POSIX capabilities are a bit tricky. If we drop our |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 770 | * capability to change uids, our attempt to use setuid() |
| 771 | * below will fail. Hang on to root caps across setuid(), then |
| 772 | * lock securebits. |
| 773 | */ |
| 774 | if (prctl(PR_SET_KEEPCAPS, 1)) |
| 775 | pdie("prctl(PR_SET_KEEPCAPS)"); |
| 776 | if (prctl |
| 777 | (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS)) |
| 778 | pdie("prctl(PR_SET_SECUREBITS)"); |
| 779 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 780 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 781 | /* |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 782 | * If we're setting no_new_privs, we can drop privileges |
| 783 | * before setting seccomp filter. This way filter policies |
| 784 | * don't need to allow privilege-dropping syscalls. |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 785 | */ |
| 786 | if (j->flags.no_new_privs) { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 787 | drop_ugid(j); |
| 788 | if (j->flags.caps) |
| 789 | drop_caps(j); |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 790 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 791 | set_seccomp_filter(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 792 | } else { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 793 | /* |
| 794 | * If we're not setting no_new_privs, |
| 795 | * we need to set seccomp filter *before* dropping privileges. |
| 796 | * WARNING: this means that filter policies *must* allow |
| 797 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 798 | * capget()/capset()/prctl() for dropping caps. |
| 799 | */ |
| 800 | set_seccomp_filter(j); |
| 801 | |
| 802 | drop_ugid(j); |
| 803 | if (j->flags.caps) |
| 804 | drop_caps(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 805 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 806 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 807 | /* |
| 808 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 809 | * privilege-dropping syscalls :) |
| 810 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 811 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) |
| 812 | pdie("prctl(PR_SET_SECCOMP)"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 813 | } |
| 814 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 815 | /* TODO(wad) will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 816 | static int init_exitstatus = 0; |
| 817 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 818 | void init_term(int __attribute__ ((unused)) sig) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 819 | { |
| 820 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 821 | } |
| 822 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 823 | int init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 824 | { |
| 825 | pid_t pid; |
| 826 | int status; |
| 827 | /* so that we exit with the right status */ |
| 828 | signal(SIGTERM, init_term); |
| 829 | /* TODO(wad) self jail with seccomp_filters here. */ |
| 830 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 831 | /* |
| 832 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 833 | * left inside our pid namespace or we get a signal. |
| 834 | */ |
| 835 | if (pid == rootpid) |
| 836 | init_exitstatus = status; |
| 837 | } |
| 838 | if (!WIFEXITED(init_exitstatus)) |
| 839 | _exit(MINIJAIL_ERR_INIT); |
| 840 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 841 | } |
| 842 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 843 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 844 | { |
| 845 | size_t sz = 0; |
| 846 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 847 | char *buf; |
| 848 | int r; |
| 849 | if (sizeof(sz) != bytes) |
| 850 | return -EINVAL; |
| 851 | if (sz > USHRT_MAX) /* Arbitrary sanity check */ |
| 852 | return -E2BIG; |
| 853 | buf = malloc(sz); |
| 854 | if (!buf) |
| 855 | return -ENOMEM; |
| 856 | bytes = read(fd, buf, sz); |
| 857 | if (bytes != sz) { |
| 858 | free(buf); |
| 859 | return -EINVAL; |
| 860 | } |
| 861 | r = minijail_unmarshal(j, buf, sz); |
| 862 | free(buf); |
| 863 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 864 | } |
| 865 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 866 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 867 | { |
| 868 | char *buf; |
| 869 | size_t sz = minijail_size(j); |
| 870 | ssize_t written; |
| 871 | int r; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 872 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 873 | if (!sz) |
| 874 | return -EINVAL; |
| 875 | buf = malloc(sz); |
| 876 | r = minijail_marshal(j, buf, sz); |
| 877 | if (r) { |
| 878 | free(buf); |
| 879 | return r; |
| 880 | } |
| 881 | /* Sends [size][minijail]. */ |
| 882 | written = write(fd, &sz, sizeof(sz)); |
| 883 | if (written != sizeof(sz)) { |
| 884 | free(buf); |
| 885 | return -EFAULT; |
| 886 | } |
| 887 | written = write(fd, buf, sz); |
| 888 | if (written < 0 || (size_t) written != sz) { |
| 889 | free(buf); |
| 890 | return -EFAULT; |
| 891 | } |
| 892 | free(buf); |
| 893 | return 0; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 894 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 895 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 896 | int setup_preload(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 897 | { |
| 898 | char *oldenv = getenv(kLdPreloadEnvVar) ? : ""; |
| 899 | char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH)); |
| 900 | if (!newenv) |
| 901 | return -ENOMEM; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 902 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 903 | /* Only insert a separating space if we have something to separate... */ |
| 904 | sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "", |
| 905 | PRELOADPATH); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 906 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 907 | /* setenv() makes a copy of the string we give it */ |
| 908 | setenv(kLdPreloadEnvVar, newenv, 1); |
| 909 | free(newenv); |
| 910 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 911 | } |
| 912 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 913 | int setup_pipe(int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 914 | { |
| 915 | int r = pipe(fds); |
| 916 | char fd_buf[11]; |
| 917 | if (r) |
| 918 | return r; |
| 919 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 920 | if (r <= 0) |
| 921 | return -EINVAL; |
| 922 | setenv(kFdEnvVar, fd_buf, 1); |
| 923 | return 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 924 | } |
| 925 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 926 | int setup_pipe_end(int fds[2], size_t index) |
| 927 | { |
| 928 | if (index > 1) |
| 929 | return -1; |
| 930 | |
| 931 | close(fds[1 - index]); |
| 932 | return fds[index]; |
| 933 | } |
| 934 | |
| 935 | int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd) |
| 936 | { |
| 937 | if (index > 1) |
| 938 | return -1; |
| 939 | |
| 940 | close(fds[1 - index]); |
| 941 | /* dup2(2) the corresponding end of the pipe into |fd|. */ |
| 942 | return dup2(fds[index], fd); |
| 943 | } |
| 944 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 945 | int API minijail_run(struct minijail *j, const char *filename, |
| 946 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 947 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 948 | return minijail_run_pid_pipes(j, filename, argv, |
| 949 | NULL, NULL, NULL, NULL); |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 953 | char *const argv[], pid_t *pchild_pid) |
| 954 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 955 | return minijail_run_pid_pipes(j, filename, argv, pchild_pid, |
| 956 | NULL, NULL, NULL); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | int API minijail_run_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 960 | char *const argv[], int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 961 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 962 | return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd, |
| 963 | NULL, NULL); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | 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] | 967 | char *const argv[], pid_t *pchild_pid, |
| 968 | int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 969 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 970 | return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd, |
| 971 | NULL, NULL); |
| 972 | } |
| 973 | |
| 974 | 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] | 975 | char *const argv[], pid_t *pchild_pid, |
| 976 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 977 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 978 | char *oldenv, *oldenv_copy = NULL; |
| 979 | pid_t child_pid; |
| 980 | int pipe_fds[2]; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 981 | int stdin_fds[2]; |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 982 | int stdout_fds[2]; |
| 983 | int stderr_fds[2]; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 984 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 985 | /* We need to remember this across the minijail_preexec() call. */ |
| 986 | int pid_namespace = j->flags.pids; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 987 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 988 | oldenv = getenv(kLdPreloadEnvVar); |
| 989 | if (oldenv) { |
| 990 | oldenv_copy = strdup(oldenv); |
| 991 | if (!oldenv_copy) |
| 992 | return -ENOMEM; |
| 993 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 994 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 995 | if (setup_preload()) |
| 996 | return -EFAULT; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 997 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 998 | /* |
| 999 | * 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] | 1000 | * a pipe(2) to send the minijail configuration over. |
| 1001 | */ |
| 1002 | if (setup_pipe(pipe_fds)) |
| 1003 | return -EFAULT; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1004 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1005 | /* |
| 1006 | * If we want to write to the child process' standard input, |
| 1007 | * create the pipe(2) now. |
| 1008 | */ |
| 1009 | if (pstdin_fd) { |
| 1010 | if (pipe(stdin_fds)) |
| 1011 | return -EFAULT; |
| 1012 | } |
| 1013 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1014 | /* |
| 1015 | * If we want to read from the child process' standard output, |
| 1016 | * create the pipe(2) now. |
| 1017 | */ |
| 1018 | if (pstdout_fd) { |
| 1019 | if (pipe(stdout_fds)) |
| 1020 | return -EFAULT; |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * If we want to read from the child process' standard error, |
| 1025 | * create the pipe(2) now. |
| 1026 | */ |
| 1027 | if (pstderr_fd) { |
| 1028 | if (pipe(stderr_fds)) |
| 1029 | return -EFAULT; |
| 1030 | } |
| 1031 | |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1032 | /* Use sys_clone() if and only if we're creating a pid namespace. |
| 1033 | * |
| 1034 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 1035 | * |
| 1036 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 1037 | * some of which may be held by other threads at the time that we call |
| 1038 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 1039 | * ensure that we hold all of these locks before it calls clone() |
| 1040 | * internally and drop them after clone() returns, but when we call |
| 1041 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 1042 | * child address space where some of libc's important locks are held by |
| 1043 | * other threads (which did not get cloned, and hence will never release |
| 1044 | * those locks). This is okay so long as we call exec() immediately |
| 1045 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 1046 | * take locks. |
| 1047 | * |
| 1048 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 1049 | * namespacing. If we follow this path, the child's address space might |
| 1050 | * have broken locks; you may only call functions that do not acquire |
| 1051 | * any locks. |
| 1052 | * |
| 1053 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 1054 | * previously detailed, so this function is highly likely to deadlock |
| 1055 | * later on (see "deadlock here") if we're multithreaded. |
| 1056 | * |
| 1057 | * We might hack around this by having the clone()d child (init of the |
| 1058 | * pid namespace) return directly, rather than leaving the clone()d |
| 1059 | * process hanging around to be init for the new namespace (and having |
| 1060 | * its fork()ed child return in turn), but that process would be crippled |
| 1061 | * with its libc locks potentially broken. We might try fork()ing in the |
| 1062 | * parent before we clone() to ensure that we own all the locks, but |
| 1063 | * then we have to have the forked child hanging around consuming |
| 1064 | * resources (and possibly having file descriptors / shared memory |
| 1065 | * regions / etc attached). We'd need to keep the child around to avoid |
| 1066 | * having its children get reparented to init. |
| 1067 | * |
| 1068 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 1069 | * problem is fixable or not. It would be nice if we worked in this |
| 1070 | * case. |
| 1071 | */ |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1072 | if (pid_namespace) |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1073 | child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL); |
| 1074 | else |
| 1075 | child_pid = fork(); |
| 1076 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1077 | if (child_pid < 0) { |
| 1078 | free(oldenv_copy); |
| 1079 | return child_pid; |
| 1080 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1081 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1082 | if (child_pid) { |
| 1083 | /* Restore parent's LD_PRELOAD. */ |
| 1084 | if (oldenv_copy) { |
| 1085 | setenv(kLdPreloadEnvVar, oldenv_copy, 1); |
| 1086 | free(oldenv_copy); |
| 1087 | } else { |
| 1088 | unsetenv(kLdPreloadEnvVar); |
| 1089 | } |
| 1090 | unsetenv(kFdEnvVar); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1091 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1092 | j->initpid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1093 | |
| 1094 | /* Send marshalled minijail. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1095 | close(pipe_fds[0]); /* read endpoint */ |
| 1096 | ret = minijail_to_fd(j, pipe_fds[1]); |
| 1097 | close(pipe_fds[1]); /* write endpoint */ |
| 1098 | if (ret) { |
| 1099 | kill(j->initpid, SIGKILL); |
| 1100 | die("failed to send marshalled minijail"); |
| 1101 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1102 | |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1103 | if (pchild_pid) |
| 1104 | *pchild_pid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1105 | |
| 1106 | /* |
| 1107 | * If we want to write to the child process' standard input, |
| 1108 | * set up the write end of the pipe. |
| 1109 | */ |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1110 | if (pstdin_fd) |
| 1111 | *pstdin_fd = setup_pipe_end(stdin_fds, |
| 1112 | 1 /* write end */); |
| 1113 | |
| 1114 | /* |
| 1115 | * If we want to read from the child process' standard output, |
| 1116 | * set up the read end of the pipe. |
| 1117 | */ |
| 1118 | if (pstdout_fd) |
| 1119 | *pstdout_fd = setup_pipe_end(stdout_fds, |
| 1120 | 0 /* read end */); |
| 1121 | |
| 1122 | /* |
| 1123 | * If we want to read from the child process' standard error, |
| 1124 | * set up the read end of the pipe. |
| 1125 | */ |
| 1126 | if (pstderr_fd) |
| 1127 | *pstderr_fd = setup_pipe_end(stderr_fds, |
| 1128 | 0 /* read end */); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1129 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1130 | return 0; |
| 1131 | } |
| 1132 | free(oldenv_copy); |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1133 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1134 | /* |
| 1135 | * If we want to write to the jailed process' standard input, |
| 1136 | * set up the read end of the pipe. |
| 1137 | */ |
| 1138 | if (pstdin_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1139 | if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */, |
| 1140 | STDIN_FILENO) < 0) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1141 | die("failed to set up stdin pipe"); |
| 1142 | } |
| 1143 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1144 | /* |
| 1145 | * If we want to read from the jailed process' standard output, |
| 1146 | * set up the write end of the pipe. |
| 1147 | */ |
| 1148 | if (pstdout_fd) { |
| 1149 | if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */, |
| 1150 | STDOUT_FILENO) < 0) |
| 1151 | die("failed to set up stdout pipe"); |
| 1152 | } |
| 1153 | |
| 1154 | /* |
| 1155 | * If we want to read from the jailed process' standard error, |
| 1156 | * set up the write end of the pipe. |
| 1157 | */ |
| 1158 | if (pstderr_fd) { |
| 1159 | if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */, |
| 1160 | STDERR_FILENO) < 0) |
| 1161 | die("failed to set up stderr pipe"); |
| 1162 | } |
| 1163 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 1164 | /* Strip out flags that cannot be inherited across execve. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1165 | minijail_preexec(j); |
| 1166 | /* Jail this process and its descendants... */ |
| 1167 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1168 | |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1169 | if (pid_namespace) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1170 | /* |
| 1171 | * pid namespace: this process will become init inside the new |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1172 | * namespace, so fork off a child to actually run the program |
| 1173 | * (we don't want all programs we might exec to have to know |
| 1174 | * how to be init). |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1175 | * |
| 1176 | * If we're multithreaded, we'll probably deadlock here. See |
| 1177 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1178 | */ |
| 1179 | child_pid = fork(); |
| 1180 | if (child_pid < 0) |
| 1181 | _exit(child_pid); |
| 1182 | else if (child_pid > 0) |
| 1183 | init(child_pid); /* never returns */ |
| 1184 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1185 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1186 | /* |
| 1187 | * If we aren't pid-namespaced: |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1188 | * calling process |
| 1189 | * -> execve()-ing process |
| 1190 | * If we are: |
| 1191 | * calling process |
| 1192 | * -> init()-ing process |
| 1193 | * -> execve()-ing process |
| 1194 | */ |
| 1195 | _exit(execve(filename, argv, environ)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1196 | } |
| 1197 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1198 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1199 | { |
| 1200 | int st; |
| 1201 | if (kill(j->initpid, SIGTERM)) |
| 1202 | return -errno; |
| 1203 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1204 | return -errno; |
| 1205 | return st; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1206 | } |
| 1207 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1208 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1209 | { |
| 1210 | int st; |
| 1211 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1212 | return -errno; |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1213 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1214 | if (!WIFEXITED(st)) { |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1215 | int error_status = st; |
| 1216 | if (WIFSIGNALED(st)) { |
| 1217 | int signum = WTERMSIG(st); |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1218 | warn("child process %d received signal %d", |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1219 | j->initpid, signum); |
| 1220 | /* |
| 1221 | * We return MINIJAIL_ERR_JAIL if the process received |
| 1222 | * SIGSYS, which happens when a syscall is blocked by |
| 1223 | * seccomp filters. |
| 1224 | * If not, we do what bash(1) does: |
| 1225 | * $? = 128 + signum |
| 1226 | */ |
| 1227 | if (signum == SIGSYS) { |
| 1228 | error_status = MINIJAIL_ERR_JAIL; |
| 1229 | } else { |
| 1230 | error_status = 128 + signum; |
| 1231 | } |
| 1232 | } |
| 1233 | return error_status; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1234 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1235 | |
| 1236 | int exit_status = WEXITSTATUS(st); |
| 1237 | if (exit_status != 0) |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1238 | info("child process %d exited with status %d", |
| 1239 | j->initpid, exit_status); |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1240 | |
| 1241 | return exit_status; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1242 | } |
| 1243 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1244 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1245 | { |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1246 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 1247 | free(j->filter_prog->filter); |
| 1248 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1249 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1250 | while (j->bindings_head) { |
| 1251 | struct binding *b = j->bindings_head; |
| 1252 | j->bindings_head = j->bindings_head->next; |
| 1253 | free(b->dest); |
| 1254 | free(b->src); |
| 1255 | free(b); |
| 1256 | } |
| 1257 | j->bindings_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1258 | if (j->user) |
| 1259 | free(j->user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1260 | if (j->chrootdir) |
| 1261 | free(j->chrootdir); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1262 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1263 | } |