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