Skip to content

Commit 143b2dd

Browse files
omjegoyhirose
andauthored
Fix memory leak due caused due to X509_STORE (#671)
* Fix memory leak due caused due to X509_STORE * Add test for repro and address sanitizer to compiler flags * Add comment * Sync * Associate ca_store with ssl context within set_ca_cert_store() * Split SlowPost test * Fix #674 Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
1 parent e2c4e9d commit 143b2dd

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

httplib.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,6 @@ class SSLClient : public ClientImpl {
11711171

11721172
std::string ca_cert_file_path_;
11731173
std::string ca_cert_dir_path_;
1174-
X509_STORE *ca_cert_store_ = nullptr;
11751174
long verify_result_ = 0;
11761175

11771176
friend class ClientImpl;
@@ -5844,7 +5843,16 @@ inline void SSLClient::set_ca_cert_path(const char *ca_cert_file_path,
58445843
}
58455844

58465845
inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) {
5847-
if (ca_cert_store) { ca_cert_store_ = ca_cert_store; }
5846+
if (ca_cert_store) {
5847+
if(ctx_) {
5848+
if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) {
5849+
// Free memory allocated for old cert and use new store `ca_cert_store`
5850+
SSL_CTX_set_cert_store(ctx_, ca_cert_store);
5851+
}
5852+
} else {
5853+
X509_STORE_free(ca_cert_store);
5854+
}
5855+
}
58485856
}
58495857

58505858
inline long SSLClient::get_openssl_verify_result() const {
@@ -5922,10 +5930,6 @@ inline bool SSLClient::load_certs() {
59225930
ca_cert_dir_path_.c_str())) {
59235931
ret = false;
59245932
}
5925-
} else if (ca_cert_store_ != nullptr) {
5926-
if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store_) {
5927-
SSL_CTX_set_cert_store(ctx_, ca_cert_store_);
5928-
}
59295933
} else {
59305934
#ifdef _WIN32
59315935
detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_));

test/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
#CXX = clang++
3-
CXXFLAGS = -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -Wtype-limits -Wconversion
3+
CXXFLAGS = -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -Wtype-limits -Wconversion -fsanitize=address
44

55
OPENSSL_DIR = /usr/local/opt/openssl@1.1
66
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto

test/test.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,6 +3125,19 @@ TEST_F(PayloadMaxLengthTest, ExceedLimit) {
31253125
}
31263126

31273127
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3128+
TEST(SSLClientTest, UpdateCAStore) {
3129+
httplib::SSLClient httplib_client("www.google.com");
3130+
auto ca_store_1 = X509_STORE_new();
3131+
X509_STORE_load_locations(ca_store_1, "/etc/ssl/certs/ca-certificates.crt",
3132+
nullptr);
3133+
httplib_client.set_ca_cert_store(ca_store_1);
3134+
3135+
auto ca_store_2 = X509_STORE_new();
3136+
X509_STORE_load_locations(ca_store_2, "/etc/ssl/certs/ca-certificates.crt",
3137+
nullptr);
3138+
httplib_client.set_ca_cert_store(ca_store_2);
3139+
}
3140+
31283141
TEST(SSLClientTest, ServerNameIndication) {
31293142
SSLClient cli("httpbin.org", 443);
31303143
auto res = cli.Get("/get");

0 commit comments

Comments
 (0)