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:"
73 struct sock_fprog
*ociseccomp
;
75 struct jail_capset capset
;
84 struct sysctl_val
**sysctl
;
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
;
104 static void free_hooklist(struct hook_execvpe
**hooklist
)
106 struct hook_execvpe
*cur
;
131 static void free_sysctl(void) {
132 struct sysctl_val
*cur
;
143 static void free_opts(bool child
) {
146 /* we need to keep argv, envp and seccomp filter in child */
148 if (opts
.ociseccomp
) {
149 free(opts
.ociseccomp
->filter
);
150 free(opts
.ociseccomp
);
153 tmp
= opts
.jail_argv
;
157 free(opts
.jail_argv
);
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
);
179 static struct blob_buf ocibuf
;
181 extern int pivot_root(const char *new_root
, const char *put_old
);
185 static char child_stack
[STACK_SIZE
];
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";
194 if (asprintf(&upperdir
, "%s%s", overlaydir
, "/upper") < 0)
197 if (asprintf(&workdir
, "%s%s", overlaydir
, "/work") < 0)
200 if (asprintf(&optsstr
, mountoptsformat
, jail_root
, upperdir
, workdir
) < 0)
203 if (mkdir_p(upperdir
, 0755) || mkdir_p(workdir
, 0755))
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
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.
215 if (asprintf(&upperetc
, "%s/etc", upperdir
) < 0)
218 if (mkdir_p(upperetc
, 0755))
219 goto upper_etc_printf
;
221 if (asprintf(&upperresolvconf
, "%s/resolv.conf", upperetc
) < 0)
222 goto upper_etc_printf
;
224 fd
= creat(upperresolvconf
, 0644);
226 ERROR("creat(%s) failed: %m\n", upperresolvconf
);
227 goto upper_resolvconf_printf
;
231 DEBUG("mount -t overlay %s %s (%s)\n", jail_root
, jail_root
, optsstr
);
233 if (mount(jail_root
, jail_root
, "overlay", MS_NOATIME
, optsstr
))
238 upper_resolvconf_printf
:
239 free(upperresolvconf
);
252 static void pass_console(int console_fd
)
254 struct ubus_context
*ctx
= ubus_connect(NULL
);
255 static struct blob_buf req
;
261 blob_buf_init(&req
, 0);
262 blobmsg_add_string(&req
, "name", opts
.name
);
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");
274 static int create_dev_console(const char *jail_root
)
277 char dev_console_path
[PATH_MAX
];
278 int slave_console_fd
;
280 /* Open UNIX/98 virtual console */
281 console_fd
= posix_openpt(O_RDWR
| O_NOCTTY
);
282 if (console_fd
== -1)
285 console_fname
= ptsname(console_fd
);
286 DEBUG("got console fd %d and PTS client name %s\n", console_fd
, console_fname
);
291 unlockpt(console_fd
);
293 /* pass PTY master to procd */
294 pass_console(console_fd
);
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));
300 if (mount(console_fname
, dev_console_path
, NULL
, MS_BIND
, NULL
))
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
);
310 INFO("using guest console %s\n", console_fname
);
319 static int hook_running
= 0;
320 static int hook_return_code
= 0;
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
,
327 static void hook_process_handler(struct uloop_process
*c
, int ret
)
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
);
334 hook_return_code
= WTERMSIG(ret
);
335 DEBUG("hook (%d) exited with signal: %d\n", c
->pid
, hook_return_code
);
341 static struct uloop_process hook_process
= {
342 .cb
= hook_process_handler
,
345 static void hook_process_timeout_cb(struct uloop_timeout
*t
)
347 DEBUG("hook process failed to stop, sending SIGKILL\n");
348 kill(hook_process
.pid
, SIGKILL
);
351 static int run_hook(struct hook_execvpe
*hook
)
355 DEBUG("executing hook %s\n", hook
->file
);
357 if (stat(hook
->file
, &s
))
360 if (!((unsigned long)s
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)))
363 if (!((unsigned long)s
.st_mode
& (S_IRUSR
| S_IRGRP
| S_IROTH
)))
369 hook_process
.pid
= fork();
370 if (hook_process
.pid
> 0) {
372 uloop_process_add(&hook_process
);
374 if (hook
->timeout
> 0)
375 uloop_timeout_set(&hook_process_timeout
, 1000 * hook
->timeout
);
379 DEBUG("uloop interrupted, killing hook process\n");
380 kill(hook_process
.pid
, SIGTERM
);
381 uloop_timeout_set(&hook_process_timeout
, 1000);
386 waitpid(hook_process
.pid
, NULL
, WCONTINUED
);
388 return hook_return_code
;
389 } else if (hook_process
.pid
== 0) {
391 execvpe(hook
->file
, hook
->argv
, hook
->envp
);
401 static int run_hooks(struct hook_execvpe
**hooklist
)
403 struct hook_execvpe
**cur
;
407 return 0; /* Nothing to do */
412 res
= run_hook(*cur
);
414 DEBUG(" error running hook %s\n", (*cur
)->file
);
416 DEBUG(" success running hook %s\n", (*cur
)->file
);
424 static int apply_sysctl(const char *jail_root
)
426 struct sysctl_val
**cur
;
427 char *procdir
, *fname
;
433 asprintf(&procdir
, "%s/proc", jail_root
);
437 mkdir(procdir
, 0700);
438 if (mount("proc", procdir
, "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0))
444 asprintf(&fname
, "%s/sys/%s", procdir
, (*cur
)->entry
);
448 DEBUG("sysctl: writing '%s' to %s\n", (*cur
)->value
, fname
);
450 f
= open(fname
, O_WRONLY
);
452 ERROR("sysctl: can't open %s\n", fname
);
455 write(f
, (*cur
)->value
, strlen((*cur
)->value
));
468 static int build_jail_fs(void)
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
;
476 if (mkdtemp(jail_root
) == NULL
) {
477 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
481 if (apply_sysctl(jail_root
)) {
482 ERROR("failed to apply sysctl values\n");
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");
493 if (mount(opts
.extroot
, jail_root
, NULL
, MS_BIND
, NULL
)) {
494 ERROR("extroot mount failed %m\n");
498 if (mount("tmpfs", jail_root
, "tmpfs", MS_NOATIME
, "mode=0755")) {
499 ERROR("tmpfs mount failed %m\n");
504 if (opts
.tmpoverlaysize
) {
505 char mountoptsstr
[] = "mode=0755,size=XXXXXXXX";
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
);
513 if (mount("tmpfs", tmpovdir
, "tmpfs", MS_NOATIME
,
515 ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts
.tmpoverlaysize
);
518 overlaydir
= tmpovdir
;
522 overlaydir
= opts
.overlaydir
;
525 mount_overlay(jail_root
, overlaydir
);
527 if (chdir(jail_root
)) {
528 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root
);
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"))
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
))
543 create_dev_console(jail_root
);
545 if (mount_all(jail_root
)) {
546 ERROR("mount_all() failed\n");
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
];
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
);
560 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink
);
563 run_hooks(opts
.hooks
.createContainer
);
565 char dirbuf
[sizeof(jail_root
) + 4];
566 snprintf(dirbuf
, sizeof(dirbuf
), "%s/old", jail_root
);
569 if (pivot_root(jail_root
, dirbuf
) == -1) {
570 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root
, dirbuf
);
574 ERROR("chdir(/) (after pivot_root) failed: %m\n");
578 snprintf(dirbuf
, sizeof(dirbuf
), "/old%s", jail_root
);
579 umount2(dirbuf
, MNT_DETACH
);
581 if (opts
.tmpoverlaysize
) {
582 char tmpdirbuf
[sizeof(tmpovdir
) + 4];
583 snprintf(tmpdirbuf
, sizeof(tmpdirbuf
), "/old%s", tmpovdir
);
584 umount2(tmpdirbuf
, MNT_DETACH
);
588 umount2("/old", MNT_DETACH
);
592 mkdir("/proc", 0755);
593 mount("proc", "/proc", "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
595 * make /proc/sys read-only while keeping read-write to
596 * /proc/sys/net if CLONE_NEWNET is set.
598 if (opts
.namespace & CLONE_NEWNET
)
599 mount("/proc/sys/net", "/proc/self/net", NULL
, MS_BIND
, 0);
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);
605 if (opts
.namespace & CLONE_NEWNET
)
606 mount("/proc/self/net", "/proc/sys/net", NULL
, MS_MOVE
, 0);
610 mount("sysfs", "/sys", "sysfs", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RDONLY
, 0);
613 mount(NULL
, "/", NULL
, MS_REMOUNT
| MS_BIND
| MS_RDONLY
, 0);
618 static int write_uid_gid_map(pid_t child_pid
, bool gidmap
, char *mapstr
)
623 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
624 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
627 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
630 if (dprintf(map_file
, "%s", mapstr
)) {
640 static int write_single_uid_gid_map(pid_t child_pid
, bool gidmap
, int id
)
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)
649 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
652 if (dprintf(map_file
, map_format
, 0, id
, 1) == -1) {
661 static int write_setgroups(pid_t child_pid
, bool allow
)
664 char setgroups_path
[64];
666 if (snprintf(setgroups_path
, sizeof(setgroups_path
), "/proc/%d/setgroups",
671 if ((setgroups_file
= open(setgroups_path
, O_WRONLY
)) == -1) {
675 if (dprintf(setgroups_file
, "%s", allow
?"allow":"deny") == -1) {
676 close(setgroups_file
);
680 close(setgroups_file
);
684 static void get_jail_user(int *user
, int *user_gid
, int *gr_gid
)
686 struct passwd
*p
= NULL
;
687 struct group
*g
= NULL
;
690 p
= getpwnam(opts
.user
);
692 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
693 opts
.user
, errno
, strerror(errno
));
697 *user_gid
= p
->pw_gid
;
704 g
= getgrnam(opts
.group
);
706 ERROR("failed to get gid for group %s: %m\n", opts
.group
);
715 static void set_jail_user(int pw_uid
, int user_gid
, int gr_gid
)
717 if (opts
.user
&& (user_gid
!= -1) && initgroups(opts
.user
, user_gid
)) {
718 ERROR("failed to initgroups() for user %s: %m\n", opts
.user
);
722 if ((gr_gid
!= -1) && setregid(gr_gid
, gr_gid
)) {
723 ERROR("failed to set group id %d: %m\n", gr_gid
);
727 if ((pw_uid
!= -1) && setreuid(pw_uid
, pw_uid
)) {
728 ERROR("failed to set user id %d: %m\n", pw_uid
);
734 static char** build_envp(const char *seccomp
, char **ocienvp
)
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");
746 if (seccomp
&& !preload_lib
) {
747 ERROR("failed to add preload-lib to env\n");
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
;
757 envp
[count
++] = container_var
;
760 envp
[count
++] = debug_var
;
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
);
773 static void usage(void)
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");
810 static int exec_jail(void *pipes_ptr
)
812 int *pipes
= (int*)pipes_ptr
;
814 int pw_uid
, pw_gid
, gr_gid
;
820 if (write(pipes
[1], buf
, 1) < 1) {
821 ERROR("can't write to parent\n");
824 if (read(pipes
[2], buf
, 1) < 1) {
825 ERROR("can't read from parent\n");
829 ERROR("parent had an error, child exiting\n");
836 if (opts
.namespace & CLONE_NEWUSER
) {
837 if (setregid(0, 0) < 0) {
841 if (setreuid(0, 0) < 0) {
845 if (setgroups(0, NULL
) < 0) {
846 ERROR("setgroups\n");
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
);
857 if ((opts
.namespace & CLONE_NEWNS
) && build_jail_fs()) {
858 ERROR("failed to build jail fs\n");
861 run_hooks(opts
.hooks
.startContainer
);
863 if (applyOCIcapabilities(opts
.capset
))
866 if (opts
.capabilities
&& drop_capabilities(opts
.capabilities
))
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");
874 if (!(opts
.namespace & CLONE_NEWUSER
)) {
875 get_jail_user(&pw_uid
, &pw_gid
, &gr_gid
);
877 set_jail_user(opts
.pw_uid
?:pw_uid
, opts
.pw_gid
?:pw_gid
, opts
.gr_gid
?:gr_gid
);
880 char **envp
= build_envp(opts
.seccomp
, opts
.envp
);
884 if (opts
.cwd
&& chdir(opts
.cwd
))
887 if (opts
.ociseccomp
&& applyOCIlinuxseccomp(opts
.ociseccomp
))
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
);
896 execve(*opts
.jail_argv
, opts
.jail_argv
, envp
);
898 /* we get there only if execve fails */
899 ERROR("failed to execve %s: %m\n", *opts
.jail_argv
);
903 static int jail_running
= 0;
904 static int jail_return_code
= 0;
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
,
911 static void jail_process_handler(struct uloop_process
*c
, int ret
)
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
);
918 jail_return_code
= WTERMSIG(ret
);
919 INFO("jail (%d) exited with signal: %d\n", c
->pid
, jail_return_code
);
925 static struct uloop_process jail_process
= {
926 .cb
= jail_process_handler
,
929 static void jail_process_timeout_cb(struct uloop_timeout
*t
)
931 DEBUG("jail process failed to stop, sending SIGKILL\n");
932 kill(jail_process
.pid
, SIGKILL
);
935 static void jail_handle_signal(int signo
)
938 DEBUG("forwarding signal %d to the hook process\n", signo
);
939 kill(hook_process
.pid
, signo
);
943 DEBUG("forwarding signal %d to the jailed process\n", signo
);
944 kill(jail_process
.pid
, signo
);
948 static int netns_open_pid(const pid_t target_ns
)
950 char pid_net_path
[PATH_MAX
];
952 snprintf(pid_net_path
, sizeof(pid_net_path
), "/proc/%u/ns/net", target_ns
);
954 return open(pid_net_path
, O_RDONLY
);
957 static void netns_updown(pid_t pid
, bool start
)
959 struct ubus_context
*ctx
= ubus_connect(NULL
);
960 static struct blob_buf req
;
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
);
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");
979 static int parseOCIenvarray(struct blob_attr
*msg
, char ***envp
)
981 struct blob_attr
*cur
;
984 blobmsg_for_each_attr(cur
, msg
, rem
)
988 *envp
= calloc(1 + sz
, sizeof(char*));
997 blobmsg_for_each_attr(cur
, msg
, rem
)
998 (*envp
)[sz
++] = strdup(blobmsg_get_string(cur
));
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
},
1017 static int parseOCIroot(const char *jsonfile
, struct blob_attr
*msg
)
1019 static char rootpath
[PATH_MAX
] = { 0 };
1020 struct blob_attr
*tb
[__OCI_ROOT_MAX
];
1023 blobmsg_parse(oci_root_policy
, __OCI_ROOT_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1025 if (!tb
[OCI_ROOT_PATH
])
1028 strncpy(rootpath
, jsonfile
, PATH_MAX
);
1029 cur
= strrchr(rootpath
, '/');
1035 strncat(rootpath
, blobmsg_get_string(tb
[OCI_ROOT_PATH
]), PATH_MAX
- (strlen(rootpath
) + 1));
1037 opts
.extroot
= rootpath
;
1039 opts
.ronly
= blobmsg_get_bool(tb
[OCI_ROOT_READONLY
]);
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
},
1061 static int parseOCIhook(struct hook_execvpe
***hooklist
, struct blob_attr
*msg
)
1063 struct blob_attr
*tb
[__OCI_HOOK_MAX
];
1064 struct blob_attr
*cur
;
1068 blobmsg_for_each_attr(cur
, msg
, rem
)
1074 *hooklist
= calloc(idx
+ 1, sizeof(struct hook_execvpe
*));
1080 blobmsg_for_each_attr(cur
, msg
, rem
) {
1081 blobmsg_parse(oci_hook_policy
, __OCI_HOOK_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1083 if (!tb
[OCI_HOOK_PATH
]) {
1088 (*hooklist
)[idx
] = malloc(sizeof(struct hook_execvpe
));
1089 if (tb
[OCI_HOOK_ARGS
]) {
1090 ret
= parseOCIenvarray(tb
[OCI_HOOK_ARGS
], &((*hooklist
)[idx
]->argv
));
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
;
1100 if (tb
[OCI_HOOK_ENV
]) {
1101 ret
= parseOCIenvarray(tb
[OCI_HOOK_ENV
], &((*hooklist
)[idx
]->envp
));
1106 if (tb
[OCI_HOOK_TIMEOUT
])
1107 (*hooklist
)[idx
]->timeout
= blobmsg_get_u32(tb
[OCI_HOOK_TIMEOUT
]);
1109 (*hooklist
)[idx
]->file
= strdup(blobmsg_get_string(tb
[OCI_HOOK_PATH
]));
1114 (*hooklist
)[idx
] = NULL
;
1116 DEBUG("added %d hooks\n", idx
);
1121 free_hooklist(*hooklist
);
1130 OCI_HOOKS_CREATERUNTIME
,
1131 OCI_HOOKS_CREATECONTAINER
,
1132 OCI_HOOKS_STARTCONTAINER
,
1133 OCI_HOOKS_POSTSTART
,
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
},
1147 static int parseOCIhooks(struct blob_attr
*msg
)
1149 struct blob_attr
*tb
[__OCI_HOOKS_MAX
];
1152 blobmsg_parse(oci_hooks_policy
, __OCI_HOOKS_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1154 if (tb
[OCI_HOOKS_PRESTART
])
1155 INFO("warning: ignoring deprecated prestart hook\n");
1157 if (tb
[OCI_HOOKS_CREATERUNTIME
]) {
1158 ret
= parseOCIhook(&opts
.hooks
.createRuntime
, tb
[OCI_HOOKS_CREATERUNTIME
]);
1163 if (tb
[OCI_HOOKS_CREATECONTAINER
]) {
1164 ret
= parseOCIhook(&opts
.hooks
.createContainer
, tb
[OCI_HOOKS_CREATECONTAINER
]);
1166 goto out_createruntime
;
1169 if (tb
[OCI_HOOKS_STARTCONTAINER
]) {
1170 ret
= parseOCIhook(&opts
.hooks
.startContainer
, tb
[OCI_HOOKS_STARTCONTAINER
]);
1172 goto out_createcontainer
;
1175 if (tb
[OCI_HOOKS_POSTSTART
]) {
1176 ret
= parseOCIhook(&opts
.hooks
.poststart
, tb
[OCI_HOOKS_POSTSTART
]);
1178 goto out_startcontainer
;
1181 if (tb
[OCI_HOOKS_POSTSTOP
]) {
1182 ret
= parseOCIhook(&opts
.hooks
.poststop
, tb
[OCI_HOOKS_POSTSTOP
]);
1190 free_hooklist(opts
.hooks
.poststart
);
1192 free_hooklist(opts
.hooks
.startContainer
);
1193 out_createcontainer
:
1194 free_hooklist(opts
.hooks
.createContainer
);
1196 free_hooklist(opts
.hooks
.createRuntime
);
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
,
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
},
1217 static int parseOCIprocessuser(struct blob_attr
*msg
) {
1218 struct blob_attr
*tb
[__OCI_PROCESS_USER_MAX
];
1220 blobmsg_parse(oci_process_user_policy
, __OCI_PROCESS_USER_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1222 if (tb
[OCI_PROCESS_USER_UID
])
1223 opts
.pw_uid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_UID
]);
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
]);
1230 /* ToDo: umask, additional GIDs */
1237 OCI_PROCESS_CAPABILITIES
,
1240 OCI_PROCESS_NONEWPRIVILEGES
,
1241 OCI_PROCESS_RLIMITS
,
1242 OCI_PROCESS_TERMINAL
,
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
},
1259 static int parseOCIprocess(struct blob_attr
*msg
)
1261 struct blob_attr
*tb
[__OCI_PROCESS_MAX
];
1264 blobmsg_parse(oci_process_policy
, __OCI_PROCESS_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1266 if (!tb
[OCI_PROCESS_ARGS
])
1269 res
= parseOCIenvarray(tb
[OCI_PROCESS_ARGS
], &opts
.jail_argv
);
1273 opts
.console
= blobmsg_get_bool(tb
[OCI_PROCESS_TERMINAL
]);
1274 opts
.no_new_privs
= blobmsg_get_bool(tb
[OCI_PROCESS_NONEWPRIVILEGES
]);
1276 if (tb
[OCI_PROCESS_CWD
])
1277 opts
.cwd
= strdup(blobmsg_get_string(tb
[OCI_PROCESS_CWD
]));
1279 if (tb
[OCI_PROCESS_ENV
]) {
1280 res
= parseOCIenvarray(tb
[OCI_PROCESS_ENV
], &opts
.envp
);
1285 if (tb
[OCI_PROCESS_USER
] && (res
= parseOCIprocessuser(tb
[OCI_PROCESS_USER
])))
1288 if (tb
[OCI_PROCESS_CAPABILITIES
] &&
1289 (res
= parseOCIcapabilities(&opts
.capset
, tb
[OCI_PROCESS_CAPABILITIES
])))
1298 OCI_LINUX_NAMESPACE_TYPE
,
1299 OCI_LINUX_NAMESPACE_PATH
,
1300 __OCI_LINUX_NAMESPACE_MAX
,
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
},
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
))
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
;
1327 static int parseOCIlinuxns(struct blob_attr
*msg
)
1329 struct blob_attr
*tb
[__OCI_LINUX_NAMESPACE_MAX
];
1331 blobmsg_parse(oci_linux_namespace_policy
, __OCI_LINUX_NAMESPACE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1333 if (!tb
[OCI_LINUX_NAMESPACE_TYPE
])
1336 if (tb
[OCI_LINUX_NAMESPACE_PATH
])
1337 return ENOTSUP
; /* ToDo */
1339 opts
.namespace |= resolve_nstype(blobmsg_get_string(tb
[OCI_LINUX_NAMESPACE_TYPE
]));
1346 OCI_LINUX_UIDGIDMAP_CONTAINERID
,
1347 OCI_LINUX_UIDGIDMAP_HOSTID
,
1348 OCI_LINUX_UIDGIDMAP_SIZE
,
1349 __OCI_LINUX_UIDGIDMAP_MAX
,
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
},
1358 static int parseOCIuidgidmappings(struct blob_attr
*msg
, bool is_gidmap
)
1360 const char *map_format
= "%d %d %d\n";
1361 struct blob_attr
*tb
[__OCI_LINUX_UIDGIDMAP_MAX
];
1362 struct blob_attr
*cur
;
1366 unsigned int cnt
= 0;
1367 size_t totallen
= 0;
1369 /* count number of mappings */
1370 blobmsg_for_each_attr(cur
, msg
, rem
)
1376 /* allocate array for mappings */
1377 mappings
= calloc(1 + cnt
, sizeof(char*));
1381 mappings
[cnt
] = NULL
;
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
));
1387 if (!tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
] ||
1388 !tb
[OCI_LINUX_UIDGIDMAP_HOSTID
] ||
1389 !tb
[OCI_LINUX_UIDGIDMAP_SIZE
])
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
]));
1404 /* allocate combined mapping string */
1405 map
= calloc(1 + totallen
, sizeof(char));
1411 /* concatenate mapping strings into combined string */
1412 curstr
= mappings
[0];
1414 strcat(map
, curstr
);
1428 OCI_LINUX_RESOURCES
,
1431 OCI_LINUX_NAMESPACES
,
1432 OCI_LINUX_UIDMAPPINGS
,
1433 OCI_LINUX_GIDMAPPINGS
,
1434 OCI_LINUX_MASKEDPATHS
,
1435 OCI_LINUX_READONLYPATHS
,
1436 OCI_LINUX_ROOTFSPROPAGATION
,
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
},
1452 static int parseOCIsysctl(struct blob_attr
*msg
)
1454 struct blob_attr
*cur
;
1459 blobmsg_for_each_attr(cur
, msg
, rem
) {
1460 if (!blobmsg_name(cur
) || !blobmsg_get_string(cur
))
1469 opts
.sysctl
= calloc(cnt
+ 1, sizeof(struct sysctl_val
*));
1474 blobmsg_for_each_attr(cur
, msg
, rem
) {
1475 opts
.sysctl
[cnt
] = malloc(sizeof(struct sysctl_val
));
1476 if (!opts
.sysctl
[cnt
])
1479 /* replace '.' with '/' in entry name */
1480 tc
= tmp
= strdup(blobmsg_name(cur
));
1481 while ((tc
= strchr(tc
, '.')))
1484 opts
.sysctl
[cnt
]->value
= strdup(blobmsg_get_string(cur
));
1485 opts
.sysctl
[cnt
]->entry
= tmp
;
1490 opts
.sysctl
[cnt
] = NULL
;
1495 static int parseOCIlinux(struct blob_attr
*msg
)
1497 struct blob_attr
*tb
[__OCI_LINUX_MAX
];
1498 struct blob_attr
*cur
;
1502 blobmsg_parse(oci_linux_policy
, __OCI_LINUX_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1504 if (tb
[OCI_LINUX_NAMESPACES
]) {
1505 blobmsg_for_each_attr(cur
, tb
[OCI_LINUX_NAMESPACES
], rem
) {
1506 res
= parseOCIlinuxns(cur
);
1512 if (tb
[OCI_LINUX_UIDMAPPINGS
]) {
1513 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 0);
1518 if (tb
[OCI_LINUX_GIDMAPPINGS
]) {
1519 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 1);
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);
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);
1540 if (tb
[OCI_LINUX_SYSCTL
]) {
1541 res
= parseOCIsysctl(tb
[OCI_LINUX_SYSCTL
]);
1546 if (tb
[OCI_LINUX_SECCOMP
]) {
1547 opts
.ociseccomp
= parseOCIlinuxseccomp(tb
[OCI_LINUX_SECCOMP
]);
1548 if (!opts
.ociseccomp
)
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
},
1576 static int parseOCI(const char *jsonfile
)
1578 struct blob_attr
*tb
[__OCI_MAX
];
1579 struct blob_attr
*cur
;
1583 blob_buf_init(&ocibuf
, 0);
1584 if (!blobmsg_add_json_from_file(&ocibuf
, jsonfile
))
1587 blobmsg_parse(oci_policy
, __OCI_MAX
, tb
, blob_data(ocibuf
.head
), blob_len(ocibuf
.head
));
1589 if (!tb
[OCI_VERSION
])
1592 if (strncmp("1.0", blobmsg_get_string(tb
[OCI_VERSION
]), 3)) {
1593 ERROR("unsupported ociVersion %s\n", blobmsg_get_string(tb
[OCI_VERSION
]));
1597 if (tb
[OCI_HOSTNAME
])
1598 opts
.hostname
= strdup(blobmsg_get_string(tb
[OCI_HOSTNAME
]));
1600 if (!tb
[OCI_PROCESS
])
1603 if ((res
= parseOCIprocess(tb
[OCI_PROCESS
])))
1609 if ((res
= parseOCIroot(jsonfile
, tb
[OCI_ROOT
])))
1612 if (!tb
[OCI_MOUNTS
])
1615 blobmsg_for_each_attr(cur
, tb
[OCI_MOUNTS
], rem
)
1616 if ((res
= parseOCImount(cur
)))
1619 if (tb
[OCI_LINUX
] && (res
= parseOCIlinux(tb
[OCI_LINUX
])))
1622 if (tb
[OCI_HOOKS
] && (res
= parseOCIhooks(tb
[OCI_HOOKS
])))
1625 blob_buf_free(&ocibuf
);
1630 int main(int argc
, char **argv
)
1633 uid_t uid
= getuid();
1634 const char log
[] = "/dev/log";
1635 const char ubus
[] = "/var/run/ubus.sock";
1636 char *jsonfile
= NULL
;
1643 ERROR("not root, aborting: %m\n");
1644 return EXIT_FAILURE
;
1649 init_library_search();
1651 while ((ch
= getopt(argc
, argv
, OPT_ARGS
)) != -1) {
1654 debug
= atoi(optarg
);
1657 opts
.namespace |= CLONE_NEWNS
;
1661 opts
.namespace |= CLONE_NEWNS
;
1665 opts
.namespace |= CLONE_NEWUSER
;
1668 opts
.namespace |= CLONE_NEWCGROUP
;
1671 opts
.extroot
= strdup(optarg
);
1674 opts
.namespace |= CLONE_NEWNS
;
1678 opts
.seccomp
= optarg
;
1679 add_mount_bind(optarg
, 1, -1);
1682 opts
.capabilities
= optarg
;
1685 opts
.no_new_privs
= 1;
1691 opts
.namespace |= CLONE_NEWNET
;
1694 opts
.namespace |= CLONE_NEWUTS
;
1695 opts
.hostname
= strdup(optarg
);
1698 opts
.namespace |= CLONE_NEWNS
;
1699 add_path_and_deps(optarg
, 1, 0, 0);
1702 opts
.namespace |= CLONE_NEWNS
;
1703 add_path_and_deps(optarg
, 0, 0, 0);
1706 opts
.namespace |= CLONE_NEWNS
;
1707 add_mount_bind(ubus
, 0, -1);
1710 opts
.namespace |= CLONE_NEWNS
;
1711 add_mount_bind(log
, 0, -1);
1717 opts
.group
= optarg
;
1720 opts
.overlaydir
= optarg
;
1723 opts
.tmpoverlaysize
= optarg
;
1726 opts
.require_jail
= 1;
1732 asprintf(&jsonfile
, "%s/config.json", optarg
);
1738 opts
.namespace |= CLONE_NEWIPC
| CLONE_NEWPID
;
1742 ocires
= parseOCI(jsonfile
);
1745 ERROR("parsing of OCI JSON spec has failed: %s (%d)\n", strerror(ocires
), ocires
);
1750 if (opts
.tmpoverlaysize
&& strlen(opts
.tmpoverlaysize
) > 8) {
1751 ERROR("size parameter too long: \"%s\"\n", opts
.tmpoverlaysize
);
1755 /* no <binary> param found */
1756 if (!jsonfile
&& (argc
- optind
< 1)) {
1758 return EXIT_FAILURE
;
1760 if (!(opts
.namespace||opts
.capabilities
||opts
.seccomp
)) {
1761 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
1763 return EXIT_FAILURE
;
1765 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
1767 opts
.capabilities
!= 0 || opts
.capset
.apply
,
1768 opts
.seccomp
!= 0 || opts
.ociseccomp
!= 0);
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
;
1776 for (size_t s
= optind
; s
< argc
; s
++)
1777 opts
.jail_argv
[s
- optind
] = strdup(argv
[s
]);
1779 if (opts
.namespace & CLONE_NEWUSER
)
1780 get_jail_user(&opts
.pw_uid
, &opts
.pw_gid
, &opts
.gr_gid
);
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");
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");
1793 if (opts
.require_jail
)
1798 prctl(PR_SET_NAME
, opts
.name
, NULL
, NULL
, NULL
);
1800 sigfillset(&sigmask
);
1801 for (i
= 0; i
< _NSIG
; i
++) {
1802 struct sigaction s
= { 0 };
1804 if (!sigismember(&sigmask
, i
))
1806 if ((i
== SIGCHLD
) || (i
== SIGPIPE
) || (i
== SIGSEGV
))
1809 s
.sa_handler
= jail_handle_signal
;
1810 sigaction(i
, &s
, NULL
);
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);
1823 if (!opts
.extroot
&& (opts
.user
|| opts
.group
)) {
1824 add_mount_bind("/etc/passwd", 0, -1);
1825 add_mount_bind("/etc/group", 0, -1);
1828 #if defined(__GLIBC__)
1830 add_mount_bind("/etc/nsswitch.conf", 0, -1);
1833 if (!(opts
.namespace & CLONE_NEWNET
)) {
1834 add_mount_bind("/etc/resolv.conf", 0, -1);
1836 char hostdir
[PATH_MAX
];
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);
1844 if (pipe(&pipes
[0]) < 0 || pipe(&pipes
[2]) < 0)
1847 jail_process
.pid
= clone(exec_jail
, child_stack
+ STACK_SIZE
, SIGCHLD
| opts
.namespace, &pipes
);
1849 jail_process
.pid
= fork();
1852 if (jail_process
.pid
> 0) {
1855 /* parent process */
1858 run_hooks(opts
.hooks
.createRuntime
);
1859 if (read(pipes
[0], sig_buf
, 1) < 1) {
1860 ERROR("can't read from child\n");
1864 if (opts
.namespace & CLONE_NEWUSER
) {
1865 if (write_setgroups(jail_process
.pid
, true)) {
1866 ERROR("can't write setgroups\n");
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
);
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);
1879 write_uid_gid_map(jail_process
.pid
, 0, opts
.uidmap
);
1881 write_uid_gid_map(jail_process
.pid
, 1, opts
.gidmap
);
1885 if (opts
.namespace & CLONE_NEWNET
) {
1887 ERROR("netns needs a named jail\n");
1890 netns_fd
= netns_open_pid(jail_process
.pid
);
1891 netns_updown(jail_process
.pid
, true);
1895 if (write(pipes
[3], sig_buf
, 1) < 0) {
1896 ERROR("can't write to child\n");
1900 run_hooks(opts
.hooks
.poststart
);
1903 uloop_process_add(&jail_process
);
1906 DEBUG("uloop interrupted, killing jail process\n");
1907 kill(jail_process
.pid
, SIGTERM
);
1908 uloop_timeout_set(&jail_process_timeout
, 1000);
1912 if (opts
.namespace & CLONE_NEWNET
) {
1913 setns(netns_fd
, CLONE_NEWNET
);
1914 netns_updown(getpid(), false);
1917 run_hooks(opts
.hooks
.poststop
);
1919 return jail_return_code
;
1920 } else if (jail_process
.pid
== 0) {
1921 /* fork child process */
1922 return exec_jail(NULL
);
1924 ERROR("failed to clone/fork: %m\n");
1925 return EXIT_FAILURE
;