get_ordered_context_list(3)
π get_ordered_context_list β the SELinux glow-up manual (section 3, bestie) π
okay so this whole page lives in the SELinux part of the manual, and it's all about figuring out which SELinux context a user session gets to wear. slay. let's go β¨
πΈ NAME πΈ
get_ordered_context_list, get_ordered_context_list_with_level, get_default_context, get_default_context_with_level, get_default_context_with_role, get_default_context_with_rolelevel, query_user_context, manual_user_enter_context, get_default_role β determine SELinux context(s) for user sessions
yes bestie, that's a whole friend group in one function family. they all show up to the party together. π
π SYNOPSIS π
#include <selinux/selinux.h>
#include <selinux/get_context_list.h>
int get_ordered_context_list(const char *user, const char *fromcon, char
***list);
int get_ordered_context_list_with_level(const char *user, const char
*level, const char *fromcon, char ***list);
int get_default_context(const char *user, const char *fromcon, char
**newcon);
int get_default_context_with_level(const char *user, const char *level,
const char *fromcon, char **newcon);
int get_default_context_with_role(const char *user, const char *role,
const char *fromcon, char **newcon);
int get_default_context_with_rolelevel(const char *user, const char
*role, const char *level, const char *fromcon, char **newcon);
int query_user_context(char **list, char **newcon);
int manual_user_enter_context(const char *user, char **newcon);
int get_default_type(const char *role, char **type);
β¨ DESCRIPTION β¨
okay so picture this: this whole little family of functions is your go-to for getting either a prioritized list of all the reachable security contexts for a given SELinux user, or a single default (highest priority) context for a given SELinux user β specifically so that login-like programs can use it. it's giving "who am I allowed to be today" energy. π
BIG important thing bestie: these functions take a SELinux user identity that must be defined in the SELinux policy as their input. not a Linux username. those are two different things and mixing them up is a whole vibe check failure, fr fr.
so most callers should typically do this little two-step:
- call
getseuserbyname(3)first to look up the SELinux user identity and level for a given Linux username π¦ - then invoke one of
get_ordered_context_list_with_level()orget_default_context_with_level()with the returned SELinux user and level as inputs
main character sequence, honestly.
π the functions, one by one π
get_ordered_context_list()β obtains the list of contexts for the specified SELinux user identity that are reachable from the specifiedfromconcontext, based on the global/etc/selinux/{SELINUXTYPE}/contexts/default_contextsfile and the per-user/etc/selinux/{SELINUXTYPE}/contexts/users/<username>file if it exists. thefromconparameter may beNULLto indicate that the current context should be used. it returns the number of contexts in the list, or-1upon errors. and listen β the list must be freed using thefreeconary(3)function. do not skip that part. π€get_ordered_context_list_with_level()β invokes theget_ordered_context_list()function and applies the specifiedlevel. that's it, that's the whole thing, and it ate. πget_default_context()β same asget_ordered_context_list()but only returns a single context, which has to be freed withfreecon(3). one context. minimalism. iconic.get_default_context_with_level()β invokes theget_default_context()function and applies the specifiedlevel. β¨get_default_context_with_role()β same asget_default_context()but only returns a context with the specifiedrole, returning-1if no such context is reachable for the user. so if that role isn't reachable?-1. no context for you. π«Άget_default_context_with_rolelevel()β invokes theget_default_context_with_role()function and applies the specifiedlevel. πquery_user_context()β takes a list of contexts, queries the user via stdin/stdout as to which context they want, and returns a new context as selected by the user (which has to be freed withfreecon(3)). it's literally asking them. democracy, but for contexts. π³οΈmanual_user_enter_context()β allows the user to manually enter a context as a fallback if a list of authorized contexts could not be obtained. caller must free viafreecon(3). emergency mode, but make it fashion. πget_default_type()β Get the default type (domain) forroleand settypeto refer to it, which has to be freed withfree. yes bestie, that one is plainfree, notfreecon. keep your frees straight or the memory leaks will humble you. π
π₯ RETURN VALUE π₯
get_ordered_context_list()andget_ordered_context_list_with_level()return the number of contexts in the list upon success, or-1upon errors.- the other functions return
0for success or-1for errors.
that's the whole contract. -1 means something went wrong, bestie. don't ignore it. π
π¦ SEE ALSO π¦
selinux(8), freeconary(3), freecon(3), security_compute_av(3), getseuserbyname(3)
reminder bestie: free your lists withfreeconary(3), free your single contexts withfreecon(3), free that default type withfreeβ and always pass a real SELinux user identity, not a Linux username. iykyk. π
this page was written by russell@coker.com.au and it's dated 1 January 2004 β so yes, she's been serving accurate SELinux knowledge since the early aughts and she's still THAT girl. periodt. ππΈ
π¦ 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
- centos-stream 9: libselinux-devel 3.6-5.el9 Β· Public Domain
- centos-stream 9: libselinux-devel 3.6-5.el9 Β· Public Domain
- debian bookworm: libselinux1-dev 3.4-1+b6
- 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
- opensuse-leap 16.0: libselinux-devel 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: libselinux-devel 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: libselinux-devel 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- opensuse-leap 16.0: libselinux-devel 3.8.1-160000.4.1 Β· SUSE-Public-Domain
- ubuntu jammy: libselinux1-dev 3.3-1build2
- ubuntu noble: libselinux1-dev 3.5-2ubuntu2 Β· LicenseRef-public-domain
- ubuntu noble: libselinux1-dev 3.5-2ubuntu2.1 Β· LicenseRef-public-domain
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms