2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 * Copyright (C) 2020 Daniel Golle <daniel@makrotopia.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <sys/mount.h>
17 #include <sys/prctl.h>
19 #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:"
74 struct sock_fprog
*ociseccomp
;
76 struct jail_capset capset
;
85 struct sysctl_val
**sysctl
;
95 gid_t
*additional_gids
;
96 size_t num_additional_gids
;
101 struct hook_execvpe
**createRuntime
;
102 struct hook_execvpe
**createContainer
;
103 struct hook_execvpe
**startContainer
;
104 struct hook_execvpe
**poststart
;
105 struct hook_execvpe
**poststop
;
109 static void free_hooklist(struct hook_execvpe
**hooklist
)
111 struct hook_execvpe
*cur
;
136 static void free_sysctl(void) {
137 struct sysctl_val
*cur
;
148 static void free_opts(bool child
) {
151 /* we need to keep argv, envp and seccomp filter in child */
153 if (opts
.ociseccomp
) {
154 free(opts
.ociseccomp
->filter
);
155 free(opts
.ociseccomp
);
158 tmp
= opts
.jail_argv
;
162 free(opts
.jail_argv
);
177 free_hooklist(opts
.hooks
.createRuntime
);
178 free_hooklist(opts
.hooks
.createContainer
);
179 free_hooklist(opts
.hooks
.startContainer
);
180 free_hooklist(opts
.hooks
.poststart
);
181 free_hooklist(opts
.hooks
.poststop
);
184 static struct blob_buf ocibuf
;
186 extern int pivot_root(const char *new_root
, const char *put_old
);
190 static char child_stack
[STACK_SIZE
];
194 static int mount_overlay(char *jail_root
, char *overlaydir
) {
195 char *upperdir
, *workdir
, *optsstr
, *upperetc
, *upperresolvconf
;
196 const char mountoptsformat
[] = "lowerdir=%s,upperdir=%s,workdir=%s";
199 if (asprintf(&upperdir
, "%s%s", overlaydir
, "/upper") < 0)
202 if (asprintf(&workdir
, "%s%s", overlaydir
, "/work") < 0)
205 if (asprintf(&optsstr
, mountoptsformat
, jail_root
, upperdir
, workdir
) < 0)
208 if (mkdir_p(upperdir
, 0755) || mkdir_p(workdir
, 0755))
212 * make sure /etc/resolv.conf exists in overlay and is owned by jail userns root
213 * this is to work-around a bug in overlayfs described in the overlayfs-userns
215 * 3. modification of a file 'hithere' which is in l but not yet
216 * in u, and which is not owned by T, is not allowed, even if
217 * writes to u are allowed. This may be a bug in overlayfs,
218 * but it is safe behavior.
220 if (asprintf(&upperetc
, "%s/etc", upperdir
) < 0)
223 if (mkdir_p(upperetc
, 0755))
224 goto upper_etc_printf
;
226 if (asprintf(&upperresolvconf
, "%s/resolv.conf", upperetc
) < 0)
227 goto upper_etc_printf
;
229 fd
= creat(upperresolvconf
, 0644);
231 ERROR("creat(%s) failed: %m\n", upperresolvconf
);
232 goto upper_resolvconf_printf
;
236 DEBUG("mount -t overlay %s %s (%s)\n", jail_root
, jail_root
, optsstr
);
238 if (mount(jail_root
, jail_root
, "overlay", MS_NOATIME
, optsstr
))
243 upper_resolvconf_printf
:
244 free(upperresolvconf
);
257 static void pass_console(int console_fd
)
259 struct ubus_context
*ctx
= ubus_connect(NULL
);
260 static struct blob_buf req
;
266 blob_buf_init(&req
, 0);
267 blobmsg_add_string(&req
, "name", opts
.name
);
269 if (ubus_lookup_id(ctx
, "container", &id
) ||
270 ubus_invoke_fd(ctx
, id
, "console_set", req
.head
, NULL
, NULL
, 3000, console_fd
))
271 INFO("ubus request failed\n");
279 static int create_dev_console(const char *jail_root
)
282 char dev_console_path
[PATH_MAX
];
283 int slave_console_fd
;
285 /* Open UNIX/98 virtual console */
286 console_fd
= posix_openpt(O_RDWR
| O_NOCTTY
);
287 if (console_fd
== -1)
290 console_fname
= ptsname(console_fd
);
291 DEBUG("got console fd %d and PTS client name %s\n", console_fd
, console_fname
);
296 unlockpt(console_fd
);
298 /* pass PTY master to procd */
299 pass_console(console_fd
);
301 /* mount-bind PTY slave to /dev/console in jail */
302 snprintf(dev_console_path
, sizeof(dev_console_path
), "%s/dev/console", jail_root
);
303 close(creat(dev_console_path
, 0620));
305 if (mount(console_fname
, dev_console_path
, NULL
, MS_BIND
, NULL
))
308 /* use PTY slave for stdio */
309 slave_console_fd
= open(console_fname
, O_RDWR
); /* | O_NOCTTY */
310 dup2(slave_console_fd
, 0);
311 dup2(slave_console_fd
, 1);
312 dup2(slave_console_fd
, 2);
313 close(slave_console_fd
);
315 INFO("using guest console %s\n", console_fname
);
324 static int hook_running
= 0;
325 static int hook_return_code
= 0;
327 static void hook_process_timeout_cb(struct uloop_timeout
*t
);
328 static struct uloop_timeout hook_process_timeout
= {
329 .cb
= hook_process_timeout_cb
,
332 static void hook_process_handler(struct uloop_process
*c
, int ret
)
334 uloop_timeout_cancel(&hook_process_timeout
);
335 if (WIFEXITED(ret
)) {
336 hook_return_code
= WEXITSTATUS(ret
);
337 DEBUG("hook (%d) exited with exit: %d\n", c
->pid
, hook_return_code
);
339 hook_return_code
= WTERMSIG(ret
);
340 DEBUG("hook (%d) exited with signal: %d\n", c
->pid
, hook_return_code
);
346 static struct uloop_process hook_process
= {
347 .cb
= hook_process_handler
,
350 static void hook_process_timeout_cb(struct uloop_timeout
*t
)
352 DEBUG("hook process failed to stop, sending SIGKILL\n");
353 kill(hook_process
.pid
, SIGKILL
);
356 static int run_hook(struct hook_execvpe
*hook
)
360 DEBUG("executing hook %s\n", hook
->file
);
362 if (stat(hook
->file
, &s
))
365 if (!((unsigned long)s
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)))
368 if (!((unsigned long)s
.st_mode
& (S_IRUSR
| S_IRGRP
| S_IROTH
)))
374 hook_process
.pid
= fork();
375 if (hook_process
.pid
> 0) {
377 uloop_process_add(&hook_process
);
379 if (hook
->timeout
> 0)
380 uloop_timeout_set(&hook_process_timeout
, 1000 * hook
->timeout
);
384 DEBUG("uloop interrupted, killing hook process\n");
385 kill(hook_process
.pid
, SIGTERM
);
386 uloop_timeout_set(&hook_process_timeout
, 1000);
391 waitpid(hook_process
.pid
, NULL
, WCONTINUED
);
393 return hook_return_code
;
394 } else if (hook_process
.pid
== 0) {
396 execvpe(hook
->file
, hook
->argv
, hook
->envp
);
406 static int run_hooks(struct hook_execvpe
**hooklist
)
408 struct hook_execvpe
**cur
;
412 return 0; /* Nothing to do */
417 res
= run_hook(*cur
);
419 DEBUG(" error running hook %s\n", (*cur
)->file
);
421 DEBUG(" success running hook %s\n", (*cur
)->file
);
429 static int apply_sysctl(const char *jail_root
)
431 struct sysctl_val
**cur
;
432 char *procdir
, *fname
;
438 asprintf(&procdir
, "%s/proc", jail_root
);
442 mkdir(procdir
, 0700);
443 if (mount("proc", procdir
, "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0))
449 asprintf(&fname
, "%s/sys/%s", procdir
, (*cur
)->entry
);
453 DEBUG("sysctl: writing '%s' to %s\n", (*cur
)->value
, fname
);
455 f
= open(fname
, O_WRONLY
);
457 ERROR("sysctl: can't open %s\n", fname
);
460 write(f
, (*cur
)->value
, strlen((*cur
)->value
));
473 static int build_jail_fs(void)
475 char jail_root
[] = "/tmp/ujail-XXXXXX";
476 char tmpovdir
[] = "/tmp/ujail-overlay-XXXXXX";
477 char tmpdevdir
[] = "/tmp/ujail-XXXXXX/dev";
478 char tmpdevptsdir
[] = "/tmp/ujail-XXXXXX/dev/pts";
479 char *overlaydir
= NULL
;
481 if (mkdtemp(jail_root
) == NULL
) {
482 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
486 if (apply_sysctl(jail_root
)) {
487 ERROR("failed to apply sysctl values\n");
491 /* oldroot can't be MS_SHARED else pivot_root() fails */
492 if (mount("none", "/", NULL
, MS_REC
|MS_PRIVATE
, NULL
)) {
493 ERROR("private mount failed %m\n");
498 if (mount(opts
.extroot
, jail_root
, NULL
, MS_BIND
, NULL
)) {
499 ERROR("extroot mount failed %m\n");
503 if (mount("tmpfs", jail_root
, "tmpfs", MS_NOATIME
, "mode=0755")) {
504 ERROR("tmpfs mount failed %m\n");
509 if (opts
.tmpoverlaysize
) {
510 char mountoptsstr
[] = "mode=0755,size=XXXXXXXX";
512 snprintf(mountoptsstr
, sizeof(mountoptsstr
),
513 "mode=0755,size=%s", opts
.tmpoverlaysize
);
514 if (mkdtemp(tmpovdir
) == NULL
) {
515 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
518 if (mount("tmpfs", tmpovdir
, "tmpfs", MS_NOATIME
,
520 ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts
.tmpoverlaysize
);
523 overlaydir
= tmpovdir
;
527 overlaydir
= opts
.overlaydir
;
530 mount_overlay(jail_root
, overlaydir
);
532 if (chdir(jail_root
)) {
533 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root
);
537 snprintf(tmpdevdir
, sizeof(tmpdevdir
), "%s/dev", jail_root
);
538 mkdir_p(tmpdevdir
, 0755);
539 if (mount(NULL
, tmpdevdir
, "tmpfs", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, "size=1M"))
542 snprintf(tmpdevptsdir
, sizeof(tmpdevptsdir
), "%s/dev/pts", jail_root
);
543 mkdir_p(tmpdevptsdir
, 0755);
544 if (mount(NULL
, tmpdevptsdir
, "devpts", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, NULL
))
548 create_dev_console(jail_root
);
550 if (mount_all(jail_root
)) {
551 ERROR("mount_all() failed\n");
555 /* make sure /etc/resolv.conf exists if in new network namespace */
556 if (opts
.namespace & CLONE_NEWNET
) {
557 char jailetc
[PATH_MAX
], jaillink
[PATH_MAX
];
559 snprintf(jailetc
, PATH_MAX
, "%s/etc", jail_root
);
560 mkdir_p(jailetc
, 0755);
561 snprintf(jaillink
, PATH_MAX
, "%s/etc/resolv.conf", jail_root
);
565 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink
);
568 run_hooks(opts
.hooks
.createContainer
);
570 char dirbuf
[sizeof(jail_root
) + 4];
571 snprintf(dirbuf
, sizeof(dirbuf
), "%s/old", jail_root
);
574 if (pivot_root(jail_root
, dirbuf
) == -1) {
575 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root
, dirbuf
);
579 ERROR("chdir(/) (after pivot_root) failed: %m\n");
583 snprintf(dirbuf
, sizeof(dirbuf
), "/old%s", jail_root
);
584 umount2(dirbuf
, MNT_DETACH
);
586 if (opts
.tmpoverlaysize
) {
587 char tmpdirbuf
[sizeof(tmpovdir
) + 4];
588 snprintf(tmpdirbuf
, sizeof(tmpdirbuf
), "/old%s", tmpovdir
);
589 umount2(tmpdirbuf
, MNT_DETACH
);
593 umount2("/old", MNT_DETACH
);
597 mkdir("/proc", 0755);
598 mount("proc", "/proc", "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
600 * make /proc/sys read-only while keeping read-write to
601 * /proc/sys/net if CLONE_NEWNET is set.
603 if (opts
.namespace & CLONE_NEWNET
)
604 mount("/proc/sys/net", "/proc/self/net", NULL
, MS_BIND
, 0);
606 mount("/proc/sys", "/proc/sys", NULL
, MS_BIND
, 0);
607 mount(NULL
, "/proc/sys", NULL
, MS_REMOUNT
| MS_RDONLY
, 0);
608 mount(NULL
, "/proc", NULL
, MS_REMOUNT
, 0);
610 if (opts
.namespace & CLONE_NEWNET
)
611 mount("/proc/self/net", "/proc/sys/net", NULL
, MS_MOVE
, 0);
615 mount("sysfs", "/sys", "sysfs", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RDONLY
, 0);
618 mount(NULL
, "/", NULL
, MS_REMOUNT
| MS_BIND
| MS_RDONLY
, 0);
623 static int write_uid_gid_map(pid_t child_pid
, bool gidmap
, char *mapstr
)
628 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
629 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
632 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
635 if (dprintf(map_file
, "%s", mapstr
)) {
645 static int write_single_uid_gid_map(pid_t child_pid
, bool gidmap
, int id
)
649 const char *map_format
= "%d %d %d\n";
650 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
651 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
654 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
657 if (dprintf(map_file
, map_format
, 0, id
, 1) == -1) {
666 static int write_setgroups(pid_t child_pid
, bool allow
)
669 char setgroups_path
[64];
671 if (snprintf(setgroups_path
, sizeof(setgroups_path
), "/proc/%d/setgroups",
676 if ((setgroups_file
= open(setgroups_path
, O_WRONLY
)) == -1) {
680 if (dprintf(setgroups_file
, "%s", allow
?"allow":"deny") == -1) {
681 close(setgroups_file
);
685 close(setgroups_file
);
689 static void get_jail_user(int *user
, int *user_gid
, int *gr_gid
)
691 struct passwd
*p
= NULL
;
692 struct group
*g
= NULL
;
695 p
= getpwnam(opts
.user
);
697 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
698 opts
.user
, errno
, strerror(errno
));
702 *user_gid
= p
->pw_gid
;
709 g
= getgrnam(opts
.group
);
711 ERROR("failed to get gid for group %s: %m\n", opts
.group
);
720 static void set_jail_user(int pw_uid
, int user_gid
, int gr_gid
)
722 if (opts
.user
&& (user_gid
!= -1) && initgroups(opts
.user
, user_gid
)) {
723 ERROR("failed to initgroups() for user %s: %m\n", opts
.user
);
727 if ((gr_gid
!= -1) && setregid(gr_gid
, gr_gid
)) {
728 ERROR("failed to set group id %d: %m\n", gr_gid
);
732 if ((pw_uid
!= -1) && setreuid(pw_uid
, pw_uid
)) {
733 ERROR("failed to set user id %d: %m\n", pw_uid
);
739 static char** build_envp(const char *seccomp
, char **ocienvp
)
741 static char *envp
[MAX_ENVP
];
742 static char preload_var
[PATH_MAX
];
743 static char seccomp_var
[PATH_MAX
];
744 static char debug_var
[] = "LD_DEBUG=all";
745 static char container_var
[] = "container=ujail";
746 const char *preload_lib
= find_lib("libpreload-seccomp.so");
751 if (seccomp
&& !preload_lib
) {
752 ERROR("failed to add preload-lib to env\n");
756 snprintf(seccomp_var
, sizeof(seccomp_var
), "SECCOMP_FILE=%s", seccomp
);
757 envp
[count
++] = seccomp_var
;
758 snprintf(preload_var
, sizeof(preload_var
), "LD_PRELOAD=%s", preload_lib
);
759 envp
[count
++] = preload_var
;
762 envp
[count
++] = container_var
;
765 envp
[count
++] = debug_var
;
768 while (addenv
&& *addenv
) {
769 envp
[count
++] = *(addenv
++);
770 if (count
>= MAX_ENVP
) {
771 ERROR("environment limited to %d extra records, truncating\n", MAX_ENVP
);
778 static void usage(void)
780 fprintf(stderr
, "ujail <options> -- <binary> <params ...>\n");
781 fprintf(stderr
, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
782 fprintf(stderr
, " -S <file>\tseccomp filter config\n");
783 fprintf(stderr
, " -C <file>\tcapabilities drop config\n");
784 fprintf(stderr
, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
785 fprintf(stderr
, " -n <name>\tthe name of the jail\n");
786 fprintf(stderr
, "namespace jail options:\n");
787 fprintf(stderr
, " -h <hostname>\tchange the hostname of the jail\n");
788 fprintf(stderr
, " -N\t\tjail has network namespace\n");
789 fprintf(stderr
, " -f\t\tjail has user namespace\n");
790 fprintf(stderr
, " -F\t\tjail has cgroups namespace\n");
791 fprintf(stderr
, " -r <file>\treadonly files that should be staged\n");
792 fprintf(stderr
, " -w <file>\twriteable files that should be staged\n");
793 fprintf(stderr
, " -p\t\tjail has /proc\n");
794 fprintf(stderr
, " -s\t\tjail has /sys\n");
795 fprintf(stderr
, " -l\t\tjail has /dev/log\n");
796 fprintf(stderr
, " -u\t\tjail has a ubus socket\n");
797 fprintf(stderr
, " -U <name>\tuser to run jailed process\n");
798 fprintf(stderr
, " -G <name>\tgroup to run jailed process\n");
799 fprintf(stderr
, " -o\t\tremont jail root (/) read only\n");
800 fprintf(stderr
, " -R <dir>\texternal jail rootfs (system container)\n");
801 fprintf(stderr
, " -O <dir>\tdirectory for r/w overlayfs\n");
802 fprintf(stderr
, " -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
803 fprintf(stderr
, " -E\t\tfail if jail cannot be setup\n");
804 fprintf(stderr
, " -y\t\tprovide jail console\n");
805 fprintf(stderr
, " -J <dir>\tstart OCI bundle\n");
806 fprintf(stderr
, "\nWarning: by default root inside the jail is the same\n\
807 and he has the same powers as root outside the jail,\n\
808 thus he can escape the jail and/or break stuff.\n\
809 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
810 If you use none of the namespace jail options,\n\
811 ujail will not use namespace/build a jail,\n\
812 and will only drop capabilities/apply seccomp filter.\n\n");
815 static int exec_jail(void *pipes_ptr
)
817 int *pipes
= (int*)pipes_ptr
;
819 int pw_uid
, pw_gid
, gr_gid
;
825 if (write(pipes
[1], buf
, 1) < 1) {
826 ERROR("can't write to parent\n");
829 if (read(pipes
[2], buf
, 1) < 1) {
830 ERROR("can't read from parent\n");
834 ERROR("parent had an error, child exiting\n");
841 if (opts
.namespace & CLONE_NEWUSER
) {
842 if (setregid(0, 0) < 0) {
846 if (setreuid(0, 0) < 0) {
850 if (setgroups(0, NULL
) < 0) {
851 ERROR("setgroups\n");
856 if (opts
.namespace && opts
.hostname
&& strlen(opts
.hostname
) > 0
857 && sethostname(opts
.hostname
, strlen(opts
.hostname
))) {
858 ERROR("sethostname(%s) failed: %m\n", opts
.hostname
);
862 if ((opts
.namespace & CLONE_NEWNS
) && build_jail_fs()) {
863 ERROR("failed to build jail fs\n");
866 run_hooks(opts
.hooks
.startContainer
);
868 if (!(opts
.namespace & CLONE_NEWUSER
)) {
869 get_jail_user(&pw_uid
, &pw_gid
, &gr_gid
);
871 set_jail_user(opts
.pw_uid
?:pw_uid
, opts
.pw_gid
?:pw_gid
, opts
.gr_gid
?:gr_gid
);
874 if (opts
.additional_gids
&&
875 (setgroups(opts
.num_additional_gids
, opts
.additional_gids
) < 0)) {
876 ERROR("setgroups failed: %m\n");
883 if (applyOCIcapabilities(opts
.capset
))
886 if (opts
.capabilities
&& drop_capabilities(opts
.capabilities
))
889 if (opts
.no_new_privs
&& prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
890 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
894 char **envp
= build_envp(opts
.seccomp
, opts
.envp
);
898 if (opts
.cwd
&& chdir(opts
.cwd
))
901 if (opts
.ociseccomp
&& applyOCIlinuxseccomp(opts
.ociseccomp
))
906 INFO("exec-ing %s\n", *opts
.jail_argv
);
907 if (opts
.envp
) /* respect PATH if potentially set in ENV */
908 execvpe(*opts
.jail_argv
, opts
.jail_argv
, envp
);
910 execve(*opts
.jail_argv
, opts
.jail_argv
, envp
);
912 /* we get there only if execve fails */
913 ERROR("failed to execve %s: %m\n", *opts
.jail_argv
);
917 static int jail_running
= 0;
918 static int jail_return_code
= 0;
920 static void jail_process_timeout_cb(struct uloop_timeout
*t
);
921 static struct uloop_timeout jail_process_timeout
= {
922 .cb
= jail_process_timeout_cb
,
925 static void jail_process_handler(struct uloop_process
*c
, int ret
)
927 uloop_timeout_cancel(&jail_process_timeout
);
928 if (WIFEXITED(ret
)) {
929 jail_return_code
= WEXITSTATUS(ret
);
930 INFO("jail (%d) exited with exit: %d\n", c
->pid
, jail_return_code
);
932 jail_return_code
= WTERMSIG(ret
);
933 INFO("jail (%d) exited with signal: %d\n", c
->pid
, jail_return_code
);
939 static struct uloop_process jail_process
= {
940 .cb
= jail_process_handler
,
943 static void jail_process_timeout_cb(struct uloop_timeout
*t
)
945 DEBUG("jail process failed to stop, sending SIGKILL\n");
946 kill(jail_process
.pid
, SIGKILL
);
949 static void jail_handle_signal(int signo
)
952 DEBUG("forwarding signal %d to the hook process\n", signo
);
953 kill(hook_process
.pid
, signo
);
957 DEBUG("forwarding signal %d to the jailed process\n", signo
);
958 kill(jail_process
.pid
, signo
);
962 static int netns_open_pid(const pid_t target_ns
)
964 char pid_net_path
[PATH_MAX
];
966 snprintf(pid_net_path
, sizeof(pid_net_path
), "/proc/%u/ns/net", target_ns
);
968 return open(pid_net_path
, O_RDONLY
);
971 static void netns_updown(pid_t pid
, bool start
)
973 struct ubus_context
*ctx
= ubus_connect(NULL
);
974 static struct blob_buf req
;
980 blob_buf_init(&req
, 0);
981 blobmsg_add_string(&req
, "jail", opts
.name
);
982 blobmsg_add_u32(&req
, "pid", pid
);
983 blobmsg_add_u8(&req
, "start", start
);
985 if (ubus_lookup_id(ctx
, "network", &id
) ||
986 ubus_invoke(ctx
, id
, "netns_updown", req
.head
, NULL
, NULL
, 3000))
987 INFO("ubus request failed\n");
993 static int parseOCIenvarray(struct blob_attr
*msg
, char ***envp
)
995 struct blob_attr
*cur
;
998 blobmsg_for_each_attr(cur
, msg
, rem
)
1002 *envp
= calloc(1 + sz
, sizeof(char*));
1011 blobmsg_for_each_attr(cur
, msg
, rem
)
1012 (*envp
)[sz
++] = strdup(blobmsg_get_string(cur
));
1026 static const struct blobmsg_policy oci_root_policy
[] = {
1027 [OCI_ROOT_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
1028 [OCI_ROOT_READONLY
] = { "readonly", BLOBMSG_TYPE_BOOL
},
1031 static int parseOCIroot(const char *jsonfile
, struct blob_attr
*msg
)
1033 static char rootpath
[PATH_MAX
] = { 0 };
1034 struct blob_attr
*tb
[__OCI_ROOT_MAX
];
1037 blobmsg_parse(oci_root_policy
, __OCI_ROOT_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1039 if (!tb
[OCI_ROOT_PATH
])
1042 strncpy(rootpath
, jsonfile
, PATH_MAX
);
1043 cur
= strrchr(rootpath
, '/');
1049 strncat(rootpath
, blobmsg_get_string(tb
[OCI_ROOT_PATH
]), PATH_MAX
- (strlen(rootpath
) + 1));
1051 opts
.extroot
= rootpath
;
1053 opts
.ronly
= blobmsg_get_bool(tb
[OCI_ROOT_READONLY
]);
1067 static const struct blobmsg_policy oci_hook_policy
[] = {
1068 [OCI_HOOK_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
1069 [OCI_HOOK_ARGS
] = { "args", BLOBMSG_TYPE_ARRAY
},
1070 [OCI_HOOK_ENV
] = { "env", BLOBMSG_TYPE_ARRAY
},
1071 [OCI_HOOK_TIMEOUT
] = { "timeout", BLOBMSG_TYPE_INT32
},
1075 static int parseOCIhook(struct hook_execvpe
***hooklist
, struct blob_attr
*msg
)
1077 struct blob_attr
*tb
[__OCI_HOOK_MAX
];
1078 struct blob_attr
*cur
;
1082 blobmsg_for_each_attr(cur
, msg
, rem
)
1088 *hooklist
= calloc(idx
+ 1, sizeof(struct hook_execvpe
*));
1094 blobmsg_for_each_attr(cur
, msg
, rem
) {
1095 blobmsg_parse(oci_hook_policy
, __OCI_HOOK_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1097 if (!tb
[OCI_HOOK_PATH
]) {
1102 (*hooklist
)[idx
] = malloc(sizeof(struct hook_execvpe
));
1103 if (tb
[OCI_HOOK_ARGS
]) {
1104 ret
= parseOCIenvarray(tb
[OCI_HOOK_ARGS
], &((*hooklist
)[idx
]->argv
));
1108 (*hooklist
)[idx
]->argv
= calloc(2, sizeof(char *));
1109 ((*hooklist
)[idx
]->argv
)[0] = strdup(blobmsg_get_string(tb
[OCI_HOOK_PATH
]));
1110 ((*hooklist
)[idx
]->argv
)[1] = NULL
;
1114 if (tb
[OCI_HOOK_ENV
]) {
1115 ret
= parseOCIenvarray(tb
[OCI_HOOK_ENV
], &((*hooklist
)[idx
]->envp
));
1120 if (tb
[OCI_HOOK_TIMEOUT
])
1121 (*hooklist
)[idx
]->timeout
= blobmsg_get_u32(tb
[OCI_HOOK_TIMEOUT
]);
1123 (*hooklist
)[idx
]->file
= strdup(blobmsg_get_string(tb
[OCI_HOOK_PATH
]));
1128 (*hooklist
)[idx
] = NULL
;
1130 DEBUG("added %d hooks\n", idx
);
1135 free_hooklist(*hooklist
);
1144 OCI_HOOKS_CREATERUNTIME
,
1145 OCI_HOOKS_CREATECONTAINER
,
1146 OCI_HOOKS_STARTCONTAINER
,
1147 OCI_HOOKS_POSTSTART
,
1152 static const struct blobmsg_policy oci_hooks_policy
[] = {
1153 [OCI_HOOKS_PRESTART
] = { "prestart", BLOBMSG_TYPE_ARRAY
},
1154 [OCI_HOOKS_CREATERUNTIME
] = { "createRuntime", BLOBMSG_TYPE_ARRAY
},
1155 [OCI_HOOKS_CREATECONTAINER
] = { "createContainer", BLOBMSG_TYPE_ARRAY
},
1156 [OCI_HOOKS_STARTCONTAINER
] = { "startContainer", BLOBMSG_TYPE_ARRAY
},
1157 [OCI_HOOKS_POSTSTART
] = { "poststart", BLOBMSG_TYPE_ARRAY
},
1158 [OCI_HOOKS_POSTSTOP
] = { "poststop", BLOBMSG_TYPE_ARRAY
},
1161 static int parseOCIhooks(struct blob_attr
*msg
)
1163 struct blob_attr
*tb
[__OCI_HOOKS_MAX
];
1166 blobmsg_parse(oci_hooks_policy
, __OCI_HOOKS_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1168 if (tb
[OCI_HOOKS_PRESTART
])
1169 INFO("warning: ignoring deprecated prestart hook\n");
1171 if (tb
[OCI_HOOKS_CREATERUNTIME
]) {
1172 ret
= parseOCIhook(&opts
.hooks
.createRuntime
, tb
[OCI_HOOKS_CREATERUNTIME
]);
1177 if (tb
[OCI_HOOKS_CREATECONTAINER
]) {
1178 ret
= parseOCIhook(&opts
.hooks
.createContainer
, tb
[OCI_HOOKS_CREATECONTAINER
]);
1180 goto out_createruntime
;
1183 if (tb
[OCI_HOOKS_STARTCONTAINER
]) {
1184 ret
= parseOCIhook(&opts
.hooks
.startContainer
, tb
[OCI_HOOKS_STARTCONTAINER
]);
1186 goto out_createcontainer
;
1189 if (tb
[OCI_HOOKS_POSTSTART
]) {
1190 ret
= parseOCIhook(&opts
.hooks
.poststart
, tb
[OCI_HOOKS_POSTSTART
]);
1192 goto out_startcontainer
;
1195 if (tb
[OCI_HOOKS_POSTSTOP
]) {
1196 ret
= parseOCIhook(&opts
.hooks
.poststop
, tb
[OCI_HOOKS_POSTSTOP
]);
1204 free_hooklist(opts
.hooks
.poststart
);
1206 free_hooklist(opts
.hooks
.startContainer
);
1207 out_createcontainer
:
1208 free_hooklist(opts
.hooks
.createContainer
);
1210 free_hooklist(opts
.hooks
.createRuntime
);
1217 OCI_PROCESS_USER_UID
,
1218 OCI_PROCESS_USER_GID
,
1219 OCI_PROCESS_USER_UMASK
,
1220 OCI_PROCESS_USER_ADDITIONALGIDS
,
1221 __OCI_PROCESS_USER_MAX
,
1224 static const struct blobmsg_policy oci_process_user_policy
[] = {
1225 [OCI_PROCESS_USER_UID
] = { "uid", BLOBMSG_TYPE_INT32
},
1226 [OCI_PROCESS_USER_GID
] = { "gid", BLOBMSG_TYPE_INT32
},
1227 [OCI_PROCESS_USER_UMASK
] = { "umask", BLOBMSG_TYPE_INT32
},
1228 [OCI_PROCESS_USER_ADDITIONALGIDS
] = { "additionalGids", BLOBMSG_TYPE_ARRAY
},
1231 static int parseOCIprocessuser(struct blob_attr
*msg
) {
1232 struct blob_attr
*tb
[__OCI_PROCESS_USER_MAX
];
1233 struct blob_attr
*cur
;
1237 blobmsg_parse(oci_process_user_policy
, __OCI_PROCESS_USER_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1239 if (tb
[OCI_PROCESS_USER_UID
])
1240 opts
.pw_uid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_UID
]);
1242 if (tb
[OCI_PROCESS_USER_GID
]) {
1243 opts
.pw_gid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_GID
]);
1244 opts
.gr_gid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_GID
]);
1248 if (tb
[OCI_PROCESS_USER_ADDITIONALGIDS
]) {
1251 blobmsg_for_each_attr(cur
, tb
[OCI_PROCESS_USER_ADDITIONALGIDS
], rem
) {
1253 if (has_gid
&& (blobmsg_get_u32(cur
) == opts
.gr_gid
))
1258 opts
.additional_gids
= calloc(gidcnt
+ has_gid
, sizeof(gid_t
));
1261 /* always add primary GID to set of GIDs if set */
1263 opts
.additional_gids
[gidcnt
++] = opts
.gr_gid
;
1265 blobmsg_for_each_attr(cur
, tb
[OCI_PROCESS_USER_ADDITIONALGIDS
], rem
) {
1266 if (has_gid
&& (blobmsg_get_u32(cur
) == opts
.gr_gid
))
1268 opts
.additional_gids
[gidcnt
++] = blobmsg_get_u32(cur
);
1270 opts
.num_additional_gids
= gidcnt
;
1272 DEBUG("read %lu additional groups\n", gidcnt
);
1275 if (tb
[OCI_PROCESS_USER_UMASK
]) {
1276 opts
.umask
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_UMASK
]);
1277 opts
.set_umask
= true;
1285 OCI_PROCESS_CAPABILITIES
,
1288 OCI_PROCESS_NONEWPRIVILEGES
,
1289 OCI_PROCESS_RLIMITS
,
1290 OCI_PROCESS_TERMINAL
,
1295 static const struct blobmsg_policy oci_process_policy
[] = {
1296 [OCI_PROCESS_ARGS
] = { "args", BLOBMSG_TYPE_ARRAY
},
1297 [OCI_PROCESS_CAPABILITIES
] = { "capabilities", BLOBMSG_TYPE_TABLE
},
1298 [OCI_PROCESS_CWD
] = { "cwd", BLOBMSG_TYPE_STRING
},
1299 [OCI_PROCESS_ENV
] = { "env", BLOBMSG_TYPE_ARRAY
},
1300 [OCI_PROCESS_NONEWPRIVILEGES
] = { "noNewPrivileges", BLOBMSG_TYPE_BOOL
},
1301 [OCI_PROCESS_RLIMITS
] = { "rlimits", BLOBMSG_TYPE_ARRAY
},
1302 [OCI_PROCESS_TERMINAL
] = { "terminal", BLOBMSG_TYPE_BOOL
},
1303 [OCI_PROCESS_USER
] = { "user", BLOBMSG_TYPE_TABLE
},
1307 static int parseOCIprocess(struct blob_attr
*msg
)
1309 struct blob_attr
*tb
[__OCI_PROCESS_MAX
];
1312 blobmsg_parse(oci_process_policy
, __OCI_PROCESS_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1314 if (!tb
[OCI_PROCESS_ARGS
])
1317 res
= parseOCIenvarray(tb
[OCI_PROCESS_ARGS
], &opts
.jail_argv
);
1321 opts
.console
= blobmsg_get_bool(tb
[OCI_PROCESS_TERMINAL
]);
1322 opts
.no_new_privs
= blobmsg_get_bool(tb
[OCI_PROCESS_NONEWPRIVILEGES
]);
1324 if (tb
[OCI_PROCESS_CWD
])
1325 opts
.cwd
= strdup(blobmsg_get_string(tb
[OCI_PROCESS_CWD
]));
1327 if (tb
[OCI_PROCESS_ENV
]) {
1328 res
= parseOCIenvarray(tb
[OCI_PROCESS_ENV
], &opts
.envp
);
1333 if (tb
[OCI_PROCESS_USER
] && (res
= parseOCIprocessuser(tb
[OCI_PROCESS_USER
])))
1336 if (tb
[OCI_PROCESS_CAPABILITIES
] &&
1337 (res
= parseOCIcapabilities(&opts
.capset
, tb
[OCI_PROCESS_CAPABILITIES
])))
1346 OCI_LINUX_NAMESPACE_TYPE
,
1347 OCI_LINUX_NAMESPACE_PATH
,
1348 __OCI_LINUX_NAMESPACE_MAX
,
1351 static const struct blobmsg_policy oci_linux_namespace_policy
[] = {
1352 [OCI_LINUX_NAMESPACE_TYPE
] = { "type", BLOBMSG_TYPE_STRING
},
1353 [OCI_LINUX_NAMESPACE_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
1356 static unsigned int resolve_nstype(char *type
) {
1357 if (!strcmp("pid", type
))
1358 return CLONE_NEWPID
;
1359 else if (!strcmp("network", type
))
1360 return CLONE_NEWNET
;
1361 else if (!strcmp("mount", type
))
1363 else if (!strcmp("ipc", type
))
1364 return CLONE_NEWIPC
;
1365 else if (!strcmp("uts", type
))
1366 return CLONE_NEWUTS
;
1367 else if (!strcmp("user", type
))
1368 return CLONE_NEWUSER
;
1369 else if (!strcmp("cgroup", type
))
1370 return CLONE_NEWCGROUP
;
1375 static int parseOCIlinuxns(struct blob_attr
*msg
)
1377 struct blob_attr
*tb
[__OCI_LINUX_NAMESPACE_MAX
];
1379 blobmsg_parse(oci_linux_namespace_policy
, __OCI_LINUX_NAMESPACE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1381 if (!tb
[OCI_LINUX_NAMESPACE_TYPE
])
1384 if (tb
[OCI_LINUX_NAMESPACE_PATH
])
1385 return ENOTSUP
; /* ToDo */
1387 opts
.namespace |= resolve_nstype(blobmsg_get_string(tb
[OCI_LINUX_NAMESPACE_TYPE
]));
1394 OCI_LINUX_UIDGIDMAP_CONTAINERID
,
1395 OCI_LINUX_UIDGIDMAP_HOSTID
,
1396 OCI_LINUX_UIDGIDMAP_SIZE
,
1397 __OCI_LINUX_UIDGIDMAP_MAX
,
1400 static const struct blobmsg_policy oci_linux_uidgidmap_policy
[] = {
1401 [OCI_LINUX_UIDGIDMAP_CONTAINERID
] = { "containerID", BLOBMSG_TYPE_INT32
},
1402 [OCI_LINUX_UIDGIDMAP_HOSTID
] = { "hostID", BLOBMSG_TYPE_INT32
},
1403 [OCI_LINUX_UIDGIDMAP_SIZE
] = { "size", BLOBMSG_TYPE_INT32
},
1406 static int parseOCIuidgidmappings(struct blob_attr
*msg
, bool is_gidmap
)
1408 const char *map_format
= "%d %d %d\n";
1409 struct blob_attr
*tb
[__OCI_LINUX_UIDGIDMAP_MAX
];
1410 struct blob_attr
*cur
;
1414 unsigned int cnt
= 0;
1415 size_t totallen
= 0;
1417 /* count number of mappings */
1418 blobmsg_for_each_attr(cur
, msg
, rem
)
1424 /* allocate array for mappings */
1425 mappings
= calloc(1 + cnt
, sizeof(char*));
1429 mappings
[cnt
] = NULL
;
1432 blobmsg_for_each_attr(cur
, msg
, rem
) {
1433 blobmsg_parse(oci_linux_uidgidmap_policy
, __OCI_LINUX_UIDGIDMAP_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1435 if (!tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
] ||
1436 !tb
[OCI_LINUX_UIDGIDMAP_HOSTID
] ||
1437 !tb
[OCI_LINUX_UIDGIDMAP_SIZE
])
1440 /* write mapping line into allocated string */
1441 len
= asprintf(&mappings
[cnt
++], map_format
,
1442 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
]),
1443 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_HOSTID
]),
1444 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_SIZE
]));
1452 /* allocate combined mapping string */
1453 map
= calloc(1 + totallen
, sizeof(char));
1459 /* concatenate mapping strings into combined string */
1460 curstr
= mappings
[0];
1462 strcat(map
, curstr
);
1476 OCI_LINUX_RESOURCES
,
1479 OCI_LINUX_NAMESPACES
,
1480 OCI_LINUX_UIDMAPPINGS
,
1481 OCI_LINUX_GIDMAPPINGS
,
1482 OCI_LINUX_MASKEDPATHS
,
1483 OCI_LINUX_READONLYPATHS
,
1484 OCI_LINUX_ROOTFSPROPAGATION
,
1488 static const struct blobmsg_policy oci_linux_policy
[] = {
1489 [OCI_LINUX_RESOURCES
] = { "resources", BLOBMSG_TYPE_TABLE
},
1490 [OCI_LINUX_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_TABLE
},
1491 [OCI_LINUX_SYSCTL
] = { "sysctl", BLOBMSG_TYPE_TABLE
},
1492 [OCI_LINUX_NAMESPACES
] = { "namespaces", BLOBMSG_TYPE_ARRAY
},
1493 [OCI_LINUX_UIDMAPPINGS
] = { "uidMappings", BLOBMSG_TYPE_ARRAY
},
1494 [OCI_LINUX_GIDMAPPINGS
] = { "gidMappings", BLOBMSG_TYPE_ARRAY
},
1495 [OCI_LINUX_MASKEDPATHS
] = { "maskedPaths", BLOBMSG_TYPE_ARRAY
},
1496 [OCI_LINUX_READONLYPATHS
] = { "readonlyPaths", BLOBMSG_TYPE_ARRAY
},
1497 [OCI_LINUX_ROOTFSPROPAGATION
] = { "rootfsPropagation", BLOBMSG_TYPE_STRING
},
1500 static int parseOCIsysctl(struct blob_attr
*msg
)
1502 struct blob_attr
*cur
;
1507 blobmsg_for_each_attr(cur
, msg
, rem
) {
1508 if (!blobmsg_name(cur
) || !blobmsg_get_string(cur
))
1517 opts
.sysctl
= calloc(cnt
+ 1, sizeof(struct sysctl_val
*));
1522 blobmsg_for_each_attr(cur
, msg
, rem
) {
1523 opts
.sysctl
[cnt
] = malloc(sizeof(struct sysctl_val
));
1524 if (!opts
.sysctl
[cnt
])
1527 /* replace '.' with '/' in entry name */
1528 tc
= tmp
= strdup(blobmsg_name(cur
));
1529 while ((tc
= strchr(tc
, '.')))
1532 opts
.sysctl
[cnt
]->value
= strdup(blobmsg_get_string(cur
));
1533 opts
.sysctl
[cnt
]->entry
= tmp
;
1538 opts
.sysctl
[cnt
] = NULL
;
1543 static int parseOCIlinux(struct blob_attr
*msg
)
1545 struct blob_attr
*tb
[__OCI_LINUX_MAX
];
1546 struct blob_attr
*cur
;
1550 blobmsg_parse(oci_linux_policy
, __OCI_LINUX_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1552 if (tb
[OCI_LINUX_NAMESPACES
]) {
1553 blobmsg_for_each_attr(cur
, tb
[OCI_LINUX_NAMESPACES
], rem
) {
1554 res
= parseOCIlinuxns(cur
);
1560 if (tb
[OCI_LINUX_UIDMAPPINGS
]) {
1561 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 0);
1566 if (tb
[OCI_LINUX_GIDMAPPINGS
]) {
1567 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 1);
1572 if (tb
[OCI_LINUX_READONLYPATHS
]) {
1573 blobmsg_for_each_attr(cur
, tb
[OCI_LINUX_READONLYPATHS
], rem
) {
1574 res
= add_mount(NULL
, blobmsg_get_string(cur
), NULL
, MS_BIND
| MS_REC
| MS_RDONLY
, NULL
, 0);
1580 if (tb
[OCI_LINUX_MASKEDPATHS
]) {
1581 blobmsg_for_each_attr(cur
, tb
[OCI_LINUX_MASKEDPATHS
], rem
) {
1582 res
= add_mount((void *)(-1), blobmsg_get_string(cur
), NULL
, 0, NULL
, 1);
1588 if (tb
[OCI_LINUX_SYSCTL
]) {
1589 res
= parseOCIsysctl(tb
[OCI_LINUX_SYSCTL
]);
1594 if (tb
[OCI_LINUX_SECCOMP
]) {
1595 opts
.ociseccomp
= parseOCIlinuxseccomp(tb
[OCI_LINUX_SECCOMP
]);
1596 if (!opts
.ociseccomp
)
1614 static const struct blobmsg_policy oci_policy
[] = {
1615 [OCI_VERSION
] = { "ociVersion", BLOBMSG_TYPE_STRING
},
1616 [OCI_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
1617 [OCI_PROCESS
] = { "process", BLOBMSG_TYPE_TABLE
},
1618 [OCI_ROOT
] = { "root", BLOBMSG_TYPE_TABLE
},
1619 [OCI_MOUNTS
] = { "mounts", BLOBMSG_TYPE_ARRAY
},
1620 [OCI_HOOKS
] = { "hooks", BLOBMSG_TYPE_TABLE
},
1621 [OCI_LINUX
] = { "linux", BLOBMSG_TYPE_TABLE
},
1624 static int parseOCI(const char *jsonfile
)
1626 struct blob_attr
*tb
[__OCI_MAX
];
1627 struct blob_attr
*cur
;
1631 blob_buf_init(&ocibuf
, 0);
1632 if (!blobmsg_add_json_from_file(&ocibuf
, jsonfile
))
1635 blobmsg_parse(oci_policy
, __OCI_MAX
, tb
, blob_data(ocibuf
.head
), blob_len(ocibuf
.head
));
1637 if (!tb
[OCI_VERSION
])
1640 if (strncmp("1.0", blobmsg_get_string(tb
[OCI_VERSION
]), 3)) {
1641 ERROR("unsupported ociVersion %s\n", blobmsg_get_string(tb
[OCI_VERSION
]));
1645 if (tb
[OCI_HOSTNAME
])
1646 opts
.hostname
= strdup(blobmsg_get_string(tb
[OCI_HOSTNAME
]));
1648 if (!tb
[OCI_PROCESS
])
1651 if ((res
= parseOCIprocess(tb
[OCI_PROCESS
])))
1657 if ((res
= parseOCIroot(jsonfile
, tb
[OCI_ROOT
])))
1660 if (!tb
[OCI_MOUNTS
])
1663 blobmsg_for_each_attr(cur
, tb
[OCI_MOUNTS
], rem
)
1664 if ((res
= parseOCImount(cur
)))
1667 if (tb
[OCI_LINUX
] && (res
= parseOCIlinux(tb
[OCI_LINUX
])))
1670 if (tb
[OCI_HOOKS
] && (res
= parseOCIhooks(tb
[OCI_HOOKS
])))
1673 blob_buf_free(&ocibuf
);
1678 int main(int argc
, char **argv
)
1681 uid_t uid
= getuid();
1682 const char log
[] = "/dev/log";
1683 const char ubus
[] = "/var/run/ubus.sock";
1684 char *jsonfile
= NULL
;
1691 ERROR("not root, aborting: %m\n");
1692 return EXIT_FAILURE
;
1697 init_library_search();
1699 while ((ch
= getopt(argc
, argv
, OPT_ARGS
)) != -1) {
1702 debug
= atoi(optarg
);
1705 opts
.namespace |= CLONE_NEWNS
;
1709 opts
.namespace |= CLONE_NEWNS
;
1713 opts
.namespace |= CLONE_NEWUSER
;
1716 opts
.namespace |= CLONE_NEWCGROUP
;
1719 opts
.extroot
= strdup(optarg
);
1722 opts
.namespace |= CLONE_NEWNS
;
1726 opts
.seccomp
= optarg
;
1727 add_mount_bind(optarg
, 1, -1);
1730 opts
.capabilities
= optarg
;
1733 opts
.no_new_privs
= 1;
1739 opts
.namespace |= CLONE_NEWNET
;
1742 opts
.namespace |= CLONE_NEWUTS
;
1743 opts
.hostname
= strdup(optarg
);
1746 opts
.namespace |= CLONE_NEWNS
;
1747 add_path_and_deps(optarg
, 1, 0, 0);
1750 opts
.namespace |= CLONE_NEWNS
;
1751 add_path_and_deps(optarg
, 0, 0, 0);
1754 opts
.namespace |= CLONE_NEWNS
;
1755 add_mount_bind(ubus
, 0, -1);
1758 opts
.namespace |= CLONE_NEWNS
;
1759 add_mount_bind(log
, 0, -1);
1765 opts
.group
= optarg
;
1768 opts
.overlaydir
= optarg
;
1771 opts
.tmpoverlaysize
= optarg
;
1774 opts
.require_jail
= 1;
1780 asprintf(&jsonfile
, "%s/config.json", optarg
);
1786 opts
.namespace |= CLONE_NEWIPC
| CLONE_NEWPID
;
1790 ocires
= parseOCI(jsonfile
);
1793 ERROR("parsing of OCI JSON spec has failed: %s (%d)\n", strerror(ocires
), ocires
);
1798 if (opts
.tmpoverlaysize
&& strlen(opts
.tmpoverlaysize
) > 8) {
1799 ERROR("size parameter too long: \"%s\"\n", opts
.tmpoverlaysize
);
1803 /* no <binary> param found */
1804 if (!jsonfile
&& (argc
- optind
< 1)) {
1806 return EXIT_FAILURE
;
1808 if (!(opts
.namespace||opts
.capabilities
||opts
.seccomp
)) {
1809 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
1811 return EXIT_FAILURE
;
1813 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
1815 opts
.capabilities
!= 0 || opts
.capset
.apply
,
1816 opts
.seccomp
!= 0 || opts
.ociseccomp
!= 0);
1819 /* allocate NULL-terminated array for argv */
1820 opts
.jail_argv
= calloc(1 + argc
- optind
, sizeof(char**));
1821 if (!opts
.jail_argv
)
1822 return EXIT_FAILURE
;
1824 for (size_t s
= optind
; s
< argc
; s
++)
1825 opts
.jail_argv
[s
- optind
] = strdup(argv
[s
]);
1827 if (opts
.namespace & CLONE_NEWUSER
)
1828 get_jail_user(&opts
.pw_uid
, &opts
.pw_gid
, &opts
.gr_gid
);
1831 if (!opts
.extroot
) {
1832 if (opts
.namespace && add_path_and_deps(*opts
.jail_argv
, 1, -1, 0)) {
1833 ERROR("failed to load dependencies\n");
1838 if (opts
.namespace && opts
.seccomp
&& add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
1839 ERROR("failed to load libpreload-seccomp.so\n");
1841 if (opts
.require_jail
)
1846 prctl(PR_SET_NAME
, opts
.name
, NULL
, NULL
, NULL
);
1848 sigfillset(&sigmask
);
1849 for (i
= 0; i
< _NSIG
; i
++) {
1850 struct sigaction s
= { 0 };
1852 if (!sigismember(&sigmask
, i
))
1854 if ((i
== SIGCHLD
) || (i
== SIGPIPE
) || (i
== SIGSEGV
))
1857 s
.sa_handler
= jail_handle_signal
;
1858 sigaction(i
, &s
, NULL
);
1861 if (opts
.namespace) {
1862 if (opts
.namespace & CLONE_NEWNS
) {
1863 add_mount_bind("/dev/full", 0, -1);
1864 add_mount_bind("/dev/null", 0, -1);
1865 add_mount_bind("/dev/random", 0, -1);
1866 add_mount_bind("/dev/urandom", 0, -1);
1867 add_mount_bind("/dev/zero", 0, -1);
1868 add_mount_bind("/dev/ptmx", 0, -1);
1869 add_mount_bind("/dev/tty", 0, -1);
1871 if (!opts
.extroot
&& (opts
.user
|| opts
.group
)) {
1872 add_mount_bind("/etc/passwd", 0, -1);
1873 add_mount_bind("/etc/group", 0, -1);
1876 #if defined(__GLIBC__)
1878 add_mount_bind("/etc/nsswitch.conf", 0, -1);
1881 if (!(opts
.namespace & CLONE_NEWNET
)) {
1882 add_mount_bind("/etc/resolv.conf", 0, -1);
1884 char hostdir
[PATH_MAX
];
1886 snprintf(hostdir
, PATH_MAX
, "/tmp/resolv.conf-%s.d", opts
.name
);
1887 mkdir_p(hostdir
, 0755);
1888 add_mount(hostdir
, "/tmp/resolv.conf.d", NULL
, MS_BIND
| MS_NOEXEC
| MS_NOATIME
| MS_NOSUID
| MS_NODEV
| MS_RDONLY
, NULL
, -1);
1892 if (pipe(&pipes
[0]) < 0 || pipe(&pipes
[2]) < 0)
1895 jail_process
.pid
= clone(exec_jail
, child_stack
+ STACK_SIZE
, SIGCHLD
| opts
.namespace, &pipes
);
1897 jail_process
.pid
= fork();
1900 if (jail_process
.pid
> 0) {
1903 /* parent process */
1906 run_hooks(opts
.hooks
.createRuntime
);
1907 if (read(pipes
[0], sig_buf
, 1) < 1) {
1908 ERROR("can't read from child\n");
1912 if (opts
.namespace & CLONE_NEWUSER
) {
1913 if (write_setgroups(jail_process
.pid
, true)) {
1914 ERROR("can't write setgroups\n");
1918 bool has_gr
= (opts
.gr_gid
!= -1);
1919 if (opts
.pw_uid
!= -1) {
1920 write_single_uid_gid_map(jail_process
.pid
, 0, opts
.pw_uid
);
1921 write_single_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:opts
.pw_gid
);
1923 write_single_uid_gid_map(jail_process
.pid
, 0, 65534);
1924 write_single_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:65534);
1927 write_uid_gid_map(jail_process
.pid
, 0, opts
.uidmap
);
1929 write_uid_gid_map(jail_process
.pid
, 1, opts
.gidmap
);
1933 if (opts
.namespace & CLONE_NEWNET
) {
1935 ERROR("netns needs a named jail\n");
1938 netns_fd
= netns_open_pid(jail_process
.pid
);
1939 netns_updown(jail_process
.pid
, true);
1943 if (write(pipes
[3], sig_buf
, 1) < 0) {
1944 ERROR("can't write to child\n");
1948 run_hooks(opts
.hooks
.poststart
);
1951 uloop_process_add(&jail_process
);
1954 DEBUG("uloop interrupted, killing jail process\n");
1955 kill(jail_process
.pid
, SIGTERM
);
1956 uloop_timeout_set(&jail_process_timeout
, 1000);
1960 if (opts
.namespace & CLONE_NEWNET
) {
1961 setns(netns_fd
, CLONE_NEWNET
);
1962 netns_updown(getpid(), false);
1965 run_hooks(opts
.hooks
.poststop
);
1967 return jail_return_code
;
1968 } else if (jail_process
.pid
== 0) {
1969 /* fork child process */
1970 return exec_jail(NULL
);
1972 ERROR("failed to clone/fork: %m\n");
1973 return EXIT_FAILURE
;