pgsql: Fix use-after-free issues due to drop of portals being set up

From: Noah Misch <noah(at)leadboat(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix use-after-free issues due to drop of portals being set up
Date: 2026-11-09 14:22:09
Message-ID: E1xQQG9-000000003zu-18G3@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix use-after-free issues due to drop of portals being set up

Between CreatePortal() and PortalStart(), a portal can run user-defined
code due to planning and executor startup, like SQL functions. A
function attempting to close a portal would free it, without its creator
knowing about it. This could lead to use-after-free issues when trying
to use portals that have already been closed.

With this change, PortalDrop() now refuses to act on portals marked as
PORTAL_NEW or PORTAL_DEFINED. CLOSE ALL skips them, as it does for
active portals. Note that AtAbort_Portals() (for transaction aborts)
and AtSubAbort_Portals() (for subtransaction aborts) now puts such
portals in a failed state so as they can be cleaned up during error
cleanup.

Two sets of tests are added:
- Portal manipulations during planning and executor startup.
- Portals and Bind messages, relying on injection points to check
various transaction cases with portals marked as either PORTAL_NEW or
PORTAL_DEFINED, where the on-error cleanup of portals marked as failed
matters.

Reported-by: Tomas Vondra <tomas(at)vondra(dot)me>
Author: Michael Paquier <michael(at)paquier(dot)xyz>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Backpatch-through: 14
Security: CVE-2026-116332

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/c65fee830f938e08a6f78034078e0253d65cd596
Author: Michael Paquier <michael(at)paquier(dot)xyz>

Modified Files
--------------
src/backend/utils/mmgr/portalmem.c | 40 +++++++++-
src/test/regress/expected/portals_inflight.out | 103 +++++++++++++++++++++++++
src/test/regress/parallel_schedule | 2 +-
src/test/regress/sql/portals_inflight.sql | 94 ++++++++++++++++++++++
4 files changed, 234 insertions(+), 5 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Noah Misch 2026-11-09 14:22:10 pgsql: Add tests for find_composite_type_dependencies() bugs
Previous Message Noah Misch 2026-11-09 14:22:08 pgsql: Fix potential buffer overrun in ts_rank_cd().