Fix GH-18986: OpenSSL backend: incorrect RAND_{load,write}_file() return value check

As noted by the LibreSSL maintainer, these functions return -1 on error.
This is further confirmed by my static analyzer that inferred the same
thing for OpenSSL.

Closes GH-19013.
This commit is contained in:
Niels Dossche 2025-07-02 22:04:09 +02:00
parent 6b2b60f683
commit a8086be81c
No known key found for this signature in database
GPG key ID: B8A8AD166DF0E2E5
2 changed files with 4 additions and 2 deletions

2
NEWS
View file

@ -37,6 +37,8 @@ PHP NEWS
- OpenSSL: - OpenSSL:
. Fixed bug #80770 (It is not possible to get client peer certificate with . Fixed bug #80770 (It is not possible to get client peer certificate with
stream_socket_server). (Jakub Zelenka) stream_socket_server). (Jakub Zelenka)
. Fixed bug GH-18986 (OpenSSL backend: incorrect RAND_{load,write}_file()
return value check). (nielsdos, botovq)
- PCNTL: - PCNTL:
. Fixed bug GH-18958 (Fatal error during shutdown after pcntl_rfork() or . Fixed bug GH-18958 (Fatal error during shutdown after pcntl_rfork() or

View file

@ -1095,7 +1095,7 @@ static int php_openssl_load_rand_file(const char * file, int *egdsocket, int *se
return SUCCESS; return SUCCESS;
#endif #endif
} }
if (file == NULL || !RAND_load_file(file, -1)) { if (file == NULL || RAND_load_file(file, -1) < 0) {
if (RAND_status() == 0) { if (RAND_status() == 0) {
php_openssl_store_errors(); php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to load random state; not enough random data!"); php_error_docref(NULL, E_WARNING, "Unable to load random state; not enough random data!");
@ -1122,7 +1122,7 @@ static int php_openssl_write_rand_file(const char * file, int egdsocket, int see
file = RAND_file_name(buffer, sizeof(buffer)); file = RAND_file_name(buffer, sizeof(buffer));
} }
PHP_OPENSSL_RAND_ADD_TIME(); PHP_OPENSSL_RAND_ADD_TIME();
if (file == NULL || !RAND_write_file(file)) { if (file == NULL || RAND_write_file(file) < 0) {
php_openssl_store_errors(); php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to write random state"); php_error_docref(NULL, E_WARNING, "Unable to write random state");
return FAILURE; return FAILURE;