2011-01-24 09:44:48 +08:00
|
|
|
/* -------------------------------------------------------------------------
|
|
|
|
*
|
2014-04-01 14:27:37 +08:00
|
|
|
* contrib/sepgsql/relation.c
|
2011-01-24 09:44:48 +08:00
|
|
|
*
|
|
|
|
* Routines corresponding to relation/attribute objects
|
|
|
|
*
|
2022-01-08 08:04:57 +08:00
|
|
|
* Copyright (c) 2010-2022, PostgreSQL Global Development Group
|
2011-01-24 09:44:48 +08:00
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2019-12-27 07:09:00 +08:00
|
|
|
#include "access/genam.h"
|
2012-09-06 02:01:15 +08:00
|
|
|
#include "access/htup_details.h"
|
2011-01-24 09:44:48 +08:00
|
|
|
#include "access/sysattr.h"
|
2019-01-22 02:18:20 +08:00
|
|
|
#include "access/table.h"
|
2011-02-03 12:39:43 +08:00
|
|
|
#include "catalog/dependency.h"
|
2011-01-24 09:44:48 +08:00
|
|
|
#include "catalog/pg_attribute.h"
|
|
|
|
#include "catalog/pg_class.h"
|
|
|
|
#include "catalog/pg_namespace.h"
|
|
|
|
#include "commands/seclabel.h"
|
2013-04-12 20:35:55 +08:00
|
|
|
#include "lib/stringinfo.h"
|
2019-10-23 11:56:22 +08:00
|
|
|
#include "sepgsql.h"
|
2013-04-12 20:35:55 +08:00
|
|
|
#include "utils/builtins.h"
|
2012-09-06 02:01:15 +08:00
|
|
|
#include "utils/catcache.h"
|
2019-10-23 11:56:22 +08:00
|
|
|
#include "utils/fmgroids.h"
|
2011-01-24 09:44:48 +08:00
|
|
|
#include "utils/lsyscache.h"
|
2012-10-24 05:07:26 +08:00
|
|
|
#include "utils/rel.h"
|
2019-01-22 09:03:15 +08:00
|
|
|
#include "utils/snapmgr.h"
|
2012-03-10 04:18:45 +08:00
|
|
|
#include "utils/syscache.h"
|
2011-01-24 09:44:48 +08:00
|
|
|
|
2012-10-24 05:07:26 +08:00
|
|
|
static void sepgsql_index_modify(Oid indexOid);
|
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
/*
|
|
|
|
* sepgsql_attribute_post_create
|
|
|
|
*
|
|
|
|
* This routine assigns a default security label on a newly defined
|
|
|
|
* column, using ALTER TABLE ... ADD COLUMN.
|
|
|
|
* Note that this routine is not invoked in the case of CREATE TABLE,
|
|
|
|
* although it also defines columns in addition to table.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_attribute_post_create(Oid relOid, AttrNumber attnum)
|
|
|
|
{
|
2011-12-21 22:12:43 +08:00
|
|
|
Relation rel;
|
|
|
|
ScanKeyData skey[2];
|
|
|
|
SysScanDesc sscan;
|
|
|
|
HeapTuple tuple;
|
|
|
|
char *scontext;
|
2011-01-24 09:44:48 +08:00
|
|
|
char *tcontext;
|
|
|
|
char *ncontext;
|
|
|
|
ObjectAddress object;
|
2011-12-21 22:12:43 +08:00
|
|
|
Form_pg_attribute attForm;
|
2013-04-12 20:35:55 +08:00
|
|
|
StringInfoData audit_name;
|
2017-04-10 05:01:58 +08:00
|
|
|
char relkind = get_rel_relkind(relOid);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
/*
|
2017-04-10 05:01:58 +08:00
|
|
|
* Only attributes within regular relations or partition relations have
|
|
|
|
* individual security labels.
|
2011-01-24 09:44:48 +08:00
|
|
|
*/
|
2017-04-10 05:01:58 +08:00
|
|
|
if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE)
|
2011-01-24 09:44:48 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
2011-12-21 22:12:43 +08:00
|
|
|
* Compute a default security label of the new column underlying the
|
|
|
|
* specified relation, and check permission to create it.
|
2011-01-24 09:44:48 +08:00
|
|
|
*/
|
2019-01-22 02:32:19 +08:00
|
|
|
rel = table_open(AttributeRelationId, AccessShareLock);
|
2011-12-21 22:12:43 +08:00
|
|
|
|
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
Anum_pg_attribute_attrelid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(relOid));
|
|
|
|
ScanKeyInit(&skey[1],
|
|
|
|
Anum_pg_attribute_attnum,
|
|
|
|
BTEqualStrategyNumber, F_INT2EQ,
|
|
|
|
Int16GetDatum(attnum));
|
|
|
|
|
|
|
|
sscan = systable_beginscan(rel, AttributeRelidNumIndexId, true,
|
|
|
|
SnapshotSelf, 2, &skey[0]);
|
|
|
|
|
|
|
|
tuple = systable_getnext(sscan);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2017-06-05 04:20:03 +08:00
|
|
|
elog(ERROR, "could not find tuple for column %d of relation %u",
|
2011-12-21 22:12:43 +08:00
|
|
|
attnum, relOid);
|
|
|
|
|
|
|
|
attForm = (Form_pg_attribute) GETSTRUCT(tuple);
|
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
scontext = sepgsql_get_client_label();
|
|
|
|
tcontext = sepgsql_get_label(RelationRelationId, relOid, 0);
|
|
|
|
ncontext = sepgsql_compute_create(scontext, tcontext,
|
2013-03-29 03:38:35 +08:00
|
|
|
SEPG_CLASS_DB_COLUMN,
|
|
|
|
NameStr(attForm->attname));
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-12-21 22:12:43 +08:00
|
|
|
/*
|
|
|
|
* check db_column:{create} permission
|
|
|
|
*/
|
2013-04-12 20:35:55 +08:00
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = 0;
|
|
|
|
|
|
|
|
initStringInfo(&audit_name);
|
|
|
|
appendStringInfo(&audit_name, "%s.%s",
|
2020-07-15 08:03:10 +08:00
|
|
|
getObjectIdentity(&object, false),
|
2013-04-12 20:35:55 +08:00
|
|
|
quote_identifier(NameStr(attForm->attname)));
|
2011-12-21 22:12:43 +08:00
|
|
|
sepgsql_avc_check_perms_label(ncontext,
|
|
|
|
SEPG_CLASS_DB_COLUMN,
|
|
|
|
SEPG_DB_COLUMN__CREATE,
|
2013-04-12 20:35:55 +08:00
|
|
|
audit_name.data,
|
2011-12-21 22:12:43 +08:00
|
|
|
true);
|
2011-04-10 23:42:00 +08:00
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
/*
|
|
|
|
* Assign the default security label on a new procedure
|
|
|
|
*/
|
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = attnum;
|
|
|
|
SetSecurityLabel(&object, SEPGSQL_LABEL_TAG, ncontext);
|
|
|
|
|
2011-12-21 22:12:43 +08:00
|
|
|
systable_endscan(sscan);
|
2019-01-22 02:32:19 +08:00
|
|
|
table_close(rel, AccessShareLock);
|
2011-12-21 22:12:43 +08:00
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
pfree(tcontext);
|
|
|
|
pfree(ncontext);
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:18:45 +08:00
|
|
|
/*
|
|
|
|
* sepgsql_attribute_drop
|
|
|
|
*
|
|
|
|
* It checks privileges to drop the supplied column.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_attribute_drop(Oid relOid, AttrNumber attnum)
|
|
|
|
{
|
|
|
|
ObjectAddress object;
|
|
|
|
char *audit_name;
|
2017-04-10 05:01:58 +08:00
|
|
|
char relkind = get_rel_relkind(relOid);
|
2012-03-10 04:18:45 +08:00
|
|
|
|
2017-04-10 05:01:58 +08:00
|
|
|
if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE)
|
2012-03-10 04:18:45 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_column:{drop} permission
|
|
|
|
*/
|
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = attnum;
|
2020-07-15 08:03:10 +08:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2012-03-10 04:18:45 +08:00
|
|
|
|
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
SEPG_CLASS_DB_COLUMN,
|
|
|
|
SEPG_DB_COLUMN__DROP,
|
|
|
|
audit_name,
|
|
|
|
true);
|
|
|
|
pfree(audit_name);
|
|
|
|
}
|
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
/*
|
|
|
|
* sepgsql_attribute_relabel
|
|
|
|
*
|
|
|
|
* It checks privileges to relabel the supplied column
|
|
|
|
* by the `seclabel'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_attribute_relabel(Oid relOid, AttrNumber attnum,
|
|
|
|
const char *seclabel)
|
|
|
|
{
|
2011-02-03 12:39:43 +08:00
|
|
|
ObjectAddress object;
|
2011-09-01 20:37:33 +08:00
|
|
|
char *audit_name;
|
2017-04-10 05:01:58 +08:00
|
|
|
char relkind = get_rel_relkind(relOid);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
2017-04-10 05:01:58 +08:00
|
|
|
if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE)
|
2011-01-24 09:44:48 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("cannot set security label on non-regular columns")));
|
|
|
|
|
2011-02-03 12:39:43 +08:00
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = attnum;
|
2020-07-15 08:03:10 +08:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_column:{setattr relabelfrom} permission
|
|
|
|
*/
|
2011-09-01 20:37:33 +08:00
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
SEPG_CLASS_DB_COLUMN,
|
|
|
|
SEPG_DB_COLUMN__SETATTR |
|
|
|
|
SEPG_DB_COLUMN__RELABELFROM,
|
|
|
|
audit_name,
|
|
|
|
true);
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
/*
|
|
|
|
* check db_column:{relabelto} permission
|
|
|
|
*/
|
2011-09-01 20:37:33 +08:00
|
|
|
sepgsql_avc_check_perms_label(seclabel,
|
|
|
|
SEPG_CLASS_DB_COLUMN,
|
|
|
|
SEPG_DB_PROCEDURE__RELABELTO,
|
|
|
|
audit_name,
|
|
|
|
true);
|
2011-02-03 12:39:43 +08:00
|
|
|
pfree(audit_name);
|
2011-01-24 09:44:48 +08:00
|
|
|
}
|
|
|
|
|
2013-03-27 20:10:14 +08:00
|
|
|
/*
|
|
|
|
* sepgsql_attribute_setattr
|
|
|
|
*
|
|
|
|
* It checks privileges to alter the supplied column.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_attribute_setattr(Oid relOid, AttrNumber attnum)
|
|
|
|
{
|
|
|
|
ObjectAddress object;
|
|
|
|
char *audit_name;
|
2017-04-10 05:01:58 +08:00
|
|
|
char relkind = get_rel_relkind(relOid);
|
2013-03-27 20:10:14 +08:00
|
|
|
|
2017-04-10 05:01:58 +08:00
|
|
|
if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE)
|
2013-03-27 20:10:14 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_column:{setattr} permission
|
|
|
|
*/
|
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = attnum;
|
2020-07-15 08:03:10 +08:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2013-03-27 20:10:14 +08:00
|
|
|
|
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
SEPG_CLASS_DB_COLUMN,
|
|
|
|
SEPG_DB_COLUMN__SETATTR,
|
|
|
|
audit_name,
|
|
|
|
true);
|
|
|
|
pfree(audit_name);
|
|
|
|
}
|
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
/*
|
|
|
|
* sepgsql_relation_post_create
|
|
|
|
*
|
|
|
|
* The post creation hook of relation/attribute
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_relation_post_create(Oid relOid)
|
|
|
|
{
|
|
|
|
Relation rel;
|
|
|
|
ScanKeyData skey;
|
|
|
|
SysScanDesc sscan;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Form_pg_class classForm;
|
|
|
|
ObjectAddress object;
|
2017-04-07 05:28:19 +08:00
|
|
|
uint16_t tclass;
|
2011-01-24 09:44:48 +08:00
|
|
|
char *scontext; /* subject */
|
|
|
|
char *tcontext; /* schema */
|
|
|
|
char *rcontext; /* relation */
|
|
|
|
char *ccontext; /* column */
|
2013-04-12 20:35:55 +08:00
|
|
|
char *nsp_name;
|
|
|
|
StringInfoData audit_name;
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch catalog record of the new relation. Because pg_class entry is not
|
|
|
|
* visible right now, we need to scan the catalog using SnapshotSelf.
|
|
|
|
*/
|
2019-01-22 02:32:19 +08:00
|
|
|
rel = table_open(RelationRelationId, AccessShareLock);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
ScanKeyInit(&skey,
|
2018-11-21 12:03:46 +08:00
|
|
|
Anum_pg_class_oid,
|
2011-01-24 09:44:48 +08:00
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(relOid));
|
|
|
|
|
|
|
|
sscan = systable_beginscan(rel, ClassOidIndexId, true,
|
|
|
|
SnapshotSelf, 1, &skey);
|
|
|
|
|
|
|
|
tuple = systable_getnext(sscan);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2017-06-05 04:20:03 +08:00
|
|
|
elog(ERROR, "could not find tuple for relation %u", relOid);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
classForm = (Form_pg_class) GETSTRUCT(tuple);
|
|
|
|
|
2012-10-24 05:07:26 +08:00
|
|
|
/* ignore indexes on toast tables */
|
|
|
|
if (classForm->relkind == RELKIND_INDEX &&
|
|
|
|
classForm->relnamespace == PG_TOAST_NAMESPACE)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_schema:{add_name} permission of the namespace
|
|
|
|
*/
|
|
|
|
object.classId = NamespaceRelationId;
|
|
|
|
object.objectId = classForm->relnamespace;
|
|
|
|
object.objectSubId = 0;
|
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
SEPG_DB_SCHEMA__ADD_NAME,
|
2020-07-15 08:03:10 +08:00
|
|
|
getObjectIdentity(&object, false),
|
2012-10-24 05:07:26 +08:00
|
|
|
true);
|
|
|
|
|
2011-12-21 22:12:43 +08:00
|
|
|
switch (classForm->relkind)
|
|
|
|
{
|
|
|
|
case RELKIND_RELATION:
|
2017-04-10 05:01:58 +08:00
|
|
|
case RELKIND_PARTITIONED_TABLE:
|
2011-12-21 22:12:43 +08:00
|
|
|
tclass = SEPG_CLASS_DB_TABLE;
|
|
|
|
break;
|
|
|
|
case RELKIND_SEQUENCE:
|
|
|
|
tclass = SEPG_CLASS_DB_SEQUENCE;
|
|
|
|
break;
|
|
|
|
case RELKIND_VIEW:
|
|
|
|
tclass = SEPG_CLASS_DB_VIEW;
|
|
|
|
break;
|
2012-10-24 05:07:26 +08:00
|
|
|
case RELKIND_INDEX:
|
|
|
|
/* deal with indexes specially; no need for tclass */
|
|
|
|
sepgsql_index_modify(relOid);
|
|
|
|
goto out;
|
2011-12-21 22:12:43 +08:00
|
|
|
default:
|
2012-10-24 05:07:26 +08:00
|
|
|
/* ignore other relkinds */
|
2011-12-21 22:12:43 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute a default security label when we create a new relation object
|
|
|
|
* under the specified namespace.
|
|
|
|
*/
|
|
|
|
scontext = sepgsql_get_client_label();
|
|
|
|
tcontext = sepgsql_get_label(NamespaceRelationId,
|
|
|
|
classForm->relnamespace, 0);
|
2013-03-29 03:38:35 +08:00
|
|
|
rcontext = sepgsql_compute_create(scontext, tcontext, tclass,
|
|
|
|
NameStr(classForm->relname));
|
2011-01-24 09:44:48 +08:00
|
|
|
|
2011-12-21 22:12:43 +08:00
|
|
|
/*
|
|
|
|
* check db_xxx:{create} permission
|
|
|
|
*/
|
2013-04-12 20:35:55 +08:00
|
|
|
nsp_name = get_namespace_name(classForm->relnamespace);
|
|
|
|
initStringInfo(&audit_name);
|
|
|
|
appendStringInfo(&audit_name, "%s.%s",
|
|
|
|
quote_identifier(nsp_name),
|
|
|
|
quote_identifier(NameStr(classForm->relname)));
|
2011-12-21 22:12:43 +08:00
|
|
|
sepgsql_avc_check_perms_label(rcontext,
|
|
|
|
tclass,
|
|
|
|
SEPG_DB_DATABASE__CREATE,
|
2013-04-12 20:35:55 +08:00
|
|
|
audit_name.data,
|
2011-12-21 22:12:43 +08:00
|
|
|
true);
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
/*
|
2017-04-10 05:01:58 +08:00
|
|
|
* Assign the default security label on the new regular or partitioned
|
|
|
|
* relation.
|
2011-01-24 09:44:48 +08:00
|
|
|
*/
|
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = 0;
|
|
|
|
SetSecurityLabel(&object, SEPGSQL_LABEL_TAG, rcontext);
|
|
|
|
|
|
|
|
/*
|
2017-04-10 05:01:58 +08:00
|
|
|
* We also assign a default security label on columns of a new table.
|
2011-01-24 09:44:48 +08:00
|
|
|
*/
|
2017-04-10 05:01:58 +08:00
|
|
|
if (classForm->relkind == RELKIND_RELATION ||
|
|
|
|
classForm->relkind == RELKIND_PARTITIONED_TABLE)
|
2011-01-24 09:44:48 +08:00
|
|
|
{
|
2011-12-21 22:12:43 +08:00
|
|
|
Relation arel;
|
|
|
|
ScanKeyData akey;
|
|
|
|
SysScanDesc ascan;
|
|
|
|
HeapTuple atup;
|
|
|
|
Form_pg_attribute attForm;
|
|
|
|
|
2019-01-22 02:32:19 +08:00
|
|
|
arel = table_open(AttributeRelationId, AccessShareLock);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
2011-12-21 22:12:43 +08:00
|
|
|
ScanKeyInit(&akey,
|
|
|
|
Anum_pg_attribute_attrelid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(relOid));
|
|
|
|
|
|
|
|
ascan = systable_beginscan(arel, AttributeRelidNumIndexId, true,
|
|
|
|
SnapshotSelf, 1, &akey);
|
|
|
|
|
|
|
|
while (HeapTupleIsValid(atup = systable_getnext(ascan)))
|
2011-01-24 09:44:48 +08:00
|
|
|
{
|
2011-12-21 22:12:43 +08:00
|
|
|
attForm = (Form_pg_attribute) GETSTRUCT(atup);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
2013-04-12 20:35:55 +08:00
|
|
|
resetStringInfo(&audit_name);
|
|
|
|
appendStringInfo(&audit_name, "%s.%s.%s",
|
|
|
|
quote_identifier(nsp_name),
|
|
|
|
quote_identifier(NameStr(classForm->relname)),
|
|
|
|
quote_identifier(NameStr(attForm->attname)));
|
2011-12-21 22:12:43 +08:00
|
|
|
|
|
|
|
ccontext = sepgsql_compute_create(scontext,
|
|
|
|
rcontext,
|
2013-03-29 03:38:35 +08:00
|
|
|
SEPG_CLASS_DB_COLUMN,
|
|
|
|
NameStr(attForm->attname));
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-12-21 22:12:43 +08:00
|
|
|
/*
|
|
|
|
* check db_column:{create} permission
|
|
|
|
*/
|
|
|
|
sepgsql_avc_check_perms_label(ccontext,
|
|
|
|
SEPG_CLASS_DB_COLUMN,
|
|
|
|
SEPG_DB_COLUMN__CREATE,
|
2013-04-12 20:35:55 +08:00
|
|
|
audit_name.data,
|
2011-12-21 22:12:43 +08:00
|
|
|
true);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
2011-12-21 22:12:43 +08:00
|
|
|
object.objectSubId = attForm->attnum;
|
2011-01-24 09:44:48 +08:00
|
|
|
SetSecurityLabel(&object, SEPGSQL_LABEL_TAG, ccontext);
|
2011-12-21 22:12:43 +08:00
|
|
|
|
|
|
|
pfree(ccontext);
|
2011-01-24 09:44:48 +08:00
|
|
|
}
|
2011-12-21 22:12:43 +08:00
|
|
|
systable_endscan(ascan);
|
2019-01-22 02:32:19 +08:00
|
|
|
table_close(arel, AccessShareLock);
|
2011-01-24 09:44:48 +08:00
|
|
|
}
|
|
|
|
pfree(rcontext);
|
2012-10-24 05:07:26 +08:00
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
out:
|
|
|
|
systable_endscan(sscan);
|
2019-01-22 02:32:19 +08:00
|
|
|
table_close(rel, AccessShareLock);
|
2011-01-24 09:44:48 +08:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:18:45 +08:00
|
|
|
/*
|
|
|
|
* sepgsql_relation_drop
|
|
|
|
*
|
|
|
|
* It checks privileges to drop the supplied relation.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_relation_drop(Oid relOid)
|
|
|
|
{
|
|
|
|
ObjectAddress object;
|
|
|
|
char *audit_name;
|
2017-04-07 05:28:19 +08:00
|
|
|
uint16_t tclass = 0;
|
2017-04-10 05:01:58 +08:00
|
|
|
char relkind = get_rel_relkind(relOid);
|
2012-03-10 04:18:45 +08:00
|
|
|
|
2012-10-24 05:07:26 +08:00
|
|
|
switch (relkind)
|
|
|
|
{
|
|
|
|
case RELKIND_RELATION:
|
2017-04-10 05:01:58 +08:00
|
|
|
case RELKIND_PARTITIONED_TABLE:
|
2012-10-24 05:07:26 +08:00
|
|
|
tclass = SEPG_CLASS_DB_TABLE;
|
|
|
|
break;
|
|
|
|
case RELKIND_SEQUENCE:
|
|
|
|
tclass = SEPG_CLASS_DB_SEQUENCE;
|
|
|
|
break;
|
|
|
|
case RELKIND_VIEW:
|
|
|
|
tclass = SEPG_CLASS_DB_VIEW;
|
|
|
|
break;
|
|
|
|
case RELKIND_INDEX:
|
|
|
|
/* ignore indexes on toast tables */
|
|
|
|
if (get_rel_namespace(relOid) == PG_TOAST_NAMESPACE)
|
|
|
|
return;
|
|
|
|
/* other indexes are handled specially below; no need for tclass */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* ignore other relkinds */
|
|
|
|
return;
|
|
|
|
}
|
2012-03-10 04:18:45 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_schema:{remove_name} permission
|
|
|
|
*/
|
|
|
|
object.classId = NamespaceRelationId;
|
|
|
|
object.objectId = get_rel_namespace(relOid);
|
|
|
|
object.objectSubId = 0;
|
2020-07-15 08:03:10 +08:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2012-03-10 04:18:45 +08:00
|
|
|
|
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
SEPG_DB_SCHEMA__REMOVE_NAME,
|
|
|
|
audit_name,
|
|
|
|
true);
|
|
|
|
pfree(audit_name);
|
|
|
|
|
2012-10-24 05:07:26 +08:00
|
|
|
/* deal with indexes specially */
|
|
|
|
if (relkind == RELKIND_INDEX)
|
|
|
|
{
|
|
|
|
sepgsql_index_modify(relOid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:18:45 +08:00
|
|
|
/*
|
|
|
|
* check db_table/sequence/view:{drop} permission
|
|
|
|
*/
|
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = 0;
|
2020-07-15 08:03:10 +08:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2012-03-10 04:18:45 +08:00
|
|
|
|
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
tclass,
|
|
|
|
SEPG_DB_TABLE__DROP,
|
|
|
|
audit_name,
|
|
|
|
true);
|
|
|
|
pfree(audit_name);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_column:{drop} permission
|
|
|
|
*/
|
2017-04-10 05:01:58 +08:00
|
|
|
if (relkind == RELKIND_RELATION || relkind == RELKIND_PARTITIONED_TABLE)
|
2012-03-10 04:18:45 +08:00
|
|
|
{
|
|
|
|
Form_pg_attribute attForm;
|
|
|
|
CatCList *attrList;
|
|
|
|
HeapTuple atttup;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
attrList = SearchSysCacheList1(ATTNUM, ObjectIdGetDatum(relOid));
|
|
|
|
for (i = 0; i < attrList->n_members; i++)
|
|
|
|
{
|
|
|
|
atttup = &attrList->members[i]->tuple;
|
|
|
|
attForm = (Form_pg_attribute) GETSTRUCT(atttup);
|
|
|
|
|
|
|
|
if (attForm->attisdropped)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = attForm->attnum;
|
2020-07-15 08:03:10 +08:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2012-03-10 04:18:45 +08:00
|
|
|
|
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
SEPG_CLASS_DB_COLUMN,
|
|
|
|
SEPG_DB_COLUMN__DROP,
|
|
|
|
audit_name,
|
|
|
|
true);
|
|
|
|
pfree(audit_name);
|
|
|
|
}
|
|
|
|
ReleaseCatCacheList(attrList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 23:41:52 +08:00
|
|
|
/*
|
|
|
|
* sepgsql_relation_truncate
|
|
|
|
*
|
|
|
|
* Check privileges to TRUNCATE the supplied relation.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_relation_truncate(Oid relOid)
|
|
|
|
{
|
|
|
|
ObjectAddress object;
|
|
|
|
char *audit_name;
|
|
|
|
uint16_t tclass = 0;
|
|
|
|
char relkind = get_rel_relkind(relOid);
|
|
|
|
|
|
|
|
switch (relkind)
|
|
|
|
{
|
|
|
|
case RELKIND_RELATION:
|
|
|
|
case RELKIND_PARTITIONED_TABLE:
|
|
|
|
tclass = SEPG_CLASS_DB_TABLE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* ignore other relkinds */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_table:{truncate} permission
|
|
|
|
*/
|
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = 0;
|
2020-07-15 08:42:21 +08:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2019-11-23 23:41:52 +08:00
|
|
|
|
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
tclass,
|
|
|
|
SEPG_DB_TABLE__TRUNCATE,
|
|
|
|
audit_name,
|
|
|
|
true);
|
|
|
|
pfree(audit_name);
|
|
|
|
}
|
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
/*
|
|
|
|
* sepgsql_relation_relabel
|
|
|
|
*
|
|
|
|
* It checks privileges to relabel the supplied relation by the `seclabel'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_relation_relabel(Oid relOid, const char *seclabel)
|
|
|
|
{
|
2011-09-01 20:37:33 +08:00
|
|
|
ObjectAddress object;
|
2011-01-24 09:44:48 +08:00
|
|
|
char *audit_name;
|
2017-04-10 05:01:58 +08:00
|
|
|
char relkind = get_rel_relkind(relOid);
|
2011-01-24 09:44:48 +08:00
|
|
|
uint16_t tclass = 0;
|
|
|
|
|
2017-04-10 05:01:58 +08:00
|
|
|
if (relkind == RELKIND_RELATION || relkind == RELKIND_PARTITIONED_TABLE)
|
2011-01-24 09:44:48 +08:00
|
|
|
tclass = SEPG_CLASS_DB_TABLE;
|
|
|
|
else if (relkind == RELKIND_SEQUENCE)
|
|
|
|
tclass = SEPG_CLASS_DB_SEQUENCE;
|
|
|
|
else if (relkind == RELKIND_VIEW)
|
|
|
|
tclass = SEPG_CLASS_DB_VIEW;
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("cannot set security labels on relations except "
|
|
|
|
"for tables, sequences or views")));
|
|
|
|
|
2011-09-01 20:37:33 +08:00
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = 0;
|
2020-07-15 08:03:10 +08:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_xxx:{setattr relabelfrom} permission
|
|
|
|
*/
|
2011-09-01 20:37:33 +08:00
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
tclass,
|
|
|
|
SEPG_DB_TABLE__SETATTR |
|
|
|
|
SEPG_DB_TABLE__RELABELFROM,
|
|
|
|
audit_name,
|
|
|
|
true);
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
/*
|
|
|
|
* check db_xxx:{relabelto} permission
|
|
|
|
*/
|
2011-09-01 20:37:33 +08:00
|
|
|
sepgsql_avc_check_perms_label(seclabel,
|
|
|
|
tclass,
|
|
|
|
SEPG_DB_TABLE__RELABELTO,
|
|
|
|
audit_name,
|
|
|
|
true);
|
2011-02-03 12:39:43 +08:00
|
|
|
pfree(audit_name);
|
2011-01-24 09:44:48 +08:00
|
|
|
}
|
2012-10-24 05:07:26 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_relation_setattr
|
|
|
|
*
|
|
|
|
* It checks privileges to set attribute of the supplied relation
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_relation_setattr(Oid relOid)
|
|
|
|
{
|
2013-03-27 20:10:14 +08:00
|
|
|
Relation rel;
|
|
|
|
ScanKeyData skey;
|
|
|
|
SysScanDesc sscan;
|
|
|
|
HeapTuple oldtup;
|
|
|
|
HeapTuple newtup;
|
|
|
|
Form_pg_class oldform;
|
|
|
|
Form_pg_class newform;
|
2012-10-24 05:07:26 +08:00
|
|
|
ObjectAddress object;
|
|
|
|
char *audit_name;
|
|
|
|
uint16_t tclass;
|
|
|
|
|
|
|
|
switch (get_rel_relkind(relOid))
|
|
|
|
{
|
|
|
|
case RELKIND_RELATION:
|
2017-04-10 05:01:58 +08:00
|
|
|
case RELKIND_PARTITIONED_TABLE:
|
2012-10-24 05:07:26 +08:00
|
|
|
tclass = SEPG_CLASS_DB_TABLE;
|
|
|
|
break;
|
|
|
|
case RELKIND_SEQUENCE:
|
|
|
|
tclass = SEPG_CLASS_DB_SEQUENCE;
|
|
|
|
break;
|
|
|
|
case RELKIND_VIEW:
|
|
|
|
tclass = SEPG_CLASS_DB_VIEW;
|
|
|
|
break;
|
|
|
|
case RELKIND_INDEX:
|
|
|
|
/* deal with indexes specially */
|
|
|
|
sepgsql_index_modify(relOid);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
/* other relkinds don't need additional work */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-27 20:10:14 +08:00
|
|
|
/*
|
|
|
|
* Fetch newer catalog
|
|
|
|
*/
|
2019-01-22 02:32:19 +08:00
|
|
|
rel = table_open(RelationRelationId, AccessShareLock);
|
2013-03-27 20:10:14 +08:00
|
|
|
|
|
|
|
ScanKeyInit(&skey,
|
2018-11-21 12:03:46 +08:00
|
|
|
Anum_pg_class_oid,
|
2013-03-27 20:10:14 +08:00
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(relOid));
|
|
|
|
|
|
|
|
sscan = systable_beginscan(rel, ClassOidIndexId, true,
|
|
|
|
SnapshotSelf, 1, &skey);
|
|
|
|
|
|
|
|
newtup = systable_getnext(sscan);
|
|
|
|
if (!HeapTupleIsValid(newtup))
|
2017-06-05 04:20:03 +08:00
|
|
|
elog(ERROR, "could not find tuple for relation %u", relOid);
|
2013-03-27 20:10:14 +08:00
|
|
|
newform = (Form_pg_class) GETSTRUCT(newtup);
|
2012-10-24 05:07:26 +08:00
|
|
|
|
|
|
|
/*
|
2013-03-27 20:10:14 +08:00
|
|
|
* Fetch older catalog
|
|
|
|
*/
|
|
|
|
oldtup = SearchSysCache1(RELOID, ObjectIdGetDatum(relOid));
|
|
|
|
if (!HeapTupleIsValid(oldtup))
|
|
|
|
elog(ERROR, "cache lookup failed for relation %u", relOid);
|
|
|
|
oldform = (Form_pg_class) GETSTRUCT(oldtup);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Does this ALTER command takes operation to namespace?
|
|
|
|
*/
|
|
|
|
if (newform->relnamespace != oldform->relnamespace)
|
|
|
|
{
|
|
|
|
sepgsql_schema_remove_name(oldform->relnamespace);
|
|
|
|
sepgsql_schema_add_name(newform->relnamespace);
|
|
|
|
}
|
|
|
|
if (strcmp(NameStr(newform->relname), NameStr(oldform->relname)) != 0)
|
|
|
|
sepgsql_schema_rename(oldform->relnamespace);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - In the future version, db_tuple:{use} of system catalog entry
|
|
|
|
* shall be checked, if tablespace configuration is changed.
|
2012-10-24 05:07:26 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_xxx:{setattr} permission
|
|
|
|
*/
|
2013-03-27 20:10:14 +08:00
|
|
|
object.classId = RelationRelationId;
|
|
|
|
object.objectId = relOid;
|
|
|
|
object.objectSubId = 0;
|
2020-07-15 08:03:10 +08:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2013-03-27 20:10:14 +08:00
|
|
|
|
2012-10-24 05:07:26 +08:00
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
tclass,
|
|
|
|
SEPG_DB_TABLE__SETATTR,
|
|
|
|
audit_name,
|
|
|
|
true);
|
|
|
|
pfree(audit_name);
|
2013-03-27 20:10:14 +08:00
|
|
|
|
|
|
|
ReleaseSysCache(oldtup);
|
|
|
|
systable_endscan(sscan);
|
2019-01-22 02:32:19 +08:00
|
|
|
table_close(rel, AccessShareLock);
|
2012-10-24 05:07:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_relation_setattr_extra
|
|
|
|
*
|
|
|
|
* It checks permission of the relation being referenced by extra attributes,
|
|
|
|
* such as pg_index entries. Like core PostgreSQL, sepgsql also does not deal
|
|
|
|
* with such entries as individual "objects", thus, modification of these
|
|
|
|
* entries shall be considered as setting an attribute of the underlying
|
|
|
|
* relation.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sepgsql_relation_setattr_extra(Relation catalog,
|
|
|
|
Oid catindex_id,
|
|
|
|
Oid extra_oid,
|
|
|
|
AttrNumber anum_relation_id,
|
|
|
|
AttrNumber anum_extra_id)
|
|
|
|
{
|
|
|
|
ScanKeyData skey;
|
|
|
|
SysScanDesc sscan;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Datum datum;
|
|
|
|
bool isnull;
|
|
|
|
|
|
|
|
ScanKeyInit(&skey, anum_extra_id,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(extra_oid));
|
|
|
|
|
|
|
|
sscan = systable_beginscan(catalog, catindex_id, true,
|
|
|
|
SnapshotSelf, 1, &skey);
|
|
|
|
tuple = systable_getnext(sscan);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2017-06-05 04:20:03 +08:00
|
|
|
elog(ERROR, "could not find tuple for object %u in catalog \"%s\"",
|
2012-10-24 05:07:26 +08:00
|
|
|
extra_oid, RelationGetRelationName(catalog));
|
|
|
|
|
|
|
|
datum = heap_getattr(tuple, anum_relation_id,
|
|
|
|
RelationGetDescr(catalog), &isnull);
|
|
|
|
Assert(!isnull);
|
|
|
|
|
|
|
|
sepgsql_relation_setattr(DatumGetObjectId(datum));
|
|
|
|
|
|
|
|
systable_endscan(sscan);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_index_modify
|
|
|
|
* Handle index create, update, drop
|
|
|
|
*
|
|
|
|
* Unlike other relation kinds, indexes do not have their own security labels,
|
|
|
|
* so instead of doing checks directly, treat them as extra attributes of their
|
|
|
|
* owning tables; so check 'setattr' permissions on the table.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sepgsql_index_modify(Oid indexOid)
|
|
|
|
{
|
2019-01-22 02:32:19 +08:00
|
|
|
Relation catalog = table_open(IndexRelationId, AccessShareLock);
|
2012-10-24 05:07:26 +08:00
|
|
|
|
|
|
|
/* check db_table:{setattr} permission of the table being indexed */
|
|
|
|
sepgsql_relation_setattr_extra(catalog,
|
|
|
|
IndexRelidIndexId,
|
|
|
|
indexOid,
|
|
|
|
Anum_pg_index_indrelid,
|
|
|
|
Anum_pg_index_indexrelid);
|
2019-01-22 02:32:19 +08:00
|
|
|
table_close(catalog, AccessShareLock);
|
2012-10-24 05:07:26 +08:00
|
|
|
}
|