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