From f2dcdf2289d297fe294f3163973045faa0ac00a8 Mon Sep 17 00:00:00 2001 From: Unbit Date: Mon, 2 Sep 2013 10:38:40 +0200 Subject: [PATCH] added --pivot-root (linux only) --- core/utils.c | 38 ++++++++++++++++++++++++++++++++++++++ core/uwsgi.c | 4 ++++ uwsgi.h | 1 + 3 files changed, 43 insertions(+) diff --git a/core/utils.c b/core/utils.c index 09c609ca..37e2e079 100644 --- a/core/utils.c +++ b/core/utils.c @@ -354,6 +354,24 @@ void uwsgi_as_root() { else { uwsgi_log("[linux-namespace] applied unshare() mask: %d\n", uwsgi.unshare); } + + struct uwsgi_string_list *usl = uwsgi.exec_post_jail; + while(usl) { + uwsgi_log("running \"%s\" (post-jail)...\n", usl->value); + int ret = uwsgi_run_command_and_wait(NULL, usl->value); + if (ret != 0) { + uwsgi_log("command \"%s\" exited with non-zero code: %d\n", usl->value, ret); + exit(1); + } + usl = usl->next; + } + + uwsgi_foreach(usl, uwsgi.call_post_jail) { + if (uwsgi_call_symbol(usl->value)) { + uwsgi_log("unaable to call function \"%s\"\n", usl->value); + } + } + } #endif @@ -372,6 +390,26 @@ void uwsgi_as_root() { #endif } +#ifdef __linux__ + if (uwsgi.pivot_root && !uwsgi.reloads) { + char *arg = uwsgi_str(uwsgi.pivot_root); + char *space = strchr(arg, ' '); + if (!space) { + uwsgi_log("invalid pivot_root syntax, new_root and put_old must be separated by a space\n"); + exit(1); + } + *space = 0; + if (pivot_root(arg, space+1)) { + uwsgi_error("pivot_root()"); + exit(1); + } + if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG]) { + uwsgi_log("*** Warning, on linux system you have to bind-mount the /proc fs in your chroot to get memory debug/report.\n"); + } + free(arg); + } +#endif + struct uwsgi_string_list *usl; uwsgi_foreach(usl, uwsgi.wait_for_interface) { diff --git a/core/uwsgi.c b/core/uwsgi.c index e06957bc..53477781 100644 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -289,6 +289,10 @@ static struct uwsgi_option uwsgi_base_options[] = { {"pidfile", required_argument, 0, "create pidfile (before privileges drop)", uwsgi_opt_set_str, &uwsgi.pidfile, 0}, {"pidfile2", required_argument, 0, "create pidfile (after privileges drop)", uwsgi_opt_set_str, &uwsgi.pidfile2, 0}, {"chroot", required_argument, 0, "chroot() to the specified directory", uwsgi_opt_set_str, &uwsgi.chroot, 0}, +#ifdef __linux__ + {"pivot_root", required_argument, 0, "pivot_root() to the specified directories (new_root and put_old must be separated with a space)", uwsgi_opt_set_str, &uwsgi.pivot_root, 0}, +#endif + {"uid", required_argument, 0, "setuid to the specified user/uid", uwsgi_opt_set_uid, NULL, 0}, {"gid", required_argument, 0, "setgid to the specified group/gid", uwsgi_opt_set_gid, NULL, 0}, {"add-gid", required_argument, 0, "add the specified group id to the process credentials", uwsgi_opt_add_string_list, &uwsgi.additional_gids, 0}, diff --git a/uwsgi.h b/uwsgi.h index 873426b5..3b7610e3 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1820,6 +1820,7 @@ struct uwsgi_server { #ifdef __linux__ int unshare; int emperor_clone; + char *pivot_root; #endif int refork;