Skip to content

Commit 669f852

Browse files
Jan-Eremicollet
authored andcommitted
1 parent 6b6444a commit 669f852

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
@@ -1881,8 +1881,13 @@ int main(int argc, char *argv[])
18811881
}
18821882
}
18831883

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

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)