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