Skip to main content

How a 22-Year-Old PostgreSQL Type Confusion Became Remote Code Execution

Updated 10 min readMarkdown
How a 22-Year-Old PostgreSQL Type Confusion Became Remote Code Execution

Four SQL statements, a login with no special privileges, and a server process that dies with signal 11. That is the entry point to CVE-2026-6460, the PostgreSQL flaw security researcher Mehmet Ince wrote up in November 2026 after tracing it to a commit from May 2004. The database project rates it CVSS 8.8, fixed it in versions 18.7, 17.12, 16.16, 15.20 and 14.25, and shipped the fix as part of a November 2026 security release carrying 52 CVEs. Six of those 52 trace back to this one line of research.

The short version: PostgreSQL lets you prepare a query, change the type of a field inside a composite type, and then run the prepared query. The value the query captured was built under the old type. The code that prints it trusts the new type. When the two disagree, the database reads attacker-chosen bytes as its own internal structures, and from there Ince walked it all the way to running a command as the operating system user that runs PostgreSQL.

A value that does not know what it is

To see why this works, you need one fact about how PostgreSQL stores values. Every value travels through the engine inside an 8-byte carrier called a Datum. A Datum carries no label saying what it is. The type lives elsewhere, in a number called an OID that points at a row in the pg_type catalog. That row says how long the value is, whether it is stored by value or by reference, and which functions convert it in and out.

An integer 1337 becomes the four bytes 39 05 00 00 inside the lower half of a Datum, and the OID 23 (int4) tells the engine to read those bytes as an integer. A text value is different: the Datum holds a pointer to a variable-length structure somewhere else in memory. Same carrier, completely different meaning, and the meaning comes entirely from the type the engine believes the value has.

That design is sound as long as the believed type and the actual bytes always agree. Ince's question, after months spent exploiting PostgreSQL extensions, was simple: what if I can change the type between the moment PostgreSQL builds a value and the moment it uses it?

The crash in four statements

Composite types are the opening. A composite type is a user-defined row shape: named fields, each with its own type. Unlike built-in types, a composite type's fields can be redefined after the type exists, with ALTER TYPE ... ALTER ATTRIBUTE ... TYPE.

Ince created a one-field composite whose field started as uuid, prepared a query holding a UUID literal, changed the field to bytea, and ran the prepared query:

CREATE TYPE pg_temp.poc AS (key uuid);

PREPARE pc_crash AS
    SELECT '(01020000-0000-0000-0000-000000000000)'
           ::pg_temp.poc;

ALTER TYPE pg_temp.poc
    ALTER ATTRIBUTE key TYPE bytea CASCADE;

EXECUTE pc_crash;

The backend died immediately:

client backend was terminated by signal 11: Segmentation fault
DETAIL: Failed process was running: EXECUTE pc_crash;

Nothing about this needed a superuser. Ince writes that he could trigger the crash as a lowest-privileged login role, with everything happening in pg_temp. The project's own advisory frames the precondition a little more narrowly, as an authenticated user able to create objects. Either way, the bar is a normal database login, not an administrator.

Why those exact bytes kill the server

The UUID literal was chosen, not random. Its sixteen bytes start 01 02 followed by zeros. Under the UUID type those bytes are inert data. Under bytea they mean something else entirely, because bytea values use a variable-length layout called varlena, and varlena starts with a small header describing the representation.

A header starting with 01 tells PostgreSQL the value is not stored inline at all. It is an external expanded object: the following bytes are a pointer to an ExpandedObjectHeader somewhere in memory. So when the prepared query runs, record_out() looks at the composite's field, sees the new type bytea, and calls byteaout() on bytes that were built as a UUID. byteaout() reads 01 02 as an external expanded-object header and the following eight zero bytes as a NULL object pointer, then tries to convert that supposed object into printable form. The call chain is short and fatal:

EXECUTE pc_crash
  ├─ record_out()             a: bytea
  ├─ byteaout()               fetch + detoast
  ├─ detoast_external_attr()  01 02 → expanded
  ├─ DatumGetEOHP()           eohptr = NULL
  ├─ EOH_get_flat_size(NULL)
  └─ NULL->eoh_methods        SIGSEGV

EOH_get_flat_size does effectively one thing: return eohptr->eoh_methods->get_flat_size(eohptr). With eohptr set to NULL by the attacker's own UUID bytes, the server reads a methods table from address zero and dies. A crash is the least interesting outcome here. The interesting part is what happens when NULL is replaced with a pointer the attacker controls.

From crash to command

An expanded object carries a methods table, ExpandedObjectMethods, holding two native function pointers: get_flat_size and flatten_into. Normal code trusts these completely, because expanded objects are supposed to be built by PostgreSQL itself. The detoasting path calls whatever function the table points at.

Ince's plan used two forged objects. Call them H, a fake ExpandedObjectHeader, and M, a fake methods table. The UUID bytes point at H. H points at M. M holds the address of system(). PostgreSQL then does the rest itself: it reads the methods pointer from H, finds system() in M, and calls it, passing H as the argument.

That last detail is what makes the chain land. system() takes one argument, a command string, and PostgreSQL hands it H itself. The first eight bytes of H sit before the methods pointer at offset +0x08, so Ince placed a NULL-terminated command there, such as id>x, with M's address after it. One detoast later, the database server runs the command as the operating system account running PostgreSQL.

Ince published the crash and the address-leak steps in full, but he deliberately did not publish the final step that makes the server run a command. The public record shows a working exploit path, not a copy-paste weapon, and this article follows the same line: the mechanism is the story, the last step is not reproduced here.

Beating ASLR with the same trick

A forged object is useless without its address, and modern systems randomise memory layout, so Ince needed a leak from the same primitive. He found it in arrays.

PostgreSQL arrays store their element type inside their own binary form, and unnest() trusts that stored type when it extracts elements. A text element is passed by reference, so unnest() returns a Datum holding a pointer to the text value. If the surrounding SQL expects a bigint, which is passed by value, PostgreSQL prints those same pointer bits as an integer. The pointer leaks as a number.

Three UUIDs give exactly 48 controlled bytes, enough to build the smallest array structure. Ince prepared the bytes while all three fields were still UUIDs, then changed only the first field's type to bigint[] before executing. The leak came back as a hex address, 55f2a589ae0c in his run, and adding 4 skips the text header to land on the controlled payload. Same primitive, same prepare-mutate-execute shape, a different confused type doing the leaking.

Why it survived 22 years

The primitive dates to commit 077db40fa1f3, 5 May 2004, when PostgreSQL gained the ability to change a column's type in place. The exploitation path leans on a second commit, 1dc5ebc9077a from 14 May 2015, which added expanded-object pointers and their native method callbacks. So the bug is 22 years old and the weapon is 11, and neither fact alone explains the survival.

Three things protected it. First, the vulnerable moment is a gap, not a place: nothing is wrong with preparing a query, nothing is wrong with altering a type, and nothing is wrong with executing a query. The flaw only exists in the combination, in the instant between the plan capturing a value and the executor trusting a descriptor that has since changed. Code review looks at functions; this bug lives between them.

Second, the type system felt safe by construction. A Datum is always paired with an OID, and the OID always says what the bytes mean, so nobody went looking for a case where the pairing itself was stale. Composite types are the exception that breaks the assumption, because their fields are user-mutable while values built from them can outlive the definition they were built under.

Third, the precondition kept it out of the usual hunting grounds. An unauthenticated scanner cannot reach it, and the crash needs deliberately chosen bytes, a UUID starting 01 02, not random fuzzing input. It took a researcher who had just spent months learning exactly how Datums and type OIDs work from an extension exploit, plus a passing conversation about prepared statements, to connect the pieces.

What the fix changes

Ince reported 21 cases in total, finding the same underlying mistake around RowExpr, JSON conversion caches and other places where a stale value could outlive its type. The November release fixed six CVEs from that work:

  • CVE-2026-6460, the umbrella fix, which introduces composite attribute versioning: a value now carries the version of the type definition it was built under, and the executor refuses to interpret it under a newer one
  • CVE-2026-120142, anonymous RECORD descriptors
  • CVE-2026-120148, backend-local anonymous-RECORD typmod
  • CVE-2026-116329, statistics import publishing values under an obsolete type
  • CVE-2026-116306, ExecEvalFieldStore and ExecEvalFieldDeForm using a mismatched type
  • CVE-2026-120139, integer overflow in tuple-size calculation

The versioning approach matters more than any single patch. The fix does not block ALTER TYPE or prepared statements, and it does not try to list every confused combination. It makes the stale-value case impossible by construction: if the definition changed since the value was captured, the value is recognised as belonging to the old definition. The error message tells the story plainly. Running the crash proof against a fixed server returns row type of datum tuple was altered after the datum's creation instead of a segfault.

The fix is a minor-release update, not a major migration: 18.7, 17.12, 16.16, 15.20 and 14.25, with no dump, reload or pg_upgrade needed for a same-major upgrade. No exploitation in the wild had been reported when the write-up was published, and the CVE record lists exploitation as none.

The managed-database angle is the part that should worry operators most. Ince ported the exploit to the managed PostgreSQL platforms and shared reproducible proofs with their security teams privately in August 2026, before the public release. By his account every vendor he tested was affected, including Postgres-compatible offerings: AWS Aurora and RDS, Microsoft Azure PostgreSQL, Google AlloyDB, Supabase, Databricks and Ubicloud. Google escalated the AlloyDB report to its highest priority, Databricks validated the exploits immediately, and some vendors had senior engineers building their own mitigations rather than waiting for the upstream release. If you run PostgreSQL anywhere, including a managed service, the patch is the whole story: check the version, and if it is older than the fixed releases, update it.

Sources


P.S. Everything I write stays free — that part doesn't change. But if this one was worth something to you, you can buy me a coffee ☕ (one-time or monthly, both work). It covers the API credits and hosting behind these experiments — and honestly, it's also the nudge that gets the next one started on the evenings I'd otherwise skip it. — Salam

Buy me a coffee

No comments yet. Be the first to comment.

More from this blog