diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c index 88557aa27d3..ac3edd0bd46 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -198,7 +198,7 @@ static SubTransactionId myTempNamespaceSubID = InvalidSubTransactionId; * of the GUC variable 'search_path'. */ char *namespace_search_path = NULL; - +bool prohibit_superuser_overrides; /* Local functions */ static void recomputeNamespacePath(void); @@ -960,6 +960,7 @@ FuncnameGetCandidates(List *names, int nargs, List *argnames, Oid namespaceId; CatCList *catlist; int i; + bool has_superuser_candidate = false; /* check for caller error */ Assert(nargs >= 0 || !(expand_variadic | expand_defaults)); @@ -1023,6 +1024,22 @@ FuncnameGetCandidates(List *names, int nargs, List *argnames, continue; /* proc is not in search path */ } + /* prohibit overrides under superuser */ + if (prohibit_superuser_overrides && superuser()) + { + bool owned_by_superuser = superuser_arg(procform->proowner); + + /* If we have superuser condidate, then ignore all non-supoeruser alternatives */ + if (resultList && has_superuser_candidate && !owned_by_superuser) + continue; + + /* If new candidate is owned by superuser then forget all non-superuser candidates */ + if (owned_by_superuser && !has_superuser_candidate) + resultList = NULL; + + has_superuser_candidate = owned_by_superuser; + } + /* * If we are asked to match to OUT arguments, then use the * proallargtypes array (which includes those); otherwise use @@ -3919,6 +3936,10 @@ recomputeNamespacePath(void) !list_member_oid(oidlist, myTempNamespace)) oidlist = lcons_oid(myTempNamespace, oidlist); + /* Always place pg_catalog at the beginning of search path */ + if (prohibit_superuser_overrides && superuser()) + oidlist = lcons_oid(PG_CATALOG_NAMESPACE, oidlist); + /* * We want to detect the case where the effective value of the base search * path variables didn't change. As long as we're doing so, we can avoid diff --git a/src/include/catalog/namespace.h b/src/include/catalog/namespace.h index 1bc55c01a5c..b821cb106a5 100644 --- a/src/include/catalog/namespace.h +++ b/src/include/catalog/namespace.h @@ -183,6 +183,7 @@ extern void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, /* stuff for search_path GUC variable */ extern PGDLLIMPORT char *namespace_search_path; +extern bool prohibit_superuser_overrides; extern List *fetch_search_path(bool includeImplicit); extern int fetch_search_path_array(Oid *sarray, int sarray_len);