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>
30 #include <linux/limits.h>
31 #include <linux/filter.h>
34 #include "capabilities.h"
39 #include "seccomp-oci.h"
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>
49 #ifndef CLONE_NEWCGROUP
50 #define CLONE_NEWCGROUP 0x02000000
53 #define STACK_SIZE (1024 * 1024)
54 #define OPT_ARGS "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:EyJ:"
69 struct sock_fprog
*ociseccomp
;
71 struct jail_capset capset
;
91 struct hook_execvpe
*createRuntime
;
92 struct hook_execvpe
*createContainer
;
93 struct hook_execvpe
*startContainer
;
94 struct hook_execvpe
*poststart
;
95 struct hook_execvpe
*poststop
;
99 static struct blob_buf ocibuf
;
101 extern int pivot_root(const char *new_root
, const char *put_old
);
105 static char child_stack
[STACK_SIZE
];
109 static int mkdir_p(char *dir
, mode_t mask
)
111 char *l
= strrchr(dir
, '/');
119 if (mkdir_p(dir
, mask
))
124 ret
= mkdir(dir
, mask
);
125 if (ret
&& errno
== EEXIST
)
129 ERROR("mkdir(%s, %d) failed: %m\n", dir
, mask
);
134 static int _mount_bind(const char *root
, const char *path
, const char *target
, int readonly
, int strict
, int error
)
139 int remount_flags
= MS_BIND
| MS_REMOUNT
;
141 if (stat(path
, &s
)) {
142 ERROR("stat(%s) failed: %m\n", path
);
146 snprintf(new, sizeof(new), "%s%s", root
, target
?target
:path
);
148 if (S_ISDIR(s
.st_mode
)) {
151 mkdir_p(dirname(new), 0755);
152 snprintf(new, sizeof(new), "%s%s", root
, target
?target
:path
);
153 fd
= creat(new, 0644);
155 ERROR("creat(%s) failed: %m\n", new);
161 if (mount(path
, new, NULL
, MS_BIND
, NULL
)) {
162 ERROR("failed to mount -B %s %s: %m\n", path
, new);
167 remount_flags
|= MS_RDONLY
;
170 remount_flags
|= MS_NOEXEC
| MS_NOSUID
| MS_NODEV
;
172 if ((strict
|| readonly
) && mount(NULL
, new, NULL
, remount_flags
, NULL
)) {
173 ERROR("failed to remount (%s%s%s) %s: %m\n", readonly
?"ro":"rw",
174 (readonly
&& strict
)?", ":"", strict
?"strict":"", new);
178 DEBUG("mount -B %s %s (%s%s%s)\n", path
, new,
179 readonly
?"ro":"rw", (readonly
&& strict
)?", ":"", strict
?"strict":"");
184 int mount_bind(const char *root
, const char *path
, int readonly
, int error
) {
185 return _mount_bind(root
, path
, NULL
, readonly
, 0, error
);
188 static int mount_overlay(char *jail_root
, char *overlaydir
) {
189 char *upperdir
, *workdir
, *optsstr
, *upperetc
, *upperresolvconf
;
190 const char mountoptsformat
[] = "lowerdir=%s,upperdir=%s,workdir=%s";
193 if (asprintf(&upperdir
, "%s%s", overlaydir
, "/upper") < 0)
196 if (asprintf(&workdir
, "%s%s", overlaydir
, "/work") < 0)
199 if (asprintf(&optsstr
, mountoptsformat
, jail_root
, upperdir
, workdir
) < 0)
202 if (mkdir_p(upperdir
, 0755) || mkdir_p(workdir
, 0755))
206 * make sure /etc/resolv.conf exists in overlay and is owned by jail userns root
207 * this is to work-around a bug in overlayfs described in the overlayfs-userns
209 * 3. modification of a file 'hithere' which is in l but not yet
210 * in u, and which is not owned by T, is not allowed, even if
211 * writes to u are allowed. This may be a bug in overlayfs,
212 * but it is safe behavior.
214 if (asprintf(&upperetc
, "%s/etc", upperdir
) < 0)
217 if (mkdir_p(upperetc
, 0755))
218 goto upper_etc_printf
;
220 if (asprintf(&upperresolvconf
, "%s/resolv.conf", upperetc
) < 0)
221 goto upper_etc_printf
;
223 fd
= creat(upperresolvconf
, 0644);
225 ERROR("creat(%s) failed: %m\n", upperresolvconf
);
226 goto upper_resolvconf_printf
;
230 DEBUG("mount -t overlay %s %s (%s)\n", jail_root
, jail_root
, optsstr
);
232 if (mount(jail_root
, jail_root
, "overlay", MS_NOATIME
, optsstr
))
237 upper_resolvconf_printf
:
238 free(upperresolvconf
);
251 static void pass_console(int console_fd
)
253 struct ubus_context
*ctx
= ubus_connect(NULL
);
254 static struct blob_buf req
;
260 blob_buf_init(&req
, 0);
261 blobmsg_add_string(&req
, "name", opts
.name
);
263 if (ubus_lookup_id(ctx
, "container", &id
) ||
264 ubus_invoke_fd(ctx
, id
, "console_set", req
.head
, NULL
, NULL
, 3000, console_fd
))
265 INFO("ubus request failed\n");
273 static int create_dev_console(const char *jail_root
)
276 char dev_console_path
[PATH_MAX
];
277 int slave_console_fd
;
279 /* Open UNIX/98 virtual console */
280 console_fd
= posix_openpt(O_RDWR
| O_NOCTTY
);
281 if (console_fd
== -1)
284 console_fname
= ptsname(console_fd
);
285 DEBUG("got console fd %d and PTS client name %s\n", console_fd
, console_fname
);
290 unlockpt(console_fd
);
292 /* pass PTY master to procd */
293 pass_console(console_fd
);
295 /* mount-bind PTY slave to /dev/console in jail */
296 snprintf(dev_console_path
, sizeof(dev_console_path
), "%s/dev/console", jail_root
);
297 close(creat(dev_console_path
, 0620));
299 if (mount(console_fname
, dev_console_path
, NULL
, MS_BIND
, NULL
))
302 /* use PTY slave for stdio */
303 slave_console_fd
= open(console_fname
, O_RDWR
); /* | O_NOCTTY */
304 dup2(slave_console_fd
, 0);
305 dup2(slave_console_fd
, 1);
306 dup2(slave_console_fd
, 2);
307 close(slave_console_fd
);
309 INFO("using guest console %s\n", console_fname
);
318 static int hook_running
= 0;
319 static int hook_return_code
= 0;
321 static void hook_process_timeout_cb(struct uloop_timeout
*t
);
322 static struct uloop_timeout hook_process_timeout
= {
323 .cb
= hook_process_timeout_cb
,
326 static void hook_process_handler(struct uloop_process
*c
, int ret
)
328 uloop_timeout_cancel(&hook_process_timeout
);
329 if (WIFEXITED(ret
)) {
330 hook_return_code
= WEXITSTATUS(ret
);
331 INFO("hook (%d) exited with exit: %d\n", c
->pid
, hook_return_code
);
333 hook_return_code
= WTERMSIG(ret
);
334 INFO("hook (%d) exited with signal: %d\n", c
->pid
, hook_return_code
);
340 static struct uloop_process hook_process
= {
341 .cb
= hook_process_handler
,
344 static void hook_process_timeout_cb(struct uloop_timeout
*t
)
346 DEBUG("hook process failed to stop, sending SIGKILL\n");
347 kill(hook_process
.pid
, SIGKILL
);
350 static void hook_handle_signal(int signo
)
352 DEBUG("forwarding signal %d to the jailed process\n", signo
);
353 kill(hook_process
.pid
, signo
);
356 static int run_hook(struct hook_execvpe
*hook
)
361 DEBUG("executing hook %s\n", hook
->file
);
363 sigfillset(&sigmask
);
364 for (i
= 0; i
< _NSIG
; i
++) {
365 struct sigaction s
= { 0 };
367 if (!sigismember(&sigmask
, i
))
369 if ((i
== SIGCHLD
) || (i
== SIGPIPE
) || (i
== SIGSEGV
))
372 s
.sa_handler
= hook_handle_signal
;
373 sigaction(i
, &s
, NULL
);
376 hook_process
.pid
= fork();
377 if (hook_process
.pid
> 0) {
379 uloop_process_add(&hook_process
);
380 if (hook
->timeout
> 0)
381 uloop_timeout_set(&hook_process_timeout
, 1000 * hook
->timeout
);
385 DEBUG("uloop interrupted, killing hook process\n");
386 kill(hook_process
.pid
, SIGTERM
);
387 uloop_timeout_set(&hook_process_timeout
, 1000);
393 } else if (hook_process
.pid
== 0) {
395 execvpe(hook
->file
, hook
->argv
, hook
->envp
);
403 static int run_hooks(struct hook_execvpe
*hook
)
405 struct hook_execvpe
*cur
;
410 res
= run_hook(cur
++);
418 static int build_jail_fs(void)
420 char jail_root
[] = "/tmp/ujail-XXXXXX";
421 char tmpovdir
[] = "/tmp/ujail-overlay-XXXXXX";
422 char tmpdevdir
[] = "/tmp/ujail-XXXXXX/dev";
423 char tmpdevptsdir
[] = "/tmp/ujail-XXXXXX/dev/pts";
424 char *overlaydir
= NULL
;
426 if (mkdtemp(jail_root
) == NULL
) {
427 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
431 /* oldroot can't be MS_SHARED else pivot_root() fails */
432 if (mount("none", "/", NULL
, MS_REC
|MS_PRIVATE
, NULL
)) {
433 ERROR("private mount failed %m\n");
438 if (mount(opts
.extroot
, jail_root
, NULL
, MS_BIND
, NULL
)) {
439 ERROR("extroot mount failed %m\n");
443 if (mount("tmpfs", jail_root
, "tmpfs", MS_NOATIME
, "mode=0755")) {
444 ERROR("tmpfs mount failed %m\n");
449 if (opts
.tmpoverlaysize
) {
450 char mountoptsstr
[] = "mode=0755,size=XXXXXXXX";
452 snprintf(mountoptsstr
, sizeof(mountoptsstr
),
453 "mode=0755,size=%s", opts
.tmpoverlaysize
);
454 if (mkdtemp(tmpovdir
) == NULL
) {
455 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
458 if (mount("tmpfs", tmpovdir
, "tmpfs", MS_NOATIME
,
460 ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts
.tmpoverlaysize
);
463 overlaydir
= tmpovdir
;
467 overlaydir
= opts
.overlaydir
;
470 mount_overlay(jail_root
, overlaydir
);
472 if (chdir(jail_root
)) {
473 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root
);
477 snprintf(tmpdevdir
, sizeof(tmpdevdir
), "%s/dev", jail_root
);
478 mkdir_p(tmpdevdir
, 0755);
479 if (mount(NULL
, tmpdevdir
, "tmpfs", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, "size=1M"))
482 snprintf(tmpdevptsdir
, sizeof(tmpdevptsdir
), "%s/dev/pts", jail_root
);
483 mkdir_p(tmpdevptsdir
, 0755);
484 if (mount(NULL
, tmpdevptsdir
, "devpts", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, NULL
))
488 create_dev_console(jail_root
);
490 if (mount_all(jail_root
)) {
491 ERROR("mount_all() failed\n");
495 if (opts
.namespace & CLONE_NEWNET
) {
496 char hostdir
[PATH_MAX
], jailetc
[PATH_MAX
], jaillink
[PATH_MAX
];
498 snprintf(hostdir
, PATH_MAX
, "/tmp/resolv.conf-%s.d", opts
.name
);
499 mkdir_p(hostdir
, 0755);
500 _mount_bind(jail_root
, hostdir
, "/tmp/resolv.conf.d", 1, 1, -1);
501 snprintf(jailetc
, PATH_MAX
, "%s/etc", jail_root
);
502 mkdir_p(jailetc
, 0755);
503 snprintf(jaillink
, PATH_MAX
, "%s/etc/resolv.conf", jail_root
);
506 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink
);
509 run_hooks(opts
.hooks
.createContainer
);
511 char dirbuf
[sizeof(jail_root
) + 4];
512 snprintf(dirbuf
, sizeof(dirbuf
), "%s/old", jail_root
);
515 if (pivot_root(jail_root
, dirbuf
) == -1) {
516 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root
, dirbuf
);
520 ERROR("chdir(/) (after pivot_root) failed: %m\n");
524 snprintf(dirbuf
, sizeof(dirbuf
), "/old%s", jail_root
);
525 umount2(dirbuf
, MNT_DETACH
);
527 if (opts
.tmpoverlaysize
) {
528 char tmpdirbuf
[sizeof(tmpovdir
) + 4];
529 snprintf(tmpdirbuf
, sizeof(tmpdirbuf
), "/old%s", tmpovdir
);
530 umount2(tmpdirbuf
, MNT_DETACH
);
534 umount2("/old", MNT_DETACH
);
538 mkdir("/proc", 0755);
539 mount("proc", "/proc", "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
541 * make /proc/sys read-only while keeping read-write to
542 * /proc/sys/net if CLONE_NEWNET is set.
544 if (opts
.namespace & CLONE_NEWNET
)
545 mount("/proc/sys/net", "/proc/self/net", NULL
, MS_BIND
, 0);
547 mount("/proc/sys", "/proc/sys", NULL
, MS_BIND
, 0);
548 mount(NULL
, "/proc/sys", NULL
, MS_REMOUNT
| MS_RDONLY
, 0);
549 mount(NULL
, "/proc", NULL
, MS_REMOUNT
, 0);
551 if (opts
.namespace & CLONE_NEWNET
)
552 mount("/proc/self/net", "/proc/sys/net", NULL
, MS_MOVE
, 0);
556 mount("sysfs", "/sys", "sysfs", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RDONLY
, 0);
559 mount(NULL
, "/", NULL
, MS_RDONLY
| MS_REMOUNT
, 0);
564 static int write_uid_gid_map(pid_t child_pid
, bool gidmap
, char *mapstr
)
569 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
570 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
573 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
576 if (dprintf(map_file
, "%s", mapstr
)) {
586 static int write_single_uid_gid_map(pid_t child_pid
, bool gidmap
, int id
)
590 const char *map_format
= "%d %d %d\n";
591 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
592 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
595 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
598 if (dprintf(map_file
, map_format
, 0, id
, 1) == -1) {
607 static int write_setgroups(pid_t child_pid
, bool allow
)
610 char setgroups_path
[64];
612 if (snprintf(setgroups_path
, sizeof(setgroups_path
), "/proc/%d/setgroups",
617 if ((setgroups_file
= open(setgroups_path
, O_WRONLY
)) == -1) {
621 if (dprintf(setgroups_file
, "%s", allow
?"allow":"deny") == -1) {
622 close(setgroups_file
);
626 close(setgroups_file
);
630 static void get_jail_user(int *user
, int *user_gid
, int *gr_gid
)
632 struct passwd
*p
= NULL
;
633 struct group
*g
= NULL
;
636 p
= getpwnam(opts
.user
);
638 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
639 opts
.user
, errno
, strerror(errno
));
643 *user_gid
= p
->pw_gid
;
650 g
= getgrnam(opts
.group
);
652 ERROR("failed to get gid for group %s: %m\n", opts
.group
);
661 static void set_jail_user(int pw_uid
, int user_gid
, int gr_gid
)
663 if (opts
.user
&& (user_gid
!= -1) && initgroups(opts
.user
, user_gid
)) {
664 ERROR("failed to initgroups() for user %s: %m\n", opts
.user
);
668 if ((gr_gid
!= -1) && setregid(gr_gid
, gr_gid
)) {
669 ERROR("failed to set group id %d: %m\n", gr_gid
);
673 if ((pw_uid
!= -1) && setreuid(pw_uid
, pw_uid
)) {
674 ERROR("failed to set user id %d: %m\n", pw_uid
);
680 static char** build_envp(const char *seccomp
, char **ocienvp
)
682 static char *envp
[MAX_ENVP
];
683 static char preload_var
[PATH_MAX
];
684 static char seccomp_var
[PATH_MAX
];
685 static char debug_var
[] = "LD_DEBUG=all";
686 static char container_var
[] = "container=ujail";
687 const char *preload_lib
= find_lib("libpreload-seccomp.so");
692 if (seccomp
&& !preload_lib
) {
693 ERROR("failed to add preload-lib to env\n");
697 snprintf(seccomp_var
, sizeof(seccomp_var
), "SECCOMP_FILE=%s", seccomp
);
698 envp
[count
++] = seccomp_var
;
699 snprintf(preload_var
, sizeof(preload_var
), "LD_PRELOAD=%s", preload_lib
);
700 envp
[count
++] = preload_var
;
703 envp
[count
++] = container_var
;
706 envp
[count
++] = debug_var
;
709 while (addenv
&& *addenv
) {
710 envp
[count
++] = *(addenv
++);
711 if (count
>= MAX_ENVP
) {
712 ERROR("environment limited to %d extra records, truncating\n", MAX_ENVP
);
719 static void usage(void)
721 fprintf(stderr
, "ujail <options> -- <binary> <params ...>\n");
722 fprintf(stderr
, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
723 fprintf(stderr
, " -S <file>\tseccomp filter config\n");
724 fprintf(stderr
, " -C <file>\tcapabilities drop config\n");
725 fprintf(stderr
, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
726 fprintf(stderr
, " -n <name>\tthe name of the jail\n");
727 fprintf(stderr
, "namespace jail options:\n");
728 fprintf(stderr
, " -h <hostname>\tchange the hostname of the jail\n");
729 fprintf(stderr
, " -N\t\tjail has network namespace\n");
730 fprintf(stderr
, " -f\t\tjail has user namespace\n");
731 fprintf(stderr
, " -F\t\tjail has cgroups namespace\n");
732 fprintf(stderr
, " -r <file>\treadonly files that should be staged\n");
733 fprintf(stderr
, " -w <file>\twriteable files that should be staged\n");
734 fprintf(stderr
, " -p\t\tjail has /proc\n");
735 fprintf(stderr
, " -s\t\tjail has /sys\n");
736 fprintf(stderr
, " -l\t\tjail has /dev/log\n");
737 fprintf(stderr
, " -u\t\tjail has a ubus socket\n");
738 fprintf(stderr
, " -U <name>\tuser to run jailed process\n");
739 fprintf(stderr
, " -G <name>\tgroup to run jailed process\n");
740 fprintf(stderr
, " -o\t\tremont jail root (/) read only\n");
741 fprintf(stderr
, " -R <dir>\texternal jail rootfs (system container)\n");
742 fprintf(stderr
, " -O <dir>\tdirectory for r/w overlayfs\n");
743 fprintf(stderr
, " -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
744 fprintf(stderr
, " -E\t\tfail if jail cannot be setup\n");
745 fprintf(stderr
, " -y\t\tprovide jail console\n");
746 fprintf(stderr
, " -J <dir>\tstart OCI bundle\n");
747 fprintf(stderr
, "\nWarning: by default root inside the jail is the same\n\
748 and he has the same powers as root outside the jail,\n\
749 thus he can escape the jail and/or break stuff.\n\
750 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
751 If you use none of the namespace jail options,\n\
752 ujail will not use namespace/build a jail,\n\
753 and will only drop capabilities/apply seccomp filter.\n\n");
756 static int exec_jail(void *pipes_ptr
)
758 int *pipes
= (int*)pipes_ptr
;
760 int pw_uid
, pw_gid
, gr_gid
;
766 if (write(pipes
[1], buf
, 1) < 1) {
767 ERROR("can't write to parent\n");
770 if (read(pipes
[2], buf
, 1) < 1) {
771 ERROR("can't read from parent\n");
775 ERROR("parent had an error, child exiting\n");
782 if (opts
.namespace & CLONE_NEWUSER
) {
783 if (setregid(0, 0) < 0) {
787 if (setreuid(0, 0) < 0) {
791 if (setgroups(0, NULL
) < 0) {
792 ERROR("setgroups\n");
797 if (opts
.namespace && opts
.hostname
&& strlen(opts
.hostname
) > 0
798 && sethostname(opts
.hostname
, strlen(opts
.hostname
))) {
799 ERROR("sethostname(%s) failed: %m\n", opts
.hostname
);
803 if ((opts
.namespace & CLONE_NEWNS
) && build_jail_fs()) {
804 ERROR("failed to build jail fs\n");
808 run_hooks(opts
.hooks
.startContainer
);
810 if (applyOCIcapabilities(opts
.capset
))
813 if (opts
.capabilities
&& drop_capabilities(opts
.capabilities
))
816 if (opts
.no_new_privs
&& prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
817 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
821 if (!(opts
.namespace & CLONE_NEWUSER
)) {
822 get_jail_user(&pw_uid
, &pw_gid
, &gr_gid
);
824 set_jail_user(opts
.pw_uid
?:pw_uid
, opts
.pw_gid
?:pw_gid
, opts
.gr_gid
?:gr_gid
);
827 char **envp
= build_envp(opts
.seccomp
, opts
.envp
);
831 if (opts
.cwd
&& chdir(opts
.cwd
))
834 if (opts
.ociseccomp
&& applyOCIlinuxseccomp(opts
.ociseccomp
))
837 INFO("exec-ing %s\n", *opts
.jail_argv
);
838 if (opts
.envp
) /* respect PATH if potentially set in ENV */
839 execvpe(*opts
.jail_argv
, opts
.jail_argv
, envp
);
841 execve(*opts
.jail_argv
, opts
.jail_argv
, envp
);
843 /* we get there only if execve fails */
844 ERROR("failed to execve %s: %m\n", *opts
.jail_argv
);
848 static int jail_running
= 1;
849 static int jail_return_code
= 0;
851 static void jail_process_timeout_cb(struct uloop_timeout
*t
);
852 static struct uloop_timeout jail_process_timeout
= {
853 .cb
= jail_process_timeout_cb
,
856 static void jail_process_handler(struct uloop_process
*c
, int ret
)
858 uloop_timeout_cancel(&jail_process_timeout
);
859 if (WIFEXITED(ret
)) {
860 jail_return_code
= WEXITSTATUS(ret
);
861 INFO("jail (%d) exited with exit: %d\n", c
->pid
, jail_return_code
);
863 jail_return_code
= WTERMSIG(ret
);
864 INFO("jail (%d) exited with signal: %d\n", c
->pid
, jail_return_code
);
870 static struct uloop_process jail_process
= {
871 .cb
= jail_process_handler
,
874 static void jail_process_timeout_cb(struct uloop_timeout
*t
)
876 DEBUG("jail process failed to stop, sending SIGKILL\n");
877 kill(jail_process
.pid
, SIGKILL
);
880 static void jail_handle_signal(int signo
)
882 DEBUG("forwarding signal %d to the jailed process\n", signo
);
883 kill(jail_process
.pid
, signo
);
886 static int netns_open_pid(const pid_t target_ns
)
888 char pid_net_path
[PATH_MAX
];
890 snprintf(pid_net_path
, sizeof(pid_net_path
), "/proc/%u/ns/net", target_ns
);
892 return open(pid_net_path
, O_RDONLY
);
895 static void netns_updown(pid_t pid
, bool start
)
897 struct ubus_context
*ctx
= ubus_connect(NULL
);
898 static struct blob_buf req
;
904 blob_buf_init(&req
, 0);
905 blobmsg_add_string(&req
, "jail", opts
.name
);
906 blobmsg_add_u32(&req
, "pid", pid
);
907 blobmsg_add_u8(&req
, "start", start
);
909 if (ubus_lookup_id(ctx
, "network", &id
) ||
910 ubus_invoke(ctx
, id
, "netns_updown", req
.head
, NULL
, NULL
, 3000))
911 INFO("ubus request failed\n");
917 static int parseOCIenvarray(struct blob_attr
*msg
, char ***envp
)
919 struct blob_attr
*cur
;
922 blobmsg_for_each_attr(cur
, msg
, rem
)
926 *envp
= calloc(1 + sz
, sizeof(char*));
935 blobmsg_for_each_attr(cur
, msg
, rem
)
936 (*envp
)[sz
++] = strdup(blobmsg_get_string(cur
));
950 static const struct blobmsg_policy oci_root_policy
[] = {
951 [OCI_ROOT_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
952 [OCI_ROOT_READONLY
] = { "readonly", BLOBMSG_TYPE_BOOL
},
955 static int parseOCIroot(const char *jsonfile
, struct blob_attr
*msg
)
957 static char rootpath
[PATH_MAX
] = { 0 };
958 struct blob_attr
*tb
[__OCI_ROOT_MAX
];
961 blobmsg_parse(oci_root_policy
, __OCI_ROOT_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
963 if (!tb
[OCI_ROOT_PATH
])
966 strncpy(rootpath
, jsonfile
, PATH_MAX
);
967 cur
= strrchr(rootpath
, '/');
973 strncat(rootpath
, blobmsg_get_string(tb
[OCI_ROOT_PATH
]), PATH_MAX
- (strlen(rootpath
) + 1));
975 opts
.extroot
= rootpath
;
977 opts
.ronly
= blobmsg_get_bool(tb
[OCI_ROOT_READONLY
]);
991 static const struct blobmsg_policy oci_hook_policy
[] = {
992 [OCI_HOOK_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
993 [OCI_HOOK_ARGS
] = { "args", BLOBMSG_TYPE_ARRAY
},
994 [OCI_HOOK_ENV
] = { "env", BLOBMSG_TYPE_ARRAY
},
995 [OCI_HOOK_TIMEOUT
] = { "timeout", BLOBMSG_TYPE_INT32
},
998 static void free_hooklist(struct hook_execvpe
*hooklist
)
1000 struct hook_execvpe
*cur
;
1018 static int parseOCIhook(struct hook_execvpe
**hooklist
, struct blob_attr
*msg
)
1020 struct blob_attr
*tb
[__OCI_HOOK_MAX
];
1021 struct blob_attr
*cur
;
1025 blobmsg_for_each_attr(cur
, msg
, rem
)
1031 *hooklist
= calloc(idx
+ 1, sizeof(struct hook_execvpe
*));
1034 blobmsg_for_each_attr(cur
, msg
, rem
) {
1035 blobmsg_parse(oci_hook_policy
, __OCI_HOOK_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1037 if (!tb
[OCI_HOOK_PATH
]) {
1042 hooklist
[idx
] = malloc(sizeof(struct hook_execvpe
));
1043 if (tb
[OCI_HOOK_ARGS
]) {
1044 ret
= parseOCIenvarray(tb
[OCI_HOOK_ARGS
], &(hooklist
[idx
]->argv
));
1049 if (tb
[OCI_HOOK_ENV
]) {
1050 ret
= parseOCIenvarray(tb
[OCI_HOOK_ENV
], &(hooklist
[idx
]->envp
));
1055 if (tb
[OCI_HOOK_TIMEOUT
])
1056 hooklist
[idx
]->timeout
= blobmsg_get_u32(tb
[OCI_HOOK_TIMEOUT
]);
1058 hooklist
[idx
]->file
= strdup(blobmsg_get_string(tb
[OCI_HOOK_PATH
]));
1063 hooklist
[idx
] = NULL
;
1067 free_hooklist(*hooklist
);
1076 OCI_HOOKS_CREATERUNTIME
,
1077 OCI_HOOKS_CREATECONTAINER
,
1078 OCI_HOOKS_STARTCONTAINER
,
1079 OCI_HOOKS_POSTSTART
,
1084 static const struct blobmsg_policy oci_hooks_policy
[] = {
1085 [OCI_HOOKS_PRESTART
] = { "prestart", BLOBMSG_TYPE_ARRAY
},
1086 [OCI_HOOKS_CREATERUNTIME
] = { "createRuntime", BLOBMSG_TYPE_ARRAY
},
1087 [OCI_HOOKS_CREATECONTAINER
] = { "createContainer", BLOBMSG_TYPE_ARRAY
},
1088 [OCI_HOOKS_STARTCONTAINER
] = { "startContainer", BLOBMSG_TYPE_ARRAY
},
1089 [OCI_HOOKS_POSTSTART
] = { "poststart", BLOBMSG_TYPE_ARRAY
},
1090 [OCI_HOOKS_POSTSTOP
] = { "poststop", BLOBMSG_TYPE_ARRAY
},
1093 static int parseOCIhooks(struct blob_attr
*msg
)
1095 struct blob_attr
*tb
[__OCI_HOOKS_MAX
];
1098 blobmsg_parse(oci_hooks_policy
, __OCI_HOOKS_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1100 if (tb
[OCI_HOOKS_PRESTART
])
1101 INFO("warning: ignoring deprecated prestart hook");
1103 if (tb
[OCI_HOOKS_CREATERUNTIME
]) {
1104 ret
= parseOCIhook(&opts
.hooks
.createRuntime
, tb
[OCI_HOOKS_CREATERUNTIME
]);
1109 if (tb
[OCI_HOOKS_CREATECONTAINER
]) {
1110 ret
= parseOCIhook(&opts
.hooks
.createContainer
, tb
[OCI_HOOKS_CREATECONTAINER
]);
1112 goto out_createruntime
;
1115 if (tb
[OCI_HOOKS_STARTCONTAINER
]) {
1116 ret
= parseOCIhook(&opts
.hooks
.startContainer
, tb
[OCI_HOOKS_STARTCONTAINER
]);
1118 goto out_createcontainer
;
1121 if (tb
[OCI_HOOKS_POSTSTART
]) {
1122 ret
= parseOCIhook(&opts
.hooks
.poststart
, tb
[OCI_HOOKS_POSTSTART
]);
1124 goto out_startcontainer
;
1127 if (tb
[OCI_HOOKS_POSTSTOP
]) {
1128 ret
= parseOCIhook(&opts
.hooks
.poststop
, tb
[OCI_HOOKS_POSTSTOP
]);
1136 free_hooklist(opts
.hooks
.poststart
);
1138 free_hooklist(opts
.hooks
.startContainer
);
1139 out_createcontainer
:
1140 free_hooklist(opts
.hooks
.createContainer
);
1142 free_hooklist(opts
.hooks
.createRuntime
);
1149 OCI_MOUNT_DESTINATION
,
1155 static const struct blobmsg_policy oci_mount_policy
[] = {
1156 [OCI_MOUNT_SOURCE
] = { "source", BLOBMSG_TYPE_STRING
},
1157 [OCI_MOUNT_DESTINATION
] = { "destination", BLOBMSG_TYPE_STRING
},
1158 [OCI_MOUNT_TYPE
] = { "type", BLOBMSG_TYPE_STRING
},
1159 [OCI_MOUNT_OPTIONS
] = { "options", BLOBMSG_TYPE_ARRAY
},
1162 static int parseOCImount(struct blob_attr
*msg
)
1164 struct blob_attr
*tb
[__OCI_MOUNT_MAX
];
1166 blobmsg_parse(oci_mount_policy
, __OCI_MOUNT_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1168 if (!tb
[OCI_MOUNT_DESTINATION
])
1171 if (!strcmp("proc", blobmsg_get_string(tb
[OCI_MOUNT_TYPE
])) &&
1172 !strcmp("/proc", blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]))) {
1177 if (!strcmp("sysfs", blobmsg_get_string(tb
[OCI_MOUNT_TYPE
])) &&
1178 !strcmp("/sys", blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]))) {
1183 if (!strcmp("tmpfs", blobmsg_get_string(tb
[OCI_MOUNT_TYPE
])) &&
1184 !strcmp("/dev", blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]))) {
1185 /* we always mount a small tmpfs on /dev */
1189 INFO("ignoring unsupported mount %s %s -t %s -o %s\n",
1190 blobmsg_get_string(tb
[OCI_MOUNT_SOURCE
]),
1191 blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]),
1192 blobmsg_get_string(tb
[OCI_MOUNT_TYPE
]),
1193 blobmsg_format_json(tb
[OCI_MOUNT_OPTIONS
], true));
1200 OCI_PROCESS_USER_UID
,
1201 OCI_PROCESS_USER_GID
,
1202 OCI_PROCESS_USER_UMASK
,
1203 OCI_PROCESS_USER_ADDITIONALGIDS
,
1204 __OCI_PROCESS_USER_MAX
,
1207 static const struct blobmsg_policy oci_process_user_policy
[] = {
1208 [OCI_PROCESS_USER_UID
] = { "uid", BLOBMSG_TYPE_INT32
},
1209 [OCI_PROCESS_USER_GID
] = { "gid", BLOBMSG_TYPE_INT32
},
1210 [OCI_PROCESS_USER_UMASK
] = { "umask", BLOBMSG_TYPE_INT32
},
1211 [OCI_PROCESS_USER_ADDITIONALGIDS
] = { "additionalGids", BLOBMSG_TYPE_ARRAY
},
1214 static int parseOCIprocessuser(struct blob_attr
*msg
) {
1215 struct blob_attr
*tb
[__OCI_PROCESS_USER_MAX
];
1217 blobmsg_parse(oci_process_user_policy
, __OCI_PROCESS_USER_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1219 if (tb
[OCI_PROCESS_USER_UID
])
1220 opts
.pw_uid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_UID
]);
1222 if (tb
[OCI_PROCESS_USER_GID
]) {
1223 opts
.pw_gid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_GID
]);
1224 opts
.gr_gid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_GID
]);
1227 /* ToDo: umask, additional GIDs */
1234 OCI_PROCESS_CAPABILITIES
,
1237 OCI_PROCESS_NONEWPRIVILEGES
,
1238 OCI_PROCESS_RLIMITS
,
1239 OCI_PROCESS_TERMINAL
,
1244 static const struct blobmsg_policy oci_process_policy
[] = {
1245 [OCI_PROCESS_ARGS
] = { "args", BLOBMSG_TYPE_ARRAY
},
1246 [OCI_PROCESS_CAPABILITIES
] = { "capabilities", BLOBMSG_TYPE_TABLE
},
1247 [OCI_PROCESS_CWD
] = { "cwd", BLOBMSG_TYPE_STRING
},
1248 [OCI_PROCESS_ENV
] = { "env", BLOBMSG_TYPE_ARRAY
},
1249 [OCI_PROCESS_NONEWPRIVILEGES
] = { "noNewPrivileges", BLOBMSG_TYPE_BOOL
},
1250 [OCI_PROCESS_RLIMITS
] = { "rlimits", BLOBMSG_TYPE_ARRAY
},
1251 [OCI_PROCESS_TERMINAL
] = { "terminal", BLOBMSG_TYPE_BOOL
},
1252 [OCI_PROCESS_USER
] = { "user", BLOBMSG_TYPE_TABLE
},
1256 static int parseOCIprocess(struct blob_attr
*msg
)
1258 struct blob_attr
*tb
[__OCI_PROCESS_MAX
];
1261 blobmsg_parse(oci_process_policy
, __OCI_PROCESS_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1263 if (!tb
[OCI_PROCESS_ARGS
])
1266 res
= parseOCIenvarray(tb
[OCI_PROCESS_ARGS
], &opts
.jail_argv
);
1270 opts
.console
= blobmsg_get_bool(tb
[OCI_PROCESS_TERMINAL
]);
1271 opts
.no_new_privs
= blobmsg_get_bool(tb
[OCI_PROCESS_NONEWPRIVILEGES
]);
1273 if (tb
[OCI_PROCESS_CWD
])
1274 opts
.cwd
= blobmsg_get_string(tb
[OCI_PROCESS_CWD
]);
1276 if (tb
[OCI_PROCESS_ENV
]) {
1277 res
= parseOCIenvarray(tb
[OCI_PROCESS_ENV
], &opts
.envp
);
1282 if (tb
[OCI_PROCESS_USER
] && (res
= parseOCIprocessuser(tb
[OCI_PROCESS_USER
])))
1285 if (tb
[OCI_PROCESS_CAPABILITIES
] &&
1286 (res
= parseOCIcapabilities(&opts
.capset
, tb
[OCI_PROCESS_CAPABILITIES
])))
1289 /* ToDo: rlimits, capabilities */
1295 OCI_LINUX_NAMESPACE_TYPE
,
1296 OCI_LINUX_NAMESPACE_PATH
,
1297 __OCI_LINUX_NAMESPACE_MAX
,
1300 static const struct blobmsg_policy oci_linux_namespace_policy
[] = {
1301 [OCI_LINUX_NAMESPACE_TYPE
] = { "type", BLOBMSG_TYPE_STRING
},
1302 [OCI_LINUX_NAMESPACE_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
1305 static unsigned int resolve_nstype(char *type
) {
1306 if (!strcmp("pid", type
))
1307 return CLONE_NEWPID
;
1308 else if (!strcmp("network", type
))
1309 return CLONE_NEWNET
;
1310 else if (!strcmp("mount", type
))
1312 else if (!strcmp("ipc", type
))
1313 return CLONE_NEWIPC
;
1314 else if (!strcmp("uts", type
))
1315 return CLONE_NEWUTS
;
1316 else if (!strcmp("user", type
))
1317 return CLONE_NEWUSER
;
1318 else if (!strcmp("cgroup", type
))
1319 return CLONE_NEWCGROUP
;
1324 static int parseOCIlinuxns(struct blob_attr
*msg
)
1326 struct blob_attr
*tb
[__OCI_LINUX_NAMESPACE_MAX
];
1329 blobmsg_parse(oci_linux_namespace_policy
, __OCI_LINUX_NAMESPACE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1331 if (!tb
[OCI_LINUX_NAMESPACE_TYPE
])
1334 if (tb
[OCI_LINUX_NAMESPACE_PATH
])
1335 return ENOTSUP
; /* ToDo */
1337 opts
.namespace |= resolve_nstype(blobmsg_get_string(tb
[OCI_LINUX_NAMESPACE_TYPE
]));
1344 OCI_LINUX_UIDGIDMAP_CONTAINERID
,
1345 OCI_LINUX_UIDGIDMAP_HOSTID
,
1346 OCI_LINUX_UIDGIDMAP_SIZE
,
1347 __OCI_LINUX_UIDGIDMAP_MAX
,
1350 static const struct blobmsg_policy oci_linux_uidgidmap_policy
[] = {
1351 [OCI_LINUX_UIDGIDMAP_CONTAINERID
] = { "containerID", BLOBMSG_TYPE_INT32
},
1352 [OCI_LINUX_UIDGIDMAP_HOSTID
] = { "hostID", BLOBMSG_TYPE_INT32
},
1353 [OCI_LINUX_UIDGIDMAP_SIZE
] = { "size", BLOBMSG_TYPE_INT32
},
1356 static int parseOCIuidgidmappings(struct blob_attr
*msg
, bool is_gidmap
)
1358 const char *map_format
= "%d %d %d\n";
1359 struct blob_attr
*tb
[__OCI_LINUX_UIDGIDMAP_MAX
];
1360 struct blob_attr
*cur
;
1364 unsigned int cnt
= 0;
1365 size_t totallen
= 0;
1367 /* count number of mappings */
1368 blobmsg_for_each_attr(cur
, msg
, rem
)
1374 /* allocate array for mappings */
1375 mappings
= calloc(1 + cnt
, sizeof(char*));
1379 mappings
[cnt
] = NULL
;
1382 blobmsg_for_each_attr(cur
, msg
, rem
) {
1383 blobmsg_parse(oci_linux_uidgidmap_policy
, __OCI_LINUX_UIDGIDMAP_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1385 if (!tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
] ||
1386 !tb
[OCI_LINUX_UIDGIDMAP_HOSTID
] ||
1387 !tb
[OCI_LINUX_UIDGIDMAP_SIZE
])
1390 /* write mapping line into allocated string */
1391 len
= asprintf(&mappings
[cnt
++], map_format
,
1392 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
]),
1393 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_HOSTID
]),
1394 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_SIZE
]));
1402 /* allocate combined mapping string */
1403 map
= calloc(1 + totallen
, sizeof(char));
1409 /* concatenate mapping strings into combined string */
1410 curstr
= mappings
[0];
1412 strcat(map
, curstr
);
1426 OCI_LINUX_RESOURCES
,
1429 OCI_LINUX_NAMESPACES
,
1430 OCI_LINUX_UIDMAPPINGS
,
1431 OCI_LINUX_GIDMAPPINGS
,
1432 OCI_LINUX_MASKEDPATHS
,
1433 OCI_LINUX_READONLYPATHS
,
1434 OCI_LINUX_ROOTFSPROPAGATION
,
1438 static const struct blobmsg_policy oci_linux_policy
[] = {
1439 [OCI_LINUX_RESOURCES
] = { "resources", BLOBMSG_TYPE_TABLE
},
1440 [OCI_LINUX_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_TABLE
},
1441 [OCI_LINUX_SYSCTL
] = { "sysctl", BLOBMSG_TYPE_TABLE
},
1442 [OCI_LINUX_NAMESPACES
] = { "namespaces", BLOBMSG_TYPE_ARRAY
},
1443 [OCI_LINUX_UIDMAPPINGS
] = { "uidMappings", BLOBMSG_TYPE_ARRAY
},
1444 [OCI_LINUX_GIDMAPPINGS
] = { "gidMappings", BLOBMSG_TYPE_ARRAY
},
1445 [OCI_LINUX_MASKEDPATHS
] = { "maskedPaths", BLOBMSG_TYPE_ARRAY
},
1446 [OCI_LINUX_READONLYPATHS
] = { "readonlyPaths", BLOBMSG_TYPE_ARRAY
},
1447 [OCI_LINUX_ROOTFSPROPAGATION
] = { "rootfsPropagation", BLOBMSG_TYPE_STRING
},
1450 static int parseOCIlinux(struct blob_attr
*msg
)
1452 struct blob_attr
*tb
[__OCI_LINUX_MAX
];
1453 struct blob_attr
*cur
;
1457 blobmsg_parse(oci_linux_policy
, __OCI_LINUX_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1459 if (tb
[OCI_LINUX_NAMESPACES
]) {
1460 blobmsg_for_each_attr(cur
, tb
[OCI_LINUX_NAMESPACES
], rem
) {
1461 res
= parseOCIlinuxns(cur
);
1467 if (tb
[OCI_LINUX_UIDMAPPINGS
]) {
1468 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 0);
1473 if (tb
[OCI_LINUX_GIDMAPPINGS
]) {
1474 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 1);
1479 if (tb
[OCI_LINUX_SECCOMP
]) {
1480 opts
.ociseccomp
= parseOCIlinuxseccomp(tb
[OCI_LINUX_SECCOMP
]);
1481 if (!opts
.ociseccomp
)
1499 static const struct blobmsg_policy oci_policy
[] = {
1500 [OCI_VERSION
] = { "ociVersion", BLOBMSG_TYPE_STRING
},
1501 [OCI_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
1502 [OCI_PROCESS
] = { "process", BLOBMSG_TYPE_TABLE
},
1503 [OCI_ROOT
] = { "root", BLOBMSG_TYPE_TABLE
},
1504 [OCI_MOUNTS
] = { "mounts", BLOBMSG_TYPE_ARRAY
},
1505 [OCI_HOOKS
] = { "hooks", BLOBMSG_TYPE_TABLE
},
1506 [OCI_LINUX
] = { "linux", BLOBMSG_TYPE_TABLE
},
1509 static int parseOCI(const char *jsonfile
)
1511 struct blob_attr
*tb
[__OCI_MAX
];
1512 struct blob_attr
*cur
;
1516 blob_buf_init(&ocibuf
, 0);
1517 if (!blobmsg_add_json_from_file(&ocibuf
, jsonfile
))
1520 blobmsg_parse(oci_policy
, __OCI_MAX
, tb
, blob_data(ocibuf
.head
), blob_len(ocibuf
.head
));
1522 if (!tb
[OCI_VERSION
])
1525 if (strncmp("1.0", blobmsg_get_string(tb
[OCI_VERSION
]), 3)) {
1526 ERROR("unsupported ociVersion %s\n", blobmsg_get_string(tb
[OCI_VERSION
]));
1530 if (tb
[OCI_HOSTNAME
])
1531 opts
.hostname
= blobmsg_get_string(tb
[OCI_HOSTNAME
]);
1533 if (!tb
[OCI_PROCESS
])
1536 if ((res
= parseOCIprocess(tb
[OCI_PROCESS
])))
1542 if ((res
= parseOCIroot(jsonfile
, tb
[OCI_ROOT
])))
1545 if (!tb
[OCI_MOUNTS
])
1548 blobmsg_for_each_attr(cur
, tb
[OCI_MOUNTS
], rem
)
1549 if ((res
= parseOCImount(cur
)))
1552 if (tb
[OCI_LINUX
] && (res
= parseOCIlinux(tb
[OCI_LINUX
])))
1555 if (tb
[OCI_HOOKS
] && (res
= parseOCIhooks(tb
[OCI_HOOKS
])))
1561 int main(int argc
, char **argv
)
1564 uid_t uid
= getuid();
1565 const char log
[] = "/dev/log";
1566 const char ubus
[] = "/var/run/ubus.sock";
1567 char *jsonfile
= NULL
;
1574 ERROR("not root, aborting: %m\n");
1575 return EXIT_FAILURE
;
1580 init_library_search();
1582 while ((ch
= getopt(argc
, argv
, OPT_ARGS
)) != -1) {
1585 debug
= atoi(optarg
);
1588 opts
.namespace |= CLONE_NEWNS
;
1592 opts
.namespace |= CLONE_NEWNS
;
1596 opts
.namespace |= CLONE_NEWUSER
;
1599 opts
.namespace |= CLONE_NEWCGROUP
;
1602 opts
.extroot
= optarg
;
1605 opts
.namespace |= CLONE_NEWNS
;
1609 opts
.seccomp
= optarg
;
1610 add_mount(optarg
, 1, -1);
1613 opts
.capabilities
= optarg
;
1616 opts
.no_new_privs
= 1;
1622 opts
.namespace |= CLONE_NEWNET
;
1625 opts
.namespace |= CLONE_NEWUTS
;
1626 opts
.hostname
= optarg
;
1629 opts
.namespace |= CLONE_NEWNS
;
1630 add_path_and_deps(optarg
, 1, 0, 0);
1633 opts
.namespace |= CLONE_NEWNS
;
1634 add_path_and_deps(optarg
, 0, 0, 0);
1637 opts
.namespace |= CLONE_NEWNS
;
1638 add_mount(ubus
, 0, -1);
1641 opts
.namespace |= CLONE_NEWNS
;
1642 add_mount(log
, 0, -1);
1648 opts
.group
= optarg
;
1651 opts
.overlaydir
= optarg
;
1654 opts
.tmpoverlaysize
= optarg
;
1657 opts
.require_jail
= 1;
1663 asprintf(&jsonfile
, "%s/config.json", optarg
);
1669 opts
.namespace |= CLONE_NEWIPC
| CLONE_NEWPID
;
1673 ocires
= parseOCI(jsonfile
);
1676 ERROR("parsing of OCI JSON spec has failed: %s (%d)\n", strerror(ocires
), ocires
);
1681 if (opts
.tmpoverlaysize
&& strlen(opts
.tmpoverlaysize
) > 8) {
1682 ERROR("size parameter too long: \"%s\"\n", opts
.tmpoverlaysize
);
1686 /* no <binary> param found */
1687 if (!jsonfile
&& (argc
- optind
< 1)) {
1689 return EXIT_FAILURE
;
1691 if (!(opts
.namespace||opts
.capabilities
||opts
.seccomp
)) {
1692 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
1694 return EXIT_FAILURE
;
1696 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
1698 opts
.capabilities
!= 0 || opts
.capset
.apply
,
1699 opts
.seccomp
!= 0 || opts
.ociseccomp
!= 0);
1702 opts
.jail_argv
= &argv
[optind
];
1703 if (opts
.namespace & CLONE_NEWUSER
)
1704 get_jail_user(&opts
.pw_uid
, &opts
.pw_gid
, &opts
.gr_gid
);
1707 if (!opts
.extroot
) {
1708 if (opts
.namespace && add_path_and_deps(*opts
.jail_argv
, 1, -1, 0)) {
1709 ERROR("failed to load dependencies\n");
1714 if (opts
.namespace && opts
.seccomp
&& add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
1715 ERROR("failed to load libpreload-seccomp.so\n");
1717 if (opts
.require_jail
)
1722 prctl(PR_SET_NAME
, opts
.name
, NULL
, NULL
, NULL
);
1726 sigfillset(&sigmask
);
1727 for (i
= 0; i
< _NSIG
; i
++) {
1728 struct sigaction s
= { 0 };
1730 if (!sigismember(&sigmask
, i
))
1732 if ((i
== SIGCHLD
) || (i
== SIGPIPE
) || (i
== SIGSEGV
))
1735 s
.sa_handler
= jail_handle_signal
;
1736 sigaction(i
, &s
, NULL
);
1739 if (opts
.namespace) {
1740 if (opts
.namespace & CLONE_NEWNS
) {
1741 add_mount("/dev/full", 0, -1);
1742 add_mount("/dev/null", 0, -1);
1743 add_mount("/dev/random", 0, -1);
1744 add_mount("/dev/urandom", 0, -1);
1745 add_mount("/dev/zero", 0, -1);
1746 add_mount("/dev/ptmx", 0, -1);
1747 add_mount("/dev/tty", 0, -1);
1749 if (!opts
.extroot
&& (opts
.user
|| opts
.group
)) {
1750 add_mount("/etc/passwd", 0, -1);
1751 add_mount("/etc/group", 0, -1);
1754 #if defined(__GLIBC__)
1756 add_mount("/etc/nsswitch.conf", 0, -1);
1759 if (!(opts
.namespace & CLONE_NEWNET
)) {
1760 add_mount("/etc/resolv.conf", 0, -1);
1764 if (pipe(&pipes
[0]) < 0 || pipe(&pipes
[2]) < 0)
1767 jail_process
.pid
= clone(exec_jail
, child_stack
+ STACK_SIZE
, SIGCHLD
| opts
.namespace, &pipes
);
1769 jail_process
.pid
= fork();
1772 if (jail_process
.pid
> 0) {
1774 /* parent process */
1777 run_hooks(opts
.hooks
.createRuntime
);
1778 if (read(pipes
[0], sig_buf
, 1) < 1) {
1779 ERROR("can't read from child\n");
1783 if (opts
.namespace & CLONE_NEWUSER
) {
1784 if (write_setgroups(jail_process
.pid
, true)) {
1785 ERROR("can't write setgroups\n");
1789 bool has_gr
= (opts
.gr_gid
!= -1);
1790 if (opts
.pw_uid
!= -1) {
1791 write_single_uid_gid_map(jail_process
.pid
, 0, opts
.pw_uid
);
1792 write_single_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:opts
.pw_gid
);
1794 write_single_uid_gid_map(jail_process
.pid
, 0, 65534);
1795 write_single_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:65534);
1798 write_uid_gid_map(jail_process
.pid
, 0, opts
.uidmap
);
1800 write_uid_gid_map(jail_process
.pid
, 1, opts
.gidmap
);
1804 if (opts
.namespace & CLONE_NEWNET
) {
1806 ERROR("netns needs a named jail\n");
1809 netns_fd
= netns_open_pid(jail_process
.pid
);
1810 netns_updown(jail_process
.pid
, true);
1814 if (write(pipes
[3], sig_buf
, 1) < 0) {
1815 ERROR("can't write to child\n");
1819 run_hooks(opts
.hooks
.poststart
);
1820 uloop_process_add(&jail_process
);
1823 DEBUG("uloop interrupted, killing jail process\n");
1824 kill(jail_process
.pid
, SIGTERM
);
1825 uloop_timeout_set(&jail_process_timeout
, 1000);
1829 if (opts
.namespace & CLONE_NEWNET
) {
1830 setns(netns_fd
, CLONE_NEWNET
);
1831 netns_updown(getpid(), false);
1834 run_hooks(opts
.hooks
.poststop
);
1835 return jail_return_code
;
1836 } else if (jail_process
.pid
== 0) {
1837 /* fork child process */
1838 return exec_jail(NULL
);
1840 ERROR("failed to clone/fork: %m\n");
1841 return EXIT_FAILURE
;