Skip to content

Commit f936fc6

Browse files
akonradidnoe
authored andcommitted
ssl: serialize accesses to SSL socket factory contexts (#4345)
Description: The ssl_ctx_ fields of the ServerSslSocketFactory and ClientSslSocketFactory are accessed and mutated from different threads without external serialization. This is a bug since instances of std::shared_ptr are not thread-safe (though different instances pointing to the same object are). This patch fixes the bug by using a mutex to serialize accesses. Risk Level: Low Testing: ran test suite Docs Changes: n/a Release Notes: n/a
1 parent e34dcd6 commit f936fc6

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

source/common/ssl/BUILD

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ envoy_cc_library(
1212
name = "ssl_socket_lib",
1313
srcs = ["ssl_socket.cc"],
1414
hdrs = ["ssl_socket.h"],
15-
external_deps = ["ssl"],
15+
external_deps = [
16+
"abseil_synchronization",
17+
"ssl",
18+
],
1619
deps = [
1720
":context_config_lib",
1821
":context_lib",
@@ -23,6 +26,7 @@ envoy_cc_library(
2326
"//source/common/common:assert_lib",
2427
"//source/common/common:empty_string",
2528
"//source/common/common:minimal_logger_lib",
29+
"//source/common/common:thread_annotations",
2630
"//source/common/http:headers_lib",
2731
],
2832
)

source/common/ssl/ssl_socket.cc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,11 @@ Network::TransportSocketPtr ClientSslSocketFactory::createTransportSocket() cons
422422
// onAddOrUpdateSecret() could be invoked in the middle of checking the existence of ssl_ctx and
423423
// creating SslSocket using ssl_ctx. Capture ssl_ctx_ into a local variable so that we check and
424424
// use the same ssl_ctx to create SslSocket.
425-
auto ssl_ctx = ssl_ctx_;
425+
ClientContextSharedPtr ssl_ctx;
426+
{
427+
absl::ReaderMutexLock l(&ssl_ctx_mu_);
428+
ssl_ctx = ssl_ctx_;
429+
}
426430
if (ssl_ctx) {
427431
return std::make_unique<Ssl::SslSocket>(std::move(ssl_ctx), Ssl::InitialState::Client);
428432
} else {
@@ -436,7 +440,10 @@ bool ClientSslSocketFactory::implementsSecureTransport() const { return true; }
436440

437441
void ClientSslSocketFactory::onAddOrUpdateSecret() {
438442
ENVOY_LOG(debug, "Secret is updated.");
439-
ssl_ctx_ = manager_.createSslClientContext(stats_scope_, *config_);
443+
{
444+
absl::WriterMutexLock l(&ssl_ctx_mu_);
445+
ssl_ctx_ = manager_.createSslClientContext(stats_scope_, *config_);
446+
}
440447
stats_.ssl_context_update_by_sds_.inc();
441448
}
442449

@@ -454,7 +461,11 @@ Network::TransportSocketPtr ServerSslSocketFactory::createTransportSocket() cons
454461
// onAddOrUpdateSecret() could be invoked in the middle of checking the existence of ssl_ctx and
455462
// creating SslSocket using ssl_ctx. Capture ssl_ctx_ into a local variable so that we check and
456463
// use the same ssl_ctx to create SslSocket.
457-
auto ssl_ctx = ssl_ctx_;
464+
ServerContextSharedPtr ssl_ctx;
465+
{
466+
absl::ReaderMutexLock l(&ssl_ctx_mu_);
467+
ssl_ctx = ssl_ctx_;
468+
}
458469
if (ssl_ctx) {
459470
return std::make_unique<Ssl::SslSocket>(std::move(ssl_ctx), Ssl::InitialState::Server);
460471
} else {
@@ -468,7 +479,10 @@ bool ServerSslSocketFactory::implementsSecureTransport() const { return true; }
468479

469480
void ServerSslSocketFactory::onAddOrUpdateSecret() {
470481
ENVOY_LOG(debug, "Secret is updated.");
471-
ssl_ctx_ = manager_.createSslServerContext(stats_scope_, *config_, server_names_);
482+
{
483+
absl::WriterMutexLock l(&ssl_ctx_mu_);
484+
ssl_ctx_ = manager_.createSslServerContext(stats_scope_, *config_, server_names_);
485+
}
472486
stats_.ssl_context_update_by_sds_.inc();
473487
}
474488

source/common/ssl/ssl_socket.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "common/common/logger.h"
1313
#include "common/ssl/context_impl.h"
1414

15+
#include "absl/synchronization/mutex.h"
1516
#include "openssl/ssl.h"
1617

1718
namespace Envoy {
@@ -101,7 +102,8 @@ class ClientSslSocketFactory : public Network::TransportSocketFactory,
101102
Stats::Scope& stats_scope_;
102103
SslSocketFactoryStats stats_;
103104
ClientContextConfigPtr config_;
104-
ClientContextSharedPtr ssl_ctx_;
105+
mutable absl::Mutex ssl_ctx_mu_;
106+
ClientContextSharedPtr ssl_ctx_ GUARDED_BY(ssl_ctx_mu_);
105107
};
106108

107109
class ServerSslSocketFactory : public Network::TransportSocketFactory,
@@ -123,7 +125,8 @@ class ServerSslSocketFactory : public Network::TransportSocketFactory,
123125
SslSocketFactoryStats stats_;
124126
ServerContextConfigPtr config_;
125127
const std::vector<std::string> server_names_;
126-
ServerContextSharedPtr ssl_ctx_;
128+
mutable absl::Mutex ssl_ctx_mu_;
129+
ServerContextSharedPtr ssl_ctx_ GUARDED_BY(ssl_ctx_mu_);
127130
};
128131

129132
} // namespace Ssl

0 commit comments

Comments
 (0)