Skip to content

Commit 3741595

Browse files
danbevdanielleadams
authored andcommitted
src: set CONF_MFLAGS_DEFAULT_SECTION for OpenSSL 3
This commit adds a call to OPENSSL_init_crypto to initialize OPENSSL_INIT_LOAD_CONFIG to avoid the default behavior where errors raised during the parsing of the OpenSSL configuration file are not propagated and cannot be detected. The motivation for this is that if FIPS is configured the OpenSSL configuration file will have an .include pointing to the fipsmodule.cnf file generated by the openssl fipsinstall command. If the path to this file is incorrect no error will be reported. For Node.js this will mean that EntropySource will be called by V8 as part of its initalization process, and EntropySource will in turn call CheckEntropy. CheckEntropy will call RAND_status which will now always return 0 leading to an endless loop and the node process will appear to hang/freeze. I'll continue investigating the cause of this and see if this is expected behavior or not, but in the mean time it would be good to be able to workaround this issue with this commit. PR-URL: #38732 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Refs: #38633 (review)
1 parent 717a8b6 commit 3741595

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/crypto/crypto_util.cc

+2
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ void InitCryptoOnce() {
109109
#ifndef OPENSSL_IS_BORINGSSL
110110
OPENSSL_INIT_SETTINGS* settings = OPENSSL_INIT_new();
111111

112+
#if OPENSSL_VERSION_MAJOR < 3
112113
// --openssl-config=...
113114
if (!per_process::cli_options->openssl_config.empty()) {
114115
const char* conf = per_process::cli_options->openssl_config.c_str();
115116
OPENSSL_INIT_set_config_filename(settings, conf);
116117
}
118+
#endif
117119

118120
OPENSSL_init_ssl(0, settings);
119121
OPENSSL_INIT_free(settings);

src/node.cc

+36-2
Original file line numberDiff line numberDiff line change
@@ -1024,12 +1024,46 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
10241024
// In the case of FIPS builds we should make sure
10251025
// the random source is properly initialized first.
10261026
#if OPENSSL_VERSION_MAJOR >= 3
1027-
if (EVP_default_properties_is_fips_enabled(nullptr)) {
1027+
// Call OPENSSL_init_crypto to initialize OPENSSL_INIT_LOAD_CONFIG to
1028+
// avoid the default behavior where errors raised during the parsing of the
1029+
// OpenSSL configuration file are not propagated and cannot be detected.
1030+
//
1031+
// If FIPS is configured the OpenSSL configuration file will have an .include
1032+
// pointing to the fipsmodule.cnf file generated by the openssl fipsinstall
1033+
// command. If the path to this file is incorrect no error will be reported.
1034+
//
1035+
// For Node.js this will mean that EntropySource will be called by V8 as part
1036+
// of its initalization process, and EntropySource will in turn call
1037+
// CheckEntropy. CheckEntropy will call RAND_status which will now always
1038+
// return 0, leading to an endless loop and the node process will appear to
1039+
// hang/freeze.
1040+
std::string env_openssl_conf;
1041+
credentials::SafeGetenv("OPENSSL_CONF", &env_openssl_conf);
1042+
1043+
bool has_cli_conf = !per_process::cli_options->openssl_config.empty();
1044+
if (has_cli_conf || !env_openssl_conf.empty()) {
1045+
OPENSSL_INIT_SETTINGS* settings = OPENSSL_INIT_new();
1046+
OPENSSL_INIT_set_config_file_flags(settings, CONF_MFLAGS_DEFAULT_SECTION);
1047+
if (has_cli_conf) {
1048+
const char* conf = per_process::cli_options->openssl_config.c_str();
1049+
OPENSSL_INIT_set_config_filename(settings, conf);
1050+
}
1051+
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, settings);
1052+
OPENSSL_INIT_free(settings);
1053+
1054+
if (ERR_peek_error() != 0) {
1055+
result.exit_code = ERR_GET_REASON(ERR_peek_error());
1056+
result.early_return = true;
1057+
fprintf(stderr, "OpenSSL configuration error:\n");
1058+
ERR_print_errors_fp(stderr);
1059+
return result;
1060+
}
1061+
}
10281062
#else
10291063
if (FIPS_mode()) {
10301064
OPENSSL_init();
1031-
#endif
10321065
}
1066+
#endif
10331067
// V8 on Windows doesn't have a good source of entropy. Seed it from
10341068
// OpenSSL's pool.
10351069
V8::SetEntropySource(crypto::EntropySource);

test/parallel/test-cli-node-options.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ if (common.isLinux) {
6161
if (common.hasCrypto) {
6262
expectNoWorker('--use-openssl-ca', 'B\n');
6363
expectNoWorker('--use-bundled-ca', 'B\n');
64-
expectNoWorker('--openssl-config=_ossl_cfg', 'B\n');
64+
if (!common.hasOpenSSL3)
65+
expectNoWorker('--openssl-config=_ossl_cfg', 'B\n');
6566
}
6667

6768
// V8 options

0 commit comments

Comments
 (0)