Validate all connection options before using libpqsrv_connect*

Connection options passed to dblink(), dblink_connect[_u](), and
dblink_exec() didn't flow through the same validation path as options
which were set via foreign server definitions. Additionally, connection
options added to subscriptions didn't go through any validation except
for the password check. This allowed the sslkeylogfile parameter and
all oauth_* parameters, both introduced in PG18, to be set by relatively
unprivileged users.

Add libpqsrv sanitization functions which block unsafe and unknown
connection options. These are called before libpqsrv_connect() and its
variants. (Extensions which use those functions are encouraged to do the
same.) walrcv_connect() makes an exception to this rule for
primary_conninfo, which is still allowed to contain any parameters.

To reduce the size of the patchset, the implementation has the following
drawbacks, which we hope to improve after release:

- The superuser is also restricted.
- The sanitization functions are not invoked when administering foreign
  server options, or when modifying subscriptions without connecting, to
  avoid interfering with pg_upgrade and dump/restore. The restrictions
  will be applied later, at connection time, which may be a surprise.

Backpatch all the way down. Though the new options aren't present in
older libpqs, running an older server with a newer libpq is common. The
tests for the newer options unfortunately cannot be easily backpatched
past PG18.

Reported-by: Andrey Krasichkov <[email protected]>
Reported-by: b.altawil <[email protected]>
Reported-by: Zsolt Parragi <[email protected]>
Suggested-by: Noah Misch <[email protected]> (sslkeylogfile part)
Reviewed-by: Noah Misch <[email protected]> (sslkeylogfile part)
Reviewed-by: Andres Freund <[email protected]> (PG14/15 backpatch)
Backpatch-through: 14
Security: CVE-2026-14661

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/0adbf6bb1f4b5d7455ba84bec7bf5d4538487bf9
Author: Jacob Champion <[email protected]>

Modified Files
--------------
contrib/dblink/dblink.c                            |  18 ++
contrib/dblink/expected/dblink.out                 |  22 ++-
contrib/dblink/sql/dblink.sql                      |  19 +-
contrib/postgres_fdw/connection.c                  |  13 ++
contrib/postgres_fdw/expected/postgres_fdw.out     |   6 +
contrib/postgres_fdw/sql/postgres_fdw.sql          |   5 +
src/backend/libpq/Makefile                         |   1 +
src/backend/libpq/be-fe-helpers.c                  | 198 +++++++++++++++++++++
src/backend/libpq/meson.build                      |   1 +
.../libpqwalreceiver/libpqwalreceiver.c            |  43 ++++-
src/bin/pg_upgrade/Makefile                        |   6 +-
src/bin/pg_upgrade/t/002_pg_upgrade.pl             |  22 +++
src/include/libpq/libpq-be-fe-helpers.h            |  72 ++++++++
.../recovery/t/040_standby_failover_slots_sync.pl  |  10 +-
src/test/regress/expected/subscription.out         |  30 ++++
src/test/regress/sql/subscription.sql              |  22 +++
src/test/subscription/t/027_nosuperuser.pl         |  21 +++
17 files changed, 500 insertions(+), 9 deletions(-)

Reply via email to