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