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