From 0a2da2a2ce72ebc9ca31309d8833197eec518fbb Mon Sep 17 00:00:00 2001 From: "roberto@maverick64" Date: Wed, 5 Jan 2011 18:48:50 +0100 Subject: [PATCH] jail-friendly setuid()/setgid() --- utils.c | 19 +++++++++++++++++++ uwsgi.c | 26 ++++++++++---------------- uwsgi.h | 2 ++ 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/utils.c b/utils.c index 52906ee1..32d675cc 100644 --- a/utils.c +++ b/utils.c @@ -288,6 +288,25 @@ void uwsgi_as_root() { } #endif } + + if (uwsgi.gidname) { + struct group *ugroup = getgrnam(uwsgi.gidname); + if (ugroup) { + uwsgi.gid = ugroup->gr_gid; + } else { + uwsgi_log("group %s not found.\n", uwsgi.gidname); + exit(1); + } + } + if (uwsgi.uidname) { + struct passwd *upasswd = getpwnam(uwsgi.uidname); + if (upasswd) { + uwsgi.uid = upasswd->pw_uid; + } else { + uwsgi_log("user %s not found.\n", uwsgi.uidname); + exit(1); + } + } if (uwsgi.gid) { uwsgi_log("setgid() to %d\n", uwsgi.gid); if (setgid(uwsgi.gid)) { diff --git a/uwsgi.c b/uwsgi.c index 474381e3..4f3e4c8f 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -717,8 +717,14 @@ int uwsgi_start(void *v_argv) { #ifdef __linux__ if (uwsgi.ns && getpid() == 1) { - if (sethostname("uwsgifakehost", strlen("uwsgifakehost"))) { - uwsgi_error("sethostname()"); + + char *ns_hostname = strchr(uwsgi.ns, ':'); + if (ns_hostname) { + ns_hostname[0] = 0; + ns_hostname++; + if (sethostname(ns_hostname, strlen(ns_hostname))) { + uwsgi_error("sethostname()"); + } } FILE *procmounts; @@ -1769,25 +1775,13 @@ end: case LONG_ARGS_GID: uwsgi.gid = atoi(optarg); if (!uwsgi.gid) { - struct group *ugroup = getgrnam(optarg); - if (ugroup) { - uwsgi.gid = ugroup->gr_gid; - } else { - uwsgi_log("group %s not found.\n", optarg); - exit(1); - } + uwsgi.gidname = optarg; } return 1; case LONG_ARGS_UID: uwsgi.uid = atoi(optarg); if (!uwsgi.uid) { - struct passwd *upasswd = getpwnam(optarg); - if (upasswd) { - uwsgi.uid = upasswd->pw_uid; - } else { - uwsgi_log("user %s not found.\n", optarg); - exit(1); - } + uwsgi.uidname = optarg; } return 1; case LONG_ARGS_BINARY_PATH: diff --git a/uwsgi.h b/uwsgi.h index 2162f8ca..dbde1bb6 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -637,6 +637,8 @@ struct uwsgi_server { char *chroot; gid_t gid; uid_t uid; + char *uidname; + char *gidname; char *mode;