PostgreSQL Source Code  git master
dml.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/tupdesc.h"
#include "catalog/catalog.h"
#include "catalog/heap.h"
#include "catalog/dependency.h"
#include "catalog/pg_attribute.h"
#include "catalog/pg_class.h"
#include "catalog/pg_inherits.h"
#include "commands/seclabel.h"
#include "commands/tablecmds.h"
#include "executor/executor.h"
#include "nodes/bitmapset.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "sepgsql.h"
Include dependency graph for dml.c:

Go to the source code of this file.

Functions

static Bitmapsetfixup_whole_row_references (Oid relOid, Bitmapset *columns)
 
static Bitmapsetfixup_inherited_columns (Oid parentId, Oid childId, Bitmapset *columns)
 
static bool check_relation_privileges (Oid relOid, Bitmapset *selected, Bitmapset *inserted, Bitmapset *updated, uint32 required, bool abort_on_violation)
 
bool sepgsql_dml_privileges (List *rangeTabls, bool abort_on_violation)
 

Function Documentation

◆ check_relation_privileges()

static bool check_relation_privileges ( Oid  relOid,
Bitmapset selected,
Bitmapset inserted,
Bitmapset updated,
uint32  required,
bool  abort_on_violation 
)
static

Definition at line 143 of file dml.c.

References Assert, attnum, bms_first_member(), bms_is_member(), bms_union(), ereport, errcode(), errmsg(), ERROR, FirstLowInvalidHeapAttributeNumber, fixup_whole_row_references(), get_rel_relkind(), getObjectDescription(), getObjectIdentity(), IsCatalogRelationOid(), pfree(), relkind, SEPG_CLASS_DB_COLUMN, SEPG_CLASS_DB_SEQUENCE, SEPG_CLASS_DB_TABLE, SEPG_CLASS_DB_VIEW, SEPG_DB_COLUMN__INSERT, SEPG_DB_COLUMN__SELECT, SEPG_DB_COLUMN__UPDATE, SEPG_DB_SEQUENCE__GET_VALUE, SEPG_DB_TABLE__DELETE, SEPG_DB_TABLE__INSERT, SEPG_DB_TABLE__SELECT, SEPG_DB_TABLE__UPDATE, SEPG_DB_VIEW__EXPAND, sepgsql_avc_check_perms(), and sepgsql_getenforce().

Referenced by sepgsql_dml_privileges().

149 {
150  ObjectAddress object;
151  char *audit_name;
152  Bitmapset *columns;
153  int index;
154  char relkind = get_rel_relkind(relOid);
155  bool result = true;
156 
157  /*
158  * Hardwired Policies: SE-PostgreSQL enforces - clients cannot modify
159  * system catalogs using DMLs - clients cannot reference/modify toast
160  * relations using DMLs
161  */
162  if (sepgsql_getenforce() > 0)
163  {
166  SEPG_DB_TABLE__DELETE)) != 0 &&
167  IsCatalogRelationOid(relOid))
168  ereport(ERROR,
169  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
170  errmsg("SELinux: hardwired security policy violation")));
171 
172  if (relkind == RELKIND_TOASTVALUE)
173  ereport(ERROR,
174  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
175  errmsg("SELinux: hardwired security policy violation")));
176  }
177 
178  /*
179  * Check permissions on the relation
180  */
181  object.classId = RelationRelationId;
182  object.objectId = relOid;
183  object.objectSubId = 0;
184  audit_name = getObjectIdentity(&object);
185  switch (relkind)
186  {
187  case RELKIND_RELATION:
188  case RELKIND_PARTITIONED_TABLE:
189  result = sepgsql_avc_check_perms(&object,
191  required,
192  audit_name,
193  abort_on_violation);
194  break;
195 
196  case RELKIND_SEQUENCE:
198 
200  result = sepgsql_avc_check_perms(&object,
203  audit_name,
204  abort_on_violation);
205  break;
206 
207  case RELKIND_VIEW:
208  result = sepgsql_avc_check_perms(&object,
211  audit_name,
212  abort_on_violation);
213  break;
214 
215  default:
216  /* nothing to be checked */
217  break;
218  }
219  pfree(audit_name);
220 
221  /*
222  * Only columns owned by relations shall be checked
223  */
224  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE)
225  return true;
226 
227  /*
228  * Check permissions on the columns
229  */
230  selected = fixup_whole_row_references(relOid, selected);
231  inserted = fixup_whole_row_references(relOid, inserted);
232  updated = fixup_whole_row_references(relOid, updated);
233  columns = bms_union(selected, bms_union(inserted, updated));
234 
235  while ((index = bms_first_member(columns)) >= 0)
236  {
238  uint32 column_perms = 0;
239 
240  if (bms_is_member(index, selected))
241  column_perms |= SEPG_DB_COLUMN__SELECT;
242  if (bms_is_member(index, inserted))
243  {
245  column_perms |= SEPG_DB_COLUMN__INSERT;
246  }
247  if (bms_is_member(index, updated))
248  {
250  column_perms |= SEPG_DB_COLUMN__UPDATE;
251  }
252  if (column_perms == 0)
253  continue;
254 
255  /* obtain column's permission */
256  attnum = index + FirstLowInvalidHeapAttributeNumber;
257 
258  object.classId = RelationRelationId;
259  object.objectId = relOid;
260  object.objectSubId = attnum;
261  audit_name = getObjectDescription(&object);
262 
263  result = sepgsql_avc_check_perms(&object,
265  column_perms,
266  audit_name,
267  abort_on_violation);
268  pfree(audit_name);
269 
270  if (!result)
271  return result;
272  }
273  return true;
274 }
#define SEPG_DB_VIEW__EXPAND
Definition: sepgsql.h:212
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:996
#define SEPG_CLASS_DB_COLUMN
Definition: sepgsql.h:49
static Bitmapset * fixup_whole_row_references(Oid relOid, Bitmapset *columns)
Definition: dml.c:39
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1805
bool sepgsql_avc_check_perms(const ObjectAddress *tobject, uint16 tclass, uint32 required, const char *audit_name, bool abort_on_violation)
Definition: uavc.c:423
#define SEPG_DB_TABLE__DELETE
Definition: sepgsql.h:146
int errcode(int sqlerrcode)
Definition: elog.c:570
bool sepgsql_getenforce(void)
Definition: selinux.c:648
#define SEPG_DB_COLUMN__INSERT
Definition: sepgsql.h:177
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
char * getObjectDescription(const ObjectAddress *object)
char relkind
Definition: pg_class.h:81
bool IsCatalogRelationOid(Oid relid)
Definition: catalog.c:117
#define SEPG_DB_TABLE__SELECT
Definition: sepgsql.h:143
Definition: type.h:89
void pfree(void *pointer)
Definition: mcxt.c:1056
#define SEPG_CLASS_DB_SEQUENCE
Definition: sepgsql.h:47
#define ERROR
Definition: elog.h:43
#define SEPG_DB_COLUMN__SELECT
Definition: sepgsql.h:175
#define SEPG_DB_SEQUENCE__GET_VALUE
Definition: sepgsql.h:155
char * getObjectIdentity(const ObjectAddress *object)
#define SEPG_DB_TABLE__UPDATE
Definition: sepgsql.h:144
#define SEPG_DB_TABLE__INSERT
Definition: sepgsql.h:145
unsigned int uint32
Definition: c.h:358
#define ereport(elevel, rest)
Definition: elog.h:141
#define SEPG_DB_COLUMN__UPDATE
Definition: sepgsql.h:176
#define SEPG_CLASS_DB_VIEW
Definition: sepgsql.h:53
int16 attnum
Definition: pg_attribute.h:79
#define SEPG_CLASS_DB_TABLE
Definition: sepgsql.h:46
#define Assert(condition)
Definition: c.h:732
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
int errmsg(const char *fmt,...)
Definition: elog.c:784
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
int16 AttrNumber
Definition: attnum.h:21

◆ fixup_inherited_columns()

static Bitmapset* fixup_inherited_columns ( Oid  parentId,
Oid  childId,
Bitmapset columns 
)
static

Definition at line 94 of file dml.c.

References attname, bms_add_member(), bms_next_member(), elog, ERROR, FirstLowInvalidHeapAttributeNumber, get_attname(), get_attnum(), InvalidAttrNumber, and pfree().

Referenced by sepgsql_dml_privileges().

95 {
96  Bitmapset *result = NULL;
97  int index;
98 
99  /*
100  * obviously, no need to do anything here
101  */
102  if (parentId == childId)
103  return columns;
104 
105  index = -1;
106  while ((index = bms_next_member(columns, index)) >= 0)
107  {
108  /* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
110  char *attname;
111 
112  /*
113  * whole-row-reference shall be fixed-up later
114  */
115  if (attno == InvalidAttrNumber)
116  {
117  result = bms_add_member(result, index);
118  continue;
119  }
120 
121  attname = get_attname(parentId, attno, false);
122  attno = get_attnum(childId, attname);
123  if (attno == InvalidAttrNumber)
124  elog(ERROR, "cache lookup failed for attribute %s of relation %u",
125  attname, childId);
126 
127  result = bms_add_member(result,
128  attno - FirstLowInvalidHeapAttributeNumber);
129 
130  pfree(attname);
131  }
132 
133  return result;
134 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
Definition: type.h:89
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:806
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define InvalidAttrNumber
Definition: attnum.h:23
#define elog(elevel,...)
Definition: elog.h:226
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:775
int16 AttrNumber
Definition: attnum.h:21

◆ fixup_whole_row_references()

static Bitmapset* fixup_whole_row_references ( Oid  relOid,
Bitmapset columns 
)
static

Definition at line 39 of file dml.c.

References ATTNUM, bms_add_member(), bms_copy(), bms_del_member(), bms_is_member(), elog, ERROR, FirstLowInvalidHeapAttributeNumber, GETSTRUCT, HeapTupleIsValid, Int16GetDatum, InvalidAttrNumber, ObjectIdGetDatum, ReleaseSysCache(), relnatts, RELOID, SearchSysCache1(), and SearchSysCache2().

Referenced by check_relation_privileges().

40 {
41  Bitmapset *result;
42  HeapTuple tuple;
43  AttrNumber natts;
44  AttrNumber attno;
45  int index;
46 
47  /* if no whole of row references, do not anything */
49  if (!bms_is_member(index, columns))
50  return columns;
51 
52  /* obtain number of attributes */
53  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relOid));
54  if (!HeapTupleIsValid(tuple))
55  elog(ERROR, "cache lookup failed for relation %u", relOid);
56  natts = ((Form_pg_class) GETSTRUCT(tuple))->relnatts;
57  ReleaseSysCache(tuple);
58 
59  /* fix up the given columns */
60  result = bms_copy(columns);
61  result = bms_del_member(result, index);
62 
63  for (attno = 1; attno <= natts; attno++)
64  {
65  tuple = SearchSysCache2(ATTNUM,
66  ObjectIdGetDatum(relOid),
67  Int16GetDatum(attno));
68  if (!HeapTupleIsValid(tuple))
69  continue;
70 
71  if (((Form_pg_attribute) GETSTRUCT(tuple))->attisdropped)
72  continue;
73 
74  index = attno - FirstLowInvalidHeapAttributeNumber;
75 
76  result = bms_add_member(result, index);
77 
78  ReleaseSysCache(tuple);
79  }
80  return result;
81 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
#define Int16GetDatum(X)
Definition: postgres.h:451
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
Definition: type.h:89
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
int16 relnatts
Definition: pg_class.h:84
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1135
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define InvalidAttrNumber
Definition: attnum.h:23
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
#define elog(elevel,...)
Definition: elog.h:226
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:773
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
int16 AttrNumber
Definition: attnum.h:21

◆ sepgsql_dml_privileges()

bool sepgsql_dml_privileges ( List rangeTabls,
bool  abort_on_violation 
)

Definition at line 282 of file dml.c.

References ACL_DELETE, ACL_INSERT, ACL_SELECT, ACL_UPDATE, bms_is_empty(), check_relation_privileges(), find_all_inheritors(), fixup_inherited_columns(), RangeTblEntry::inh, RangeTblEntry::insertedCols, lfirst, lfirst_oid, list_free(), list_make1_oid, NoLock, RangeTblEntry::relid, generate_unaccent_rules::required, RangeTblEntry::requiredPerms, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, SEPG_DB_TABLE__DELETE, SEPG_DB_TABLE__INSERT, SEPG_DB_TABLE__LOCK, SEPG_DB_TABLE__SELECT, SEPG_DB_TABLE__UPDATE, and RangeTblEntry::updatedCols.

Referenced by sepgsql_exec_check_perms().

283 {
284  ListCell *lr;
285 
286  foreach(lr, rangeTabls)
287  {
288  RangeTblEntry *rte = lfirst(lr);
289  uint32 required = 0;
290  List *tableIds;
291  ListCell *li;
292 
293  /*
294  * Only regular relations shall be checked
295  */
296  if (rte->rtekind != RTE_RELATION)
297  continue;
298 
299  /*
300  * Find out required permissions
301  */
302  if (rte->requiredPerms & ACL_SELECT)
303  required |= SEPG_DB_TABLE__SELECT;
304  if (rte->requiredPerms & ACL_INSERT)
305  required |= SEPG_DB_TABLE__INSERT;
306  if (rte->requiredPerms & ACL_UPDATE)
307  {
308  if (!bms_is_empty(rte->updatedCols))
309  required |= SEPG_DB_TABLE__UPDATE;
310  else
311  required |= SEPG_DB_TABLE__LOCK;
312  }
313  if (rte->requiredPerms & ACL_DELETE)
314  required |= SEPG_DB_TABLE__DELETE;
315 
316  /*
317  * Skip, if nothing to be checked
318  */
319  if (required == 0)
320  continue;
321 
322  /*
323  * If this RangeTblEntry is also supposed to reference inherited
324  * tables, we need to check security label of the child tables. So, we
325  * expand rte->relid into list of OIDs of inheritance hierarchy, then
326  * checker routine will be invoked for each relations.
327  */
328  if (!rte->inh)
329  tableIds = list_make1_oid(rte->relid);
330  else
331  tableIds = find_all_inheritors(rte->relid, NoLock, NULL);
332 
333  foreach(li, tableIds)
334  {
335  Oid tableOid = lfirst_oid(li);
336  Bitmapset *selectedCols;
337  Bitmapset *insertedCols;
338  Bitmapset *updatedCols;
339 
340  /*
341  * child table has different attribute numbers, so we need to fix
342  * up them.
343  */
344  selectedCols = fixup_inherited_columns(rte->relid, tableOid,
345  rte->selectedCols);
346  insertedCols = fixup_inherited_columns(rte->relid, tableOid,
347  rte->insertedCols);
348  updatedCols = fixup_inherited_columns(rte->relid, tableOid,
349  rte->updatedCols);
350 
351  /*
352  * check permissions on individual tables
353  */
354  if (!check_relation_privileges(tableOid,
355  selectedCols,
356  insertedCols,
357  updatedCols,
358  required, abort_on_violation))
359  return false;
360  }
361  list_free(tableIds);
362  }
363  return true;
364 }
static bool check_relation_privileges(Oid relOid, Bitmapset *selected, Bitmapset *inserted, Bitmapset *updated, uint32 required, bool abort_on_violation)
Definition: dml.c:143
#define SEPG_DB_TABLE__DELETE
Definition: sepgsql.h:146
static Bitmapset * fixup_inherited_columns(Oid parentId, Oid childId, Bitmapset *columns)
Definition: dml.c:94
#define ACL_DELETE
Definition: parsenodes.h:77
AclMode requiredPerms
Definition: parsenodes.h:1096
unsigned int Oid
Definition: postgres_ext.h:31
#define SEPG_DB_TABLE__SELECT
Definition: sepgsql.h:143
Bitmapset * selectedCols
Definition: parsenodes.h:1098
#define SEPG_DB_TABLE__UPDATE
Definition: sepgsql.h:144
#define NoLock
Definition: lockdefs.h:34
#define SEPG_DB_TABLE__INSERT
Definition: sepgsql.h:145
unsigned int uint32
Definition: c.h:358
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define ACL_UPDATE
Definition: parsenodes.h:76
#define ACL_SELECT
Definition: parsenodes.h:75
#define list_make1_oid(x1)
Definition: pg_list.h:249
Bitmapset * updatedCols
Definition: parsenodes.h:1100
#define lfirst(lc)
Definition: pg_list.h:190
#define ACL_INSERT
Definition: parsenodes.h:74
RTEKind rtekind
Definition: parsenodes.h:974
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:165
void list_free(List *list)
Definition: list.c:1377
Bitmapset * insertedCols
Definition: parsenodes.h:1099
Definition: pg_list.h:50
#define SEPG_DB_TABLE__LOCK
Definition: sepgsql.h:147
#define lfirst_oid(lc)
Definition: pg_list.h:192