security_compute_av(3)

πŸ“¦
version 3.11 Β· 2 versions
, in alpine 3.24, centos-stream 10, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, ubuntu resolute, ubuntu stonking

πŸ’– 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 to security_compute_av but it additionally sets the flags field 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 to security_compute_create() but it also takes the name of the new object in creation as an argument (objname). when a TYPE_TRANSITION rule 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 the pam_selinux.so source and the newrole source 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; use get_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 the mlsvalidatetrans and validatetrans constraints in the loaded policy. returns 0 if allowed, and -1 if an error occurred with errno set. βœ…βŒ
  • 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() and security_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 the passwd class. selinux_check_passwd_access() uses getprevcon(3) for the source and target security contexts. πŸ”
  • checkPasswdAccess() β€” a deprecated alias of the selinux_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 πŸŽ€


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