PostgreSQL Source Code git master
Loading...
Searching...
No Matches
rowsecurity.c File Reference
#include "postgres.h"
#include "access/table.h"
#include "catalog/pg_class.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/pg_list.h"
#include "parser/parse_relation.h"
#include "rewrite/rewriteDefine.h"
#include "rewrite/rewriteManip.h"
#include "rewrite/rowsecurity.h"
#include "utils/acl.h"
#include "utils/rel.h"
#include "utils/rls.h"
Include dependency graph for rowsecurity.c:

Go to the source code of this file.

Macros

#define QUAL_FOR_WCO(policy)
 

Functions

static void get_policies_for_relation (Relation relation, CmdType cmd, Oid user_id, List **permissive_policies, List **restrictive_policies)
 
static void sort_policies_by_name (List *policies)
 
static int row_security_policy_cmp (const ListCell *a, const ListCell *b)
 
static void add_security_quals (int rt_index, List *permissive_policies, List *restrictive_policies, List **securityQuals, bool *hasSubLinks)
 
static void add_with_check_options (Relation rel, int rt_index, WCOKind kind, List *permissive_policies, List *restrictive_policies, List **withCheckOptions, bool *hasSubLinks, bool force_using)
 
static bool check_role_for_policy (ArrayType *policy_roles, Oid user_id)
 
void get_row_security_policies (Query *root, RangeTblEntry *rte, int rt_index, List **securityQuals, List **withCheckOptions, bool *hasRowSecurity, bool *hasSubLinks)
 

Variables

row_security_policy_hook_type row_security_policy_hook_permissive = NULL
 
row_security_policy_hook_type row_security_policy_hook_restrictive = NULL
 

Macro Definition Documentation

◆ QUAL_FOR_WCO

#define QUAL_FOR_WCO (   policy)
Value:
( !force_using && \
(policy)->with_check_qual != NULL ? \
(policy)->with_check_qual : (policy)->qual )
static int fb(int x)

Function Documentation

◆ add_security_quals()

static void add_security_quals ( int  rt_index,
List permissive_policies,
List restrictive_policies,
List **  securityQuals,
bool hasSubLinks 
)
static

Definition at line 740 of file rowsecurity.c.

745{
746 ListCell *item;
749
750 /*
751 * First collect up the permissive quals. If we do not find any
752 * permissive policies then no rows are visible (this is handled below).
753 */
754 foreach(item, permissive_policies)
755 {
757
758 if (policy->qual != NULL)
759 {
761 copyObject(policy->qual));
762 *hasSubLinks |= policy->hassublinks;
763 }
764 }
765
766 /*
767 * We must have permissive quals, always, or no rows are visible.
768 *
769 * If we do not, then we simply return a single 'false' qual which results
770 * in no rows being visible.
771 */
772 if (permissive_quals != NIL)
773 {
774 /*
775 * We now know that permissive policies exist, so we can now add
776 * security quals based on the USING clauses from the restrictive
777 * policies. Since these need to be combined together using AND, we
778 * can just add them one at a time.
779 */
780 foreach(item, restrictive_policies)
781 {
783 Expr *qual;
784
785 if (policy->qual != NULL)
786 {
787 qual = copyObject(policy->qual);
788 ChangeVarNodes((Node *) qual, 1, rt_index, 0);
789
791 *hasSubLinks |= policy->hassublinks;
792 }
793 }
794
795 /*
796 * Then add a single security qual combining together the USING
797 * clauses from all the permissive policies using OR.
798 */
801 else
803
804 ChangeVarNodes((Node *) rowsec_expr, 1, rt_index, 0);
806 }
807 else
808
809 /*
810 * A permissive policy must exist for rows to be visible at all.
811 * Therefore, if there were no permissive policies found, return a
812 * single always-false clause.
813 */
816 sizeof(bool), BoolGetDatum(false),
817 false, true));
818}
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_append_unique(List *list, void *datum)
Definition list.c:1343
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition makefuncs.c:420
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition makefuncs.c:350
#define copyObject(obj)
Definition nodes.h:230
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define linitial(l)
Definition pg_list.h:178
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
#define InvalidOid
@ OR_EXPR
Definition primnodes.h:945
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
Definition pg_list.h:54
Definition nodes.h:133

References BoolGetDatum(), ChangeVarNodes(), copyObject, fb(), InvalidOid, lappend(), lfirst, linitial, list_append_unique(), list_length(), makeBoolExpr(), makeConst(), NIL, and OR_EXPR.

Referenced by get_row_security_policies().

◆ add_with_check_options()

static void add_with_check_options ( Relation  rel,
int  rt_index,
WCOKind  kind,
List permissive_policies,
List restrictive_policies,
List **  withCheckOptions,
bool hasSubLinks,
bool  force_using 
)
static

Definition at line 836 of file rowsecurity.c.

844{
845 ListCell *item;
847
848#define QUAL_FOR_WCO(policy) \
849 ( !force_using && \
850 (policy)->with_check_qual != NULL ? \
851 (policy)->with_check_qual : (policy)->qual )
852
853 /*
854 * First collect up the permissive policy clauses, similar to
855 * add_security_quals.
856 */
857 foreach(item, permissive_policies)
858 {
860 Expr *qual = QUAL_FOR_WCO(policy);
861
862 if (qual != NULL)
863 {
865 *hasSubLinks |= policy->hassublinks;
866 }
867 }
868
869 /*
870 * There must be at least one permissive qual found or no rows are allowed
871 * to be added. This is the same as in add_security_quals.
872 *
873 * If there are no permissive_quals then we fall through and return a
874 * single 'false' WCO, preventing all new rows.
875 */
876 if (permissive_quals != NIL)
877 {
878 /*
879 * Add a single WithCheckOption for all the permissive policy clauses,
880 * combining them together using OR. This check has no policy name,
881 * since if the check fails it means that no policy granted permission
882 * to perform the update, rather than any particular policy being
883 * violated.
884 */
886
888 wco->kind = kind;
889 wco->relname = pstrdup(RelationGetRelationName(rel));
890 wco->polname = NULL;
891 wco->cascaded = false;
892
894 wco->qual = (Node *) linitial(permissive_quals);
895 else
896 wco->qual = (Node *) makeBoolExpr(OR_EXPR, permissive_quals, -1);
897
898 ChangeVarNodes(wco->qual, 1, rt_index, 0);
899
901
902 /*
903 * Now add WithCheckOptions for each of the restrictive policy clauses
904 * (which will be combined together using AND). We use a separate
905 * WithCheckOption for each restrictive policy to allow the policy
906 * name to be included in error reports if the policy is violated.
907 */
908 foreach(item, restrictive_policies)
909 {
911 Expr *qual = QUAL_FOR_WCO(policy);
912
913 if (qual != NULL)
914 {
915 qual = copyObject(qual);
916 ChangeVarNodes((Node *) qual, 1, rt_index, 0);
917
919 wco->kind = kind;
920 wco->relname = pstrdup(RelationGetRelationName(rel));
921 wco->polname = pstrdup(policy->policy_name);
922 wco->qual = (Node *) qual;
923 wco->cascaded = false;
924
926 *hasSubLinks |= policy->hassublinks;
927 }
928 }
929 }
930 else
931 {
932 /*
933 * If there were no policy clauses to check new data, add a single
934 * always-false WCO (a default-deny policy).
935 */
937
939 wco->kind = kind;
940 wco->relname = pstrdup(RelationGetRelationName(rel));
941 wco->polname = NULL;
942 wco->qual = (Node *) makeConst(BOOLOID, -1, InvalidOid,
943 sizeof(bool), BoolGetDatum(false),
944 false, true);
945 wco->cascaded = false;
946
948 }
949}
char * pstrdup(const char *in)
Definition mcxt.c:1910
#define makeNode(_type_)
Definition nodes.h:159
#define RelationGetRelationName(relation)
Definition rel.h:550
#define QUAL_FOR_WCO(policy)

References BoolGetDatum(), ChangeVarNodes(), copyObject, fb(), InvalidOid, lappend(), lfirst, linitial, list_append_unique(), list_length(), makeBoolExpr(), makeConst(), makeNode, NIL, OR_EXPR, pstrdup(), QUAL_FOR_WCO, and RelationGetRelationName.

Referenced by get_row_security_policies().

◆ check_role_for_policy()

static bool check_role_for_policy ( ArrayType policy_roles,
Oid  user_id 
)
static

Definition at line 956 of file rowsecurity.c.

957{
958 int i;
959 Oid *roles = (Oid *) ARR_DATA_PTR(policy_roles);
960
961 /* Quick fall-thru for policies applied to all roles */
962 if (roles[0] == ACL_ID_PUBLIC)
963 return true;
964
965 for (i = 0; i < ARR_DIMS(policy_roles)[0]; i++)
966 {
967 if (has_privs_of_role(user_id, roles[i]))
968 return true;
969 }
970
971 return false;
972}
bool has_privs_of_role(Oid member, Oid role)
Definition acl.c:5317
#define ACL_ID_PUBLIC
Definition acl.h:46
#define ARR_DATA_PTR(a)
Definition array.h:322
#define ARR_DIMS(a)
Definition array.h:294
int i
Definition isn.c:77
unsigned int Oid

References ACL_ID_PUBLIC, ARR_DATA_PTR, ARR_DIMS, fb(), has_privs_of_role(), and i.

Referenced by get_policies_for_relation().

◆ get_policies_for_relation()

static void get_policies_for_relation ( Relation  relation,
CmdType  cmd,
Oid  user_id,
List **  permissive_policies,
List **  restrictive_policies 
)
static

Definition at line 581 of file rowsecurity.c.

584{
585 ListCell *item;
586
589
590 /* First find all internal policies for the relation. */
591 foreach(item, relation->rd_rsdesc->policies)
592 {
593 bool cmd_matches = false;
595
596 /* Always add ALL policies, if they exist. */
597 if (policy->polcmd == '*')
598 cmd_matches = true;
599 else
600 {
601 /* Check whether the policy applies to the specified command type */
602 switch (cmd)
603 {
604 case CMD_SELECT:
605 if (policy->polcmd == ACL_SELECT_CHR)
606 cmd_matches = true;
607 break;
608 case CMD_INSERT:
609 if (policy->polcmd == ACL_INSERT_CHR)
610 cmd_matches = true;
611 break;
612 case CMD_UPDATE:
613 if (policy->polcmd == ACL_UPDATE_CHR)
614 cmd_matches = true;
615 break;
616 case CMD_DELETE:
617 if (policy->polcmd == ACL_DELETE_CHR)
618 cmd_matches = true;
619 break;
620 case CMD_MERGE:
621
622 /*
623 * We do not support a separate policy for MERGE command.
624 * Instead it derives from the policies defined for other
625 * commands.
626 */
627 break;
628 default:
629 elog(ERROR, "unrecognized policy command type %d",
630 (int) cmd);
631 break;
632 }
633 }
634
635 /*
636 * Add this policy to the relevant list of policies if it applies to
637 * the specified role.
638 */
639 if (cmd_matches && check_role_for_policy(policy->roles, user_id))
640 {
641 if (policy->permissive)
643 else
645 }
646 }
647
648 /*
649 * We sort restrictive policies by name so that any WCOs they generate are
650 * checked in a well-defined order.
651 */
653
654 /*
655 * Then add any permissive or restrictive policies defined by extensions.
656 * These are simply appended to the lists of internal policies, if they
657 * apply to the specified role.
658 */
660 {
662 (*row_security_policy_hook_restrictive) (cmd, relation);
663
664 /*
665 * As with built-in restrictive policies, we sort any hook-provided
666 * restrictive policies by name also. Note that we also intentionally
667 * always check all built-in restrictive policies, in name order,
668 * before checking restrictive policies added by hooks, in name order.
669 */
671
672 foreach(item, hook_policies)
673 {
675
676 if (check_role_for_policy(policy->roles, user_id))
678 }
679 }
680
682 {
684 (*row_security_policy_hook_permissive) (cmd, relation);
685
686 foreach(item, hook_policies)
687 {
689
690 if (check_role_for_policy(policy->roles, user_id))
692 }
693 }
694}
#define ACL_SELECT_CHR
Definition acl.h:138
#define ACL_DELETE_CHR
Definition acl.h:140
#define ACL_INSERT_CHR
Definition acl.h:137
#define ACL_UPDATE_CHR
Definition acl.h:139
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
@ CMD_MERGE
Definition nodes.h:277
@ CMD_INSERT
Definition nodes.h:275
@ CMD_DELETE
Definition nodes.h:276
@ CMD_UPDATE
Definition nodes.h:274
@ CMD_SELECT
Definition nodes.h:273
row_security_policy_hook_type row_security_policy_hook_permissive
Definition rowsecurity.c:86
static bool check_role_for_policy(ArrayType *policy_roles, Oid user_id)
row_security_policy_hook_type row_security_policy_hook_restrictive
Definition rowsecurity.c:87
static void sort_policies_by_name(List *policies)
struct RowSecurityDesc * rd_rsdesc
Definition rel.h:119

References ACL_DELETE_CHR, ACL_INSERT_CHR, ACL_SELECT_CHR, ACL_UPDATE_CHR, check_role_for_policy(), CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_SELECT, CMD_UPDATE, elog, ERROR, fb(), lappend(), lfirst, NIL, RowSecurityDesc::policies, RelationData::rd_rsdesc, row_security_policy_hook_permissive, row_security_policy_hook_restrictive, and sort_policies_by_name().

Referenced by get_row_security_policies().

◆ get_row_security_policies()

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

Definition at line 98 of file rowsecurity.c.

101{
102 Oid user_id;
103 int rls_status;
104 Relation rel;
105 CmdType commandType;
109
110 /* Defaults for the return values */
113 *hasRowSecurity = false;
114 *hasSubLinks = false;
115
116 Assert(rte->rtekind == RTE_RELATION);
117
118 /* If this is not a normal relation, just return immediately */
119 if (rte->relkind != RELKIND_RELATION &&
120 rte->relkind != RELKIND_PARTITIONED_TABLE)
121 return;
122
123 perminfo = getRTEPermissionInfo(root->rteperminfos, rte);
124
125 /* Switch to checkAsUser if it's set */
126 user_id = OidIsValid(perminfo->checkAsUser) ?
127 perminfo->checkAsUser : GetUserId();
128
129 /* Determine the state of RLS for this, pass checkAsUser explicitly */
130 rls_status = check_enable_rls(rte->relid, perminfo->checkAsUser, false);
131
132 /* If there is no RLS on this table at all, nothing to do */
133 if (rls_status == RLS_NONE)
134 return;
135
136 /*
137 * RLS_NONE_ENV means we are not doing any RLS now, but that may change
138 * with changes to the environment, so we mark it as hasRowSecurity to
139 * force a re-plan when the environment changes.
140 */
142 {
143 /*
144 * Indicate that this query may involve RLS and must therefore be
145 * replanned if the environment changes (GUCs, role), but we are not
146 * adding anything here.
147 */
148 *hasRowSecurity = true;
149
150 return;
151 }
152
153 /*
154 * RLS is enabled for this relation.
155 *
156 * Get the security policies that should be applied, based on the command
157 * type. Note that if this isn't the target relation, we actually want
158 * the relation's SELECT policies, regardless of the query command type,
159 * for example in UPDATE t1 ... FROM t2 we need to apply t1's UPDATE
160 * policies and t2's SELECT policies.
161 */
162 rel = table_open(rte->relid, NoLock);
163
164 commandType = rt_index == root->resultRelation ?
165 root->commandType : CMD_SELECT;
166
167 /*
168 * In some cases, we need to apply USING policies (which control the
169 * visibility of records) associated with multiple command types (see
170 * specific cases below).
171 *
172 * When considering the order in which to apply these USING policies, we
173 * prefer to apply higher privileged policies, those which allow the user
174 * to lock records (UPDATE and DELETE), first, followed by policies which
175 * don't (SELECT).
176 *
177 * Note that the optimizer is free to push down and reorder quals which
178 * use leakproof functions.
179 *
180 * In all cases, if there are no policy clauses allowing access to rows in
181 * the table for the specific type of operation, then a single
182 * always-false clause (a default-deny policy) will be added (see
183 * add_security_quals).
184 */
185
186 /*
187 * For a SELECT, if UPDATE privileges are required (eg: the user has
188 * specified FOR [KEY] UPDATE/SHARE), then add the UPDATE USING quals
189 * first.
190 *
191 * This way, we filter out any records from the SELECT FOR SHARE/UPDATE
192 * which the user does not have access to via the UPDATE USING policies,
193 * similar to how we require normal UPDATE rights for these queries.
194 */
195 if (commandType == CMD_SELECT && perminfo->requiredPerms & ACL_UPDATE)
196 {
199
203
204 add_security_quals(rt_index,
209 }
210
211 /*
212 * For SELECT, UPDATE and DELETE, add security quals to enforce the USING
213 * policies. These security quals control access to existing table rows.
214 * Restrictive policies are combined together using AND, and permissive
215 * policies are combined together using OR.
216 */
217
218 get_policies_for_relation(rel, commandType, user_id, &permissive_policies,
220
221 if (commandType == CMD_SELECT ||
222 commandType == CMD_UPDATE ||
223 commandType == CMD_DELETE)
224 add_security_quals(rt_index,
229
230 /*
231 * Similar to above, during an UPDATE, DELETE, or MERGE, if SELECT rights
232 * are also required (eg: when a RETURNING clause exists, or the user has
233 * provided a WHERE clause which involves columns from the relation), we
234 * collect up CMD_SELECT policies and add them via add_security_quals
235 * first.
236 *
237 * This way, we filter out any records which are not visible through an
238 * ALL or SELECT USING policy.
239 */
240 if ((commandType == CMD_UPDATE || commandType == CMD_DELETE ||
241 commandType == CMD_MERGE) &&
242 perminfo->requiredPerms & ACL_SELECT)
243 {
246
250
251 add_security_quals(rt_index,
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 ?
276 false);
277
278 /*
279 * Get and add ALL/SELECT policies, if SELECT rights are required for
280 * this relation (eg: when RETURNING is used). These are added as WCO
281 * policies rather than security quals to ensure that an error is
282 * raised if a policy is violated; otherwise, we might end up silently
283 * dropping rows to be added.
284 */
285 if (perminfo->requiredPerms & ACL_SELECT)
286 {
289
293 add_with_check_options(rel, rt_index,
294 commandType == CMD_INSERT ?
300 true);
301 }
302
303 /*
304 * For INSERT ... ON CONFLICT DO SELECT/UPDATE we need additional
305 * policy checks for the SELECT/UPDATE which may be applied to the
306 * same RTE.
307 */
308 if (commandType == CMD_INSERT && root->onConflict &&
309 (root->onConflict->action == ONCONFLICT_UPDATE ||
310 root->onConflict->action == ONCONFLICT_SELECT))
311 {
316
317 if (perminfo->requiredPerms & ACL_UPDATE)
318 {
319 /*
320 * Get the policies that apply to the auxiliary UPDATE or
321 * SELECT FOR UPDATE/SHARE.
322 */
326
327 /*
328 * Enforce the USING clauses of the UPDATE policies using WCOs
329 * rather than security quals. This ensures that an error is
330 * raised if the conflicting row cannot be updated/locked due
331 * to RLS, rather than the change being silently dropped.
332 */
333 add_with_check_options(rel, rt_index,
339 true);
340 }
341
342 /*
343 * Get and add ALL/SELECT policies, as WCO_RLS_CONFLICT_CHECK WCOs
344 * to ensure they are considered when taking the SELECT/UPDATE
345 * path of an INSERT .. ON CONFLICT, if SELECT rights are required
346 * for this relation, also as WCO policies, again, to avoid
347 * silently dropping data. See above.
348 */
349 if (perminfo->requiredPerms & ACL_SELECT)
350 {
354 add_with_check_options(rel, rt_index,
360 true);
361 }
362
363 /*
364 * For INSERT .. ON CONFLICT DO UPDATE, add additional policies to
365 * be checked when the auxiliary UPDATE is executed.
366 */
367 if (root->onConflict->action == ONCONFLICT_UPDATE)
368 {
369 /* Enforce the WITH CHECK clauses of the UPDATE policies */
370 add_with_check_options(rel, rt_index,
376 false);
377
378 /*
379 * Add ALL/SELECT policies as WCO_RLS_UPDATE_CHECK WCOs, to
380 * ensure that the final updated row is visible when taking
381 * the UPDATE path of an INSERT .. ON CONFLICT, if SELECT
382 * rights are required for this relation.
383 */
384 if (perminfo->requiredPerms & ACL_SELECT)
385 add_with_check_options(rel, rt_index,
391 true);
392 }
393 }
394 }
395
396 /*
397 * UPDATE/DELETE FOR PORTION OF may insert leftover rows to preserve the
398 * portions of the old row not covered by the target range. Those hidden
399 * inserts go through ExecInsert(), so they need the same INSERT RLS WITH
400 * CHECK options as ordinary INSERTs. SELECT rights are never needed for
401 * the leftover rows, because they are not considered by RETURNING.
402 */
403 if (root->forPortionOf != NULL && rt_index == root->resultRelation &&
404 (commandType == CMD_UPDATE || commandType == CMD_DELETE))
405 {
408
412 add_with_check_options(rel, rt_index,
418 false);
419 }
420
421 /*
422 * FOR MERGE, we fetch policies for UPDATE, DELETE and INSERT (and ALL)
423 * and set them up so that we can enforce the appropriate policy depending
424 * on the final action we take.
425 *
426 * We already fetched the SELECT policies above, to check existing rows,
427 * but we must also check that new rows created by INSERT/UPDATE actions
428 * are visible, if SELECT rights are required. For INSERT actions, we only
429 * do this if RETURNING is specified, to be consistent with a plain INSERT
430 * command, which can only require SELECT rights when RETURNING is used.
431 *
432 * We don't push the UPDATE/DELETE USING quals to the RTE because we don't
433 * really want to apply them while scanning the relation since we don't
434 * know whether we will be doing an UPDATE or a DELETE at the end. We
435 * apply the respective policy once we decide the final action on the
436 * target tuple.
437 *
438 * XXX We are setting up USING quals as WITH CHECK. If RLS prohibits
439 * UPDATE/DELETE on the target row, we shall throw an error instead of
440 * silently ignoring the row. This is different than how normal
441 * UPDATE/DELETE works and more in line with INSERT ON CONFLICT DO
442 * SELECT/UPDATE handling.
443 */
444 if (commandType == CMD_MERGE)
445 {
454
455 /*
456 * Fetch the UPDATE policies and set them up to execute on the
457 * existing target row before doing UPDATE.
458 */
462
463 /*
464 * WCO_RLS_MERGE_UPDATE_CHECK is used to check UPDATE USING quals on
465 * the existing target row.
466 */
467 add_with_check_options(rel, rt_index,
473 true);
474
475 /* Enforce the WITH CHECK clauses of the UPDATE policies */
476 add_with_check_options(rel, rt_index,
482 false);
483
484 /*
485 * Add ALL/SELECT policies as WCO_RLS_UPDATE_CHECK WCOs, to ensure
486 * that the updated row is visible when executing an UPDATE action, if
487 * SELECT rights are required for this relation.
488 */
489 if (perminfo->requiredPerms & ACL_SELECT)
490 {
494 add_with_check_options(rel, rt_index,
500 true);
501 }
502
503 /*
504 * Fetch the DELETE policies and set them up to execute on the
505 * existing target row before doing DELETE.
506 */
510
511 /*
512 * WCO_RLS_MERGE_DELETE_CHECK is used to check DELETE USING quals on
513 * the existing target row.
514 */
515 add_with_check_options(rel, rt_index,
521 true);
522
523 /*
524 * No special handling is required for INSERT policies. They will be
525 * checked and enforced during ExecInsert(). But we must add them to
526 * withCheckOptions.
527 */
531
532 add_with_check_options(rel, rt_index,
538 false);
539
540 /*
541 * Add ALL/SELECT policies as WCO_RLS_INSERT_CHECK WCOs, to ensure
542 * that the inserted row is visible when executing an INSERT action,
543 * if RETURNING is specified and SELECT rights are required for this
544 * relation.
545 */
546 if (perminfo->requiredPerms & ACL_SELECT && root->returningList)
547 add_with_check_options(rel, rt_index,
553 true);
554 }
555
556 table_close(rel, NoLock);
557
558 /*
559 * Copy checkAsUser to the row security quals and WithCheckOption checks,
560 * in case they contain any subqueries referring to other relations.
561 */
562 setRuleCheckAsUser((Node *) *securityQuals, perminfo->checkAsUser);
564
565 /*
566 * Mark this query as having row security, so plancache can invalidate it
567 * when necessary (eg: role changes)
568 */
569 *hasRowSecurity = true;
570}
#define Assert(condition)
Definition c.h:1002
#define OidIsValid(objectId)
Definition c.h:917
#define false
return true
Definition isn.c:130
#define NoLock
Definition lockdefs.h:34
Oid GetUserId(void)
Definition miscinit.c:470
@ ONCONFLICT_SELECT
Definition nodes.h:429
@ ONCONFLICT_UPDATE
Definition nodes.h:428
CmdType
Definition nodes.h:271
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
@ WCO_RLS_MERGE_UPDATE_CHECK
@ WCO_RLS_CONFLICT_CHECK
@ WCO_RLS_INSERT_CHECK
@ WCO_RLS_UPDATE_CHECK
@ WCO_RLS_MERGE_DELETE_CHECK
#define ACL_UPDATE
Definition parsenodes.h:78
@ RTE_RELATION
#define ACL_SELECT
Definition parsenodes.h:77
tree ctl root
Definition radixtree.h:1857
void setRuleCheckAsUser(Node *node, Oid userid)
int check_enable_rls(Oid relid, Oid checkAsUser, bool noError)
Definition rls.c:52
@ RLS_NONE
Definition rls.h:43
@ RLS_NONE_ENV
Definition rls.h:44
static void add_with_check_options(Relation rel, int rt_index, WCOKind kind, List *permissive_policies, List *restrictive_policies, List **withCheckOptions, bool *hasSubLinks, bool force_using)
static void get_policies_for_relation(Relation relation, CmdType cmd, Oid user_id, List **permissive_policies, List **restrictive_policies)
static void add_security_quals(int rt_index, List *permissive_policies, List *restrictive_policies, List **securityQuals, bool *hasSubLinks)
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40

References ACL_SELECT, ACL_UPDATE, add_security_quals(), add_with_check_options(), Assert, check_enable_rls(), CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_SELECT, CMD_UPDATE, fb(), get_policies_for_relation(), getRTEPermissionInfo(), GetUserId(), NIL, NoLock, OidIsValid, ONCONFLICT_SELECT, ONCONFLICT_UPDATE, RLS_NONE, RLS_NONE_ENV, root, RTE_RELATION, setRuleCheckAsUser(), table_close(), table_open(), WCO_RLS_CONFLICT_CHECK, WCO_RLS_INSERT_CHECK, WCO_RLS_MERGE_DELETE_CHECK, WCO_RLS_MERGE_UPDATE_CHECK, and WCO_RLS_UPDATE_CHECK.

Referenced by fireRIRrules().

◆ row_security_policy_cmp()

static int row_security_policy_cmp ( const ListCell a,
const ListCell b 
)
static

Definition at line 714 of file rowsecurity.c.

715{
716 const RowSecurityPolicy *pa = (const RowSecurityPolicy *) lfirst(a);
717 const RowSecurityPolicy *pb = (const RowSecurityPolicy *) lfirst(b);
718
719 /* Guard against NULL policy names from extensions */
720 if (pa->policy_name == NULL)
721 return pb->policy_name == NULL ? 0 : 1;
722 if (pb->policy_name == NULL)
723 return -1;
724
725 return strcmp(pa->policy_name, pb->policy_name);
726}
int b
Definition isn.c:74
int a
Definition isn.c:73

References a, b, fb(), lfirst, and RowSecurityPolicy::policy_name.

Referenced by sort_policies_by_name().

◆ sort_policies_by_name()

static void sort_policies_by_name ( List policies)
static

Definition at line 705 of file rowsecurity.c.

706{
708}
void list_sort(List *list, list_sort_comparator cmp)
Definition list.c:1674
static int row_security_policy_cmp(const ListCell *a, const ListCell *b)

References list_sort(), and row_security_policy_cmp().

Referenced by get_policies_for_relation().

Variable Documentation

◆ row_security_policy_hook_permissive

row_security_policy_hook_type row_security_policy_hook_permissive = NULL

Definition at line 86 of file rowsecurity.c.

Referenced by _PG_init(), and get_policies_for_relation().

◆ row_security_policy_hook_restrictive

row_security_policy_hook_type row_security_policy_hook_restrictive = NULL

Definition at line 87 of file rowsecurity.c.

Referenced by _PG_init(), and get_policies_for_relation().