PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
rowsecurity.h File Reference
#include "nodes/parsenodes.h"
#include "utils/array.h"
#include "utils/relcache.h"
Include dependency graph for rowsecurity.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  RowSecurityPolicy
 
struct  RowSecurityDesc
 

Typedefs

typedef struct RowSecurityPolicy RowSecurityPolicy
 
typedef struct RowSecurityDesc RowSecurityDesc
 
typedef List *(* row_security_policy_hook_type )(CmdType cmdtype, Relation relation)
 

Functions

void get_row_security_policies (Query *root, RangeTblEntry *rte, int rt_index, List **securityQuals, List **withCheckOptions, bool *hasRowSecurity, bool *hasSubLinks)
 

Variables

PGDLLIMPORT
row_security_policy_hook_type 
row_security_policy_hook_permissive
 
PGDLLIMPORT
row_security_policy_hook_type 
row_security_policy_hook_restrictive
 

Typedef Documentation

typedef List*(* row_security_policy_hook_type)(CmdType cmdtype, Relation relation)

Definition at line 37 of file rowsecurity.h.

Function Documentation

void get_row_security_policies ( Query root,
RangeTblEntry rte,
int  rt_index,
List **  securityQuals,
List **  withCheckOptions,
bool hasRowSecurity,
bool hasSubLinks 
)

Definition at line 106 of file rowsecurity.c.

References ACL_SELECT, ACL_UPDATE, OnConflictExpr::action, add_security_quals(), add_with_check_options(), Assert, check_enable_rls(), RangeTblEntry::checkAsUser, CMD_DELETE, CMD_INSERT, CMD_SELECT, CMD_UPDATE, Query::commandType, get_policies_for_relation(), GetUserId(), heap_close, heap_open(), NIL, NoLock, Query::onConflict, ONCONFLICT_UPDATE, RangeTblEntry::relid, RangeTblEntry::relkind, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, RangeTblEntry::requiredPerms, Query::resultRelation, RLS_NONE, RLS_NONE_ENV, WCO_RLS_CONFLICT_CHECK, WCO_RLS_INSERT_CHECK, and WCO_RLS_UPDATE_CHECK.

Referenced by fireRIRrules().

109 {
110  Oid user_id;
111  int rls_status;
112  Relation rel;
113  CmdType commandType;
114  List *permissive_policies;
115  List *restrictive_policies;
116 
117  /* Defaults for the return values */
118  *securityQuals = NIL;
119  *withCheckOptions = NIL;
120  *hasRowSecurity = false;
121  *hasSubLinks = false;
122 
123  /* If this is not a normal relation, just return immediately */
124  if (rte->relkind != RELKIND_RELATION &&
126  return;
127 
128  /* Switch to checkAsUser if it's set */
129  user_id = rte->checkAsUser ? rte->checkAsUser : GetUserId();
130 
131  /* Determine the state of RLS for this, pass checkAsUser explicitly */
132  rls_status = check_enable_rls(rte->relid, rte->checkAsUser, false);
133 
134  /* If there is no RLS on this table at all, nothing to do */
135  if (rls_status == RLS_NONE)
136  return;
137 
138  /*
139  * RLS_NONE_ENV means we are not doing any RLS now, but that may change
140  * with changes to the environment, so we mark it as hasRowSecurity to
141  * force a re-plan when the environment changes.
142  */
143  if (rls_status == RLS_NONE_ENV)
144  {
145  /*
146  * Indicate that this query may involve RLS and must therefore be
147  * replanned if the environment changes (GUCs, role), but we are not
148  * adding anything here.
149  */
150  *hasRowSecurity = true;
151 
152  return;
153  }
154 
155  /*
156  * RLS is enabled for this relation.
157  *
158  * Get the security policies that should be applied, based on the command
159  * type. Note that if this isn't the target relation, we actually want
160  * the relation's SELECT policies, regardless of the query command type,
161  * for example in UPDATE t1 ... FROM t2 we need to apply t1's UPDATE
162  * policies and t2's SELECT policies.
163  */
164  rel = heap_open(rte->relid, NoLock);
165 
166  commandType = rt_index == root->resultRelation ?
167  root->commandType : CMD_SELECT;
168 
169  /*
170  * In some cases, we need to apply USING policies (which control the
171  * visibility of records) associated with multiple command types (see
172  * specific cases below).
173  *
174  * When considering the order in which to apply these USING policies, we
175  * prefer to apply higher privileged policies, those which allow the user
176  * to lock records (UPDATE and DELETE), first, followed by policies which
177  * don't (SELECT).
178  *
179  * Note that the optimizer is free to push down and reorder quals which
180  * use leakproof functions.
181  *
182  * In all cases, if there are no policy clauses allowing access to rows in
183  * the table for the specific type of operation, then a single
184  * always-false clause (a default-deny policy) will be added (see
185  * add_security_quals).
186  */
187 
188  /*
189  * For a SELECT, if UPDATE privileges are required (eg: the user has
190  * specified FOR [KEY] UPDATE/SHARE), then add the UPDATE USING quals
191  * first.
192  *
193  * This way, we filter out any records from the SELECT FOR SHARE/UPDATE
194  * which the user does not have access to via the UPDATE USING policies,
195  * similar to how we require normal UPDATE rights for these queries.
196  */
197  if (commandType == CMD_SELECT && rte->requiredPerms & ACL_UPDATE)
198  {
199  List *update_permissive_policies;
200  List *update_restrictive_policies;
201 
203  &update_permissive_policies,
204  &update_restrictive_policies);
205 
206  add_security_quals(rt_index,
207  update_permissive_policies,
208  update_restrictive_policies,
209  securityQuals,
210  hasSubLinks);
211  }
212 
213  /*
214  * For SELECT, UPDATE and DELETE, add security quals to enforce the USING
215  * policies. These security quals control access to existing table rows.
216  * Restrictive policies are combined together using AND, and permissive
217  * policies are combined together using OR.
218  */
219 
220  get_policies_for_relation(rel, commandType, user_id, &permissive_policies,
221  &restrictive_policies);
222 
223  if (commandType == CMD_SELECT ||
224  commandType == CMD_UPDATE ||
225  commandType == CMD_DELETE)
226  add_security_quals(rt_index,
227  permissive_policies,
228  restrictive_policies,
229  securityQuals,
230  hasSubLinks);
231 
232  /*
233  * Similar to above, during an UPDATE or DELETE, if SELECT rights are also
234  * required (eg: when a RETURNING clause exists, or the user has provided
235  * a WHERE clause which involves columns from the relation), we collect up
236  * CMD_SELECT policies and add them via add_security_quals first.
237  *
238  * This way, we filter out any records which are not visible through an
239  * ALL or SELECT USING policy.
240  */
241  if ((commandType == CMD_UPDATE || commandType == CMD_DELETE) &&
242  rte->requiredPerms & ACL_SELECT)
243  {
244  List *select_permissive_policies;
245  List *select_restrictive_policies;
246 
248  &select_permissive_policies,
249  &select_restrictive_policies);
250 
251  add_security_quals(rt_index,
252  select_permissive_policies,
253  select_restrictive_policies,
254  securityQuals,
255  hasSubLinks);
256  }
257 
258  /*
259  * For INSERT and UPDATE, add withCheckOptions to verify that any new
260  * records added are consistent with the security policies. This will use
261  * each policy's WITH CHECK clause, or its USING clause if no explicit
262  * WITH CHECK clause is defined.
263  */
264  if (commandType == CMD_INSERT || commandType == CMD_UPDATE)
265  {
266  /* This should be the target relation */
267  Assert(rt_index == root->resultRelation);
268 
269  add_with_check_options(rel, rt_index,
270  commandType == CMD_INSERT ?
272  permissive_policies,
273  restrictive_policies,
274  withCheckOptions,
275  hasSubLinks);
276 
277  /*
278  * Get and add ALL/SELECT policies, if SELECT rights are required for
279  * this relation (eg: when RETURNING is used). These are added as WCO
280  * policies rather than security quals to ensure that an error is
281  * raised if a policy is violated; otherwise, we might end up silently
282  * dropping rows to be added.
283  */
284  if (rte->requiredPerms & ACL_SELECT)
285  {
286  List *select_permissive_policies = NIL;
287  List *select_restrictive_policies = NIL;
288 
290  &select_permissive_policies,
291  &select_restrictive_policies);
292  add_with_check_options(rel, rt_index,
293  commandType == CMD_INSERT ?
295  select_permissive_policies,
296  select_restrictive_policies,
297  withCheckOptions,
298  hasSubLinks);
299  }
300 
301  /*
302  * For INSERT ... ON CONFLICT DO UPDATE we need additional policy
303  * checks for the UPDATE which may be applied to the same RTE.
304  */
305  if (commandType == CMD_INSERT &&
306  root->onConflict && root->onConflict->action == ONCONFLICT_UPDATE)
307  {
308  List *conflict_permissive_policies;
309  List *conflict_restrictive_policies;
310 
311  /* Get the policies that apply to the auxiliary UPDATE */
313  &conflict_permissive_policies,
314  &conflict_restrictive_policies);
315 
316  /*
317  * Enforce the USING clauses of the UPDATE policies using WCOs
318  * rather than security quals. This ensures that an error is
319  * raised if the conflicting row cannot be updated due to RLS,
320  * rather than the change being silently dropped.
321  */
322  add_with_check_options(rel, rt_index,
324  conflict_permissive_policies,
325  conflict_restrictive_policies,
326  withCheckOptions,
327  hasSubLinks);
328 
329  /*
330  * Get and add ALL/SELECT policies, as WCO_RLS_CONFLICT_CHECK WCOs
331  * to ensure they are considered when taking the UPDATE path of an
332  * INSERT .. ON CONFLICT DO UPDATE, if SELECT rights are required
333  * for this relation, also as WCO policies, again, to avoid
334  * silently dropping data. See above.
335  */
336  if (rte->requiredPerms & ACL_SELECT)
337  {
338  List *conflict_select_permissive_policies = NIL;
339  List *conflict_select_restrictive_policies = NIL;
340 
342  &conflict_select_permissive_policies,
343  &conflict_select_restrictive_policies);
344  add_with_check_options(rel, rt_index,
346  conflict_select_permissive_policies,
347  conflict_select_restrictive_policies,
348  withCheckOptions,
349  hasSubLinks);
350  }
351 
352  /* Enforce the WITH CHECK clauses of the UPDATE policies */
353  add_with_check_options(rel, rt_index,
355  conflict_permissive_policies,
356  conflict_restrictive_policies,
357  withCheckOptions,
358  hasSubLinks);
359  }
360  }
361 
362  heap_close(rel, NoLock);
363 
364  /*
365  * Mark this query as having row security, so plancache can invalidate it
366  * when necessary (eg: role changes)
367  */
368  *hasRowSecurity = true;
369 
370  return;
371 }
#define NIL
Definition: pg_list.h:69
static void add_with_check_options(Relation rel, int rt_index, WCOKind kind, List *permissive_policies, List *restrictive_policies, List **withCheckOptions, bool *hasSubLinks)
Definition: rowsecurity.c:656
Oid GetUserId(void)
Definition: miscinit.c:283
OnConflictExpr * onConflict
Definition: parsenodes.h:142
int resultRelation
Definition: parsenodes.h:120
AclMode requiredPerms
Definition: parsenodes.h:1019
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
static void add_security_quals(int rt_index, List *permissive_policies, List *restrictive_policies, List **securityQuals, bool *hasSubLinks)
Definition: rowsecurity.c:560
#define NoLock
Definition: lockdefs.h:34
OnConflictAction action
Definition: primnodes.h:1486
#define ACL_UPDATE
Definition: parsenodes.h:74
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
#define ACL_SELECT
Definition: parsenodes.h:73
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
CmdType commandType
Definition: parsenodes.h:110
int check_enable_rls(Oid relid, Oid checkAsUser, bool noError)
Definition: rls.c:53
#define Assert(condition)
Definition: c.h:675
#define RELKIND_RELATION
Definition: pg_class.h:160
Definition: rls.h:43
Definition: pg_list.h:45
CmdType
Definition: nodes.h:648
static void get_policies_for_relation(Relation relation, CmdType cmd, Oid user_id, List **permissive_policies, List **restrictive_policies)
Definition: rowsecurity.c:382

Variable Documentation

PGDLLIMPORT row_security_policy_hook_type row_security_policy_hook_permissive

Definition at line 94 of file rowsecurity.c.

Referenced by _PG_fini(), _PG_init(), and get_policies_for_relation().

PGDLLIMPORT row_security_policy_hook_type row_security_policy_hook_restrictive

Definition at line 95 of file rowsecurity.c.

Referenced by _PG_fini(), _PG_init(), and get_policies_for_relation().