Skip to content

Commit ace9973

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 2091177 commit ace9973

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
@@ -80,7 +80,10 @@ static void ComputeIndexAttrs(IndexInfo *indexInfo,
8080
Oid relId,
8181
const char *accessMethodName, Oid accessMethodId,
8282
bool amcanorder,
83-
bool isconstraint);
83+
bool isconstraint,
84+
Oid ddl_userid,
85+
int ddl_sec_context,
86+
int *ddl_save_nestlevel);
8487
static char *ChooseIndexName(const char *tabname, Oid namespaceId,
8588
List *colnames, List *exclusionOpNames,
8689
bool primary, bool isconstraint);
@@ -220,9 +223,8 @@ CheckIndexCompatible(Oid oldId,
220223
* Compute the operator classes, collations, and exclusion operators for
221224
* the new index, so we can test whether it's compatible with the existing
222225
* one. Note that ComputeIndexAttrs might fail here, but that's OK:
223-
* DefineIndex would have called this function with the same arguments
224-
* later on, and it would have failed then anyway. Our attributeList
225-
* contains only key attributes, thus we're filling ii_NumIndexAttrs and
226+
* DefineIndex would have failed later. Our attributeList contains only
227+
* key attributes, thus we're filling ii_NumIndexAttrs and
226228
* ii_NumIndexKeyAttrs with same value.
227229
*/
228230
indexInfo = makeIndexInfo(numberOfAttributes, numberOfAttributes,
@@ -236,7 +238,7 @@ CheckIndexCompatible(Oid oldId,
236238
coloptions, attributeList,
237239
exclusionOpNames, relationId,
238240
accessMethodName, accessMethodId,
239-
amcanorder, isconstraint);
241+
amcanorder, isconstraint, InvalidOid, 0, NULL);
240242

241243

242244
/* Get the soon-obsolete pg_index tuple. */
@@ -482,6 +484,19 @@ WaitForOlderSnapshots(TransactionId limitXmin, bool progress)
482484
* DefineIndex
483485
* Creates a new index.
484486
*
487+
* This function manages the current userid according to the needs of pg_dump.
488+
* Recreating old-database catalog entries in new-database is fine, regardless
489+
* of which users would have permission to recreate those entries now. That's
490+
* just preservation of state. Running opaque expressions, like calling a
491+
* function named in a catalog entry or evaluating a pg_node_tree in a catalog
492+
* entry, as anyone other than the object owner, is not fine. To adhere to
493+
* those principles and to remain fail-safe, use the table owner userid for
494+
* most ACL checks. Use the original userid for ACL checks reached without
495+
* traversing opaque expressions. (pg_dump can predict such ACL checks from
496+
* catalogs.) Overall, this is a mess. Future DDL development should
497+
* consider offering one DDL command for catalog setup and a separate DDL
498+
* command for steps that run opaque expressions.
499+
*
485500
* 'relationId': the OID of the heap relation on which the index is to be
486501
* created
487502
* 'stmt': IndexStmt describing the properties of the new index.
@@ -899,7 +914,8 @@ DefineIndex(Oid relationId,
899914
coloptions, allIndexParams,
900915
stmt->excludeOpNames, relationId,
901916
accessMethodName, accessMethodId,
902-
amcanorder, stmt->isconstraint);
917+
amcanorder, stmt->isconstraint, root_save_userid,
918+
root_save_sec_context, &root_save_nestlevel);
903919

904920
/*
905921
* Extra checks when creating a PRIMARY KEY index.
@@ -1179,11 +1195,8 @@ DefineIndex(Oid relationId,
11791195

11801196
/*
11811197
* Roll back any GUC changes executed by index functions, and keep
1182-
* subsequent changes local to this command. It's barely possible that
1183-
* some index function changed a behavior-affecting GUC, e.g. xmloption,
1184-
* that affects subsequent steps. This improves bug-compatibility with
1185-
* older PostgreSQL versions. They did the AtEOXact_GUC() here for the
1186-
* purpose of clearing the above default_tablespace change.
1198+
* subsequent changes local to this command. This is essential if some
1199+
* index function changed a behavior-affecting GUC, e.g. search_path.
11871200
*/
11881201
AtEOXact_GUC(false, root_save_nestlevel);
11891202
root_save_nestlevel = NewGUCNestLevel();
@@ -1739,6 +1752,10 @@ CheckPredicate(Expr *predicate)
17391752
* Compute per-index-column information, including indexed column numbers
17401753
* or index expressions, opclasses and their options. Note, all output vectors
17411754
* should be allocated for all columns, including "including" ones.
1755+
*
1756+
* If the caller switched to the table owner, ddl_userid is the role for ACL
1757+
* checks reached without traversing opaque expressions. Otherwise, it's
1758+
* InvalidOid, and other ddl_* arguments are undefined.
17421759
*/
17431760
static void
17441761
ComputeIndexAttrs(IndexInfo *indexInfo,
@@ -1752,12 +1769,17 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
17521769
const char *accessMethodName,
17531770
Oid accessMethodId,
17541771
bool amcanorder,
1755-
bool isconstraint)
1772+
bool isconstraint,
1773+
Oid ddl_userid,
1774+
int ddl_sec_context,
1775+
int *ddl_save_nestlevel)
17561776
{
17571777
ListCell *nextExclOp;
17581778
ListCell *lc;
17591779
int attn;
17601780
int nkeycols = indexInfo->ii_NumIndexKeyAttrs;
1781+
Oid save_userid;
1782+
int save_sec_context;
17611783

17621784
/* Allocate space for exclusion operator info, if needed */
17631785
if (exclusionOpNames)
@@ -1771,6 +1793,9 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
17711793
else
17721794
nextExclOp = NULL;
17731795

1796+
if (OidIsValid(ddl_userid))
1797+
GetUserIdAndSecContext(&save_userid, &save_sec_context);
1798+
17741799
/*
17751800
* process attributeList
17761801
*/
@@ -1901,10 +1926,24 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
19011926
}
19021927

19031928
/*
1904-
* Apply collation override if any
1929+
* Apply collation override if any. Use of ddl_userid is necessary
1930+
* due to ACL checks therein, and it's safe because collations don't
1931+
* contain opaque expressions (or non-opaque expressions).
19051932
*/
19061933
if (attribute->collation)
1934+
{
1935+
if (OidIsValid(ddl_userid))
1936+
{
1937+
AtEOXact_GUC(false, *ddl_save_nestlevel);
1938+
SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
1939+
}
19071940
attcollation = get_collation_oid(attribute->collation, false);
1941+
if (OidIsValid(ddl_userid))
1942+
{
1943+
SetUserIdAndSecContext(save_userid, save_sec_context);
1944+
*ddl_save_nestlevel = NewGUCNestLevel();
1945+
}
1946+
}
19081947

19091948
/*
19101949
* Check we have a collation iff it's a collatable type. The only
@@ -1932,12 +1971,25 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
19321971
collationOidP[attn] = attcollation;
19331972

19341973
/*
1935-
* Identify the opclass to use.
1974+
* Identify the opclass to use. Use of ddl_userid is necessary due to
1975+
* ACL checks therein. This is safe despite opclasses containing
1976+
* opaque expressions (specifically, functions), because only
1977+
* superusers can define opclasses.
19361978
*/
1979+
if (OidIsValid(ddl_userid))
1980+
{
1981+
AtEOXact_GUC(false, *ddl_save_nestlevel);
1982+
SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
1983+
}
19371984
classOidP[attn] = ResolveOpClass(attribute->opclass,
19381985
atttype,
19391986
accessMethodName,
19401987
accessMethodId);
1988+
if (OidIsValid(ddl_userid))
1989+
{
1990+
SetUserIdAndSecContext(save_userid, save_sec_context);
1991+
*ddl_save_nestlevel = NewGUCNestLevel();
1992+
}
19411993

19421994
/*
19431995
* Identify the exclusion operator, if any.
@@ -1951,9 +2003,23 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
19512003

19522004
/*
19532005
* Find the operator --- it must accept the column datatype
1954-
* without runtime coercion (but binary compatibility is OK)
2006+
* without runtime coercion (but binary compatibility is OK).
2007+
* Operators contain opaque expressions (specifically, functions).
2008+
* compatible_oper_opid() boils down to oper() and
2009+
* IsBinaryCoercible(). PostgreSQL would have security problems
2010+
* elsewhere if oper() started calling opaque expressions.
19552011
*/
2012+
if (OidIsValid(ddl_userid))
2013+
{
2014+
AtEOXact_GUC(false, *ddl_save_nestlevel);
2015+
SetUserIdAndSecContext(ddl_userid, ddl_sec_context);
2016+
}
19562017
opid = compatible_oper_opid(opname, atttype, atttype, false);
2018+
if (OidIsValid(ddl_userid))
2019+
{
2020+
SetUserIdAndSecContext(save_userid, save_sec_context);
2021+
*ddl_save_nestlevel = NewGUCNestLevel();
2022+
}
19572023

19582024
/*
19592025
* Only allow commutative operators to be used in exclusion

0 commit comments

Comments
 (0)