PostgreSQL 17: Bug in libpq when libpq is dlopened/closed multiple times

From: Daniel Schreiber <daniel(dot)schreiber(at)hrz(dot)tu-chemnitz(dot)de>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: PostgreSQL 17: Bug in libpq when libpq is dlopened/closed multiple times
Date: 2026-04-15 15:55:01
Message-ID: f7fe39b3-7e99-4939-8852-07350549161d@hrz.tu-chemnitz.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Dear PostgreSQL developers,

my colleagues and I probably found a bug in libpq when libpq is dlopened
and closed multiple times during the lifetime of a process. In our setup
we use a PAM module which links to libpq. The process using PAM is
linked against openssl, so openssl is loaded during the complete
lifetime of the process whereas libpq is loaded only during PAM
authentication (and unloaded when PAM has finished).

We observed the bug on a Debian 13 system using libpq from Debian. To
reproduce the bug, compile the attached c file using the following gcc
command line:

gcc libpq1-dlopen.c -Wall -Wextra -o libpq1-dlopen -ldl -lssl -lcrypto

Then run the binary with a postgresql connection string as an argument.
The connection string has to include 'sslmode=require'. The program will
in a loop try to dlopen libpq, then connect to the server, finish the
connection and unload libpq.

According to our findings every time a connection is established after
dlopening libpq one of the 127 available BIO_METHOD structures in
OpenSSL is consumed:
https://github.com/postgres/postgres/blob/REL_17_9/src/interfaces/libpq/fe-secure-openssl.c#L1987

So after 127 cycles registering the callbacks fails and in our use case
the application is no longer able to authenticate using PAM. As a
workaround we LD_PRELOAD libpq in the application.

I am not subscribed yet to the mailing list, so please CC me.

Thank you,

Daniel
--
Daniel Schreiber
Facharbeitsgruppe Systemsoftware
Universitaetsrechenzentrum

Technische Universität Chemnitz
Straße der Nationen 62 (Raum B303)
09111 Chemnitz
Germany

Tel: +49 371 531 35444

Attachment Content-Type Size
libpq1-dlopen.c text/x-csrc 3.0 KB

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Leendert Gravendeel 2026-04-15 18:56:50 BUG: PL/pgSQL FOREACH misparses variable named "slice" with SLICE clause
Previous Message David G. Johnston 2026-04-15 15:19:02 Re: BUG #19456: # Unit-Labeling Issue: `pg_size_pretty()` Incorrectly Labels Binary Units as Decimal Units