summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2020-04-17 13:41:59 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2020-04-17 13:41:59 -0400
commit3125a5baec1cf6d3aaeb8964bc3b3c49835e0452 (patch)
tree917d977ad4a5ef88e43904b810f8fe3582da0f25
parent4db819ba403901d38c4e4328883412c59061bb58 (diff)
Fix possible future cache reference leak in ALTER EXTENSION ADD/DROP.
recordExtObjInitPriv and removeExtObjInitPriv were sloppy about calling ReleaseSysCache. The cases cannot occur given current usage in ALTER EXTENSION ADD/DROP, since we wouldn't get here for these relkinds; but it seems wise to clean up better. In passing, extend test logic in test_pg_dump to exercise the dropped-column code paths here. Since the case is unreachable at present, there seems no great need to back-patch; hence fix HEAD only. Kyotaro Horiguchi, with test case and comment adjustments by me Discussion: https://postgr.es/m/20200417.151831.1153577605111650154.horikyota.ntt@gmail.com
-rw-r--r--src/backend/catalog/aclchk.c47
-rw-r--r--src/test/modules/test_pg_dump/README2
-rw-r--r--src/test/modules/test_pg_dump/expected/test_pg_dump.out3
-rw-r--r--src/test/modules/test_pg_dump/sql/test_pg_dump.sql3
4 files changed, 35 insertions, 20 deletions
diff --git a/src/backend/catalog/aclchk.c b/src/backend/catalog/aclchk.c
index a3f680d388a..06687c53a60 100644
--- a/src/backend/catalog/aclchk.c
+++ b/src/backend/catalog/aclchk.c
@@ -5580,19 +5580,22 @@ recordExtObjInitPriv(Oid objoid, Oid classoid)
elog(ERROR, "cache lookup failed for relation %u", objoid);
pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
- /* Indexes don't have permissions */
+ /*
+ * Indexes don't have permissions, neither do the pg_class rows for
+ * composite types. (These cases are unreachable given the
+ * restrictions in ALTER EXTENSION ADD, but let's check anyway.)
+ */
if (pg_class_tuple->relkind == RELKIND_INDEX ||
- pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX)
- return;
-
- /* Composite types don't have permissions either */
- if (pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
+ pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX ||
+ pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
+ {
+ ReleaseSysCache(tuple);
return;
+ }
/*
- * If this isn't a sequence, index, or composite type then it's
- * possibly going to have columns associated with it that might have
- * ACLs.
+ * If this isn't a sequence then it's possibly going to have
+ * column-level ACLs associated with it.
*/
if (pg_class_tuple->relkind != RELKIND_SEQUENCE)
{
@@ -5724,6 +5727,11 @@ recordExtObjInitPriv(Oid objoid, Oid classoid)
SysScanDesc scan;
Relation relation;
+ /*
+ * Note: this is dead code, given that we don't allow large objects to
+ * be made extension members. But it seems worth carrying in case
+ * some future caller of this function has need for it.
+ */
relation = table_open(LargeObjectMetadataRelationId, RowExclusiveLock);
/* There's no syscache for pg_largeobject_metadata */
@@ -5866,19 +5874,22 @@ removeExtObjInitPriv(Oid objoid, Oid classoid)
elog(ERROR, "cache lookup failed for relation %u", objoid);
pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
- /* Indexes don't have permissions */
+ /*
+ * Indexes don't have permissions, neither do the pg_class rows for
+ * composite types. (These cases are unreachable given the
+ * restrictions in ALTER EXTENSION DROP, but let's check anyway.)
+ */
if (pg_class_tuple->relkind == RELKIND_INDEX ||
- pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX)
- return;
-
- /* Composite types don't have permissions either */
- if (pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
+ pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX ||
+ pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
+ {
+ ReleaseSysCache(tuple);
return;
+ }
/*
- * If this isn't a sequence, index, or composite type then it's
- * possibly going to have columns associated with it that might have
- * ACLs.
+ * If this isn't a sequence then it's possibly going to have
+ * column-level ACLs associated with it.
*/
if (pg_class_tuple->relkind != RELKIND_SEQUENCE)
{
diff --git a/src/test/modules/test_pg_dump/README b/src/test/modules/test_pg_dump/README
index e6c78b822c3..b7c2e337ca7 100644
--- a/src/test/modules/test_pg_dump/README
+++ b/src/test/modules/test_pg_dump/README
@@ -1,2 +1,4 @@
test_pg_dump is an extension explicitly to test pg_dump when
extensions are present in the system.
+
+We also make use of this module to test ALTER EXTENSION ADD/DROP.
diff --git a/src/test/modules/test_pg_dump/expected/test_pg_dump.out b/src/test/modules/test_pg_dump/expected/test_pg_dump.out
index c9bc6f76258..a50eaf6125d 100644
--- a/src/test/modules/test_pg_dump/expected/test_pg_dump.out
+++ b/src/test/modules/test_pg_dump/expected/test_pg_dump.out
@@ -4,7 +4,8 @@ ALTER EXTENSION test_pg_dump ADD DATABASE postgres; -- error
ERROR: syntax error at or near "DATABASE"
LINE 1: ALTER EXTENSION test_pg_dump ADD DATABASE postgres;
^
-CREATE TABLE test_pg_dump_t1 (c1 int);
+CREATE TABLE test_pg_dump_t1 (c1 int, junk text);
+ALTER TABLE test_pg_dump_t1 DROP COLUMN junk; -- to exercise dropped-col cases
CREATE VIEW test_pg_dump_v1 AS SELECT * FROM test_pg_dump_t1;
CREATE MATERIALIZED VIEW test_pg_dump_mv1 AS SELECT * FROM test_pg_dump_t1;
CREATE SCHEMA test_pg_dump_s1;
diff --git a/src/test/modules/test_pg_dump/sql/test_pg_dump.sql b/src/test/modules/test_pg_dump/sql/test_pg_dump.sql
index e463dec4040..a61a7c8c4ce 100644
--- a/src/test/modules/test_pg_dump/sql/test_pg_dump.sql
+++ b/src/test/modules/test_pg_dump/sql/test_pg_dump.sql
@@ -3,7 +3,8 @@ CREATE EXTENSION test_pg_dump;
ALTER EXTENSION test_pg_dump ADD DATABASE postgres; -- error
-CREATE TABLE test_pg_dump_t1 (c1 int);
+CREATE TABLE test_pg_dump_t1 (c1 int, junk text);
+ALTER TABLE test_pg_dump_t1 DROP COLUMN junk; -- to exercise dropped-col cases
CREATE VIEW test_pg_dump_v1 AS SELECT * FROM test_pg_dump_t1;
CREATE MATERIALIZED VIEW test_pg_dump_mv1 AS SELECT * FROM test_pg_dump_t1;
CREATE SCHEMA test_pg_dump_s1;