Fix SHMEM_ATTACH_UNKNOWN_SIZE when the shmem area doesn't exist

SHMEM_ATTACH_UNKNOWN_SIZE can be passed as argument to
ShmemRequestStruct() when the caller wants to attach to an existing
shared memory structure whose size it doesn't know.  If the shared
memory structure doesn't exist, the request should fail, but instead,
ProcessShmemRequestsAfterStartup() tried to create the structure with
size = -1.  That led to integer overflow in ShmemAllocRaw() and memory
corruption.

Fix by rejecting requests with SHMEM_ATTACH_UNKNOWN_SIZE when the
structure doesn't exist.  Also add an integer overflow check in
ShmemAllocRaw(), to protect from this kind of confusion or simply too
large requests.  Also document SHMEM_ATTACH_UNKNOWN_SIZE.

Author: Ashutosh Bapat <[email protected]>
Discussion: 
https://www.postgresql.org/message-id/caexhw5u_ftsoas85kg981vu6er1gv-344rup6zyew7xmjed...@mail.gmail.com
Backpatch-through: 19

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/b118f8c841d53f24d9fd159df83a386ea3ce204d

Modified Files
--------------
doc/src/sgml/xfunc.sgml                            | 10 ++++++++++
src/backend/storage/ipc/shmem.c                    | 22 ++++++++++++++++++++--
.../modules/test_shmem/t/001_late_shmem_alloc.pl   | 17 +++++++++++++++++
src/test/modules/test_shmem/test_shmem.c           | 11 +++++++----
4 files changed, 54 insertions(+), 6 deletions(-)

Reply via email to