Commit f73bed30 authored by Tom Lane's avatar Tom Lane

Repair a longstanding bug in CLUSTER and the rewriting variants of ALTER

TABLE: if the command is executed by someone other than the table owner (eg,
a superuser) and the table has a toast table, the toast table's pg_type row
ends up with the wrong typowner, ie, the command issuer not the table owner.
This is quite harmless for most purposes, since no interesting permissions
checks consult the pg_type row.  However, it could lead to unexpected failures
if one later tries to drop the role that issued the command (in 8.1 or 8.2),
or strange warnings from pg_dump afterwards (in 8.3 and up, which will allow
the DROP ROLE because we don't create a "redundant" owner dependency for table
rowtypes).  Problem identified by Cott Lang.

Back-patch to 8.1.  The problem is actually far older --- the CLUSTER variant
can be demonstrated in 7.0 --- but it's mostly cosmetic before 8.1 because we
didn't track ownership dependencies before 8.1.  Also, fixing it before 8.1
would require changing the call signature of heap_create_with_catalog(), which
seems to carry a nontrivial risk of breaking add-on modules.
parent 7de7876c
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/catalog/heap.c,v 1.350 2009/01/22 20:16:01 tgl Exp $ * $PostgreSQL: pgsql/src/backend/catalog/heap.c,v 1.351 2009/02/24 01:38:09 tgl Exp $
* *
* *
* INTERFACE ROUTINES * INTERFACE ROUTINES
...@@ -79,6 +79,7 @@ static Oid AddNewRelationType(const char *typeName, ...@@ -79,6 +79,7 @@ static Oid AddNewRelationType(const char *typeName,
Oid typeNamespace, Oid typeNamespace,
Oid new_rel_oid, Oid new_rel_oid,
char new_rel_kind, char new_rel_kind,
Oid ownerid,
Oid new_array_type); Oid new_array_type);
static void RelationRemoveInheritance(Oid relid); static void RelationRemoveInheritance(Oid relid);
static void StoreRelCheck(Relation rel, char *ccname, Node *expr, static void StoreRelCheck(Relation rel, char *ccname, Node *expr,
...@@ -784,6 +785,7 @@ AddNewRelationType(const char *typeName, ...@@ -784,6 +785,7 @@ AddNewRelationType(const char *typeName,
Oid typeNamespace, Oid typeNamespace,
Oid new_rel_oid, Oid new_rel_oid,
char new_rel_kind, char new_rel_kind,
Oid ownerid,
Oid new_array_type) Oid new_array_type)
{ {
return return
...@@ -792,6 +794,7 @@ AddNewRelationType(const char *typeName, ...@@ -792,6 +794,7 @@ AddNewRelationType(const char *typeName,
typeNamespace, /* type namespace */ typeNamespace, /* type namespace */
new_rel_oid, /* relation oid */ new_rel_oid, /* relation oid */
new_rel_kind, /* relation kind */ new_rel_kind, /* relation kind */
ownerid, /* owner's ID */
-1, /* internal size (varlena) */ -1, /* internal size (varlena) */
TYPTYPE_COMPOSITE, /* type-type (composite) */ TYPTYPE_COMPOSITE, /* type-type (composite) */
TYPCATEGORY_COMPOSITE, /* type-category (ditto) */ TYPCATEGORY_COMPOSITE, /* type-category (ditto) */
...@@ -955,6 +958,7 @@ heap_create_with_catalog(const char *relname, ...@@ -955,6 +958,7 @@ heap_create_with_catalog(const char *relname,
relnamespace, relnamespace,
relid, relid,
relkind, relkind,
ownerid,
new_array_oid); new_array_oid);
/* /*
...@@ -971,6 +975,7 @@ heap_create_with_catalog(const char *relname, ...@@ -971,6 +975,7 @@ heap_create_with_catalog(const char *relname,
relnamespace, /* Same namespace as parent */ relnamespace, /* Same namespace as parent */
InvalidOid, /* Not composite, no relationOid */ InvalidOid, /* Not composite, no relationOid */
0, /* relkind, also N/A here */ 0, /* relkind, also N/A here */
ownerid, /* owner's ID */
-1, /* Internal size (varlena) */ -1, /* Internal size (varlena) */
TYPTYPE_BASE, /* Not composite - typelem is */ TYPTYPE_BASE, /* Not composite - typelem is */
TYPCATEGORY_ARRAY, /* type-category (array) */ TYPCATEGORY_ARRAY, /* type-category (array) */
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/catalog/pg_type.c,v 1.124 2009/01/22 20:16:01 tgl Exp $ * $PostgreSQL: pgsql/src/backend/catalog/pg_type.c,v 1.125 2009/02/24 01:38:09 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
* ---------------------------------------------------------------- * ----------------------------------------------------------------
*/ */
Oid Oid
TypeShellMake(const char *typeName, Oid typeNamespace) TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
{ {
Relation pg_type_desc; Relation pg_type_desc;
TupleDesc tupDesc; TupleDesc tupDesc;
...@@ -87,7 +87,7 @@ TypeShellMake(const char *typeName, Oid typeNamespace) ...@@ -87,7 +87,7 @@ TypeShellMake(const char *typeName, Oid typeNamespace)
namestrcpy(&name, typeName); namestrcpy(&name, typeName);
values[i++] = NameGetDatum(&name); /* typname */ values[i++] = NameGetDatum(&name); /* typname */
values[i++] = ObjectIdGetDatum(typeNamespace); /* typnamespace */ values[i++] = ObjectIdGetDatum(typeNamespace); /* typnamespace */
values[i++] = ObjectIdGetDatum(GetUserId()); /* typowner */ values[i++] = ObjectIdGetDatum(ownerId); /* typowner */
values[i++] = Int16GetDatum(sizeof(int4)); /* typlen */ values[i++] = Int16GetDatum(sizeof(int4)); /* typlen */
values[i++] = BoolGetDatum(true); /* typbyval */ values[i++] = BoolGetDatum(true); /* typbyval */
values[i++] = CharGetDatum(TYPTYPE_PSEUDO); /* typtype */ values[i++] = CharGetDatum(TYPTYPE_PSEUDO); /* typtype */
...@@ -134,7 +134,7 @@ TypeShellMake(const char *typeName, Oid typeNamespace) ...@@ -134,7 +134,7 @@ TypeShellMake(const char *typeName, Oid typeNamespace)
typoid, typoid,
InvalidOid, InvalidOid,
0, 0,
GetUserId(), ownerId,
F_SHELL_IN, F_SHELL_IN,
F_SHELL_OUT, F_SHELL_OUT,
InvalidOid, InvalidOid,
...@@ -173,6 +173,7 @@ TypeCreate(Oid newTypeOid, ...@@ -173,6 +173,7 @@ TypeCreate(Oid newTypeOid,
Oid typeNamespace, Oid typeNamespace,
Oid relationOid, /* only for relation rowtypes */ Oid relationOid, /* only for relation rowtypes */
char relationKind, /* ditto */ char relationKind, /* ditto */
Oid ownerId,
int16 internalSize, int16 internalSize,
char typeType, char typeType,
char typeCategory, char typeCategory,
...@@ -310,7 +311,7 @@ TypeCreate(Oid newTypeOid, ...@@ -310,7 +311,7 @@ TypeCreate(Oid newTypeOid,
namestrcpy(&name, typeName); namestrcpy(&name, typeName);
values[i++] = NameGetDatum(&name); /* typname */ values[i++] = NameGetDatum(&name); /* typname */
values[i++] = ObjectIdGetDatum(typeNamespace); /* typnamespace */ values[i++] = ObjectIdGetDatum(typeNamespace); /* typnamespace */
values[i++] = ObjectIdGetDatum(GetUserId()); /* typowner */ values[i++] = ObjectIdGetDatum(ownerId); /* typowner */
values[i++] = Int16GetDatum(internalSize); /* typlen */ values[i++] = Int16GetDatum(internalSize); /* typlen */
values[i++] = BoolGetDatum(passedByValue); /* typbyval */ values[i++] = BoolGetDatum(passedByValue); /* typbyval */
values[i++] = CharGetDatum(typeType); /* typtype */ values[i++] = CharGetDatum(typeType); /* typtype */
...@@ -380,7 +381,7 @@ TypeCreate(Oid newTypeOid, ...@@ -380,7 +381,7 @@ TypeCreate(Oid newTypeOid,
/* /*
* shell type must have been created by same owner * shell type must have been created by same owner
*/ */
if (((Form_pg_type) GETSTRUCT(tup))->typowner != GetUserId()) if (((Form_pg_type) GETSTRUCT(tup))->typowner != ownerId)
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE, typeName); aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE, typeName);
/* trouble if caller wanted to force the OID */ /* trouble if caller wanted to force the OID */
...@@ -426,7 +427,7 @@ TypeCreate(Oid newTypeOid, ...@@ -426,7 +427,7 @@ TypeCreate(Oid newTypeOid,
typeObjectId, typeObjectId,
relationOid, relationOid,
relationKind, relationKind,
GetUserId(), ownerId,
inputProcedure, inputProcedure,
outputProcedure, outputProcedure,
receiveProcedure, receiveProcedure,
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.107 2009/01/06 02:01:27 tgl Exp $ * $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.108 2009/02/24 01:38:09 tgl Exp $
* *
* DESCRIPTION * DESCRIPTION
* These routines take the parse tree and pick out the * These routines take the parse tree and pick out the
...@@ -144,7 +144,7 @@ compute_return_type(TypeName *returnType, Oid languageOid, ...@@ -144,7 +144,7 @@ compute_return_type(TypeName *returnType, Oid languageOid,
if (aclresult != ACLCHECK_OK) if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, ACL_KIND_NAMESPACE, aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
get_namespace_name(namespaceId)); get_namespace_name(namespaceId));
rettype = TypeShellMake(typname, namespaceId); rettype = TypeShellMake(typname, namespaceId, GetUserId());
Assert(OidIsValid(rettype)); Assert(OidIsValid(rettype));
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.131 2009/02/02 19:31:39 alvherre Exp $ * $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.132 2009/02/24 01:38:09 tgl Exp $
* *
* DESCRIPTION * DESCRIPTION
* The "DefineFoo" routines take the parse tree and pick out the * The "DefineFoo" routines take the parse tree and pick out the
...@@ -199,7 +199,7 @@ DefineType(List *names, List *parameters) ...@@ -199,7 +199,7 @@ DefineType(List *names, List *parameters)
*/ */
if (!OidIsValid(typoid)) if (!OidIsValid(typoid))
{ {
typoid = TypeShellMake(typeName, typeNamespace); typoid = TypeShellMake(typeName, typeNamespace, GetUserId());
/* Make new shell type visible for modification below */ /* Make new shell type visible for modification below */
CommandCounterIncrement(); CommandCounterIncrement();
...@@ -536,6 +536,7 @@ DefineType(List *names, List *parameters) ...@@ -536,6 +536,7 @@ DefineType(List *names, List *parameters)
typeNamespace, /* namespace */ typeNamespace, /* namespace */
InvalidOid, /* relation oid (n/a here) */ InvalidOid, /* relation oid (n/a here) */
0, /* relation kind (ditto) */ 0, /* relation kind (ditto) */
GetUserId(), /* owner's ID */
internalLength, /* internal size */ internalLength, /* internal size */
TYPTYPE_BASE, /* type-type (base type) */ TYPTYPE_BASE, /* type-type (base type) */
category, /* type-category */ category, /* type-category */
...@@ -574,6 +575,7 @@ DefineType(List *names, List *parameters) ...@@ -574,6 +575,7 @@ DefineType(List *names, List *parameters)
typeNamespace, /* namespace */ typeNamespace, /* namespace */
InvalidOid, /* relation oid (n/a here) */ InvalidOid, /* relation oid (n/a here) */
0, /* relation kind (ditto) */ 0, /* relation kind (ditto) */
GetUserId(), /* owner's ID */
-1, /* internal size (always varlena) */ -1, /* internal size (always varlena) */
TYPTYPE_BASE, /* type-type (base type) */ TYPTYPE_BASE, /* type-type (base type) */
TYPCATEGORY_ARRAY, /* type-category (array) */ TYPCATEGORY_ARRAY, /* type-category (array) */
...@@ -1018,6 +1020,7 @@ DefineDomain(CreateDomainStmt *stmt) ...@@ -1018,6 +1020,7 @@ DefineDomain(CreateDomainStmt *stmt)
domainNamespace, /* namespace */ domainNamespace, /* namespace */
InvalidOid, /* relation oid (n/a here) */ InvalidOid, /* relation oid (n/a here) */
0, /* relation kind (ditto) */ 0, /* relation kind (ditto) */
GetUserId(), /* owner's ID */
internalLength, /* internal size */ internalLength, /* internal size */
TYPTYPE_DOMAIN, /* type-type (domain type) */ TYPTYPE_DOMAIN, /* type-type (domain type) */
category, /* type-category */ category, /* type-category */
...@@ -1131,6 +1134,7 @@ DefineEnum(CreateEnumStmt *stmt) ...@@ -1131,6 +1134,7 @@ DefineEnum(CreateEnumStmt *stmt)
enumNamespace, /* namespace */ enumNamespace, /* namespace */
InvalidOid, /* relation oid (n/a here) */ InvalidOid, /* relation oid (n/a here) */
0, /* relation kind (ditto) */ 0, /* relation kind (ditto) */
GetUserId(), /* owner's ID */
sizeof(Oid), /* internal size */ sizeof(Oid), /* internal size */
TYPTYPE_ENUM, /* type-type (enum type) */ TYPTYPE_ENUM, /* type-type (enum type) */
TYPCATEGORY_ENUM, /* type-category (enum type) */ TYPCATEGORY_ENUM, /* type-category (enum type) */
...@@ -1169,6 +1173,7 @@ DefineEnum(CreateEnumStmt *stmt) ...@@ -1169,6 +1173,7 @@ DefineEnum(CreateEnumStmt *stmt)
enumNamespace, /* namespace */ enumNamespace, /* namespace */
InvalidOid, /* relation oid (n/a here) */ InvalidOid, /* relation oid (n/a here) */
0, /* relation kind (ditto) */ 0, /* relation kind (ditto) */
GetUserId(), /* owner's ID */
-1, /* internal size (always varlena) */ -1, /* internal size (always varlena) */
TYPTYPE_BASE, /* type-type (base type) */ TYPTYPE_BASE, /* type-type (base type) */
TYPCATEGORY_ARRAY, /* type-category (array) */ TYPCATEGORY_ARRAY, /* type-category (array) */
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/catalog/pg_type_fn.h,v 1.3 2009/01/01 17:23:58 momjian Exp $ * $PostgreSQL: pgsql/src/include/catalog/pg_type_fn.h,v 1.4 2009/02/24 01:38:10 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -17,13 +17,16 @@ ...@@ -17,13 +17,16 @@
#include "nodes/nodes.h" #include "nodes/nodes.h"
extern Oid TypeShellMake(const char *typeName, Oid typeNamespace); extern Oid TypeShellMake(const char *typeName,
Oid typeNamespace,
Oid ownerId);
extern Oid TypeCreate(Oid newTypeOid, extern Oid TypeCreate(Oid newTypeOid,
const char *typeName, const char *typeName,
Oid typeNamespace, Oid typeNamespace,
Oid relationOid, Oid relationOid,
char relationKind, char relationKind,
Oid ownerId,
int16 internalSize, int16 internalSize,
char typeType, char typeType,
char typeCategory, char typeCategory,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment