From 665cc39b95bc4c198ffdcb976acb663639237511 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Sun, 14 Feb 2016 10:54:26 +0000 Subject: [PATCH] [mod_cgi] edge case chdir "/" when docroot "/" (fixes #2460) From: Glenn Strauss git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@3077 152afb58-edef-0310-8abb-c4023f1b3aa9 --- NEWS | 1 + src/mod_cgi.c | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 1adf0d0a..d9ba9a5d 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,7 @@ NEWS * add force_assert for more allocation results * [mod_cgi] use MAP_PRIVATE to mmap temporary file (fixes #2715) * [core] do not send SIGHUP to process group unless server.max-workers is used (fixes #2711) + * [mod_cgi] edge case chdir "/" when docroot "/" (fixes #2460) - 1.4.39 - 2016-01-02 * [core] fix memset_s call (fixes #2698) diff --git a/src/mod_cgi.c b/src/mod_cgi.c index 2be7d1ff..83caad57 100644 --- a/src/mod_cgi.c +++ b/src/mod_cgi.c @@ -1075,10 +1075,13 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer * /* search for the last / */ if (NULL != (c = strrchr(con->physical.path->ptr, '/'))) { - *c = '\0'; + /* handle special case of file in root directory */ + const char* physdir = (c == con->physical.path->ptr) ? "/" : con->physical.path->ptr; + /* temporarily shorten con->physical.path to directory without terminating '/' */ + *c = '\0'; /* change to the physical directory */ - if (-1 == chdir(con->physical.path->ptr)) { + if (-1 == chdir(physdir)) { log_error_write(srv, __FILE__, __LINE__, "ssb", "chdir failed:", strerror(errno), con->physical.path); } *c = '/';