jail: handle mount propagation flags
[project/procd.git] / jail / jail.c
1 /*
2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 * Copyright (C) 2020 Daniel Golle <daniel@makrotopia.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #define _GNU_SOURCE
16 #include <sys/mount.h>
17 #include <sys/prctl.h>
18 #include <sys/wait.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/stat.h>
23 #include <sys/sysmacros.h>
24
25 /* musl only defined 15 limit types, make sure all 16 are supported */
26 #ifndef RLIMIT_RTTIME
27 #define RLIMIT_RTTIME 15
28 #undef RLIMIT_NLIMITS
29 #define RLIMIT_NLIMITS 16
30 #undef RLIM_NLIMITS
31 #define RLIM_NLIMITS 16
32 #endif
33
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <pwd.h>
38 #include <grp.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <sched.h>
42 #include <linux/filter.h>
43 #include <linux/limits.h>
44 #include <linux/nsfs.h>
45 #include <linux/securebits.h>
46 #include <signal.h>
47 #include <inttypes.h>
48
49 #include "capabilities.h"
50 #include "elf.h"
51 #include "fs.h"
52 #include "jail.h"
53 #include "log.h"
54 #include "seccomp-oci.h"
55 #include "cgroups.h"
56
57 #include <libubox/utils.h>
58 #include <libubox/blobmsg.h>
59 #include <libubox/blobmsg_json.h>
60 #include <libubox/list.h>
61 #include <libubox/vlist.h>
62 #include <libubox/uloop.h>
63 #include <libubus.h>
64
65 #ifndef CLONE_NEWCGROUP
66 #define CLONE_NEWCGROUP 0x02000000
67 #endif
68
69 #define STACK_SIZE (1024 * 1024)
70 #define OPT_ARGS "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:EyJ:iP:"
71
72 #define OCI_VERSION_STRING "1.0.2"
73
74 struct hook_execvpe {
75 char *file;
76 char **argv;
77 char **envp;
78 int timeout;
79 };
80
81 struct sysctl_val {
82 char *entry;
83 char *value;
84 };
85
86 struct mknod_args {
87 char *path;
88 mode_t mode;
89 dev_t dev;
90 uid_t uid;
91 gid_t gid;
92 };
93
94 static struct {
95 char *name;
96 char *hostname;
97 char **jail_argv;
98 char *cwd;
99 char *seccomp;
100 struct sock_fprog *ociseccomp;
101 char *capabilities;
102 struct jail_capset capset;
103 char *user;
104 char *group;
105 char *extroot;
106 char *overlaydir;
107 char *tmpoverlaysize;
108 char **envp;
109 char *uidmap;
110 char *gidmap;
111 char *pidfile;
112 struct sysctl_val **sysctl;
113 int no_new_privs;
114 int namespace;
115 struct {
116 int pid;
117 int net;
118 int ns;
119 int ipc;
120 int uts;
121 int user;
122 int cgroup;
123 #ifdef CLONE_NEWTIME
124 int time;
125 #endif
126 } setns;
127 int procfs;
128 int ronly;
129 int sysfs;
130 int console;
131 int pw_uid;
132 int pw_gid;
133 int gr_gid;
134 gid_t *additional_gids;
135 size_t num_additional_gids;
136 mode_t umask;
137 bool set_umask;
138 int require_jail;
139 struct {
140 struct hook_execvpe **createRuntime;
141 struct hook_execvpe **createContainer;
142 struct hook_execvpe **startContainer;
143 struct hook_execvpe **poststart;
144 struct hook_execvpe **poststop;
145 } hooks;
146 struct rlimit *rlimits[RLIM_NLIMITS];
147 int oom_score_adj;
148 bool set_oom_score_adj;
149 struct mknod_args **devices;
150 char *ocibundle;
151 bool immediately;
152 struct blob_attr *annotations;
153 } opts;
154
155 static struct blob_buf ocibuf;
156
157 extern int pivot_root(const char *new_root, const char *put_old);
158
159 int debug = 0;
160
161 static char child_stack[STACK_SIZE];
162
163 static struct ubus_context *parent_ctx;
164
165 int console_fd;
166
167
168 static inline bool has_namespaces(void)
169 {
170 return ((opts.setns.pid != -1) ||
171 (opts.setns.net != -1) ||
172 (opts.setns.ns != -1) ||
173 (opts.setns.ipc != -1) ||
174 (opts.setns.uts != -1) ||
175 (opts.setns.user != -1) ||
176 (opts.setns.cgroup != -1) ||
177 #ifdef CLONE_NEWTIME
178 (opts.setns.time != -1) ||
179 #endif
180 opts.namespace);
181 }
182
183 static void free_hooklist(struct hook_execvpe **hooklist)
184 {
185 struct hook_execvpe *cur;
186 char **tmp;
187
188 if (!hooklist)
189 return;
190
191 cur = *hooklist;
192 while (cur) {
193 free(cur->file);
194 tmp = cur->argv;
195 while (tmp)
196 free(*(tmp++));
197
198 free(cur->argv);
199
200 tmp = cur->envp;
201 while (tmp)
202 free(*(tmp++));
203
204 free(cur->envp);
205 free(cur++);
206 }
207 free(hooklist);
208 }
209
210 static void free_sysctl(void) {
211 struct sysctl_val *cur;
212 cur = *opts.sysctl;
213
214 while (cur) {
215 free(cur->entry);
216 free(cur->value);
217 free(cur++);
218 }
219 free(opts.sysctl);
220 }
221
222 static void free_devices(void) {
223 struct mknod_args **cur;
224
225 if (!opts.devices)
226 return;
227
228 cur = opts.devices;
229
230 while (*cur) {
231 free((*cur)->path);
232 free(*(cur++));
233 }
234 free(opts.devices);
235 }
236
237 static void free_rlimits(void) {
238 int type;
239
240 for (type = 0; type < RLIM_NLIMITS; ++type)
241 free(opts.rlimits[type]);
242 }
243
244 static void free_opts(bool parent) {
245 char **tmp;
246
247 /* we need to keep argv, envp and seccomp filter in child */
248 if (parent) { /* parent-only */
249 if (opts.ociseccomp) {
250 free(opts.ociseccomp->filter);
251 free(opts.ociseccomp);
252 }
253
254 tmp = opts.jail_argv;
255 while(tmp)
256 free(*(tmp++));
257
258 free(opts.jail_argv);
259
260 tmp = opts.envp;
261 while (tmp)
262 free(*(tmp++));
263
264 free(opts.envp);
265 } else { /* child-only */
266 if (opts.ocibundle)
267 cgroups_free();
268 }
269
270 free_rlimits();
271 free_sysctl();
272 free_devices();
273 free(opts.hostname);
274 free(opts.cwd);
275 free(opts.extroot);
276 free(opts.uidmap);
277 free(opts.gidmap);
278 free(opts.annotations);
279 free(opts.ocibundle);
280 free(opts.pidfile);
281 free_hooklist(opts.hooks.createRuntime);
282 free_hooklist(opts.hooks.createContainer);
283 free_hooklist(opts.hooks.startContainer);
284 free_hooklist(opts.hooks.poststart);
285 free_hooklist(opts.hooks.poststop);
286 }
287
288 static int mount_overlay(char *jail_root, char *overlaydir) {
289 char *upperdir, *workdir, *optsstr, *upperetc, *upperresolvconf;
290 const char mountoptsformat[] = "lowerdir=%s,upperdir=%s,workdir=%s";
291 int ret = -1, fd;
292
293 if (asprintf(&upperdir, "%s%s", overlaydir, "/upper") < 0)
294 goto out;
295
296 if (asprintf(&workdir, "%s%s", overlaydir, "/work") < 0)
297 goto upper_printf;
298
299 if (asprintf(&optsstr, mountoptsformat, jail_root, upperdir, workdir) < 0)
300 goto work_printf;
301
302 if (mkdir_p(upperdir, 0755) || mkdir_p(workdir, 0755))
303 goto opts_printf;
304
305 /*
306 * make sure /etc/resolv.conf exists in overlay and is owned by jail userns root
307 * this is to work-around a bug in overlayfs described in the overlayfs-userns
308 * patch:
309 * 3. modification of a file 'hithere' which is in l but not yet
310 * in u, and which is not owned by T, is not allowed, even if
311 * writes to u are allowed. This may be a bug in overlayfs,
312 * but it is safe behavior.
313 */
314 if (asprintf(&upperetc, "%s/etc", upperdir) < 0)
315 goto opts_printf;
316
317 if (mkdir_p(upperetc, 0755))
318 goto upper_etc_printf;
319
320 if (asprintf(&upperresolvconf, "%s/resolv.conf", upperetc) < 0)
321 goto upper_etc_printf;
322
323 fd = creat(upperresolvconf, 0644);
324 if (fd == -1) {
325 ERROR("creat(%s) failed: %m\n", upperresolvconf);
326 goto upper_resolvconf_printf;
327 }
328 close(fd);
329
330 DEBUG("mount -t overlay %s %s (%s)\n", jail_root, jail_root, optsstr);
331
332 if (mount(jail_root, jail_root, "overlay", MS_NOATIME, optsstr))
333 goto opts_printf;
334
335 ret = 0;
336
337 upper_resolvconf_printf:
338 free(upperresolvconf);
339 upper_etc_printf:
340 free(upperetc);
341 opts_printf:
342 free(optsstr);
343 work_printf:
344 free(workdir);
345 upper_printf:
346 free(upperdir);
347 out:
348 return ret;
349 }
350
351 static void pass_console(int console_fd)
352 {
353 struct ubus_context *child_ctx = ubus_connect(NULL);
354 static struct blob_buf req;
355 uint32_t id;
356
357 if (!child_ctx)
358 return;
359
360 blob_buf_init(&req, 0);
361 blobmsg_add_string(&req, "name", opts.name);
362
363 if (ubus_lookup_id(child_ctx, "container", &id) ||
364 ubus_invoke_fd(child_ctx, id, "console_set", req.head, NULL, NULL, 3000, console_fd))
365 INFO("ubus request failed\n");
366 else
367 close(console_fd);
368
369 blob_buf_free(&req);
370 ubus_free(child_ctx);
371 }
372
373 static int create_dev_console(const char *jail_root)
374 {
375 char *console_fname;
376 char dev_console_path[PATH_MAX];
377 int slave_console_fd;
378
379 /* Open UNIX/98 virtual console */
380 console_fd = posix_openpt(O_RDWR | O_NOCTTY);
381 if (console_fd == -1)
382 return -1;
383
384 console_fname = ptsname(console_fd);
385 DEBUG("got console fd %d and PTS client name %s\n", console_fd, console_fname);
386 if (!console_fname)
387 goto no_console;
388
389 grantpt(console_fd);
390 unlockpt(console_fd);
391
392 /* pass PTY master to procd */
393 pass_console(console_fd);
394
395 /* mount-bind PTY slave to /dev/console in jail */
396 snprintf(dev_console_path, sizeof(dev_console_path), "%s/dev/console", jail_root);
397 close(creat(dev_console_path, 0620));
398
399 if (mount(console_fname, dev_console_path, NULL, MS_BIND, NULL))
400 goto no_console;
401
402 /* use PTY slave for stdio */
403 slave_console_fd = open(console_fname, O_RDWR); /* | O_NOCTTY */
404 dup2(slave_console_fd, 0);
405 dup2(slave_console_fd, 1);
406 dup2(slave_console_fd, 2);
407 close(slave_console_fd);
408
409 INFO("using guest console %s\n", console_fname);
410
411 return 0;
412
413 no_console:
414 close(console_fd);
415 return 1;
416 }
417
418 static int hook_running = 0;
419 static int hook_return_code = 0;
420 static struct hook_execvpe **current_hook = NULL;
421 typedef void (*hook_return_handler)(void);
422 static hook_return_handler hook_return_cb = NULL;
423
424 static void hook_process_timeout_cb(struct uloop_timeout *t);
425 static struct uloop_timeout hook_process_timeout = {
426 .cb = hook_process_timeout_cb,
427 };
428
429 static void run_hooklist(void);
430 static void hook_process_handler(struct uloop_process *c, int ret)
431 {
432 uloop_timeout_cancel(&hook_process_timeout);
433
434 if (WIFEXITED(ret)) {
435 hook_return_code = WEXITSTATUS(ret);
436 if (hook_return_code)
437 ERROR("hook (%d) exited with exit: %d\n", c->pid, hook_return_code);
438 else
439 DEBUG("hook (%d) exited with exit: %d\n", c->pid, hook_return_code);
440
441 } else {
442 hook_return_code = WTERMSIG(ret);
443 ERROR("hook (%d) exited with signal: %d\n", c->pid, hook_return_code);
444 }
445 hook_running = 0;
446 ++current_hook;
447 run_hooklist();
448 }
449
450 static struct uloop_process hook_process = {
451 .cb = hook_process_handler,
452 };
453
454 static void hook_process_timeout_cb(struct uloop_timeout *t)
455 {
456 DEBUG("hook process failed to stop, sending SIGKILL\n");
457 kill(hook_process.pid, SIGKILL);
458 }
459
460 static void run_hooklist(void)
461 {
462 struct hook_execvpe *hook = *current_hook;
463 struct stat s;
464
465 if (!hook)
466 hook_return_cb();
467
468 DEBUG("executing hook %s\n", hook->file);
469
470 if (stat(hook->file, &s))
471 hook_process_handler(&hook_process, ENOENT);
472
473 if (!((unsigned long)s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
474 hook_process_handler(&hook_process, EPERM);
475
476 if (!((unsigned long)s.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)))
477 hook_process_handler(&hook_process, EPERM);
478
479 hook_running = 1;
480 hook_process.pid = fork();
481 if (hook_process.pid == 0) {
482 /* child */
483 execve(hook->file, hook->argv, hook->envp);
484 ERROR("execve error %m\n");
485 _exit(errno);
486 } else if (hook_process.pid < 0) {
487 /* fork error */
488 ERROR("hook fork error\n");
489 hook_running = 0;
490 hook_process_handler(&hook_process, errno);
491 }
492
493 /* parent */
494 uloop_process_add(&hook_process);
495
496 if (hook->timeout > 0)
497 uloop_timeout_set(&hook_process_timeout, 1000 * hook->timeout);
498
499 uloop_run();
500 if (hook_running) {
501 DEBUG("uloop interrupted, killing jail process\n");
502 kill(hook_process.pid, SIGTERM);
503 uloop_timeout_set(&hook_process_timeout, 1000);
504 uloop_run();
505 }
506 }
507
508 static void run_hooks(struct hook_execvpe **hooklist, hook_return_handler return_cb)
509 {
510 if (!hooklist)
511 return_cb();
512
513 current_hook = hooklist;
514 hook_return_cb = return_cb;
515
516 run_hooklist();
517 }
518
519 static int apply_sysctl(const char *jail_root)
520 {
521 struct sysctl_val **cur;
522 char *procdir, *fname;
523 int f;
524
525 if (!opts.sysctl)
526 return 0;
527
528 asprintf(&procdir, "%s/proc", jail_root);
529 if (!procdir)
530 return ENOMEM;
531
532 mkdir(procdir, 0700);
533 if (mount("proc", procdir, "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0))
534 return EPERM;
535
536 cur = opts.sysctl;
537
538 while (*cur) {
539 asprintf(&fname, "%s/sys/%s", procdir, (*cur)->entry);
540 if (!fname)
541 return ENOMEM;
542
543 DEBUG("sysctl: writing '%s' to %s\n", (*cur)->value, fname);
544
545 f = open(fname, O_WRONLY);
546 if (f == -1) {
547 ERROR("sysctl: can't open %s\n", fname);
548 return errno;
549 }
550 write(f, (*cur)->value, strlen((*cur)->value));
551
552 free(fname);
553 close(f);
554 ++cur;
555 }
556 umount(procdir);
557 rmdir(procdir);
558 free(procdir);
559
560 return 0;
561 }
562
563 /* glibc defines makedev calling a function. make sure it's a pure macro */
564 #if defined(__GLIBC__)
565 #undef makedev
566 /* from musl's sys/sysmacros.h */
567 #define makedev(x,y) ( \
568 (((x)&0xfffff000ULL) << 32) | \
569 (((x)&0x00000fffULL) << 8) | \
570 (((y)&0xffffff00ULL) << 12) | \
571 (((y)&0x000000ffULL)) )
572 #endif
573
574 static struct mknod_args default_devices[] = {
575 { .path = "/dev/null", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 3) },
576 { .path = "/dev/zero", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 5) },
577 { .path = "/dev/full", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 7) },
578 { .path = "/dev/random", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 8) },
579 { .path = "/dev/urandom", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 9) },
580 { .path = "/dev/tty", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP), .dev = makedev(5, 0), .gid = 5 },
581 { 0 },
582 };
583
584 static int create_devices(void)
585 {
586 struct mknod_args **cur, *curdef;
587
588 if (!opts.devices)
589 goto only_default_devices;
590
591 cur = opts.devices;
592
593 while (*cur) {
594 DEBUG("creating %s (mode=%08o)\n", (*cur)->path, (*cur)->mode);
595 if (mknod((*cur)->path, (*cur)->mode, (*cur)->dev))
596 return errno;
597
598 if (((*cur)->uid || (*cur)->gid) &&
599 chown((*cur)->path, (*cur)->uid, (*cur)->gid))
600 return errno;
601
602 ++cur;
603 }
604
605 only_default_devices:
606 curdef = default_devices;
607 while(curdef->path) {
608 DEBUG("creating %s (mode=%08o)\n", curdef->path, curdef->mode);
609 if (mknod(curdef->path, curdef->mode, curdef->dev)) {
610 ++curdef;
611 continue; /* may already exist, eg. due to a bind-mount */
612 }
613 if ((curdef->uid || curdef->gid) &&
614 chown(curdef->path, curdef->uid, curdef->gid))
615 return errno;
616
617 ++curdef;
618 }
619
620 /* Dev symbolic links as defined in OCI spec */
621 symlink("/dev/pts/ptmx", "/dev/ptmx");
622 symlink("/proc/self/fd", "/dev/fd");
623 symlink("/proc/self/fd/0", "/dev/stdin");
624 symlink("/proc/self/fd/1", "/dev/stdout");
625 symlink("/proc/self/fd/2", "/dev/stderr");
626
627 return 0;
628 }
629
630 static char jail_root[] = "/tmp/ujail-XXXXXX";
631 static char tmpovdir[] = "/tmp/ujail-overlay-XXXXXX";
632 static mode_t old_umask;
633 static void enter_jail_fs(void);
634 static int build_jail_fs(void)
635 {
636 char *overlaydir = NULL;
637
638 old_umask = umask(0);
639
640 if (mkdtemp(jail_root) == NULL) {
641 ERROR("mkdtemp(%s) failed: %m\n", jail_root);
642 return -1;
643 }
644
645 if (apply_sysctl(jail_root)) {
646 ERROR("failed to apply sysctl values\n");
647 return -1;
648 }
649
650 /* oldroot can't be MS_SHARED else pivot_root() fails */
651 if (mount("none", "/", NULL, MS_REC|MS_PRIVATE, NULL)) {
652 ERROR("private mount failed %m\n");
653 return -1;
654 }
655
656 if (opts.extroot) {
657 if (mount(opts.extroot, jail_root, NULL, MS_BIND, NULL)) {
658 ERROR("extroot mount failed %m\n");
659 return -1;
660 }
661 } else {
662 if (mount("tmpfs", jail_root, "tmpfs", MS_NOATIME, "mode=0755")) {
663 ERROR("tmpfs mount failed %m\n");
664 return -1;
665 }
666 }
667
668 if (opts.tmpoverlaysize) {
669 char mountoptsstr[] = "mode=0755,size=XXXXXXXX";
670
671 snprintf(mountoptsstr, sizeof(mountoptsstr),
672 "mode=0755,size=%s", opts.tmpoverlaysize);
673 if (mkdtemp(tmpovdir) == NULL) {
674 ERROR("mkdtemp(%s) failed: %m\n", jail_root);
675 return -1;
676 }
677 if (mount("tmpfs", tmpovdir, "tmpfs", MS_NOATIME,
678 mountoptsstr)) {
679 ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts.tmpoverlaysize);
680 return -1;
681 }
682 overlaydir = tmpovdir;
683 }
684
685 if (opts.overlaydir)
686 overlaydir = opts.overlaydir;
687
688 if (overlaydir)
689 mount_overlay(jail_root, overlaydir);
690
691 if (chdir(jail_root)) {
692 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root);
693 return -1;
694 }
695
696 if (mount_all(jail_root)) {
697 ERROR("mount_all() failed\n");
698 return -1;
699 }
700
701 if (opts.console)
702 create_dev_console(jail_root);
703
704 /* make sure /etc/resolv.conf exists if in new network namespace */
705 if (opts.namespace & CLONE_NEWNET) {
706 char jailetc[PATH_MAX], jaillink[PATH_MAX];
707
708 snprintf(jailetc, PATH_MAX, "%s/etc", jail_root);
709 mkdir_p(jailetc, 0755);
710 snprintf(jaillink, PATH_MAX, "%s/etc/resolv.conf", jail_root);
711 if (overlaydir)
712 unlink(jaillink);
713
714 symlink("../dev/resolv.conf.d/resolv.conf.auto", jaillink);
715 }
716
717 run_hooks(opts.hooks.createContainer, enter_jail_fs);
718
719 return 0;
720 }
721
722 static void post_jail_fs(void);
723 static void enter_jail_fs(void)
724 {
725 char dirbuf[sizeof(jail_root) + 4];
726
727 snprintf(dirbuf, sizeof(dirbuf), "%s/old", jail_root);
728 mkdir(dirbuf, 0755);
729
730 if (pivot_root(jail_root, dirbuf) == -1) {
731 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root, dirbuf);
732 exit(-1);
733 }
734 if (chdir("/")) {
735 ERROR("chdir(/) (after pivot_root) failed: %m\n");
736 exit(-1);
737 }
738
739 snprintf(dirbuf, sizeof(dirbuf), "/old%s", jail_root);
740 umount2(dirbuf, MNT_DETACH);
741 rmdir(dirbuf);
742 if (opts.tmpoverlaysize) {
743 char tmpdirbuf[sizeof(tmpovdir) + 4];
744 snprintf(tmpdirbuf, sizeof(tmpdirbuf), "/old%s", tmpovdir);
745 umount2(tmpdirbuf, MNT_DETACH);
746 rmdir(tmpdirbuf);
747 }
748
749 umount2("/old", MNT_DETACH);
750 rmdir("/old");
751
752 if (create_devices()) {
753 ERROR("create_devices() failed\n");
754 exit(-1);
755 }
756 if (opts.ronly)
757 mount(NULL, "/", NULL, MS_REMOUNT | MS_BIND | MS_RDONLY, 0);
758
759 umask(old_umask);
760 post_jail_fs();
761 }
762
763 static int write_uid_gid_map(pid_t child_pid, bool gidmap, char *mapstr)
764 {
765 int map_file;
766 char map_path[64];
767
768 if (snprintf(map_path, sizeof(map_path), "/proc/%d/%s",
769 child_pid, gidmap?"gid_map":"uid_map") < 0)
770 return -1;
771
772 if ((map_file = open(map_path, O_WRONLY)) == -1)
773 return -1;
774
775 if (dprintf(map_file, "%s", mapstr)) {
776 close(map_file);
777 return -1;
778 }
779
780 close(map_file);
781 free(mapstr);
782 return 0;
783 }
784
785 static int write_single_uid_gid_map(pid_t child_pid, bool gidmap, int id)
786 {
787 int map_file;
788 char map_path[64];
789 const char *map_format = "%d %d %d\n";
790 if (snprintf(map_path, sizeof(map_path), "/proc/%d/%s",
791 child_pid, gidmap?"gid_map":"uid_map") < 0)
792 return -1;
793
794 if ((map_file = open(map_path, O_WRONLY)) == -1)
795 return -1;
796
797 if (dprintf(map_file, map_format, 0, id, 1) == -1) {
798 close(map_file);
799 return -1;
800 }
801
802 close(map_file);
803 return 0;
804 }
805
806 static int write_setgroups(pid_t child_pid, bool allow)
807 {
808 int setgroups_file;
809 char setgroups_path[64];
810
811 if (snprintf(setgroups_path, sizeof(setgroups_path), "/proc/%d/setgroups",
812 child_pid) < 0) {
813 return -1;
814 }
815
816 if ((setgroups_file = open(setgroups_path, O_WRONLY)) == -1) {
817 return -1;
818 }
819
820 if (dprintf(setgroups_file, "%s", allow?"allow":"deny") == -1) {
821 close(setgroups_file);
822 return -1;
823 }
824
825 close(setgroups_file);
826 return 0;
827 }
828
829 static void get_jail_user(int *user, int *user_gid, int *gr_gid)
830 {
831 struct passwd *p = NULL;
832 struct group *g = NULL;
833
834 if (opts.user) {
835 p = getpwnam(opts.user);
836 if (!p) {
837 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
838 opts.user, errno, strerror(errno));
839 exit(EXIT_FAILURE);
840 }
841 *user = p->pw_uid;
842 *user_gid = p->pw_gid;
843 } else {
844 *user = -1;
845 *user_gid = -1;
846 }
847
848 if (opts.group) {
849 g = getgrnam(opts.group);
850 if (!g) {
851 ERROR("failed to get gid for group %s: %m\n", opts.group);
852 exit(EXIT_FAILURE);
853 }
854 *gr_gid = g->gr_gid;
855 } else {
856 *gr_gid = -1;
857 }
858 };
859
860 static void set_jail_user(int pw_uid, int user_gid, int gr_gid)
861 {
862 if (opts.user && (user_gid != -1) && initgroups(opts.user, user_gid)) {
863 ERROR("failed to initgroups() for user %s: %m\n", opts.user);
864 exit(EXIT_FAILURE);
865 }
866
867 if ((gr_gid != -1) && setregid(gr_gid, gr_gid)) {
868 ERROR("failed to set group id %d: %m\n", gr_gid);
869 exit(EXIT_FAILURE);
870 }
871
872 if ((pw_uid != -1) && setreuid(pw_uid, pw_uid)) {
873 ERROR("failed to set user id %d: %m\n", pw_uid);
874 exit(EXIT_FAILURE);
875 }
876 }
877
878 static int apply_rlimits(void)
879 {
880 int resource;
881
882 for (resource = 0; resource < RLIM_NLIMITS; ++resource) {
883 if (opts.rlimits[resource])
884 DEBUG("applying limits to resource %u\n", resource);
885
886 if (opts.rlimits[resource] &&
887 setrlimit(resource, opts.rlimits[resource]))
888 return errno;
889 }
890
891 return 0;
892 }
893
894 #define MAX_ENVP 8
895 static char** build_envp(const char *seccomp, char **ocienvp)
896 {
897 static char *envp[MAX_ENVP];
898 static char preload_var[PATH_MAX];
899 static char seccomp_var[PATH_MAX];
900 static char debug_var[] = "LD_DEBUG=all";
901 static char container_var[] = "container=ujail";
902 const char *preload_lib = find_lib("libpreload-seccomp.so");
903 char **addenv;
904
905 int count = 0;
906
907 if (seccomp && !preload_lib) {
908 ERROR("failed to add preload-lib to env\n");
909 return NULL;
910 }
911 if (seccomp) {
912 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
913 envp[count++] = seccomp_var;
914 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
915 envp[count++] = preload_var;
916 }
917
918 envp[count++] = container_var;
919
920 if (debug > 1)
921 envp[count++] = debug_var;
922
923 addenv = ocienvp;
924 while (addenv && *addenv) {
925 envp[count++] = *(addenv++);
926 if (count >= MAX_ENVP) {
927 ERROR("environment limited to %d extra records, truncating\n", MAX_ENVP);
928 break;
929 }
930 }
931 return envp;
932 }
933
934 static void usage(void)
935 {
936 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
937 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
938 fprintf(stderr, " -S <file>\tseccomp filter config\n");
939 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
940 fprintf(stderr, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
941 fprintf(stderr, " -n <name>\tthe name of the jail\n");
942 fprintf(stderr, "namespace jail options:\n");
943 fprintf(stderr, " -h <hostname>\tchange the hostname of the jail\n");
944 fprintf(stderr, " -N\t\tjail has network namespace\n");
945 fprintf(stderr, " -f\t\tjail has user namespace\n");
946 fprintf(stderr, " -F\t\tjail has cgroups namespace\n");
947 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
948 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
949 fprintf(stderr, " -p\t\tjail has /proc\n");
950 fprintf(stderr, " -s\t\tjail has /sys\n");
951 fprintf(stderr, " -l\t\tjail has /dev/log\n");
952 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
953 fprintf(stderr, " -U <name>\tuser to run jailed process\n");
954 fprintf(stderr, " -G <name>\tgroup to run jailed process\n");
955 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
956 fprintf(stderr, " -R <dir>\texternal jail rootfs (system container)\n");
957 fprintf(stderr, " -O <dir>\tdirectory for r/w overlayfs\n");
958 fprintf(stderr, " -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
959 fprintf(stderr, " -E\t\tfail if jail cannot be setup\n");
960 fprintf(stderr, " -y\t\tprovide jail console\n");
961 fprintf(stderr, " -J <dir>\tcreate container from OCI bundle\n");
962 fprintf(stderr, " -j\t\tstart container immediately\n");
963 fprintf(stderr, " -P <pidfile>\tcreate <pidfile>\n");
964 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
965 and he has the same powers as root outside the jail,\n\
966 thus he can escape the jail and/or break stuff.\n\
967 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
968 If you use none of the namespace jail options,\n\
969 ujail will not use namespace/build a jail,\n\
970 and will only drop capabilities/apply seccomp filter.\n\n");
971 }
972
973 static int* get_namespace_fd(const unsigned int nstype)
974 {
975 switch (nstype) {
976 case CLONE_NEWPID:
977 return &opts.setns.pid;
978 case CLONE_NEWNET:
979 return &opts.setns.net;
980 case CLONE_NEWNS:
981 return &opts.setns.ns;
982 case CLONE_NEWIPC:
983 return &opts.setns.ipc;
984 case CLONE_NEWUTS:
985 return &opts.setns.uts;
986 case CLONE_NEWUSER:
987 return &opts.setns.user;
988 case CLONE_NEWCGROUP:
989 return &opts.setns.cgroup;
990 #ifdef CLONE_NEWTIME
991 case CLONE_NEWTIME:
992 return &opts.setns.time;
993 #endif
994 default:
995 return NULL;
996 }
997 }
998
999 static int setns_open(unsigned long nstype)
1000 {
1001 int *fd = get_namespace_fd(nstype);
1002
1003 if (!*fd)
1004 return EFAULT;
1005
1006 if (*fd == -1)
1007 return 0;
1008
1009 if (setns(*fd, nstype) == -1) {
1010 close(*fd);
1011 return errno;
1012 }
1013
1014 close(*fd);
1015 return 0;
1016 }
1017
1018 static int jail_running = 0;
1019 static int jail_return_code = 0;
1020
1021 static void jail_process_timeout_cb(struct uloop_timeout *t);
1022 static struct uloop_timeout jail_process_timeout = {
1023 .cb = jail_process_timeout_cb,
1024 };
1025 static void poststop(void);
1026 static void jail_process_handler(struct uloop_process *c, int ret)
1027 {
1028 uloop_timeout_cancel(&jail_process_timeout);
1029 if (WIFEXITED(ret)) {
1030 jail_return_code = WEXITSTATUS(ret);
1031 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
1032 } else {
1033 jail_return_code = WTERMSIG(ret);
1034 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
1035 }
1036 jail_running = 0;
1037 poststop();
1038 }
1039
1040 static struct uloop_process jail_process = {
1041 .cb = jail_process_handler,
1042 };
1043
1044 static void jail_process_timeout_cb(struct uloop_timeout *t)
1045 {
1046 DEBUG("jail process failed to stop, sending SIGKILL\n");
1047 kill(jail_process.pid, SIGKILL);
1048 }
1049
1050 static void jail_handle_signal(int signo)
1051 {
1052 if (hook_running) {
1053 DEBUG("forwarding signal %d to the hook process\n", signo);
1054 kill(hook_process.pid, signo);
1055 }
1056
1057 if (jail_running) {
1058 DEBUG("forwarding signal %d to the jailed process\n", signo);
1059 kill(jail_process.pid, signo);
1060 }
1061 }
1062
1063 static void signals_init(void)
1064 {
1065 int i;
1066 sigset_t sigmask;
1067
1068 sigfillset(&sigmask);
1069 for (i = 0; i < _NSIG; i++) {
1070 struct sigaction s = { 0 };
1071
1072 if (!sigismember(&sigmask, i))
1073 continue;
1074 if ((i == SIGCHLD) || (i == SIGPIPE) || (i == SIGSEGV))
1075 continue;
1076
1077 s.sa_handler = jail_handle_signal;
1078 sigaction(i, &s, NULL);
1079 }
1080 }
1081
1082 static void pre_exec_jail(struct uloop_timeout *t);
1083 static struct uloop_timeout pre_exec_timeout = {
1084 .cb = pre_exec_jail,
1085 };
1086
1087 int pipes[4];
1088 static int exec_jail(void *arg)
1089 {
1090 char buf[1];
1091
1092 uloop_init();
1093 signals_init();
1094
1095 close(pipes[0]);
1096 close(pipes[3]);
1097
1098 setns_open(CLONE_NEWUSER);
1099 setns_open(CLONE_NEWNET);
1100 setns_open(CLONE_NEWNS);
1101 setns_open(CLONE_NEWIPC);
1102 setns_open(CLONE_NEWUTS);
1103
1104 buf[0] = 'i';
1105 if (write(pipes[1], buf, 1) < 1) {
1106 ERROR("can't write to parent\n");
1107 return EXIT_FAILURE;
1108 }
1109 close(pipes[1]);
1110 if (read(pipes[2], buf, 1) < 1) {
1111 ERROR("can't read from parent\n");
1112 return EXIT_FAILURE;
1113 }
1114 if (buf[0] != 'O') {
1115 ERROR("parent had an error, child exiting\n");
1116 return EXIT_FAILURE;
1117 }
1118
1119 if (opts.namespace & CLONE_NEWCGROUP)
1120 unshare(CLONE_NEWCGROUP);
1121
1122 if ((opts.namespace & CLONE_NEWUSER) || (opts.setns.user != -1)) {
1123 if (setregid(0, 0) < 0) {
1124 ERROR("setgid\n");
1125 exit(EXIT_FAILURE);
1126 }
1127 if (setreuid(0, 0) < 0) {
1128 ERROR("setuid\n");
1129 exit(EXIT_FAILURE);
1130 }
1131 if (setgroups(0, NULL) < 0) {
1132 ERROR("setgroups\n");
1133 exit(EXIT_FAILURE);
1134 }
1135 }
1136
1137 if (opts.namespace && opts.hostname && strlen(opts.hostname) > 0
1138 && sethostname(opts.hostname, strlen(opts.hostname))) {
1139 ERROR("sethostname(%s) failed: %m\n", opts.hostname);
1140 exit(EXIT_FAILURE);
1141 }
1142
1143 uloop_timeout_add(&pre_exec_timeout);
1144 uloop_run();
1145
1146 exit(-1);
1147 }
1148
1149 static void pre_exec_jail(struct uloop_timeout *t)
1150 {
1151 if ((opts.namespace & CLONE_NEWNS) && build_jail_fs()) {
1152 ERROR("failed to build jail fs\n");
1153 exit(EXIT_FAILURE);
1154 } else {
1155 run_hooks(opts.hooks.createContainer, post_jail_fs);
1156 }
1157 }
1158
1159 static void post_start_hook(void);
1160 static void post_jail_fs(void)
1161 {
1162 char buf[1];
1163
1164 if (read(pipes[2], buf, 1) < 1) {
1165 ERROR("can't read from parent\n");
1166 exit(EXIT_FAILURE);
1167 }
1168 if (buf[0] != '!') {
1169 ERROR("parent had an error, child exiting\n");
1170 exit(EXIT_FAILURE);
1171 }
1172 close(pipes[2]);
1173
1174 run_hooks(opts.hooks.startContainer, post_start_hook);
1175 }
1176
1177 static void post_start_hook(void)
1178 {
1179 int pw_uid, pw_gid, gr_gid;
1180
1181 if (prctl(PR_SET_SECUREBITS, SECBIT_NO_SETUID_FIXUP)) {
1182 ERROR("prctl(PR_SET_SECUREBITS) failed: %m\n");
1183 exit(EXIT_FAILURE);
1184 }
1185
1186 /* drop capabilities, retain those still needed to further setup jail */
1187 if (applyOCIcapabilities(opts.capset, (1LLU << CAP_SETGID) | (1LLU << CAP_SETUID) | (1LLU << CAP_SETPCAP)))
1188 exit(EXIT_FAILURE);
1189
1190 /* use either cmdline-supplied user/group or uid/gid from OCI spec */
1191 get_jail_user(&pw_uid, &pw_gid, &gr_gid);
1192 set_jail_user(opts.pw_uid?:pw_uid, opts.pw_gid?:pw_gid, opts.gr_gid?:gr_gid);
1193
1194 if (opts.additional_gids &&
1195 (setgroups(opts.num_additional_gids, opts.additional_gids) < 0)) {
1196 ERROR("setgroups failed: %m\n");
1197 exit(EXIT_FAILURE);
1198 }
1199
1200 if (opts.set_umask)
1201 umask(opts.umask);
1202
1203 if (prctl(PR_SET_SECUREBITS, 0)) {
1204 ERROR("prctl(PR_SET_SECUREBITS) failed: %m\n");
1205 exit(EXIT_FAILURE);
1206 }
1207
1208 /* drop remaining capabilities to end up with specified sets */
1209 if (applyOCIcapabilities(opts.capset, 0))
1210 exit(EXIT_FAILURE);
1211
1212 if (opts.no_new_privs && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
1213 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
1214 exit(EXIT_FAILURE);
1215 }
1216
1217 char **envp = build_envp(opts.seccomp, opts.envp);
1218 if (!envp)
1219 exit(EXIT_FAILURE);
1220
1221 if (opts.cwd && chdir(opts.cwd))
1222 exit(EXIT_FAILURE);
1223
1224 if (opts.ociseccomp && applyOCIlinuxseccomp(opts.ociseccomp))
1225 exit(EXIT_FAILURE);
1226
1227 uloop_end();
1228 free_opts(false);
1229 INFO("exec-ing %s\n", *opts.jail_argv);
1230 if (opts.envp) /* respect PATH if potentially set in ENV */
1231 execvpe(*opts.jail_argv, opts.jail_argv, envp);
1232 else
1233 execve(*opts.jail_argv, opts.jail_argv, envp);
1234
1235 /* we get there only if execve fails */
1236 ERROR("failed to execve %s: %m\n", *opts.jail_argv);
1237 exit(EXIT_FAILURE);
1238 }
1239
1240 static int ns_open_pid(const char *nstype, const pid_t target_ns)
1241 {
1242 char pid_pid_path[PATH_MAX];
1243
1244 snprintf(pid_pid_path, sizeof(pid_pid_path), "/proc/%u/ns/%s", target_ns, nstype);
1245
1246 return open(pid_pid_path, O_RDONLY);
1247 }
1248
1249 static void netns_updown(pid_t pid, bool start)
1250 {
1251 static struct blob_buf req;
1252 uint32_t id;
1253
1254 if (!parent_ctx)
1255 return;
1256
1257 blob_buf_init(&req, 0);
1258 blobmsg_add_string(&req, "jail", opts.name);
1259 blobmsg_add_u32(&req, "pid", pid);
1260 blobmsg_add_u8(&req, "start", start);
1261
1262 if (ubus_lookup_id(parent_ctx, "network", &id) ||
1263 ubus_invoke(parent_ctx, id, "netns_updown", req.head, NULL, NULL, 3000))
1264 INFO("ubus request failed\n");
1265
1266 blob_buf_free(&req);
1267 }
1268
1269 static int parseOCIenvarray(struct blob_attr *msg, char ***envp)
1270 {
1271 struct blob_attr *cur;
1272 int sz = 0, rem;
1273
1274 blobmsg_for_each_attr(cur, msg, rem)
1275 ++sz;
1276
1277 if (sz > 0) {
1278 *envp = calloc(1 + sz, sizeof(char*));
1279 if (!(*envp))
1280 return ENOMEM;
1281 } else {
1282 *envp = NULL;
1283 return 0;
1284 }
1285
1286 sz = 0;
1287 blobmsg_for_each_attr(cur, msg, rem)
1288 (*envp)[sz++] = strdup(blobmsg_get_string(cur));
1289
1290 if (sz)
1291 (*envp)[sz] = NULL;
1292
1293 return 0;
1294 }
1295
1296 enum {
1297 OCI_ROOT_PATH,
1298 OCI_ROOT_READONLY,
1299 __OCI_ROOT_MAX,
1300 };
1301
1302 static const struct blobmsg_policy oci_root_policy[] = {
1303 [OCI_ROOT_PATH] = { "path", BLOBMSG_TYPE_STRING },
1304 [OCI_ROOT_READONLY] = { "readonly", BLOBMSG_TYPE_BOOL },
1305 };
1306
1307 static int parseOCIroot(const char *jsonfile, struct blob_attr *msg)
1308 {
1309 static char rootpath[PATH_MAX] = { 0 };
1310 struct blob_attr *tb[__OCI_ROOT_MAX];
1311 char *cur;
1312
1313 blobmsg_parse(oci_root_policy, __OCI_ROOT_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1314
1315 if (!tb[OCI_ROOT_PATH])
1316 return ENODATA;
1317
1318 strncpy(rootpath, jsonfile, PATH_MAX);
1319 cur = strrchr(rootpath, '/');
1320
1321 if (!cur)
1322 return ENOTDIR;
1323
1324 *(++cur) = '\0';
1325 strncat(rootpath, blobmsg_get_string(tb[OCI_ROOT_PATH]), PATH_MAX - (strlen(rootpath) + 1));
1326
1327 opts.extroot = rootpath;
1328
1329 if (tb[OCI_ROOT_READONLY])
1330 opts.ronly = blobmsg_get_bool(tb[OCI_ROOT_READONLY]);
1331
1332 return 0;
1333 }
1334
1335
1336 enum {
1337 OCI_HOOK_PATH,
1338 OCI_HOOK_ARGS,
1339 OCI_HOOK_ENV,
1340 OCI_HOOK_TIMEOUT,
1341 __OCI_HOOK_MAX,
1342 };
1343
1344 static const struct blobmsg_policy oci_hook_policy[] = {
1345 [OCI_HOOK_PATH] = { "path", BLOBMSG_TYPE_STRING },
1346 [OCI_HOOK_ARGS] = { "args", BLOBMSG_TYPE_ARRAY },
1347 [OCI_HOOK_ENV] = { "env", BLOBMSG_TYPE_ARRAY },
1348 [OCI_HOOK_TIMEOUT] = { "timeout", BLOBMSG_TYPE_INT32 },
1349 };
1350
1351
1352 static int parseOCIhook(struct hook_execvpe ***hooklist, struct blob_attr *msg)
1353 {
1354 struct blob_attr *tb[__OCI_HOOK_MAX];
1355 struct blob_attr *cur;
1356 int rem, ret = 0;
1357 int idx = 0;
1358
1359 blobmsg_for_each_attr(cur, msg, rem)
1360 ++idx;
1361
1362 if (!idx)
1363 return 0;
1364
1365 *hooklist = calloc(idx + 1, sizeof(struct hook_execvpe *));
1366 idx = 0;
1367
1368 if (!(*hooklist))
1369 return ENOMEM;
1370
1371 blobmsg_for_each_attr(cur, msg, rem) {
1372 blobmsg_parse(oci_hook_policy, __OCI_HOOK_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
1373
1374 if (!tb[OCI_HOOK_PATH]) {
1375 ret = EINVAL;
1376 goto errout;
1377 }
1378
1379 (*hooklist)[idx] = calloc(1, sizeof(struct hook_execvpe));
1380 if (tb[OCI_HOOK_ARGS]) {
1381 ret = parseOCIenvarray(tb[OCI_HOOK_ARGS], &((*hooklist)[idx]->argv));
1382 if (ret)
1383 goto errout;
1384 } else {
1385 (*hooklist)[idx]->argv = calloc(2, sizeof(char *));
1386 ((*hooklist)[idx]->argv)[0] = strdup(blobmsg_get_string(tb[OCI_HOOK_PATH]));
1387 ((*hooklist)[idx]->argv)[1] = NULL;
1388 };
1389
1390
1391 if (tb[OCI_HOOK_ENV]) {
1392 ret = parseOCIenvarray(tb[OCI_HOOK_ENV], &((*hooklist)[idx]->envp));
1393 if (ret)
1394 goto errout;
1395 }
1396
1397 if (tb[OCI_HOOK_TIMEOUT])
1398 (*hooklist)[idx]->timeout = blobmsg_get_u32(tb[OCI_HOOK_TIMEOUT]);
1399
1400 (*hooklist)[idx]->file = strdup(blobmsg_get_string(tb[OCI_HOOK_PATH]));
1401
1402 ++idx;
1403 }
1404
1405 (*hooklist)[idx] = NULL;
1406
1407 DEBUG("added %d hooks\n", idx);
1408
1409 return 0;
1410
1411 errout:
1412 free_hooklist(*hooklist);
1413 *hooklist = NULL;
1414
1415 return ret;
1416 };
1417
1418
1419 enum {
1420 OCI_HOOKS_PRESTART,
1421 OCI_HOOKS_CREATERUNTIME,
1422 OCI_HOOKS_CREATECONTAINER,
1423 OCI_HOOKS_STARTCONTAINER,
1424 OCI_HOOKS_POSTSTART,
1425 OCI_HOOKS_POSTSTOP,
1426 __OCI_HOOKS_MAX,
1427 };
1428
1429 static const struct blobmsg_policy oci_hooks_policy[] = {
1430 [OCI_HOOKS_PRESTART] = { "prestart", BLOBMSG_TYPE_ARRAY },
1431 [OCI_HOOKS_CREATERUNTIME] = { "createRuntime", BLOBMSG_TYPE_ARRAY },
1432 [OCI_HOOKS_CREATECONTAINER] = { "createContainer", BLOBMSG_TYPE_ARRAY },
1433 [OCI_HOOKS_STARTCONTAINER] = { "startContainer", BLOBMSG_TYPE_ARRAY },
1434 [OCI_HOOKS_POSTSTART] = { "poststart", BLOBMSG_TYPE_ARRAY },
1435 [OCI_HOOKS_POSTSTOP] = { "poststop", BLOBMSG_TYPE_ARRAY },
1436 };
1437
1438 static int parseOCIhooks(struct blob_attr *msg)
1439 {
1440 struct blob_attr *tb[__OCI_HOOKS_MAX];
1441 int ret;
1442
1443 blobmsg_parse(oci_hooks_policy, __OCI_HOOKS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1444
1445 if (tb[OCI_HOOKS_PRESTART])
1446 INFO("warning: ignoring deprecated prestart hook\n");
1447
1448 if (tb[OCI_HOOKS_CREATERUNTIME]) {
1449 ret = parseOCIhook(&opts.hooks.createRuntime, tb[OCI_HOOKS_CREATERUNTIME]);
1450 if (ret)
1451 return ret;
1452 }
1453
1454 if (tb[OCI_HOOKS_CREATECONTAINER]) {
1455 ret = parseOCIhook(&opts.hooks.createContainer, tb[OCI_HOOKS_CREATECONTAINER]);
1456 if (ret)
1457 goto out_createruntime;
1458 }
1459
1460 if (tb[OCI_HOOKS_STARTCONTAINER]) {
1461 ret = parseOCIhook(&opts.hooks.startContainer, tb[OCI_HOOKS_STARTCONTAINER]);
1462 if (ret)
1463 goto out_createcontainer;
1464 }
1465
1466 if (tb[OCI_HOOKS_POSTSTART]) {
1467 ret = parseOCIhook(&opts.hooks.poststart, tb[OCI_HOOKS_POSTSTART]);
1468 if (ret)
1469 goto out_startcontainer;
1470 }
1471
1472 if (tb[OCI_HOOKS_POSTSTOP]) {
1473 ret = parseOCIhook(&opts.hooks.poststop, tb[OCI_HOOKS_POSTSTOP]);
1474 if (ret)
1475 goto out_poststart;
1476 }
1477
1478 return 0;
1479
1480 out_poststart:
1481 free_hooklist(opts.hooks.poststart);
1482 out_startcontainer:
1483 free_hooklist(opts.hooks.startContainer);
1484 out_createcontainer:
1485 free_hooklist(opts.hooks.createContainer);
1486 out_createruntime:
1487 free_hooklist(opts.hooks.createRuntime);
1488
1489 return ret;
1490 };
1491
1492
1493 enum {
1494 OCI_PROCESS_USER_UID,
1495 OCI_PROCESS_USER_GID,
1496 OCI_PROCESS_USER_UMASK,
1497 OCI_PROCESS_USER_ADDITIONALGIDS,
1498 __OCI_PROCESS_USER_MAX,
1499 };
1500
1501 static const struct blobmsg_policy oci_process_user_policy[] = {
1502 [OCI_PROCESS_USER_UID] = { "uid", BLOBMSG_TYPE_INT32 },
1503 [OCI_PROCESS_USER_GID] = { "gid", BLOBMSG_TYPE_INT32 },
1504 [OCI_PROCESS_USER_UMASK] = { "umask", BLOBMSG_TYPE_INT32 },
1505 [OCI_PROCESS_USER_ADDITIONALGIDS] = { "additionalGids", BLOBMSG_TYPE_ARRAY },
1506 };
1507
1508 static int parseOCIprocessuser(struct blob_attr *msg) {
1509 struct blob_attr *tb[__OCI_PROCESS_USER_MAX];
1510 struct blob_attr *cur;
1511 int rem;
1512 int has_gid = 0;
1513
1514 blobmsg_parse(oci_process_user_policy, __OCI_PROCESS_USER_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1515
1516 if (tb[OCI_PROCESS_USER_UID])
1517 opts.pw_uid = blobmsg_get_u32(tb[OCI_PROCESS_USER_UID]);
1518
1519 if (tb[OCI_PROCESS_USER_GID]) {
1520 opts.pw_gid = blobmsg_get_u32(tb[OCI_PROCESS_USER_GID]);
1521 opts.gr_gid = blobmsg_get_u32(tb[OCI_PROCESS_USER_GID]);
1522 has_gid = 1;
1523 }
1524
1525 if (tb[OCI_PROCESS_USER_ADDITIONALGIDS]) {
1526 size_t gidcnt = 0;
1527
1528 blobmsg_for_each_attr(cur, tb[OCI_PROCESS_USER_ADDITIONALGIDS], rem) {
1529 ++gidcnt;
1530 if (has_gid && (blobmsg_get_u32(cur) == opts.gr_gid))
1531 continue;
1532 }
1533
1534 if (gidcnt) {
1535 opts.additional_gids = calloc(gidcnt + has_gid, sizeof(gid_t));
1536 gidcnt = 0;
1537
1538 /* always add primary GID to set of GIDs if set */
1539 if (has_gid)
1540 opts.additional_gids[gidcnt++] = opts.gr_gid;
1541
1542 blobmsg_for_each_attr(cur, tb[OCI_PROCESS_USER_ADDITIONALGIDS], rem) {
1543 if (has_gid && (blobmsg_get_u32(cur) == opts.gr_gid))
1544 continue;
1545 opts.additional_gids[gidcnt++] = blobmsg_get_u32(cur);
1546 }
1547 opts.num_additional_gids = gidcnt;
1548 }
1549 DEBUG("read %zu additional groups\n", gidcnt);
1550 }
1551
1552 if (tb[OCI_PROCESS_USER_UMASK]) {
1553 opts.umask = blobmsg_get_u32(tb[OCI_PROCESS_USER_UMASK]);
1554 opts.set_umask = true;
1555 }
1556
1557 return 0;
1558 }
1559
1560 enum {
1561 OCI_PROCESS_RLIMIT_TYPE,
1562 OCI_PROCESS_RLIMIT_SOFT,
1563 OCI_PROCESS_RLIMIT_HARD,
1564 __OCI_PROCESS_RLIMIT_MAX,
1565 };
1566
1567 static const struct blobmsg_policy oci_process_rlimit_policy[] = {
1568 [OCI_PROCESS_RLIMIT_TYPE] = { "type", BLOBMSG_TYPE_STRING },
1569 [OCI_PROCESS_RLIMIT_SOFT] = { "soft", BLOBMSG_CAST_INT64 },
1570 [OCI_PROCESS_RLIMIT_HARD] = { "hard", BLOBMSG_CAST_INT64 },
1571 };
1572
1573 /* from manpage GETRLIMIT(2) */
1574 static const char* const rlimit_names[RLIM_NLIMITS] = {
1575 [RLIMIT_AS] = "AS",
1576 [RLIMIT_CORE] = "CORE",
1577 [RLIMIT_CPU] = "CPU",
1578 [RLIMIT_DATA] = "DATA",
1579 [RLIMIT_FSIZE] = "FSIZE",
1580 [RLIMIT_LOCKS] = "LOCKS",
1581 [RLIMIT_MEMLOCK] = "MEMLOCK",
1582 [RLIMIT_MSGQUEUE] = "MSGQUEUE",
1583 [RLIMIT_NICE] = "NICE",
1584 [RLIMIT_NOFILE] = "NOFILE",
1585 [RLIMIT_NPROC] = "NPROC",
1586 [RLIMIT_RSS] = "RSS",
1587 [RLIMIT_RTPRIO] = "RTPRIO",
1588 [RLIMIT_RTTIME] = "RTTIME",
1589 [RLIMIT_SIGPENDING] = "SIGPENDING",
1590 [RLIMIT_STACK] = "STACK",
1591 };
1592
1593 static int resolve_rlimit(char *type) {
1594 unsigned int rltype;
1595
1596 for (rltype = 0; rltype < RLIM_NLIMITS; ++rltype)
1597 if (rlimit_names[rltype] &&
1598 !strncmp("RLIMIT_", type, 7) &&
1599 !strcmp(rlimit_names[rltype], type + 7))
1600 return rltype;
1601
1602 return -1;
1603 }
1604
1605
1606 static int parseOCIrlimit(struct blob_attr *msg)
1607 {
1608 struct blob_attr *tb[__OCI_PROCESS_RLIMIT_MAX];
1609 int limtype = -1;
1610 struct rlimit *curlim;
1611
1612 blobmsg_parse(oci_process_rlimit_policy, __OCI_PROCESS_RLIMIT_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1613
1614 if (!tb[OCI_PROCESS_RLIMIT_TYPE] ||
1615 !tb[OCI_PROCESS_RLIMIT_SOFT] ||
1616 !tb[OCI_PROCESS_RLIMIT_HARD])
1617 return ENODATA;
1618
1619 limtype = resolve_rlimit(blobmsg_get_string(tb[OCI_PROCESS_RLIMIT_TYPE]));
1620
1621 if (limtype < 0)
1622 return EINVAL;
1623
1624 if (opts.rlimits[limtype])
1625 return ENOTUNIQ;
1626
1627 curlim = malloc(sizeof(struct rlimit));
1628 curlim->rlim_cur = blobmsg_cast_u64(tb[OCI_PROCESS_RLIMIT_SOFT]);
1629 curlim->rlim_max = blobmsg_cast_u64(tb[OCI_PROCESS_RLIMIT_HARD]);
1630
1631 opts.rlimits[limtype] = curlim;
1632
1633 return 0;
1634 };
1635
1636 enum {
1637 OCI_PROCESS_ARGS,
1638 OCI_PROCESS_CAPABILITIES,
1639 OCI_PROCESS_CWD,
1640 OCI_PROCESS_ENV,
1641 OCI_PROCESS_OOMSCOREADJ,
1642 OCI_PROCESS_NONEWPRIVILEGES,
1643 OCI_PROCESS_RLIMITS,
1644 OCI_PROCESS_TERMINAL,
1645 OCI_PROCESS_USER,
1646 __OCI_PROCESS_MAX,
1647 };
1648
1649 static const struct blobmsg_policy oci_process_policy[] = {
1650 [OCI_PROCESS_ARGS] = { "args", BLOBMSG_TYPE_ARRAY },
1651 [OCI_PROCESS_CAPABILITIES] = { "capabilities", BLOBMSG_TYPE_TABLE },
1652 [OCI_PROCESS_CWD] = { "cwd", BLOBMSG_TYPE_STRING },
1653 [OCI_PROCESS_ENV] = { "env", BLOBMSG_TYPE_ARRAY },
1654 [OCI_PROCESS_OOMSCOREADJ] = { "oomScoreAdj", BLOBMSG_TYPE_INT32 },
1655 [OCI_PROCESS_NONEWPRIVILEGES] = { "noNewPrivileges", BLOBMSG_TYPE_BOOL },
1656 [OCI_PROCESS_RLIMITS] = { "rlimits", BLOBMSG_TYPE_ARRAY },
1657 [OCI_PROCESS_TERMINAL] = { "terminal", BLOBMSG_TYPE_BOOL },
1658 [OCI_PROCESS_USER] = { "user", BLOBMSG_TYPE_TABLE },
1659 };
1660
1661
1662 static int parseOCIprocess(struct blob_attr *msg)
1663 {
1664 struct blob_attr *tb[__OCI_PROCESS_MAX], *cur;
1665 int rem, res;
1666
1667 blobmsg_parse(oci_process_policy, __OCI_PROCESS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1668
1669 if (!tb[OCI_PROCESS_ARGS])
1670 return ENOENT;
1671
1672 res = parseOCIenvarray(tb[OCI_PROCESS_ARGS], &opts.jail_argv);
1673 if (res)
1674 return res;
1675
1676 if (tb[OCI_PROCESS_TERMINAL])
1677 opts.console = blobmsg_get_bool(tb[OCI_PROCESS_TERMINAL]);
1678
1679 if (tb[OCI_PROCESS_NONEWPRIVILEGES])
1680 opts.no_new_privs = blobmsg_get_bool(tb[OCI_PROCESS_NONEWPRIVILEGES]);
1681
1682 if (tb[OCI_PROCESS_CWD])
1683 opts.cwd = strdup(blobmsg_get_string(tb[OCI_PROCESS_CWD]));
1684
1685 if (tb[OCI_PROCESS_ENV]) {
1686 res = parseOCIenvarray(tb[OCI_PROCESS_ENV], &opts.envp);
1687 if (res)
1688 return res;
1689 }
1690
1691 if (tb[OCI_PROCESS_USER] && (res = parseOCIprocessuser(tb[OCI_PROCESS_USER])))
1692 return res;
1693
1694 if (tb[OCI_PROCESS_CAPABILITIES] &&
1695 (res = parseOCIcapabilities(&opts.capset, tb[OCI_PROCESS_CAPABILITIES])))
1696 return res;
1697
1698 if (tb[OCI_PROCESS_RLIMITS]) {
1699 blobmsg_for_each_attr(cur, tb[OCI_PROCESS_RLIMITS], rem) {
1700 res = parseOCIrlimit(cur);
1701 if (res)
1702 return res;
1703 }
1704 }
1705
1706 if (tb[OCI_PROCESS_OOMSCOREADJ]) {
1707 opts.oom_score_adj = blobmsg_get_u32(tb[OCI_PROCESS_OOMSCOREADJ]);
1708 opts.set_oom_score_adj = true;
1709 }
1710
1711 return 0;
1712 }
1713
1714 enum {
1715 OCI_LINUX_NAMESPACE_TYPE,
1716 OCI_LINUX_NAMESPACE_PATH,
1717 __OCI_LINUX_NAMESPACE_MAX,
1718 };
1719
1720 static const struct blobmsg_policy oci_linux_namespace_policy[] = {
1721 [OCI_LINUX_NAMESPACE_TYPE] = { "type", BLOBMSG_TYPE_STRING },
1722 [OCI_LINUX_NAMESPACE_PATH] = { "path", BLOBMSG_TYPE_STRING },
1723 };
1724
1725 static int resolve_nstype(char *type) {
1726 if (!strcmp("pid", type))
1727 return CLONE_NEWPID;
1728 else if (!strcmp("network", type))
1729 return CLONE_NEWNET;
1730 else if (!strcmp("mount", type))
1731 return CLONE_NEWNS;
1732 else if (!strcmp("ipc", type))
1733 return CLONE_NEWIPC;
1734 else if (!strcmp("uts", type))
1735 return CLONE_NEWUTS;
1736 else if (!strcmp("user", type))
1737 return CLONE_NEWUSER;
1738 else if (!strcmp("cgroup", type))
1739 return CLONE_NEWCGROUP;
1740 #ifdef CLONE_NEWTIME
1741 else if (!strcmp("time", type))
1742 return CLONE_NEWTIME;
1743 #endif
1744 else
1745 return 0;
1746 }
1747
1748 static int parseOCIlinuxns(struct blob_attr *msg)
1749 {
1750 struct blob_attr *tb[__OCI_LINUX_NAMESPACE_MAX];
1751 int nstype;
1752 int *setns;
1753 int fd;
1754
1755 blobmsg_parse(oci_linux_namespace_policy, __OCI_LINUX_NAMESPACE_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1756
1757 if (!tb[OCI_LINUX_NAMESPACE_TYPE])
1758 return EINVAL;
1759
1760 nstype = resolve_nstype(blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]));
1761 if (!nstype)
1762 return EINVAL;
1763
1764 if (opts.namespace & nstype)
1765 return ENOTUNIQ;
1766
1767 setns = get_namespace_fd(nstype);
1768
1769 if (!setns)
1770 return EFAULT;
1771
1772 if (*setns != -1)
1773 return ENOTUNIQ;
1774
1775 if (tb[OCI_LINUX_NAMESPACE_PATH]) {
1776 DEBUG("opening existing %s namespace from path %s\n",
1777 blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]),
1778 blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_PATH]));
1779
1780 fd = open(blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_PATH]), O_RDONLY);
1781 if (fd == -1)
1782 return errno?:ESTALE;
1783
1784 if (ioctl(fd, NS_GET_NSTYPE) != nstype)
1785 return EINVAL;
1786
1787 DEBUG("opened existing %s namespace got filehandler %u\n",
1788 blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]),
1789 fd);
1790
1791 *setns = fd;
1792 } else {
1793 opts.namespace |= nstype;
1794 }
1795
1796 return 0;
1797 };
1798
1799
1800 enum {
1801 OCI_LINUX_UIDGIDMAP_CONTAINERID,
1802 OCI_LINUX_UIDGIDMAP_HOSTID,
1803 OCI_LINUX_UIDGIDMAP_SIZE,
1804 __OCI_LINUX_UIDGIDMAP_MAX,
1805 };
1806
1807 static const struct blobmsg_policy oci_linux_uidgidmap_policy[] = {
1808 [OCI_LINUX_UIDGIDMAP_CONTAINERID] = { "containerID", BLOBMSG_TYPE_INT32 },
1809 [OCI_LINUX_UIDGIDMAP_HOSTID] = { "hostID", BLOBMSG_TYPE_INT32 },
1810 [OCI_LINUX_UIDGIDMAP_SIZE] = { "size", BLOBMSG_TYPE_INT32 },
1811 };
1812
1813 static int parseOCIuidgidmappings(struct blob_attr *msg, bool is_gidmap)
1814 {
1815 const char *map_format = "%d %d %d\n";
1816 struct blob_attr *tb[__OCI_LINUX_UIDGIDMAP_MAX];
1817 struct blob_attr *cur;
1818 int rem, len;
1819 char **mappings;
1820 char *map, *curstr;
1821 unsigned int cnt = 0;
1822 size_t totallen = 0;
1823
1824 /* count number of mappings */
1825 blobmsg_for_each_attr(cur, msg, rem)
1826 cnt++;
1827
1828 if (!cnt)
1829 return 0;
1830
1831 /* allocate array for mappings */
1832 mappings = calloc(1 + cnt, sizeof(char*));
1833 if (!mappings)
1834 return ENOMEM;
1835
1836 mappings[cnt] = NULL;
1837
1838 cnt = 0;
1839 blobmsg_for_each_attr(cur, msg, rem) {
1840 blobmsg_parse(oci_linux_uidgidmap_policy, __OCI_LINUX_UIDGIDMAP_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
1841
1842 if (!tb[OCI_LINUX_UIDGIDMAP_CONTAINERID] ||
1843 !tb[OCI_LINUX_UIDGIDMAP_HOSTID] ||
1844 !tb[OCI_LINUX_UIDGIDMAP_SIZE])
1845 return EINVAL;
1846
1847 /* write mapping line into allocated string */
1848 len = asprintf(&mappings[cnt++], map_format,
1849 blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_CONTAINERID]),
1850 blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_HOSTID]),
1851 blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_SIZE]));
1852
1853 if (len < 0)
1854 return ENOMEM;
1855
1856 totallen += len;
1857 }
1858
1859 /* allocate combined mapping string */
1860 map = calloc(1 + totallen, sizeof(char));
1861 if (!map)
1862 return ENOMEM;
1863
1864 map[0] = '\0';
1865
1866 /* concatenate mapping strings into combined string */
1867 curstr = mappings[0];
1868 while (curstr) {
1869 strcat(map, curstr);
1870 free(curstr++);
1871 }
1872 free(mappings);
1873
1874 if (is_gidmap)
1875 opts.gidmap = map;
1876 else
1877 opts.uidmap = map;
1878
1879 return 0;
1880 }
1881
1882 enum {
1883 OCI_DEVICES_TYPE,
1884 OCI_DEVICES_PATH,
1885 OCI_DEVICES_MAJOR,
1886 OCI_DEVICES_MINOR,
1887 OCI_DEVICES_FILEMODE,
1888 OCI_DEVICES_UID,
1889 OCI_DEVICES_GID,
1890 __OCI_DEVICES_MAX,
1891 };
1892
1893 static const struct blobmsg_policy oci_devices_policy[] = {
1894 [OCI_DEVICES_TYPE] = { "type", BLOBMSG_TYPE_STRING },
1895 [OCI_DEVICES_PATH] = { "path", BLOBMSG_TYPE_STRING },
1896 [OCI_DEVICES_MAJOR] = { "major", BLOBMSG_TYPE_INT32 },
1897 [OCI_DEVICES_MINOR] = { "minor", BLOBMSG_TYPE_INT32 },
1898 [OCI_DEVICES_FILEMODE] = { "fileMode", BLOBMSG_TYPE_INT32 },
1899 [OCI_DEVICES_UID] = { "uid", BLOBMSG_TYPE_INT32 },
1900 [OCI_DEVICES_GID] = { "uid", BLOBMSG_TYPE_INT32 },
1901 };
1902
1903 static mode_t resolve_devtype(char *tstr)
1904 {
1905 if (!strcmp("c", tstr) ||
1906 !strcmp("u", tstr))
1907 return S_IFCHR;
1908 else if (!strcmp("b", tstr))
1909 return S_IFBLK;
1910 else if (!strcmp("p", tstr))
1911 return S_IFIFO;
1912 else
1913 return 0;
1914 }
1915
1916 static int parseOCIdevices(struct blob_attr *msg)
1917 {
1918 struct blob_attr *tb[__OCI_DEVICES_MAX];
1919 struct blob_attr *cur;
1920 int rem;
1921 size_t cnt = 0;
1922 struct mknod_args *tmp;
1923
1924 blobmsg_for_each_attr(cur, msg, rem)
1925 ++cnt;
1926
1927 opts.devices = calloc(cnt + 1, sizeof(struct mknod_args *));
1928
1929 cnt = 0;
1930 blobmsg_for_each_attr(cur, msg, rem) {
1931 blobmsg_parse(oci_devices_policy, __OCI_DEVICES_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
1932 if (!tb[OCI_DEVICES_TYPE] ||
1933 !tb[OCI_DEVICES_PATH])
1934 return ENODATA;
1935
1936 tmp = calloc(1, sizeof(struct mknod_args));
1937 if (!tmp)
1938 return ENOMEM;
1939
1940 tmp->mode = resolve_devtype(blobmsg_get_string(tb[OCI_DEVICES_TYPE]));
1941 if (!tmp->mode)
1942 return EINVAL;
1943
1944 if (tmp->mode != S_IFIFO) {
1945 if (!tb[OCI_DEVICES_MAJOR] || !tb[OCI_DEVICES_MINOR])
1946 return ENODATA;
1947
1948 tmp->dev = makedev(blobmsg_get_u32(tb[OCI_DEVICES_MAJOR]),
1949 blobmsg_get_u32(tb[OCI_DEVICES_MINOR]));
1950 }
1951
1952 if (tb[OCI_DEVICES_FILEMODE]) {
1953 if (~(S_IRWXU|S_IRWXG|S_IRWXO) & blobmsg_get_u32(tb[OCI_DEVICES_FILEMODE]))
1954 return EINVAL;
1955
1956 tmp->mode |= blobmsg_get_u32(tb[OCI_DEVICES_FILEMODE]);
1957 } else {
1958 tmp->mode |= (S_IRUSR|S_IWUSR); /* 0600 */
1959 }
1960
1961 tmp->path = strdup(blobmsg_get_string(tb[OCI_DEVICES_PATH]));
1962
1963 if (tb[OCI_DEVICES_UID])
1964 tmp->uid = blobmsg_get_u32(tb[OCI_DEVICES_UID]);
1965 else
1966 tmp->uid = -1;
1967
1968 if (tb[OCI_DEVICES_GID])
1969 tmp->gid = blobmsg_get_u32(tb[OCI_DEVICES_GID]);
1970 else
1971 tmp->gid = -1;
1972
1973 DEBUG("read device %s (%s)\n", blobmsg_get_string(tb[OCI_DEVICES_PATH]), blobmsg_get_string(tb[OCI_DEVICES_TYPE]));
1974 opts.devices[cnt++] = tmp;
1975 }
1976
1977 opts.devices[cnt] = NULL;
1978
1979 return 0;
1980 }
1981
1982 static int parseOCIsysctl(struct blob_attr *msg)
1983 {
1984 struct blob_attr *cur;
1985 int rem;
1986 char *tmp, *tc;
1987 size_t cnt = 0;
1988
1989 blobmsg_for_each_attr(cur, msg, rem) {
1990 if (!blobmsg_name(cur) || !blobmsg_get_string(cur))
1991 return EINVAL;
1992
1993 ++cnt;
1994 }
1995
1996 if (!cnt)
1997 return 0;
1998
1999 opts.sysctl = calloc(cnt + 1, sizeof(struct sysctl_val *));
2000 if (!opts.sysctl)
2001 return ENOMEM;
2002
2003 cnt = 0;
2004 blobmsg_for_each_attr(cur, msg, rem) {
2005 opts.sysctl[cnt] = malloc(sizeof(struct sysctl_val));
2006 if (!opts.sysctl[cnt])
2007 return ENOMEM;
2008
2009 /* replace '.' with '/' in entry name */
2010 tc = tmp = strdup(blobmsg_name(cur));
2011 while ((tc = strchr(tc, '.')))
2012 *tc = '/';
2013
2014 opts.sysctl[cnt]->value = strdup(blobmsg_get_string(cur));
2015 opts.sysctl[cnt]->entry = tmp;
2016
2017 ++cnt;
2018 }
2019
2020 opts.sysctl[cnt] = NULL;
2021
2022 return 0;
2023 }
2024
2025
2026 enum {
2027 OCI_LINUX_CGROUPSPATH,
2028 OCI_LINUX_RESOURCES,
2029 OCI_LINUX_SECCOMP,
2030 OCI_LINUX_SYSCTL,
2031 OCI_LINUX_NAMESPACES,
2032 OCI_LINUX_DEVICES,
2033 OCI_LINUX_UIDMAPPINGS,
2034 OCI_LINUX_GIDMAPPINGS,
2035 OCI_LINUX_MASKEDPATHS,
2036 OCI_LINUX_READONLYPATHS,
2037 OCI_LINUX_ROOTFSPROPAGATION,
2038 __OCI_LINUX_MAX,
2039 };
2040
2041 static const struct blobmsg_policy oci_linux_policy[] = {
2042 [OCI_LINUX_CGROUPSPATH] = { "cgroupsPath", BLOBMSG_TYPE_STRING },
2043 [OCI_LINUX_RESOURCES] = { "resources", BLOBMSG_TYPE_TABLE },
2044 [OCI_LINUX_SECCOMP] = { "seccomp", BLOBMSG_TYPE_TABLE },
2045 [OCI_LINUX_SYSCTL] = { "sysctl", BLOBMSG_TYPE_TABLE },
2046 [OCI_LINUX_NAMESPACES] = { "namespaces", BLOBMSG_TYPE_ARRAY },
2047 [OCI_LINUX_DEVICES] = { "devices", BLOBMSG_TYPE_ARRAY },
2048 [OCI_LINUX_UIDMAPPINGS] = { "uidMappings", BLOBMSG_TYPE_ARRAY },
2049 [OCI_LINUX_GIDMAPPINGS] = { "gidMappings", BLOBMSG_TYPE_ARRAY },
2050 [OCI_LINUX_MASKEDPATHS] = { "maskedPaths", BLOBMSG_TYPE_ARRAY },
2051 [OCI_LINUX_READONLYPATHS] = { "readonlyPaths", BLOBMSG_TYPE_ARRAY },
2052 [OCI_LINUX_ROOTFSPROPAGATION] = { "rootfsPropagation", BLOBMSG_TYPE_STRING },
2053 };
2054
2055 static int parseOCIlinux(struct blob_attr *msg)
2056 {
2057 struct blob_attr *tb[__OCI_LINUX_MAX];
2058 struct blob_attr *cur;
2059 int rem;
2060 int res = 0;
2061 char *cgpath;
2062 char cgfullpath[256] = "/sys/fs/cgroup";
2063
2064 blobmsg_parse(oci_linux_policy, __OCI_LINUX_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
2065
2066 if (tb[OCI_LINUX_NAMESPACES]) {
2067 blobmsg_for_each_attr(cur, tb[OCI_LINUX_NAMESPACES], rem) {
2068 res = parseOCIlinuxns(cur);
2069 if (res)
2070 return res;
2071 }
2072 }
2073
2074 if (tb[OCI_LINUX_UIDMAPPINGS]) {
2075 res = parseOCIuidgidmappings(tb[OCI_LINUX_GIDMAPPINGS], 0);
2076 if (res)
2077 return res;
2078 }
2079
2080 if (tb[OCI_LINUX_GIDMAPPINGS]) {
2081 res = parseOCIuidgidmappings(tb[OCI_LINUX_GIDMAPPINGS], 1);
2082 if (res)
2083 return res;
2084 }
2085
2086 if (tb[OCI_LINUX_READONLYPATHS]) {
2087 blobmsg_for_each_attr(cur, tb[OCI_LINUX_READONLYPATHS], rem) {
2088 res = add_mount(NULL, blobmsg_get_string(cur), NULL, MS_BIND | MS_REC | MS_RDONLY, 0, NULL, 0);
2089 if (res)
2090 return res;
2091 }
2092 }
2093
2094 if (tb[OCI_LINUX_MASKEDPATHS]) {
2095 blobmsg_for_each_attr(cur, tb[OCI_LINUX_MASKEDPATHS], rem) {
2096 res = add_mount((void *)(-1), blobmsg_get_string(cur), NULL, 0, 0, NULL, 1);
2097 if (res)
2098 return res;
2099 }
2100 }
2101
2102 if (tb[OCI_LINUX_SYSCTL]) {
2103 res = parseOCIsysctl(tb[OCI_LINUX_SYSCTL]);
2104 if (res)
2105 return res;
2106 }
2107
2108 if (tb[OCI_LINUX_SECCOMP]) {
2109 opts.ociseccomp = parseOCIlinuxseccomp(tb[OCI_LINUX_SECCOMP]);
2110 if (!opts.ociseccomp)
2111 return EINVAL;
2112 }
2113
2114 if (tb[OCI_LINUX_DEVICES]) {
2115 res = parseOCIdevices(tb[OCI_LINUX_DEVICES]);
2116 if (res)
2117 return res;
2118 }
2119
2120 if (tb[OCI_LINUX_CGROUPSPATH]) {
2121 cgpath = blobmsg_get_string(tb[OCI_LINUX_CGROUPSPATH]);
2122 if (cgpath[0] == '/') {
2123 if (strlen(cgpath) >= (sizeof(cgfullpath) - strlen(cgfullpath)))
2124 return E2BIG;
2125
2126 strcat(cgfullpath, cgpath);
2127 } else {
2128 strcat(cgfullpath, "/containers/");
2129 strcat(cgfullpath, opts.name); /* should be container name rather than jail name */
2130 strcat(cgfullpath, "/");
2131 if (strlen(cgpath) >= (sizeof(cgfullpath) - strlen(cgfullpath)))
2132 return E2BIG;
2133
2134 strcat(cgfullpath, cgpath);
2135 }
2136 } else {
2137 strcat(cgfullpath, "/containers/");
2138 strcat(cgfullpath, opts.name); /* should be container name rather than jail name */
2139 strcat(cgfullpath, "/");
2140 strcat(cgfullpath, opts.name); /* should be container instance name rather than jail name */
2141 }
2142
2143 cgroups_init(cgfullpath);
2144
2145 if (tb[OCI_LINUX_RESOURCES]) {
2146 res = parseOCIlinuxcgroups(tb[OCI_LINUX_RESOURCES]);
2147 if (res)
2148 return res;
2149 }
2150
2151 return 0;
2152 }
2153
2154 enum {
2155 OCI_VERSION,
2156 OCI_HOSTNAME,
2157 OCI_PROCESS,
2158 OCI_ROOT,
2159 OCI_MOUNTS,
2160 OCI_HOOKS,
2161 OCI_LINUX,
2162 OCI_ANNOTATIONS,
2163 __OCI_MAX,
2164 };
2165
2166 static const struct blobmsg_policy oci_policy[] = {
2167 [OCI_VERSION] = { "ociVersion", BLOBMSG_TYPE_STRING },
2168 [OCI_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
2169 [OCI_PROCESS] = { "process", BLOBMSG_TYPE_TABLE },
2170 [OCI_ROOT] = { "root", BLOBMSG_TYPE_TABLE },
2171 [OCI_MOUNTS] = { "mounts", BLOBMSG_TYPE_ARRAY },
2172 [OCI_HOOKS] = { "hooks", BLOBMSG_TYPE_TABLE },
2173 [OCI_LINUX] = { "linux", BLOBMSG_TYPE_TABLE },
2174 [OCI_ANNOTATIONS] = { "annotations", BLOBMSG_TYPE_TABLE },
2175 };
2176
2177 static int parseOCI(const char *jsonfile)
2178 {
2179 struct blob_attr *tb[__OCI_MAX];
2180 struct blob_attr *cur;
2181 int rem;
2182 int res;
2183
2184 blob_buf_init(&ocibuf, 0);
2185 if (!blobmsg_add_json_from_file(&ocibuf, jsonfile))
2186 return ENOENT;
2187
2188 blobmsg_parse(oci_policy, __OCI_MAX, tb, blob_data(ocibuf.head), blob_len(ocibuf.head));
2189
2190 if (!tb[OCI_VERSION])
2191 return ENOMSG;
2192
2193 if (strncmp("1.0", blobmsg_get_string(tb[OCI_VERSION]), 3)) {
2194 ERROR("unsupported ociVersion %s\n", blobmsg_get_string(tb[OCI_VERSION]));
2195 return ENOTSUP;
2196 }
2197
2198 if (tb[OCI_HOSTNAME])
2199 opts.hostname = strdup(blobmsg_get_string(tb[OCI_HOSTNAME]));
2200
2201 if (!tb[OCI_PROCESS])
2202 return ENODATA;
2203
2204 if ((res = parseOCIprocess(tb[OCI_PROCESS])))
2205 return res;
2206
2207 if (!tb[OCI_ROOT])
2208 return ENODATA;
2209
2210 if ((res = parseOCIroot(jsonfile, tb[OCI_ROOT])))
2211 return res;
2212
2213 if (!tb[OCI_MOUNTS])
2214 return ENODATA;
2215
2216 blobmsg_for_each_attr(cur, tb[OCI_MOUNTS], rem)
2217 if ((res = parseOCImount(cur)))
2218 return res;
2219
2220 if (tb[OCI_LINUX] && (res = parseOCIlinux(tb[OCI_LINUX])))
2221 return res;
2222
2223 if (tb[OCI_HOOKS] && (res = parseOCIhooks(tb[OCI_HOOKS])))
2224 return res;
2225
2226 if (tb[OCI_ANNOTATIONS])
2227 opts.annotations = blob_memdup(tb[OCI_ANNOTATIONS]);
2228
2229 blob_buf_free(&ocibuf);
2230
2231 return 0;
2232 }
2233
2234 static int set_oom_score_adj(void)
2235 {
2236 int f;
2237 char fname[32];
2238
2239 if (!opts.set_oom_score_adj)
2240 return 0;
2241
2242 snprintf(fname, sizeof(fname), "/proc/%u/oom_score_adj", jail_process.pid);
2243 f = open(fname, O_WRONLY | O_TRUNC);
2244 if (f == -1)
2245 return errno;
2246
2247 dprintf(f, "%d", opts.oom_score_adj);
2248 close(f);
2249
2250 return 0;
2251 }
2252
2253
2254 enum {
2255 OCI_STATE_CREATING,
2256 OCI_STATE_CREATED,
2257 OCI_STATE_RUNNING,
2258 OCI_STATE_STOPPED,
2259 };
2260
2261 static int jail_oci_state = OCI_STATE_CREATED;
2262 static void pipe_send_start_container(struct uloop_timeout *t);
2263 static struct uloop_timeout start_container_timeout = {
2264 .cb = pipe_send_start_container,
2265 };
2266
2267 static int handle_start(struct ubus_context *ctx, struct ubus_object *obj,
2268 struct ubus_request_data *req, const char *method,
2269 struct blob_attr *msg)
2270 {
2271 if (jail_oci_state != OCI_STATE_CREATED)
2272 return UBUS_STATUS_INVALID_ARGUMENT;
2273
2274 uloop_timeout_add(&start_container_timeout);
2275
2276 return UBUS_STATUS_OK;
2277 }
2278
2279 static struct blob_buf bb;
2280 static int handle_state(struct ubus_context *ctx, struct ubus_object *obj,
2281 struct ubus_request_data *req, const char *method,
2282 struct blob_attr *msg)
2283 {
2284 char *statusstr;
2285
2286 switch (jail_oci_state) {
2287 case OCI_STATE_CREATING:
2288 statusstr = "creating";
2289 break;
2290 case OCI_STATE_CREATED:
2291 statusstr = "created";
2292 break;
2293 case OCI_STATE_RUNNING:
2294 statusstr = "running";
2295 break;
2296 case OCI_STATE_STOPPED:
2297 statusstr = "stopped";
2298 break;
2299 default:
2300 statusstr = "unknown";
2301 }
2302
2303 blob_buf_init(&bb, 0);
2304 blobmsg_add_string(&bb, "ociVersion", OCI_VERSION_STRING);
2305 blobmsg_add_string(&bb, "id", opts.name);
2306 blobmsg_add_string(&bb, "status", statusstr);
2307 if (jail_oci_state == OCI_STATE_CREATED ||
2308 jail_oci_state == OCI_STATE_RUNNING)
2309 blobmsg_add_u32(&bb, "pid", jail_process.pid);
2310
2311 blobmsg_add_string(&bb, "bundle", opts.ocibundle);
2312
2313 if (opts.annotations)
2314 blobmsg_add_blob(&bb, opts.annotations);
2315
2316 ubus_send_reply(ctx, req, bb.head);
2317
2318 return UBUS_STATUS_OK;
2319 }
2320
2321 enum {
2322 CONTAINER_KILL_ATTR_SIGNAL,
2323 __CONTAINER_KILL_ATTR_MAX,
2324 };
2325
2326 static const struct blobmsg_policy container_kill_attrs[__CONTAINER_KILL_ATTR_MAX] = {
2327 [CONTAINER_KILL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
2328 };
2329
2330 static int
2331 container_handle_kill(struct ubus_context *ctx, struct ubus_object *obj,
2332 struct ubus_request_data *req, const char *method,
2333 struct blob_attr *msg)
2334 {
2335 struct blob_attr *tb[__CONTAINER_KILL_ATTR_MAX], *cur;
2336 int sig = SIGTERM;
2337
2338 blobmsg_parse(container_kill_attrs, __CONTAINER_KILL_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
2339
2340 cur = tb[CONTAINER_KILL_ATTR_SIGNAL];
2341 if (cur)
2342 sig = blobmsg_get_u32(cur);
2343
2344 if (jail_oci_state == OCI_STATE_CREATING)
2345 return UBUS_STATUS_NOT_FOUND;
2346
2347 if (kill(jail_process.pid, sig) == 0)
2348 return 0;
2349
2350 switch (errno) {
2351 case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
2352 case EPERM: return UBUS_STATUS_PERMISSION_DENIED;
2353 case ESRCH: return UBUS_STATUS_NOT_FOUND;
2354 }
2355
2356 return UBUS_STATUS_UNKNOWN_ERROR;
2357 }
2358
2359 static int
2360 jail_writepid(pid_t pid)
2361 {
2362 FILE *_pidfile;
2363
2364 if (!opts.pidfile)
2365 return 0;
2366
2367 _pidfile = fopen(opts.pidfile, "w");
2368 if (_pidfile == NULL)
2369 return errno;
2370
2371 if (fprintf(_pidfile, "%d\n", pid) < 0) {
2372 fclose(_pidfile);
2373 return errno;
2374 }
2375
2376 if (fclose(_pidfile))
2377 return errno;
2378
2379 return 0;
2380 }
2381
2382 static struct ubus_method container_methods[] = {
2383 UBUS_METHOD_NOARG("start", handle_start),
2384 UBUS_METHOD_NOARG("state", handle_state),
2385 UBUS_METHOD("kill", container_handle_kill, container_kill_attrs),
2386 };
2387
2388 static struct ubus_object_type container_object_type =
2389 UBUS_OBJECT_TYPE("container", container_methods);
2390
2391 static struct ubus_object container_object = {
2392 .type = &container_object_type,
2393 .methods = container_methods,
2394 .n_methods = ARRAY_SIZE(container_methods),
2395 };
2396
2397 static void post_main(struct uloop_timeout *t);
2398 static struct uloop_timeout post_main_timeout = {
2399 .cb = post_main,
2400 };
2401 static int netns_fd;
2402 static int pidns_fd;
2403 #ifdef CLONE_NEWTIME
2404 static int timens_fd;
2405 #endif
2406 static void post_create_runtime(void);
2407 int main(int argc, char **argv)
2408 {
2409 uid_t uid = getuid();
2410 const char log[] = "/dev/log";
2411 const char ubus[] = "/var/run/ubus/ubus.sock";
2412 int ch, ret;
2413
2414 if (uid) {
2415 ERROR("not root, aborting: %m\n");
2416 return EXIT_FAILURE;
2417 }
2418
2419 umask(022);
2420 mount_list_init();
2421 init_library_search();
2422
2423 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
2424 switch (ch) {
2425 case 'd':
2426 debug = atoi(optarg);
2427 break;
2428 case 'p':
2429 opts.namespace |= CLONE_NEWNS;
2430 opts.procfs = 1;
2431 break;
2432 case 'o':
2433 opts.namespace |= CLONE_NEWNS;
2434 opts.ronly = 1;
2435 break;
2436 case 'f':
2437 opts.namespace |= CLONE_NEWUSER;
2438 break;
2439 case 'F':
2440 opts.namespace |= CLONE_NEWCGROUP;
2441 break;
2442 case 'R':
2443 opts.extroot = strdup(optarg);
2444 break;
2445 case 's':
2446 opts.namespace |= CLONE_NEWNS;
2447 opts.sysfs = 1;
2448 break;
2449 case 'S':
2450 opts.seccomp = optarg;
2451 add_mount_bind(optarg, 1, -1);
2452 break;
2453 case 'C':
2454 opts.capabilities = optarg;
2455 break;
2456 case 'c':
2457 opts.no_new_privs = 1;
2458 break;
2459 case 'n':
2460 opts.name = optarg;
2461 break;
2462 case 'N':
2463 opts.namespace |= CLONE_NEWNET;
2464 break;
2465 case 'h':
2466 opts.namespace |= CLONE_NEWUTS;
2467 opts.hostname = strdup(optarg);
2468 break;
2469 case 'r':
2470 opts.namespace |= CLONE_NEWNS;
2471 add_path_and_deps(optarg, 1, 0, 0);
2472 break;
2473 case 'w':
2474 opts.namespace |= CLONE_NEWNS;
2475 add_path_and_deps(optarg, 0, 0, 0);
2476 break;
2477 case 'u':
2478 opts.namespace |= CLONE_NEWNS;
2479 add_mount_bind(ubus, 0, -1);
2480 break;
2481 case 'l':
2482 opts.namespace |= CLONE_NEWNS;
2483 add_mount_bind(log, 0, -1);
2484 break;
2485 case 'U':
2486 opts.user = optarg;
2487 break;
2488 case 'G':
2489 opts.group = optarg;
2490 break;
2491 case 'O':
2492 opts.overlaydir = optarg;
2493 break;
2494 case 'T':
2495 opts.tmpoverlaysize = optarg;
2496 break;
2497 case 'E':
2498 opts.require_jail = 1;
2499 break;
2500 case 'y':
2501 opts.console = 1;
2502 break;
2503 case 'J':
2504 opts.ocibundle = strdup(optarg);
2505 break;
2506 case 'i':
2507 opts.immediately = true;
2508 break;
2509 case 'P':
2510 opts.pidfile = strdup(optarg);
2511 break;
2512 }
2513 }
2514
2515 if (opts.namespace && !opts.ocibundle)
2516 opts.namespace |= CLONE_NEWIPC | CLONE_NEWPID;
2517
2518 /* those are filehandlers, so -1 indicates unused */
2519 opts.setns.pid = -1;
2520 opts.setns.net = -1;
2521 opts.setns.ns = -1;
2522 opts.setns.ipc = -1;
2523 opts.setns.uts = -1;
2524 opts.setns.user = -1;
2525 opts.setns.cgroup = -1;
2526 #ifdef CLONE_NEWTIME
2527 opts.setns.time = -1;
2528 #endif
2529
2530 if (opts.capabilities && parseOCIcapabilities_from_file(&opts.capset, opts.capabilities)) {
2531 ERROR("failed to read capabilities from file %s\n", opts.capabilities);
2532 return -1;
2533 }
2534
2535 if (opts.ocibundle) {
2536 char *jsonfile;
2537 int ocires;
2538
2539 asprintf(&jsonfile, "%s/config.json", opts.ocibundle);
2540 ocires = parseOCI(jsonfile);
2541 free(jsonfile);
2542 if (ocires) {
2543 ERROR("parsing of OCI JSON spec has failed: %s (%d)\n", strerror(ocires), ocires);
2544 return ocires;
2545 }
2546 }
2547
2548 if (opts.tmpoverlaysize && strlen(opts.tmpoverlaysize) > 8) {
2549 ERROR("size parameter too long: \"%s\"\n", opts.tmpoverlaysize);
2550 return -1;
2551 }
2552
2553 /* no <binary> param found */
2554 if (!opts.ocibundle && (argc - optind < 1)) {
2555 usage();
2556 return EXIT_FAILURE;
2557 }
2558 if (!(opts.ocibundle||opts.namespace||opts.capabilities||opts.seccomp)) {
2559 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
2560 usage();
2561 return EXIT_FAILURE;
2562 }
2563 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
2564 opts.namespace,
2565 opts.capset.apply,
2566 opts.seccomp != 0 || opts.ociseccomp != 0);
2567
2568 uloop_init();
2569 signals_init();
2570
2571 parent_ctx = ubus_connect(NULL);
2572 ubus_add_uloop(parent_ctx);
2573
2574 if (opts.ocibundle) {
2575 char *objname;
2576 if (asprintf(&objname, "container.%s", opts.name) < 0)
2577 exit(-ENOMEM);
2578
2579 container_object.name = objname;
2580 ret = ubus_add_object(parent_ctx, &container_object);
2581 if (ret) {
2582 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
2583 exit(-1);
2584 }
2585 }
2586
2587 /* deliberately not using 'else' on unrelated conditional branches */
2588 if (!opts.ocibundle) {
2589 /* allocate NULL-terminated array for argv */
2590 opts.jail_argv = calloc(1 + argc - optind, sizeof(char**));
2591 if (!opts.jail_argv)
2592 return EXIT_FAILURE;
2593
2594 for (size_t s = optind; s < argc; s++)
2595 opts.jail_argv[s - optind] = strdup(argv[s]);
2596
2597 if (opts.namespace & CLONE_NEWUSER)
2598 get_jail_user(&opts.pw_uid, &opts.pw_gid, &opts.gr_gid);
2599 }
2600
2601 if (!opts.extroot) {
2602 if (opts.namespace && add_path_and_deps(*opts.jail_argv, 1, -1, 0)) {
2603 ERROR("failed to load dependencies\n");
2604 return -1;
2605 }
2606 }
2607
2608 if (opts.namespace && opts.seccomp && add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
2609 ERROR("failed to load libpreload-seccomp.so\n");
2610 opts.seccomp = 0;
2611 if (opts.require_jail)
2612 return -1;
2613 }
2614
2615 uloop_timeout_add(&post_main_timeout);
2616 uloop_run();
2617
2618 /* unreachable */
2619 return 0;
2620 }
2621
2622 static void post_main(struct uloop_timeout *t)
2623 {
2624 if (apply_rlimits()) {
2625 ERROR("error applying resource limits\n");
2626 exit(EXIT_FAILURE);
2627 }
2628
2629 if (opts.name)
2630 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
2631
2632 if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0)
2633 exit(-1);
2634
2635 if (has_namespaces()) {
2636 if (opts.namespace & CLONE_NEWNS) {
2637 if (!opts.extroot && (opts.user || opts.group)) {
2638 add_mount_bind("/etc/passwd", 1, -1);
2639 add_mount_bind("/etc/group", 1, -1);
2640 }
2641
2642 #if defined(__GLIBC__)
2643 if (!opts.extroot)
2644 add_mount_bind("/etc/nsswitch.conf", 1, -1);
2645 #endif
2646
2647 if (!(opts.namespace & CLONE_NEWNET)) {
2648 add_mount_bind("/etc/resolv.conf", 1, -1);
2649 } else if (opts.setns.net == -1) {
2650 char hostdir[PATH_MAX];
2651
2652 snprintf(hostdir, PATH_MAX, "/tmp/resolv.conf-%s.d", opts.name);
2653 mkdir_p(hostdir, 0755);
2654 add_mount(hostdir, "/dev/resolv.conf.d", NULL, MS_BIND | MS_NOEXEC | MS_NOATIME | MS_NOSUID | MS_NODEV | MS_RDONLY, 0, NULL, -1);
2655 }
2656
2657 /* default mounts */
2658 add_mount(NULL, "/dev", "tmpfs", MS_NOATIME | MS_NOEXEC | MS_NOSUID, 0, "size=1M", -1);
2659 add_mount(NULL, "/dev/pts", "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, 0, "newinstance,ptmxmode=0666,mode=0620,gid=5", 0);
2660
2661 if (opts.procfs || opts.ocibundle) {
2662 add_mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0, NULL, -1);
2663
2664 /*
2665 * hack to make /proc/sys/net read-write while the rest of /proc/sys is read-only
2666 * which cannot be expressed with OCI spec, but happends to be very useful.
2667 * Only apply it if '/proc/sys' is not already listed as mount, maskedPath or
2668 * readonlyPath.
2669 * If not running in a new network namespace, only make /proc/sys read-only.
2670 * If running in a new network namespace, temporarily stash (ie. mount-bind)
2671 * /proc/sys/net into (totally unrelated, but surely existing) /proc/self/net.
2672 * Then we mount-bind /proc/sys read-only and then mount-move /proc/self/net into
2673 * /proc/sys/net.
2674 * This works because mounts are executed in incrementing strcmp() order and
2675 * /proc/self/net appears there before /proc/sys/net and hence the operation
2676 * succeeds as the bind-mount of /proc/self/net is performed first and then
2677 * move-mount of /proc/sys/net follows because 'e' preceeds 'y' in the ASCII
2678 * table (and in the alphabet).
2679 */
2680 if (!add_mount(NULL, "/proc/sys", NULL, MS_BIND | MS_RDONLY, 0, NULL, -1))
2681 if (opts.namespace & CLONE_NEWNET)
2682 if (!add_mount_inner("/proc/self/net", "/proc/sys/net", NULL, MS_MOVE, 0, NULL, -1))
2683 add_mount_inner("/proc/sys/net", "/proc/self/net", NULL, MS_BIND, 0, NULL, -1);
2684
2685 }
2686 if (opts.sysfs || opts.ocibundle)
2687 add_mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, 0, NULL, -1);
2688
2689 if (opts.ocibundle)
2690 add_mount("shm", "/dev/shm", "tmpfs", MS_NOSUID | MS_NOEXEC | MS_NODEV, 0, "mode=1777", -1);
2691
2692 }
2693
2694 if (opts.setns.pid != -1) {
2695 pidns_fd = ns_open_pid("pid", getpid());
2696 setns_open(CLONE_NEWPID);
2697 } else {
2698 pidns_fd = -1;
2699 }
2700
2701 #ifdef CLONE_NEWTIME
2702 if (opts.setns.time != -1) {
2703 timens_fd = ns_open_pid("time", getpid());
2704 setns_open(CLONE_NEWTIME);
2705 }
2706 #endif
2707
2708 jail_process.pid = clone(exec_jail, child_stack + STACK_SIZE, SIGCHLD | (opts.namespace & (~CLONE_NEWCGROUP)), NULL);
2709 } else {
2710 jail_process.pid = fork();
2711 }
2712
2713 if (jail_process.pid > 0) {
2714 /* parent process */
2715 char sig_buf[1];
2716
2717 uloop_process_add(&jail_process);
2718 jail_running = 1;
2719 seteuid(0);
2720 if (pidns_fd != -1) {
2721 setns(pidns_fd, CLONE_NEWPID);
2722 close(pidns_fd);
2723 }
2724 #ifdef CLONE_NEWTIME
2725 if (timens_fd != -1)
2726 setns(timens_fd, CLONE_NEWTIME);
2727 close(timens_fd);
2728 }
2729 #endif
2730 if (opts.setns.net != -1)
2731 close(opts.setns.net);
2732 if (opts.setns.ns != -1)
2733 close(opts.setns.ns);
2734 if (opts.setns.ipc != -1)
2735 close(opts.setns.ipc);
2736 if (opts.setns.uts != -1)
2737 close(opts.setns.uts);
2738 if (opts.setns.user != -1)
2739 close(opts.setns.user);
2740 if (opts.setns.cgroup != -1)
2741 close(opts.setns.cgroup);
2742 close(pipes[1]);
2743 close(pipes[2]);
2744 if (read(pipes[0], sig_buf, 1) < 1) {
2745 ERROR("can't read from child\n");
2746 exit(-1);
2747 }
2748 close(pipes[0]);
2749 set_oom_score_adj();
2750
2751 if (opts.ocibundle)
2752 cgroups_apply(jail_process.pid);
2753
2754 if (opts.namespace & CLONE_NEWUSER) {
2755 if (write_setgroups(jail_process.pid, true)) {
2756 ERROR("can't write setgroups\n");
2757 exit(-1);
2758 }
2759 if (!opts.uidmap) {
2760 bool has_gr = (opts.gr_gid != -1);
2761 if (opts.pw_uid != -1) {
2762 write_single_uid_gid_map(jail_process.pid, 0, opts.pw_uid);
2763 write_single_uid_gid_map(jail_process.pid, 1, has_gr?opts.gr_gid:opts.pw_gid);
2764 } else {
2765 write_single_uid_gid_map(jail_process.pid, 0, 65534);
2766 write_single_uid_gid_map(jail_process.pid, 1, has_gr?opts.gr_gid:65534);
2767 }
2768 } else {
2769 write_uid_gid_map(jail_process.pid, 0, opts.uidmap);
2770 if (opts.gidmap)
2771 write_uid_gid_map(jail_process.pid, 1, opts.gidmap);
2772 }
2773 }
2774
2775 if (opts.namespace & CLONE_NEWNET) {
2776 if (!opts.name) {
2777 ERROR("netns needs a named jail\n");
2778 exit(-1);
2779 }
2780 netns_fd = ns_open_pid("net", jail_process.pid);
2781 netns_updown(jail_process.pid, true);
2782 }
2783 if (jail_writepid(jail_process.pid)) {
2784 ERROR("failed to write pidfile: %m\n");
2785 exit(-1);
2786 }
2787 } else if (jail_process.pid == 0) {
2788 /* fork child process */
2789 exit(exec_jail(NULL));
2790 } else {
2791 ERROR("failed to clone/fork: %m\n");
2792 exit(EXIT_FAILURE);
2793 }
2794 run_hooks(opts.hooks.createRuntime, post_create_runtime);
2795 }
2796
2797 static void post_poststart(void);
2798 static void post_create_runtime(void)
2799 {
2800 char sig_buf[1];
2801
2802 sig_buf[0] = 'O';
2803 if (write(pipes[3], sig_buf, 1) < 0) {
2804 ERROR("can't write to child\n");
2805 exit(-1);
2806 }
2807
2808 jail_oci_state = OCI_STATE_CREATED;
2809 if (opts.ocibundle && !opts.immediately)
2810 uloop_run(); /* wait for 'start' command via ubus */
2811 else
2812 pipe_send_start_container(NULL);
2813 }
2814
2815 static void pipe_send_start_container(struct uloop_timeout *t)
2816 {
2817 char sig_buf[1];
2818
2819 jail_oci_state = OCI_STATE_RUNNING;
2820 sig_buf[0] = '!';
2821 if (write(pipes[3], sig_buf, 1) < 0) {
2822 ERROR("can't write to child\n");
2823 exit(-1);
2824 }
2825 close(pipes[3]);
2826
2827 run_hooks(opts.hooks.poststart, post_poststart);
2828 }
2829
2830 static void post_poststart(void)
2831 {
2832 uloop_run(); /* idle here while jail is running */
2833 if (jail_running) {
2834 DEBUG("uloop interrupted, killing jail process\n");
2835 kill(jail_process.pid, SIGTERM);
2836 uloop_timeout_set(&jail_process_timeout, 1000);
2837 uloop_run();
2838 }
2839 uloop_done();
2840 poststop();
2841 }
2842
2843 static void post_poststop(void);
2844 static void poststop(void) {
2845 if (opts.namespace & CLONE_NEWNET) {
2846 setns(netns_fd, CLONE_NEWNET);
2847 netns_updown(getpid(), false);
2848 close(netns_fd);
2849 }
2850 run_hooks(opts.hooks.poststop, post_poststop);
2851 }
2852
2853 static void post_poststop(void)
2854 {
2855 free_opts(true);
2856 if (parent_ctx)
2857 ubus_free(parent_ctx);
2858
2859 exit(jail_return_code);
2860 }