Skip to content

Commit 8d9835a

Browse files
authored
[stable-25-2] NBS-6427 IGNIETFERRO-1843: backport openssl/grpc crash fix from trunk (#26428)
2 parents e447d29 + 72a34fa commit 8d9835a

File tree

2 files changed

+7
-60
lines changed

2 files changed

+7
-60
lines changed

library/cpp/openssl/init/init.cpp

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,13 @@
1-
#include "init.h"
2-
3-
#include <util/generic/singleton.h>
4-
#include <util/generic/vector.h>
5-
#include <util/generic/ptr.h>
6-
#include <util/generic/buffer.h>
7-
8-
#include <util/system/yassert.h>
9-
#include <util/system/mutex.h>
10-
#include <util/system/thread.h>
11-
12-
#include <util/random/entropy.h>
13-
#include <util/stream/input.h>
14-
15-
#include <openssl/bio.h>
16-
#include <openssl/ssl.h>
17-
#include <openssl/err.h>
18-
#include <openssl/rand.h>
19-
#include <openssl/conf.h>
201
#include <openssl/crypto.h>
212

223
namespace {
23-
struct TInitSsl {
24-
struct TOpensslLocks {
25-
inline TOpensslLocks()
26-
: Mutexes(CRYPTO_num_locks())
27-
{
28-
for (auto& mpref : Mutexes) {
29-
mpref.Reset(new TMutex());
30-
}
31-
}
32-
33-
inline void LockOP(int mode, int n) {
34-
auto& mutex = *Mutexes.at(n);
35-
36-
if (mode & CRYPTO_LOCK) {
37-
mutex.Acquire();
38-
} else {
39-
mutex.Release();
40-
}
41-
}
42-
43-
TVector<TAutoPtr<TMutex>> Mutexes;
44-
};
45-
46-
inline TInitSsl() {
47-
OPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, nullptr);
48-
}
49-
50-
inline ~TInitSsl() {
51-
OPENSSL_cleanup();
52-
}
53-
54-
static void LockingFunction(int mode, int n, const char* /*file*/, int /*line*/) {
55-
Singleton<TOpensslLocks>()->LockOP(mode, n);
56-
}
57-
58-
static unsigned long ThreadIdFunction() {
59-
return TThread::CurrentThreadId();
60-
}
61-
};
4+
// Initialize OpenSSL as early as possible
5+
// in order to prevent any further initializations with different flags.
6+
//
7+
// Initialize it with OPENSSL_INIT_NO_ATEXIT thus omitting the cleanup routine at process exit
8+
// (it looks like it does nothing when openssl is linked statically).
9+
[[maybe_unused]] auto _ = OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN | OPENSSL_INIT_NO_ATEXIT, nullptr);
6210
}
6311

6412
void InitOpenSSL() {
65-
(void)SingletonWithPriority<TInitSsl, 0>();
6613
}

library/cpp/openssl/init/ya.make

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PEERDIR(
55
)
66

77
SRCS(
8-
init.cpp
8+
GLOBAL init.cpp
99
)
1010

1111
END()

0 commit comments

Comments
 (0)