2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
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
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.
15 #include <sys/mount.h>
16 #include <sys/prctl.h>
18 #include <sys/types.h>
29 #include <linux/limits.h>
30 #include <linux/filter.h>
33 #include "capabilities.h"
38 #include "seccomp-oci.h"
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>
48 #ifndef CLONE_NEWCGROUP
49 #define CLONE_NEWCGROUP 0x02000000
52 #define STACK_SIZE (1024 * 1024)
53 #define OPT_ARGS "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:EyJ:"
68 struct sock_fprog
*ociseccomp
;
70 struct jail_capset capset
;
90 struct hook_execvpe
*createRuntime
;
91 struct hook_execvpe
*createContainer
;
92 struct hook_execvpe
*startContainer
;
93 struct hook_execvpe
*poststart
;
94 struct hook_execvpe
*poststop
;
98 static void free_hooklist(struct hook_execvpe
*hooklist
)
100 struct hook_execvpe
*cur
;
121 static void free_opts(bool child
) {
124 /* we need to keep argv, envp and seccomp filter in child */
126 if (opts
.ociseccomp
) {
127 free(opts
.ociseccomp
->filter
);
128 free(opts
.ociseccomp
);
131 tmp
= opts
.jail_argv
;
135 free(opts
.jail_argv
);
149 free_hooklist(opts
.hooks
.createRuntime
);
150 free_hooklist(opts
.hooks
.createContainer
);
151 free_hooklist(opts
.hooks
.startContainer
);
152 free_hooklist(opts
.hooks
.poststart
);
153 free_hooklist(opts
.hooks
.poststop
);
156 static struct blob_buf ocibuf
;
158 extern int pivot_root(const char *new_root
, const char *put_old
);
162 static char child_stack
[STACK_SIZE
];
166 static int mount_overlay(char *jail_root
, char *overlaydir
) {
167 char *upperdir
, *workdir
, *optsstr
, *upperetc
, *upperresolvconf
;
168 const char mountoptsformat
[] = "lowerdir=%s,upperdir=%s,workdir=%s";
171 if (asprintf(&upperdir
, "%s%s", overlaydir
, "/upper") < 0)
174 if (asprintf(&workdir
, "%s%s", overlaydir
, "/work") < 0)
177 if (asprintf(&optsstr
, mountoptsformat
, jail_root
, upperdir
, workdir
) < 0)
180 if (mkdir_p(upperdir
, 0755) || mkdir_p(workdir
, 0755))
184 * make sure /etc/resolv.conf exists in overlay and is owned by jail userns root
185 * this is to work-around a bug in overlayfs described in the overlayfs-userns
187 * 3. modification of a file 'hithere' which is in l but not yet
188 * in u, and which is not owned by T, is not allowed, even if
189 * writes to u are allowed. This may be a bug in overlayfs,
190 * but it is safe behavior.
192 if (asprintf(&upperetc
, "%s/etc", upperdir
) < 0)
195 if (mkdir_p(upperetc
, 0755))
196 goto upper_etc_printf
;
198 if (asprintf(&upperresolvconf
, "%s/resolv.conf", upperetc
) < 0)
199 goto upper_etc_printf
;
201 fd
= creat(upperresolvconf
, 0644);
203 ERROR("creat(%s) failed: %m\n", upperresolvconf
);
204 goto upper_resolvconf_printf
;
208 DEBUG("mount -t overlay %s %s (%s)\n", jail_root
, jail_root
, optsstr
);
210 if (mount(jail_root
, jail_root
, "overlay", MS_NOATIME
, optsstr
))
215 upper_resolvconf_printf
:
216 free(upperresolvconf
);
229 static void pass_console(int console_fd
)
231 struct ubus_context
*ctx
= ubus_connect(NULL
);
232 static struct blob_buf req
;
238 blob_buf_init(&req
, 0);
239 blobmsg_add_string(&req
, "name", opts
.name
);
241 if (ubus_lookup_id(ctx
, "container", &id
) ||
242 ubus_invoke_fd(ctx
, id
, "console_set", req
.head
, NULL
, NULL
, 3000, console_fd
))
243 INFO("ubus request failed\n");
251 static int create_dev_console(const char *jail_root
)
254 char dev_console_path
[PATH_MAX
];
255 int slave_console_fd
;
257 /* Open UNIX/98 virtual console */
258 console_fd
= posix_openpt(O_RDWR
| O_NOCTTY
);
259 if (console_fd
== -1)
262 console_fname
= ptsname(console_fd
);
263 DEBUG("got console fd %d and PTS client name %s\n", console_fd
, console_fname
);
268 unlockpt(console_fd
);
270 /* pass PTY master to procd */
271 pass_console(console_fd
);
273 /* mount-bind PTY slave to /dev/console in jail */
274 snprintf(dev_console_path
, sizeof(dev_console_path
), "%s/dev/console", jail_root
);
275 close(creat(dev_console_path
, 0620));
277 if (mount(console_fname
, dev_console_path
, NULL
, MS_BIND
, NULL
))
280 /* use PTY slave for stdio */
281 slave_console_fd
= open(console_fname
, O_RDWR
); /* | O_NOCTTY */
282 dup2(slave_console_fd
, 0);
283 dup2(slave_console_fd
, 1);
284 dup2(slave_console_fd
, 2);
285 close(slave_console_fd
);
287 INFO("using guest console %s\n", console_fname
);
296 static int hook_running
= 0;
297 static int hook_return_code
= 0;
299 static void hook_process_timeout_cb(struct uloop_timeout
*t
);
300 static struct uloop_timeout hook_process_timeout
= {
301 .cb
= hook_process_timeout_cb
,
304 static void hook_process_handler(struct uloop_process
*c
, int ret
)
306 uloop_timeout_cancel(&hook_process_timeout
);
307 if (WIFEXITED(ret
)) {
308 hook_return_code
= WEXITSTATUS(ret
);
309 INFO("hook (%d) exited with exit: %d\n", c
->pid
, hook_return_code
);
311 hook_return_code
= WTERMSIG(ret
);
312 INFO("hook (%d) exited with signal: %d\n", c
->pid
, hook_return_code
);
318 static struct uloop_process hook_process
= {
319 .cb
= hook_process_handler
,
322 static void hook_process_timeout_cb(struct uloop_timeout
*t
)
324 DEBUG("hook process failed to stop, sending SIGKILL\n");
325 kill(hook_process
.pid
, SIGKILL
);
328 static void hook_handle_signal(int signo
)
330 DEBUG("forwarding signal %d to the jailed process\n", signo
);
331 kill(hook_process
.pid
, signo
);
334 static int run_hook(struct hook_execvpe
*hook
)
339 DEBUG("executing hook %s\n", hook
->file
);
341 sigfillset(&sigmask
);
342 for (i
= 0; i
< _NSIG
; i
++) {
343 struct sigaction s
= { 0 };
345 if (!sigismember(&sigmask
, i
))
347 if ((i
== SIGCHLD
) || (i
== SIGPIPE
) || (i
== SIGSEGV
))
350 s
.sa_handler
= hook_handle_signal
;
351 sigaction(i
, &s
, NULL
);
354 hook_process
.pid
= fork();
355 if (hook_process
.pid
> 0) {
357 uloop_process_add(&hook_process
);
358 if (hook
->timeout
> 0)
359 uloop_timeout_set(&hook_process_timeout
, 1000 * hook
->timeout
);
363 DEBUG("uloop interrupted, killing hook process\n");
364 kill(hook_process
.pid
, SIGTERM
);
365 uloop_timeout_set(&hook_process_timeout
, 1000);
371 } else if (hook_process
.pid
== 0) {
373 execvpe(hook
->file
, hook
->argv
, hook
->envp
);
381 static int run_hooks(struct hook_execvpe
*hook
)
383 struct hook_execvpe
*cur
;
388 res
= run_hook(cur
++);
396 static int build_jail_fs(void)
398 char jail_root
[] = "/tmp/ujail-XXXXXX";
399 char tmpovdir
[] = "/tmp/ujail-overlay-XXXXXX";
400 char tmpdevdir
[] = "/tmp/ujail-XXXXXX/dev";
401 char tmpdevptsdir
[] = "/tmp/ujail-XXXXXX/dev/pts";
402 char *overlaydir
= NULL
;
404 if (mkdtemp(jail_root
) == NULL
) {
405 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
409 /* oldroot can't be MS_SHARED else pivot_root() fails */
410 if (mount("none", "/", NULL
, MS_REC
|MS_PRIVATE
, NULL
)) {
411 ERROR("private mount failed %m\n");
416 if (mount(opts
.extroot
, jail_root
, NULL
, MS_BIND
, NULL
)) {
417 ERROR("extroot mount failed %m\n");
421 if (mount("tmpfs", jail_root
, "tmpfs", MS_NOATIME
, "mode=0755")) {
422 ERROR("tmpfs mount failed %m\n");
427 if (opts
.tmpoverlaysize
) {
428 char mountoptsstr
[] = "mode=0755,size=XXXXXXXX";
430 snprintf(mountoptsstr
, sizeof(mountoptsstr
),
431 "mode=0755,size=%s", opts
.tmpoverlaysize
);
432 if (mkdtemp(tmpovdir
) == NULL
) {
433 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
436 if (mount("tmpfs", tmpovdir
, "tmpfs", MS_NOATIME
,
438 ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts
.tmpoverlaysize
);
441 overlaydir
= tmpovdir
;
445 overlaydir
= opts
.overlaydir
;
448 mount_overlay(jail_root
, overlaydir
);
450 if (chdir(jail_root
)) {
451 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root
);
455 snprintf(tmpdevdir
, sizeof(tmpdevdir
), "%s/dev", jail_root
);
456 mkdir_p(tmpdevdir
, 0755);
457 if (mount(NULL
, tmpdevdir
, "tmpfs", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, "size=1M"))
460 snprintf(tmpdevptsdir
, sizeof(tmpdevptsdir
), "%s/dev/pts", jail_root
);
461 mkdir_p(tmpdevptsdir
, 0755);
462 if (mount(NULL
, tmpdevptsdir
, "devpts", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, NULL
))
466 create_dev_console(jail_root
);
468 if (mount_all(jail_root
)) {
469 ERROR("mount_all() failed\n");
473 /* make sure /etc/resolv.conf exists if in new network namespace */
474 if (opts
.namespace & CLONE_NEWNET
) {
475 char jailetc
[PATH_MAX
], jaillink
[PATH_MAX
];
477 snprintf(jailetc
, PATH_MAX
, "%s/etc", jail_root
);
478 mkdir_p(jailetc
, 0755);
479 snprintf(jaillink
, PATH_MAX
, "%s/etc/resolv.conf", jail_root
);
483 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink
);
486 run_hooks(opts
.hooks
.createContainer
);
488 char dirbuf
[sizeof(jail_root
) + 4];
489 snprintf(dirbuf
, sizeof(dirbuf
), "%s/old", jail_root
);
492 if (pivot_root(jail_root
, dirbuf
) == -1) {
493 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root
, dirbuf
);
497 ERROR("chdir(/) (after pivot_root) failed: %m\n");
501 snprintf(dirbuf
, sizeof(dirbuf
), "/old%s", jail_root
);
502 umount2(dirbuf
, MNT_DETACH
);
504 if (opts
.tmpoverlaysize
) {
505 char tmpdirbuf
[sizeof(tmpovdir
) + 4];
506 snprintf(tmpdirbuf
, sizeof(tmpdirbuf
), "/old%s", tmpovdir
);
507 umount2(tmpdirbuf
, MNT_DETACH
);
511 umount2("/old", MNT_DETACH
);
515 mkdir("/proc", 0755);
516 mount("proc", "/proc", "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
518 * make /proc/sys read-only while keeping read-write to
519 * /proc/sys/net if CLONE_NEWNET is set.
521 if (opts
.namespace & CLONE_NEWNET
)
522 mount("/proc/sys/net", "/proc/self/net", NULL
, MS_BIND
, 0);
524 mount("/proc/sys", "/proc/sys", NULL
, MS_BIND
, 0);
525 mount(NULL
, "/proc/sys", NULL
, MS_REMOUNT
| MS_RDONLY
, 0);
526 mount(NULL
, "/proc", NULL
, MS_REMOUNT
, 0);
528 if (opts
.namespace & CLONE_NEWNET
)
529 mount("/proc/self/net", "/proc/sys/net", NULL
, MS_MOVE
, 0);
533 mount("sysfs", "/sys", "sysfs", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RDONLY
, 0);
536 mount(NULL
, "/", NULL
, MS_RDONLY
| MS_REMOUNT
, 0);
541 static int write_uid_gid_map(pid_t child_pid
, bool gidmap
, char *mapstr
)
546 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
547 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
550 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
553 if (dprintf(map_file
, "%s", mapstr
)) {
563 static int write_single_uid_gid_map(pid_t child_pid
, bool gidmap
, int id
)
567 const char *map_format
= "%d %d %d\n";
568 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
569 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
572 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
575 if (dprintf(map_file
, map_format
, 0, id
, 1) == -1) {
584 static int write_setgroups(pid_t child_pid
, bool allow
)
587 char setgroups_path
[64];
589 if (snprintf(setgroups_path
, sizeof(setgroups_path
), "/proc/%d/setgroups",
594 if ((setgroups_file
= open(setgroups_path
, O_WRONLY
)) == -1) {
598 if (dprintf(setgroups_file
, "%s", allow
?"allow":"deny") == -1) {
599 close(setgroups_file
);
603 close(setgroups_file
);
607 static void get_jail_user(int *user
, int *user_gid
, int *gr_gid
)
609 struct passwd
*p
= NULL
;
610 struct group
*g
= NULL
;
613 p
= getpwnam(opts
.user
);
615 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
616 opts
.user
, errno
, strerror(errno
));
620 *user_gid
= p
->pw_gid
;
627 g
= getgrnam(opts
.group
);
629 ERROR("failed to get gid for group %s: %m\n", opts
.group
);
638 static void set_jail_user(int pw_uid
, int user_gid
, int gr_gid
)
640 if (opts
.user
&& (user_gid
!= -1) && initgroups(opts
.user
, user_gid
)) {
641 ERROR("failed to initgroups() for user %s: %m\n", opts
.user
);
645 if ((gr_gid
!= -1) && setregid(gr_gid
, gr_gid
)) {
646 ERROR("failed to set group id %d: %m\n", gr_gid
);
650 if ((pw_uid
!= -1) && setreuid(pw_uid
, pw_uid
)) {
651 ERROR("failed to set user id %d: %m\n", pw_uid
);
657 static char** build_envp(const char *seccomp
, char **ocienvp
)
659 static char *envp
[MAX_ENVP
];
660 static char preload_var
[PATH_MAX
];
661 static char seccomp_var
[PATH_MAX
];
662 static char debug_var
[] = "LD_DEBUG=all";
663 static char container_var
[] = "container=ujail";
664 const char *preload_lib
= find_lib("libpreload-seccomp.so");
669 if (seccomp
&& !preload_lib
) {
670 ERROR("failed to add preload-lib to env\n");
674 snprintf(seccomp_var
, sizeof(seccomp_var
), "SECCOMP_FILE=%s", seccomp
);
675 envp
[count
++] = seccomp_var
;
676 snprintf(preload_var
, sizeof(preload_var
), "LD_PRELOAD=%s", preload_lib
);
677 envp
[count
++] = preload_var
;
680 envp
[count
++] = container_var
;
683 envp
[count
++] = debug_var
;
686 while (addenv
&& *addenv
) {
687 envp
[count
++] = *(addenv
++);
688 if (count
>= MAX_ENVP
) {
689 ERROR("environment limited to %d extra records, truncating\n", MAX_ENVP
);
696 static void usage(void)
698 fprintf(stderr
, "ujail <options> -- <binary> <params ...>\n");
699 fprintf(stderr
, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
700 fprintf(stderr
, " -S <file>\tseccomp filter config\n");
701 fprintf(stderr
, " -C <file>\tcapabilities drop config\n");
702 fprintf(stderr
, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
703 fprintf(stderr
, " -n <name>\tthe name of the jail\n");
704 fprintf(stderr
, "namespace jail options:\n");
705 fprintf(stderr
, " -h <hostname>\tchange the hostname of the jail\n");
706 fprintf(stderr
, " -N\t\tjail has network namespace\n");
707 fprintf(stderr
, " -f\t\tjail has user namespace\n");
708 fprintf(stderr
, " -F\t\tjail has cgroups namespace\n");
709 fprintf(stderr
, " -r <file>\treadonly files that should be staged\n");
710 fprintf(stderr
, " -w <file>\twriteable files that should be staged\n");
711 fprintf(stderr
, " -p\t\tjail has /proc\n");
712 fprintf(stderr
, " -s\t\tjail has /sys\n");
713 fprintf(stderr
, " -l\t\tjail has /dev/log\n");
714 fprintf(stderr
, " -u\t\tjail has a ubus socket\n");
715 fprintf(stderr
, " -U <name>\tuser to run jailed process\n");
716 fprintf(stderr
, " -G <name>\tgroup to run jailed process\n");
717 fprintf(stderr
, " -o\t\tremont jail root (/) read only\n");
718 fprintf(stderr
, " -R <dir>\texternal jail rootfs (system container)\n");
719 fprintf(stderr
, " -O <dir>\tdirectory for r/w overlayfs\n");
720 fprintf(stderr
, " -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
721 fprintf(stderr
, " -E\t\tfail if jail cannot be setup\n");
722 fprintf(stderr
, " -y\t\tprovide jail console\n");
723 fprintf(stderr
, " -J <dir>\tstart OCI bundle\n");
724 fprintf(stderr
, "\nWarning: by default root inside the jail is the same\n\
725 and he has the same powers as root outside the jail,\n\
726 thus he can escape the jail and/or break stuff.\n\
727 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
728 If you use none of the namespace jail options,\n\
729 ujail will not use namespace/build a jail,\n\
730 and will only drop capabilities/apply seccomp filter.\n\n");
733 static int exec_jail(void *pipes_ptr
)
735 int *pipes
= (int*)pipes_ptr
;
737 int pw_uid
, pw_gid
, gr_gid
;
743 if (write(pipes
[1], buf
, 1) < 1) {
744 ERROR("can't write to parent\n");
747 if (read(pipes
[2], buf
, 1) < 1) {
748 ERROR("can't read from parent\n");
752 ERROR("parent had an error, child exiting\n");
759 if (opts
.namespace & CLONE_NEWUSER
) {
760 if (setregid(0, 0) < 0) {
764 if (setreuid(0, 0) < 0) {
768 if (setgroups(0, NULL
) < 0) {
769 ERROR("setgroups\n");
774 if (opts
.namespace && opts
.hostname
&& strlen(opts
.hostname
) > 0
775 && sethostname(opts
.hostname
, strlen(opts
.hostname
))) {
776 ERROR("sethostname(%s) failed: %m\n", opts
.hostname
);
780 if ((opts
.namespace & CLONE_NEWNS
) && build_jail_fs()) {
781 ERROR("failed to build jail fs\n");
785 run_hooks(opts
.hooks
.startContainer
);
787 if (applyOCIcapabilities(opts
.capset
))
790 if (opts
.capabilities
&& drop_capabilities(opts
.capabilities
))
793 if (opts
.no_new_privs
&& prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
794 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
798 if (!(opts
.namespace & CLONE_NEWUSER
)) {
799 get_jail_user(&pw_uid
, &pw_gid
, &gr_gid
);
801 set_jail_user(opts
.pw_uid
?:pw_uid
, opts
.pw_gid
?:pw_gid
, opts
.gr_gid
?:gr_gid
);
804 char **envp
= build_envp(opts
.seccomp
, opts
.envp
);
808 if (opts
.cwd
&& chdir(opts
.cwd
))
811 if (opts
.ociseccomp
&& applyOCIlinuxseccomp(opts
.ociseccomp
))
815 INFO("exec-ing %s\n", *opts
.jail_argv
);
816 if (opts
.envp
) /* respect PATH if potentially set in ENV */
817 execvpe(*opts
.jail_argv
, opts
.jail_argv
, envp
);
819 execve(*opts
.jail_argv
, opts
.jail_argv
, envp
);
821 /* we get there only if execve fails */
822 ERROR("failed to execve %s: %m\n", *opts
.jail_argv
);
826 static int jail_running
= 1;
827 static int jail_return_code
= 0;
829 static void jail_process_timeout_cb(struct uloop_timeout
*t
);
830 static struct uloop_timeout jail_process_timeout
= {
831 .cb
= jail_process_timeout_cb
,
834 static void jail_process_handler(struct uloop_process
*c
, int ret
)
836 uloop_timeout_cancel(&jail_process_timeout
);
837 if (WIFEXITED(ret
)) {
838 jail_return_code
= WEXITSTATUS(ret
);
839 INFO("jail (%d) exited with exit: %d\n", c
->pid
, jail_return_code
);
841 jail_return_code
= WTERMSIG(ret
);
842 INFO("jail (%d) exited with signal: %d\n", c
->pid
, jail_return_code
);
848 static struct uloop_process jail_process
= {
849 .cb
= jail_process_handler
,
852 static void jail_process_timeout_cb(struct uloop_timeout
*t
)
854 DEBUG("jail process failed to stop, sending SIGKILL\n");
855 kill(jail_process
.pid
, SIGKILL
);
858 static void jail_handle_signal(int signo
)
860 DEBUG("forwarding signal %d to the jailed process\n", signo
);
861 kill(jail_process
.pid
, signo
);
864 static int netns_open_pid(const pid_t target_ns
)
866 char pid_net_path
[PATH_MAX
];
868 snprintf(pid_net_path
, sizeof(pid_net_path
), "/proc/%u/ns/net", target_ns
);
870 return open(pid_net_path
, O_RDONLY
);
873 static void netns_updown(pid_t pid
, bool start
)
875 struct ubus_context
*ctx
= ubus_connect(NULL
);
876 static struct blob_buf req
;
882 blob_buf_init(&req
, 0);
883 blobmsg_add_string(&req
, "jail", opts
.name
);
884 blobmsg_add_u32(&req
, "pid", pid
);
885 blobmsg_add_u8(&req
, "start", start
);
887 if (ubus_lookup_id(ctx
, "network", &id
) ||
888 ubus_invoke(ctx
, id
, "netns_updown", req
.head
, NULL
, NULL
, 3000))
889 INFO("ubus request failed\n");
895 static int parseOCIenvarray(struct blob_attr
*msg
, char ***envp
)
897 struct blob_attr
*cur
;
900 blobmsg_for_each_attr(cur
, msg
, rem
)
904 *envp
= calloc(1 + sz
, sizeof(char*));
913 blobmsg_for_each_attr(cur
, msg
, rem
)
914 (*envp
)[sz
++] = strdup(blobmsg_get_string(cur
));
928 static const struct blobmsg_policy oci_root_policy
[] = {
929 [OCI_ROOT_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
930 [OCI_ROOT_READONLY
] = { "readonly", BLOBMSG_TYPE_BOOL
},
933 static int parseOCIroot(const char *jsonfile
, struct blob_attr
*msg
)
935 static char rootpath
[PATH_MAX
] = { 0 };
936 struct blob_attr
*tb
[__OCI_ROOT_MAX
];
939 blobmsg_parse(oci_root_policy
, __OCI_ROOT_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
941 if (!tb
[OCI_ROOT_PATH
])
944 strncpy(rootpath
, jsonfile
, PATH_MAX
);
945 cur
= strrchr(rootpath
, '/');
951 strncat(rootpath
, blobmsg_get_string(tb
[OCI_ROOT_PATH
]), PATH_MAX
- (strlen(rootpath
) + 1));
953 opts
.extroot
= rootpath
;
955 opts
.ronly
= blobmsg_get_bool(tb
[OCI_ROOT_READONLY
]);
969 static const struct blobmsg_policy oci_hook_policy
[] = {
970 [OCI_HOOK_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
971 [OCI_HOOK_ARGS
] = { "args", BLOBMSG_TYPE_ARRAY
},
972 [OCI_HOOK_ENV
] = { "env", BLOBMSG_TYPE_ARRAY
},
973 [OCI_HOOK_TIMEOUT
] = { "timeout", BLOBMSG_TYPE_INT32
},
977 static int parseOCIhook(struct hook_execvpe
**hooklist
, struct blob_attr
*msg
)
979 struct blob_attr
*tb
[__OCI_HOOK_MAX
];
980 struct blob_attr
*cur
;
984 blobmsg_for_each_attr(cur
, msg
, rem
)
990 *hooklist
= calloc(idx
+ 1, sizeof(struct hook_execvpe
*));
993 blobmsg_for_each_attr(cur
, msg
, rem
) {
994 blobmsg_parse(oci_hook_policy
, __OCI_HOOK_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
996 if (!tb
[OCI_HOOK_PATH
]) {
1001 hooklist
[idx
] = malloc(sizeof(struct hook_execvpe
));
1002 if (tb
[OCI_HOOK_ARGS
]) {
1003 ret
= parseOCIenvarray(tb
[OCI_HOOK_ARGS
], &(hooklist
[idx
]->argv
));
1008 if (tb
[OCI_HOOK_ENV
]) {
1009 ret
= parseOCIenvarray(tb
[OCI_HOOK_ENV
], &(hooklist
[idx
]->envp
));
1014 if (tb
[OCI_HOOK_TIMEOUT
])
1015 hooklist
[idx
]->timeout
= blobmsg_get_u32(tb
[OCI_HOOK_TIMEOUT
]);
1017 hooklist
[idx
]->file
= strdup(blobmsg_get_string(tb
[OCI_HOOK_PATH
]));
1022 hooklist
[idx
] = NULL
;
1026 free_hooklist(*hooklist
);
1035 OCI_HOOKS_CREATERUNTIME
,
1036 OCI_HOOKS_CREATECONTAINER
,
1037 OCI_HOOKS_STARTCONTAINER
,
1038 OCI_HOOKS_POSTSTART
,
1043 static const struct blobmsg_policy oci_hooks_policy
[] = {
1044 [OCI_HOOKS_PRESTART
] = { "prestart", BLOBMSG_TYPE_ARRAY
},
1045 [OCI_HOOKS_CREATERUNTIME
] = { "createRuntime", BLOBMSG_TYPE_ARRAY
},
1046 [OCI_HOOKS_CREATECONTAINER
] = { "createContainer", BLOBMSG_TYPE_ARRAY
},
1047 [OCI_HOOKS_STARTCONTAINER
] = { "startContainer", BLOBMSG_TYPE_ARRAY
},
1048 [OCI_HOOKS_POSTSTART
] = { "poststart", BLOBMSG_TYPE_ARRAY
},
1049 [OCI_HOOKS_POSTSTOP
] = { "poststop", BLOBMSG_TYPE_ARRAY
},
1052 static int parseOCIhooks(struct blob_attr
*msg
)
1054 struct blob_attr
*tb
[__OCI_HOOKS_MAX
];
1057 blobmsg_parse(oci_hooks_policy
, __OCI_HOOKS_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1059 if (tb
[OCI_HOOKS_PRESTART
])
1060 INFO("warning: ignoring deprecated prestart hook");
1062 if (tb
[OCI_HOOKS_CREATERUNTIME
]) {
1063 ret
= parseOCIhook(&opts
.hooks
.createRuntime
, tb
[OCI_HOOKS_CREATERUNTIME
]);
1068 if (tb
[OCI_HOOKS_CREATECONTAINER
]) {
1069 ret
= parseOCIhook(&opts
.hooks
.createContainer
, tb
[OCI_HOOKS_CREATECONTAINER
]);
1071 goto out_createruntime
;
1074 if (tb
[OCI_HOOKS_STARTCONTAINER
]) {
1075 ret
= parseOCIhook(&opts
.hooks
.startContainer
, tb
[OCI_HOOKS_STARTCONTAINER
]);
1077 goto out_createcontainer
;
1080 if (tb
[OCI_HOOKS_POSTSTART
]) {
1081 ret
= parseOCIhook(&opts
.hooks
.poststart
, tb
[OCI_HOOKS_POSTSTART
]);
1083 goto out_startcontainer
;
1086 if (tb
[OCI_HOOKS_POSTSTOP
]) {
1087 ret
= parseOCIhook(&opts
.hooks
.poststop
, tb
[OCI_HOOKS_POSTSTOP
]);
1095 free_hooklist(opts
.hooks
.poststart
);
1097 free_hooklist(opts
.hooks
.startContainer
);
1098 out_createcontainer
:
1099 free_hooklist(opts
.hooks
.createContainer
);
1101 free_hooklist(opts
.hooks
.createRuntime
);
1108 OCI_PROCESS_USER_UID
,
1109 OCI_PROCESS_USER_GID
,
1110 OCI_PROCESS_USER_UMASK
,
1111 OCI_PROCESS_USER_ADDITIONALGIDS
,
1112 __OCI_PROCESS_USER_MAX
,
1115 static const struct blobmsg_policy oci_process_user_policy
[] = {
1116 [OCI_PROCESS_USER_UID
] = { "uid", BLOBMSG_TYPE_INT32
},
1117 [OCI_PROCESS_USER_GID
] = { "gid", BLOBMSG_TYPE_INT32
},
1118 [OCI_PROCESS_USER_UMASK
] = { "umask", BLOBMSG_TYPE_INT32
},
1119 [OCI_PROCESS_USER_ADDITIONALGIDS
] = { "additionalGids", BLOBMSG_TYPE_ARRAY
},
1122 static int parseOCIprocessuser(struct blob_attr
*msg
) {
1123 struct blob_attr
*tb
[__OCI_PROCESS_USER_MAX
];
1125 blobmsg_parse(oci_process_user_policy
, __OCI_PROCESS_USER_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1127 if (tb
[OCI_PROCESS_USER_UID
])
1128 opts
.pw_uid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_UID
]);
1130 if (tb
[OCI_PROCESS_USER_GID
]) {
1131 opts
.pw_gid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_GID
]);
1132 opts
.gr_gid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_GID
]);
1135 /* ToDo: umask, additional GIDs */
1142 OCI_PROCESS_CAPABILITIES
,
1145 OCI_PROCESS_NONEWPRIVILEGES
,
1146 OCI_PROCESS_RLIMITS
,
1147 OCI_PROCESS_TERMINAL
,
1152 static const struct blobmsg_policy oci_process_policy
[] = {
1153 [OCI_PROCESS_ARGS
] = { "args", BLOBMSG_TYPE_ARRAY
},
1154 [OCI_PROCESS_CAPABILITIES
] = { "capabilities", BLOBMSG_TYPE_TABLE
},
1155 [OCI_PROCESS_CWD
] = { "cwd", BLOBMSG_TYPE_STRING
},
1156 [OCI_PROCESS_ENV
] = { "env", BLOBMSG_TYPE_ARRAY
},
1157 [OCI_PROCESS_NONEWPRIVILEGES
] = { "noNewPrivileges", BLOBMSG_TYPE_BOOL
},
1158 [OCI_PROCESS_RLIMITS
] = { "rlimits", BLOBMSG_TYPE_ARRAY
},
1159 [OCI_PROCESS_TERMINAL
] = { "terminal", BLOBMSG_TYPE_BOOL
},
1160 [OCI_PROCESS_USER
] = { "user", BLOBMSG_TYPE_TABLE
},
1164 static int parseOCIprocess(struct blob_attr
*msg
)
1166 struct blob_attr
*tb
[__OCI_PROCESS_MAX
];
1169 blobmsg_parse(oci_process_policy
, __OCI_PROCESS_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1171 if (!tb
[OCI_PROCESS_ARGS
])
1174 res
= parseOCIenvarray(tb
[OCI_PROCESS_ARGS
], &opts
.jail_argv
);
1178 opts
.console
= blobmsg_get_bool(tb
[OCI_PROCESS_TERMINAL
]);
1179 opts
.no_new_privs
= blobmsg_get_bool(tb
[OCI_PROCESS_NONEWPRIVILEGES
]);
1181 if (tb
[OCI_PROCESS_CWD
])
1182 opts
.cwd
= strdup(blobmsg_get_string(tb
[OCI_PROCESS_CWD
]));
1184 if (tb
[OCI_PROCESS_ENV
]) {
1185 res
= parseOCIenvarray(tb
[OCI_PROCESS_ENV
], &opts
.envp
);
1190 if (tb
[OCI_PROCESS_USER
] && (res
= parseOCIprocessuser(tb
[OCI_PROCESS_USER
])))
1193 if (tb
[OCI_PROCESS_CAPABILITIES
] &&
1194 (res
= parseOCIcapabilities(&opts
.capset
, tb
[OCI_PROCESS_CAPABILITIES
])))
1197 /* ToDo: rlimits, capabilities */
1203 OCI_LINUX_NAMESPACE_TYPE
,
1204 OCI_LINUX_NAMESPACE_PATH
,
1205 __OCI_LINUX_NAMESPACE_MAX
,
1208 static const struct blobmsg_policy oci_linux_namespace_policy
[] = {
1209 [OCI_LINUX_NAMESPACE_TYPE
] = { "type", BLOBMSG_TYPE_STRING
},
1210 [OCI_LINUX_NAMESPACE_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
1213 static unsigned int resolve_nstype(char *type
) {
1214 if (!strcmp("pid", type
))
1215 return CLONE_NEWPID
;
1216 else if (!strcmp("network", type
))
1217 return CLONE_NEWNET
;
1218 else if (!strcmp("mount", type
))
1220 else if (!strcmp("ipc", type
))
1221 return CLONE_NEWIPC
;
1222 else if (!strcmp("uts", type
))
1223 return CLONE_NEWUTS
;
1224 else if (!strcmp("user", type
))
1225 return CLONE_NEWUSER
;
1226 else if (!strcmp("cgroup", type
))
1227 return CLONE_NEWCGROUP
;
1232 static int parseOCIlinuxns(struct blob_attr
*msg
)
1234 struct blob_attr
*tb
[__OCI_LINUX_NAMESPACE_MAX
];
1237 blobmsg_parse(oci_linux_namespace_policy
, __OCI_LINUX_NAMESPACE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1239 if (!tb
[OCI_LINUX_NAMESPACE_TYPE
])
1242 if (tb
[OCI_LINUX_NAMESPACE_PATH
])
1243 return ENOTSUP
; /* ToDo */
1245 opts
.namespace |= resolve_nstype(blobmsg_get_string(tb
[OCI_LINUX_NAMESPACE_TYPE
]));
1252 OCI_LINUX_UIDGIDMAP_CONTAINERID
,
1253 OCI_LINUX_UIDGIDMAP_HOSTID
,
1254 OCI_LINUX_UIDGIDMAP_SIZE
,
1255 __OCI_LINUX_UIDGIDMAP_MAX
,
1258 static const struct blobmsg_policy oci_linux_uidgidmap_policy
[] = {
1259 [OCI_LINUX_UIDGIDMAP_CONTAINERID
] = { "containerID", BLOBMSG_TYPE_INT32
},
1260 [OCI_LINUX_UIDGIDMAP_HOSTID
] = { "hostID", BLOBMSG_TYPE_INT32
},
1261 [OCI_LINUX_UIDGIDMAP_SIZE
] = { "size", BLOBMSG_TYPE_INT32
},
1264 static int parseOCIuidgidmappings(struct blob_attr
*msg
, bool is_gidmap
)
1266 const char *map_format
= "%d %d %d\n";
1267 struct blob_attr
*tb
[__OCI_LINUX_UIDGIDMAP_MAX
];
1268 struct blob_attr
*cur
;
1272 unsigned int cnt
= 0;
1273 size_t totallen
= 0;
1275 /* count number of mappings */
1276 blobmsg_for_each_attr(cur
, msg
, rem
)
1282 /* allocate array for mappings */
1283 mappings
= calloc(1 + cnt
, sizeof(char*));
1287 mappings
[cnt
] = NULL
;
1290 blobmsg_for_each_attr(cur
, msg
, rem
) {
1291 blobmsg_parse(oci_linux_uidgidmap_policy
, __OCI_LINUX_UIDGIDMAP_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1293 if (!tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
] ||
1294 !tb
[OCI_LINUX_UIDGIDMAP_HOSTID
] ||
1295 !tb
[OCI_LINUX_UIDGIDMAP_SIZE
])
1298 /* write mapping line into allocated string */
1299 len
= asprintf(&mappings
[cnt
++], map_format
,
1300 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
]),
1301 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_HOSTID
]),
1302 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_SIZE
]));
1310 /* allocate combined mapping string */
1311 map
= calloc(1 + totallen
, sizeof(char));
1317 /* concatenate mapping strings into combined string */
1318 curstr
= mappings
[0];
1320 strcat(map
, curstr
);
1334 OCI_LINUX_RESOURCES
,
1337 OCI_LINUX_NAMESPACES
,
1338 OCI_LINUX_UIDMAPPINGS
,
1339 OCI_LINUX_GIDMAPPINGS
,
1340 OCI_LINUX_MASKEDPATHS
,
1341 OCI_LINUX_READONLYPATHS
,
1342 OCI_LINUX_ROOTFSPROPAGATION
,
1346 static const struct blobmsg_policy oci_linux_policy
[] = {
1347 [OCI_LINUX_RESOURCES
] = { "resources", BLOBMSG_TYPE_TABLE
},
1348 [OCI_LINUX_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_TABLE
},
1349 [OCI_LINUX_SYSCTL
] = { "sysctl", BLOBMSG_TYPE_TABLE
},
1350 [OCI_LINUX_NAMESPACES
] = { "namespaces", BLOBMSG_TYPE_ARRAY
},
1351 [OCI_LINUX_UIDMAPPINGS
] = { "uidMappings", BLOBMSG_TYPE_ARRAY
},
1352 [OCI_LINUX_GIDMAPPINGS
] = { "gidMappings", BLOBMSG_TYPE_ARRAY
},
1353 [OCI_LINUX_MASKEDPATHS
] = { "maskedPaths", BLOBMSG_TYPE_ARRAY
},
1354 [OCI_LINUX_READONLYPATHS
] = { "readonlyPaths", BLOBMSG_TYPE_ARRAY
},
1355 [OCI_LINUX_ROOTFSPROPAGATION
] = { "rootfsPropagation", BLOBMSG_TYPE_STRING
},
1358 static int parseOCIlinux(struct blob_attr
*msg
)
1360 struct blob_attr
*tb
[__OCI_LINUX_MAX
];
1361 struct blob_attr
*cur
;
1365 blobmsg_parse(oci_linux_policy
, __OCI_LINUX_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1367 if (tb
[OCI_LINUX_NAMESPACES
]) {
1368 blobmsg_for_each_attr(cur
, tb
[OCI_LINUX_NAMESPACES
], rem
) {
1369 res
= parseOCIlinuxns(cur
);
1375 if (tb
[OCI_LINUX_UIDMAPPINGS
]) {
1376 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 0);
1381 if (tb
[OCI_LINUX_GIDMAPPINGS
]) {
1382 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 1);
1387 if (tb
[OCI_LINUX_SECCOMP
]) {
1388 opts
.ociseccomp
= parseOCIlinuxseccomp(tb
[OCI_LINUX_SECCOMP
]);
1389 if (!opts
.ociseccomp
)
1407 static const struct blobmsg_policy oci_policy
[] = {
1408 [OCI_VERSION
] = { "ociVersion", BLOBMSG_TYPE_STRING
},
1409 [OCI_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
1410 [OCI_PROCESS
] = { "process", BLOBMSG_TYPE_TABLE
},
1411 [OCI_ROOT
] = { "root", BLOBMSG_TYPE_TABLE
},
1412 [OCI_MOUNTS
] = { "mounts", BLOBMSG_TYPE_ARRAY
},
1413 [OCI_HOOKS
] = { "hooks", BLOBMSG_TYPE_TABLE
},
1414 [OCI_LINUX
] = { "linux", BLOBMSG_TYPE_TABLE
},
1417 static int parseOCI(const char *jsonfile
)
1419 struct blob_attr
*tb
[__OCI_MAX
];
1420 struct blob_attr
*cur
;
1424 blob_buf_init(&ocibuf
, 0);
1425 if (!blobmsg_add_json_from_file(&ocibuf
, jsonfile
))
1428 blobmsg_parse(oci_policy
, __OCI_MAX
, tb
, blob_data(ocibuf
.head
), blob_len(ocibuf
.head
));
1430 if (!tb
[OCI_VERSION
])
1433 if (strncmp("1.0", blobmsg_get_string(tb
[OCI_VERSION
]), 3)) {
1434 ERROR("unsupported ociVersion %s\n", blobmsg_get_string(tb
[OCI_VERSION
]));
1438 if (tb
[OCI_HOSTNAME
])
1439 opts
.hostname
= strdup(blobmsg_get_string(tb
[OCI_HOSTNAME
]));
1441 if (!tb
[OCI_PROCESS
])
1444 if ((res
= parseOCIprocess(tb
[OCI_PROCESS
])))
1450 if ((res
= parseOCIroot(jsonfile
, tb
[OCI_ROOT
])))
1453 if (!tb
[OCI_MOUNTS
])
1456 blobmsg_for_each_attr(cur
, tb
[OCI_MOUNTS
], rem
)
1457 if ((res
= parseOCImount(cur
)))
1460 if (tb
[OCI_LINUX
] && (res
= parseOCIlinux(tb
[OCI_LINUX
])))
1463 if (tb
[OCI_HOOKS
] && (res
= parseOCIhooks(tb
[OCI_HOOKS
])))
1466 blob_buf_free(&ocibuf
);
1471 int main(int argc
, char **argv
)
1474 uid_t uid
= getuid();
1475 const char log
[] = "/dev/log";
1476 const char ubus
[] = "/var/run/ubus.sock";
1477 char *jsonfile
= NULL
;
1484 ERROR("not root, aborting: %m\n");
1485 return EXIT_FAILURE
;
1490 init_library_search();
1492 while ((ch
= getopt(argc
, argv
, OPT_ARGS
)) != -1) {
1495 debug
= atoi(optarg
);
1498 opts
.namespace |= CLONE_NEWNS
;
1502 opts
.namespace |= CLONE_NEWNS
;
1506 opts
.namespace |= CLONE_NEWUSER
;
1509 opts
.namespace |= CLONE_NEWCGROUP
;
1512 opts
.extroot
= strdup(optarg
);
1515 opts
.namespace |= CLONE_NEWNS
;
1519 opts
.seccomp
= optarg
;
1520 add_mount_bind(optarg
, 1, -1);
1523 opts
.capabilities
= optarg
;
1526 opts
.no_new_privs
= 1;
1532 opts
.namespace |= CLONE_NEWNET
;
1535 opts
.namespace |= CLONE_NEWUTS
;
1536 opts
.hostname
= strdup(optarg
);
1539 opts
.namespace |= CLONE_NEWNS
;
1540 add_path_and_deps(optarg
, 1, 0, 0);
1543 opts
.namespace |= CLONE_NEWNS
;
1544 add_path_and_deps(optarg
, 0, 0, 0);
1547 opts
.namespace |= CLONE_NEWNS
;
1548 add_mount_bind(ubus
, 0, -1);
1551 opts
.namespace |= CLONE_NEWNS
;
1552 add_mount_bind(log
, 0, -1);
1558 opts
.group
= optarg
;
1561 opts
.overlaydir
= optarg
;
1564 opts
.tmpoverlaysize
= optarg
;
1567 opts
.require_jail
= 1;
1573 asprintf(&jsonfile
, "%s/config.json", optarg
);
1579 opts
.namespace |= CLONE_NEWIPC
| CLONE_NEWPID
;
1583 ocires
= parseOCI(jsonfile
);
1586 ERROR("parsing of OCI JSON spec has failed: %s (%d)\n", strerror(ocires
), ocires
);
1591 if (opts
.tmpoverlaysize
&& strlen(opts
.tmpoverlaysize
) > 8) {
1592 ERROR("size parameter too long: \"%s\"\n", opts
.tmpoverlaysize
);
1596 /* no <binary> param found */
1597 if (!jsonfile
&& (argc
- optind
< 1)) {
1599 return EXIT_FAILURE
;
1601 if (!(opts
.namespace||opts
.capabilities
||opts
.seccomp
)) {
1602 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
1604 return EXIT_FAILURE
;
1606 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
1608 opts
.capabilities
!= 0 || opts
.capset
.apply
,
1609 opts
.seccomp
!= 0 || opts
.ociseccomp
!= 0);
1612 /* allocate NULL-terminated array for argv */
1613 opts
.jail_argv
= calloc(1 + argc
- optind
, sizeof(char**));
1614 if (!opts
.jail_argv
)
1615 return EXIT_FAILURE
;
1617 for (size_t s
= optind
; s
< argc
; s
++)
1618 opts
.jail_argv
[s
- optind
] = strdup(argv
[s
]);
1620 if (opts
.namespace & CLONE_NEWUSER
)
1621 get_jail_user(&opts
.pw_uid
, &opts
.pw_gid
, &opts
.gr_gid
);
1624 if (!opts
.extroot
) {
1625 if (opts
.namespace && add_path_and_deps(*opts
.jail_argv
, 1, -1, 0)) {
1626 ERROR("failed to load dependencies\n");
1631 if (opts
.namespace && opts
.seccomp
&& add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
1632 ERROR("failed to load libpreload-seccomp.so\n");
1634 if (opts
.require_jail
)
1639 prctl(PR_SET_NAME
, opts
.name
, NULL
, NULL
, NULL
);
1643 sigfillset(&sigmask
);
1644 for (i
= 0; i
< _NSIG
; i
++) {
1645 struct sigaction s
= { 0 };
1647 if (!sigismember(&sigmask
, i
))
1649 if ((i
== SIGCHLD
) || (i
== SIGPIPE
) || (i
== SIGSEGV
))
1652 s
.sa_handler
= jail_handle_signal
;
1653 sigaction(i
, &s
, NULL
);
1656 if (opts
.namespace) {
1657 if (opts
.namespace & CLONE_NEWNS
) {
1658 add_mount_bind("/dev/full", 0, -1);
1659 add_mount_bind("/dev/null", 0, -1);
1660 add_mount_bind("/dev/random", 0, -1);
1661 add_mount_bind("/dev/urandom", 0, -1);
1662 add_mount_bind("/dev/zero", 0, -1);
1663 add_mount_bind("/dev/ptmx", 0, -1);
1664 add_mount_bind("/dev/tty", 0, -1);
1666 if (!opts
.extroot
&& (opts
.user
|| opts
.group
)) {
1667 add_mount_bind("/etc/passwd", 0, -1);
1668 add_mount_bind("/etc/group", 0, -1);
1671 #if defined(__GLIBC__)
1673 add_mount_bind("/etc/nsswitch.conf", 0, -1);
1676 if (!(opts
.namespace & CLONE_NEWNET
)) {
1677 add_mount_bind("/etc/resolv.conf", 0, -1);
1679 char hostdir
[PATH_MAX
];
1681 snprintf(hostdir
, PATH_MAX
, "/tmp/resolv.conf-%s.d", opts
.name
);
1682 mkdir_p(hostdir
, 0755);
1683 add_mount(hostdir
, "/tmp/resolv.conf.d", NULL
, MS_BIND
| MS_NOEXEC
| MS_NOATIME
| MS_NOSUID
| MS_NODEV
| MS_RDONLY
, NULL
, -1);
1687 if (pipe(&pipes
[0]) < 0 || pipe(&pipes
[2]) < 0)
1690 jail_process
.pid
= clone(exec_jail
, child_stack
+ STACK_SIZE
, SIGCHLD
| opts
.namespace, &pipes
);
1692 jail_process
.pid
= fork();
1695 if (jail_process
.pid
> 0) {
1697 /* parent process */
1700 run_hooks(opts
.hooks
.createRuntime
);
1701 if (read(pipes
[0], sig_buf
, 1) < 1) {
1702 ERROR("can't read from child\n");
1706 if (opts
.namespace & CLONE_NEWUSER
) {
1707 if (write_setgroups(jail_process
.pid
, true)) {
1708 ERROR("can't write setgroups\n");
1712 bool has_gr
= (opts
.gr_gid
!= -1);
1713 if (opts
.pw_uid
!= -1) {
1714 write_single_uid_gid_map(jail_process
.pid
, 0, opts
.pw_uid
);
1715 write_single_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:opts
.pw_gid
);
1717 write_single_uid_gid_map(jail_process
.pid
, 0, 65534);
1718 write_single_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:65534);
1721 write_uid_gid_map(jail_process
.pid
, 0, opts
.uidmap
);
1723 write_uid_gid_map(jail_process
.pid
, 1, opts
.gidmap
);
1727 if (opts
.namespace & CLONE_NEWNET
) {
1729 ERROR("netns needs a named jail\n");
1732 netns_fd
= netns_open_pid(jail_process
.pid
);
1733 netns_updown(jail_process
.pid
, true);
1737 if (write(pipes
[3], sig_buf
, 1) < 0) {
1738 ERROR("can't write to child\n");
1742 run_hooks(opts
.hooks
.poststart
);
1743 uloop_process_add(&jail_process
);
1746 DEBUG("uloop interrupted, killing jail process\n");
1747 kill(jail_process
.pid
, SIGTERM
);
1748 uloop_timeout_set(&jail_process_timeout
, 1000);
1752 if (opts
.namespace & CLONE_NEWNET
) {
1753 setns(netns_fd
, CLONE_NEWNET
);
1754 netns_updown(getpid(), false);
1757 run_hooks(opts
.hooks
.poststop
);
1759 return jail_return_code
;
1760 } else if (jail_process
.pid
== 0) {
1761 /* fork child process */
1762 return exec_jail(NULL
);
1764 ERROR("failed to clone/fork: %m\n");
1765 return EXIT_FAILURE
;