Skip to content

Commit d824e28

Browse files
committed
Disallow converting a table to a view if row security is present.
When DefineQueryRewrite() is about to convert a table to a view, it checks the table for features unavailable to views. For example, it rejects tables having triggers. It omits to reject tables having relrowsecurity or a pg_policy record. Fix that. To faciliate the repair, invent relation_has_policies() which indicates the presence of policies on a relation even when row security is disabled for that relation. Reported by Noah Misch. Patch by me, review by Stephen Frost. Back-patch to 9.5 where RLS was introduced.
1 parent f781a0f commit d824e28

File tree

5 files changed

+97
-5
lines changed

5 files changed

+97
-5
lines changed

src/backend/commands/policy.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,3 +1037,32 @@ get_relation_policy_oid(Oid relid, const char *policy_name, bool missing_ok)
10371037

10381038
return policy_oid;
10391039
}
1040+
1041+
/*
1042+
* relation_has_policies - Determine if relation has any policies
1043+
*/
1044+
bool
1045+
relation_has_policies(Relation rel)
1046+
{
1047+
Relation catalog;
1048+
ScanKeyData skey;
1049+
SysScanDesc sscan;
1050+
HeapTuple policy_tuple;
1051+
bool ret = false;
1052+
1053+
catalog = heap_open(PolicyRelationId, AccessShareLock);
1054+
ScanKeyInit(&skey,
1055+
Anum_pg_policy_polrelid,
1056+
BTEqualStrategyNumber, F_OIDEQ,
1057+
ObjectIdGetDatum(RelationGetRelid(rel)));
1058+
sscan = systable_beginscan(catalog, PolicyPolrelidPolnameIndexId, true,
1059+
NULL, 1, &skey);
1060+
policy_tuple = systable_getnext(sscan);
1061+
if (HeapTupleIsValid(policy_tuple))
1062+
ret = true;
1063+
1064+
systable_endscan(sscan);
1065+
heap_close(catalog, AccessShareLock);
1066+
1067+
return ret;
1068+
}

src/backend/rewrite/rewriteDefine.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "catalog/objectaccess.h"
2828
#include "catalog/pg_rewrite.h"
2929
#include "catalog/storage.h"
30+
#include "commands/policy.h"
3031
#include "miscadmin.h"
3132
#include "nodes/nodeFuncs.h"
3233
#include "parser/parse_utilcmd.h"
@@ -410,11 +411,12 @@ DefineQueryRewrite(char *rulename,
410411
*
411412
* If so, check that the relation is empty because the storage for the
412413
* relation is going to be deleted. Also insist that the rel not have
413-
* any triggers, indexes, or child tables. (Note: these tests are too
414-
* strict, because they will reject relations that once had such but
415-
* don't anymore. But we don't really care, because this whole
416-
* business of converting relations to views is just a kluge to allow
417-
* dump/reload of views that participate in circular dependencies.)
414+
* any triggers, indexes, child tables, policies, or RLS enabled.
415+
* (Note: these tests are too strict, because they will reject
416+
* relations that once had such but don't anymore. But we don't
417+
* really care, because this whole business of converting relations
418+
* to views is just a kluge to allow dump/reload of views that
419+
* participate in circular dependencies.)
418420
*/
419421
if (event_relation->rd_rel->relkind != RELKIND_VIEW &&
420422
event_relation->rd_rel->relkind != RELKIND_MATVIEW)
@@ -451,6 +453,18 @@ DefineQueryRewrite(char *rulename,
451453
errmsg("could not convert table \"%s\" to a view because it has child tables",
452454
RelationGetRelationName(event_relation))));
453455

456+
if (event_relation->rd_rel->relrowsecurity)
457+
ereport(ERROR,
458+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
459+
errmsg("could not convert table \"%s\" to a view because it has row security enabled",
460+
RelationGetRelationName(event_relation))));
461+
462+
if (relation_has_policies(event_relation))
463+
ereport(ERROR,
464+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
465+
errmsg("could not convert table \"%s\" to a view because it has row security policies",
466+
RelationGetRelationName(event_relation))));
467+
454468
RelisBecomingView = true;
455469
}
456470
}

src/include/commands/policy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ extern Oid get_relation_policy_oid(Oid relid, const char *policy_name,
3131

3232
extern ObjectAddress rename_policy(RenameStmt *stmt);
3333

34+
extern bool relation_has_policies(Relation rel);
3435

3536
#endif /* POLICY_H */

src/test/regress/expected/rowsecurity.out

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2997,6 +2997,29 @@ DROP ROLE bob; -- succeeds
29972997
ROLLBACK TO q;
29982998
ROLLBACK; -- cleanup
29992999
--
3000+
-- Converting table to view
3001+
--
3002+
BEGIN;
3003+
SET ROW_SECURITY = FORCE;
3004+
CREATE TABLE t (c int);
3005+
CREATE POLICY p ON t USING (c % 2 = 1);
3006+
ALTER TABLE t ENABLE ROW LEVEL SECURITY;
3007+
SAVEPOINT q;
3008+
CREATE RULE "_RETURN" AS ON SELECT TO t DO INSTEAD
3009+
SELECT * FROM generate_series(1,5) t0(c); -- fails due to row level security enabled
3010+
ERROR: could not convert table "t" to a view because it has row security enabled
3011+
ROLLBACK TO q;
3012+
ALTER TABLE t DISABLE ROW LEVEL SECURITY;
3013+
SAVEPOINT q;
3014+
CREATE RULE "_RETURN" AS ON SELECT TO t DO INSTEAD
3015+
SELECT * FROM generate_series(1,5) t0(c); -- fails due to policy p on t
3016+
ERROR: could not convert table "t" to a view because it has row security policies
3017+
ROLLBACK TO q;
3018+
DROP POLICY p ON t;
3019+
CREATE RULE "_RETURN" AS ON SELECT TO t DO INSTEAD
3020+
SELECT * FROM generate_series(1,5) t0(c); -- succeeds
3021+
ROLLBACK;
3022+
--
30003023
-- Clean up objects
30013024
--
30023025
RESET SESSION AUTHORIZATION;

src/test/regress/sql/rowsecurity.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,31 @@ ROLLBACK TO q;
12601260

12611261
ROLLBACK; -- cleanup
12621262

1263+
--
1264+
-- Converting table to view
1265+
--
1266+
BEGIN;
1267+
SET ROW_SECURITY = FORCE;
1268+
CREATE TABLE t (c int);
1269+
CREATE POLICY p ON t USING (c % 2 = 1);
1270+
ALTER TABLE t ENABLE ROW LEVEL SECURITY;
1271+
1272+
SAVEPOINT q;
1273+
CREATE RULE "_RETURN" AS ON SELECT TO t DO INSTEAD
1274+
SELECT * FROM generate_series(1,5) t0(c); -- fails due to row level security enabled
1275+
ROLLBACK TO q;
1276+
1277+
ALTER TABLE t DISABLE ROW LEVEL SECURITY;
1278+
SAVEPOINT q;
1279+
CREATE RULE "_RETURN" AS ON SELECT TO t DO INSTEAD
1280+
SELECT * FROM generate_series(1,5) t0(c); -- fails due to policy p on t
1281+
ROLLBACK TO q;
1282+
1283+
DROP POLICY p ON t;
1284+
CREATE RULE "_RETURN" AS ON SELECT TO t DO INSTEAD
1285+
SELECT * FROM generate_series(1,5) t0(c); -- succeeds
1286+
ROLLBACK;
1287+
12631288
--
12641289
-- Clean up objects
12651290
--

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy