Avoid out-of-bounds accesses in FDW batch inserts.

Foreign data wrappers are expected to return the number of
successfully inserted tuples via ExecForeignBatchInsert's numSlots
parameter.  Both the executor and COPY FROM use that number as the
length of the slot array.  If the FDW reports more rows than we
sent, the backend proceeds to read past the end of the array.
(postgres_fdw is acutely affected: if the remote table has an
INSERT rule, the reported count may be higher.)  To fix, clamp the
reported count to the number of rows that were sent.

This is a stopgap.  Clamping keeps statements that happen to
succeed today succeeding, at the cost of misreporting the number
of rows.  A better solution will likely involve reworking the API,
but that is left as a future exercise.

Reported-by: Sumedh Pathak <[email protected]>
Reported-by: Sven Klemm <[email protected]>
Author: Nathan Bossart <[email protected]>
Reviewed-by: Bertrand Drouvot <[email protected]>
Reviewed-by: Jacob Champion <[email protected]>
Reviewed-by: Tomas Vondra <[email protected]>
Reviewed-by: Etsuro Fujita <[email protected]>
Security: CVE-2026-120149
Backpatch-through: 14

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/1e0a55a0fb26e13a6f0b1da87dea06957780b9e4
Author: Nathan Bossart <[email protected]>

Modified Files
--------------
contrib/postgres_fdw/expected/postgres_fdw.out | 18 ++++++++++++++++++
contrib/postgres_fdw/sql/postgres_fdw.sql      | 15 +++++++++++++++
src/backend/commands/copyfrom.c                |  3 +++
src/backend/executor/nodeModifyTable.c         |  9 +++++++++
4 files changed, 45 insertions(+)

Reply via email to