Skip to content

Commit d707a8c

Browse files
committed
Fix SSL deadlock risk in libpq
In libpq, we set up and pass to OpenSSL callback routines to handle locking. When we run out of SSL connections, we try to clean things up by de-registering the hooks. Unfortunately, we had a few calls into the OpenSSL library after these hooks were de-registered during SSL cleanup which lead to deadlocking. This moves the thread callback cleanup to be after all SSL-cleanup related OpenSSL library calls. I've been unable to reproduce the deadlock with this fix. In passing, also move the close_SSL call to be after unlocking our ssl_config mutex when in a failure state. While it looks pretty unlikely to be an issue, it could have resulted in deadlocks if we ended up in this code path due to something other than SSL_new failing. Thanks to Heikki for pointing this out. Back-patch to all supported versions; note that the close_SSL issue only goes back to 9.0, so that hunk isn't included in the 8.4 patch. Initially found and reported by Vesa-Matti J Kari; many thanks to both Heikki and Andres for their help running down the specific issue and reviewing the patch.
1 parent 4392db8 commit d707a8c

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/interfaces/libpq/fe-secure.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,11 @@ pqsecure_open_client(PGconn *conn)
283283
libpq_gettext("could not establish SSL connection: %s\n"),
284284
err);
285285
SSLerrfree(err);
286-
close_SSL(conn);
287286
#ifdef ENABLE_THREAD_SAFETY
288287
pthread_mutex_unlock(&ssl_config_mutex);
289288
#endif
289+
close_SSL(conn);
290+
290291
return PGRES_POLLING_FAILED;
291292
}
292293
#ifdef ENABLE_THREAD_SAFETY
@@ -1538,15 +1539,23 @@ open_client_SSL(PGconn *conn)
15381539
static void
15391540
close_SSL(PGconn *conn)
15401541
{
1542+
bool destroy_needed = false;
1543+
15411544
if (conn->ssl)
15421545
{
15431546
DECLARE_SIGPIPE_INFO(spinfo);
15441547

1548+
/*
1549+
* We can't destroy everything SSL-related here due to the possible
1550+
* later calls to OpenSSL routines which may need our thread
1551+
* callbacks, so set a flag here and check at the end.
1552+
*/
1553+
destroy_needed = true;
1554+
15451555
DISABLE_SIGPIPE(conn, spinfo, (void) 0);
15461556
SSL_shutdown(conn->ssl);
15471557
SSL_free(conn->ssl);
15481558
conn->ssl = NULL;
1549-
pqsecure_destroy();
15501559
/* We have to assume we got EPIPE */
15511560
REMEMBER_EPIPE(spinfo, true);
15521561
RESTORE_SIGPIPE(conn, spinfo);
@@ -1566,6 +1575,17 @@ close_SSL(PGconn *conn)
15661575
conn->engine = NULL;
15671576
}
15681577
#endif
1578+
1579+
/*
1580+
* This will remove our SSL locking hooks, if this is the last SSL
1581+
* connection, which means we must wait to call it until after all
1582+
* SSL calls have been made, otherwise we can end up with a race
1583+
* condition and possible deadlocks.
1584+
*
1585+
* See comments above destroy_ssl_system().
1586+
*/
1587+
if (destroy_needed)
1588+
pqsecure_destroy();
15691589
}
15701590

15711591
/*

0 commit comments

Comments
 (0)