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