postgresql/src/backend/catalog
Tom Lane fa2952d8eb Fix missing role dependencies for some schema and type ACLs.
This patch fixes several related cases in which pg_shdepend entries were
never made, or were lost, for references to roles appearing in the ACLs of
schemas and/or types.  While that did no immediate harm, if a referenced
role were later dropped, the drop would be allowed and would leave a
dangling reference in the object's ACL.  That still wasn't a big problem
for normal database usage, but it would cause obscure failures in
subsequent dump/reload or pg_upgrade attempts, taking the form of
attempts to grant privileges to all-numeric role names.  (I think I've
seen field reports matching that symptom, but can't find any right now.)

Several cases are fixed here:

1. ALTER DOMAIN SET/DROP DEFAULT would lose the dependencies for any
existing ACL entries for the domain.  This case is ancient, dating
back as far as we've had pg_shdepend tracking at all.

2. If a default type privilege applies, CREATE TYPE recorded the
ACL properly but forgot to install dependency entries for it.
This dates to the addition of default privileges for types in 9.2.

3. If a default schema privilege applies, CREATE SCHEMA recorded the
ACL properly but forgot to install dependency entries for it.
This dates to the addition of default privileges for schemas in v10
(commit ab89e465c).

Another somewhat-related problem is that when creating a relation
rowtype or implicit array type, TypeCreate would apply any available
default type privileges to that type, which we don't really want
since such an object isn't supposed to have privileges of its own.
(You can't, for example, drop such privileges once they've been added
to an array type.)

ab89e465c is also to blame for a race condition in the regression tests:
privileges.sql transiently installed globally-applicable default
privileges on schemas, which sometimes got absorbed into the ACLs of
schemas created by concurrent test scripts.  This should have resulted
in failures when privileges.sql tried to drop the role holding such
privileges; but thanks to the bug fixed here, it instead led to dangling
ACLs in the final state of the regression database.  We'd managed not to
notice that, but it became obvious in the wake of commit da906766c, which
allowed the race condition to occur in pg_upgrade tests.

To fix, add a function recordDependencyOnNewAcl to encapsulate what
callers of get_user_default_acl need to do; while the original call
sites got that right via ad-hoc code, none of the later-added ones
have.  Also change GenerateTypeDependencies to generate these
dependencies, which requires adding the typacl to its parameter list.
(That might be annoying if there are any extensions calling that
function directly; but if there are, they're most likely buggy in the
same way as the core callers were, so they need work anyway.)  While
I was at it, I changed GenerateTypeDependencies to accept most of its
parameters in the form of a Form_pg_type pointer, making its parameter
list a bit less unwieldy and mistake-prone.

The test race condition is fixed just by wrapping the addition and
removal of default privileges into a single transaction, so that that
state is never visible externally.  We might eventually prefer to
separate out tests of default privileges into a script that runs by
itself, but that would be a bigger change and would make the tests
run slower overall.

Back-patch relevant parts to all supported branches.

Discussion: https://postgr.es/m/15719.1541725287@sss.pgh.pa.us
2018-11-09 20:42:14 -05:00
..
.gitignore Replace our traditional initial-catalog-data format with a better design. 2018-04-08 13:17:27 -04:00
Catalog.pm Add a "return" statement to pacify perlcritic. 2018-09-20 16:10:23 -04:00
Makefile Rearrange makefile rules for running Gen_fmgrtab.pl. 2018-05-03 17:54:18 -04:00
aclchk.c Fix missing role dependencies for some schema and type ACLs. 2018-11-09 20:42:14 -05:00
catalog.c Add toast tables to most system catalogs 2018-07-20 07:43:41 +09:00
dependency.c Create an RTE field to record the query's lock mode for each relation. 2018-09-30 13:55:51 -04:00
genbki.pl Teach genbki.pl to auto-generate pg_type entries for array types. 2018-09-20 15:14:46 -04:00
heap.c Fix missing role dependencies for some schema and type ACLs. 2018-11-09 20:42:14 -05:00
index.c Remove obsolete pg_constraint.consrc column 2018-11-01 20:36:05 +01:00
indexing.c Centralize executor's opening/closing of Relations for rangetable entries. 2018-10-04 14:03:42 -04:00
information_schema.sql Add prokind column, replacing proisagg and proiswindow 2018-03-02 13:48:33 -05:00
namespace.c Clarify comment about assignment and reset of temp namespace ID in MyProc 2018-08-21 08:32:18 +09:00
objectaccess.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
objectaddress.c Refactor routines for subscription and publication lookups 2018-09-18 12:00:18 +09:00
partition.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
pg_aggregate.c Fix type checking for support functions of parallel VARIADIC aggregates. 2018-05-15 15:06:53 -04:00
pg_collation.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
pg_constraint.c Remove obsolete pg_constraint.consrc column 2018-11-01 20:36:05 +01:00
pg_conversion.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
pg_db_role_setting.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
pg_depend.c Local partitioned indexes 2018-01-19 11:49:22 -03:00
pg_enum.c Relax transactional restrictions on ALTER TYPE ... ADD VALUE (redux). 2018-10-09 12:51:01 +13:00
pg_inherits.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
pg_largeobject.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
pg_namespace.c Fix missing role dependencies for some schema and type ACLs. 2018-11-09 20:42:14 -05:00
pg_operator.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
pg_proc.c Fix missing role dependencies for some schema and type ACLs. 2018-11-09 20:42:14 -05:00
pg_publication.c Refactor routines for subscription and publication lookups 2018-09-18 12:00:18 +09:00
pg_range.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
pg_shdepend.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
pg_subscription.c Refactor routines for subscription and publication lookups 2018-09-18 12:00:18 +09:00
pg_type.c Fix missing role dependencies for some schema and type ACLs. 2018-11-09 20:42:14 -05:00
sql_feature_packages.txt > I have installed your patch and adjusted the names of the standards 2004-12-02 22:51:28 +00:00
sql_features.txt Update SQL features list 2018-05-21 15:29:22 -04:00
storage.c Further cleanup of client dependencies on src/include/catalog headers. 2018-04-09 14:39:58 -04:00
system_views.sql Switch pg_promote to be parallel-safe 2018-11-06 14:11:21 +09:00
toasting.c Rename IndexInfo.ii_KeyAttrNumbers array 2018-04-12 13:02:45 +03:00