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 <[email protected]>
Reported-by: Andres Freund <[email protected]>
Reported-by: Andrew Gierth <[email protected]>
Reported-by: Alexander Lakhin <[email protected]>
Reported-by: Francesco Verardi <[email protected]>
Reported-by: Hcamael <[email protected]>
Reported-by: Tomer Fichman <[email protected]>
Reported-by: OpenAI Security Research Team
Reported-by: Peter Geoghegan <[email protected]>
Reported-by: Mehmet Ince @mdisec <[email protected]>
Reported-by: Bryan Green <[email protected]>
Reported-by: Sumedh Pathak <[email protected]>
Reported-by: Nicholas Carlini <[email protected]>
Reported-by: Sergey Shinderuk <[email protected]>
Reported-by: Yuxiao Guo <[email protected]>
Reported-by: Nikhil Sontakke <[email protected]>
Reported-by: Stephan Lipp <[email protected]>
Reported-by: Daniel Bakker <[email protected]>
Reviewed-by: Melanie Plageman <[email protected]>
Reviewed-by: Tomas Vondra <[email protected]>
Reviewed-by: Noah Misch <[email protected]>
Reviewed-by: Robert Haas <[email protected]> (parts)
Security: CVE-2026-6460
Backpatch-through: 14

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/245eb832f88631c986a2dadad2cbe94f57089fd6
Author: Andres Freund <[email protected]>

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_parameters.dat          |   9 +
src/backend/utils/misc/guc_tables.c                |   8 +
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 +
50 files changed, 2634 insertions(+), 391 deletions(-)

Reply via email to