Re: SHMEM_ATTACH_UNKNOWN_SIZE reaches InitShmemIndexEntry()

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Heikki Linnakangas <heikki(dot)linnakangas(at)databricks(dot)com>
Subject: Re: SHMEM_ATTACH_UNKNOWN_SIZE reaches InitShmemIndexEntry()
Date: 2026-09-18 09:16:05
Message-ID: b9b66874-49f5-4bac-9179-cca2cac64128@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 18/09/2026 10:00, Ashutosh Bapat wrote:
> 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 does not know, after the
> startup. If the shared memory structure it wants to attach to does not
> exist, the request should fail. But instead
> ProcessShmemRequestsAfterStartup() ended up creating the structure
> with size = -1. InitShmemIndexEntry() did not catch it and created a
> ShmemIndexEntry with size = SIZE_MAX since size is an unsigned
> integer. ShmemAllocRaw() did not catch the overflow in address
> arithmetic and ended up allocating the new structure overlapping the
> earlier structure which can potentially cause memory corruption.
>
> Attached patch fixes ProcessShmemRequestsAfterStartup() throw an error
> in this case, adds an Assert() in InitShmemIndexEntry() to make sure
> that a request with unknown size never reaches it, makes
> ShmemAllocRaw() check for overflow, and documents use of
> SHMEM_ATTACH_UNKNOWN_SIZE.
>
> I found this problem when working on resizable shared structures where
> the size of the structure may have changed from its initial size and
> hence may not be known. It's good to defend our shared memory
> structures from a bug in extension code.

Committed with minor cosmetic changes, thanks!

- Heikki

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Langote 2026-09-18 09:21:55 Re: PG19: two RI fast-path issues found while testing the batching revert
Previous Message Etsuro Fujita 2026-09-18 09:15:14 Re: [PG19][PATCH] Make postgres_fdw statistics import atomic