Skip to content

Commit 2a74cb4

Browse files
committed
Merge branch 'sec53' into sec54
* sec53: fix bug #61367 - open_basedir bypass using libxml RSHUTDOWN
2 parents b08b7fe + 167e2fd commit 2a74cb4

File tree

4 files changed

+115
-5
lines changed

4 files changed

+115
-5
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ PHP NEWS
4848
- mbstring:
4949
. MFH mb_ereg_replace_callback() for security enhancements. (Rui)
5050

51+
- Libxml:
52+
. Fixed bug #61367 (open_basedir bypass using libxml RSHUTDOWN).
53+
(Tim Starling)
54+
5155
- mysqli
5256
. Fixed bug #61003 (mysql_stat() require a valid connection). (Johannes).
5357

ext/libxml/libxml.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ ZEND_GET_MODULE(libxml)
8686
static PHP_MINIT_FUNCTION(libxml);
8787
static PHP_RINIT_FUNCTION(libxml);
8888
static PHP_MSHUTDOWN_FUNCTION(libxml);
89-
static PHP_RSHUTDOWN_FUNCTION(libxml);
9089
static PHP_MINFO_FUNCTION(libxml);
90+
static int php_libxml_post_deactivate();
9191

9292
/* }}} */
9393

@@ -137,13 +137,13 @@ zend_module_entry libxml_module_entry = {
137137
PHP_MINIT(libxml), /* extension-wide startup function */
138138
PHP_MSHUTDOWN(libxml), /* extension-wide shutdown function */
139139
PHP_RINIT(libxml), /* per-request startup function */
140-
PHP_RSHUTDOWN(libxml), /* per-request shutdown function */
140+
NULL, /* per-request shutdown function */
141141
PHP_MINFO(libxml), /* information function */
142142
NO_VERSION_YET,
143143
PHP_MODULE_GLOBALS(libxml), /* globals descriptor */
144144
PHP_GINIT(libxml), /* globals ctor */
145145
NULL, /* globals dtor */
146-
NULL, /* post deactivate */
146+
php_libxml_post_deactivate, /* post deactivate */
147147
STANDARD_MODULE_PROPERTIES_EX
148148
};
149149

@@ -861,9 +861,9 @@ static PHP_MSHUTDOWN_FUNCTION(libxml)
861861
return SUCCESS;
862862
}
863863

864-
865-
static PHP_RSHUTDOWN_FUNCTION(libxml)
864+
static int php_libxml_post_deactivate()
866865
{
866+
TSRMLS_FETCH();
867867
/* reset libxml generic error handling */
868868
if (_php_libxml_per_request_initialization) {
869869
xmlSetGenericErrorFunc(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)

0 commit comments

Comments
 (0)