postgres.git / summary / log / commit / refs
commit cacc71935952b1e47d852a9e9be3a548fc5ac40e
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
Fix potential buffer overrun in tsvector_delete().
tsvector_delete_by_indices() sized its output as VARSIZE(input) on the
assumption that the result can never be larger, but that only holds
when the input's lexeme data area is laid out in WordEntry order, like
the output's will be. With a different ordering, additional pad bytes
could be needed for alignment of position data. ts_type.h explicitly
says that a different lexeme data ordering is legal, and
tsvectorrecv() for one can produce other orderings. To fix, use the
same solution as tsvector_concat(): allocate one extra byte per
output lexeme. That's probably considerably more than enough, but
it's not worth working harder.
Reported-by: Anthropic OSS program
Author: Anthropic OSS program
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Backpatch-through: 14
Security: CVE-2026-116315
src/backend/utils/adt/tsvector_op.c | 24 +++++++++++++++++++++---
1 file changed, 21 insertions(+), 3 deletions(-)
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 8fe02a517ab..7aa4bb24145 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -485,16 +485,24 @@ tsvector_delete_by_indices(TSVector tsv, int *indices_to_delete,
/*
* Here we overestimate tsout size, since we don't know how much space is
* used by the deleted lexeme(s). We will set exact size below.
+ *
+ * Counterintuitively, the output could be larger than the input: we emit
+ * lexeme data in an order possibly different from what is in the input,
+ * and hence it could require different/more padding before position data.
+ * As in tsvector_concat(), allow for one pad byte per output entry.
+ *
+ * Since the MAXSTRPOS limit constrains the input to be considerably less
+ * than MaxAllocSize, we don't need to worry about integer overflow here,
+ * nor in the data-copying steps below. We do need to enforce that the
+ * result meets the MAXSTRPOS limit, but we check that once at the end.
*/
- tsout = (TSVector) palloc0(VARSIZE(tsv));
+ tsout = (TSVector) palloc0(VARSIZE(tsv) + tsv->size - indices_count);
/* This count must be correct because STRPTR(tsout) relies on it. */
tsout->size = tsv->size - indices_count;
/*
* Copy tsv to tsout, skipping lexemes listed in indices_to_delete.
- *
- * Output can't be bigger than input, so no need for overflow checks.
*/
arrout = ARRPTR(tsout);
dataout = STRPTR(tsout);
@@ -540,6 +548,16 @@ tsvector_delete_by_indices(TSVector tsv, int *indices_to_delete,
*/
Assert(k == indices_count);
+ /*
+ * Instead of checking each offset individually, we check for overflow of
+ * pos fields once at the end.
+ */
+ if (curoff > MAXSTRPOS)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("string is too long for tsvector (%zu bytes, max %zu bytes)",
+ (size_t) curoff, (size_t) MAXSTRPOS)));
+
SET_VARSIZE(tsout, CALCDATASIZE(tsout->size, curoff));
return tsout;
}
[parent: 3094cdb7d973]