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

Commit 7e92f78

Browse files
committed
In extensions, don't replace objects not belonging to the extension.
Previously, if an extension script did CREATE OR REPLACE and there was an existing object not belonging to the extension, it would overwrite the object and adopt it into the extension. This is problematic, first because the overwrite is probably unintentional, and second because we didn't change the object's ownership. Thus a hostile user could create an object in advance of an expected CREATE EXTENSION command, and would then have ownership rights on an extension object, which could be modified for trojan-horse-type attacks. Hence, forbid CREATE OR REPLACE of an existing object unless it already belongs to the extension. (Note that we've always forbidden replacing an object that belongs to some other extension; only the behavior for previously-free-standing objects changes here.) For the same reason, also fail CREATE IF NOT EXISTS when there is an existing object that doesn't belong to the extension. Our thanks to Sven Klemm for reporting this problem. Security: CVE-2022-2625
1 parent 330c48b commit 7e92f78

21 files changed

+540
-53
lines changed

doc/src/sgml/extend.sgml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,17 +1288,6 @@ SELECT * FROM pg_extension_update_paths('<replaceable>extension_name</replaceabl
12881288
trusted if it depends on another one, unless that other one is always
12891289
installed in <literal>pg_catalog</literal>.
12901290
</para>
1291-
1292-
<para>
1293-
Do <emphasis>not</emphasis> use <command>CREATE OR REPLACE
1294-
FUNCTION</command>, except in an update script that must change the
1295-
definition of a function that is known to be an extension member
1296-
already. (Likewise for other <literal>OR REPLACE</literal> options.)
1297-
Using <literal>OR REPLACE</literal> unnecessarily not only has a risk
1298-
of accidentally overwriting someone else's function, but it creates a
1299-
security hazard since the overwritten function would still be owned by
1300-
its original owner, who could modify it.
1301-
</para>
13021291
</sect3>
13031292
</sect2>
13041293

src/backend/catalog/pg_collation.c

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,25 @@ CollationCreate(const char *collname, Oid collnamespace,
7878
* friendlier error message. The unique index provides a backstop against
7979
* race conditions.
8080
*/
81-
if (SearchSysCacheExists3(COLLNAMEENCNSP,
82-
PointerGetDatum(collname),
83-
Int32GetDatum(collencoding),
84-
ObjectIdGetDatum(collnamespace)))
81+
oid = GetSysCacheOid3(COLLNAMEENCNSP,
82+
Anum_pg_collation_oid,
83+
PointerGetDatum(collname),
84+
Int32GetDatum(collencoding),
85+
ObjectIdGetDatum(collnamespace));
86+
if (OidIsValid(oid))
8587
{
8688
if (quiet)
8789
return InvalidOid;
8890
else if (if_not_exists)
8991
{
92+
/*
93+
* If we are in an extension script, insist that the pre-existing
94+
* object be a member of the extension, to avoid security risks.
95+
*/
96+
ObjectAddressSet(myself, CollationRelationId, oid);
97+
checkMembershipInCurrentExtension(&myself);
98+
99+
/* OK to skip */
90100
ereport(NOTICE,
91101
(errcode(ERRCODE_DUPLICATE_OBJECT),
92102
collencoding == -1
@@ -116,16 +126,19 @@ CollationCreate(const char *collname, Oid collnamespace,
116126
* so we take a ShareRowExclusiveLock earlier, to protect against
117127
* concurrent changes fooling this check.
118128
*/
119-
if ((collencoding == -1 &&
120-
SearchSysCacheExists3(COLLNAMEENCNSP,
121-
PointerGetDatum(collname),
122-
Int32GetDatum(GetDatabaseEncoding()),
123-
ObjectIdGetDatum(collnamespace))) ||
124-
(collencoding != -1 &&
125-
SearchSysCacheExists3(COLLNAMEENCNSP,
126-
PointerGetDatum(collname),
127-
Int32GetDatum(-1),
128-
ObjectIdGetDatum(collnamespace))))
129+
if (collencoding == -1)
130+
oid = GetSysCacheOid3(COLLNAMEENCNSP,
131+
Anum_pg_collation_oid,
132+
PointerGetDatum(collname),
133+
Int32GetDatum(GetDatabaseEncoding()),
134+
ObjectIdGetDatum(collnamespace));
135+
else
136+
oid = GetSysCacheOid3(COLLNAMEENCNSP,
137+
Anum_pg_collation_oid,
138+
PointerGetDatum(collname),
139+
Int32GetDatum(-1),
140+
ObjectIdGetDatum(collnamespace));
141+
if (OidIsValid(oid))
129142
{
130143
if (quiet)
131144
{
@@ -134,6 +147,14 @@ CollationCreate(const char *collname, Oid collnamespace,
134147
}
135148
else if (if_not_exists)
136149
{
150+
/*
151+
* If we are in an extension script, insist that the pre-existing
152+
* object be a member of the extension, to avoid security risks.
153+
*/
154+
ObjectAddressSet(myself, CollationRelationId, oid);
155+
checkMembershipInCurrentExtension(&myself);
156+
157+
/* OK to skip */
137158
table_close(rel, NoLock);
138159
ereport(NOTICE,
139160
(errcode(ERRCODE_DUPLICATE_OBJECT),

src/backend/catalog/pg_depend.c

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,23 @@ recordMultipleDependencies(const ObjectAddress *depender,
124124

125125
/*
126126
* If we are executing a CREATE EXTENSION operation, mark the given object
127-
* as being a member of the extension. Otherwise, do nothing.
127+
* as being a member of the extension, or check that it already is one.
128+
* Otherwise, do nothing.
128129
*
129130
* This must be called during creation of any user-definable object type
130131
* that could be a member of an extension.
131132
*
132-
* If isReplace is true, the object already existed (or might have already
133-
* existed), so we must check for a pre-existing extension membership entry.
134-
* Passing false is a guarantee that the object is newly created, and so
135-
* could not already be a member of any extension.
133+
* isReplace must be true if the object already existed, and false if it is
134+
* newly created. In the former case we insist that it already be a member
135+
* of the current extension. In the latter case we can skip checking whether
136+
* it is already a member of any extension.
136137
*
137138
* Note: isReplace = true is typically used when updating a object in
138-
* CREATE OR REPLACE and similar commands. The net effect is that if an
139-
* extension script uses such a command on a pre-existing free-standing
140-
* object, the object will be absorbed into the extension. If the object
141-
* is already a member of some other extension, the command will fail.
142-
* This behavior is desirable for cases such as replacing a shell type.
139+
* CREATE OR REPLACE and similar commands. We used to allow the target
140+
* object to not already be an extension member, instead silently absorbing
141+
* it into the current extension. However, this was both error-prone
142+
* (extensions might accidentally overwrite free-standing objects) and
143+
* a security hazard (since the object would retain its previous ownership).
143144
*/
144145
void
145146
recordDependencyOnCurrentExtension(const ObjectAddress *object,
@@ -157,6 +158,12 @@ recordDependencyOnCurrentExtension(const ObjectAddress *object,
157158
{
158159
Oid oldext;
159160

161+
/*
162+
* Side note: these catalog lookups are safe only because the
163+
* object is a pre-existing one. In the not-isReplace case, the
164+
* caller has most likely not yet done a CommandCounterIncrement
165+
* that would make the new object visible.
166+
*/
160167
oldext = getExtensionOfObject(object->classId, object->objectId);
161168
if (OidIsValid(oldext))
162169
{
@@ -170,6 +177,13 @@ recordDependencyOnCurrentExtension(const ObjectAddress *object,
170177
getObjectDescription(object),
171178
get_extension_name(oldext))));
172179
}
180+
/* It's a free-standing object, so reject */
181+
ereport(ERROR,
182+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
183+
errmsg("%s is not a member of extension \"%s\"",
184+
getObjectDescription(object),
185+
get_extension_name(CurrentExtensionObject)),
186+
errdetail("An extension is not allowed to replace an object that it does not own.")));
173187
}
174188

175189
/* OK, record it as a member of CurrentExtensionObject */
@@ -181,6 +195,49 @@ recordDependencyOnCurrentExtension(const ObjectAddress *object,
181195
}
182196
}
183197

198+
/*
199+
* If we are executing a CREATE EXTENSION operation, check that the given
200+
* object is a member of the extension, and throw an error if it isn't.
201+
* Otherwise, do nothing.
202+
*
203+
* This must be called whenever a CREATE IF NOT EXISTS operation (for an
204+
* object type that can be an extension member) has found that an object of
205+
* the desired name already exists. It is insecure for an extension to use
206+
* IF NOT EXISTS except when the conflicting object is already an extension
207+
* member; otherwise a hostile user could substitute an object with arbitrary
208+
* properties.
209+
*/
210+
void
211+
checkMembershipInCurrentExtension(const ObjectAddress *object)
212+
{
213+
/*
214+
* This is actually the same condition tested in
215+
* recordDependencyOnCurrentExtension; but we want to issue a
216+
* differently-worded error, and anyway it would be pretty confusing to
217+
* call recordDependencyOnCurrentExtension in these circumstances.
218+
*/
219+
220+
/* Only whole objects can be extension members */
221+
Assert(object->objectSubId == 0);
222+
223+
if (creating_extension)
224+
{
225+
Oid oldext;
226+
227+
oldext = getExtensionOfObject(object->classId, object->objectId);
228+
/* If already a member of this extension, OK */
229+
if (oldext == CurrentExtensionObject)
230+
return;
231+
/* Else complain */
232+
ereport(ERROR,
233+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
234+
errmsg("%s is not a member of extension \"%s\"",
235+
getObjectDescription(object),
236+
get_extension_name(CurrentExtensionObject)),
237+
errdetail("An extension may only use CREATE ... IF NOT EXISTS to skip object creation if the conflicting object is one that it already owns.")));
238+
}
239+
}
240+
184241
/*
185242
* deleteDependencyRecordsFor -- delete all records with given depender
186243
* classId/objectId. Returns the number of records deleted.

src/backend/catalog/pg_operator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ makeOperatorDependencies(HeapTuple tuple,
874874

875875
/* Dependency on extension */
876876
if (makeExtensionDep)
877-
recordDependencyOnCurrentExtension(&myself, true);
877+
recordDependencyOnCurrentExtension(&myself, isUpdate);
878878

879879
return myself;
880880
}

src/backend/catalog/pg_type.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,11 @@ TypeCreate(Oid newTypeOid,
541541
* rebuild should be true if this is a pre-existing type. We will remove
542542
* existing dependencies and rebuild them from scratch. This is needed for
543543
* ALTER TYPE, and also when replacing a shell type. We don't remove any
544-
* existing extension dependency, though (hence, if makeExtensionDep is also
545-
* true and the type belongs to some other extension, an error will occur).
544+
* existing extension dependency, though; hence, if makeExtensionDep is also
545+
* true and we're in an extension script, an error will occur unless the
546+
* type already belongs to the current extension. That's the behavior we
547+
* want when replacing a shell type, which is the only case where both flags
548+
* are true.
546549
*/
547550
void
548551
GenerateTypeDependencies(HeapTuple typeTuple,

src/backend/commands/createas.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,27 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
242242
if (stmt->if_not_exists)
243243
{
244244
Oid nspid;
245+
Oid oldrelid;
245246

246-
nspid = RangeVarGetCreationNamespace(stmt->into->rel);
247+
nspid = RangeVarGetCreationNamespace(into->rel);
247248

248-
if (get_relname_relid(stmt->into->rel->relname, nspid))
249+
oldrelid = get_relname_relid(into->rel->relname, nspid);
250+
if (OidIsValid(oldrelid))
249251
{
252+
/*
253+
* The relation exists and IF NOT EXISTS has been specified.
254+
*
255+
* If we are in an extension script, insist that the pre-existing
256+
* object be a member of the extension, to avoid security risks.
257+
*/
258+
ObjectAddressSet(address, RelationRelationId, oldrelid);
259+
checkMembershipInCurrentExtension(&address);
260+
261+
/* OK to skip */
250262
ereport(NOTICE,
251263
(errcode(ERRCODE_DUPLICATE_TABLE),
252264
errmsg("relation \"%s\" already exists, skipping",
253-
stmt->into->rel->relname)));
265+
into->rel->relname)));
254266
return InvalidObjectAddress;
255267
}
256268
}

src/backend/commands/foreigncmds.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,13 +883,22 @@ CreateForeignServer(CreateForeignServerStmt *stmt)
883883
ownerId = GetUserId();
884884

885885
/*
886-
* Check that there is no other foreign server by this name. Do nothing if
887-
* IF NOT EXISTS was enforced.
886+
* Check that there is no other foreign server by this name. If there is
887+
* one, do nothing if IF NOT EXISTS was specified.
888888
*/
889-
if (GetForeignServerByName(stmt->servername, true) != NULL)
889+
srvId = get_foreign_server_oid(stmt->servername, true);
890+
if (OidIsValid(srvId))
890891
{
891892
if (stmt->if_not_exists)
892893
{
894+
/*
895+
* If we are in an extension script, insist that the pre-existing
896+
* object be a member of the extension, to avoid security risks.
897+
*/
898+
ObjectAddressSet(myself, ForeignServerRelationId, srvId);
899+
checkMembershipInCurrentExtension(&myself);
900+
901+
/* OK to skip */
893902
ereport(NOTICE,
894903
(errcode(ERRCODE_DUPLICATE_OBJECT),
895904
errmsg("server \"%s\" already exists, skipping",
@@ -1178,6 +1187,10 @@ CreateUserMapping(CreateUserMappingStmt *stmt)
11781187
{
11791188
if (stmt->if_not_exists)
11801189
{
1190+
/*
1191+
* Since user mappings aren't members of extensions (see comments
1192+
* below), no need for checkMembershipInCurrentExtension here.
1193+
*/
11811194
ereport(NOTICE,
11821195
(errcode(ERRCODE_DUPLICATE_OBJECT),
11831196
errmsg("user mapping for \"%s\" already exists for server \"%s\", skipping",

src/backend/commands/schemacmds.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,25 @@ CreateSchemaCommand(CreateSchemaStmt *stmt, const char *queryString,
112112
* the permissions checks, but since CREATE TABLE IF NOT EXISTS makes its
113113
* creation-permission check first, we do likewise.
114114
*/
115-
if (stmt->if_not_exists &&
116-
SearchSysCacheExists1(NAMESPACENAME, PointerGetDatum(schemaName)))
115+
if (stmt->if_not_exists)
117116
{
118-
ereport(NOTICE,
119-
(errcode(ERRCODE_DUPLICATE_SCHEMA),
120-
errmsg("schema \"%s\" already exists, skipping",
121-
schemaName)));
122-
return InvalidOid;
117+
namespaceId = get_namespace_oid(schemaName, true);
118+
if (OidIsValid(namespaceId))
119+
{
120+
/*
121+
* If we are in an extension script, insist that the pre-existing
122+
* object be a member of the extension, to avoid security risks.
123+
*/
124+
ObjectAddressSet(address, NamespaceRelationId, namespaceId);
125+
checkMembershipInCurrentExtension(&address);
126+
127+
/* OK to skip */
128+
ereport(NOTICE,
129+
(errcode(ERRCODE_DUPLICATE_SCHEMA),
130+
errmsg("schema \"%s\" already exists, skipping",
131+
schemaName)));
132+
return InvalidOid;
133+
}
123134
}
124135

125136
/*

src/backend/commands/sequence.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
149149
RangeVarGetAndCheckCreationNamespace(seq->sequence, NoLock, &seqoid);
150150
if (OidIsValid(seqoid))
151151
{
152+
/*
153+
* If we are in an extension script, insist that the pre-existing
154+
* object be a member of the extension, to avoid security risks.
155+
*/
156+
ObjectAddressSet(address, RelationRelationId, seqoid);
157+
checkMembershipInCurrentExtension(&address);
158+
159+
/* OK to skip */
152160
ereport(NOTICE,
153161
(errcode(ERRCODE_DUPLICATE_TABLE),
154162
errmsg("relation \"%s\" already exists, skipping",

src/backend/commands/statscmds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ CreateStatistics(CreateStatsStmt *stmt)
176176
{
177177
if (stmt->if_not_exists)
178178
{
179+
/*
180+
* Since stats objects aren't members of extensions (see comments
181+
* below), no need for checkMembershipInCurrentExtension here.
182+
*/
179183
ereport(NOTICE,
180184
(errcode(ERRCODE_DUPLICATE_OBJECT),
181185
errmsg("statistics object \"%s\" already exists, skipping",

src/backend/commands/view.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace,
190190
CommandCounterIncrement();
191191

192192
/*
193-
* Finally update the view options.
193+
* Update the view's options.
194194
*
195195
* The new options list replaces the existing options list, even if
196196
* it's empty.
@@ -203,8 +203,22 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace,
203203
/* EventTriggerAlterTableStart called by ProcessUtilitySlow */
204204
AlterTableInternal(viewOid, atcmds, true);
205205

206+
/*
207+
* There is very little to do here to update the view's dependencies.
208+
* Most view-level dependency relationships, such as those on the
209+
* owner, schema, and associated composite type, aren't changing.
210+
* Because we don't allow changing type or collation of an existing
211+
* view column, those dependencies of the existing columns don't
212+
* change either, while the AT_AddColumnToView machinery took care of
213+
* adding such dependencies for new view columns. The dependencies of
214+
* the view's query could have changed arbitrarily, but that was dealt
215+
* with inside StoreViewQuery. What remains is only to check that
216+
* view replacement is allowed when we're creating an extension.
217+
*/
206218
ObjectAddressSet(address, RelationRelationId, viewOid);
207219

220+
recordDependencyOnCurrentExtension(&address, true);
221+
208222
/*
209223
* Seems okay, so return the OID of the pre-existing view.
210224
*/

0 commit comments

Comments
 (0)