[PATCH] fix race condition in libpq (related to ssl connections)
Willi Mann <w.mann@celonis.de>
From: Willi Mann <w.mann@celonis.de>
To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2023-11-20T13:37:39Z
Lists: pgsql-hackers
Attachments
- 0001-libpq-Fix-race-condition-in-initialization-of-my_bio.patch (text/x-patch) patch 0001
- testcase.tar.gz (application/gzip)
Hi, I've found a race condition in libpq. It is about the initialization of the my_bio_methods static variable in fe-secure-openssl.c, which is not protected by any lock. The race condition may make the initialization of the connection fail, and as an additional weird consequence, it might cause openssl call close(0), so stdin of the client application gets closed. I've prepared a patch to protect the initialization of my_bio_methods from the race. This is my first patch submission to the postgresql project, so I hope I didn't miss anything. Any comments and suggestions are of course very welcome. I also prepared a testcase. In the testcase tarball, there is a patch that adds sleeps at the right positions to make the close(0) problem occur practically always. It also includes comments to explain how the race can end up calling close(0). Concerning the patch, it is only tested on Linux. I'm unsure about whether the simple initialization of the mutex would work nowadays also on Windows or whether the more complicated initialization also to be found for the ssl_config_mutex in the same source file needs to be used. Let me know whether I should adapt that. We discovered the problem with release 11.5, but the patch and the testcase are against the master branch. Regards, Willi -- ___________________________________________________ Dr. Willi Mann | Principal Software Engineer, Tech Lead PQL Celonis SE | Theresienstrasse 6 | 80333 Munich, Germany F: +4989416159679 w.mann@celonis.com | www.celonis.com | LinkedIn | Twitter | Xing AG Munich HRB 225439 | Management: Martin Klenk, Bastian Nominacher, Alexander Rinke
Commits
-
Fix race condition with BIO methods initialization in libpq with threads
- 0217a7444a57 12.18 landed
- 09f680d114b5 13.14 landed
- c3b79223f355 14.11 landed
- b97226815030 15.6 landed
- 8984480b545d 16.2 landed
- 01eca6a913c4 17.0 landed
-
Doc: Adjust libpq docs about thread safety.
- ce0b0fa3e792 17.0 cited