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/dependency.h"
#include "catalog/heap.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 "sepgsql.h"
#include "utils/lsyscache.h"
#include "utils/syscache.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 141 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(), 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().

147 {
148  ObjectAddress object;
149  char *audit_name;
150  Bitmapset *columns;
151  int index;
152  char relkind = get_rel_relkind(relOid);
153  bool result = true;
154 
155  /*
156  * Hardwired Policies: SE-PostgreSQL enforces - clients cannot modify
157  * system catalogs using DMLs - clients cannot reference/modify toast
158  * relations using DMLs
159  */
160  if (sepgsql_getenforce() > 0)
161  {
164  SEPG_DB_TABLE__DELETE)) != 0 &&
165  IsCatalogRelationOid(relOid))
166  ereport(ERROR,
167  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
168  errmsg("SELinux: hardwired security policy violation")));
169 
170  if (relkind == RELKIND_TOASTVALUE)
171  ereport(ERROR,
172  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
173  errmsg("SELinux: hardwired security policy violation")));
174  }
175 
176  /*
177  * Check permissions on the relation
178  */
179  object.classId = RelationRelationId;
180  object.objectId = relOid;
181  object.objectSubId = 0;
182  audit_name = getObjectIdentity(&object);
183  switch (relkind)
184  {
185  case RELKIND_RELATION:
186  case RELKIND_PARTITIONED_TABLE:
187  result = sepgsql_avc_check_perms(&object,
189  required,
190  audit_name,
191  abort_on_violation);
192  break;
193 
194  case RELKIND_SEQUENCE:
196 
198  result = sepgsql_avc_check_perms(&object,
201  audit_name,
202  abort_on_violation);
203  break;
204 
205  case RELKIND_VIEW:
206  result = sepgsql_avc_check_perms(&object,
209  audit_name,
210  abort_on_violation);
211  break;
212 
213  default:
214  /* nothing to be checked */
215  break;
216  }
217  pfree(audit_name);
218 
219  /*
220  * Only columns owned by relations shall be checked
221  */
222  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE)
223  return true;
224 
225  /*
226  * Check permissions on the columns
227  */
228  selected = fixup_whole_row_references(relOid, selected);
229  inserted = fixup_whole_row_references(relOid, inserted);
230  updated = fixup_whole_row_references(relOid, updated);
231  columns = bms_union(selected, bms_union(inserted, updated));
232 
233  while ((index = bms_first_member(columns)) >= 0)
234  {
236  uint32 column_perms = 0;
237 
238  if (bms_is_member(index, selected))
239  column_perms |= SEPG_DB_COLUMN__SELECT;
240  if (bms_is_member(index, inserted))
241  {
243  column_perms |= SEPG_DB_COLUMN__INSERT;
244  }
245  if (bms_is_member(index, updated))
246  {
248  column_perms |= SEPG_DB_COLUMN__UPDATE;
249  }
250  if (column_perms == 0)
251  continue;
252 
253  /* obtain column's permission */
254  attnum = index + FirstLowInvalidHeapAttributeNumber;
255 
256  object.classId = RelationRelationId;
257  object.objectId = relOid;
258  object.objectSubId = attnum;
259  audit_name = getObjectDescription(&object);
260 
261  result = sepgsql_avc_check_perms(&object,
263  column_perms,
264  audit_name,
265  abort_on_violation);
266  pfree(audit_name);
267 
268  if (!result)
269  return result;
270  }
271  return true;
272 }
#define SEPG_DB_VIEW__EXPAND
Definition: sepgsql.h:213
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:38
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1866
bool sepgsql_avc_check_perms(const ObjectAddress *tobject, uint16 tclass, uint32 required, const char *audit_name, bool abort_on_violation)
Definition: uavc.c:419
#define SEPG_DB_TABLE__DELETE
Definition: sepgsql.h:146
int errcode(int sqlerrcode)
Definition: elog.c:610
bool sepgsql_getenforce(void)
Definition: selinux.c:651
#define SEPG_DB_COLUMN__INSERT
Definition: sepgsql.h:178
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
char * getObjectDescription(const ObjectAddress *object)
bool IsCatalogRelationOid(Oid relid)
Definition: catalog.c:115
#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:176
#define SEPG_DB_SEQUENCE__GET_VALUE
Definition: sepgsql.h:156
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:367
#define SEPG_DB_COLUMN__UPDATE
Definition: sepgsql.h:177
#define SEPG_CLASS_DB_VIEW
Definition: sepgsql.h:53
int16 attnum
Definition: pg_attribute.h:79
#define ereport(elevel,...)
Definition: elog.h:144
#define SEPG_CLASS_DB_TABLE
Definition: sepgsql.h:46
#define Assert(condition)
Definition: c.h:738
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
int errmsg(const char *fmt,...)
Definition: elog.c:824
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 92 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().

93 {
94  Bitmapset *result = NULL;
95  int index;
96 
97  /*
98  * obviously, no need to do anything here
99  */
100  if (parentId == childId)
101  return columns;
102 
103  index = -1;
104  while ((index = bms_next_member(columns, index)) >= 0)
105  {
106  /* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
108  char *attname;
109 
110  /*
111  * whole-row-reference shall be fixed-up later
112  */
113  if (attno == InvalidAttrNumber)
114  {
115  result = bms_add_member(result, index);
116  continue;
117  }
118 
119  attname = get_attname(parentId, attno, false);
120  attno = get_attnum(childId, attname);
121  if (attno == InvalidAttrNumber)
122  elog(ERROR, "cache lookup failed for attribute %s of relation %u",
123  attname, childId);
124 
125  result = bms_add_member(result,
126  attno - FirstLowInvalidHeapAttributeNumber);
127 
128  pfree(attname);
129  }
130 
131  return result;
132 }
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:807
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define InvalidAttrNumber
Definition: attnum.h:23
#define elog(elevel,...)
Definition: elog.h:214
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:776
int16 AttrNumber
Definition: attnum.h:21

◆ fixup_whole_row_references()

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

Definition at line 38 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(), RELOID, SearchSysCache1(), and SearchSysCache2().

Referenced by check_relation_privileges().

39 {
40  Bitmapset *result;
41  HeapTuple tuple;
42  AttrNumber natts;
43  AttrNumber attno;
44  int index;
45 
46  /* if no whole-row references, nothing to do */
48  if (!bms_is_member(index, columns))
49  return columns;
50 
51  /* obtain number of attributes */
52  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relOid));
53  if (!HeapTupleIsValid(tuple))
54  elog(ERROR, "cache lookup failed for relation %u", relOid);
55  natts = ((Form_pg_class) GETSTRUCT(tuple))->relnatts;
56  ReleaseSysCache(tuple);
57 
58  /* remove bit 0 from column set, add in all the non-dropped columns */
59  result = bms_copy(columns);
60  result = bms_del_member(result, index);
61 
62  for (attno = 1; attno <= natts; attno++)
63  {
64  tuple = SearchSysCache2(ATTNUM,
65  ObjectIdGetDatum(relOid),
66  Int16GetDatum(attno));
67  if (!HeapTupleIsValid(tuple))
68  continue; /* unexpected case, should we error? */
69 
70  if (!((Form_pg_attribute) GETSTRUCT(tuple))->attisdropped)
71  {
72  index = attno - FirstLowInvalidHeapAttributeNumber;
73  result = bms_add_member(result, index);
74  }
75 
76  ReleaseSysCache(tuple);
77  }
78  return result;
79 }
#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:193
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
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:153
#define elog(elevel,...)
Definition: elog.h:214
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 280 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().

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