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>
18 #include <sys/types.h>
30 #include <linux/limits.h>
33 #include "capabilities.h"
39 #include <libubox/uloop.h>
42 #define STACK_SIZE (1024 * 1024)
43 #define OPT_ARGS "S:C:n:h:r:w:d:psulocU:G:N"
45 #define NAMESPACE_MOUNT (1U << 0)
46 #define NAMESPACE_IPC (1U << 1)
47 #define NAMESPACE_NET (1U << 2)
48 #define NAMESPACE_PID (1U << 3)
49 #define NAMESPACE_USER (1U << 4)
50 #define NAMESPACE_UTS (1U << 5)
51 #define NAMESPACE_CGROUP (1U << 6)
68 extern int pivot_root(const char *new_root, const char *put_old);
72 static char child_stack[STACK_SIZE];
74 static int mkdir_p(char *dir, mode_t mask)
76 char *l = strrchr(dir, '/');
84 if (mkdir_p(dir, mask))
89 ret = mkdir(dir, mask);
90 if (ret && errno == EEXIST)
94 ERROR("mkdir(%s, %d) failed: %m\n", dir, mask);
99 int mount_bind(const char *root, const char *path, int readonly, int error)
105 if (stat(path, &s)) {
106 ERROR("stat(%s) failed: %m\n", path);
110 snprintf(new, sizeof(new), "%s%s", root, path);
111 if (S_ISDIR(s.st_mode)) {
114 mkdir_p(dirname(new), 0755);
115 snprintf(new, sizeof(new), "%s%s", root, path);
116 fd = creat(new, 0644);
118 ERROR("creat(%s) failed: %m\n", new);
124 if (mount(path, new, NULL, MS_BIND, NULL)) {
125 ERROR("failed to mount -B %s %s: %m\n", path, new);
129 if (readonly && mount(NULL, new, NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL)) {
130 ERROR("failed to remount ro %s: %m\n", new);
134 DEBUG("mount -B %s %s (%s)\n", path, new, readonly?"ro":"rw");
139 static int build_jail_fs(void)
141 char jail_root[] = "/tmp/ujail-XXXXXX";
142 if (mkdtemp(jail_root) == NULL) {
143 ERROR("mkdtemp(%s) failed: %m\n", jail_root);
147 /* oldroot can't be MS_SHARED else pivot_root() fails */
148 if (mount("none", "/", NULL, MS_REC|MS_PRIVATE, NULL)) {
149 ERROR("private mount failed %m\n");
153 if (mount("tmpfs", jail_root, "tmpfs", MS_NOATIME, "mode=0755")) {
154 ERROR("tmpfs mount failed %m\n");
158 if (chdir(jail_root)) {
159 ERROR("chdir(%s) (jail_root) failed: %m\n", jail_root);
163 if (mount_all(jail_root)) {
164 ERROR("mount_all() failed\n");
168 char dirbuf[sizeof(jail_root) + 4];
169 snprintf(dirbuf, sizeof(dirbuf), "%s/old", jail_root);
172 if (pivot_root(jail_root, dirbuf) == -1) {
173 ERROR("pivot_root(%s, %s) failed: %m\n", jail_root, dirbuf);
177 ERROR("chdir(/) (after pivot_root) failed: %m\n");
181 snprintf(dirbuf, sizeof(dirbuf), "/old%s", jail_root);
183 umount2("/old", MNT_DETACH);
187 mkdir("/proc", 0755);
188 mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
192 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
195 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
201 static char** build_envp(const char *seccomp)
203 static char *envp[MAX_ENVP];
204 static char preload_var[PATH_MAX];
205 static char seccomp_var[PATH_MAX];
206 static char debug_var[] = "LD_DEBUG=all";
207 const char *preload_lib = find_lib("libpreload-seccomp.so");
210 if (seccomp && !preload_lib) {
211 ERROR("failed to add preload-lib to env\n");
215 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
216 envp[count++] = seccomp_var;
217 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
218 envp[count++] = preload_var;
221 envp[count++] = debug_var;
226 static void usage(void)
228 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
229 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
230 fprintf(stderr, " -S <file>\tseccomp filter config\n");
231 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
232 fprintf(stderr, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
233 fprintf(stderr, " -n <name>\tthe name of the jail\n");
234 fprintf(stderr, "namespace jail options:\n");
235 fprintf(stderr, " -h <hostname>\tchange the hostname of the jail\n");
236 fprintf(stderr, " -N\t\tjail has network namespace\n");
237 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
238 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
239 fprintf(stderr, " -p\t\tjail has /proc\n");
240 fprintf(stderr, " -s\t\tjail has /sys\n");
241 fprintf(stderr, " -l\t\tjail has /dev/log\n");
242 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
243 fprintf(stderr, " -U <name>\tuser to run jailed process\n");
244 fprintf(stderr, " -G <name>\tgroup to run jailed process\n");
245 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
246 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
247 and he has the same powers as root outside the jail,\n\
248 thus he can escape the jail and/or break stuff.\n\
249 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
250 If you use none of the namespace jail options,\n\
251 ujail will not use namespace/build a jail,\n\
252 and will only drop capabilities/apply seccomp filter.\n\n");
255 static int exec_jail(void *_notused)
257 struct passwd *p = NULL;
258 struct group *g = NULL;
260 if (opts.capabilities && drop_capabilities(opts.capabilities))
263 if (opts.no_new_privs && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
264 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %m\n");
268 if (opts.namespace && opts.hostname && strlen(opts.hostname) > 0
269 && sethostname(opts.hostname, strlen(opts.hostname))) {
270 ERROR("sethostname(%s) failed: %m\n", opts.hostname);
274 if (opts.namespace && build_jail_fs()) {
275 ERROR("failed to build jail fs\n");
280 p = getpwnam(opts.user);
282 ERROR("failed to get uid/gid for user %s: %d (%s)\n",
283 opts.user, errno, strerror(errno));
289 g = getgrnam(opts.group);
291 ERROR("failed to get gid for group %s: %m\n", opts.group);
296 if (p && p->pw_gid && initgroups(opts.user, p->pw_gid)) {
297 ERROR("failed to initgroups() for user %s: %m\n", opts.user);
301 if (g && g->gr_gid && setgid(g->gr_gid)) {
302 ERROR("failed to set group id %d: %m\n", g?g->gr_gid:p->pw_gid);
306 if (p && p->pw_uid && setuid(p->pw_uid)) {
307 ERROR("failed to set user id %d: %m\n", p->pw_uid);
312 char **envp = build_envp(opts.seccomp);
316 INFO("exec-ing %s\n", *opts.jail_argv);
317 execve(*opts.jail_argv, opts.jail_argv, envp);
318 /* we get there only if execve fails */
319 ERROR("failed to execve %s: %m\n", *opts.jail_argv);
323 static int jail_running = 1;
324 static int jail_return_code = 0;
326 static void jail_process_timeout_cb(struct uloop_timeout *t);
327 static struct uloop_timeout jail_process_timeout = {
328 .cb = jail_process_timeout_cb,
331 static void jail_process_handler(struct uloop_process *c, int ret)
333 uloop_timeout_cancel(&jail_process_timeout);
334 if (WIFEXITED(ret)) {
335 jail_return_code = WEXITSTATUS(ret);
336 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
338 jail_return_code = WTERMSIG(ret);
339 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
345 static struct uloop_process jail_process = {
346 .cb = jail_process_handler,
349 static void jail_process_timeout_cb(struct uloop_timeout *t)
351 DEBUG("jail process failed to stop, sending SIGKILL\n");
352 kill(jail_process.pid, SIGKILL);
355 static void jail_handle_signal(int signo)
357 DEBUG("forwarding signal %d to the jailed process\n", signo);
358 kill(jail_process.pid, signo);
361 static void netns_updown(bool start)
363 struct ubus_context *ctx = ubus_connect(NULL);
364 static struct blob_buf req;
366 pid_t pid = getpid();
371 blob_buf_init(&req, 0);
372 blobmsg_add_string(&req, "jail", opts.name);
373 blobmsg_add_u32(&req, "pid", pid);
374 blobmsg_add_u8(&req, "start", start);
376 if (ubus_lookup_id(ctx, "network", &id) ||
377 ubus_invoke(ctx, id, "netns_updown", req.head, NULL, NULL, 3000))
378 INFO("ubus request failed\n");
384 int main(int argc, char **argv)
387 uid_t uid = getuid();
388 char log[] = "/dev/log";
389 char ubus[] = "/var/run/ubus.sock";
393 ERROR("not root, aborting: %m\n");
399 init_library_search();
401 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
404 debug = atoi(optarg);
407 opts.namespace |= NAMESPACE_MOUNT;
411 opts.namespace |= NAMESPACE_MOUNT;
415 opts.namespace |= NAMESPACE_MOUNT;
419 opts.seccomp = optarg;
420 add_mount(optarg, 1, -1);
423 opts.capabilities = optarg;
426 opts.no_new_privs = 1;
432 opts.namespace |= NAMESPACE_NET;
435 opts.hostname = optarg;
438 opts.namespace |= NAMESPACE_MOUNT;
439 add_path_and_deps(optarg, 1, 0, 0);
442 opts.namespace |= NAMESPACE_MOUNT;
443 add_path_and_deps(optarg, 0, 0, 0);
446 opts.namespace |= NAMESPACE_MOUNT;
447 add_mount(ubus, 0, -1);
450 opts.namespace |= NAMESPACE_MOUNT;
451 add_mount(log, 0, -1);
462 /* no <binary> param found */
463 if (argc - optind < 1) {
467 if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
468 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
472 DEBUG("Using namespaces(%d), capabilities(%d), seccomp(%d)\n",
474 opts.capabilities != 0,
477 opts.jail_argv = &argv[optind];
479 if (opts.namespace && add_path_and_deps(*opts.jail_argv, 1, -1, 0)) {
480 ERROR("failed to load dependencies\n");
484 if (opts.namespace && opts.seccomp && add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
485 ERROR("failed to load libpreload-seccomp.so\n");
490 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
494 sigfillset(&sigmask);
495 for (i = 0; i < _NSIG; i++) {
496 struct sigaction s = { 0 };
498 if (!sigismember(&sigmask, i))
500 if ((i == SIGCHLD) || (i == SIGPIPE))
503 s.sa_handler = jail_handle_signal;
504 sigaction(i, &s, NULL);
507 if (opts.namespace) {
508 int flags = SIGCHLD | CLONE_NEWPID | CLONE_NEWIPC;
510 if (opts.namespace & NAMESPACE_MOUNT) {
511 flags |= CLONE_NEWNS;
512 add_mount("/dev/full", 0, -1);
513 add_mount("/dev/null", 0, -1);
514 add_mount("/dev/urandom", 0, -1);
515 add_mount("/dev/zero", 0, -1);
517 if (opts.user || opts.group) {
518 add_mount("/etc/passwd", 0, -1);
519 add_mount("/etc/group", 0, -1);
524 flags |= CLONE_NEWUTS;
526 if (opts.namespace & NAMESPACE_NET) {
527 unshare(CLONE_NEWNET);
531 jail_process.pid = clone(exec_jail, child_stack + STACK_SIZE, flags, NULL);
533 jail_process.pid = fork();
536 if (jail_process.pid > 0) {
538 uloop_process_add(&jail_process);
541 DEBUG("uloop interrupted, killing jail process\n");
542 kill(jail_process.pid, SIGTERM);
543 uloop_timeout_set(&jail_process_timeout, 1000);
547 if (opts.namespace & NAMESPACE_NET)
550 return jail_return_code;
551 } else if (jail_process.pid == 0) {
552 /* fork child process */
553 return exec_jail(NULL);
555 ERROR("failed to clone/fork: %m\n");