Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c0d21bd

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 c4c4594 commit c0d21bd

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
@@ -3433,14 +3433,12 @@ static void
34333433
afterTriggerFreeEventList(AfterTriggerEventList *events)
34343434
{
34353435
AfterTriggerEventChunk *chunk;
3436-
AfterTriggerEventChunk *next_chunk;
34373436

3438-
for (chunk = events->head; chunk != NULL; chunk = next_chunk)
3437+
while ((chunk = events->head) != NULL)
34393438
{
3440-
next_chunk = chunk->next;
3439+
events->head = chunk->next;
34413440
pfree(chunk);
34423441
}
3443-
events->head = NULL;
34443442
events->tail = NULL;
34453443
events->tailfree = NULL;
34463444
}
@@ -3484,6 +3482,23 @@ afterTriggerRestoreEventList(AfterTriggerEventList *events,
34843482
}
34853483
}
34863484

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

34883503
/* ----------
34893504
* AfterTriggerExecute()
@@ -3853,7 +3868,7 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
38533868
/*
38543869
* If it's last chunk, must sync event list's tailfree too. Note
38553870
* that delete_ok must NOT be passed as true if there could be
3856-
* stacked AfterTriggerEventList values pointing at this event
3871+
* additional AfterTriggerEventList values pointing at this event
38573872
* list, since we'd fail to fix their copies of tailfree.
38583873
*/
38593874
if (chunk == events->tail)
@@ -3985,23 +4000,45 @@ AfterTriggerEndQuery(EState *estate)
39854000
* will instead fire any triggers in a dedicated query level. Foreign key
39864001
* enforcement triggers do add to the current query level, thanks to their
39874002
* passing fire_triggers = false to SPI_execute_snapshot(). Other
3988-
* C-language triggers might do likewise. Be careful here: firing a
3989-
* trigger could result in query_stack being repalloc'd, so we can't save
3990-
* its address across afterTriggerInvokeEvents calls.
4003+
* C-language triggers might do likewise.
39914004
*
39924005
* If we find no firable events, we don't have to increment
39934006
* firing_counter.
39944007
*/
4008+
events = &afterTriggers.query_stack[afterTriggers.query_depth];
4009+
39954010
for (;;)
39964011
{
3997-
events = &afterTriggers.query_stack[afterTriggers.query_depth];
39984012
if (afterTriggerMarkEvents(events, &afterTriggers.events, true))
39994013
{
40004014
CommandId firing_id = afterTriggers.firing_counter++;
4015+
AfterTriggerEventChunk *oldtail = events->tail;
40014016

4002-
/* OK to delete the immediate events after processing them */
4003-
if (afterTriggerInvokeEvents(events, firing_id, estate, true))
4017+
if (afterTriggerInvokeEvents(events, firing_id, estate, false))
40044018
break; /* all fired */
4019+
4020+
/*
4021+
* Firing a trigger could result in query_stack being repalloc'd,
4022+
* so we must recalculate ptr after each afterTriggerInvokeEvents
4023+
* call. Furthermore, it's unsafe to pass delete_ok = true here,
4024+
* because that could cause afterTriggerInvokeEvents to try to
4025+
* access *events after the stack has been repalloc'd.
4026+
*/
4027+
events = &afterTriggers.query_stack[afterTriggers.query_depth];
4028+
4029+
/*
4030+
* We'll need to scan the events list again. To reduce the cost
4031+
* of doing so, get rid of completely-fired chunks. We know that
4032+
* all events were marked IN_PROGRESS or DONE at the conclusion of
4033+
* afterTriggerMarkEvents, so any still-interesting events must
4034+
* have been added after that, and so must be in the chunk that
4035+
* was then the tail chunk, or in later chunks. So, zap all
4036+
* chunks before oldtail. This is approximately the same set of
4037+
* events we would have gotten rid of by passing delete_ok = true.
4038+
*/
4039+
Assert(oldtail != NULL);
4040+
while (events->head != oldtail)
4041+
afterTriggerDeleteHeadEventChunk(events);
40054042
}
40064043
else
40074044
break;

0 commit comments

Comments
 (0)