PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
schema.c
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------
2  *
3  * contrib/sepgsql/schema.c
4  *
5  * Routines corresponding to schema objects
6  *
7  * Copyright (c) 2010-2017, PostgreSQL Global Development Group
8  *
9  * -------------------------------------------------------------------------
10  */
11 #include "postgres.h"
12 
13 #include "access/genam.h"
14 #include "access/heapam.h"
15 #include "access/htup_details.h"
16 #include "access/sysattr.h"
17 #include "catalog/dependency.h"
18 #include "catalog/indexing.h"
19 #include "catalog/pg_database.h"
20 #include "catalog/pg_namespace.h"
21 #include "commands/seclabel.h"
22 #include "lib/stringinfo.h"
23 #include "miscadmin.h"
24 #include "utils/builtins.h"
25 #include "utils/fmgroids.h"
26 #include "utils/lsyscache.h"
27 #include "utils/tqual.h"
28 
29 #include "sepgsql.h"
30 
31 /*
32  * sepgsql_schema_post_create
33  *
34  * This routine assigns a default security label on a newly defined
35  * schema.
36  */
37 void
39 {
40  Relation rel;
41  ScanKeyData skey;
42  SysScanDesc sscan;
43  HeapTuple tuple;
44  char *tcontext;
45  char *ncontext;
46  const char *nsp_name;
47  ObjectAddress object;
48  Form_pg_namespace nspForm;
49  StringInfoData audit_name;
50 
51  /*
52  * Compute a default security label when we create a new schema object
53  * under the working database.
54  *
55  * XXX - uncoming version of libselinux supports to take object name to
56  * handle special treatment on default security label; such as special
57  * label on "pg_temp" schema.
58  */
60 
61  ScanKeyInit(&skey,
63  BTEqualStrategyNumber, F_OIDEQ,
64  ObjectIdGetDatum(namespaceId));
65 
66  sscan = systable_beginscan(rel, NamespaceOidIndexId, true,
67  SnapshotSelf, 1, &skey);
68  tuple = systable_getnext(sscan);
69  if (!HeapTupleIsValid(tuple))
70  elog(ERROR, "catalog lookup failed for namespace %u", namespaceId);
71 
72  nspForm = (Form_pg_namespace) GETSTRUCT(tuple);
73  nsp_name = NameStr(nspForm->nspname);
74  if (strncmp(nsp_name, "pg_temp_", 8) == 0)
75  nsp_name = "pg_temp";
76  else if (strncmp(nsp_name, "pg_toast_temp_", 14) == 0)
77  nsp_name = "pg_toast_temp";
78 
81  tcontext,
83  nsp_name);
84 
85  /*
86  * check db_schema:{create}
87  */
88  initStringInfo(&audit_name);
89  appendStringInfo(&audit_name, "%s", quote_identifier(nsp_name));
93  audit_name.data,
94  true);
95  systable_endscan(sscan);
97 
98  /*
99  * Assign the default security label on a new procedure
100  */
101  object.classId = NamespaceRelationId;
102  object.objectId = namespaceId;
103  object.objectSubId = 0;
104  SetSecurityLabel(&object, SEPGSQL_LABEL_TAG, ncontext);
105 
106  pfree(ncontext);
107  pfree(tcontext);
108 }
109 
110 /*
111  * sepgsql_schema_drop
112  *
113  * It checks privileges to drop the supplied schema object.
114  */
115 void
117 {
118  ObjectAddress object;
119  char *audit_name;
120 
121  /*
122  * check db_schema:{drop} permission
123  */
124  object.classId = NamespaceRelationId;
125  object.objectId = namespaceId;
126  object.objectSubId = 0;
127  audit_name = getObjectIdentity(&object);
128 
129  sepgsql_avc_check_perms(&object,
132  audit_name,
133  true);
134  pfree(audit_name);
135 }
136 
137 /*
138  * sepgsql_schema_relabel
139  *
140  * It checks privileges to relabel the supplied schema
141  * by the `seclabel'.
142  */
143 void
144 sepgsql_schema_relabel(Oid namespaceId, const char *seclabel)
145 {
146  ObjectAddress object;
147  char *audit_name;
148 
149  object.classId = NamespaceRelationId;
150  object.objectId = namespaceId;
151  object.objectSubId = 0;
152  audit_name = getObjectIdentity(&object);
153 
154  /*
155  * check db_schema:{setattr relabelfrom} permission
156  */
157  sepgsql_avc_check_perms(&object,
161  audit_name,
162  true);
163 
164  /*
165  * check db_schema:{relabelto} permission
166  */
170  audit_name,
171  true);
172  pfree(audit_name);
173 }
174 
175 /*
176  * sepgsql_schema_check_perms
177  *
178  * utility routine to check db_schema:{xxx} permissions
179  */
180 static bool
181 check_schema_perms(Oid namespaceId, uint32 required, bool abort_on_violation)
182 {
183  ObjectAddress object;
184  char *audit_name;
185  bool result;
186 
187  object.classId = NamespaceRelationId;
188  object.objectId = namespaceId;
189  object.objectSubId = 0;
190  audit_name = getObjectIdentity(&object);
191 
192  result = sepgsql_avc_check_perms(&object,
194  required,
195  audit_name,
196  abort_on_violation);
197  pfree(audit_name);
198 
199  return result;
200 }
201 
202 /* db_schema:{setattr} permission */
203 void
205 {
206  check_schema_perms(namespaceId, SEPG_DB_SCHEMA__SETATTR, true);
207 }
208 
209 /* db_schema:{search} permission */
210 bool
211 sepgsql_schema_search(Oid namespaceId, bool abort_on_violation)
212 {
213  return check_schema_perms(namespaceId,
215  abort_on_violation);
216 }
217 
218 void
220 {
221  check_schema_perms(namespaceId, SEPG_DB_SCHEMA__ADD_NAME, true);
222 }
223 
224 void
226 {
228 }
229 
230 void
232 {
233  check_schema_perms(namespaceId,
236  true);
237 }
bool sepgsql_avc_check_perms_label(const char *tcontext, uint16 tclass, uint32 required, const char *audit_name, bool abort_on_violation)
Definition: uavc.c:346
#define NamespaceOidIndexId
Definition: indexing.h:195
#define SEPG_DB_SCHEMA__DROP
Definition: sepgsql.h:128
#define NamespaceRelationId
Definition: pg_namespace.h:34
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
void sepgsql_schema_setattr(Oid namespaceId)
Definition: schema.c:204
FormData_pg_namespace * Form_pg_namespace
Definition: pg_namespace.h:51
void SetSecurityLabel(const ObjectAddress *object, const char *provider, const char *label)
Definition: seclabel.c:327
void sepgsql_schema_relabel(Oid namespaceId, const char *seclabel)
Definition: schema.c:144
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10254
void sepgsql_schema_drop(Oid namespaceId)
Definition: schema.c:116
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define DatabaseRelationId
Definition: pg_database.h:29
void sepgsql_schema_add_name(Oid namespaceId)
Definition: schema.c:219
char * sepgsql_get_label(Oid classId, Oid objectId, int32 subId)
Definition: label.c:463
bool sepgsql_avc_check_perms(const ObjectAddress *tobject, uint16 tclass, uint32 required, const char *audit_name, bool abort_on_violation)
Definition: uavc.c:428
void sepgsql_schema_remove_name(Oid namespaceId)
Definition: schema.c:225
#define AccessShareLock
Definition: lockdefs.h:36
static bool check_schema_perms(Oid namespaceId, uint32 required, bool abort_on_violation)
Definition: schema.c:181
return result
Definition: formatting.c:1618
#define heap_close(r, l)
Definition: heapam.h:97
#define SEPG_CLASS_DB_SCHEMA
Definition: sepgsql.h:45
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
#define SEPG_DB_SCHEMA__RELABELFROM
Definition: sepgsql.h:131
void sepgsql_schema_post_create(Oid namespaceId)
Definition: schema.c:38
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
void pfree(void *pointer)
Definition: mcxt.c:950
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
char * getObjectIdentity(const ObjectAddress *object)
void sepgsql_schema_rename(Oid namespaceId)
Definition: schema.c:231
#define SEPGSQL_LABEL_TAG
Definition: sepgsql.h:23
unsigned int uint32
Definition: c.h:268
#define SnapshotSelf
Definition: tqual.h:27
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define SEPG_DB_SCHEMA__SEARCH
Definition: sepgsql.h:133
Oid MyDatabaseId
Definition: globals.c:76
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
bool sepgsql_schema_search(Oid namespaceId, bool abort_on_violation)
Definition: schema.c:211
#define SEPG_DB_SCHEMA__ADD_NAME
Definition: sepgsql.h:134
#define SEPG_DB_SCHEMA__SETATTR
Definition: sepgsql.h:130
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define SEPG_DB_SCHEMA__CREATE
Definition: sepgsql.h:127
char * sepgsql_get_client_label(void)
Definition: label.c:91
char * sepgsql_compute_create(const char *scontext, const char *tcontext, uint16 tclass, const char *objname)
Definition: selinux.c:837
#define NameStr(name)
Definition: c.h:499
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define elog
Definition: elog.h:219
#define SEPG_DB_SCHEMA__RELABELTO
Definition: sepgsql.h:132
#define SEPG_DB_SCHEMA__REMOVE_NAME
Definition: sepgsql.h:135
#define BTEqualStrategyNumber
Definition: stratnum.h:31