pgsql: Harden memory context allocators against bogus chunk pointers. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Harden memory context allocators against bogus chunk pointers.
Date
Msg-id E1oi1X1-001oKP-PO@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Harden memory context allocators against bogus chunk pointers.

Before commit c6e0fe1f2, functions such as AllocSetFree could pretty
safely presume that they were given a valid chunk pointer for their
own type of context, because the indirect call through a memory
context object and method struct would be very unlikely to work
otherwise.  But now, if pfree() is mistakenly invoked on a pointer
to garbage, we have three chances in eight of ending up at one of
these functions.  That means we need to take extra measures to
verify that we are looking at what we're supposed to be looking at,
especially in debug builds.

Hence, add code to verify that the chunk's back-link to a block header
leads to a memory context object that satisfies the right sort of
IsA() check.  This is still a bit weaker than what we did before,
but for the moment assume that an IsA() check is sufficient.

As a compromise between speed and safety, implement these checks
as Asserts when dealing with small chunks but plain test-and-elogs
when dealing with large (external) chunks.  The latter case should
not be too performance-critical, but the former case probably is.
In slab.c, all chunks are small; but nonetheless use a plain test
in SlabRealloc, because that is certainly not performance-critical,
indeed we should be suspicious that it's being called in error.

In aset.c, additionally add some assertions that the "value" field
of the chunk header is within the small range allowed for freelist
indexes.  Without that, we might find ourselves trying to wipe
most of memory when CLOBBER_FREED_MEMORY is enabled, or scribbling
on a "freelist header" that's far away from the context object.

Eventually, field experience might show us that it's smarter for
these tests to be active always, but for now we'll try to get
away with just having them as assertions.

While at it, also be more uniform about asserting that context
objects passed as parameters are of the type we expect.  Some
places missed that altogether, and slab.c was for no very good
reason doing it differently from the other allocators.

Discussion: https://postgr.es/m/3578387.1665244345@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/0e87dfe46443286e630e9bcbc0c39a39c2c2cbb2

Modified Files
--------------
src/backend/utils/mmgr/aset.c       | 114 ++++++++++++++++++++++++++----------
src/backend/utils/mmgr/generation.c |  53 ++++++++++++++++-
src/backend/utils/mmgr/slab.c       |  71 ++++++++++++++++------
3 files changed, 187 insertions(+), 51 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Doc: update release date for v15.
Next
From: Michael Paquier
Date:
Subject: pgsql: Add support for COPY TO callback functions