getexeccon(3)
β¨ getexeccon β the SELinux exec-context page π
okay bestie so this is from the SELinux API documentation set, and it's giving "how do I tell the kernel what security context my next exec should wear" energy. three main characters in this one: getexeccon, setexeccon, and rpm_execcon (plus their raw cousins and one little helper-setter). let's GO π¦
πΈ NAME πΈ
getexeccon, setexeccon β grab or set the SELinux security context that gets used when you execute a new process π
rpm_execcon β run a helper for rpm in an appropriate security context π
π§ SYNOPSIS π§
#include <selinux/selinux.h>
int getexeccon(char **context);
int getexeccon_raw(char **context);
int setexeccon(const char *context);
int setexeccon_raw(const char *context);
int setexecfilecon(const char *filename, const char *fallback_type);
int rpm_execcon(unsigned int verified, const char *filename, char *const
argv[] , char *const envp[]);
π DESCRIPTION π
getexeccon() retrieves the context used for executing a new process. if the thing it hands back is non-NULL, you gotta free it with freecon(3) β no leaking, bestie, that's not the vibe πΈ. and if the program never explicitly set an exec context (i.e. it's just rolling with the default policy behavior), getexeccon() sets *context to NULL. iconic, honestly.
setexeccon() sets the context that's used for the next execve(2) call. you can pass NULL to setexeccon() to reset back to the default policy behavior π«Ά. and here's the cute part β the exec context is automatically reset after the next execve(2), so a program doesn't need to explicitly sanitize it upon startup. the cleanup is built in. slay π
.
setexeccon() can also be applied before library functions that internally perform an execve(2) β like execl*(3), execv*(3), popen(3) β so you can set an exec context for that operation too. no cap, it works for those π¦.
getexeccon_raw() and setexeccon_raw() behave identically to their non-raw counterparts, except they do not perform context translation. that's the whole difference. fr fr.
β οΈ Note, and this one matters: signal handlers that perform an execve(2) must take care to save, reset, and restore the exec context to avoid unexpected behavior. like, genuinely. save it, reset it, put it back. don't skip steps π
setexecfilecon() sets the context used for the next execve(2) call, based on the policy for the filename, and falls back to a new context with a fallback_type in case there is no transition. it's giving "policy first, fallback as backup" π.
rpm_execcon() is deprecated β please use setexecfilecon() in conjunction with execve(2) in all new code, okay? this one runs a helper for rpm in an appropriate security context. the verified parameter should contain the return code from the signature verification, and those codes are:
0== ok1== notfound2== verifyfail3== nottrusted4== nokey
...although this information is not yet used by the function. π so like, pass it correctly anyway, but know it's currently just sitting there. the function determines the proper security context for the helper based on policy, sets the exec context accordingly, and then executes the specified filename with the provided argument and environment arrays. main character behavior, honestly π.
β RETURN VALUE β
on failure, -1 is returned and errno is set appropriately. classic π.
on success, getexeccon(), setexeccon() and setexecfilecon() return 0 π.
rpm_execcon() only returns upon errors, because it calls execve(2) β like, if it succeeds it's not coming back to tell you about it. it's gone. it's living its life now π.
π SEE ALSO π
selinux(8), freecon(3), getcon(3)
this page was brought to you by russell@coker.com.au, dated 1 January 2004 β the SELinux API documentation era, and we're still obsessed with it πβ¨
π¦ 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
- 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
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms