blob: 208ac7619d79a366201f61ed661fb18bc440e833 [file] [log] [blame]
Dylan Reid837c74a2016-01-22 17:25:21 -08001/* 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 Reid2bd9ea92016-04-07 20:57:47 -070018#include <sys/wait.h>
Dylan Reid837c74a2016-01-22 17:25:21 -080019#include <unistd.h>
20
21#include "container_cgroup.h"
22#include "libcontainer.h"
23#include "libminijail.h"
24
Luis Hector Chavez479b95f2016-06-06 08:01:05 -070025#define FREE_AND_NULL(ptr) \
26do { \
27 free(ptr); \
28 ptr = NULL; \
29} while(0)
30
Luis Hector Chavez945af482016-06-03 08:39:34 -070031static int container_teardown(struct container *c);
32
Luis Hector Chavez479b95f2016-06-06 08:01:05 -070033static 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 Reid837c74a2016-01-22 17:25:21 -080044struct 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
58struct 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 Reid355d5e42016-04-29 16:53:31 -070064 int copy_minor; /* Copy the minor from existing node, ignores |minor| */
Dylan Reid837c74a2016-01-22 17:25:21 -080065 int uid;
66 int gid;
67 int read_allowed;
68 int write_allowed;
69 int modify_allowed;
70};
71
Chinyue Chenfac909e2016-06-24 14:17:42 +080072struct container_cpu_cgroup {
73 int shares;
74 int quota;
75 int period;
76 int rt_runtime;
77 int rt_period;
78};
79
Dylan Reid837c74a2016-01-22 17:25:21 -080080/*
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 Reid1874feb2016-06-22 17:53:50 -070086 * uid - The uid the container will run as.
Dylan Reid837c74a2016-01-22 17:25:21 -080087 * uid_map - Mapping of UIDs in the container, e.g. "0 100000 1024"
Dylan Reid1874feb2016-06-22 17:53:50 -070088 * gid - The gid the container will run as.
Dylan Reid837c74a2016-01-22 17:25:21 -080089 * 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 Reid2bd9ea92016-04-07 20:57:47 -070095 * run_setfiles - Should run setfiles on mounts to enable selinux.
Chinyue Chenfac909e2016-06-24 14:17:42 +080096 * cpu_cgparams - CPU cgroup params.
Dylan Reid837c74a2016-01-22 17:25:21 -080097 */
98struct container_config {
99 char *rootfs;
100 char **program_argv;
101 size_t num_args;
Dylan Reid1874feb2016-06-22 17:53:50 -0700102 uid_t uid;
Dylan Reid837c74a2016-01-22 17:25:21 -0800103 char *uid_map;
Dylan Reid1874feb2016-06-22 17:53:50 -0700104 gid_t gid;
Dylan Reid837c74a2016-01-22 17:25:21 -0800105 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 Chavez479b95f2016-06-06 08:01:05 -0700111 char *run_setfiles;
Chinyue Chenfac909e2016-06-24 14:17:42 +0800112 struct container_cpu_cgroup cpu_cgparams;
Dylan Reid837c74a2016-01-22 17:25:21 -0800113};
114
115struct container_config *container_config_create()
116{
117 return calloc(1, sizeof(struct container_config));
118}
119
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700120static 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
132static 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
141static void container_config_free_device(struct container_device *device)
142{
143 FREE_AND_NULL(device->path);
144}
145
Dylan Reid837c74a2016-01-22 17:25:21 -0800146void container_config_destroy(struct container_config *c)
147{
148 size_t i;
149
150 if (c == NULL)
151 return;
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700152 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 Reid837c74a2016-01-22 17:25:21 -0800157 for (i = 0; i < c->num_mounts; ++i) {
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700158 container_config_free_mount(&c->mounts[i]);
Dylan Reid837c74a2016-01-22 17:25:21 -0800159 }
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700160 FREE_AND_NULL(c->mounts);
Dylan Reid837c74a2016-01-22 17:25:21 -0800161 for (i = 0; i < c->num_devices; ++i) {
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700162 container_config_free_device(&c->devices[i]);
Dylan Reid837c74a2016-01-22 17:25:21 -0800163 }
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700164 FREE_AND_NULL(c->devices);
165 FREE_AND_NULL(c->run_setfiles);
166 FREE_AND_NULL(c);
Dylan Reid837c74a2016-01-22 17:25:21 -0800167}
168
169int container_config_rootfs(struct container_config *c, const char *rootfs)
170{
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700171 return strdup_and_free(&c->rootfs, rootfs);
Dylan Reid837c74a2016-01-22 17:25:21 -0800172}
173
Dylan Reid11456722016-05-02 11:24:50 -0700174const char *container_config_get_rootfs(const struct container_config *c)
175{
176 return c->rootfs;
177}
178
Dylan Reid837c74a2016-01-22 17:25:21 -0800179int container_config_program_argv(struct container_config *c,
180 char **argv, size_t num_args)
181{
182 size_t i;
183
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700184 container_free_program_args(c);
Dylan Reid837c74a2016-01-22 17:25:21 -0800185 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 Chavez479b95f2016-06-06 08:01:05 -0700190 if (strdup_and_free(&c->program_argv[i], argv[i]))
191 goto error_free_return;
Dylan Reid837c74a2016-01-22 17:25:21 -0800192 }
193 c->program_argv[num_args] = NULL;
194 return 0;
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700195
196error_free_return:
197 container_free_program_args(c);
198 return -ENOMEM;
Dylan Reid837c74a2016-01-22 17:25:21 -0800199}
200
Dylan Reid11456722016-05-02 11:24:50 -0700201size_t container_config_get_num_program_args(const struct container_config *c)
202{
203 return c->num_args;
204}
205
206const 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 Reid1874feb2016-06-22 17:53:50 -0700214void container_config_uid(struct container_config *c, uid_t uid)
215{
216 c->uid = uid;
217}
218
219uid_t container_config_get_uid(const struct container_config *c)
220{
221 return c->uid;
222}
223
Dylan Reid837c74a2016-01-22 17:25:21 -0800224int container_config_uid_map(struct container_config *c, const char *uid_map)
225{
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700226 return strdup_and_free(&c->uid_map, uid_map);
Dylan Reid837c74a2016-01-22 17:25:21 -0800227}
228
Dylan Reid1874feb2016-06-22 17:53:50 -0700229void container_config_gid(struct container_config *c, gid_t gid)
230{
231 c->gid = gid;
232}
233
234gid_t container_config_get_gid(const struct container_config *c)
235{
236 return c->gid;
237}
238
Dylan Reid837c74a2016-01-22 17:25:21 -0800239int container_config_gid_map(struct container_config *c, const char *gid_map)
240{
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700241 return strdup_and_free(&c->gid_map, gid_map);
Dylan Reid837c74a2016-01-22 17:25:21 -0800242}
243
244int container_config_alt_syscall_table(struct container_config *c,
245 const char *alt_syscall_table)
246{
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700247 return strdup_and_free(&c->alt_syscall_table, alt_syscall_table);
Dylan Reid837c74a2016-01-22 17:25:21 -0800248}
249
250int 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 Chavez479b95f2016-06-06 08:01:05 -0700264 struct container_mount *current_mount;
Dylan Reid837c74a2016-01-22 17:25:21 -0800265
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 Chavez479b95f2016-06-06 08:01:05 -0700275 current_mount = &c->mounts[c->num_mounts];
276 memset(current_mount, 0, sizeof(struct container_mount));
277
278 if (strdup_and_free(&current_mount->name, name))
279 goto error_free_return;
280 if (strdup_and_free(&current_mount->source, source))
281 goto error_free_return;
282 if (strdup_and_free(&current_mount->destination, destination))
283 goto error_free_return;
284 if (strdup_and_free(&current_mount->type, type))
285 goto error_free_return;
286 if (data && strdup_and_free(&current_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 Reid837c74a2016-01-22 17:25:21 -0800294 ++c->num_mounts;
295 return 0;
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700296
297error_free_return:
298 container_config_free_mount(current_mount);
299 return -ENOMEM;
Dylan Reid837c74a2016-01-22 17:25:21 -0800300}
301
302int 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 Reid355d5e42016-04-29 16:53:31 -0700308 int copy_minor,
Dylan Reid837c74a2016-01-22 17:25:21 -0800309 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 Chavez479b95f2016-06-06 08:01:05 -0700316 struct container_device *current_dev;
Dylan Reid837c74a2016-01-22 17:25:21 -0800317
318 if (path == NULL)
319 return -EINVAL;
Dylan Reid355d5e42016-04-29 16:53:31 -0700320 /* If using a dynamic minor number, ensure that minor is -1. */
321 if (copy_minor && (minor != -1))
322 return -EINVAL;
323
Dylan Reid837c74a2016-01-22 17:25:21 -0800324 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 Chavez479b95f2016-06-06 08:01:05 -0700329 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(&current_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 Reid837c74a2016-01-22 17:25:21 -0800344 ++c->num_devices;
345 return 0;
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700346
347error_free_return:
348 container_config_free_device(current_dev);
349 return -ENOMEM;
Dylan Reid837c74a2016-01-22 17:25:21 -0800350}
351
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700352int container_config_run_setfiles(struct container_config *c,
Dylan Reid2bd9ea92016-04-07 20:57:47 -0700353 const char *setfiles_cmd)
354{
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700355 return strdup_and_free(&c->run_setfiles, setfiles_cmd);
Dylan Reid2bd9ea92016-04-07 20:57:47 -0700356}
Dylan Reid837c74a2016-01-22 17:25:21 -0800357
Dylan Reid11456722016-05-02 11:24:50 -0700358const char *container_config_get_run_setfiles(const struct container_config *c)
359{
360 return c->run_setfiles;
361}
362
Chinyue Chenfac909e2016-06-24 14:17:42 +0800363int 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
373int 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
392int 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 Reid837c74a2016-01-22 17:25:21 -0800408/*
409 * Container manipulation
410 */
411struct container {
Dylan Reid837c74a2016-01-22 17:25:21 -0800412 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 Reide040c6b2016-05-02 18:49:02 -0700419 char **ext_mounts; /* Mounts made outside of the minijail */
420 size_t num_ext_mounts;
Luis Hector Chavez8e7b6d52016-06-02 20:40:43 -0700421 char *name;
Dylan Reid837c74a2016-01-22 17:25:21 -0800422};
423
424struct container *container_new(const char *name,
Dylan Reide040c6b2016-05-02 18:49:02 -0700425 const char *rundir)
Dylan Reid837c74a2016-01-22 17:25:21 -0800426{
427 struct container *c;
428
Dylan Reid837c74a2016-01-22 17:25:21 -0800429 c = calloc(1, sizeof(*c));
Dylan Reidb435c682016-04-12 04:17:49 -0700430 if (!c)
431 return NULL;
Dylan Reid837c74a2016-01-22 17:25:21 -0800432 c->cgroup = container_cgroup_new(name, "/sys/fs/cgroup");
433 c->rundir = strdup(rundir);
Luis Hector Chavez8e7b6d52016-06-02 20:40:43 -0700434 c->name = strdup(name);
435 if (!c->cgroup || !c->rundir || !c->name) {
Dylan Reid684975e2016-05-02 15:44:47 -0700436 container_destroy(c);
Dylan Reid837c74a2016-01-22 17:25:21 -0800437 return NULL;
Dylan Reidb435c682016-04-12 04:17:49 -0700438 }
Dylan Reid837c74a2016-01-22 17:25:21 -0800439 return c;
440}
441
442void container_destroy(struct container *c)
443{
Dylan Reid684975e2016-05-02 15:44:47 -0700444 if (c->cgroup)
445 container_cgroup_destroy(c->cgroup);
Luis Hector Chavez8e7b6d52016-06-02 20:40:43 -0700446 if (c->jail)
447 minijail_destroy(c->jail);
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700448 FREE_AND_NULL(c->name);
449 FREE_AND_NULL(c->rundir);
450 FREE_AND_NULL(c);
Dylan Reid837c74a2016-01-22 17:25:21 -0800451}
452
453static 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
464static 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 */
481static int setup_mount_destination(const struct container_mount *mnt,
Dylan Reid2149be92016-04-28 18:38:57 -0700482 const char *source,
Dylan Reid837c74a2016-01-22 17:25:21 -0800483 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 Reid2149be92016-04-28 18:38:57 -0700495 rc = stat(source, &st_buf);
Dylan Reid837c74a2016-01-22 17:25:21 -0800496 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 Reid2bd9ea92016-04-07 20:57:47 -0700502/* Fork and exec the setfiles command to configure the selinux policy. */
Dylan Reide040c6b2016-05-02 18:49:02 -0700503static int run_setfiles_command(const struct container *c,
504 const struct container_config *config,
505 const char *dest)
Dylan Reid2bd9ea92016-04-07 20:57:47 -0700506{
507 int rc;
508 int status;
509 int pid;
510 char *context_path;
511
Dylan Reide040c6b2016-05-02 18:49:02 -0700512 if (!config->run_setfiles)
Dylan Reid2bd9ea92016-04-07 20:57:47 -0700513 return 0;
514
Dylan Reidb3621832016-03-24 10:24:57 -0700515 /* 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 Reid2bd9ea92016-04-07 20:57:47 -0700523 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 Reide040c6b2016-05-02 18:49:02 -0700530 config->run_setfiles,
Dylan Reid2bd9ea92016-04-07 20:57:47 -0700531 "-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 Reide040c6b2016-05-02 18:49:02 -0700557/*
558 * Unmounts anything we mounted in this mount namespace in the opposite order
559 * that they were mounted.
560 */
561static 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 Chavez479b95f2016-06-06 08:01:05 -0700567 if (!c->ext_mounts[c->num_ext_mounts])
568 continue;
Dylan Reide040c6b2016-05-02 18:49:02 -0700569 if (umount(c->ext_mounts[c->num_ext_mounts]))
570 ret = -errno;
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700571 FREE_AND_NULL(c->ext_mounts[c->num_ext_mounts]);
Dylan Reide040c6b2016-05-02 18:49:02 -0700572 }
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700573 FREE_AND_NULL(c->ext_mounts);
Dylan Reide040c6b2016-05-02 18:49:02 -0700574 return ret;
575}
576
Luis Hector Chavez3341ed62016-06-06 08:04:04 -0700577static 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 Chavez479b95f2016-06-06 08:01:05 -0700616 rc = strdup_and_free(&c->ext_mounts[c->num_ext_mounts], dest);
617 if (rc)
Luis Hector Chavez3341ed62016-06-06 08:04:04 -0700618 goto error_free_return;
619 c->num_ext_mounts++;
620 }
621
622 goto exit;
623
624error_free_return:
625 if (!rc)
626 rc = -errno;
627exit:
628 free(source);
629 free(dest);
630 return rc;
631}
632
Dylan Reide040c6b2016-05-02 18:49:02 -0700633static int do_container_mounts(struct container *c,
634 const struct container_config *config)
Dylan Reid7daf9982016-04-28 16:55:42 -0700635{
636 unsigned int i;
Luis Hector Chavez8e7b6d52016-06-02 20:40:43 -0700637 int rc = 0;
Dylan Reid7daf9982016-04-28 16:55:42 -0700638
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700639 unmount_external_mounts(c);
Dylan Reide040c6b2016-05-02 18:49:02 -0700640 /*
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 Chavez3341ed62016-06-06 08:04:04 -0700649 rc = do_container_mount(c, &config->mounts[i]);
650 if (rc)
651 goto error_free_return;
Dylan Reid7daf9982016-04-28 16:55:42 -0700652 }
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700653
Dylan Reid7daf9982016-04-28 16:55:42 -0700654 return 0;
Dylan Reid2149be92016-04-28 18:38:57 -0700655
656error_free_return:
Dylan Reide040c6b2016-05-02 18:49:02 -0700657 unmount_external_mounts(c);
Luis Hector Chavez8e7b6d52016-06-02 20:40:43 -0700658 return rc;
Dylan Reid7daf9982016-04-28 16:55:42 -0700659}
660
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700661static 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
692error_free_return:
693 rc = -errno;
694exit:
695 free(path);
696 return rc;
697}
698
Dylan Reide040c6b2016-05-02 18:49:02 -0700699int container_start(struct container *c, const struct container_config *config)
Dylan Reid837c74a2016-01-22 17:25:21 -0800700{
Dylan Reidb3621832016-03-24 10:24:57 -0700701 static const mode_t root_dir_mode = 0660;
Luis Hector Chavez945af482016-06-03 08:39:34 -0700702 int rc = 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800703 unsigned int i;
Dylan Reide040c6b2016-05-02 18:49:02 -0700704 const char *rootfs = config->rootfs;
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700705 char *runfs_template = NULL;
Dylan Reid837c74a2016-01-22 17:25:21 -0800706
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700707 if (!c)
708 return -EINVAL;
Dylan Reide040c6b2016-05-02 18:49:02 -0700709 if (!config)
710 return -EINVAL;
711 if (!config->program_argv || !config->program_argv[0])
712 return -EINVAL;
713
Dylan Reid837c74a2016-01-22 17:25:21 -0800714 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 Reidb3621832016-03-24 10:24:57 -0700722 /* Make sure the container uid can access the rootfs. */
Dylan Reid4c6af2e2016-06-22 18:04:24 -0700723 if (chmod(c->runfs, 0700))
Dylan Reidb3621832016-03-24 10:24:57 -0700724 goto error_rmdir;
Dylan Reid1874feb2016-06-22 17:53:50 -0700725 if (chown(c->runfs, config->uid, config->gid))
726 goto error_rmdir;
Dylan Reidb3621832016-03-24 10:24:57 -0700727
Luis Hector Chavez945af482016-06-03 08:39:34 -0700728 if (asprintf(&c->runfsroot, "%s/root", c->runfs) < 0)
Dylan Reid837c74a2016-01-22 17:25:21 -0800729 goto error_rmdir;
730
Luis Hector Chavez945af482016-06-03 08:39:34 -0700731 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 Reid837c74a2016-01-22 17:25:21 -0800737 goto error_rmdir;
738
739 c->jail = minijail_new();
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700740 if (!c->jail)
Luis Hector Chavez945af482016-06-03 08:39:34 -0700741 goto error_rmdir;
Dylan Reid837c74a2016-01-22 17:25:21 -0800742
Luis Hector Chavez8e7b6d52016-06-02 20:40:43 -0700743 rc = do_container_mounts(c, config);
744 if (rc)
Dylan Reid7daf9982016-04-28 16:55:42 -0700745 goto error_rmdir;
Dylan Reid837c74a2016-01-22 17:25:21 -0800746
747 c->cgroup->ops->deny_all_devices(c->cgroup);
748
Dylan Reide040c6b2016-05-02 18:49:02 -0700749 for (i = 0; i < config->num_devices; i++) {
750 const struct container_device *dev = &config->devices[i];
Dylan Reid355d5e42016-04-29 16:53:31 -0700751 int minor = dev->minor;
Dylan Reid837c74a2016-01-22 17:25:21 -0800752
Dylan Reid355d5e42016-04-29 16:53:31 -0700753 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 Chavez479b95f2016-06-06 08:01:05 -0700761 rc = container_create_device(c, dev, minor);
762 if (rc)
Dylan Reid355d5e42016-04-29 16:53:31 -0700763 goto error_rmdir;
Dylan Reid837c74a2016-01-22 17:25:21 -0800764 }
765
766 rc = c->cgroup->ops->add_device(c->cgroup, dev->major,
Dylan Reid355d5e42016-04-29 16:53:31 -0700767 minor, dev->read_allowed,
Dylan Reid837c74a2016-01-22 17:25:21 -0800768 dev->write_allowed,
769 dev->modify_allowed, dev->type);
770 if (rc)
771 goto error_rmdir;
772 }
773
Dylan Reidd7229582016-04-27 17:08:40 -0700774 /* Potentailly run setfiles on mounts configured outside of the jail */
Dylan Reide040c6b2016-05-02 18:49:02 -0700775 for (i = 0; i < config->num_mounts; i++) {
776 const struct container_mount *mnt = &config->mounts[i];
Dylan Reidd7229582016-04-27 17:08:40 -0700777 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 Reide040c6b2016-05-02 18:49:02 -0700783 rc = run_setfiles_command(c, config, dest);
Dylan Reidd7229582016-04-27 17:08:40 -0700784 free(dest);
785 if (rc)
786 goto error_rmdir;
787 }
788
Chinyue Chenfac909e2016-06-24 14:17:42 +0800789 /* 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 Reid837c74a2016-01-22 17:25:21 -0800817 /* 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 Reid837c74a2016-01-22 17:25:21 -0800828 minijail_namespace_user(c->jail);
Dylan Reide040c6b2016-05-02 18:49:02 -0700829 rc = minijail_uidmap(c->jail, config->uid_map);
Dylan Reid837c74a2016-01-22 17:25:21 -0800830 if (rc)
831 goto error_rmdir;
Dylan Reide040c6b2016-05-02 18:49:02 -0700832 rc = minijail_gidmap(c->jail, config->gid_map);
Dylan Reid837c74a2016-01-22 17:25:21 -0800833 if (rc)
834 goto error_rmdir;
Dylan Reid837c74a2016-01-22 17:25:21 -0800835
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 Reide040c6b2016-05-02 18:49:02 -0700857 if (config->alt_syscall_table)
858 minijail_use_alt_syscall(c->jail, config->alt_syscall_table);
Dylan Reid837c74a2016-01-22 17:25:21 -0800859
860 minijail_run_as_init(c->jail);
861
Dylan Reid3da683b2016-04-05 03:35:35 -0700862 /* TODO(dgreid) - remove this once shared mounts are cleaned up. */
863 minijail_skip_remount_private(c->jail);
864
Dylan Reid837c74a2016-01-22 17:25:21 -0800865 rc = minijail_run_pid_pipes_no_preload(c->jail,
Dylan Reide040c6b2016-05-02 18:49:02 -0700866 config->program_argv[0],
867 config->program_argv,
Dylan Reid837c74a2016-01-22 17:25:21 -0800868 &c->init_pid, NULL, NULL,
869 NULL);
870 if (rc)
871 goto error_rmdir;
872 return 0;
873
874error_rmdir:
Luis Hector Chavez945af482016-06-03 08:39:34 -0700875 if (!rc)
876 rc = -errno;
877 container_teardown(c);
Dylan Reid837c74a2016-01-22 17:25:21 -0800878 return rc;
879}
880
881const char *container_root(struct container *c)
882{
883 return c->runfs;
884}
885
886int container_pid(struct container *c)
887{
888 return c->init_pid;
889}
890
891static int container_teardown(struct container *c)
892{
Dylan Reid837c74a2016-01-22 17:25:21 -0800893 int ret = 0;
894
Dylan Reide040c6b2016-05-02 18:49:02 -0700895 unmount_external_mounts(c);
Luis Hector Chavez945af482016-06-03 08:39:34 -0700896 if (c->runfsroot) {
897 if (umount(c->runfsroot))
898 ret = -errno;
899 if (rmdir(c->runfsroot))
900 ret = -errno;
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700901 FREE_AND_NULL(c->runfsroot);
Luis Hector Chavez945af482016-06-03 08:39:34 -0700902 }
903 if (c->pid_file_path) {
904 if (unlink(c->pid_file_path))
905 ret = -errno;
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700906 FREE_AND_NULL(c->pid_file_path);
Luis Hector Chavez945af482016-06-03 08:39:34 -0700907 }
908 if (c->runfs) {
909 if (rmdir(c->runfs))
910 ret = -errno;
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700911 FREE_AND_NULL(c->runfs);
Luis Hector Chavez945af482016-06-03 08:39:34 -0700912 }
Dylan Reid837c74a2016-01-22 17:25:21 -0800913 return ret;
914}
915
916int container_wait(struct container *c)
917{
Dylan Reidcf745c52016-04-22 10:18:03 -0700918 int rc;
919
920 do {
921 rc = minijail_wait(c->jail);
Luis Hector Chavez4641e852016-06-02 15:40:19 -0700922 } while (rc == -EINTR);
Dylan Reidcf745c52016-04-22 10:18:03 -0700923
Luis Hector Chavez945af482016-06-03 08:39:34 -0700924 // If the process had already been reaped, still perform teardown.
925 if (rc == -ECHILD || rc >= 0) {
Dylan Reidcf745c52016-04-22 10:18:03 -0700926 rc = container_teardown(c);
Luis Hector Chavez945af482016-06-03 08:39:34 -0700927 }
Dylan Reidcf745c52016-04-22 10:18:03 -0700928 return rc;
Dylan Reid837c74a2016-01-22 17:25:21 -0800929}
930
931int container_kill(struct container *c)
932{
Luis Hector Chavez945af482016-06-03 08:39:34 -0700933 if (kill(c->init_pid, SIGKILL) && errno != ESRCH)
Dylan Reid837c74a2016-01-22 17:25:21 -0800934 return -errno;
935 return container_wait(c);
936}