Detect integer overflow while computing new array dimensions.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 6 Nov 2023 15:56:43 +0000 (10:56 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 6 Nov 2023 15:56:43 +0000 (10:56 -0500)
commit3bc6bc3ee2dea4812203b3649096049af97fb13b
tree79eeabce66be205c437c2f79c9b538723344e368
parent4f4a422fbb15e49ff5e9dc410cd8713d63fdda24
Detect integer overflow while computing new array dimensions.

array_set_element() and related functions allow an array to be
enlarged by assigning to subscripts outside the current array bounds.
While these places were careful to check that the new bounds are
allowable, they neglected to consider the risk of integer overflow
in computing the new bounds.  In edge cases, we could compute new
bounds that are invalid but get past the subsequent checks,
allowing bad things to happen.  Memory stomps that are potentially
exploitable for arbitrary code execution are possible, and so is
disclosure of server memory.

To fix, perform the hazardous computations using overflow-detecting
arithmetic routines, which fortunately exist in all still-supported
branches.

The test cases added for this generate (after patching) errors that
mention the value of MaxArraySize, which is platform-dependent.
Rather than introduce multiple expected-files, use psql's VERBOSITY
parameter to suppress the printing of the message text.  v11 psql
lacks that parameter, so omit the tests in that branch.

Our thanks to Pedro Gallegos for reporting this problem.

Security: CVE-2023-5869
src/backend/utils/adt/arrayfuncs.c
src/backend/utils/adt/arrayutils.c
src/include/utils/array.h
src/test/regress/expected/arrays.out
src/test/regress/sql/arrays.sql