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 |
Arthur Gautier | 7a56907 | 2016-04-23 17:25:20 +0000 | [diff] [blame] | 7 | #define _DEFAULT_SOURCE |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 8 | #define _GNU_SOURCE |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 9 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 10 | #include <asm/unistd.h> |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 11 | #include <dirent.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 12 | #include <errno.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 13 | #include <fcntl.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 14 | #include <grp.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 15 | #include <linux/capability.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 16 | #include <sched.h> |
| 17 | #include <signal.h> |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 18 | #include <stdbool.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 19 | #include <stddef.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 23 | #include <sys/capability.h> |
| 24 | #include <sys/mount.h> |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 25 | #include <sys/param.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 26 | #include <sys/prctl.h> |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 27 | #include <sys/resource.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 29 | #include <sys/sysmacros.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 30 | #include <sys/types.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 31 | #include <sys/user.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 32 | #include <sys/wait.h> |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 33 | #include <syscall.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
| 36 | #include "libminijail.h" |
| 37 | #include "libminijail-private.h" |
| 38 | |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 39 | #include "signal_handler.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 40 | #include "syscall_filter.h" |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 41 | #include "syscall_wrapper.h" |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 42 | #include "system.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 | |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 45 | /* Until these are reliably available in linux/prctl.h. */ |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 46 | #ifndef PR_ALT_SYSCALL |
| 47 | # define PR_ALT_SYSCALL 0x43724f53 |
| 48 | #endif |
| 49 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 50 | /* Seccomp filter related flags. */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 51 | #ifndef PR_SET_NO_NEW_PRIVS |
| 52 | # define PR_SET_NO_NEW_PRIVS 38 |
| 53 | #endif |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 54 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 55 | #ifndef SECCOMP_MODE_FILTER |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 56 | #define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */ |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 57 | #endif |
| 58 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 59 | #ifndef SECCOMP_SET_MODE_STRICT |
| 60 | # define SECCOMP_SET_MODE_STRICT 0 |
| 61 | #endif |
| 62 | #ifndef SECCOMP_SET_MODE_FILTER |
| 63 | # define SECCOMP_SET_MODE_FILTER 1 |
| 64 | #endif |
| 65 | |
| 66 | #ifndef SECCOMP_FILTER_FLAG_TSYNC |
| 67 | # define SECCOMP_FILTER_FLAG_TSYNC 1 |
| 68 | #endif |
| 69 | /* End seccomp filter related flags. */ |
| 70 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 71 | /* New cgroup namespace might not be in linux-headers yet. */ |
| 72 | #ifndef CLONE_NEWCGROUP |
| 73 | # define CLONE_NEWCGROUP 0x02000000 |
| 74 | #endif |
| 75 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 76 | #define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */ |
| 77 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 78 | #define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */ |
| 79 | |
Stephen Barber | 0d1cbf6 | 2017-10-16 22:19:38 -0700 | [diff] [blame] | 80 | #define MAX_PRESERVED_FDS 32U |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 81 | |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 82 | /* Keyctl commands. */ |
| 83 | #define KEYCTL_JOIN_SESSION_KEYRING 1 |
| 84 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 85 | struct minijail_rlimit { |
| 86 | int type; |
| 87 | uint32_t cur; |
| 88 | uint32_t max; |
| 89 | }; |
| 90 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 91 | struct mountpoint { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 92 | char *src; |
| 93 | char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 94 | char *type; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 95 | char *data; |
| 96 | int has_data; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 97 | unsigned long flags; |
| 98 | struct mountpoint *next; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 99 | }; |
| 100 | |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 101 | struct hook { |
| 102 | minijail_hook_t hook; |
| 103 | void *payload; |
| 104 | minijail_hook_event_t event; |
| 105 | struct hook *next; |
| 106 | }; |
| 107 | |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 108 | struct preserved_fd { |
| 109 | int parent_fd; |
| 110 | int child_fd; |
| 111 | }; |
| 112 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 113 | struct minijail { |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 114 | /* |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 115 | * WARNING: if you add a flag here you need to make sure it's |
| 116 | * accounted for in minijail_pre{enter|exec}() below. |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 117 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 118 | struct { |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 119 | int uid : 1; |
| 120 | int gid : 1; |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 121 | int inherit_suppl_gids : 1; |
| 122 | int set_suppl_gids : 1; |
| 123 | int keep_suppl_gids : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 124 | int use_caps : 1; |
| 125 | int capbset_drop : 1; |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 126 | int set_ambient_caps : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 127 | int vfs : 1; |
| 128 | int enter_vfs : 1; |
| 129 | int skip_remount_private : 1; |
| 130 | int pids : 1; |
| 131 | int ipc : 1; |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 132 | int uts : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 133 | int net : 1; |
| 134 | int enter_net : 1; |
| 135 | int ns_cgroups : 1; |
| 136 | int userns : 1; |
| 137 | int disable_setgroups : 1; |
| 138 | int seccomp : 1; |
| 139 | int remount_proc_ro : 1; |
| 140 | int no_new_privs : 1; |
| 141 | int seccomp_filter : 1; |
| 142 | int seccomp_filter_tsync : 1; |
| 143 | int seccomp_filter_logging : 1; |
| 144 | int chroot : 1; |
| 145 | int pivot_root : 1; |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 146 | int mount_dev : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 147 | int mount_tmp : 1; |
| 148 | int do_init : 1; |
Luis Hector Chavez | ac981fc | 2017-09-18 15:52:38 -0700 | [diff] [blame] | 149 | int run_as_init : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 150 | int pid_file : 1; |
| 151 | int cgroups : 1; |
| 152 | int alt_syscall : 1; |
| 153 | int reset_signal_mask : 1; |
| 154 | int close_open_fds : 1; |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 155 | int new_session_keyring : 1; |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 156 | int forward_signals : 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 157 | } flags; |
| 158 | uid_t uid; |
| 159 | gid_t gid; |
| 160 | gid_t usergid; |
| 161 | char *user; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 162 | size_t suppl_gid_count; |
| 163 | gid_t *suppl_gid_list; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 164 | uint64_t caps; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 165 | uint64_t cap_bset; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 166 | pid_t initpid; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 167 | int mountns_fd; |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 168 | int netns_fd; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 169 | char *chrootdir; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 170 | char *pid_file_path; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 171 | char *uidmap; |
| 172 | char *gidmap; |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 173 | char *hostname; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 174 | size_t filter_len; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 175 | struct sock_fprog *filter_prog; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 176 | char *alt_syscall_table; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 177 | struct mountpoint *mounts_head; |
| 178 | struct mountpoint *mounts_tail; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 179 | size_t mounts_count; |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 180 | size_t tmpfs_size; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 181 | char *cgroups[MAX_CGROUPS]; |
| 182 | size_t cgroup_count; |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 183 | struct minijail_rlimit rlimits[MAX_RLIMITS]; |
| 184 | size_t rlimit_count; |
Luis Hector Chavez | ec0a2c1 | 2017-06-29 20:29:57 -0700 | [diff] [blame] | 185 | uint64_t securebits_skip_mask; |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 186 | struct hook *hooks_head; |
| 187 | struct hook *hooks_tail; |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 188 | struct preserved_fd preserved_fds[MAX_PRESERVED_FDS]; |
| 189 | size_t preserved_fd_count; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 190 | }; |
| 191 | |
Luis Hector Chavez | 64730af | 2017-09-13 13:18:59 -0700 | [diff] [blame] | 192 | static void run_hooks_or_die(const struct minijail *j, |
| 193 | minijail_hook_event_t event); |
| 194 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 195 | static void free_mounts_list(struct minijail *j) |
| 196 | { |
| 197 | while (j->mounts_head) { |
| 198 | struct mountpoint *m = j->mounts_head; |
| 199 | j->mounts_head = j->mounts_head->next; |
| 200 | free(m->data); |
| 201 | free(m->type); |
| 202 | free(m->dest); |
| 203 | free(m->src); |
| 204 | free(m); |
| 205 | } |
| 206 | // No need to clear mounts_head as we know it's NULL after the loop. |
| 207 | j->mounts_tail = NULL; |
| 208 | } |
| 209 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 210 | /* |
| 211 | * Strip out flags meant for the parent. |
| 212 | * We keep things that are not inherited across execve(2) (e.g. capabilities), |
| 213 | * or are easier to set after execve(2) (e.g. seccomp filters). |
| 214 | */ |
| 215 | void minijail_preenter(struct minijail *j) |
| 216 | { |
| 217 | j->flags.vfs = 0; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 218 | j->flags.enter_vfs = 0; |
Shuhei Takahashi | 3da4031 | 2016-03-07 17:37:49 +0900 | [diff] [blame] | 219 | j->flags.skip_remount_private = 0; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 220 | j->flags.remount_proc_ro = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 221 | j->flags.pids = 0; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 222 | j->flags.do_init = 0; |
Luis Hector Chavez | ac981fc | 2017-09-18 15:52:38 -0700 | [diff] [blame] | 223 | j->flags.run_as_init = 0; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 224 | j->flags.pid_file = 0; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 225 | j->flags.cgroups = 0; |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 226 | j->flags.forward_signals = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Strip out flags meant for the child. |
| 231 | * We keep things that are inherited across execve(2). |
| 232 | */ |
| 233 | void minijail_preexec(struct minijail *j) |
| 234 | { |
| 235 | int vfs = j->flags.vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 236 | int enter_vfs = j->flags.enter_vfs; |
Jorge Lucangeli Obes | 87bf01d | 2016-03-08 11:20:03 -0800 | [diff] [blame] | 237 | int skip_remount_private = j->flags.skip_remount_private; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 238 | int remount_proc_ro = j->flags.remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 239 | int userns = j->flags.userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 240 | if (j->user) |
| 241 | free(j->user); |
| 242 | j->user = NULL; |
Jorge Lucangeli Obes | e81a52f | 2015-12-04 16:05:23 -0800 | [diff] [blame] | 243 | if (j->suppl_gid_list) |
| 244 | free(j->suppl_gid_list); |
| 245 | j->suppl_gid_list = NULL; |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 246 | free_mounts_list(j); |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 247 | memset(&j->flags, 0, sizeof(j->flags)); |
| 248 | /* Now restore anything we meant to keep. */ |
| 249 | j->flags.vfs = vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 250 | j->flags.enter_vfs = enter_vfs; |
Jorge Lucangeli Obes | 87bf01d | 2016-03-08 11:20:03 -0800 | [diff] [blame] | 251 | j->flags.skip_remount_private = skip_remount_private; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 252 | j->flags.remount_proc_ro = remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 253 | j->flags.userns = userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 254 | /* Note, |pids| will already have been used before this call. */ |
| 255 | } |
| 256 | |
| 257 | /* Minijail API. */ |
| 258 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 259 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 260 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 261 | return calloc(1, sizeof(struct minijail)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 262 | } |
| 263 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 264 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 265 | { |
| 266 | if (uid == 0) |
| 267 | die("useless change to uid 0"); |
| 268 | j->uid = uid; |
| 269 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 270 | } |
| 271 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 272 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 273 | { |
| 274 | if (gid == 0) |
| 275 | die("useless change to gid 0"); |
| 276 | j->gid = gid; |
| 277 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 278 | } |
| 279 | |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 280 | void API minijail_set_supplementary_gids(struct minijail *j, size_t size, |
| 281 | const gid_t *list) |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 282 | { |
Jorge Lucangeli Obes | 06940be | 2015-12-04 18:09:21 -0800 | [diff] [blame] | 283 | size_t i; |
| 284 | |
Jorge Lucangeli Obes | 3454319 | 2017-01-11 16:07:57 -0500 | [diff] [blame] | 285 | if (j->flags.inherit_suppl_gids) |
| 286 | die("cannot inherit *and* set supplementary groups"); |
| 287 | if (j->flags.keep_suppl_gids) |
| 288 | die("cannot keep *and* set supplementary groups"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 289 | |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 290 | if (size == 0) { |
| 291 | /* Clear supplementary groups. */ |
| 292 | j->suppl_gid_list = NULL; |
| 293 | j->suppl_gid_count = 0; |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 294 | j->flags.set_suppl_gids = 1; |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 295 | return; |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 296 | } |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 297 | |
| 298 | /* Copy the gid_t array. */ |
| 299 | j->suppl_gid_list = calloc(size, sizeof(gid_t)); |
| 300 | if (!j->suppl_gid_list) { |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 301 | die("failed to allocate internal supplementary group array"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 302 | } |
Jorge Lucangeli Obes | 06940be | 2015-12-04 18:09:21 -0800 | [diff] [blame] | 303 | for (i = 0; i < size; i++) { |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 304 | j->suppl_gid_list[i] = list[i]; |
| 305 | } |
| 306 | j->suppl_gid_count = size; |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 307 | j->flags.set_suppl_gids = 1; |
| 308 | } |
| 309 | |
| 310 | void API minijail_keep_supplementary_gids(struct minijail *j) { |
| 311 | j->flags.keep_suppl_gids = 1; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 314 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 315 | { |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 316 | uid_t uid; |
| 317 | gid_t gid; |
| 318 | int rc = lookup_user(user, &uid, &gid); |
| 319 | if (rc) |
| 320 | return rc; |
| 321 | minijail_change_uid(j, uid); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 322 | j->user = strdup(user); |
| 323 | if (!j->user) |
| 324 | return -ENOMEM; |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 325 | j->usergid = gid; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 326 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 327 | } |
| 328 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 329 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 330 | { |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 331 | gid_t gid; |
| 332 | int rc = lookup_group(group, &gid); |
| 333 | if (rc) |
| 334 | return rc; |
| 335 | minijail_change_gid(j, gid); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 336 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 337 | } |
| 338 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 339 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 340 | { |
| 341 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 344 | void API minijail_no_new_privs(struct minijail *j) |
| 345 | { |
| 346 | j->flags.no_new_privs = 1; |
| 347 | } |
| 348 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 349 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 350 | { |
| 351 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 352 | } |
| 353 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 354 | void API minijail_set_seccomp_filter_tsync(struct minijail *j) |
| 355 | { |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 356 | if (j->filter_len > 0 && j->filter_prog != NULL) { |
| 357 | die("minijail_set_seccomp_filter_tsync() must be called " |
| 358 | "before minijail_parse_seccomp_filters()"); |
| 359 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 360 | j->flags.seccomp_filter_tsync = 1; |
| 361 | } |
| 362 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 363 | void API minijail_log_seccomp_filter_failures(struct minijail *j) |
| 364 | { |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 365 | if (j->filter_len > 0 && j->filter_prog != NULL) { |
| 366 | die("minijail_log_seccomp_filter_failures() must be called " |
| 367 | "before minijail_parse_seccomp_filters()"); |
| 368 | } |
| 369 | j->flags.seccomp_filter_logging = 1; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 372 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 373 | { |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 374 | /* |
| 375 | * 'minijail_use_caps' configures a runtime-capabilities-only |
| 376 | * environment, including a bounding set matching the thread's runtime |
| 377 | * (permitted|inheritable|effective) sets. |
| 378 | * Therefore, it will override any existing bounding set configurations |
| 379 | * since the latter would allow gaining extra runtime capabilities from |
| 380 | * file capabilities. |
| 381 | */ |
| 382 | if (j->flags.capbset_drop) { |
| 383 | warn("overriding bounding set configuration"); |
| 384 | j->cap_bset = 0; |
| 385 | j->flags.capbset_drop = 0; |
| 386 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 387 | j->caps = capmask; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 388 | j->flags.use_caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 391 | void API minijail_capbset_drop(struct minijail *j, uint64_t capmask) |
| 392 | { |
| 393 | if (j->flags.use_caps) { |
| 394 | /* |
| 395 | * 'minijail_use_caps' will have already configured a capability |
| 396 | * bounding set matching the (permitted|inheritable|effective) |
| 397 | * sets. Abort if the user tries to configure a separate |
| 398 | * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps' |
| 399 | * are mutually exclusive. |
| 400 | */ |
| 401 | die("runtime capabilities already configured, can't drop " |
| 402 | "bounding set separately"); |
| 403 | } |
| 404 | j->cap_bset = capmask; |
| 405 | j->flags.capbset_drop = 1; |
| 406 | } |
| 407 | |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 408 | void API minijail_set_ambient_caps(struct minijail *j) |
| 409 | { |
| 410 | j->flags.set_ambient_caps = 1; |
| 411 | } |
| 412 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 413 | void API minijail_reset_signal_mask(struct minijail *j) |
| 414 | { |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 415 | j->flags.reset_signal_mask = 1; |
| 416 | } |
| 417 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 418 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 419 | { |
| 420 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 421 | } |
| 422 | |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 423 | void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path) |
| 424 | { |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 425 | int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC); |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 426 | if (ns_fd < 0) { |
| 427 | pdie("failed to open namespace '%s'", ns_path); |
| 428 | } |
| 429 | j->mountns_fd = ns_fd; |
| 430 | j->flags.enter_vfs = 1; |
| 431 | } |
| 432 | |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 433 | void API minijail_new_session_keyring(struct minijail *j) |
| 434 | { |
| 435 | j->flags.new_session_keyring = 1; |
| 436 | } |
| 437 | |
Luis Hector Chavez | ec0a2c1 | 2017-06-29 20:29:57 -0700 | [diff] [blame] | 438 | void API minijail_skip_setting_securebits(struct minijail *j, |
| 439 | uint64_t securebits_skip_mask) |
| 440 | { |
| 441 | j->securebits_skip_mask = securebits_skip_mask; |
| 442 | } |
| 443 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 444 | void API minijail_skip_remount_private(struct minijail *j) |
| 445 | { |
| 446 | j->flags.skip_remount_private = 1; |
| 447 | } |
| 448 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 449 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 450 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 451 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 452 | j->flags.remount_proc_ro = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 453 | j->flags.pids = 1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 454 | j->flags.do_init = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 455 | } |
| 456 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 457 | void API minijail_namespace_ipc(struct minijail *j) |
| 458 | { |
| 459 | j->flags.ipc = 1; |
| 460 | } |
| 461 | |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 462 | void API minijail_namespace_uts(struct minijail *j) |
| 463 | { |
| 464 | j->flags.uts = 1; |
| 465 | } |
| 466 | |
| 467 | int API minijail_namespace_set_hostname(struct minijail *j, const char *name) |
| 468 | { |
| 469 | if (j->hostname) |
| 470 | return -EINVAL; |
| 471 | minijail_namespace_uts(j); |
| 472 | j->hostname = strdup(name); |
| 473 | if (!j->hostname) |
| 474 | return -ENOMEM; |
| 475 | return 0; |
| 476 | } |
| 477 | |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 478 | void API minijail_namespace_net(struct minijail *j) |
| 479 | { |
| 480 | j->flags.net = 1; |
| 481 | } |
| 482 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 483 | void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path) |
| 484 | { |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 485 | int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 486 | if (ns_fd < 0) { |
| 487 | pdie("failed to open namespace '%s'", ns_path); |
| 488 | } |
| 489 | j->netns_fd = ns_fd; |
| 490 | j->flags.enter_net = 1; |
| 491 | } |
| 492 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 493 | void API minijail_namespace_cgroups(struct minijail *j) |
| 494 | { |
| 495 | j->flags.ns_cgroups = 1; |
| 496 | } |
| 497 | |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 498 | void API minijail_close_open_fds(struct minijail *j) |
| 499 | { |
| 500 | j->flags.close_open_fds = 1; |
| 501 | } |
| 502 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 503 | void API minijail_remount_proc_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 504 | { |
| 505 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 506 | j->flags.remount_proc_ro = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 507 | } |
| 508 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 509 | void API minijail_namespace_user(struct minijail *j) |
| 510 | { |
| 511 | j->flags.userns = 1; |
| 512 | } |
| 513 | |
Jorge Lucangeli Obes | 200299c | 2016-09-23 15:21:57 -0400 | [diff] [blame] | 514 | void API minijail_namespace_user_disable_setgroups(struct minijail *j) |
| 515 | { |
| 516 | j->flags.disable_setgroups = 1; |
| 517 | } |
| 518 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 519 | int API minijail_uidmap(struct minijail *j, const char *uidmap) |
| 520 | { |
| 521 | j->uidmap = strdup(uidmap); |
| 522 | if (!j->uidmap) |
| 523 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 524 | char *ch; |
| 525 | for (ch = j->uidmap; *ch; ch++) { |
| 526 | if (*ch == ',') |
| 527 | *ch = '\n'; |
| 528 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | int API minijail_gidmap(struct minijail *j, const char *gidmap) |
| 533 | { |
| 534 | j->gidmap = strdup(gidmap); |
| 535 | if (!j->gidmap) |
| 536 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 537 | char *ch; |
| 538 | for (ch = j->gidmap; *ch; ch++) { |
| 539 | if (*ch == ',') |
| 540 | *ch = '\n'; |
| 541 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 545 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 546 | { |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 547 | j->flags.inherit_suppl_gids = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 550 | void API minijail_run_as_init(struct minijail *j) |
| 551 | { |
| 552 | /* |
| 553 | * Since the jailed program will become 'init' in the new PID namespace, |
| 554 | * Minijail does not need to fork an 'init' process. |
| 555 | */ |
Luis Hector Chavez | ac981fc | 2017-09-18 15:52:38 -0700 | [diff] [blame] | 556 | j->flags.run_as_init = 1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 557 | } |
| 558 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 559 | int API minijail_enter_chroot(struct minijail *j, const char *dir) |
| 560 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 561 | if (j->chrootdir) |
| 562 | return -EINVAL; |
| 563 | j->chrootdir = strdup(dir); |
| 564 | if (!j->chrootdir) |
| 565 | return -ENOMEM; |
| 566 | j->flags.chroot = 1; |
| 567 | return 0; |
| 568 | } |
| 569 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 570 | int API minijail_enter_pivot_root(struct minijail *j, const char *dir) |
| 571 | { |
| 572 | if (j->chrootdir) |
| 573 | return -EINVAL; |
| 574 | j->chrootdir = strdup(dir); |
| 575 | if (!j->chrootdir) |
| 576 | return -ENOMEM; |
| 577 | j->flags.pivot_root = 1; |
| 578 | return 0; |
| 579 | } |
| 580 | |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 581 | char API *minijail_get_original_path(struct minijail *j, |
| 582 | const char *path_inside_chroot) |
| 583 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 584 | struct mountpoint *b; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 585 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 586 | b = j->mounts_head; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 587 | while (b) { |
| 588 | /* |
| 589 | * If |path_inside_chroot| is the exact destination of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 590 | * mount, then the original path is exactly the source of |
| 591 | * the mount. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 592 | * for example: "-b /some/path/exe,/chroot/path/exe" |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 593 | * mount source = /some/path/exe, mount dest = |
| 594 | * /chroot/path/exe Then when getting the original path of |
| 595 | * "/chroot/path/exe", the source of that mount, |
| 596 | * "/some/path/exe" is what should be returned. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 597 | */ |
| 598 | if (!strcmp(b->dest, path_inside_chroot)) |
| 599 | return strdup(b->src); |
| 600 | |
| 601 | /* |
| 602 | * If |path_inside_chroot| is within the destination path of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 603 | * mount, take the suffix of the chroot path relative to the |
| 604 | * mount destination path, and append it to the mount source |
| 605 | * path. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 606 | */ |
| 607 | if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) { |
| 608 | const char *relative_path = |
| 609 | path_inside_chroot + strlen(b->dest); |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 610 | return path_join(b->src, relative_path); |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 611 | } |
| 612 | b = b->next; |
| 613 | } |
| 614 | |
| 615 | /* If there is a chroot path, append |path_inside_chroot| to that. */ |
| 616 | if (j->chrootdir) |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 617 | return path_join(j->chrootdir, path_inside_chroot); |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 618 | |
| 619 | /* No chroot, so the path outside is the same as it is inside. */ |
| 620 | return strdup(path_inside_chroot); |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 623 | size_t minijail_get_tmpfs_size(const struct minijail *j) |
| 624 | { |
| 625 | return j->tmpfs_size; |
| 626 | } |
| 627 | |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 628 | void API minijail_mount_dev(struct minijail *j) |
| 629 | { |
| 630 | j->flags.mount_dev = 1; |
| 631 | } |
| 632 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 633 | void API minijail_mount_tmp(struct minijail *j) |
| 634 | { |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 635 | minijail_mount_tmp_size(j, 64 * 1024 * 1024); |
| 636 | } |
| 637 | |
| 638 | void API minijail_mount_tmp_size(struct minijail *j, size_t size) |
| 639 | { |
| 640 | j->tmpfs_size = size; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 641 | j->flags.mount_tmp = 1; |
| 642 | } |
| 643 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 644 | int API minijail_write_pid_file(struct minijail *j, const char *path) |
| 645 | { |
| 646 | j->pid_file_path = strdup(path); |
| 647 | if (!j->pid_file_path) |
| 648 | return -ENOMEM; |
| 649 | j->flags.pid_file = 1; |
| 650 | return 0; |
| 651 | } |
| 652 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 653 | int API minijail_add_to_cgroup(struct minijail *j, const char *path) |
| 654 | { |
| 655 | if (j->cgroup_count >= MAX_CGROUPS) |
| 656 | return -ENOMEM; |
| 657 | j->cgroups[j->cgroup_count] = strdup(path); |
| 658 | if (!j->cgroups[j->cgroup_count]) |
| 659 | return -ENOMEM; |
| 660 | j->cgroup_count++; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 661 | j->flags.cgroups = 1; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 665 | int API minijail_rlimit(struct minijail *j, int type, uint32_t cur, |
| 666 | uint32_t max) |
| 667 | { |
| 668 | size_t i; |
| 669 | |
| 670 | if (j->rlimit_count >= MAX_RLIMITS) |
| 671 | return -ENOMEM; |
| 672 | /* It's an error if the caller sets the same rlimit multiple times. */ |
| 673 | for (i = 0; i < j->rlimit_count; i++) { |
| 674 | if (j->rlimits[i].type == type) |
| 675 | return -EEXIST; |
| 676 | } |
| 677 | |
| 678 | j->rlimits[j->rlimit_count].type = type; |
| 679 | j->rlimits[j->rlimit_count].cur = cur; |
| 680 | j->rlimits[j->rlimit_count].max = max; |
| 681 | j->rlimit_count++; |
| 682 | return 0; |
| 683 | } |
| 684 | |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 685 | int API minijail_forward_signals(struct minijail *j) |
| 686 | { |
| 687 | j->flags.forward_signals = 1; |
| 688 | return 0; |
| 689 | } |
| 690 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 691 | int API minijail_mount_with_data(struct minijail *j, const char *src, |
| 692 | const char *dest, const char *type, |
| 693 | unsigned long flags, const char *data) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 694 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 695 | struct mountpoint *m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 696 | |
| 697 | if (*dest != '/') |
| 698 | return -EINVAL; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 699 | m = calloc(1, sizeof(*m)); |
| 700 | if (!m) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 701 | return -ENOMEM; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 702 | m->dest = strdup(dest); |
| 703 | if (!m->dest) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 704 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 705 | m->src = strdup(src); |
| 706 | if (!m->src) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 707 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 708 | m->type = strdup(type); |
| 709 | if (!m->type) |
| 710 | goto error; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 711 | if (data) { |
| 712 | m->data = strdup(data); |
| 713 | if (!m->data) |
| 714 | goto error; |
| 715 | m->has_data = 1; |
| 716 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 717 | m->flags = flags; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 718 | |
Jorge Lucangeli Obes | 6c755d2 | 2016-01-28 15:24:40 -0800 | [diff] [blame] | 719 | info("mount %s -> %s type '%s'", src, dest, type); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 720 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 721 | /* |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 722 | * Force vfs namespacing so the mounts don't leak out into the |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 723 | * containing vfs namespace. |
| 724 | */ |
| 725 | minijail_namespace_vfs(j); |
| 726 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 727 | if (j->mounts_tail) |
| 728 | j->mounts_tail->next = m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 729 | else |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 730 | j->mounts_head = m; |
| 731 | j->mounts_tail = m; |
| 732 | j->mounts_count++; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 733 | |
| 734 | return 0; |
| 735 | |
| 736 | error: |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 737 | free(m->type); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 738 | free(m->src); |
| 739 | free(m->dest); |
| 740 | free(m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 741 | return -ENOMEM; |
| 742 | } |
| 743 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 744 | int API minijail_mount(struct minijail *j, const char *src, const char *dest, |
| 745 | const char *type, unsigned long flags) |
| 746 | { |
| 747 | return minijail_mount_with_data(j, src, dest, type, flags, NULL); |
| 748 | } |
| 749 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 750 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
| 751 | int writeable) |
| 752 | { |
| 753 | unsigned long flags = MS_BIND; |
| 754 | |
| 755 | if (!writeable) |
| 756 | flags |= MS_RDONLY; |
| 757 | |
| 758 | return minijail_mount(j, src, dest, "", flags); |
| 759 | } |
| 760 | |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 761 | int API minijail_add_hook(struct minijail *j, minijail_hook_t hook, |
| 762 | void *payload, minijail_hook_event_t event) |
| 763 | { |
| 764 | struct hook *c; |
| 765 | |
| 766 | if (hook == NULL) |
| 767 | return -EINVAL; |
| 768 | if (event >= MINIJAIL_HOOK_EVENT_MAX) |
| 769 | return -EINVAL; |
| 770 | c = calloc(1, sizeof(*c)); |
| 771 | if (!c) |
| 772 | return -ENOMEM; |
| 773 | |
| 774 | c->hook = hook; |
| 775 | c->payload = payload; |
| 776 | c->event = event; |
| 777 | |
| 778 | if (j->hooks_tail) |
| 779 | j->hooks_tail->next = c; |
| 780 | else |
| 781 | j->hooks_head = c; |
| 782 | j->hooks_tail = c; |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 787 | int API minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd) |
| 788 | { |
| 789 | if (parent_fd < 0 || child_fd < 0) |
| 790 | return -EINVAL; |
| 791 | if (j->preserved_fd_count >= MAX_PRESERVED_FDS) |
| 792 | return -ENOMEM; |
| 793 | j->preserved_fds[j->preserved_fd_count].parent_fd = parent_fd; |
| 794 | j->preserved_fds[j->preserved_fd_count].child_fd = child_fd; |
| 795 | j->preserved_fd_count++; |
| 796 | return 0; |
| 797 | } |
| 798 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 799 | static void clear_seccomp_options(struct minijail *j) |
| 800 | { |
| 801 | j->flags.seccomp_filter = 0; |
| 802 | j->flags.seccomp_filter_tsync = 0; |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 803 | j->flags.seccomp_filter_logging = 0; |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 804 | j->filter_len = 0; |
| 805 | j->filter_prog = NULL; |
| 806 | j->flags.no_new_privs = 0; |
| 807 | } |
| 808 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 809 | static int seccomp_should_parse_filters(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 810 | { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 811 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) { |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 812 | /* |
| 813 | * |errno| will be set to EINVAL when seccomp has not been |
| 814 | * compiled into the kernel. On certain platforms and kernel |
| 815 | * versions this is not a fatal failure. In that case, and only |
| 816 | * in that case, disable seccomp and skip loading the filters. |
| 817 | */ |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 818 | if ((errno == EINVAL) && seccomp_can_softfail()) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 819 | warn("not loading seccomp filters, seccomp filter not " |
| 820 | "supported"); |
| 821 | clear_seccomp_options(j); |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 822 | return 0; |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 823 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 824 | /* |
| 825 | * If |errno| != EINVAL or seccomp_can_softfail() is false, |
| 826 | * we can proceed. Worst case scenario minijail_enter() will |
| 827 | * abort() if seccomp fails. |
| 828 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 829 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 830 | if (j->flags.seccomp_filter_tsync) { |
| 831 | /* Are the seccomp(2) syscall and the TSYNC option supported? */ |
| 832 | if (sys_seccomp(SECCOMP_SET_MODE_FILTER, |
| 833 | SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) { |
| 834 | int saved_errno = errno; |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 835 | if (saved_errno == ENOSYS && seccomp_can_softfail()) { |
| 836 | warn("seccomp(2) syscall not supported"); |
| 837 | clear_seccomp_options(j); |
| 838 | return 0; |
| 839 | } else if (saved_errno == EINVAL && |
| 840 | seccomp_can_softfail()) { |
| 841 | warn( |
| 842 | "seccomp filter thread sync not supported"); |
| 843 | clear_seccomp_options(j); |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 844 | return 0; |
| 845 | } |
| 846 | /* |
| 847 | * Similar logic here. If seccomp_can_softfail() is |
| 848 | * false, or |errno| != ENOSYS, or |errno| != EINVAL, |
| 849 | * we can proceed. Worst case scenario minijail_enter() |
| 850 | * will abort() if seccomp or TSYNC fail. |
| 851 | */ |
| 852 | } |
| 853 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 854 | return 1; |
| 855 | } |
| 856 | |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 857 | static int parse_seccomp_filters(struct minijail *j, const char *filename, |
| 858 | FILE *policy_file) |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 859 | { |
| 860 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 861 | int use_ret_trap = |
| 862 | j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging; |
| 863 | int allow_logging = j->flags.seccomp_filter_logging; |
| 864 | |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 865 | if (compile_filter(filename, policy_file, fprog, use_ret_trap, |
| 866 | allow_logging)) { |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 867 | free(fprog); |
| 868 | return -1; |
| 869 | } |
| 870 | |
| 871 | j->filter_len = fprog->len; |
| 872 | j->filter_prog = fprog; |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
| 877 | { |
| 878 | if (!seccomp_should_parse_filters(j)) |
| 879 | return; |
| 880 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 881 | FILE *file = fopen(path, "r"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 882 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 883 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 884 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 885 | |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 886 | if (parse_seccomp_filters(j, path, file) != 0) { |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 887 | die("failed to compile seccomp filter BPF program in '%s'", |
| 888 | path); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 889 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 890 | fclose(file); |
| 891 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 892 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 893 | void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd) |
| 894 | { |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 895 | char *fd_path, *path; |
| 896 | FILE *file; |
| 897 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 898 | if (!seccomp_should_parse_filters(j)) |
| 899 | return; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 900 | |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 901 | file = fdopen(fd, "r"); |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 902 | if (!file) { |
| 903 | pdie("failed to associate stream with fd %d", fd); |
| 904 | } |
| 905 | |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 906 | if (asprintf(&fd_path, "/proc/self/fd/%d", fd) == -1) |
| 907 | pdie("failed to create path for fd %d", fd); |
| 908 | path = realpath(fd_path, NULL); |
| 909 | if (path == NULL) |
| 910 | pwarn("failed to get path of fd %d", fd); |
| 911 | free(fd_path); |
| 912 | |
| 913 | if (parse_seccomp_filters(j, path ? path : "<fd>", file) != 0) { |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 914 | die("failed to compile seccomp filter BPF program from fd %d", |
| 915 | fd); |
| 916 | } |
Luis Hector Chavez | 7624e71 | 2017-08-28 19:30:59 -0700 | [diff] [blame] | 917 | free(path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 918 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 919 | } |
| 920 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 921 | int API minijail_use_alt_syscall(struct minijail *j, const char *table) |
| 922 | { |
| 923 | j->alt_syscall_table = strdup(table); |
| 924 | if (!j->alt_syscall_table) |
| 925 | return -ENOMEM; |
| 926 | j->flags.alt_syscall = 1; |
| 927 | return 0; |
| 928 | } |
| 929 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 930 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 931 | size_t available; |
| 932 | size_t total; |
| 933 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 934 | }; |
| 935 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 936 | void marshal_state_init(struct marshal_state *state, char *buf, |
| 937 | size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 938 | { |
| 939 | state->available = available; |
| 940 | state->buf = buf; |
| 941 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 942 | } |
| 943 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 944 | void marshal_append(struct marshal_state *state, void *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 945 | { |
| 946 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 947 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 948 | /* Up to |available| will be written. */ |
| 949 | if (copy_len) { |
| 950 | memcpy(state->buf, src, copy_len); |
| 951 | state->buf += copy_len; |
| 952 | state->available -= copy_len; |
| 953 | } |
| 954 | /* |total| will contain the expected length. */ |
| 955 | state->total += length; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 956 | } |
| 957 | |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 958 | void marshal_mount(struct marshal_state *state, const struct mountpoint *m) |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 959 | { |
| 960 | marshal_append(state, m->src, strlen(m->src) + 1); |
| 961 | marshal_append(state, m->dest, strlen(m->dest) + 1); |
| 962 | marshal_append(state, m->type, strlen(m->type) + 1); |
| 963 | marshal_append(state, (char *)&m->has_data, sizeof(m->has_data)); |
| 964 | if (m->has_data) |
| 965 | marshal_append(state, m->data, strlen(m->data) + 1); |
| 966 | marshal_append(state, (char *)&m->flags, sizeof(m->flags)); |
| 967 | } |
| 968 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 969 | void minijail_marshal_helper(struct marshal_state *state, |
| 970 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 971 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 972 | struct mountpoint *m = NULL; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 973 | size_t i; |
| 974 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 975 | marshal_append(state, (char *)j, sizeof(*j)); |
| 976 | if (j->user) |
| 977 | marshal_append(state, j->user, strlen(j->user) + 1); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 978 | if (j->suppl_gid_list) { |
| 979 | marshal_append(state, j->suppl_gid_list, |
| 980 | j->suppl_gid_count * sizeof(gid_t)); |
| 981 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 982 | if (j->chrootdir) |
| 983 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 984 | if (j->hostname) |
| 985 | marshal_append(state, j->hostname, strlen(j->hostname) + 1); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 986 | if (j->alt_syscall_table) { |
| 987 | marshal_append(state, j->alt_syscall_table, |
| 988 | strlen(j->alt_syscall_table) + 1); |
| 989 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 990 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 991 | struct sock_fprog *fp = j->filter_prog; |
| 992 | marshal_append(state, (char *)fp->filter, |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 993 | fp->len * sizeof(struct sock_filter)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 994 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 995 | for (m = j->mounts_head; m; m = m->next) { |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 996 | marshal_mount(state, m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 997 | } |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 998 | for (i = 0; i < j->cgroup_count; ++i) |
| 999 | marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1000 | } |
| 1001 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1002 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1003 | { |
| 1004 | struct marshal_state state; |
| 1005 | marshal_state_init(&state, NULL, 0); |
| 1006 | minijail_marshal_helper(&state, j); |
| 1007 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 1008 | } |
| 1009 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1010 | int minijail_marshal(const struct minijail *j, char *buf, size_t available) |
| 1011 | { |
| 1012 | struct marshal_state state; |
| 1013 | marshal_state_init(&state, buf, available); |
| 1014 | minijail_marshal_helper(&state, j); |
| 1015 | return (state.total > available); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 1016 | } |
| 1017 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1018 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 1019 | { |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 1020 | size_t i; |
| 1021 | size_t count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1022 | int ret = -EINVAL; |
| 1023 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1024 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1025 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1026 | memcpy((void *)j, serialized, sizeof(*j)); |
| 1027 | serialized += sizeof(*j); |
| 1028 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1029 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1030 | /* Potentially stale pointers not used as signals. */ |
Jorge Lucangeli Obes | 3b2e6e4 | 2016-08-04 12:26:19 -0400 | [diff] [blame] | 1031 | j->pid_file_path = NULL; |
| 1032 | j->uidmap = NULL; |
| 1033 | j->gidmap = NULL; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1034 | j->mounts_head = NULL; |
| 1035 | j->mounts_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1036 | j->filter_prog = NULL; |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 1037 | j->hooks_head = NULL; |
| 1038 | j->hooks_tail = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1039 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1040 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1041 | char *user = consumestr(&serialized, &length); |
| 1042 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1043 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1044 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1045 | if (!j->user) |
| 1046 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1047 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1048 | |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1049 | if (j->suppl_gid_list) { /* stale pointer */ |
| 1050 | if (j->suppl_gid_count > NGROUPS_MAX) { |
| 1051 | goto bad_gid_list; |
| 1052 | } |
| 1053 | size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t); |
| 1054 | void *gid_list_bytes = |
| 1055 | consumebytes(gid_list_size, &serialized, &length); |
| 1056 | if (!gid_list_bytes) |
| 1057 | goto bad_gid_list; |
| 1058 | |
| 1059 | j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t)); |
| 1060 | if (!j->suppl_gid_list) |
| 1061 | goto bad_gid_list; |
| 1062 | |
| 1063 | memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size); |
| 1064 | } |
| 1065 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 1066 | if (j->chrootdir) { /* stale pointer */ |
| 1067 | char *chrootdir = consumestr(&serialized, &length); |
| 1068 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1069 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 1070 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1071 | if (!j->chrootdir) |
| 1072 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 1073 | } |
| 1074 | |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1075 | if (j->hostname) { /* stale pointer */ |
| 1076 | char *hostname = consumestr(&serialized, &length); |
| 1077 | if (!hostname) |
| 1078 | goto bad_hostname; |
| 1079 | j->hostname = strdup(hostname); |
| 1080 | if (!j->hostname) |
| 1081 | goto bad_hostname; |
| 1082 | } |
| 1083 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1084 | if (j->alt_syscall_table) { /* stale pointer */ |
| 1085 | char *alt_syscall_table = consumestr(&serialized, &length); |
| 1086 | if (!alt_syscall_table) |
| 1087 | goto bad_syscall_table; |
| 1088 | j->alt_syscall_table = strdup(alt_syscall_table); |
| 1089 | if (!j->alt_syscall_table) |
| 1090 | goto bad_syscall_table; |
| 1091 | } |
| 1092 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1093 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 1094 | size_t ninstrs = j->filter_len; |
| 1095 | if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) || |
| 1096 | ninstrs > USHRT_MAX) |
| 1097 | goto bad_filters; |
| 1098 | |
| 1099 | size_t program_len = ninstrs * sizeof(struct sock_filter); |
| 1100 | void *program = consumebytes(program_len, &serialized, &length); |
| 1101 | if (!program) |
| 1102 | goto bad_filters; |
| 1103 | |
| 1104 | j->filter_prog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1105 | if (!j->filter_prog) |
| 1106 | goto bad_filters; |
| 1107 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1108 | j->filter_prog->len = ninstrs; |
| 1109 | j->filter_prog->filter = malloc(program_len); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1110 | if (!j->filter_prog->filter) |
| 1111 | goto bad_filter_prog_instrs; |
| 1112 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1113 | memcpy(j->filter_prog->filter, program, program_len); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1114 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1115 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1116 | count = j->mounts_count; |
| 1117 | j->mounts_count = 0; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1118 | for (i = 0; i < count; ++i) { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1119 | unsigned long *flags; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1120 | int *has_data; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1121 | const char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1122 | const char *type; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1123 | const char *data = NULL; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1124 | const char *src = consumestr(&serialized, &length); |
| 1125 | if (!src) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1126 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1127 | dest = consumestr(&serialized, &length); |
| 1128 | if (!dest) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1129 | goto bad_mounts; |
| 1130 | type = consumestr(&serialized, &length); |
| 1131 | if (!type) |
| 1132 | goto bad_mounts; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1133 | has_data = consumebytes(sizeof(*has_data), &serialized, |
| 1134 | &length); |
| 1135 | if (!has_data) |
| 1136 | goto bad_mounts; |
| 1137 | if (*has_data) { |
| 1138 | data = consumestr(&serialized, &length); |
| 1139 | if (!data) |
| 1140 | goto bad_mounts; |
| 1141 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1142 | flags = consumebytes(sizeof(*flags), &serialized, &length); |
| 1143 | if (!flags) |
| 1144 | goto bad_mounts; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1145 | if (minijail_mount_with_data(j, src, dest, type, *flags, data)) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1146 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1147 | } |
| 1148 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1149 | count = j->cgroup_count; |
| 1150 | j->cgroup_count = 0; |
| 1151 | for (i = 0; i < count; ++i) { |
| 1152 | char *cgroup = consumestr(&serialized, &length); |
| 1153 | if (!cgroup) |
| 1154 | goto bad_cgroups; |
| 1155 | j->cgroups[i] = strdup(cgroup); |
| 1156 | if (!j->cgroups[i]) |
| 1157 | goto bad_cgroups; |
| 1158 | ++j->cgroup_count; |
| 1159 | } |
| 1160 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1161 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1162 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1163 | bad_cgroups: |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1164 | free_mounts_list(j); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1165 | for (i = 0; i < j->cgroup_count; ++i) |
| 1166 | free(j->cgroups[i]); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1167 | bad_mounts: |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1168 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 1169 | free(j->filter_prog->filter); |
| 1170 | free(j->filter_prog); |
| 1171 | } |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1172 | bad_filter_prog_instrs: |
| 1173 | if (j->filter_prog) |
| 1174 | free(j->filter_prog); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1175 | bad_filters: |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1176 | if (j->alt_syscall_table) |
| 1177 | free(j->alt_syscall_table); |
| 1178 | bad_syscall_table: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1179 | if (j->chrootdir) |
| 1180 | free(j->chrootdir); |
| 1181 | bad_chrootdir: |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1182 | if (j->hostname) |
| 1183 | free(j->hostname); |
| 1184 | bad_hostname: |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1185 | if (j->suppl_gid_list) |
| 1186 | free(j->suppl_gid_list); |
| 1187 | bad_gid_list: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1188 | if (j->user) |
| 1189 | free(j->user); |
| 1190 | clear_pointers: |
| 1191 | j->user = NULL; |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1192 | j->suppl_gid_list = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1193 | j->chrootdir = NULL; |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1194 | j->hostname = NULL; |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1195 | j->alt_syscall_table = NULL; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1196 | j->cgroup_count = 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1197 | out: |
| 1198 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 1199 | } |
| 1200 | |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1201 | struct dev_spec { |
| 1202 | const char *name; |
| 1203 | mode_t mode; |
| 1204 | dev_t major, minor; |
| 1205 | }; |
| 1206 | |
| 1207 | static const struct dev_spec device_nodes[] = { |
| 1208 | { |
| 1209 | "null", |
| 1210 | S_IFCHR | 0666, 1, 3, |
| 1211 | }, |
| 1212 | { |
| 1213 | "zero", |
| 1214 | S_IFCHR | 0666, 1, 5, |
| 1215 | }, |
| 1216 | { |
| 1217 | "full", |
| 1218 | S_IFCHR | 0666, 1, 7, |
| 1219 | }, |
| 1220 | { |
| 1221 | "urandom", |
| 1222 | S_IFCHR | 0444, 1, 9, |
| 1223 | }, |
| 1224 | { |
| 1225 | "tty", |
| 1226 | S_IFCHR | 0666, 5, 0, |
| 1227 | }, |
| 1228 | }; |
| 1229 | |
| 1230 | struct dev_sym_spec { |
| 1231 | const char *source, *dest; |
| 1232 | }; |
| 1233 | |
| 1234 | static const struct dev_sym_spec device_symlinks[] = { |
| 1235 | { "ptmx", "pts/ptmx", }, |
| 1236 | { "fd", "/proc/self/fd", }, |
| 1237 | { "stdin", "fd/0", }, |
| 1238 | { "stdout", "fd/1", }, |
| 1239 | { "stderr", "fd/2", }, |
| 1240 | }; |
| 1241 | |
| 1242 | /* |
| 1243 | * Clean up the temporary dev path we had setup previously. In case of errors, |
| 1244 | * we don't want to go leaking empty tempdirs. |
| 1245 | */ |
| 1246 | static void mount_dev_cleanup(char *dev_path) |
| 1247 | { |
| 1248 | umount2(dev_path, MNT_DETACH); |
| 1249 | rmdir(dev_path); |
| 1250 | free(dev_path); |
| 1251 | } |
| 1252 | |
| 1253 | /* |
| 1254 | * Set up the pseudo /dev path at the temporary location. |
| 1255 | * See mount_dev_finalize for more details. |
| 1256 | */ |
| 1257 | static int mount_dev(char **dev_path_ret) |
| 1258 | { |
| 1259 | int ret; |
| 1260 | int dev_fd; |
| 1261 | size_t i; |
| 1262 | mode_t mask; |
| 1263 | char *dev_path; |
| 1264 | |
| 1265 | /* |
| 1266 | * Create a temp path for the /dev init. We'll relocate this to the |
| 1267 | * final location later on in the startup process. |
| 1268 | */ |
| 1269 | dev_path = *dev_path_ret = strdup("/tmp/minijail.dev.XXXXXX"); |
| 1270 | if (dev_path == NULL || mkdtemp(dev_path) == NULL) |
| 1271 | pdie("could not create temp path for /dev"); |
| 1272 | |
| 1273 | /* Set up the empty /dev mount point first. */ |
| 1274 | ret = mount("minijail-devfs", dev_path, "tmpfs", |
| 1275 | MS_NOEXEC | MS_NOSUID, "size=5M,mode=755"); |
| 1276 | if (ret) { |
| 1277 | rmdir(dev_path); |
| 1278 | return ret; |
| 1279 | } |
| 1280 | |
| 1281 | /* We want to set the mode directly from the spec. */ |
| 1282 | mask = umask(0); |
| 1283 | |
| 1284 | /* Get a handle to the temp dev path for *at funcs below. */ |
| 1285 | dev_fd = open(dev_path, O_DIRECTORY|O_PATH|O_CLOEXEC); |
| 1286 | if (dev_fd < 0) { |
| 1287 | ret = 1; |
| 1288 | goto done; |
| 1289 | } |
| 1290 | |
| 1291 | /* Create all the nodes in /dev. */ |
| 1292 | for (i = 0; i < ARRAY_SIZE(device_nodes); ++i) { |
| 1293 | const struct dev_spec *ds = &device_nodes[i]; |
| 1294 | ret = mknodat(dev_fd, ds->name, ds->mode, |
| 1295 | makedev(ds->major, ds->minor)); |
| 1296 | if (ret) |
| 1297 | goto done; |
| 1298 | } |
| 1299 | |
| 1300 | /* Create all the symlinks in /dev. */ |
| 1301 | for (i = 0; i < ARRAY_SIZE(device_symlinks); ++i) { |
| 1302 | const struct dev_sym_spec *ds = &device_symlinks[i]; |
| 1303 | ret = symlinkat(ds->dest, dev_fd, ds->source); |
| 1304 | if (ret) |
| 1305 | goto done; |
| 1306 | } |
| 1307 | |
| 1308 | /* Restore old mask. */ |
| 1309 | done: |
| 1310 | close(dev_fd); |
| 1311 | umask(mask); |
| 1312 | |
| 1313 | if (ret) |
| 1314 | mount_dev_cleanup(dev_path); |
| 1315 | |
| 1316 | return ret; |
| 1317 | } |
| 1318 | |
| 1319 | /* |
| 1320 | * Relocate the temporary /dev mount to its final /dev place. |
| 1321 | * We have to do this two step process so people can bind mount extra |
| 1322 | * /dev paths like /dev/log. |
| 1323 | */ |
| 1324 | static int mount_dev_finalize(const struct minijail *j, char *dev_path) |
| 1325 | { |
| 1326 | int ret = -1; |
| 1327 | char *dest = NULL; |
| 1328 | |
| 1329 | /* Unmount the /dev mount if possible. */ |
| 1330 | if (umount2("/dev", MNT_DETACH)) |
| 1331 | goto done; |
| 1332 | |
| 1333 | if (asprintf(&dest, "%s/dev", j->chrootdir ? : "") < 0) |
| 1334 | goto done; |
| 1335 | |
| 1336 | if (mount(dev_path, dest, NULL, MS_MOVE, NULL)) |
| 1337 | goto done; |
| 1338 | |
| 1339 | ret = 0; |
| 1340 | done: |
| 1341 | free(dest); |
| 1342 | mount_dev_cleanup(dev_path); |
| 1343 | |
| 1344 | return ret; |
| 1345 | } |
| 1346 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1347 | /* |
| 1348 | * mount_one: Applies mounts from @m for @j, recursing as needed. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1349 | * @j Minijail these mounts are for |
| 1350 | * @m Head of list of mounts |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1351 | * |
| 1352 | * Returns 0 for success. |
| 1353 | */ |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1354 | static int mount_one(const struct minijail *j, struct mountpoint *m, |
| 1355 | const char *dev_path) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 1356 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1357 | int ret; |
| 1358 | char *dest; |
| 1359 | int remount_ro = 0; |
| 1360 | |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1361 | /* We assume |dest| has a leading "/". */ |
| 1362 | if (dev_path && strncmp("/dev/", m->dest, 5) == 0) { |
| 1363 | /* Since the temp path is rooted at /dev, skip that dest part. */ |
| 1364 | if (asprintf(&dest, "%s%s", dev_path, m->dest + 4) < 0) |
| 1365 | return -ENOMEM; |
| 1366 | } else { |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1367 | if (asprintf(&dest, "%s%s", j->chrootdir ?: "", m->dest) < 0) |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1368 | return -ENOMEM; |
| 1369 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1370 | |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1371 | ret = setup_mount_destination(m->src, dest, j->uid, j->gid, |
| 1372 | (m->flags & MS_BIND)); |
| 1373 | if (ret) { |
| 1374 | pwarn("creating mount target '%s' failed", dest); |
| 1375 | return ret; |
| 1376 | } |
Dylan Reid | eec7796 | 2016-06-30 19:35:10 -0700 | [diff] [blame] | 1377 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1378 | /* |
Jorge Lucangeli Obes | 2b12ba4 | 2016-01-26 10:37:51 -0800 | [diff] [blame] | 1379 | * R/O bind mounts have to be remounted since 'bind' and 'ro' |
| 1380 | * can't both be specified in the original bind mount. |
| 1381 | * Remount R/O after the initial mount. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1382 | */ |
| 1383 | if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) { |
| 1384 | remount_ro = 1; |
| 1385 | m->flags &= ~MS_RDONLY; |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 1386 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1387 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1388 | ret = mount(m->src, dest, m->type, m->flags, m->data); |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1389 | if (ret) { |
| 1390 | pwarn("mount: %s -> %s", m->src, dest); |
| 1391 | return ret; |
| 1392 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1393 | |
| 1394 | if (remount_ro) { |
| 1395 | m->flags |= MS_RDONLY; |
| 1396 | ret = mount(m->src, dest, NULL, |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1397 | m->flags | MS_REMOUNT, m->data); |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1398 | if (ret) { |
| 1399 | pwarn("bind ro: %s -> %s", m->src, dest); |
| 1400 | return ret; |
| 1401 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1404 | free(dest); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1405 | if (m->next) |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1406 | return mount_one(j, m->next, dev_path); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1407 | return ret; |
| 1408 | } |
| 1409 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1410 | static void process_mounts_or_die(const struct minijail *j) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 1411 | { |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1412 | /* |
| 1413 | * We have to mount /dev first in case there are bind mounts from |
| 1414 | * the original /dev into the new unique tmpfs one. |
| 1415 | */ |
| 1416 | char *dev_path = NULL; |
| 1417 | if (j->flags.mount_dev && mount_dev(&dev_path)) |
| 1418 | pdie("mount_dev failed"); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1419 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1420 | if (j->mounts_head && mount_one(j, j->mounts_head, dev_path)) { |
| 1421 | if (dev_path) { |
| 1422 | int saved_errno = errno; |
| 1423 | mount_dev_cleanup(dev_path); |
| 1424 | errno = saved_errno; |
| 1425 | } |
| 1426 | pdie("mount_one failed"); |
| 1427 | } |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1428 | |
| 1429 | /* |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1430 | * Once all bind mounts have been processed, move the temp dev to |
| 1431 | * its final /dev home. |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1432 | */ |
| 1433 | if (j->flags.mount_dev && mount_dev_finalize(j, dev_path)) |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1434 | pdie("mount_dev_finalize failed"); |
| 1435 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1436 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1437 | static int enter_chroot(const struct minijail *j) |
| 1438 | { |
Luis Hector Chavez | 64730af | 2017-09-13 13:18:59 -0700 | [diff] [blame] | 1439 | run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT); |
| 1440 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1441 | if (chroot(j->chrootdir)) |
| 1442 | return -errno; |
| 1443 | |
| 1444 | if (chdir("/")) |
| 1445 | return -errno; |
| 1446 | |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1450 | static int enter_pivot_root(const struct minijail *j) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1451 | { |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1452 | int oldroot, newroot; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1453 | |
Luis Hector Chavez | 64730af | 2017-09-13 13:18:59 -0700 | [diff] [blame] | 1454 | run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_CHROOT); |
| 1455 | |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1456 | /* |
| 1457 | * Keep the fd for both old and new root. |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1458 | * It will be used in fchdir(2) later. |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1459 | */ |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1460 | oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1461 | if (oldroot < 0) |
| 1462 | pdie("failed to open / for fchdir"); |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1463 | newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1464 | if (newroot < 0) |
| 1465 | pdie("failed to open %s for fchdir", j->chrootdir); |
| 1466 | |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1467 | /* |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1468 | * To ensure j->chrootdir is the root of a filesystem, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1469 | * do a self bind mount. |
| 1470 | */ |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1471 | if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, "")) |
| 1472 | pdie("failed to bind mount '%s'", j->chrootdir); |
| 1473 | if (chdir(j->chrootdir)) |
| 1474 | return -errno; |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1475 | if (syscall(SYS_pivot_root, ".", ".")) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1476 | pdie("pivot_root"); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1477 | |
| 1478 | /* |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1479 | * Now the old root is mounted on top of the new root. Use fchdir(2) to |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1480 | * change to the old root and unmount it. |
| 1481 | */ |
| 1482 | if (fchdir(oldroot)) |
| 1483 | pdie("failed to fchdir to old /"); |
Hidehiko Abe | 097b719 | 2016-03-16 18:00:36 +0900 | [diff] [blame] | 1484 | |
| 1485 | /* |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1486 | * If j->flags.skip_remount_private was enabled for minijail_enter(), |
| 1487 | * there could be a shared mount point under |oldroot|. In that case, |
| 1488 | * mounts under this shared mount point will be unmounted below, and |
| 1489 | * this unmounting will propagate to the original mount namespace |
| 1490 | * (because the mount point is shared). To prevent this unexpected |
| 1491 | * unmounting, remove these mounts from their peer groups by recursively |
| 1492 | * remounting them as MS_PRIVATE. |
Hidehiko Abe | 097b719 | 2016-03-16 18:00:36 +0900 | [diff] [blame] | 1493 | */ |
| 1494 | if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL)) |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1495 | pdie("failed to mount(/, private) before umount(/)"); |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1496 | /* The old root might be busy, so use lazy unmount. */ |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1497 | if (umount2(".", MNT_DETACH)) |
| 1498 | pdie("umount(/)"); |
| 1499 | /* Change back to the new root. */ |
| 1500 | if (fchdir(newroot)) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1501 | return -errno; |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1502 | if (close(oldroot)) |
| 1503 | return -errno; |
| 1504 | if (close(newroot)) |
| 1505 | return -errno; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1506 | if (chroot("/")) |
| 1507 | return -errno; |
Jorge Lucangeli Obes | 46a5509 | 2015-10-12 15:31:59 -0700 | [diff] [blame] | 1508 | /* Set correct CWD for getcwd(3). */ |
| 1509 | if (chdir("/")) |
| 1510 | return -errno; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1511 | |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1515 | static int mount_tmp(const struct minijail *j) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1516 | { |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1517 | const char fmt[] = "size=%zu,mode=1777"; |
| 1518 | /* Count for the user storing ULLONG_MAX literally + extra space. */ |
| 1519 | char data[sizeof(fmt) + sizeof("18446744073709551615ULL")]; |
| 1520 | int ret; |
| 1521 | |
| 1522 | ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size); |
| 1523 | |
| 1524 | if (ret <= 0) |
| 1525 | pdie("tmpfs size spec error"); |
| 1526 | else if ((size_t)ret >= sizeof(data)) |
| 1527 | pdie("tmpfs size spec too large"); |
Mike Frysinger | b91d404 | 2017-01-13 19:03:34 -0500 | [diff] [blame] | 1528 | return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1529 | data); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1532 | static int remount_proc_readonly(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1533 | { |
| 1534 | const char *kProcPath = "/proc"; |
| 1535 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1536 | /* |
| 1537 | * 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] | 1538 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 1539 | * mutate our parent's mount as well, even though we're in a VFS |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1540 | * namespace (!). Instead, remove their mount from our namespace lazily |
| 1541 | * (MNT_DETACH) and make our own. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1542 | */ |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1543 | if (umount2(kProcPath, MNT_DETACH)) { |
| 1544 | /* |
| 1545 | * If we are in a new user namespace, umount(2) will fail. |
| 1546 | * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html |
| 1547 | */ |
| 1548 | if (j->flags.userns) { |
| 1549 | info("umount(/proc, MNT_DETACH) failed, " |
| 1550 | "this is expected when using user namespaces"); |
| 1551 | } else { |
| 1552 | return -errno; |
| 1553 | } |
| 1554 | } |
Mike Frysinger | 3ba8157 | 2017-01-17 23:33:28 -0500 | [diff] [blame] | 1555 | if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1556 | return -errno; |
| 1557 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1558 | } |
| 1559 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1560 | static void kill_child_and_die(const struct minijail *j, const char *msg) |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1561 | { |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1562 | kill(j->initpid, SIGKILL); |
| 1563 | die("%s", msg); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1564 | } |
| 1565 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1566 | static void write_pid_file_or_die(const struct minijail *j) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1567 | { |
Keshav Santhanam | db6dab4 | 2016-08-10 16:33:34 -0700 | [diff] [blame] | 1568 | if (write_pid_to_path(j->initpid, j->pid_file_path)) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1569 | kill_child_and_die(j, "failed to write pid file"); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1570 | } |
| 1571 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1572 | static void add_to_cgroups_or_die(const struct minijail *j) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1573 | { |
| 1574 | size_t i; |
| 1575 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1576 | for (i = 0; i < j->cgroup_count; ++i) { |
Keshav Santhanam | db6dab4 | 2016-08-10 16:33:34 -0700 | [diff] [blame] | 1577 | if (write_pid_to_path(j->initpid, j->cgroups[i])) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1578 | kill_child_and_die(j, "failed to add to cgroups"); |
| 1579 | } |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1580 | } |
| 1581 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 1582 | static void set_rlimits_or_die(const struct minijail *j) |
| 1583 | { |
| 1584 | size_t i; |
| 1585 | |
| 1586 | for (i = 0; i < j->rlimit_count; ++i) { |
| 1587 | struct rlimit limit; |
| 1588 | limit.rlim_cur = j->rlimits[i].cur; |
| 1589 | limit.rlim_max = j->rlimits[i].max; |
| 1590 | if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL)) |
| 1591 | kill_child_and_die(j, "failed to set rlimit"); |
| 1592 | } |
| 1593 | } |
| 1594 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1595 | static void write_ugid_maps_or_die(const struct minijail *j) |
| 1596 | { |
| 1597 | if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0) |
| 1598 | kill_child_and_die(j, "failed to write uid_map"); |
Mike Frysinger | 6b190c0 | 2017-01-04 17:18:42 -0500 | [diff] [blame] | 1599 | if (j->gidmap && j->flags.disable_setgroups) { |
| 1600 | /* Older kernels might not have the /proc/<pid>/setgroups files. */ |
| 1601 | int ret = write_proc_file(j->initpid, "deny", "setgroups"); |
Mike Frysinger | eea841b | 2017-01-13 18:11:57 -0500 | [diff] [blame] | 1602 | if (ret != 0) { |
Mike Frysinger | 6b190c0 | 2017-01-04 17:18:42 -0500 | [diff] [blame] | 1603 | if (ret == -ENOENT) { |
| 1604 | /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */ |
| 1605 | warn("could not disable setgroups(2)"); |
| 1606 | } else |
| 1607 | kill_child_and_die(j, "failed to disable setgroups(2)"); |
| 1608 | } |
| 1609 | } |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1610 | if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0) |
| 1611 | kill_child_and_die(j, "failed to write gid_map"); |
| 1612 | } |
| 1613 | |
| 1614 | static void enter_user_namespace(const struct minijail *j) |
| 1615 | { |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 1616 | int uid = j->flags.uid ? j->uid : 0; |
| 1617 | int gid = j->flags.gid ? j->gid : 0; |
| 1618 | if (j->gidmap && setresgid(gid, gid, gid)) { |
| 1619 | pdie("user_namespaces: setresgid(%d, %d, %d) failed", gid, gid, |
| 1620 | gid); |
| 1621 | } |
| 1622 | if (j->uidmap && setresuid(uid, uid, uid)) { |
| 1623 | pdie("user_namespaces: setresuid(%d, %d, %d) failed", uid, uid, |
| 1624 | uid); |
| 1625 | } |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | static void parent_setup_complete(int *pipe_fds) |
| 1629 | { |
| 1630 | close(pipe_fds[0]); |
| 1631 | close(pipe_fds[1]); |
| 1632 | } |
| 1633 | |
| 1634 | /* |
| 1635 | * wait_for_parent_setup: Called by the child process to wait for any |
| 1636 | * further parent-side setup to complete before continuing. |
| 1637 | */ |
| 1638 | static void wait_for_parent_setup(int *pipe_fds) |
| 1639 | { |
| 1640 | char buf; |
| 1641 | |
| 1642 | close(pipe_fds[1]); |
| 1643 | |
| 1644 | /* Wait for parent to complete setup and close the pipe. */ |
| 1645 | if (read(pipe_fds[0], &buf, 1) != 0) |
| 1646 | die("failed to sync with parent"); |
| 1647 | close(pipe_fds[0]); |
| 1648 | } |
| 1649 | |
| 1650 | static void drop_ugid(const struct minijail *j) |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1651 | { |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1652 | if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids + |
| 1653 | j->flags.set_suppl_gids > 1) { |
Jorge Lucangeli Obes | 3454319 | 2017-01-11 16:07:57 -0500 | [diff] [blame] | 1654 | die("can only do one of inherit, keep, or set supplementary " |
| 1655 | "groups"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1656 | } |
| 1657 | |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1658 | if (j->flags.inherit_suppl_gids) { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1659 | if (initgroups(j->user, j->usergid)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1660 | pdie("initgroups(%s, %d) failed", j->user, j->usergid); |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1661 | } else if (j->flags.set_suppl_gids) { |
| 1662 | if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1663 | pdie("setgroups(suppl_gids) failed"); |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 1664 | } else if (!j->flags.keep_suppl_gids && !j->flags.disable_setgroups) { |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1665 | /* |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1666 | * Only attempt to clear supplementary groups if we are changing |
Luis Hector Chavez | 7132355 | 2017-09-05 09:17:22 -0700 | [diff] [blame] | 1667 | * users or groups, and if the caller did not request to disable |
| 1668 | * setgroups (used when entering a user namespace as a |
| 1669 | * non-privileged user). |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1670 | */ |
Jorge Lucangeli Obes | 2449956 | 2016-12-01 11:59:27 -0500 | [diff] [blame] | 1671 | if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1672 | pdie("setgroups(0, NULL) failed"); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1676 | pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1677 | |
| 1678 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1679 | pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1682 | static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap) |
| 1683 | { |
| 1684 | const uint64_t one = 1; |
| 1685 | unsigned int i; |
| 1686 | for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) { |
| 1687 | if (keep_mask & (one << i)) |
| 1688 | continue; |
| 1689 | if (prctl(PR_CAPBSET_DROP, i)) |
| 1690 | pdie("could not drop capability from bounding set"); |
| 1691 | } |
| 1692 | } |
| 1693 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1694 | static void drop_caps(const struct minijail *j, unsigned int last_valid_cap) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1695 | { |
Jorge Lucangeli Obes | 7ea269e | 2016-02-26 22:07:09 -0800 | [diff] [blame] | 1696 | if (!j->flags.use_caps) |
| 1697 | return; |
| 1698 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1699 | cap_t caps = cap_get_proc(); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1700 | cap_value_t flag[1]; |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1701 | const size_t ncaps = sizeof(j->caps) * 8; |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1702 | const uint64_t one = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1703 | unsigned int i; |
| 1704 | if (!caps) |
| 1705 | die("can't get process caps"); |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1706 | if (cap_clear(caps)) |
| 1707 | die("can't clear caps"); |
| 1708 | |
| 1709 | for (i = 0; i < ncaps && i <= last_valid_cap; ++i) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1710 | /* Keep CAP_SETPCAP for dropping bounding set bits. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1711 | if (i != CAP_SETPCAP && !(j->caps & (one << i))) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1712 | continue; |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1713 | flag[0] = i; |
| 1714 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1715 | die("can't add effective cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1716 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1717 | die("can't add permitted cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1718 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1719 | die("can't add inheritable cap"); |
| 1720 | } |
| 1721 | if (cap_set_proc(caps)) |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1722 | die("can't apply initial cleaned capset"); |
| 1723 | |
| 1724 | /* |
| 1725 | * Instead of dropping bounding set first, do it here in case |
| 1726 | * the caller had a more permissive bounding set which could |
| 1727 | * have been used above to raise a capability that wasn't already |
| 1728 | * present. This requires CAP_SETPCAP, so we raised/kept it above. |
| 1729 | */ |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1730 | drop_capbset(j->caps, last_valid_cap); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1731 | |
| 1732 | /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1733 | if ((j->caps & (one << CAP_SETPCAP)) == 0) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1734 | flag[0] = CAP_SETPCAP; |
| 1735 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR)) |
| 1736 | die("can't clear effective cap"); |
| 1737 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR)) |
| 1738 | die("can't clear permitted cap"); |
| 1739 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR)) |
| 1740 | die("can't clear inheritable cap"); |
| 1741 | } |
| 1742 | |
| 1743 | if (cap_set_proc(caps)) |
| 1744 | die("can't apply final cleaned capset"); |
| 1745 | |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1746 | /* |
| 1747 | * If ambient capabilities are supported, clear all capabilities first, |
| 1748 | * then raise the requested ones. |
| 1749 | */ |
| 1750 | if (j->flags.set_ambient_caps) { |
| 1751 | if (!cap_ambient_supported()) { |
| 1752 | pdie("ambient capabilities not supported"); |
| 1753 | } |
Jorge Lucangeli Obes | f6058c3 | 2017-04-26 10:26:59 -0400 | [diff] [blame] | 1754 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != |
| 1755 | 0) { |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1756 | pdie("can't clear ambient capabilities"); |
| 1757 | } |
| 1758 | |
| 1759 | for (i = 0; i < ncaps && i <= last_valid_cap; ++i) { |
| 1760 | if (!(j->caps & (one << i))) |
| 1761 | continue; |
| 1762 | |
| 1763 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, |
| 1764 | 0) != 0) { |
| 1765 | pdie("prctl(PR_CAP_AMBIENT, " |
| 1766 | "PR_CAP_AMBIENT_RAISE, %u) failed", |
| 1767 | i); |
| 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1772 | cap_free(caps); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1773 | } |
| 1774 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1775 | static void set_seccomp_filter(const struct minijail *j) |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1776 | { |
| 1777 | /* |
| 1778 | * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c> |
| 1779 | * in the kernel source tree for an explanation of the parameters. |
| 1780 | */ |
| 1781 | if (j->flags.no_new_privs) { |
| 1782 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 1783 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 1784 | } |
| 1785 | |
| 1786 | /* |
Jorge Lucangeli Obes | 2413f37 | 2016-04-06 18:43:10 -0700 | [diff] [blame] | 1787 | * Code running with ASan |
| 1788 | * (https://github.com/google/sanitizers/wiki/AddressSanitizer) |
| 1789 | * will make system calls not included in the syscall filter policy, |
| 1790 | * which will likely crash the program. Skip setting seccomp filter in |
| 1791 | * that case. |
| 1792 | * 'running_with_asan()' has no inputs and is completely defined at |
| 1793 | * build time, so this cannot be used by an attacker to skip setting |
| 1794 | * seccomp filter. |
| 1795 | */ |
| 1796 | if (j->flags.seccomp_filter && running_with_asan()) { |
| 1797 | warn("running with ASan, not setting seccomp filter"); |
| 1798 | return; |
| 1799 | } |
| 1800 | |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 1801 | if (j->flags.seccomp_filter) { |
| 1802 | if (j->flags.seccomp_filter_logging) { |
| 1803 | /* |
| 1804 | * If logging seccomp filter failures, |
| 1805 | * install the SIGSYS handler first. |
| 1806 | */ |
| 1807 | if (install_sigsys_handler()) |
| 1808 | pdie("failed to install SIGSYS handler"); |
| 1809 | warn("logging seccomp filter failures"); |
| 1810 | } else if (j->flags.seccomp_filter_tsync) { |
| 1811 | /* |
| 1812 | * If setting thread sync, |
| 1813 | * reset the SIGSYS signal handler so that |
| 1814 | * the entire thread group is killed. |
| 1815 | */ |
| 1816 | if (signal(SIGSYS, SIG_DFL) == SIG_ERR) |
| 1817 | pdie("failed to reset SIGSYS disposition"); |
| 1818 | info("reset SIGSYS disposition"); |
| 1819 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * Install the syscall filter. |
| 1824 | */ |
| 1825 | if (j->flags.seccomp_filter) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 1826 | if (j->flags.seccomp_filter_tsync) { |
| 1827 | if (sys_seccomp(SECCOMP_SET_MODE_FILTER, |
| 1828 | SECCOMP_FILTER_FLAG_TSYNC, |
| 1829 | j->filter_prog)) { |
| 1830 | pdie("seccomp(tsync) failed"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1831 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 1832 | } else { |
| 1833 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, |
| 1834 | j->filter_prog)) { |
| 1835 | pdie("prctl(seccomp_filter) failed"); |
| 1836 | } |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1837 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 1841 | static pid_t forward_pid = -1; |
| 1842 | |
| 1843 | static void forward_signal(__attribute__((unused)) int nr, |
| 1844 | __attribute__((unused)) siginfo_t *siginfo, |
| 1845 | __attribute__((unused)) void *void_context) |
| 1846 | { |
| 1847 | if (forward_pid != -1) { |
| 1848 | kill(forward_pid, nr); |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | static void install_signal_handlers(void) |
| 1853 | { |
| 1854 | struct sigaction act; |
| 1855 | |
| 1856 | memset(&act, 0, sizeof(act)); |
| 1857 | act.sa_sigaction = &forward_signal; |
| 1858 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
| 1859 | |
| 1860 | /* Handle all signals, except SIGCHLD. */ |
| 1861 | for (int nr = 1; nr < NSIG; nr++) { |
| 1862 | /* |
| 1863 | * We don't care if we get EINVAL: that just means that we |
| 1864 | * can't handle this signal, so let's skip it and continue. |
| 1865 | */ |
| 1866 | sigaction(nr, &act, NULL); |
| 1867 | } |
| 1868 | /* Reset SIGCHLD's handler. */ |
| 1869 | signal(SIGCHLD, SIG_DFL); |
| 1870 | |
| 1871 | /* Handle real-time signals. */ |
| 1872 | for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) { |
| 1873 | sigaction(nr, &act, NULL); |
| 1874 | } |
| 1875 | } |
| 1876 | |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 1877 | static const char *lookup_hook_name(minijail_hook_event_t event) |
| 1878 | { |
| 1879 | switch (event) { |
| 1880 | case MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS: |
| 1881 | return "pre-drop-caps"; |
| 1882 | case MINIJAIL_HOOK_EVENT_PRE_EXECVE: |
| 1883 | return "pre-execve"; |
Luis Hector Chavez | 64730af | 2017-09-13 13:18:59 -0700 | [diff] [blame] | 1884 | case MINIJAIL_HOOK_EVENT_PRE_CHROOT: |
| 1885 | return "pre-chroot"; |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 1886 | case MINIJAIL_HOOK_EVENT_MAX: |
| 1887 | /* |
| 1888 | * Adding this in favor of a default case to force the |
| 1889 | * compiler to error out if a new enum value is added. |
| 1890 | */ |
| 1891 | break; |
| 1892 | } |
| 1893 | return "unknown"; |
| 1894 | } |
| 1895 | |
| 1896 | static void run_hooks_or_die(const struct minijail *j, |
| 1897 | minijail_hook_event_t event) |
| 1898 | { |
| 1899 | int rc; |
| 1900 | int hook_index = 0; |
| 1901 | for (struct hook *c = j->hooks_head; c; c = c->next) { |
| 1902 | if (c->event != event) |
| 1903 | continue; |
| 1904 | rc = c->hook(c->payload); |
| 1905 | if (rc != 0) { |
| 1906 | errno = -rc; |
| 1907 | pdie("%s hook (index %d) failed", |
| 1908 | lookup_hook_name(event), hook_index); |
| 1909 | } |
| 1910 | /* Only increase the index within the same hook event type. */ |
| 1911 | ++hook_index; |
| 1912 | } |
| 1913 | } |
| 1914 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1915 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1916 | { |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1917 | /* |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1918 | * If we're dropping caps, get the last valid cap from /proc now, |
| 1919 | * since /proc can be unmounted before drop_caps() is called. |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1920 | */ |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1921 | unsigned int last_valid_cap = 0; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1922 | if (j->flags.capbset_drop || j->flags.use_caps) |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1923 | last_valid_cap = get_last_valid_cap(); |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1924 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1925 | if (j->flags.pids) |
| 1926 | die("tried to enter a pid-namespaced jail;" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1927 | " try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1928 | |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1929 | if (j->flags.inherit_suppl_gids && !j->user) |
Jorge Lucangeli Obes | 3454319 | 2017-01-11 16:07:57 -0500 | [diff] [blame] | 1930 | die("cannot inherit supplementary groups without setting a " |
| 1931 | "username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1932 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1933 | /* |
| 1934 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1935 | * so we don't even try. If any of our operations fail, we abort() the |
| 1936 | * entire process. |
| 1937 | */ |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1938 | if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1939 | pdie("setns(CLONE_NEWNS) failed"); |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1940 | |
Jorge Lucangeli Obes | 805be39 | 2015-10-12 15:55:59 -0700 | [diff] [blame] | 1941 | if (j->flags.vfs) { |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1942 | if (unshare(CLONE_NEWNS)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1943 | pdie("unshare(CLONE_NEWNS) failed"); |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1944 | /* |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1945 | * Unless asked not to, remount all filesystems as private. |
| 1946 | * If they are shared, new bind mounts will creep out of our |
| 1947 | * namespace. |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1948 | * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt |
| 1949 | */ |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1950 | if (!j->flags.skip_remount_private) { |
| 1951 | if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1952 | pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE," |
| 1953 | " NULL) failed"); |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1954 | } |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1955 | } |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 1956 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 1957 | if (j->flags.ipc && unshare(CLONE_NEWIPC)) { |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1958 | pdie("unshare(CLONE_NEWIPC) failed"); |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 1959 | } |
| 1960 | |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1961 | if (j->flags.uts) { |
| 1962 | if (unshare(CLONE_NEWUTS)) |
| 1963 | pdie("unshare(CLONE_NEWUTS) failed"); |
| 1964 | |
| 1965 | if (j->hostname && sethostname(j->hostname, strlen(j->hostname))) |
| 1966 | pdie("sethostname(%s) failed", j->hostname); |
| 1967 | } |
| 1968 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 1969 | if (j->flags.enter_net) { |
| 1970 | if (setns(j->netns_fd, CLONE_NEWNET)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1971 | pdie("setns(CLONE_NEWNET) failed"); |
Mike Frysinger | 7559dfe | 2016-11-15 18:58:39 -0500 | [diff] [blame] | 1972 | } else if (j->flags.net) { |
| 1973 | if (unshare(CLONE_NEWNET)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1974 | pdie("unshare(CLONE_NEWNET) failed"); |
| 1975 | config_net_loopback(); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 1976 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1977 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 1978 | if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1979 | pdie("unshare(CLONE_NEWCGROUP) failed"); |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 1980 | |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 1981 | if (j->flags.new_session_keyring) { |
| 1982 | if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0) |
| 1983 | pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed"); |
| 1984 | } |
| 1985 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1986 | /* We have to process all the mounts before we chroot/pivot_root. */ |
| 1987 | process_mounts_or_die(j); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1988 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1989 | if (j->flags.chroot && enter_chroot(j)) |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1990 | pdie("chroot"); |
Mike Frysinger | 33ffef3 | 2017-01-13 19:53:19 -0500 | [diff] [blame] | 1991 | |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 1992 | if (j->flags.pivot_root && enter_pivot_root(j)) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1993 | pdie("pivot_root"); |
| 1994 | |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1995 | if (j->flags.mount_tmp && mount_tmp(j)) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1996 | pdie("mount_tmp"); |
| 1997 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 1998 | if (j->flags.remount_proc_ro && remount_proc_readonly(j)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1999 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2000 | |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 2001 | run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS); |
| 2002 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 2003 | /* |
| 2004 | * If we're only dropping capabilities from the bounding set, but not |
| 2005 | * from the thread's (permitted|inheritable|effective) sets, do it now. |
| 2006 | */ |
| 2007 | if (j->flags.capbset_drop) { |
| 2008 | drop_capbset(j->cap_bset, last_valid_cap); |
| 2009 | } |
| 2010 | |
| 2011 | if (j->flags.use_caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2012 | /* |
| 2013 | * POSIX capabilities are a bit tricky. If we drop our |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2014 | * capability to change uids, our attempt to use setuid() |
| 2015 | * below will fail. Hang on to root caps across setuid(), then |
| 2016 | * lock securebits. |
| 2017 | */ |
| 2018 | if (prctl(PR_SET_KEEPCAPS, 1)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2019 | pdie("prctl(PR_SET_KEEPCAPS) failed"); |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 2020 | |
Luis Hector Chavez | ec0a2c1 | 2017-06-29 20:29:57 -0700 | [diff] [blame] | 2021 | if (lock_securebits(j->securebits_skip_mask) < 0) { |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 2022 | pdie("locking securebits failed"); |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 2023 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2024 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2025 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 2026 | if (j->flags.no_new_privs) { |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 2027 | /* |
| 2028 | * If we're setting no_new_privs, we can drop privileges |
| 2029 | * before setting seccomp filter. This way filter policies |
| 2030 | * don't need to allow privilege-dropping syscalls. |
| 2031 | */ |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2032 | drop_ugid(j); |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 2033 | drop_caps(j, last_valid_cap); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2034 | set_seccomp_filter(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2035 | } else { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2036 | /* |
| 2037 | * If we're not setting no_new_privs, |
| 2038 | * we need to set seccomp filter *before* dropping privileges. |
| 2039 | * WARNING: this means that filter policies *must* allow |
| 2040 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 2041 | * capget()/capset()/prctl() for dropping caps. |
| 2042 | */ |
| 2043 | set_seccomp_filter(j); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 2044 | drop_ugid(j); |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 2045 | drop_caps(j, last_valid_cap); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2046 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2047 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2048 | /* |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 2049 | * Select the specified alternate syscall table. The table must not |
| 2050 | * block prctl(2) if we're using seccomp as well. |
| 2051 | */ |
| 2052 | if (j->flags.alt_syscall) { |
| 2053 | if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2054 | pdie("prctl(PR_ALT_SYSCALL) failed"); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | /* |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2058 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2059 | * privilege-dropping syscalls :) |
| 2060 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 2061 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) { |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 2062 | if ((errno == EINVAL) && seccomp_can_softfail()) { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 2063 | warn("seccomp not supported"); |
| 2064 | return; |
| 2065 | } |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 2066 | pdie("prctl(PR_SET_SECCOMP) failed"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 2067 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2068 | } |
| 2069 | |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 2070 | /* TODO(wad): will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2071 | static int init_exitstatus = 0; |
| 2072 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2073 | void init_term(int __attribute__ ((unused)) sig) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2074 | { |
| 2075 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2076 | } |
| 2077 | |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2078 | void init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2079 | { |
| 2080 | pid_t pid; |
| 2081 | int status; |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 2082 | /* So that we exit with the right status. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2083 | signal(SIGTERM, init_term); |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 2084 | /* TODO(wad): self jail with seccomp filters here. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2085 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2086 | /* |
| 2087 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2088 | * left inside our pid namespace or we get a signal. |
| 2089 | */ |
| 2090 | if (pid == rootpid) |
| 2091 | init_exitstatus = status; |
| 2092 | } |
| 2093 | if (!WIFEXITED(init_exitstatus)) |
| 2094 | _exit(MINIJAIL_ERR_INIT); |
| 2095 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2096 | } |
| 2097 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2098 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2099 | { |
| 2100 | size_t sz = 0; |
| 2101 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 2102 | char *buf; |
| 2103 | int r; |
| 2104 | if (sizeof(sz) != bytes) |
| 2105 | return -EINVAL; |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2106 | if (sz > USHRT_MAX) /* arbitrary sanity check */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2107 | return -E2BIG; |
| 2108 | buf = malloc(sz); |
| 2109 | if (!buf) |
| 2110 | return -ENOMEM; |
| 2111 | bytes = read(fd, buf, sz); |
| 2112 | if (bytes != sz) { |
| 2113 | free(buf); |
| 2114 | return -EINVAL; |
| 2115 | } |
| 2116 | r = minijail_unmarshal(j, buf, sz); |
| 2117 | free(buf); |
| 2118 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 2119 | } |
| 2120 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2121 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2122 | { |
| 2123 | char *buf; |
| 2124 | size_t sz = minijail_size(j); |
| 2125 | ssize_t written; |
| 2126 | int r; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2127 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2128 | if (!sz) |
| 2129 | return -EINVAL; |
| 2130 | buf = malloc(sz); |
| 2131 | r = minijail_marshal(j, buf, sz); |
| 2132 | if (r) { |
| 2133 | free(buf); |
| 2134 | return r; |
| 2135 | } |
| 2136 | /* Sends [size][minijail]. */ |
| 2137 | written = write(fd, &sz, sizeof(sz)); |
| 2138 | if (written != sizeof(sz)) { |
| 2139 | free(buf); |
| 2140 | return -EFAULT; |
| 2141 | } |
| 2142 | written = write(fd, buf, sz); |
| 2143 | if (written < 0 || (size_t) written != sz) { |
| 2144 | free(buf); |
| 2145 | return -EFAULT; |
| 2146 | } |
| 2147 | free(buf); |
| 2148 | return 0; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 2149 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2150 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2151 | int setup_preload(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2152 | { |
Daniel Erat | 5b7a318 | 2015-08-19 16:06:22 -0600 | [diff] [blame] | 2153 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 2154 | /* Don't use LDPRELOAD on Android. */ |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 2155 | return 0; |
| 2156 | #else |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2157 | char *oldenv = getenv(kLdPreloadEnvVar) ? : ""; |
| 2158 | char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH)); |
| 2159 | if (!newenv) |
| 2160 | return -ENOMEM; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2161 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2162 | /* Only insert a separating space if we have something to separate... */ |
| 2163 | sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "", |
| 2164 | PRELOADPATH); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2165 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2166 | /* setenv() makes a copy of the string we give it. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2167 | setenv(kLdPreloadEnvVar, newenv, 1); |
| 2168 | free(newenv); |
| 2169 | return 0; |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 2170 | #endif |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2171 | } |
| 2172 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 2173 | static int setup_pipe(int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2174 | { |
| 2175 | int r = pipe(fds); |
| 2176 | char fd_buf[11]; |
| 2177 | if (r) |
| 2178 | return r; |
| 2179 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 2180 | if (r <= 0) |
| 2181 | return -EINVAL; |
| 2182 | setenv(kFdEnvVar, fd_buf, 1); |
| 2183 | return 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 2184 | } |
| 2185 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 2186 | static int close_open_fds(int *inheritable_fds, size_t size) |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2187 | { |
| 2188 | const char *kFdPath = "/proc/self/fd"; |
| 2189 | |
| 2190 | DIR *d = opendir(kFdPath); |
| 2191 | struct dirent *dir_entry; |
| 2192 | |
| 2193 | if (d == NULL) |
| 2194 | return -1; |
| 2195 | int dir_fd = dirfd(d); |
| 2196 | while ((dir_entry = readdir(d)) != NULL) { |
| 2197 | size_t i; |
| 2198 | char *end; |
| 2199 | bool should_close = true; |
| 2200 | const int fd = strtol(dir_entry->d_name, &end, 10); |
| 2201 | |
| 2202 | if ((*end) != '\0') { |
| 2203 | continue; |
| 2204 | } |
| 2205 | /* |
| 2206 | * We might have set up some pipes that we want to share with |
| 2207 | * the parent process, and should not be closed. |
| 2208 | */ |
| 2209 | for (i = 0; i < size; ++i) { |
| 2210 | if (fd == inheritable_fds[i]) { |
| 2211 | should_close = false; |
| 2212 | break; |
| 2213 | } |
| 2214 | } |
| 2215 | /* Also avoid closing the directory fd. */ |
| 2216 | if (should_close && fd != dir_fd) |
| 2217 | close(fd); |
| 2218 | } |
| 2219 | closedir(d); |
| 2220 | return 0; |
| 2221 | } |
| 2222 | |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 2223 | static int redirect_fds(struct minijail *j) |
| 2224 | { |
| 2225 | size_t i, i2; |
| 2226 | int closeable; |
| 2227 | for (i = 0; i < j->preserved_fd_count; i++) { |
| 2228 | if (dup2(j->preserved_fds[i].parent_fd, |
| 2229 | j->preserved_fds[i].child_fd) == -1) { |
| 2230 | return -1; |
| 2231 | } |
| 2232 | } |
| 2233 | /* |
| 2234 | * After all fds have been duped, we are now free to close all parent |
| 2235 | * fds that are *not* child fds. |
| 2236 | */ |
| 2237 | for (i = 0; i < j->preserved_fd_count; i++) { |
| 2238 | closeable = true; |
| 2239 | for (i2 = 0; i2 < j->preserved_fd_count; i2++) { |
| 2240 | closeable &= j->preserved_fds[i].parent_fd != |
| 2241 | j->preserved_fds[i2].child_fd; |
| 2242 | } |
| 2243 | if (closeable) |
| 2244 | close(j->preserved_fds[i].parent_fd); |
| 2245 | } |
| 2246 | return 0; |
| 2247 | } |
| 2248 | |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2249 | /* |
| 2250 | * Structure that specifies how to start a minijail. |
| 2251 | * |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2252 | * filename - The program to exec in the child. Required if `exec_in_child` = 1. |
| 2253 | * argv - Arguments for the child program. Required if `exec_in_child` = 1. |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2254 | * use_preload - If true use LD_PRELOAD. |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2255 | * exec_in_child - If true, run `filename`. Otherwise, the child will return to |
| 2256 | * the caller. |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2257 | */ |
| 2258 | struct minijail_run_config { |
| 2259 | const char *filename; |
| 2260 | char *const *argv; |
| 2261 | int use_preload; |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2262 | int exec_in_child; |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2263 | }; |
| 2264 | |
| 2265 | /* |
| 2266 | * Set of pointers to fill with values from minijail_run. |
| 2267 | * All arguments are allowed to be NULL if unused. |
| 2268 | * |
| 2269 | * pstdin_fd - Filled with stdin pipe if non-NULL. |
| 2270 | * pstdout_fd - Filled with stdout pipe if non-NULL. |
| 2271 | * pstderr_fd - Filled with stderr pipe if non-NULL. |
| 2272 | * pchild_pid - Filled with the pid of the child process if non-NULL. |
| 2273 | */ |
| 2274 | struct minijail_run_status { |
| 2275 | int *pstdin_fd; |
| 2276 | int *pstdout_fd; |
| 2277 | int *pstderr_fd; |
| 2278 | pid_t *pchild_pid; |
| 2279 | }; |
| 2280 | |
Dylan Reid | 18c49c8 | 2017-08-25 14:52:27 -0700 | [diff] [blame] | 2281 | static int minijail_run_internal(struct minijail *j, |
| 2282 | const struct minijail_run_config *config, |
| 2283 | struct minijail_run_status *status_out); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2284 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2285 | int API minijail_run(struct minijail *j, const char *filename, |
| 2286 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2287 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2288 | struct minijail_run_config config = { |
| 2289 | .filename = filename, |
| 2290 | .argv = argv, |
| 2291 | .use_preload = true, |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2292 | .exec_in_child = true, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2293 | }; |
| 2294 | struct minijail_run_status status = {}; |
| 2295 | return minijail_run_internal(j, &config, &status); |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 2299 | char *const argv[], pid_t *pchild_pid) |
| 2300 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2301 | struct minijail_run_config config = { |
| 2302 | .filename = filename, |
| 2303 | .argv = argv, |
| 2304 | .use_preload = true, |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2305 | .exec_in_child = true, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2306 | }; |
| 2307 | struct minijail_run_status status = { |
| 2308 | .pchild_pid = pchild_pid, |
| 2309 | }; |
| 2310 | return minijail_run_internal(j, &config, &status); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | int API minijail_run_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 2314 | char *const argv[], int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2315 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2316 | struct minijail_run_config config = { |
| 2317 | .filename = filename, |
| 2318 | .argv = argv, |
| 2319 | .use_preload = true, |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2320 | .exec_in_child = true, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2321 | }; |
| 2322 | struct minijail_run_status status = { |
| 2323 | .pstdin_fd = pstdin_fd, |
| 2324 | }; |
| 2325 | return minijail_run_internal(j, &config, &status); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2326 | } |
| 2327 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2328 | 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] | 2329 | char *const argv[], pid_t *pchild_pid, |
| 2330 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2331 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2332 | struct minijail_run_config config = { |
| 2333 | .filename = filename, |
| 2334 | .argv = argv, |
| 2335 | .use_preload = true, |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2336 | .exec_in_child = true, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2337 | }; |
| 2338 | struct minijail_run_status status = { |
| 2339 | .pstdin_fd = pstdin_fd, |
| 2340 | .pstdout_fd = pstdout_fd, |
| 2341 | .pstderr_fd = pstderr_fd, |
| 2342 | .pchild_pid = pchild_pid, |
| 2343 | }; |
| 2344 | return minijail_run_internal(j, &config, &status); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | int API minijail_run_no_preload(struct minijail *j, const char *filename, |
| 2348 | char *const argv[]) |
| 2349 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2350 | struct minijail_run_config config = { |
| 2351 | .filename = filename, |
| 2352 | .argv = argv, |
| 2353 | .use_preload = false, |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2354 | .exec_in_child = true, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2355 | }; |
| 2356 | struct minijail_run_status status = {}; |
| 2357 | return minijail_run_internal(j, &config, &status); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 2360 | int API minijail_run_pid_pipes_no_preload(struct minijail *j, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 2361 | const char *filename, |
| 2362 | char *const argv[], |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 2363 | pid_t *pchild_pid, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2364 | int *pstdin_fd, |
| 2365 | int *pstdout_fd, |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 2366 | int *pstderr_fd) |
| 2367 | { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2368 | struct minijail_run_config config = { |
| 2369 | .filename = filename, |
| 2370 | .argv = argv, |
| 2371 | .use_preload = false, |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2372 | .exec_in_child = true, |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2373 | }; |
| 2374 | struct minijail_run_status status = { |
| 2375 | .pstdin_fd = pstdin_fd, |
| 2376 | .pstdout_fd = pstdout_fd, |
| 2377 | .pstderr_fd = pstderr_fd, |
| 2378 | .pchild_pid = pchild_pid, |
| 2379 | }; |
| 2380 | return minijail_run_internal(j, &config, &status); |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2383 | pid_t API minijail_fork(struct minijail *j) |
| 2384 | { |
| 2385 | struct minijail_run_config config = {}; |
| 2386 | struct minijail_run_status status = {}; |
| 2387 | return minijail_run_internal(j, &config, &status); |
| 2388 | } |
| 2389 | |
Dylan Reid | 18c49c8 | 2017-08-25 14:52:27 -0700 | [diff] [blame] | 2390 | static int minijail_run_internal(struct minijail *j, |
| 2391 | const struct minijail_run_config *config, |
| 2392 | struct minijail_run_status *status_out) |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2393 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2394 | char *oldenv, *oldenv_copy = NULL; |
| 2395 | pid_t child_pid; |
| 2396 | int pipe_fds[2]; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2397 | int stdin_fds[2]; |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2398 | int stdout_fds[2]; |
| 2399 | int stderr_fds[2]; |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2400 | int child_sync_pipe_fds[2]; |
| 2401 | int sync_child = 0; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2402 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 2403 | /* We need to remember this across the minijail_preexec() call. */ |
| 2404 | int pid_namespace = j->flags.pids; |
Luis Hector Chavez | ac981fc | 2017-09-18 15:52:38 -0700 | [diff] [blame] | 2405 | /* |
| 2406 | * Create an init process if we are entering a pid namespace, unless the |
| 2407 | * user has explicitly opted out by calling minijail_run_as_init(). |
| 2408 | */ |
| 2409 | int do_init = j->flags.do_init && !j->flags.run_as_init; |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2410 | int use_preload = config->use_preload; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 2411 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2412 | if (use_preload) { |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2413 | if (j->hooks_head != NULL) |
| 2414 | die("Minijail hooks are not supported with LD_PRELOAD"); |
| 2415 | if (!config->exec_in_child) |
| 2416 | die("minijail_fork is not supported with LD_PRELOAD"); |
| 2417 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2418 | oldenv = getenv(kLdPreloadEnvVar); |
| 2419 | if (oldenv) { |
| 2420 | oldenv_copy = strdup(oldenv); |
| 2421 | if (!oldenv_copy) |
| 2422 | return -ENOMEM; |
| 2423 | } |
| 2424 | |
| 2425 | if (setup_preload()) |
| 2426 | return -EFAULT; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2427 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 2428 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2429 | if (!use_preload) { |
Luis Hector Chavez | fe5fb8e | 2017-06-29 10:41:27 -0700 | [diff] [blame] | 2430 | if (j->flags.use_caps && j->caps != 0 && |
| 2431 | !j->flags.set_ambient_caps) { |
| 2432 | die("non-empty, non-ambient capabilities are not " |
| 2433 | "supported without LD_PRELOAD"); |
| 2434 | } |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2435 | } |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 2436 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2437 | if (use_preload) { |
| 2438 | /* |
| 2439 | * Before we fork(2) and execve(2) the child process, we need |
| 2440 | * to open a pipe(2) to send the minijail configuration over. |
| 2441 | */ |
| 2442 | if (setup_pipe(pipe_fds)) |
| 2443 | return -EFAULT; |
| 2444 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2445 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2446 | /* |
| 2447 | * If we want to write to the child process' standard input, |
| 2448 | * create the pipe(2) now. |
| 2449 | */ |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2450 | if (status_out->pstdin_fd) { |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2451 | if (pipe(stdin_fds)) |
| 2452 | return -EFAULT; |
| 2453 | } |
| 2454 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2455 | /* |
| 2456 | * If we want to read from the child process' standard output, |
| 2457 | * create the pipe(2) now. |
| 2458 | */ |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2459 | if (status_out->pstdout_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2460 | if (pipe(stdout_fds)) |
| 2461 | return -EFAULT; |
| 2462 | } |
| 2463 | |
| 2464 | /* |
| 2465 | * If we want to read from the child process' standard error, |
| 2466 | * create the pipe(2) now. |
| 2467 | */ |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2468 | if (status_out->pstderr_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2469 | if (pipe(stderr_fds)) |
| 2470 | return -EFAULT; |
| 2471 | } |
| 2472 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2473 | /* |
Jorge Lucangeli Obes | ab6fa6f | 2016-08-04 15:42:48 -0400 | [diff] [blame] | 2474 | * If we want to set up a new uid/gid map in the user namespace, |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 2475 | * or if we need to add the child process to cgroups, create the pipe(2) |
| 2476 | * to sync between parent and child. |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2477 | */ |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 2478 | if (j->flags.userns || j->flags.cgroups) { |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2479 | sync_child = 1; |
| 2480 | if (pipe(child_sync_pipe_fds)) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2481 | return -EFAULT; |
| 2482 | } |
| 2483 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 2484 | /* |
| 2485 | * 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] | 2486 | * |
| 2487 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 2488 | * |
| 2489 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 2490 | * some of which may be held by other threads at the time that we call |
| 2491 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 2492 | * ensure that we hold all of these locks before it calls clone() |
| 2493 | * internally and drop them after clone() returns, but when we call |
| 2494 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 2495 | * child address space where some of libc's important locks are held by |
| 2496 | * other threads (which did not get cloned, and hence will never release |
| 2497 | * those locks). This is okay so long as we call exec() immediately |
| 2498 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 2499 | * take locks. |
| 2500 | * |
| 2501 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 2502 | * namespacing. If we follow this path, the child's address space might |
| 2503 | * have broken locks; you may only call functions that do not acquire |
| 2504 | * any locks. |
| 2505 | * |
| 2506 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 2507 | * previously detailed, so this function is highly likely to deadlock |
| 2508 | * later on (see "deadlock here") if we're multithreaded. |
| 2509 | * |
| 2510 | * We might hack around this by having the clone()d child (init of the |
| 2511 | * pid namespace) return directly, rather than leaving the clone()d |
| 2512 | * 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] | 2513 | * its fork()ed child return in turn), but that process would be |
| 2514 | * crippled with its libc locks potentially broken. We might try |
| 2515 | * fork()ing in the parent before we clone() to ensure that we own all |
| 2516 | * the locks, but then we have to have the forked child hanging around |
| 2517 | * consuming resources (and possibly having file descriptors / shared |
| 2518 | * memory regions / etc attached). We'd need to keep the child around to |
| 2519 | * avoid having its children get reparented to init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2520 | * |
| 2521 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 2522 | * problem is fixable or not. It would be nice if we worked in this |
| 2523 | * case. |
| 2524 | */ |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2525 | if (pid_namespace) { |
| 2526 | int clone_flags = CLONE_NEWPID | SIGCHLD; |
| 2527 | if (j->flags.userns) |
| 2528 | clone_flags |= CLONE_NEWUSER; |
| 2529 | child_pid = syscall(SYS_clone, clone_flags, NULL); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2530 | } else { |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2531 | child_pid = fork(); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2532 | } |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2533 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2534 | if (child_pid < 0) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2535 | if (use_preload) { |
| 2536 | free(oldenv_copy); |
| 2537 | } |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 2538 | die("failed to fork child"); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2539 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 2540 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2541 | if (child_pid) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2542 | if (use_preload) { |
| 2543 | /* Restore parent's LD_PRELOAD. */ |
| 2544 | if (oldenv_copy) { |
| 2545 | setenv(kLdPreloadEnvVar, oldenv_copy, 1); |
| 2546 | free(oldenv_copy); |
| 2547 | } else { |
| 2548 | unsetenv(kLdPreloadEnvVar); |
| 2549 | } |
| 2550 | unsetenv(kFdEnvVar); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2551 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2552 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2553 | j->initpid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2554 | |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 2555 | if (j->flags.forward_signals) { |
| 2556 | forward_pid = child_pid; |
| 2557 | install_signal_handlers(); |
| 2558 | } |
| 2559 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 2560 | if (j->flags.pid_file) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2561 | write_pid_file_or_die(j); |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 2562 | |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 2563 | if (j->flags.cgroups) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2564 | add_to_cgroups_or_die(j); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 2565 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 2566 | if (j->rlimit_count) |
| 2567 | set_rlimits_or_die(j); |
| 2568 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2569 | if (j->flags.userns) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2570 | write_ugid_maps_or_die(j); |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2571 | |
| 2572 | if (sync_child) |
| 2573 | parent_setup_complete(child_sync_pipe_fds); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2574 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2575 | if (use_preload) { |
| 2576 | /* Send marshalled minijail. */ |
| 2577 | close(pipe_fds[0]); /* read endpoint */ |
| 2578 | ret = minijail_to_fd(j, pipe_fds[1]); |
| 2579 | close(pipe_fds[1]); /* write endpoint */ |
| 2580 | if (ret) { |
| 2581 | kill(j->initpid, SIGKILL); |
| 2582 | die("failed to send marshalled minijail"); |
| 2583 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2584 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2585 | |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2586 | if (status_out->pchild_pid) |
| 2587 | *status_out->pchild_pid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2588 | |
| 2589 | /* |
| 2590 | * If we want to write to the child process' standard input, |
| 2591 | * set up the write end of the pipe. |
| 2592 | */ |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2593 | if (status_out->pstdin_fd) |
| 2594 | *status_out->pstdin_fd = |
| 2595 | setup_pipe_end(stdin_fds, 1 /* write end */); |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2596 | |
| 2597 | /* |
| 2598 | * If we want to read from the child process' standard output, |
| 2599 | * set up the read end of the pipe. |
| 2600 | */ |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2601 | if (status_out->pstdout_fd) |
| 2602 | *status_out->pstdout_fd = |
| 2603 | setup_pipe_end(stdout_fds, 0 /* read end */); |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2604 | |
| 2605 | /* |
| 2606 | * If we want to read from the child process' standard error, |
| 2607 | * set up the read end of the pipe. |
| 2608 | */ |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2609 | if (status_out->pstderr_fd) |
| 2610 | *status_out->pstderr_fd = |
| 2611 | setup_pipe_end(stderr_fds, 0 /* read end */); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2612 | |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2613 | /* |
| 2614 | * If forking return the child pid, in the normal exec case |
| 2615 | * return 0 for success. |
| 2616 | */ |
| 2617 | if (!config->exec_in_child) |
| 2618 | return child_pid; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2619 | return 0; |
| 2620 | } |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2621 | /* Child process. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2622 | free(oldenv_copy); |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 2623 | |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 2624 | if (j->flags.reset_signal_mask) { |
| 2625 | sigset_t signal_mask; |
| 2626 | if (sigemptyset(&signal_mask) != 0) |
| 2627 | pdie("sigemptyset failed"); |
| 2628 | if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0) |
| 2629 | pdie("sigprocmask failed"); |
| 2630 | } |
| 2631 | |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2632 | if (j->flags.close_open_fds) { |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 2633 | const size_t kMaxInheritableFdsSize = 10 + MAX_PRESERVED_FDS; |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2634 | int inheritable_fds[kMaxInheritableFdsSize]; |
| 2635 | size_t size = 0; |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 2636 | size_t i; |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2637 | if (use_preload) { |
| 2638 | inheritable_fds[size++] = pipe_fds[0]; |
| 2639 | inheritable_fds[size++] = pipe_fds[1]; |
| 2640 | } |
| 2641 | if (sync_child) { |
| 2642 | inheritable_fds[size++] = child_sync_pipe_fds[0]; |
| 2643 | inheritable_fds[size++] = child_sync_pipe_fds[1]; |
| 2644 | } |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2645 | if (status_out->pstdin_fd) { |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2646 | inheritable_fds[size++] = stdin_fds[0]; |
| 2647 | inheritable_fds[size++] = stdin_fds[1]; |
| 2648 | } |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2649 | if (status_out->pstdout_fd) { |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2650 | inheritable_fds[size++] = stdout_fds[0]; |
| 2651 | inheritable_fds[size++] = stdout_fds[1]; |
| 2652 | } |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2653 | if (status_out->pstderr_fd) { |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2654 | inheritable_fds[size++] = stderr_fds[0]; |
| 2655 | inheritable_fds[size++] = stderr_fds[1]; |
| 2656 | } |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 2657 | for (i = 0; i < j->preserved_fd_count; i++) { |
| 2658 | /* |
| 2659 | * Preserve all parent_fds. They will be dup2(2)-ed in |
| 2660 | * the child later. |
| 2661 | */ |
| 2662 | inheritable_fds[size++] = j->preserved_fds[i].parent_fd; |
| 2663 | } |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2664 | |
| 2665 | if (close_open_fds(inheritable_fds, size) < 0) |
| 2666 | die("failed to close open file descriptors"); |
| 2667 | } |
| 2668 | |
Luis Hector Chavez | 1617f63 | 2017-08-01 18:32:30 -0700 | [diff] [blame] | 2669 | if (redirect_fds(j)) |
| 2670 | die("failed to set up fd redirections"); |
| 2671 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2672 | if (sync_child) |
| 2673 | wait_for_parent_setup(child_sync_pipe_fds); |
| 2674 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2675 | if (j->flags.userns) |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2676 | enter_user_namespace(j); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2677 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2678 | /* |
| 2679 | * If we want to write to the jailed process' standard input, |
| 2680 | * set up the read end of the pipe. |
| 2681 | */ |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2682 | if (status_out->pstdin_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2683 | if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */, |
| 2684 | STDIN_FILENO) < 0) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2685 | die("failed to set up stdin pipe"); |
| 2686 | } |
| 2687 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2688 | /* |
| 2689 | * If we want to read from the jailed process' standard output, |
| 2690 | * set up the write end of the pipe. |
| 2691 | */ |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2692 | if (status_out->pstdout_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2693 | if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */, |
| 2694 | STDOUT_FILENO) < 0) |
| 2695 | die("failed to set up stdout pipe"); |
| 2696 | } |
| 2697 | |
| 2698 | /* |
| 2699 | * If we want to read from the jailed process' standard error, |
| 2700 | * set up the write end of the pipe. |
| 2701 | */ |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2702 | if (status_out->pstderr_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2703 | if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */, |
| 2704 | STDERR_FILENO) < 0) |
| 2705 | die("failed to set up stderr pipe"); |
| 2706 | } |
| 2707 | |
Jorge Lucangeli Obes | aa235b9 | 2016-11-23 13:48:15 -0500 | [diff] [blame] | 2708 | /* |
| 2709 | * If any of stdin, stdout, or stderr are TTYs, create a new session. |
| 2710 | * This prevents the jailed process from using the TIOCSTI ioctl |
| 2711 | * to push characters into the parent process terminal's input buffer, |
| 2712 | * therefore escaping the jail. |
Stephen Barber | 5dd5b1b | 2017-10-16 23:02:39 -0700 | [diff] [blame] | 2713 | * |
| 2714 | * Since it has just forked, the child will not be a process group |
| 2715 | * leader, and this call to setsid() should always succeed. |
Jorge Lucangeli Obes | aa235b9 | 2016-11-23 13:48:15 -0500 | [diff] [blame] | 2716 | */ |
| 2717 | if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || |
| 2718 | isatty(STDERR_FILENO)) { |
| 2719 | if (setsid() < 0) { |
| 2720 | pdie("setsid() failed"); |
| 2721 | } |
| 2722 | } |
| 2723 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 2724 | /* If running an init program, let it decide when/how to mount /proc. */ |
| 2725 | if (pid_namespace && !do_init) |
| 2726 | j->flags.remount_proc_ro = 0; |
| 2727 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2728 | if (use_preload) { |
| 2729 | /* Strip out flags that cannot be inherited across execve(2). */ |
| 2730 | minijail_preexec(j); |
| 2731 | } else { |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2732 | /* |
| 2733 | * If not using LD_PRELOAD, do all jailing before execve(2). |
| 2734 | * Note that PID namespaces can only be entered on fork(2), |
| 2735 | * so that flag is still cleared. |
| 2736 | */ |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2737 | j->flags.pids = 0; |
| 2738 | } |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2739 | |
| 2740 | /* |
| 2741 | * Jail this process. |
| 2742 | * If forking, return. |
| 2743 | * If not, execve(2) the target. |
| 2744 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2745 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2746 | |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2747 | if (config->exec_in_child && pid_namespace && do_init) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2748 | /* |
| 2749 | * pid namespace: this process will become init inside the new |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 2750 | * 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] | 2751 | * 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] | 2752 | * a child to actually run the program. If |do_init == 0|, we |
| 2753 | * let the program keep pid 1 and be init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2754 | * |
| 2755 | * If we're multithreaded, we'll probably deadlock here. See |
| 2756 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2757 | */ |
| 2758 | child_pid = fork(); |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2759 | if (child_pid < 0) { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2760 | _exit(child_pid); |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2761 | } else if (child_pid > 0) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 2762 | /* |
| 2763 | * Best effort. Don't bother checking the return value. |
| 2764 | */ |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2765 | prctl(PR_SET_NAME, "minijail-init"); |
| 2766 | init(child_pid); /* Never returns. */ |
| 2767 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2768 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2769 | |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 2770 | run_hooks_or_die(j, MINIJAIL_HOOK_EVENT_PRE_EXECVE); |
| 2771 | |
Dylan Reid | 0412dcc | 2017-08-24 11:33:15 -0700 | [diff] [blame] | 2772 | if (!config->exec_in_child) |
| 2773 | return 0; |
| 2774 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2775 | /* |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2776 | * 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] | 2777 | * calling process |
| 2778 | * -> execve()-ing process |
| 2779 | * If we are: |
| 2780 | * calling process |
| 2781 | * -> init()-ing process |
| 2782 | * -> execve()-ing process |
| 2783 | */ |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2784 | ret = execve(config->filename, config->argv, environ); |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2785 | if (ret == -1) { |
Dylan Reid | acfb8be | 2017-08-25 12:56:51 -0700 | [diff] [blame] | 2786 | pwarn("execve(%s) failed", config->filename); |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2787 | } |
| 2788 | _exit(ret); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2789 | } |
| 2790 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2791 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2792 | { |
| 2793 | int st; |
| 2794 | if (kill(j->initpid, SIGTERM)) |
| 2795 | return -errno; |
| 2796 | if (waitpid(j->initpid, &st, 0) < 0) |
| 2797 | return -errno; |
| 2798 | return st; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2799 | } |
| 2800 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2801 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2802 | { |
| 2803 | int st; |
| 2804 | if (waitpid(j->initpid, &st, 0) < 0) |
| 2805 | return -errno; |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 2806 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 2807 | if (!WIFEXITED(st)) { |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 2808 | int error_status = st; |
| 2809 | if (WIFSIGNALED(st)) { |
| 2810 | int signum = WTERMSIG(st); |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 2811 | warn("child process %d received signal %d", |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 2812 | j->initpid, signum); |
| 2813 | /* |
| 2814 | * We return MINIJAIL_ERR_JAIL if the process received |
| 2815 | * SIGSYS, which happens when a syscall is blocked by |
| 2816 | * seccomp filters. |
| 2817 | * If not, we do what bash(1) does: |
| 2818 | * $? = 128 + signum |
| 2819 | */ |
| 2820 | if (signum == SIGSYS) { |
| 2821 | error_status = MINIJAIL_ERR_JAIL; |
| 2822 | } else { |
| 2823 | error_status = 128 + signum; |
| 2824 | } |
| 2825 | } |
| 2826 | return error_status; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 2827 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 2828 | |
| 2829 | int exit_status = WEXITSTATUS(st); |
| 2830 | if (exit_status != 0) |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 2831 | info("child process %d exited with status %d", |
| 2832 | j->initpid, exit_status); |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 2833 | |
| 2834 | return exit_status; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2835 | } |
| 2836 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2837 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2838 | { |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 2839 | size_t i; |
| 2840 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 2841 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 2842 | free(j->filter_prog->filter); |
| 2843 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2844 | } |
Mike Frysinger | ac08a68 | 2017-10-10 02:04:50 -0400 | [diff] [blame] | 2845 | free_mounts_list(j); |
Luis Hector Chavez | e0ba4ce | 2017-07-20 15:12:22 -0700 | [diff] [blame] | 2846 | while (j->hooks_head) { |
| 2847 | struct hook *c = j->hooks_head; |
| 2848 | j->hooks_head = c->next; |
| 2849 | free(c); |
| 2850 | } |
| 2851 | j->hooks_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2852 | if (j->user) |
| 2853 | free(j->user); |
Jorge Lucangeli Obes | e81a52f | 2015-12-04 16:05:23 -0800 | [diff] [blame] | 2854 | if (j->suppl_gid_list) |
| 2855 | free(j->suppl_gid_list); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 2856 | if (j->chrootdir) |
| 2857 | free(j->chrootdir); |
Jorge Lucangeli Obes | 3b2e6e4 | 2016-08-04 12:26:19 -0400 | [diff] [blame] | 2858 | if (j->pid_file_path) |
| 2859 | free(j->pid_file_path); |
| 2860 | if (j->uidmap) |
| 2861 | free(j->uidmap); |
| 2862 | if (j->gidmap) |
| 2863 | free(j->gidmap); |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 2864 | if (j->hostname) |
| 2865 | free(j->hostname); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 2866 | if (j->alt_syscall_table) |
| 2867 | free(j->alt_syscall_table); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 2868 | for (i = 0; i < j->cgroup_count; ++i) |
| 2869 | free(j->cgroups[i]); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2870 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2871 | } |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 2872 | |
| 2873 | void API minijail_log_to_fd(int fd, int min_priority) |
| 2874 | { |
| 2875 | init_logging(LOG_TO_FD, fd, min_priority); |
| 2876 | } |