Skip to content

Commit 3ee3a1f

Browse files
Jan-Eremicollet
authored andcommitted
1 parent cfe1b1a commit 3ee3a1f

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
@@ -1835,8 +1835,13 @@ int main(int argc, char *argv[])
18351835
}
18361836
}
18371837

1838+
/* Apache CGI will pass the query string to the command line if it doesn't contain a '='.
1839+
* This can create an issue where a malicious request can pass command line arguments to
1840+
* the executable. Ideally we skip argument parsing when we're in cgi or fastcgi mode,
1841+
* but that breaks PHP scripts on Linux with a hashbang: `#!/php-cgi -d option=value`.
1842+
* Therefore, this code only prevents passing arguments if the query string starts with a '-'.
1843+
* Similarly, scripts spawned in subprocesses on Windows may have the same issue. */
18381844
if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) {
1839-
/* we've got query string that has no = - apache CGI will pass it to command line */
18401845
unsigned char *p;
18411846
decoded_query_string = strdup(query_string);
18421847
php_url_decode(decoded_query_string, strlen(decoded_query_string));
@@ -1846,6 +1851,22 @@ int main(int argc, char *argv[])
18461851
if(*p == '-') {
18471852
skip_getopt = 1;
18481853
}
1854+
1855+
/* On Windows we have to take into account the "best fit" mapping behaviour. */
1856+
#ifdef PHP_WIN32
1857+
if (*p >= 0x80) {
1858+
wchar_t wide_buf[1];
1859+
wide_buf[0] = *p;
1860+
char char_buf[4];
1861+
size_t wide_buf_len = sizeof(wide_buf) / sizeof(wide_buf[0]);
1862+
size_t char_buf_len = sizeof(char_buf) / sizeof(char_buf[0]);
1863+
if (WideCharToMultiByte(CP_ACP, 0, wide_buf, wide_buf_len, char_buf, char_buf_len, NULL, NULL) == 0
1864+
|| char_buf[0] == '-') {
1865+
skip_getopt = 1;
1866+
}
1867+
}
1868+
#endif
1869+
18491870
free(decoded_query_string);
18501871
}
18511872

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)