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 <[email protected]>
Reviewed-by: Andrew Dunstan <[email protected]>
Backpatch-through: 14
Security: CVE-2026-116307

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/b4ebd7240bbdfde6cc80255f8fddbe364bf7a49c
Author: Masahiko Sawada <[email protected]>

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(-)

Reply via email to