postgres.git / summary / log / commit / refs

commit    5788eaee3f2efd9cd6f01b27dfb49d96d9f2dd0c
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Commit:   Noah Misch <noah@leadboat.com>
Date:     Mon Nov 09 14:20:46 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    Prevent construction of invalid "name" values via element assignment.
    
    Although type "name" is of length NAMEDATALEN, we must not allow the
    last byte to become nonzero.  Otherwise, operations that treat it as
    a C string will read off the end, resulting in crashes or disclosure
    of server memory.  array_set_element() failed to enforce this.
    
    Another bad outcome of element assignment could be to construct an
    invalidly-encoded multibyte character.  Check that too while we're
    at it.
    
    Ideally we'd extend array_set_element()'s API to pass the array type
    OID, but that's infeasible in released branches, and it seems not
    worth the trouble even in master given the tiny usage of fixed-length
    array types.  Instead just test for elmlen == 1 && arraytyplen ==
    NAMEDATALEN to decide whether it's a name.
    
    Reported-by: Anthropic OSS program
    Author: Tom Lane <tgl@sss.pgh.pa.us>
    Reviewed-by: Noah Misch <noah@leadboat.com>
    Backpatch-through: 14
    Security: CVE-2026-116321


src/backend/utils/adt/arrayfuncs.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index 75cc4eeca52..1df8ae65449 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -2237,6 +2237,8 @@ array_set_element(Datum arraydatum, * fixed-length arrays -- these are assumed to be 1-d, 0-based. We * cannot extend them, either. */ + bool isname; + int numelems; char *resultarray; if (nSubscripts != 1) @@ -2244,7 +2246,22 @@ array_set_element(Datum arraydatum, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); - if (indx[0] < 0 || indx[0] >= arraytyplen / elmlen) + /* + * Normally, any element of the array can be assigned to. But for + * type "name" we should not allow assignment to the last element, + * since it's supposed to stay '\0'. Unfortunately we can't directly + * test whether the array is of type "name" without changing this + * function's API. But given the tiny usage of fixed-length-array + * types, it should be good enough to check for elmlen = 1 and + * arraytyplen = NAMEDATALEN. + */ + isname = (elmlen == 1 && arraytyplen == NAMEDATALEN); + if (isname) + numelems = NAMEDATALEN - 1; + else + numelems = arraytyplen / elmlen; + + if (indx[0] < 0 || indx[0] >= numelems) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array subscript out of range"))); @@ -2258,6 +2275,14 @@ array_set_element(Datum arraydatum, memcpy(resultarray, DatumGetPointer(arraydatum), arraytyplen); elt_ptr = (char *) resultarray + indx[0] * elmlen; ArrayCastAndSet(dataValue, elmlen, elmbyval, elmalign, elt_ptr); + + /* + * Another problem with allowing element assignment for type "name" is + * that it can produce an invalidly-encoded string. Reject that. + */ + if (isname) + pg_verifymbstr(resultarray, strlen(resultarray), false); + return PointerGetDatum(resultarray); } @@ -2849,6 +2874,9 @@ array_set_slice(Datum arraydatum, { /* * fixed-length arrays -- not got round to doing this... + * + * If we ever do implement this, note the special handling of type + * "name" in array_set_element(). */ ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), [parent: 7f8424ece5d0]