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>
40 #include "capabilities.h"
42 #include <libubox/utils.h>
43 #include <libubox/list.h>
44 #include <libubox/uloop.h>
46 #define STACK_SIZE (1024 * 1024)
47 #define OPT_ARGS "P:S:C:n:r:w:d:psulo"
61 struct list_head list;
68 static LIST_HEAD(extras);
70 extern int pivot_root(const char *new_root, const char *put_old);
74 static char child_stack[STACK_SIZE];
76 static int mkdir_p(char *dir, mode_t mask)
78 char *l = strrchr(dir, '/');
86 if (mkdir_p(dir, mask))
91 ret = mkdir(dir, mask);
92 if (ret && errno == EEXIST)
96 ERROR("mkdir failed on %s: %s\n", dir, strerror(errno));
101 static int mount_bind(const char *root, const char *path, const char *name, int readonly, int error)
103 const char *p = path;
109 if (strstr(p, "local"))
112 snprintf(old, sizeof(old), "%s/%s", path, name);
113 snprintf(new, sizeof(new), "%s%s", root, p);
117 snprintf(new, sizeof(new), "%s%s/%s", root, p, name);
120 ERROR("%s does not exist\n", old);
124 if (S_ISDIR(s.st_mode)) {
127 fd = creat(new, 0644);
129 ERROR("failed to create %s: %s\n", new, strerror(errno));
135 if (mount(old, new, NULL, MS_BIND, NULL)) {
136 ERROR("failed to mount -B %s %s: %s\n", old, new, strerror(errno));
140 if (readonly && mount(NULL, new, NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL)) {
141 ERROR("failed to remount ro %s: %s\n", new, strerror(errno));
145 DEBUG("mount -B %s %s\n", old, new);
150 static int build_jail_fs()
155 if (mount("tmpfs", opts.path, "tmpfs", MS_NOATIME, "mode=0755")) {
156 ERROR("tmpfs mount failed %s\n", strerror(errno));
160 if (chdir(opts.path)) {
161 ERROR("failed to chdir() in the jail root\n");
165 avl_init(&libraries, avl_strcmp, false, NULL);
166 alloc_library_path("/lib64");
167 alloc_library_path("/lib");
168 alloc_library_path("/usr/lib");
169 load_ldso_conf("/etc/ld.so.conf");
171 if (elf_load_deps(*opts.jail_argv)) {
172 ERROR("failed to load dependencies\n");
176 if (opts.seccomp && elf_load_deps("libpreload-seccomp.so")) {
177 ERROR("failed to load libpreload-seccomp.so\n");
181 avl_for_each_element(&libraries, l, avl)
182 if (mount_bind(opts.path, l->path, l->name, 1, -1))
185 list_for_each_entry(m, &extras, list)
186 if (mount_bind(opts.path, m->path, m->name, m->readonly, 0))
190 if (asprintf(&mpoint, "%s/old", opts.path) < 0) {
191 ERROR("failed to alloc pivot path: %s\n", strerror(errno));
194 mkdir_p(mpoint, 0755);
195 if (pivot_root(opts.path, mpoint) == -1) {
196 ERROR("pivot_root failed:%s\n", strerror(errno));
201 umount2("/old", MNT_DETACH);
204 mkdir("/proc", 0755);
205 mount("proc", "/proc", "proc", MS_NOATIME, 0);
209 mount("sysfs", "/sys", "sysfs", MS_NOATIME, 0);
212 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
218 static char** build_envp(const char *seccomp)
220 static char *envp[MAX_ENVP];
221 static char preload_var[64];
222 static char seccomp_var[64];
223 static char debug_var[] = "LD_DEBUG=all";
224 char *preload_lib = find_lib("libpreload-seccomp.so");
227 if (seccomp && !preload_lib) {
228 ERROR("failed to add preload-lib to env\n");
232 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
233 envp[count++] = seccomp_var;
234 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
235 envp[count++] = preload_var;
238 envp[count++] = debug_var;
243 static void usage(void)
245 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
246 fprintf(stderr, " -P <path>\tpath where the jail will be staged\n");
247 fprintf(stderr, " -S <file>\tseccomp filter\n");
248 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
249 fprintf(stderr, " -n <name>\tthe name of the jail\n");
250 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
251 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
252 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
253 fprintf(stderr, " -p\t\tjail has /proc\n");
254 fprintf(stderr, " -s\t\tjail has /sys\n");
255 fprintf(stderr, " -l\t\tjail has /dev/log\n");
256 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
257 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
258 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
259 and he has the same powers as root outside the jail,\n\
260 thus he can escape the jail and/or break stuff.\n\
261 Please use an appropriate seccomp/capabilities filter (-S/-C) to restrict his powers\n");
264 static int spawn_jail(void *arg)
266 if (opts.name && sethostname(opts.name, strlen(opts.name))) {
267 ERROR("failed to sethostname: %s\n", strerror(errno));
270 if (build_jail_fs()) {
271 ERROR("failed to build jail fs");
275 char **envp = build_envp(opts.seccomp);
279 if (opts.capabilities && drop_capabilities(opts.capabilities))
282 INFO("exec-ing %s\n", *opts.jail_argv);
283 execve(*opts.jail_argv, opts.jail_argv, envp);
284 //we get there only if execve fails
285 ERROR("failed to execve %s: %s\n", *opts.jail_argv, strerror(errno));
289 static int jail_running = 1;
290 static int jail_return_code = 0;
292 static void jail_process_handler(struct uloop_process *c, int ret)
294 if (WIFEXITED(ret)) {
295 jail_return_code = WEXITSTATUS(ret);
296 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
298 jail_return_code = WTERMSIG(ret);
299 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
305 static struct uloop_process jail_process = {
306 .cb = jail_process_handler,
309 static void add_extra(char *name, int readonly)
314 ERROR("%s is not an absolute path\n", name);
318 f = calloc(1, sizeof(struct extra));
320 f->name = basename(name);
321 f->path = dirname(strdup(name));
322 f->readonly = readonly;
324 list_add_tail(&f->list, &extras);
327 int main(int argc, char **argv)
329 uid_t uid = getuid();
330 char log[] = "/dev/log";
331 char ubus[] = "/var/run/ubus.sock";
332 int ret = EXIT_SUCCESS;
336 ERROR("not root, aborting: %s\n", strerror(errno));
342 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
345 debug = atoi(optarg);
357 opts.seccomp = optarg;
358 add_extra(optarg, 1);
361 opts.capabilities = optarg;
362 add_extra(optarg, 1);
371 add_extra(optarg, 1);
374 add_extra(optarg, 0);
385 //no <binary> param found
386 if (argc - optind < 1) {
391 opts.jail_argv = &argv[optind];
394 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
396 if (!opts.path && asprintf(&opts.path, "/tmp/%s", basename(*opts.jail_argv)) == -1) {
397 ERROR("failed to asprintf root path: %s\n", strerror(errno));
401 if (mkdir(opts.path, 0755)) {
402 ERROR("unable to create root path: %s (%s)\n", opts.path, strerror(errno));
407 jail_process.pid = clone(spawn_jail,
408 child_stack + STACK_SIZE,
409 CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | SIGCHLD, argv);
411 if (jail_process.pid != -1) {
412 uloop_process_add(&jail_process);
416 kill(jail_process.pid, SIGTERM);
417 waitpid(jail_process.pid, NULL, 0);
420 ERROR("failed to spawn namespace: %s\n", strerror(errno));
424 if (rmdir(opts.path)) {
425 ERROR("Unable to remove root path: %s (%s)\n", opts.path, strerror(errno));
432 return jail_return_code;