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