Fix use-after-free bug with event triggers in an extension script

ALTER TABLE commands in an extension script are added to an event
trigger command list; but starting with commit b5810de3f4 they do so in
a memory context that's too short-lived, so when execution ends and time
comes to use the entries, they've already been freed.

(This would also be a problem with ALTER TABLE commands in a
multi-command query string, but these serendipitously end in
PortalContext -- which probably explains why it took so long for this to
be reported.)

Fix by using the memory context specifically set for that, instead.

Backpatch to 13, where the aforementioned commit appeared.

Reported-by: Philippe Beaudoin
Author: Jehan-Guillaume de Rorthais <jgdr@dalibo.com>
Discussion: https://postgr.es/m/20200902193715.6e0269d4@firost
This commit is contained in:
Alvaro Herrera 2020-09-15 21:03:14 -03:00
parent 10a5b35a00
commit ced138e8cb
No known key found for this signature in database
GPG Key ID: 1C20ACB9D5C564AE
7 changed files with 47 additions and 2 deletions

View File

@ -1646,9 +1646,15 @@ EventTriggerAlterTableEnd(void)
/* If no subcommands, don't collect */
if (list_length(currentEventTriggerState->currentCommand->d.alterTable.subcmds) != 0)
{
MemoryContext oldcxt;
oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt);
currentEventTriggerState->commandList =
lappend(currentEventTriggerState->commandList,
currentEventTriggerState->currentCommand);
MemoryContextSwitchTo(oldcxt);
}
else
pfree(currentEventTriggerState->currentCommand);

View File

@ -4,11 +4,13 @@ MODULE = test_extensions
PGFILEDESC = "test_extensions - regression testing for EXTENSION support"
EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \
test_ext7 test_ext8 test_ext_cyclic1 test_ext_cyclic2
test_ext7 test_ext8 test_ext_cyclic1 test_ext_cyclic2 \
test_ext_evttrig
DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \
test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \
test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \
test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql
test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \
test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql
REGRESS = test_extensions test_extdepend

View File

@ -154,3 +154,8 @@ DROP TABLE test_ext4_tab;
DROP FUNCTION create_extension_with_temp_schema();
RESET client_min_messages;
\unset SHOW_CONTEXT
-- Test case of an event trigger run in an extension upgrade script.
-- See: https://postgr.es/m/20200902193715.6e0269d4@firost
CREATE EXTENSION test_ext_evttrig;
ALTER EXTENSION test_ext_evttrig UPDATE TO '2.0';
DROP EXTENSION test_ext_evttrig;

View File

@ -93,3 +93,9 @@ DROP TABLE test_ext4_tab;
DROP FUNCTION create_extension_with_temp_schema();
RESET client_min_messages;
\unset SHOW_CONTEXT
-- Test case of an event trigger run in an extension upgrade script.
-- See: https://postgr.es/m/20200902193715.6e0269d4@firost
CREATE EXTENSION test_ext_evttrig;
ALTER EXTENSION test_ext_evttrig UPDATE TO '2.0';
DROP EXTENSION test_ext_evttrig;

View File

@ -0,0 +1,7 @@
/* src/test/modules/test_extensions/test_event_trigger--1.0--2.0.sql */
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
\echo Use "ALTER EXTENSION test_event_trigger UPDATE TO '2.0'" to load this file. \quit
-- Test extension upgrade with event trigger.
ALTER EVENT TRIGGER table_rewrite_trg DISABLE;
ALTER TABLE t DROP COLUMN id;

View File

@ -0,0 +1,16 @@
/* src/test/modules/test_extensions/test_event_trigger--1.0.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION test_event_trigger" to load this file. \quit
-- Base table with event trigger, used in a regression test involving
-- extension upgrades.
CREATE TABLE t (id text);
CREATE OR REPLACE FUNCTION _evt_table_rewrite_fnct()
RETURNS EVENT_TRIGGER LANGUAGE plpgsql AS
$$
BEGIN
END;
$$;
CREATE EVENT TRIGGER table_rewrite_trg
ON table_rewrite
EXECUTE PROCEDURE _evt_table_rewrite_fnct();

View File

@ -0,0 +1,3 @@
comment = 'Test extension - event trigger'
default_version = '1.0'
relocatable = true