Skip to content

Commit a2764d8

Browse files
committed
Avoid corner-case memory leak in SSL parameter processing.
After reading the root cert list from the ssl_ca_file, immediately install it as client CA list of the new SSL context. That gives the SSL context ownership of the list, so that SSL_CTX_free will free it. This avoids a permanent memory leak if we fail further down in be_tls_init(), which could happen if bogus CRL data is offered. The leak could only amount to something if the CRL parameters get broken after server start (else we'd just quit) and then the server is SIGHUP'd many times without fixing the CRL data. That's rather unlikely perhaps, but it seems worth fixing, if only because the code is clearer this way. While we're here, add some comments about the memory management aspects of this logic. Noted by Jelte Fennema and independently by Andres Freund. Back-patch to v10; before commit de41869 it doesn't matter, since we'd not re-execute this code during SIGHUP. Discussion: https://postgr.es/m/16160-18367e56e9a28264@postgresql.org
1 parent 33aa7d1 commit a2764d8

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/backend/libpq/be-secure-openssl.c

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ static bool ssl_is_server_start;
7575
int
7676
be_tls_init(bool isServerStart)
7777
{
78-
STACK_OF(X509_NAME) *root_cert_list = NULL;
7978
SSL_CTX *context;
8079

8180
/* This stuff need be done only once. */
@@ -92,6 +91,10 @@ be_tls_init(bool isServerStart)
9291
}
9392

9493
/*
94+
* Create a new SSL context into which we'll load all the configuration
95+
* settings. If we fail partway through, we can avoid memory leakage by
96+
* freeing this context; we don't install it as active until the end.
97+
*
9598
* We use SSLv23_method() because it can negotiate use of the highest
9699
* mutually supported protocol version, while alternatives like
97100
* TLSv1_2_method() permit only one specific version. Note that we don't
@@ -218,6 +221,8 @@ be_tls_init(bool isServerStart)
218221
*/
219222
if (ssl_ca_file[0])
220223
{
224+
STACK_OF(X509_NAME) * root_cert_list;
225+
221226
if (SSL_CTX_load_verify_locations(context, ssl_ca_file, NULL) != 1 ||
222227
(root_cert_list = SSL_load_client_CA_file(ssl_ca_file)) == NULL)
223228
{
@@ -227,6 +232,25 @@ be_tls_init(bool isServerStart)
227232
ssl_ca_file, SSLerrmessage(ERR_get_error()))));
228233
goto error;
229234
}
235+
236+
/*
237+
* Tell OpenSSL to send the list of root certs we trust to clients in
238+
* CertificateRequests. This lets a client with a keystore select the
239+
* appropriate client certificate to send to us. Also, this ensures
240+
* that the SSL context will "own" the root_cert_list and remember to
241+
* free it when no longer needed.
242+
*/
243+
SSL_CTX_set_client_CA_list(context, root_cert_list);
244+
245+
/*
246+
* Always ask for SSL client cert, but don't fail if it's not
247+
* presented. We might fail such connections later, depending on what
248+
* we find in pg_hba.conf.
249+
*/
250+
SSL_CTX_set_verify(context,
251+
(SSL_VERIFY_PEER |
252+
SSL_VERIFY_CLIENT_ONCE),
253+
verify_cb);
230254
}
231255

232256
/*----------
@@ -266,26 +290,6 @@ be_tls_init(bool isServerStart)
266290
}
267291
}
268292

269-
if (ssl_ca_file[0])
270-
{
271-
/*
272-
* Always ask for SSL client cert, but don't fail if it's not
273-
* presented. We might fail such connections later, depending on what
274-
* we find in pg_hba.conf.
275-
*/
276-
SSL_CTX_set_verify(context,
277-
(SSL_VERIFY_PEER |
278-
SSL_VERIFY_CLIENT_ONCE),
279-
verify_cb);
280-
281-
/*
282-
* Tell OpenSSL to send the list of root certs we trust to clients in
283-
* CertificateRequests. This lets a client with a keystore select the
284-
* appropriate client certificate to send to us.
285-
*/
286-
SSL_CTX_set_client_CA_list(context, root_cert_list);
287-
}
288-
289293
/*
290294
* Success! Replace any existing SSL_context.
291295
*/
@@ -304,6 +308,7 @@ be_tls_init(bool isServerStart)
304308

305309
return 0;
306310

311+
/* Clean up by releasing working context. */
307312
error:
308313
if (context)
309314
SSL_CTX_free(context);

0 commit comments

Comments
 (0)