PostgreSQL Source Code  git master
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-2019, PostgreSQL Global Development Group
8  *
9  * -------------------------------------------------------------------------
10  */
11 #include "postgres.h"
12 
13 #include "access/genam.h"
14 #include "access/htup_details.h"
15 #include "access/sysattr.h"
16 #include "access/table.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 "sepgsql.h"
25 #include "utils/builtins.h"
26 #include "utils/fmgroids.h"
27 #include "utils/lsyscache.h"
28 #include "utils/snapmgr.h"
29 
30 /*
31  * sepgsql_schema_post_create
32  *
33  * This routine assigns a default security label on a newly defined
34  * schema.
35  */
36 void
38 {
39  Relation rel;
40  ScanKeyData skey;
41  SysScanDesc sscan;
42  HeapTuple tuple;
43  char *tcontext;
44  char *ncontext;
45  const char *nsp_name;
46  ObjectAddress object;
47  Form_pg_namespace nspForm;
48  StringInfoData audit_name;
49 
50  /*
51  * Compute a default security label when we create a new schema object
52  * under the working database.
53  *
54  * XXX - upcoming version of libselinux supports to take object name to
55  * handle special treatment on default security label; such as special
56  * label on "pg_temp" schema.
57  */
58  rel = table_open(NamespaceRelationId, AccessShareLock);
59 
60  ScanKeyInit(&skey,
61  Anum_pg_namespace_oid,
62  BTEqualStrategyNumber, F_OIDEQ,
63  ObjectIdGetDatum(namespaceId));
64 
65  sscan = systable_beginscan(rel, NamespaceOidIndexId, true,
66  SnapshotSelf, 1, &skey);
67  tuple = systable_getnext(sscan);
68  if (!HeapTupleIsValid(tuple))
69  elog(ERROR, "could not find tuple for namespace %u", namespaceId);
70 
71  nspForm = (Form_pg_namespace) GETSTRUCT(tuple);
72  nsp_name = NameStr(nspForm->nspname);
73  if (strncmp(nsp_name, "pg_temp_", 8) == 0)
74  nsp_name = "pg_temp";
75  else if (strncmp(nsp_name, "pg_toast_temp_", 14) == 0)
76  nsp_name = "pg_toast_temp";
77 
78  tcontext = sepgsql_get_label(DatabaseRelationId, MyDatabaseId, 0);
80  tcontext,
82  nsp_name);
83 
84  /*
85  * check db_schema:{create}
86  */
87  initStringInfo(&audit_name);
88  appendStringInfo(&audit_name, "%s", quote_identifier(nsp_name));
92  audit_name.data,
93  true);
94  systable_endscan(sscan);
96 
97  /*
98  * Assign the default security label on a new procedure
99  */
100  object.classId = NamespaceRelationId;
101  object.objectId = namespaceId;
102  object.objectSubId = 0;
103  SetSecurityLabel(&object, SEPGSQL_LABEL_TAG, ncontext);
104 
105  pfree(ncontext);
106  pfree(tcontext);
107 }
108 
109 /*
110  * sepgsql_schema_drop
111  *
112  * It checks privileges to drop the supplied schema object.
113  */
114 void
116 {
117  ObjectAddress object;
118  char *audit_name;
119 
120  /*
121  * check db_schema:{drop} permission
122  */
123  object.classId = NamespaceRelationId;
124  object.objectId = namespaceId;
125  object.objectSubId = 0;
126  audit_name = getObjectIdentity(&object);
127 
128  sepgsql_avc_check_perms(&object,
131  audit_name,
132  true);
133  pfree(audit_name);
134 }
135 
136 /*
137  * sepgsql_schema_relabel
138  *
139  * It checks privileges to relabel the supplied schema
140  * by the `seclabel'.
141  */
142 void
143 sepgsql_schema_relabel(Oid namespaceId, const char *seclabel)
144 {
145  ObjectAddress object;
146  char *audit_name;
147 
148  object.classId = NamespaceRelationId;
149  object.objectId = namespaceId;
150  object.objectSubId = 0;
151  audit_name = getObjectIdentity(&object);
152 
153  /*
154  * check db_schema:{setattr relabelfrom} permission
155  */
156  sepgsql_avc_check_perms(&object,
160  audit_name,
161  true);
162 
163  /*
164  * check db_schema:{relabelto} permission
165  */
169  audit_name,
170  true);
171  pfree(audit_name);
172 }
173 
174 /*
175  * sepgsql_schema_check_perms
176  *
177  * utility routine to check db_schema:{xxx} permissions
178  */
179 static bool
180 check_schema_perms(Oid namespaceId, uint32 required, bool abort_on_violation)
181 {
182  ObjectAddress object;
183  char *audit_name;
184  bool result;
185 
186  object.classId = NamespaceRelationId;
187  object.objectId = namespaceId;
188  object.objectSubId = 0;
189  audit_name = getObjectIdentity(&object);
190 
191  result = sepgsql_avc_check_perms(&object,
193  required,
194  audit_name,
195  abort_on_violation);
196  pfree(audit_name);
197 
198  return result;
199 }
200 
201 /* db_schema:{setattr} permission */
202 void
204 {
205  check_schema_perms(namespaceId, SEPG_DB_SCHEMA__SETATTR, true);
206 }
207 
208 /* db_schema:{search} permission */
209 bool
210 sepgsql_schema_search(Oid namespaceId, bool abort_on_violation)
211 {
212  return check_schema_perms(namespaceId,
214  abort_on_violation);
215 }
216 
217 void
219 {
220  check_schema_perms(namespaceId, SEPG_DB_SCHEMA__ADD_NAME, true);
221 }
222 
223 void
225 {
227 }
228 
229 void
231 {
232  check_schema_perms(namespaceId,
235  true);
236 }
bool sepgsql_avc_check_perms_label(const char *tcontext, uint16 tclass, uint32 required, const char *audit_name, bool abort_on_violation)
Definition: uavc.c:337
#define NamespaceOidIndexId
Definition: indexing.h:192
#define SEPG_DB_SCHEMA__DROP
Definition: sepgsql.h:128
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:525
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void sepgsql_schema_setattr(Oid namespaceId)
Definition: schema.c:203
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:143
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10772
void sepgsql_schema_drop(Oid namespaceId)
Definition: schema.c:115
void sepgsql_schema_add_name(Oid namespaceId)
Definition: schema.c:218
char * sepgsql_get_label(Oid classId, Oid objectId, int32 subId)
Definition: label.c:446
bool sepgsql_avc_check_perms(const ObjectAddress *tobject, uint16 tclass, uint32 required, const char *audit_name, bool abort_on_violation)
Definition: uavc.c:419
void sepgsql_schema_remove_name(Oid namespaceId)
Definition: schema.c:224
#define AccessShareLock
Definition: lockdefs.h:36
static bool check_schema_perms(Oid namespaceId, uint32 required, bool abort_on_violation)
Definition: schema.c:180
#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:352
#define SEPG_DB_SCHEMA__RELABELFROM
Definition: sepgsql.h:131
void sepgsql_schema_post_create(Oid namespaceId)
Definition: schema.c:37
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
void pfree(void *pointer)
Definition: mcxt.c:1056
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
char * getObjectIdentity(const ObjectAddress *object)
void sepgsql_schema_rename(Oid namespaceId)
Definition: schema.c:230
#define SnapshotSelf
Definition: snapmgr.h:68
#define SEPGSQL_LABEL_TAG
Definition: sepgsql.h:23
unsigned int uint32
Definition: c.h:359
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define SEPG_DB_SCHEMA__SEARCH
Definition: sepgsql.h:133
Oid MyDatabaseId
Definition: globals.c:85
bool sepgsql_schema_search(Oid namespaceId, bool abort_on_violation)
Definition: schema.c:210
#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:78
#define SEPG_DB_SCHEMA__CREATE
Definition: sepgsql.h:127
char * sepgsql_get_client_label(void)
Definition: label.c:81
char * sepgsql_compute_create(const char *scontext, const char *tcontext, uint16 tclass, const char *objname)
Definition: selinux.c:836
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:616
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#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