In extensions, don't replace objects not belonging to the extension.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Aug 2022 15:12:31 +0000 (11:12 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Aug 2022 15:12:31 +0000 (11:12 -0400)
commitb9b21acc766db54d8c337d508d0fe2f5bf2daab0
treef1e302dbebbfb0ec7cc53f9bbfbd3ef91463c481
parent7e29a79a46d30dc236d097825ab849158929d977
In extensions, don't replace objects not belonging to the extension.

Previously, if an extension script did CREATE OR REPLACE and there was
an existing object not belonging to the extension, it would overwrite
the object and adopt it into the extension.  This is problematic, first
because the overwrite is probably unintentional, and second because we
didn't change the object's ownership.  Thus a hostile user could create
an object in advance of an expected CREATE EXTENSION command, and would
then have ownership rights on an extension object, which could be
modified for trojan-horse-type attacks.

Hence, forbid CREATE OR REPLACE of an existing object unless it already
belongs to the extension.  (Note that we've always forbidden replacing
an object that belongs to some other extension; only the behavior for
previously-free-standing objects changes here.)

For the same reason, also fail CREATE IF NOT EXISTS when there is
an existing object that doesn't belong to the extension.

Our thanks to Sven Klemm for reporting this problem.

Security: CVE-2022-2625
21 files changed:
doc/src/sgml/extend.sgml
src/backend/catalog/pg_collation.c
src/backend/catalog/pg_depend.c
src/backend/catalog/pg_operator.c
src/backend/catalog/pg_type.c
src/backend/commands/createas.c
src/backend/commands/foreigncmds.c
src/backend/commands/schemacmds.c
src/backend/commands/sequence.c
src/backend/commands/statscmds.c
src/backend/commands/view.c
src/backend/parser/parse_utilcmd.c
src/include/catalog/dependency.h
src/test/modules/test_extensions/Makefile
src/test/modules/test_extensions/expected/test_extensions.out
src/test/modules/test_extensions/sql/test_extensions.sql
src/test/modules/test_extensions/test_ext_cine--1.0--1.1.sql [new file with mode: 0644]
src/test/modules/test_extensions/test_ext_cine--1.0.sql [new file with mode: 0644]
src/test/modules/test_extensions/test_ext_cine.control [new file with mode: 0644]
src/test/modules/test_extensions/test_ext_cor--1.0.sql [new file with mode: 0644]
src/test/modules/test_extensions/test_ext_cor.control [new file with mode: 0644]