pgsql: Introduce attribute versioning to prevent unsafe deforming

From: Noah Misch <noah(at)leadboat(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Introduce attribute versioning to prevent unsafe deforming
Date: 2026-11-09 14:22:01
Message-ID: E1xQQG1-000000003jI-2cdw@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Introduce attribute versioning to prevent unsafe deforming

Using a tuple descriptor that does not match the "shape" of a composite
datum can lead to crashes, spurious errors and can be used for exploits.
While postgres has code that prevents such mismatches, there are a
number of weaknesses in those protections that are nontrivial to fix,
particularly in the back-branches.

This commit introduces a new safety layer to prevent the worst of the
negative consequences. From now on, composite datums are marked as such
and named composite datums are versioned, allowing detection of
mismatches between a tuple descriptor and the composite during
deforming. In case of mismatches, errors are raised.

For details about how this works, see
src/backend/catalog/README.attver.md.

Note that, over time, we should prevent most of the things that the
safety layer detects at a higher level.

This fixes a number of security relevant composite type confusions.

This is a significant piece of infrastructure grafted onto the existing
code, in a back-patchable way. There unfortunately still are some
compatibility issues due to this commit:

- To get the full protection, extensions need to be rebuilt, due to
DatumGetHeapTupleHeader() not being a macro anymore (but an
out-of-line function). That also means that some extensions built
against a minor version with this change will fail with a linker
failure when loaded in an earlier minor version.

Extensions that have not been recompiled can observe
HeapTupleHeaderGetTypMod() returning parts of the version instead of
-1.

- Upgrading to a minor version with this fix, performing DDL,
downgrading and then upgrading again can cause errors for composite
datums formed while downgraded; DDL while downgraded can additionally
break the version invariants.

- Code looking at pg_attribute.attoptions will see the new pg_attver
key, which could lead to external applications setting this new key
via DDL.

To reduce the impact of that, ALTER COLUMN ... SET (pg_attver = ...)
is ignored.

- Dumps of a server with this change, made by a pg_dump without it,
contain ALTER COLUMN ... SET (pg_attver = ...), which servers without
this change reject. As pg_upgrade uses the target's pg_dump,
upgrading from a minor version with this change to a minor version of
a later major version without it fails.

- Use of TupleDescCopy() is forbidden with tupledescs for named
composites. The new TupleDescCopySize()/TupleDescCopySized() can copy
any descriptor, and verify that the destination is large enough.

- Code that forms datums and then modifies fields in t_choice.t_datum
without removing HEAP_IS_DATUM can trigger spurious errors. To reduce
the impact of that, the version check skips some known cases of such
modifications (see list below for the known effects on extensions).

- DatumGetHeapTupleHeader() will be slower when pre-upgrade tuples are
passed, due to copying those datums before modifying them.

- Tuples returned by heap_form_tuple() are now marked as datum tuples
and, for named composites, version-checked when deformed. Code that
writes such tuples to its own storage needs to clear the marking (via
HeapTupleHeaderClearDatum()).

- heap_modify_tuple() used to copy t_ctid, which is not possible
anymore, due to that containing the datum version. No callers could be
found that need the copy.

At least some of the above should be mentioned in the release notes.

Investigation via github codesearch, a dump of all the extensions on
pgxn, extensions from various distributions, etc, suggests the following
effects on extensions:

- PostGIS does some quite broken things on the upgrade from < 3.6 to
3.6, including corrupting the catalog. This is more noticeable with
these changes. In particular WARNINGs will be emitted and pg_upgrade
will fail, until PostGIS is fixed.

See https://trac.osgeo.org/postgis/ticket/6142

- PL/R (trigger arguments), pg_variables (stored records) and pljs copy
tuples and set the composite datum header fields by hand, which leads
to spurious errors when the result is read. They should use
heap_copy_tuple_as_datum() instead.

- ddlx fails for columns with an explicitly set attribute option, as it
expects at most one option per column. pgcontext refuses to convert
versioned columns, as it rejects columns with any attribute options.

- firebird_fdw spuriously errors out in some cases, due to overwriting
the xmax (but not xmin). Other FDWs clear out xmin, which this commit
uses as a signal that the version information is not usable, but
there's no easy way to detect this in the firebird_fdw case. The fix
would be a one-liner.

- storage_engine's rowcompress table AM stores composite datum-style
tuples, which will raise errors on read if the table schema is
subsequently changed. The fix is trivial.

- pgrx-based extensions will not be protected, even after recompiling,
due to pgrx copying postgres code like heap_getattr(). pgrx will need
to update that copied code.

- pg_strom's arrow FDW bypasses core functionality for more than 100
columns and inserts attributes itself. That does not set the attribute
versions correctly and thus will trigger spurious errors if composites
of that FDW are used.

- timescaledb has a bug causing crashes during chunk creation as part of
DDL commands if attribute options are present. With this change every
new / altered attribute has an attribute option.

A later commit will work around these crashes.

- Citus' distributed aggregation path theoretically can hit the new
error in TupleDescCopy(). However, it looks like Citus' planner does
not ever generate such plans - but the ERROR can be reached by a user
issuing a query with calls to the relevant internal Citus functions.

The fix again is trivial, should it be needed (use
CreateTupleDescCopy() instead of open-coding it).

The new composite_type_mismatch developer option allows superusers to
turn the version mismatch error into a warning, or to disable the check.
Both settings are unsafe.

With this infrastructure in place, additional checks can be
performed (e.g. in heap_datum_check(), heap_datum_update_type()), at the
cost of some complexity and runtime overhead. A subsequent commit adds
those.

Reported-by: Heikki Linnakangas <heikki(dot)linnakangas(at)iki(dot)fi>
Reported-by: Andres Freund <andres(at)anarazel(dot)de>
Reported-by: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
Reported-by: Alexander Lakhin <exclusion(at)gmail(dot)com>
Reported-by: Francesco Verardi <frevadiscor89(at)gmail(dot)com>
Reported-by: Hcamael <baiyjrh(at)gmail(dot)com>
Reported-by: Tomer Fichman <tomer(at)irregular(dot)com>
Reported-by: OpenAI Security Research Team
Reported-by: Peter Geoghegan <pg(at)bowt(dot)ie>
Reported-by: Mehmet Ince @mdisec <mehmet(at)mehmetince(dot)net>
Reported-by: Bryan Green <dbryan(dot)green(at)gmail(dot)com>
Reported-by: Sumedh Pathak <sumedhpathak(at)microsoft(dot)com>
Reported-by: Nicholas Carlini <npc(at)anthropic(dot)com>
Reported-by: Sergey Shinderuk <s(dot)shinderuk(at)postgrespro(dot)ru>
Reported-by: Yuxiao Guo <dllggyx(at)outlook(dot)com>
Reported-by: Nikhil Sontakke <nikhil(at)planetscale(dot)com>
Reported-by: Stephan Lipp <Stephan(at)Adalogics(dot)com>
Reported-by: Daniel Bakker <daniel(at)jackds(dot)nl>
Reviewed-by: Melanie Plageman <melanieplageman(at)gmail(dot)com>
Reviewed-by: Tomas Vondra <tomas(at)vondra(dot)me>
Reviewed-by: Noah Misch <noah(at)leadboat(dot)com>
Reviewed-by: Robert Haas <robertmhaas(at)gmail(dot)com> (parts)
Security: CVE-2026-6460
Backpatch-through: 14

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/cfdd09589b9f19fbc78c244e7868f1af46c460b8
Author: Andres Freund <andres(at)anarazel(dot)de>

Modified Files
--------------
contrib/amcheck/verify_heapam.c | 7 +
doc/src/sgml/catalogs.sgml | 5 +-
doc/src/sgml/config.sgml | 26 ++
src/backend/access/common/heaptuple.c | 467 ++++++++++++++++++++-
src/backend/access/common/indextuple.c | 5 +-
src/backend/access/common/reloptions.c | 110 +++++
src/backend/access/common/tupdesc.c | 273 +++++++++++-
src/backend/access/heap/heapam.c | 16 +
src/backend/access/heap/heaptoast.c | 15 +-
src/backend/access/heap/hio.c | 3 +
src/backend/access/heap/rewriteheap.c | 10 +
src/backend/catalog/README.attver.md | 301 +++++++++++++
src/backend/catalog/catalog.c | 41 ++
src/backend/catalog/heap.c | 318 +++++++++++++-
src/backend/commands/sequence.c | 14 +-
src/backend/commands/tablecmds.c | 114 ++++-
src/backend/executor/execExprInterp.c | 33 +-
src/backend/executor/execSRF.c | 28 ++
src/backend/executor/execTuples.c | 64 ++-
src/backend/executor/spi.c | 21 +-
src/backend/utils/adt/expandedrecord.c | 30 +-
src/backend/utils/adt/jsonfuncs.c | 10 +
src/backend/utils/adt/pg_upgrade_support.c | 46 ++
src/backend/utils/cache/relcache.c | 57 +++
src/backend/utils/cache/typcache.c | 5 +-
src/backend/utils/errcodes.txt | 1 +
src/backend/utils/misc/guc_tables.c | 19 +
src/backend/utils/sort/tuplesortvariants.c | 10 +
src/bin/pg_amcheck/t/004_verify_heapam.pl | 10 +-
src/bin/pg_dump/pg_dump.c | 120 +++++-
src/bin/pg_dump/pg_dump.h | 2 +
src/include/access/htup_details.h | 207 ++++++++-
src/include/access/reloptions.h | 4 +
src/include/access/tupdesc.h | 9 +
src/include/catalog/binary_upgrade.h | 13 +
src/include/catalog/catalog.h | 1 +
src/include/catalog/heap.h | 8 +
src/include/catalog/pg_attribute.h | 9 +
src/include/catalog/pg_class.h | 11 +
src/include/catalog/pg_proc.dat | 5 +
src/include/fmgr.h | 12 +-
src/include/storage/itemptr.h | 9 +
src/include/utils/expandedrecord.h | 1 +
src/pl/plpgsql/src/expected/plpgsql_record.out | 16 +-
src/pl/plpgsql/src/pl_exec.c | 34 +-
src/test/isolation/expected/rowtypes-alter.out | 44 +-
.../spgist_name_ops/expected/spgist_name_ops.out | 6 +-
src/test/regress/expected/rowtypes_alter.out | 454 +++++++++-----------
src/tools/pgindent/typedefs.list | 3 +
49 files changed, 2636 insertions(+), 391 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Noah Misch 2026-11-09 14:22:02 pgsql: Fix PL/Perl to cope with errors thrown from tied structures.
Previous Message Noah Misch 2026-11-09 14:22:00 pgsql: Enforce view row visibility for INSERT ... ON CONFLICT target ro