Skip to content

Commit 3084af3

Browse files
addaleaxjuanarbol
authored andcommitted
src: add proper mutexes for accessing FIPS state
The FIPS state handling and OpenSSL initialization code makes accesses to global OpenSSL state without any protection against parallel modifications from multiple threads. This commit adds such protections. PR-URL: #42278 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent fa7edb7 commit 3084af3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/crypto/crypto_util.cc

+17
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ bool InitCryptoOnce(Isolate* isolate) {
136136
return true;
137137
}
138138

139+
// Protect accesses to FIPS state with a mutex. This should potentially
140+
// be part of a larger mutex for global OpenSSL state.
141+
static Mutex fips_mutex;
142+
139143
void InitCryptoOnce() {
144+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
145+
Mutex::ScopedLock fips_lock(fips_mutex);
140146
#ifndef OPENSSL_IS_BORINGSSL
141147
OPENSSL_INIT_SETTINGS* settings = OPENSSL_INIT_new();
142148

@@ -196,6 +202,9 @@ void InitCryptoOnce() {
196202
}
197203

198204
void GetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
205+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
206+
Mutex::ScopedLock fips_lock(fips_mutex);
207+
199208
#if OPENSSL_VERSION_MAJOR >= 3
200209
args.GetReturnValue().Set(EVP_default_properties_is_fips_enabled(nullptr) ?
201210
1 : 0);
@@ -205,8 +214,13 @@ void GetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
205214
}
206215

207216
void SetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
217+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
218+
Mutex::ScopedLock fips_lock(fips_mutex);
219+
208220
CHECK(!per_process::cli_options->force_fips_crypto);
209221
Environment* env = Environment::GetCurrent(args);
222+
// TODO(addaleax): This should not be possible to set from worker threads.
223+
// CHECK(env->owns_process_state());
210224
bool enable = args[0]->BooleanValue(env->isolate());
211225

212226
#if OPENSSL_VERSION_MAJOR >= 3
@@ -227,6 +241,9 @@ void SetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
227241
}
228242

229243
void TestFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args) {
244+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
245+
Mutex::ScopedLock fips_lock(fips_mutex);
246+
230247
#ifdef OPENSSL_FIPS
231248
#if OPENSSL_VERSION_MAJOR >= 3
232249
OSSL_PROVIDER* fips_provider = nullptr;

0 commit comments

Comments
 (0)