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 | |
| 21 | #include "container_cgroup.h" |
| 22 | #include "libcontainer.h" |
| 23 | #include "libminijail.h" |
| 24 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 25 | #define FREE_AND_NULL(ptr) \ |
| 26 | do { \ |
| 27 | free(ptr); \ |
| 28 | ptr = NULL; \ |
| 29 | } while(0) |
| 30 | |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 31 | static int container_teardown(struct container *c); |
| 32 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 33 | static int strdup_and_free(char **dest, const char *src) |
| 34 | { |
| 35 | char *copy = strdup(src); |
| 36 | if (!copy) |
| 37 | return -ENOMEM; |
| 38 | if (*dest) |
| 39 | free(*dest); |
| 40 | *dest = copy; |
| 41 | return 0; |
| 42 | } |
| 43 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 44 | struct container_mount { |
| 45 | char *name; |
| 46 | char *source; |
| 47 | char *destination; |
| 48 | char *type; |
| 49 | char *data; |
| 50 | int flags; |
| 51 | int uid; |
| 52 | int gid; |
| 53 | int mode; |
| 54 | int mount_in_ns; /* True if mount should happen in new vfs ns */ |
| 55 | int create; /* True if target should be created if it doesn't exist */ |
| 56 | }; |
| 57 | |
| 58 | struct container_device { |
| 59 | char type; /* 'c' or 'b' for char or block */ |
| 60 | char *path; |
| 61 | int fs_permissions; |
| 62 | int major; |
| 63 | int minor; |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 64 | int copy_minor; /* Copy the minor from existing node, ignores |minor| */ |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 65 | int uid; |
| 66 | int gid; |
| 67 | int read_allowed; |
| 68 | int write_allowed; |
| 69 | int modify_allowed; |
| 70 | }; |
| 71 | |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame^] | 72 | struct container_cpu_cgroup { |
| 73 | int shares; |
| 74 | int quota; |
| 75 | int period; |
| 76 | int rt_runtime; |
| 77 | int rt_period; |
| 78 | }; |
| 79 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 80 | /* |
| 81 | * Structure that configures how the container is run. |
| 82 | * |
| 83 | * rootfs - Path to the root of the container's filesystem. |
| 84 | * program_argv - The program to run and args, e.g. "/sbin/init". |
| 85 | * num_args - Number of args in program_argv. |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 86 | * uid - The uid the container will run as. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 87 | * 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] | 88 | * gid - The gid the container will run as. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 89 | * gid_map - Mapping of GIDs in the container, e.g. "0 100000 1024" |
| 90 | * alt_syscall_table - Syscall table to use or NULL if none. |
| 91 | * mounts - Filesystems to mount in the new namespace. |
| 92 | * num_mounts - Number of above. |
| 93 | * devices - Device nodes to create. |
| 94 | * num_devices - Number of above. |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 95 | * run_setfiles - Should run setfiles on mounts to enable selinux. |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame^] | 96 | * cpu_cgparams - CPU cgroup params. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 97 | */ |
| 98 | struct container_config { |
| 99 | char *rootfs; |
| 100 | char **program_argv; |
| 101 | size_t num_args; |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 102 | uid_t uid; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 103 | char *uid_map; |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 104 | gid_t gid; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 105 | char *gid_map; |
| 106 | char *alt_syscall_table; |
| 107 | struct container_mount *mounts; |
| 108 | size_t num_mounts; |
| 109 | struct container_device *devices; |
| 110 | size_t num_devices; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 111 | char *run_setfiles; |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame^] | 112 | struct container_cpu_cgroup cpu_cgparams; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | struct container_config *container_config_create() |
| 116 | { |
| 117 | return calloc(1, sizeof(struct container_config)); |
| 118 | } |
| 119 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 120 | static void container_free_program_args(struct container_config *c) |
| 121 | { |
| 122 | int i; |
| 123 | |
| 124 | if (!c->program_argv) |
| 125 | return; |
| 126 | for (i = 0; i < c->num_args; ++i) { |
| 127 | FREE_AND_NULL(c->program_argv[i]); |
| 128 | } |
| 129 | FREE_AND_NULL(c->program_argv); |
| 130 | } |
| 131 | |
| 132 | static void container_config_free_mount(struct container_mount *mount) |
| 133 | { |
| 134 | FREE_AND_NULL(mount->name); |
| 135 | FREE_AND_NULL(mount->source); |
| 136 | FREE_AND_NULL(mount->destination); |
| 137 | FREE_AND_NULL(mount->type); |
| 138 | FREE_AND_NULL(mount->data); |
| 139 | } |
| 140 | |
| 141 | static void container_config_free_device(struct container_device *device) |
| 142 | { |
| 143 | FREE_AND_NULL(device->path); |
| 144 | } |
| 145 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 146 | void container_config_destroy(struct container_config *c) |
| 147 | { |
| 148 | size_t i; |
| 149 | |
| 150 | if (c == NULL) |
| 151 | return; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 152 | FREE_AND_NULL(c->rootfs); |
| 153 | container_free_program_args(c); |
| 154 | FREE_AND_NULL(c->uid_map); |
| 155 | FREE_AND_NULL(c->gid_map); |
| 156 | FREE_AND_NULL(c->alt_syscall_table); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 157 | for (i = 0; i < c->num_mounts; ++i) { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 158 | container_config_free_mount(&c->mounts[i]); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 159 | } |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 160 | FREE_AND_NULL(c->mounts); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 161 | for (i = 0; i < c->num_devices; ++i) { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 162 | container_config_free_device(&c->devices[i]); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 163 | } |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 164 | FREE_AND_NULL(c->devices); |
| 165 | FREE_AND_NULL(c->run_setfiles); |
| 166 | FREE_AND_NULL(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | int container_config_rootfs(struct container_config *c, const char *rootfs) |
| 170 | { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 171 | return strdup_and_free(&c->rootfs, rootfs); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 174 | const char *container_config_get_rootfs(const struct container_config *c) |
| 175 | { |
| 176 | return c->rootfs; |
| 177 | } |
| 178 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 179 | int container_config_program_argv(struct container_config *c, |
| 180 | char **argv, size_t num_args) |
| 181 | { |
| 182 | size_t i; |
| 183 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 184 | container_free_program_args(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 185 | c->num_args = num_args; |
| 186 | c->program_argv = calloc(num_args + 1, sizeof(char *)); |
| 187 | if (!c->program_argv) |
| 188 | return -ENOMEM; |
| 189 | for (i = 0; i < num_args; ++i) { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 190 | if (strdup_and_free(&c->program_argv[i], argv[i])) |
| 191 | goto error_free_return; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 192 | } |
| 193 | c->program_argv[num_args] = NULL; |
| 194 | return 0; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 195 | |
| 196 | error_free_return: |
| 197 | container_free_program_args(c); |
| 198 | return -ENOMEM; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 201 | size_t container_config_get_num_program_args(const struct container_config *c) |
| 202 | { |
| 203 | return c->num_args; |
| 204 | } |
| 205 | |
| 206 | const char *container_config_get_program_arg(const struct container_config *c, |
| 207 | size_t index) |
| 208 | { |
| 209 | if (index >= c->num_args) |
| 210 | return NULL; |
| 211 | return c->program_argv[index]; |
| 212 | } |
| 213 | |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 214 | void container_config_uid(struct container_config *c, uid_t uid) |
| 215 | { |
| 216 | c->uid = uid; |
| 217 | } |
| 218 | |
| 219 | uid_t container_config_get_uid(const struct container_config *c) |
| 220 | { |
| 221 | return c->uid; |
| 222 | } |
| 223 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 224 | int container_config_uid_map(struct container_config *c, const char *uid_map) |
| 225 | { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 226 | return strdup_and_free(&c->uid_map, uid_map); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 229 | void container_config_gid(struct container_config *c, gid_t gid) |
| 230 | { |
| 231 | c->gid = gid; |
| 232 | } |
| 233 | |
| 234 | gid_t container_config_get_gid(const struct container_config *c) |
| 235 | { |
| 236 | return c->gid; |
| 237 | } |
| 238 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 239 | int container_config_gid_map(struct container_config *c, const char *gid_map) |
| 240 | { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 241 | return strdup_and_free(&c->gid_map, gid_map); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | int container_config_alt_syscall_table(struct container_config *c, |
| 245 | const char *alt_syscall_table) |
| 246 | { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 247 | return strdup_and_free(&c->alt_syscall_table, alt_syscall_table); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | int container_config_add_mount(struct container_config *c, |
| 251 | const char *name, |
| 252 | const char *source, |
| 253 | const char *destination, |
| 254 | const char *type, |
| 255 | const char *data, |
| 256 | int flags, |
| 257 | int uid, |
| 258 | int gid, |
| 259 | int mode, |
| 260 | int mount_in_ns, |
| 261 | int create) |
| 262 | { |
| 263 | struct container_mount *mount_ptr; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 264 | struct container_mount *current_mount; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 265 | |
| 266 | if (name == NULL || source == NULL || |
| 267 | destination == NULL || type == NULL) |
| 268 | return -EINVAL; |
| 269 | |
| 270 | mount_ptr = realloc(c->mounts, |
| 271 | sizeof(c->mounts[0]) * (c->num_mounts + 1)); |
| 272 | if (!mount_ptr) |
| 273 | return -ENOMEM; |
| 274 | c->mounts = mount_ptr; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 275 | current_mount = &c->mounts[c->num_mounts]; |
| 276 | memset(current_mount, 0, sizeof(struct container_mount)); |
| 277 | |
| 278 | if (strdup_and_free(¤t_mount->name, name)) |
| 279 | goto error_free_return; |
| 280 | if (strdup_and_free(¤t_mount->source, source)) |
| 281 | goto error_free_return; |
| 282 | if (strdup_and_free(¤t_mount->destination, destination)) |
| 283 | goto error_free_return; |
| 284 | if (strdup_and_free(¤t_mount->type, type)) |
| 285 | goto error_free_return; |
| 286 | if (data && strdup_and_free(¤t_mount->data, data)) |
| 287 | goto error_free_return; |
| 288 | current_mount->flags = flags; |
| 289 | current_mount->uid = uid; |
| 290 | current_mount->gid = gid; |
| 291 | current_mount->mode = mode; |
| 292 | current_mount->mount_in_ns = mount_in_ns; |
| 293 | current_mount->create = create; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 294 | ++c->num_mounts; |
| 295 | return 0; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 296 | |
| 297 | error_free_return: |
| 298 | container_config_free_mount(current_mount); |
| 299 | return -ENOMEM; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | int container_config_add_device(struct container_config *c, |
| 303 | char type, |
| 304 | const char *path, |
| 305 | int fs_permissions, |
| 306 | int major, |
| 307 | int minor, |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 308 | int copy_minor, |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 309 | int uid, |
| 310 | int gid, |
| 311 | int read_allowed, |
| 312 | int write_allowed, |
| 313 | int modify_allowed) |
| 314 | { |
| 315 | struct container_device *dev_ptr; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 316 | struct container_device *current_dev; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 317 | |
| 318 | if (path == NULL) |
| 319 | return -EINVAL; |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 320 | /* If using a dynamic minor number, ensure that minor is -1. */ |
| 321 | if (copy_minor && (minor != -1)) |
| 322 | return -EINVAL; |
| 323 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 324 | dev_ptr = realloc(c->devices, |
| 325 | sizeof(c->devices[0]) * (c->num_devices + 1)); |
| 326 | if (!dev_ptr) |
| 327 | return -ENOMEM; |
| 328 | c->devices = dev_ptr; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 329 | current_dev = &c->devices[c->num_devices]; |
| 330 | memset(current_dev, 0, sizeof(struct container_device)); |
| 331 | |
| 332 | current_dev->type = type; |
| 333 | if (strdup_and_free(¤t_dev->path, path)) |
| 334 | goto error_free_return; |
| 335 | current_dev->fs_permissions = fs_permissions; |
| 336 | current_dev->major = major; |
| 337 | current_dev->minor = minor; |
| 338 | current_dev->copy_minor = copy_minor; |
| 339 | current_dev->uid = uid; |
| 340 | current_dev->gid = gid; |
| 341 | current_dev->read_allowed = read_allowed; |
| 342 | current_dev->write_allowed = write_allowed; |
| 343 | current_dev->modify_allowed = modify_allowed; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 344 | ++c->num_devices; |
| 345 | return 0; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 346 | |
| 347 | error_free_return: |
| 348 | container_config_free_device(current_dev); |
| 349 | return -ENOMEM; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 352 | int container_config_run_setfiles(struct container_config *c, |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 353 | const char *setfiles_cmd) |
| 354 | { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 355 | return strdup_and_free(&c->run_setfiles, setfiles_cmd); |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 356 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 357 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 358 | const char *container_config_get_run_setfiles(const struct container_config *c) |
| 359 | { |
| 360 | return c->run_setfiles; |
| 361 | } |
| 362 | |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame^] | 363 | int container_config_set_cpu_shares(struct container_config *c, int shares) |
| 364 | { |
| 365 | /* CPU shares must be 2 or higher. */ |
| 366 | if (shares < 2) |
| 367 | return -EINVAL; |
| 368 | |
| 369 | c->cpu_cgparams.shares = shares; |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | int container_config_set_cpu_cfs_params(struct container_config *c, |
| 374 | int quota, |
| 375 | int period) |
| 376 | { |
| 377 | /* |
| 378 | * quota could be set higher than period to utilize more than one CPU. |
| 379 | * quota could also be set as -1 to indicate the cgroup does not adhere |
| 380 | * to any CPU time restrictions. |
| 381 | */ |
| 382 | if (quota <= 0 && quota != -1) |
| 383 | return -EINVAL; |
| 384 | if (period <= 0) |
| 385 | return -EINVAL; |
| 386 | |
| 387 | c->cpu_cgparams.quota = quota; |
| 388 | c->cpu_cgparams.period = period; |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | int container_config_set_cpu_rt_params(struct container_config *c, |
| 393 | int rt_runtime, |
| 394 | int rt_period) |
| 395 | { |
| 396 | /* |
| 397 | * rt_runtime could be set as 0 to prevent the cgroup from using |
| 398 | * realtime CPU. |
| 399 | */ |
| 400 | if (rt_runtime < 0 || rt_runtime >= rt_period) |
| 401 | return -EINVAL; |
| 402 | |
| 403 | c->cpu_cgparams.rt_runtime = rt_runtime; |
| 404 | c->cpu_cgparams.rt_period = rt_period; |
| 405 | return 0; |
| 406 | } |
| 407 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 408 | /* |
| 409 | * Container manipulation |
| 410 | */ |
| 411 | struct container { |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 412 | struct container_cgroup *cgroup; |
| 413 | struct minijail *jail; |
| 414 | pid_t init_pid; |
| 415 | char *runfs; |
| 416 | char *rundir; |
| 417 | char *runfsroot; |
| 418 | char *pid_file_path; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 419 | char **ext_mounts; /* Mounts made outside of the minijail */ |
| 420 | size_t num_ext_mounts; |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 421 | char *name; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | struct container *container_new(const char *name, |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 425 | const char *rundir) |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 426 | { |
| 427 | struct container *c; |
| 428 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 429 | c = calloc(1, sizeof(*c)); |
Dylan Reid | b435c68 | 2016-04-12 04:17:49 -0700 | [diff] [blame] | 430 | if (!c) |
| 431 | return NULL; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 432 | c->cgroup = container_cgroup_new(name, "/sys/fs/cgroup"); |
| 433 | c->rundir = strdup(rundir); |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 434 | c->name = strdup(name); |
| 435 | if (!c->cgroup || !c->rundir || !c->name) { |
Dylan Reid | 684975e | 2016-05-02 15:44:47 -0700 | [diff] [blame] | 436 | container_destroy(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 437 | return NULL; |
Dylan Reid | b435c68 | 2016-04-12 04:17:49 -0700 | [diff] [blame] | 438 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 439 | return c; |
| 440 | } |
| 441 | |
| 442 | void container_destroy(struct container *c) |
| 443 | { |
Dylan Reid | 684975e | 2016-05-02 15:44:47 -0700 | [diff] [blame] | 444 | if (c->cgroup) |
| 445 | container_cgroup_destroy(c->cgroup); |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 446 | if (c->jail) |
| 447 | minijail_destroy(c->jail); |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 448 | FREE_AND_NULL(c->name); |
| 449 | FREE_AND_NULL(c->rundir); |
| 450 | FREE_AND_NULL(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static int make_dir(const char *path, int uid, int gid, int mode) |
| 454 | { |
| 455 | if (mkdir(path, mode)) |
| 456 | return -errno; |
| 457 | if (chmod(path, mode)) |
| 458 | return -errno; |
| 459 | if (chown(path, uid, gid)) |
| 460 | return -errno; |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static int touch_file(const char *path, int uid, int gid, int mode) |
| 465 | { |
| 466 | int rc; |
| 467 | int fd = open(path, O_RDWR | O_CREAT, mode); |
| 468 | if (fd < 0) |
| 469 | return -errno; |
| 470 | rc = fchown(fd, uid, gid); |
| 471 | close(fd); |
| 472 | |
| 473 | if (rc) |
| 474 | return -errno; |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /* Make sure the mount target exists in the new rootfs. Create if needed and |
| 479 | * possible. |
| 480 | */ |
| 481 | static int setup_mount_destination(const struct container_mount *mnt, |
Dylan Reid | 2149be9 | 2016-04-28 18:38:57 -0700 | [diff] [blame] | 482 | const char *source, |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 483 | const char *dest) |
| 484 | { |
| 485 | int rc; |
| 486 | struct stat st_buf; |
| 487 | |
| 488 | rc = stat(dest, &st_buf); |
| 489 | if (rc == 0) /* destination exists */ |
| 490 | return 0; |
| 491 | |
| 492 | /* Try to create the destination. Either make directory or touch a file |
| 493 | * depending on the source type. |
| 494 | */ |
Dylan Reid | 2149be9 | 2016-04-28 18:38:57 -0700 | [diff] [blame] | 495 | rc = stat(source, &st_buf); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 496 | if (rc || S_ISDIR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)) |
| 497 | return make_dir(dest, mnt->uid, mnt->gid, mnt->mode); |
| 498 | |
| 499 | return touch_file(dest, mnt->uid, mnt->gid, mnt->mode); |
| 500 | } |
| 501 | |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 502 | /* Fork and exec the setfiles command to configure the selinux policy. */ |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 503 | static int run_setfiles_command(const struct container *c, |
| 504 | const struct container_config *config, |
| 505 | const char *dest) |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 506 | { |
| 507 | int rc; |
| 508 | int status; |
| 509 | int pid; |
| 510 | char *context_path; |
| 511 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 512 | if (!config->run_setfiles) |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 513 | return 0; |
| 514 | |
Dylan Reid | b362183 | 2016-03-24 10:24:57 -0700 | [diff] [blame] | 515 | /* Really gross hack to avoid setfiles on /data, this should be removed |
| 516 | * when data isn't under /home/chronos/user where we can't access it as |
| 517 | * the android user. |
| 518 | * TODO(b/28705740) - Fix permission to the data directory. |
| 519 | */ |
| 520 | if (strlen(dest) >= 5 && !strcmp(&dest[strlen(dest) - 5], "/data")) |
| 521 | return 0; |
| 522 | |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 523 | if (asprintf(&context_path, "%s/file_contexts", |
| 524 | c->runfsroot) < 0) |
| 525 | return -errno; |
| 526 | |
| 527 | pid = fork(); |
| 528 | if (pid == 0) { |
| 529 | const char *argv[] = { |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 530 | config->run_setfiles, |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 531 | "-r", |
| 532 | c->runfsroot, |
| 533 | context_path, |
| 534 | dest, |
| 535 | NULL, |
| 536 | }; |
| 537 | const char *env[] = { |
| 538 | NULL, |
| 539 | }; |
| 540 | |
| 541 | execve(argv[0], (char *const*)argv, (char *const*)env); |
| 542 | |
| 543 | /* Command failed to exec if execve returns. */ |
| 544 | _exit(-errno); |
| 545 | } |
| 546 | free(context_path); |
| 547 | if (pid < 0) |
| 548 | return -errno; |
| 549 | do { |
| 550 | rc = waitpid(pid, &status, 0); |
| 551 | } while (rc == -1 && errno == EINTR); |
| 552 | if (rc < 0) |
| 553 | return -errno; |
| 554 | return status; |
| 555 | } |
| 556 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 557 | /* |
| 558 | * Unmounts anything we mounted in this mount namespace in the opposite order |
| 559 | * that they were mounted. |
| 560 | */ |
| 561 | static int unmount_external_mounts(struct container *c) |
| 562 | { |
| 563 | int ret = 0; |
| 564 | |
| 565 | while (c->num_ext_mounts) { |
| 566 | c->num_ext_mounts--; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 567 | if (!c->ext_mounts[c->num_ext_mounts]) |
| 568 | continue; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 569 | if (umount(c->ext_mounts[c->num_ext_mounts])) |
| 570 | ret = -errno; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 571 | FREE_AND_NULL(c->ext_mounts[c->num_ext_mounts]); |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 572 | } |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 573 | FREE_AND_NULL(c->ext_mounts); |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 574 | return ret; |
| 575 | } |
| 576 | |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 577 | static int do_container_mount(struct container *c, |
| 578 | const struct container_mount *mnt) |
| 579 | { |
| 580 | char *source = NULL; |
| 581 | char *dest = NULL; |
| 582 | int rc = 0; |
| 583 | |
| 584 | if (asprintf(&dest, "%s%s", c->runfsroot, mnt->destination) < 0) |
| 585 | return -errno; |
| 586 | |
| 587 | /* |
| 588 | * If it's a bind mount relative to rootfs, append source to |
| 589 | * rootfs path, otherwise source path is absolute. |
| 590 | */ |
| 591 | if ((mnt->flags & MS_BIND) && mnt->source[0] != '/') { |
| 592 | if (asprintf(&source, "%s/%s", c->runfsroot, mnt->source) < 0) |
| 593 | goto error_free_return; |
| 594 | } else { |
| 595 | if (asprintf(&source, "%s", mnt->source) < 0) |
| 596 | goto error_free_return; |
| 597 | } |
| 598 | |
| 599 | if (mnt->create) { |
| 600 | rc = setup_mount_destination(mnt, source, dest); |
| 601 | if (rc) |
| 602 | goto error_free_return; |
| 603 | } |
| 604 | if (mnt->mount_in_ns) { |
| 605 | /* We can mount this with minijail. */ |
| 606 | rc = minijail_mount(c->jail, source, mnt->destination, |
| 607 | mnt->type, mnt->flags); |
| 608 | if (rc) |
| 609 | goto error_free_return; |
| 610 | } else { |
| 611 | /* Mount this externally and unmount it on exit. */ |
| 612 | if (mount(source, dest, mnt->type, mnt->flags, |
| 613 | mnt->data)) |
| 614 | goto error_free_return; |
| 615 | /* Save this to unmount when shutting down. */ |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 616 | rc = strdup_and_free(&c->ext_mounts[c->num_ext_mounts], dest); |
| 617 | if (rc) |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 618 | goto error_free_return; |
| 619 | c->num_ext_mounts++; |
| 620 | } |
| 621 | |
| 622 | goto exit; |
| 623 | |
| 624 | error_free_return: |
| 625 | if (!rc) |
| 626 | rc = -errno; |
| 627 | exit: |
| 628 | free(source); |
| 629 | free(dest); |
| 630 | return rc; |
| 631 | } |
| 632 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 633 | static int do_container_mounts(struct container *c, |
| 634 | const struct container_config *config) |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 635 | { |
| 636 | unsigned int i; |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 637 | int rc = 0; |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 638 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 639 | unmount_external_mounts(c); |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 640 | /* |
| 641 | * Allocate space to track anything we mount in our mount namespace. |
| 642 | * This over-allocates as it has space for all mounts. |
| 643 | */ |
| 644 | c->ext_mounts = calloc(config->num_mounts, sizeof(*c->ext_mounts)); |
| 645 | if (!c->ext_mounts) |
| 646 | return -errno; |
| 647 | |
| 648 | for (i = 0; i < config->num_mounts; ++i) { |
Luis Hector Chavez | 3341ed6 | 2016-06-06 08:04:04 -0700 | [diff] [blame] | 649 | rc = do_container_mount(c, &config->mounts[i]); |
| 650 | if (rc) |
| 651 | goto error_free_return; |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 652 | } |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 653 | |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 654 | return 0; |
Dylan Reid | 2149be9 | 2016-04-28 18:38:57 -0700 | [diff] [blame] | 655 | |
| 656 | error_free_return: |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 657 | unmount_external_mounts(c); |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 658 | return rc; |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 661 | static int container_create_device(const struct container *c, |
| 662 | const struct container_device *dev, |
| 663 | int minor) |
| 664 | { |
| 665 | char *path = NULL; |
| 666 | int rc = 0; |
| 667 | int mode; |
| 668 | |
| 669 | switch (dev->type) { |
| 670 | case 'b': |
| 671 | mode = S_IFBLK; |
| 672 | break; |
| 673 | case 'c': |
| 674 | mode = S_IFCHR; |
| 675 | break; |
| 676 | default: |
| 677 | return -EINVAL; |
| 678 | } |
| 679 | mode |= dev->fs_permissions; |
| 680 | |
| 681 | if (asprintf(&path, "%s%s", c->runfsroot, dev->path) < 0) |
| 682 | goto error_free_return; |
| 683 | if (mknod(path, mode, makedev(dev->major, minor)) && errno != EEXIST) |
| 684 | goto error_free_return; |
| 685 | if (chown(path, dev->uid, dev->gid)) |
| 686 | goto error_free_return; |
| 687 | if (chmod(path, dev->fs_permissions)) |
| 688 | goto error_free_return; |
| 689 | |
| 690 | goto exit; |
| 691 | |
| 692 | error_free_return: |
| 693 | rc = -errno; |
| 694 | exit: |
| 695 | free(path); |
| 696 | return rc; |
| 697 | } |
| 698 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 699 | int container_start(struct container *c, const struct container_config *config) |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 700 | { |
Dylan Reid | b362183 | 2016-03-24 10:24:57 -0700 | [diff] [blame] | 701 | static const mode_t root_dir_mode = 0660; |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 702 | int rc = 0; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 703 | unsigned int i; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 704 | const char *rootfs = config->rootfs; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 705 | char *runfs_template = NULL; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 706 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 707 | if (!c) |
| 708 | return -EINVAL; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 709 | if (!config) |
| 710 | return -EINVAL; |
| 711 | if (!config->program_argv || !config->program_argv[0]) |
| 712 | return -EINVAL; |
| 713 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 714 | if (asprintf(&runfs_template, "%s/%s_XXXXXX", c->rundir, c->name) < 0) |
| 715 | return -errno; |
| 716 | |
| 717 | c->runfs = mkdtemp(runfs_template); |
| 718 | if (!c->runfs) { |
| 719 | free(runfs_template); |
| 720 | return -errno; |
| 721 | } |
Dylan Reid | b362183 | 2016-03-24 10:24:57 -0700 | [diff] [blame] | 722 | /* Make sure the container uid can access the rootfs. */ |
Dylan Reid | 4c6af2e | 2016-06-22 18:04:24 -0700 | [diff] [blame] | 723 | if (chmod(c->runfs, 0700)) |
Dylan Reid | b362183 | 2016-03-24 10:24:57 -0700 | [diff] [blame] | 724 | goto error_rmdir; |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 725 | if (chown(c->runfs, config->uid, config->gid)) |
| 726 | goto error_rmdir; |
Dylan Reid | b362183 | 2016-03-24 10:24:57 -0700 | [diff] [blame] | 727 | |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 728 | if (asprintf(&c->runfsroot, "%s/root", c->runfs) < 0) |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 729 | goto error_rmdir; |
| 730 | |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 731 | if (mkdir(c->runfsroot, root_dir_mode)) |
| 732 | goto error_rmdir; |
| 733 | if (chmod(c->runfsroot, root_dir_mode)) |
| 734 | goto error_rmdir; |
| 735 | |
| 736 | if (mount(rootfs, c->runfsroot, "", MS_BIND | MS_RDONLY, NULL)) |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 737 | goto error_rmdir; |
| 738 | |
| 739 | c->jail = minijail_new(); |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 740 | if (!c->jail) |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 741 | goto error_rmdir; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 742 | |
Luis Hector Chavez | 8e7b6d5 | 2016-06-02 20:40:43 -0700 | [diff] [blame] | 743 | rc = do_container_mounts(c, config); |
| 744 | if (rc) |
Dylan Reid | 7daf998 | 2016-04-28 16:55:42 -0700 | [diff] [blame] | 745 | goto error_rmdir; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 746 | |
| 747 | c->cgroup->ops->deny_all_devices(c->cgroup); |
| 748 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 749 | for (i = 0; i < config->num_devices; i++) { |
| 750 | const struct container_device *dev = &config->devices[i]; |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 751 | int minor = dev->minor; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 752 | |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 753 | if (dev->copy_minor) { |
| 754 | struct stat st_buff; |
| 755 | if (stat(dev->path, &st_buff) < 0) |
| 756 | goto error_rmdir; |
| 757 | /* Use the minor macro to extract the device number. */ |
| 758 | minor = minor(st_buff.st_rdev); |
| 759 | } |
| 760 | if (minor >= 0) { |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 761 | rc = container_create_device(c, dev, minor); |
| 762 | if (rc) |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 763 | goto error_rmdir; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | rc = c->cgroup->ops->add_device(c->cgroup, dev->major, |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 767 | minor, dev->read_allowed, |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 768 | dev->write_allowed, |
| 769 | dev->modify_allowed, dev->type); |
| 770 | if (rc) |
| 771 | goto error_rmdir; |
| 772 | } |
| 773 | |
Dylan Reid | d722958 | 2016-04-27 17:08:40 -0700 | [diff] [blame] | 774 | /* Potentailly run setfiles on mounts configured outside of the jail */ |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 775 | for (i = 0; i < config->num_mounts; i++) { |
| 776 | const struct container_mount *mnt = &config->mounts[i]; |
Dylan Reid | d722958 | 2016-04-27 17:08:40 -0700 | [diff] [blame] | 777 | char *dest; |
| 778 | |
| 779 | if (mnt->mount_in_ns) |
| 780 | continue; |
| 781 | if (asprintf(&dest, "%s%s", c->runfsroot, mnt->destination) < 0) |
| 782 | goto error_rmdir; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 783 | rc = run_setfiles_command(c, config, dest); |
Dylan Reid | d722958 | 2016-04-27 17:08:40 -0700 | [diff] [blame] | 784 | free(dest); |
| 785 | if (rc) |
| 786 | goto error_rmdir; |
| 787 | } |
| 788 | |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame^] | 789 | /* Setup CPU cgroup params. */ |
| 790 | if (config->cpu_cgparams.shares) { |
| 791 | rc = c->cgroup->ops->set_cpu_shares( |
| 792 | c->cgroup, config->cpu_cgparams.shares); |
| 793 | if (rc) |
| 794 | goto error_rmdir; |
| 795 | } |
| 796 | if (config->cpu_cgparams.period) { |
| 797 | rc = c->cgroup->ops->set_cpu_quota( |
| 798 | c->cgroup, config->cpu_cgparams.quota); |
| 799 | if (rc) |
| 800 | goto error_rmdir; |
| 801 | rc = c->cgroup->ops->set_cpu_period( |
| 802 | c->cgroup, config->cpu_cgparams.period); |
| 803 | if (rc) |
| 804 | goto error_rmdir; |
| 805 | } |
| 806 | if (config->cpu_cgparams.rt_period) { |
| 807 | rc = c->cgroup->ops->set_cpu_rt_runtime( |
| 808 | c->cgroup, config->cpu_cgparams.rt_runtime); |
| 809 | if (rc) |
| 810 | goto error_rmdir; |
| 811 | rc = c->cgroup->ops->set_cpu_rt_period( |
| 812 | c->cgroup, config->cpu_cgparams.rt_period); |
| 813 | if (rc) |
| 814 | goto error_rmdir; |
| 815 | } |
| 816 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 817 | /* Setup and start the container with libminijail. */ |
| 818 | if (asprintf(&c->pid_file_path, "%s/container.pid", c->runfs) < 0) |
| 819 | goto error_rmdir; |
| 820 | minijail_write_pid_file(c->jail, c->pid_file_path); |
| 821 | minijail_reset_signal_mask(c->jail); |
| 822 | |
| 823 | /* Setup container namespaces. */ |
| 824 | minijail_namespace_ipc(c->jail); |
| 825 | minijail_namespace_vfs(c->jail); |
| 826 | minijail_namespace_net(c->jail); |
| 827 | minijail_namespace_pids(c->jail); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 828 | minijail_namespace_user(c->jail); |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 829 | rc = minijail_uidmap(c->jail, config->uid_map); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 830 | if (rc) |
| 831 | goto error_rmdir; |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 832 | rc = minijail_gidmap(c->jail, config->gid_map); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 833 | if (rc) |
| 834 | goto error_rmdir; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 835 | |
| 836 | rc = minijail_enter_pivot_root(c->jail, c->runfsroot); |
| 837 | if (rc) |
| 838 | goto error_rmdir; |
| 839 | |
| 840 | /* Add the cgroups configured above. */ |
| 841 | rc = minijail_add_to_cgroup(c->jail, cgroup_cpu_tasks_path(c->cgroup)); |
| 842 | if (rc) |
| 843 | goto error_rmdir; |
| 844 | rc = minijail_add_to_cgroup(c->jail, |
| 845 | cgroup_cpuacct_tasks_path(c->cgroup)); |
| 846 | if (rc) |
| 847 | goto error_rmdir; |
| 848 | rc = minijail_add_to_cgroup(c->jail, |
| 849 | cgroup_devices_tasks_path(c->cgroup)); |
| 850 | if (rc) |
| 851 | goto error_rmdir; |
| 852 | rc = minijail_add_to_cgroup(c->jail, |
| 853 | cgroup_freezer_tasks_path(c->cgroup)); |
| 854 | if (rc) |
| 855 | goto error_rmdir; |
| 856 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 857 | if (config->alt_syscall_table) |
| 858 | minijail_use_alt_syscall(c->jail, config->alt_syscall_table); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 859 | |
| 860 | minijail_run_as_init(c->jail); |
| 861 | |
Dylan Reid | 3da683b | 2016-04-05 03:35:35 -0700 | [diff] [blame] | 862 | /* TODO(dgreid) - remove this once shared mounts are cleaned up. */ |
| 863 | minijail_skip_remount_private(c->jail); |
| 864 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 865 | rc = minijail_run_pid_pipes_no_preload(c->jail, |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 866 | config->program_argv[0], |
| 867 | config->program_argv, |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 868 | &c->init_pid, NULL, NULL, |
| 869 | NULL); |
| 870 | if (rc) |
| 871 | goto error_rmdir; |
| 872 | return 0; |
| 873 | |
| 874 | error_rmdir: |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 875 | if (!rc) |
| 876 | rc = -errno; |
| 877 | container_teardown(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 878 | return rc; |
| 879 | } |
| 880 | |
| 881 | const char *container_root(struct container *c) |
| 882 | { |
| 883 | return c->runfs; |
| 884 | } |
| 885 | |
| 886 | int container_pid(struct container *c) |
| 887 | { |
| 888 | return c->init_pid; |
| 889 | } |
| 890 | |
| 891 | static int container_teardown(struct container *c) |
| 892 | { |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 893 | int ret = 0; |
| 894 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 895 | unmount_external_mounts(c); |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 896 | if (c->runfsroot) { |
| 897 | if (umount(c->runfsroot)) |
| 898 | ret = -errno; |
| 899 | if (rmdir(c->runfsroot)) |
| 900 | ret = -errno; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 901 | FREE_AND_NULL(c->runfsroot); |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 902 | } |
| 903 | if (c->pid_file_path) { |
| 904 | if (unlink(c->pid_file_path)) |
| 905 | ret = -errno; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 906 | FREE_AND_NULL(c->pid_file_path); |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 907 | } |
| 908 | if (c->runfs) { |
| 909 | if (rmdir(c->runfs)) |
| 910 | ret = -errno; |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 911 | FREE_AND_NULL(c->runfs); |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 912 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 913 | return ret; |
| 914 | } |
| 915 | |
| 916 | int container_wait(struct container *c) |
| 917 | { |
Dylan Reid | cf745c5 | 2016-04-22 10:18:03 -0700 | [diff] [blame] | 918 | int rc; |
| 919 | |
| 920 | do { |
| 921 | rc = minijail_wait(c->jail); |
Luis Hector Chavez | 4641e85 | 2016-06-02 15:40:19 -0700 | [diff] [blame] | 922 | } while (rc == -EINTR); |
Dylan Reid | cf745c5 | 2016-04-22 10:18:03 -0700 | [diff] [blame] | 923 | |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 924 | // If the process had already been reaped, still perform teardown. |
| 925 | if (rc == -ECHILD || rc >= 0) { |
Dylan Reid | cf745c5 | 2016-04-22 10:18:03 -0700 | [diff] [blame] | 926 | rc = container_teardown(c); |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 927 | } |
Dylan Reid | cf745c5 | 2016-04-22 10:18:03 -0700 | [diff] [blame] | 928 | return rc; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | int container_kill(struct container *c) |
| 932 | { |
Luis Hector Chavez | 945af48 | 2016-06-03 08:39:34 -0700 | [diff] [blame] | 933 | if (kill(c->init_pid, SIGKILL) && errno != ESRCH) |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 934 | return -errno; |
| 935 | return container_wait(c); |
| 936 | } |