aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/typecmds.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2020-08-10 10:44:42 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2020-08-10 10:44:42 -0400
commit98ca64899cec6a4bf3099481aff43b8777319c41 (patch)
treef91b083b0a82c2ff27a0dd139b65e9aea1169679 /src/backend/commands/typecmds.c
parent378bd1ed6e4314a8b8b32c555f73524c2283b016 (diff)
downloadpostgresql-98ca64899cec6a4bf3099481aff43b8777319c41.tar.gz
postgresql-98ca64899cec6a4bf3099481aff43b8777319c41.zip
Make contrib modules' installation scripts more secure.
Hostile objects located within the installation-time search_path could capture references in an extension's installation or upgrade script. If the extension is being installed with superuser privileges, this opens the door to privilege escalation. While such hazards have existed all along, their urgency increases with the v13 "trusted extensions" feature, because that lets a non-superuser control the installation path for a superuser-privileged script. Therefore, make a number of changes to make such situations more secure: * Tweak the construction of the installation-time search_path to ensure that references to objects in pg_catalog can't be subverted; and explicitly add pg_temp to the end of the path to prevent attacks using temporary objects. * Disable check_function_bodies within installation/upgrade scripts, so that any security gaps in SQL-language or PL-language function bodies cannot create a risk of unwanted installation-time code execution. * Adjust lookup of type input/receive functions and join estimator functions to complain if there are multiple candidate functions. This prevents capture of references to functions whose signature is not the first one checked; and it's arguably more user-friendly anyway. * Modify various contrib upgrade scripts to ensure that catalog modification queries are executed with secure search paths. (These are in-place modifications with no extension version changes, since it is the update process itself that is at issue, not the end result.) Extensions that depend on other extensions cannot be made fully secure by these methods alone; therefore, revert the "trusted" marking that commit eb67623c9 applied to earthdistance and hstore_plperl, pending some better solution to that set of issues. Also add documentation around these issues, to help extension authors write secure installation scripts. Patch by me, following an observation by Andres Freund; thanks to Noah Misch for review. Security: CVE-2020-14350
Diffstat (limited to 'src/backend/commands/typecmds.c')
-rw-r--r--src/backend/commands/typecmds.c50
1 files changed, 36 insertions, 14 deletions
diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c
index 2e107ace39b..483bb65ddc8 100644
--- a/src/backend/commands/typecmds.c
+++ b/src/backend/commands/typecmds.c
@@ -1627,21 +1627,31 @@ findTypeInputFunction(List *procname, Oid typeOid)
{
Oid argList[3];
Oid procOid;
+ Oid procOid2;
/*
* Input functions can take a single argument of type CSTRING, or three
- * arguments (string, typioparam OID, typmod). They must return the
- * target type.
+ * arguments (string, typioparam OID, typmod). Whine about ambiguity if
+ * both forms exist.
*/
argList[0] = CSTRINGOID;
+ argList[1] = OIDOID;
+ argList[2] = INT4OID;
procOid = LookupFuncName(procname, 1, argList, true);
- if (!OidIsValid(procOid))
+ procOid2 = LookupFuncName(procname, 3, argList, true);
+ if (OidIsValid(procOid))
{
- argList[1] = OIDOID;
- argList[2] = INT4OID;
-
- procOid = LookupFuncName(procname, 3, argList, true);
+ if (OidIsValid(procOid2))
+ ereport(ERROR,
+ (errcode(ERRCODE_AMBIGUOUS_FUNCTION),
+ errmsg("type input function %s has multiple matches",
+ NameListToString(procname))));
+ }
+ else
+ {
+ procOid = procOid2;
+ /* If not found, reference the 1-argument signature in error msg */
if (!OidIsValid(procOid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_FUNCTION),
@@ -1649,6 +1659,7 @@ findTypeInputFunction(List *procname, Oid typeOid)
func_signature_string(procname, 1, NIL, argList))));
}
+ /* Input functions must return the target type. */
if (get_func_rettype(procOid) != typeOid)
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
@@ -1714,21 +1725,31 @@ findTypeReceiveFunction(List *procname, Oid typeOid)
{
Oid argList[3];
Oid procOid;
+ Oid procOid2;
/*
* Receive functions can take a single argument of type INTERNAL, or three
- * arguments (internal, typioparam OID, typmod). They must return the
- * target type.
+ * arguments (internal, typioparam OID, typmod). Whine about ambiguity if
+ * both forms exist.
*/
argList[0] = INTERNALOID;
+ argList[1] = OIDOID;
+ argList[2] = INT4OID;
procOid = LookupFuncName(procname, 1, argList, true);
- if (!OidIsValid(procOid))
+ procOid2 = LookupFuncName(procname, 3, argList, true);
+ if (OidIsValid(procOid))
{
- argList[1] = OIDOID;
- argList[2] = INT4OID;
-
- procOid = LookupFuncName(procname, 3, argList, true);
+ if (OidIsValid(procOid2))
+ ereport(ERROR,
+ (errcode(ERRCODE_AMBIGUOUS_FUNCTION),
+ errmsg("type receive function %s has multiple matches",
+ NameListToString(procname))));
+ }
+ else
+ {
+ procOid = procOid2;
+ /* If not found, reference the 1-argument signature in error msg */
if (!OidIsValid(procOid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_FUNCTION),
@@ -1736,6 +1757,7 @@ findTypeReceiveFunction(List *procname, Oid typeOid)
func_signature_string(procname, 1, NIL, argList))));
}
+ /* Receive functions must return the target type. */
if (get_func_rettype(procOid) != typeOid)
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),