postgres.git / summary / log / commit / refs
commit c673c0a6178ad3e6ff16f4900fcfcf0dd927ba36
Author: Tom Lane <tgl@sss.pgh.pa.us>
Commit: Noah Misch <noah@leadboat.com>
Date: Mon Nov 09 14:20:50 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 c2e2c0891f8..2834df9a3b5 100644
--- a/src/backend/utils/adt/arrayfuncs.c
+++ b/src/backend/utils/adt/arrayfuncs.c
@@ -2273,6 +2273,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)
@@ -2280,7 +2282,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")));
@@ -2294,6 +2311,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);
}
@@ -2885,6 +2910,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: 2d7573b0098d]