security_compute_av(3)
π security_compute_av β the SELinux kernel policy-query girlies π
okay so bestie, welcome to security_compute_av(3), part of the SELinux API documentation. this is the whole lil family of functions you use to go ask the SELinux kernel security server (aka the policy engine) what it thinks about stuff. it's giving β¨all-knowing oracleβ¨ but make it syscalls.
πΈ NAME πΈ
security_compute_av, security_compute_av_flags, security_compute_create, security_compute_create_name, security_compute_relabel, security_compute_member, security_compute_user, security_validatetrans, security_get_initial_context β query the SELinux policy database in the kernel π§
β¨ SYNOPSIS β¨
#include <selinux/selinux.h>
int security_compute_av(const char *scon, const char *tcon,
security_class_t tclass, access_vector_t requested, struct av_decision
*avd);
int security_compute_av_raw(const char *scon, const char *tcon,
security_class_t tclass, access_vector_t requested, struct av_decision
*avd);
int security_compute_av_flags(const char *scon, const char *tcon,
security_class_t tclass, access_vector_t requested, struct av_decision
*avd);
int security_compute_av_flags_raw(const char *scon, const char *tcon,
security_class_t tclass, access_vector_t requested, struct av_decision
*avd);
int security_compute_create(const char *scon, const char *tcon,
security_class_t tclass, char **newcon);
int security_compute_create_raw(const char *scon, const char *tcon,
security_class_t tclass, char **newcon);
int security_compute_create_name(const char *scon, const char *tcon,
security_class_t tclass, const char *objname, char **newcon);
int security_compute_create_name_raw(const char *scon, const char *tcon,
security_class_t tclass, const char *objname, char **newcon);
int security_compute_relabel(const char *scon, const char *tcon,
security_class_t tclass, char **newcon);
int security_compute_relabel_raw(const char *scon, const char *tcon,
security_class_t tclass, char **newcon);
int security_compute_member(const char *scon, const char *tcon,
security_class_t tclass, char **newcon);
int security_compute_member_raw(const char *scon, const char *tcon,
security_class_t tclass, char **newcon);
int security_compute_user(const char *scon, const char *username, char
***con);
int security_compute_user_raw(const char *scon, const char *username,
char ***con);
int security_validatetrans(const char *scon, const char *tcon,
security_class_t tclass, const char *newcon);
int security_validatetrans_raw(const char *scon, const char *tcon,
security_class_t tclass, const char *newcon);
int security_get_initial_context(const char *name, char **con);
int security_get_initial_context_raw(const char *name, char **con);
int selinux_check_access(const char *scon, const char *tcon, const char
*class, const char *perm, void *auditdata);
int selinux_check_passwd_access(access_vector_t requested);
int checkPasswdAccess(access_vector_t requested);
π¦ DESCRIPTION π¦
so this whole family of functions exists to pull policy decisions out of the SELinux kernel security server (the policy engine). periodt. that's the gig.
ngl, the manpage itself says that directly using security_compute_av() and its variant interfaces is discouraged, and you should lowkey use selinux_check_access() instead. why? because selinux_check_access() automatically handles:
- the dynamic mapping of class and permission names to their policy values π
- initialization and use of the Access Vector Cache (AVC) π
- proper handling of per-domain and global permissive mode and
allow_unknownπ
like it just does all of that for you. main character behavior honestly.
when you're using any of the functions that take policy integer values for classes or permissions as inputs, use string_to_security_class(3) and string_to_av_perm(3) to map the class and permission names to their policy values. β οΈ these values may change across a policy reload, so they should be re-acquired on every use, or by using a SELINUX_CB_POLICYLOAD callback set via selinux_set_callback(3).
an alternative approach is using selinux_set_mapping(3) to create a mapping from class and permission index values used by the application to the policy values. that way your app can pass its own fixed constants for the classes and permissions to these functions, and it internally maps them on demand. cute, right? π but heads up β this also requires setting up a callback as above to address policy reloads. no skipping that step bestie.
π the actual functions, one by one π
security_compute_av()β queries whether the policy permits the source context scon to access the target context tcon via class tclass with the requested access vector. the decision comes back in avd. π«security_compute_av_flags()β literally identical tosecurity_compute_avbut it additionally sets theflagsfield of avd. right now there's exactly one flag supported:SELINUX_AVD_FLAGS_PERMISSIVE, which indicates the decision is computed on a permissive domain. one flag, one vibe. πΈsecurity_compute_create()β computes a context to use for labeling a new object in a particular class, based on a SID pair. πsecurity_compute_create_name()β identical tosecurity_compute_create()but it also takes the name of the new object in creation as an argument (objname). when aTYPE_TRANSITIONrule on the given class and a SID pair has an object name extension, we shall be able to obtain a correct newcon according to the security policy. π note that this interface is only supported on the linux 2.6.40 or later. on an older kernel, the object name will simply be ignored. πsecurity_compute_relabel()β computes the new context to use when relabeling an object. it's used in thepam_selinux.sosource and thenewrolesource to determine the correct label for the tty at login time, but it can be used for other things too. π·οΈsecurity_compute_member()β computes the context to use when labeling a polyinstantiated object instance. π«Άsecurity_compute_user()β determines the set of user contexts that can be reached from a source context. this function is deprecated; useget_ordered_context_list(3)instead. πsecurity_validatetrans()β determines if a transition from scon to newcon using tcon as the object is valid for object class tclass. this checks against themlsvalidatetransandvalidatetransconstraints in the loaded policy. returns0if allowed, and-1if an error occurred witherrnoset. β βsecurity_get_initial_context()β gets the context of a kernel initial security identifier specified by name. βsecurity_compute_av_raw(),security_compute_av_flags_raw(),security_compute_create_raw(),security_compute_create_name_raw(),security_compute_relabel_raw(),security_compute_member_raw(),security_compute_user_raw(),security_validatetrans_raw()andsecurity_get_initial_context_raw()β behave identically to their non-raw counterparts but do not perform context translation. that's the whole difference. raw is raw. π§selinux_check_access()β checks if the source context has the access permission for the specified class on the target context. πselinux_check_passwd_access()β checks for a permission in thepasswdclass.selinux_check_passwd_access()usesgetprevcon(3)for the source and target security contexts. πcheckPasswdAccess()β a deprecated alias of theselinux_check_passwd_access()function. rest in peace lil buddy π
π RETURN VALUE π
returns zero on success or -1 on error. that's it. that's the tea. π
π CAVEATS π
β οΈ these functions are not thread-safe. you have to protect them from concurrent calls using exclusive locks when multiple threads are executing. no cap. if you skip this you're gonna have a bad time bestie, protect your threads. π
π SEE ALSO π
string_to_security_class(3)string_to_av_perm(3)selinux_set_callback(3)selinux_set_mapping(3)getprevcon(3)get_ordered_context_list(3)selinux(8)
okay that's the whole page bestie, you ate. π β¨
this lil masterpiece was brought to you by russell@coker.com.au, dated 1 January 2004, from the SELinux API documentation. iconic behavior for real. π«Ά
π¦ where this page comes from
- alpine 3.24: libselinux-doc 3.10-r1 Β· Public-Domain
- centos-stream 10: libselinux-devel 3.11-3.el10 Β· LicenseRef-Fedora-Public-Domain
- fedora 43: libselinux-devel 3.9-5.fc43 Β· LicenseRef-Fedora-Public-Domain
- fedora 43: libselinux-devel 3.9-5.fc43 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-devel 3.10-1.fc44 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-devel 3.10-1.fc44 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-devel 3.11-2.fc44 Β· LicenseRef-Fedora-Public-Domain
- fedora 44: libselinux-devel 3.11-2.fc44 Β· LicenseRef-Fedora-Public-Domain
- nixos 26.05: libselinux 3.10
- nixpkgs unstable: libselinux 3.11
- ubuntu resolute: libselinux-dev 3.9-4build1 Β· LicenseRef-public-domain
- ubuntu stonking: libselinux-dev 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