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 | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 21 | #include <stdbool.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 | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 33 | #include <sys/user.h> |
Jeff Vander Stoep | 2885bef | 2016-01-11 15:22:42 -0800 | [diff] [blame] | 34 | #include <sys/utsname.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 35 | #include <sys/wait.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 36 | #include <unistd.h> |
| 37 | |
| 38 | #include "libminijail.h" |
| 39 | #include "libminijail-private.h" |
| 40 | |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 41 | #include "signal_handler.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 42 | #include "syscall_filter.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 43 | #include "util.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 44 | |
Lei Zhang | eee3155 | 2012-10-17 21:27:10 -0700 | [diff] [blame] | 45 | #ifdef HAVE_SECUREBITS_H |
| 46 | #include <linux/securebits.h> |
| 47 | #else |
| 48 | #define SECURE_ALL_BITS 0x15 |
| 49 | #define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) |
| 50 | #endif |
| 51 | |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 52 | /* Until these are reliably available in linux/prctl.h */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 53 | #ifndef PR_SET_SECCOMP |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 54 | # define PR_SET_SECCOMP 22 |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 55 | #endif |
| 56 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 57 | #ifndef PR_ALT_SYSCALL |
| 58 | # define PR_ALT_SYSCALL 0x43724f53 |
| 59 | #endif |
| 60 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 61 | /* For seccomp_filter using BPF. */ |
| 62 | #ifndef PR_SET_NO_NEW_PRIVS |
| 63 | # define PR_SET_NO_NEW_PRIVS 38 |
| 64 | #endif |
| 65 | #ifndef SECCOMP_MODE_FILTER |
| 66 | # define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */ |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 67 | #endif |
| 68 | |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 69 | #ifdef USE_SECCOMP_SOFTFAIL |
| 70 | # define SECCOMP_SOFTFAIL 1 |
| 71 | #else |
| 72 | # define SECCOMP_SOFTFAIL 0 |
| 73 | #endif |
| 74 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 75 | #define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */ |
| 76 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 77 | struct mountpoint { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 78 | char *src; |
| 79 | char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 80 | char *type; |
| 81 | unsigned long flags; |
| 82 | struct mountpoint *next; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 83 | }; |
| 84 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 85 | struct minijail { |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 86 | /* |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 87 | * WARNING: if you add a flag here you need to make sure it's |
| 88 | * accounted for in minijail_pre{enter|exec}() below. |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 89 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 90 | struct { |
| 91 | int uid:1; |
| 92 | int gid:1; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 93 | int usergroups:1; |
| 94 | int suppl_gids:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 95 | int caps:1; |
| 96 | int vfs:1; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 97 | int enter_vfs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 98 | int pids:1; |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 99 | int ipc:1; |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 100 | int net:1; |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 101 | int enter_net:1; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 102 | int userns:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 103 | int seccomp:1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 104 | int remount_proc_ro:1; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 105 | int no_new_privs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 106 | int seccomp_filter:1; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 107 | int log_seccomp_filter:1; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 108 | int chroot:1; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 109 | int pivot_root:1; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 110 | int mount_tmp:1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 111 | int do_init:1; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 112 | int pid_file:1; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 113 | int cgroups:1; |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 114 | int alt_syscall:1; |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 115 | int reset_signal_mask:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 116 | } flags; |
| 117 | uid_t uid; |
| 118 | gid_t gid; |
| 119 | gid_t usergid; |
| 120 | char *user; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 121 | size_t suppl_gid_count; |
| 122 | gid_t *suppl_gid_list; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 123 | uint64_t caps; |
| 124 | pid_t initpid; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 125 | int mountns_fd; |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 126 | int netns_fd; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 127 | char *chrootdir; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 128 | char *pid_file_path; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 129 | char *uidmap; |
| 130 | char *gidmap; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 131 | size_t filter_len; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 132 | struct sock_fprog *filter_prog; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 133 | char *alt_syscall_table; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 134 | struct mountpoint *mounts_head; |
| 135 | struct mountpoint *mounts_tail; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 136 | size_t mounts_count; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 137 | char *cgroups[MAX_CGROUPS]; |
| 138 | size_t cgroup_count; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 139 | }; |
| 140 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 141 | /* |
| 142 | * Strip out flags meant for the parent. |
| 143 | * We keep things that are not inherited across execve(2) (e.g. capabilities), |
| 144 | * or are easier to set after execve(2) (e.g. seccomp filters). |
| 145 | */ |
| 146 | void minijail_preenter(struct minijail *j) |
| 147 | { |
| 148 | j->flags.vfs = 0; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 149 | j->flags.enter_vfs = 0; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 150 | j->flags.remount_proc_ro = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 151 | j->flags.pids = 0; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 152 | j->flags.do_init = 0; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 153 | j->flags.pid_file = 0; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 154 | j->flags.cgroups = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Strip out flags meant for the child. |
| 159 | * We keep things that are inherited across execve(2). |
| 160 | */ |
| 161 | void minijail_preexec(struct minijail *j) |
| 162 | { |
| 163 | int vfs = j->flags.vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 164 | int enter_vfs = j->flags.enter_vfs; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 165 | int remount_proc_ro = j->flags.remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 166 | int userns = j->flags.userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 167 | if (j->user) |
| 168 | free(j->user); |
| 169 | j->user = NULL; |
Jorge Lucangeli Obes | e81a52f | 2015-12-04 16:05:23 -0800 | [diff] [blame] | 170 | if (j->suppl_gid_list) |
| 171 | free(j->suppl_gid_list); |
| 172 | j->suppl_gid_list = NULL; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 173 | memset(&j->flags, 0, sizeof(j->flags)); |
| 174 | /* Now restore anything we meant to keep. */ |
| 175 | j->flags.vfs = vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 176 | j->flags.enter_vfs = enter_vfs; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 177 | j->flags.remount_proc_ro = remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 178 | j->flags.userns = userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 179 | /* Note, |pids| will already have been used before this call. */ |
| 180 | } |
| 181 | |
Jorge Lucangeli Obes | 272e3ab | 2016-01-12 21:18:59 -0800 | [diff] [blame] | 182 | /* Returns true if the kernel version is less than 3.8. */ |
| 183 | int seccomp_kernel_support_not_required() |
Jeff Vander Stoep | 2885bef | 2016-01-11 15:22:42 -0800 | [diff] [blame] | 184 | { |
| 185 | int major, minor; |
| 186 | struct utsname uts; |
| 187 | return (uname(&uts) != -1 && |
| 188 | sscanf(uts.release, "%d.%d", &major, &minor) == 2 && |
| 189 | ((major < 3) || ((major == 3) && (minor < 8)))); |
| 190 | } |
| 191 | |
Jorge Lucangeli Obes | 272e3ab | 2016-01-12 21:18:59 -0800 | [diff] [blame] | 192 | /* Allow seccomp soft-fail on Android devices with kernel version < 3.8. */ |
| 193 | int can_softfail() |
Jeff Vander Stoep | 2885bef | 2016-01-11 15:22:42 -0800 | [diff] [blame] | 194 | { |
| 195 | #if SECCOMP_SOFTFAIL |
| 196 | if (is_android()) { |
| 197 | if (seccomp_kernel_support_not_required()) |
| 198 | return 1; |
| 199 | else |
| 200 | return 0; |
| 201 | } else { |
| 202 | return 1; |
| 203 | } |
| 204 | #endif |
| 205 | return 0; |
| 206 | } |
| 207 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 208 | /* Minijail API. */ |
| 209 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 210 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 211 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 212 | return calloc(1, sizeof(struct minijail)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 215 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 216 | { |
| 217 | if (uid == 0) |
| 218 | die("useless change to uid 0"); |
| 219 | j->uid = uid; |
| 220 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 223 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 224 | { |
| 225 | if (gid == 0) |
| 226 | die("useless change to gid 0"); |
| 227 | j->gid = gid; |
| 228 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 231 | void API minijail_set_supplementary_gids(struct minijail *j, size_t size, |
| 232 | const gid_t *list) |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 233 | { |
Jorge Lucangeli Obes | 06940be | 2015-12-04 18:09:21 -0800 | [diff] [blame] | 234 | size_t i; |
| 235 | |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 236 | if (j->flags.usergroups) |
| 237 | die("cannot inherit *and* set supplementary groups"); |
| 238 | |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 239 | if (size == 0) { |
| 240 | /* Clear supplementary groups. */ |
| 241 | j->suppl_gid_list = NULL; |
| 242 | j->suppl_gid_count = 0; |
| 243 | j->flags.suppl_gids = 1; |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 244 | return; |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 245 | } |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 246 | |
| 247 | /* Copy the gid_t array. */ |
| 248 | j->suppl_gid_list = calloc(size, sizeof(gid_t)); |
| 249 | if (!j->suppl_gid_list) { |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 250 | die("failed to allocate internal supplementary group array"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 251 | } |
Jorge Lucangeli Obes | 06940be | 2015-12-04 18:09:21 -0800 | [diff] [blame] | 252 | for (i = 0; i < size; i++) { |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 253 | j->suppl_gid_list[i] = list[i]; |
| 254 | } |
| 255 | j->suppl_gid_count = size; |
| 256 | j->flags.suppl_gids = 1; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 259 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 260 | { |
| 261 | char *buf = NULL; |
| 262 | struct passwd pw; |
| 263 | struct passwd *ppw = NULL; |
| 264 | ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 265 | if (sz == -1) |
| 266 | sz = 65536; /* your guess is as good as mine... */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 267 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 268 | /* |
| 269 | * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 270 | * the maximum needed size of the buffer, so we don't have to search. |
| 271 | */ |
| 272 | buf = malloc(sz); |
| 273 | if (!buf) |
| 274 | return -ENOMEM; |
| 275 | getpwnam_r(user, &pw, buf, sz, &ppw); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 276 | /* |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 277 | * We're safe to free the buffer here. The strings inside |pw| point |
| 278 | * inside |buf|, but we don't use any of them; this leaves the pointers |
| 279 | * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3) succeeded. |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 280 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 281 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 282 | /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 283 | if (!ppw) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 284 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 285 | minijail_change_uid(j, ppw->pw_uid); |
| 286 | j->user = strdup(user); |
| 287 | if (!j->user) |
| 288 | return -ENOMEM; |
| 289 | j->usergid = ppw->pw_gid; |
| 290 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 293 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 294 | { |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 295 | char *buf = NULL; |
Yabin Cui | 1b21c8f | 2015-07-22 10:34:45 -0700 | [diff] [blame] | 296 | struct group gr; |
| 297 | struct group *pgr = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 298 | ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 299 | if (sz == -1) |
| 300 | sz = 65536; /* and mine is as good as yours, really */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 301 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 302 | /* |
| 303 | * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 304 | * the maximum needed size of the buffer, so we don't have to search. |
| 305 | */ |
| 306 | buf = malloc(sz); |
| 307 | if (!buf) |
| 308 | return -ENOMEM; |
| 309 | getgrnam_r(group, &gr, buf, sz, &pgr); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 310 | /* |
| 311 | * We're safe to free the buffer here. The strings inside gr point |
| 312 | * inside buf, but we don't use any of them; this leaves the pointers |
| 313 | * dangling but it's safe. pgr points at gr if getgrnam_r succeeded. |
| 314 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 315 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 316 | /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 317 | if (!pgr) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 318 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 319 | minijail_change_gid(j, pgr->gr_gid); |
| 320 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 321 | } |
| 322 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 323 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 324 | { |
| 325 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 328 | void API minijail_no_new_privs(struct minijail *j) |
| 329 | { |
| 330 | j->flags.no_new_privs = 1; |
| 331 | } |
| 332 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 333 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 334 | { |
| 335 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 338 | void API minijail_log_seccomp_filter_failures(struct minijail *j) |
| 339 | { |
| 340 | j->flags.log_seccomp_filter = 1; |
| 341 | } |
| 342 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 343 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 344 | { |
| 345 | j->caps = capmask; |
| 346 | j->flags.caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 349 | void API minijail_reset_signal_mask(struct minijail* j) { |
| 350 | j->flags.reset_signal_mask = 1; |
| 351 | } |
| 352 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 353 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 354 | { |
| 355 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 358 | void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path) |
| 359 | { |
| 360 | int ns_fd = open(ns_path, O_RDONLY); |
| 361 | if (ns_fd < 0) { |
| 362 | pdie("failed to open namespace '%s'", ns_path); |
| 363 | } |
| 364 | j->mountns_fd = ns_fd; |
| 365 | j->flags.enter_vfs = 1; |
| 366 | } |
| 367 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 368 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 369 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 370 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 371 | j->flags.remount_proc_ro = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 372 | j->flags.pids = 1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 373 | j->flags.do_init = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 374 | } |
| 375 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 376 | void API minijail_namespace_ipc(struct minijail *j) |
| 377 | { |
| 378 | j->flags.ipc = 1; |
| 379 | } |
| 380 | |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 381 | void API minijail_namespace_net(struct minijail *j) |
| 382 | { |
| 383 | j->flags.net = 1; |
| 384 | } |
| 385 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 386 | void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path) |
| 387 | { |
| 388 | int ns_fd = open(ns_path, O_RDONLY); |
| 389 | if (ns_fd < 0) { |
| 390 | pdie("failed to open namespace '%s'", ns_path); |
| 391 | } |
| 392 | j->netns_fd = ns_fd; |
| 393 | j->flags.enter_net = 1; |
| 394 | } |
| 395 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 396 | void API minijail_remount_proc_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 397 | { |
| 398 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 399 | j->flags.remount_proc_ro = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 400 | } |
| 401 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 402 | void API minijail_namespace_user(struct minijail *j) |
| 403 | { |
| 404 | j->flags.userns = 1; |
| 405 | } |
| 406 | |
| 407 | int API minijail_uidmap(struct minijail *j, const char *uidmap) |
| 408 | { |
| 409 | j->uidmap = strdup(uidmap); |
| 410 | if (!j->uidmap) |
| 411 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 412 | char *ch; |
| 413 | for (ch = j->uidmap; *ch; ch++) { |
| 414 | if (*ch == ',') |
| 415 | *ch = '\n'; |
| 416 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | int API minijail_gidmap(struct minijail *j, const char *gidmap) |
| 421 | { |
| 422 | j->gidmap = strdup(gidmap); |
| 423 | if (!j->gidmap) |
| 424 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 425 | char *ch; |
| 426 | for (ch = j->gidmap; *ch; ch++) { |
| 427 | if (*ch == ',') |
| 428 | *ch = '\n'; |
| 429 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 433 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 434 | { |
| 435 | j->flags.usergroups = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 438 | void API minijail_run_as_init(struct minijail *j) |
| 439 | { |
| 440 | /* |
| 441 | * Since the jailed program will become 'init' in the new PID namespace, |
| 442 | * Minijail does not need to fork an 'init' process. |
| 443 | */ |
| 444 | j->flags.do_init = 0; |
| 445 | } |
| 446 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 447 | int API minijail_enter_chroot(struct minijail *j, const char *dir) |
| 448 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 449 | if (j->chrootdir) |
| 450 | return -EINVAL; |
| 451 | j->chrootdir = strdup(dir); |
| 452 | if (!j->chrootdir) |
| 453 | return -ENOMEM; |
| 454 | j->flags.chroot = 1; |
| 455 | return 0; |
| 456 | } |
| 457 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 458 | int API minijail_enter_pivot_root(struct minijail *j, const char *dir) |
| 459 | { |
| 460 | if (j->chrootdir) |
| 461 | return -EINVAL; |
| 462 | j->chrootdir = strdup(dir); |
| 463 | if (!j->chrootdir) |
| 464 | return -ENOMEM; |
| 465 | j->flags.pivot_root = 1; |
| 466 | return 0; |
| 467 | } |
| 468 | |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 469 | static char *append_external_path(const char *external_path, |
| 470 | const char *path_inside_chroot) |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 471 | { |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 472 | char *path; |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 473 | size_t pathlen; |
| 474 | |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 475 | /* One extra char for '/' and one for '\0', hence + 2. */ |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 476 | pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2; |
| 477 | path = malloc(pathlen); |
| 478 | snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot); |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 479 | |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 480 | return path; |
| 481 | } |
| 482 | |
| 483 | char API *minijail_get_original_path(struct minijail *j, |
| 484 | const char *path_inside_chroot) |
| 485 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 486 | struct mountpoint *b; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 487 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 488 | b = j->mounts_head; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 489 | while (b) { |
| 490 | /* |
| 491 | * If |path_inside_chroot| is the exact destination of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 492 | * mount, then the original path is exactly the source of |
| 493 | * the mount. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 494 | * for example: "-b /some/path/exe,/chroot/path/exe" |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 495 | * mount source = /some/path/exe, mount dest = |
| 496 | * /chroot/path/exe Then when getting the original path of |
| 497 | * "/chroot/path/exe", the source of that mount, |
| 498 | * "/some/path/exe" is what should be returned. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 499 | */ |
| 500 | if (!strcmp(b->dest, path_inside_chroot)) |
| 501 | return strdup(b->src); |
| 502 | |
| 503 | /* |
| 504 | * If |path_inside_chroot| is within the destination path of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 505 | * mount, take the suffix of the chroot path relative to the |
| 506 | * mount destination path, and append it to the mount source |
| 507 | * path. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 508 | */ |
| 509 | if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) { |
| 510 | const char *relative_path = |
| 511 | path_inside_chroot + strlen(b->dest); |
| 512 | return append_external_path(b->src, relative_path); |
| 513 | } |
| 514 | b = b->next; |
| 515 | } |
| 516 | |
| 517 | /* If there is a chroot path, append |path_inside_chroot| to that. */ |
| 518 | if (j->chrootdir) |
| 519 | return append_external_path(j->chrootdir, path_inside_chroot); |
| 520 | |
| 521 | /* No chroot, so the path outside is the same as it is inside. */ |
| 522 | return strdup(path_inside_chroot); |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 525 | void API minijail_mount_tmp(struct minijail *j) |
| 526 | { |
| 527 | j->flags.mount_tmp = 1; |
| 528 | } |
| 529 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 530 | int API minijail_write_pid_file(struct minijail *j, const char *path) |
| 531 | { |
| 532 | j->pid_file_path = strdup(path); |
| 533 | if (!j->pid_file_path) |
| 534 | return -ENOMEM; |
| 535 | j->flags.pid_file = 1; |
| 536 | return 0; |
| 537 | } |
| 538 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 539 | int API minijail_add_to_cgroup(struct minijail *j, const char *path) |
| 540 | { |
| 541 | if (j->cgroup_count >= MAX_CGROUPS) |
| 542 | return -ENOMEM; |
| 543 | j->cgroups[j->cgroup_count] = strdup(path); |
| 544 | if (!j->cgroups[j->cgroup_count]) |
| 545 | return -ENOMEM; |
| 546 | j->cgroup_count++; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 547 | j->flags.cgroups = 1; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 551 | int API minijail_mount(struct minijail *j, const char *src, const char *dest, |
| 552 | const char *type, unsigned long flags) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 553 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 554 | struct mountpoint *m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 555 | |
| 556 | if (*dest != '/') |
| 557 | return -EINVAL; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 558 | m = calloc(1, sizeof(*m)); |
| 559 | if (!m) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 560 | return -ENOMEM; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 561 | m->dest = strdup(dest); |
| 562 | if (!m->dest) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 563 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 564 | m->src = strdup(src); |
| 565 | if (!m->src) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 566 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 567 | m->type = strdup(type); |
| 568 | if (!m->type) |
| 569 | goto error; |
| 570 | m->flags = flags; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 571 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 572 | info("mount %s -> %s type %s", src, dest, type); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 573 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 574 | /* |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 575 | * Force vfs namespacing so the mounts don't leak out into the |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 576 | * containing vfs namespace. |
| 577 | */ |
| 578 | minijail_namespace_vfs(j); |
| 579 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 580 | if (j->mounts_tail) |
| 581 | j->mounts_tail->next = m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 582 | else |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 583 | j->mounts_head = m; |
| 584 | j->mounts_tail = m; |
| 585 | j->mounts_count++; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 586 | |
| 587 | return 0; |
| 588 | |
| 589 | error: |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 590 | free(m->src); |
| 591 | free(m->dest); |
| 592 | free(m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 593 | return -ENOMEM; |
| 594 | } |
| 595 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 596 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
| 597 | int writeable) |
| 598 | { |
| 599 | unsigned long flags = MS_BIND; |
| 600 | |
| 601 | if (!writeable) |
| 602 | flags |= MS_RDONLY; |
| 603 | |
| 604 | return minijail_mount(j, src, dest, "", flags); |
| 605 | } |
| 606 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 607 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 608 | { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 609 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) { |
Jeff Vander Stoep | 2885bef | 2016-01-11 15:22:42 -0800 | [diff] [blame] | 610 | if ((errno == EINVAL) && can_softfail()) { |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 611 | warn("not loading seccomp filter," |
| 612 | " seccomp not supported"); |
Jeff Vander Stoep | 2885bef | 2016-01-11 15:22:42 -0800 | [diff] [blame] | 613 | j->flags.seccomp_filter = 0; |
| 614 | j->flags.log_seccomp_filter = 0; |
| 615 | j->filter_len = 0; |
| 616 | j->filter_prog = NULL; |
| 617 | j->flags.no_new_privs = 0; |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 620 | FILE *file = fopen(path, "r"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 621 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 622 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 623 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 624 | |
| 625 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 626 | if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) { |
| 627 | die("failed to compile seccomp filter BPF program in '%s'", |
| 628 | path); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | j->filter_len = fprog->len; |
| 632 | j->filter_prog = fprog; |
| 633 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 634 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 635 | } |
| 636 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 637 | int API minijail_use_alt_syscall(struct minijail *j, const char *table) |
| 638 | { |
| 639 | j->alt_syscall_table = strdup(table); |
| 640 | if (!j->alt_syscall_table) |
| 641 | return -ENOMEM; |
| 642 | j->flags.alt_syscall = 1; |
| 643 | return 0; |
| 644 | } |
| 645 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 646 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 647 | size_t available; |
| 648 | size_t total; |
| 649 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 650 | }; |
| 651 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 652 | void marshal_state_init(struct marshal_state *state, |
| 653 | char *buf, size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 654 | { |
| 655 | state->available = available; |
| 656 | state->buf = buf; |
| 657 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 658 | } |
| 659 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 660 | void marshal_append(struct marshal_state *state, |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 661 | void *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 662 | { |
| 663 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 664 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 665 | /* Up to |available| will be written. */ |
| 666 | if (copy_len) { |
| 667 | memcpy(state->buf, src, copy_len); |
| 668 | state->buf += copy_len; |
| 669 | state->available -= copy_len; |
| 670 | } |
| 671 | /* |total| will contain the expected length. */ |
| 672 | state->total += length; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 673 | } |
| 674 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 675 | void minijail_marshal_helper(struct marshal_state *state, |
| 676 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 677 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 678 | struct mountpoint *m = NULL; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 679 | size_t i; |
| 680 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 681 | marshal_append(state, (char *)j, sizeof(*j)); |
| 682 | if (j->user) |
| 683 | marshal_append(state, j->user, strlen(j->user) + 1); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 684 | if (j->suppl_gid_list) { |
| 685 | marshal_append(state, j->suppl_gid_list, |
| 686 | j->suppl_gid_count * sizeof(gid_t)); |
| 687 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 688 | if (j->chrootdir) |
| 689 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 690 | if (j->alt_syscall_table) { |
| 691 | marshal_append(state, j->alt_syscall_table, |
| 692 | strlen(j->alt_syscall_table) + 1); |
| 693 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 694 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 695 | struct sock_fprog *fp = j->filter_prog; |
| 696 | marshal_append(state, (char *)fp->filter, |
| 697 | fp->len * sizeof(struct sock_filter)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 698 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 699 | for (m = j->mounts_head; m; m = m->next) { |
| 700 | marshal_append(state, m->src, strlen(m->src) + 1); |
| 701 | marshal_append(state, m->dest, strlen(m->dest) + 1); |
| 702 | marshal_append(state, m->type, strlen(m->type) + 1); |
| 703 | marshal_append(state, (char *)&m->flags, sizeof(m->flags)); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 704 | } |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 705 | for (i = 0; i < j->cgroup_count; ++i) |
| 706 | marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 707 | } |
| 708 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 709 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 710 | { |
| 711 | struct marshal_state state; |
| 712 | marshal_state_init(&state, NULL, 0); |
| 713 | minijail_marshal_helper(&state, j); |
| 714 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 715 | } |
| 716 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 717 | int minijail_marshal(const struct minijail *j, char *buf, size_t available) |
| 718 | { |
| 719 | struct marshal_state state; |
| 720 | marshal_state_init(&state, buf, available); |
| 721 | minijail_marshal_helper(&state, j); |
| 722 | return (state.total > available); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 723 | } |
| 724 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 725 | /* |
| 726 | * consumebytes: consumes @length bytes from a buffer @buf of length @buflength |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 727 | * @length Number of bytes to consume |
| 728 | * @buf Buffer to consume from |
| 729 | * @buflength Size of @buf |
| 730 | * |
| 731 | * Returns a pointer to the base of the bytes, or NULL for errors. |
| 732 | */ |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 733 | void *consumebytes(size_t length, char **buf, size_t *buflength) |
| 734 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 735 | char *p = *buf; |
| 736 | if (length > *buflength) |
| 737 | return NULL; |
| 738 | *buf += length; |
| 739 | *buflength -= length; |
| 740 | return p; |
| 741 | } |
| 742 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 743 | /* |
| 744 | * consumestr: consumes a C string from a buffer @buf of length @length |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 745 | * @buf Buffer to consume |
| 746 | * @length Length of buffer |
| 747 | * |
| 748 | * Returns a pointer to the base of the string, or NULL for errors. |
| 749 | */ |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 750 | char *consumestr(char **buf, size_t *buflength) |
| 751 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 752 | size_t len = strnlen(*buf, *buflength); |
| 753 | if (len == *buflength) |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 754 | /* There's no null-terminator. */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 755 | return NULL; |
| 756 | return consumebytes(len + 1, buf, buflength); |
| 757 | } |
| 758 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 759 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 760 | { |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 761 | size_t i; |
| 762 | size_t count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 763 | int ret = -EINVAL; |
| 764 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 765 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 766 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 767 | memcpy((void *)j, serialized, sizeof(*j)); |
| 768 | serialized += sizeof(*j); |
| 769 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 770 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 771 | /* Potentially stale pointers not used as signals. */ |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 772 | j->mounts_head = NULL; |
| 773 | j->mounts_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 774 | j->filter_prog = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 775 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 776 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 777 | char *user = consumestr(&serialized, &length); |
| 778 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 779 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 780 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 781 | if (!j->user) |
| 782 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 783 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 784 | |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 785 | if (j->suppl_gid_list) { /* stale pointer */ |
| 786 | if (j->suppl_gid_count > NGROUPS_MAX) { |
| 787 | goto bad_gid_list; |
| 788 | } |
| 789 | size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t); |
| 790 | void *gid_list_bytes = |
| 791 | consumebytes(gid_list_size, &serialized, &length); |
| 792 | if (!gid_list_bytes) |
| 793 | goto bad_gid_list; |
| 794 | |
| 795 | j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t)); |
| 796 | if (!j->suppl_gid_list) |
| 797 | goto bad_gid_list; |
| 798 | |
| 799 | memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size); |
| 800 | } |
| 801 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 802 | if (j->chrootdir) { /* stale pointer */ |
| 803 | char *chrootdir = consumestr(&serialized, &length); |
| 804 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 805 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 806 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 807 | if (!j->chrootdir) |
| 808 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 809 | } |
| 810 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 811 | if (j->alt_syscall_table) { /* stale pointer */ |
| 812 | char *alt_syscall_table = consumestr(&serialized, &length); |
| 813 | if (!alt_syscall_table) |
| 814 | goto bad_syscall_table; |
| 815 | j->alt_syscall_table = strdup(alt_syscall_table); |
| 816 | if (!j->alt_syscall_table) |
| 817 | goto bad_syscall_table; |
| 818 | } |
| 819 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 820 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 821 | size_t ninstrs = j->filter_len; |
| 822 | if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) || |
| 823 | ninstrs > USHRT_MAX) |
| 824 | goto bad_filters; |
| 825 | |
| 826 | size_t program_len = ninstrs * sizeof(struct sock_filter); |
| 827 | void *program = consumebytes(program_len, &serialized, &length); |
| 828 | if (!program) |
| 829 | goto bad_filters; |
| 830 | |
| 831 | j->filter_prog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 832 | if (!j->filter_prog) |
| 833 | goto bad_filters; |
| 834 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 835 | j->filter_prog->len = ninstrs; |
| 836 | j->filter_prog->filter = malloc(program_len); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 837 | if (!j->filter_prog->filter) |
| 838 | goto bad_filter_prog_instrs; |
| 839 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 840 | memcpy(j->filter_prog->filter, program, program_len); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 841 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 842 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 843 | count = j->mounts_count; |
| 844 | j->mounts_count = 0; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 845 | for (i = 0; i < count; ++i) { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 846 | unsigned long *flags; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 847 | const char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 848 | const char *type; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 849 | const char *src = consumestr(&serialized, &length); |
| 850 | if (!src) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 851 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 852 | dest = consumestr(&serialized, &length); |
| 853 | if (!dest) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 854 | goto bad_mounts; |
| 855 | type = consumestr(&serialized, &length); |
| 856 | if (!type) |
| 857 | goto bad_mounts; |
| 858 | flags = consumebytes(sizeof(*flags), &serialized, &length); |
| 859 | if (!flags) |
| 860 | goto bad_mounts; |
| 861 | if (minijail_mount(j, src, dest, type, *flags)) |
| 862 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 863 | } |
| 864 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 865 | count = j->cgroup_count; |
| 866 | j->cgroup_count = 0; |
| 867 | for (i = 0; i < count; ++i) { |
| 868 | char *cgroup = consumestr(&serialized, &length); |
| 869 | if (!cgroup) |
| 870 | goto bad_cgroups; |
| 871 | j->cgroups[i] = strdup(cgroup); |
| 872 | if (!j->cgroups[i]) |
| 873 | goto bad_cgroups; |
| 874 | ++j->cgroup_count; |
| 875 | } |
| 876 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 877 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 878 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 879 | bad_cgroups: |
| 880 | while (j->mounts_head) { |
| 881 | struct mountpoint *m = j->mounts_head; |
| 882 | j->mounts_head = j->mounts_head->next; |
| 883 | free(m->type); |
| 884 | free(m->dest); |
| 885 | free(m->src); |
| 886 | free(m); |
| 887 | } |
| 888 | for (i = 0; i < j->cgroup_count; ++i) |
| 889 | free(j->cgroups[i]); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 890 | bad_mounts: |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 891 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 892 | free(j->filter_prog->filter); |
| 893 | free(j->filter_prog); |
| 894 | } |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 895 | bad_filter_prog_instrs: |
| 896 | if (j->filter_prog) |
| 897 | free(j->filter_prog); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 898 | bad_filters: |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 899 | if (j->alt_syscall_table) |
| 900 | free(j->alt_syscall_table); |
| 901 | bad_syscall_table: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 902 | if (j->chrootdir) |
| 903 | free(j->chrootdir); |
| 904 | bad_chrootdir: |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 905 | if (j->suppl_gid_list) |
| 906 | free(j->suppl_gid_list); |
| 907 | bad_gid_list: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 908 | if (j->user) |
| 909 | free(j->user); |
| 910 | clear_pointers: |
| 911 | j->user = NULL; |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 912 | j->suppl_gid_list = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 913 | j->chrootdir = NULL; |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 914 | j->alt_syscall_table = NULL; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 915 | j->cgroup_count = 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 916 | out: |
| 917 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 918 | } |
| 919 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 920 | static void write_ugid_mappings(const struct minijail *j) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 921 | { |
| 922 | int fd, ret, len; |
| 923 | size_t sz; |
| 924 | char fname[32]; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 925 | |
| 926 | sz = sizeof(fname); |
| 927 | if (j->uidmap) { |
| 928 | ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid); |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 929 | if (ret < 0 || (size_t)ret >= sz) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 930 | die("failed to write file name of uid_map"); |
| 931 | fd = open(fname, O_WRONLY); |
| 932 | if (fd < 0) |
| 933 | pdie("failed to open '%s'", fname); |
| 934 | len = strlen(j->uidmap); |
| 935 | if (write(fd, j->uidmap, len) < len) |
| 936 | die("failed to set uid_map"); |
| 937 | close(fd); |
| 938 | } |
| 939 | if (j->gidmap) { |
| 940 | ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid); |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 941 | if (ret < 0 || (size_t)ret >= sz) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 942 | die("failed to write file name of gid_map"); |
| 943 | fd = open(fname, O_WRONLY); |
| 944 | if (fd < 0) |
| 945 | pdie("failed to open '%s'", fname); |
| 946 | len = strlen(j->gidmap); |
| 947 | if (write(fd, j->gidmap, len) < len) |
| 948 | die("failed to set gid_map"); |
| 949 | close(fd); |
| 950 | } |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 951 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 952 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 953 | static void parent_setup_complete(int *pipe_fds) |
| 954 | { |
| 955 | close(pipe_fds[0]); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 956 | close(pipe_fds[1]); |
| 957 | } |
| 958 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 959 | /* |
| 960 | * wait_for_parent_setup: Called by the child process to wait for any |
| 961 | * further parent-side setup to complete before continuing. |
| 962 | */ |
| 963 | static void wait_for_parent_setup(int *pipe_fds) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 964 | { |
| 965 | char buf; |
| 966 | |
| 967 | close(pipe_fds[1]); |
| 968 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 969 | /* Wait for parent to complete setup and close the pipe. */ |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 970 | if (read(pipe_fds[0], &buf, 1) != 0) |
| 971 | die("failed to sync with parent"); |
| 972 | close(pipe_fds[0]); |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 973 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 974 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 975 | static void enter_user_namespace(const struct minijail *j) |
| 976 | { |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 977 | if (j->uidmap && setresuid(0, 0, 0)) |
| 978 | pdie("setresuid"); |
| 979 | if (j->gidmap && setresgid(0, 0, 0)) |
| 980 | pdie("setresgid"); |
| 981 | } |
| 982 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 983 | /* |
| 984 | * mount_one: Applies mounts from @m for @j, recursing as needed. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 985 | * @j Minijail these mounts are for |
| 986 | * @m Head of list of mounts |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 987 | * |
| 988 | * Returns 0 for success. |
| 989 | */ |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 990 | static int mount_one(const struct minijail *j, struct mountpoint *m) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 991 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 992 | int ret; |
| 993 | char *dest; |
| 994 | int remount_ro = 0; |
| 995 | |
Jorge Lucangeli Obes | 2b12ba4 | 2016-01-26 10:37:51 -0800 | [diff] [blame^] | 996 | /* |dest| has a leading "/". */ |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 997 | if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 998 | return -ENOMEM; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 999 | |
| 1000 | /* |
Jorge Lucangeli Obes | 2b12ba4 | 2016-01-26 10:37:51 -0800 | [diff] [blame^] | 1001 | * R/O bind mounts have to be remounted since 'bind' and 'ro' |
| 1002 | * can't both be specified in the original bind mount. |
| 1003 | * Remount R/O after the initial mount. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1004 | */ |
| 1005 | if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) { |
| 1006 | remount_ro = 1; |
| 1007 | m->flags &= ~MS_RDONLY; |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 1008 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1009 | |
| 1010 | ret = mount(m->src, dest, m->type, m->flags, NULL); |
| 1011 | if (ret) |
| 1012 | pdie("mount: %s -> %s", m->src, dest); |
| 1013 | |
| 1014 | if (remount_ro) { |
| 1015 | m->flags |= MS_RDONLY; |
| 1016 | ret = mount(m->src, dest, NULL, |
| 1017 | m->flags | MS_REMOUNT, NULL); |
| 1018 | if (ret) |
| 1019 | pdie("bind ro: %s -> %s", m->src, dest); |
| 1020 | } |
| 1021 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1022 | free(dest); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1023 | if (m->next) |
| 1024 | return mount_one(j, m->next); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1025 | return ret; |
| 1026 | } |
| 1027 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 1028 | int enter_chroot(const struct minijail *j) |
| 1029 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1030 | int ret; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1031 | |
| 1032 | if (j->mounts_head && (ret = mount_one(j, j->mounts_head))) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1033 | return ret; |
| 1034 | |
| 1035 | if (chroot(j->chrootdir)) |
| 1036 | return -errno; |
| 1037 | |
| 1038 | if (chdir("/")) |
| 1039 | return -errno; |
| 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1044 | int enter_pivot_root(const struct minijail *j) |
| 1045 | { |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1046 | int ret, oldroot, newroot; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1047 | |
| 1048 | if (j->mounts_head && (ret = mount_one(j, j->mounts_head))) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1049 | return ret; |
| 1050 | |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1051 | /* |
| 1052 | * Keep the fd for both old and new root. |
| 1053 | * It will be used in fchdir later. |
| 1054 | */ |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1055 | oldroot = open("/", O_DIRECTORY | O_RDONLY); |
| 1056 | if (oldroot < 0) |
| 1057 | pdie("failed to open / for fchdir"); |
| 1058 | newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY); |
| 1059 | if (newroot < 0) |
| 1060 | pdie("failed to open %s for fchdir", j->chrootdir); |
| 1061 | |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1062 | /* |
| 1063 | * To ensure chrootdir is the root of a file system, |
| 1064 | * do a self bind mount. |
| 1065 | */ |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1066 | if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, "")) |
| 1067 | pdie("failed to bind mount '%s'", j->chrootdir); |
| 1068 | if (chdir(j->chrootdir)) |
| 1069 | return -errno; |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1070 | if (syscall(SYS_pivot_root, ".", ".")) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1071 | pdie("pivot_root"); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1072 | |
| 1073 | /* |
| 1074 | * Now the old root is mounted on top of the new root. Use fchdir to |
| 1075 | * change to the old root and unmount it. |
| 1076 | */ |
| 1077 | if (fchdir(oldroot)) |
| 1078 | pdie("failed to fchdir to old /"); |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1079 | /* The old root might be busy, so use lazy unmount. */ |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1080 | if (umount2(".", MNT_DETACH)) |
| 1081 | pdie("umount(/)"); |
| 1082 | /* Change back to the new root. */ |
| 1083 | if (fchdir(newroot)) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1084 | return -errno; |
| 1085 | if (chroot("/")) |
| 1086 | return -errno; |
Jorge Lucangeli Obes | 46a5509 | 2015-10-12 15:31:59 -0700 | [diff] [blame] | 1087 | /* Set correct CWD for getcwd(3). */ |
| 1088 | if (chdir("/")) |
| 1089 | return -errno; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1094 | int mount_tmp(void) |
| 1095 | { |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 1096 | return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777"); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 1099 | int remount_proc_readonly(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1100 | { |
| 1101 | const char *kProcPath = "/proc"; |
| 1102 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1103 | /* |
| 1104 | * 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] | 1105 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 1106 | * mutate our parent's mount as well, even though we're in a VFS |
| 1107 | * namespace (!). Instead, remove their mount from our namespace |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1108 | * and make our own. However, if we are in a new user namespace, /proc |
| 1109 | * 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] | 1110 | */ |
Jorge Lucangeli Obes | 805be39 | 2015-10-12 15:55:59 -0700 | [diff] [blame] | 1111 | if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1112 | return -errno; |
| 1113 | if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
| 1114 | return -errno; |
| 1115 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1116 | } |
| 1117 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1118 | static void write_pid_to_path(pid_t pid, const char *path) |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1119 | { |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1120 | FILE *fp = fopen(path, "w"); |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1121 | |
| 1122 | if (!fp) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1123 | pdie("failed to open '%s'", path); |
| 1124 | if (fprintf(fp, "%d\n", (int)pid) < 0) |
| 1125 | pdie("fprintf(%s)", path); |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1126 | if (fclose(fp)) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1127 | pdie("fclose(%s)", path); |
| 1128 | } |
| 1129 | |
| 1130 | static void write_pid_file(const struct minijail *j) |
| 1131 | { |
| 1132 | write_pid_to_path(j->initpid, j->pid_file_path); |
| 1133 | } |
| 1134 | |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 1135 | static void add_to_cgroups(const struct minijail *j) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1136 | { |
| 1137 | size_t i; |
| 1138 | |
| 1139 | for (i = 0; i < j->cgroup_count; ++i) |
| 1140 | write_pid_to_path(j->initpid, j->cgroups[i]); |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1143 | void drop_ugid(const struct minijail *j) |
| 1144 | { |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1145 | if (j->flags.usergroups && j->flags.suppl_gids) { |
| 1146 | die("tried to inherit *and* set supplementary groups;" |
| 1147 | " can only do one"); |
| 1148 | } |
| 1149 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1150 | if (j->flags.usergroups) { |
| 1151 | if (initgroups(j->user, j->usergid)) |
| 1152 | pdie("initgroups"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1153 | } else if (j->flags.suppl_gids) { |
| 1154 | if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) { |
| 1155 | pdie("setgroups"); |
| 1156 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1157 | } else { |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1158 | /* |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1159 | * Only attempt to clear supplementary groups if we are changing |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1160 | * users. |
| 1161 | */ |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1162 | if ((j->uid || j->gid) && setgroups(0, NULL)) |
| 1163 | pdie("setgroups"); |
| 1164 | } |
| 1165 | |
| 1166 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
| 1167 | pdie("setresgid"); |
| 1168 | |
| 1169 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
| 1170 | pdie("setresuid"); |
| 1171 | } |
| 1172 | |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1173 | /* |
| 1174 | * We specifically do not use cap_valid() as that only tells us the last |
| 1175 | * valid cap we were *compiled* against (i.e. what the version of kernel |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 1176 | * headers says). If we run on a different kernel version, then it's not |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1177 | * uncommon for that to be less (if an older kernel) or more (if a newer |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 1178 | * kernel). |
| 1179 | * Normally, we suck up the answer via /proc. On Android, not all processes are |
| 1180 | * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we |
| 1181 | * programmatically find the value by calling prctl(PR_CAPBSET_READ). |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1182 | */ |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 1183 | static unsigned int get_last_valid_cap() |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1184 | { |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 1185 | unsigned int last_valid_cap = 0; |
| 1186 | if (is_android()) { |
| 1187 | for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0; |
| 1188 | ++last_valid_cap); |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1189 | |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 1190 | /* |last_valid_cap| will be the first failing value. */ |
| 1191 | if (last_valid_cap > 0) { |
| 1192 | last_valid_cap--; |
| 1193 | } |
| 1194 | } else { |
| 1195 | const char cap_file[] = "/proc/sys/kernel/cap_last_cap"; |
| 1196 | FILE *fp = fopen(cap_file, "re"); |
| 1197 | if (fscanf(fp, "%u", &last_valid_cap) != 1) |
| 1198 | pdie("fscanf(%s)", cap_file); |
| 1199 | fclose(fp); |
| 1200 | } |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1201 | return last_valid_cap; |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1202 | } |
| 1203 | |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 1204 | void drop_caps(const struct minijail *j, unsigned int last_valid_cap) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1205 | { |
| 1206 | cap_t caps = cap_get_proc(); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1207 | cap_value_t flag[1]; |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1208 | const uint64_t one = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1209 | unsigned int i; |
| 1210 | if (!caps) |
| 1211 | die("can't get process caps"); |
| 1212 | if (cap_clear_flag(caps, CAP_INHERITABLE)) |
| 1213 | die("can't clear inheritable caps"); |
| 1214 | if (cap_clear_flag(caps, CAP_EFFECTIVE)) |
| 1215 | die("can't clear effective caps"); |
| 1216 | if (cap_clear_flag(caps, CAP_PERMITTED)) |
| 1217 | die("can't clear permitted caps"); |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1218 | for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1219 | /* Keep CAP_SETPCAP for dropping bounding set bits. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1220 | if (i != CAP_SETPCAP && !(j->caps & (one << i))) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1221 | continue; |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1222 | flag[0] = i; |
| 1223 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1224 | die("can't add effective cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1225 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1226 | die("can't add permitted cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1227 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1228 | die("can't add inheritable cap"); |
| 1229 | } |
| 1230 | if (cap_set_proc(caps)) |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1231 | die("can't apply initial cleaned capset"); |
| 1232 | |
| 1233 | /* |
| 1234 | * Instead of dropping bounding set first, do it here in case |
| 1235 | * the caller had a more permissive bounding set which could |
| 1236 | * have been used above to raise a capability that wasn't already |
| 1237 | * present. This requires CAP_SETPCAP, so we raised/kept it above. |
| 1238 | */ |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1239 | for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) { |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1240 | if (j->caps & (one << i)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1241 | continue; |
| 1242 | if (prctl(PR_CAPBSET_DROP, i)) |
| 1243 | pdie("prctl(PR_CAPBSET_DROP)"); |
| 1244 | } |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1245 | |
| 1246 | /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1247 | if ((j->caps & (one << CAP_SETPCAP)) == 0) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1248 | flag[0] = CAP_SETPCAP; |
| 1249 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR)) |
| 1250 | die("can't clear effective cap"); |
| 1251 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR)) |
| 1252 | die("can't clear permitted cap"); |
| 1253 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR)) |
| 1254 | die("can't clear inheritable cap"); |
| 1255 | } |
| 1256 | |
| 1257 | if (cap_set_proc(caps)) |
| 1258 | die("can't apply final cleaned capset"); |
| 1259 | |
| 1260 | cap_free(caps); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1261 | } |
| 1262 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1263 | void set_seccomp_filter(const struct minijail *j) |
| 1264 | { |
| 1265 | /* |
| 1266 | * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c> |
| 1267 | * in the kernel source tree for an explanation of the parameters. |
| 1268 | */ |
| 1269 | if (j->flags.no_new_privs) { |
| 1270 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 1271 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 1272 | } |
| 1273 | |
| 1274 | /* |
| 1275 | * If we're logging seccomp filter failures, |
| 1276 | * install the SIGSYS handler first. |
| 1277 | */ |
| 1278 | if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) { |
| 1279 | if (install_sigsys_handler()) |
| 1280 | pdie("install SIGSYS handler"); |
| 1281 | warn("logging seccomp filter failures"); |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Install the syscall filter. |
| 1286 | */ |
| 1287 | if (j->flags.seccomp_filter) { |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1288 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, |
| 1289 | j->filter_prog)) { |
Jeff Vander Stoep | 2885bef | 2016-01-11 15:22:42 -0800 | [diff] [blame] | 1290 | if ((errno == EINVAL) && can_softfail()) { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1291 | warn("seccomp not supported"); |
| 1292 | return; |
| 1293 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1294 | pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1295 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1299 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1300 | { |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1301 | /* |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1302 | * If we're dropping caps, get the last valid cap from /proc now, |
| 1303 | * since /proc can be unmounted before drop_caps() is called. |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1304 | */ |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1305 | unsigned int last_valid_cap = 0; |
| 1306 | if (j->flags.caps) |
| 1307 | last_valid_cap = get_last_valid_cap(); |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1308 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1309 | if (j->flags.pids) |
| 1310 | die("tried to enter a pid-namespaced jail;" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1311 | " try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1312 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1313 | if (j->flags.usergroups && !j->user) |
| 1314 | die("usergroup inheritance without username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1315 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1316 | /* |
| 1317 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1318 | * so we don't even try. If any of our operations fail, we abort() the |
| 1319 | * entire process. |
| 1320 | */ |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1321 | if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS)) |
| 1322 | pdie("setns(CLONE_NEWNS)"); |
| 1323 | |
Jorge Lucangeli Obes | 805be39 | 2015-10-12 15:55:59 -0700 | [diff] [blame] | 1324 | if (j->flags.vfs) { |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1325 | if (unshare(CLONE_NEWNS)) |
| 1326 | pdie("unshare(vfs)"); |
| 1327 | /* |
| 1328 | * Remount all filesystems as private. If they are shared |
| 1329 | * new bind mounts will creep out of our namespace. |
| 1330 | * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt |
| 1331 | */ |
| 1332 | if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) |
| 1333 | pdie("mount(/, private)"); |
| 1334 | } |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 1335 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 1336 | if (j->flags.ipc && unshare(CLONE_NEWIPC)) { |
| 1337 | pdie("unshare(ipc)"); |
| 1338 | } |
| 1339 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 1340 | if (j->flags.enter_net) { |
| 1341 | if (setns(j->netns_fd, CLONE_NEWNET)) |
| 1342 | pdie("setns(CLONE_NEWNET)"); |
| 1343 | } else if (j->flags.net && unshare(CLONE_NEWNET)) { |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 1344 | pdie("unshare(net)"); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 1345 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1346 | |
Jorge Lucangeli Obes | 2b12ba4 | 2016-01-26 10:37:51 -0800 | [diff] [blame^] | 1347 | if (j->mounts_head && !(j->flags.chroot || j->flags.pivot_root)) |
| 1348 | die("can't bind-mount without chroot or pivot_root"); |
| 1349 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1350 | if (j->flags.chroot && enter_chroot(j)) |
| 1351 | pdie("chroot"); |
| 1352 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1353 | if (j->flags.pivot_root && enter_pivot_root(j)) |
| 1354 | pdie("pivot_root"); |
| 1355 | |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 1356 | if (j->flags.mount_tmp && mount_tmp()) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1357 | pdie("mount_tmp"); |
| 1358 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 1359 | if (j->flags.remount_proc_ro && remount_proc_readonly(j)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1360 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1361 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1362 | if (j->flags.caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1363 | /* |
| 1364 | * POSIX capabilities are a bit tricky. If we drop our |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1365 | * capability to change uids, our attempt to use setuid() |
| 1366 | * below will fail. Hang on to root caps across setuid(), then |
| 1367 | * lock securebits. |
| 1368 | */ |
| 1369 | if (prctl(PR_SET_KEEPCAPS, 1)) |
| 1370 | pdie("prctl(PR_SET_KEEPCAPS)"); |
| 1371 | if (prctl |
| 1372 | (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS)) |
| 1373 | pdie("prctl(PR_SET_SECUREBITS)"); |
| 1374 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1375 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1376 | /* |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1377 | * If we're setting no_new_privs, we can drop privileges |
| 1378 | * before setting seccomp filter. This way filter policies |
| 1379 | * don't need to allow privilege-dropping syscalls. |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1380 | */ |
| 1381 | if (j->flags.no_new_privs) { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1382 | drop_ugid(j); |
| 1383 | if (j->flags.caps) |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1384 | drop_caps(j, last_valid_cap); |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1385 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1386 | set_seccomp_filter(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1387 | } else { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1388 | /* |
| 1389 | * If we're not setting no_new_privs, |
| 1390 | * we need to set seccomp filter *before* dropping privileges. |
| 1391 | * WARNING: this means that filter policies *must* allow |
| 1392 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 1393 | * capget()/capset()/prctl() for dropping caps. |
| 1394 | */ |
| 1395 | set_seccomp_filter(j); |
| 1396 | |
| 1397 | drop_ugid(j); |
| 1398 | if (j->flags.caps) |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1399 | drop_caps(j, last_valid_cap); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1400 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1401 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1402 | /* |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1403 | * Select the specified alternate syscall table. The table must not |
| 1404 | * block prctl(2) if we're using seccomp as well. |
| 1405 | */ |
| 1406 | if (j->flags.alt_syscall) { |
| 1407 | if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table)) |
| 1408 | pdie("prctl(PR_ALT_SYSCALL)"); |
| 1409 | } |
| 1410 | |
| 1411 | /* |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1412 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1413 | * privilege-dropping syscalls :) |
| 1414 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1415 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) { |
Jeff Vander Stoep | 2885bef | 2016-01-11 15:22:42 -0800 | [diff] [blame] | 1416 | if ((errno == EINVAL) && can_softfail()) { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1417 | warn("seccomp not supported"); |
| 1418 | return; |
| 1419 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1420 | pdie("prctl(PR_SET_SECCOMP)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1421 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1422 | } |
| 1423 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1424 | /* TODO(wad) will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1425 | static int init_exitstatus = 0; |
| 1426 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1427 | void init_term(int __attribute__ ((unused)) sig) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1428 | { |
| 1429 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1430 | } |
| 1431 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1432 | int init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1433 | { |
| 1434 | pid_t pid; |
| 1435 | int status; |
| 1436 | /* so that we exit with the right status */ |
| 1437 | signal(SIGTERM, init_term); |
| 1438 | /* TODO(wad) self jail with seccomp_filters here. */ |
| 1439 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1440 | /* |
| 1441 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1442 | * left inside our pid namespace or we get a signal. |
| 1443 | */ |
| 1444 | if (pid == rootpid) |
| 1445 | init_exitstatus = status; |
| 1446 | } |
| 1447 | if (!WIFEXITED(init_exitstatus)) |
| 1448 | _exit(MINIJAIL_ERR_INIT); |
| 1449 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1450 | } |
| 1451 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1452 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1453 | { |
| 1454 | size_t sz = 0; |
| 1455 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 1456 | char *buf; |
| 1457 | int r; |
| 1458 | if (sizeof(sz) != bytes) |
| 1459 | return -EINVAL; |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1460 | if (sz > USHRT_MAX) /* arbitrary sanity check */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1461 | return -E2BIG; |
| 1462 | buf = malloc(sz); |
| 1463 | if (!buf) |
| 1464 | return -ENOMEM; |
| 1465 | bytes = read(fd, buf, sz); |
| 1466 | if (bytes != sz) { |
| 1467 | free(buf); |
| 1468 | return -EINVAL; |
| 1469 | } |
| 1470 | r = minijail_unmarshal(j, buf, sz); |
| 1471 | free(buf); |
| 1472 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1473 | } |
| 1474 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1475 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1476 | { |
| 1477 | char *buf; |
| 1478 | size_t sz = minijail_size(j); |
| 1479 | ssize_t written; |
| 1480 | int r; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1481 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1482 | if (!sz) |
| 1483 | return -EINVAL; |
| 1484 | buf = malloc(sz); |
| 1485 | r = minijail_marshal(j, buf, sz); |
| 1486 | if (r) { |
| 1487 | free(buf); |
| 1488 | return r; |
| 1489 | } |
| 1490 | /* Sends [size][minijail]. */ |
| 1491 | written = write(fd, &sz, sizeof(sz)); |
| 1492 | if (written != sizeof(sz)) { |
| 1493 | free(buf); |
| 1494 | return -EFAULT; |
| 1495 | } |
| 1496 | written = write(fd, buf, sz); |
| 1497 | if (written < 0 || (size_t) written != sz) { |
| 1498 | free(buf); |
| 1499 | return -EFAULT; |
| 1500 | } |
| 1501 | free(buf); |
| 1502 | return 0; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1503 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1504 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1505 | int setup_preload(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1506 | { |
Daniel Erat | 5b7a318 | 2015-08-19 16:06:22 -0600 | [diff] [blame] | 1507 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 1508 | /* Don't use LDPRELOAD on Brillo. */ |
| 1509 | return 0; |
| 1510 | #else |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1511 | char *oldenv = getenv(kLdPreloadEnvVar) ? : ""; |
| 1512 | char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH)); |
| 1513 | if (!newenv) |
| 1514 | return -ENOMEM; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1515 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1516 | /* Only insert a separating space if we have something to separate... */ |
| 1517 | sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "", |
| 1518 | PRELOADPATH); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1519 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1520 | /* setenv() makes a copy of the string we give it. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1521 | setenv(kLdPreloadEnvVar, newenv, 1); |
| 1522 | free(newenv); |
| 1523 | return 0; |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 1524 | #endif |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1525 | } |
| 1526 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1527 | int setup_pipe(int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1528 | { |
| 1529 | int r = pipe(fds); |
| 1530 | char fd_buf[11]; |
| 1531 | if (r) |
| 1532 | return r; |
| 1533 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 1534 | if (r <= 0) |
| 1535 | return -EINVAL; |
| 1536 | setenv(kFdEnvVar, fd_buf, 1); |
| 1537 | return 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1538 | } |
| 1539 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1540 | int setup_pipe_end(int fds[2], size_t index) |
| 1541 | { |
| 1542 | if (index > 1) |
| 1543 | return -1; |
| 1544 | |
| 1545 | close(fds[1 - index]); |
| 1546 | return fds[index]; |
| 1547 | } |
| 1548 | |
| 1549 | int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd) |
| 1550 | { |
| 1551 | if (index > 1) |
| 1552 | return -1; |
| 1553 | |
| 1554 | close(fds[1 - index]); |
| 1555 | /* dup2(2) the corresponding end of the pipe into |fd|. */ |
| 1556 | return dup2(fds[index], fd); |
| 1557 | } |
| 1558 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1559 | int minijail_run_internal(struct minijail *j, const char *filename, |
| 1560 | char *const argv[], pid_t *pchild_pid, |
| 1561 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd, |
| 1562 | int use_preload); |
| 1563 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1564 | int API minijail_run(struct minijail *j, const char *filename, |
| 1565 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1566 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1567 | return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL, |
| 1568 | true); |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 1572 | char *const argv[], pid_t *pchild_pid) |
| 1573 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1574 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1575 | NULL, NULL, NULL, true); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | int API minijail_run_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 1579 | char *const argv[], int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1580 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1581 | return minijail_run_internal(j, filename, argv, NULL, pstdin_fd, |
| 1582 | NULL, NULL, true); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1585 | 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] | 1586 | char *const argv[], pid_t *pchild_pid, |
| 1587 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1588 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1589 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1590 | pstdin_fd, pstdout_fd, pstderr_fd, true); |
| 1591 | } |
| 1592 | |
| 1593 | int API minijail_run_no_preload(struct minijail *j, const char *filename, |
| 1594 | char *const argv[]) |
| 1595 | { |
| 1596 | return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL, |
| 1597 | false); |
| 1598 | } |
| 1599 | |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 1600 | int API minijail_run_pid_pipes_no_preload(struct minijail *j, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1601 | const char *filename, |
| 1602 | char *const argv[], |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 1603 | pid_t *pchild_pid, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1604 | int *pstdin_fd, int *pstdout_fd, |
| 1605 | int *pstderr_fd) { |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 1606 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1607 | pstdin_fd, pstdout_fd, pstderr_fd, false); |
| 1608 | } |
| 1609 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1610 | int minijail_run_internal(struct minijail *j, const char *filename, |
| 1611 | char *const argv[], pid_t *pchild_pid, |
| 1612 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd, |
| 1613 | int use_preload) |
| 1614 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1615 | char *oldenv, *oldenv_copy = NULL; |
| 1616 | pid_t child_pid; |
| 1617 | int pipe_fds[2]; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1618 | int stdin_fds[2]; |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1619 | int stdout_fds[2]; |
| 1620 | int stderr_fds[2]; |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 1621 | int child_sync_pipe_fds[2]; |
| 1622 | int sync_child = 0; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1623 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1624 | /* We need to remember this across the minijail_preexec() call. */ |
| 1625 | int pid_namespace = j->flags.pids; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1626 | int do_init = j->flags.do_init; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1627 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1628 | if (use_preload) { |
| 1629 | oldenv = getenv(kLdPreloadEnvVar); |
| 1630 | if (oldenv) { |
| 1631 | oldenv_copy = strdup(oldenv); |
| 1632 | if (!oldenv_copy) |
| 1633 | return -ENOMEM; |
| 1634 | } |
| 1635 | |
| 1636 | if (setup_preload()) |
| 1637 | return -EFAULT; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1638 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1639 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1640 | if (!use_preload) { |
| 1641 | if (j->flags.caps) |
Jorge Lucangeli Obes | 2b12ba4 | 2016-01-26 10:37:51 -0800 | [diff] [blame^] | 1642 | die("capabilities are not supported without " |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1643 | "LD_PRELOAD"); |
| 1644 | } |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1645 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1646 | /* |
Jorge Lucangeli Obes | 3c84df1 | 2015-05-14 17:37:58 -0700 | [diff] [blame] | 1647 | * Make the process group ID of this process equal to its PID, so that |
| 1648 | * both the Minijail process and the jailed process can be killed |
| 1649 | * together. |
| 1650 | * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when |
| 1651 | * the process is already a process group leader. |
| 1652 | */ |
| 1653 | if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) { |
| 1654 | if (errno != EPERM) { |
| 1655 | pdie("setpgid(0, 0)"); |
| 1656 | } |
| 1657 | } |
| 1658 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1659 | if (use_preload) { |
| 1660 | /* |
| 1661 | * Before we fork(2) and execve(2) the child process, we need |
| 1662 | * to open a pipe(2) to send the minijail configuration over. |
| 1663 | */ |
| 1664 | if (setup_pipe(pipe_fds)) |
| 1665 | return -EFAULT; |
| 1666 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1667 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1668 | /* |
| 1669 | * If we want to write to the child process' standard input, |
| 1670 | * create the pipe(2) now. |
| 1671 | */ |
| 1672 | if (pstdin_fd) { |
| 1673 | if (pipe(stdin_fds)) |
| 1674 | return -EFAULT; |
| 1675 | } |
| 1676 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1677 | /* |
| 1678 | * If we want to read from the child process' standard output, |
| 1679 | * create the pipe(2) now. |
| 1680 | */ |
| 1681 | if (pstdout_fd) { |
| 1682 | if (pipe(stdout_fds)) |
| 1683 | return -EFAULT; |
| 1684 | } |
| 1685 | |
| 1686 | /* |
| 1687 | * If we want to read from the child process' standard error, |
| 1688 | * create the pipe(2) now. |
| 1689 | */ |
| 1690 | if (pstderr_fd) { |
| 1691 | if (pipe(stderr_fds)) |
| 1692 | return -EFAULT; |
| 1693 | } |
| 1694 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1695 | /* |
| 1696 | * If we want to set up a new uid/gid mapping in the user namespace, |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 1697 | * or if we need to add the child process to cgroups, create the pipe(2) |
| 1698 | * to sync between parent and child. |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1699 | */ |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 1700 | if (j->flags.userns || j->flags.cgroups) { |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 1701 | sync_child = 1; |
| 1702 | if (pipe(child_sync_pipe_fds)) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1703 | return -EFAULT; |
| 1704 | } |
| 1705 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1706 | /* |
| 1707 | * Use sys_clone() if and only if we're creating a pid namespace. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1708 | * |
| 1709 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 1710 | * |
| 1711 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 1712 | * some of which may be held by other threads at the time that we call |
| 1713 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 1714 | * ensure that we hold all of these locks before it calls clone() |
| 1715 | * internally and drop them after clone() returns, but when we call |
| 1716 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 1717 | * child address space where some of libc's important locks are held by |
| 1718 | * other threads (which did not get cloned, and hence will never release |
| 1719 | * those locks). This is okay so long as we call exec() immediately |
| 1720 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 1721 | * take locks. |
| 1722 | * |
| 1723 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 1724 | * namespacing. If we follow this path, the child's address space might |
| 1725 | * have broken locks; you may only call functions that do not acquire |
| 1726 | * any locks. |
| 1727 | * |
| 1728 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 1729 | * previously detailed, so this function is highly likely to deadlock |
| 1730 | * later on (see "deadlock here") if we're multithreaded. |
| 1731 | * |
| 1732 | * We might hack around this by having the clone()d child (init of the |
| 1733 | * pid namespace) return directly, rather than leaving the clone()d |
| 1734 | * process hanging around to be init for the new namespace (and having |
| 1735 | * its fork()ed child return in turn), but that process would be crippled |
| 1736 | * with its libc locks potentially broken. We might try fork()ing in the |
| 1737 | * parent before we clone() to ensure that we own all the locks, but |
| 1738 | * then we have to have the forked child hanging around consuming |
| 1739 | * resources (and possibly having file descriptors / shared memory |
| 1740 | * regions / etc attached). We'd need to keep the child around to avoid |
| 1741 | * having its children get reparented to init. |
| 1742 | * |
| 1743 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 1744 | * problem is fixable or not. It would be nice if we worked in this |
| 1745 | * case. |
| 1746 | */ |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1747 | if (pid_namespace) { |
| 1748 | int clone_flags = CLONE_NEWPID | SIGCHLD; |
| 1749 | if (j->flags.userns) |
| 1750 | clone_flags |= CLONE_NEWUSER; |
| 1751 | child_pid = syscall(SYS_clone, clone_flags, NULL); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1752 | } else { |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1753 | child_pid = fork(); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1754 | } |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1755 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1756 | if (child_pid < 0) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1757 | if (use_preload) { |
| 1758 | free(oldenv_copy); |
| 1759 | } |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1760 | die("failed to fork child"); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1761 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1762 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1763 | if (child_pid) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1764 | if (use_preload) { |
| 1765 | /* Restore parent's LD_PRELOAD. */ |
| 1766 | if (oldenv_copy) { |
| 1767 | setenv(kLdPreloadEnvVar, oldenv_copy, 1); |
| 1768 | free(oldenv_copy); |
| 1769 | } else { |
| 1770 | unsetenv(kLdPreloadEnvVar); |
| 1771 | } |
| 1772 | unsetenv(kFdEnvVar); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1773 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1774 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1775 | j->initpid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1776 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1777 | if (j->flags.pid_file) |
| 1778 | write_pid_file(j); |
| 1779 | |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 1780 | if (j->flags.cgroups) |
| 1781 | add_to_cgroups(j); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1782 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1783 | if (j->flags.userns) |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 1784 | write_ugid_mappings(j); |
| 1785 | |
| 1786 | if (sync_child) |
| 1787 | parent_setup_complete(child_sync_pipe_fds); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1788 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1789 | if (use_preload) { |
| 1790 | /* Send marshalled minijail. */ |
| 1791 | close(pipe_fds[0]); /* read endpoint */ |
| 1792 | ret = minijail_to_fd(j, pipe_fds[1]); |
| 1793 | close(pipe_fds[1]); /* write endpoint */ |
| 1794 | if (ret) { |
| 1795 | kill(j->initpid, SIGKILL); |
| 1796 | die("failed to send marshalled minijail"); |
| 1797 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1798 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1799 | |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1800 | if (pchild_pid) |
| 1801 | *pchild_pid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1802 | |
| 1803 | /* |
| 1804 | * If we want to write to the child process' standard input, |
| 1805 | * set up the write end of the pipe. |
| 1806 | */ |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1807 | if (pstdin_fd) |
| 1808 | *pstdin_fd = setup_pipe_end(stdin_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1809 | 1 /* write end */); |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1810 | |
| 1811 | /* |
| 1812 | * If we want to read from the child process' standard output, |
| 1813 | * set up the read end of the pipe. |
| 1814 | */ |
| 1815 | if (pstdout_fd) |
| 1816 | *pstdout_fd = setup_pipe_end(stdout_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1817 | 0 /* read end */); |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1818 | |
| 1819 | /* |
| 1820 | * If we want to read from the child process' standard error, |
| 1821 | * set up the read end of the pipe. |
| 1822 | */ |
| 1823 | if (pstderr_fd) |
| 1824 | *pstderr_fd = setup_pipe_end(stderr_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1825 | 0 /* read end */); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1826 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1827 | return 0; |
| 1828 | } |
| 1829 | free(oldenv_copy); |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1830 | |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 1831 | if (j->flags.reset_signal_mask) { |
| 1832 | sigset_t signal_mask; |
| 1833 | if (sigemptyset(&signal_mask) != 0) |
| 1834 | pdie("sigemptyset failed"); |
| 1835 | if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0) |
| 1836 | pdie("sigprocmask failed"); |
| 1837 | } |
| 1838 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 1839 | if (sync_child) |
| 1840 | wait_for_parent_setup(child_sync_pipe_fds); |
| 1841 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1842 | if (j->flags.userns) |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 1843 | enter_user_namespace(j); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1844 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1845 | /* |
| 1846 | * If we want to write to the jailed process' standard input, |
| 1847 | * set up the read end of the pipe. |
| 1848 | */ |
| 1849 | if (pstdin_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1850 | if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */, |
| 1851 | STDIN_FILENO) < 0) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1852 | die("failed to set up stdin pipe"); |
| 1853 | } |
| 1854 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1855 | /* |
| 1856 | * If we want to read from the jailed process' standard output, |
| 1857 | * set up the write end of the pipe. |
| 1858 | */ |
| 1859 | if (pstdout_fd) { |
| 1860 | if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */, |
| 1861 | STDOUT_FILENO) < 0) |
| 1862 | die("failed to set up stdout pipe"); |
| 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * If we want to read from the jailed process' standard error, |
| 1867 | * set up the write end of the pipe. |
| 1868 | */ |
| 1869 | if (pstderr_fd) { |
| 1870 | if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */, |
| 1871 | STDERR_FILENO) < 0) |
| 1872 | die("failed to set up stderr pipe"); |
| 1873 | } |
| 1874 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 1875 | /* If running an init program, let it decide when/how to mount /proc. */ |
| 1876 | if (pid_namespace && !do_init) |
| 1877 | j->flags.remount_proc_ro = 0; |
| 1878 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1879 | if (use_preload) { |
| 1880 | /* Strip out flags that cannot be inherited across execve(2). */ |
| 1881 | minijail_preexec(j); |
| 1882 | } else { |
| 1883 | j->flags.pids = 0; |
| 1884 | } |
| 1885 | /* Jail this process, then execve() the target. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1886 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1887 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1888 | if (pid_namespace && do_init) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1889 | /* |
| 1890 | * pid namespace: this process will become init inside the new |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1891 | * namespace. We don't want all programs we might exec to have |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1892 | * to know how to be init. Normally (do_init == 1) we fork off |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1893 | * a child to actually run the program. If |do_init == 0|, we |
| 1894 | * let the program keep pid 1 and be init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1895 | * |
| 1896 | * If we're multithreaded, we'll probably deadlock here. See |
| 1897 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1898 | */ |
| 1899 | child_pid = fork(); |
| 1900 | if (child_pid < 0) |
| 1901 | _exit(child_pid); |
| 1902 | else if (child_pid > 0) |
| 1903 | init(child_pid); /* never returns */ |
| 1904 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1905 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1906 | /* |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1907 | * If we aren't pid-namespaced, or the jailed program asked to be init: |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1908 | * calling process |
| 1909 | * -> execve()-ing process |
| 1910 | * If we are: |
| 1911 | * calling process |
| 1912 | * -> init()-ing process |
| 1913 | * -> execve()-ing process |
| 1914 | */ |
| 1915 | _exit(execve(filename, argv, environ)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1916 | } |
| 1917 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1918 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1919 | { |
| 1920 | int st; |
| 1921 | if (kill(j->initpid, SIGTERM)) |
| 1922 | return -errno; |
| 1923 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1924 | return -errno; |
| 1925 | return st; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1926 | } |
| 1927 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1928 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1929 | { |
| 1930 | int st; |
| 1931 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1932 | return -errno; |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1933 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1934 | if (!WIFEXITED(st)) { |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1935 | int error_status = st; |
| 1936 | if (WIFSIGNALED(st)) { |
| 1937 | int signum = WTERMSIG(st); |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1938 | warn("child process %d received signal %d", |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1939 | j->initpid, signum); |
| 1940 | /* |
| 1941 | * We return MINIJAIL_ERR_JAIL if the process received |
| 1942 | * SIGSYS, which happens when a syscall is blocked by |
| 1943 | * seccomp filters. |
| 1944 | * If not, we do what bash(1) does: |
| 1945 | * $? = 128 + signum |
| 1946 | */ |
| 1947 | if (signum == SIGSYS) { |
| 1948 | error_status = MINIJAIL_ERR_JAIL; |
| 1949 | } else { |
| 1950 | error_status = 128 + signum; |
| 1951 | } |
| 1952 | } |
| 1953 | return error_status; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1954 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1955 | |
| 1956 | int exit_status = WEXITSTATUS(st); |
| 1957 | if (exit_status != 0) |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1958 | info("child process %d exited with status %d", |
| 1959 | j->initpid, exit_status); |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1960 | |
| 1961 | return exit_status; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1962 | } |
| 1963 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1964 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1965 | { |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1966 | size_t i; |
| 1967 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1968 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 1969 | free(j->filter_prog->filter); |
| 1970 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1971 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1972 | while (j->mounts_head) { |
| 1973 | struct mountpoint *m = j->mounts_head; |
| 1974 | j->mounts_head = j->mounts_head->next; |
| 1975 | free(m->type); |
| 1976 | free(m->dest); |
| 1977 | free(m->src); |
| 1978 | free(m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1979 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1980 | j->mounts_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1981 | if (j->user) |
| 1982 | free(j->user); |
Jorge Lucangeli Obes | e81a52f | 2015-12-04 16:05:23 -0800 | [diff] [blame] | 1983 | if (j->suppl_gid_list) |
| 1984 | free(j->suppl_gid_list); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1985 | if (j->chrootdir) |
| 1986 | free(j->chrootdir); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1987 | if (j->alt_syscall_table) |
| 1988 | free(j->alt_syscall_table); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1989 | for (i = 0; i < j->cgroup_count; ++i) |
| 1990 | free(j->cgroups[i]); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1991 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1992 | } |