Skip to content

Commit 21fa963

Browse files
committed
Merge remote branch 'security/PHP-5.3' into PHP-5.3
* security/PHP-5.3: fix bug #61367 - open_basedir bypass using libxml RSHUTDOWN open_basedir check for linkinfo NEWS entry for readline fix Add open_basedir checks to readline_write_history and readline_read_history
2 parents 67bf07f + 167e2fd commit 21fa963

File tree

6 files changed

+137
-6
lines changed

6 files changed

+137
-6
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ PHP NEWS
5454
- Firebird Database extension (ibase):
5555
. Fixed bug #60802 (ibase_trans() gives segfault when passing params).
5656

57+
- Libxml:
58+
. Fixed bug #61367 (open_basedir bypass using libxml RSHUTDOWN).
59+
(Tim Starling)
60+
5761
- mysqli
5862
. Fixed bug #61003 (mysql_stat() require a valid connection). (Johannes).
5963

@@ -83,6 +87,8 @@ PHP NEWS
8387
- Readline:
8488
. Fixed bug #61088 (Memory leak in readline_callback_handler_install).
8589
(Nikic, Laruence)
90+
. Add open_basedir checks to readline_write_history and readline_read_history.
91+
(Rasmus, reported by Mateusz Goik)
8692

8793
- Reflection:
8894
. Fixed bug #61388 (ReflectionObject:getProperties() issues invalid reads

ext/libxml/libxml.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ ZEND_GET_MODULE(libxml)
8282
static PHP_MINIT_FUNCTION(libxml);
8383
static PHP_RINIT_FUNCTION(libxml);
8484
static PHP_MSHUTDOWN_FUNCTION(libxml);
85-
static PHP_RSHUTDOWN_FUNCTION(libxml);
8685
static PHP_MINFO_FUNCTION(libxml);
86+
static int php_libxml_post_deactivate();
8787

8888
/* }}} */
8989

@@ -129,13 +129,13 @@ zend_module_entry libxml_module_entry = {
129129
PHP_MINIT(libxml), /* extension-wide startup function */
130130
PHP_MSHUTDOWN(libxml), /* extension-wide shutdown function */
131131
PHP_RINIT(libxml), /* per-request startup function */
132-
PHP_RSHUTDOWN(libxml), /* per-request shutdown function */
132+
NULL, /* per-request shutdown function */
133133
PHP_MINFO(libxml), /* information function */
134134
NO_VERSION_YET,
135135
PHP_MODULE_GLOBALS(libxml), /* globals descriptor */
136136
PHP_GINIT(libxml), /* globals ctor */
137137
NULL, /* globals dtor */
138-
NULL, /* post deactivate */
138+
php_libxml_post_deactivate, /* post deactivate */
139139
STANDARD_MODULE_PROPERTIES_EX
140140
};
141141

@@ -655,9 +655,9 @@ static PHP_MSHUTDOWN_FUNCTION(libxml)
655655
return SUCCESS;
656656
}
657657

658-
659-
static PHP_RSHUTDOWN_FUNCTION(libxml)
658+
static int php_libxml_post_deactivate()
660659
{
660+
TSRMLS_FETCH();
661661
/* reset libxml generic error handling */
662662
xmlSetGenericErrorFunc(NULL, NULL);
663663
xmlSetStructuredErrorFunc(NULL, NULL);

ext/libxml/tests/bug61367-read.phpt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--TEST--
2+
Bug #61367: open_basedir bypass in libxml RSHUTDOWN: read test
3+
--SKIPIF--
4+
<?php if(!extension_loaded('dom')) echo 'skip'; ?>
5+
--INI--
6+
open_basedir=.
7+
; Suppress spurious "Trying to get property of non-object" notices
8+
error_reporting=E_ALL & ~E_NOTICE
9+
--FILE--
10+
<?php
11+
12+
class StreamExploiter {
13+
public function stream_close ( ) {
14+
$doc = new DOMDocument;
15+
$doc->resolveExternals = true;
16+
$doc->substituteEntities = true;
17+
$dir = htmlspecialchars(dirname(getcwd()));
18+
$doc->loadXML( <<<XML
19+
<!DOCTYPE doc [
20+
<!ENTITY file SYSTEM "file:///$dir/bad">
21+
]>
22+
<doc>&file;</doc>
23+
XML
24+
);
25+
print $doc->documentElement->firstChild->nodeValue;
26+
}
27+
28+
public function stream_open ( $path , $mode , $options , &$opened_path ) {
29+
return true;
30+
}
31+
}
32+
33+
var_dump(mkdir('test_bug_61367'));
34+
var_dump(mkdir('test_bug_61367/base'));
35+
var_dump(file_put_contents('test_bug_61367/bad', 'blah'));
36+
var_dump(chdir('test_bug_61367/base'));
37+
38+
stream_wrapper_register( 'exploit', 'StreamExploiter' );
39+
$s = fopen( 'exploit://', 'r' );
40+
41+
?>
42+
--CLEAN--
43+
<?php
44+
unlink('test_bug_61367/bad');
45+
rmdir('test_bug_61367/base');
46+
rmdir('test_bug_61367');
47+
?>
48+
--EXPECTF--
49+
bool(true)
50+
bool(true)
51+
int(4)
52+
bool(true)
53+
54+
Warning: DOMDocument::loadXML(): I/O warning : failed to load external entity "file:///%s/test_bug_61367/bad" in %s on line %d
55+
56+
Warning: DOMDocument::loadXML(): Failure to process entity file in Entity, line: 4 in %s on line %d
57+
58+
Warning: DOMDocument::loadXML(): Entity 'file' not defined in Entity, line: 4 in %s on line %d

ext/libxml/tests/bug61367-write.phpt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
Bug #61367: open_basedir bypass in libxml RSHUTDOWN: write test
3+
--SKIPIF--
4+
<?php if(!extension_loaded('dom')) echo 'skip'; ?>
5+
--INI--
6+
open_basedir=.
7+
; Suppress spurious "Trying to get property of non-object" notices
8+
error_reporting=E_ALL & ~E_NOTICE
9+
--FILE--
10+
<?php
11+
12+
class StreamExploiter {
13+
public function stream_close ( ) {
14+
$doc = new DOMDocument;
15+
$doc->appendChild($doc->createTextNode('hello'));
16+
var_dump($doc->save(dirname(getcwd()) . '/bad'));
17+
}
18+
19+
public function stream_open ( $path , $mode , $options , &$opened_path ) {
20+
return true;
21+
}
22+
}
23+
24+
var_dump(mkdir('test_bug_61367'));
25+
var_dump(mkdir('test_bug_61367/base'));
26+
var_dump(file_put_contents('test_bug_61367/bad', 'blah'));
27+
var_dump(chdir('test_bug_61367/base'));
28+
29+
stream_wrapper_register( 'exploit', 'StreamExploiter' );
30+
$s = fopen( 'exploit://', 'r' );
31+
32+
?>
33+
--CLEAN--
34+
<?php
35+
@unlink('test_bug_61367/bad');
36+
rmdir('test_bug_61367/base');
37+
rmdir('test_bug_61367');
38+
?>
39+
--EXPECTF--
40+
bool(true)
41+
bool(true)
42+
int(4)
43+
bool(true)
44+
45+
Warning: DOMDocument::save(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
46+
47+
Warning: DOMDocument::save(%s): failed to open stream: Operation not permitted in %s on line %d
48+
bool(false)

ext/readline/readline.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ PHP_FUNCTION(readline_read_history)
369369
return;
370370
}
371371

372+
if (php_check_open_basedir(arg TSRMLS_CC)) {
373+
RETURN_FALSE;
374+
}
375+
372376
/* XXX from & to NYI */
373377
if (read_history(arg)) {
374378
RETURN_FALSE;
@@ -389,6 +393,10 @@ PHP_FUNCTION(readline_write_history)
389393
return;
390394
}
391395

396+
if (php_check_open_basedir(arg TSRMLS_CC)) {
397+
RETURN_FALSE;
398+
}
399+
392400
if (write_history(arg)) {
393401
RETURN_FALSE;
394402
} else {

ext/standard/link.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,31 @@ PHP_FUNCTION(readlink)
9494
PHP_FUNCTION(linkinfo)
9595
{
9696
char *link;
97-
int link_len;
97+
char *dirname;
98+
int link_len, dir_len;
9899
struct stat sb;
99100
int ret;
100101

101102
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &link, &link_len) == FAILURE) {
102103
return;
103104
}
104105

106+
dirname = estrndup(link, link_len);
107+
dir_len = php_dirname(dirname, link_len);
108+
109+
if (php_check_open_basedir(dirname TSRMLS_CC)) {
110+
efree(dirname);
111+
RETURN_FALSE;
112+
}
113+
105114
ret = VCWD_LSTAT(link, &sb);
106115
if (ret == -1) {
107116
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
117+
efree(dirname);
108118
RETURN_LONG(-1L);
109119
}
110120

121+
efree(dirname);
111122
RETURN_LONG((long) sb.st_dev);
112123
}
113124
/* }}} */

0 commit comments

Comments
 (0)