Skip to content

Commit 6374e4e

Browse files
Jan-Eremicollet
authored andcommitted
1 parent 8d4db37 commit 6374e4e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

sapi/cgi/cgi_main.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1887,8 +1887,13 @@ int main(int argc, char *argv[])
18871887
}
18881888
}
18891889

1890+
/* Apache CGI will pass the query string to the command line if it doesn't contain a '='.
1891+
* This can create an issue where a malicious request can pass command line arguments to
1892+
* the executable. Ideally we skip argument parsing when we're in cgi or fastcgi mode,
1893+
* but that breaks PHP scripts on Linux with a hashbang: `#!/php-cgi -d option=value`.
1894+
* Therefore, this code only prevents passing arguments if the query string starts with a '-'.
1895+
* Similarly, scripts spawned in subprocesses on Windows may have the same issue. */
18901896
if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) {
1891-
/* we've got query string that has no = - apache CGI will pass it to command line */
18921897
unsigned char *p;
18931898
decoded_query_string = strdup(query_string);
18941899
php_url_decode(decoded_query_string, strlen(decoded_query_string));
@@ -1898,6 +1903,22 @@ int main(int argc, char *argv[])
18981903
if(*p == '-') {
18991904
skip_getopt = 1;
19001905
}
1906+
1907+
/* On Windows we have to take into account the "best fit" mapping behaviour. */
1908+
#ifdef PHP_WIN32
1909+
if (*p >= 0x80) {
1910+
wchar_t wide_buf[1];
1911+
wide_buf[0] = *p;
1912+
char char_buf[4];
1913+
size_t wide_buf_len = sizeof(wide_buf) / sizeof(wide_buf[0]);
1914+
size_t char_buf_len = sizeof(char_buf) / sizeof(char_buf[0]);
1915+
if (WideCharToMultiByte(CP_ACP, 0, wide_buf, wide_buf_len, char_buf, char_buf_len, NULL, NULL) == 0
1916+
|| char_buf[0] == '-') {
1917+
skip_getopt = 1;
1918+
}
1919+
}
1920+
#endif
1921+
19011922
free(decoded_query_string);
19021923
}
19031924

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
GHSA-3qgc-jrrr-25jv
3+
--SKIPIF--
4+
<?php
5+
include 'skipif.inc';
6+
if (PHP_OS_FAMILY !== "Windows") die("skip Only for Windows");
7+
8+
$codepage = trim(shell_exec("powershell Get-ItemPropertyValue HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Nls\\CodePage ACP"));
9+
if ($codepage !== '932' && $codepage !== '936' && $codepage !== '950') die("skip Wrong codepage");
10+
?>
11+
--FILE--
12+
<?php
13+
include 'include.inc';
14+
15+
$filename = __DIR__."/GHSA-3qgc-jrrr-25jv_tmp.php";
16+
$script = '<?php echo "hello "; echo "world"; ?>';
17+
file_put_contents($filename, $script);
18+
19+
$php = get_cgi_path();
20+
reset_env_vars();
21+
22+
putenv("SERVER_NAME=Test");
23+
putenv("SCRIPT_FILENAME=$filename");
24+
putenv("QUERY_STRING=%ads");
25+
putenv("REDIRECT_STATUS=1");
26+
27+
passthru("$php -s");
28+
29+
?>
30+
--CLEAN--
31+
<?php
32+
@unlink(__DIR__."/GHSA-3qgc-jrrr-25jv_tmp.php");
33+
?>
34+
--EXPECTF--
35+
X-Powered-By: PHP/%s
36+
Content-type: %s
37+
38+
hello world

0 commit comments

Comments
 (0)