Grab bag of smaller OpenSSL fixups

Started by Daniel Gustafsson2 months ago15 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

This thread has been committed, so CI has stopped here. Anything below is the last result it produced.

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t253065
psql -h localhost -U postgres

Built from patchset v11 (message #11), July 28, 2026 at 10:44 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t253065_11 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t253065_11 && git checkout t253065_11

Patchset v11 (message #11) is on t253065_11

Jump to latest
#1Daniel Gustafsson
daniel@yesql.se

I've been collecting a few small fixups to the OpenSSL code and figured it was
time to send them over to keep the number of fixes more manageable.

* 0001: Remove the use of static variable to not interfere with the future
multithreading efforts
* 0002: Replace SSLv23_method with TLS_method, it has been an alias to the new
name since 2015 in OpenSSL
* 0003: Replace deprecated API X509_NAME_get_text_by_NID with the recommended
alternative X509_NAME_get_index_by_NID + X509_NAME_get_entry. The API was
deprecated in OpenSSL 4 and risk getting removed, with the alternatives being
available in all supported versions.
* 0004: Fix TLS protocol detection. We have been using the wrong macro for
feature test for a long time, replace with the actual feature test macro.
(There is no protocol downgrade here, it will error out when misconfiguring).

None of these change existing functionality or introduce new functionality.

--
Daniel Gustafsson

Attachments:

0004-ssl-Use-the-correct-feature-macros-for-TLS-protocol-.patchapplication/octet-stream; name=0004-ssl-Use-the-correct-feature-macros-for-TLS-protocol-.patch; x-unix-mode=0644Download+12-7
0003-ssl-Replace-deprecated-API-to-get-commonName.patchapplication/octet-stream; name=0003-ssl-Replace-deprecated-API-to-get-commonName.patch; x-unix-mode=0644Download+19-13
0002-ssl-Use-TLS_method-instead-of-deprecated-SSLv23_meth.patchapplication/octet-stream; name=0002-ssl-Use-TLS_method-instead-of-deprecated-SSLv23_meth.patch; x-unix-mode=0644Download+3-9
0001-ssl-Remove-static-var-tracking-tls_init_hook-warning.patchapplication/octet-stream; name=0001-ssl-Remove-static-var-tracking-tls_init_hook-warning.patch; x-unix-mode=0644Download+7-8
#2Tristan Partin
tristan@partin.io
In reply to: Daniel Gustafsson (#1)
Re: Grab bag of smaller OpenSSL fixups

On Mon Jul 13, 2026 at 2:17 PM UTC, Daniel Gustafsson wrote:

I've been collecting a few small fixups to the OpenSSL code and figured it was
time to send them over to keep the number of fixes more manageable.

* 0001: Remove the use of static variable to not interfere with the future
multithreading efforts
* 0002: Replace SSLv23_method with TLS_method, it has been an alias to the new
name since 2015 in OpenSSL
* 0003: Replace deprecated API X509_NAME_get_text_by_NID with the recommended
alternative X509_NAME_get_index_by_NID + X509_NAME_get_entry. The API was
deprecated in OpenSSL 4 and risk getting removed, with the alternatives being
available in all supported versions.
* 0004: Fix TLS protocol detection. We have been using the wrong macro for
feature test for a long time, replace with the actual feature test macro.
(There is no protocol downgrade here, it will error out when misconfiguring).

None of these change existing functionality or introduce new functionality.

The patches look good. I did have one comment in patch 0001. For the
hasWarned argument to init_host_context, do we you think we should add
an Assert(hasWarned) or add a pg_attribute_nonnull(3)? I see we
dereference the pointer without first checking for its validity. I know
it isn't common in Postgres source code to add such protections, but
I figured I would point it out anyway.

--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

#3Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Daniel Gustafsson (#1)
Re: Grab bag of smaller OpenSSL fixups

On 7/13/26 16:16, Daniel Gustafsson wrote:

I've been collecting a few small fixups to the OpenSSL code and figured it was
time to send them over to keep the number of fixes more manageable.

Nice!

* 0001: Remove the use of static variable to not interfere with the future
multithreading efforts

Looks good!

* 0002: Replace SSLv23_method with TLS_method, it has been an alias to the new
name since 2015 in OpenSSL

Looks good!

* 0003: Replace deprecated API X509_NAME_get_text_by_NID with the recommended
alternative X509_NAME_get_index_by_NID + X509_NAME_get_entry. The API was
deprecated in OpenSSL 4 and risk getting removed, with the alternatives being
available in all supported versions.

You should declare entry in the inner scope, not in the function scope.

The comment should maybe also add that entry and peer_cn_asn1 too are
OpenSSL owned pointers. Just saying it for peer_cn_internal seems a bit
odd to me.

Also this is subjective but I am not personally a fan of
MemoryContextAllocZero() + memcpy(). I prefer MemoryContextAlloc() +
memcpy() + peer_cn[len] = 0. I feel that explains better what is going
on. I have also seen us use strncpy() for this purpose which I guess
works too.

* 0004: Fix TLS protocol detection. We have been using the wrong macro for
feature test for a long time, replace with the actual feature test macro.
(There is no protocol downgrade here, it will error out when misconfiguring).

Looks good! That was a funny and easy to make mistake :)

--
Andreas Karlsson
Percona

#4Daniel Gustafsson
daniel@yesql.se
In reply to: Tristan Partin (#2)
Re: Grab bag of smaller OpenSSL fixups

On 13 Jul 2026, at 22:38, Tristan Partin <tristan@partin.io> wrote:

The patches look good.

Thanks for reviewing!

I did have one comment in patch 0001. For the
hasWarned argument to init_host_context, do we you think we should add
an Assert(hasWarned) or add a pg_attribute_nonnull(3)? I see we
dereference the pointer without first checking for its validity. I know
it isn't common in Postgres source code to add such protections, but
I figured I would point it out anyway.

Given that this is a very niche static function with very few callsites, I'm
not too worried about it being called with NULL. That being said, defensive
programming isn't just to defend against what we know but also what we don't
know about so I'm not against adding such protections. There are likely many
other functions in this file which could benefit more from pg_attribute_nonnull
but we also need to start somewhere.

--
Daniel Gustafsson

#5Daniel Gustafsson
daniel@yesql.se
In reply to: Andreas Karlsson (#3)
Re: Grab bag of smaller OpenSSL fixups

On 14 Jul 2026, at 00:25, Andreas Karlsson <andreas@proxel.se> wrote:
On 7/13/26 16:16, Daniel Gustafsson wrote:

* 0003: Replace deprecated API X509_NAME_get_text_by_NID with the recommended
alternative X509_NAME_get_index_by_NID + X509_NAME_get_entry. The API was
deprecated in OpenSSL 4 and risk getting removed, with the alternatives being
available in all supported versions.

You should declare entry in the inner scope, not in the function scope.

Ah, yes. It was already in the inner scope, but it should've been in
inner-inner scope =)

The comment should maybe also add that entry and peer_cn_asn1 too are OpenSSL owned pointers. Just saying it for peer_cn_internal seems a bit odd to me.

The other pointers are of OpenSSL data types and those are rarely freed unless
a matching _new or _dup method has been called, whereas peer_cn_internal is a
char pointer extracted with _get0_data. To me the latter seemed more
reasonable to believe it was caller owned, but I can remove the comment
altogether if it's deemed useless.

Also this is subjective but I am not personally a fan of MemoryContextAllocZero() + memcpy(). I prefer MemoryContextAlloc() + memcpy() + peer_cn[len] = 0. I feel that explains better what is going on. I have also seen us use strncpy() for this purpose which I guess works too.

We can't use strncpy or strlcpy here since OpenSSL had defined the string as
const unsigned char * so we'd get pointer-sign compiler warnings unless we do
trickery, which seems not worth it. I don't have strong opinions on how to
zero the buffer so changed to your proposal.

The attached also contains the non-null guards from Tristans review upthread.

--
Daniel Gustafsson

Attachments:

v2-0001-ssl-Remove-static-var-tracking-tls_init_hook-warn.patchapplication/octet-stream; name=v2-0001-ssl-Remove-static-var-tracking-tls_init_hook-warn.patch; x-unix-mode=0644Download+10-8
v2-0002-ssl-Use-TLS_method-instead-of-deprecated-SSLv23_m.patchapplication/octet-stream; name=v2-0002-ssl-Use-TLS_method-instead-of-deprecated-SSLv23_m.patch; x-unix-mode=0644Download+3-9
v2-0003-ssl-Replace-deprecated-API-to-get-commonName.patchapplication/octet-stream; name=v2-0003-ssl-Replace-deprecated-API-to-get-commonName.patch; x-unix-mode=0644Download+18-11
v2-0004-ssl-Use-the-correct-feature-macros-for-TLS-protoc.patchapplication/octet-stream; name=v2-0004-ssl-Use-the-correct-feature-macros-for-TLS-protoc.patch; x-unix-mode=0644Download+12-7
#6Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Daniel Gustafsson (#5)
Re: Grab bag of smaller OpenSSL fixups

On 7/14/26 10:35, Daniel Gustafsson wrote:

The comment should maybe also add that entry and peer_cn_asn1 too are OpenSSL owned pointers. Just saying it for peer_cn_internal seems a bit odd to me.

The other pointers are of OpenSSL data types and those are rarely freed unless
a matching _new or _dup method has been called, whereas peer_cn_internal is a
char pointer extracted with _get0_data. To me the latter seemed more
reasonable to believe it was caller owned, but I can remove the comment
altogether if it's deemed useless.

I don't have a strong opinion here so do whatever you feel like, but at
least to me the current comment does not add much value.

The attached also contains the non-null guards from Tristans review upthread.

My compiler did not like the mix of both assert and nonnull attribute.

In file included from ../postgresql/src/include/postgres.h:48,
from
../postgresql/src/backend/libpq/be-secure-openssl.c:17:
../postgresql/src/backend/libpq/be-secure-openssl.c: In function
‘init_host_context’:
../postgresql/src/include/c.h:1018:20: warning: ‘nonnull’ argument
‘hasWarned’ compared to NULL [-Wnonnull-compare]
1018 | if (!(condition)) \
| ^
../postgresql/src/backend/libpq/be-secure-openssl.c:613:9: note: in
expansion of macro ‘Assert’
613 | Assert(hasWarned);
| ^~~~~~

--
Andreas Karlsson
Percona

#7Daniel Gustafsson
daniel@yesql.se
In reply to: Andreas Karlsson (#6)
Re: Grab bag of smaller OpenSSL fixups

On 14 Jul 2026, at 14:57, Andreas Karlsson <andreas@proxel.se> wrote:

My compiler did not like the mix of both assert and nonnull attribute.

Interesting, mine didn't complain, but it's also pretty redundant to have both
so removing the Assert and keeping the cost compile-time rather than runtime
seem the right move.

--
Daniel Gustafsson

#8Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Daniel Gustafsson (#7)
Re: Grab bag of smaller OpenSSL fixups

On 7/14/26 15:17, Daniel Gustafsson wrote:

On 14 Jul 2026, at 14:57, Andreas Karlsson <andreas@proxel.se> wrote:

My compiler did not like the mix of both assert and nonnull attribute.

Interesting, mine didn't complain, but it's also pretty redundant to have both
so removing the Assert and keeping the cost compile-time rather than runtime
seem the right move.

If you are curious I am running gcc 13.3.

Compiler:

gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0

Flags according to meson compile --verbose:

cc -Isrc/backend/postgres_lib.a.p -Isrc/include
-I../postgresql/src/include -I/usr/include/libxml2
-I/usr/include/security -fdiagnostics-color=always
-D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -fno-strict-aliasing
-fwrapv -fexcess-precision=standard -D_GNU_SOURCE -Wpointer-arith
-Werror=vla -Wmissing-format-attribute -Wcast-function-type
-Wshadow=compatible-local -Wformat-security -Wmissing-prototypes
-Wold-style-declaration -Wold-style-definition -Wstrict-prototypes
-Wimplicit-fallthrough=5 -Wdeclaration-after-statement
-Wno-format-truncation -Wno-stringop-truncation -DUSE_VALGRIND -fPIC
-isystem /usr/include/mit-krb5 -pthread -DBUILDING_DLL -MD -MQ
src/backend/postgres_lib.a.p/libpq_be-secure-openssl.c.o -MF
src/backend/postgres_lib.a.p/libpq_be-secure-openssl.c.o.d -o
src/backend/postgres_lib.a.p/libpq_be-secure-openssl.c.o -c
../postgresql/src/backend/libpq/be-secure-openssl.c

--
Andreas Karlsson
Percona

#9Tristan Partin
tristan@partin.io
In reply to: Daniel Gustafsson (#4)
Re: Grab bag of smaller OpenSSL fixups

On Tue Jul 14, 2026 at 7:49 AM UTC, Daniel Gustafsson wrote:

On 13 Jul 2026, at 22:38, Tristan Partin <tristan@partin.io> wrote:
I did have one comment in patch 0001. For the
hasWarned argument to init_host_context, do we you think we should add
an Assert(hasWarned) or add a pg_attribute_nonnull(3)? I see we
dereference the pointer without first checking for its validity. I know
it isn't common in Postgres source code to add such protections, but
I figured I would point it out anyway.

Given that this is a very niche static function with very few callsites, I'm
not too worried about it being called with NULL. That being said, defensive
programming isn't just to defend against what we know but also what we don't
know about so I'm not against adding such protections. There are likely many
other functions in this file which could benefit more from pg_attribute_nonnull
but we also need to start somewhere.

Indeed. There are places all over the codebase that would likely benefit
from pg_attribute_nonnull. Choosing when to add the annotation takes
a bit of thought.

--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

#10Tristan Partin
tristan@partin.io
In reply to: Daniel Gustafsson (#5)
Re: Grab bag of smaller OpenSSL fixups

On Tue Jul 14, 2026 at 8:35 AM UTC, Daniel Gustafsson wrote:

On 14 Jul 2026, at 00:25, Andreas Karlsson <andreas@proxel.se> wrote:
On 7/13/26 16:16, Daniel Gustafsson wrote:

* 0003: Replace deprecated API X509_NAME_get_text_by_NID with the recommended
alternative X509_NAME_get_index_by_NID + X509_NAME_get_entry. The API was
deprecated in OpenSSL 4 and risk getting removed, with the alternatives being
available in all supported versions.

You should declare entry in the inner scope, not in the function scope.

Ah, yes. It was already in the inner scope, but it should've been in
inner-inner scope =)

The comment should maybe also add that entry and peer_cn_asn1 too are OpenSSL owned pointers. Just saying it for peer_cn_internal seems a bit odd to me.

The other pointers are of OpenSSL data types and those are rarely freed unless
a matching _new or _dup method has been called, whereas peer_cn_internal is a
char pointer extracted with _get0_data. To me the latter seemed more
reasonable to believe it was caller owned, but I can remove the comment
altogether if it's deemed useless.

Also this is subjective but I am not personally a fan of MemoryContextAllocZero() + memcpy(). I prefer MemoryContextAlloc() + memcpy() + peer_cn[len] = 0. I feel that explains better what is going on. I have also seen us use strncpy() for this purpose which I guess works too.

We can't use strncpy or strlcpy here since OpenSSL had defined the string as
const unsigned char * so we'd get pointer-sign compiler warnings unless we do
trickery, which seems not worth it. I don't have strong opinions on how to
zero the buffer so changed to your proposal.

I wonder if it is worth leaving your justification in a comment. Other
than that, nothing to add.

--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

#11Daniel Gustafsson
daniel@yesql.se
In reply to: Tristan Partin (#10)
Re: Grab bag of smaller OpenSSL fixups

On 14 Jul 2026, at 22:50, Tristan Partin <tristan@partin.io> wrote:

I wonder if it is worth leaving your justification in a comment. Other
than that, nothing to add.

Not sure if it's all that interesting, I did however add a function comment on
ssl_init_context which explains the hasWarned parameter as that was lacking.
The attached v3 removes the openssl-owned comment and fixed the compiler
warning, both mentioned upthread.

--
Daniel Gustafsson

Attachments:

t253065_11
v3-0001-ssl-Remove-static-var-tracking-tls_init_hook-warn.patchapplication/octet-stream; name=v3-0001-ssl-Remove-static-var-tracking-tls_init_hook-warn.patch; x-unix-mode=0644Download+22-8
v3-0002-ssl-Use-TLS_method-instead-of-deprecated-SSLv23_m.patchapplication/octet-stream; name=v3-0002-ssl-Use-TLS_method-instead-of-deprecated-SSLv23_m.patch; x-unix-mode=0644Download+3-9
v3-0003-ssl-Replace-deprecated-API-to-get-commonName.patchapplication/octet-stream; name=v3-0003-ssl-Replace-deprecated-API-to-get-commonName.patch; x-unix-mode=0644Download+12-11
v3-0004-ssl-Use-the-correct-feature-macros-for-TLS-protoc.patchapplication/octet-stream; name=v3-0004-ssl-Use-the-correct-feature-macros-for-TLS-protoc.patch; x-unix-mode=0644Download+12-7
#12Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Daniel Gustafsson (#11)
Re: Grab bag of smaller OpenSSL fixups

On 7/15/26 16:20, Daniel Gustafsson wrote:

On 14 Jul 2026, at 22:50, Tristan Partin <tristan@partin.io> wrote:

I wonder if it is worth leaving your justification in a comment. Other
than that, nothing to add.

Not sure if it's all that interesting, I did however add a function comment on
ssl_init_context which explains the hasWarned parameter as that was lacking.
The attached v3 removes the openssl-owned comment and fixed the compiler
warning, both mentioned upthread.

The patches all look good now.

Andreas

#13Daniel Gustafsson
daniel@yesql.se
In reply to: Andreas Karlsson (#12)
Re: Grab bag of smaller OpenSSL fixups

On 26 Jul 2026, at 16:12, Andreas Karlsson <andreas@proxel.se> wrote:

On 7/15/26 16:20, Daniel Gustafsson wrote:

On 14 Jul 2026, at 22:50, Tristan Partin <tristan@partin.io> wrote:
I wonder if it is worth leaving your justification in a comment. Other
than that, nothing to add.

Not sure if it's all that interesting, I did however add a function comment on
ssl_init_context which explains the hasWarned parameter as that was lacking.
The attached v3 removes the openssl-owned comment and fixed the compiler
warning, both mentioned upthread.

The patches all look good now.

Thanks everyone for review, I'll go ahead applying these.

--
Daniel Gustafsson

#14Yilin Zhang
jiezhilove@126.com
In reply to: Daniel Gustafsson (#13)
Re: Grab bag of smaller OpenSSL fixups

At 2026-07-27 16:50:44, "Daniel Gustafsson" <daniel@yesql.se> wrote:

On 26 Jul 2026, at 16:12, Andreas Karlsson <andreas@proxel.se> wrote:

On 7/15/26 16:20, Daniel Gustafsson wrote:

On 14 Jul 2026, at 22:50, Tristan Partin <tristan@partin.io> wrote:
I wonder if it is worth leaving your justification in a comment. Other
than that, nothing to add.

Not sure if it's all that interesting, I did however add a function comment on
ssl_init_context which explains the hasWarned parameter as that was lacking.
The attached v3 removes the openssl-owned comment and fixed the compiler
warning, both mentioned upthread.

The patches all look good now.

Thanks everyone for review, I'll go ahead applying these.

Hi,
I have a minor comment on the v3 patch.

In v3-0004-ssl-Use-the-correct-feature-macros-for-TLS-protoc.patch:
diff --git a/src/backend/libpq/be-secure-openssl.c b/src/backend/libpq/be-secure-openssl.c
index 00d7519957d..aaea6bedfca 100644
--- a/src/backend/libpq/be-secure-openssl.c
+++ b/src/backend/libpq/be-secure-openssl.c
@@ -2421,21 +2421,25 @@ ssl_protocol_version_to_openssl(int v)
 		case PG_TLS_ANY:
 			return 0;
 		case PG_TLS1_VERSION:
+#ifndef OPENSSL_NO_TLS1
 			return TLS1_VERSION;
+#else
+			break;
+#endif
 		case PG_TLS1_1_VERSION:
-#ifdef TLS1_1_VERSION
+#ifndef OPENSSL_NO_TLS1_1
 			return TLS1_1_VERSION;
 #else
 			break;
 #endif
 		case PG_TLS1_2_VERSION:
-#ifdef TLS1_2_VERSION
+#ifndef OPENSSL_NO_TLS1_2
 			return TLS1_2_VERSION;
 #else
 			break;
 #endif
 		case PG_TLS1_3_VERSION:
-#ifdef TLS1_3_VERSION
+#ifdef OPENSSL_NO_TLS1_3
 			return TLS1_3_VERSION;
 #else
 			break;

For all other protocol versions (TLS 1.0/1.1/1.2, both frontend and backend),
we uniformly use `#ifndef OPENSSL_NO_TLSx` — return if the library supports the protocol.
Only the backend TLS 1.3 path uses `#ifdef OPENSSL_NO_TLS1_3` — return when the library lacks support.
I believe this is a one-character mistake introduced during copy-paste: `ifdef` was not changed to `ifndef`.

Best regards,

--

Yilin Zhang

#15Daniel Gustafsson
daniel@yesql.se
In reply to: Yilin Zhang (#14)
Re: Grab bag of smaller OpenSSL fixups

On 28 Jul 2026, at 09:47, Yilin Zhang <jiezhilove@126.com> wrote:

For all other protocol versions (TLS 1.0/1.1/1.2, both frontend and backend),
we uniformly use `#ifndef OPENSSL_NO_TLSx` — return if the library supports the protocol.
Only the backend TLS 1.3 path uses `#ifdef OPENSSL_NO_TLS1_3` — return when the library lacks support.
I believe this is a one-character mistake introduced during copy-paste: `ifdef` was not changed to `ifndef`.

Nice catch, thanks! Fixed.

--
Daniel Gustafsson