Skip to content

Commit 7da6c0f

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix segfault and assertion failure with refcounted props and arrays Fix incorrect uri check in SOAP caching Fix bug #66150: SOAP WSDL cache race condition causes Segmentation Fault
2 parents 8347740 + 1b16646 commit 7da6c0f

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ PHP NEWS
3333
- SOAP:
3434
. Fixed bug GH-12392 (Segmentation fault on SoapClient::__getTypes).
3535
(nielsdos)
36+
. Fixed bug #66150 (SOAP WSDL cache race condition causes Segmentation
37+
Fault). (nielsdos)
38+
. Fixed bug #67617 (SOAP leaves incomplete cache file on ENOSPC). (nielsdos)
39+
. Fix incorrect uri check in SOAP caching. (nielsdos)
40+
. Fix segfault and assertion failure with refcounted props and arrays.
41+
(nielsdos)
3642

3743
- XSL:
3844
. Add missing module dependency. (nielsdos)

ext/soap/php_encoding.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,10 +1561,12 @@ static zval *to_zval_object_ex(zval *ret, encodeTypePtr type, xmlNodePtr data, z
15611561
if (Z_TYPE_P(prop) != IS_ARRAY) {
15621562
/* Convert into array */
15631563
array_init(&arr);
1564-
Z_ADDREF_P(prop);
1564+
Z_TRY_ADDREF_P(prop);
15651565
add_next_index_zval(&arr, prop);
15661566
set_zval_property(ret, (char*)trav->name, &arr);
15671567
prop = &arr;
1568+
} else {
1569+
SEPARATE_ARRAY(prop);
15681570
}
15691571
/* Add array element */
15701572
add_next_index_zval(prop, &tmpVal);

ext/soap/php_sdl.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "ext/standard/md5.h"
2424
#include "zend_virtual_cwd.h"
25+
#include "main/php_open_temporary_file.h"
2526

2627
#include <sys/types.h>
2728
#include <sys/stat.h>
@@ -1536,7 +1537,7 @@ static HashTable* sdl_deserialize_parameters(encodePtr *encoders, sdlTypePtr *ty
15361537
return ht;
15371538
}
15381539

1539-
static sdlPtr get_sdl_from_cache(const char *fn, const char *uri, time_t t, time_t *cached)
1540+
static sdlPtr get_sdl_from_cache(const char *fn, const char *uri, size_t uri_len, time_t t, time_t *cached)
15401541
{
15411542
sdlPtr sdl;
15421543
time_t old_t;
@@ -1583,7 +1584,7 @@ static sdlPtr get_sdl_from_cache(const char *fn, const char *uri, time_t t, time
15831584
*cached = old_t;
15841585

15851586
WSDL_CACHE_GET_INT(i, &in);
1586-
if (i == 0 && strncmp(in, uri, i) != 0) {
1587+
if (i != uri_len || strncmp(in, uri, i) != 0) {
15871588
unlink(fn);
15881589
efree(buf);
15891590
return NULL;
@@ -2119,7 +2120,10 @@ static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr s
21192120
HashTable tmp_bindings;
21202121
HashTable tmp_functions;
21212122

2122-
f = open(fn,O_CREAT|O_WRONLY|O_EXCL|O_BINARY,S_IREAD|S_IWRITE);
2123+
/* To avoid race conditions, we first create a temporary file and then rename it atomically
2124+
* at the end of the function. (see bug #66150) */
2125+
zend_string *temp_file_path;
2126+
f = php_open_temporary_fd_ex(SOAP_GLOBAL(cache_dir), "tmp.wsdl.", &temp_file_path, PHP_TMP_FILE_SILENT);
21232127

21242128
if (f < 0) {return;}
21252129

@@ -2371,13 +2375,21 @@ static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr s
23712375
} ZEND_HASH_FOREACH_END();
23722376
}
23732377

2374-
php_ignore_value(write(f, ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)));
2378+
bool valid_file = write(f, ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)) == ZSTR_LEN(buf.s);
23752379
close(f);
2380+
2381+
/* Make sure that incomplete files (e.g. due to disk space issues, see bug #66150) are not utilised. */
2382+
if (valid_file) {
2383+
/* This is allowed to fail, this means that another process was raced to create the file. */
2384+
(void) VCWD_RENAME(ZSTR_VAL(temp_file_path), fn);
2385+
}
2386+
23762387
smart_str_free(&buf);
23772388
zend_hash_destroy(&tmp_functions);
23782389
zend_hash_destroy(&tmp_bindings);
23792390
zend_hash_destroy(&tmp_encoders);
23802391
zend_hash_destroy(&tmp_types);
2392+
zend_string_release_ex(temp_file_path, false);
23812393
}
23822394

23832395

@@ -3232,7 +3244,7 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
32323244
}
32333245
memcpy(key+len,md5str,sizeof(md5str));
32343246

3235-
if ((sdl = get_sdl_from_cache(key, uri, t-SOAP_GLOBAL(cache_ttl), &cached)) != NULL) {
3247+
if ((sdl = get_sdl_from_cache(key, uri, uri_len, t-SOAP_GLOBAL(cache_ttl), &cached)) != NULL) {
32363248
t = cached;
32373249
efree(key);
32383250
goto cache_in_memory;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
Segfault and assertion failure with refcounted props and arrays
3+
--INI--
4+
soap.wsdl_cache_enabled=0
5+
--EXTENSIONS--
6+
soap
7+
--FILE--
8+
<?php
9+
class TestSoapClient extends SoapClient {
10+
function __doRequest($request, $location, $action, $version, $one_way = false): ?string {
11+
return <<<EOF
12+
<?xml version="1.0" encoding="UTF-8"?>
13+
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.nothing.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body>
14+
<ns1:dotest2Response><res xsi:type="SOAP-ENC:Struct">
15+
<a xsi:type="xsd:string">Hello</a>
16+
<b xsi:type="xsd:string">World</b>
17+
</res>
18+
</ns1:dotest2Response></SOAP-ENV:Body></SOAP-ENV:Envelope>
19+
EOF;
20+
}
21+
}
22+
23+
trait A {
24+
public $a = [self::class . 'a'];
25+
public $b = self::class . 'b';
26+
}
27+
28+
class DummyClass {
29+
use A;
30+
}
31+
32+
$client = new TestSoapClient(__DIR__."/../classmap.wsdl", ['classmap' => ['Struct' => 'DummyClass']]);
33+
var_dump($client->dotest2("???"));
34+
?>
35+
--EXPECT--
36+
object(DummyClass)#2 (2) {
37+
["a"]=>
38+
array(2) {
39+
[0]=>
40+
string(11) "DummyClassa"
41+
[1]=>
42+
string(5) "Hello"
43+
}
44+
["b"]=>
45+
array(2) {
46+
[0]=>
47+
string(11) "DummyClassb"
48+
[1]=>
49+
string(5) "World"
50+
}
51+
}

0 commit comments

Comments
 (0)