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