Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd707a8c

Browse files
committed
Fix SSL deadlock risk in libpq
In libpq, we set up and pass to OpenSSL callback routines to handlelocking. When we run out of SSL connections, we try to clean thingsup by de-registering the hooks. Unfortunately, we had a few callsinto the OpenSSL library after these hooks were de-registered duringSSL cleanup which lead to deadlocking. This moves the thread callbackcleanup 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 ourssl_config mutex when in a failure state. While it looks prettyunlikely to be an issue, it could have resulted in deadlocks if weended up in this code path due to something other than SSL_newfailing. Thanks to Heikki for pointing this out.Back-patch to all supported versions; note that the close_SSL issueonly 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 toboth Heikki and Andres for their help running down the specificissue and reviewing the patch.
1 parent4392db8 commitd707a8c

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
#ifdefENABLE_THREAD_SAFETY
288287
pthread_mutex_unlock(&ssl_config_mutex);
289288
#endif
289+
close_SSL(conn);
290+
290291
returnPGRES_POLLING_FAILED;
291292
}
292293
#ifdefENABLE_THREAD_SAFETY
@@ -1538,15 +1539,23 @@ open_client_SSL(PGconn *conn)
15381539
staticvoid
15391540
close_SSL(PGconn*conn)
15401541
{
1542+
booldestroy_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)

[8]ページ先頭

©2009-2025 Movatter.jp