Skip to content

Commit 4c027ea

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: ext/soap: Fix integer overflow when decoding SOAP array indexes
2 parents 7f7238e + 9d72072 commit 4c027ea

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
@@ -14,6 +14,7 @@
1414
+----------------------------------------------------------------------+
1515
*/
1616

17+
#include <limits.h>
1718
#include <time.h>
1819

1920
#include "php_soap.h"
@@ -2048,6 +2049,15 @@ static int calc_dimension_12(const char* str)
20482049
return i;
20492050
}
20502051

2052+
static void soap_array_position_add_digit(int *position, int digit)
2053+
{
2054+
if (*position > (INT_MAX - digit) / 10) {
2055+
soap_error0(E_ERROR, "Encoding: array index out of range");
2056+
}
2057+
2058+
*position = (*position * 10) + digit;
2059+
}
2060+
20512061
static int* get_position_12(int dimension, const char* str)
20522062
{
20532063
int *pos;
@@ -2068,7 +2078,7 @@ static int* get_position_12(int dimension, const char* str)
20682078
i++;
20692079
flag = 1;
20702080
}
2071-
pos[i] = (pos[i]*10)+(*str-'0');
2081+
soap_array_position_add_digit(&pos[i], *str - '0');
20722082
} else if (*str == '*') {
20732083
soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
20742084
} else {
@@ -2098,7 +2108,7 @@ static void get_position_ex(int dimension, const char* str, int** pos)
20982108
memset(*pos,0,sizeof(int)*dimension);
20992109
while (*str != ']' && *str != '\0' && i < dimension) {
21002110
if (*str >= '0' && *str <= '9') {
2101-
(*pos)[i] = ((*pos)[i]*10)+(*str-'0');
2111+
soap_array_position_add_digit(&(*pos)[i], *str - '0');
21022112
} else if (*str == ',') {
21032113
i++;
21042114
}
@@ -2685,16 +2695,20 @@ static zval *to_zval_array(zval *ret, encodeTypePtr type, xmlNodePtr data)
26852695
/* Increment position */
26862696
i = dimension;
26872697
while (i > 0) {
2688-
i--;
2689-
pos[i]++;
2690-
if (pos[i] >= dims[i]) {
2691-
if (i > 0) {
2692-
pos[i] = 0;
2693-
} else {
2694-
/* TODO: Array index overflow */
2695-
}
2696-
} else {
2697-
break;
2698+
i--;
2699+
if (pos[i] == INT_MAX) {
2700+
efree(dims);
2701+
efree(pos);
2702+
zval_ptr_dtor(ret);
2703+
ZVAL_UNDEF(ret);
2704+
soap_error0(E_ERROR, "Encoding: array index out of range");
2705+
}
2706+
pos[i]++;
2707+
if (pos[i] < dims[i]) {
2708+
break;
2709+
}
2710+
if (i > 0) {
2711+
pos[i] = 0;
26982712
}
26992713
}
27002714
}

ext/soap/php_packet_soap.c

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

1717
#include "php_soap.h"
1818

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

198220
tmp = get_node(fault->children, "faultactor");
199221
if (tmp != NULL && tmp->children != NULL) {
200222
zval zv;
201-
master_to_zval(&zv, get_conversion(IS_STRING), tmp);
223+
master_to_zval_with_doc_cleanup(&zv, get_conversion(IS_STRING), tmp, response);
202224
convert_to_string(&zv)
203225
faultactor = Z_STR(zv);
204226
}
205227

206228
tmp = get_node(fault->children, "detail");
207229
if (tmp != NULL) {
208-
master_to_zval(&details, NULL, tmp);
230+
master_to_zval_with_doc_cleanup(&details, NULL, tmp, response);
209231
}
210232
} else {
211233
tmp = get_node(fault->children, "Code");
@@ -221,7 +243,7 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
221243
tmp = get_node(tmp->children,"Text");
222244
if (tmp != NULL && tmp->children != NULL) {
223245
zval zv;
224-
master_to_zval(&zv, get_conversion(IS_STRING), tmp);
246+
master_to_zval_with_doc_cleanup(&zv, get_conversion(IS_STRING), tmp, response);
225247
convert_to_string(&zv)
226248
faultstring = Z_STR(zv);
227249

@@ -236,7 +258,7 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
236258

237259
tmp = get_node(fault->children,"Detail");
238260
if (tmp != NULL) {
239-
master_to_zval(&details, NULL, tmp);
261+
master_to_zval_with_doc_cleanup(&details, NULL, tmp, response);
240262
}
241263
}
242264
add_soap_fault(this_ptr, faultcode, faultstring ? ZSTR_VAL(faultstring) : NULL, faultactor ? ZSTR_VAL(faultactor) : NULL, &details, lang);
@@ -330,9 +352,9 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
330352
} else {
331353
/* Decoding value of parameter */
332354
if (param != NULL) {
333-
master_to_zval(&tmp, param->encode, val);
355+
master_to_zval_with_doc_cleanup(&tmp, param->encode, val, response);
334356
} else {
335-
master_to_zval(&tmp, NULL, val);
357+
master_to_zval_with_doc_cleanup(&tmp, NULL, val, response);
336358
}
337359
}
338360
add_assoc_zval(return_value, param->paramName, &tmp);
@@ -353,7 +375,7 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
353375
zval tmp;
354376
zval *arr;
355377

356-
master_to_zval(&tmp, NULL, val);
378+
master_to_zval_with_doc_cleanup(&tmp, NULL, val, response);
357379
if (val->name) {
358380
if ((arr = zend_hash_str_find(Z_ARRVAL_P(return_value), (char*)val->name, strlen((char*)val->name))) != NULL) {
359381
add_next_index_zval(arr, &tmp);
@@ -418,7 +440,7 @@ bool parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctio
418440
}
419441
smart_str_free(&key);
420442
}
421-
master_to_zval(&val, enc, trav);
443+
master_to_zval_with_doc_cleanup(&val, enc, trav, response);
422444
add_assoc_zval(soap_headers, (char*)trav->name, &val);
423445
}
424446
trav = trav->next;

ext/soap/soap.c

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

24512451
if (ret && Z_TYPE(response) == IS_STRING) {
2452+
bool parse_bailout = false;
2453+
24522454
encode_reset_ns();
2453-
ret = parse_packet_soap(this_ptr, Z_STRVAL(response), Z_STRLEN(response), fn, NULL, return_value, output_headers);
2455+
zend_try {
2456+
ret = parse_packet_soap(this_ptr, Z_STRVAL(response), Z_STRLEN(response), fn, NULL, return_value, output_headers);
2457+
} zend_catch {
2458+
parse_bailout = true;
2459+
} zend_end_try();
24542460
encode_finish();
2461+
if (parse_bailout) {
2462+
zval_ptr_dtor(&response);
2463+
zend_bailout();
2464+
}
24552465
}
24562466

24572467
zval_ptr_dtor(&response);
@@ -2493,9 +2503,19 @@ static void do_soap_call(zend_execute_data *execute_data,
24932503
request = NULL;
24942504

24952505
if (ret && Z_TYPE(response) == IS_STRING) {
2506+
bool parse_bailout = false;
2507+
24962508
encode_reset_ns();
2497-
ret = parse_packet_soap(this_ptr, Z_STRVAL(response), Z_STRLEN(response), NULL, NULL, return_value, output_headers);
2509+
zend_try {
2510+
ret = parse_packet_soap(this_ptr, Z_STRVAL(response), Z_STRLEN(response), NULL, NULL, return_value, output_headers);
2511+
} zend_catch {
2512+
parse_bailout = true;
2513+
} zend_end_try();
24982514
encode_finish();
2515+
if (parse_bailout) {
2516+
zval_ptr_dtor(&response);
2517+
zend_bailout();
2518+
}
24992519
}
25002520

25012521
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)