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