Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

proc.c File Reference

#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/seclabel.h"
#include "lib/stringinfo.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
#include "sepgsql.h"
Include dependency graph for proc.c:

Go to the source code of this file.

Functions

void sepgsql_proc_post_create (Oid functionId)
void sepgsql_proc_drop (Oid functionId)
void sepgsql_proc_relabel (Oid functionId, const char *seclabel)
void sepgsql_proc_setattr (Oid functionId)
void sepgsql_proc_execute (Oid functionId)

Function Documentation

void sepgsql_proc_drop ( Oid  functionId  ) 

Definition at line 157 of file proc.c.

References get_func_namespace(), getObjectIdentity(), pfree(), SEPG_CLASS_DB_PROCEDURE, SEPG_CLASS_DB_SCHEMA, SEPG_DB_PROCEDURE__DROP, SEPG_DB_SCHEMA__REMOVE_NAME, and sepgsql_avc_check_perms().

Referenced by sepgsql_object_access().

{
    ObjectAddress object;
    char       *audit_name;

    /*
     * check db_schema:{remove_name} permission
     */
    object.classId = NamespaceRelationId;
    object.objectId = get_func_namespace(functionId);
    object.objectSubId = 0;
    audit_name = getObjectIdentity(&object);

    sepgsql_avc_check_perms(&object,
                            SEPG_CLASS_DB_SCHEMA,
                            SEPG_DB_SCHEMA__REMOVE_NAME,
                            audit_name,
                            true);
    pfree(audit_name);

    /*
     * check db_procedure:{drop} permission
     */
    object.classId = ProcedureRelationId;
    object.objectId = functionId;
    object.objectSubId = 0;
    audit_name = getObjectIdentity(&object);

    sepgsql_avc_check_perms(&object,
                            SEPG_CLASS_DB_PROCEDURE,
                            SEPG_DB_PROCEDURE__DROP,
                            audit_name,
                            true);
    pfree(audit_name);
}

void sepgsql_proc_execute ( Oid  functionId  ) 

Definition at line 317 of file proc.c.

References getObjectIdentity(), pfree(), SEPG_CLASS_DB_PROCEDURE, SEPG_DB_PROCEDURE__EXECUTE, and sepgsql_avc_check_perms().

Referenced by sepgsql_object_access().

{
    ObjectAddress object;
    char       *audit_name;

    /*
     * check db_procedure:{execute} permission
     */
    object.classId = ProcedureRelationId;
    object.objectId = functionId;
    object.objectSubId = 0;
    audit_name = getObjectIdentity(&object);
    sepgsql_avc_check_perms(&object,
                            SEPG_CLASS_DB_PROCEDURE,
                            SEPG_DB_PROCEDURE__EXECUTE,
                            audit_name,
                            true);
    pfree(audit_name);
}

void sepgsql_proc_post_create ( Oid  functionId  ) 

Definition at line 39 of file proc.c.

References AccessShareLock, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), BTEqualStrategyNumber, StringInfoData::data, elog, ERROR, get_namespace_name(), getObjectIdentity(), GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, i, initStringInfo(), NamespaceRelationId, NameStr, ObjectIdAttributeNumber, ObjectIdGetDatum, pfree(), ProcedureOidIndexId, ProcedureRelationId, quote_qualified_identifier(), ScanKeyInit(), SEPG_CLASS_DB_PROCEDURE, SEPG_CLASS_DB_SCHEMA, SEPG_DB_SCHEMA__ADD_NAME, sepgsql_avc_check_perms(), sepgsql_avc_check_perms_label(), sepgsql_compute_create(), sepgsql_get_client_label(), sepgsql_get_label(), SEPGSQL_LABEL_TAG, SetSecurityLabel(), SnapshotSelf, systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by sepgsql_object_access().

{
    Relation    rel;
    ScanKeyData skey;
    SysScanDesc sscan;
    HeapTuple   tuple;
    char       *nsp_name;
    char       *scontext;
    char       *tcontext;
    char       *ncontext;
    uint32      required;
    int         i;
    StringInfoData audit_name;
    ObjectAddress object;
    Form_pg_proc proForm;

    /*
     * Fetch namespace of the new procedure. Because pg_proc entry is not
     * visible right now, we need to scan the catalog using SnapshotSelf.
     */
    rel = heap_open(ProcedureRelationId, AccessShareLock);

    ScanKeyInit(&skey,
                ObjectIdAttributeNumber,
                BTEqualStrategyNumber, F_OIDEQ,
                ObjectIdGetDatum(functionId));

    sscan = systable_beginscan(rel, ProcedureOidIndexId, true,
                               SnapshotSelf, 1, &skey);

    tuple = systable_getnext(sscan);
    if (!HeapTupleIsValid(tuple))
        elog(ERROR, "catalog lookup failed for proc %u", functionId);

    proForm = (Form_pg_proc) GETSTRUCT(tuple);

    /*
     * check db_schema:{add_name} permission of the namespace
     */
    object.classId = NamespaceRelationId;
    object.objectId = proForm->pronamespace;
    object.objectSubId = 0;
    sepgsql_avc_check_perms(&object,
                            SEPG_CLASS_DB_SCHEMA,
                            SEPG_DB_SCHEMA__ADD_NAME,
                            getObjectIdentity(&object),
                            true);

    /*
     * XXX - db_language:{implement} also should be checked here
     */


    /*
     * Compute a default security label when we create a new procedure object
     * under the specified namespace.
     */
    scontext = sepgsql_get_client_label();
    tcontext = sepgsql_get_label(NamespaceRelationId,
                                 proForm->pronamespace, 0);
    ncontext = sepgsql_compute_create(scontext, tcontext,
                                      SEPG_CLASS_DB_PROCEDURE,
                                      NameStr(proForm->proname));

    /*
     * check db_procedure:{create (install)} permission
     */
    initStringInfo(&audit_name);
    nsp_name = get_namespace_name(proForm->pronamespace);
    appendStringInfo(&audit_name, "%s(",
            quote_qualified_identifier(nsp_name, NameStr(proForm->proname)));
    for (i = 0; i < proForm->pronargs; i++)
    {
        if (i > 0)
            appendStringInfoChar(&audit_name, ',');

        object.classId = TypeRelationId;
        object.objectId = proForm->proargtypes.values[i];
        object.objectSubId = 0;
        appendStringInfoString(&audit_name, getObjectIdentity(&object));
    }
    appendStringInfoChar(&audit_name, ')');

    required = SEPG_DB_PROCEDURE__CREATE;
    if (proForm->proleakproof)
        required |= SEPG_DB_PROCEDURE__INSTALL;

    sepgsql_avc_check_perms_label(ncontext,
                                  SEPG_CLASS_DB_PROCEDURE,
                                  required,
                                  audit_name.data,
                                  true);

    /*
     * Assign the default security label on a new procedure
     */
    object.classId = ProcedureRelationId;
    object.objectId = functionId;
    object.objectSubId = 0;
    SetSecurityLabel(&object, SEPGSQL_LABEL_TAG, ncontext);

    /*
     * Cleanup
     */
    systable_endscan(sscan);
    heap_close(rel, AccessShareLock);

    pfree(audit_name.data);
    pfree(tcontext);
    pfree(ncontext);
}

void sepgsql_proc_relabel ( Oid  functionId,
const char *  seclabel 
)

Definition at line 200 of file proc.c.

References getObjectIdentity(), pfree(), SEPG_CLASS_DB_PROCEDURE, SEPG_DB_PROCEDURE__RELABELFROM, SEPG_DB_PROCEDURE__RELABELTO, SEPG_DB_PROCEDURE__SETATTR, sepgsql_avc_check_perms(), and sepgsql_avc_check_perms_label().

Referenced by sepgsql_object_relabel().

{
    ObjectAddress object;
    char       *audit_name;

    object.classId = ProcedureRelationId;
    object.objectId = functionId;
    object.objectSubId = 0;
    audit_name = getObjectIdentity(&object);

    /*
     * check db_procedure:{setattr relabelfrom} permission
     */
    sepgsql_avc_check_perms(&object,
                            SEPG_CLASS_DB_PROCEDURE,
                            SEPG_DB_PROCEDURE__SETATTR |
                            SEPG_DB_PROCEDURE__RELABELFROM,
                            audit_name,
                            true);

    /*
     * check db_procedure:{relabelto} permission
     */
    sepgsql_avc_check_perms_label(seclabel,
                                  SEPG_CLASS_DB_PROCEDURE,
                                  SEPG_DB_PROCEDURE__RELABELTO,
                                  audit_name,
                                  true);
    pfree(audit_name);
}

void sepgsql_proc_setattr ( Oid  functionId  ) 

Definition at line 237 of file proc.c.

References AccessShareLock, BTEqualStrategyNumber, elog, ERROR, getObjectIdentity(), GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, NameStr, ObjectIdAttributeNumber, ObjectIdGetDatum, pfree(), ProcedureOidIndexId, ProcedureRelationId, PROCOID, ReleaseSysCache(), ScanKeyInit(), SearchSysCache1, SEPG_CLASS_DB_PROCEDURE, sepgsql_avc_check_perms(), sepgsql_schema_add_name(), sepgsql_schema_remove_name(), sepgsql_schema_rename(), SnapshotSelf, systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by sepgsql_object_access().

{
    Relation        rel;
    ScanKeyData     skey;
    SysScanDesc     sscan;
    HeapTuple       oldtup;
    HeapTuple       newtup;
    Form_pg_proc    oldform;
    Form_pg_proc    newform;
    uint32          required;
    ObjectAddress   object;
    char           *audit_name;

    /*
     * Fetch newer catalog
     */
    rel = heap_open(ProcedureRelationId, AccessShareLock);

    ScanKeyInit(&skey,
                ObjectIdAttributeNumber,
                BTEqualStrategyNumber, F_OIDEQ,
                ObjectIdGetDatum(functionId));

    sscan = systable_beginscan(rel, ProcedureOidIndexId, true,
                               SnapshotSelf, 1, &skey);
    newtup = systable_getnext(sscan);
    if (!HeapTupleIsValid(newtup))
        elog(ERROR, "catalog lookup failed for function %u", functionId);
    newform = (Form_pg_proc) GETSTRUCT(newtup);

    /*
     * Fetch older catalog
     */
    oldtup = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
    if (!HeapTupleIsValid(oldtup))
        elog(ERROR, "cache lookup failed for function %u", functionId);
    oldform = (Form_pg_proc) GETSTRUCT(oldtup);

    /*
     * Does this ALTER command takes operation to namespace?
     */
    if (newform->pronamespace != oldform->pronamespace)
    {
        sepgsql_schema_remove_name(oldform->pronamespace);
        sepgsql_schema_add_name(oldform->pronamespace);
    }
    if (strcmp(NameStr(newform->proname), NameStr(oldform->proname)) != 0)
        sepgsql_schema_rename(oldform->pronamespace);

    /*
     * check db_procedure:{setattr (install)} permission
     */
    required = SEPG_DB_PROCEDURE__SETATTR;
    if (!oldform->proleakproof && newform->proleakproof)
        required |= SEPG_DB_PROCEDURE__INSTALL;

    object.classId = ProcedureRelationId;
    object.objectId = functionId;
    object.objectSubId = 0;
    audit_name = getObjectIdentity(&object);

    sepgsql_avc_check_perms(&object,
                            SEPG_CLASS_DB_PROCEDURE,
                            required,
                            audit_name,
                            true);
    /* cleanups */
    pfree(audit_name);

    ReleaseSysCache(oldtup);
    systable_endscan(sscan);
    heap_close(rel, AccessShareLock);
}