mirror of
https://github.com/postgres/postgres.git
synced 2025-11-24 00:23:06 +03:00
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
This commit is contained in:
@@ -170,22 +170,23 @@ recordMultipleDependencies(const ObjectAddress *depender,
|
||||
|
||||
/*
|
||||
* If we are executing a CREATE EXTENSION operation, mark the given object
|
||||
* as being a member of the extension. Otherwise, do nothing.
|
||||
* as being a member of the extension, or check that it already is one.
|
||||
* Otherwise, do nothing.
|
||||
*
|
||||
* This must be called during creation of any user-definable object type
|
||||
* that could be a member of an extension.
|
||||
*
|
||||
* If isReplace is true, the object already existed (or might have already
|
||||
* existed), so we must check for a pre-existing extension membership entry.
|
||||
* Passing false is a guarantee that the object is newly created, and so
|
||||
* could not already be a member of any extension.
|
||||
* isReplace must be true if the object already existed, and false if it is
|
||||
* newly created. In the former case we insist that it already be a member
|
||||
* of the current extension. In the latter case we can skip checking whether
|
||||
* it is already a member of any extension.
|
||||
*
|
||||
* Note: isReplace = true is typically used when updating an object in
|
||||
* CREATE OR REPLACE and similar commands. The net effect is that if an
|
||||
* extension script uses such a command on a pre-existing free-standing
|
||||
* object, the object will be absorbed into the extension. If the object
|
||||
* is already a member of some other extension, the command will fail.
|
||||
* This behavior is desirable for cases such as replacing a shell type.
|
||||
* CREATE OR REPLACE and similar commands. We used to allow the target
|
||||
* object to not already be an extension member, instead silently absorbing
|
||||
* it into the current extension. However, this was both error-prone
|
||||
* (extensions might accidentally overwrite free-standing objects) and
|
||||
* a security hazard (since the object would retain its previous ownership).
|
||||
*/
|
||||
void
|
||||
recordDependencyOnCurrentExtension(const ObjectAddress *object,
|
||||
@@ -203,6 +204,12 @@ recordDependencyOnCurrentExtension(const ObjectAddress *object,
|
||||
{
|
||||
Oid oldext;
|
||||
|
||||
/*
|
||||
* Side note: these catalog lookups are safe only because the
|
||||
* object is a pre-existing one. In the not-isReplace case, the
|
||||
* caller has most likely not yet done a CommandCounterIncrement
|
||||
* that would make the new object visible.
|
||||
*/
|
||||
oldext = getExtensionOfObject(object->classId, object->objectId);
|
||||
if (OidIsValid(oldext))
|
||||
{
|
||||
@@ -216,6 +223,13 @@ recordDependencyOnCurrentExtension(const ObjectAddress *object,
|
||||
getObjectDescription(object, false),
|
||||
get_extension_name(oldext))));
|
||||
}
|
||||
/* It's a free-standing object, so reject */
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
||||
errmsg("%s is not a member of extension \"%s\"",
|
||||
getObjectDescription(object, false),
|
||||
get_extension_name(CurrentExtensionObject)),
|
||||
errdetail("An extension is not allowed to replace an object that it does not own.")));
|
||||
}
|
||||
|
||||
/* OK, record it as a member of CurrentExtensionObject */
|
||||
@@ -227,6 +241,49 @@ recordDependencyOnCurrentExtension(const ObjectAddress *object,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* If we are executing a CREATE EXTENSION operation, check that the given
|
||||
* object is a member of the extension, and throw an error if it isn't.
|
||||
* Otherwise, do nothing.
|
||||
*
|
||||
* This must be called whenever a CREATE IF NOT EXISTS operation (for an
|
||||
* object type that can be an extension member) has found that an object of
|
||||
* the desired name already exists. It is insecure for an extension to use
|
||||
* IF NOT EXISTS except when the conflicting object is already an extension
|
||||
* member; otherwise a hostile user could substitute an object with arbitrary
|
||||
* properties.
|
||||
*/
|
||||
void
|
||||
checkMembershipInCurrentExtension(const ObjectAddress *object)
|
||||
{
|
||||
/*
|
||||
* This is actually the same condition tested in
|
||||
* recordDependencyOnCurrentExtension; but we want to issue a
|
||||
* differently-worded error, and anyway it would be pretty confusing to
|
||||
* call recordDependencyOnCurrentExtension in these circumstances.
|
||||
*/
|
||||
|
||||
/* Only whole objects can be extension members */
|
||||
Assert(object->objectSubId == 0);
|
||||
|
||||
if (creating_extension)
|
||||
{
|
||||
Oid oldext;
|
||||
|
||||
oldext = getExtensionOfObject(object->classId, object->objectId);
|
||||
/* If already a member of this extension, OK */
|
||||
if (oldext == CurrentExtensionObject)
|
||||
return;
|
||||
/* Else complain */
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
||||
errmsg("%s is not a member of extension \"%s\"",
|
||||
getObjectDescription(object, false),
|
||||
get_extension_name(CurrentExtensionObject)),
|
||||
errdetail("An extension may only use CREATE ... IF NOT EXISTS to skip object creation if the conflicting object is one that it already owns.")));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* deleteDependencyRecordsFor -- delete all records with given depender
|
||||
* classId/objectId. Returns the number of records deleted.
|
||||
|
||||
Reference in New Issue
Block a user