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