diff options
author | Robert Haas <rhaas@postgresql.org> | 2014-02-17 09:33:31 -0500 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2014-02-17 09:33:31 -0500 |
commit | 5f173040e324f6c2eebb90d86cf1b0cdb5890f0a (patch) | |
tree | 9c3d313acb74c092bc2664b862720f340460bb80 /src/backend/commands/indexcmds.c | |
parent | 540b4e5bc85f7e44842493a810b04a84881db20f (diff) | |
download | postgresql-5f173040e324f6c2eebb90d86cf1b0cdb5890f0a.tar.gz postgresql-5f173040e324f6c2eebb90d86cf1b0cdb5890f0a.zip |
Avoid repeated name lookups during table and index DDL.
If the name lookups come to different conclusions due to concurrent
activity, we might perform some parts of the DDL on a different table
than other parts. At least in the case of CREATE INDEX, this can be
used to cause the permissions checks to be performed against a
different table than the index creation, allowing for a privilege
escalation attack.
This changes the calling convention for DefineIndex, CreateTrigger,
transformIndexStmt, transformAlterTableStmt, CheckIndexCompatible
(in 9.2 and newer), and AlterTable (in 9.1 and older). In addition,
CheckRelationOwnership is removed in 9.2 and newer and the calling
convention is changed in older branches. A field has also been added
to the Constraint node (FkConstraint in 8.4). Third-party code calling
these functions or using the Constraint node will require updating.
Report by Andres Freund. Patch by Robert Haas and Andres Freund,
reviewed by Tom Lane.
Security: CVE-2014-0062
Diffstat (limited to 'src/backend/commands/indexcmds.c')
-rw-r--r-- | src/backend/commands/indexcmds.c | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 4259c4757a6..38ce023a8a2 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -112,7 +112,6 @@ static void RangeVarCallbackForReindexIndex(const RangeVar *relation, */ bool CheckIndexCompatible(Oid oldId, - RangeVar *heapRelation, char *accessMethodName, List *attributeList, List *exclusionOpNames) @@ -140,7 +139,7 @@ CheckIndexCompatible(Oid oldId, Datum d; /* Caller should already have the relation locked in some way. */ - relationId = RangeVarGetRelid(heapRelation, NoLock, false); + relationId = IndexGetRelation(oldId, false); /* * We can pretend isconstraint = false unconditionally. It only serves to @@ -280,6 +279,8 @@ CheckIndexCompatible(Oid oldId, * DefineIndex * Creates a new index. * + * 'relationId': the OID of the heap relation on which the index is to be + * created * 'stmt': IndexStmt describing the properties of the new index. * 'indexRelationId': normally InvalidOid, but during bootstrap can be * nonzero to specify a preselected OID for the index. @@ -293,7 +294,8 @@ CheckIndexCompatible(Oid oldId, * Returns the OID of the created index. */ Oid -DefineIndex(IndexStmt *stmt, +DefineIndex(Oid relationId, + IndexStmt *stmt, Oid indexRelationId, bool is_alter_table, bool check_rights, @@ -306,7 +308,6 @@ DefineIndex(IndexStmt *stmt, Oid *collationObjectId; Oid *classObjectId; Oid accessMethodId; - Oid relationId; Oid namespaceId; Oid tablespaceId; List *indexColNames; @@ -325,6 +326,7 @@ DefineIndex(IndexStmt *stmt, int n_old_snapshots; LockRelId heaprelid; LOCKTAG heaplocktag; + LOCKMODE lockmode; Snapshot snapshot; int i; @@ -343,14 +345,18 @@ DefineIndex(IndexStmt *stmt, INDEX_MAX_KEYS))); /* - * Open heap relation, acquire a suitable lock on it, remember its OID - * * Only SELECT ... FOR UPDATE/SHARE are allowed while doing a standard * index build; but for concurrent builds we allow INSERT/UPDATE/DELETE * (but not VACUUM). + * + * NB: Caller is responsible for making sure that relationId refers + * to the relation on which the index should be built; except in bootstrap + * mode, this will typically require the caller to have already locked + * the relation. To avoid lock upgrade hazards, that lock should be at + * least as strong as the one we take here. */ - rel = heap_openrv(stmt->relation, - (stmt->concurrent ? ShareUpdateExclusiveLock : ShareLock)); + lockmode = stmt->concurrent ? ShareUpdateExclusiveLock : ShareLock; + rel = heap_open(relationId, lockmode); relationId = RelationGetRelid(rel); namespaceId = RelationGetNamespace(rel); |