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>
30 #include "capabilities.h"
32 #include <libubox/list.h>
33 #include <libubox/uloop.h>
35 #define STACK_SIZE (1024 * 1024)
36 #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 | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
199 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 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 const 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, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
237 fprintf(stderr, " -S <file>\tseccomp filter config\n");
238 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
239 fprintf(stderr, " -n <name>\tthe name of the jail\n");
240 fprintf(stderr, "namespace jail options:\n");
241 fprintf(stderr, " -P <path>\tpath where the jail will be staged\n");
242 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
243 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
244 fprintf(stderr, " -p\t\tjail has /proc\n");
245 fprintf(stderr, " -s\t\tjail has /sys\n");
246 fprintf(stderr, " -l\t\tjail has /dev/log\n");
247 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
248 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
249 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
250 and he has the same powers as root outside the jail,\n\
251 thus he can escape the jail and/or break stuff.\n\
252 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
253 If you use none of the namespace jail options,\n\
254 ujail will not use namespace/build a jail,\n\
255 and will only drop capabilities/apply seccomp filter.\n\n");
258 static int exec_jail()
260 char **envp = build_envp(opts.seccomp);
264 if (opts.capabilities && drop_capabilities(opts.capabilities))
267 INFO("exec-ing %s\n", *opts.jail_argv);
268 execve(*opts.jail_argv, opts.jail_argv, envp);
269 //we get there only if execve fails
270 ERROR("failed to execve %s: %s\n", *opts.jail_argv, strerror(errno));
274 static int spawn_jail(void *_notused)
276 if (opts.name && sethostname(opts.name, strlen(opts.name))) {
277 ERROR("failed to sethostname: %s\n", strerror(errno));
280 if (build_jail_fs()) {
281 ERROR("failed to build jail fs");
288 static int jail_running = 1;
289 static int jail_return_code = 0;
291 static void jail_process_handler(struct uloop_process *c, int ret)
293 if (WIFEXITED(ret)) {
294 jail_return_code = WEXITSTATUS(ret);
295 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
297 jail_return_code = WTERMSIG(ret);
298 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
304 static struct uloop_process jail_process = {
305 .cb = jail_process_handler,
308 static void add_extra(char *name, int readonly)
313 ERROR("%s is not an absolute path\n", name);
317 f = calloc(1, sizeof(struct extra));
319 f->name = basename(name);
320 f->path = dirname(strdup(name));
321 f->readonly = readonly;
323 list_add_tail(&f->list, &extras);
326 int main(int argc, char **argv)
328 uid_t uid = getuid();
329 char log[] = "/dev/log";
330 char ubus[] = "/var/run/ubus.sock";
331 int ret = EXIT_SUCCESS;
335 ERROR("not root, aborting: %s\n", strerror(errno));
341 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
344 debug = atoi(optarg);
359 opts.seccomp = optarg;
360 add_extra(optarg, 1);
363 opts.capabilities = optarg;
364 add_extra(optarg, 1);
375 add_extra(optarg, 1);
379 add_extra(optarg, 0);
392 //no <binary> param found
393 if (argc - optind < 1) {
397 if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
398 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
402 DEBUG("Using namespaces(%d), capabilities(%d), seccomp(%d)\n",
404 opts.capabilities != 0,
407 opts.jail_argv = &argv[optind];
410 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
412 if (opts.namespace && !opts.path && asprintf(&opts.path, "/tmp/%s", basename(*opts.jail_argv)) == -1) {
413 ERROR("failed to asprintf root path: %s\n", strerror(errno));
417 if (opts.namespace && mkdir(opts.path, 0755)) {
418 ERROR("unable to create root path: %s (%s)\n", opts.path, strerror(errno));
423 if (opts.namespace) {
424 jail_process.pid = clone(spawn_jail,
425 child_stack + STACK_SIZE,
426 CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | SIGCHLD, NULL);
428 jail_process.pid = fork();
431 if (jail_process.pid > 0) {
433 uloop_process_add(&jail_process);
437 DEBUG("uloop interrupted, killing jail process\n");
438 kill(jail_process.pid, SIGTERM);
439 waitpid(jail_process.pid, NULL, 0);
441 } else if (jail_process.pid == 0) {
445 ERROR("failed to clone/fork: %s\n", strerror(errno));
449 if (opts.namespace && rmdir(opts.path)) {
450 ERROR("Unable to remove root path: %s (%s)\n", opts.path, strerror(errno));
457 return jail_return_code;