8000 Fix possible dangling pointer dereference in trigger.c. · koderP/postgres@b1be335 · GitHub
[go: up one dir, main page]

Skip to content

Commit b1be335

Browse files
committed
Fix possible dangling pointer dereference in trigger.c.
AfterTriggerEndQuery correctly notes that the query_stack could get repalloc'd during a trigger firing, but it nonetheless passes the address of a query_stack entry to afterTriggerInvokeEvents, so that if such a repalloc occurs, afterTriggerInvokeEvents is already working with an obsolete dangling pointer while it scans the rest of the events. Oops. The only code at risk is its "delete_ok" cleanup code, so we can prevent unsafe behavior by passing delete_ok = false instead of true. However, that could have a significant performance penalty, because the point of passing delete_ok = true is to not have to re-scan possibly a large number of dead trigger events on the next time through the loop. There's more than one way to skin that cat, though. What we can do is delete all the "chunks" in the event list except the last one, since we know all events in them must be dead. Deleting the chunks is work we'd have had to do later in AfterTriggerEndQuery anyway, and it ends up saving rescanning of just about the same events we'd have gotten rid of with delete_ok = true. In v10 and HEAD, we also have to be careful to mop up any per-table after_trig_events pointers that would become dangling. This is slightly annoying, but I don't think that normal use-cases will traverse this code path often enough for it to be a performance problem. It's pretty hard to hit this in practice because of the unlikelihood of the query_stack getting resized at just the wrong time. Nonetheless, it's definitely a live bug of ancient standing, so back-patch to all supported branches. Discussion: https://postgr.es/m/2891.1505419542@sss.pgh.pa.us
1 parent a42f8d9 commit b1be335

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

src/backend/commands/trigger.c

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,14 +3300,12 @@ static void
33003300
afterTriggerFreeEventList(AfterTriggerEventList *events)
33013301
{
33023302
AfterTriggerEventChunk *chunk;
3303-
AfterTriggerEventChunk *next_chunk;
33043303

3305-
for (chunk = events->head; chunk != NULL; chunk = next_chunk)
3304+
while ((chunk = events->head) != NULL)
33063305
{
3307-
next_chunk = chunk->next;
3306+
events->head = chunk->next;
33083307
pfree(chunk);
33093308
}
3310-
events->head = NULL;
33113309
events->tail = NULL;
33123310
events->tailfree = NULL;
33133311
}
@@ -3351,6 +3349,23 @@ afterTriggerRestoreEventList(AfterTriggerEventList *events,
33513349
}
33523350
}
33533351

3352+
/* ----------
3353+
* afterTriggerDeleteHeadEventChunk()
3354+
*
3355+
* Remove the first chunk of events from the given event list.
3356+
* ----------
3357+
*/
3358+
static void
3359+
afterTriggerDeleteHeadEventChunk(AfterTriggerEventList *events)
3360+
{
3361+
AfterTriggerEventChunk *target = events->head;
3362+
3363+
Assert(target && target->next);
3364+
3365+
events->head = target->next;
3366+
pfree(target);
3367+
}
3368+
33543369

33553370
/* ----------
33563371
* AfterTriggerExecute()
@@ -3661,7 +3676,7 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
36613676
/*
36623677
* If it's last chunk, must sync event list's tailfree too. Note
36633678
* that delete_ok must NOT be passed as true if there could be
3664-
* stacked AfterTriggerEventList values pointing at this event
3679+
* additional AfterTriggerEventList values pointing at this event
36653680
* list, since we'd fail to fix their copies of tailfree.
36663681
*/
36673682
if (chunk == events->tail)
@@ -3810,24 +3825,50 @@ AfterTriggerEndQuery(EState *estate)
38103825
* IMMEDIATE: all events we have decided to defer will be available for it
38113826
* to fire.
38123827
*
3813-
* We loop in case a trigger queues more events at the same query level
3814-
* (is that even possible?). Be careful here: firing a trigger could
3815-
* result in query_stack being repalloc'd, so we can't save its address
3816-
* across afterTriggerInvokeEvents calls.
3828+
* We loop in case a trigger queues more events at the same query level.
3829+
* Ordinary trigger functions, including all PL/pgSQL trigger functions,
3830+
* will instead fire any triggers in a dedicated query level. Foreign key
3831+
* enforcement triggers do add to the current query level, thanks to their
3832+
* passing fire_triggers = false to SPI_execute_snapshot(). Other
3833+
* C-language triggers might do likewise.
38173834
*
38183835
* If we find no firable events, we don't have to increment
38193836
* firing_counter.
38203837
*/
3838+
events = &afterTriggers->query_stack[afterTriggers->query_depth];
3839+
38213840
for (;;)
38223841
{
3823-
events = &afterTriggers->query_stack[afterTriggers->query_depth];
38243842
if (afterTriggerMarkEvents(events, &afterTriggers->events, true))
38253843
{
38263844
CommandId firing_id = afterTriggers->firing_counter++;
3845+
AfterTriggerEventChunk *oldtail = events->tail;
38273846

3828-
/* OK to delete the immediate events after processing them */
3829-
if (afterTriggerInvokeEvents(events, firing_id, estate, true))
3847+
if (afterTriggerInvokeEvents(events, firing_id, estate, false))
38303848
break; /* all fired */
3849+
3850+
/*
3851+
* Firing a trigger could result in query_stack being repalloc'd,
3852+
* so we must recalculate ptr after each afterTriggerInvokeEvents
3853+
* call. Furthermore, it's unsafe to pass delete_ok = true here,
3854+
* because that could cause afterTriggerInvokeEvents to try to
3855+
* access *events after the stack has been repalloc'd.
3856+
*/
3857+
events = &afterTriggers->query_stack[afterTriggers->query_depth];
3858+
3859+
/*
3860+
* We'll need to scan the events list again. To reduce the cost
3861+
* of doing so, get rid of completely-fired chunks. We know that
3862+
* all events were marked IN_PROGRESS or DONE at the conclusion of
3863+
* afterTriggerMarkEvents, so any still-interesting events must
3864+
* have been added after that, and so must be in the chunk that
3865+
* was then the tail chunk, or in later chunks. So, zap all
3866+
* chunks before oldtail. This is approximately the same set of
3867+
* events we would have gotten rid of by passing delete_ok = true.
3868+
*/
3869+
Assert(oldtail != NULL);
3870+
while (events->head != oldtail)
3871+
afterTriggerDeleteHeadEventChunk(events);
38313872
}
38323873
else
38333874
break;

0 commit comments

Comments
 (0)
0