selabel_db(5)
πΈ selabel_db β the SELinux API manual, section 5 πΈ
okay bestie, buckle up, this is the page about selabel_db, the userspace SELinux labeling interface and config file format for the RDBMS objects context backend. it lives in the SELinux API documentation, section 5. it's long. we're doing it. π
β¨ NAME β¨
selabel_db β userspace SELinux labeling interface and configuration file format for the RDBMS objects context backend.
π¦ SYNOPSIS π¦
#include <selinux/label.h>
int selabel_lookup(struct selabel_handle *hnd,
char **context,
const char *object_name, int object_type);
int selabel_lookup_raw(struct selabel_handle *hnd,
char **context,
const char *object_name, int object_type);
π DESCRIPTION π
so the DB contexts backend is the lil matchmaker that maps from a pair of object name and class into security contexts. it's used to find the appropriate context for database objects when you're relabeling a certain database. the returned context must be freed using freecon(3). and selabel_lookup(3) is where you go to read about the function with its return and error codes. π«Ά
the object_name should be a fully qualified name using the hierarchy of database objects. for example, the pg_class table in the postgres database and pg_catalog schema should be qualified as:
Bpostgres.pg_catalog.pg_class
(no i did not typo that, that's literally what it says. and yes, the NOTES section below has further info on database support for namespace hierarchies. keep reading, bestie. π)
the object_type argument should be set to one of these values:
SELABEL_DB_DATABASEβ theobject_nameargument specifies the name of a database itself, such as"postgres".SELABEL_DB_SCHEMAβ theobject_nameargument specifies the name of a schema object, such as"postgres.public".SELABEL_DB_TABLEβ theobject_nameargument specifies the name of a table object, such as"postgres.public.my_table"SELABEL_DB_COLUMNβ theobject_nameargument specifies the name of a column object, such as"postgres.public.my_table.user_id"SELABEL_DB_TUPLEβ theobject_nameargument specifies the name of a table object which contains the tuples to be relabeled, such as"postgresql.public.my_table". heads up tho: we have no way to identify individual tuple objects, except for WHERE clause on DML statements, because it has no name. πSELABEL_DB_PROCEDUREβ theobject_nameargument specifies the name of a procedure object, such as"postgres.public.my_func". and note that we don't support lookup of individual security contexts for procedures which have the same name but different arguments. nope. not a thing.SELABEL_DB_SEQUENCEβ theobject_nameargument specifies the name of a sequence object, such as"postgres.public.my_seq".SELABEL_DB_BLOBβ theobject_nameargument specifies the name of a large object, such as"postgres.16308". note that a large object does not have a name, so it is identified by its identifier value. it's giving "who needs a name anyway" πSELABEL_DB_VIEWβ theobject_nameargument specifies the name of a view object, such as"postgres.public.my_view".SELABEL_DB_LANGUAGEβ theobject_nameargument specifies the name of a language object, such as"postgres.public.tcl".SELABEL_DB_EXCEPTIONβ theobject_nameargument specifies the name of a exception object.SELABEL_DB_DATATYPEβ theobject_nameargument specifies the name of a type or domain object, such aspostgres.public.my_type.
any messages generated by selabel_lookup(3) are sent to stderr by default, although this can be changed by selinux_set_callback(3). β¨
selabel_lookup_raw(3) behaves identically to selabel_lookup(3) but does not perform context translation. same vibe, one less step.
the FILES section (below, keep scrolling) details the configuration files used to determine the database object context.
π OPTIONS π
in addition to the global options described in selabel_open(3), this backend recognizes the following options:
SELABEL_OPT_PATHβ a non-null value for this option specifies a path to a file that will be opened in lieu of the standard DB contexts file. it tries to open the specfile designed for SE-PostgreSQL as default, so if another RDBMS uses this interface, it needs to give an explicit specfile designed for that RDBMS (see the FILES section for details).
ποΈ FILES ποΈ
the database context file used to retrieve a context depends on the SELABEL_OPT_PATH parameter passed to selabel_open(3). if NULL, then the SELABEL_OPT_PATH value will default to the active policy database contexts location (as returned by selinux_sepgsql_context_path(3)), otherwise the actual SELABEL_OPT_PATH value specified is used. (this option must be used to support databases other than SE-PostgreSQL, fyi.)
the default database object contexts file is:
/etc/selinux/{SELINUXTYPE}/contexts/sepgsql_context
where {SELINUXTYPE} is the entry from the selinux configuration file config (see selinux_config(5)).
the entries within the database contexts file are shown in the Object Name String Values and FILE FORMAT sections. π
β Object Name String Values β
the string name assigned to each object_type argument that can be present in the database contexts file are:
SELABEL_DB_DATABASEβdb_databaseSELABEL_DB_SCHEMAβdb_schemaSELABEL_DB_VIEWβdb_viewSELABEL_DB_LANGUAGEβdb_languageSELABEL_DB_TABLEβdb_tableSELABEL_DB_COLUMNβdb_columnSELABEL_DB_TUPLEβdb_tupleSELABEL_DB_PROCEDUREβdb_procedureSELABEL_DB_SEQUENCEβdb_sequenceSELABEL_DB_BLOBβdb_blobSELABEL_DB_EXCEPTIONβdb_exceptionSELABEL_DB_DATATYPEβdb_datatype
π FILE FORMAT π
each line within the database contexts file is as follows:
object_type object_name context
where:
object_typeβ this is the string representation of the object type shown in the Object Name String Values section.object_nameβ the key used to obtain the context based on theobject_type.
the entry can contain '*' for wildcard matching or '?' for substitution.
note that if the'*'is used, then be aware that the order of entries in the file is important. the'*'on its own is used to ensure a default fallback context is assigned and should be the last entry in theobject_typeblock. π don't skip this one bestie, it will absolutely bite you.
contextβ the security context that will be applied to the object.
the following example is for SE-PostgreSQL:
# ./contexts/sepgsql_contexts file
# object_type object_name context
db_database my_database system_u:object_r:sepgsql_db_t:s0
db_database * system_u:object_r:sepgsql_db_t:s0
db_schema *.* system_u:object_r:sepgsql_schema_t:s0
db_tuple row_low system_u:object_r:sepgsql_table_t:s0
db_tuple row_high system_u:object_r:sepgsql_table_t:s0:c1023
db_tuple *.*.* system_u:object_r:sepgsql_table_t:s0
π§ NOTES π§
- a suitable database contexts file needs to be written for the target RDBMS and the
SELABEL_OPT_PATHoption must be used inselabel_open(3)to load it. - the hierarchy of the namespace for database objects depends on the RDBMS, however the
selabel*interfaces do not have any specific support for a namespace hierarchy.
SE-PostgreSQL has a namespace hierarchy where a database is the top level object with the schema being the next level. under the schema object there can be other types of objects such as tables and procedures. this hierarchy is supported as follows:
- if a security context is required for
"my_table"table in the"public"schema within the"postgres"database, then theselabel_lookup(3)parameters forobject_typewould beSELABEL_DB_TABLEand theobject_namewould be"postgres.public.my_table", the security context (if available), would be returned incontext.
- if contexts are to be validated, then the global option
SELABEL_OPT_VALIDATEmust be set before callingselabel_open(3). if this is not set, then it is possible for an invalid context to be returned. π¬ so like, set it. please.
π SEE ALSO β the besties, the mutuals, the whole group chat π
ok so if u loved this one (and you did, obviously, you're obsessed, we're both obsessed) go read its friends, they're all linked up and they all ate:
selinux(8)β the big picture, the main character of the whole SE Linux saga πselabel_open(3)β how u actually open a labeling handle in the first placeselabel_lookup(3)β the one that does the looking-up, the whole point of the exercise β¨selabel_stats(3)β numbers!! stats!! for when u need receipts πselabel_close(3)β clean up after urself bestie, close that handle π«Άselinux_set_callback(3)β callbacks, the customizable lil hooksselinux_sepgsql_context_path(3)β the postgresql-flavoured path helper πfreecon(3)β free the con, always free the con πselinux_config(5)β the config file page, for when u wanna know where all this stuff lives
fr fr, read them all, they're a set, they're giving complete-documentation-energy π
and that's the page!! that's it, we did it, we got through the whole thing together πΈ
okay tiny sign-off moment bc the footer deserves its flowers too: this whole page comes from the Security Enhanced Linux crew, and it's dated 01 DEC 2011 β yes it's been serving accurate docs since way back, iconic behavior honestly πβ¨
go label something responsibly. mwah ππ¦
π¦ where this page comes from
- alpine 3.24: libselinux-doc 3.10-r1 Β· Public-Domain
- centos-stream 10: libselinux-utils 3.11-3.el10 Β· LicenseRef-Fedora-Public-Domain
- centos-stream 9: libselinux-utils 3.6-5.el9 Β· Public Domain
- debian bookworm: selinux-utils 3.4-1+b6
- debian trixie: selinux-utils 3.8.1-1 Β· LicenseRef-public-domain
- fedora 43: libselinux-utils 3.9-5.fc43 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-utils 3.10-1.fc44 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-utils 3.11-2.fc44 Β· LicenseRef-Fedora-Public-Domain
- nixos 26.05: libselinux 3.10
- nixpkgs unstable: libselinux 3.11
- opensuse-leap 16.0: selinux-tools 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: selinux-tools 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: selinux-tools 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: selinux-tools 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- ubuntu jammy: selinux-utils 3.3-1build2
- ubuntu noble: selinux-utils 3.5-2ubuntu2 Β· LicenseRef-public-domain
- ubuntu noble: selinux-utils 3.5-2ubuntu2.1 Β· LicenseRef-public-domain
- ubuntu questing: selinux-utils 3.8.1-1build1
- ubuntu resolute: selinux-utils 3.9-4build1 Β· LicenseRef-public-domain
- ubuntu stonking: selinux-utils 3.11-2.1 Β· LicenseRef-public-domain
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms