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