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>
31 #include "capabilities.h"
33 #include <libubox/list.h>
34 #include <libubox/uloop.h>
36 #define STACK_SIZE (1024 * 1024)
37 #define OPT_ARGS "P:S:C:n:r:w:d:psulo"
51 struct list_head list
;
58 static LIST_HEAD(extras
);
60 extern int pivot_root(const char *new_root
, const char *put_old
);
64 static char child_stack
[STACK_SIZE
];
66 static int mkdir_p(char *dir
, mode_t mask
)
68 char *l
= strrchr(dir
, '/');
76 if (mkdir_p(dir
, mask
))
81 ret
= mkdir(dir
, mask
);
82 if (ret
&& errno
== EEXIST
)
86 ERROR("mkdir failed on %s: %s\n", dir
, strerror(errno
));
91 static int mount_bind(const char *root
, const char *path
, const char *name
, int readonly
, int error
)
99 if (strstr(p
, "local"))
102 snprintf(old
, sizeof(old
), "%s/%s", path
, name
);
103 snprintf(new, sizeof(new), "%s%s", root
, p
);
107 snprintf(new, sizeof(new), "%s%s/%s", root
, p
, name
);
110 ERROR("%s does not exist\n", old
);
114 if (S_ISDIR(s
.st_mode
)) {
117 fd
= creat(new, 0644);
119 ERROR("failed to create %s: %s\n", new, strerror(errno
));
125 if (mount(old
, new, NULL
, MS_BIND
, NULL
)) {
126 ERROR("failed to mount -B %s %s: %s\n", old
, new, strerror(errno
));
130 if (readonly
&& mount(NULL
, new, NULL
, MS_BIND
| MS_REMOUNT
| MS_RDONLY
, NULL
)) {
131 ERROR("failed to remount ro %s: %s\n", new, strerror(errno
));
135 DEBUG("mount -B %s %s\n", old
, new);
140 static int build_jail_fs()
145 if (mount("tmpfs", opts
.path
, "tmpfs", MS_NOATIME
, "mode=0755")) {
146 ERROR("tmpfs mount failed %s\n", strerror(errno
));
150 if (chdir(opts
.path
)) {
151 ERROR("failed to chdir() in the jail root\n");
155 avl_init(&libraries
, avl_strcmp
, false, NULL
);
156 alloc_library_path("/lib64");
157 alloc_library_path("/lib");
158 alloc_library_path("/usr/lib");
159 load_ldso_conf("/etc/ld.so.conf");
161 if (elf_load_deps(*opts
.jail_argv
)) {
162 ERROR("failed to load dependencies\n");
166 if (opts
.seccomp
&& elf_load_deps("libpreload-seccomp.so")) {
167 ERROR("failed to load libpreload-seccomp.so\n");
171 avl_for_each_element(&libraries
, l
, avl
)
172 if (mount_bind(opts
.path
, l
->path
, l
->name
, 1, -1))
175 list_for_each_entry(m
, &extras
, list
)
176 if (mount_bind(opts
.path
, m
->path
, m
->name
, m
->readonly
, 0))
180 if (asprintf(&mpoint
, "%s/old", opts
.path
) < 0) {
181 ERROR("failed to alloc pivot path: %s\n", strerror(errno
));
184 mkdir_p(mpoint
, 0755);
185 if (pivot_root(opts
.path
, mpoint
) == -1) {
186 ERROR("pivot_root failed:%s\n", strerror(errno
));
191 umount2("/old", MNT_DETACH
);
194 mkdir("/proc", 0755);
195 mount("proc", "/proc", "proc", MS_NOATIME
, 0);
199 mount("sysfs", "/sys", "sysfs", MS_NOATIME
, 0);
202 mount(NULL
, "/", NULL
, MS_RDONLY
| MS_REMOUNT
, 0);
208 static char** build_envp(const char *seccomp
)
210 static char *envp
[MAX_ENVP
];
211 static char preload_var
[64];
212 static char seccomp_var
[64];
213 static char debug_var
[] = "LD_DEBUG=all";
214 char *preload_lib
= find_lib("libpreload-seccomp.so");
217 if (seccomp
&& !preload_lib
) {
218 ERROR("failed to add preload-lib to env\n");
222 snprintf(seccomp_var
, sizeof(seccomp_var
), "SECCOMP_FILE=%s", seccomp
);
223 envp
[count
++] = seccomp_var
;
224 snprintf(preload_var
, sizeof(preload_var
), "LD_PRELOAD=%s", preload_lib
);
225 envp
[count
++] = preload_var
;
228 envp
[count
++] = debug_var
;
233 static void usage(void)
235 fprintf(stderr
, "ujail <options> -- <binary> <params ...>\n");
236 fprintf(stderr
, " -P <path>\tpath where the jail will be staged\n");
237 fprintf(stderr
, " -S <file>\tseccomp filter\n");
238 fprintf(stderr
, " -C <file>\tcapabilities drop config\n");
239 fprintf(stderr
, " -n <name>\tthe name of the jail\n");
240 fprintf(stderr
, " -r <file>\treadonly files that should be staged\n");
241 fprintf(stderr
, " -w <file>\twriteable files that should be staged\n");
242 fprintf(stderr
, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
243 fprintf(stderr
, " -p\t\tjail has /proc\n");
244 fprintf(stderr
, " -s\t\tjail has /sys\n");
245 fprintf(stderr
, " -l\t\tjail has /dev/log\n");
246 fprintf(stderr
, " -u\t\tjail has a ubus socket\n");
247 fprintf(stderr
, " -o\t\tremont jail root (/) read only\n");
248 fprintf(stderr
, "\nWarning: by default root inside the jail is the same\n\
249 and he has the same powers as root outside the jail,\n\
250 thus he can escape the jail and/or break stuff.\n\
251 Please use an appropriate seccomp/capabilities filter (-S/-C) to restrict his powers\n");
254 static int spawn_jail(void *arg
)
256 if (opts
.name
&& sethostname(opts
.name
, strlen(opts
.name
))) {
257 ERROR("failed to sethostname: %s\n", strerror(errno
));
260 if (build_jail_fs()) {
261 ERROR("failed to build jail fs");
265 char **envp
= build_envp(opts
.seccomp
);
269 if (opts
.capabilities
&& drop_capabilities(opts
.capabilities
))
272 INFO("exec-ing %s\n", *opts
.jail_argv
);
273 execve(*opts
.jail_argv
, opts
.jail_argv
, envp
);
274 //we get there only if execve fails
275 ERROR("failed to execve %s: %s\n", *opts
.jail_argv
, strerror(errno
));
279 static int jail_running
= 1;
280 static int jail_return_code
= 0;
282 static void jail_process_handler(struct uloop_process
*c
, int ret
)
284 if (WIFEXITED(ret
)) {
285 jail_return_code
= WEXITSTATUS(ret
);
286 INFO("jail (%d) exited with exit: %d\n", c
->pid
, jail_return_code
);
288 jail_return_code
= WTERMSIG(ret
);
289 INFO("jail (%d) exited with signal: %d\n", c
->pid
, jail_return_code
);
295 static struct uloop_process jail_process
= {
296 .cb
= jail_process_handler
,
299 static void add_extra(char *name
, int readonly
)
304 ERROR("%s is not an absolute path\n", name
);
308 f
= calloc(1, sizeof(struct extra
));
310 f
->name
= basename(name
);
311 f
->path
= dirname(strdup(name
));
312 f
->readonly
= readonly
;
314 list_add_tail(&f
->list
, &extras
);
317 int main(int argc
, char **argv
)
319 uid_t uid
= getuid();
320 char log
[] = "/dev/log";
321 char ubus
[] = "/var/run/ubus.sock";
322 int ret
= EXIT_SUCCESS
;
326 ERROR("not root, aborting: %s\n", strerror(errno
));
332 while ((ch
= getopt(argc
, argv
, OPT_ARGS
)) != -1) {
335 debug
= atoi(optarg
);
347 opts
.seccomp
= optarg
;
348 add_extra(optarg
, 1);
351 opts
.capabilities
= optarg
;
352 add_extra(optarg
, 1);
361 add_extra(optarg
, 1);
364 add_extra(optarg
, 0);
375 //no <binary> param found
376 if (argc
- optind
< 1) {
381 opts
.jail_argv
= &argv
[optind
];
384 prctl(PR_SET_NAME
, opts
.name
, NULL
, NULL
, NULL
);
386 if (!opts
.path
&& asprintf(&opts
.path
, "/tmp/%s", basename(*opts
.jail_argv
)) == -1) {
387 ERROR("failed to asprintf root path: %s\n", strerror(errno
));
391 if (mkdir(opts
.path
, 0755)) {
392 ERROR("unable to create root path: %s (%s)\n", opts
.path
, strerror(errno
));
397 jail_process
.pid
= clone(spawn_jail
,
398 child_stack
+ STACK_SIZE
,
399 CLONE_NEWUTS
| CLONE_NEWPID
| CLONE_NEWNS
| CLONE_NEWIPC
| SIGCHLD
, argv
);
401 if (jail_process
.pid
!= -1) {
402 uloop_process_add(&jail_process
);
406 kill(jail_process
.pid
, SIGTERM
);
407 waitpid(jail_process
.pid
, NULL
, 0);
410 ERROR("failed to spawn namespace: %s\n", strerror(errno
));
414 if (rmdir(opts
.path
)) {
415 ERROR("Unable to remove root path: %s (%s)\n", opts
.path
, strerror(errno
));
422 return jail_return_code
;