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>
27 #include <linux/limits.h>
29 #include "capabilities.h"
35 #include <libubox/uloop.h>
37 #define STACK_SIZE (1024 * 1024)
38 #define OPT_ARGS "P:S:C:n:r:w:d:psulo"
52 extern int pivot_root(const char *new_root, const char *put_old);
56 static char child_stack[STACK_SIZE];
58 static int mkdir_p(char *dir, mode_t mask)
60 char *l = strrchr(dir, '/');
68 if (mkdir_p(dir, mask))
73 ret = mkdir(dir, mask);
74 if (ret && errno == EEXIST)
78 ERROR("mkdir failed on %s: %s\n", dir, strerror(errno));
83 int mount_bind(const char *root, const char *path, int readonly, int error)
90 ERROR("stat(%s) failed: %s\n", path, strerror(errno));
94 snprintf(new, sizeof(new), "%s%s", root, path);
95 if (S_ISDIR(s.st_mode)) {
98 mkdir_p(dirname(new), 0755);
99 snprintf(new, sizeof(new), "%s%s", root, path);
100 fd = creat(new, 0644);
102 ERROR("failed to create %s: %s\n", new, strerror(errno));
108 if (mount(path, new, NULL, MS_BIND, NULL)) {
109 ERROR("failed to mount -B %s %s: %s\n", path, new, strerror(errno));
113 if (readonly && mount(NULL, new, NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL)) {
114 ERROR("failed to remount ro %s: %s\n", new, strerror(errno));
118 DEBUG("mount -B %s %s\n", path, new);
123 static int build_jail_fs(void)
125 if (mount("tmpfs", opts.path, "tmpfs", MS_NOATIME, "mode=0755")) {
126 ERROR("tmpfs mount failed %s\n", strerror(errno));
130 if (chdir(opts.path)) {
131 ERROR("failed to chdir() in the jail root\n");
135 if (add_path_and_deps(*opts.jail_argv, 1, -1, 0)) {
136 ERROR("failed to load dependencies\n");
140 if (opts.seccomp && add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
141 ERROR("failed to load libpreload-seccomp.so\n");
145 if (mount_all(opts.path)) {
146 ERROR("mount_all() failed\n");
151 if (asprintf(&mpoint, "%s/old", opts.path) < 0) {
152 ERROR("failed to alloc pivot path: %s\n", strerror(errno));
155 mkdir_p(mpoint, 0755);
156 if (pivot_root(opts.path, mpoint) == -1) {
157 ERROR("pivot_root failed:%s\n", strerror(errno));
162 umount2("/old", MNT_DETACH);
165 mkdir("/proc", 0755);
166 mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
170 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
173 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
179 static char** build_envp(const char *seccomp)
181 static char *envp[MAX_ENVP];
182 static char preload_var[PATH_MAX];
183 static char seccomp_var[PATH_MAX];
184 static char debug_var[] = "LD_DEBUG=all";
185 const char *preload_lib = find_lib("libpreload-seccomp.so");
188 if (seccomp && !preload_lib) {
189 ERROR("failed to add preload-lib to env\n");
193 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
194 envp[count++] = seccomp_var;
195 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
196 envp[count++] = preload_var;
199 envp[count++] = debug_var;
204 static void usage(void)
206 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
207 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
208 fprintf(stderr, " -S <file>\tseccomp filter config\n");
209 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
210 fprintf(stderr, " -n <name>\tthe name of the jail\n");
211 fprintf(stderr, "namespace jail options:\n");
212 fprintf(stderr, " -P <path>\tpath where the jail will be staged\n");
213 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
214 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
215 fprintf(stderr, " -p\t\tjail has /proc\n");
216 fprintf(stderr, " -s\t\tjail has /sys\n");
217 fprintf(stderr, " -l\t\tjail has /dev/log\n");
218 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
219 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
220 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
221 and he has the same powers as root outside the jail,\n\
222 thus he can escape the jail and/or break stuff.\n\
223 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
224 If you use none of the namespace jail options,\n\
225 ujail will not use namespace/build a jail,\n\
226 and will only drop capabilities/apply seccomp filter.\n\n");
229 static int exec_jail(void)
231 char **envp = build_envp(opts.seccomp);
235 if (opts.capabilities && drop_capabilities(opts.capabilities))
238 INFO("exec-ing %s\n", *opts.jail_argv);
239 execve(*opts.jail_argv, opts.jail_argv, envp);
240 //we get there only if execve fails
241 ERROR("failed to execve %s: %s\n", *opts.jail_argv, strerror(errno));
245 static int spawn_jail(void *_notused)
247 if (opts.name && sethostname(opts.name, strlen(opts.name))) {
248 ERROR("failed to sethostname: %s\n", strerror(errno));
251 if (build_jail_fs()) {
252 ERROR("failed to build jail fs");
259 static int jail_running = 1;
260 static int jail_return_code = 0;
262 static void jail_process_handler(struct uloop_process *c, int ret)
264 if (WIFEXITED(ret)) {
265 jail_return_code = WEXITSTATUS(ret);
266 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
268 jail_return_code = WTERMSIG(ret);
269 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
275 static struct uloop_process jail_process = {
276 .cb = jail_process_handler,
279 int main(int argc, char **argv)
281 uid_t uid = getuid();
282 char log[] = "/dev/log";
283 char ubus[] = "/var/run/ubus.sock";
284 int ret = EXIT_SUCCESS;
288 ERROR("not root, aborting: %s\n", strerror(errno));
294 init_library_search();
296 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
299 debug = atoi(optarg);
314 opts.seccomp = optarg;
315 add_mount(optarg, 1, -1);
318 opts.capabilities = optarg;
319 add_mount(optarg, 1, -1);
330 add_path_and_deps(optarg, 1, 0, 0);
334 add_path_and_deps(optarg, 0, 0, 0);
338 add_mount(ubus, 0, -1);
342 add_mount(log, 0, -1);
347 //no <binary> param found
348 if (argc - optind < 1) {
352 if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
353 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
357 DEBUG("Using namespaces(%d), capabilities(%d), seccomp(%d)\n",
359 opts.capabilities != 0,
362 opts.jail_argv = &argv[optind];
365 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
367 if (opts.namespace && !opts.path && asprintf(&opts.path, "/tmp/%s", basename(*opts.jail_argv)) == -1) {
368 ERROR("failed to asprintf root path: %s\n", strerror(errno));
372 if (opts.namespace && mkdir(opts.path, 0755)) {
373 ERROR("unable to create root path: %s (%s)\n", opts.path, strerror(errno));
378 if (opts.namespace) {
379 jail_process.pid = clone(spawn_jail,
380 child_stack + STACK_SIZE,
381 CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | SIGCHLD, NULL);
383 jail_process.pid = fork();
386 if (jail_process.pid > 0) {
388 uloop_process_add(&jail_process);
392 DEBUG("uloop interrupted, killing jail process\n");
393 kill(jail_process.pid, SIGTERM);
394 waitpid(jail_process.pid, NULL, 0);
396 } else if (jail_process.pid == 0) {
400 ERROR("failed to clone/fork: %s\n", strerror(errno));
404 if (opts.namespace && rmdir(opts.path)) {
405 ERROR("Unable to remove root path: %s (%s)\n", opts.path, strerror(errno));
412 return jail_return_code;