RLS: Keep deny policy when only restrictive exist
authorStephen Frost <[email protected]>
Mon, 3 Aug 2015 19:32:49 +0000 (15:32 -0400)
committerPavan Deolasee <[email protected]>
Wed, 26 Oct 2016 07:38:15 +0000 (13:08 +0530)
Only remove the default deny policy when a permissive policy exists
(either from the hook or defined by the user).  If only restrictive
policies exist then no rows will be visible, as restrictive policies
shouldn't make rows visible.  To address this requirement, a single
"USING (true)" permissive policy can be created.

Update the test_rls_hooks regression tests to create the necessary
"USING (true)" permissive policy.

Back-patch to 9.5 where RLS was added.

Per discussion with Dean.

src/backend/rewrite/rowsecurity.c
src/test/modules/test_rls_hooks/expected/test_rls_hooks.out
src/test/modules/test_rls_hooks/sql/test_rls_hooks.sql
src/test/modules/test_rls_hooks/test_rls_hooks.c

index aaf0061164b2969f727ec9adad2272280f4f622a..181a3929e74903d687e97fd034996abd7cb89d0b 100644 (file)
@@ -225,12 +225,18 @@ get_row_security_policies(Query *root, CmdType commandType, RangeTblEntry *rte,
        }
 
        /*
-        * If the only built-in policy is the default-deny one, and hook policies
-        * exist, then use the hook policies only and do not apply the
+        * If the only built-in policy is the default-deny one, and permissive hook
+        * policies exist, then use the hook policies only and do not apply the
         * default-deny policy.  Otherwise, we will apply both sets below.
+        *
+        * Note that we do not remove the defaultDeny policy if only *restrictive*
+        * policies exist as restrictive policies should only ever be reducing what
+        * is visible.  Therefore, at least one permissive policy must exist which
+        * allows records to be seen before restrictive policies can remove rows
+        * from that set.  A single "true" policy can be created to address this
+        * requirement, if necessary.
         */
-       if (defaultDeny &&
-               (hook_policies_restrictive != NIL || hook_policies_permissive != NIL))
+       if (defaultDeny && hook_policies_permissive != NIL)
        {
                rowsec_expr = NULL;
                rowsec_with_check_expr = NULL;
index 3a7a4c329f3adaedb4be5b0c152f3c9ad8689d0a..4587eb014b7fa4ada1fa0842d5f2c3fbb5f09a59 100644 (file)
@@ -13,6 +13,11 @@ CREATE TABLE rls_test_restrictive (
     supervisor      name,
     data            integer
 );
+-- At least one permissive policy must exist, otherwise
+-- the default deny policy will be applied.  For
+-- testing the only-restrictive-policies from the hook,
+-- create a simple 'allow all' policy.
+CREATE POLICY p1 ON rls_test_restrictive USING (true);
 -- initial test data
 INSERT INTO rls_test_restrictive VALUES ('r1','s1',1);
 INSERT INTO rls_test_restrictive VALUES ('r2','s2',2);
@@ -109,6 +114,8 @@ RESET ROLE;
 -- Create "internal" policies, to check that the policies from
 -- the hooks are combined correctly.
 CREATE POLICY p1 ON rls_test_permissive USING (data % 2 = 0);
+-- Remove the original allow-all policy
+DROP POLICY p1 ON rls_test_restrictive;
 CREATE POLICY p1 ON rls_test_restrictive USING (data % 2 = 0);
 CREATE POLICY p1 ON rls_test_both USING (data % 2 = 0);
 SET ROLE r1;
index ece4ab9dc94822cf2c65b07bd3ac55691d29ae70..3071213732f79eb7f34e381554fe37b548881898 100644 (file)
@@ -17,6 +17,12 @@ CREATE TABLE rls_test_restrictive (
     data            integer
 );
 
+-- At least one permissive policy must exist, otherwise
+-- the default deny policy will be applied.  For
+-- testing the only-restrictive-policies from the hook,
+-- create a simple 'allow all' policy.
+CREATE POLICY p1 ON rls_test_restrictive USING (true);
+
 -- initial test data
 INSERT INTO rls_test_restrictive VALUES ('r1','s1',1);
 INSERT INTO rls_test_restrictive VALUES ('r2','s2',2);
@@ -101,6 +107,8 @@ RESET ROLE;
 -- the hooks are combined correctly.
 CREATE POLICY p1 ON rls_test_permissive USING (data % 2 = 0);
 
+-- Remove the original allow-all policy
+DROP POLICY p1 ON rls_test_restrictive;
 CREATE POLICY p1 ON rls_test_restrictive USING (data % 2 = 0);
 
 CREATE POLICY p1 ON rls_test_both USING (data % 2 = 0);
index 61b62d55b4cf948dfbd6ae21340e266c17659702..f5cbc8002b67f974533b474a9bdb15692a0ec68b 100644 (file)
@@ -119,6 +119,11 @@ test_rls_hooks_permissive(CmdType cmdtype, Relation relation)
 
 /*
  * Return restrictive policies to be added
+ *
+ * Note that a permissive policy must exist or the default-deny policy
+ * will be included and nothing will be visible.  If no filtering should
+ * be done except for the restrictive policy, then a single "USING (true)"
+ * permissive policy can be used; see the regression tests.
  */
 List *
 test_rls_hooks_restrictive(CmdType cmdtype, Relation relation)