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

Commit ceac450

Browse files
committed
Fix handling of COMMENT for domain constraints
For a non-superuser, changing a comment on a domain constraint was leading to a cache lookup failure as the code tried to perform the ownership lookup on the constraint OID itself, thinking that it was a type, but this check needs to happen on the type the domain constraint relies on. As the type a domain constraint relies on can be guessed directly based on the constraint OID, first fetch its type OID and perform the ownership on it. This is broken since 7eca575, which has split the handling of comments for table constraints and domain constraints, so back-patch down to 9.5. Reported-by: Clemens Ladisch Author: Daniel Gustafsson, Michael Paquier Reviewed-by: Álvaro Herrera Discussion: https://postgr.es/m/15833-808e11904835d26f@postgresql.org Backpatch-through: 9.5
1 parent e788e84 commit ceac450

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/backend/catalog/objectaddress.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -2295,10 +2295,32 @@ check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address,
22952295
case OBJECT_TYPE:
22962296
case OBJECT_DOMAIN:
22972297
case OBJECT_ATTRIBUTE:
2298-
case OBJECT_DOMCONSTRAINT:
22992298
if (!pg_type_ownercheck(address.objectId, roleid))
23002299
aclcheck_error_type(ACLCHECK_NOT_OWNER, address.objectId);
23012300
break;
2301+
case OBJECT_DOMCONSTRAINT:
2302+
{
2303+
HeapTuple tuple;
2304+
Oid contypid;
2305+
2306+
tuple = SearchSysCache1(CONSTROID,
2307+
ObjectIdGetDatum(address.objectId));
2308+
if (!HeapTupleIsValid(tuple))
2309+
elog(ERROR, "constraint with OID %u does not exist",
2310+
address.objectId);
2311+
2312+
contypid = ((Form_pg_constraint) GETSTRUCT(tuple))->contypid;
2313+
2314+
ReleaseSysCache(tuple);
2315+
2316+
/*
2317+
* Fallback to type ownership check in this case as this is
2318+
* what domain constraints rely on.
2319+
*/
2320+
if (!pg_type_ownercheck(contypid, roleid))
2321+
aclcheck_error_type(ACLCHECK_NOT_OWNER, contypid);
2322+
}
2323+
break;
23022324
case OBJECT_AGGREGATE:
23032325
case OBJECT_FUNCTION:
23042326
case OBJECT_PROCEDURE:

src/test/regress/input/constraints.source

+15
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,10 @@ ALTER TABLE deferred_excl ADD EXCLUDE (f1 WITH =);
518518
DROP TABLE deferred_excl;
519519

520520
-- Comments
521+
-- Setup a low-level role to enforce non-superuser checks.
522+
CREATE ROLE regress_constraint_comments;
523+
SET SESSION AUTHORIZATION regress_constraint_comments;
524+
521525
CREATE TABLE constraint_comments_tbl (a int CONSTRAINT the_constraint CHECK (a > 0));
522526
CREATE DOMAIN constraint_comments_dom AS int CONSTRAINT the_constraint CHECK (value > 0);
523527

@@ -535,5 +539,16 @@ COMMENT ON CONSTRAINT the_constraint ON DOMAIN no_comments_dom IS 'another bad c
535539
COMMENT ON CONSTRAINT the_constraint ON constraint_comments_tbl IS NULL;
536540
COMMENT ON CONSTRAINT the_constraint ON DOMAIN constraint_comments_dom IS NULL;
537541

542+
-- unauthorized user
543+
RESET SESSION AUTHORIZATION;
544+
CREATE ROLE regress_constraint_comments_noaccess;
545+
SET SESSION AUTHORIZATION regress_constraint_comments_noaccess;
546+
COMMENT ON CONSTRAINT the_constraint ON constraint_comments_tbl IS 'no, the comment';
547+
COMMENT ON CONSTRAINT the_constraint ON DOMAIN constraint_comments_dom IS 'no, another comment';
548+
RESET SESSION AUTHORIZATION;
549+
538550
DROP TABLE constraint_comments_tbl;
539551
DROP DOMAIN constraint_comments_dom;
552+
553+
DROP ROLE regress_constraint_comments;
554+
DROP ROLE regress_constraint_comments_noaccess;

src/test/regress/output/constraints.source

+14
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,9 @@ ERROR: could not create exclusion constraint "deferred_excl_f1_excl"
704704
DETAIL: Key (f1)=(3) conflicts with key (f1)=(3).
705705
DROP TABLE deferred_excl;
706706
-- Comments
707+
-- Setup a low-level role to enforce non-superuser checks.
708+
CREATE ROLE regress_constraint_comments;
709+
SET SESSION AUTHORIZATION regress_constraint_comments;
707710
CREATE TABLE constraint_comments_tbl (a int CONSTRAINT the_constraint CHECK (a > 0));
708711
CREATE DOMAIN constraint_comments_dom AS int CONSTRAINT the_constraint CHECK (value > 0);
709712
COMMENT ON CONSTRAINT the_constraint ON constraint_comments_tbl IS 'yes, the comment';
@@ -720,5 +723,16 @@ COMMENT ON CONSTRAINT the_constraint ON DOMAIN no_comments_dom IS 'another bad c
720723
ERROR: type "no_comments_dom" does not exist
721724
COMMENT ON CONSTRAINT the_constraint ON constraint_comments_tbl IS NULL;
722725
COMMENT ON CONSTRAINT the_constraint ON DOMAIN constraint_comments_dom IS NULL;
726+
-- unauthorized user
727+
RESET SESSION AUTHORIZATION;
728+
CREATE ROLE regress_constraint_comments_noaccess;
729+
SET SESSION AUTHORIZATION regress_constraint_comments_noaccess;
730+
COMMENT ON CONSTRAINT the_constraint ON constraint_comments_tbl IS 'no, the comment';
731+
ERROR: must be owner of relation constraint_comments_tbl
732+
COMMENT ON CONSTRAINT the_constraint ON DOMAIN constraint_comments_dom IS 'no, another comment';
733+
ERROR: must be owner of type constraint_comments_dom
734+
RESET SESSION AUTHORIZATION;
723735
DROP TABLE constraint_comments_tbl;
724736
DROP DOMAIN constraint_comments_dom;
737+
DROP ROLE regress_constraint_comments;
738+
DROP ROLE regress_constraint_comments_noaccess;

0 commit comments

Comments
 (0)