PostgreSQL Source Code git master
database.c
Go to the documentation of this file.
1/* -------------------------------------------------------------------------
2 *
3 * contrib/sepgsql/database.c
4 *
5 * Routines corresponding to database objects
6 *
7 * Copyright (c) 2010-2025, 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/pg_database.h"
19#include "commands/seclabel.h"
20#include "sepgsql.h"
21#include "utils/builtins.h"
22#include "utils/fmgroids.h"
23#include "utils/snapmgr.h"
24
25/*
26 * sepgsql_database_post_create
27 *
28 * This routine assigns a default security label on a newly defined
29 * database, and check permission needed for its creation.
30 */
31void
32sepgsql_database_post_create(Oid databaseId, const char *dtemplate)
33{
34 Relation rel;
35 ScanKeyData skey;
36 SysScanDesc sscan;
37 HeapTuple tuple;
38 char *tcontext;
39 char *ncontext;
40 ObjectAddress object;
41 Form_pg_database datForm;
42 StringInfoData audit_name;
43
44 /*
45 * Oid of the source database is not saved in pg_database catalog, so we
46 * collect its identifier using contextual information. If NULL, its
47 * default is "template1" according to createdb().
48 */
49 if (!dtemplate)
50 dtemplate = "template1";
51
52 object.classId = DatabaseRelationId;
53 object.objectId = get_database_oid(dtemplate, false);
54 object.objectSubId = 0;
55
56 tcontext = sepgsql_get_label(object.classId,
57 object.objectId,
58 object.objectSubId);
59
60 /*
61 * check db_database:{getattr} permission
62 */
63 initStringInfo(&audit_name);
64 appendStringInfoString(&audit_name, quote_identifier(dtemplate));
68 audit_name.data,
69 true);
70
71 /*
72 * Compute a default security label of the newly created database based on
73 * a pair of security label of client and source database.
74 *
75 * XXX - upcoming version of libselinux supports to take object name to
76 * handle special treatment on default security label.
77 */
78 rel = table_open(DatabaseRelationId, AccessShareLock);
79
80 ScanKeyInit(&skey,
81 Anum_pg_database_oid,
82 BTEqualStrategyNumber, F_OIDEQ,
83 ObjectIdGetDatum(databaseId));
84
85 sscan = systable_beginscan(rel, DatabaseOidIndexId, true,
86 SnapshotSelf, 1, &skey);
87 tuple = systable_getnext(sscan);
88 if (!HeapTupleIsValid(tuple))
89 elog(ERROR, "could not find tuple for database %u", databaseId);
90
91 datForm = (Form_pg_database) GETSTRUCT(tuple);
92
94 tcontext,
96 NameStr(datForm->datname));
97
98 /*
99 * check db_database:{create} permission
100 */
101 resetStringInfo(&audit_name);
102 appendStringInfoString(&audit_name,
103 quote_identifier(NameStr(datForm->datname)));
107 audit_name.data,
108 true);
109
110 systable_endscan(sscan);
112
113 /*
114 * Assign the default security label on the new database
115 */
116 object.classId = DatabaseRelationId;
117 object.objectId = databaseId;
118 object.objectSubId = 0;
119
120 SetSecurityLabel(&object, SEPGSQL_LABEL_TAG, ncontext);
121
122 pfree(ncontext);
123 pfree(tcontext);
124}
125
126/*
127 * sepgsql_database_drop
128 *
129 * It checks privileges to drop the supplied database
130 */
131void
133{
134 ObjectAddress object;
135 char *audit_name;
136
137 /*
138 * check db_database:{drop} permission
139 */
140 object.classId = DatabaseRelationId;
141 object.objectId = databaseId;
142 object.objectSubId = 0;
143 audit_name = getObjectIdentity(&object, false);
144
148 audit_name,
149 true);
150 pfree(audit_name);
151}
152
153/*
154 * sepgsql_database_post_alter
155 *
156 * It checks privileges to alter the supplied database
157 */
158void
160{
161 ObjectAddress object;
162 char *audit_name;
163
164 /*
165 * check db_database:{setattr} permission
166 */
167 object.classId = DatabaseRelationId;
168 object.objectId = databaseId;
169 object.objectSubId = 0;
170 audit_name = getObjectIdentity(&object, false);
171
175 audit_name,
176 true);
177 pfree(audit_name);
178}
179
180/*
181 * sepgsql_database_relabel
182 *
183 * It checks privileges to relabel the supplied database with the `seclabel'
184 */
185void
186sepgsql_database_relabel(Oid databaseId, const char *seclabel)
187{
188 ObjectAddress object;
189 char *audit_name;
190
191 object.classId = DatabaseRelationId;
192 object.objectId = databaseId;
193 object.objectSubId = 0;
194 audit_name = getObjectIdentity(&object, false);
195
196 /*
197 * check db_database:{setattr relabelfrom} permission
198 */
203 audit_name,
204 true);
205
206 /*
207 * check db_database:{relabelto} permission
208 */
212 audit_name,
213 true);
214 pfree(audit_name);
215}
#define NameStr(name)
Definition: c.h:751
void sepgsql_database_relabel(Oid databaseId, const char *seclabel)
Definition: database.c:186
void sepgsql_database_post_create(Oid databaseId, const char *dtemplate)
Definition: database.c:32
void sepgsql_database_drop(Oid databaseId)
Definition: database.c:132
void sepgsql_database_setattr(Oid databaseId)
Definition: database.c:159
Oid get_database_oid(const char *dbname, bool missing_ok)
Definition: dbcommands.c:3167
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
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
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
char * sepgsql_get_label(Oid classId, Oid objectId, int32 subId)
Definition: label.c:444
char * sepgsql_get_client_label(void)
Definition: label.c:79
#define AccessShareLock
Definition: lockdefs.h:36
void pfree(void *pointer)
Definition: mcxt.c:1594
char * getObjectIdentity(const ObjectAddress *object, bool missing_ok)
FormData_pg_database * Form_pg_database
Definition: pg_database.h:96
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
unsigned int Oid
Definition: postgres_ext.h:32
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:13058
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
void SetSecurityLabel(const ObjectAddress *object, const char *provider, const char *label)
Definition: seclabel.c:404
char * sepgsql_compute_create(const char *scontext, const char *tcontext, uint16 tclass, const char *objname)
Definition: selinux.c:842
#define SEPG_DB_DATABASE__CREATE
Definition: sepgsql.h:118
#define SEPG_CLASS_DB_DATABASE
Definition: sepgsql.h:44
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 SEPG_DB_DATABASE__SETATTR
Definition: sepgsql.h:121
#define SEPG_DB_DATABASE__RELABELTO
Definition: sepgsql.h:123
#define SEPG_DB_DATABASE__DROP
Definition: sepgsql.h:119
#define SEPG_DB_DATABASE__GETATTR
Definition: sepgsql.h:120
#define SEPGSQL_LABEL_TAG
Definition: sepgsql.h:23
#define SEPG_DB_DATABASE__RELABELFROM
Definition: sepgsql.h:122
bool sepgsql_avc_check_perms(const ObjectAddress *tobject, uint16 tclass, uint32 required, const char *audit_name, bool abort_on_violation)
Definition: uavc.c:420
#define SnapshotSelf
Definition: snapmgr.h:32
#define BTEqualStrategyNumber
Definition: stratnum.h:31
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:126
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40