Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita5b652f

Browse files
committed
Make inherited TRUNCATE perform access permission checks on parent table only.
Previously, TRUNCATE command through a parent table checked thepermissions on not only the parent table but also the children tablesinherited from it. This was a bug and inherited queries should performaccess permission checks on the parent table only. This commit fixesthat bug.Back-patch to all supported branches.Author: Amit LangoteReviewed-by: Fujii MasaoDiscussion:https://postgr.es/m/CAHGQGwFHdSvifhJE+-GSNqUHSfbiKxaeQQ7HGcYz6SC2n_oDcg@mail.gmail.com
1 parent374464c commita5b652f

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ struct DropRelationCallbackState
300300
#definechild_dependency_type(child_is_partition)\
301301
((child_is_partition) ? DEPENDENCY_AUTO : DEPENDENCY_NORMAL)
302302

303-
staticvoidtruncate_check_rel(Relationrel);
303+
staticvoidtruncate_check_rel(Oidrelid,Form_pg_classreltuple);
304+
staticvoidtruncate_check_perms(Oidrelid,Form_pg_classreltuple);
305+
staticvoidtruncate_check_activity(Relationrel);
304306
staticList*MergeAttributes(List*schema,List*supers,charrelpersistence,
305307
boolis_partition,List**supOids,List**supconstr,
306308
int*supOidCount);
@@ -1381,7 +1383,11 @@ ExecuteTruncate(TruncateStmt *stmt)
13811383
heap_close(rel,lockmode);
13821384
continue;
13831385
}
1384-
truncate_check_rel(rel);
1386+
1387+
truncate_check_rel(myrelid,rel->rd_rel);
1388+
truncate_check_perms(myrelid,rel->rd_rel);
1389+
truncate_check_activity(rel);
1390+
13851391
rels=lappend(rels,rel);
13861392
relids=lappend_oid(relids,myrelid);
13871393
/* Log this relation only if needed for logical decoding */
@@ -1420,7 +1426,15 @@ ExecuteTruncate(TruncateStmt *stmt)
14201426
continue;
14211427
}
14221428

1423-
truncate_check_rel(rel);
1429+
/*
1430+
* Inherited TRUNCATE commands perform access
1431+
* permission checks on the parent table only.
1432+
* So we skip checking the children's permissions
1433+
* and don't call truncate_check_perms() here.
1434+
*/
1435+
truncate_check_rel(RelationGetRelid(rel),rel->rd_rel);
1436+
truncate_check_activity(rel);
1437+
14241438
rels=lappend(rels,rel);
14251439
relids=lappend_oid(relids,childrelid);
14261440
/* Log this relation only if needed for logical decoding */
@@ -1503,7 +1517,9 @@ ExecuteTruncateGuts(List *explicit_rels, List *relids, List *relids_logged,
15031517
ereport(NOTICE,
15041518
(errmsg("truncate cascades to table \"%s\"",
15051519
RelationGetRelationName(rel))));
1506-
truncate_check_rel(rel);
1520+
truncate_check_rel(relid,rel->rd_rel);
1521+
truncate_check_perms(relid,rel->rd_rel);
1522+
truncate_check_activity(rel);
15071523
rels=lappend(rels,rel);
15081524
relids=lappend_oid(relids,relid);
15091525
/* Log this relation only if needed for logical decoding */
@@ -1759,35 +1775,51 @@ ExecuteTruncateGuts(List *explicit_rels, List *relids, List *relids_logged,
17591775
* Check that a given rel is safe to truncate. Subroutine for ExecuteTruncate
17601776
*/
17611777
staticvoid
1762-
truncate_check_rel(Relationrel)
1778+
truncate_check_rel(Oidrelid,Form_pg_classreltuple)
17631779
{
1764-
AclResultaclresult;
1780+
char*relname=NameStr(reltuple->relname);
17651781

17661782
/*
17671783
* Only allow truncate on regular tables and partitioned tables (although,
17681784
* the latter are only being included here for the following checks; no
17691785
* physical truncation will occur in their case.)
17701786
*/
1771-
if (rel->rd_rel->relkind!=RELKIND_RELATION&&
1772-
rel->rd_rel->relkind!=RELKIND_PARTITIONED_TABLE)
1787+
if (reltuple->relkind!=RELKIND_RELATION&&
1788+
reltuple->relkind!=RELKIND_PARTITIONED_TABLE)
17731789
ereport(ERROR,
17741790
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1775-
errmsg("\"%s\" is not a table",
1776-
RelationGetRelationName(rel))));
1777-
1778-
/* Permissions checks */
1779-
aclresult=pg_class_aclcheck(RelationGetRelid(rel),GetUserId(),
1780-
ACL_TRUNCATE);
1781-
if (aclresult!=ACLCHECK_OK)
1782-
aclcheck_error(aclresult,get_relkind_objtype(rel->rd_rel->relkind),
1783-
RelationGetRelationName(rel));
1791+
errmsg("\"%s\" is not a table",relname)));
17841792

1785-
if (!allowSystemTableMods&&IsSystemRelation(rel))
1793+
if (!allowSystemTableMods&&IsSystemClass(relid,reltuple))
17861794
ereport(ERROR,
17871795
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
17881796
errmsg("permission denied: \"%s\" is a system catalog",
1789-
RelationGetRelationName(rel))));
1797+
relname)));
1798+
}
1799+
1800+
/*
1801+
* Check that current user has the permission to truncate given relation.
1802+
*/
1803+
staticvoid
1804+
truncate_check_perms(Oidrelid,Form_pg_classreltuple)
1805+
{
1806+
char*relname=NameStr(reltuple->relname);
1807+
AclResultaclresult;
17901808

1809+
/* Permissions checks */
1810+
aclresult=pg_class_aclcheck(relid,GetUserId(),ACL_TRUNCATE);
1811+
if (aclresult!=ACLCHECK_OK)
1812+
aclcheck_error(aclresult,get_relkind_objtype(reltuple->relkind),
1813+
relname);
1814+
}
1815+
1816+
/*
1817+
* Set of extra sanity checks to check if a given relation is safe to
1818+
* truncate.
1819+
*/
1820+
staticvoid
1821+
truncate_check_activity(Relationrel)
1822+
{
17911823
/*
17921824
* Don't allow truncate on temp tables of other backends ... their local
17931825
* buffer manager is not going to cope.

‎src/test/regress/expected/privileges.out

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,27 @@ SELECT oid FROM atestp2; -- ok
695695
-----
696696
(0 rows)
697697

698+
-- child's permissions do not apply when operating on parent
699+
SET SESSION AUTHORIZATION regress_priv_user1;
700+
REVOKE ALL ON atestc FROM regress_priv_user2;
701+
GRANT ALL ON atestp1 TO regress_priv_user2;
702+
SET SESSION AUTHORIZATION regress_priv_user2;
703+
SELECT f2 FROM atestp1; -- ok
704+
f2
705+
----
706+
(0 rows)
707+
708+
SELECT f2 FROM atestc; -- fail
709+
ERROR: permission denied for table atestc
710+
DELETE FROM atestp1; -- ok
711+
DELETE FROM atestc; -- fail
712+
ERROR: permission denied for table atestc
713+
UPDATE atestp1 SET f1 = 1; -- ok
714+
UPDATE atestc SET f1 = 1; -- fail
715+
ERROR: permission denied for table atestc
716+
TRUNCATE atestp1; -- ok
717+
TRUNCATE atestc; -- fail
718+
ERROR: permission denied for table atestc
698719
-- privileges on functions, languages
699720
-- switch to superuser
700721
\c -

‎src/test/regress/sql/privileges.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,20 @@ SELECT fy FROM atestp2; -- ok
446446
SELECT atestp2FROM atestp2;-- ok
447447
SELECToidFROM atestp2;-- ok
448448

449+
-- child's permissions do not apply when operating on parent
450+
SET SESSION AUTHORIZATION regress_priv_user1;
451+
REVOKE ALLON atestcFROM regress_priv_user2;
452+
GRANT ALLON atestp1 TO regress_priv_user2;
453+
SET SESSION AUTHORIZATION regress_priv_user2;
454+
SELECT f2FROM atestp1;-- ok
455+
SELECT f2FROM atestc;-- fail
456+
DELETEFROM atestp1;-- ok
457+
DELETEFROM atestc;-- fail
458+
UPDATE atestp1SET f1=1;-- ok
459+
UPDATE atestcSET f1=1;-- fail
460+
TRUNCATE atestp1;-- ok
461+
TRUNCATE atestc;-- fail
462+
449463
-- privileges on functions, languages
450464

451465
-- switch to superuser

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp