Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 59bd34c

Browse files
committed
Fix bugs in manipulation of large objects.
In v16 and up (since commit afbfc02), large object ownership checking has been broken because object_ownercheck() didn't take care of the discrepancy between our object-address representation of large objects (classId == LargeObjectRelationId) and the catalog where their ownership info is actually stored (LargeObjectMetadataRelationId). This resulted in failures such as "unrecognized class ID: 2613" when trying to update blob properties as a non-superuser. Poking around for related bugs, I found that AlterObjectOwner_internal would pass the wrong classId to the PostAlterHook in the no-op code path where the large object already has the desired owner. Also, recordExtObjInitPriv checked for the wrong classId; that bug is only latent because the stanza is dead code anyway, but as long as we're carrying it around it should be less wrong. These bugs are quite old. In HEAD, we can reduce the scope for future bugs of this ilk by changing AlterObjectOwner_internal's API to let the translation happen inside that function, rather than requiring callers to know about it. A more bulletproof fix, perhaps, would be to start using LargeObjectMetadataRelationId as the dependency and object-address classId for blobs. However that has substantial risk of breaking third-party code; even within our own code, it'd create hassles for pg_dump which would have to cope with a version-dependent representation. For now, keep the status quo. Discussion: https://postgr.es/m/2650449.1702497209@sss.pgh.pa.us
1 parent 0e91750 commit 59bd34c

File tree

9 files changed

+54
-55
lines changed

9 files changed

+54
-55
lines changed

src/backend/catalog/aclchk.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -4103,9 +4103,14 @@ object_ownercheck(Oid classid, Oid objectid, Oid roleid)
41034103
if (superuser_arg(roleid))
41044104
return true;
41054105

4106+
/* For large objects, the catalog to consult is pg_largeobject_metadata */
4107+
if (classid == LargeObjectRelationId)
4108+
classid = LargeObjectMetadataRelationId;
4109+
41064110
cacheid = get_object_catcache_oid(classid);
41074111
if (cacheid != -1)
41084112
{
4113+
/* we can get the object's tuple from the syscache */
41094114
HeapTuple tuple;
41104115

41114116
tuple = SearchSysCache1(cacheid, ObjectIdGetDatum(objectid));
@@ -4122,7 +4127,6 @@ object_ownercheck(Oid classid, Oid objectid, Oid roleid)
41224127
else
41234128
{
41244129
/* for catalogs without an appropriate syscache */
4125-
41264130
Relation rel;
41274131
ScanKeyData entry[1];
41284132
SysScanDesc scan;
@@ -4442,9 +4446,9 @@ recordExtObjInitPriv(Oid objoid, Oid classoid)
44424446

44434447
ReleaseSysCache(tuple);
44444448
}
4445-
/* pg_largeobject_metadata */
4446-
else if (classoid == LargeObjectMetadataRelationId)
4449+
else if (classoid == LargeObjectRelationId)
44474450
{
4451+
/* For large objects, we must consult pg_largeobject_metadata */
44484452
Datum aclDatum;
44494453
bool isNull;
44504454
HeapTuple tuple;

src/backend/catalog/pg_shdepend.c

+3-14
Original file line numberDiff line numberDiff line change
@@ -1614,20 +1614,9 @@ shdepReassignOwned(List *roleids, Oid newrole)
16141614
case DatabaseRelationId:
16151615
case TSConfigRelationId:
16161616
case TSDictionaryRelationId:
1617-
{
1618-
Oid classId = sdepForm->classid;
1619-
Relation catalog;
1620-
1621-
if (classId == LargeObjectRelationId)
1622-
classId = LargeObjectMetadataRelationId;
1623-
1624-
catalog = table_open(classId, RowExclusiveLock);
1625-
1626-
AlterObjectOwner_internal(catalog, sdepForm->objid,
1627-
newrole);
1628-
1629-
table_close(catalog, NoLock);
1630-
}
1617+
AlterObjectOwner_internal(sdepForm->classid,
1618+
sdepForm->objid,
1619+
newrole);
16311620
break;
16321621

16331622
default:

src/backend/commands/alter.c

+22-26
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,7 @@ ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
918918
case OBJECT_TSDICTIONARY:
919919
case OBJECT_TSCONFIGURATION:
920920
{
921-
Relation catalog;
922921
Relation relation;
923-
Oid classId;
924922
ObjectAddress address;
925923

926924
address = get_object_address(stmt->objectType,
@@ -929,20 +927,9 @@ ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
929927
AccessExclusiveLock,
930928
false);
931929
Assert(relation == NULL);
932-
classId = address.classId;
933930

934-
/*
935-
* XXX - get_object_address returns Oid of pg_largeobject
936-
* catalog for OBJECT_LARGEOBJECT because of historical
937-
* reasons. Fix up it here.
938-
*/
939-
if (classId == LargeObjectRelationId)
940-
classId = LargeObjectMetadataRelationId;
941-
942-
catalog = table_open(classId, RowExclusiveLock);
943-
944-
AlterObjectOwner_internal(catalog, address.objectId, newowner);
945-
table_close(catalog, RowExclusiveLock);
931+
AlterObjectOwner_internal(address.classId, address.objectId,
932+
newowner);
946933

947934
return address;
948935
}
@@ -960,25 +947,32 @@ ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
960947
* cases (won't work for tables, nor other cases where we need to do more than
961948
* change the ownership column of a single catalog entry).
962949
*
963-
* rel: catalog relation containing object (RowExclusiveLock'd by caller)
950+
* classId: OID of catalog containing object
964951
* objectId: OID of object to change the ownership of
965952
* new_ownerId: OID of new object owner
953+
*
954+
* This will work on large objects, but we have to beware of the fact that
955+
* classId isn't the OID of the catalog to modify in that case.
966956
*/
967957
void
968-
AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
958+
AlterObjectOwner_internal(Oid classId, Oid objectId, Oid new_ownerId)
969959
{
970-
Oid classId = RelationGetRelid(rel);
971-
AttrNumber Anum_oid = get_object_attnum_oid(classId);
972-
AttrNumber Anum_owner = get_object_attnum_owner(classId);
973-
AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
974-
AttrNumber Anum_acl = get_object_attnum_acl(classId);
975-
AttrNumber Anum_name = get_object_attnum_name(classId);
960+
/* For large objects, the catalog to modify is pg_largeobject_metadata */
961+
Oid catalogId = (classId == LargeObjectRelationId) ? LargeObjectMetadataRelationId : classId;
962+
AttrNumber Anum_oid = get_object_attnum_oid(catalogId);
963+
AttrNumber Anum_owner = get_object_attnum_owner(catalogId);
964+
AttrNumber Anum_namespace = get_object_attnum_namespace(catalogId);
965+
AttrNumber Anum_acl = get_object_attnum_acl(catalogId);
966+
AttrNumber Anum_name = get_object_attnum_name(catalogId);
967+
Relation rel;
976968
HeapTuple oldtup;
977969
Datum datum;
978970
bool isnull;
979971
Oid old_ownerId;
980972
Oid namespaceId = InvalidOid;
981973

974+
rel = table_open(catalogId, RowExclusiveLock);
975+
982976
oldtup = get_catalog_object_by_oid(rel, Anum_oid, objectId);
983977
if (oldtup == NULL)
984978
elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
@@ -1026,7 +1020,8 @@ AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
10261020
snprintf(namebuf, sizeof(namebuf), "%u", objectId);
10271021
objname = namebuf;
10281022
}
1029-
aclcheck_error(ACLCHECK_NOT_OWNER, get_object_type(classId, objectId),
1023+
aclcheck_error(ACLCHECK_NOT_OWNER,
1024+
get_object_type(catalogId, objectId),
10301025
objname);
10311026
}
10321027
/* Must be able to become new owner */
@@ -1079,8 +1074,6 @@ AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
10791074
CatalogTupleUpdate(rel, &newtup->t_self, newtup);
10801075

10811076
/* Update owner dependency reference */
1082-
if (classId == LargeObjectMetadataRelationId)
1083-
classId = LargeObjectRelationId;
10841077
changeDependencyOnOwner(classId, objectId, new_ownerId);
10851078

10861079
/* Release memory */
@@ -1089,5 +1082,8 @@ AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
10891082
pfree(replaces);
10901083
}
10911084

1085+
/* Note the post-alter hook gets classId not catalogId */
10921086
InvokeObjectPostAlterHook(classId, objectId, 0);
1087+
1088+
table_close(rel, RowExclusiveLock);
10931089
}

src/backend/libpq/be-fsstubs.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#include <unistd.h>
4444

4545
#include "access/xact.h"
46-
#include "catalog/pg_largeobject_metadata.h"
46+
#include "catalog/pg_largeobject.h"
4747
#include "libpq/be-fsstubs.h"
4848
#include "libpq/libpq-fs.h"
4949
#include "miscadmin.h"
@@ -323,7 +323,7 @@ be_lo_unlink(PG_FUNCTION_ARGS)
323323
* relevant FDs.
324324
*/
325325
if (!lo_compat_privileges &&
326-
!object_ownercheck(LargeObjectMetadataRelationId, lobjId, GetUserId()))
326+
!object_ownercheck(LargeObjectRelationId, lobjId, GetUserId()))
327327
ereport(ERROR,
328328
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
329329
errmsg("must be owner of large object %u", lobjId)));

src/backend/storage/large_object/inv_api.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,10 @@ inv_create(Oid lobjId)
221221
/*
222222
* dependency on the owner of largeobject
223223
*
224-
* The reason why we use LargeObjectRelationId instead of
225-
* LargeObjectMetadataRelationId here is to provide backward compatibility
226-
* to the applications which utilize a knowledge about internal layout of
227-
* system catalogs. OID of pg_largeobject_metadata and loid of
228-
* pg_largeobject are same value, so there are no actual differences here.
224+
* Note that LO dependencies are recorded using classId
225+
* LargeObjectRelationId for backwards-compatibility reasons. Using
226+
* LargeObjectMetadataRelationId instead would simplify matters for the
227+
* backend, but it'd complicate pg_dump and possibly break other clients.
229228
*/
230229
recordDependencyOnOwner(LargeObjectRelationId,
231230
lobjId_new, GetUserId());

src/include/commands/alter.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "catalog/dependency.h"
1818
#include "catalog/objectaddress.h"
1919
#include "nodes/parsenodes.h"
20-
#include "utils/relcache.h"
2120

2221
extern ObjectAddress ExecRenameStmt(RenameStmt *stmt);
2322

@@ -29,7 +28,7 @@ extern Oid AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid,
2928
ObjectAddresses *objsMoved);
3029

3130
extern ObjectAddress ExecAlterOwnerStmt(AlterOwnerStmt *stmt);
32-
extern void AlterObjectOwner_internal(Relation rel, Oid objectId,
31+
extern void AlterObjectOwner_internal(Oid classId, Oid objectId,
3332
Oid new_ownerId);
3433

3534
#endif /* ALTER_H */

src/test/regress/expected/largeobject.out

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
\getenv abs_builddir PG_ABS_BUILDDIR
77
-- ensure consistent test output regardless of the default bytea format
88
SET bytea_output TO escape;
9-
-- Test ALTER LARGE OBJECT OWNER, GRANT, COMMENT
9+
-- Test ALTER LARGE OBJECT OWNER
1010
CREATE ROLE regress_lo_user;
1111
SELECT lo_create(42);
1212
lo_create
@@ -15,8 +15,11 @@ SELECT lo_create(42);
1515
(1 row)
1616

1717
ALTER LARGE OBJECT 42 OWNER TO regress_lo_user;
18+
-- Test GRANT, COMMENT as non-superuser
19+
SET SESSION AUTHORIZATION regress_lo_user;
1820
GRANT SELECT ON LARGE OBJECT 42 TO public;
1921
COMMENT ON LARGE OBJECT 42 IS 'the ultimate answer';
22+
RESET SESSION AUTHORIZATION;
2023
-- Test psql's \lo_list et al (we assume no other LOs exist yet)
2124
\lo_list
2225
Large objects

src/test/regress/expected/largeobject_1.out

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
\getenv abs_builddir PG_ABS_BUILDDIR
77
-- ensure consistent test output regardless of the default bytea format
88
SET bytea_output TO escape;
9-
-- Test ALTER LARGE OBJECT OWNER, GRANT, COMMENT
9+
-- Test ALTER LARGE OBJECT OWNER
1010
CREATE ROLE regress_lo_user;
1111
SELECT lo_create(42);
1212
lo_create
@@ -15,8 +15,11 @@ SELECT lo_create(42);
1515
(1 row)
1616

1717
ALTER LARGE OBJECT 42 OWNER TO regress_lo_user;
18+
-- Test GRANT, COMMENT as non-superuser
19+
SET SESSION AUTHORIZATION regress_lo_user;
1820
GRANT SELECT ON LARGE OBJECT 42 TO public;
1921
COMMENT ON LARGE OBJECT 42 IS 'the ultimate answer';
22+
RESET SESSION AUTHORIZATION;
2023
-- Test psql's \lo_list et al (we assume no other LOs exist yet)
2124
\lo_list
2225
Large objects

src/test/regress/sql/largeobject.sql

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@
99
-- ensure consistent test output regardless of the default bytea format
1010
SET bytea_output TO escape;
1111

12-
-- Test ALTER LARGE OBJECT OWNER, GRANT, COMMENT
12+
-- Test ALTER LARGE OBJECT OWNER
1313
CREATE ROLE regress_lo_user;
1414
SELECT lo_create(42);
1515
ALTER LARGE OBJECT 42 OWNER TO regress_lo_user;
16+
17+
-- Test GRANT, COMMENT as non-superuser
18+
SET SESSION AUTHORIZATION regress_lo_user;
19+
1620
GRANT SELECT ON LARGE OBJECT 42 TO public;
1721
COMMENT ON LARGE OBJECT 42 IS 'the ultimate answer';
1822

23+
RESET SESSION AUTHORIZATION;
24+
1925
-- Test psql's \lo_list et al (we assume no other LOs exist yet)
2026
\lo_list
2127
\lo_list+

0 commit comments

Comments
 (0)