From 6ebaf85812d223d2709898230104c74e28e1c3c3 Mon Sep 17 00:00:00 2001 From: ndossche <7771979+ndossche@users.noreply.github.com> Date: Fri, 6 Mar 2026 17:59:36 +0100 Subject: [PATCH] openssl: Fix missing error propagation for BIO_printf() calls Since these go through a file, this can fail. For some of these, the error is already checked but not propagated to userland, causing a "true" return value but an incomplete file. For others, the error is not checked and can also lead to an incomplete file. Solve this by always propagating failure, especially as the other write calls are already checked for failure. --- ext/openssl/openssl.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c index 2f2aae1e7335b..ae7b66449741e 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -5897,16 +5897,21 @@ PHP_FUNCTION(openssl_pkcs7_encrypt) /* tack on extra headers */ if (zheaders) { ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zheaders), strindex, zcertval) { + int ret; zend_string *str = zval_try_get_string(zcertval); if (UNEXPECTED(!str)) { goto clean_exit; } if (strindex) { - BIO_printf(outfile, "%s: %s\n", ZSTR_VAL(strindex), ZSTR_VAL(str)); + ret = BIO_printf(outfile, "%s: %s\n", ZSTR_VAL(strindex), ZSTR_VAL(str)); } else { - BIO_printf(outfile, "%s\n", ZSTR_VAL(str)); + ret = BIO_printf(outfile, "%s\n", ZSTR_VAL(str)); } zend_string_release(str); + if (ret < 0) { + php_openssl_store_errors(); + goto clean_exit; + } } ZEND_HASH_FOREACH_END(); } @@ -6125,6 +6130,7 @@ PHP_FUNCTION(openssl_pkcs7_sign) zend_string_release(str); if (ret < 0) { php_openssl_store_errors(); + goto clean_exit; } } ZEND_HASH_FOREACH_END(); } @@ -6515,16 +6521,21 @@ PHP_FUNCTION(openssl_cms_encrypt) /* tack on extra headers */ if (zheaders && encoding == ENCODING_SMIME) { ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zheaders), strindex, zcertval) { + int ret; zend_string *str = zval_try_get_string(zcertval); if (UNEXPECTED(!str)) { goto clean_exit; } if (strindex) { - BIO_printf(outfile, "%s: %s\n", ZSTR_VAL(strindex), ZSTR_VAL(str)); + ret = BIO_printf(outfile, "%s: %s\n", ZSTR_VAL(strindex), ZSTR_VAL(str)); } else { - BIO_printf(outfile, "%s\n", ZSTR_VAL(str)); + ret = BIO_printf(outfile, "%s\n", ZSTR_VAL(str)); } zend_string_release(str); + if (ret < 0) { + php_openssl_store_errors(); + goto clean_exit; + } } ZEND_HASH_FOREACH_END(); } @@ -6804,6 +6815,7 @@ PHP_FUNCTION(openssl_cms_sign) zend_string_release(str); if (ret < 0) { php_openssl_store_errors(); + goto clean_exit; } } ZEND_HASH_FOREACH_END(); }