Skip to content

Commit 86fa07c

Browse files
committed
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. Refs: #38633 (review)
1 parent 910efc2 commit 86fa07c

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/crypto/crypto_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ int NoPasswordCallback(char* buf, int size, int rwflag, void* u) {
106106
}
107107

108108
void InitCryptoOnce() {
109-
#ifndef OPENSSL_IS_BORINGSSL
109+
#if !defined(OPENSSL_IS_BORINGSSL) && OPENSSL_VERSION_MAJOR < 3
110110
OPENSSL_INIT_SETTINGS* settings = OPENSSL_INIT_new();
111111

112112
// --openssl-config=...

src/node.cc

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,12 +1024,48 @@ 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+
bool has_env_conf = !env_openssl_conf.empty();
1045+
1046+
if (has_cli_conf || has_env_conf) {
1047+
const char* conf = has_cli_conf ?
1048+
per_process::cli_options->openssl_config.c_str() :
1049+
env_openssl_conf.c_str();
1050+
OPENSSL_INIT_SETTINGS* settings = OPENSSL_INIT_new();
1051+
OPENSSL_INIT_set_config_filename(settings, conf);
1052+
OPENSSL_INIT_set_config_file_flags(settings, CONF_MFLAGS_DEFAULT_SECTION);
1053+
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, settings);
1054+
OPENSSL_INIT_free(settings);
1055+
1056+
if (ERR_peek_error() != 0) {
1057+
result.exit_code = ERR_GET_REASON(ERR_peek_error());
1058+
result.early_return = true;
1059+
fprintf(stderr, "OpenSSL configuration error:\n");
1060+
ERR_print_errors_fp(stderr);
1061+
return result;
1062+
}
1063+
}
10281064
#else
10291065
if (FIPS_mode()) {
10301066
OPENSSL_init();
1031-
#endif
10321067
}
1068+
#endif
10331069
// V8 on Windows doesn't have a good source of entropy. Seed it from
10341070
// OpenSSL's pool.
10351071
V8::SetEntropySource(crypto::EntropySource);

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

Lines changed: 2 additions & 1 deletion
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)