In extensions, don't replace objects not belonging to the extension.
commit5579726bd60a6e7afb04a3548bced348cd5ffd89
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Aug 2022 15:12:31 +0000 (8 11:12 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Aug 2022 15:12:31 +0000 (8 11:12 -0400)
tree53ecbc2d22126c128871073254f58f5227f4adf8
parente12f5fdf39436897fb4d9c9673cc18ad48a91660
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]