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>
28 #include <linux/limits.h>
31 #include "capabilities.h"
34 #include <libubox/list.h>
35 #include <libubox/uloop.h>
37 #define STACK_SIZE (1024 * 1024)
38 #define OPT_ARGS "P:S:C:n:r:w:d:psulo"
53 struct list_head list;
60 static LIST_HEAD(extras);
62 extern int pivot_root(const char *new_root, const char *put_old);
66 static char child_stack[STACK_SIZE];
68 static int mkdir_p(char *dir, mode_t mask)
70 char *l = strrchr(dir, '/');
78 if (mkdir_p(dir, mask))
83 ret = mkdir(dir, mask);
84 if (ret && errno == EEXIST)
88 ERROR("mkdir failed on %s: %s\n", dir, strerror(errno));
93 static int mount_bind(const char *root, const char *path, const char *name, int readonly, int error)
101 if (strstr(p, "local"))
104 snprintf(old, sizeof(old), "%s/%s", path, name);
105 snprintf(new, sizeof(new), "%s%s", root, p);
109 snprintf(new, sizeof(new), "%s%s/%s", root, p, name);
112 ERROR("%s does not exist\n", old);
116 if (S_ISDIR(s.st_mode)) {
119 fd = creat(new, 0644);
121 ERROR("failed to create %s: %s\n", new, strerror(errno));
127 if (mount(old, new, NULL, MS_BIND, NULL)) {
128 ERROR("failed to mount -B %s %s: %s\n", old, new, strerror(errno));
132 if (readonly && mount(NULL, new, NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL)) {
133 ERROR("failed to remount ro %s: %s\n", new, strerror(errno));
137 DEBUG("mount -B %s %s\n", old, new);
142 static int build_jail_fs(void)
147 if (mount("tmpfs", opts.path, "tmpfs", MS_NOATIME, "mode=0755")) {
148 ERROR("tmpfs mount failed %s\n", strerror(errno));
152 if (chdir(opts.path)) {
153 ERROR("failed to chdir() in the jail root\n");
157 init_library_search();
159 if (elf_load_deps(*opts.jail_argv)) {
160 ERROR("failed to load dependencies\n");
164 if (opts.seccomp && elf_load_deps("libpreload-seccomp.so")) {
165 ERROR("failed to load libpreload-seccomp.so\n");
169 avl_for_each_element(&libraries, l, avl)
170 if (mount_bind(opts.path, l->path, l->name, 1, -1))
173 list_for_each_entry(m, &extras, list)
174 if (mount_bind(opts.path, m->path, m->name, m->readonly, 0))
178 if (asprintf(&mpoint, "%s/old", opts.path) < 0) {
179 ERROR("failed to alloc pivot path: %s\n", strerror(errno));
182 mkdir_p(mpoint, 0755);
183 if (pivot_root(opts.path, mpoint) == -1) {
184 ERROR("pivot_root failed:%s\n", strerror(errno));
189 umount2("/old", MNT_DETACH);
192 mkdir("/proc", 0755);
193 mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
197 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
200 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
206 static char** build_envp(const char *seccomp)
208 static char *envp[MAX_ENVP];
209 static char preload_var[PATH_MAX];
210 static char seccomp_var[PATH_MAX];
211 static char debug_var[] = "LD_DEBUG=all";
212 const char *preload_lib = find_lib("libpreload-seccomp.so");
215 if (seccomp && !preload_lib) {
216 ERROR("failed to add preload-lib to env\n");
220 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
221 envp[count++] = seccomp_var;
222 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
223 envp[count++] = preload_var;
226 envp[count++] = debug_var;
231 static void usage(void)
233 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
234 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
235 fprintf(stderr, " -S <file>\tseccomp filter config\n");
236 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
237 fprintf(stderr, " -n <name>\tthe name of the jail\n");
238 fprintf(stderr, "namespace jail options:\n");
239 fprintf(stderr, " -P <path>\tpath where the jail will be staged\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, " -p\t\tjail has /proc\n");
243 fprintf(stderr, " -s\t\tjail has /sys\n");
244 fprintf(stderr, " -l\t\tjail has /dev/log\n");
245 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
246 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
247 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
248 and he has the same powers as root outside the jail,\n\
249 thus he can escape the jail and/or break stuff.\n\
250 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
251 If you use none of the namespace jail options,\n\
252 ujail will not use namespace/build a jail,\n\
253 and will only drop capabilities/apply seccomp filter.\n\n");
256 static int exec_jail(void)
258 char **envp = build_envp(opts.seccomp);
262 if (opts.capabilities && drop_capabilities(opts.capabilities))
265 INFO("exec-ing %s\n", *opts.jail_argv);
266 execve(*opts.jail_argv, opts.jail_argv, envp);
267 //we get there only if execve fails
268 ERROR("failed to execve %s: %s\n", *opts.jail_argv, strerror(errno));
272 static int spawn_jail(void *_notused)
274 if (opts.name && sethostname(opts.name, strlen(opts.name))) {
275 ERROR("failed to sethostname: %s\n", strerror(errno));
278 if (build_jail_fs()) {
279 ERROR("failed to build jail fs");
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);
357 opts.seccomp = optarg;
358 add_extra(optarg, 1);
361 opts.capabilities = optarg;
362 add_extra(optarg, 1);
373 add_extra(optarg, 1);
377 add_extra(optarg, 0);
390 //no <binary> param found
391 if (argc - optind < 1) {
395 if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
396 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
400 DEBUG("Using namespaces(%d), capabilities(%d), seccomp(%d)\n",
402 opts.capabilities != 0,
405 opts.jail_argv = &argv[optind];
408 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
410 if (opts.namespace && !opts.path && asprintf(&opts.path, "/tmp/%s", basename(*opts.jail_argv)) == -1) {
411 ERROR("failed to asprintf root path: %s\n", strerror(errno));
415 if (opts.namespace && mkdir(opts.path, 0755)) {
416 ERROR("unable to create root path: %s (%s)\n", opts.path, strerror(errno));
421 if (opts.namespace) {
422 jail_process.pid = clone(spawn_jail,
423 child_stack + STACK_SIZE,
424 CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | SIGCHLD, NULL);
426 jail_process.pid = fork();
429 if (jail_process.pid > 0) {
431 uloop_process_add(&jail_process);
435 DEBUG("uloop interrupted, killing jail process\n");
436 kill(jail_process.pid, SIGTERM);
437 waitpid(jail_process.pid, NULL, 0);
439 } else if (jail_process.pid == 0) {
443 ERROR("failed to clone/fork: %s\n", strerror(errno));
447 if (opts.namespace && rmdir(opts.path)) {
448 ERROR("Unable to remove root path: %s (%s)\n", opts.path, strerror(errno));
455 return jail_return_code;