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