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>
27 #include <linux/limits.h>
30 #include "capabilities.h"
36 #include <libubox/uloop.h>
38 #define STACK_SIZE (1024 * 1024)
39 #define OPT_ARGS "S:C:n:h:r:w:d:psuloc"
54 extern int pivot_root(const char *new_root
, const char *put_old
);
58 static char child_stack
[STACK_SIZE
];
60 static int mkdir_p(char *dir
, mode_t mask
)
62 char *l
= strrchr(dir
, '/');
70 if (mkdir_p(dir
, mask
))
75 ret
= mkdir(dir
, mask
);
76 if (ret
&& errno
== EEXIST
)
80 ERROR("mkdir(%s, %d) failed: %m\n", dir
, mask
);
85 int mount_bind(const char *root
, const char *path
, int readonly
, int error
)
92 ERROR("stat(%s) failed: %m\n", path
);
96 snprintf(new, sizeof(new), "%s%s", root
, path
);
97 if (S_ISDIR(s
.st_mode
)) {
100 mkdir_p(dirname(new), 0755);
101 snprintf(new, sizeof(new), "%s%s", root
, path
);
102 fd
= creat(new, 0644);
104 ERROR("creat(%s) failed: %m\n", new);
110 if (mount(path
, new, NULL
, MS_BIND
, NULL
)) {
111 ERROR("failed to mount -B %s %s: %m\n", path
, new);
115 if (readonly
&& mount(NULL
, new, NULL
, MS_BIND
| MS_REMOUNT
| MS_RDONLY
, NULL
)) {
116 ERROR("failed to remount ro %s: %m\n", new);
120 DEBUG("mount -B %s %s (%s)\n", path
, new, readonly
?"ro":"rw");
125 static int build_jail_fs(void)
127 char jail_root
[] = "/tmp/ujail-XXXXXX";
128 if (mkdtemp(jail_root
) == NULL
) {
129 ERROR("mkdtemp(%s) failed: %m\n", jail_root
);
133 /* oldroot can't be MS_SHARED else pivot_root() fails */
134 if (mount("none", "/", NULL
, MS_REC
|MS_PRIVATE
, NULL
)) {
135 ERROR("private mount failed %m\n");
139 if (mount("tmpfs", jail_root
, "tmpfs", MS_NOATIME
, "mode=0755")) {
140 ERROR("tmpfs mount failed %m\n");
144 if (chdir(jail_root
)) {
145 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root
);
149 if (mount_all(jail_root
)) {
150 ERROR("mount_all() failed\n");
154 char dirbuf
[sizeof(jail_root
) + 4];
155 snprintf(dirbuf
, sizeof(dirbuf
), "%s/old", jail_root
);
158 if (pivot_root(jail_root
, dirbuf
) == -1) {
159 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root
, dirbuf
);
163 ERROR("chdir(/) (after pivot_root) failed: %m\n");
167 snprintf(dirbuf
, sizeof(dirbuf
), "/old%s", jail_root
);
169 umount2("/old", MNT_DETACH
);
173 mkdir("/proc", 0755);
174 mount("proc", "/proc", "proc", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
178 mount("sysfs", "/sys", "sysfs", MS_NOATIME
| MS_NODEV
| MS_NOEXEC
| MS_NOSUID
, 0);
181 mount(NULL
, "/", NULL
, MS_RDONLY
| MS_REMOUNT
, 0);
187 static char** build_envp(const char *seccomp
)
189 static char *envp
[MAX_ENVP
];
190 static char preload_var
[PATH_MAX
];
191 static char seccomp_var
[PATH_MAX
];
192 static char debug_var
[] = "LD_DEBUG=all";
193 const char *preload_lib
= find_lib("libpreload-seccomp.so");
196 if (seccomp
&& !preload_lib
) {
197 ERROR("failed to add preload-lib to env\n");
201 snprintf(seccomp_var
, sizeof(seccomp_var
), "SECCOMP_FILE=%s", seccomp
);
202 envp
[count
++] = seccomp_var
;
203 snprintf(preload_var
, sizeof(preload_var
), "LD_PRELOAD=%s", preload_lib
);
204 envp
[count
++] = preload_var
;
207 envp
[count
++] = debug_var
;
212 static void usage(void)
214 fprintf(stderr
, "ujail <options> -- <binary> <params ...>\n");
215 fprintf(stderr
, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
216 fprintf(stderr
, " -S <file>\tseccomp filter config\n");
217 fprintf(stderr
, " -C <file>\tcapabilities drop config\n");
218 fprintf(stderr
, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
219 fprintf(stderr
, " -n <name>\tthe name of the jail\n");
220 fprintf(stderr
, "namespace jail options:\n");
221 fprintf(stderr
, " -h <hostname>\tchange the hostname of the jail\n");
222 fprintf(stderr
, " -r <file>\treadonly files that should be staged\n");
223 fprintf(stderr
, " -w <file>\twriteable files that should be staged\n");
224 fprintf(stderr
, " -p\t\tjail has /proc\n");
225 fprintf(stderr
, " -s\t\tjail has /sys\n");
226 fprintf(stderr
, " -l\t\tjail has /dev/log\n");
227 fprintf(stderr
, " -u\t\tjail has a ubus socket\n");
228 fprintf(stderr
, " -o\t\tremont jail root (/) read only\n");
229 fprintf(stderr
, "\nWarning: by default root inside the jail is the same\n\
230 and he has the same powers as root outside the jail,\n\
231 thus he can escape the jail and/or break stuff.\n\
232 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
233 If you use none of the namespace jail options,\n\
234 ujail will not use namespace/build a jail,\n\
235 and will only drop capabilities/apply seccomp filter.\n\n");
238 static int exec_jail(void *_notused
)
240 if (opts
.capabilities
&& drop_capabilities(opts
.capabilities
))
243 if (opts
.no_new_privs
&& prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
244 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
248 if (opts
.namespace && opts
.hostname
&& strlen(opts
.hostname
) > 0
249 && sethostname(opts
.hostname
, strlen(opts
.hostname
))) {
250 ERROR("sethostname(%s) failed: %m\n", opts
.hostname
);
254 if (opts
.namespace && build_jail_fs()) {
255 ERROR("failed to build jail fs\n");
259 char **envp
= build_envp(opts
.seccomp
);
263 INFO("exec-ing %s\n", *opts
.jail_argv
);
264 execve(*opts
.jail_argv
, opts
.jail_argv
, envp
);
265 /* we get there only if execve fails */
266 ERROR("failed to execve %s: %m\n", *opts
.jail_argv
);
270 static int jail_running
= 1;
271 static int jail_return_code
= 0;
273 static void jail_process_timeout_cb(struct uloop_timeout
*t
);
274 static struct uloop_timeout jail_process_timeout
= {
275 .cb
= jail_process_timeout_cb
,
278 static void jail_process_handler(struct uloop_process
*c
, int ret
)
280 uloop_timeout_cancel(&jail_process_timeout
);
281 if (WIFEXITED(ret
)) {
282 jail_return_code
= WEXITSTATUS(ret
);
283 INFO("jail (%d) exited with exit: %d\n", c
->pid
, jail_return_code
);
285 jail_return_code
= WTERMSIG(ret
);
286 INFO("jail (%d) exited with signal: %d\n", c
->pid
, jail_return_code
);
292 static struct uloop_process jail_process
= {
293 .cb
= jail_process_handler
,
296 static void jail_process_timeout_cb(struct uloop_timeout
*t
)
298 DEBUG("jail process failed to stop, sending SIGKILL\n");
299 kill(jail_process
.pid
, SIGKILL
);
302 static void jail_handle_signal(int signo
)
304 DEBUG("forwarding signal %d to the jailed process\n", signo
);
305 kill(jail_process
.pid
, signo
);
308 int main(int argc
, char **argv
)
311 uid_t uid
= getuid();
312 char log
[] = "/dev/log";
313 char ubus
[] = "/var/run/ubus.sock";
317 ERROR("not root, aborting: %m\n");
323 init_library_search();
325 while ((ch
= getopt(argc
, argv
, OPT_ARGS
)) != -1) {
328 debug
= atoi(optarg
);
343 opts
.seccomp
= optarg
;
344 add_mount(optarg
, 1, -1);
347 opts
.capabilities
= optarg
;
350 opts
.no_new_privs
= 1;
356 opts
.hostname
= optarg
;
360 add_path_and_deps(optarg
, 1, 0, 0);
364 add_path_and_deps(optarg
, 0, 0, 0);
368 add_mount(ubus
, 0, -1);
372 add_mount(log
, 0, -1);
377 /* no <binary> param found */
378 if (argc
- optind
< 1) {
382 if (!(opts
.namespace||opts
.capabilities
||opts
.seccomp
)) {
383 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
387 DEBUG("Using namespaces(%d), capabilities(%d), seccomp(%d)\n",
389 opts
.capabilities
!= 0,
392 opts
.jail_argv
= &argv
[optind
];
394 if (opts
.namespace && add_path_and_deps(*opts
.jail_argv
, 1, -1, 0)) {
395 ERROR("failed to load dependencies\n");
399 if (opts
.namespace && opts
.seccomp
&& add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
400 ERROR("failed to load libpreload-seccomp.so\n");
405 prctl(PR_SET_NAME
, opts
.name
, NULL
, NULL
, NULL
);
409 sigfillset(&sigmask
);
410 for (i
= 0; i
< _NSIG
; i
++) {
411 struct sigaction s
= { 0 };
413 if (!sigismember(&sigmask
, i
))
415 if ((i
== SIGCHLD
) || (i
== SIGPIPE
))
418 s
.sa_handler
= jail_handle_signal
;
419 sigaction(i
, &s
, NULL
);
422 if (opts
.namespace) {
423 add_mount("/dev/full", 0, -1);
424 add_mount("/dev/null", 0, -1);
425 add_mount("/dev/urandom", 0, -1);
426 add_mount("/dev/zero", 0, -1);
428 int flags
= CLONE_NEWPID
| CLONE_NEWNS
| CLONE_NEWIPC
| SIGCHLD
;
430 flags
|= CLONE_NEWUTS
;
431 jail_process
.pid
= clone(exec_jail
, child_stack
+ STACK_SIZE
, flags
, NULL
);
433 jail_process
.pid
= fork();
436 if (jail_process
.pid
> 0) {
438 uloop_process_add(&jail_process
);
441 DEBUG("uloop interrupted, killing jail process\n");
442 kill(jail_process
.pid
, SIGTERM
);
443 uloop_timeout_set(&jail_process_timeout
, 1000);
447 return jail_return_code
;
448 } else if (jail_process
.pid
== 0) {
449 /* fork child process */
450 return exec_jail(NULL
);
452 ERROR("failed to clone/fork: %m\n");