postgresql/src/include/storage
Tom Lane e1c30458a1 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:47 -07:00
..
.gitignore When trace_lwlocks is used, identify individual lwlocks by name. 2015-09-11 14:01:39 -04:00
aio.h aio: Fix assertion, clarify README 2025-07-22 08:32:14 -04:00
aio_internal.h aio: Stop using enum bitfields due to bad code generation 2025-08-27 19:12:50 -04:00
aio_subsys.h Fix typos and grammar in the code 2025-04-19 19:17:42 +09:00
aio_types.h aio: Fix reference to outdated name 2025-06-30 10:21:49 -04:00
barrier.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
block.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
buf.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
buf_internals.h aio: Add test_aio module 2025-04-01 13:47:46 -04:00
buffile.h Swap order of extern/static and pg_nodiscard 2025-03-14 07:18:07 +01:00
bufmgr.h Assert lack of hazardous buffer locks before possible catalog read. 2025-04-17 05:00:30 -07:00
bufpage.h bufmgr: Implement AIO read support 2025-03-30 17:28:03 -04:00
bulk_write.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
checksum.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
checksum_impl.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
condition_variable.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
copydir.h Sync typedefs.list with the buildfarm. 2025-06-15 13:04:24 -04:00
dsm.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
dsm_impl.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
dsm_registry.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
fd.h Add file_extend_method=posix_fallocate,write_zeros. 2026-02-06 17:38:39 +13:00
fileset.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
freespace.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
fsm_internals.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
indexfsm.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
io_worker.h aio: Infrastructure for io_method=worker 2025-03-18 11:54:01 -04:00
ipc.h pg_noreturn to replace pg_attribute_noreturn() 2025-03-13 12:37:26 +01:00
item.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
itemid.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
itemptr.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
large_object.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
latch.h Split WaitEventSet functions to separate source file 2025-03-06 01:26:16 +02:00
lmgr.h Add GUC option to log lock acquisition failures. 2025-03-14 23:14:12 +09:00
lock.h Rename log_lock_failure GUC to log_lock_failures for consistency. 2025-06-03 10:02:55 +09:00
lockdefs.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
lwlock.h Revert function to get memory context stats for processes 2025-05-23 15:44:54 +02:00
lwlocklist.h Revert "Get rid of WALBufMappingLock" 2025-08-22 19:32:12 +03:00
md.h Harmonize function parameter names for Postgres 18. 2025-04-12 12:07:36 -04:00
meson.build Update copyright for 2025 2025-01-01 11:21:55 -05:00
off.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
pg_sema.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
pg_shmem.h Add support for basic NUMA awareness 2025-04-07 23:08:17 +02:00
pmsignal.h Don't use double-quotes in #include's of system headers. 2025-04-26 20:30:27 -04:00
predicate.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
predicate_internals.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
proc.h Fix OldestMemberMXactId and OldestVisibleMXactId array usage 2026-03-02 19:19:26 +02:00
procarray.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
proclist.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
proclist_types.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
procnumber.h Fix comment for MAX_BACKENDS. 2025-02-24 15:02:09 -06:00
procsignal.h Fix ABI break by moving PROCSIG_SLOTSYNC_MESSAGE in ProcSignalReason 2026-04-09 15:25:40 +09:00
read_stream.h read_stream: Introduce and use optional batchmode support 2025-03-30 18:36:41 -04:00
reinit.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
relfilelocator.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
s_lock.h Use a non-locking initial test in TAS_SPIN on AArch64. 2025-01-10 13:18:04 -06:00
sharedfileset.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
shm_mq.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
shm_toc.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
shmem.h Make palloc_array() and friends safe against integer overflow. 2026-05-11 05:13:47 -07:00
sinval.h Sync typedefs.list with the buildfarm. 2025-06-15 13:04:24 -04:00
sinvaladt.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
smgr.h Add missing PGDLLIMPORT markings 2025-04-11 08:59:52 +02:00
spin.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
standby.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
standbydefs.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
sync.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
waiteventset.h Fix incorrect year in some copyright notices 2025-05-19 09:46:52 +09:00