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>
33 #include "capabilities.h"
39 #include <libubox/uloop.h>
42 #define STACK_SIZE (1024 * 1024)
43 #define OPT_ARGS "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:E"
68 extern int pivot_root(const char *new_root
, const char *put_old
);
72 static char child_stack
[STACK_SIZE
];
74 static int mkdir_p(char *dir
, mode_t mask
)
76 char *l
= strrchr(dir
, '/');
84 if (mkdir_p(dir
, mask
))
89 ret
= mkdir(dir
, mask
);
90 if (ret
&& errno
== EEXIST
)
94 ERROR("mkdir(%s, %d) failed: %m\n", dir
, mask
);
99 static int _mount_bind(const char *root
, const char *path
, const char *target
, int readonly
, int strict
, int error
)
104 int remount_flags
= MS_BIND
| MS_REMOUNT
;
106 if (stat(path
, &s
)) {
107 ERROR("stat(%s) failed: %m\n", path
);
111 snprintf(new, sizeof(new), "%s%s", root
, target
?target
:path
);
113 if (S_ISDIR(s
.st_mode
)) {
116 mkdir_p(dirname(new), 0755);
117 snprintf(new, sizeof(new), "%s%s", root
, target
?target
:path
);
118 fd
= creat(new, 0644);
120 ERROR("creat(%s) failed: %m\n", new);
126 if (mount(path
, new, NULL
, MS_BIND
, NULL
)) {
127 ERROR("failed to mount -B %s %s: %m\n", path
, new);
132 remount_flags
|= MS_RDONLY
;
135 remount_flags
|= MS_NOEXEC
| MS_NOSUID
| MS_NODEV
;
137 if ((strict
|| readonly
) && mount(NULL
, new, NULL
, remount_flags
, NULL
)) {
138 ERROR("failed to remount (%s%s%s) %s: %m\n", readonly
?"ro":"rw",
139 (readonly
&& strict
)?", ":"", strict
?"strict":"", new);
143 DEBUG("mount -B %s %s (%s%s%s)\n", path
, new,
144 readonly
?"ro":"rw", (readonly
&& strict
)?", ":"", strict
?"strict":"");
149 int mount_bind(const char *root
, const char *path
, int readonly
, int error
) {
150 return _mount_bind(root
, path
, NULL
, readonly
, 0, error
);
153 static int mount_overlay(char *jail_root
, char *overlaydir
) {
154 char *upperdir
, *workdir
, *optsstr
;
155 const char mountoptsformat
[] = "lowerdir=%s,upperdir=%s,workdir=%s";
158 if (asprintf(&upperdir
, "%s%s", overlaydir
, "/upper") < 0)
161 if (asprintf(&workdir
, "%s%s", overlaydir
, "/work") < 0)
164 if (asprintf(&optsstr
, mountoptsformat
, jail_root
, upperdir
, workdir
) < 0)
167 if (mkdir_p(upperdir
, 0755) || mkdir_p(workdir
, 0755))
170 DEBUG("mount -t overlay %s %s (%s)\n", jail_root
, jail_root
, optsstr
);
172 if (mount(jail_root
, jail_root
, "overlay", MS_NOATIME
, optsstr
))
187 static int build_jail_fs(void)
189 char jail_root
[] = "/tmp/ujail-XXXXXX";
190 char tmpovdir
[] = "/tmp/ujail-overlay-XXXXXX";
191 char tmpdevdir
[] = "/tmp/ujail-XXXXXX/dev";
192 char *overlaydir
= NULL
;
194 if (mkdtemp(jail_root
) == NULL
) {
195 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
199 /* oldroot can't be MS_SHARED else pivot_root() fails */
200 if (mount("none", "/", NULL
, MS_REC
|MS_PRIVATE
, NULL
)) {
201 ERROR("private mount failed %m\n");
206 if (mount(opts
.extroot
, jail_root
, NULL
, MS_BIND
, NULL
)) {
207 ERROR("extroot mount failed %m\n");
211 if (mount("tmpfs", jail_root
, "tmpfs", MS_NOATIME
, "mode=0755")) {
212 ERROR("tmpfs mount failed %m\n");
217 if (opts
.tmpoverlaysize
) {
218 char mountoptsstr
[] = "mode=0755,size=XXXXXXXX";
220 snprintf(mountoptsstr
, sizeof(mountoptsstr
),
221 "mode=0755,size=%s", opts
.tmpoverlaysize
);
222 if (mkdtemp(tmpovdir
) == NULL
) {
223 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
226 if (mount("tmpfs", tmpovdir
, "tmpfs", MS_NOATIME
,
228 ERROR("failed to mount tmpfs for overlay (size=%s)\n", opts
.tmpoverlaysize
);
231 overlaydir
= tmpovdir
;
235 overlaydir
= opts
.overlaydir
;
238 mount_overlay(jail_root
, overlaydir
);
240 if (chdir(jail_root
)) {
241 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root
);
245 snprintf(tmpdevdir
, sizeof(tmpdevdir
), "%s/dev", jail_root
);
246 mkdir_p(tmpdevdir
, 0755);
247 if (mount(NULL
, tmpdevdir
, "tmpfs", MS_NOATIME
| MS_NOEXEC
| MS_NOSUID
, "size=1M"))
250 if (mount_all(jail_root
)) {
251 ERROR("mount_all() failed\n");
255 if (opts
.namespace & CLONE_NEWNET
) {
256 char hostdir
[PATH_MAX
], jailetc
[PATH_MAX
], jaillink
[PATH_MAX
];
258 snprintf(hostdir
, PATH_MAX
, "/tmp/resolv.conf-%s.d", opts
.name
);
259 mkdir_p(hostdir
, 0755);
260 _mount_bind(jail_root
, hostdir
, "/tmp/resolv.conf.d", 1, 1, -1);
261 snprintf(jailetc
, PATH_MAX
, "%s/etc", jail_root
);
262 mkdir_p(jailetc
, 0755);
263 snprintf(jaillink
, PATH_MAX
, "%s/etc/resolv.conf", jail_root
);
266 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink
);
269 char dirbuf
[sizeof(jail_root
) + 4];
270 snprintf(dirbuf
, sizeof(dirbuf
), "%s/old", jail_root
);
273 if (pivot_root(jail_root
, dirbuf
) == -1) {
274 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root
, dirbuf
);
278 ERROR("chdir(/) (after pivot_root) failed: %m\n");
282 snprintf(dirbuf
, sizeof(dirbuf
), "/old%s", jail_root
);
283 umount2(dirbuf
, MNT_DETACH
);
285 if (opts
.tmpoverlaysize
) {
286 char tmpdirbuf
[sizeof(tmpovdir
) + 4];
287 snprintf(tmpdirbuf
, sizeof(tmpdirbuf
), "/old%s", tmpovdir
);
288 umount2(tmpdirbuf
, MNT_DETACH
);
292 umount2("/old", MNT_DETACH
);
296 mkdir("/proc", 0755);
297 mount("proc", "/proc", "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
299 * make /proc/sys read-only while keeping read-write to
300 * /proc/sys/net if CLONE_NEWNET is set.
302 if (opts
.namespace & CLONE_NEWNET
)
303 mount("/proc/sys/net", "/proc/self/net", NULL
, MS_BIND
, 0);
305 mount("/proc/sys", "/proc/sys", NULL
, MS_BIND
, 0);
306 mount(NULL
, "/proc/sys", NULL
, MS_REMOUNT
| MS_RDONLY
, 0);
307 mount(NULL
, "/proc", NULL
, MS_REMOUNT
, 0);
309 if (opts
.namespace & CLONE_NEWNET
)
310 mount("/proc/self/net", "/proc/sys/net", NULL
, MS_MOVE
, 0);
314 mount("sysfs", "/sys", "sysfs", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RDONLY
, 0);
317 mount(NULL
, "/", NULL
, MS_RDONLY
| MS_REMOUNT
, 0);
322 static int write_uid_gid_map(pid_t child_pid
, bool gidmap
, int id
)
326 const char *map_format
= "%d %d %d\n";
327 if (snprintf(map_path
, sizeof(map_path
), "/proc/%d/%s",
328 child_pid
, gidmap
?"gid_map":"uid_map") < 0)
331 if ((map_file
= open(map_path
, O_WRONLY
)) == -1)
334 if (dprintf(map_file
, map_format
, 0, id
, 1) == -1) {
343 static int write_setgroups(pid_t child_pid
, bool allow
)
346 char setgroups_path
[64];
348 if (snprintf(setgroups_path
, sizeof(setgroups_path
), "/proc/%d/setgroups",
353 if ((setgroups_file
= open(setgroups_path
, O_WRONLY
)) == -1) {
357 if (dprintf(setgroups_file
, allow
?"allow":"deny") == -1) {
358 close(setgroups_file
);
362 close(setgroups_file
);
366 static void get_jail_user(int *user
, int *user_gid
, int *gr_gid
)
368 struct passwd
*p
= NULL
;
369 struct group
*g
= NULL
;
372 p
= getpwnam(opts
.user
);
374 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
375 opts
.user
, errno
, strerror(errno
));
379 *user_gid
= p
->pw_gid
;
386 g
= getgrnam(opts
.group
);
388 ERROR("failed to get gid for group %s: %m\n", opts
.group
);
397 static void set_jail_user(int pw_uid
, int user_gid
, int gr_gid
)
399 if ((user_gid
!= -1) && initgroups(opts
.user
, user_gid
)) {
400 ERROR("failed to initgroups() for user %s: %m\n", opts
.user
);
404 if ((gr_gid
!= -1) && setregid(gr_gid
, gr_gid
)) {
405 ERROR("failed to set group id %d: %m\n", gr_gid
);
409 if ((pw_uid
!= -1) && setreuid(pw_uid
, pw_uid
)) {
410 ERROR("failed to set user id %d: %m\n", pw_uid
);
416 static char** build_envp(const char *seccomp
)
418 static char *envp
[MAX_ENVP
];
419 static char preload_var
[PATH_MAX
];
420 static char seccomp_var
[PATH_MAX
];
421 static char debug_var
[] = "LD_DEBUG=all";
422 static char container_var
[] = "container=ujail";
423 const char *preload_lib
= find_lib("libpreload-seccomp.so");
426 if (seccomp
&& !preload_lib
) {
427 ERROR("failed to add preload-lib to env\n");
431 snprintf(seccomp_var
, sizeof(seccomp_var
), "SECCOMP_FILE=%s", seccomp
);
432 envp
[count
++] = seccomp_var
;
433 snprintf(preload_var
, sizeof(preload_var
), "LD_PRELOAD=%s", preload_lib
);
434 envp
[count
++] = preload_var
;
437 envp
[count
++] = container_var
;
440 envp
[count
++] = debug_var
;
445 static void usage(void)
447 fprintf(stderr
, "ujail <options> -- <binary> <params ...>\n");
448 fprintf(stderr
, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
449 fprintf(stderr
, " -S <file>\tseccomp filter config\n");
450 fprintf(stderr
, " -C <file>\tcapabilities drop config\n");
451 fprintf(stderr
, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
452 fprintf(stderr
, " -n <name>\tthe name of the jail\n");
453 fprintf(stderr
, "namespace jail options:\n");
454 fprintf(stderr
, " -h <hostname>\tchange the hostname of the jail\n");
455 fprintf(stderr
, " -N\t\tjail has network namespace\n");
456 fprintf(stderr
, " -f\t\tjail has user namespace\n");
457 fprintf(stderr
, " -F\t\tjail has cgroups namespace\n");
458 fprintf(stderr
, " -r <file>\treadonly files that should be staged\n");
459 fprintf(stderr
, " -w <file>\twriteable files that should be staged\n");
460 fprintf(stderr
, " -p\t\tjail has /proc\n");
461 fprintf(stderr
, " -s\t\tjail has /sys\n");
462 fprintf(stderr
, " -l\t\tjail has /dev/log\n");
463 fprintf(stderr
, " -u\t\tjail has a ubus socket\n");
464 fprintf(stderr
, " -U <name>\tuser to run jailed process\n");
465 fprintf(stderr
, " -G <name>\tgroup to run jailed process\n");
466 fprintf(stderr
, " -o\t\tremont jail root (/) read only\n");
467 fprintf(stderr
, " -R <dir>\texternal jail rootfs (system container)\n");
468 fprintf(stderr
, " -O <dir>\tdirectory for r/w overlayfs\n");
469 fprintf(stderr
, " -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
470 fprintf(stderr
, " -E\t\tfail if jail cannot be setup\n");
471 fprintf(stderr
, "\nWarning: by default root inside the jail is the same\n\
472 and he has the same powers as root outside the jail,\n\
473 thus he can escape the jail and/or break stuff.\n\
474 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
475 If you use none of the namespace jail options,\n\
476 ujail will not use namespace/build a jail,\n\
477 and will only drop capabilities/apply seccomp filter.\n\n");
480 static int exec_jail(void *pipes_ptr
)
482 int *pipes
= (int*)pipes_ptr
;
484 int pw_uid
, pw_gid
, gr_gid
;
491 if (write(pipes
[1], buf
, 1) < 1) {
492 ERROR("can't write to parent\n");
495 if (read(pipes
[2], buf
, 1) < 1) {
496 ERROR("can't read from parent\n");
500 ERROR("parent had an error, child exiting\n");
507 if (opts
.namespace & CLONE_NEWUSER
) {
516 // if (setgroups(0, NULL) < 0) {
517 // ERROR("setgroups\n");
518 // exit(EXIT_FAILURE);
522 if (opts
.namespace && opts
.hostname
&& strlen(opts
.hostname
) > 0
523 && sethostname(opts
.hostname
, strlen(opts
.hostname
))) {
524 ERROR("sethostname(%s) failed: %m\n", opts
.hostname
);
528 if ((opts
.namespace & CLONE_NEWNS
) && build_jail_fs()) {
529 ERROR("failed to build jail fs\n");
533 if (opts
.capabilities
&& drop_capabilities(opts
.capabilities
))
536 if (opts
.no_new_privs
&& prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
537 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
541 if (!(opts
.namespace & CLONE_NEWUSER
)) {
542 get_jail_user(&pw_uid
, &pw_gid
, &gr_gid
);
543 set_jail_user(pw_uid
, pw_gid
, gr_gid
);
546 char **envp
= build_envp(opts
.seccomp
);
550 INFO("exec-ing %s\n", *opts
.jail_argv
);
551 execve(*opts
.jail_argv
, opts
.jail_argv
, envp
);
552 /* we get there only if execve fails */
553 ERROR("failed to execve %s: %m\n", *opts
.jail_argv
);
557 static int jail_running
= 1;
558 static int jail_return_code
= 0;
560 static void jail_process_timeout_cb(struct uloop_timeout
*t
);
561 static struct uloop_timeout jail_process_timeout
= {
562 .cb
= jail_process_timeout_cb
,
565 static void jail_process_handler(struct uloop_process
*c
, int ret
)
567 uloop_timeout_cancel(&jail_process_timeout
);
568 if (WIFEXITED(ret
)) {
569 jail_return_code
= WEXITSTATUS(ret
);
570 INFO("jail (%d) exited with exit: %d\n", c
->pid
, jail_return_code
);
572 jail_return_code
= WTERMSIG(ret
);
573 INFO("jail (%d) exited with signal: %d\n", c
->pid
, jail_return_code
);
579 static struct uloop_process jail_process
= {
580 .cb
= jail_process_handler
,
583 static void jail_process_timeout_cb(struct uloop_timeout
*t
)
585 DEBUG("jail process failed to stop, sending SIGKILL\n");
586 kill(jail_process
.pid
, SIGKILL
);
589 static void jail_handle_signal(int signo
)
591 DEBUG("forwarding signal %d to the jailed process\n", signo
);
592 kill(jail_process
.pid
, signo
);
595 static int netns_open_pid(const pid_t target_ns
)
597 char pid_net_path
[PATH_MAX
];
599 snprintf(pid_net_path
, sizeof(pid_net_path
), "/proc/%u/ns/net", target_ns
);
601 return open(pid_net_path
, O_RDONLY
);
604 static void netns_updown(pid_t pid
, bool start
)
606 struct ubus_context
*ctx
= ubus_connect(NULL
);
607 static struct blob_buf req
;
613 blob_buf_init(&req
, 0);
614 blobmsg_add_string(&req
, "jail", opts
.name
);
615 blobmsg_add_u32(&req
, "pid", pid
);
616 blobmsg_add_u8(&req
, "start", start
);
618 if (ubus_lookup_id(ctx
, "network", &id
) ||
619 ubus_invoke(ctx
, id
, "netns_updown", req
.head
, NULL
, NULL
, 3000))
620 INFO("ubus request failed\n");
626 int main(int argc
, char **argv
)
629 uid_t uid
= getuid();
630 char log
[] = "/dev/log";
631 char ubus
[] = "/var/run/ubus.sock";
638 ERROR("not root, aborting: %m\n");
644 init_library_search();
646 while ((ch
= getopt(argc
, argv
, OPT_ARGS
)) != -1) {
649 debug
= atoi(optarg
);
652 opts
.namespace |= CLONE_NEWNS
;
656 opts
.namespace |= CLONE_NEWNS
;
660 opts
.namespace |= CLONE_NEWUSER
;
663 opts
.namespace |= CLONE_NEWCGROUP
;
666 opts
.extroot
= optarg
;
669 opts
.namespace |= CLONE_NEWNS
;
673 opts
.seccomp
= optarg
;
674 add_mount(optarg
, 1, -1);
677 opts
.capabilities
= optarg
;
680 opts
.no_new_privs
= 1;
686 opts
.namespace |= CLONE_NEWNET
;
689 opts
.namespace |= CLONE_NEWUTS
;
690 opts
.hostname
= optarg
;
693 opts
.namespace |= CLONE_NEWNS
;
694 add_path_and_deps(optarg
, 1, 0, 0);
697 opts
.namespace |= CLONE_NEWNS
;
698 add_path_and_deps(optarg
, 0, 0, 0);
701 opts
.namespace |= CLONE_NEWNS
;
702 add_mount(ubus
, 0, -1);
705 opts
.namespace |= CLONE_NEWNS
;
706 add_mount(log
, 0, -1);
715 opts
.overlaydir
= optarg
;
718 opts
.tmpoverlaysize
= optarg
;
721 opts
.require_jail
= 1;
727 opts
.namespace |= CLONE_NEWIPC
| CLONE_NEWPID
;
729 if (opts
.tmpoverlaysize
&& strlen(opts
.tmpoverlaysize
) > 8) {
730 ERROR("size parameter too long: \"%s\"\n", opts
.tmpoverlaysize
);
734 /* no <binary> param found */
735 if (argc
- optind
< 1) {
739 if (!(opts
.namespace||opts
.capabilities
||opts
.seccomp
)) {
740 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
744 DEBUG("Using namespaces(0x%08x), capabilities(%d), seccomp(%d)\n",
746 opts
.capabilities
!= 0,
749 opts
.jail_argv
= &argv
[optind
];
751 get_jail_user(&opts
.pw_uid
, &opts
.pw_gid
, &opts
.gr_gid
);
754 if (opts
.namespace && add_path_and_deps(*opts
.jail_argv
, 1, -1, 0)) {
755 ERROR("failed to load dependencies\n");
760 if (opts
.namespace && opts
.seccomp
&& add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
761 ERROR("failed to load libpreload-seccomp.so\n");
763 if (opts
.require_jail
)
768 prctl(PR_SET_NAME
, opts
.name
, NULL
, NULL
, NULL
);
772 sigfillset(&sigmask
);
773 for (i
= 0; i
< _NSIG
; i
++) {
774 struct sigaction s
= { 0 };
776 if (!sigismember(&sigmask
, i
))
778 if ((i
== SIGCHLD
) || (i
== SIGPIPE
) || (i
== SIGSEGV
))
781 s
.sa_handler
= jail_handle_signal
;
782 sigaction(i
, &s
, NULL
);
785 if (opts
.namespace) {
786 if (opts
.namespace & CLONE_NEWNS
) {
787 add_mount("/dev/full", 0, -1);
788 add_mount("/dev/null", 0, -1);
789 add_mount("/dev/random", 0, -1);
790 add_mount("/dev/urandom", 0, -1);
791 add_mount("/dev/tty", 0, -1);
792 add_mount("/dev/zero", 0, -1);
793 add_mount("/dev/console", 0, -1);
795 if (!opts
.extroot
&& (opts
.user
|| opts
.group
)) {
796 add_mount("/etc/passwd", 0, -1);
797 add_mount("/etc/group", 0, -1);
800 #if defined(__GLIBC__)
802 add_mount("/etc/nsswitch.conf", 0, -1);
805 if (!(opts
.namespace & CLONE_NEWNET
)) {
806 add_mount("/etc/resolv.conf", 0, -1);
810 if (pipe(&pipes
[0]) < 0 || pipe(&pipes
[2]) < 0)
813 jail_process
.pid
= clone(exec_jail
, child_stack
+ STACK_SIZE
, SIGCHLD
| opts
.namespace, &pipes
);
815 jail_process
.pid
= fork();
818 if (jail_process
.pid
> 0) {
823 if (read(pipes
[0], sig_buf
, 1) < 1) {
824 ERROR("can't read from child\n");
828 if (opts
.namespace & CLONE_NEWUSER
) {
829 bool has_gr
= (opts
.gr_gid
!= -1);
830 if (write_setgroups(jail_process
.pid
, false)) {
831 ERROR("can't write setgroups\n");
834 if (opts
.pw_uid
!= -1) {
835 write_uid_gid_map(jail_process
.pid
, 0, opts
.pw_uid
);
836 write_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:opts
.pw_gid
);
838 write_uid_gid_map(jail_process
.pid
, 0, 65534);
839 write_uid_gid_map(jail_process
.pid
, 1, has_gr
?opts
.gr_gid
:65534);
843 if (opts
.namespace & CLONE_NEWNET
) {
844 netns_fd
= netns_open_pid(jail_process
.pid
);
845 netns_updown(jail_process
.pid
, true);
849 if (write(pipes
[3], sig_buf
, 1) < 0) {
850 ERROR("can't write to child\n");
854 uloop_process_add(&jail_process
);
857 DEBUG("uloop interrupted, killing jail process\n");
858 kill(jail_process
.pid
, SIGTERM
);
859 uloop_timeout_set(&jail_process_timeout
, 1000);
863 if (opts
.namespace & CLONE_NEWNET
) {
864 setns(netns_fd
, CLONE_NEWNET
);
865 netns_updown(getpid(), false);
868 return jail_return_code
;
869 } else if (jail_process
.pid
== 0) {
870 /* fork child process */
871 return exec_jail(NULL
);
873 ERROR("failed to clone/fork: %m\n");