PostgreSQL Source Code git master
Loading...
Searching...
No Matches
seclabel.h File Reference
Include dependency graph for seclabel.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef void(* check_object_relabel_type) (const ObjectAddress *object, const char *seclabel)
 

Functions

charGetSecurityLabel (const ObjectAddress *object, const char *provider)
 
void SetSecurityLabel (const ObjectAddress *object, const char *provider, const char *label)
 
void DeleteSecurityLabel (const ObjectAddress *object)
 
void DeleteSharedSecurityLabel (Oid objectId, Oid classId)
 
ObjectAddress ExecSecLabelStmt (SecLabelStmt *stmt)
 
void register_label_provider (const char *provider_name, check_object_relabel_type hook)
 

Typedef Documentation

◆ check_object_relabel_type

typedef void(* check_object_relabel_type) (const ObjectAddress *object, const char *seclabel)

Definition at line 29 of file seclabel.h.

Function Documentation

◆ DeleteSecurityLabel()

void DeleteSecurityLabel ( const ObjectAddress object)
extern

Definition at line 523 of file seclabel.c.

524{
526 ScanKeyData skey[3];
527 SysScanDesc scan;
529 int nkeys;
530
531 /* Shared objects have their own security label catalog. */
532 if (IsSharedRelation(object->classId))
533 {
534 Assert(object->objectSubId == 0);
536 return;
537 }
538
539 ScanKeyInit(&skey[0],
542 ObjectIdGetDatum(object->objectId));
543 ScanKeyInit(&skey[1],
546 ObjectIdGetDatum(object->classId));
547 if (object->objectSubId != 0)
548 {
549 ScanKeyInit(&skey[2],
552 Int32GetDatum(object->objectSubId));
553 nkeys = 3;
554 }
555 else
556 nkeys = 2;
557
559
561 NULL, nkeys, skey);
564 systable_endscan(scan);
565
567}
#define Assert(condition)
Definition c.h:873
bool IsSharedRelation(Oid relationId)
Definition catalog.c:304
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
#define HeapTupleIsValid(tuple)
Definition htup.h:78
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365
#define RowExclusiveLock
Definition lockdefs.h:38
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
static int fb(int x)
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
void DeleteSharedSecurityLabel(Oid objectId, Oid classId)
Definition seclabel.c:491
#define BTEqualStrategyNumber
Definition stratnum.h:31
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40

References Assert, BTEqualStrategyNumber, CatalogTupleDelete(), ObjectAddress::classId, DeleteSharedSecurityLabel(), fb(), HeapTupleIsValid, Int32GetDatum(), IsSharedRelation(), ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by deleteOneObject().

◆ DeleteSharedSecurityLabel()

◆ ExecSecLabelStmt()

ObjectAddress ExecSecLabelStmt ( SecLabelStmt stmt)
extern

Definition at line 115 of file seclabel.c.

116{
118 ObjectAddress address;
119 Relation relation;
120 ListCell *lc;
121
122 /*
123 * Find the named label provider, or if none specified, check whether
124 * there's exactly one, and if so use it.
125 */
126 if (stmt->provider == NULL)
127 {
131 errmsg("no security label providers have been loaded")));
135 errmsg("must specify provider when multiple security label providers have been loaded")));
137 }
138 else
139 {
140 foreach(lc, label_provider_list)
141 {
143
144 if (strcmp(stmt->provider, lp->provider_name) == 0)
145 {
146 provider = lp;
147 break;
148 }
149 }
150 if (provider == NULL)
153 errmsg("security label provider \"%s\" is not loaded",
154 stmt->provider)));
155 }
156
157 if (!SecLabelSupportsObjectType(stmt->objtype))
160 errmsg("security labels are not supported for this type of object")));
161
162 /*
163 * Translate the parser representation which identifies this object into
164 * an ObjectAddress. get_object_address() will throw an error if the
165 * object does not exist, and will also acquire a lock on the target to
166 * guard against concurrent modifications.
167 */
168 address = get_object_address(stmt->objtype, stmt->object,
169 &relation, ShareUpdateExclusiveLock, false);
170
171 /* Require ownership of the target object. */
172 check_object_ownership(GetUserId(), stmt->objtype, address,
173 stmt->object, relation);
174
175 /* Perform other integrity checks as needed. */
176 switch (stmt->objtype)
177 {
178 case OBJECT_COLUMN:
179
180 /*
181 * Allow security labels only on columns of tables, views,
182 * materialized views, composite types, and foreign tables (which
183 * are the only relkinds for which pg_dump will dump labels).
184 */
185 if (relation->rd_rel->relkind != RELKIND_RELATION &&
186 relation->rd_rel->relkind != RELKIND_VIEW &&
187 relation->rd_rel->relkind != RELKIND_MATVIEW &&
188 relation->rd_rel->relkind != RELKIND_COMPOSITE_TYPE &&
189 relation->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
190 relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
193 errmsg("cannot set security label on relation \"%s\"",
194 RelationGetRelationName(relation)),
195 errdetail_relkind_not_supported(relation->rd_rel->relkind)));
196 break;
197 default:
198 break;
199 }
200
201 /* Provider gets control here, may throw ERROR to veto new label. */
202 provider->hook(&address, stmt->label);
203
204 /* Apply new label. */
205 SetSecurityLabel(&address, provider->provider_name, stmt->label);
206
207 /*
208 * If get_object_address() opened the relation for us, we close it to keep
209 * the reference count correct - but we retain any locks acquired by
210 * get_object_address() until commit time, to guard against concurrent
211 * activity.
212 */
213 if (relation != NULL)
214 relation_close(relation, NoLock);
215
216 return address;
217}
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#define stmt
#define NoLock
Definition lockdefs.h:34
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
Oid GetUserId(void)
Definition miscinit.c:469
void check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address, Node *object, Relation relation)
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
@ OBJECT_COLUMN
int errdetail_relkind_not_supported(char relkind)
Definition pg_class.c:24
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define linitial(l)
Definition pg_list.h:178
#define RelationGetRelationName(relation)
Definition rel.h:548
void SetSecurityLabel(const ObjectAddress *object, const char *provider, const char *label)
Definition seclabel.c:404
static List * label_provider_list
Definition seclabel.c:34
static bool SecLabelSupportsObjectType(ObjectType objtype)
Definition seclabel.c:37
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:205
Form_pg_class rd_rel
Definition rel.h:111

References check_object_ownership(), ereport, errcode(), errdetail_relkind_not_supported(), errmsg(), ERROR, fb(), get_object_address(), GetUserId(), label_provider_list, lfirst, linitial, list_length(), NIL, NoLock, OBJECT_COLUMN, RelationData::rd_rel, relation_close(), RelationGetRelationName, SecLabelSupportsObjectType(), SetSecurityLabel(), ShareUpdateExclusiveLock, and stmt.

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

◆ GetSecurityLabel()

char * GetSecurityLabel ( const ObjectAddress object,
const char provider 
)
extern

Definition at line 272 of file seclabel.c.

273{
275 ScanKeyData keys[4];
276 SysScanDesc scan;
277 HeapTuple tuple;
278 Datum datum;
279 bool isnull;
280 char *seclabel = NULL;
281
282 /* Shared objects have their own security label catalog. */
283 if (IsSharedRelation(object->classId))
284 return GetSharedSecurityLabel(object, provider);
285
286 /* Must be an unshared object, so examine pg_seclabel. */
287 ScanKeyInit(&keys[0],
290 ObjectIdGetDatum(object->objectId));
291 ScanKeyInit(&keys[1],
294 ObjectIdGetDatum(object->classId));
295 ScanKeyInit(&keys[2],
298 Int32GetDatum(object->objectSubId));
299 ScanKeyInit(&keys[3],
303
305
307 NULL, 4, keys);
308
309 tuple = systable_getnext(scan);
310 if (HeapTupleIsValid(tuple))
311 {
313 RelationGetDescr(pg_seclabel), &isnull);
314 if (!isnull)
316 }
317 systable_endscan(scan);
318
320
321 return seclabel;
322}
#define CStringGetTextDatum(s)
Definition builtins.h:97
#define TextDatumGetCString(d)
Definition builtins.h:98
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
#define AccessShareLock
Definition lockdefs.h:36
uint64_t Datum
Definition postgres.h:70
#define RelationGetDescr(relation)
Definition rel.h:540
static char * GetSharedSecurityLabel(const ObjectAddress *object, const char *provider)
Definition seclabel.c:224

References AccessShareLock, BTEqualStrategyNumber, ObjectAddress::classId, CStringGetTextDatum, fb(), GetSharedSecurityLabel(), heap_getattr(), HeapTupleIsValid, Int32GetDatum(), IsSharedRelation(), ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, RelationGetDescr, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), and TextDatumGetCString.

Referenced by sepgsql_avc_check_perms(), sepgsql_avc_trusted_proc(), and sepgsql_get_label().

◆ register_label_provider()

void register_label_provider ( const char provider_name,
check_object_relabel_type  hook 
)
extern

Definition at line 570 of file seclabel.c.

571{
574
577 provider->provider_name = pstrdup(provider_name);
578 provider->hook = hook;
581}
#define palloc_object(type)
Definition fe_memutils.h:74
List * lappend(List *list, void *datum)
Definition list.c:339
char * pstrdup(const char *in)
Definition mcxt.c:1781
MemoryContext TopMemoryContext
Definition mcxt.c:166
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124

References fb(), label_provider_list, lappend(), MemoryContextSwitchTo(), palloc_object, pstrdup(), and TopMemoryContext.

Referenced by _PG_init().

◆ SetSecurityLabel()

void SetSecurityLabel ( const ObjectAddress object,
const char provider,
const char label 
)
extern

Definition at line 404 of file seclabel.c.

406{
408 ScanKeyData keys[4];
409 SysScanDesc scan;
413 bool nulls[Natts_pg_seclabel];
415
416 /* Shared objects have their own security label catalog. */
417 if (IsSharedRelation(object->classId))
418 {
420 return;
421 }
422
423 /* Prepare to form or update a tuple, if necessary. */
424 memset(nulls, false, sizeof(nulls));
425 memset(replaces, false, sizeof(replaces));
430 if (label != NULL)
432
433 /* Use the index to search for a matching old tuple */
434 ScanKeyInit(&keys[0],
437 ObjectIdGetDatum(object->objectId));
438 ScanKeyInit(&keys[1],
441 ObjectIdGetDatum(object->classId));
442 ScanKeyInit(&keys[2],
445 Int32GetDatum(object->objectSubId));
446 ScanKeyInit(&keys[3],
450
452
454 NULL, 4, keys);
455
456 oldtup = systable_getnext(scan);
458 {
459 if (label == NULL)
461 else
462 {
465 values, nulls, replaces);
467 }
468 }
469 systable_endscan(scan);
470
471 /* If we didn't find an old tuple, insert a new one */
472 if (newtup == NULL && label != NULL)
473 {
475 values, nulls);
477 }
478
479 /* Update indexes, if necessary */
480 if (newtup != NULL)
482
484}
static Datum values[MAXATTR]
Definition bootstrap.c:155
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1210
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1435
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition indexing.c:233
static char * label
static void SetSharedSecurityLabel(const ObjectAddress *object, const char *provider, const char *label)
Definition seclabel.c:329

References BTEqualStrategyNumber, CatalogTupleDelete(), CatalogTupleInsert(), CatalogTupleUpdate(), ObjectAddress::classId, CStringGetTextDatum, fb(), heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, Int32GetDatum(), IsSharedRelation(), label, ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, RelationGetDescr, RowExclusiveLock, ScanKeyInit(), SetSharedSecurityLabel(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), and values.

Referenced by exec_object_restorecon(), ExecSecLabelStmt(), sepgsql_attribute_post_create(), sepgsql_database_post_create(), sepgsql_proc_post_create(), sepgsql_relation_post_create(), and sepgsql_schema_post_create().