Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8782ce4

Browse files
committed
CREATE INDEX: use the original userid for more ACL checks.
Commit a117ceb used the original userid for ACL checks located directly in DefineIndex(), but it still adopted the table owner userid for more ACL checks than intended. That broke dump/reload of indexes that refer to an operator class, collation, or exclusion operator in a schema other than "public" or "pg_catalog". Back-patch to v10 (all supported versions), like the earlier commit. Nathan Bossart and Noah Misch Discussion: https://postgr.es/m/f8a4105f076544c180a87ef0c4822352@stmuk.bayern.de
1 parent e8f037a commit 8782ce4

File tree

4 files changed

+239
-16
lines changed

4 files changed

+239
-16
lines changed

contrib/citext/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ DATA = citext--1.4.sql \
1111
citext--1.0--1.1.sql
1212
PGFILEDESC = "citext - case-insensitive character string data type"
1313

14-
REGRESS = citext
14+
REGRESS = create_index_acl citext
1515

1616
ifdef USE_PGXS
1717
PG_CONFIG = pg_config
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
-- Each DefineIndex() ACL check uses either the original userid or the table
2+
-- owner userid; see its header comment. Here, confirm that DefineIndex()
3+
-- uses its original userid where necessary. The test works by creating
4+
-- indexes that refer to as many sorts of objects as possible, with the table
5+
-- owner having as few applicable privileges as possible. (The privileges.sql
6+
-- regress_sro_user tests look for the opposite defect; they confirm that
7+
-- DefineIndex() uses the table owner userid where necessary.)
8+
-- Don't override tablespaces; this version lacks allow_in_place_tablespaces.
9+
BEGIN;
10+
CREATE ROLE regress_minimal;
11+
CREATE SCHEMA s;
12+
CREATE EXTENSION citext SCHEMA s;
13+
-- Revoke all conceivably-relevant ACLs within the extension. The system
14+
-- doesn't check all these ACLs, but this will provide some coverage if that
15+
-- ever changes.
16+
REVOKE ALL ON TYPE s.citext FROM PUBLIC;
17+
REVOKE ALL ON FUNCTION s.citext_pattern_lt FROM PUBLIC;
18+
REVOKE ALL ON FUNCTION s.citext_pattern_le FROM PUBLIC;
19+
REVOKE ALL ON FUNCTION s.citext_eq FROM PUBLIC;
20+
REVOKE ALL ON FUNCTION s.citext_pattern_ge FROM PUBLIC;
21+
REVOKE ALL ON FUNCTION s.citext_pattern_gt FROM PUBLIC;
22+
REVOKE ALL ON FUNCTION s.citext_pattern_cmp FROM PUBLIC;
23+
-- Functions sufficient for making an index column that has the side effect of
24+
-- changing search_path at expression planning time.
25+
CREATE FUNCTION public.setter() RETURNS bool VOLATILE
26+
LANGUAGE SQL AS $$SET search_path = s; SELECT true$$;
27+
CREATE FUNCTION s.const() RETURNS bool IMMUTABLE
28+
LANGUAGE SQL AS $$SELECT public.setter()$$;
29+
CREATE FUNCTION s.index_this_expr(s.citext, bool) RETURNS s.citext IMMUTABLE
30+
LANGUAGE SQL AS $$SELECT $1$$;
31+
REVOKE ALL ON FUNCTION public.setter FROM PUBLIC;
32+
REVOKE ALL ON FUNCTION s.const FROM PUBLIC;
33+
REVOKE ALL ON FUNCTION s.index_this_expr FROM PUBLIC;
34+
-- Even for an empty table, expression planning calls s.const & public.setter.
35+
GRANT EXECUTE ON FUNCTION public.setter TO regress_minimal;
36+
GRANT EXECUTE ON FUNCTION s.const TO regress_minimal;
37+
-- Function for index predicate.
38+
CREATE FUNCTION s.index_row_if(s.citext) RETURNS bool IMMUTABLE
39+
LANGUAGE SQL AS $$SELECT $1 IS NOT NULL$$;
40+
REVOKE ALL ON FUNCTION s.index_row_if FROM PUBLIC;
41+
-- Even for an empty table, CREATE INDEX checks ii_Predicate permissions.
42+
GRANT EXECUTE ON FUNCTION s.index_row_if TO regress_minimal;
43+
-- Non-extension, non-function objects.
44+
CREATE COLLATION s.coll (LOCALE="C");
45+
CREATE TABLE s.x (y s.citext);
46+
ALTER TABLE s.x OWNER TO regress_minimal;
47+
-- Empty-table DefineIndex()
48+
CREATE UNIQUE INDEX u0rows ON s.x USING btree
49+
((s.index_this_expr(y, s.const())) COLLATE s.coll s.citext_pattern_ops)
50+
WHERE s.index_row_if(y);
51+
ALTER TABLE s.x ADD CONSTRAINT e0rows EXCLUDE USING btree
52+
((s.index_this_expr(y, s.const())) COLLATE s.coll WITH s.=)
53+
WHERE (s.index_row_if(y));
54+
-- Make the table nonempty.
55+
INSERT INTO s.x VALUES ('foo'), ('bar');
56+
-- If the INSERT runs the planner on index expressions, a search_path change
57+
-- survives. As of 2022-06, the INSERT reuses a cached plan. It does so even
58+
-- under debug_discard_caches, since each index is new-in-transaction. If
59+
-- future work changes a cache lifecycle, this RESET may become necessary.
60+
RESET search_path;
61+
-- For a nonempty table, owner needs permissions throughout ii_Expressions.
62+
GRANT EXECUTE ON FUNCTION s.index_this_expr TO regress_minimal;
63+
CREATE UNIQUE INDEX u2rows ON s.x USING btree
64+
((s.index_this_expr(y, s.const())) COLLATE s.coll s.citext_pattern_ops)
65+
WHERE s.index_row_if(y);
66+
ALTER TABLE s.x ADD CONSTRAINT e2rows EXCLUDE USING btree
67+
((s.index_this_expr(y, s.const())) COLLATE s.coll WITH s.=)
68+
WHERE (s.index_row_if(y));
69+
-- Shall not find s.coll via search_path, despite the s.const->public.setter
70+
-- call having set search_path=s during expression planning. Suppress the
71+
-- message itself, which depends on the database encoding.
72+
\set VERBOSITY sqlstate
73+
ALTER TABLE s.x ADD CONSTRAINT underqualified EXCLUDE USING btree
74+
((s.index_this_expr(y, s.const())) COLLATE coll WITH s.=)
75+
WHERE (s.index_row_if(y));
76+
ERROR: 42704
77+
\set VERBOSITY default
78+
ROLLBACK;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
-- Each DefineIndex() ACL check uses either the original userid or the table
2+
-- owner userid; see its header comment. Here, confirm that DefineIndex()
3+
-- uses its original userid where necessary. The test works by creating
4+
-- indexes that refer to as many sorts of objects as possible, with the table
5+
-- owner having as few applicable privileges as possible. (The privileges.sql
6+
-- regress_sro_user tests look for the opposite defect; they confirm that
7+
-- DefineIndex() uses the table owner userid where necessary.)
8+
9+
-- Don't override tablespaces; this version lacks allow_in_place_tablespaces.
10+
11+
BEGIN;
12+
CREATE ROLE regress_minimal;
13+
CREATE SCHEMA s;
14+
CREATE EXTENSION citext SCHEMA s;
15+
-- Revoke all conceivably-relevant ACLs within the extension. The system
16+
-- doesn't check all these ACLs, but this will provide some coverage if that
17+
-- ever changes.
18+
REVOKE ALL ON TYPE s.citext FROM PUBLIC;
19+
REVOKE ALL ON FUNCTION s.citext_pattern_lt FROM PUBLIC;
20+
REVOKE ALL ON FUNCTION s.citext_pattern_le FROM PUBLIC;
21+
REVOKE ALL ON FUNCTION s.citext_eq FROM PUBLIC;
22+
REVOKE ALL ON FUNCTION s.citext_pattern_ge FROM PUBLIC;
23+
REVOKE ALL ON FUNCTION s.citext_pattern_gt FROM PUBLIC;
24+
REVOKE ALL ON FUNCTION s.citext_pattern_cmp FROM PUBLIC;
25+
-- Functions sufficient for making an index column that has the side effect of
26+
-- changing search_path at expression planning time.
27+
CREATE FUNCTION public.setter() RETURNS bool VOLATILE
28+
LANGUAGE SQL AS $$SET search_path = s; SELECT true$$;
29+
CREATE FUNCTION s.const() RETURNS bool IMMUTABLE
30+
LANGUAGE SQL AS $$SELECT public.setter()$$;
31+
CREATE FUNCTION s.index_this_expr(s.citext, bool) RETURNS s.citext IMMUTABLE
32+
LANGUAGE SQL AS $$SELECT $1$$;
33+
REVOKE ALL ON FUNCTION public.setter FROM PUBLIC;
34+
REVOKE ALL ON FUNCTION s.const FROM PUBLIC;
35+
REVOKE ALL ON FUNCTION s.index_this_expr FROM PUBLIC;
36+
-- Even for an empty table, expression planning calls s.const & public.setter.
37+
GRANT EXECUTE ON FUNCTION public.setter TO regress_minimal;
38+
GRANT EXECUTE ON FUNCTION s.const TO regress_minimal;
39+
-- Function for index predicate.
40+
CREATE FUNCTION s.index_row_if(s.citext) RETURNS bool IMMUTABLE
41+
LANGUAGE SQL AS $$SELECT $1 IS NOT NULL$$;
42+
REVOKE ALL ON FUNCTION s.index_row_if FROM PUBLIC;
43+
-- Even for an empty table, CREATE INDEX checks ii_Predicate permissions.
44+
GRANT EXECUTE ON FUNCTION s.index_row_if TO regress_minimal;
45+
-- Non-extension, non-function objects.
46+
CREATE COLLATION s.coll (LOCALE="C");
47+
CREATE TABLE s.x (y s.citext);
48+
ALTER TABLE s.x OWNER TO regress_minimal;
49+
-- Empty-table DefineIndex()
50+
CREATE UNIQUE INDEX u0rows ON s.x USING btree
51+
((s.index_this_expr(y, s.const())) COLLATE s.coll s.citext_pattern_ops)
52+
WHERE s.index_row_if(y);
53+
ALTER TABLE s.x ADD CONSTRAINT e0rows EXCLUDE USING btree
54+
((s.index_this_expr(y, s.const())) COLLATE s.coll WITH s.=)
55+
WHERE (s.index_row_if(y));
56+
-- Make the table nonempty.
57+
INSERT INTO s.x VALUES ('foo'), ('bar');
58+
-- If the INSERT runs the planner on index expressions, a search_path change
59+
-- survives. As of 2022-06, the INSERT reuses a cached plan. It does so even
60+
-- under debug_discard_caches, since each index is new-in-transaction. If
61+
-- future work changes a cache lifecycle, this RESET may become necessary.
62+
RESET search_path;
63+
-- For a nonempty table, owner needs permissions throughout ii_Expressions.
64+
GRANT EXECUTE ON FUNCTION s.index_this_expr TO regress_minimal;
65+
CREATE UNIQUE INDEX u2rows ON s.x USING btree
66+
((s.index_this_expr(y, s.const())) COLLATE s.coll s.citext_pattern_ops)
67+
WHERE s.index_row_if(y);
68+
ALTER TABLE s.x ADD CONSTRAINT e2rows EXCLUDE USING btree
69+
((s.index_this_expr(y, s.const())) COLLATE s.coll WITH s.=)
70+
WHERE (s.index_row_if(y));
71+
-- Shall not find s.coll via search_path, despite the s.const->public.setter
72+
-- call having set search_path=s during expression planning. Suppress the
73+
-- message itself, which depends on the database encoding.
74+
\set VERBOSITY sqlstate
75+
ALTER TABLE s.x ADD CONSTRAINT underqualified EXCLUDE USING btree
76+
((s.index_this_expr(y, s.const())) COLLATE coll WITH s.=)
77+
WHERE (s.index_row_if(y));
78+
\set VERBOSITY default
79+
ROLLBACK;

src/backend/commands/indexcmds.c

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ static void ComputeIndexAttrs(IndexInfo *indexInfo,
7979
Oid relId,
8080
const char *accessMethodName, Oid accessMethodId,
8181
bool amcanorder,
82-
bool isconstraint);
82+
bool isconstraint,
83+
Oid ddl_userid,
84+
int ddl_sec_context,
85+
int *ddl_save_nestlevel);
8386
static char *ChooseIndexName(const char *tabname, Oid namespaceId,
8487
List *colnames, List *exclusionOpNames,
8588
bool primary, bool isconstraint);
@@ -198,9 +201,8 @@ CheckIndexCompatible(Oid oldId,
198201
* Compute the operator classes, collations, and exclusion operators for
199202
* the new index, so we can test whether it's compatible with the existing
200203
* one. Note that ComputeIndexAttrs might fail here, but that's OK:
201-
* DefineIndex would have called this function with the same arguments
202-
* later on, and it would have failed then anyway. Our attributeList
203-
* contains only key attributes, thus we're filling ii_NumIndexAttrs and
204+
* DefineIndex would have failed later. Our attributeList contains only
205+
* key attributes, thus we're filling ii_NumIndexAttrs and
204206
* ii_NumIndexKeyAttrs with same value.
205207
*/
206208
indexInfo = makeIndexInfo(numberOfAttributes, numberOfAttributes,
@@ -214,7 +216,7 @@ CheckIndexCompatible(Oid oldId,
214216
coloptions, attributeList,
215217
exclusionOpNames, relationId,
216218
accessMethodName, accessMethodId,
217-
amcanorder, isconstraint);
219+
amcanorder, isconstraint, InvalidOid, 0, NULL);
218220

219221

220222
/* Get the soon-obsolete pg_index tuple. */
@@ -455,6 +457,19 @@ WaitForOlderSnapshots(TransactionId limitXmin, bool progress)
455457
* DefineIndex
456458
* Creates a new index.
457459
*
460+
* This function manages the current userid according to the needs of pg_dump.
461+
* Recreating old-database catalog entries in new-database is fine, regardless
462+
* of which users would have permission to recreate those entries now. That's
463+
* just preservation of state. Running opaque expressions, like calling a
464+
* function named in a catalog entry or evaluating a pg_node_tree in a catalog
465+
* entry, as anyone other than the object owner, is not fine. To adhere to
466+
* those principles and to remain fail-safe, use the table owner userid for
467+
* most ACL checks. Use the original userid for ACL checks reached without
468+
* traversing opaque expressions. (pg_dump can predict such ACL checks from
469+
* catalogs.) Overall, this is a mess. Future DDL development should
470+
* consider offering one DDL command for catalog setup and a separate DDL
471+
* command for steps that run opaque expressions.
472+
*
458473
* 'relationId': the OID of the heap relation on which the index is to be
459474
* created
460475
* 'stmt': IndexStmt describing the properties of the new index.
@@ -871,7 +886,8 @@ DefineIndex(Oid relationId,
871886
coloptions, allIndexParams,
872887
stmt->excludeOpNames, relationId,
873888
accessMethodName, accessMethodId,
874-
amcanorder, stmt->isconstraint);
889+
amcanorder, stmt->isconstraint, root_save_userid,
890+
root_save_sec_context, &root_save_nestlevel);
875891

876892
/*
877893
* Extra checks when creating a PRIMARY KEY index.
@@ -1147,11 +1163,8 @@ DefineIndex(Oid relationId,
11471163

11481164
/*
11491165
* Roll back any GUC changes executed by index functions, and keep
1150-
* subsequent changes local to this command. It's barely possible that
1151-
* some index function changed a behavior-affecting GUC, e.g. xmloption,
1152-
* that affects subsequent steps. This improves bug-compatibility with
1153-
* older PostgreSQL versions. They did the AtEOXact_GUC() here for the
1154-
* purpose of clearing the above default_tablespace change.
1166+
* subsequent changes local to this command. This is essential if some
1167+
* index function changed a behavior-affecting GUC, e.g. search_path.
11551168
*/
11561169
AtEOXact_GUC(false, root_save_nestlevel);
11571170
root_save_nestlevel = NewGUCNestLevel();
@@ -1686,6 +1699,10 @@ CheckPredicate(Expr *predicate)
16861699
* Compute per-index-column information, including indexed column numbers
16871700
* or index expressions, opclasses and their options. Note, all output vectors
16881701
* should be allocated for all columns, including "including" ones.
1702+
*
1703+
* If the caller switched to the table owner, ddl_userid is the role for ACL
1704+
* checks reached without traversing opaque expressions. Otherwise, it's
1705+
* InvalidOid, and other ddl_* arguments are undefined.
16891706
*/
16901707
static void
16911708
ComputeIndexAttrs(IndexInfo *indexInfo,
@@ -1699,12 +1716,17 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
16991716
const char *accessMethodName,
17001717
Oid accessMethodId,
17011718
bool amcanorder,
1702-
bool isconstraint)
1719+
bool isconstraint,
1720+
Oid ddl_userid,
1721+
int ddl_sec_context,
1722+
int *ddl_save_nestlevel)
17031723
{
17041724
ListCell *nextExclOp;
17051725
ListCell *lc;
17061726
int attn;
17071727
int nkeycols = indexInfo->ii_NumIndexKeyAttrs;
1728+
Oid save_userid;
1729+
int save_sec_context;
17081730

17091731
/* Allocate space for exclusion operator info, if needed */
17101732
if (exclusionOpNames)
@@ -1718,6 +1740,9 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
17181740
else
17191741
nextExclOp = NULL;
17201742

1743+
if (OidIsValid(ddl_userid))
1744+
GetUserIdAndSecContext(&save_userid, &save_sec_context);
1745+
17211746
/*
17221747
* process attributeList
17231748
*/
@@ -1848,10 +1873,24 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
18481873
}
18491874

18501875
/*
1851-
* Apply collation override if any
1876+
* Apply collation override if any. Use of ddl_userid is necessary
1877+
* due to ACL checks therein, and it's safe because collations don't
1878+
* contain opaque expressions (or non-opaque expressions).
18521879
*/
18531880
if (attribute->collation)
1881+
{
1882+
if (OidIsValid(ddl_userid))
1883+
{
1884+
AtEOXact_GUC(false, *ddl_save_nestlevel);
1885+
SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
1886+
}
18541887
attcollation = get_collation_oid(attribute->collation, false);
1888+
if (OidIsValid(ddl_userid))
1889+
{
1890+
SetUserIdAndSecContext(save_userid, save_sec_context);
1891+
*ddl_save_nestlevel = NewGUCNestLevel();
1892+
}
1893+
}
18551894

18561895
/*
18571896
* Check we have a collation iff it's a collatable type. The only
@@ -1879,12 +1918,25 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
18791918
collationOidP[attn] = attcollation;
18801919

18811920
/*
1882-
* Identify the opclass to use.
1921+
* Identify the opclass to use. Use of ddl_userid is necessary due to
1922+
* ACL checks therein. This is safe despite opclasses containing
1923+
* opaque expressions (specifically, functions), because only
1924+
* superusers can define opclasses.
18831925
*/
1926+
if (OidIsValid(ddl_userid))
1927+
{
1928+
AtEOXact_GUC(false, *ddl_save_nestlevel);
1929+
SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
1930+
}
18841931
classOidP[attn] = ResolveOpClass(attribute->opclass,
18851932
atttype,
18861933
accessMethodName,
18871934
accessMethodId);
1935+
if (OidIsValid(ddl_userid))
1936+
{
1937+
SetUserIdAndSecContext(save_userid, save_sec_context);
1938+
*ddl_save_nestlevel = NewGUCNestLevel();
1939+
}
18881940

18891941
/*
18901942
* Identify the exclusion operator, if any.
@@ -1898,9 +1950,23 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
18981950

18991951
/*
19001952
* Find the operator --- it must accept the column datatype
1901-
* without runtime coercion (but binary compatibility is OK)
1953+
* without runtime coercion (but binary compatibility is OK).
1954+
* Operators contain opaque expressions (specifically, functions).
1955+
* compatible_oper_opid() boils down to oper() and
1956+
* IsBinaryCoercible(). PostgreSQL would have security problems
1957+
* elsewhere if oper() started calling opaque expressions.
19021958
*/
1959+
if (OidIsValid(ddl_userid))
1960+
{
1961+
AtEOXact_GUC(false, *ddl_save_nestlevel);
1962+
SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
1963+
}
19031964
opid = compatible_oper_opid(opname, atttype, atttype, false);
1965+
if (OidIsValid(ddl_userid))
1966+
{
1967+
SetUserIdAndSecContext(save_userid, save_sec_context);
1968+
*ddl_save_nestlevel = NewGUCNestLevel();
1969+
}
19041970

19051971
/*
19061972
* Only allow commutative operators to be used in exclusion

0 commit comments

Comments
 (0)