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/syscall.h>
17 #include <sys/utsname.h>
18 #include <sys/types.h>
19 #include <sys/syscall.h>
20 #include <sys/types.h>
21 #include <sys/mount.h>
22 #include <sys/prctl.h>
41 #include <libubox/utils.h>
42 #include <libubox/list.h>
43 #include <libubox/uloop.h>
45 #define STACK_SIZE (1024 * 1024)
46 #define OPT_ARGS "P:S:n:r:w:d:psulo"
59 struct list_head list;
66 static LIST_HEAD(extras);
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 failed on %s: %s\n", dir, strerror(errno));
99 static int mount_bind(const char *root, const char *path, const char *name, int readonly, int error)
101 const char *p = path;
107 if (strstr(p, "local"))
110 snprintf(old, sizeof(old), "%s/%s", path, name);
111 snprintf(new, sizeof(new), "%s%s", root, p);
115 snprintf(new, sizeof(new), "%s%s/%s", root, p, name);
118 ERROR("%s does not exist\n", old);
122 if (S_ISDIR(s.st_mode)) {
125 fd = creat(new, 0644);
127 ERROR("failed to create %s: %s\n", new, strerror(errno));
133 if (mount(old, new, NULL, MS_BIND, NULL)) {
134 ERROR("failed to mount -B %s %s: %s\n", old, new, strerror(errno));
138 if (readonly && mount(NULL, new, NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL)) {
139 ERROR("failed to remount ro %s: %s\n", new, strerror(errno));
143 DEBUG("mount -B %s %s\n", old, new);
148 static int build_jail_fs()
153 if (mount("tmpfs", opts.path, "tmpfs", MS_NOATIME, "mode=0755")) {
154 ERROR("tmpfs mount failed %s\n", strerror(errno));
158 if (chdir(opts.path)) {
159 ERROR("failed to chdir() in the jail root\n");
163 avl_init(&libraries, avl_strcmp, false, NULL);
164 alloc_library_path("/lib64");
165 alloc_library_path("/lib");
166 alloc_library_path("/usr/lib");
167 load_ldso_conf("/etc/ld.so.conf");
169 if (elf_load_deps(*opts.jail_argv)) {
170 ERROR("failed to load dependencies\n");
174 if (opts.seccomp && elf_load_deps("libpreload-seccomp.so")) {
175 ERROR("failed to load libpreload-seccomp.so\n");
179 avl_for_each_element(&libraries, l, avl)
180 if (mount_bind(opts.path, l->path, l->name, 1, -1))
183 list_for_each_entry(m, &extras, list)
184 if (mount_bind(opts.path, m->path, m->name, m->readonly, 0))
188 if (asprintf(&mpoint, "%s/old", opts.path) < 0) {
189 ERROR("failed to alloc pivot path: %s\n", strerror(errno));
192 mkdir_p(mpoint, 0755);
193 if (pivot_root(opts.path, mpoint) == -1) {
194 ERROR("pivot_root failed:%s\n", strerror(errno));
199 umount2("/old", MNT_DETACH);
202 mkdir("/proc", 0755);
203 mount("proc", "/proc", "proc", MS_NOATIME, 0);
207 mount("sysfs", "/sys", "sysfs", MS_NOATIME, 0);
210 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
216 static char** build_envp(const char *seccomp)
218 static char *envp[MAX_ENVP];
219 static char preload_var[64];
220 static char seccomp_var[64];
221 static char debug_var[] = "LD_DEBUG=all";
222 char *preload_lib = find_lib("libpreload-seccomp.so");
225 if (seccomp && !preload_lib) {
226 ERROR("failed to add preload-lib to env\n");
230 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
231 envp[count++] = seccomp_var;
232 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
233 envp[count++] = preload_var;
236 envp[count++] = debug_var;
241 static void usage(void)
243 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
244 fprintf(stderr, " -P <path>\tpath where the jail will be staged\n");
245 fprintf(stderr, " -S <file>\tseccomp filter\n");
246 fprintf(stderr, " -n <name>\tthe name of the jail\n");
247 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
248 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
249 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
250 fprintf(stderr, " -p\t\tjail has /proc\n");
251 fprintf(stderr, " -s\t\tjail has /sys\n");
252 fprintf(stderr, " -l\t\tjail has /dev/log\n");
253 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
254 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
255 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
256 and he has the same powers as root outside the jail,\n\
257 thus he can escape the jail and/or break stuff.\n\
258 Please use an appropriate seccomp filter (-S) to restrict his powers\n");
261 static int spawn_jail(void *arg)
263 if (opts.name && sethostname(opts.name, strlen(opts.name))) {
264 ERROR("failed to sethostname: %s\n", strerror(errno));
267 if (build_jail_fs()) {
268 ERROR("failed to build jail fs");
272 char **envp = build_envp(opts.seccomp);
276 //TODO: drop capabilities() here
277 //prctl(PR_CAPBSET_DROP, ..., 0, 0, 0);
279 INFO("exec-ing %s\n", *opts.jail_argv);
280 execve(*opts.jail_argv, opts.jail_argv, envp);
281 //we get there only if execve fails
282 ERROR("failed to execve %s: %s\n", *opts.jail_argv, strerror(errno));
286 static int jail_running = 1;
287 static int jail_return_code = 0;
289 static void jail_process_handler(struct uloop_process *c, int ret)
291 if (WIFEXITED(ret)) {
292 jail_return_code = WEXITSTATUS(ret);
293 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
295 jail_return_code = WTERMSIG(ret);
296 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
302 static struct uloop_process jail_process = {
303 .cb = jail_process_handler,
306 static void add_extra(char *name, int readonly)
311 ERROR("%s is not an absolute path\n", name);
315 f = calloc(1, sizeof(struct extra));
317 f->name = basename(name);
318 f->path = dirname(strdup(name));
319 f->readonly = readonly;
321 list_add_tail(&f->list, &extras);
324 int main(int argc, char **argv)
326 uid_t uid = getuid();
327 char log[] = "/dev/log";
328 char ubus[] = "/var/run/ubus.sock";
329 int ret = EXIT_SUCCESS;
333 ERROR("not root, aborting: %s\n", strerror(errno));
339 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
342 debug = atoi(optarg);
354 opts.seccomp = optarg;
355 add_extra(optarg, 1);
364 add_extra(optarg, 1);
367 add_extra(optarg, 0);
378 //no <binary> param found
379 if (argc - optind < 1) {
384 opts.jail_argv = &argv[optind];
387 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
389 if (!opts.path && asprintf(&opts.path, "/tmp/%s", basename(*opts.jail_argv)) == -1) {
390 ERROR("failed to asprintf root path: %s\n", strerror(errno));
394 if (mkdir(opts.path, 0755)) {
395 ERROR("unable to create root path: %s (%s)\n", opts.path, strerror(errno));
400 jail_process.pid = clone(spawn_jail,
401 child_stack + STACK_SIZE,
402 CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | SIGCHLD, argv);
404 if (jail_process.pid != -1) {
405 uloop_process_add(&jail_process);
409 kill(jail_process.pid, SIGTERM);
410 waitpid(jail_process.pid, NULL, 0);
413 ERROR("failed to spawn namespace: %s\n", strerror(errno));
417 if (rmdir(opts.path)) {
418 ERROR("Unable to remove root path: %s (%s)\n", opts.path, strerror(errno));
425 return jail_return_code;