Skip to content

Commit 9d72072

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: ext/soap: Fix integer overflow when decoding SOAP array indexes
2 parents a846c8f + f5db09d commit 9d72072

4 files changed

Lines changed: 168 additions & 23 deletions

File tree

ext/soap/php_encoding.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
+----------------------------------------------------------------------+
1717
*/
1818

19+
#include <limits.h>
1920
#include <time.h>
2021

2122
#include "php_soap.h"
@@ -2050,6 +2051,15 @@ static int calc_dimension_12(const char* str)
20502051
return i;
20512052
}
20522053

2054+
static void soap_array_position_add_digit(int *position, int digit)
2055+
{
2056+
if (*position > (INT_MAX - digit) / 10) {
2057+
soap_error0(E_ERROR, "Encoding: array index out of range");
2058+
}
2059+
2060+
*position = (*position * 10) + digit;
2061+
}
2062+
20532063
static int* get_position_12(int dimension, const char* str)
20542064
{
20552065
int *pos;
@@ -2070,7 +2080,7 @@ static int* get_position_12(int dimension, const char* str)
20702080
i++;
20712081
flag = 1;
20722082
}
2073-
pos[i] = (pos[i]*10)+(*str-'0');
2083+
soap_array_position_add_digit(&pos[i], *str - '0');
20742084
} else if (*str == '*') {
20752085
soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
20762086
} else {
@@ -2100,7 +2110,7 @@ static void get_position_ex(int dimension, const char* str, int** pos)
21002110
memset(*pos,0,sizeof(int)*dimension);
21012111
while (*str != ']' && *str != '\0' && i < dimension) {
21022112
if (*str >= '0' && *str <= '9') {
2103-
(*pos)[i] = ((*pos)[i]*10)+(*str-'0');
2113+
soap_array_position_add_digit(&(*pos)[i], *str - '0');
21042114
} else if (*str == ',') {
21052115
i++;
21062116
}
@@ -2693,16 +2703,20 @@ static zval *to_zval_array(zval *ret, encodeTypePtr type, xmlNodePtr data)
26932703
/* Increment position */
26942704
i = dimension;
26952705
while (i > 0) {
2696-
i--;
2697-
pos[i]++;
2698-
if (pos[i] >= dims[i]) {
2699-
if (i > 0) {
2700-
pos[i] = 0;
2701-
} else {
2702-
/* TODO: Array index overflow */
2703-
}
2704-
} else {
2705-
break;
2706+
i--;
2707+
if (pos[i] == INT_MAX) {
2708+
efree(dims);
2709+
efree(pos);
2710+
zval_ptr_dtor(ret);
2711+
ZVAL_UNDEF(ret);
2712+
soap_error0(E_ERROR, "Encoding: array index out of range");
2713+
}
2714+
pos[i]++;
2715+
if (pos[i] < dims[i]) {
2716+
break;
2717+
}
2718+
if (i > 0) {
2719+
pos[i] = 0;
27062720
}
27072721
}
27082722
}

ext/soap/php_packet_soap.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@
1818

1919
#include "php_soap.h"
2020

21+
static void master_to_zval_with_doc_cleanup(zval *ret, encodePtr encode, xmlNodePtr data, xmlDocPtr doc)
22+
{
23+
bool bailout = false;
24+
25+
ZVAL_UNDEF(ret);
26+
27+
/* SoapClient can turn decode errors into a bailout before parse_packet_soap() frees the response doc. */
28+
zend_try {
29+
master_to_zval(ret, encode, data);
30+
} zend_catch {
31+
bailout = true;
32+
} zend_end_try();
33+
34+
if (bailout) {
35+
if (!Z_ISUNDEF_P(ret)) {
36+
zval_ptr_dtor(ret);
37+
}
38+
xmlFreeDoc(doc);
39+
zend_bailout();
40+
}
41+
}
42+
2143
/* SOAP client calls this function to parse response from SOAP server */
2244
bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctionPtr fn, char *fn_name, zval *return_value, zval *soap_headers)
2345
{
@@ -192,22 +214,22 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
192214
tmp = get_node(fault->children, "faultstring");
193215
if (tmp != NULL && tmp->children != NULL) {
194216
zval zv;
195-
master_to_zval(&zv, get_conversion(IS_STRING), tmp);
217+
master_to_zval_with_doc_cleanup(&zv, get_conversion(IS_STRING), tmp, response);
196218
convert_to_string(&zv)
197219
faultstring = Z_STR(zv);
198220
}
199221

200222
tmp = get_node(fault->children, "faultactor");
201223
if (tmp != NULL && tmp->children != NULL) {
202224
zval zv;
203-
master_to_zval(&zv, get_conversion(IS_STRING), tmp);
225+
master_to_zval_with_doc_cleanup(&zv, get_conversion(IS_STRING), tmp, response);
204226
convert_to_string(&zv)
205227
faultactor = Z_STR(zv);
206228
}
207229

208230
tmp = get_node(fault->children, "detail");
209231
if (tmp != NULL) {
210-
master_to_zval(&details, NULL, tmp);
232+
master_to_zval_with_doc_cleanup(&details, NULL, tmp, response);
211233
}
212234
} else {
213235
tmp = get_node(fault->children, "Code");
@@ -223,7 +245,7 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
223245
tmp = get_node(tmp->children,"Text");
224246
if (tmp != NULL && tmp->children != NULL) {
225247
zval zv;
226-
master_to_zval(&zv, get_conversion(IS_STRING), tmp);
248+
master_to_zval_with_doc_cleanup(&zv, get_conversion(IS_STRING), tmp, response);
227249
convert_to_string(&zv)
228250
faultstring = Z_STR(zv);
229251

@@ -238,7 +260,7 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
238260

239261
tmp = get_node(fault->children,"Detail");
240262
if (tmp != NULL) {
241-
master_to_zval(&details, NULL, tmp);
263+
master_to_zval_with_doc_cleanup(&details, NULL, tmp, response);
242264
}
243265
}
244266
add_soap_fault(this_ptr, faultcode, faultstring ? ZSTR_VAL(faultstring) : NULL, faultactor ? ZSTR_VAL(faultactor) : NULL, &details, lang);
@@ -332,9 +354,9 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
332354
} else {
333355
/* Decoding value of parameter */
334356
if (param != NULL) {
335-
master_to_zval(&tmp, param->encode, val);
357+
master_to_zval_with_doc_cleanup(&tmp, param->encode, val, response);
336358
} else {
337-
master_to_zval(&tmp, NULL, val);
359+
master_to_zval_with_doc_cleanup(&tmp, NULL, val, response);
338360
}
339361
}
340362
add_assoc_zval(return_value, param->paramName, &tmp);
@@ -355,7 +377,7 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
355377
zval tmp;
356378
zval *arr;
357379

358-
master_to_zval(&tmp, NULL, val);
380+
master_to_zval_with_doc_cleanup(&tmp, NULL, val, response);
359381
if (val->name) {
360382
if ((arr = zend_hash_str_find(Z_ARRVAL_P(return_value), (char*)val->name, strlen((char*)val->name))) != NULL) {
361383
add_next_index_zval(arr, &tmp);
@@ -420,7 +442,7 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
420442
}
421443
smart_str_free(&key);
422444
}
423-
master_to_zval(&val, enc, trav);
445+
master_to_zval_with_doc_cleanup(&val, enc, trav, response);
424446
add_assoc_zval(soap_headers, (char*)trav->name, &val);
425447
}
426448
trav = trav->next;

ext/soap/soap.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,9 +2426,19 @@ static void do_soap_call(zend_execute_data *execute_data,
24262426
request = NULL;
24272427

24282428
if (ret && Z_TYPE(response) == IS_STRING) {
2429+
bool parse_bailout = false;
2430+
24292431
encode_reset_ns();
2430-
ret = parse_packet_soap(this_ptr, Z_STRVAL(response), Z_STRLEN(response), fn, NULL, return_value, output_headers);
2432+
zend_try {
2433+
ret = parse_packet_soap(this_ptr, Z_STRVAL(response), Z_STRLEN(response), fn, NULL, return_value, output_headers);
2434+
} zend_catch {
2435+
parse_bailout = true;
2436+
} zend_end_try();
24312437
encode_finish();
2438+
if (parse_bailout) {
2439+
zval_ptr_dtor(&response);
2440+
zend_bailout();
2441+
}
24322442
}
24332443

24342444
zval_ptr_dtor(&response);
@@ -2470,9 +2480,19 @@ static void do_soap_call(zend_execute_data *execute_data,
24702480
request = NULL;
24712481

24722482
if (ret && Z_TYPE(response) == IS_STRING) {
2483+
bool parse_bailout = false;
2484+
24732485
encode_reset_ns();
2474-
ret = parse_packet_soap(this_ptr, Z_STRVAL(response), Z_STRLEN(response), NULL, NULL, return_value, output_headers);
2486+
zend_try {
2487+
ret = parse_packet_soap(this_ptr, Z_STRVAL(response), Z_STRLEN(response), NULL, NULL, return_value, output_headers);
2488+
} zend_catch {
2489+
parse_bailout = true;
2490+
} zend_end_try();
24752491
encode_finish();
2492+
if (parse_bailout) {
2493+
zval_ptr_dtor(&response);
2494+
zend_bailout();
2495+
}
24762496
}
24772497

24782498
zval_ptr_dtor(&response);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
--TEST--
2+
SOAP array index overflow is rejected
3+
--EXTENSIONS--
4+
soap
5+
--FILE--
6+
<?php
7+
class TestSoapClient extends SoapClient {
8+
public string $response;
9+
10+
public function __doRequest($request, $location, $action, $version, $one_way = false, ?string $uriParserClass = null): string {
11+
return $this->response;
12+
}
13+
}
14+
15+
function soap_response(string $attributes, string $itemAttributes = ''): string {
16+
return <<<XML
17+
<?xml version="1.0" encoding="UTF-8"?>
18+
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
19+
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
20+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xmlns:ns1="http://example.org/"
23+
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
24+
<SOAP-ENV:Body>
25+
<ns1:testResponse>
26+
<return $attributes>
27+
<item xsi:type="xsd:string" $itemAttributes>value</item>
28+
</return>
29+
</ns1:testResponse>
30+
</SOAP-ENV:Body>
31+
</SOAP-ENV:Envelope>
32+
XML;
33+
}
34+
35+
function test_overflow(string $name, string $response): void {
36+
$client = new TestSoapClient(NULL, [
37+
'location' => 'test://',
38+
'uri' => 'http://example.org/',
39+
'exceptions' => true,
40+
]);
41+
$client->response = $response;
42+
43+
try {
44+
$client->test();
45+
echo "$name: no fault\n";
46+
} catch (SoapFault $e) {
47+
echo "$name: $e->faultstring\n";
48+
}
49+
}
50+
51+
function test_boundary_position(): void {
52+
$client = new TestSoapClient(NULL, [
53+
'location' => 'test://',
54+
'uri' => 'http://example.org/',
55+
'exceptions' => true,
56+
]);
57+
$client->response = soap_response(
58+
'SOAP-ENC:arrayType="xsd:string[1]" xsi:type="SOAP-ENC:Array"',
59+
'SOAP-ENC:position="[2147483646]"'
60+
);
61+
62+
var_dump($client->test());
63+
}
64+
65+
test_overflow(
66+
'arrayType',
67+
soap_response('SOAP-ENC:arrayType="xsd:string[2147483648]" xsi:type="SOAP-ENC:Array"')
68+
);
69+
70+
test_overflow(
71+
'offset',
72+
soap_response('SOAP-ENC:arrayType="xsd:string[1]" SOAP-ENC:offset="[2147483648]" xsi:type="SOAP-ENC:Array"')
73+
);
74+
75+
test_overflow(
76+
'position',
77+
soap_response('SOAP-ENC:arrayType="xsd:string[1]" xsi:type="SOAP-ENC:Array"', 'SOAP-ENC:position="[2147483647]"')
78+
);
79+
80+
test_boundary_position();
81+
?>
82+
--EXPECT--
83+
arrayType: SOAP-ERROR: Encoding: array index out of range
84+
offset: SOAP-ERROR: Encoding: array index out of range
85+
position: SOAP-ERROR: Encoding: array index out of range
86+
array(1) {
87+
[2147483646]=>
88+
string(5) "value"
89+
}

0 commit comments

Comments
 (0)