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 "S:C:n:h:r:w:d:psuloc"
53 extern int pivot_root(const char *new_root, const char *put_old);
57 static char child_stack[STACK_SIZE];
59 static int mkdir_p(char *dir, mode_t mask)
61 char *l = strrchr(dir, '/');
69 if (mkdir_p(dir, mask))
74 ret = mkdir(dir, mask);
75 if (ret && errno == EEXIST)
79 ERROR("mkdir failed on %s: %s\n", dir, strerror(errno));
84 int mount_bind(const char *root, const char *path, int readonly, int error)
91 ERROR("stat(%s) failed: %s\n", path, strerror(errno));
95 snprintf(new, sizeof(new), "%s%s", root, path);
96 if (S_ISDIR(s.st_mode)) {
99 mkdir_p(dirname(new), 0755);
100 snprintf(new, sizeof(new), "%s%s", root, path);
101 fd = creat(new, 0644);
103 ERROR("failed to create %s: %s\n", new, strerror(errno));
109 if (mount(path, new, NULL, MS_BIND, NULL)) {
110 ERROR("failed to mount -B %s %s: %s\n", path, new, strerror(errno));
114 if (readonly && mount(NULL, new, NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL)) {
115 ERROR("failed to remount ro %s: %s\n", new, strerror(errno));
119 DEBUG("mount -B %s %s\n", path, new);
124 static int build_jail_fs(void)
126 char jail_root[] = "/tmp/ujail-XXXXXX";
127 if (mkdtemp(jail_root) == NULL) {
128 ERROR("mkdtemp(jail_root) failed: %s\n", strerror(errno));
132 if (mount("tmpfs", jail_root, "tmpfs", MS_NOATIME, "mode=0755")) {
133 ERROR("tmpfs mount failed %s\n", strerror(errno));
137 if (chdir(jail_root)) {
138 ERROR("failed to chdir() in the jail root\n");
142 if (add_path_and_deps(*opts.jail_argv, 1, -1, 0)) {
143 ERROR("failed to load dependencies\n");
147 if (opts.seccomp && add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
148 ERROR("failed to load libpreload-seccomp.so\n");
152 if (mount_all(jail_root)) {
153 ERROR("mount_all() failed\n");
157 char dirbuf[sizeof(jail_root) + 4];
158 snprintf(dirbuf, sizeof(dirbuf), "%s/old", jail_root);
161 if (pivot_root(jail_root, dirbuf) == -1) {
162 ERROR("pivot_root failed: %s\n", strerror(errno));
166 snprintf(dirbuf, sizeof(dirbuf), "/old%s", jail_root);
168 umount2("/old", MNT_DETACH);
172 mkdir("/proc", 0755);
173 mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
177 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
180 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
186 static char** build_envp(const char *seccomp)
188 static char *envp[MAX_ENVP];
189 static char preload_var[PATH_MAX];
190 static char seccomp_var[PATH_MAX];
191 static char debug_var[] = "LD_DEBUG=all";
192 const char *preload_lib = find_lib("libpreload-seccomp.so");
195 if (seccomp && !preload_lib) {
196 ERROR("failed to add preload-lib to env\n");
200 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
201 envp[count++] = seccomp_var;
202 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
203 envp[count++] = preload_var;
206 envp[count++] = debug_var;
211 static void usage(void)
213 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
214 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
215 fprintf(stderr, " -S <file>\tseccomp filter config\n");
216 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
217 fprintf(stderr, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
218 fprintf(stderr, " -n <name>\tthe name of the jail\n");
219 fprintf(stderr, "namespace jail options:\n");
220 fprintf(stderr, " -h <hostname>\tchange the hostname of the jail\n");
221 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
222 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
223 fprintf(stderr, " -p\t\tjail has /proc\n");
224 fprintf(stderr, " -s\t\tjail has /sys\n");
225 fprintf(stderr, " -l\t\tjail has /dev/log\n");
226 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
227 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
228 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
229 and he has the same powers as root outside the jail,\n\
230 thus he can escape the jail and/or break stuff.\n\
231 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
232 If you use none of the namespace jail options,\n\
233 ujail will not use namespace/build a jail,\n\
234 and will only drop capabilities/apply seccomp filter.\n\n");
237 static int exec_jail(void)
239 char **envp = build_envp(opts.seccomp);
243 if (opts.capabilities && drop_capabilities(opts.capabilities))
246 if (opts.no_new_privs && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
247 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %s\n", strerror(errno));
251 INFO("exec-ing %s\n", *opts.jail_argv);
252 execve(*opts.jail_argv, opts.jail_argv, envp);
253 /* we get there only if execve fails */
254 ERROR("failed to execve %s: %s\n", *opts.jail_argv, strerror(errno));
258 static int spawn_jail(void *_notused)
260 if (opts.hostname && sethostname(opts.hostname, strlen(opts.hostname))) {
261 ERROR("sethostname(%s) failed: %s\n", opts.hostname, strerror(errno));
264 if (build_jail_fs()) {
265 ERROR("failed to build jail fs");
272 static int jail_running = 1;
273 static int jail_return_code = 0;
275 static void jail_process_handler(struct uloop_process *c, int ret)
277 if (WIFEXITED(ret)) {
278 jail_return_code = WEXITSTATUS(ret);
279 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
281 jail_return_code = WTERMSIG(ret);
282 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
288 static struct uloop_process jail_process = {
289 .cb = jail_process_handler,
292 int main(int argc, char **argv)
294 uid_t uid = getuid();
295 char log[] = "/dev/log";
296 char ubus[] = "/var/run/ubus.sock";
300 ERROR("not root, aborting: %s\n", strerror(errno));
306 init_library_search();
308 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
311 debug = atoi(optarg);
326 opts.seccomp = optarg;
327 add_mount(optarg, 1, -1);
330 opts.capabilities = optarg;
331 add_mount(optarg, 1, -1);
334 opts.no_new_privs = 1;
340 opts.hostname = optarg;
344 add_path_and_deps(optarg, 1, 0, 0);
348 add_path_and_deps(optarg, 0, 0, 0);
352 add_mount(ubus, 0, -1);
356 add_mount(log, 0, -1);
361 /* no <binary> param found */
362 if (argc - optind < 1) {
366 if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
367 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
371 DEBUG("Using namespaces(%d), capabilities(%d), seccomp(%d)\n",
373 opts.capabilities != 0,
376 opts.jail_argv = &argv[optind];
379 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
382 if (opts.namespace) {
383 jail_process.pid = clone(spawn_jail,
384 child_stack + STACK_SIZE,
385 CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | SIGCHLD, NULL);
387 jail_process.pid = fork();
390 if (jail_process.pid > 0) {
392 uloop_process_add(&jail_process);
396 DEBUG("uloop interrupted, killing jail process\n");
397 kill(jail_process.pid, SIGTERM);
398 waitpid(jail_process.pid, NULL, 0);
400 return jail_return_code;
401 } else if (jail_process.pid == 0) {
402 /* fork child process */
405 ERROR("failed to clone/fork: %s\n", strerror(errno));