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:psuldo"
49 struct list_head list;
56 static LIST_HEAD(extras);
58 extern int pivot_root(const char *new_root, const char *put_old);
62 static char child_stack[STACK_SIZE];
64 static int mkdir_p(char *dir, mode_t mask)
66 char *l = strrchr(dir, '/');
74 if (mkdir_p(dir, mask))
79 ret = mkdir(dir, mask);
80 if (ret && errno == EEXIST)
84 ERROR("mkdir failed on %s: %s\n", dir, strerror(errno));
89 static int mount_bind(const char *root, const char *path, const char *name, int readonly, int error)
97 if (strstr(p, "local"))
100 snprintf(old, sizeof(old), "%s/%s", path, name);
101 snprintf(new, sizeof(new), "%s%s", root, p);
105 snprintf(new, sizeof(new), "%s%s/%s", root, p, name);
108 ERROR("%s does not exist\n", old);
112 if (S_ISDIR(s.st_mode)) {
115 fd = creat(new, 0644);
117 ERROR("failed to create %s: %s\n", new, strerror(errno));
123 if (mount(old, new, NULL, MS_BIND, NULL)) {
124 ERROR("failed to mount -B %s %s: %s\n", old, new, strerror(errno));
128 if (readonly && mount(old, new, NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL)) {
129 ERROR("failed to remount ro %s: %s\n", new, strerror(errno));
133 DEBUG("mount -B %s %s\n", old, new);
138 static int build_jail(const char *path)
146 if (mount("tmpfs", path, "tmpfs", MS_NOATIME, "mode=0755")) {
147 ERROR("tmpfs mount failed %s\n", strerror(errno));
151 avl_for_each_element(&libraries, l, avl)
152 if (mount_bind(path, l->path, l->name, 1, -1))
155 list_for_each_entry(m, &extras, list)
156 if (mount_bind(path, m->path, m->name, m->readonly, 0))
162 static void _umount(const char *root, const char *path)
166 if (asprintf(&buf, "%s%s", root, path) < 0) {
167 ERROR("failed to alloc umount buffer: %s\n", strerror(errno));
169 DEBUG("umount %s\n", buf);
175 static int stop_jail(const char *root)
180 avl_for_each_element(&libraries, l, avl) {
184 if (strstr(p, "local"))
187 snprintf(path, sizeof(path), "%s%s/%s", root, p, l->name);
188 DEBUG("umount %s\n", path);
192 list_for_each_entry(m, &extras, list) {
195 snprintf(path, sizeof(path), "%s%s/%s", root, m->path, m->name);
196 DEBUG("umount %s\n", path);
200 _umount(root, "/proc");
201 _umount(root, "/sys");
203 DEBUG("umount %s\n", root);
211 static char** build_envp(const char *seccomp, int debug)
213 static char *envp[MAX_ENVP];
214 static char preload_var[64];
215 static char seccomp_var[64];
216 static char debug_var[] = "LD_DEBUG=all";
217 char *preload_lib = find_lib("libpreload-seccomp.so");
220 if (seccomp && !preload_lib) {
221 ERROR("failed to add preload-lib to env\n");
225 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
226 envp[count++] = seccomp_var;
227 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
228 envp[count++] = preload_var;
231 envp[count++] = debug_var;
236 static int spawn(const char *path, char **argv, const char *seccomp)
241 ERROR("failed to spawn %s: %s\n", *argv, strerror(errno));
244 char **envp = build_envp(seccomp, 0);
246 INFO("spawning %s\n", *argv);
247 execve(*argv, argv, envp);
248 ERROR("failed to spawn child %s: %s\n", *argv, strerror(errno));
255 static int usage(void)
257 fprintf(stderr, "jail <options> -D <binary> <params ...>\n");
258 fprintf(stderr, " -P <path>\tpath where the jail will be staged\n");
259 fprintf(stderr, " -S <file>\tseccomp filter\n");
260 fprintf(stderr, " -n <name>\tthe name of the jail\n");
261 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
262 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
263 fprintf(stderr, " -p\t\tjail has /proc\t\n");
264 fprintf(stderr, " -s\t\tjail has /sys\t\n");
265 fprintf(stderr, " -l\t\tjail has /dev/log\t\n");
266 fprintf(stderr, " -u\t\tjail has a ubus socket\t\n");
271 static int child_running = 1;
273 static void child_process_handler(struct uloop_process *c, int ret)
275 INFO("child (%d) exited: %d\n", c->pid, ret);
280 struct uloop_process child_process = {
281 .cb = child_process_handler,
284 static int spawn_child(void *arg)
286 char *path = get_current_dir_name();
287 int procfs = 0, sysfs = 0;
288 char *seccomp = NULL;
298 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
316 if (sethostname(optarg, strlen(optarg)))
317 ERROR("failed to sethostname: %s\n", strerror(errno));
322 if (asprintf(&mpoint, "%s/old", path) < 0) {
323 ERROR("failed to alloc pivot path: %s\n", strerror(errno));
326 mkdir_p(mpoint, 0755);
327 if (pivot_root(path, mpoint) == -1) {
328 ERROR("pivot_root failed:%s\n", strerror(errno));
332 umount2("/old", MNT_DETACH);
335 mkdir("/proc", 0755);
336 mount("proc", "/proc", "proc", MS_NOATIME, 0);
340 mount("sysfs", "/sys", "sysfs", MS_NOATIME, 0);
343 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
347 child_process.pid = spawn(path, &argv[optind], seccomp);
348 uloop_process_add(&child_process);
352 kill(child_process.pid, SIGTERM);
353 waitpid(child_process.pid, NULL, 0);
359 static int namespace_running = 1;
361 static void namespace_process_handler(struct uloop_process *c, int ret)
363 INFO("namespace (%d) exited: %d\n", c->pid, ret);
365 namespace_running = 0;
368 struct uloop_process namespace_process = {
369 .cb = namespace_process_handler,
372 static void spawn_namespace(const char *path, int argc, char **argv)
374 char *dir = get_current_dir_name();
378 ERROR("failed to chdir() into the jail\n");
381 namespace_process.pid = clone(spawn_child,
382 child_stack + STACK_SIZE,
383 CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | SIGCHLD, argv);
385 if (namespace_process.pid != -1) {
387 ERROR("failed to chdir() out of the jail\n");
389 uloop_process_add(&namespace_process);
392 if (namespace_running) {
393 kill(namespace_process.pid, SIGTERM);
394 waitpid(namespace_process.pid, NULL, 0);
397 ERROR("failed to spawn namespace: %s\n", strerror(errno));
401 static void add_extra(char *name, int readonly)
406 ERROR("%s is not an absolute path\n", name);
410 f = calloc(1, sizeof(struct extra));
412 f->name = basename(name);
413 f->path = dirname(strdup(name));
414 f->readonly = readonly;
416 list_add_tail(&f->list, &extras);
419 int main(int argc, char **argv)
421 uid_t uid = getuid();
422 const char *name = NULL;
426 char log[] = "/dev/log";
427 char ubus[] = "/var/run/ubus.sock";
430 ERROR("not root, aborting: %s\n", strerror(errno));
436 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
449 add_extra(optarg, 1);
452 add_extra(optarg, 0);
463 if (argc - optind < 1)
466 if (!path && asprintf(&path, "/tmp/%s", basename(argv[optind])) == -1) {
467 ERROR("failed to set root path\n: %s", strerror(errno));
471 if (!stat(path, &s)) {
472 ERROR("%s already exists: %s\n", path, strerror(errno));
477 prctl(PR_SET_NAME, name, NULL, NULL, NULL);
479 avl_init(&libraries, avl_strcmp, false, NULL);
480 alloc_library_path("/lib64");
481 alloc_library_path("/lib");
482 alloc_library_path("/usr/lib");
483 load_ldso_conf("/etc/ld.so.conf");
485 if (elf_load_deps(argv[optind])) {
486 ERROR("failed to load dependencies\n");
490 if (elf_load_deps("libpreload-seccomp.so")) {
491 ERROR("failed to load libpreload-seccomp.so\n");
495 ret = build_jail(path);
498 spawn_namespace(path, argc, argv);
500 ERROR("failed to build jail\n");