Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lCYgf-0002bU-NV for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Feb 2021 02:05:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lCYg7-0008Ly-8b for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Feb 2021 02:04:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lCYg5-0008Ll-SB for pgsql-hackers@lists.postgresql.org; Thu, 18 Feb 2021 02:04:27 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lCYg0-0003Tj-J1 for pgsql-hackers@lists.postgresql.org; Thu, 18 Feb 2021 02:04:25 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 9241D5C012A; Wed, 17 Feb 2021 21:04:17 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Wed, 17 Feb 2021 21:04:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm1; bh=VDDrgFxU2dFe3sPDJMx5w+wGbqT vuYGFfmRbPIA7JoA=; b=2Ok6we7oiwzGxBwgbw+Pw0i8mokFGy6WKVdfjtzew0E BnBRVh3g0DxIec5N4nzs9ojwt3I12xYN4tyOR/dvTLUVqPd3Ale5ksi/B//DRWN4 j549qwOBTa3VL8whZj3D2rX00ngBasWVcOC+8nFPx0KL3pynL9FhGZNracvfe+Xc fa+gEASUc+xG4WSovU9NS6st3wGT8B67Vyeo/RS7qXluD1XV2e33+RllZDtjIKVN q5EY9k5x57J0h3K8UryXnpAcFiB8dYzMf8dQle1auglgdeuK3Me/geWQITtfyp0C SQE7KcasFZLZmPZdvu48ux3t34tgRuMph4J5ZiRl8tw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=VDDrgF xU2dFe3sPDJMx5w+wGbqTvuYGFfmRbPIA7JoA=; b=Lxo0Rz0+7z2e7AyU/Mu3/o LtKEEFo3W6kJirvt6YT/mYfL1lPgv4Z6roKPWvNKirR8sFsW+tLAcnzGass+gJI9 c+TkirNj2G1b8N86OAfeiuSVz5FKZDhndsYF/IoHzGdkOLy0AVNN8JNcsjwxPKam 5HB4PTHY0cXzyWvtrT+V7Y1By0Xc++kjJo+ULKHRO8vMVhHh0/xSLpwhD/fz3+rC pUje0fub7Rt90ohlZl1S/83lF5iEZmEqGNvlXiVpavroUJt3vgv/uVcQrYV3zBt2 vgXTAaa8fb9OMs+VeBPJ88y66g7lZp9U7pFZigsGtpLd/n1sfW2SngyknL5NGMkA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrjeefgdegvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfg hrlhcuvffnffculdejtddmnecujfgurhepfffhvffukfhfgggtuggjsehgtderredttddv necuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesphgrqh huihgvrhdrgiihiieqnecuggftrfgrthhtvghrnhepvdegudeuhfdtueeltedtveejheeh ieevueeigeelteegleejleeiueeiheegvefhnecukfhppeduuddurddutddvrddukedtrd dukeehnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhep mhhitghhrggvlhesphgrqhhuihgvrhdrgiihii X-ME-Proxy: Received: from paquier.xyz (ee0822lan1.rev.em-net.ne.jp [111.102.180.185]) by mail.messagingengine.com (Postfix) with ESMTPA id 7B0A2240064; Wed, 17 Feb 2021 21:04:15 -0500 (EST) Date: Thu, 18 Feb 2021 11:04:05 +0900 From: Michael Paquier To: Jacob Champion Cc: "pgsql-hackers@lists.postgresql.org" Subject: Re: cryptohash: missing locking functions for OpenSSL <= 1.0.2? Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="A4BPO1AEM/bXA+3C" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --A4BPO1AEM/bXA+3C Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Feb 17, 2021 at 06:34:36PM +0000, Jacob Champion wrote: > This would only affect threaded libpq clients running OpenSSL 1.0.2 and > below, and it looks like the most likely code path to be affected is > the OpenSSL error stack. So if anything went wrong with one of those > hash calls, it's possible that libpq would crash (or worse, silently > misbehave somewhere in the TLS stack) instead of gracefully reporting > an error. [2] is an example of this in the wild. I have been discussing a bit this issue with Jacob, and that's a problem we would need to address on HEAD. First, I have been looking at this stuff in older versions with MD5 and SHA256 used by SCRAM when it comes to ~13 with libpq: - MD5 is based on the internal implementation of Postgres even when building libpq with OpenSSL, so that would not be an issue. - SHA256 is a different story though, because when building with OpenSSL we would go through SHA256_{Init,Update,Final} for SCRAM authentication. In the context of a SSL connection, the crypto part is initialized. But that would *not* be the case of a connection in a non-SSL context. Fortunately, and after looking at the OpenSSL code (fips_md_init_ctx, HASH_UPDATE, etc.), there is no sign of locking handling or errors, so I think that we are safe there. Now comes the case of HEAD that uses EVP for MD5 and SHA256. A SSL connection would initialize the crypto part, but that does not happen for a non-SSL connection. So, logically, one could run into issues if using MD5 or SCRAM with OpenSSL <= 1.0.2 (pgbench with a high number of threads does not complain by the way), and we are not yet in a stage where we should drop this version either, even if it has been EOL'd by upstream at the end of 2019. We have the code in place to properly initialize the crypto locking in libpq with ENABLE_THREAD_SAFETY, but the root of the issue is that the SSL and crypto initializations are grouped together. What we need to do here is to split those phases of the initialization so as non-SSL connections can use the crypto part properly, as pqsecure_initialize gets only called now when libpq negotiates SSL with the postmaster. It seems to me that we should make sure of a proper reset of the crypto part within pqDropConnection(), while the initialization needs to happen in PQconnectPoll(). -- Michael --A4BPO1AEM/bXA+3C Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmAtyxUACgkQnvQgOdby QH0rABAAj6aPKbcLJZOQZ+/9/vsgAkOGlyIQzCsuBk/TugQTim7T29reNfcne929 V7ixSvwfrRkR+Tvr7EfFFHjuxSkLyY+OZTzCRx4jEwWX5fOfvPokfH6w5StGvD47 cg4CayYbrMxzCGiRhgpalHO8uF0TtWwkwWATJa+jQC8+rxJi9YKk2k3bQ77IzW3y QyZn4WfKk+LefxXUyOrguxfxGLTMO/2bNaMyiscIWoXIUTdNb5eGbsfETq/8u9rw lrQ1dXMcXQAZ+q3oGhzYe+Gc1FmqcHSzQjB0i0v0gzx7Btoc5dnPNN0ug72glquv N7gLhZWLNejXBXLwV7DghPkhmK41AVNOqAQ3QIYXaLQrqlne3QYJCEENaQhe53MY yWX0xuaAYKbXPM6LEagsoWUscfAeCS5OWngnbH2/OFXcbcGwDyFMe87ivEglWTQL 6zr0lXRpJI/sfE1jiPiN9qy2nNuLzc42jbK61QOnVntdxsqq1WXzPjKajo8wXLlc iFB09Qys8j7r1pLmc4m9BfCvDHrDUGK+UUzVJbAqbV5TWT+QgSmWXE/XlJ8RCYja iNqxCXXgSr6PP7G8q+obwlcx8LpdRDS5/rLaB97cIZBCDCEz31A+p2x2/+4tYLkK bUCQbgp/QpyomvbrvVv5Yn9WYJlsOyaoMhh9+9bV7broE9URjIA= =1Rbf -----END PGP SIGNATURE----- --A4BPO1AEM/bXA+3C--