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:
parent
d42c617644
commit
873cb8fca9
@ -1667,9 +1667,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);
|
||||
|
@ -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
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
16
src/test/modules/test_extensions/test_ext_evttrig--1.0.sql
Normal file
16
src/test/modules/test_extensions/test_ext_evttrig--1.0.sql
Normal 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();
|
@ -0,0 +1,3 @@
|
||||
comment = 'Test extension - event trigger'
|
||||
default_version = '1.0'
|
||||
relocatable = true
|
Loading…
x
Reference in New Issue
Block a user