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