Skip to content

Commit 6cc4ae1

Browse files
committed
Fix GH-18640: heap-use-after-free ext/soap/php_encoding.c:299:32 in soap_check_zval_ref
For attributes, relying on the ref_map doesn't make sense the first place as you can't really refer to attributes from attributes. The code therefore assumes that the node is unique, which is broken. Closes GH-19181.
1 parent 3128693 commit 6cc4ae1

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ PHP NEWS
2222
return value check). (nielsdos, botovq)
2323
. Fix error return check of EVP_CIPHER_CTX_ctrl(). (nielsdos)
2424

25+
- SOAP:
26+
. Fixed bug GH-18640 (heap-use-after-free ext/soap/php_encoding.c:299:32
27+
in soap_check_zval_ref). (nielsdos)
28+
2529
- Sockets:
2630
. Fix some potential crashes on incorrect argument value. (nielsdos)
2731

ext/soap/php_encoding.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,11 @@ static xmlNodePtr to_xml_object(encodeTypePtr type, zval *data, int style, xmlNo
19361936
sdlAttributePtr attr;
19371937
zval *zattr, rv;
19381938

1939+
/* Attributes can't refer to other attributes as there's nothing to attach the href to. */
1940+
HashTable **ref_map = &SOAP_GLOBAL(ref_map);
1941+
HashTable *old_ref_map = *ref_map;
1942+
*ref_map = NULL;
1943+
19391944
ZEND_HASH_FOREACH_PTR(sdlType->attributes, attr) {
19401945
if (attr->name) {
19411946
zattr = get_zval_property(data, attr->name, &rv);
@@ -1965,6 +1970,8 @@ static xmlNodePtr to_xml_object(encodeTypePtr type, zval *data, int style, xmlNo
19651970
}
19661971
}
19671972
} ZEND_HASH_FOREACH_END();
1973+
1974+
*ref_map = old_ref_map;
19681975
}
19691976
}
19701977
if (style == SOAP_ENCODED) {
@@ -3034,6 +3041,12 @@ static xmlNodePtr to_xml_list(encodeTypePtr enc, zval *data, int style, xmlNodeP
30343041
ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
30353042
xmlAddChild(parent, ret);
30363043
FIND_ZVAL_NULL(data, ret, style);
3044+
3045+
/* Literals are unique and can't refer to other references via attributes. */
3046+
HashTable **ref_map = &SOAP_GLOBAL(ref_map);
3047+
HashTable *old_ref_map = *ref_map;
3048+
*ref_map = NULL;
3049+
30373050
if (Z_TYPE_P(data) == IS_ARRAY) {
30383051
zval *tmp;
30393052
smart_str list = {0};
@@ -3108,6 +3121,7 @@ static xmlNodePtr to_xml_list(encodeTypePtr enc, zval *data, int style, xmlNodeP
31083121
zval_ptr_dtor_str(&tmp);
31093122
}
31103123
}
3124+
*ref_map = old_ref_map;
31113125
return ret;
31123126
}
31133127

ext/soap/tests/bugs/gh18640.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST---
2+
GH-18640 (heap-use-after-free ext/soap/php_encoding.c:299:32 in soap_check_zval_ref)
3+
--EXTENSIONS--
4+
soap
5+
--CREDITS--
6+
YuanchengJiang
7+
--FILE--
8+
<?php
9+
$wsdl = __DIR__."/bug35142.wsdl";
10+
11+
class TestSoapClient extends SoapClient {
12+
function __doRequest($request, $location, $action, $version, $one_way = 0): ?string {
13+
var_dump($request);
14+
return '';
15+
}
16+
}
17+
18+
$soapClient = new TestSoapClient($wsdl, ['trace' => 1, 'classmap' => ['logOnEvent' => 'LogOnEvent', 'events' => 'IVREvents']]);
19+
$timestamp = new LogOnEvent(); // Bogus!
20+
$logOffEvents[] = new LogOffEvent($timestamp);
21+
$logOffEvents[] = new LogOffEvent($timestamp);
22+
$ivrEvents = new IVREvents($logOffEvents);
23+
$result = $soapClient->PostEvents($ivrEvents);
24+
25+
class LogOffEvent {
26+
function __construct(public $timestamp) {
27+
$this->timestamp = $timestamp;
28+
}
29+
}
30+
31+
class LogOnEvent {
32+
}
33+
34+
class IVREvents {
35+
function __construct(public $logOffEvent) {
36+
}
37+
}
38+
?>
39+
--EXPECT--
40+
string(359) "<?xml version="1.0" encoding="UTF-8"?>
41+
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://testurl/Events" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://testurl/Message"><SOAP-ENV:Body><ns2:ivrEvents><ns2:logOffEvent/><ns2:logOffEvent/></ns2:ivrEvents></SOAP-ENV:Body></SOAP-ENV:Envelope>
42+
"

0 commit comments

Comments
 (0)