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:"
62 struct sock_fprog
*ociseccomp
;
64 struct jail_capset capset
;
85 static struct blob_buf ocibuf
;
87 extern int pivot_root(const char *new_root
, const char *put_old
);
91 static char child_stack
[STACK_SIZE
];
95 static int mkdir_p(char *dir
, mode_t mask
)
97 char *l
= strrchr(dir
, '/');
105 if (mkdir_p(dir
, mask
))
110 ret
= mkdir(dir
, mask
);
111 if (ret
&& errno
== EEXIST
)
115 ERROR("mkdir(%s, %d) failed: %m\n", dir
, mask
);
120 static int _mount_bind(const char *root
, const char *path
, const char *target
, int readonly
, int strict
, int error
)
125 int remount_flags
= MS_BIND
| MS_REMOUNT
;
127 if (stat(path
, &s
)) {
128 ERROR("stat(%s) failed: %m\n", path
);
132 snprintf(new, sizeof(new), "%s%s", root
, target
?target
:path
);
134 if (S_ISDIR(s
.st_mode
)) {
137 mkdir_p(dirname(new), 0755);
138 snprintf(new, sizeof(new), "%s%s", root
, target
?target
:path
);
139 fd
= creat(new, 0644);
141 ERROR("creat(%s) failed: %m\n", new);
147 if (mount(path
, new, NULL
, MS_BIND
, NULL
)) {
148 ERROR("failed to mount -B %s %s: %m\n", path
, new);
153 remount_flags
|= MS_RDONLY
;
156 remount_flags
|= MS_NOEXEC
| MS_NOSUID
| MS_NODEV
;
158 if ((strict
|| readonly
) && mount(NULL
, new, NULL
, remount_flags
, NULL
)) {
159 ERROR("failed to remount (%s%s%s) %s: %m\n", readonly
?"ro":"rw",
160 (readonly
&& strict
)?", ":"", strict
?"strict":"", new);
164 DEBUG("mount -B %s %s (%s%s%s)\n", path
, new,
165 readonly
?"ro":"rw", (readonly
&& strict
)?", ":"", strict
?"strict":"");
170 int mount_bind(const char *root
, const char *path
, int readonly
, int error
) {
171 return _mount_bind(root
, path
, NULL
, readonly
, 0, error
);
174 static int mount_overlay(char *jail_root
, char *overlaydir
) {
175 char *upperdir
, *workdir
, *optsstr
, *upperetc
, *upperresolvconf
;
176 const char mountoptsformat
[] = "lowerdir=%s,upperdir=%s,workdir=%s";
179 if (asprintf(&upperdir
, "%s%s", overlaydir
, "/upper") < 0)
182 if (asprintf(&workdir
, "%s%s", overlaydir
, "/work") < 0)
185 if (asprintf(&optsstr
, mountoptsformat
, jail_root
, upperdir
, workdir
) < 0)
188 if (mkdir_p(upperdir
, 0755) || mkdir_p(workdir
, 0755))
192 * make sure /etc/resolv.conf exists in overlay and is owned by jail userns root
193 * this is to work-around a bug in overlayfs described in the overlayfs-userns
195 * 3. modification of a file 'hithere' which is in l but not yet
196 * in u, and which is not owned by T, is not allowed, even if
197 * writes to u are allowed. This may be a bug in overlayfs,
198 * but it is safe behavior.
200 if (asprintf(&upperetc
, "%s/etc", upperdir
) < 0)
203 if (mkdir_p(upperetc
, 0755))
204 goto upper_etc_printf
;
206 if (asprintf(&upperresolvconf
, "%s/resolv.conf", upperetc
) < 0)
207 goto upper_etc_printf
;
209 fd
= creat(upperresolvconf
, 0644);
211 ERROR("creat(%s) failed: %m\n", upperresolvconf
);
212 goto upper_resolvconf_printf
;
216 DEBUG("mount -t overlay %s %s (%s)\n", jail_root
, jail_root
, optsstr
);
218 if (mount(jail_root
, jail_root
, "overlay", MS_NOATIME
, optsstr
))
223 upper_resolvconf_printf
:
224 free(upperresolvconf
);
237 static void pass_console(int console_fd
)
239 struct ubus_context
*ctx
= ubus_connect(NULL
);
240 static struct blob_buf req
;
246 blob_buf_init(&req
, 0);
247 blobmsg_add_string(&req
, "name", opts
.name
);
249 if (ubus_lookup_id(ctx
, "container", &id
) ||
250 ubus_invoke_fd(ctx
, id
, "console_set", req
.head
, NULL
, NULL
, 3000, console_fd
))
251 INFO("ubus request failed\n");
259 static int create_dev_console(const char *jail_root
)
262 char dev_console_path
[PATH_MAX
];
263 int slave_console_fd
;
265 /* Open UNIX/98 virtual console */
266 console_fd
= posix_openpt(O_RDWR
| O_NOCTTY
);
267 if (console_fd
== -1)
270 console_fname
= ptsname(console_fd
);
271 DEBUG("got console fd %d and PTS client name %s\n", console_fd
, console_fname
);
276 unlockpt(console_fd
);
278 /* pass PTY master to procd */
279 pass_console(console_fd
);
281 /* mount-bind PTY slave to /dev/console in jail */
282 snprintf(dev_console_path
, sizeof(dev_console_path
), "%s/dev/console", jail_root
);
283 close(creat(dev_console_path
, 0620));
285 if (mount(console_fname
, dev_console_path
, NULL
, MS_BIND
, NULL
))
288 /* use PTY slave for stdio */
289 slave_console_fd
= open(console_fname
, O_RDWR
); /* | O_NOCTTY */
290 dup2(slave_console_fd
, 0);
291 dup2(slave_console_fd
, 1);
292 dup2(slave_console_fd
, 2);
293 close(slave_console_fd
);
295 INFO("using guest console %s\n", console_fname
);
304 static int build_jail_fs(void)
306 char jail_root
[] = "/tmp/ujail-XXXXXX";
307 char tmpovdir
[] = "/tmp/ujail-overlay-XXXXXX";
308 char tmpdevdir
[] = "/tmp/ujail-XXXXXX/dev";
309 char tmpdevptsdir
[] = "/tmp/ujail-XXXXXX/dev/pts";
310 char *overlaydir
= NULL
;
312 if (mkdtemp(jail_root
) == NULL
) {
313 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
317 /* oldroot can't be MS_SHARED else pivot_root() fails */
318 if (mount("none", "/", NULL
, MS_REC
|MS_PRIVATE
, NULL
)) {
319 ERROR("private mount failed %m\n");
324 if (mount(opts
.extroot
, jail_root
, NULL
, MS_BIND
, NULL
)) {
325 ERROR("extroot mount failed %m\n");
329 if (mount("tmpfs", jail_root
, "tmpfs", MS_NOATIME
, "mode=0755")) {
330 ERROR("tmpfs mount failed %m\n");
335 if (opts
.tmpoverlaysize
) {
336 char mountoptsstr
[] = "mode=0755,size=XXXXXXXX";
338 snprintf(mountoptsstr
, sizeof(mountoptsstr
),
339 "mode=0755,size=%s", opts
.tmpoverlaysize
);
340 if (mkdtemp(tmpovdir
) == NULL
) {
341 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
344 if (mount("tmpfs", tmpovdir
, "tmpfs", MS_NOATIME
,
346 ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts
.tmpoverlaysize
);
349 overlaydir
= tmpovdir
;
353 overlaydir
= opts
.overlaydir
;
356 mount_overlay(jail_root
, overlaydir
);
358 if (chdir(jail_root
)) {
359 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root
);
363 snprintf(tmpdevdir
, sizeof(tmpdevdir
), "%s/dev", jail_root
);
364 mkdir_p(tmpdevdir
, 0755);
365 if (mount(NULL
, tmpdevdir
, "tmpfs", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, "size=1M"))
368 snprintf(tmpdevptsdir
, sizeof(tmpdevptsdir
), "%s/dev/pts", jail_root
);
369 mkdir_p(tmpdevptsdir
, 0755);
370 if (mount(NULL
, tmpdevptsdir
, "devpts", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, NULL
))
374 create_dev_console(jail_root
);
376 if (mount_all(jail_root
)) {
377 ERROR("mount_all() failed\n");
381 if (opts
.namespace & CLONE_NEWNET
) {
382 char hostdir
[PATH_MAX
], jailetc
[PATH_MAX
], jaillink
[PATH_MAX
];
384 snprintf(hostdir
, PATH_MAX
, "/tmp/resolv.conf-%s.d", opts
.name
);
385 mkdir_p(hostdir
, 0755);
386 _mount_bind(jail_root
, hostdir
, "/tmp/resolv.conf.d", 1, 1, -1);
387 snprintf(jailetc
, PATH_MAX
, "%s/etc", jail_root
);
388 mkdir_p(jailetc
, 0755);
389 snprintf(jaillink
, PATH_MAX
, "%s/etc/resolv.conf", jail_root
);
392 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink
);
395 char dirbuf
[sizeof(jail_root
) + 4];
396 snprintf(dirbuf
, sizeof(dirbuf
), "%s/old", jail_root
);
399 if (pivot_root(jail_root
, dirbuf
) == -1) {
400 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root
, dirbuf
);
404 ERROR("chdir(/) (after pivot_root) failed: %m\n");
408 snprintf(dirbuf
, sizeof(dirbuf
), "/old%s", jail_root
);
409 umount2(dirbuf
, MNT_DETACH
);
411 if (opts
.tmpoverlaysize
) {
412 char tmpdirbuf
[sizeof(tmpovdir
) + 4];
413 snprintf(tmpdirbuf
, sizeof(tmpdirbuf
), "/old%s", tmpovdir
);
414 umount2(tmpdirbuf
, MNT_DETACH
);
418 umount2("/old", MNT_DETACH
);
422 mkdir("/proc", 0755);
423 mount("proc", "/proc", "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
425 * make /proc/sys read-only while keeping read-write to
426 * /proc/sys/net if CLONE_NEWNET is set.
428 if (opts
.namespace & CLONE_NEWNET
)
429 mount("/proc/sys/net", "/proc/self/net", NULL
, MS_BIND
, 0);
431 mount("/proc/sys", "/proc/sys", NULL
, MS_BIND
, 0);
432 mount(NULL
, "/proc/sys", NULL
, MS_REMOUNT
| MS_RDONLY
, 0);
433 mount(NULL
, "/proc", NULL
, MS_REMOUNT
, 0);
435 if (opts
.namespace & CLONE_NEWNET
)
436 mount("/proc/self/net", "/proc/sys/net", NULL
, MS_MOVE
, 0);
440 mount("sysfs", "/sys", "sysfs", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RDONLY
, 0);
443 mount(NULL
, "/", NULL
, MS_RDONLY
| MS_REMOUNT
, 0);
448 static int write_uid_gid_map(pid_t child_pid
, bool gidmap
, char *mapstr
)
453 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
454 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
457 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
460 if (dprintf(map_file
, "%s", mapstr
)) {
470 static int write_single_uid_gid_map(pid_t child_pid
, bool gidmap
, int id
)
474 const char *map_format
= "%d %d %d\n";
475 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
476 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
479 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
482 if (dprintf(map_file
, map_format
, 0, id
, 1) == -1) {
491 static int write_setgroups(pid_t child_pid
, bool allow
)
494 char setgroups_path
[64];
496 if (snprintf(setgroups_path
, sizeof(setgroups_path
), "/proc/%d/setgroups",
501 if ((setgroups_file
= open(setgroups_path
, O_WRONLY
)) == -1) {
505 if (dprintf(setgroups_file
, "%s", allow
?"allow":"deny") == -1) {
506 close(setgroups_file
);
510 close(setgroups_file
);
514 static void get_jail_user(int *user
, int *user_gid
, int *gr_gid
)
516 struct passwd
*p
= NULL
;
517 struct group
*g
= NULL
;
520 p
= getpwnam(opts
.user
);
522 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
523 opts
.user
, errno
, strerror(errno
));
527 *user_gid
= p
->pw_gid
;
534 g
= getgrnam(opts
.group
);
536 ERROR("failed to get gid for group %s: %m\n", opts
.group
);
545 static void set_jail_user(int pw_uid
, int user_gid
, int gr_gid
)
547 if (opts
.user
&& (user_gid
!= -1) && initgroups(opts
.user
, user_gid
)) {
548 ERROR("failed to initgroups() for user %s: %m\n", opts
.user
);
552 if ((gr_gid
!= -1) && setregid(gr_gid
, gr_gid
)) {
553 ERROR("failed to set group id %d: %m\n", gr_gid
);
557 if ((pw_uid
!= -1) && setreuid(pw_uid
, pw_uid
)) {
558 ERROR("failed to set user id %d: %m\n", pw_uid
);
564 static char** build_envp(const char *seccomp
, char **ocienvp
)
566 static char *envp
[MAX_ENVP
];
567 static char preload_var
[PATH_MAX
];
568 static char seccomp_var
[PATH_MAX
];
569 static char debug_var
[] = "LD_DEBUG=all";
570 static char container_var
[] = "container=ujail";
571 const char *preload_lib
= find_lib("libpreload-seccomp.so");
576 if (seccomp
&& !preload_lib
) {
577 ERROR("failed to add preload-lib to env\n");
581 snprintf(seccomp_var
, sizeof(seccomp_var
), "SECCOMP_FILE=%s", seccomp
);
582 envp
[count
++] = seccomp_var
;
583 snprintf(preload_var
, sizeof(preload_var
), "LD_PRELOAD=%s", preload_lib
);
584 envp
[count
++] = preload_var
;
587 envp
[count
++] = container_var
;
590 envp
[count
++] = debug_var
;
593 while (addenv
&& *addenv
) {
594 envp
[count
++] = *(addenv
++);
595 if (count
>= MAX_ENVP
) {
596 ERROR("environment limited to %d extra records, truncating\n", MAX_ENVP
);
603 static void usage(void)
605 fprintf(stderr
, "ujail <options> -- <binary> <params ...>\n");
606 fprintf(stderr
, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
607 fprintf(stderr
, " -S <file>\tseccomp filter config\n");
608 fprintf(stderr
, " -C <file>\tcapabilities drop config\n");
609 fprintf(stderr
, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
610 fprintf(stderr
, " -n <name>\tthe name of the jail\n");
611 fprintf(stderr
, "namespace jail options:\n");
612 fprintf(stderr
, " -h <hostname>\tchange the hostname of the jail\n");
613 fprintf(stderr
, " -N\t\tjail has network namespace\n");
614 fprintf(stderr
, " -f\t\tjail has user namespace\n");
615 fprintf(stderr
, " -F\t\tjail has cgroups namespace\n");
616 fprintf(stderr
, " -r <file>\treadonly files that should be staged\n");
617 fprintf(stderr
, " -w <file>\twriteable files that should be staged\n");
618 fprintf(stderr
, " -p\t\tjail has /proc\n");
619 fprintf(stderr
, " -s\t\tjail has /sys\n");
620 fprintf(stderr
, " -l\t\tjail has /dev/log\n");
621 fprintf(stderr
, " -u\t\tjail has a ubus socket\n");
622 fprintf(stderr
, " -U <name>\tuser to run jailed process\n");
623 fprintf(stderr
, " -G <name>\tgroup to run jailed process\n");
624 fprintf(stderr
, " -o\t\tremont jail root (/) read only\n");
625 fprintf(stderr
, " -R <dir>\texternal jail rootfs (system container)\n");
626 fprintf(stderr
, " -O <dir>\tdirectory for r/w overlayfs\n");
627 fprintf(stderr
, " -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
628 fprintf(stderr
, " -E\t\tfail if jail cannot be setup\n");
629 fprintf(stderr
, " -y\t\tprovide jail console\n");
630 fprintf(stderr
, " -J <dir>\tstart OCI bundle\n");
631 fprintf(stderr
, "\nWarning: by default root inside the jail is the same\n\
632 and he has the same powers as root outside the jail,\n\
633 thus he can escape the jail and/or break stuff.\n\
634 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
635 If you use none of the namespace jail options,\n\
636 ujail will not use namespace/build a jail,\n\
637 and will only drop capabilities/apply seccomp filter.\n\n");
640 static int exec_jail(void *pipes_ptr
)
642 int *pipes
= (int*)pipes_ptr
;
644 int pw_uid
, pw_gid
, gr_gid
;
650 if (write(pipes
[1], buf
, 1) < 1) {
651 ERROR("can't write to parent\n");
654 if (read(pipes
[2], buf
, 1) < 1) {
655 ERROR("can't read from parent\n");
659 ERROR("parent had an error, child exiting\n");
666 if (opts
.namespace & CLONE_NEWUSER
) {
667 if (setregid(0, 0) < 0) {
671 if (setreuid(0, 0) < 0) {
675 if (setgroups(0, NULL
) < 0) {
676 ERROR("setgroups\n");
681 if (opts
.namespace && opts
.hostname
&& strlen(opts
.hostname
) > 0
682 && sethostname(opts
.hostname
, strlen(opts
.hostname
))) {
683 ERROR("sethostname(%s) failed: %m\n", opts
.hostname
);
687 if ((opts
.namespace & CLONE_NEWNS
) && build_jail_fs()) {
688 ERROR("failed to build jail fs\n");
692 if (applyOCIcapabilities(opts
.capset
))
695 if (opts
.capabilities
&& drop_capabilities(opts
.capabilities
))
698 if (opts
.no_new_privs
&& prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
699 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
703 if (!(opts
.namespace & CLONE_NEWUSER
)) {
704 get_jail_user(&pw_uid
, &pw_gid
, &gr_gid
);
706 set_jail_user(opts
.pw_uid
?:pw_uid
, opts
.pw_gid
?:pw_gid
, opts
.gr_gid
?:gr_gid
);
709 char **envp
= build_envp(opts
.seccomp
, opts
.envp
);
713 if (opts
.ociseccomp
&& applyOCIlinuxseccomp(opts
.ociseccomp
))
716 INFO("exec-ing %s\n", *opts
.jail_argv
);
717 execve(*opts
.jail_argv
, opts
.jail_argv
, envp
);
718 /* we get there only if execve fails */
719 ERROR("failed to execve %s: %m\n", *opts
.jail_argv
);
723 static int jail_running
= 1;
724 static int jail_return_code
= 0;
726 static void jail_process_timeout_cb(struct uloop_timeout
*t
);
727 static struct uloop_timeout jail_process_timeout
= {
728 .cb
= jail_process_timeout_cb
,
731 static void jail_process_handler(struct uloop_process
*c
, int ret
)
733 uloop_timeout_cancel(&jail_process_timeout
);
734 if (WIFEXITED(ret
)) {
735 jail_return_code
= WEXITSTATUS(ret
);
736 INFO("jail (%d) exited with exit: %d\n", c
->pid
, jail_return_code
);
738 jail_return_code
= WTERMSIG(ret
);
739 INFO("jail (%d) exited with signal: %d\n", c
->pid
, jail_return_code
);
745 static struct uloop_process jail_process
= {
746 .cb
= jail_process_handler
,
749 static void jail_process_timeout_cb(struct uloop_timeout
*t
)
751 DEBUG("jail process failed to stop, sending SIGKILL\n");
752 kill(jail_process
.pid
, SIGKILL
);
755 static void jail_handle_signal(int signo
)
757 DEBUG("forwarding signal %d to the jailed process\n", signo
);
758 kill(jail_process
.pid
, signo
);
761 static int netns_open_pid(const pid_t target_ns
)
763 char pid_net_path
[PATH_MAX
];
765 snprintf(pid_net_path
, sizeof(pid_net_path
), "/proc/%u/ns/net", target_ns
);
767 return open(pid_net_path
, O_RDONLY
);
770 static void netns_updown(pid_t pid
, bool start
)
772 struct ubus_context
*ctx
= ubus_connect(NULL
);
773 static struct blob_buf req
;
779 blob_buf_init(&req
, 0);
780 blobmsg_add_string(&req
, "jail", opts
.name
);
781 blobmsg_add_u32(&req
, "pid", pid
);
782 blobmsg_add_u8(&req
, "start", start
);
784 if (ubus_lookup_id(ctx
, "network", &id
) ||
785 ubus_invoke(ctx
, id
, "netns_updown", req
.head
, NULL
, NULL
, 3000))
786 INFO("ubus request failed\n");
799 static const struct blobmsg_policy oci_root_policy
[] = {
800 [OCI_ROOT_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
801 [OCI_ROOT_READONLY
] = { "readonly", BLOBMSG_TYPE_BOOL
},
804 static int parseOCIroot(const char *jsonfile
, struct blob_attr
*msg
)
806 static char rootpath
[PATH_MAX
] = { 0 };
807 struct blob_attr
*tb
[__OCI_ROOT_MAX
];
810 blobmsg_parse(oci_root_policy
, __OCI_ROOT_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
812 if (!tb
[OCI_ROOT_PATH
])
815 strncpy(rootpath
, jsonfile
, PATH_MAX
);
816 cur
= strrchr(rootpath
, '/');
822 strncat(rootpath
, blobmsg_get_string(tb
[OCI_ROOT_PATH
]), PATH_MAX
- (strlen(rootpath
) + 1));
824 opts
.extroot
= rootpath
;
826 opts
.ronly
= blobmsg_get_bool(tb
[OCI_ROOT_READONLY
]);
834 OCI_MOUNT_DESTINATION
,
840 static const struct blobmsg_policy oci_mount_policy
[] = {
841 [OCI_MOUNT_SOURCE
] = { "source", BLOBMSG_TYPE_STRING
},
842 [OCI_MOUNT_DESTINATION
] = { "destination", BLOBMSG_TYPE_STRING
},
843 [OCI_MOUNT_TYPE
] = { "type", BLOBMSG_TYPE_STRING
},
844 [OCI_MOUNT_OPTIONS
] = { "options", BLOBMSG_TYPE_ARRAY
},
847 static int parseOCImount(struct blob_attr
*msg
)
849 struct blob_attr
*tb
[__OCI_MOUNT_MAX
];
851 blobmsg_parse(oci_mount_policy
, __OCI_MOUNT_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
853 if (!tb
[OCI_MOUNT_DESTINATION
])
856 if (!strcmp("proc", blobmsg_get_string(tb
[OCI_MOUNT_TYPE
])) &&
857 !strcmp("/proc", blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]))) {
862 if (!strcmp("sysfs", blobmsg_get_string(tb
[OCI_MOUNT_TYPE
])) &&
863 !strcmp("/sys", blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]))) {
868 if (!strcmp("tmpfs", blobmsg_get_string(tb
[OCI_MOUNT_TYPE
])) &&
869 !strcmp("/dev", blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]))) {
870 /* we always mount a small tmpfs on /dev */
874 INFO("ignoring unsupported mount %s %s -t %s -o %s\n",
875 blobmsg_get_string(tb
[OCI_MOUNT_SOURCE
]),
876 blobmsg_get_string(tb
[OCI_MOUNT_DESTINATION
]),
877 blobmsg_get_string(tb
[OCI_MOUNT_TYPE
]),
878 blobmsg_format_json(tb
[OCI_MOUNT_OPTIONS
], true));
885 OCI_PROCESS_USER_UID
,
886 OCI_PROCESS_USER_GID
,
887 OCI_PROCESS_USER_UMASK
,
888 OCI_PROCESS_USER_ADDITIONALGIDS
,
889 __OCI_PROCESS_USER_MAX
,
892 static const struct blobmsg_policy oci_process_user_policy
[] = {
893 [OCI_PROCESS_USER_UID
] = { "uid", BLOBMSG_TYPE_INT32
},
894 [OCI_PROCESS_USER_GID
] = { "gid", BLOBMSG_TYPE_INT32
},
895 [OCI_PROCESS_USER_UMASK
] = { "umask", BLOBMSG_TYPE_INT32
},
896 [OCI_PROCESS_USER_ADDITIONALGIDS
] = { "additionalGids", BLOBMSG_TYPE_ARRAY
},
899 static int parseOCIprocessuser(struct blob_attr
*msg
) {
900 struct blob_attr
*tb
[__OCI_PROCESS_USER_MAX
];
902 blobmsg_parse(oci_process_user_policy
, __OCI_PROCESS_USER_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
904 if (tb
[OCI_PROCESS_USER_UID
])
905 opts
.pw_uid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_UID
]);
907 if (tb
[OCI_PROCESS_USER_GID
]) {
908 opts
.pw_gid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_GID
]);
909 opts
.gr_gid
= blobmsg_get_u32(tb
[OCI_PROCESS_USER_GID
]);
912 /* ToDo: umask, additional GIDs */
919 OCI_PROCESS_CAPABILITIES
,
922 OCI_PROCESS_NONEWPRIVILEGES
,
924 OCI_PROCESS_TERMINAL
,
929 static const struct blobmsg_policy oci_process_policy
[] = {
930 [OCI_PROCESS_ARGS
] = { "args", BLOBMSG_TYPE_ARRAY
},
931 [OCI_PROCESS_CAPABILITIES
] = { "capabilities", BLOBMSG_TYPE_TABLE
},
932 [OCI_PROCESS_CWD
] = { "cwd", BLOBMSG_TYPE_STRING
},
933 [OCI_PROCESS_ENV
] = { "env", BLOBMSG_TYPE_ARRAY
},
934 [OCI_PROCESS_NONEWPRIVILEGES
] = { "noNewPrivileges", BLOBMSG_TYPE_BOOL
},
935 [OCI_PROCESS_RLIMITS
] = { "rlimits", BLOBMSG_TYPE_ARRAY
},
936 [OCI_PROCESS_TERMINAL
] = { "terminal", BLOBMSG_TYPE_BOOL
},
937 [OCI_PROCESS_USER
] = { "user", BLOBMSG_TYPE_TABLE
},
940 static int parseOCIprocess(struct blob_attr
*msg
)
942 struct blob_attr
*tb
[__OCI_PROCESS_MAX
];
943 struct blob_attr
*cur
;
948 blobmsg_parse(oci_process_policy
, __OCI_PROCESS_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
950 if (!tb
[OCI_PROCESS_ARGS
])
953 blobmsg_for_each_attr(cur
, tb
[OCI_PROCESS_ARGS
], rem
)
959 opts
.jail_argv
= calloc(1 + sz
, sizeof(char*));
964 blobmsg_for_each_attr(cur
, tb
[OCI_PROCESS_ARGS
], rem
)
965 opts
.jail_argv
[sz
++] = blobmsg_get_string(cur
);
967 opts
.console
= blobmsg_get_bool(tb
[OCI_PROCESS_TERMINAL
]);
968 opts
.no_new_privs
= blobmsg_get_bool(tb
[OCI_PROCESS_NONEWPRIVILEGES
]);
970 if (tb
[OCI_PROCESS_CWD
])
971 opts
.cwd
= blobmsg_get_string(tb
[OCI_PROCESS_CWD
]);
974 blobmsg_for_each_attr(cur
, tb
[OCI_PROCESS_ENV
], rem
)
978 opts
.envp
= calloc(1 + sz
, sizeof(char*));
984 blobmsg_for_each_attr(cur
, tb
[OCI_PROCESS_ENV
], rem
)
985 opts
.envp
[sz
++] = strdup(blobmsg_get_string(cur
));
987 if (tb
[OCI_PROCESS_USER
] && (res
= parseOCIprocessuser(tb
[OCI_PROCESS_USER
])))
990 if (tb
[OCI_PROCESS_CAPABILITIES
] &&
991 (res
= parseOCIcapabilities(&opts
.capset
, tb
[OCI_PROCESS_CAPABILITIES
])))
994 /* ToDo: rlimits, capabilities */
1000 OCI_LINUX_NAMESPACE_TYPE
,
1001 OCI_LINUX_NAMESPACE_PATH
,
1002 __OCI_LINUX_NAMESPACE_MAX
,
1005 static const struct blobmsg_policy oci_linux_namespace_policy
[] = {
1006 [OCI_LINUX_NAMESPACE_TYPE
] = { "type", BLOBMSG_TYPE_STRING
},
1007 [OCI_LINUX_NAMESPACE_PATH
] = { "path", BLOBMSG_TYPE_STRING
},
1010 static unsigned int resolve_nstype(char *type
) {
1011 if (!strcmp("pid", type
))
1012 return CLONE_NEWPID
;
1013 else if (!strcmp("network", type
))
1014 return CLONE_NEWNET
;
1015 else if (!strcmp("mount", type
))
1017 else if (!strcmp("ipc", type
))
1018 return CLONE_NEWIPC
;
1019 else if (!strcmp("uts", type
))
1020 return CLONE_NEWUTS
;
1021 else if (!strcmp("user", type
))
1022 return CLONE_NEWUSER
;
1023 else if (!strcmp("cgroup", type
))
1024 return CLONE_NEWCGROUP
;
1029 static int parseOCIlinuxns(struct blob_attr
*msg
)
1031 struct blob_attr
*tb
[__OCI_LINUX_NAMESPACE_MAX
];
1034 blobmsg_parse(oci_linux_namespace_policy
, __OCI_LINUX_NAMESPACE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1036 if (!tb
[OCI_LINUX_NAMESPACE_TYPE
])
1039 if (tb
[OCI_LINUX_NAMESPACE_PATH
])
1040 return ENOTSUP
; /* ToDo */
1042 opts
.namespace |= resolve_nstype(blobmsg_get_string(tb
[OCI_LINUX_NAMESPACE_TYPE
]));
1049 OCI_LINUX_UIDGIDMAP_CONTAINERID
,
1050 OCI_LINUX_UIDGIDMAP_HOSTID
,
1051 OCI_LINUX_UIDGIDMAP_SIZE
,
1052 __OCI_LINUX_UIDGIDMAP_MAX
,
1055 static const struct blobmsg_policy oci_linux_uidgidmap_policy
[] = {
1056 [OCI_LINUX_UIDGIDMAP_CONTAINERID
] = { "containerID", BLOBMSG_TYPE_INT32
},
1057 [OCI_LINUX_UIDGIDMAP_HOSTID
] = { "hostID", BLOBMSG_TYPE_INT32
},
1058 [OCI_LINUX_UIDGIDMAP_SIZE
] = { "size", BLOBMSG_TYPE_INT32
},
1061 static int parseOCIuidgidmappings(struct blob_attr
*msg
, bool is_gidmap
)
1063 const char *map_format
= "%d %d %d\n";
1064 struct blob_attr
*tb
[__OCI_LINUX_UIDGIDMAP_MAX
];
1065 struct blob_attr
*cur
;
1069 unsigned int cnt
= 0;
1070 size_t totallen
= 0;
1072 /* count number of mappings */
1073 blobmsg_for_each_attr(cur
, msg
, rem
)
1079 /* allocate array for mappings */
1080 mappings
= calloc(1 + cnt
, sizeof(char*));
1084 mappings
[cnt
] = NULL
;
1087 blobmsg_for_each_attr(cur
, msg
, rem
) {
1088 blobmsg_parse(oci_linux_uidgidmap_policy
, __OCI_LINUX_UIDGIDMAP_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1090 if (!tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
] ||
1091 !tb
[OCI_LINUX_UIDGIDMAP_HOSTID
] ||
1092 !tb
[OCI_LINUX_UIDGIDMAP_SIZE
])
1095 /* write mapping line into allocated string */
1096 len
= asprintf(&mappings
[cnt
++], map_format
,
1097 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_CONTAINERID
]),
1098 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_HOSTID
]),
1099 blobmsg_get_u32(tb
[OCI_LINUX_UIDGIDMAP_SIZE
]));
1107 /* allocate combined mapping string */
1108 map
= calloc(1 + len
, sizeof(char));
1114 /* concatenate mapping strings into combined string */
1115 curstr
= mappings
[0];
1117 strcat(map
, curstr
);
1131 OCI_LINUX_RESOURCES
,
1134 OCI_LINUX_NAMESPACES
,
1135 OCI_LINUX_UIDMAPPINGS
,
1136 OCI_LINUX_GIDMAPPINGS
,
1137 OCI_LINUX_MASKEDPATHS
,
1138 OCI_LINUX_READONLYPATHS
,
1139 OCI_LINUX_ROOTFSPROPAGATION
,
1143 static const struct blobmsg_policy oci_linux_policy
[] = {
1144 [OCI_LINUX_RESOURCES
] = { "resources", BLOBMSG_TYPE_TABLE
},
1145 [OCI_LINUX_SECCOMP
] = { "seccomp", BLOBMSG_TYPE_TABLE
},
1146 [OCI_LINUX_SYSCTL
] = { "sysctl", BLOBMSG_TYPE_TABLE
},
1147 [OCI_LINUX_NAMESPACES
] = { "namespaces", BLOBMSG_TYPE_ARRAY
},
1148 [OCI_LINUX_UIDMAPPINGS
] = { "uidMappings", BLOBMSG_TYPE_ARRAY
},
1149 [OCI_LINUX_GIDMAPPINGS
] = { "gidMappings", BLOBMSG_TYPE_ARRAY
},
1150 [OCI_LINUX_MASKEDPATHS
] = { "maskedPaths", BLOBMSG_TYPE_ARRAY
},
1151 [OCI_LINUX_READONLYPATHS
] = { "readonlyPaths", BLOBMSG_TYPE_ARRAY
},
1152 [OCI_LINUX_ROOTFSPROPAGATION
] = { "rootfsPropagation", BLOBMSG_TYPE_STRING
},
1155 static int parseOCIlinux(struct blob_attr
*msg
)
1157 struct blob_attr
*tb
[__OCI_LINUX_MAX
];
1158 struct blob_attr
*cur
;
1162 blobmsg_parse(oci_linux_policy
, __OCI_LINUX_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
1164 if (tb
[OCI_LINUX_NAMESPACES
]) {
1165 blobmsg_for_each_attr(cur
, tb
[OCI_LINUX_NAMESPACES
], rem
) {
1166 res
= parseOCIlinuxns(cur
);
1172 if (tb
[OCI_LINUX_UIDMAPPINGS
]) {
1173 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 0);
1178 if (tb
[OCI_LINUX_GIDMAPPINGS
]) {
1179 res
= parseOCIuidgidmappings(tb
[OCI_LINUX_GIDMAPPINGS
], 1);
1184 if (tb
[OCI_LINUX_SECCOMP
]) {
1185 opts
.ociseccomp
= parseOCIlinuxseccomp(tb
[OCI_LINUX_SECCOMP
]);
1186 if (!opts
.ociseccomp
)
1203 static const struct blobmsg_policy oci_policy
[] = {
1204 [OCI_VERSION
] = { "ociVersion", BLOBMSG_TYPE_STRING
},
1205 [OCI_HOSTNAME
] = { "hostname", BLOBMSG_TYPE_STRING
},
1206 [OCI_PROCESS
] = { "process", BLOBMSG_TYPE_TABLE
},
1207 [OCI_ROOT
] = { "root", BLOBMSG_TYPE_TABLE
},
1208 [OCI_MOUNTS
] = { "mounts", BLOBMSG_TYPE_ARRAY
},
1209 [OCI_LINUX
] = { "linux", BLOBMSG_TYPE_TABLE
},
1212 static int parseOCI(const char *jsonfile
)
1214 struct blob_attr
*tb
[__OCI_MAX
];
1215 struct blob_attr
*cur
;
1219 blob_buf_init(&ocibuf
, 0);
1220 if (!blobmsg_add_json_from_file(&ocibuf
, jsonfile
))
1223 blobmsg_parse(oci_policy
, __OCI_MAX
, tb
, blob_data(ocibuf
.head
), blob_len(ocibuf
.head
));
1225 if (!tb
[OCI_VERSION
])
1228 if (strncmp("1.0", blobmsg_get_string(tb
[OCI_VERSION
]), 3)) {
1229 ERROR("unsupported ociVersion %s\n", blobmsg_get_string(tb
[OCI_VERSION
]));
1233 if (tb
[OCI_HOSTNAME
])
1234 opts
.hostname
= blobmsg_get_string(tb
[OCI_HOSTNAME
]);
1236 if (!tb
[OCI_PROCESS
])
1239 if ((res
= parseOCIprocess(tb
[OCI_PROCESS
])))
1245 if ((res
= parseOCIroot(jsonfile
, tb
[OCI_ROOT
])))
1248 if (!tb
[OCI_MOUNTS
])
1251 blobmsg_for_each_attr(cur
, tb
[OCI_MOUNTS
], rem
)
1252 if ((res
= parseOCImount(cur
)))
1255 if (tb
[OCI_LINUX
] && (res
= parseOCIlinux(tb
[OCI_LINUX
])))
1261 int main(int argc
, char **argv
)
1264 uid_t uid
= getuid();
1265 const char log
[] = "/dev/log";
1266 const char ubus
[] = "/var/run/ubus.sock";
1267 char *jsonfile
= NULL
;
1274 ERROR("not root, aborting: %m\n");
1275 return EXIT_FAILURE
;
1280 init_library_search();
1282 while ((ch
= getopt(argc
, argv
, OPT_ARGS
)) != -1) {
1285 debug
= atoi(optarg
);
1288 opts
.namespace |= CLONE_NEWNS
;
1292 opts
.namespace |= CLONE_NEWNS
;
1296 opts
.namespace |= CLONE_NEWUSER
;
1299 opts
.namespace |= CLONE_NEWCGROUP
;
1302 opts
.extroot
= optarg
;
1305 opts
.namespace |= CLONE_NEWNS
;
1309 opts
.seccomp
= optarg
;
1310 add_mount(optarg
, 1, -1);
1313 opts
.capabilities
= optarg
;
1316 opts
.no_new_privs
= 1;
1322 opts
.namespace |= CLONE_NEWNET
;
1325 opts
.namespace |= CLONE_NEWUTS
;
1326 opts
.hostname
= optarg
;
1329 opts
.namespace |= CLONE_NEWNS
;
1330 add_path_and_deps(optarg
, 1, 0, 0);
1333 opts
.namespace |= CLONE_NEWNS
;
1334 add_path_and_deps(optarg
, 0, 0, 0);
1337 opts
.namespace |= CLONE_NEWNS
;
1338 add_mount(ubus
, 0, -1);
1341 opts
.namespace |= CLONE_NEWNS
;
1342 add_mount(log
, 0, -1);
1348 opts
.group
= optarg
;
1351 opts
.overlaydir
= optarg
;
1354 opts
.tmpoverlaysize
= optarg
;
1357 opts
.require_jail
= 1;
1363 asprintf(&jsonfile
, "%s/config.json", optarg
);
1369 opts
.namespace |= CLONE_NEWIPC
| CLONE_NEWPID
;
1373 ocires
= parseOCI(jsonfile
);
1376 ERROR("parsing of OCI JSON spec has failed: %s (%d)\n", strerror(ocires
), ocires
);
1381 if (opts
.tmpoverlaysize
&& strlen(opts
.tmpoverlaysize
) > 8) {
1382 ERROR("size parameter too long: \"%s\"\n", opts
.tmpoverlaysize
);
1386 /* no <binary> param found */
1387 if (!jsonfile
&& (argc
- optind
< 1)) {
1389 return EXIT_FAILURE
;
1391 if (!(opts
.namespace||opts
.capabilities
||opts
.seccomp
)) {
1392 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
1394 return EXIT_FAILURE
;
1396 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
1398 opts
.capabilities
!= 0 || opts
.capset
.apply
,
1399 opts
.seccomp
!= 0 || opts
.ociseccomp
!= 0);
1402 opts
.jail_argv
= &argv
[optind
];
1403 if (opts
.namespace & CLONE_NEWUSER
)
1404 get_jail_user(&opts
.pw_uid
, &opts
.pw_gid
, &opts
.gr_gid
);
1407 if (!opts
.extroot
) {
1408 if (opts
.namespace && add_path_and_deps(*opts
.jail_argv
, 1, -1, 0)) {
1409 ERROR("failed to load dependencies\n");
1414 if (opts
.namespace && opts
.seccomp
&& add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
1415 ERROR("failed to load libpreload-seccomp.so\n");
1417 if (opts
.require_jail
)
1422 prctl(PR_SET_NAME
, opts
.name
, NULL
, NULL
, NULL
);
1426 sigfillset(&sigmask
);
1427 for (i
= 0; i
< _NSIG
; i
++) {
1428 struct sigaction s
= { 0 };
1430 if (!sigismember(&sigmask
, i
))
1432 if ((i
== SIGCHLD
) || (i
== SIGPIPE
) || (i
== SIGSEGV
))
1435 s
.sa_handler
= jail_handle_signal
;
1436 sigaction(i
, &s
, NULL
);
1439 if (opts
.namespace) {
1440 if (opts
.namespace & CLONE_NEWNS
) {
1441 add_mount("/dev/full", 0, -1);
1442 add_mount("/dev/null", 0, -1);
1443 add_mount("/dev/random", 0, -1);
1444 add_mount("/dev/urandom", 0, -1);
1445 add_mount("/dev/zero", 0, -1);
1446 add_mount("/dev/ptmx", 0, -1);
1447 add_mount("/dev/tty", 0, -1);
1449 if (!opts
.extroot
&& (opts
.user
|| opts
.group
)) {
1450 add_mount("/etc/passwd", 0, -1);
1451 add_mount("/etc/group", 0, -1);
1454 #if defined(__GLIBC__)
1456 add_mount("/etc/nsswitch.conf", 0, -1);
1459 if (!(opts
.namespace & CLONE_NEWNET
)) {
1460 add_mount("/etc/resolv.conf", 0, -1);
1464 if (pipe(&pipes
[0]) < 0 || pipe(&pipes
[2]) < 0)
1467 jail_process
.pid
= clone(exec_jail
, child_stack
+ STACK_SIZE
, SIGCHLD
| opts
.namespace, &pipes
);
1469 jail_process
.pid
= fork();
1472 if (jail_process
.pid
> 0) {
1474 /* parent process */
1477 if (read(pipes
[0], sig_buf
, 1) < 1) {
1478 ERROR("can't read from child\n");
1482 if (opts
.namespace & CLONE_NEWUSER
) {
1483 if (write_setgroups(jail_process
.pid
, true)) {
1484 ERROR("can't write setgroups\n");
1488 bool has_gr
= (opts
.gr_gid
!= -1);
1489 if (opts
.pw_uid
!= -1) {
1490 write_single_uid_gid_map(jail_process
.pid
, 0, opts
.pw_uid
);
1491 write_single_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:opts
.pw_gid
);
1493 write_single_uid_gid_map(jail_process
.pid
, 0, 65534);
1494 write_single_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:65534);
1497 write_uid_gid_map(jail_process
.pid
, 0, opts
.uidmap
);
1499 write_uid_gid_map(jail_process
.pid
, 1, opts
.gidmap
);
1503 if (opts
.namespace & CLONE_NEWNET
) {
1505 ERROR("netns needs a named jail\n");
1508 netns_fd
= netns_open_pid(jail_process
.pid
);
1509 netns_updown(jail_process
.pid
, true);
1513 if (write(pipes
[3], sig_buf
, 1) < 0) {
1514 ERROR("can't write to child\n");
1518 uloop_process_add(&jail_process
);
1521 DEBUG("uloop interrupted, killing jail process\n");
1522 kill(jail_process
.pid
, SIGTERM
);
1523 uloop_timeout_set(&jail_process_timeout
, 1000);
1527 if (opts
.namespace & CLONE_NEWNET
) {
1528 setns(netns_fd
, CLONE_NEWNET
);
1529 netns_updown(getpid(), false);
1532 return jail_return_code
;
1533 } else if (jail_process
.pid
== 0) {
1534 /* fork child process */
1535 return exec_jail(NULL
);
1537 ERROR("failed to clone/fork: %m\n");
1538 return EXIT_FAILURE
;