PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
dml.c
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------
2  *
3  * contrib/sepgsql/dml.c
4  *
5  * Routines to handle DML permission checks
6  *
7  * Copyright (c) 2010-2017, PostgreSQL Global Development Group
8  *
9  * -------------------------------------------------------------------------
10  */
11 #include "postgres.h"
12 
13 #include "access/htup_details.h"
14 #include "access/sysattr.h"
15 #include "access/tupdesc.h"
16 #include "catalog/catalog.h"
17 #include "catalog/heap.h"
18 #include "catalog/dependency.h"
19 #include "catalog/pg_attribute.h"
20 #include "catalog/pg_class.h"
21 #include "catalog/pg_inherits_fn.h"
22 #include "commands/seclabel.h"
23 #include "commands/tablecmds.h"
24 #include "executor/executor.h"
25 #include "nodes/bitmapset.h"
26 #include "utils/lsyscache.h"
27 #include "utils/syscache.h"
28 
29 #include "sepgsql.h"
30 
31 /*
32  * fixup_whole_row_references
33  *
34  * When user reference a whole of row, it is equivalent to reference to
35  * all the user columns (not system columns). So, we need to fix up the
36  * given bitmapset, if it contains a whole of the row reference.
37  */
38 static Bitmapset *
40 {
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 }
82 
83 /*
84  * fixup_inherited_columns
85  *
86  * When user is querying on a table with children, it implicitly accesses
87  * child tables also. So, we also need to check security label of child
88  * tables and columns, but here is no guarantee attribute numbers are
89  * same between the parent ans children.
90  * It returns a bitmapset which contains attribute number of the child
91  * table based on the given bitmapset of the parent.
92  */
93 static Bitmapset *
94 fixup_inherited_columns(Oid parentId, Oid childId, Bitmapset *columns)
95 {
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);
122  if (!attname)
123  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
124  attno, parentId);
125  attno = get_attnum(childId, attname);
126  if (attno == InvalidAttrNumber)
127  elog(ERROR, "cache lookup failed for attribute %s of relation %u",
128  attname, childId);
129 
130  result = bms_add_member(result,
131  attno - FirstLowInvalidHeapAttributeNumber);
132 
133  pfree(attname);
134  }
135 
136  return result;
137 }
138 
139 /*
140  * check_relation_privileges
141  *
142  * It actually checks required permissions on a certain relation
143  * and its columns.
144  */
145 static bool
147  Bitmapset *selected,
148  Bitmapset *inserted,
149  Bitmapset *updated,
150  uint32 required,
151  bool abort_on_violation)
152 {
153  ObjectAddress object;
154  char *audit_name;
155  Bitmapset *columns;
156  int index;
157  char relkind = get_rel_relkind(relOid);
158  bool result = true;
159 
160  /*
161  * Hardwired Policies: SE-PostgreSQL enforces - clients cannot modify
162  * system catalogs using DMLs - clients cannot reference/modify toast
163  * relations using DMLs
164  */
165  if (sepgsql_getenforce() > 0)
166  {
167  Oid relnamespace = get_rel_namespace(relOid);
168 
169  if (IsSystemNamespace(relnamespace) &&
170  (required & (SEPG_DB_TABLE__UPDATE |
172  SEPG_DB_TABLE__DELETE)) != 0)
173  ereport(ERROR,
174  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
175  errmsg("SELinux: hardwired security policy violation")));
176 
177  if (relkind == RELKIND_TOASTVALUE)
178  ereport(ERROR,
179  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
180  errmsg("SELinux: hardwired security policy violation")));
181  }
182 
183  /*
184  * Check permissions on the relation
185  */
186  object.classId = RelationRelationId;
187  object.objectId = relOid;
188  object.objectSubId = 0;
189  audit_name = getObjectIdentity(&object);
190  switch (relkind)
191  {
192  case RELKIND_RELATION:
193  result = sepgsql_avc_check_perms(&object,
195  required,
196  audit_name,
197  abort_on_violation);
198  break;
199 
200  case RELKIND_SEQUENCE:
201  Assert((required & ~SEPG_DB_TABLE__SELECT) == 0);
202 
203  if (required & SEPG_DB_TABLE__SELECT)
204  result = sepgsql_avc_check_perms(&object,
207  audit_name,
208  abort_on_violation);
209  break;
210 
211  case RELKIND_VIEW:
212  result = sepgsql_avc_check_perms(&object,
215  audit_name,
216  abort_on_violation);
217  break;
218 
219  default:
220  /* nothing to be checked */
221  break;
222  }
223  pfree(audit_name);
224 
225  /*
226  * Only columns owned by relations shall be checked
227  */
228  if (relkind != RELKIND_RELATION)
229  return true;
230 
231  /*
232  * Check permissions on the columns
233  */
234  selected = fixup_whole_row_references(relOid, selected);
235  inserted = fixup_whole_row_references(relOid, inserted);
236  updated = fixup_whole_row_references(relOid, updated);
237  columns = bms_union(selected, bms_union(inserted, updated));
238 
239  while ((index = bms_first_member(columns)) >= 0)
240  {
241  AttrNumber attnum;
242  uint32 column_perms = 0;
243 
244  if (bms_is_member(index, selected))
245  column_perms |= SEPG_DB_COLUMN__SELECT;
246  if (bms_is_member(index, inserted))
247  {
248  if (required & SEPG_DB_TABLE__INSERT)
249  column_perms |= SEPG_DB_COLUMN__INSERT;
250  }
251  if (bms_is_member(index, updated))
252  {
253  if (required & SEPG_DB_TABLE__UPDATE)
254  column_perms |= SEPG_DB_COLUMN__UPDATE;
255  }
256  if (column_perms == 0)
257  continue;
258 
259  /* obtain column's permission */
260  attnum = index + FirstLowInvalidHeapAttributeNumber;
261 
262  object.classId = RelationRelationId;
263  object.objectId = relOid;
264  object.objectSubId = attnum;
265  audit_name = getObjectDescription(&object);
266 
267  result = sepgsql_avc_check_perms(&object,
269  column_perms,
270  audit_name,
271  abort_on_violation);
272  pfree(audit_name);
273 
274  if (!result)
275  return result;
276  }
277  return true;
278 }
279 
280 /*
281  * sepgsql_dml_privileges
282  *
283  * Entrypoint of the DML permission checks
284  */
285 bool
286 sepgsql_dml_privileges(List *rangeTabls, bool abort_on_violation)
287 {
288  ListCell *lr;
289 
290  foreach(lr, rangeTabls)
291  {
292  RangeTblEntry *rte = lfirst(lr);
293  uint32 required = 0;
294  List *tableIds;
295  ListCell *li;
296 
297  /*
298  * Only regular relations shall be checked
299  */
300  if (rte->rtekind != RTE_RELATION)
301  continue;
302 
303  /*
304  * Find out required permissions
305  */
306  if (rte->requiredPerms & ACL_SELECT)
307  required |= SEPG_DB_TABLE__SELECT;
308  if (rte->requiredPerms & ACL_INSERT)
309  required |= SEPG_DB_TABLE__INSERT;
310  if (rte->requiredPerms & ACL_UPDATE)
311  {
312  if (!bms_is_empty(rte->updatedCols))
313  required |= SEPG_DB_TABLE__UPDATE;
314  else
315  required |= SEPG_DB_TABLE__LOCK;
316  }
317  if (rte->requiredPerms & ACL_DELETE)
318  required |= SEPG_DB_TABLE__DELETE;
319 
320  /*
321  * Skip, if nothing to be checked
322  */
323  if (required == 0)
324  continue;
325 
326  /*
327  * If this RangeTblEntry is also supposed to reference inherited
328  * tables, we need to check security label of the child tables. So, we
329  * expand rte->relid into list of OIDs of inheritance hierarchy, then
330  * checker routine will be invoked for each relations.
331  */
332  if (!rte->inh)
333  tableIds = list_make1_oid(rte->relid);
334  else
335  tableIds = find_all_inheritors(rte->relid, NoLock, NULL);
336 
337  foreach(li, tableIds)
338  {
339  Oid tableOid = lfirst_oid(li);
340  Bitmapset *selectedCols;
341  Bitmapset *insertedCols;
342  Bitmapset *updatedCols;
343 
344  /*
345  * child table has different attribute numbers, so we need to fix
346  * up them.
347  */
348  selectedCols = fixup_inherited_columns(rte->relid, tableOid,
349  rte->selectedCols);
350  insertedCols = fixup_inherited_columns(rte->relid, tableOid,
351  rte->insertedCols);
352  updatedCols = fixup_inherited_columns(rte->relid, tableOid,
353  rte->updatedCols);
354 
355  /*
356  * check permissions on individual tables
357  */
358  if (!check_relation_privileges(tableOid,
359  selectedCols,
360  insertedCols,
361  updatedCols,
362  required, abort_on_violation))
363  return false;
364  }
365  list_free(tableIds);
366  }
367  return true;
368 }
#define SEPG_DB_VIEW__EXPAND
Definition: sepgsql.h:212
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:885
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
#define SEPG_CLASS_DB_COLUMN
Definition: sepgsql.h:49
static Bitmapset * fixup_whole_row_references(Oid relOid, Bitmapset *columns)
Definition: dml.c:39
static bool check_relation_privileges(Oid relOid, Bitmapset *selected, Bitmapset *inserted, Bitmapset *updated, uint32 required, bool abort_on_violation)
Definition: dml.c:146
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1769
#define RelationRelationId
Definition: pg_class.h:29
bool sepgsql_avc_check_perms(const ObjectAddress *tobject, uint16 tclass, uint32 required, const char *audit_name, bool abort_on_violation)
Definition: uavc.c:428
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1718
#define SEPG_DB_TABLE__DELETE
Definition: sepgsql.h:146
#define Int16GetDatum(X)
Definition: postgres.h:457
static Bitmapset * fixup_inherited_columns(Oid parentId, Oid childId, Bitmapset *columns)
Definition: dml.c:94
int errcode(int sqlerrcode)
Definition: elog.c:575
bool sepgsql_getenforce(void)
Definition: selinux.c:648
#define ACL_DELETE
Definition: parsenodes.h:68
#define SEPG_DB_COLUMN__INSERT
Definition: sepgsql.h:177
return result
Definition: formatting.c:1618
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
AclMode requiredPerms
Definition: parsenodes.h:1004
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
char * getObjectDescription(const ObjectAddress *object)
char * get_attname(Oid relid, AttrNumber attnum)
Definition: lsyscache.c:774
#define SEPG_DB_TABLE__SELECT
Definition: sepgsql.h:143
Definition: type.h:90
Bitmapset * selectedCols
Definition: parsenodes.h:1006
void pfree(void *pointer)
Definition: mcxt.c:950
#define SEPG_CLASS_DB_SEQUENCE
Definition: sepgsql.h:47
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#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 NoLock
Definition: lockdefs.h:34
#define SEPG_DB_TABLE__INSERT
Definition: sepgsql.h:145
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:821
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
unsigned int uint32
Definition: c.h:268
bool sepgsql_dml_privileges(List *rangeTabls, bool abort_on_violation)
Definition: dml.c:286
#define ereport(elevel, rest)
Definition: elog.h:122
#define SEPG_DB_COLUMN__UPDATE
Definition: sepgsql.h:176
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
#define SEPG_CLASS_DB_VIEW
Definition: sepgsql.h:53
#define ACL_UPDATE
Definition: parsenodes.h:67
#define RELKIND_TOASTVALUE
Definition: pg_class.h:163
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
#define ACL_SELECT
Definition: parsenodes.h:66
#define list_make1_oid(x1)
Definition: pg_list.h:145
bool IsSystemNamespace(Oid namespaceId)
Definition: catalog.c:162
Bitmapset * updatedCols
Definition: parsenodes.h:1008
#define SEPG_CLASS_DB_TABLE
Definition: sepgsql.h:46
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
#define ACL_INSERT
Definition: parsenodes.h:65
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:916
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define RELKIND_VIEW
Definition: pg_class.h:164
void list_free(List *list)
Definition: list.c:1133
Bitmapset * insertedCols
Definition: parsenodes.h:1007
#define elog
Definition: elog.h:219
#define RELKIND_RELATION
Definition: pg_class.h:160
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:735
#define RELKIND_SEQUENCE
Definition: pg_class.h:162
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
int16 AttrNumber
Definition: attnum.h:21
#define SEPG_DB_TABLE__LOCK
Definition: sepgsql.h:147
#define lfirst_oid(lc)
Definition: pg_list.h:108
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:154