jail: refactor mount support to cover OCI spec
[project/procd.git] / jail / jail.c
1 /*
2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #define _GNU_SOURCE
15 #include <sys/mount.h>
16 #include <sys/prctl.h>
17 #include <sys/wait.h>
18 #include <sys/types.h>
19
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <sched.h>
29 #include <linux/limits.h>
30 #include <linux/filter.h>
31 #include <signal.h>
32
33 #include "capabilities.h"
34 #include "elf.h"
35 #include "fs.h"
36 #include "jail.h"
37 #include "log.h"
38 #include "seccomp-oci.h"
39
40 #include <libubox/utils.h>
41 #include <libubox/blobmsg.h>
42 #include <libubox/blobmsg_json.h>
43 #include <libubox/list.h>
44 #include <libubox/vlist.h>
45 #include <libubox/uloop.h>
46 #include <libubus.h>
47
48 #ifndef CLONE_NEWCGROUP
49 #define CLONE_NEWCGROUP 0x02000000
50 #endif
51
52 #define STACK_SIZE (1024 * 1024)
53 #define OPT_ARGS "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:EyJ:"
54
55 struct hook_execvpe {
56 char *file;
57 char **argv;
58 char **envp;
59 int timeout;
60 };
61
62 static struct {
63 char *name;
64 char *hostname;
65 char **jail_argv;
66 char *cwd;
67 char *seccomp;
68 struct sock_fprog *ociseccomp;
69 char *capabilities;
70 struct jail_capset capset;
71 char *user;
72 char *group;
73 char *extroot;
74 char *overlaydir;
75 char *tmpoverlaysize;
76 char **envp;
77 char *uidmap;
78 char *gidmap;
79 int no_new_privs;
80 int namespace;
81 int procfs;
82 int ronly;
83 int sysfs;
84 int console;
85 int pw_uid;
86 int pw_gid;
87 int gr_gid;
88 int require_jail;
89 struct {
90 struct hook_execvpe *createRuntime;
91 struct hook_execvpe *createContainer;
92 struct hook_execvpe *startContainer;
93 struct hook_execvpe *poststart;
94 struct hook_execvpe *poststop;
95 } hooks;
96 } opts;
97
98 static void free_hooklist(struct hook_execvpe *hooklist)
99 {
100 struct hook_execvpe *cur;
101 char **tmp;
102
103 cur = hooklist;
104 while (cur) {
105 free(cur->file);
106 tmp = cur->argv;
107 while (tmp)
108 free(*(tmp++));
109
110 free(cur->argv);
111
112 tmp = cur->envp;
113 while (tmp)
114 free(*(tmp++));
115
116 free(cur->envp);
117 free(cur++);
118 }
119 }
120
121 static void free_opts(bool child) {
122 char **tmp;
123
124 /* we need to keep argv, envp and seccomp filter in child */
125 if (child) {
126 if (opts.ociseccomp) {
127 free(opts.ociseccomp->filter);
128 free(opts.ociseccomp);
129 }
130
131 tmp = opts.jail_argv;
132 while(tmp)
133 free(*(tmp++));
134
135 free(opts.jail_argv);
136
137 tmp = opts.envp;
138 while (tmp)
139 free(*(tmp++));
140
141 free(opts.envp);
142 };
143
144 free(opts.hostname);
145 free(opts.cwd);
146 free(opts.extroot);
147 free(opts.uidmap);
148 free(opts.gidmap);
149 free_hooklist(opts.hooks.createRuntime);
150 free_hooklist(opts.hooks.createContainer);
151 free_hooklist(opts.hooks.startContainer);
152 free_hooklist(opts.hooks.poststart);
153 free_hooklist(opts.hooks.poststop);
154 }
155
156 static struct blob_buf ocibuf;
157
158 extern int pivot_root(const char *new_root, const char *put_old);
159
160 int debug = 0;
161
162 static char child_stack[STACK_SIZE];
163
164 int console_fd;
165
166 static int mount_overlay(char *jail_root, char *overlaydir) {
167 char *upperdir, *workdir, *optsstr, *upperetc, *upperresolvconf;
168 const char mountoptsformat[] = "lowerdir=%s,upperdir=%s,workdir=%s";
169 int ret = -1, fd;
170
171 if (asprintf(&upperdir, "%s%s", overlaydir, "/upper") < 0)
172 goto out;
173
174 if (asprintf(&workdir, "%s%s", overlaydir, "/work") < 0)
175 goto upper_printf;
176
177 if (asprintf(&optsstr, mountoptsformat, jail_root, upperdir, workdir) < 0)
178 goto work_printf;
179
180 if (mkdir_p(upperdir, 0755) || mkdir_p(workdir, 0755))
181 goto opts_printf;
182
183 /*
184 * make sure /etc/resolv.conf exists in overlay and is owned by jail userns root
185 * this is to work-around a bug in overlayfs described in the overlayfs-userns
186 * patch:
187 * 3. modification of a file 'hithere' which is in l but not yet
188 * in u, and which is not owned by T, is not allowed, even if
189 * writes to u are allowed. This may be a bug in overlayfs,
190 * but it is safe behavior.
191 */
192 if (asprintf(&upperetc, "%s/etc", upperdir) < 0)
193 goto opts_printf;
194
195 if (mkdir_p(upperetc, 0755))
196 goto upper_etc_printf;
197
198 if (asprintf(&upperresolvconf, "%s/resolv.conf", upperetc) < 0)
199 goto upper_etc_printf;
200
201 fd = creat(upperresolvconf, 0644);
202 if (fd == -1) {
203 ERROR("creat(%s) failed: %m\n", upperresolvconf);
204 goto upper_resolvconf_printf;
205 }
206 close(fd);
207
208 DEBUG("mount -t overlay %s %s (%s)\n", jail_root, jail_root, optsstr);
209
210 if (mount(jail_root, jail_root, "overlay", MS_NOATIME, optsstr))
211 goto opts_printf;
212
213 ret = 0;
214
215 upper_resolvconf_printf:
216 free(upperresolvconf);
217 upper_etc_printf:
218 free(upperetc);
219 opts_printf:
220 free(optsstr);
221 work_printf:
222 free(workdir);
223 upper_printf:
224 free(upperdir);
225 out:
226 return ret;
227 }
228
229 static void pass_console(int console_fd)
230 {
231 struct ubus_context *ctx = ubus_connect(NULL);
232 static struct blob_buf req;
233 uint32_t id;
234
235 if (!ctx)
236 return;
237
238 blob_buf_init(&req, 0);
239 blobmsg_add_string(&req, "name", opts.name);
240
241 if (ubus_lookup_id(ctx, "container", &id) ||
242 ubus_invoke_fd(ctx, id, "console_set", req.head, NULL, NULL, 3000, console_fd))
243 INFO("ubus request failed\n");
244 else
245 close(console_fd);
246
247 blob_buf_free(&req);
248 ubus_free(ctx);
249 }
250
251 static int create_dev_console(const char *jail_root)
252 {
253 char *console_fname;
254 char dev_console_path[PATH_MAX];
255 int slave_console_fd;
256
257 /* Open UNIX/98 virtual console */
258 console_fd = posix_openpt(O_RDWR | O_NOCTTY);
259 if (console_fd == -1)
260 return -1;
261
262 console_fname = ptsname(console_fd);
263 DEBUG("got console fd %d and PTS client name %s\n", console_fd, console_fname);
264 if (!console_fname)
265 goto no_console;
266
267 grantpt(console_fd);
268 unlockpt(console_fd);
269
270 /* pass PTY master to procd */
271 pass_console(console_fd);
272
273 /* mount-bind PTY slave to /dev/console in jail */
274 snprintf(dev_console_path, sizeof(dev_console_path), "%s/dev/console", jail_root);
275 close(creat(dev_console_path, 0620));
276
277 if (mount(console_fname, dev_console_path, NULL, MS_BIND, NULL))
278 goto no_console;
279
280 /* use PTY slave for stdio */
281 slave_console_fd = open(console_fname, O_RDWR); /* | O_NOCTTY */
282 dup2(slave_console_fd, 0);
283 dup2(slave_console_fd, 1);
284 dup2(slave_console_fd, 2);
285 close(slave_console_fd);
286
287 INFO("using guest console %s\n", console_fname);
288
289 return 0;
290
291 no_console:
292 close(console_fd);
293 return 1;
294 }
295
296 static int hook_running = 0;
297 static int hook_return_code = 0;
298
299 static void hook_process_timeout_cb(struct uloop_timeout *t);
300 static struct uloop_timeout hook_process_timeout = {
301 .cb = hook_process_timeout_cb,
302 };
303
304 static void hook_process_handler(struct uloop_process *c, int ret)
305 {
306 uloop_timeout_cancel(&hook_process_timeout);
307 if (WIFEXITED(ret)) {
308 hook_return_code = WEXITSTATUS(ret);
309 INFO("hook (%d) exited with exit: %d\n", c->pid, hook_return_code);
310 } else {
311 hook_return_code = WTERMSIG(ret);
312 INFO("hook (%d) exited with signal: %d\n", c->pid, hook_return_code);
313 }
314 hook_running = 0;
315 uloop_end();
316 }
317
318 static struct uloop_process hook_process = {
319 .cb = hook_process_handler,
320 };
321
322 static void hook_process_timeout_cb(struct uloop_timeout *t)
323 {
324 DEBUG("hook process failed to stop, sending SIGKILL\n");
325 kill(hook_process.pid, SIGKILL);
326 }
327
328 static void hook_handle_signal(int signo)
329 {
330 DEBUG("forwarding signal %d to the jailed process\n", signo);
331 kill(hook_process.pid, signo);
332 }
333
334 static int run_hook(struct hook_execvpe *hook)
335 {
336 sigset_t sigmask;
337 int i;
338
339 DEBUG("executing hook %s\n", hook->file);
340 uloop_init();
341 sigfillset(&sigmask);
342 for (i = 0; i < _NSIG; i++) {
343 struct sigaction s = { 0 };
344
345 if (!sigismember(&sigmask, i))
346 continue;
347 if ((i == SIGCHLD) || (i == SIGPIPE) || (i == SIGSEGV))
348 continue;
349
350 s.sa_handler = hook_handle_signal;
351 sigaction(i, &s, NULL);
352 }
353 hook_running = 1;
354 hook_process.pid = fork();
355 if (hook_process.pid > 0) {
356 /* parent */
357 uloop_process_add(&hook_process);
358 if (hook->timeout > 0)
359 uloop_timeout_set(&hook_process_timeout, 1000 * hook->timeout);
360
361 uloop_run();
362 if (hook_running) {
363 DEBUG("uloop interrupted, killing hook process\n");
364 kill(hook_process.pid, SIGTERM);
365 uloop_timeout_set(&hook_process_timeout, 1000);
366 uloop_run();
367 }
368 uloop_done();
369
370 return 0;
371 } else if (hook_process.pid == 0) {
372 /* child */
373 execvpe(hook->file, hook->argv, hook->envp);
374 return errno;
375 } else {
376 /* fork error */
377 return errno;
378 }
379 }
380
381 static int run_hooks(struct hook_execvpe *hook)
382 {
383 struct hook_execvpe *cur;
384 int res;
385
386 cur = hook;
387 while (cur) {
388 res = run_hook(cur++);
389 if (res)
390 return res;
391 }
392
393 return 0;
394 }
395
396 static int build_jail_fs(void)
397 {
398 char jail_root[] = "/tmp/ujail-XXXXXX";
399 char tmpovdir[] = "/tmp/ujail-overlay-XXXXXX";
400 char tmpdevdir[] = "/tmp/ujail-XXXXXX/dev";
401 char tmpdevptsdir[] = "/tmp/ujail-XXXXXX/dev/pts";
402 char *overlaydir = NULL;
403
404 if (mkdtemp(jail_root) == NULL) {
405 ERROR("mkdtemp(%s) failed: %m\n", jail_root);
406 return -1;
407 }
408
409 /* oldroot can't be MS_SHARED else pivot_root() fails */
410 if (mount("none", "/", NULL, MS_REC|MS_PRIVATE, NULL)) {
411 ERROR("private mount failed %m\n");
412 return -1;
413 }
414
415 if (opts.extroot) {
416 if (mount(opts.extroot, jail_root, NULL, MS_BIND, NULL)) {
417 ERROR("extroot mount failed %m\n");
418 return -1;
419 }
420 } else {
421 if (mount("tmpfs", jail_root, "tmpfs", MS_NOATIME, "mode=0755")) {
422 ERROR("tmpfs mount failed %m\n");
423 return -1;
424 }
425 }
426
427 if (opts.tmpoverlaysize) {
428 char mountoptsstr[] = "mode=0755,size=XXXXXXXX";
429
430 snprintf(mountoptsstr, sizeof(mountoptsstr),
431 "mode=0755,size=%s", opts.tmpoverlaysize);
432 if (mkdtemp(tmpovdir) == NULL) {
433 ERROR("mkdtemp(%s) failed: %m\n", jail_root);
434 return -1;
435 }
436 if (mount("tmpfs", tmpovdir, "tmpfs", MS_NOATIME,
437 mountoptsstr)) {
438 ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts.tmpoverlaysize);
439 return -1;
440 }
441 overlaydir = tmpovdir;
442 }
443
444 if (opts.overlaydir)
445 overlaydir = opts.overlaydir;
446
447 if (overlaydir)
448 mount_overlay(jail_root, overlaydir);
449
450 if (chdir(jail_root)) {
451 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root);
452 return -1;
453 }
454
455 snprintf(tmpdevdir, sizeof(tmpdevdir), "%s/dev", jail_root);
456 mkdir_p(tmpdevdir, 0755);
457 if (mount(NULL, tmpdevdir, "tmpfs", MS_NOATIME | MS_NOEXEC | MS_NOSUID, "size=1M"))
458 return -1;
459
460 snprintf(tmpdevptsdir, sizeof(tmpdevptsdir), "%s/dev/pts", jail_root);
461 mkdir_p(tmpdevptsdir, 0755);
462 if (mount(NULL, tmpdevptsdir, "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, NULL))
463 return -1;
464
465 if (opts.console)
466 create_dev_console(jail_root);
467
468 if (mount_all(jail_root)) {
469 ERROR("mount_all() failed\n");
470 return -1;
471 }
472
473 /* make sure /etc/resolv.conf exists if in new network namespace */
474 if (opts.namespace & CLONE_NEWNET) {
475 char jailetc[PATH_MAX], jaillink[PATH_MAX];
476
477 snprintf(jailetc, PATH_MAX, "%s/etc", jail_root);
478 mkdir_p(jailetc, 0755);
479 snprintf(jaillink, PATH_MAX, "%s/etc/resolv.conf", jail_root);
480 if (overlaydir)
481 unlink(jaillink);
482
483 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink);
484 }
485
486 run_hooks(opts.hooks.createContainer);
487
488 char dirbuf[sizeof(jail_root) + 4];
489 snprintf(dirbuf, sizeof(dirbuf), "%s/old", jail_root);
490 mkdir(dirbuf, 0755);
491
492 if (pivot_root(jail_root, dirbuf) == -1) {
493 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root, dirbuf);
494 return -1;
495 }
496 if (chdir("/")) {
497 ERROR("chdir(/) (after pivot_root) failed: %m\n");
498 return -1;
499 }
500
501 snprintf(dirbuf, sizeof(dirbuf), "/old%s", jail_root);
502 umount2(dirbuf, MNT_DETACH);
503 rmdir(dirbuf);
504 if (opts.tmpoverlaysize) {
505 char tmpdirbuf[sizeof(tmpovdir) + 4];
506 snprintf(tmpdirbuf, sizeof(tmpdirbuf), "/old%s", tmpovdir);
507 umount2(tmpdirbuf, MNT_DETACH);
508 rmdir(tmpdirbuf);
509 }
510
511 umount2("/old", MNT_DETACH);
512 rmdir("/old");
513
514 if (opts.procfs) {
515 mkdir("/proc", 0755);
516 mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
517 /*
518 * make /proc/sys read-only while keeping read-write to
519 * /proc/sys/net if CLONE_NEWNET is set.
520 */
521 if (opts.namespace & CLONE_NEWNET)
522 mount("/proc/sys/net", "/proc/self/net", NULL, MS_BIND, 0);
523
524 mount("/proc/sys", "/proc/sys", NULL, MS_BIND, 0);
525 mount(NULL, "/proc/sys", NULL, MS_REMOUNT | MS_RDONLY, 0);
526 mount(NULL, "/proc", NULL, MS_REMOUNT, 0);
527
528 if (opts.namespace & CLONE_NEWNET)
529 mount("/proc/self/net", "/proc/sys/net", NULL, MS_MOVE, 0);
530 }
531 if (opts.sysfs) {
532 mkdir("/sys", 0755);
533 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, 0);
534 }
535 if (opts.ronly)
536 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
537
538 return 0;
539 }
540
541 static int write_uid_gid_map(pid_t child_pid, bool gidmap, char *mapstr)
542 {
543 int map_file;
544 char map_path[64];
545
546 if (snprintf(map_path, sizeof(map_path), "/proc/%d/%s",
547 child_pid, gidmap?"gid_map":"uid_map") < 0)
548 return -1;
549
550 if ((map_file = open(map_path, O_WRONLY)) == -1)
551 return -1;
552
553 if (dprintf(map_file, "%s", mapstr)) {
554 close(map_file);
555 return -1;
556 }
557
558 close(map_file);
559 free(mapstr);
560 return 0;
561 }
562
563 static int write_single_uid_gid_map(pid_t child_pid, bool gidmap, int id)
564 {
565 int map_file;
566 char map_path[64];
567 const char *map_format = "%d %d %d\n";
568 if (snprintf(map_path, sizeof(map_path), "/proc/%d/%s",
569 child_pid, gidmap?"gid_map":"uid_map") < 0)
570 return -1;
571
572 if ((map_file = open(map_path, O_WRONLY)) == -1)
573 return -1;
574
575 if (dprintf(map_file, map_format, 0, id, 1) == -1) {
576 close(map_file);
577 return -1;
578 }
579
580 close(map_file);
581 return 0;
582 }
583
584 static int write_setgroups(pid_t child_pid, bool allow)
585 {
586 int setgroups_file;
587 char setgroups_path[64];
588
589 if (snprintf(setgroups_path, sizeof(setgroups_path), "/proc/%d/setgroups",
590 child_pid) < 0) {
591 return -1;
592 }
593
594 if ((setgroups_file = open(setgroups_path, O_WRONLY)) == -1) {
595 return -1;
596 }
597
598 if (dprintf(setgroups_file, "%s", allow?"allow":"deny") == -1) {
599 close(setgroups_file);
600 return -1;
601 }
602
603 close(setgroups_file);
604 return 0;
605 }
606
607 static void get_jail_user(int *user, int *user_gid, int *gr_gid)
608 {
609 struct passwd *p = NULL;
610 struct group *g = NULL;
611
612 if (opts.user) {
613 p = getpwnam(opts.user);
614 if (!p) {
615 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
616 opts.user, errno, strerror(errno));
617 exit(EXIT_FAILURE);
618 }
619 *user = p->pw_uid;
620 *user_gid = p->pw_gid;
621 } else {
622 *user = -1;
623 *user_gid = -1;
624 }
625
626 if (opts.group) {
627 g = getgrnam(opts.group);
628 if (!g) {
629 ERROR("failed to get gid for group %s: %m\n", opts.group);
630 exit(EXIT_FAILURE);
631 }
632 *gr_gid = g->gr_gid;
633 } else {
634 *gr_gid = -1;
635 }
636 };
637
638 static void set_jail_user(int pw_uid, int user_gid, int gr_gid)
639 {
640 if (opts.user && (user_gid != -1) && initgroups(opts.user, user_gid)) {
641 ERROR("failed to initgroups() for user %s: %m\n", opts.user);
642 exit(EXIT_FAILURE);
643 }
644
645 if ((gr_gid != -1) && setregid(gr_gid, gr_gid)) {
646 ERROR("failed to set group id %d: %m\n", gr_gid);
647 exit(EXIT_FAILURE);
648 }
649
650 if ((pw_uid != -1) && setreuid(pw_uid, pw_uid)) {
651 ERROR("failed to set user id %d: %m\n", pw_uid);
652 exit(EXIT_FAILURE);
653 }
654 }
655
656 #define MAX_ENVP 8
657 static char** build_envp(const char *seccomp, char **ocienvp)
658 {
659 static char *envp[MAX_ENVP];
660 static char preload_var[PATH_MAX];
661 static char seccomp_var[PATH_MAX];
662 static char debug_var[] = "LD_DEBUG=all";
663 static char container_var[] = "container=ujail";
664 const char *preload_lib = find_lib("libpreload-seccomp.so");
665 char **addenv;
666
667 int count = 0;
668
669 if (seccomp && !preload_lib) {
670 ERROR("failed to add preload-lib to env\n");
671 return NULL;
672 }
673 if (seccomp) {
674 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
675 envp[count++] = seccomp_var;
676 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
677 envp[count++] = preload_var;
678 }
679
680 envp[count++] = container_var;
681
682 if (debug > 1)
683 envp[count++] = debug_var;
684
685 addenv = ocienvp;
686 while (addenv && *addenv) {
687 envp[count++] = *(addenv++);
688 if (count >= MAX_ENVP) {
689 ERROR("environment limited to %d extra records, truncating\n", MAX_ENVP);
690 break;
691 }
692 }
693 return envp;
694 }
695
696 static void usage(void)
697 {
698 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
699 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
700 fprintf(stderr, " -S <file>\tseccomp filter config\n");
701 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
702 fprintf(stderr, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
703 fprintf(stderr, " -n <name>\tthe name of the jail\n");
704 fprintf(stderr, "namespace jail options:\n");
705 fprintf(stderr, " -h <hostname>\tchange the hostname of the jail\n");
706 fprintf(stderr, " -N\t\tjail has network namespace\n");
707 fprintf(stderr, " -f\t\tjail has user namespace\n");
708 fprintf(stderr, " -F\t\tjail has cgroups namespace\n");
709 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
710 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
711 fprintf(stderr, " -p\t\tjail has /proc\n");
712 fprintf(stderr, " -s\t\tjail has /sys\n");
713 fprintf(stderr, " -l\t\tjail has /dev/log\n");
714 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
715 fprintf(stderr, " -U <name>\tuser to run jailed process\n");
716 fprintf(stderr, " -G <name>\tgroup to run jailed process\n");
717 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
718 fprintf(stderr, " -R <dir>\texternal jail rootfs (system container)\n");
719 fprintf(stderr, " -O <dir>\tdirectory for r/w overlayfs\n");
720 fprintf(stderr, " -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
721 fprintf(stderr, " -E\t\tfail if jail cannot be setup\n");
722 fprintf(stderr, " -y\t\tprovide jail console\n");
723 fprintf(stderr, " -J <dir>\tstart OCI bundle\n");
724 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
725 and he has the same powers as root outside the jail,\n\
726 thus he can escape the jail and/or break stuff.\n\
727 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
728 If you use none of the namespace jail options,\n\
729 ujail will not use namespace/build a jail,\n\
730 and will only drop capabilities/apply seccomp filter.\n\n");
731 }
732
733 static int exec_jail(void *pipes_ptr)
734 {
735 int *pipes = (int*)pipes_ptr;
736 char buf[1];
737 int pw_uid, pw_gid, gr_gid;
738
739 close(pipes[0]);
740 close(pipes[3]);
741
742 buf[0] = 'i';
743 if (write(pipes[1], buf, 1) < 1) {
744 ERROR("can't write to parent\n");
745 exit(EXIT_FAILURE);
746 }
747 if (read(pipes[2], buf, 1) < 1) {
748 ERROR("can't read from parent\n");
749 exit(EXIT_FAILURE);
750 }
751 if (buf[0] != 'O') {
752 ERROR("parent had an error, child exiting\n");
753 exit(EXIT_FAILURE);
754 }
755
756 close(pipes[1]);
757 close(pipes[2]);
758
759 if (opts.namespace & CLONE_NEWUSER) {
760 if (setregid(0, 0) < 0) {
761 ERROR("setgid\n");
762 exit(EXIT_FAILURE);
763 }
764 if (setreuid(0, 0) < 0) {
765 ERROR("setuid\n");
766 exit(EXIT_FAILURE);
767 }
768 if (setgroups(0, NULL) < 0) {
769 ERROR("setgroups\n");
770 exit(EXIT_FAILURE);
771 }
772 }
773
774 if (opts.namespace && opts.hostname && strlen(opts.hostname) > 0
775 && sethostname(opts.hostname, strlen(opts.hostname))) {
776 ERROR("sethostname(%s) failed: %m\n", opts.hostname);
777 exit(EXIT_FAILURE);
778 }
779
780 if ((opts.namespace & CLONE_NEWNS) && build_jail_fs()) {
781 ERROR("failed to build jail fs\n");
782 exit(EXIT_FAILURE);
783 }
784
785 run_hooks(opts.hooks.startContainer);
786
787 if (applyOCIcapabilities(opts.capset))
788 exit(EXIT_FAILURE);
789
790 if (opts.capabilities && drop_capabilities(opts.capabilities))
791 exit(EXIT_FAILURE);
792
793 if (opts.no_new_privs && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
794 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
795 exit(EXIT_FAILURE);
796 }
797
798 if (!(opts.namespace & CLONE_NEWUSER)) {
799 get_jail_user(&pw_uid, &pw_gid, &gr_gid);
800
801 set_jail_user(opts.pw_uid?:pw_uid, opts.pw_gid?:pw_gid, opts.gr_gid?:gr_gid);
802 }
803
804 char **envp = build_envp(opts.seccomp, opts.envp);
805 if (!envp)
806 exit(EXIT_FAILURE);
807
808 if (opts.cwd && chdir(opts.cwd))
809 exit(EXIT_FAILURE);
810
811 if (opts.ociseccomp && applyOCIlinuxseccomp(opts.ociseccomp))
812 exit(EXIT_FAILURE);
813
814 free_opts(false);
815 INFO("exec-ing %s\n", *opts.jail_argv);
816 if (opts.envp) /* respect PATH if potentially set in ENV */
817 execvpe(*opts.jail_argv, opts.jail_argv, envp);
818 else
819 execve(*opts.jail_argv, opts.jail_argv, envp);
820
821 /* we get there only if execve fails */
822 ERROR("failed to execve %s: %m\n", *opts.jail_argv);
823 exit(EXIT_FAILURE);
824 }
825
826 static int jail_running = 1;
827 static int jail_return_code = 0;
828
829 static void jail_process_timeout_cb(struct uloop_timeout *t);
830 static struct uloop_timeout jail_process_timeout = {
831 .cb = jail_process_timeout_cb,
832 };
833
834 static void jail_process_handler(struct uloop_process *c, int ret)
835 {
836 uloop_timeout_cancel(&jail_process_timeout);
837 if (WIFEXITED(ret)) {
838 jail_return_code = WEXITSTATUS(ret);
839 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
840 } else {
841 jail_return_code = WTERMSIG(ret);
842 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
843 }
844 jail_running = 0;
845 uloop_end();
846 }
847
848 static struct uloop_process jail_process = {
849 .cb = jail_process_handler,
850 };
851
852 static void jail_process_timeout_cb(struct uloop_timeout *t)
853 {
854 DEBUG("jail process failed to stop, sending SIGKILL\n");
855 kill(jail_process.pid, SIGKILL);
856 }
857
858 static void jail_handle_signal(int signo)
859 {
860 DEBUG("forwarding signal %d to the jailed process\n", signo);
861 kill(jail_process.pid, signo);
862 }
863
864 static int netns_open_pid(const pid_t target_ns)
865 {
866 char pid_net_path[PATH_MAX];
867
868 snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%u/ns/net", target_ns);
869
870 return open(pid_net_path, O_RDONLY);
871 }
872
873 static void netns_updown(pid_t pid, bool start)
874 {
875 struct ubus_context *ctx = ubus_connect(NULL);
876 static struct blob_buf req;
877 uint32_t id;
878
879 if (!ctx)
880 return;
881
882 blob_buf_init(&req, 0);
883 blobmsg_add_string(&req, "jail", opts.name);
884 blobmsg_add_u32(&req, "pid", pid);
885 blobmsg_add_u8(&req, "start", start);
886
887 if (ubus_lookup_id(ctx, "network", &id) ||
888 ubus_invoke(ctx, id, "netns_updown", req.head, NULL, NULL, 3000))
889 INFO("ubus request failed\n");
890
891 blob_buf_free(&req);
892 ubus_free(ctx);
893 }
894
895 static int parseOCIenvarray(struct blob_attr *msg, char ***envp)
896 {
897 struct blob_attr *cur;
898 int sz = 0, rem;
899
900 blobmsg_for_each_attr(cur, msg, rem)
901 ++sz;
902
903 if (sz > 0) {
904 *envp = calloc(1 + sz, sizeof(char*));
905 if (!(*envp))
906 return ENOMEM;
907 } else {
908 *envp = NULL;
909 return 0;
910 }
911
912 sz = 0;
913 blobmsg_for_each_attr(cur, msg, rem)
914 (*envp)[sz++] = strdup(blobmsg_get_string(cur));
915
916 if (sz)
917 (*envp)[sz] = NULL;
918
919 return 0;
920 }
921
922 enum {
923 OCI_ROOT_PATH,
924 OCI_ROOT_READONLY,
925 __OCI_ROOT_MAX,
926 };
927
928 static const struct blobmsg_policy oci_root_policy[] = {
929 [OCI_ROOT_PATH] = { "path", BLOBMSG_TYPE_STRING },
930 [OCI_ROOT_READONLY] = { "readonly", BLOBMSG_TYPE_BOOL },
931 };
932
933 static int parseOCIroot(const char *jsonfile, struct blob_attr *msg)
934 {
935 static char rootpath[PATH_MAX] = { 0 };
936 struct blob_attr *tb[__OCI_ROOT_MAX];
937 char *cur;
938
939 blobmsg_parse(oci_root_policy, __OCI_ROOT_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
940
941 if (!tb[OCI_ROOT_PATH])
942 return ENODATA;
943
944 strncpy(rootpath, jsonfile, PATH_MAX);
945 cur = strrchr(rootpath, '/');
946
947 if (!cur)
948 return ENOTDIR;
949
950 *(++cur) = '\0';
951 strncat(rootpath, blobmsg_get_string(tb[OCI_ROOT_PATH]), PATH_MAX - (strlen(rootpath) + 1));
952
953 opts.extroot = rootpath;
954
955 opts.ronly = blobmsg_get_bool(tb[OCI_ROOT_READONLY]);
956
957 return 0;
958 }
959
960
961 enum {
962 OCI_HOOK_PATH,
963 OCI_HOOK_ARGS,
964 OCI_HOOK_ENV,
965 OCI_HOOK_TIMEOUT,
966 __OCI_HOOK_MAX,
967 };
968
969 static const struct blobmsg_policy oci_hook_policy[] = {
970 [OCI_HOOK_PATH] = { "path", BLOBMSG_TYPE_STRING },
971 [OCI_HOOK_ARGS] = { "args", BLOBMSG_TYPE_ARRAY },
972 [OCI_HOOK_ENV] = { "env", BLOBMSG_TYPE_ARRAY },
973 [OCI_HOOK_TIMEOUT] = { "timeout", BLOBMSG_TYPE_INT32 },
974 };
975
976
977 static int parseOCIhook(struct hook_execvpe **hooklist, struct blob_attr *msg)
978 {
979 struct blob_attr *tb[__OCI_HOOK_MAX];
980 struct blob_attr *cur;
981 int rem, ret = 0;
982 int idx = 0;
983
984 blobmsg_for_each_attr(cur, msg, rem)
985 ++idx;
986
987 if (!idx)
988 goto errout;
989
990 *hooklist = calloc(idx + 1, sizeof(struct hook_execvpe *));
991 idx = 0;
992
993 blobmsg_for_each_attr(cur, msg, rem) {
994 blobmsg_parse(oci_hook_policy, __OCI_HOOK_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
995
996 if (!tb[OCI_HOOK_PATH]) {
997 ret = EINVAL;
998 goto errout;
999 }
1000
1001 hooklist[idx] = malloc(sizeof(struct hook_execvpe));
1002 if (tb[OCI_HOOK_ARGS]) {
1003 ret = parseOCIenvarray(tb[OCI_HOOK_ARGS], &(hooklist[idx]->argv));
1004 if (ret)
1005 goto errout;
1006 }
1007
1008 if (tb[OCI_HOOK_ENV]) {
1009 ret = parseOCIenvarray(tb[OCI_HOOK_ENV], &(hooklist[idx]->envp));
1010 if (ret)
1011 goto errout;
1012 }
1013
1014 if (tb[OCI_HOOK_TIMEOUT])
1015 hooklist[idx]->timeout = blobmsg_get_u32(tb[OCI_HOOK_TIMEOUT]);
1016
1017 hooklist[idx]->file = strdup(blobmsg_get_string(tb[OCI_HOOK_PATH]));
1018
1019 ++idx;
1020 }
1021
1022 hooklist[idx] = NULL;
1023 return 0;
1024
1025 errout:
1026 free_hooklist(*hooklist);
1027 *hooklist = NULL;
1028
1029 return ret;
1030 };
1031
1032
1033 enum {
1034 OCI_HOOKS_PRESTART,
1035 OCI_HOOKS_CREATERUNTIME,
1036 OCI_HOOKS_CREATECONTAINER,
1037 OCI_HOOKS_STARTCONTAINER,
1038 OCI_HOOKS_POSTSTART,
1039 OCI_HOOKS_POSTSTOP,
1040 __OCI_HOOKS_MAX,
1041 };
1042
1043 static const struct blobmsg_policy oci_hooks_policy[] = {
1044 [OCI_HOOKS_PRESTART] = { "prestart", BLOBMSG_TYPE_ARRAY },
1045 [OCI_HOOKS_CREATERUNTIME] = { "createRuntime", BLOBMSG_TYPE_ARRAY },
1046 [OCI_HOOKS_CREATECONTAINER] = { "createContainer", BLOBMSG_TYPE_ARRAY },
1047 [OCI_HOOKS_STARTCONTAINER] = { "startContainer", BLOBMSG_TYPE_ARRAY },
1048 [OCI_HOOKS_POSTSTART] = { "poststart", BLOBMSG_TYPE_ARRAY },
1049 [OCI_HOOKS_POSTSTOP] = { "poststop", BLOBMSG_TYPE_ARRAY },
1050 };
1051
1052 static int parseOCIhooks(struct blob_attr *msg)
1053 {
1054 struct blob_attr *tb[__OCI_HOOKS_MAX];
1055 int ret;
1056
1057 blobmsg_parse(oci_hooks_policy, __OCI_HOOKS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1058
1059 if (tb[OCI_HOOKS_PRESTART])
1060 INFO("warning: ignoring deprecated prestart hook");
1061
1062 if (tb[OCI_HOOKS_CREATERUNTIME]) {
1063 ret = parseOCIhook(&opts.hooks.createRuntime, tb[OCI_HOOKS_CREATERUNTIME]);
1064 if (ret)
1065 return ret;
1066 }
1067
1068 if (tb[OCI_HOOKS_CREATECONTAINER]) {
1069 ret = parseOCIhook(&opts.hooks.createContainer, tb[OCI_HOOKS_CREATECONTAINER]);
1070 if (ret)
1071 goto out_createruntime;
1072 }
1073
1074 if (tb[OCI_HOOKS_STARTCONTAINER]) {
1075 ret = parseOCIhook(&opts.hooks.startContainer, tb[OCI_HOOKS_STARTCONTAINER]);
1076 if (ret)
1077 goto out_createcontainer;
1078 }
1079
1080 if (tb[OCI_HOOKS_POSTSTART]) {
1081 ret = parseOCIhook(&opts.hooks.poststart, tb[OCI_HOOKS_POSTSTART]);
1082 if (ret)
1083 goto out_startcontainer;
1084 }
1085
1086 if (tb[OCI_HOOKS_POSTSTOP]) {
1087 ret = parseOCIhook(&opts.hooks.poststop, tb[OCI_HOOKS_POSTSTOP]);
1088 if (ret)
1089 goto out_poststart;
1090 }
1091
1092 return 0;
1093
1094 out_poststart:
1095 free_hooklist(opts.hooks.poststart);
1096 out_startcontainer:
1097 free_hooklist(opts.hooks.startContainer);
1098 out_createcontainer:
1099 free_hooklist(opts.hooks.createContainer);
1100 out_createruntime:
1101 free_hooklist(opts.hooks.createRuntime);
1102
1103 return ret;
1104 };
1105
1106
1107 enum {
1108 OCI_PROCESS_USER_UID,
1109 OCI_PROCESS_USER_GID,
1110 OCI_PROCESS_USER_UMASK,
1111 OCI_PROCESS_USER_ADDITIONALGIDS,
1112 __OCI_PROCESS_USER_MAX,
1113 };
1114
1115 static const struct blobmsg_policy oci_process_user_policy[] = {
1116 [OCI_PROCESS_USER_UID] = { "uid", BLOBMSG_TYPE_INT32 },
1117 [OCI_PROCESS_USER_GID] = { "gid", BLOBMSG_TYPE_INT32 },
1118 [OCI_PROCESS_USER_UMASK] = { "umask", BLOBMSG_TYPE_INT32 },
1119 [OCI_PROCESS_USER_ADDITIONALGIDS] = { "additionalGids", BLOBMSG_TYPE_ARRAY },
1120 };
1121
1122 static int parseOCIprocessuser(struct blob_attr *msg) {
1123 struct blob_attr *tb[__OCI_PROCESS_USER_MAX];
1124
1125 blobmsg_parse(oci_process_user_policy, __OCI_PROCESS_USER_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1126
1127 if (tb[OCI_PROCESS_USER_UID])
1128 opts.pw_uid = blobmsg_get_u32(tb[OCI_PROCESS_USER_UID]);
1129
1130 if (tb[OCI_PROCESS_USER_GID]) {
1131 opts.pw_gid = blobmsg_get_u32(tb[OCI_PROCESS_USER_GID]);
1132 opts.gr_gid = blobmsg_get_u32(tb[OCI_PROCESS_USER_GID]);
1133 }
1134
1135 /* ToDo: umask, additional GIDs */
1136
1137 return 0;
1138 }
1139
1140 enum {
1141 OCI_PROCESS_ARGS,
1142 OCI_PROCESS_CAPABILITIES,
1143 OCI_PROCESS_CWD,
1144 OCI_PROCESS_ENV,
1145 OCI_PROCESS_NONEWPRIVILEGES,
1146 OCI_PROCESS_RLIMITS,
1147 OCI_PROCESS_TERMINAL,
1148 OCI_PROCESS_USER,
1149 __OCI_PROCESS_MAX,
1150 };
1151
1152 static const struct blobmsg_policy oci_process_policy[] = {
1153 [OCI_PROCESS_ARGS] = { "args", BLOBMSG_TYPE_ARRAY },
1154 [OCI_PROCESS_CAPABILITIES] = { "capabilities", BLOBMSG_TYPE_TABLE },
1155 [OCI_PROCESS_CWD] = { "cwd", BLOBMSG_TYPE_STRING },
1156 [OCI_PROCESS_ENV] = { "env", BLOBMSG_TYPE_ARRAY },
1157 [OCI_PROCESS_NONEWPRIVILEGES] = { "noNewPrivileges", BLOBMSG_TYPE_BOOL },
1158 [OCI_PROCESS_RLIMITS] = { "rlimits", BLOBMSG_TYPE_ARRAY },
1159 [OCI_PROCESS_TERMINAL] = { "terminal", BLOBMSG_TYPE_BOOL },
1160 [OCI_PROCESS_USER] = { "user", BLOBMSG_TYPE_TABLE },
1161 };
1162
1163
1164 static int parseOCIprocess(struct blob_attr *msg)
1165 {
1166 struct blob_attr *tb[__OCI_PROCESS_MAX];
1167 int res;
1168
1169 blobmsg_parse(oci_process_policy, __OCI_PROCESS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1170
1171 if (!tb[OCI_PROCESS_ARGS])
1172 return ENOENT;
1173
1174 res = parseOCIenvarray(tb[OCI_PROCESS_ARGS], &opts.jail_argv);
1175 if (res)
1176 return res;
1177
1178 opts.console = blobmsg_get_bool(tb[OCI_PROCESS_TERMINAL]);
1179 opts.no_new_privs = blobmsg_get_bool(tb[OCI_PROCESS_NONEWPRIVILEGES]);
1180
1181 if (tb[OCI_PROCESS_CWD])
1182 opts.cwd = strdup(blobmsg_get_string(tb[OCI_PROCESS_CWD]));
1183
1184 if (tb[OCI_PROCESS_ENV]) {
1185 res = parseOCIenvarray(tb[OCI_PROCESS_ENV], &opts.envp);
1186 if (res)
1187 return res;
1188 }
1189
1190 if (tb[OCI_PROCESS_USER] && (res = parseOCIprocessuser(tb[OCI_PROCESS_USER])))
1191 return res;
1192
1193 if (tb[OCI_PROCESS_CAPABILITIES] &&
1194 (res = parseOCIcapabilities(&opts.capset, tb[OCI_PROCESS_CAPABILITIES])))
1195 return res;
1196
1197 /* ToDo: rlimits, capabilities */
1198
1199 return 0;
1200 }
1201
1202 enum {
1203 OCI_LINUX_NAMESPACE_TYPE,
1204 OCI_LINUX_NAMESPACE_PATH,
1205 __OCI_LINUX_NAMESPACE_MAX,
1206 };
1207
1208 static const struct blobmsg_policy oci_linux_namespace_policy[] = {
1209 [OCI_LINUX_NAMESPACE_TYPE] = { "type", BLOBMSG_TYPE_STRING },
1210 [OCI_LINUX_NAMESPACE_PATH] = { "path", BLOBMSG_TYPE_STRING },
1211 };
1212
1213 static unsigned int resolve_nstype(char *type) {
1214 if (!strcmp("pid", type))
1215 return CLONE_NEWPID;
1216 else if (!strcmp("network", type))
1217 return CLONE_NEWNET;
1218 else if (!strcmp("mount", type))
1219 return CLONE_NEWNS;
1220 else if (!strcmp("ipc", type))
1221 return CLONE_NEWIPC;
1222 else if (!strcmp("uts", type))
1223 return CLONE_NEWUTS;
1224 else if (!strcmp("user", type))
1225 return CLONE_NEWUSER;
1226 else if (!strcmp("cgroup", type))
1227 return CLONE_NEWCGROUP;
1228 else
1229 return 0;
1230 }
1231
1232 static int parseOCIlinuxns(struct blob_attr *msg)
1233 {
1234 struct blob_attr *tb[__OCI_LINUX_NAMESPACE_MAX];
1235
1236
1237 blobmsg_parse(oci_linux_namespace_policy, __OCI_LINUX_NAMESPACE_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1238
1239 if (!tb[OCI_LINUX_NAMESPACE_TYPE])
1240 return EINVAL;
1241
1242 if (tb[OCI_LINUX_NAMESPACE_PATH])
1243 return ENOTSUP; /* ToDo */
1244
1245 opts.namespace |= resolve_nstype(blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]));
1246
1247 return 0;
1248 };
1249
1250
1251 enum {
1252 OCI_LINUX_UIDGIDMAP_CONTAINERID,
1253 OCI_LINUX_UIDGIDMAP_HOSTID,
1254 OCI_LINUX_UIDGIDMAP_SIZE,
1255 __OCI_LINUX_UIDGIDMAP_MAX,
1256 };
1257
1258 static const struct blobmsg_policy oci_linux_uidgidmap_policy[] = {
1259 [OCI_LINUX_UIDGIDMAP_CONTAINERID] = { "containerID", BLOBMSG_TYPE_INT32 },
1260 [OCI_LINUX_UIDGIDMAP_HOSTID] = { "hostID", BLOBMSG_TYPE_INT32 },
1261 [OCI_LINUX_UIDGIDMAP_SIZE] = { "size", BLOBMSG_TYPE_INT32 },
1262 };
1263
1264 static int parseOCIuidgidmappings(struct blob_attr *msg, bool is_gidmap)
1265 {
1266 const char *map_format = "%d %d %d\n";
1267 struct blob_attr *tb[__OCI_LINUX_UIDGIDMAP_MAX];
1268 struct blob_attr *cur;
1269 int rem, len;
1270 char **mappings;
1271 char *map, *curstr;
1272 unsigned int cnt = 0;
1273 size_t totallen = 0;
1274
1275 /* count number of mappings */
1276 blobmsg_for_each_attr(cur, msg, rem)
1277 cnt++;
1278
1279 if (!cnt)
1280 return 0;
1281
1282 /* allocate array for mappings */
1283 mappings = calloc(1 + cnt, sizeof(char*));
1284 if (!mappings)
1285 return ENOMEM;
1286
1287 mappings[cnt] = NULL;
1288
1289 cnt = 0;
1290 blobmsg_for_each_attr(cur, msg, rem) {
1291 blobmsg_parse(oci_linux_uidgidmap_policy, __OCI_LINUX_UIDGIDMAP_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
1292
1293 if (!tb[OCI_LINUX_UIDGIDMAP_CONTAINERID] ||
1294 !tb[OCI_LINUX_UIDGIDMAP_HOSTID] ||
1295 !tb[OCI_LINUX_UIDGIDMAP_SIZE])
1296 return EINVAL;
1297
1298 /* write mapping line into allocated string */
1299 len = asprintf(&mappings[cnt++], map_format,
1300 blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_CONTAINERID]),
1301 blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_HOSTID]),
1302 blobmsg_get_u32(tb[OCI_LINUX_UIDGIDMAP_SIZE]));
1303
1304 if (len < 0)
1305 return ENOMEM;
1306
1307 totallen += len;
1308 }
1309
1310 /* allocate combined mapping string */
1311 map = calloc(1 + totallen, sizeof(char));
1312 if (!map)
1313 return ENOMEM;
1314
1315 map[0] = '\0';
1316
1317 /* concatenate mapping strings into combined string */
1318 curstr = mappings[0];
1319 while (curstr) {
1320 strcat(map, curstr);
1321 free(curstr++);
1322 }
1323 free(mappings);
1324
1325 if (is_gidmap)
1326 opts.gidmap = map;
1327 else
1328 opts.uidmap = map;
1329
1330 return 0;
1331 }
1332
1333 enum {
1334 OCI_LINUX_RESOURCES,
1335 OCI_LINUX_SECCOMP,
1336 OCI_LINUX_SYSCTL,
1337 OCI_LINUX_NAMESPACES,
1338 OCI_LINUX_UIDMAPPINGS,
1339 OCI_LINUX_GIDMAPPINGS,
1340 OCI_LINUX_MASKEDPATHS,
1341 OCI_LINUX_READONLYPATHS,
1342 OCI_LINUX_ROOTFSPROPAGATION,
1343 __OCI_LINUX_MAX,
1344 };
1345
1346 static const struct blobmsg_policy oci_linux_policy[] = {
1347 [OCI_LINUX_RESOURCES] = { "resources", BLOBMSG_TYPE_TABLE },
1348 [OCI_LINUX_SECCOMP] = { "seccomp", BLOBMSG_TYPE_TABLE },
1349 [OCI_LINUX_SYSCTL] = { "sysctl", BLOBMSG_TYPE_TABLE },
1350 [OCI_LINUX_NAMESPACES] = { "namespaces", BLOBMSG_TYPE_ARRAY },
1351 [OCI_LINUX_UIDMAPPINGS] = { "uidMappings", BLOBMSG_TYPE_ARRAY },
1352 [OCI_LINUX_GIDMAPPINGS] = { "gidMappings", BLOBMSG_TYPE_ARRAY },
1353 [OCI_LINUX_MASKEDPATHS] = { "maskedPaths", BLOBMSG_TYPE_ARRAY },
1354 [OCI_LINUX_READONLYPATHS] = { "readonlyPaths", BLOBMSG_TYPE_ARRAY },
1355 [OCI_LINUX_ROOTFSPROPAGATION] = { "rootfsPropagation", BLOBMSG_TYPE_STRING },
1356 };
1357
1358 static int parseOCIlinux(struct blob_attr *msg)
1359 {
1360 struct blob_attr *tb[__OCI_LINUX_MAX];
1361 struct blob_attr *cur;
1362 int rem;
1363 int res = 0;
1364
1365 blobmsg_parse(oci_linux_policy, __OCI_LINUX_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
1366
1367 if (tb[OCI_LINUX_NAMESPACES]) {
1368 blobmsg_for_each_attr(cur, tb[OCI_LINUX_NAMESPACES], rem) {
1369 res = parseOCIlinuxns(cur);
1370 if (res)
1371 return res;
1372 }
1373 }
1374
1375 if (tb[OCI_LINUX_UIDMAPPINGS]) {
1376 res = parseOCIuidgidmappings(tb[OCI_LINUX_GIDMAPPINGS], 0);
1377 if (res)
1378 return res;
1379 }
1380
1381 if (tb[OCI_LINUX_GIDMAPPINGS]) {
1382 res = parseOCIuidgidmappings(tb[OCI_LINUX_GIDMAPPINGS], 1);
1383 if (res)
1384 return res;
1385 }
1386
1387 if (tb[OCI_LINUX_SECCOMP]) {
1388 opts.ociseccomp = parseOCIlinuxseccomp(tb[OCI_LINUX_SECCOMP]);
1389 if (!opts.ociseccomp)
1390 return EINVAL;
1391 }
1392
1393 return 0;
1394 }
1395
1396 enum {
1397 OCI_VERSION,
1398 OCI_HOSTNAME,
1399 OCI_PROCESS,
1400 OCI_ROOT,
1401 OCI_MOUNTS,
1402 OCI_HOOKS,
1403 OCI_LINUX,
1404 __OCI_MAX,
1405 };
1406
1407 static const struct blobmsg_policy oci_policy[] = {
1408 [OCI_VERSION] = { "ociVersion", BLOBMSG_TYPE_STRING },
1409 [OCI_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
1410 [OCI_PROCESS] = { "process", BLOBMSG_TYPE_TABLE },
1411 [OCI_ROOT] = { "root", BLOBMSG_TYPE_TABLE },
1412 [OCI_MOUNTS] = { "mounts", BLOBMSG_TYPE_ARRAY },
1413 [OCI_HOOKS] = { "hooks", BLOBMSG_TYPE_TABLE },
1414 [OCI_LINUX] = { "linux", BLOBMSG_TYPE_TABLE },
1415 };
1416
1417 static int parseOCI(const char *jsonfile)
1418 {
1419 struct blob_attr *tb[__OCI_MAX];
1420 struct blob_attr *cur;
1421 int rem;
1422 int res;
1423
1424 blob_buf_init(&ocibuf, 0);
1425 if (!blobmsg_add_json_from_file(&ocibuf, jsonfile))
1426 return ENOENT;
1427
1428 blobmsg_parse(oci_policy, __OCI_MAX, tb, blob_data(ocibuf.head), blob_len(ocibuf.head));
1429
1430 if (!tb[OCI_VERSION])
1431 return ENOMSG;
1432
1433 if (strncmp("1.0", blobmsg_get_string(tb[OCI_VERSION]), 3)) {
1434 ERROR("unsupported ociVersion %s\n", blobmsg_get_string(tb[OCI_VERSION]));
1435 return ENOTSUP;
1436 }
1437
1438 if (tb[OCI_HOSTNAME])
1439 opts.hostname = strdup(blobmsg_get_string(tb[OCI_HOSTNAME]));
1440
1441 if (!tb[OCI_PROCESS])
1442 return ENODATA;
1443
1444 if ((res = parseOCIprocess(tb[OCI_PROCESS])))
1445 return res;
1446
1447 if (!tb[OCI_ROOT])
1448 return ENODATA;
1449
1450 if ((res = parseOCIroot(jsonfile, tb[OCI_ROOT])))
1451 return res;
1452
1453 if (!tb[OCI_MOUNTS])
1454 return ENODATA;
1455
1456 blobmsg_for_each_attr(cur, tb[OCI_MOUNTS], rem)
1457 if ((res = parseOCImount(cur)))
1458 return res;
1459
1460 if (tb[OCI_LINUX] && (res = parseOCIlinux(tb[OCI_LINUX])))
1461 return res;
1462
1463 if (tb[OCI_HOOKS] && (res = parseOCIhooks(tb[OCI_HOOKS])))
1464 return res;
1465
1466 blob_buf_free(&ocibuf);
1467
1468 return 0;
1469 }
1470
1471 int main(int argc, char **argv)
1472 {
1473 sigset_t sigmask;
1474 uid_t uid = getuid();
1475 const char log[] = "/dev/log";
1476 const char ubus[] = "/var/run/ubus.sock";
1477 char *jsonfile = NULL;
1478 int ch, i;
1479 int pipes[4];
1480 char sig_buf[1];
1481 int netns_fd;
1482
1483 if (uid) {
1484 ERROR("not root, aborting: %m\n");
1485 return EXIT_FAILURE;
1486 }
1487
1488 umask(022);
1489 mount_list_init();
1490 init_library_search();
1491
1492 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
1493 switch (ch) {
1494 case 'd':
1495 debug = atoi(optarg);
1496 break;
1497 case 'p':
1498 opts.namespace |= CLONE_NEWNS;
1499 opts.procfs = 1;
1500 break;
1501 case 'o':
1502 opts.namespace |= CLONE_NEWNS;
1503 opts.ronly = 1;
1504 break;
1505 case 'f':
1506 opts.namespace |= CLONE_NEWUSER;
1507 break;
1508 case 'F':
1509 opts.namespace |= CLONE_NEWCGROUP;
1510 break;
1511 case 'R':
1512 opts.extroot = strdup(optarg);
1513 break;
1514 case 's':
1515 opts.namespace |= CLONE_NEWNS;
1516 opts.sysfs = 1;
1517 break;
1518 case 'S':
1519 opts.seccomp = optarg;
1520 add_mount_bind(optarg, 1, -1);
1521 break;
1522 case 'C':
1523 opts.capabilities = optarg;
1524 break;
1525 case 'c':
1526 opts.no_new_privs = 1;
1527 break;
1528 case 'n':
1529 opts.name = optarg;
1530 break;
1531 case 'N':
1532 opts.namespace |= CLONE_NEWNET;
1533 break;
1534 case 'h':
1535 opts.namespace |= CLONE_NEWUTS;
1536 opts.hostname = strdup(optarg);
1537 break;
1538 case 'r':
1539 opts.namespace |= CLONE_NEWNS;
1540 add_path_and_deps(optarg, 1, 0, 0);
1541 break;
1542 case 'w':
1543 opts.namespace |= CLONE_NEWNS;
1544 add_path_and_deps(optarg, 0, 0, 0);
1545 break;
1546 case 'u':
1547 opts.namespace |= CLONE_NEWNS;
1548 add_mount_bind(ubus, 0, -1);
1549 break;
1550 case 'l':
1551 opts.namespace |= CLONE_NEWNS;
1552 add_mount_bind(log, 0, -1);
1553 break;
1554 case 'U':
1555 opts.user = optarg;
1556 break;
1557 case 'G':
1558 opts.group = optarg;
1559 break;
1560 case 'O':
1561 opts.overlaydir = optarg;
1562 break;
1563 case 'T':
1564 opts.tmpoverlaysize = optarg;
1565 break;
1566 case 'E':
1567 opts.require_jail = 1;
1568 break;
1569 case 'y':
1570 opts.console = 1;
1571 break;
1572 case 'J':
1573 asprintf(&jsonfile, "%s/config.json", optarg);
1574 break;
1575 }
1576 }
1577
1578 if (opts.namespace)
1579 opts.namespace |= CLONE_NEWIPC | CLONE_NEWPID;
1580
1581 if (jsonfile) {
1582 int ocires;
1583 ocires = parseOCI(jsonfile);
1584 free(jsonfile);
1585 if (ocires) {
1586 ERROR("parsing of OCI JSON spec has failed: %s (%d)\n", strerror(ocires), ocires);
1587 return ocires;
1588 }
1589 }
1590
1591 if (opts.tmpoverlaysize && strlen(opts.tmpoverlaysize) > 8) {
1592 ERROR("size parameter too long: \"%s\"\n", opts.tmpoverlaysize);
1593 return -1;
1594 }
1595
1596 /* no <binary> param found */
1597 if (!jsonfile && (argc - optind < 1)) {
1598 usage();
1599 return EXIT_FAILURE;
1600 }
1601 if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
1602 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
1603 usage();
1604 return EXIT_FAILURE;
1605 }
1606 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
1607 opts.namespace,
1608 opts.capabilities != 0 || opts.capset.apply,
1609 opts.seccomp != 0 || opts.ociseccomp != 0);
1610
1611 if (!jsonfile) {
1612 /* allocate NULL-terminated array for argv */
1613 opts.jail_argv = calloc(1 + argc - optind, sizeof(char**));
1614 if (!opts.jail_argv)
1615 return EXIT_FAILURE;
1616
1617 for (size_t s = optind; s < argc; s++)
1618 opts.jail_argv[s - optind] = strdup(argv[s]);
1619
1620 if (opts.namespace & CLONE_NEWUSER)
1621 get_jail_user(&opts.pw_uid, &opts.pw_gid, &opts.gr_gid);
1622 }
1623
1624 if (!opts.extroot) {
1625 if (opts.namespace && add_path_and_deps(*opts.jail_argv, 1, -1, 0)) {
1626 ERROR("failed to load dependencies\n");
1627 return -1;
1628 }
1629 }
1630
1631 if (opts.namespace && opts.seccomp && add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
1632 ERROR("failed to load libpreload-seccomp.so\n");
1633 opts.seccomp = 0;
1634 if (opts.require_jail)
1635 return -1;
1636 }
1637
1638 if (opts.name)
1639 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
1640
1641 uloop_init();
1642
1643 sigfillset(&sigmask);
1644 for (i = 0; i < _NSIG; i++) {
1645 struct sigaction s = { 0 };
1646
1647 if (!sigismember(&sigmask, i))
1648 continue;
1649 if ((i == SIGCHLD) || (i == SIGPIPE) || (i == SIGSEGV))
1650 continue;
1651
1652 s.sa_handler = jail_handle_signal;
1653 sigaction(i, &s, NULL);
1654 }
1655
1656 if (opts.namespace) {
1657 if (opts.namespace & CLONE_NEWNS) {
1658 add_mount_bind("/dev/full", 0, -1);
1659 add_mount_bind("/dev/null", 0, -1);
1660 add_mount_bind("/dev/random", 0, -1);
1661 add_mount_bind("/dev/urandom", 0, -1);
1662 add_mount_bind("/dev/zero", 0, -1);
1663 add_mount_bind("/dev/ptmx", 0, -1);
1664 add_mount_bind("/dev/tty", 0, -1);
1665
1666 if (!opts.extroot && (opts.user || opts.group)) {
1667 add_mount_bind("/etc/passwd", 0, -1);
1668 add_mount_bind("/etc/group", 0, -1);
1669 }
1670
1671 #if defined(__GLIBC__)
1672 if (!opts.extroot)
1673 add_mount_bind("/etc/nsswitch.conf", 0, -1);
1674 #endif
1675
1676 if (!(opts.namespace & CLONE_NEWNET)) {
1677 add_mount_bind("/etc/resolv.conf", 0, -1);
1678 } else {
1679 char hostdir[PATH_MAX];
1680
1681 snprintf(hostdir, PATH_MAX, "/tmp/resolv.conf-%s.d", opts.name);
1682 mkdir_p(hostdir, 0755);
1683 add_mount(hostdir, "/tmp/resolv.conf.d", NULL, MS_BIND | MS_NOEXEC | MS_NOATIME | MS_NOSUID | MS_NODEV | MS_RDONLY, NULL, -1);
1684 }
1685 }
1686
1687 if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0)
1688 return -1;
1689
1690 jail_process.pid = clone(exec_jail, child_stack + STACK_SIZE, SIGCHLD | opts.namespace, &pipes);
1691 } else {
1692 jail_process.pid = fork();
1693 }
1694
1695 if (jail_process.pid > 0) {
1696 seteuid(0);
1697 /* parent process */
1698 close(pipes[1]);
1699 close(pipes[2]);
1700 run_hooks(opts.hooks.createRuntime);
1701 if (read(pipes[0], sig_buf, 1) < 1) {
1702 ERROR("can't read from child\n");
1703 return -1;
1704 }
1705 close(pipes[0]);
1706 if (opts.namespace & CLONE_NEWUSER) {
1707 if (write_setgroups(jail_process.pid, true)) {
1708 ERROR("can't write setgroups\n");
1709 return -1;
1710 }
1711 if (!opts.uidmap) {
1712 bool has_gr = (opts.gr_gid != -1);
1713 if (opts.pw_uid != -1) {
1714 write_single_uid_gid_map(jail_process.pid, 0, opts.pw_uid);
1715 write_single_uid_gid_map(jail_process.pid, 1, has_gr?opts.gr_gid:opts.pw_gid);
1716 } else {
1717 write_single_uid_gid_map(jail_process.pid, 0, 65534);
1718 write_single_uid_gid_map(jail_process.pid, 1, has_gr?opts.gr_gid:65534);
1719 }
1720 } else {
1721 write_uid_gid_map(jail_process.pid, 0, opts.uidmap);
1722 if (opts.gidmap)
1723 write_uid_gid_map(jail_process.pid, 1, opts.gidmap);
1724 }
1725 }
1726
1727 if (opts.namespace & CLONE_NEWNET) {
1728 if (!opts.name) {
1729 ERROR("netns needs a named jail\n");
1730 return -1;
1731 }
1732 netns_fd = netns_open_pid(jail_process.pid);
1733 netns_updown(jail_process.pid, true);
1734 }
1735
1736 sig_buf[0] = 'O';
1737 if (write(pipes[3], sig_buf, 1) < 0) {
1738 ERROR("can't write to child\n");
1739 return -1;
1740 }
1741 close(pipes[3]);
1742 run_hooks(opts.hooks.poststart);
1743 uloop_process_add(&jail_process);
1744 uloop_run();
1745 if (jail_running) {
1746 DEBUG("uloop interrupted, killing jail process\n");
1747 kill(jail_process.pid, SIGTERM);
1748 uloop_timeout_set(&jail_process_timeout, 1000);
1749 uloop_run();
1750 }
1751 uloop_done();
1752 if (opts.namespace & CLONE_NEWNET) {
1753 setns(netns_fd, CLONE_NEWNET);
1754 netns_updown(getpid(), false);
1755 close(netns_fd);
1756 }
1757 run_hooks(opts.hooks.poststop);
1758 free_opts(true);
1759 return jail_return_code;
1760 } else if (jail_process.pid == 0) {
1761 /* fork child process */
1762 return exec_jail(NULL);
1763 } else {
1764 ERROR("failed to clone/fork: %m\n");
1765 return EXIT_FAILURE;
1766 }
1767 }