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

Commit09f680d

Browse files
committed
Fix race condition with BIO methods initialization in libpq with threads
The libpq code in charge of creating per-connection SSL objects wasprone to a race condition when loading the custom BIO methods needed bymy_SSL_set_fd(). As BIO methods are stored as a static variable, theinitialization of a connection could fail because it could be possibleto have one thread refer to my_bio_methods while it is being manipulatedby a second concurrent thread.This error has been introduced by8bb14cd, that has removedssl_config_mutex around the call of my_SSL_set_fd(), that itself setsthe custom BIO methods used in libpq. Like previously, the BIO methodinitialization is now protected by the existing ssl_config_mutex, itselfinitialized earlier for WIN32.While on it, document that my_bio_methods is protected byssl_config_mutex, as this can be easy to miss.Reported-by: Willi MannAuthor: Willi Mann, Michael PaquierDiscussion:https://postgr.es/m/e77abc4c-4d03-4058-a9d7-ef0035657e04@celonis.comBackpatch-through: 12
1 parent69a9134 commit09f680d

File tree

1 file changed

+51
-22
lines changed

1 file changed

+51
-22
lines changed

‎src/interfaces/libpq/fe-secure-openssl.c

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,7 @@ PQsslAttribute(PGconn *conn, const char *attribute_name)
16071607
#defineBIO_set_data(bio,data) (bio->ptr = data)
16081608
#endif
16091609

1610+
/* protected by ssl_config_mutex */
16101611
staticBIO_METHOD*my_bio_methods;
16111612

16121613
staticint
@@ -1672,6 +1673,15 @@ my_sock_write(BIO *h, const char *buf, int size)
16721673
staticBIO_METHOD*
16731674
my_BIO_s_socket(void)
16741675
{
1676+
BIO_METHOD*res;
1677+
1678+
#ifdefENABLE_THREAD_SAFETY
1679+
if (pthread_mutex_lock(&ssl_config_mutex))
1680+
returnNULL;
1681+
#endif
1682+
1683+
res=my_bio_methods;
1684+
16751685
if (!my_bio_methods)
16761686
{
16771687
BIO_METHOD*biom= (BIO_METHOD*)BIO_s_socket();
@@ -1680,39 +1690,58 @@ my_BIO_s_socket(void)
16801690

16811691
my_bio_index=BIO_get_new_index();
16821692
if (my_bio_index==-1)
1683-
returnNULL;
1693+
gotoerr;
16841694
my_bio_index |= (BIO_TYPE_DESCRIPTOR |BIO_TYPE_SOURCE_SINK);
1685-
my_bio_methods=BIO_meth_new(my_bio_index,"libpq socket");
1686-
if (!my_bio_methods)
1687-
returnNULL;
1695+
res=BIO_meth_new(my_bio_index,"libpq socket");
1696+
if (!res)
1697+
gotoerr;
16881698

16891699
/*
16901700
* As of this writing, these functions never fail. But check anyway,
16911701
* like OpenSSL's own examples do.
16921702
*/
1693-
if (!BIO_meth_set_write(my_bio_methods,my_sock_write)||
1694-
!BIO_meth_set_read(my_bio_methods,my_sock_read)||
1695-
!BIO_meth_set_gets(my_bio_methods,BIO_meth_get_gets(biom))||
1696-
!BIO_meth_set_puts(my_bio_methods,BIO_meth_get_puts(biom))||
1697-
!BIO_meth_set_ctrl(my_bio_methods,BIO_meth_get_ctrl(biom))||
1698-
!BIO_meth_set_create(my_bio_methods,BIO_meth_get_create(biom))||
1699-
!BIO_meth_set_destroy(my_bio_methods,BIO_meth_get_destroy(biom))||
1700-
!BIO_meth_set_callback_ctrl(my_bio_methods,BIO_meth_get_callback_ctrl(biom)))
1703+
if (!BIO_meth_set_write(res,my_sock_write)||
1704+
!BIO_meth_set_read(res,my_sock_read)||
1705+
!BIO_meth_set_gets(res,BIO_meth_get_gets(biom))||
1706+
!BIO_meth_set_puts(res,BIO_meth_get_puts(biom))||
1707+
!BIO_meth_set_ctrl(res,BIO_meth_get_ctrl(biom))||
1708+
!BIO_meth_set_create(res,BIO_meth_get_create(biom))||
1709+
!BIO_meth_set_destroy(res,BIO_meth_get_destroy(biom))||
1710+
!BIO_meth_set_callback_ctrl(res,BIO_meth_get_callback_ctrl(biom)))
17011711
{
1702-
BIO_meth_free(my_bio_methods);
1703-
my_bio_methods=NULL;
1704-
returnNULL;
1712+
gotoerr;
17051713
}
17061714
#else
1707-
my_bio_methods=malloc(sizeof(BIO_METHOD));
1708-
if (!my_bio_methods)
1709-
returnNULL;
1710-
memcpy(my_bio_methods,biom,sizeof(BIO_METHOD));
1711-
my_bio_methods->bread=my_sock_read;
1712-
my_bio_methods->bwrite=my_sock_write;
1715+
res=malloc(sizeof(BIO_METHOD));
1716+
if (!res)
1717+
gotoerr;
1718+
memcpy(res,biom,sizeof(BIO_METHOD));
1719+
res->bread=my_sock_read;
1720+
res->bwrite=my_sock_write;
17131721
#endif
17141722
}
1715-
returnmy_bio_methods;
1723+
1724+
my_bio_methods=res;
1725+
1726+
#ifdefENABLE_THREAD_SAFETY
1727+
pthread_mutex_unlock(&ssl_config_mutex);
1728+
#endif
1729+
1730+
returnres;
1731+
1732+
err:
1733+
#ifdefHAVE_BIO_METH_NEW
1734+
if (res)
1735+
BIO_meth_free(res);
1736+
#else
1737+
if (res)
1738+
free(res);
1739+
#endif
1740+
1741+
#ifdefENABLE_THREAD_SAFETY
1742+
pthread_mutex_unlock(&ssl_config_mutex);
1743+
#endif
1744+
returnNULL;
17161745
}
17171746

17181747
/* This should exactly match OpenSSL's SSL_set_fd except for using my BIO */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp