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:"
45 #define NAMESPACE_MOUNT (1U << 0)
46 #define NAMESPACE_IPC (1U << 1)
47 #define NAMESPACE_NET (1U << 2)
48 #define NAMESPACE_PID (1U << 3)
49 #define NAMESPACE_USER (1U << 4)
50 #define NAMESPACE_UTS (1U << 5)
51 #define NAMESPACE_CGROUP (1U << 6)
69 extern int pivot_root(const char *new_root
, const char *put_old
);
73 static char child_stack
[STACK_SIZE
];
75 static int mkdir_p(char *dir
, mode_t mask
)
77 char *l
= strrchr(dir
, '/');
85 if (mkdir_p(dir
, mask
))
90 ret
= mkdir(dir
, mask
);
91 if (ret
&& errno
== EEXIST
)
95 ERROR("mkdir(%s, %d) failed: %m\n", dir
, mask
);
100 static int _mount_bind(const char *root
, const char *path
, const char *target
, int readonly
, int strict
, int error
)
105 int remount_flags
= MS_BIND
| MS_REMOUNT
;
107 if (stat(path
, &s
)) {
108 ERROR("stat(%s) failed: %m\n", path
);
112 snprintf(new, sizeof(new), "%s%s", root
, target
?target
:path
);
114 if (S_ISDIR(s
.st_mode
)) {
117 mkdir_p(dirname(new), 0755);
118 snprintf(new, sizeof(new), "%s%s", root
, target
?target
:path
);
119 fd
= creat(new, 0644);
121 ERROR("creat(%s) failed: %m\n", new);
127 if (mount(path
, new, NULL
, MS_BIND
, NULL
)) {
128 ERROR("failed to mount -B %s %s: %m\n", path
, new);
133 remount_flags
|= MS_RDONLY
;
136 remount_flags
|= MS_NOEXEC
| MS_NOSUID
| MS_NODEV
;
138 if ((strict
|| readonly
) && mount(NULL
, new, NULL
, remount_flags
, NULL
)) {
139 ERROR("failed to remount (%s%s%s) %s: %m\n", readonly
?"ro":"rw",
140 (readonly
&& strict
)?", ":"", strict
?"strict":"", new);
144 DEBUG("mount -B %s %s (%s%s%s)\n", path
, new,
145 readonly
?"ro":"rw", (readonly
&& strict
)?", ":"", strict
?"strict":"");
150 int mount_bind(const char *root
, const char *path
, int readonly
, int error
) {
151 return _mount_bind(root
, path
, NULL
, readonly
, 0, error
);
154 static int build_jail_fs(void)
156 char jail_root
[] = "/tmp/ujail-XXXXXX";
157 if (mkdtemp(jail_root
) == NULL
) {
158 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
162 /* oldroot can't be MS_SHARED else pivot_root() fails */
163 if (mount("none", "/", NULL
, MS_REC
|MS_PRIVATE
, NULL
)) {
164 ERROR("private mount failed %m\n");
169 if (mount(opts
.extroot
, jail_root
, NULL
, MS_BIND
| MS_REC
, NULL
)) {
170 ERROR("extroot mount failed %m\n");
174 if (mount("tmpfs", jail_root
, "tmpfs", MS_NOATIME
, "mode=0755")) {
175 ERROR("tmpfs mount failed %m\n");
180 if (chdir(jail_root
)) {
181 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root
);
185 if (mount_all(jail_root
)) {
186 ERROR("mount_all() failed\n");
190 if (opts
.namespace & NAMESPACE_NET
) {
191 char hostdir
[PATH_MAX
], jailetc
[PATH_MAX
], jaillink
[PATH_MAX
];
193 snprintf(hostdir
, PATH_MAX
, "/tmp/resolv.conf-%s.d", opts
.name
);
194 mkdir_p(hostdir
, 0755);
195 _mount_bind(jail_root
, hostdir
, "/tmp/resolv.conf.d", 1, 1, -1);
196 snprintf(jailetc
, PATH_MAX
, "%s/etc", jail_root
);
197 mkdir_p(jailetc
, 0755);
198 snprintf(jaillink
, PATH_MAX
, "%s/etc/resolv.conf", jail_root
);
199 symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink
);
202 char dirbuf
[sizeof(jail_root
) + 4];
203 snprintf(dirbuf
, sizeof(dirbuf
), "%s/old", jail_root
);
206 if (pivot_root(jail_root
, dirbuf
) == -1) {
207 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root
, dirbuf
);
211 ERROR("chdir(/) (after pivot_root) failed: %m\n");
215 snprintf(dirbuf
, sizeof(dirbuf
), "/old%s", jail_root
);
217 umount2("/old", MNT_DETACH
);
221 mkdir("/proc", 0755);
222 mount("proc", "/proc", "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
226 mount("sysfs", "/sys", "sysfs", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
229 mount(NULL
, "/", NULL
, MS_RDONLY
| MS_REMOUNT
, 0);
235 static char** build_envp(const char *seccomp
)
237 static char *envp
[MAX_ENVP
];
238 static char preload_var
[PATH_MAX
];
239 static char seccomp_var
[PATH_MAX
];
240 static char debug_var
[] = "LD_DEBUG=all";
241 static char container_var
[] = "container=ujail";
242 const char *preload_lib
= find_lib("libpreload-seccomp.so");
245 if (seccomp
&& !preload_lib
) {
246 ERROR("failed to add preload-lib to env\n");
250 snprintf(seccomp_var
, sizeof(seccomp_var
), "SECCOMP_FILE=%s", seccomp
);
251 envp
[count
++] = seccomp_var
;
252 snprintf(preload_var
, sizeof(preload_var
), "LD_PRELOAD=%s", preload_lib
);
253 envp
[count
++] = preload_var
;
257 envp
[count
++] = container_var
;
260 envp
[count
++] = debug_var
;
265 static void usage(void)
267 fprintf(stderr
, "ujail <options> -- <binary> <params ...>\n");
268 fprintf(stderr
, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
269 fprintf(stderr
, " -S <file>\tseccomp filter config\n");
270 fprintf(stderr
, " -C <file>\tcapabilities drop config\n");
271 fprintf(stderr
, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
272 fprintf(stderr
, " -n <name>\tthe name of the jail\n");
273 fprintf(stderr
, "namespace jail options:\n");
274 fprintf(stderr
, " -h <hostname>\tchange the hostname of the jail\n");
275 fprintf(stderr
, " -N\t\tjail has network namespace\n");
276 fprintf(stderr
, " -r <file>\treadonly files that should be staged\n");
277 fprintf(stderr
, " -w <file>\twriteable files that should be staged\n");
278 fprintf(stderr
, " -p\t\tjail has /proc\n");
279 fprintf(stderr
, " -s\t\tjail has /sys\n");
280 fprintf(stderr
, " -l\t\tjail has /dev/log\n");
281 fprintf(stderr
, " -u\t\tjail has a ubus socket\n");
282 fprintf(stderr
, " -U <name>\tuser to run jailed process\n");
283 fprintf(stderr
, " -G <name>\tgroup to run jailed process\n");
284 fprintf(stderr
, " -o\t\tremont jail root (/) read only\n");
285 fprintf(stderr
, " -R <dir>\texternal jail rootfs (system container)\n");
286 fprintf(stderr
, "\nWarning: by default root inside the jail is the same\n\
287 and he has the same powers as root outside the jail,\n\
288 thus he can escape the jail and/or break stuff.\n\
289 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
290 If you use none of the namespace jail options,\n\
291 ujail will not use namespace/build a jail,\n\
292 and will only drop capabilities/apply seccomp filter.\n\n");
295 static int exec_jail(void *_notused
)
297 struct passwd
*p
= NULL
;
298 struct group
*g
= NULL
;
300 if (opts
.capabilities
&& drop_capabilities(opts
.capabilities
))
303 if (opts
.no_new_privs
&& prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
304 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
308 if (opts
.namespace && opts
.hostname
&& strlen(opts
.hostname
) > 0
309 && sethostname(opts
.hostname
, strlen(opts
.hostname
))) {
310 ERROR("sethostname(%s) failed: %m\n", opts
.hostname
);
314 if (opts
.namespace && build_jail_fs()) {
315 ERROR("failed to build jail fs\n");
320 p
= getpwnam(opts
.user
);
322 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
323 opts
.user
, errno
, strerror(errno
));
329 g
= getgrnam(opts
.group
);
331 ERROR("failed to get gid for group %s: %m\n", opts
.group
);
336 if (p
&& p
->pw_gid
&& initgroups(opts
.user
, p
->pw_gid
)) {
337 ERROR("failed to initgroups() for user %s: %m\n", opts
.user
);
341 if (g
&& g
->gr_gid
&& setgid(g
->gr_gid
)) {
342 ERROR("failed to set group id %d: %m\n", g
?g
->gr_gid
:p
->pw_gid
);
346 if (p
&& p
->pw_uid
&& setuid(p
->pw_uid
)) {
347 ERROR("failed to set user id %d: %m\n", p
->pw_uid
);
352 char **envp
= build_envp(opts
.seccomp
);
356 INFO("exec-ing %s\n", *opts
.jail_argv
);
357 execve(*opts
.jail_argv
, opts
.jail_argv
, envp
);
358 /* we get there only if execve fails */
359 ERROR("failed to execve %s: %m\n", *opts
.jail_argv
);
363 static int jail_running
= 1;
364 static int jail_return_code
= 0;
366 static void jail_process_timeout_cb(struct uloop_timeout
*t
);
367 static struct uloop_timeout jail_process_timeout
= {
368 .cb
= jail_process_timeout_cb
,
371 static void jail_process_handler(struct uloop_process
*c
, int ret
)
373 uloop_timeout_cancel(&jail_process_timeout
);
374 if (WIFEXITED(ret
)) {
375 jail_return_code
= WEXITSTATUS(ret
);
376 INFO("jail (%d) exited with exit: %d\n", c
->pid
, jail_return_code
);
378 jail_return_code
= WTERMSIG(ret
);
379 INFO("jail (%d) exited with signal: %d\n", c
->pid
, jail_return_code
);
385 static struct uloop_process jail_process
= {
386 .cb
= jail_process_handler
,
389 static void jail_process_timeout_cb(struct uloop_timeout
*t
)
391 DEBUG("jail process failed to stop, sending SIGKILL\n");
392 kill(jail_process
.pid
, SIGKILL
);
395 static void jail_handle_signal(int signo
)
397 DEBUG("forwarding signal %d to the jailed process\n", signo
);
398 kill(jail_process
.pid
, signo
);
401 static void netns_updown(bool start
)
403 struct ubus_context
*ctx
= ubus_connect(NULL
);
404 static struct blob_buf req
;
406 pid_t pid
= getpid();
411 blob_buf_init(&req
, 0);
412 blobmsg_add_string(&req
, "jail", opts
.name
);
413 blobmsg_add_u32(&req
, "pid", pid
);
414 blobmsg_add_u8(&req
, "start", start
);
416 if (ubus_lookup_id(ctx
, "network", &id
) ||
417 ubus_invoke(ctx
, id
, "netns_updown", req
.head
, NULL
, NULL
, 3000))
418 INFO("ubus request failed\n");
424 int main(int argc
, char **argv
)
427 uid_t uid
= getuid();
428 char log
[] = "/dev/log";
429 char ubus
[] = "/var/run/ubus.sock";
433 ERROR("not root, aborting: %m\n");
439 init_library_search();
441 while ((ch
= getopt(argc
, argv
, OPT_ARGS
)) != -1) {
444 debug
= atoi(optarg
);
447 opts
.namespace |= NAMESPACE_MOUNT
;
451 opts
.namespace |= NAMESPACE_MOUNT
;
455 opts
.namespace |= NAMESPACE_MOUNT
| NAMESPACE_UTS
;
456 opts
.extroot
= optarg
;
459 opts
.namespace |= NAMESPACE_MOUNT
;
463 opts
.seccomp
= optarg
;
464 add_mount(optarg
, 1, -1);
467 opts
.capabilities
= optarg
;
470 opts
.no_new_privs
= 1;
476 opts
.namespace |= NAMESPACE_NET
;
479 opts
.hostname
= optarg
;
482 opts
.namespace |= NAMESPACE_MOUNT
;
483 add_path_and_deps(optarg
, 1, 0, 0);
486 opts
.namespace |= NAMESPACE_MOUNT
;
487 add_path_and_deps(optarg
, 0, 0, 0);
490 opts
.namespace |= NAMESPACE_MOUNT
;
491 add_mount(ubus
, 0, -1);
494 opts
.namespace |= NAMESPACE_MOUNT
;
495 add_mount(log
, 0, -1);
506 /* no <binary> param found */
507 if (argc
- optind
< 1) {
511 if (!(opts
.namespace||opts
.capabilities
||opts
.seccomp
)) {
512 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
516 DEBUG("Using namespaces(%d), capabilities(%d), seccomp(%d)\n",
518 opts
.capabilities
!= 0,
521 opts
.jail_argv
= &argv
[optind
];
524 if (opts
.namespace && add_path_and_deps(*opts
.jail_argv
, 1, -1, 0)) {
525 ERROR("failed to load dependencies\n");
529 if (opts
.namespace && opts
.seccomp
&& add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
530 ERROR("failed to load libpreload-seccomp.so\n");
536 prctl(PR_SET_NAME
, opts
.name
, NULL
, NULL
, NULL
);
540 sigfillset(&sigmask
);
541 for (i
= 0; i
< _NSIG
; i
++) {
542 struct sigaction s
= { 0 };
544 if (!sigismember(&sigmask
, i
))
546 if ((i
== SIGCHLD
) || (i
== SIGPIPE
))
549 s
.sa_handler
= jail_handle_signal
;
550 sigaction(i
, &s
, NULL
);
553 if (opts
.namespace) {
554 int flags
= SIGCHLD
| CLONE_NEWPID
| CLONE_NEWIPC
;
556 if (opts
.namespace & NAMESPACE_MOUNT
) {
557 flags
|= CLONE_NEWNS
;
558 add_mount("/dev/full", 0, -1);
559 add_mount("/dev/null", 0, -1);
560 add_mount("/dev/random", 0, -1);
561 add_mount("/dev/urandom", 0, -1);
562 add_mount("/dev/tty", 0, -1);
563 add_mount("/dev/zero", 0, -1);
564 add_mount("/dev/console", 0, -1);
566 if (opts
.user
|| opts
.group
) {
567 add_mount("/etc/passwd", 0, -1);
568 add_mount("/etc/group", 0, -1);
571 if (!(opts
.namespace & CLONE_NEWNET
)) {
572 add_mount("/etc/resolv.conf", 0, -1);
577 flags
|= CLONE_NEWUTS
;
579 if (opts
.namespace & NAMESPACE_NET
) {
580 unshare(CLONE_NEWNET
);
584 jail_process
.pid
= clone(exec_jail
, child_stack
+ STACK_SIZE
, flags
, NULL
);
586 jail_process
.pid
= fork();
589 if (jail_process
.pid
> 0) {
591 uloop_process_add(&jail_process
);
594 DEBUG("uloop interrupted, killing jail process\n");
595 kill(jail_process
.pid
, SIGTERM
);
596 uloop_timeout_set(&jail_process_timeout
, 1000);
600 if (opts
.namespace & NAMESPACE_NET
)
603 return jail_return_code
;
604 } else if (jail_process
.pid
== 0) {
605 /* fork child process */
606 return exec_jail(NULL
);
608 ERROR("failed to clone/fork: %m\n");