Skip to content

Fix bug #66150: SOAP WSDL cache race condition causes Segmentation Fault #12469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions ext/soap/php_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "ext/standard/md5.h"
#include "zend_virtual_cwd.h"
#include "main/php_open_temporary_file.h"

#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -2119,7 +2120,10 @@ static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr s
HashTable tmp_bindings;
HashTable tmp_functions;

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

if (f < 0) {return;}

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

php_ignore_value(write(f, ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)));
bool valid_file = write(f, ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)) == ZSTR_LEN(buf.s);
close(f);

/* Make sure that incomplete files (e.g. due to disk space issues, see bug #66150) are not utilised. */
if (valid_file) {
/* This is allowed to fail, this means that another process was raced to create the file. */
(void) VCWD_RENAME(ZSTR_VAL(temp_file_path), fn);
}

smart_str_free(&buf);
zend_hash_destroy(&tmp_functions);
zend_hash_destroy(&tmp_bindings);
zend_hash_destroy(&tmp_encoders);
zend_hash_destroy(&tmp_types);
zend_string_release_ex(temp_file_path, false);
}


Expand Down