Skip to content

Commit ea71812

Browse files
committed
Avoid leaking old SSL context on reconnect
The SSL context is currently unconditionally created during connect, while it is only destroyed in free. Move the initialization to init, since the context is independent from the individual connection.
1 parent 9595f0e commit ea71812

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/netlog/netlog-dtls.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ int dtls_connect(DTLSManager *m, SocketAddress *address) {
5959
_cleanup_free_ char *pretty = NULL;
6060
const SSL_CIPHER *cipher;
6161
socklen_t salen;
62-
SSL_CTX *ctx;
6362
struct timeval timeout = {
6463
.tv_sec = 3,
6564
.tv_usec = 0,
@@ -68,6 +67,7 @@ int dtls_connect(DTLSManager *m, SocketAddress *address) {
6867
int r;
6968

7069
assert(m);
70+
assert(m->ctx);
7171
assert(address);
7272

7373
switch (address->sockaddr.sa.sa_family) {
@@ -95,12 +95,7 @@ int dtls_connect(DTLSManager *m, SocketAddress *address) {
9595

9696
log_debug("DTLS: Connected to remote server: '%s'", pretty);
9797

98-
ctx = SSL_CTX_new(DTLS_method());
99-
if (!ctx)
100-
return log_error_errno(SYNTHETIC_ERRNO(ENOMEM),
101-
"DTLS: Failed to allocate memory for SSL CTX: %m");
102-
103-
ssl = SSL_new(ctx);
98+
ssl = SSL_new(m->ctx);
10499
if (!ssl)
105100
return log_error_errno(SYNTHETIC_ERRNO(ENOMEM),
106101
"DTLS: Failed to allocate memory for ssl: %s",
@@ -125,9 +120,8 @@ int dtls_connect(DTLSManager *m, SocketAddress *address) {
125120
SSL_set_verify(ssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, ssl_verify_certificate_validity);
126121
} else {
127122
log_debug("DTLS: disable certificate verification");
128-
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
123+
SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
129124
}
130-
SSL_CTX_set_default_verify_paths(ctx);
131125

132126
r = SSL_connect(ssl);
133127
if (r <= 0)
@@ -158,7 +152,6 @@ int dtls_connect(DTLSManager *m, SocketAddress *address) {
158152
BIO_ctrl(bio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
159153

160154
m->ssl = TAKE_PTR(ssl);
161-
m->ctx = ctx;
162155
m->fd = TAKE_FD(fd);
163156

164157
m->connected = true;
@@ -193,13 +186,22 @@ void dtls_manager_free(DTLSManager *m) {
193186

194187
int dtls_manager_init(OpenSSLCertificateAuthMode auth_mode, DTLSManager **ret) {
195188
_cleanup_(dtls_manager_freep) DTLSManager *m = NULL;
189+
_cleanup_(SSL_CTX_freep) SSL_CTX *ctx = NULL;
190+
191+
ctx = SSL_CTX_new(DTLS_method());
192+
if (!ctx)
193+
return log_error_errno(SYNTHETIC_ERRNO(ENOMEM),
194+
"DTLS: Failed to allocate memory for SSL CTX: %m");
195+
196+
SSL_CTX_set_default_verify_paths(ctx);
196197

197198
m = new(DTLSManager, 1);
198199
if (!m)
199200
return log_oom();
200201

201202
*m = (DTLSManager) {
202203
.auth_mode = auth_mode,
204+
.ctx = TAKE_PTR(ctx),
203205
.fd = -1,
204206
};
205207

src/netlog/netlog-ssl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33

44
#include <openssl/ssl.h>
55

6+
#include "macro.h"
7+
68
int ssl_verify_certificate_validity(int status, X509_STORE_CTX *store);
9+
10+
DEFINE_TRIVIAL_CLEANUP_FUNC(SSL_CTX*, SSL_CTX_free);

src/netlog/netlog-tls.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ int tls_connect(TLSManager *m, SocketAddress *address) {
6868
_cleanup_free_ char *pretty = NULL;
6969
const SSL_CIPHER *cipher;
7070
socklen_t salen;
71-
SSL_CTX *ctx;
7271
_cleanup_close_ int fd = -1;
7372
int r;
7473

7574
assert(m);
75+
assert(m->ctx);
7676
assert(address);
7777

7878
switch (address->sockaddr.sa.sa_family) {
@@ -100,12 +100,7 @@ int tls_connect(TLSManager *m, SocketAddress *address) {
100100

101101
log_debug("TLS: Connected to remote server: '%s'", pretty);
102102

103-
ctx = SSL_CTX_new(SSLv23_client_method());
104-
if (!ctx)
105-
return log_error_errno(SYNTHETIC_ERRNO(ENOMEM),
106-
"TLS: Failed to allocate memory for SSL CTX: %m");
107-
108-
ssl = SSL_new(ctx);
103+
ssl = SSL_new(m->ctx);
109104
if (!ssl)
110105
return log_error_errno(SYNTHETIC_ERRNO(ENOMEM),
111106
"TLS: Failed to allocate memory for ssl: %s",
@@ -125,10 +120,9 @@ int tls_connect(TLSManager *m, SocketAddress *address) {
125120
SSL_set_verify(ssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, ssl_verify_certificate_validity);
126121
} else {
127122
log_debug("TLS: disable certificate verification");
128-
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
123+
SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
129124
}
130125

131-
SSL_CTX_set_default_verify_paths(ctx);
132126
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
133127

134128
r = SSL_connect(ssl);
@@ -158,7 +152,6 @@ int tls_connect(TLSManager *m, SocketAddress *address) {
158152
}
159153

160154
m->ssl = TAKE_PTR(ssl);
161-
m->ctx = ctx;
162155
m->fd = TAKE_FD(fd);
163156

164157
m->connected = true;
@@ -193,13 +186,22 @@ void tls_manager_free(TLSManager *m) {
193186

194187
int tls_manager_init(OpenSSLCertificateAuthMode auth, TLSManager **ret ) {
195188
_cleanup_(tls_manager_freep) TLSManager *m = NULL;
189+
_cleanup_(SSL_CTX_freep) SSL_CTX *ctx = NULL;
190+
191+
ctx = SSL_CTX_new(TLS_client_method());
192+
if (!ctx)
193+
return log_error_errno(SYNTHETIC_ERRNO(ENOMEM),
194+
"TLS: Failed to allocate memory for SSL CTX: %m");
195+
196+
SSL_CTX_set_default_verify_paths(ctx);
196197

197198
m = new(TLSManager, 1);
198199
if (!m)
199200
return log_oom();
200201

201202
*m = (TLSManager) {
202203
.auth_mode = auth,
204+
.ctx = TAKE_PTR(ctx),
203205
.fd = -1,
204206
};
205207

0 commit comments

Comments
 (0)