Skip to content

Commit 43e6316

Browse files
committedOct 19, 2023
Fix bug #66150: SOAP WSDL cache race condition causes Segmentation Fault
When we have two processes both trying to cache a WSDL, they might start writing the data to the same temporary file, causing file corruption due to the race condition. Fix this by creating a temporary file first, and then moving it to the final location. If moving fails then we know another process finished caching first. This also fixes #67617 as a consequence of its implementation. Closes GH-12469.
1 parent af3d2f7 commit 43e6316

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed
 

‎NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ PHP NEWS
2424
- SOAP:
2525
. Fixed bug GH-12392 (Segmentation fault on SoapClient::__getTypes).
2626
(nielsdos)
27+
. Fixed bug #66150 (SOAP WSDL cache race condition causes Segmentation
28+
Fault). (nielsdos)
29+
. Fixed bug #67617 (SOAP leaves incomplete cache file on ENOSPC). (nielsdos)
2730

2831
- XSL:
2932
. Add missing module dependency. (nielsdos)

‎ext/soap/php_sdl.c

+14-2
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>
@@ -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

0 commit comments

Comments
 (0)