Commit aa5d3c0b authored by Stephen Frost's avatar Stephen Frost

RLS: Fix ALL vs. SELECT+UPDATE policy usage

When we add the SELECT-privilege based policies to the RLS with check
options (such as for an UPDATE statement, or when we have INSERT ...
RETURNING), we need to be sure and use the 'USING' case if the policy is
actually an 'ALL' policy (which could have both a USING clause and an
independent WITH CHECK clause).

This could result in policies acting differently when built using ALL
(when the ALL had both USING and WITH CHECK clauses) and when building
the policies independently as SELECT and UPDATE policies.

Fix this by adding an explicit boolean to add_with_check_options() to
indicate when the USING policy should be used, even if the policy has
both USING and WITH CHECK policies on it.

Reported by: Rod Taylor

Back-patch to 9.5 where RLS was introduced.
parent b58c433e
...@@ -78,7 +78,8 @@ static void add_with_check_options(Relation rel, ...@@ -78,7 +78,8 @@ static void add_with_check_options(Relation rel,
List *permissive_policies, List *permissive_policies,
List *restrictive_policies, List *restrictive_policies,
List **withCheckOptions, List **withCheckOptions,
bool *hasSubLinks); bool *hasSubLinks,
bool force_using);
static bool check_role_for_policy(ArrayType *policy_roles, Oid user_id); static bool check_role_for_policy(ArrayType *policy_roles, Oid user_id);
...@@ -272,7 +273,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index, ...@@ -272,7 +273,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index,
permissive_policies, permissive_policies,
restrictive_policies, restrictive_policies,
withCheckOptions, withCheckOptions,
hasSubLinks); hasSubLinks,
false);
/* /*
* Get and add ALL/SELECT policies, if SELECT rights are required for * Get and add ALL/SELECT policies, if SELECT rights are required for
...@@ -295,7 +297,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index, ...@@ -295,7 +297,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index,
select_permissive_policies, select_permissive_policies,
select_restrictive_policies, select_restrictive_policies,
withCheckOptions, withCheckOptions,
hasSubLinks); hasSubLinks,
true);
} }
/* /*
...@@ -324,7 +327,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index, ...@@ -324,7 +327,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index,
conflict_permissive_policies, conflict_permissive_policies,
conflict_restrictive_policies, conflict_restrictive_policies,
withCheckOptions, withCheckOptions,
hasSubLinks); hasSubLinks,
true);
/* /*
* Get and add ALL/SELECT policies, as WCO_RLS_CONFLICT_CHECK WCOs * Get and add ALL/SELECT policies, as WCO_RLS_CONFLICT_CHECK WCOs
...@@ -346,7 +350,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index, ...@@ -346,7 +350,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index,
conflict_select_permissive_policies, conflict_select_permissive_policies,
conflict_select_restrictive_policies, conflict_select_restrictive_policies,
withCheckOptions, withCheckOptions,
hasSubLinks); hasSubLinks,
true);
} }
/* Enforce the WITH CHECK clauses of the UPDATE policies */ /* Enforce the WITH CHECK clauses of the UPDATE policies */
...@@ -355,7 +360,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index, ...@@ -355,7 +360,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index,
conflict_permissive_policies, conflict_permissive_policies,
conflict_restrictive_policies, conflict_restrictive_policies,
withCheckOptions, withCheckOptions,
hasSubLinks); hasSubLinks,
false);
} }
} }
...@@ -659,13 +665,14 @@ add_with_check_options(Relation rel, ...@@ -659,13 +665,14 @@ add_with_check_options(Relation rel,
List *permissive_policies, List *permissive_policies,
List *restrictive_policies, List *restrictive_policies,
List **withCheckOptions, List **withCheckOptions,
bool *hasSubLinks) bool *hasSubLinks,
bool force_using)
{ {
ListCell *item; ListCell *item;
List *permissive_quals = NIL; List *permissive_quals = NIL;
#define QUAL_FOR_WCO(policy) \ #define QUAL_FOR_WCO(policy) \
( kind != WCO_RLS_CONFLICT_CHECK && \ ( !force_using && \
(policy)->with_check_qual != NULL ? \ (policy)->with_check_qual != NULL ? \
(policy)->with_check_qual : (policy)->qual ) (policy)->with_check_qual : (policy)->qual )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment