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