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