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