jail-friendly setuid()/setgid()

This commit is contained in:
roberto@maverick64
2011-01-05 18:48:50 +01:00
parent 72f677bb0d
commit 0a2da2a2ce
3 changed files with 31 additions and 16 deletions
+19
View File
@@ -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)) {
+10 -16
View File
@@ -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:
+2
View File
@@ -637,6 +637,8 @@ struct uwsgi_server {
char *chroot;
gid_t gid;
uid_t uid;
char *uidname;
char *gidname;
char *mode;