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