Skip to content

Commit 7754eaf

Browse files
committed
Fix memory leaks when sk_X509_new_null() fails
In a lot of places the return value is not checked, and when the function fails the code continues execution. However, this means that operations on the stack fail and will cause memory leaks on the objects that weren't pushed. We also notice an inconsistency in how these failures are handled. For example, in one place we explicitly have a fatal error `php_error_docref(NULL, E_ERROR, "Memory allocation failure");` but this is the only place to do so. Closes GH-20957.
1 parent d9cbc31 commit 7754eaf

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ PHP NEWS
3030
. Fixed bug GH-20818 (Segfault in Tracing JIT with object reference).
3131
(khasinski)
3232

33+
- OpenSSL:
34+
. Fix memory leaks when sk_X509_new_null() fails. (ndossche)
35+
3336
- Phar:
3437
. Fixed bug GH-20882 (buildFromIterator breaks with missing base directory).
3538
(ndossche)

ext/openssl/openssl.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,9 @@ static STACK_OF(X509) *php_array_to_X509_sk(zval * zcerts, uint32_t arg_num, con
25522552
bool free_cert;
25532553

25542554
sk = sk_X509_new_null();
2555+
if (sk == NULL) {
2556+
goto clean_exit;
2557+
}
25552558

25562559
/* get certs */
25572560
if (Z_TYPE_P(zcerts) == IS_ARRAY) {
@@ -5797,6 +5800,9 @@ PHP_FUNCTION(openssl_pkcs7_encrypt)
57975800
}
57985801

57995802
recipcerts = sk_X509_new_null();
5803+
if (recipcerts == NULL) {
5804+
goto clean_exit;
5805+
}
58005806

58015807
/* get certs */
58025808
if (Z_TYPE_P(zrecipcerts) == IS_ARRAY) {
@@ -6404,6 +6410,9 @@ PHP_FUNCTION(openssl_cms_encrypt)
64046410
}
64056411

64066412
recipcerts = sk_X509_new_null();
6413+
if (recipcerts == NULL) {
6414+
goto clean_exit;
6415+
}
64076416

64086417
/* get certs */
64096418
if (Z_TYPE_P(zrecipcerts) == IS_ARRAY) {

0 commit comments

Comments
 (0)