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> |
| 17 | #include <linux/securebits.h> |
| 18 | #include <pwd.h> |
| 19 | #include <sched.h> |
| 20 | #include <signal.h> |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 21 | #include <stdarg.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 22 | #include <stddef.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <syscall.h> |
| 27 | #include <sys/capability.h> |
| 28 | #include <sys/mount.h> |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 29 | #include <sys/param.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 30 | #include <sys/prctl.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 31 | #include <sys/user.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 32 | #include <sys/wait.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
| 35 | #include "libminijail.h" |
| 36 | #include "libminijail-private.h" |
| 37 | |
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 | |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 41 | /* Until these are reliably available in linux/prctl.h */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 42 | #ifndef PR_SET_SECCOMP |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 43 | # define PR_SET_SECCOMP 22 |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | /* For seccomp_filter using BPF. */ |
| 47 | #ifndef PR_SET_NO_NEW_PRIVS |
| 48 | # define PR_SET_NO_NEW_PRIVS 38 |
| 49 | #endif |
| 50 | #ifndef SECCOMP_MODE_FILTER |
| 51 | # define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */ |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 52 | #endif |
| 53 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 54 | struct binding { |
| 55 | char *src; |
| 56 | char *dest; |
| 57 | int writeable; |
| 58 | struct binding *next; |
| 59 | }; |
| 60 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 61 | struct minijail { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 62 | struct { |
| 63 | int uid:1; |
| 64 | int gid:1; |
| 65 | int caps:1; |
| 66 | int vfs:1; |
| 67 | int pids:1; |
| 68 | int seccomp:1; |
| 69 | int readonly:1; |
| 70 | int usergroups:1; |
| 71 | int ptrace:1; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 72 | int no_new_privs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 73 | int seccomp_filter:1; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 74 | int chroot:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 75 | } flags; |
| 76 | uid_t uid; |
| 77 | gid_t gid; |
| 78 | gid_t usergid; |
| 79 | char *user; |
| 80 | uint64_t caps; |
| 81 | pid_t initpid; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 82 | int filter_len; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 83 | int binding_count; |
| 84 | char *chrootdir; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 85 | struct sock_fprog *filter_prog; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 86 | struct binding *bindings_head; |
| 87 | struct binding *bindings_tail; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 88 | }; |
| 89 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 90 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 91 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 92 | return calloc(1, sizeof(struct minijail)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 95 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 96 | { |
| 97 | if (uid == 0) |
| 98 | die("useless change to uid 0"); |
| 99 | j->uid = uid; |
| 100 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 103 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 104 | { |
| 105 | if (gid == 0) |
| 106 | die("useless change to gid 0"); |
| 107 | j->gid = gid; |
| 108 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 111 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 112 | { |
| 113 | char *buf = NULL; |
| 114 | struct passwd pw; |
| 115 | struct passwd *ppw = NULL; |
| 116 | ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 117 | if (sz == -1) |
| 118 | sz = 65536; /* your guess is as good as mine... */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 119 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 120 | /* |
| 121 | * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 122 | * the maximum needed size of the buffer, so we don't have to search. |
| 123 | */ |
| 124 | buf = malloc(sz); |
| 125 | if (!buf) |
| 126 | return -ENOMEM; |
| 127 | getpwnam_r(user, &pw, buf, sz, &ppw); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 128 | /* |
| 129 | * We're safe to free the buffer here. The strings inside pw point |
| 130 | * inside buf, but we don't use any of them; this leaves the pointers |
| 131 | * dangling but it's safe. ppw points at pw if getpwnam_r succeeded. |
| 132 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 133 | free(buf); |
| 134 | if (!ppw) |
| 135 | return -errno; |
| 136 | minijail_change_uid(j, ppw->pw_uid); |
| 137 | j->user = strdup(user); |
| 138 | if (!j->user) |
| 139 | return -ENOMEM; |
| 140 | j->usergid = ppw->pw_gid; |
| 141 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 144 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 145 | { |
| 146 | char *buf = NULL; |
| 147 | struct group gr; |
| 148 | struct group *pgr = NULL; |
| 149 | ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 150 | if (sz == -1) |
| 151 | sz = 65536; /* and mine is as good as yours, really */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 152 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 153 | /* |
| 154 | * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 155 | * the maximum needed size of the buffer, so we don't have to search. |
| 156 | */ |
| 157 | buf = malloc(sz); |
| 158 | if (!buf) |
| 159 | return -ENOMEM; |
| 160 | getgrnam_r(group, &gr, buf, sz, &pgr); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 161 | /* |
| 162 | * We're safe to free the buffer here. The strings inside gr point |
| 163 | * inside buf, but we don't use any of them; this leaves the pointers |
| 164 | * dangling but it's safe. pgr points at gr if getgrnam_r succeeded. |
| 165 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 166 | free(buf); |
| 167 | if (!pgr) |
| 168 | return -errno; |
| 169 | minijail_change_gid(j, pgr->gr_gid); |
| 170 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 173 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 174 | { |
| 175 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 178 | void API minijail_no_new_privs(struct minijail *j) |
| 179 | { |
| 180 | j->flags.no_new_privs = 1; |
| 181 | } |
| 182 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 183 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 184 | { |
| 185 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 188 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 189 | { |
| 190 | j->caps = capmask; |
| 191 | j->flags.caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 194 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 195 | { |
| 196 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 199 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 200 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 201 | j->flags.vfs = 1; |
| 202 | j->flags.readonly = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 203 | j->flags.pids = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 206 | void API minijail_remount_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 207 | { |
| 208 | j->flags.vfs = 1; |
| 209 | j->flags.readonly = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 212 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 213 | { |
| 214 | j->flags.usergroups = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 217 | void API minijail_disable_ptrace(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 218 | { |
| 219 | j->flags.ptrace = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 222 | int API minijail_enter_chroot(struct minijail *j, const char *dir) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 223 | if (j->chrootdir) |
| 224 | return -EINVAL; |
| 225 | j->chrootdir = strdup(dir); |
| 226 | if (!j->chrootdir) |
| 227 | return -ENOMEM; |
| 228 | j->flags.chroot = 1; |
| 229 | return 0; |
| 230 | } |
| 231 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 232 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
| 233 | int writeable) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 234 | struct binding *b; |
| 235 | |
| 236 | if (*dest != '/') |
| 237 | return -EINVAL; |
| 238 | b = calloc(1, sizeof(*b)); |
| 239 | if (!b) |
| 240 | return -ENOMEM; |
| 241 | b->dest = strdup(dest); |
| 242 | if (!b->dest) |
| 243 | goto error; |
| 244 | b->src = strdup(src); |
| 245 | if (!b->src) |
| 246 | goto error; |
| 247 | b->writeable = writeable; |
| 248 | |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 249 | info("bind %s -> %s", src, dest); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 250 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 251 | /* |
| 252 | * 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] | 253 | * containing vfs namespace. |
| 254 | */ |
| 255 | minijail_namespace_vfs(j); |
| 256 | |
| 257 | if (j->bindings_tail) |
| 258 | j->bindings_tail->next = b; |
| 259 | else |
| 260 | j->bindings_head = b; |
| 261 | j->bindings_tail = b; |
| 262 | j->binding_count++; |
| 263 | |
| 264 | return 0; |
| 265 | |
| 266 | error: |
| 267 | free(b->src); |
| 268 | free(b->dest); |
| 269 | free(b); |
| 270 | return -ENOMEM; |
| 271 | } |
| 272 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 273 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 274 | { |
| 275 | FILE *file = fopen(path, "r"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 276 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 277 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 278 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 279 | |
| 280 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
| 281 | if (compile_filter(file, fprog)) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 282 | die("failed to compile seccomp filter BPF program in '%s'", path); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | j->filter_len = fprog->len; |
| 286 | j->filter_prog = fprog; |
| 287 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 288 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 291 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 292 | size_t available; |
| 293 | size_t total; |
| 294 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 295 | }; |
| 296 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 297 | void marshal_state_init(struct marshal_state *state, |
| 298 | char *buf, size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 299 | { |
| 300 | state->available = available; |
| 301 | state->buf = buf; |
| 302 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 303 | } |
| 304 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 305 | void marshal_append(struct marshal_state *state, |
| 306 | char *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 307 | { |
| 308 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 309 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 310 | /* Up to |available| will be written. */ |
| 311 | if (copy_len) { |
| 312 | memcpy(state->buf, src, copy_len); |
| 313 | state->buf += copy_len; |
| 314 | state->available -= copy_len; |
| 315 | } |
| 316 | /* |total| will contain the expected length. */ |
| 317 | state->total += length; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 320 | void minijail_marshal_helper(struct marshal_state *state, |
| 321 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 322 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 323 | struct binding *b = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 324 | marshal_append(state, (char *)j, sizeof(*j)); |
| 325 | if (j->user) |
| 326 | marshal_append(state, j->user, strlen(j->user) + 1); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 327 | if (j->chrootdir) |
| 328 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 329 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 330 | struct sock_fprog *fp = j->filter_prog; |
| 331 | marshal_append(state, (char *)fp->filter, |
| 332 | fp->len * sizeof(struct sock_filter)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 333 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 334 | for (b = j->bindings_head; b; b = b->next) { |
| 335 | marshal_append(state, b->src, strlen(b->src) + 1); |
| 336 | marshal_append(state, b->dest, strlen(b->dest) + 1); |
| 337 | marshal_append(state, (char *)&b->writeable, sizeof(b->writeable)); |
| 338 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 339 | } |
| 340 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 341 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 342 | { |
| 343 | struct marshal_state state; |
| 344 | marshal_state_init(&state, NULL, 0); |
| 345 | minijail_marshal_helper(&state, j); |
| 346 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 349 | int minijail_marshal(const struct minijail *j, char *buf, size_t available) |
| 350 | { |
| 351 | struct marshal_state state; |
| 352 | marshal_state_init(&state, buf, available); |
| 353 | minijail_marshal_helper(&state, j); |
| 354 | return (state.total > available); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 355 | } |
| 356 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 357 | /* consumebytes: consumes @length bytes from a buffer @buf of length @buflength |
| 358 | * @length Number of bytes to consume |
| 359 | * @buf Buffer to consume from |
| 360 | * @buflength Size of @buf |
| 361 | * |
| 362 | * Returns a pointer to the base of the bytes, or NULL for errors. |
| 363 | */ |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 364 | void *consumebytes(size_t length, char **buf, size_t *buflength) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 365 | char *p = *buf; |
| 366 | if (length > *buflength) |
| 367 | return NULL; |
| 368 | *buf += length; |
| 369 | *buflength -= length; |
| 370 | return p; |
| 371 | } |
| 372 | |
| 373 | /* consumestr: consumes a C string from a buffer @buf of length @length |
| 374 | * @buf Buffer to consume |
| 375 | * @length Length of buffer |
| 376 | * |
| 377 | * Returns a pointer to the base of the string, or NULL for errors. |
| 378 | */ |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 379 | char *consumestr(char **buf, size_t *buflength) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 380 | size_t len = strnlen(*buf, *buflength); |
| 381 | if (len == *buflength) |
| 382 | /* There's no null-terminator */ |
| 383 | return NULL; |
| 384 | return consumebytes(len + 1, buf, buflength); |
| 385 | } |
| 386 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 387 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 388 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 389 | int i; |
| 390 | int count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 391 | int ret = -EINVAL; |
| 392 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 393 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 394 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 395 | memcpy((void *)j, serialized, sizeof(*j)); |
| 396 | serialized += sizeof(*j); |
| 397 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 398 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 399 | /* Potentially stale pointers not used as signals. */ |
| 400 | j->bindings_head = NULL; |
| 401 | j->bindings_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 402 | j->filter_prog = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 403 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 404 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 405 | char *user = consumestr(&serialized, &length); |
| 406 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 407 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 408 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 409 | if (!j->user) |
| 410 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 411 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 412 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 413 | if (j->chrootdir) { /* stale pointer */ |
| 414 | char *chrootdir = consumestr(&serialized, &length); |
| 415 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 416 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 417 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 418 | if (!j->chrootdir) |
| 419 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 422 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 423 | size_t ninstrs = j->filter_len; |
| 424 | if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) || |
| 425 | ninstrs > USHRT_MAX) |
| 426 | goto bad_filters; |
| 427 | |
| 428 | size_t program_len = ninstrs * sizeof(struct sock_filter); |
| 429 | void *program = consumebytes(program_len, &serialized, &length); |
| 430 | if (!program) |
| 431 | goto bad_filters; |
| 432 | |
| 433 | j->filter_prog = malloc(sizeof(struct sock_fprog)); |
| 434 | j->filter_prog->len = ninstrs; |
| 435 | j->filter_prog->filter = malloc(program_len); |
| 436 | memcpy(j->filter_prog->filter, program, program_len); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 437 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 438 | |
| 439 | count = j->binding_count; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 440 | j->binding_count = 0; |
| 441 | for (i = 0; i < count; ++i) { |
| 442 | int *writeable; |
| 443 | const char *dest; |
| 444 | const char *src = consumestr(&serialized, &length); |
| 445 | if (!src) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 446 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 447 | dest = consumestr(&serialized, &length); |
| 448 | if (!dest) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 449 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 450 | writeable = consumebytes(sizeof(*writeable), &serialized, &length); |
| 451 | if (!writeable) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 452 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 453 | if (minijail_bind(j, src, dest, *writeable)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 454 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 455 | } |
| 456 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 457 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 458 | |
| 459 | bad_bindings: |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 460 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 461 | free(j->filter_prog->filter); |
| 462 | free(j->filter_prog); |
| 463 | } |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 464 | bad_filters: |
| 465 | if (j->chrootdir) |
| 466 | free(j->chrootdir); |
| 467 | bad_chrootdir: |
| 468 | if (j->user) |
| 469 | free(j->user); |
| 470 | clear_pointers: |
| 471 | j->user = NULL; |
| 472 | j->chrootdir = NULL; |
| 473 | out: |
| 474 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 475 | } |
| 476 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 477 | void minijail_preenter(struct minijail *j) |
| 478 | { |
| 479 | /* Strip out options which are minijail_run() only. */ |
| 480 | j->flags.vfs = 0; |
| 481 | j->flags.readonly = 0; |
| 482 | j->flags.pids = 0; |
Will Drewry | fe4a372 | 2011-09-16 14:50:50 -0500 | [diff] [blame] | 483 | } |
| 484 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 485 | void minijail_preexec(struct minijail *j) |
| 486 | { |
| 487 | int vfs = j->flags.vfs; |
| 488 | int readonly = j->flags.readonly; |
| 489 | if (j->user) |
| 490 | free(j->user); |
| 491 | j->user = NULL; |
| 492 | memset(&j->flags, 0, sizeof(j->flags)); |
| 493 | /* Now restore anything we meant to keep. */ |
| 494 | j->flags.vfs = vfs; |
| 495 | j->flags.readonly = readonly; |
| 496 | /* Note, pidns will already have been used before this call. */ |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 497 | } |
| 498 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 499 | /* bind_one: Applies bindings from @b for @j, recursing as needed. |
| 500 | * @j Minijail these bindings are for |
| 501 | * @b Head of list of bindings |
| 502 | * |
| 503 | * Returns 0 for success. |
| 504 | */ |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 505 | int bind_one(const struct minijail *j, struct binding *b) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 506 | int ret = 0; |
| 507 | char *dest = NULL; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 508 | if (ret) |
| 509 | return ret; |
| 510 | /* dest has a leading "/" */ |
| 511 | if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0) |
| 512 | return -ENOMEM; |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 513 | ret = mount(b->src, dest, NULL, MS_BIND, NULL); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 514 | if (ret) |
| 515 | pdie("bind: %s -> %s", b->src, dest); |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 516 | if (!b->writeable) { |
| 517 | ret = mount(b->src, dest, NULL, |
| 518 | MS_BIND | MS_REMOUNT | MS_RDONLY, NULL); |
| 519 | if (ret) |
| 520 | pdie("bind ro: %s -> %s", b->src, dest); |
| 521 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 522 | free(dest); |
| 523 | if (b->next) |
| 524 | return bind_one(j, b->next); |
| 525 | return ret; |
| 526 | } |
| 527 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 528 | int enter_chroot(const struct minijail *j) { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 529 | int ret; |
| 530 | if (j->bindings_head && (ret = bind_one(j, j->bindings_head))) |
| 531 | return ret; |
| 532 | |
| 533 | if (chroot(j->chrootdir)) |
| 534 | return -errno; |
| 535 | |
| 536 | if (chdir("/")) |
| 537 | return -errno; |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 542 | int remount_readonly(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 543 | { |
| 544 | const char *kProcPath = "/proc"; |
| 545 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 546 | /* |
| 547 | * 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] | 548 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 549 | * mutate our parent's mount as well, even though we're in a VFS |
| 550 | * namespace (!). Instead, remove their mount from our namespace |
| 551 | * and make our own. |
| 552 | */ |
| 553 | if (umount(kProcPath)) |
| 554 | return -errno; |
| 555 | if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
| 556 | return -errno; |
| 557 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 558 | } |
| 559 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 560 | void drop_caps(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 561 | { |
| 562 | cap_t caps = cap_get_proc(); |
| 563 | cap_value_t raise_flag[1]; |
| 564 | unsigned int i; |
| 565 | if (!caps) |
| 566 | die("can't get process caps"); |
| 567 | if (cap_clear_flag(caps, CAP_INHERITABLE)) |
| 568 | die("can't clear inheritable caps"); |
| 569 | if (cap_clear_flag(caps, CAP_EFFECTIVE)) |
| 570 | die("can't clear effective caps"); |
| 571 | if (cap_clear_flag(caps, CAP_PERMITTED)) |
| 572 | die("can't clear permitted caps"); |
| 573 | for (i = 0; i < sizeof(j->caps) * 8 && cap_valid((int)i); ++i) { |
| 574 | if (i != CAP_SETPCAP && !(j->caps & (1 << i))) |
| 575 | continue; |
| 576 | raise_flag[0] = i; |
| 577 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, raise_flag, CAP_SET)) |
| 578 | die("can't add effective cap"); |
| 579 | if (cap_set_flag(caps, CAP_PERMITTED, 1, raise_flag, CAP_SET)) |
| 580 | die("can't add permitted cap"); |
| 581 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, raise_flag, CAP_SET)) |
| 582 | die("can't add inheritable cap"); |
| 583 | } |
| 584 | if (cap_set_proc(caps)) |
| 585 | die("can't apply cleaned capset"); |
| 586 | cap_free(caps); |
| 587 | for (i = 0; i < sizeof(j->caps) * 8 && cap_valid((int)i); ++i) { |
| 588 | if (j->caps & (1 << i)) |
| 589 | continue; |
| 590 | if (prctl(PR_CAPBSET_DROP, i)) |
| 591 | pdie("prctl(PR_CAPBSET_DROP)"); |
| 592 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 593 | } |
| 594 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 595 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 596 | { |
| 597 | if (j->flags.pids) |
| 598 | die("tried to enter a pid-namespaced jail;" |
| 599 | "try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 600 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 601 | if (j->flags.usergroups && !j->user) |
| 602 | die("usergroup inheritance without username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 603 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 604 | /* |
| 605 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 606 | * so we don't even try. If any of our operations fail, we abort() the |
| 607 | * entire process. |
| 608 | */ |
| 609 | if (j->flags.vfs && unshare(CLONE_NEWNS)) |
| 610 | pdie("unshare"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 611 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 612 | if (j->flags.chroot && enter_chroot(j)) |
| 613 | pdie("chroot"); |
| 614 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 615 | if (j->flags.readonly && remount_readonly()) |
| 616 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 617 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 618 | if (j->flags.caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 619 | /* |
| 620 | * POSIX capabilities are a bit tricky. If we drop our |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 621 | * capability to change uids, our attempt to use setuid() |
| 622 | * below will fail. Hang on to root caps across setuid(), then |
| 623 | * lock securebits. |
| 624 | */ |
| 625 | if (prctl(PR_SET_KEEPCAPS, 1)) |
| 626 | pdie("prctl(PR_SET_KEEPCAPS)"); |
| 627 | if (prctl |
| 628 | (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS)) |
| 629 | pdie("prctl(PR_SET_SECUREBITS)"); |
| 630 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 631 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 632 | /* |
Elly Jones | 1c888ae | 2012-07-31 12:23:47 -0400 | [diff] [blame] | 633 | * Set no_new_privs before installing seccomp filter. See |
| 634 | * </kernel/seccomp.c> and </kernel/sys.c> in the kernel source tree for |
| 635 | * an explanation of the parameters. |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 636 | */ |
| 637 | if (j->flags.no_new_privs) { |
| 638 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 639 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Install seccomp filter before dropping root and caps. |
| 644 | * WARNING: this means that filter policies *must* allow |
| 645 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 646 | * capget()/capset()/prctl() for dropping caps. |
| 647 | */ |
| 648 | if (j->flags.seccomp_filter) { |
| 649 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) |
| 650 | pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)"); |
| 651 | } |
| 652 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 653 | if (j->flags.usergroups) { |
| 654 | if (initgroups(j->user, j->usergid)) |
| 655 | pdie("initgroups"); |
| 656 | } else { |
| 657 | /* Only attempt to clear supplemental groups if we are changing |
| 658 | * users. */ |
| 659 | if ((j->uid || j->gid) && setgroups(0, NULL)) |
| 660 | pdie("setgroups"); |
| 661 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 662 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 663 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
| 664 | pdie("setresgid"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 665 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 666 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
| 667 | pdie("setresuid"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 668 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 669 | if (j->flags.caps) |
| 670 | drop_caps(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 671 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 672 | /* |
| 673 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 674 | * privilege-dropping syscalls :) |
| 675 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 676 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) |
| 677 | pdie("prctl(PR_SET_SECCOMP)"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 678 | } |
| 679 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 680 | /* TODO(wad) will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 681 | static int init_exitstatus = 0; |
| 682 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 683 | void init_term(int __attribute__ ((unused)) sig) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 684 | { |
| 685 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 686 | } |
| 687 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 688 | int init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 689 | { |
| 690 | pid_t pid; |
| 691 | int status; |
| 692 | /* so that we exit with the right status */ |
| 693 | signal(SIGTERM, init_term); |
| 694 | /* TODO(wad) self jail with seccomp_filters here. */ |
| 695 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 696 | /* |
| 697 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 698 | * left inside our pid namespace or we get a signal. |
| 699 | */ |
| 700 | if (pid == rootpid) |
| 701 | init_exitstatus = status; |
| 702 | } |
| 703 | if (!WIFEXITED(init_exitstatus)) |
| 704 | _exit(MINIJAIL_ERR_INIT); |
| 705 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 708 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 709 | { |
| 710 | size_t sz = 0; |
| 711 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 712 | char *buf; |
| 713 | int r; |
| 714 | if (sizeof(sz) != bytes) |
| 715 | return -EINVAL; |
| 716 | if (sz > USHRT_MAX) /* Arbitrary sanity check */ |
| 717 | return -E2BIG; |
| 718 | buf = malloc(sz); |
| 719 | if (!buf) |
| 720 | return -ENOMEM; |
| 721 | bytes = read(fd, buf, sz); |
| 722 | if (bytes != sz) { |
| 723 | free(buf); |
| 724 | return -EINVAL; |
| 725 | } |
| 726 | r = minijail_unmarshal(j, buf, sz); |
| 727 | free(buf); |
| 728 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 729 | } |
| 730 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 731 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 732 | { |
| 733 | char *buf; |
| 734 | size_t sz = minijail_size(j); |
| 735 | ssize_t written; |
| 736 | int r; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 737 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 738 | if (!sz) |
| 739 | return -EINVAL; |
| 740 | buf = malloc(sz); |
| 741 | r = minijail_marshal(j, buf, sz); |
| 742 | if (r) { |
| 743 | free(buf); |
| 744 | return r; |
| 745 | } |
| 746 | /* Sends [size][minijail]. */ |
| 747 | written = write(fd, &sz, sizeof(sz)); |
| 748 | if (written != sizeof(sz)) { |
| 749 | free(buf); |
| 750 | return -EFAULT; |
| 751 | } |
| 752 | written = write(fd, buf, sz); |
| 753 | if (written < 0 || (size_t) written != sz) { |
| 754 | free(buf); |
| 755 | return -EFAULT; |
| 756 | } |
| 757 | free(buf); |
| 758 | return 0; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 759 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 760 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 761 | int setup_preload(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 762 | { |
| 763 | char *oldenv = getenv(kLdPreloadEnvVar) ? : ""; |
| 764 | char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH)); |
| 765 | if (!newenv) |
| 766 | return -ENOMEM; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 767 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 768 | /* Only insert a separating space if we have something to separate... */ |
| 769 | sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "", |
| 770 | PRELOADPATH); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 771 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 772 | /* setenv() makes a copy of the string we give it */ |
| 773 | setenv(kLdPreloadEnvVar, newenv, 1); |
| 774 | free(newenv); |
| 775 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 776 | } |
| 777 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 778 | int setup_pipe(int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 779 | { |
| 780 | int r = pipe(fds); |
| 781 | char fd_buf[11]; |
| 782 | if (r) |
| 783 | return r; |
| 784 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 785 | if (r <= 0) |
| 786 | return -EINVAL; |
| 787 | setenv(kFdEnvVar, fd_buf, 1); |
| 788 | return 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 789 | } |
| 790 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 791 | int API minijail_run(struct minijail *j, const char *filename, |
| 792 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 793 | { |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 794 | return minijail_run_pid(j, filename, argv, NULL); |
| 795 | } |
| 796 | |
| 797 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 798 | char *const argv[], pid_t *pchild_pid) |
| 799 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 800 | char *oldenv, *oldenv_copy = NULL; |
| 801 | pid_t child_pid; |
| 802 | int pipe_fds[2]; |
| 803 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 804 | /* We need to remember this across the minijail_preexec() call. */ |
| 805 | int pid_namespace = j->flags.pids; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 806 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 807 | oldenv = getenv(kLdPreloadEnvVar); |
| 808 | if (oldenv) { |
| 809 | oldenv_copy = strdup(oldenv); |
| 810 | if (!oldenv_copy) |
| 811 | return -ENOMEM; |
| 812 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 813 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 814 | if (setup_preload()) |
| 815 | return -EFAULT; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 816 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 817 | /* |
| 818 | * 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] | 819 | * a pipe(2) to send the minijail configuration over. |
| 820 | */ |
| 821 | if (setup_pipe(pipe_fds)) |
| 822 | return -EFAULT; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 823 | |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 824 | /* Use sys_clone() if and only if we're creating a pid namespace. |
| 825 | * |
| 826 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 827 | * |
| 828 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 829 | * some of which may be held by other threads at the time that we call |
| 830 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 831 | * ensure that we hold all of these locks before it calls clone() |
| 832 | * internally and drop them after clone() returns, but when we call |
| 833 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 834 | * child address space where some of libc's important locks are held by |
| 835 | * other threads (which did not get cloned, and hence will never release |
| 836 | * those locks). This is okay so long as we call exec() immediately |
| 837 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 838 | * take locks. |
| 839 | * |
| 840 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 841 | * namespacing. If we follow this path, the child's address space might |
| 842 | * have broken locks; you may only call functions that do not acquire |
| 843 | * any locks. |
| 844 | * |
| 845 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 846 | * previously detailed, so this function is highly likely to deadlock |
| 847 | * later on (see "deadlock here") if we're multithreaded. |
| 848 | * |
| 849 | * We might hack around this by having the clone()d child (init of the |
| 850 | * pid namespace) return directly, rather than leaving the clone()d |
| 851 | * process hanging around to be init for the new namespace (and having |
| 852 | * its fork()ed child return in turn), but that process would be crippled |
| 853 | * with its libc locks potentially broken. We might try fork()ing in the |
| 854 | * parent before we clone() to ensure that we own all the locks, but |
| 855 | * then we have to have the forked child hanging around consuming |
| 856 | * resources (and possibly having file descriptors / shared memory |
| 857 | * regions / etc attached). We'd need to keep the child around to avoid |
| 858 | * having its children get reparented to init. |
| 859 | * |
| 860 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 861 | * problem is fixable or not. It would be nice if we worked in this |
| 862 | * case. |
| 863 | */ |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 864 | if (pid_namespace) |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 865 | child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL); |
| 866 | else |
| 867 | child_pid = fork(); |
| 868 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 869 | if (child_pid < 0) { |
| 870 | free(oldenv_copy); |
| 871 | return child_pid; |
| 872 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 873 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 874 | if (child_pid) { |
| 875 | /* Restore parent's LD_PRELOAD. */ |
| 876 | if (oldenv_copy) { |
| 877 | setenv(kLdPreloadEnvVar, oldenv_copy, 1); |
| 878 | free(oldenv_copy); |
| 879 | } else { |
| 880 | unsetenv(kLdPreloadEnvVar); |
| 881 | } |
| 882 | unsetenv(kFdEnvVar); |
| 883 | j->initpid = child_pid; |
| 884 | close(pipe_fds[0]); /* read endpoint */ |
| 885 | ret = minijail_to_fd(j, pipe_fds[1]); |
| 886 | close(pipe_fds[1]); /* write endpoint */ |
| 887 | if (ret) { |
| 888 | kill(j->initpid, SIGKILL); |
| 889 | die("failed to send marshalled minijail"); |
| 890 | } |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 891 | if (pchild_pid) |
| 892 | *pchild_pid = child_pid; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 893 | return 0; |
| 894 | } |
| 895 | free(oldenv_copy); |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 896 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 897 | /* Drop everything that cannot be inherited across execve. */ |
| 898 | minijail_preexec(j); |
| 899 | /* Jail this process and its descendants... */ |
| 900 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 901 | |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 902 | if (pid_namespace) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 903 | /* |
| 904 | * pid namespace: this process will become init inside the new |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 905 | * namespace, so fork off a child to actually run the program |
| 906 | * (we don't want all programs we might exec to have to know |
| 907 | * how to be init). |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 908 | * |
| 909 | * If we're multithreaded, we'll probably deadlock here. See |
| 910 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 911 | */ |
| 912 | child_pid = fork(); |
| 913 | if (child_pid < 0) |
| 914 | _exit(child_pid); |
| 915 | else if (child_pid > 0) |
| 916 | init(child_pid); /* never returns */ |
| 917 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 918 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 919 | /* |
| 920 | * If we aren't pid-namespaced: |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 921 | * calling process |
| 922 | * -> execve()-ing process |
| 923 | * If we are: |
| 924 | * calling process |
| 925 | * -> init()-ing process |
| 926 | * -> execve()-ing process |
| 927 | */ |
| 928 | _exit(execve(filename, argv, environ)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 929 | } |
| 930 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 931 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 932 | { |
| 933 | int st; |
| 934 | if (kill(j->initpid, SIGTERM)) |
| 935 | return -errno; |
| 936 | if (waitpid(j->initpid, &st, 0) < 0) |
| 937 | return -errno; |
| 938 | return st; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 939 | } |
| 940 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 941 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 942 | { |
| 943 | int st; |
| 944 | if (waitpid(j->initpid, &st, 0) < 0) |
| 945 | return -errno; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 946 | if (!WIFEXITED(st)) { |
| 947 | if (WIFSIGNALED(st)) |
| 948 | warn("child process received signal %d", WTERMSIG(st)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 949 | return MINIJAIL_ERR_JAIL; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 950 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 951 | return WEXITSTATUS(st); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 952 | } |
| 953 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 954 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 955 | { |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 956 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 957 | free(j->filter_prog->filter); |
| 958 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 959 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 960 | while (j->bindings_head) { |
| 961 | struct binding *b = j->bindings_head; |
| 962 | j->bindings_head = j->bindings_head->next; |
| 963 | free(b->dest); |
| 964 | free(b->src); |
| 965 | free(b); |
| 966 | } |
| 967 | j->bindings_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 968 | if (j->user) |
| 969 | free(j->user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 970 | if (j->chrootdir) |
| 971 | free(j->chrootdir); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 972 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 973 | } |