postgresql/src/include/common
Tom Lane cfb610eaa0 Make palloc_array() and friends safe against integer overflow.
Sufficiently large "count" arguments could result in undetected
overflow, causing the allocated memory chunk to be much smaller
than what the caller will subsequently write into it.  This is
unlikely to be a hazard with 64-bit size_t but can sometimes
happen on 32-bit builds, primarily where a function allocates
workspace that's significantly larger than its input data.
Rather than trying to patch the at-risk callers piecemeal,
let's just redefine these macros so that they always check.

To do that, move the longstanding add_size() and mul_size() functions
into palloc.h and mcxt.c, and adjust them to not be specific to
shared-memory allocation.  Then invent palloc_mul(), palloc0_mul(),
palloc_mul_extended() to use these functions.  Actually, the latter
use inlined copies to save one function call.  repalloc_array() gets
similar treatment.  I didn't bother trying to inline the calls for
repalloc0_array() though.

In v14 and v15, this also adds repalloc_extended(), which previously
was only available in v16 and up.

We need copies of all this in fe_memutils.[hc] as well, since that
module also provides palloc_array() etc.

Reported-by: Xint Code
Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Backpatch-through: 14
Security: CVE-2026-6473
2026-05-11 05:13:49 -07:00
..
archive.h Revert refactoring of restore command code to shell_restore.c 2023-02-06 08:28:42 +09:00
base64.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
checksum_helper.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
compression.h Support long distance matching for zstd compression 2023-04-06 17:18:42 +02:00
config_info.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
connect.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
controldata_utils.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
cryptohash.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
fe_memutils.h Make palloc_array() and friends safe against integer overflow. 2026-05-11 05:13:49 -07:00
file_perm.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
file_utils.h Revise pg_pwrite_zeros() 2023-03-06 13:21:33 +09:00
hashfn.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
hmac.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
int.h Add pg_add_size_overflow() and friends 2026-05-11 05:13:49 -07:00
int128.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
ip.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
jsonapi.h Remove stray mid-sentence tabs in comments 2023-05-19 16:13:16 +02:00
keywords.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
kwlookup.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
link-canary.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
logging.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
md5.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
openssl.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
percentrepl.h Common function for percent placeholder replacement 2023-01-11 10:42:35 +01:00
pg_lzcompress.h Apply PGDLLIMPORT markings broadly. 2022-04-08 08:16:38 -04:00
pg_prng.h Invent random_normal() to provide normally-distributed random numbers. 2023-01-09 12:44:00 -05:00
relpath.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
restricted_token.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
saslprep.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
scram-common.h Make SCRAM iteration count configurable 2023-03-27 09:46:29 +02:00
sha1.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
sha2.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
shortest_dec.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
string.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
unicode_east_asian_fw_table.h Update Unicode data to Unicode 15.0.0 2022-09-19 18:30:05 -04:00
unicode_nonspacing_table.h Update Unicode data to Unicode 15.0.0 2022-09-19 18:30:05 -04:00
unicode_norm.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
unicode_norm_hashfunc.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
unicode_norm_table.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
unicode_normprops_table.h Update Unicode data to Unicode 15.0.0 2022-09-19 18:30:05 -04:00
username.h Update copyright for 2023 2023-01-02 15:00:37 -05:00