Skip to content

Commit

Permalink
When there is any sort of error in setting up daemon process group, k…
Browse files Browse the repository at this point in the history
…ill the process rather than risk running in an unexpected state.
  • Loading branch information
GrahamDumpleton committed Jun 18, 2014
1 parent a8ac502 commit 545354a
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions src/server/mod_wsgi.c
Expand Up @@ -7087,7 +7087,7 @@ static void wsgi_setup_daemon_name(WSGIDaemonProcess *daemon, apr_pool_t *p)
#endif
}

static void wsgi_setup_access(WSGIDaemonProcess *daemon)
static int wsgi_setup_access(WSGIDaemonProcess *daemon)
{
/* Setup the umask for the effective user. */

Expand All @@ -7101,6 +7101,8 @@ static void wsgi_setup_access(WSGIDaemonProcess *daemon)
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, wsgi_server,
"mod_wsgi (pid=%d): Unable to change root "
"directory to '%s'.", getpid(), daemon->group->root);

return -1;
}
}

Expand All @@ -7111,6 +7113,8 @@ static void wsgi_setup_access(WSGIDaemonProcess *daemon)
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, wsgi_server,
"mod_wsgi (pid=%d): Unable to change working "
"directory to '%s'.", getpid(), daemon->group->home);

return -1;
}
}
else if (geteuid()) {
Expand All @@ -7123,12 +7127,16 @@ static void wsgi_setup_access(WSGIDaemonProcess *daemon)
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, wsgi_server,
"mod_wsgi (pid=%d): Unable to change working "
"directory to '%s'.", getpid(), pwent->pw_dir);

return -1;
}
}
else {
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, wsgi_server,
"mod_wsgi (pid=%d): Unable to determine home "
"directory for uid=%ld.", getpid(), (long)geteuid());

return -1;
}
}
else {
Expand All @@ -7141,27 +7149,33 @@ static void wsgi_setup_access(WSGIDaemonProcess *daemon)
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, wsgi_server,
"mod_wsgi (pid=%d): Unable to change working "
"directory to '%s'.", getpid(), pwent->pw_dir);

return -1;
}
}
else {
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, wsgi_server,
"mod_wsgi (pid=%d): Unable to determine home "
"directory for uid=%ld.", getpid(),
(long)daemon->group->uid);

return -1;
}
}

/* Don't bother switch user/group if not root. */

if (geteuid())
return;
return 0;

/* Setup the daemon process real and effective group. */

if (setgid(daemon->group->gid) == -1) {
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, wsgi_server,
"mod_wsgi (pid=%d): Unable to set group id to gid=%u.",
getpid(), (unsigned)daemon->group->gid);

return -1;
}
else {
if (daemon->group->groups) {
Expand All @@ -7172,13 +7186,17 @@ static void wsgi_setup_access(WSGIDaemonProcess *daemon)
"to set supplementary groups for uname=%s "
"of '%s'.", getpid(), daemon->group->user,
daemon->group->groups_list);

return -1;
}
}
else if (initgroups(daemon->group->user, daemon->group->gid) == -1) {
ap_log_error(APLOG_MARK, APLOG_ALERT, errno,
wsgi_server, "mod_wsgi (pid=%d): Unable "
"to set groups for uname=%s and gid=%u.", getpid(),
daemon->group->user, (unsigned)daemon->group->gid);

return -1;
}
}

Expand All @@ -7196,8 +7214,19 @@ static void wsgi_setup_access(WSGIDaemonProcess *daemon)
* reached their process limit. In that case will be left
* running as wrong user. Just exit on all failures to be
* safe. Don't die immediately to avoid a fork bomb.
*
* We could just return -1 here and let the caller do the
* sleep() and exit() but this failure is critical enough
* that we still do it here so it is obvious that the issue
* is being addressed.
*/

ap_log_error(APLOG_MARK, APLOG_ALERT, 0, wsgi_server,
"mod_wsgi (pid=%d): Failure to configure the "
"daemon process correctly and process left in "
"unspecified state. Restarting daemon process "
"after delay.", getpid());

sleep(20);

exit(-1);
Expand All @@ -7219,6 +7248,8 @@ static void wsgi_setup_access(WSGIDaemonProcess *daemon)
}
}
#endif

return 0;
}

static int wsgi_setup_socket(WSGIProcessGroup *process)
Expand Down Expand Up @@ -8561,7 +8592,24 @@ static int wsgi_start_process(apr_pool_t *p, WSGIDaemonProcess *daemon)

/* Setup daemon process user/group/umask etc. */

wsgi_setup_access(daemon);
if (wsgi_setup_access(daemon) == -1) {
/*
* If we get any failure from setting up the appropriate
* permissions or working directory for the daemon process
* then we exit the process. Don't die immediately to avoid
* a fork bomb.
*/

ap_log_error(APLOG_MARK, APLOG_ALERT, 0, wsgi_server,
"mod_wsgi (pid=%d): Failure to configure the "
"daemon process correctly and process left in "
"unspecified state. Restarting daemon process "
"after delay.", getpid());

sleep(20);

exit(-1);
}

/* Reinitialise accept mutex in daemon process. */

Expand Down

0 comments on commit 545354a

Please sign in to comment.