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