Re: Support for NSS as a libpq TLS backend
Jacob Champion <pchampion@vmware.com>
From: Jacob Champion <pchampion@vmware.com>
To: "sfrost@snowman.net" <sfrost@snowman.net>
Cc: "daniel@yesql.se" <daniel@yesql.se>, "pgsql-hackers@lists.postgresql.org" <pgsql-hackers@lists.postgresql.org>, "hlinnaka@iki.fi" <hlinnaka@iki.fi>, "andrew.dunstan@2ndquadrant.com" <andrew.dunstan@2ndquadrant.com>, "thomas.munro@gmail.com" <thomas.munro@gmail.com>, "michael@paquier.xyz" <michael@paquier.xyz>, "andres@anarazel.de" <andres@anarazel.de>
Date: 2021-03-31T22:15:15Z
Lists: pgsql-hackers
On Fri, 2021-03-26 at 18:05 -0400, Stephen Frost wrote: > * Jacob Champion (pchampion@vmware.com) wrote: > > Yeah. I was hoping to avoid implementing our own locks and refcounts, > > but it seems like it's going to be required. > > Yeah, afraid so. I think it gets worse, after having debugged some confusing crashes. There's already been a discussion on PR_Init upthread a bit: > Once we settle on a version we can confirm if PR_Init is/isn't needed and > remove all traces of it if not. What the NSPR documentation omits is that implicit initialization is not threadsafe. So NSS_InitContext() is technically "threadsafe" because it's built on PR_CallOnce(), but if you haven't called PR_Init() yet, multiple simultaneous PR_CallOnce() calls can crash into each other. So, fine. We just add our own locks around NSS_InitContext() (or around a single call to PR_Init()). Well, the first thread to win and successfully initialize NSPR gets marked as the "primordial" thread using thread-local state. And it gets a pthread destructor that does... something. So lazy initialization seems a bit dangerous regardless of whether or not we add locks, but I can't really prove whether it's dangerous or not in practice. I do know that only the primordial thread is allowed to call PR_Cleanup(), and of course we wouldn't be able to control which thread does what for libpq clients. I don't know what other assumptions are made about the primordial thread, or if there are any platform-specific behaviors with older versions of NSPR that we'd need to worry about. It used to be that the primordial thread was not allowed to exit before any other threads, but that restriction was lifted at some point [1]. I think we're going to need some analogue to PQinitOpenSSL() to help client applications cut through the mess, but I'm not sure what it should look like, or how we would maintain any sort of API compatibility between the two flavors. And does libpq already have some notion of a "main thread" that I'm missing? --Jacob [1] https://bugzilla.mozilla.org/show_bug.cgi?id=294955
Commits
-
Add tab-completion for CREATE FOREIGN TABLE.
- 74527c3e022d 15.0 cited
-
Add tap tests for the schema publications.
- 6b0f6f79eef2 15.0 cited
-
Move Perl test modules to a better namespace
- b3b4d8e68ae8 15.0 cited
-
Adjust configure to insist on Perl version >= 5.8.3.
- 92e6a98c3636 15.0 cited
-
Simplify code related to compilation of SSL and OpenSSL
- 092b785fad3d 14.0 landed
-
Introduce --with-ssl={openssl} as a configure option
- fe61df7f82aa 14.0 landed
-
Implement support for bulk inserts in postgres_fdw
- b663a4136331 14.0 cited
-
Fix redundant error messages in client tools
- 6be725e70161 14.0 cited
-
doc: Apply more consistently <productname> markup for OpenSSL
- 089da3c4778f 14.0 landed
-
Check ssl_in_use flag when reporting statistics
- 6a5c750f3f72 14.0 cited