Skip to content

Commit bc09747

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 143fcf9 commit bc09747

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

src/backend/commands/trigger.c

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,14 +3437,12 @@ static void
34373437
afterTriggerFreeEventList(AfterTriggerEventList *events)
34383438
{
34393439
AfterTriggerEventChunk *chunk;
3440-
AfterTriggerEventChunk *next_chunk;
34413440

3442-
for (chunk = events->head; chunk != NULL; chunk = next_chunk)
3441+
while ((chunk = events->head) != NULL)
34433442
{
3444-
next_chunk = chunk->next;
3443+
events->head = chunk->next;
34453444
pfree(chunk);
34463445
}
3447-
events->head = NULL;
34483446
events->tail = NULL;
34493447
events->tailfree = NULL;
34503448
}
@@ -3488,6 +3486,23 @@ afterTriggerRestoreEventList(AfterTriggerEventList *events,
34883486
}
34893487
}
34903488

3489+
/* ----------
3490+
* afterTriggerDeleteHeadEventChunk()
3491+
*
3492+
* Remove the first chunk of events from the given event list.
3493+
* ----------
3494+
*/
3495+
static void
3496+
afterTriggerDeleteHeadEventChunk(AfterTriggerEventList *events)
3497+
{
3498+
AfterTriggerEventChunk *target = events->head;
3499+
3500+
Assert(target && target->next);
3501+
3502+
events->head = target->next;
3503+
pfree(target);
3504+
}
3505+
34913506

34923507
/* ----------
34933508
* AfterTriggerExecute()
@@ -3859,7 +3874,7 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
38593874
/*
38603875
* If it's last chunk, must sync event list's tailfree too. Note
38613876
* that delete_ok must NOT be passed as true if there could be
3862-
* stacked AfterTriggerEventList values pointing at this event
3877+
* additional AfterTriggerEventList values pointing at this event
38633878
* list, since we'd fail to fix their copies of tailfree.
38643879
*/
38653880
if (chunk == events->tail)
@@ -4029,23 +4044,45 @@ AfterTriggerEndQuery(EState *estate)
40294044
* will instead fire any triggers in a dedicated query level. Foreign key
40304045
* enforcement triggers do add to the current query level, thanks to their
40314046
* passing fire_triggers = false to SPI_execute_snapshot(). Other
4032-
* C-language triggers might do likewise. Be careful here: firing a
4033-
* trigger could result in query_stack being repalloc'd, so we can't save
4034-
* its address across afterTriggerInvokeEvents calls.
4047+
* C-language triggers might do likewise.
40354048
*
40364049
* If we find no firable events, we don't have to increment
40374050
* firing_counter.
40384051
*/
4052+
events = &afterTriggers->query_stack[afterTriggers->query_depth];
4053+
40394054
for (;;)
40404055
{
4041-
events = &afterTriggers->query_stack[afterTriggers->query_depth];
40424056
if (afterTriggerMarkEvents(events, &afterTriggers->events, true))
40434057
{
40444058
CommandId firing_id = afterTriggers->firing_counter++;
4059+
AfterTriggerEventChunk *oldtail = events->tail;
40454060

4046-
/* OK to delete the immediate events after processing them */
4047-
if (afterTriggerInvokeEvents(events, firing_id, estate, true))
4061+
if (afterTriggerInvokeEvents(events, firing_id, estate, false))
40484062
break; /* all fired */
4063+
4064+
/*
4065+
* Firing a trigger could result in query_stack being repalloc'd,
4066+
* so we must recalculate ptr after each afterTriggerInvokeEvents
4067+
* call. Furthermore, it's unsafe to pass delete_ok = true here,
4068+
* because that could cause afterTriggerInvokeEvents to try to
4069+
* access *events after the stack has been repalloc'd.
4070+
*/
4071+
events = &afterTriggers->query_stack[afterTriggers->query_depth];
4072+
4073+
/*
4074+
* We'll need to scan the events list again. To reduce the cost
4075+
* of doing so, get rid of completely-fired chunks. We know that
4076+
* all events were marked IN_PROGRESS or DONE at the conclusion of
4077+
* afterTriggerMarkEvents, so any still-interesting events must
4078+
* have been added after that, and so must be in the chunk that
4079+
* was then the tail chunk, or in later chunks. So, zap all
4080+
* chunks before oldtail. This is approximately the same set of
4081+
* events we would have gotten rid of by passing delete_ok = true.
4082+
*/
4083+
Assert(oldtail != NULL);
4084+
while (events->head != oldtail)
4085+
afterTriggerDeleteHeadEventChunk(events);
40494086
}
40504087
else
40514088
break;

0 commit comments

Comments
 (0)