Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1 | /* Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | */ |
| 5 | |
| 6 | #define _GNU_SOURCE /* For asprintf */ |
| 7 | |
| 8 | #include <errno.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <malloc.h> |
| 11 | #include <signal.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <sys/mount.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <sys/types.h> |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 18 | #include <sys/wait.h> |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 21 | #include <linux/loop.h> |
| 22 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 23 | #include "container_cgroup.h" |
| 24 | #include "libcontainer.h" |
| 25 | #include "libminijail.h" |
| 26 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 27 | #define FREE_AND_NULL(ptr) \ |
| 28 | do { \ |
| 29 | free(ptr); \ |
| 30 | ptr = NULL; \ |
| 31 | } while(0) |
| 32 | |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 33 | #define MAX_NUM_SETFILES_ARGS 128 |
| 34 | |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 35 | static const char loopdev_ctl[] = "/dev/loop-control"; |
| 36 | |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 37 | static int container_teardown(struct container *c); |
| 38 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 39 | static int strdup_and_free(char **dest, const char *src) |
| 40 | { |
| 41 | char *copy = strdup(src); |
| 42 | if (!copy) |
| 43 | return -ENOMEM; |
| 44 | if (*dest) |
| 45 | free(*dest); |
| 46 | *dest = copy; |
| 47 | return 0; |
| 48 | } |
| 49 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 50 | struct container_mount { |
| 51 | char *name; |
| 52 | char *source; |
| 53 | char *destination; |
| 54 | char *type; |
| 55 | char *data; |
| 56 | int flags; |
| 57 | int uid; |
| 58 | int gid; |
| 59 | int mode; |
| 60 | int mount_in_ns; /* True if mount should happen in new vfs ns */ |
| 61 | int create; /* True if target should be created if it doesn't exist */ |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 62 | int loopback; /* True if target should be mounted via loopback */ |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | struct container_device { |
| 66 | char type; /* 'c' or 'b' for char or block */ |
| 67 | char *path; |
| 68 | int fs_permissions; |
| 69 | int major; |
| 70 | int minor; |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 71 | int copy_minor; /* Copy the minor from existing node, ignores |minor| */ |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 72 | int uid; |
| 73 | int gid; |
| 74 | int read_allowed; |
| 75 | int write_allowed; |
| 76 | int modify_allowed; |
| 77 | }; |
| 78 | |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 79 | struct container_cpu_cgroup { |
| 80 | int shares; |
| 81 | int quota; |
| 82 | int period; |
| 83 | int rt_runtime; |
| 84 | int rt_period; |
| 85 | }; |
| 86 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 87 | /* |
| 88 | * Structure that configures how the container is run. |
| 89 | * |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame^] | 90 | * config_root - Path to the root of the container itself. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 91 | * rootfs - Path to the root of the container's filesystem. |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 92 | * rootfs_mount_flags - Flags that will be passed to mount() for the rootfs. |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 93 | * premounted_runfs - Path to where the container will be run. |
| 94 | * pid_file_path - Path to the file where the pid should be written. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 95 | * program_argv - The program to run and args, e.g. "/sbin/init". |
| 96 | * num_args - Number of args in program_argv. |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 97 | * uid - The uid the container will run as. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 98 | * uid_map - Mapping of UIDs in the container, e.g. "0 100000 1024" |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 99 | * gid - The gid the container will run as. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 100 | * gid_map - Mapping of GIDs in the container, e.g. "0 100000 1024" |
| 101 | * alt_syscall_table - Syscall table to use or NULL if none. |
| 102 | * mounts - Filesystems to mount in the new namespace. |
| 103 | * num_mounts - Number of above. |
| 104 | * devices - Device nodes to create. |
| 105 | * num_devices - Number of above. |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 106 | * run_setfiles - Should run setfiles on mounts to enable selinux. |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 107 | * cpu_cgparams - CPU cgroup params. |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 108 | * cgroup_parent - Parent dir for cgroup creation |
| 109 | * cgroup_owner - uid to own the created cgroups |
Dmitry Torokhov | 14eef72 | 2016-09-27 16:40:37 -0700 | [diff] [blame] | 110 | * cgroup_group - gid to own the created cgroups |
Keshav Santhanam | 1b6bf67 | 2016-08-10 18:35:12 -0700 | [diff] [blame] | 111 | * share_host_netns - Enable sharing of the host network namespace. |
Dylan Reid | c433584 | 2016-11-11 10:24:52 -0800 | [diff] [blame] | 112 | * keep_fds_open - Allow the child process to keep open FDs (for stdin/out/err). |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 113 | */ |
| 114 | struct container_config { |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame^] | 115 | char *config_root; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 116 | char *rootfs; |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 117 | unsigned long rootfs_mount_flags; |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 118 | char *premounted_runfs; |
| 119 | char *pid_file_path; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 120 | char **program_argv; |
| 121 | size_t num_args; |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 122 | uid_t uid; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 123 | char *uid_map; |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 124 | gid_t gid; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 125 | char *gid_map; |
| 126 | char *alt_syscall_table; |
| 127 | struct container_mount *mounts; |
| 128 | size_t num_mounts; |
| 129 | struct container_device *devices; |
| 130 | size_t num_devices; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 131 | char *run_setfiles; |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 132 | struct container_cpu_cgroup cpu_cgparams; |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 133 | char *cgroup_parent; |
| 134 | uid_t cgroup_owner; |
Dmitry Torokhov | 14eef72 | 2016-09-27 16:40:37 -0700 | [diff] [blame] | 135 | gid_t cgroup_group; |
Keshav Santhanam | 1b6bf67 | 2016-08-10 18:35:12 -0700 | [diff] [blame] | 136 | int share_host_netns; |
Dylan Reid | c433584 | 2016-11-11 10:24:52 -0800 | [diff] [blame] | 137 | int keep_fds_open; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | struct container_config *container_config_create() |
| 141 | { |
| 142 | return calloc(1, sizeof(struct container_config)); |
| 143 | } |
| 144 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 145 | static void container_free_program_args(struct container_config *c) |
| 146 | { |
| 147 | int i; |
| 148 | |
| 149 | if (!c->program_argv) |
| 150 | return; |
| 151 | for (i = 0; i < c->num_args; ++i) { |
| 152 | FREE_AND_NULL(c->program_argv[i]); |
| 153 | } |
| 154 | FREE_AND_NULL(c->program_argv); |
| 155 | } |
| 156 | |
| 157 | static void container_config_free_mount(struct container_mount *mount) |
| 158 | { |
| 159 | FREE_AND_NULL(mount->name); |
| 160 | FREE_AND_NULL(mount->source); |
| 161 | FREE_AND_NULL(mount->destination); |
| 162 | FREE_AND_NULL(mount->type); |
| 163 | FREE_AND_NULL(mount->data); |
| 164 | } |
| 165 | |
| 166 | static void container_config_free_device(struct container_device *device) |
| 167 | { |
| 168 | FREE_AND_NULL(device->path); |
| 169 | } |
| 170 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 171 | void container_config_destroy(struct container_config *c) |
| 172 | { |
| 173 | size_t i; |
| 174 | |
| 175 | if (c == NULL) |
| 176 | return; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 177 | FREE_AND_NULL(c->rootfs); |
| 178 | container_free_program_args(c); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 179 | FREE_AND_NULL(c->premounted_runfs); |
| 180 | FREE_AND_NULL(c->pid_file_path); |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 181 | FREE_AND_NULL(c->uid_map); |
| 182 | FREE_AND_NULL(c->gid_map); |
| 183 | FREE_AND_NULL(c->alt_syscall_table); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 184 | for (i = 0; i < c->num_mounts; ++i) { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 185 | container_config_free_mount(&c->mounts[i]); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 186 | } |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 187 | FREE_AND_NULL(c->mounts); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 188 | for (i = 0; i < c->num_devices; ++i) { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 189 | container_config_free_device(&c->devices[i]); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 190 | } |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 191 | FREE_AND_NULL(c->devices); |
| 192 | FREE_AND_NULL(c->run_setfiles); |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 193 | FREE_AND_NULL(c->cgroup_parent); |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 194 | FREE_AND_NULL(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame^] | 197 | int container_config_config_root(struct container_config *c, |
| 198 | const char *config_root) |
| 199 | { |
| 200 | return strdup_and_free(&c->config_root, config_root); |
| 201 | } |
| 202 | |
| 203 | const char *container_config_get_config_root(const struct container_config *c) |
| 204 | { |
| 205 | return c->config_root; |
| 206 | } |
| 207 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 208 | int container_config_rootfs(struct container_config *c, const char *rootfs) |
| 209 | { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 210 | return strdup_and_free(&c->rootfs, rootfs); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 213 | const char *container_config_get_rootfs(const struct container_config *c) |
| 214 | { |
| 215 | return c->rootfs; |
| 216 | } |
| 217 | |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 218 | void container_config_rootfs_mount_flags(struct container_config *c, |
| 219 | unsigned long rootfs_mount_flags) |
| 220 | { |
| 221 | /* Since we are going to add MS_REMOUNT anyways, add it here so we can |
| 222 | * simply check against zero later. MS_BIND is also added to avoid |
| 223 | * re-mounting the original filesystem, since the rootfs is always |
| 224 | * bind-mounted. |
| 225 | */ |
| 226 | c->rootfs_mount_flags = MS_REMOUNT | MS_BIND | rootfs_mount_flags; |
| 227 | } |
| 228 | |
| 229 | unsigned long container_config_get_rootfs_mount_flags( |
| 230 | const struct container_config *c) |
| 231 | { |
| 232 | return c->rootfs_mount_flags; |
| 233 | } |
| 234 | |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 235 | int container_config_premounted_runfs(struct container_config *c, const char *runfs) |
| 236 | { |
| 237 | return strdup_and_free(&c->premounted_runfs, runfs); |
| 238 | } |
| 239 | |
| 240 | const char *container_config_get_premounted_runfs(const struct container_config *c) |
| 241 | { |
| 242 | return c->premounted_runfs; |
| 243 | } |
| 244 | |
| 245 | int container_config_pid_file(struct container_config *c, const char *path) |
| 246 | { |
| 247 | return strdup_and_free(&c->pid_file_path, path); |
| 248 | } |
| 249 | |
| 250 | const char *container_config_get_pid_file(const struct container_config *c) |
| 251 | { |
| 252 | return c->pid_file_path; |
| 253 | } |
| 254 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 255 | int container_config_program_argv(struct container_config *c, |
Dylan Reid | 17fd53f | 2016-11-18 19:14:41 -0800 | [diff] [blame] | 256 | const char **argv, size_t num_args) |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 257 | { |
| 258 | size_t i; |
| 259 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 260 | container_free_program_args(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 261 | c->num_args = num_args; |
| 262 | c->program_argv = calloc(num_args + 1, sizeof(char *)); |
| 263 | if (!c->program_argv) |
| 264 | return -ENOMEM; |
| 265 | for (i = 0; i < num_args; ++i) { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 266 | if (strdup_and_free(&c->program_argv[i], argv[i])) |
| 267 | goto error_free_return; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 268 | } |
| 269 | c->program_argv[num_args] = NULL; |
| 270 | return 0; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 271 | |
| 272 | error_free_return: |
| 273 | container_free_program_args(c); |
| 274 | return -ENOMEM; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 277 | size_t container_config_get_num_program_args(const struct container_config *c) |
| 278 | { |
| 279 | return c->num_args; |
| 280 | } |
| 281 | |
| 282 | const char *container_config_get_program_arg(const struct container_config *c, |
| 283 | size_t index) |
| 284 | { |
| 285 | if (index >= c->num_args) |
| 286 | return NULL; |
| 287 | return c->program_argv[index]; |
| 288 | } |
| 289 | |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 290 | void container_config_uid(struct container_config *c, uid_t uid) |
| 291 | { |
| 292 | c->uid = uid; |
| 293 | } |
| 294 | |
| 295 | uid_t container_config_get_uid(const struct container_config *c) |
| 296 | { |
| 297 | return c->uid; |
| 298 | } |
| 299 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 300 | int container_config_uid_map(struct container_config *c, const char *uid_map) |
| 301 | { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 302 | return strdup_and_free(&c->uid_map, uid_map); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 305 | void container_config_gid(struct container_config *c, gid_t gid) |
| 306 | { |
| 307 | c->gid = gid; |
| 308 | } |
| 309 | |
| 310 | gid_t container_config_get_gid(const struct container_config *c) |
| 311 | { |
| 312 | return c->gid; |
| 313 | } |
| 314 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 315 | int container_config_gid_map(struct container_config *c, const char *gid_map) |
| 316 | { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 317 | return strdup_and_free(&c->gid_map, gid_map); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | int container_config_alt_syscall_table(struct container_config *c, |
| 321 | const char *alt_syscall_table) |
| 322 | { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 323 | return strdup_and_free(&c->alt_syscall_table, alt_syscall_table); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | int container_config_add_mount(struct container_config *c, |
| 327 | const char *name, |
| 328 | const char *source, |
| 329 | const char *destination, |
| 330 | const char *type, |
| 331 | const char *data, |
| 332 | int flags, |
| 333 | int uid, |
| 334 | int gid, |
| 335 | int mode, |
| 336 | int mount_in_ns, |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 337 | int create, |
| 338 | int loopback) |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 339 | { |
| 340 | struct container_mount *mount_ptr; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 341 | struct container_mount *current_mount; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 342 | |
| 343 | if (name == NULL || source == NULL || |
| 344 | destination == NULL || type == NULL) |
| 345 | return -EINVAL; |
| 346 | |
| 347 | mount_ptr = realloc(c->mounts, |
| 348 | sizeof(c->mounts[0]) * (c->num_mounts + 1)); |
| 349 | if (!mount_ptr) |
| 350 | return -ENOMEM; |
| 351 | c->mounts = mount_ptr; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 352 | current_mount = &c->mounts[c->num_mounts]; |
| 353 | memset(current_mount, 0, sizeof(struct container_mount)); |
| 354 | |
| 355 | if (strdup_and_free(¤t_mount->name, name)) |
| 356 | goto error_free_return; |
| 357 | if (strdup_and_free(¤t_mount->source, source)) |
| 358 | goto error_free_return; |
| 359 | if (strdup_and_free(¤t_mount->destination, destination)) |
| 360 | goto error_free_return; |
| 361 | if (strdup_and_free(¤t_mount->type, type)) |
| 362 | goto error_free_return; |
| 363 | if (data && strdup_and_free(¤t_mount->data, data)) |
| 364 | goto error_free_return; |
| 365 | current_mount->flags = flags; |
| 366 | current_mount->uid = uid; |
| 367 | current_mount->gid = gid; |
| 368 | current_mount->mode = mode; |
| 369 | current_mount->mount_in_ns = mount_in_ns; |
| 370 | current_mount->create = create; |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 371 | current_mount->loopback = loopback; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 372 | ++c->num_mounts; |
| 373 | return 0; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 374 | |
| 375 | error_free_return: |
| 376 | container_config_free_mount(current_mount); |
| 377 | return -ENOMEM; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | int container_config_add_device(struct container_config *c, |
| 381 | char type, |
| 382 | const char *path, |
| 383 | int fs_permissions, |
| 384 | int major, |
| 385 | int minor, |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 386 | int copy_minor, |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 387 | int uid, |
| 388 | int gid, |
| 389 | int read_allowed, |
| 390 | int write_allowed, |
| 391 | int modify_allowed) |
| 392 | { |
| 393 | struct container_device *dev_ptr; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 394 | struct container_device *current_dev; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 395 | |
| 396 | if (path == NULL) |
| 397 | return -EINVAL; |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 398 | /* If using a dynamic minor number, ensure that minor is -1. */ |
| 399 | if (copy_minor && (minor != -1)) |
| 400 | return -EINVAL; |
| 401 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 402 | dev_ptr = realloc(c->devices, |
| 403 | sizeof(c->devices[0]) * (c->num_devices + 1)); |
| 404 | if (!dev_ptr) |
| 405 | return -ENOMEM; |
| 406 | c->devices = dev_ptr; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 407 | current_dev = &c->devices[c->num_devices]; |
| 408 | memset(current_dev, 0, sizeof(struct container_device)); |
| 409 | |
| 410 | current_dev->type = type; |
| 411 | if (strdup_and_free(¤t_dev->path, path)) |
| 412 | goto error_free_return; |
| 413 | current_dev->fs_permissions = fs_permissions; |
| 414 | current_dev->major = major; |
| 415 | current_dev->minor = minor; |
| 416 | current_dev->copy_minor = copy_minor; |
| 417 | current_dev->uid = uid; |
| 418 | current_dev->gid = gid; |
| 419 | current_dev->read_allowed = read_allowed; |
| 420 | current_dev->write_allowed = write_allowed; |
| 421 | current_dev->modify_allowed = modify_allowed; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 422 | ++c->num_devices; |
| 423 | return 0; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 424 | |
| 425 | error_free_return: |
| 426 | container_config_free_device(current_dev); |
| 427 | return -ENOMEM; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 430 | int container_config_run_setfiles(struct container_config *c, |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 431 | const char *setfiles_cmd) |
| 432 | { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 433 | return strdup_and_free(&c->run_setfiles, setfiles_cmd); |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 434 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 435 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 436 | const char *container_config_get_run_setfiles(const struct container_config *c) |
| 437 | { |
| 438 | return c->run_setfiles; |
| 439 | } |
| 440 | |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 441 | int container_config_set_cpu_shares(struct container_config *c, int shares) |
| 442 | { |
| 443 | /* CPU shares must be 2 or higher. */ |
| 444 | if (shares < 2) |
| 445 | return -EINVAL; |
| 446 | |
| 447 | c->cpu_cgparams.shares = shares; |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | int container_config_set_cpu_cfs_params(struct container_config *c, |
| 452 | int quota, |
| 453 | int period) |
| 454 | { |
| 455 | /* |
| 456 | * quota could be set higher than period to utilize more than one CPU. |
| 457 | * quota could also be set as -1 to indicate the cgroup does not adhere |
| 458 | * to any CPU time restrictions. |
| 459 | */ |
| 460 | if (quota <= 0 && quota != -1) |
| 461 | return -EINVAL; |
| 462 | if (period <= 0) |
| 463 | return -EINVAL; |
| 464 | |
| 465 | c->cpu_cgparams.quota = quota; |
| 466 | c->cpu_cgparams.period = period; |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | int container_config_set_cpu_rt_params(struct container_config *c, |
| 471 | int rt_runtime, |
| 472 | int rt_period) |
| 473 | { |
| 474 | /* |
| 475 | * rt_runtime could be set as 0 to prevent the cgroup from using |
| 476 | * realtime CPU. |
| 477 | */ |
| 478 | if (rt_runtime < 0 || rt_runtime >= rt_period) |
| 479 | return -EINVAL; |
| 480 | |
| 481 | c->cpu_cgparams.rt_runtime = rt_runtime; |
| 482 | c->cpu_cgparams.rt_period = rt_period; |
| 483 | return 0; |
| 484 | } |
| 485 | |
Chinyue Chen | 4f3fd68 | 2016-07-01 14:11:42 +0800 | [diff] [blame] | 486 | int container_config_get_cpu_shares(struct container_config *c) |
| 487 | { |
| 488 | return c->cpu_cgparams.shares; |
| 489 | } |
| 490 | |
| 491 | int container_config_get_cpu_quota(struct container_config *c) |
| 492 | { |
| 493 | return c->cpu_cgparams.quota; |
| 494 | } |
| 495 | |
| 496 | int container_config_get_cpu_period(struct container_config *c) |
| 497 | { |
| 498 | return c->cpu_cgparams.period; |
| 499 | } |
| 500 | |
| 501 | int container_config_get_cpu_rt_runtime(struct container_config *c) |
| 502 | { |
| 503 | return c->cpu_cgparams.rt_runtime; |
| 504 | } |
| 505 | |
| 506 | int container_config_get_cpu_rt_period(struct container_config *c) |
| 507 | { |
| 508 | return c->cpu_cgparams.rt_period; |
| 509 | } |
| 510 | |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 511 | int container_config_set_cgroup_parent(struct container_config *c, |
| 512 | const char *parent, |
Dmitry Torokhov | 14eef72 | 2016-09-27 16:40:37 -0700 | [diff] [blame] | 513 | uid_t cgroup_owner, gid_t cgroup_group) |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 514 | { |
| 515 | c->cgroup_owner = cgroup_owner; |
Dmitry Torokhov | 14eef72 | 2016-09-27 16:40:37 -0700 | [diff] [blame] | 516 | c->cgroup_group = cgroup_group; |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 517 | return strdup_and_free(&c->cgroup_parent, parent); |
| 518 | } |
| 519 | |
| 520 | const char *container_config_get_cgroup_parent(struct container_config *c) |
| 521 | { |
| 522 | return c->cgroup_parent; |
| 523 | } |
| 524 | |
Keshav Santhanam | 1b6bf67 | 2016-08-10 18:35:12 -0700 | [diff] [blame] | 525 | void container_config_share_host_netns(struct container_config *c) |
| 526 | { |
| 527 | c->share_host_netns = 1; |
| 528 | } |
| 529 | |
| 530 | int get_container_config_share_host_netns(struct container_config *c) |
| 531 | { |
| 532 | return c->share_host_netns; |
| 533 | } |
| 534 | |
Dylan Reid | c433584 | 2016-11-11 10:24:52 -0800 | [diff] [blame] | 535 | void container_config_keep_fds_open(struct container_config *c) |
| 536 | { |
| 537 | c->keep_fds_open = 1; |
| 538 | } |
| 539 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 540 | /* |
| 541 | * Container manipulation |
| 542 | */ |
| 543 | struct container { |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 544 | struct container_cgroup *cgroup; |
| 545 | struct minijail *jail; |
| 546 | pid_t init_pid; |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame^] | 547 | char *config_root; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 548 | char *runfs; |
| 549 | char *rundir; |
| 550 | char *runfsroot; |
| 551 | char *pid_file_path; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 552 | char **ext_mounts; /* Mounts made outside of the minijail */ |
| 553 | size_t num_ext_mounts; |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 554 | char **loopdevs; |
| 555 | size_t num_loopdevs; |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 556 | char *name; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | struct container *container_new(const char *name, |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 560 | const char *rundir) |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 561 | { |
| 562 | struct container *c; |
| 563 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 564 | c = calloc(1, sizeof(*c)); |
Dylan Reid | b435c68 | 2016-04-12 04:17:49 -0700 | [diff] [blame] | 565 | if (!c) |
| 566 | return NULL; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 567 | c->rundir = strdup(rundir); |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 568 | c->name = strdup(name); |
Dylan Reid | a996642 | 2016-07-21 10:11:34 -0700 | [diff] [blame] | 569 | if (!c->rundir || !c->name) { |
Dylan Reid | 684975e | 2016-05-02 15:44:47 -0700 | [diff] [blame] | 570 | container_destroy(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 571 | return NULL; |
Dylan Reid | b435c68 | 2016-04-12 04:17:49 -0700 | [diff] [blame] | 572 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 573 | return c; |
| 574 | } |
| 575 | |
| 576 | void container_destroy(struct container *c) |
| 577 | { |
Dylan Reid | 684975e | 2016-05-02 15:44:47 -0700 | [diff] [blame] | 578 | if (c->cgroup) |
| 579 | container_cgroup_destroy(c->cgroup); |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 580 | if (c->jail) |
| 581 | minijail_destroy(c->jail); |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame^] | 582 | FREE_AND_NULL(c->config_root); |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 583 | FREE_AND_NULL(c->name); |
| 584 | FREE_AND_NULL(c->rundir); |
| 585 | FREE_AND_NULL(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static int make_dir(const char *path, int uid, int gid, int mode) |
| 589 | { |
| 590 | if (mkdir(path, mode)) |
| 591 | return -errno; |
| 592 | if (chmod(path, mode)) |
| 593 | return -errno; |
| 594 | if (chown(path, uid, gid)) |
| 595 | return -errno; |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int touch_file(const char *path, int uid, int gid, int mode) |
| 600 | { |
| 601 | int rc; |
| 602 | int fd = open(path, O_RDWR | O_CREAT, mode); |
| 603 | if (fd < 0) |
| 604 | return -errno; |
| 605 | rc = fchown(fd, uid, gid); |
| 606 | close(fd); |
| 607 | |
| 608 | if (rc) |
| 609 | return -errno; |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /* Make sure the mount target exists in the new rootfs. Create if needed and |
| 614 | * possible. |
| 615 | */ |
| 616 | static int setup_mount_destination(const struct container_mount *mnt, |
Dylan Reid | 2149be9 | 2016-04-28 18:38:57 -0700 | [diff] [blame] | 617 | const char *source, |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 618 | const char *dest) |
| 619 | { |
| 620 | int rc; |
| 621 | struct stat st_buf; |
| 622 | |
| 623 | rc = stat(dest, &st_buf); |
| 624 | if (rc == 0) /* destination exists */ |
| 625 | return 0; |
| 626 | |
| 627 | /* Try to create the destination. Either make directory or touch a file |
| 628 | * depending on the source type. |
| 629 | */ |
Dylan Reid | 2149be9 | 2016-04-28 18:38:57 -0700 | [diff] [blame] | 630 | rc = stat(source, &st_buf); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 631 | if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) |
| 632 | return make_dir(dest, mnt->uid, mnt->gid, mnt->mode); |
| 633 | |
| 634 | return touch_file(dest, mnt->uid, mnt->gid, mnt->mode); |
| 635 | } |
| 636 | |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 637 | /* Fork and exec the setfiles command to configure the selinux policy. */ |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 638 | static int run_setfiles_command(const struct container *c, |
| 639 | const struct container_config *config, |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 640 | char *const *destinations, size_t num_destinations) |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 641 | { |
| 642 | int rc; |
| 643 | int status; |
| 644 | int pid; |
| 645 | char *context_path; |
| 646 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 647 | if (!config->run_setfiles) |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 648 | return 0; |
| 649 | |
| 650 | if (asprintf(&context_path, "%s/file_contexts", |
| 651 | c->runfsroot) < 0) |
| 652 | return -errno; |
| 653 | |
| 654 | pid = fork(); |
| 655 | if (pid == 0) { |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 656 | size_t i; |
| 657 | size_t arg_index = 0; |
| 658 | const char *argv[MAX_NUM_SETFILES_ARGS]; |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 659 | const char *env[] = { |
| 660 | NULL, |
| 661 | }; |
| 662 | |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 663 | argv[arg_index++] = config->run_setfiles; |
| 664 | argv[arg_index++] = "-r"; |
| 665 | argv[arg_index++] = c->runfsroot; |
| 666 | argv[arg_index++] = context_path; |
| 667 | if (arg_index + num_destinations >= MAX_NUM_SETFILES_ARGS) |
| 668 | _exit(-E2BIG); |
| 669 | for (i = 0; i < num_destinations; ++i) { |
| 670 | argv[arg_index++] = destinations[i]; |
| 671 | } |
| 672 | argv[arg_index] = NULL; |
| 673 | |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 674 | execve(argv[0], (char *const*)argv, (char *const*)env); |
| 675 | |
| 676 | /* Command failed to exec if execve returns. */ |
| 677 | _exit(-errno); |
| 678 | } |
| 679 | free(context_path); |
| 680 | if (pid < 0) |
| 681 | return -errno; |
| 682 | do { |
| 683 | rc = waitpid(pid, &status, 0); |
| 684 | } while (rc == -1 && errno == EINTR); |
| 685 | if (rc < 0) |
| 686 | return -errno; |
| 687 | return status; |
| 688 | } |
| 689 | |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 690 | /* Find a free loop device and attach it. */ |
| 691 | static int loopdev_setup(char **loopdev_ret, const char *source) |
| 692 | { |
| 693 | int ret = 0; |
| 694 | int source_fd = -1; |
| 695 | int control_fd = -1; |
| 696 | int loop_fd = -1; |
| 697 | char *loopdev = NULL; |
| 698 | |
| 699 | source_fd = open(source, O_RDONLY|O_CLOEXEC); |
| 700 | if (source_fd < 0) |
| 701 | goto error; |
| 702 | |
| 703 | control_fd = open(loopdev_ctl, O_RDWR|O_NOFOLLOW|O_CLOEXEC); |
| 704 | if (control_fd < 0) |
| 705 | goto error; |
| 706 | |
| 707 | while (1) { |
| 708 | int num = ioctl(control_fd, LOOP_CTL_GET_FREE); |
| 709 | if (num < 0) |
| 710 | goto error; |
| 711 | |
| 712 | if (asprintf(&loopdev, "/dev/loop%i", num) < 0) |
| 713 | goto error; |
| 714 | |
| 715 | loop_fd = open(loopdev, O_RDONLY|O_NOFOLLOW|O_CLOEXEC); |
| 716 | if (loop_fd < 0) |
| 717 | goto error; |
| 718 | |
| 719 | if (ioctl(loop_fd, LOOP_SET_FD, source_fd) == 0) |
| 720 | break; |
| 721 | |
| 722 | if (errno != EBUSY) |
| 723 | goto error; |
| 724 | |
| 725 | /* Clean up resources for the next pass. */ |
| 726 | free(loopdev); |
| 727 | close(loop_fd); |
| 728 | } |
| 729 | |
| 730 | *loopdev_ret = loopdev; |
| 731 | goto exit; |
| 732 | |
| 733 | error: |
| 734 | ret = -errno; |
| 735 | free(loopdev); |
| 736 | exit: |
| 737 | if (source_fd != -1) |
| 738 | close(source_fd); |
| 739 | if (control_fd != -1) |
| 740 | close(control_fd); |
| 741 | if (loop_fd != -1) |
| 742 | close(loop_fd); |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | /* Detach the specified loop device. */ |
| 747 | static int loopdev_detach(const char *loopdev) |
| 748 | { |
| 749 | int ret = 0; |
| 750 | int fd; |
| 751 | |
| 752 | fd = open(loopdev, O_RDONLY|O_NOFOLLOW|O_CLOEXEC); |
| 753 | if (fd < 0) |
| 754 | goto error; |
| 755 | if (ioctl(fd, LOOP_CLR_FD) < 0) |
| 756 | goto error; |
| 757 | |
| 758 | goto exit; |
| 759 | |
| 760 | error: |
| 761 | ret = -errno; |
| 762 | exit: |
| 763 | if (fd != -1) |
| 764 | close(fd); |
| 765 | return ret; |
| 766 | } |
| 767 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 768 | /* |
| 769 | * Unmounts anything we mounted in this mount namespace in the opposite order |
| 770 | * that they were mounted. |
| 771 | */ |
| 772 | static int unmount_external_mounts(struct container *c) |
| 773 | { |
| 774 | int ret = 0; |
| 775 | |
| 776 | while (c->num_ext_mounts) { |
| 777 | c->num_ext_mounts--; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 778 | if (!c->ext_mounts[c->num_ext_mounts]) |
| 779 | continue; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 780 | if (umount(c->ext_mounts[c->num_ext_mounts])) |
| 781 | ret = -errno; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 782 | FREE_AND_NULL(c->ext_mounts[c->num_ext_mounts]); |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 783 | } |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 784 | FREE_AND_NULL(c->ext_mounts); |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 785 | |
| 786 | while (c->num_loopdevs) { |
| 787 | c->num_loopdevs--; |
| 788 | if (loopdev_detach(c->loopdevs[c->num_loopdevs])) |
| 789 | ret = -errno; |
| 790 | FREE_AND_NULL(c->loopdevs[c->num_loopdevs]); |
| 791 | } |
| 792 | FREE_AND_NULL(c->loopdevs); |
| 793 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 794 | return ret; |
| 795 | } |
| 796 | |
Junichi Uekawa | 5d27277 | 2016-07-21 16:07:19 +0900 | [diff] [blame] | 797 | /* |
| 798 | * Match mount_one in minijail, mount one mountpoint with |
| 799 | * consideration for combination of MS_BIND/MS_RDONLY flag. |
| 800 | */ |
| 801 | static int mount_external(const char *src, const char *dest, const char *type, |
| 802 | unsigned long flags, const void *data) |
| 803 | { |
| 804 | int remount_ro = 0; |
| 805 | |
| 806 | /* |
| 807 | * R/O bind mounts have to be remounted since 'bind' and 'ro' |
| 808 | * can't both be specified in the original bind mount. |
| 809 | * Remount R/O after the initial mount. |
| 810 | */ |
| 811 | if ((flags & MS_BIND) && (flags & MS_RDONLY)) { |
| 812 | remount_ro = 1; |
| 813 | flags &= ~MS_RDONLY; |
| 814 | } |
| 815 | |
| 816 | if (mount(src, dest, type, flags, data) == -1) |
| 817 | return -1; |
| 818 | |
| 819 | if (remount_ro) { |
| 820 | flags |= MS_RDONLY; |
| 821 | if (mount(src, dest, NULL, flags | MS_REMOUNT, data) == -1) |
| 822 | return -1; |
| 823 | } |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 828 | static int do_container_mount(struct container *c, |
| 829 | const struct container_mount *mnt) |
| 830 | { |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 831 | char *loop_source = NULL; |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 832 | char *source = NULL; |
| 833 | char *dest = NULL; |
| 834 | int rc = 0; |
| 835 | |
| 836 | if (asprintf(&dest, "%s%s", c->runfsroot, mnt->destination) < 0) |
| 837 | return -errno; |
| 838 | |
| 839 | /* |
| 840 | * If it's a bind mount relative to rootfs, append source to |
| 841 | * rootfs path, otherwise source path is absolute. |
| 842 | */ |
| 843 | if ((mnt->flags & MS_BIND) && mnt->source[0] != '/') { |
| 844 | if (asprintf(&source, "%s/%s", c->runfsroot, mnt->source) < 0) |
| 845 | goto error_free_return; |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame^] | 846 | } else if (mnt->loopback && mnt->source[0] != '/' && c->config_root) { |
| 847 | if (asprintf(&source, "%s/%s", c->config_root, mnt->source) < 0) |
| 848 | goto error_free_return; |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 849 | } else { |
| 850 | if (asprintf(&source, "%s", mnt->source) < 0) |
| 851 | goto error_free_return; |
| 852 | } |
| 853 | |
| 854 | if (mnt->create) { |
| 855 | rc = setup_mount_destination(mnt, source, dest); |
| 856 | if (rc) |
| 857 | goto error_free_return; |
| 858 | } |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 859 | if (mnt->loopback) { |
| 860 | /* Record this loopback file for cleanup later. */ |
| 861 | loop_source = source; |
| 862 | source = NULL; |
| 863 | rc = loopdev_setup(&source, loop_source); |
| 864 | if (rc) |
| 865 | goto error_free_return; |
| 866 | |
| 867 | /* Save this to unmount when shutting down. */ |
| 868 | rc = strdup_and_free(&c->loopdevs[c->num_loopdevs], source); |
| 869 | if (rc) |
| 870 | goto error_free_return; |
| 871 | c->num_loopdevs++; |
| 872 | } |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 873 | if (mnt->mount_in_ns) { |
| 874 | /* We can mount this with minijail. */ |
Dylan Reid | 36b9c01 | 2016-06-24 18:27:08 -0700 | [diff] [blame] | 875 | rc = minijail_mount_with_data(c->jail, source, mnt->destination, |
| 876 | mnt->type, mnt->flags, mnt->data); |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 877 | if (rc) |
| 878 | goto error_free_return; |
| 879 | } else { |
| 880 | /* Mount this externally and unmount it on exit. */ |
Junichi Uekawa | 5d27277 | 2016-07-21 16:07:19 +0900 | [diff] [blame] | 881 | if (mount_external(source, dest, mnt->type, mnt->flags, |
| 882 | mnt->data)) |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 883 | goto error_free_return; |
| 884 | /* Save this to unmount when shutting down. */ |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 885 | rc = strdup_and_free(&c->ext_mounts[c->num_ext_mounts], dest); |
| 886 | if (rc) |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 887 | goto error_free_return; |
| 888 | c->num_ext_mounts++; |
| 889 | } |
| 890 | |
| 891 | goto exit; |
| 892 | |
| 893 | error_free_return: |
| 894 | if (!rc) |
| 895 | rc = -errno; |
| 896 | exit: |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 897 | free(loop_source); |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 898 | free(source); |
| 899 | free(dest); |
| 900 | return rc; |
| 901 | } |
| 902 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 903 | static int do_container_mounts(struct container *c, |
| 904 | const struct container_config *config) |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 905 | { |
| 906 | unsigned int i; |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 907 | int rc = 0; |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 908 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 909 | unmount_external_mounts(c); |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 910 | /* |
| 911 | * Allocate space to track anything we mount in our mount namespace. |
| 912 | * This over-allocates as it has space for all mounts. |
| 913 | */ |
| 914 | c->ext_mounts = calloc(config->num_mounts, sizeof(*c->ext_mounts)); |
| 915 | if (!c->ext_mounts) |
| 916 | return -errno; |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 917 | c->loopdevs = calloc(config->num_mounts, sizeof(*c->loopdevs)); |
| 918 | if (!c->loopdevs) |
| 919 | return -errno; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 920 | |
| 921 | for (i = 0; i < config->num_mounts; ++i) { |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 922 | rc = do_container_mount(c, &config->mounts[i]); |
| 923 | if (rc) |
| 924 | goto error_free_return; |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 925 | } |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 926 | |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 927 | return 0; |
Dylan Reid | 2149be9 | 2016-04-28 18:38:57 -0700 | [diff] [blame] | 928 | |
| 929 | error_free_return: |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 930 | unmount_external_mounts(c); |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 931 | return rc; |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 934 | static int container_create_device(const struct container *c, |
| 935 | const struct container_device *dev, |
| 936 | int minor) |
| 937 | { |
| 938 | char *path = NULL; |
| 939 | int rc = 0; |
| 940 | int mode; |
| 941 | |
| 942 | switch (dev->type) { |
| 943 | case 'b': |
| 944 | mode = S_IFBLK; |
| 945 | break; |
| 946 | case 'c': |
| 947 | mode = S_IFCHR; |
| 948 | break; |
| 949 | default: |
| 950 | return -EINVAL; |
| 951 | } |
| 952 | mode |= dev->fs_permissions; |
| 953 | |
| 954 | if (asprintf(&path, "%s%s", c->runfsroot, dev->path) < 0) |
| 955 | goto error_free_return; |
| 956 | if (mknod(path, mode, makedev(dev->major, minor)) && errno != EEXIST) |
| 957 | goto error_free_return; |
| 958 | if (chown(path, dev->uid, dev->gid)) |
| 959 | goto error_free_return; |
| 960 | if (chmod(path, dev->fs_permissions)) |
| 961 | goto error_free_return; |
| 962 | |
| 963 | goto exit; |
| 964 | |
| 965 | error_free_return: |
| 966 | rc = -errno; |
| 967 | exit: |
| 968 | free(path); |
| 969 | return rc; |
| 970 | } |
| 971 | |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 972 | static int mount_runfs(struct container *c, const struct container_config *config) |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 973 | { |
Dylan Reid | b362183 | 2016-03-24 10:24:57 -0700 | [diff] [blame] | 974 | static const mode_t root_dir_mode = 0660; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 975 | const char *rootfs = config->rootfs; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 976 | char *runfs_template = NULL; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 977 | |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 978 | if (asprintf(&runfs_template, "%s/%s_XXXXXX", c->rundir, c->name) < 0) |
| 979 | return -ENOMEM; |
| 980 | |
| 981 | c->runfs = mkdtemp(runfs_template); |
| 982 | if (!c->runfs) { |
| 983 | free(runfs_template); |
| 984 | return -errno; |
| 985 | } |
| 986 | |
| 987 | /* Make sure the container uid can access the rootfs. */ |
| 988 | if (chmod(c->runfs, 0700)) |
| 989 | return -errno; |
| 990 | if (chown(c->runfs, config->uid, config->gid)) |
| 991 | return -errno; |
| 992 | |
| 993 | if (asprintf(&c->runfsroot, "%s/root", c->runfs) < 0) |
| 994 | return -errno; |
| 995 | |
| 996 | if (mkdir(c->runfsroot, root_dir_mode)) |
| 997 | return -errno; |
| 998 | if (chmod(c->runfsroot, root_dir_mode)) |
| 999 | return -errno; |
| 1000 | |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 1001 | if (mount(rootfs, c->runfsroot, "", MS_BIND, NULL)) |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 1002 | return -errno; |
| 1003 | |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 1004 | /* MS_BIND ignores any flags passed to it (except MS_REC). We need a |
| 1005 | * second call to mount() to actually set them. |
| 1006 | */ |
| 1007 | if (config->rootfs_mount_flags && |
| 1008 | mount(rootfs, c->runfsroot, "", |
| 1009 | config->rootfs_mount_flags, NULL)) { |
| 1010 | return -errno; |
| 1011 | } |
| 1012 | |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 1013 | return 0; |
| 1014 | } |
| 1015 | |
Keshav Santhanam | 36485ff | 2016-08-02 16:21:02 -0700 | [diff] [blame] | 1016 | static int get_userns_id(const char *map, int id) |
| 1017 | { |
| 1018 | char *map_copy, *mapping, *saveptr1, *saveptr2; |
| 1019 | int inside, outside, length; |
| 1020 | int result = 0; |
| 1021 | errno = 0; |
| 1022 | |
| 1023 | if (asprintf(&map_copy, "%s", map) < 0) |
| 1024 | return -ENOMEM; |
| 1025 | |
| 1026 | mapping = strtok_r(map_copy, ",", &saveptr1); |
| 1027 | while (mapping) { |
| 1028 | inside = strtol(strtok_r(mapping, " ", &saveptr2), NULL, 10); |
| 1029 | outside = strtol(strtok_r(NULL, " ", &saveptr2), NULL, 10); |
| 1030 | length = strtol(strtok_r(NULL, "\0", &saveptr2), NULL, 10); |
| 1031 | if (errno) { |
| 1032 | goto error_free_return; |
| 1033 | } else if (inside < 0 || outside < 0 || length < 0) { |
| 1034 | errno = EINVAL; |
| 1035 | goto error_free_return; |
| 1036 | } |
| 1037 | |
| 1038 | if (id >= outside && id <= (outside + length)) { |
| 1039 | result = id - (outside - inside); |
| 1040 | goto exit; |
| 1041 | } |
| 1042 | |
| 1043 | mapping = strtok_r(NULL, ",", &saveptr1); |
| 1044 | } |
| 1045 | errno = EINVAL; |
| 1046 | |
| 1047 | error_free_return: |
| 1048 | result = -errno; |
| 1049 | exit: |
| 1050 | free(map_copy); |
| 1051 | return result; |
| 1052 | } |
| 1053 | |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 1054 | int container_start(struct container *c, const struct container_config *config) |
| 1055 | { |
| 1056 | int rc = 0; |
| 1057 | unsigned int i; |
Keshav Santhanam | 36485ff | 2016-08-02 16:21:02 -0700 | [diff] [blame] | 1058 | int uid_userns, gid_userns; |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 1059 | char **destinations; |
| 1060 | size_t num_destinations; |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 1061 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 1062 | if (!c) |
| 1063 | return -EINVAL; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 1064 | if (!config) |
| 1065 | return -EINVAL; |
| 1066 | if (!config->program_argv || !config->program_argv[0]) |
| 1067 | return -EINVAL; |
| 1068 | |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame^] | 1069 | if (config->config_root) { |
| 1070 | c->config_root = strdup(config->config_root); |
| 1071 | if (!c->config_root) { |
| 1072 | rc = -ENOMEM; |
| 1073 | goto error_rmdir; |
| 1074 | } |
| 1075 | } |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 1076 | if (config->premounted_runfs) { |
| 1077 | c->runfs = NULL; |
| 1078 | c->runfsroot = strdup(config->premounted_runfs); |
| 1079 | if (!c->runfsroot) { |
| 1080 | rc = -ENOMEM; |
| 1081 | goto error_rmdir; |
| 1082 | } |
| 1083 | } else { |
| 1084 | rc = mount_runfs(c, config); |
| 1085 | if (rc) |
| 1086 | goto error_rmdir; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1087 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1088 | |
| 1089 | c->jail = minijail_new(); |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 1090 | if (!c->jail) |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 1091 | goto error_rmdir; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1092 | |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 1093 | rc = do_container_mounts(c, config); |
| 1094 | if (rc) |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 1095 | goto error_rmdir; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1096 | |
Dylan Reid | a996642 | 2016-07-21 10:11:34 -0700 | [diff] [blame] | 1097 | c->cgroup = container_cgroup_new(c->name, |
| 1098 | "/sys/fs/cgroup", |
| 1099 | config->cgroup_parent, |
Dmitry Torokhov | 14eef72 | 2016-09-27 16:40:37 -0700 | [diff] [blame] | 1100 | config->cgroup_owner, |
| 1101 | config->cgroup_group); |
Dylan Reid | a996642 | 2016-07-21 10:11:34 -0700 | [diff] [blame] | 1102 | if (!c->cgroup) |
| 1103 | goto error_rmdir; |
| 1104 | |
Keshav Santhanam | 268fa03 | 2016-07-14 09:59:24 -0700 | [diff] [blame] | 1105 | /* Must be root to modify device cgroup or mknod */ |
| 1106 | if (getuid() == 0) { |
| 1107 | c->cgroup->ops->deny_all_devices(c->cgroup); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1108 | |
Keshav Santhanam | 268fa03 | 2016-07-14 09:59:24 -0700 | [diff] [blame] | 1109 | for (i = 0; i < config->num_devices; i++) { |
| 1110 | const struct container_device *dev = &config->devices[i]; |
| 1111 | int minor = dev->minor; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1112 | |
Keshav Santhanam | 268fa03 | 2016-07-14 09:59:24 -0700 | [diff] [blame] | 1113 | if (dev->copy_minor) { |
| 1114 | struct stat st_buff; |
| 1115 | if (stat(dev->path, &st_buff) < 0) |
| 1116 | continue; |
| 1117 | /* Use the minor macro to extract the device number. */ |
| 1118 | minor = minor(st_buff.st_rdev); |
| 1119 | } |
| 1120 | if (minor >= 0) { |
| 1121 | rc = container_create_device(c, dev, minor); |
| 1122 | if (rc) |
| 1123 | goto error_rmdir; |
| 1124 | } |
| 1125 | |
| 1126 | rc = c->cgroup->ops->add_device(c->cgroup, dev->major, |
| 1127 | minor, dev->read_allowed, |
| 1128 | dev->write_allowed, |
| 1129 | dev->modify_allowed, dev->type); |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 1130 | if (rc) |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 1131 | goto error_rmdir; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1132 | } |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 1133 | |
| 1134 | for (i = 0; i < c->num_loopdevs; ++i) { |
| 1135 | struct stat st; |
| 1136 | |
| 1137 | if (stat(c->loopdevs[i], &st) < 0) |
| 1138 | goto error_rmdir; |
| 1139 | rc = c->cgroup->ops->add_device(c->cgroup, major(st.st_rdev), |
| 1140 | minor(st.st_rdev), 1, 0, 0, 'b'); |
| 1141 | if (rc) |
| 1142 | goto error_rmdir; |
| 1143 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Dylan Reid | d722958 | 2016-04-27 17:08:40 -0700 | [diff] [blame] | 1146 | /* Potentailly run setfiles on mounts configured outside of the jail */ |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 1147 | destinations = calloc(config->num_mounts, sizeof(char *)); |
| 1148 | num_destinations = 0; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 1149 | for (i = 0; i < config->num_mounts; i++) { |
| 1150 | const struct container_mount *mnt = &config->mounts[i]; |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 1151 | char* dest = mnt->destination; |
Dylan Reid | d722958 | 2016-04-27 17:08:40 -0700 | [diff] [blame] | 1152 | |
| 1153 | if (mnt->mount_in_ns) |
| 1154 | continue; |
Junichi Uekawa | 5d27277 | 2016-07-21 16:07:19 +0900 | [diff] [blame] | 1155 | if (mnt->flags & MS_RDONLY) |
| 1156 | continue; |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 1157 | |
Yusuke Sato | d33db43 | 2016-12-05 16:24:37 -0800 | [diff] [blame] | 1158 | /* A hack to avoid setfiles on /data and /cache. */ |
| 1159 | if (!strcmp(dest, "/data") || !strcmp(dest, "/cache")) |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 1160 | continue; |
| 1161 | |
| 1162 | if (asprintf(&dest, "%s%s", c->runfsroot, mnt->destination) < 0) { |
| 1163 | size_t j; |
| 1164 | for (j = 0; j < num_destinations; ++j) { |
| 1165 | free(destinations[j]); |
| 1166 | } |
| 1167 | free(destinations); |
Dylan Reid | d722958 | 2016-04-27 17:08:40 -0700 | [diff] [blame] | 1168 | goto error_rmdir; |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | destinations[num_destinations++] = dest; |
Dylan Reid | d722958 | 2016-04-27 17:08:40 -0700 | [diff] [blame] | 1172 | } |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 1173 | if (num_destinations) { |
| 1174 | size_t i; |
| 1175 | rc = run_setfiles_command(c, config, destinations, num_destinations); |
| 1176 | for (i = 0; i < num_destinations; ++i) { |
| 1177 | free(destinations[i]); |
| 1178 | } |
| 1179 | } |
| 1180 | free(destinations); |
| 1181 | if (rc) |
| 1182 | goto error_rmdir; |
Dylan Reid | d722958 | 2016-04-27 17:08:40 -0700 | [diff] [blame] | 1183 | |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 1184 | /* Setup CPU cgroup params. */ |
| 1185 | if (config->cpu_cgparams.shares) { |
| 1186 | rc = c->cgroup->ops->set_cpu_shares( |
| 1187 | c->cgroup, config->cpu_cgparams.shares); |
| 1188 | if (rc) |
| 1189 | goto error_rmdir; |
| 1190 | } |
| 1191 | if (config->cpu_cgparams.period) { |
| 1192 | rc = c->cgroup->ops->set_cpu_quota( |
| 1193 | c->cgroup, config->cpu_cgparams.quota); |
| 1194 | if (rc) |
| 1195 | goto error_rmdir; |
| 1196 | rc = c->cgroup->ops->set_cpu_period( |
| 1197 | c->cgroup, config->cpu_cgparams.period); |
| 1198 | if (rc) |
| 1199 | goto error_rmdir; |
| 1200 | } |
| 1201 | if (config->cpu_cgparams.rt_period) { |
| 1202 | rc = c->cgroup->ops->set_cpu_rt_runtime( |
| 1203 | c->cgroup, config->cpu_cgparams.rt_runtime); |
| 1204 | if (rc) |
| 1205 | goto error_rmdir; |
| 1206 | rc = c->cgroup->ops->set_cpu_rt_period( |
| 1207 | c->cgroup, config->cpu_cgparams.rt_period); |
| 1208 | if (rc) |
| 1209 | goto error_rmdir; |
| 1210 | } |
| 1211 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1212 | /* Setup and start the container with libminijail. */ |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 1213 | if (config->pid_file_path) { |
| 1214 | c->pid_file_path = strdup(config->pid_file_path); |
| 1215 | if (!c->pid_file_path) { |
| 1216 | rc = -ENOMEM; |
| 1217 | goto error_rmdir; |
| 1218 | } |
| 1219 | } else if (c->runfs) { |
| 1220 | if (asprintf(&c->pid_file_path, "%s/container.pid", c->runfs) < 0) { |
| 1221 | rc = -ENOMEM; |
| 1222 | goto error_rmdir; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | if (c->pid_file_path) |
| 1227 | minijail_write_pid_file(c->jail, c->pid_file_path); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1228 | minijail_reset_signal_mask(c->jail); |
| 1229 | |
| 1230 | /* Setup container namespaces. */ |
| 1231 | minijail_namespace_ipc(c->jail); |
| 1232 | minijail_namespace_vfs(c->jail); |
Keshav Santhanam | 1b6bf67 | 2016-08-10 18:35:12 -0700 | [diff] [blame] | 1233 | if (!config->share_host_netns) |
| 1234 | minijail_namespace_net(c->jail); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1235 | minijail_namespace_pids(c->jail); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1236 | minijail_namespace_user(c->jail); |
Mike Frysinger | fbd6055 | 2017-01-03 17:28:48 -0500 | [diff] [blame] | 1237 | if (getuid() != 0) |
| 1238 | minijail_namespace_user_disable_setgroups(c->jail); |
Dylan Reid | c6ca104 | 2016-07-11 15:03:27 -0700 | [diff] [blame] | 1239 | minijail_namespace_cgroups(c->jail); |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 1240 | rc = minijail_uidmap(c->jail, config->uid_map); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1241 | if (rc) |
| 1242 | goto error_rmdir; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 1243 | rc = minijail_gidmap(c->jail, config->gid_map); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1244 | if (rc) |
| 1245 | goto error_rmdir; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1246 | |
Keshav Santhanam | 36485ff | 2016-08-02 16:21:02 -0700 | [diff] [blame] | 1247 | /* Set the UID/GID inside the container if not 0. */ |
| 1248 | uid_userns = get_userns_id(config->uid_map, config->uid); |
| 1249 | if (uid_userns < 0) |
| 1250 | goto error_rmdir; |
| 1251 | else if (uid_userns > 0) |
| 1252 | minijail_change_uid(c->jail, (uid_t) uid_userns); |
| 1253 | gid_userns = get_userns_id(config->gid_map, config->gid); |
| 1254 | if (gid_userns < 0) |
| 1255 | goto error_rmdir; |
| 1256 | else if (gid_userns > 0) |
| 1257 | minijail_change_gid(c->jail, (gid_t) gid_userns); |
| 1258 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1259 | rc = minijail_enter_pivot_root(c->jail, c->runfsroot); |
| 1260 | if (rc) |
| 1261 | goto error_rmdir; |
| 1262 | |
| 1263 | /* Add the cgroups configured above. */ |
Dmitry Torokhov | 0d253a6 | 2017-01-05 09:41:33 -0800 | [diff] [blame] | 1264 | for (i = 0; i < NUM_CGROUP_TYPES; i++) { |
| 1265 | if (c->cgroup->cgroup_tasks_paths[i]) { |
| 1266 | rc = minijail_add_to_cgroup(c->jail, |
| 1267 | c->cgroup->cgroup_tasks_paths[i]); |
| 1268 | if (rc) |
| 1269 | goto error_rmdir; |
| 1270 | } |
| 1271 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1272 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 1273 | if (config->alt_syscall_table) |
| 1274 | minijail_use_alt_syscall(c->jail, config->alt_syscall_table); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1275 | |
| 1276 | minijail_run_as_init(c->jail); |
| 1277 | |
Dylan Reid | 3da683b | 2016-04-05 03:35:35 -0700 | [diff] [blame] | 1278 | /* TODO(dgreid) - remove this once shared mounts are cleaned up. */ |
| 1279 | minijail_skip_remount_private(c->jail); |
| 1280 | |
Dylan Reid | c433584 | 2016-11-11 10:24:52 -0800 | [diff] [blame] | 1281 | if (!config->keep_fds_open) |
| 1282 | minijail_close_open_fds(c->jail); |
Luis Hector Chavez | e18e7d4 | 2016-10-12 07:35:32 -0700 | [diff] [blame] | 1283 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1284 | rc = minijail_run_pid_pipes_no_preload(c->jail, |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 1285 | config->program_argv[0], |
| 1286 | config->program_argv, |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1287 | &c->init_pid, NULL, NULL, |
| 1288 | NULL); |
| 1289 | if (rc) |
| 1290 | goto error_rmdir; |
| 1291 | return 0; |
| 1292 | |
| 1293 | error_rmdir: |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 1294 | if (!rc) |
| 1295 | rc = -errno; |
| 1296 | container_teardown(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1297 | return rc; |
| 1298 | } |
| 1299 | |
| 1300 | const char *container_root(struct container *c) |
| 1301 | { |
| 1302 | return c->runfs; |
| 1303 | } |
| 1304 | |
| 1305 | int container_pid(struct container *c) |
| 1306 | { |
| 1307 | return c->init_pid; |
| 1308 | } |
| 1309 | |
| 1310 | static int container_teardown(struct container *c) |
| 1311 | { |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1312 | int ret = 0; |
| 1313 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 1314 | unmount_external_mounts(c); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 1315 | if (c->runfsroot && c->runfs) { |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 1316 | if (umount(c->runfsroot)) |
| 1317 | ret = -errno; |
| 1318 | if (rmdir(c->runfsroot)) |
| 1319 | ret = -errno; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 1320 | FREE_AND_NULL(c->runfsroot); |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 1321 | } |
| 1322 | if (c->pid_file_path) { |
| 1323 | if (unlink(c->pid_file_path)) |
| 1324 | ret = -errno; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 1325 | FREE_AND_NULL(c->pid_file_path); |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 1326 | } |
| 1327 | if (c->runfs) { |
| 1328 | if (rmdir(c->runfs)) |
| 1329 | ret = -errno; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 1330 | FREE_AND_NULL(c->runfs); |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 1331 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1332 | return ret; |
| 1333 | } |
| 1334 | |
| 1335 | int container_wait(struct container *c) |
| 1336 | { |
Dylan Reid | cf745c5 | 2016-04-22 10:18:03 -0700 | [diff] [blame] | 1337 | int rc; |
| 1338 | |
| 1339 | do { |
| 1340 | rc = minijail_wait(c->jail); |
Luis Hector Chavez | 4641e85 | 2016-06-02 15:40:19 -0700 | [diff] [blame] | 1341 | } while (rc == -EINTR); |
Dylan Reid | cf745c5 | 2016-04-22 10:18:03 -0700 | [diff] [blame] | 1342 | |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 1343 | // If the process had already been reaped, still perform teardown. |
| 1344 | if (rc == -ECHILD || rc >= 0) { |
Dylan Reid | cf745c5 | 2016-04-22 10:18:03 -0700 | [diff] [blame] | 1345 | rc = container_teardown(c); |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 1346 | } |
Dylan Reid | cf745c5 | 2016-04-22 10:18:03 -0700 | [diff] [blame] | 1347 | return rc; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | int container_kill(struct container *c) |
| 1351 | { |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 1352 | if (kill(c->init_pid, SIGKILL) && errno != ESRCH) |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1353 | return -errno; |
| 1354 | return container_wait(c); |
| 1355 | } |