Subject: Re: improve ssl error code, 2147483650

From: Tom Lane <[email-masked]>
Date: Thu, 07 Mar 2024 12:52:12 -0500

Stephen Frost <[email-masked]> writes:
> * Heikki Linnakangas ([email-masked]) wrote:
>> That's pretty unfortunate. As typical with OpenSSL, this stuff is not very
>> well documented, but I think we could do something like this in
>> SSLerrmessage():
>> 
>> if (ERR_SYSTEM_ERROR(e))
>> errreason = strerror(ERR_GET_REASON(e));
>> 
>> ERR_SYSTEM_ERROR only exists in OpenSSL 3.0 and above, and the only
>> documentation I could find was in this one obscure place in the man pages: https://www.openssl.org/docs/man3.2/man3/BIO_dgram_get_local_addr_enable.html.
>> But as a best-effort thing, it would still be better than "SSL error code
>> 2147483650".

> Agreed that it doesn't seem well documented.  I was trying to figure out
> what the 'right' answer here was myself and not having much success.  If
> the above works, then +1 to that.

My reaction as well --- I was just gearing up to test this idea,
unless one of you are already on it?

			regards, tom lane

---

From: Tom Lane <[email-masked]>
Date: Thu, 07 Mar 2024 14:58:31 -0500

> unless one of you are already on it?

I've confirmed that this:

diff --git a/src/backend/libpq/be-secure-openssl.c b/src/backend/libpq/be-secure-openssl.c
index e12b1cc9e3..47eee4b59d 100644
--- a/src/backend/libpq/be-secure-openssl.c
+++ b/src/backend/libpq/be-secure-openssl.c
@@ -1363,6 +1363,10 @@ SSLerrmessage(unsigned long ecode)
 	errreason = ERR_reason_error_string(ecode);
 	if (errreason != NULL)
 		return errreason;
+#ifdef ERR_SYSTEM_ERROR
+	if (ERR_SYSTEM_ERROR(ecode))
+		return strerror(ERR_GET_REASON(ecode));
+#endif
 	snprintf(errbuf, sizeof(errbuf), _("SSL error code %lu"), ecode);
 	return errbuf;
 }

seems to be enough to fix the problem on OpenSSL 3.1.1.  The #ifdef
is needed to avoid compile failure against OpenSSL 1.1.1 --- but that
version doesn't have the problem, so we don't need to sweat.

This could probably do with a comment, and we need to propagate
the fix into libpq's copy of the function too.  Barring objections,
I'll take care of that and push it later today.

			regards, tom lane