Skip to content

Commit a8722f5

Browse files
author
Yasuo Ohgaki
committed
Add NULL byte protection to exec, system and passthru
1 parent 5e3f0f5 commit a8722f5

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

ext/standard/exec.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
188188
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute a blank command");
189189
RETURN_FALSE;
190190
}
191+
if (strlen(cmd) != cmd_len) {
192+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "NULL byte detected. Possible attack");
193+
RETURN_FALSE;
194+
}
191195

192196
if (!ret_array) {
193197
ret = php_exec(mode, cmd, NULL, return_value TSRMLS_CC);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
exec, system, passthru — Basic command execution functions
3+
--SKIPIF--
4+
<?php
5+
// If this does not work for Windows, please uncomment or fix test
6+
// if(substr(PHP_OS, 0, 3) == "WIN") die("skip not for Windows");
7+
?>
8+
--FILE--
9+
<?php
10+
$cmd = "echo abc\n\0command";
11+
var_dump(exec($cmd, $output));
12+
var_dump($output);
13+
var_dump(system($cmd));
14+
var_dump(passthru($cmd));
15+
?>
16+
--EXPECTF--
17+
Warning: exec(): NULL byte detected. Possible attack in %s on line %d
18+
bool(false)
19+
NULL
20+
21+
Warning: system(): NULL byte detected. Possible attack in %s on line %d
22+
bool(false)
23+
24+
Warning: passthru(): NULL byte detected. Possible attack in %s on line %d
25+
bool(false)

0 commit comments

Comments
 (0)