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