Skip to content

Commit 0d89b54

Browse files
bukkaericmann
authored andcommitted
Add proc_open escaping for cmd file execution
1 parent 6b40a5a commit 0d89b54

File tree

4 files changed

+168
-5
lines changed

4 files changed

+168
-5
lines changed

ext/standard/proc_open.c

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,32 @@ static void append_backslashes(smart_str *str, size_t num_bs)
538538
}
539539
}
540540

541-
/* See https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments */
542-
static void append_win_escaped_arg(smart_str *str, zend_string *arg)
541+
const char *special_chars = "()!^\"<>&|%";
542+
543+
static bool is_special_character_present(const zend_string *arg)
544+
{
545+
for (size_t i = 0; i < ZSTR_LEN(arg); ++i) {
546+
if (strchr(special_chars, ZSTR_VAL(arg)[i]) != NULL) {
547+
return true;
548+
}
549+
}
550+
return false;
551+
}
552+
553+
/* See https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments and
554+
* https://learn.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way */
555+
static void append_win_escaped_arg(smart_str *str, zend_string *arg, bool is_cmd_argument)
543556
{
544557
size_t num_bs = 0;
558+
bool has_special_character = false;
545559

560+
if (is_cmd_argument) {
561+
has_special_character = is_special_character_present(arg);
562+
if (has_special_character) {
563+
/* Escape double quote with ^ if executed by cmd.exe. */
564+
smart_str_appendc(str, '^');
565+
}
566+
}
546567
smart_str_appendc(str, '"');
547568
for (size_t i = 0; i < ZSTR_LEN(arg); ++i) {
548569
char c = ZSTR_VAL(arg)[i];
@@ -556,18 +577,71 @@ static void append_win_escaped_arg(smart_str *str, zend_string *arg)
556577
num_bs = num_bs * 2 + 1;
557578
}
558579
append_backslashes(str, num_bs);
580+
if (has_special_character && strchr(special_chars, c) != NULL) {
581+
/* Escape special chars with ^ if executed by cmd.exe. */
582+
smart_str_appendc(str, '^');
583+
}
559584
smart_str_appendc(str, c);
560585
num_bs = 0;
561586
}
562587
append_backslashes(str, num_bs * 2);
588+
if (has_special_character) {
589+
/* Escape double quote with ^ if executed by cmd.exe. */
590+
smart_str_appendc(str, '^');
591+
}
563592
smart_str_appendc(str, '"');
564593
}
565594

595+
static inline int stricmp_end(const char* suffix, const char* str) {
596+
size_t suffix_len = strlen(suffix);
597+
size_t str_len = strlen(str);
598+
599+
if (suffix_len > str_len) {
600+
return -1; /* Suffix is longer than string, cannot match. */
601+
}
602+
603+
/* Compare the end of the string with the suffix, ignoring case. */
604+
return _stricmp(str + (str_len - suffix_len), suffix);
605+
}
606+
607+
static bool is_executed_by_cmd(const char *prog_name)
608+
{
609+
/* If program name is cmd.exe, then return true. */
610+
if (_stricmp("cmd.exe", prog_name) == 0 || _stricmp("cmd", prog_name) == 0
611+
|| stricmp_end("\\cmd.exe", prog_name) == 0 || stricmp_end("\\cmd", prog_name) == 0) {
612+
return true;
613+
}
614+
615+
/* Find the last occurrence of the directory separator (backslash or forward slash). */
616+
char *last_separator = strrchr(prog_name, '\\');
617+
char *last_separator_fwd = strrchr(prog_name, '/');
618+
if (last_separator_fwd && (!last_separator || last_separator < last_separator_fwd)) {
619+
last_separator = last_separator_fwd;
620+
}
621+
622+
/* Find the last dot in the filename after the last directory separator. */
623+
char *extension = NULL;
624+
if (last_separator != NULL) {
625+
extension = strrchr(last_separator, '.');
626+
} else {
627+
extension = strrchr(prog_name, '.');
628+
}
629+
630+
if (extension == NULL || extension == prog_name) {
631+
/* No file extension found, it is not batch file. */
632+
return false;
633+
}
634+
635+
/* Check if the file extension is ".bat" or ".cmd" which is always executed by cmd.exe. */
636+
return _stricmp(extension, ".bat") == 0 || _stricmp(extension, ".cmd") == 0;
637+
}
638+
566639
static zend_string *create_win_command_from_args(HashTable *args)
567640
{
568641
smart_str str = {0};
569642
zval *arg_zv;
570-
bool is_prog_name = 1;
643+
bool is_prog_name = true;
644+
bool is_cmd_execution = false;
571645
int elem_num = 0;
572646

573647
ZEND_HASH_FOREACH_VAL(args, arg_zv) {
@@ -577,11 +651,13 @@ static zend_string *create_win_command_from_args(HashTable *args)
577651
return NULL;
578652
}
579653

580-
if (!is_prog_name) {
654+
if (is_prog_name) {
655+
is_cmd_execution = is_executed_by_cmd(ZSTR_VAL(arg_str));
656+
} else {
581657
smart_str_appendc(&str, ' ');
582658
}
583659

584-
append_win_escaped_arg(&str, arg_str);
660+
append_win_escaped_arg(&str, arg_str, !is_prog_name && is_cmd_execution);
585661

586662
is_prog_name = 0;
587663
zend_string_release(arg_str);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
GHSA-54hq-v5wp-fqgv - proc_open does not correctly escape args for bat files
3+
--SKIPIF--
4+
<?php
5+
if( substr(PHP_OS, 0, 3) != "WIN" )
6+
die('skip Run only on Windows');
7+
?>
8+
--FILE--
9+
<?php
10+
11+
$batch_file_content = <<<EOT
12+
@echo off
13+
powershell -Command "Write-Output '%1%'"
14+
EOT;
15+
$batch_file_path = __DIR__ . '/ghsa-54hq-v5wp-fqgv.bat';
16+
17+
file_put_contents($batch_file_path, $batch_file_content);
18+
19+
$descriptorspec = [STDIN, STDOUT, STDOUT];
20+
$proc = proc_open([$batch_file_path, "\"&notepad.exe"], $descriptorspec, $pipes);
21+
proc_close($proc);
22+
23+
?>
24+
--EXPECT--
25+
"&notepad.exe
26+
--CLEAN--
27+
<?php
28+
@unlink(__DIR__ . '/ghsa-54hq-v5wp-fqgv.bat');
29+
?>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
GHSA-54hq-v5wp-fqgv - proc_open does not correctly escape args for cmd files
3+
--SKIPIF--
4+
<?php
5+
if( substr(PHP_OS, 0, 3) != "WIN" )
6+
die('skip Run only on Windows');
7+
?>
8+
--FILE--
9+
<?php
10+
11+
$batch_file_content = <<<EOT
12+
@echo off
13+
powershell -Command "Write-Output '%1%'"
14+
EOT;
15+
$batch_file_path = __DIR__ . '/ghsa-54hq-v5wp-fqgv.cmd';
16+
17+
file_put_contents($batch_file_path, $batch_file_content);
18+
19+
$descriptorspec = [STDIN, STDOUT, STDOUT];
20+
$proc = proc_open([$batch_file_path, "\"&notepad<>^()!.exe"], $descriptorspec, $pipes);
21+
proc_close($proc);
22+
23+
?>
24+
--EXPECT--
25+
"&notepad<>^()!.exe
26+
--CLEAN--
27+
<?php
28+
@unlink(__DIR__ . '/ghsa-54hq-v5wp-fqgv.cmd');
29+
?>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
GHSA-54hq-v5wp-fqgv - proc_open does not correctly escape args for cmd executing batch files
3+
--SKIPIF--
4+
<?php
5+
if( substr(PHP_OS, 0, 3) != "WIN" )
6+
die('skip Run only on Windows');
7+
?>
8+
--FILE--
9+
<?php
10+
11+
$batch_file_content = <<<EOT
12+
@echo off
13+
powershell -Command "Write-Output '%1%'"
14+
EOT;
15+
$batch_file_path = __DIR__ . '/ghsa-54hq-v5wp-fqgv.bat';
16+
17+
file_put_contents($batch_file_path, $batch_file_content);
18+
19+
$descriptorspec = [STDIN, STDOUT, STDOUT];
20+
$proc = proc_open(["cmd.exe", "/c", $batch_file_path, "\"&notepad.exe"], $descriptorspec, $pipes);
21+
proc_close($proc);
22+
23+
?>
24+
--EXPECT--
25+
"&notepad.exe
26+
--CLEAN--
27+
<?php
28+
@unlink(__DIR__ . '/ghsa-54hq-v5wp-fqgv.bat');
29+
?>

0 commit comments

Comments
 (0)