PostgreSQL Source Code  git master
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-2019, 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.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 {
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 }
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 {
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 }
135 
136 /*
137  * check_relation_privileges
138  *
139  * It actually checks required permissions on a certain relation
140  * and its columns.
141  */
142 static bool
144  Bitmapset *selected,
145  Bitmapset *inserted,
146  Bitmapset *updated,
148  bool abort_on_violation)
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  {
164  if ((required & (SEPG_DB_TABLE__UPDATE |
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:
197  Assert((required & ~SEPG_DB_TABLE__SELECT) == 0);
198 
199  if (required & SEPG_DB_TABLE__SELECT)
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  {
244  if (required & SEPG_DB_TABLE__INSERT)
245  column_perms |= SEPG_DB_COLUMN__INSERT;
246  }
247  if (bms_is_member(index, updated))
248  {
249  if (required & SEPG_DB_TABLE__UPDATE)
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 }
275 
276 /*
277  * sepgsql_dml_privileges
278  *
279  * Entrypoint of the DML permission checks
280  */
281 bool
282 sepgsql_dml_privileges(List *rangeTabls, bool abort_on_violation)
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 }
#define SEPG_DB_VIEW__EXPAND
Definition: sepgsql.h:212
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:996
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
#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:143
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
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
#define SEPG_DB_TABLE__DELETE
Definition: sepgsql.h:146
#define Int16GetDatum(X)
Definition: postgres.h:451
static Bitmapset * fixup_inherited_columns(Oid parentId, Oid childId, Bitmapset *columns)
Definition: dml.c:94
int errcode(int sqlerrcode)
Definition: elog.c:570
bool sepgsql_getenforce(void)
Definition: selinux.c:648
#define ACL_DELETE
Definition: parsenodes.h:77
#define SEPG_DB_COLUMN__INSERT
Definition: sepgsql.h:177
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
AclMode requiredPerms
Definition: parsenodes.h:1096
unsigned int Oid
Definition: postgres_ext.h:31
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
Bitmapset * selectedCols
Definition: parsenodes.h:1098
void pfree(void *pointer)
Definition: mcxt.c:1056
#define SEPG_CLASS_DB_SEQUENCE
Definition: sepgsql.h:47
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
#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:806
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
unsigned int uint32
Definition: c.h:358
bool sepgsql_dml_privileges(List *rangeTabls, bool abort_on_violation)
Definition: dml.c:282
#define ereport(elevel, rest)
Definition: elog.h:141
#define SEPG_DB_COLUMN__UPDATE
Definition: sepgsql.h:176
int16 relnatts
Definition: pg_class.h:84
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define SEPG_CLASS_DB_VIEW
Definition: sepgsql.h:53
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
#define ACL_UPDATE
Definition: parsenodes.h:76
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define ACL_SELECT
Definition: parsenodes.h:75
#define list_make1_oid(x1)
Definition: pg_list.h:249
int16 attnum
Definition: pg_attribute.h:79
Bitmapset * updatedCols
Definition: parsenodes.h:1100
#define SEPG_CLASS_DB_TABLE
Definition: sepgsql.h:46
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
#define ACL_INSERT
Definition: parsenodes.h:74
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
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
RTEKind rtekind
Definition: parsenodes.h:974
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:165
int errmsg(const char *fmt,...)
Definition: elog.c:784
void list_free(List *list)
Definition: list.c:1377
#define elog(elevel,...)
Definition: elog.h:226
Bitmapset * insertedCols
Definition: parsenodes.h:1099
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:775
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:773
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
int16 AttrNumber
Definition: attnum.h:21
#define SEPG_DB_TABLE__LOCK
Definition: sepgsql.h:147
#define lfirst_oid(lc)
Definition: pg_list.h:192