1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-24 00:23:06 +03:00

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
This commit is contained in:
Tom Lane
2022-10-10 18:45:34 -04:00
parent 235eb4db98
commit 0e87dfe464
3 changed files with 187 additions and 51 deletions

View File

@@ -111,6 +111,21 @@ typedef struct SlabBlock
#define SlabChunkIndex(slab, block, chunk) \
(((char *) chunk - SlabBlockStart(block)) / slab->fullChunkSize)
/*
* SlabIsValid
* True iff set is valid slab allocation set.
*/
#define SlabIsValid(set) \
(PointerIsValid(set) && IsA(set, SlabContext))
/*
* SlabBlockIsValid
* True iff block is valid block of slab allocation set.
*/
#define SlabBlockIsValid(block) \
(PointerIsValid(block) && SlabIsValid((block)->slab))
/*
* SlabContextCreate
* Create a new Slab context.
@@ -236,10 +251,10 @@ SlabContextCreate(MemoryContext parent,
void
SlabReset(MemoryContext context)
{
SlabContext *slab = (SlabContext *) context;
int i;
SlabContext *slab = castNode(SlabContext, context);
Assert(slab);
AssertArg(SlabIsValid(slab));
#ifdef MEMORY_CONTEXT_CHECKING
/* Check for corruption and leaks before freeing */
@@ -293,12 +308,12 @@ SlabDelete(MemoryContext context)
void *
SlabAlloc(MemoryContext context, Size size)
{
SlabContext *slab = castNode(SlabContext, context);
SlabContext *slab = (SlabContext *) context;
SlabBlock *block;
MemoryChunk *chunk;
int idx;
Assert(slab);
AssertArg(SlabIsValid(slab));
Assert((slab->minFreeChunks >= 0) &&
(slab->minFreeChunks < slab->chunksPerBlock));
@@ -450,10 +465,18 @@ SlabAlloc(MemoryContext context, Size size)
void
SlabFree(void *pointer)
{
int idx;
MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
SlabBlock *block = MemoryChunkGetBlock(chunk);
SlabContext *slab = block->slab;
SlabContext *slab;
int idx;
/*
* For speed reasons we just Assert that the referenced block is good.
* Future field experience may show that this Assert had better become a
* regular runtime test-and-elog check.
*/
AssertArg(SlabBlockIsValid(block));
slab = block->slab;
#ifdef MEMORY_CONTEXT_CHECKING
/* Test for someone scribbling on unused space in chunk */
@@ -540,9 +563,18 @@ SlabRealloc(void *pointer, Size size)
{
MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
SlabBlock *block = MemoryChunkGetBlock(chunk);
SlabContext *slab = block->slab;
SlabContext *slab;
/*
* Try to verify that we have a sane block pointer: the block header
* should reference a slab context. (We use a test-and-elog, not just
* Assert, because it seems highly likely that we're here in error in the
* first place.)
*/
if (!SlabBlockIsValid(block))
elog(ERROR, "could not find block containing chunk %p", chunk);
slab = block->slab;
Assert(slab);
/* can't do actual realloc with slab, but let's try to be gentle */
if (size == slab->chunkSize)
return pointer;
@@ -560,11 +592,9 @@ SlabGetChunkContext(void *pointer)
{
MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
SlabBlock *block = MemoryChunkGetBlock(chunk);
SlabContext *slab = block->slab;
Assert(slab != NULL);
return &slab->header;
AssertArg(SlabBlockIsValid(block));
return &block->slab->header;
}
/*
@@ -577,9 +607,10 @@ SlabGetChunkSpace(void *pointer)
{
MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
SlabBlock *block = MemoryChunkGetBlock(chunk);
SlabContext *slab = block->slab;
SlabContext *slab;
Assert(slab);
AssertArg(SlabBlockIsValid(block));
slab = block->slab;
return slab->fullChunkSize;
}
@@ -591,9 +622,9 @@ SlabGetChunkSpace(void *pointer)
bool
SlabIsEmpty(MemoryContext context)
{
SlabContext *slab = castNode(SlabContext, context);
SlabContext *slab = (SlabContext *) context;
Assert(slab);
AssertArg(SlabIsValid(slab));
return (slab->nblocks == 0);
}
@@ -613,13 +644,15 @@ SlabStats(MemoryContext context,
MemoryContextCounters *totals,
bool print_to_stderr)
{
SlabContext *slab = castNode(SlabContext, context);
SlabContext *slab = (SlabContext *) context;
Size nblocks = 0;
Size freechunks = 0;
Size totalspace;
Size freespace = 0;
int i;
AssertArg(SlabIsValid(slab));
/* Include context header in totalspace */
totalspace = slab->headerSize;
@@ -672,11 +705,11 @@ SlabStats(MemoryContext context,
void
SlabCheck(MemoryContext context)
{
SlabContext *slab = (SlabContext *) context;
int i;
SlabContext *slab = castNode(SlabContext, context);
const char *name = slab->header.name;
Assert(slab);
AssertArg(SlabIsValid(slab));
Assert(slab->chunksPerBlock > 0);
/* walk all the freelists */