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