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