]> granicus.if.org Git - postgresql/commitdiff
Require the schema qualification in pg_temp.type_name(arg).
authorNoah Misch <noah@leadboat.com>
Mon, 5 Aug 2019 14:48:41 +0000 (07:48 -0700)
committerNoah Misch <noah@leadboat.com>
Mon, 5 Aug 2019 14:48:45 +0000 (07:48 -0700)
Commit aa27977fe21a7dfa4da4376ad66ae37cb8f0d0b5 introduced this
restriction for pg_temp.function_name(arg); do likewise for types
created in temporary schemas.  Programs that this breaks should add
"pg_temp." schema qualification or switch to arg::type_name syntax.
Back-patch to 9.4 (all supported versions).

Reviewed by Tom Lane.  Reported by Tom Lane.

Security: CVE-2019-10208

doc/src/sgml/config.sgml
src/backend/catalog/namespace.c
src/backend/parser/parse_func.c
src/backend/parser/parse_type.c
src/backend/utils/adt/ruleutils.c
src/include/catalog/namespace.h
src/include/parser/parse_type.h
src/test/regress/expected/temp.out
src/test/regress/sql/temp.sql

index 66b331bce2b6b8044d28b27abe5475251785b97b..9aa1e83ecfc96fb38788c740814247fe978347b3 100644 (file)
@@ -5524,6 +5524,10 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
         be searched <emphasis>before</> searching any of the path items.
        </para>
 
+       <!-- To further split hairs, funcname('foo') does not use the temporary
+            schema, even when it considers typname='funcname'.  This paragraph
+            refers to function names in a loose sense, "pg_proc.proname or
+            func_name grammar production". -->
        <para>
         Likewise, the current session's temporary-table schema,
         <literal>pg_temp_<replaceable>nnn</></>, is always searched if it
index ac30f77f024de29f77dac11ec7016378b83b79f3..53ba8a3141a9176d45dd443d907501a8a8a224f2 100644 (file)
@@ -753,13 +753,23 @@ RelationIsVisible(Oid relid)
 
 /*
  * TypenameGetTypid
+ *             Wrapper for binary compatibility.
+ */
+Oid
+TypenameGetTypid(const char *typname)
+{
+       return TypenameGetTypidExtended(typname, true);
+}
+
+/*
+ * TypenameGetTypidExtended
  *             Try to resolve an unqualified datatype name.
  *             Returns OID if type found in search path, else InvalidOid.
  *
  * This is essentially the same as RelnameGetRelid.
  */
 Oid
-TypenameGetTypid(const char *typname)
+TypenameGetTypidExtended(const char *typname, bool temp_ok)
 {
        Oid                     typid;
        ListCell   *l;
@@ -770,6 +780,9 @@ TypenameGetTypid(const char *typname)
        {
                Oid                     namespaceId = lfirst_oid(l);
 
+               if (!temp_ok && namespaceId == myTempNamespace)
+                       continue;                       /* do not look in temp namespace */
+
                typid = GetSysCacheOid2(TYPENAMENSP,
                                                                PointerGetDatum(typname),
                                                                ObjectIdGetDatum(namespaceId));
index 554ca9d8c47e5f38eddb5579dd70160a2d5d363b..044addd250166a64559d1f820619a470b2a2ca8e 100644 (file)
@@ -1743,7 +1743,12 @@ FuncNameAsType(List *funcname)
        Oid                     result;
        Type            typtup;
 
-       typtup = LookupTypeName(NULL, makeTypeNameFromNameList(funcname), NULL, false);
+       /*
+        * temp_ok=false protects the <refsect1 id="sql-createfunction-security">
+        * contract for writing SECURITY DEFINER functions safely.
+        */
+       typtup = LookupTypeNameExtended(NULL, makeTypeNameFromNameList(funcname),
+                                                                       NULL, false, false);
        if (typtup == NULL)
                return InvalidOid;
 
index 9db3553d80a54a58535474f4923c2fea436c3b88..640cb1e153eaacedddeb8965023f2fd750f0d3f2 100644 (file)
@@ -33,6 +33,18 @@ static int32 typenameTypeMod(ParseState *pstate, const TypeName *typeName,
 
 /*
  * LookupTypeName
+ *             Wrapper for typical case.
+ */
+Type
+LookupTypeName(ParseState *pstate, const TypeName *typeName,
+                          int32 *typmod_p, bool missing_ok)
+{
+       return LookupTypeNameExtended(pstate,
+                                                                 typeName, typmod_p, true, missing_ok);
+}
+
+/*
+ * LookupTypeNameExtended
  *             Given a TypeName object, lookup the pg_type syscache entry of the type.
  *             Returns NULL if no such type can be found.  If the type is found,
  *             the typmod value represented in the TypeName struct is computed and
@@ -51,11 +63,17 @@ static int32 typenameTypeMod(ParseState *pstate, const TypeName *typeName,
  * found but is a shell, and there is typmod decoration, an error will be
  * thrown --- this is intentional.
  *
+ * If temp_ok is false, ignore types in the temporary namespace.  Pass false
+ * when the caller will decide, using goodness of fit criteria, whether the
+ * typeName is actually a type or something else.  If typeName always denotes
+ * a type (or denotes nothing), pass true.
+ *
  * pstate is only used for error location info, and may be NULL.
  */
 Type
-LookupTypeName(ParseState *pstate, const TypeName *typeName,
-                          int32 *typmod_p, bool missing_ok)
+LookupTypeNameExtended(ParseState *pstate,
+                                          const TypeName *typeName, int32 *typmod_p,
+                                          bool temp_ok, bool missing_ok)
 {
        Oid                     typoid;
        HeapTuple       tup;
@@ -172,7 +190,7 @@ LookupTypeName(ParseState *pstate, const TypeName *typeName,
                else
                {
                        /* Unqualified type name, so search the search path */
-                       typoid = TypenameGetTypid(typname);
+                       typoid = TypenameGetTypidExtended(typname, temp_ok);
                }
 
                /* If an array reference, return the array type instead */
index bef01cde86e87d0f39cd5b779c947b614fa2ab81..9f7eb2ba9f256f3376caffc8b3c5d041ef33f2e4 100644 (file)
@@ -8583,6 +8583,14 @@ get_coercion_expr(Node *arg, deparse_context *context,
                if (!PRETTY_PAREN(context))
                        appendStringInfoChar(buf, ')');
        }
+
+       /*
+        * Never emit resulttype(arg) functional notation. A pg_proc entry could
+        * take precedence, and a resulttype in pg_temp would require schema
+        * qualification that format_type_with_typemod() would usually omit. We've
+        * standardized on arg::resulttype, but CAST(arg AS resulttype) notation
+        * would work fine.
+        */
        appendStringInfo(buf, "::%s",
                                         format_type_with_typemod(resulttype, resulttypmod));
 }
index f3b005fa9d80983551658168bc51230c1f439b8e..e2a11c65aa01eebb40484c092b5305e3c1de5027 100644 (file)
@@ -66,6 +66,7 @@ extern Oid    RelnameGetRelid(const char *relname);
 extern bool RelationIsVisible(Oid relid);
 
 extern Oid     TypenameGetTypid(const char *typname);
+extern Oid     TypenameGetTypidExtended(const char *typname, bool temp_ok);
 extern bool TypeIsVisible(Oid typid);
 
 extern FuncCandidateList FuncnameGetCandidates(List *names,
index f95bfe78eab73838c199ce66ba67294f691d2bf6..6f1a7b993e957aeb4acb42d488eb41138d2f5a30 100644 (file)
@@ -21,6 +21,9 @@ typedef HeapTuple Type;
 
 extern Type LookupTypeName(ParseState *pstate, const TypeName *typeName,
                           int32 *typmod_p, bool missing_ok);
+extern Type LookupTypeNameExtended(ParseState *pstate,
+                                                                  const TypeName *typeName, int32 *typmod_p,
+                                                                  bool temp_ok, bool missing_ok);
 extern Oid LookupTypeNameOid(ParseState *pstate, const TypeName *typeName,
                                  bool missing_ok);
 extern Type typenameType(ParseState *pstate, const TypeName *typeName,
index addf1ec444397cbeae37d2ac1838503646d488d4..0df7ba3ae1a3c0baa95d3d71254aaf2e9152a562 100644 (file)
@@ -199,3 +199,18 @@ select pg_temp.whoami();
 (1 row)
 
 drop table public.whereami;
+-- types in temp schema
+set search_path = pg_temp, public;
+create domain pg_temp.nonempty as text check (value <> '');
+-- function-syntax invocation of types matches rules for functions
+select nonempty('');
+ERROR:  function nonempty(unknown) does not exist
+LINE 1: select nonempty('');
+               ^
+HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
+select pg_temp.nonempty('');
+ERROR:  value for domain nonempty violates check constraint "nonempty_check"
+-- other syntax matches rules for tables
+select ''::nonempty;
+ERROR:  value for domain nonempty violates check constraint "nonempty_check"
+reset search_path;
index 5183c727f5e70139d71ab345ce548f8d175baf06..6936b999295e8c6a928fcbf85cd9281788fe3283 100644 (file)
@@ -151,3 +151,14 @@ select whoami();
 select pg_temp.whoami();
 
 drop table public.whereami;
+
+-- types in temp schema
+set search_path = pg_temp, public;
+create domain pg_temp.nonempty as text check (value <> '');
+-- function-syntax invocation of types matches rules for functions
+select nonempty('');
+select pg_temp.nonempty('');
+-- other syntax matches rules for tables
+select ''::nonempty;
+
+reset search_path;