pgsql: plperl: Refuse to free a prepared query that is being executed.

From: Noah Misch <noah(at)leadboat(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: plperl: Refuse to free a prepared query that is being executed.
Date: 2026-11-09 14:21:53
Message-ID: E1xQQFt-000000003RP-1AFK@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

plperl: Refuse to free a prepared query that is being executed.

Previously, spi_freeplan() released a prepared query's plan, and the
memory context holding it, without checking whether the query was
still running. Executing a prepared query can re-enter plperl, and the
re-entered code could free the query being run. That memory context
also holds state the running execution still needs, so freeing it
midway left that execution using freed memory and crashed the backend.
plperl is trusted, so any role able to create a function can reach
this.

Fix this by counting the executions in progress for each prepared
query and refusing spi_freeplan() while that count is above zero. A
count rather than a flag is needed because the same query can be
running more than once at a time.

Backpatch to all supported branches, where spi_freeplan() has always
freed the plan unconditionally.

Reported-by: Zheng Yu <zheng(at)depthfirst(dot)com>
Reviewed-by: Andrew Dunstan <andrew(at)dunslane(dot)net>
Backpatch-through: 14
Security: CVE-2026-116307

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/31d908432bd48ae7cd59260e32461a34ce5ec22b
Author: Masahiko Sawada <msawada(at)postgresql(dot)org>

Modified Files
--------------
src/pl/plperl/expected/plperl.out | 34 +++++++++++++++++++++++++
src/pl/plperl/plperl.c | 53 ++++++++++++++++++++++++++++++++++++---
src/pl/plperl/sql/plperl.sql | 34 +++++++++++++++++++++++++
3 files changed, 117 insertions(+), 4 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Noah Misch 2026-11-09 14:21:54 pgsql: Fix use-after-free in pg_show_all_settings().
Previous Message Noah Misch 2026-11-09 14:21:52 pgsql: Avoid use-after-free hazards in re-entrant use of pltcl.