Skip to content

Commit 27bb328

Browse files
laruencecmb69
authored andcommitted
Fixed bug #79029 (Use After Free's in XMLReader / XMLWriter).
We backport the fix PHP 7.3, since this branch is affected as well. (cherry picked from commit b5e0043) (cherry picked from commit e36daa6) (cherry picked from commit 2704ee6)
1 parent 37d11d1 commit 27bb328

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ PHP NEWS
1717
. Fixed bug #78923 (Artifacts when convoluting image with transparency).
1818
(wilson chen)
1919

20+
- Libxml:
21+
. Fixed bug #79029 (Use After Free's in XMLReader / XMLWriter). (Laruence)
22+
2023
- Pcntl:
2124
. Fixed bug #78402 (Converting null to string in error message is bad DX).
2225
(SATŌ Kentarō)

ext/libxml/libxml.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ static void *php_libxml_streams_IO_open_wrapper(const char *filename, const char
358358
context = php_stream_context_from_zval(Z_ISUNDEF(LIBXML(stream_context))? NULL : &LIBXML(stream_context), 0);
359359

360360
ret_val = php_stream_open_wrapper_ex(path_to_open, (char *)mode, REPORT_ERRORS, NULL, context);
361+
if (ret_val) {
362+
/* Prevent from closing this by fclose() */
363+
((php_stream*)ret_val)->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
364+
}
361365
if (isescaped) {
362366
xmlFree(resolved_path);
363367
}

ext/xmlwriter/php_xmlwriter.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ typedef int (*xmlwriter_read_int_t)(xmlTextWriterPtr writer);
9191
static void xmlwriter_free_resource_ptr(xmlwriter_object *intern)
9292
{
9393
if (intern) {
94-
if (intern->ptr) {
95-
xmlFreeTextWriter(intern->ptr);
96-
intern->ptr = NULL;
97-
}
98-
if (intern->output) {
99-
xmlBufferFree(intern->output);
100-
intern->output = NULL;
94+
if (EG(active)) {
95+
if (intern->ptr) {
96+
xmlFreeTextWriter(intern->ptr);
97+
intern->ptr = NULL;
98+
}
99+
if (intern->output) {
100+
xmlBufferFree(intern->output);
101+
intern->output = NULL;
102+
}
101103
}
102104
efree(intern);
103105
}

ext/xmlwriter/tests/bug79029.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
#79029 (Use After Free's in XMLReader / XMLWriter)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("xmlwriter")) print "skip xmlwriter extension not available";
6+
if (!extension_loaded("xmlreader")) print "skip xmlreader extension not available";
7+
?>
8+
--FILE--
9+
<?php
10+
$x = array( new XMLWriter() );
11+
$x[0]->openUri("bug79029_1.txt");
12+
$x[0]->startComment();
13+
14+
$x = new XMLWriter();
15+
$x->openUri("bug79029_2.txt");
16+
fclose(@end(get_resources()));
17+
18+
file_put_contents("bug79029_3.txt", "a");
19+
$x = new XMLReader();
20+
$x->open("bug79029_3.txt");
21+
fclose(@end(get_resources()));
22+
?>
23+
okey
24+
--CLEAN--
25+
<?php
26+
@unlink("bug79029_1.txt");
27+
@unlink("bug79029_2.txt");
28+
@unlink("bug79029_3.txt");
29+
?>
30+
--EXPECTF--
31+
Warning: fclose(): %d is not a valid stream resource in %sbug79029.php on line %d
32+
33+
Warning: fclose(): %d is not a valid stream resource in %sbug79029.php on line %d
34+
okey

0 commit comments

Comments
 (0)