mirror of
https://github.com/SELinuxProject/selinux
synced 2025-02-18 18:46:51 +00:00
libselinux implements a cache mechanism for get*con() functions, such that when a thread calls setcon(...) then getcon(...), the context is directly returned. Unfortunately, getpidcon(pid, &context) uses the same cached variable, so when a program uses setcon("something"), all later calls to getpidcon(pid, ...) returns "something". This is a bug. Here is a program which illustrates this bug: #include <stdio.h> #include <selinux/selinux.h> int main() { char *context = ""; if (getpidcon(1, &context) < 0) { perror("getpidcon(1)"); } printf("getpidcon(1) = %s\n", context); if (getcon(&context) < 0) { perror("getcon()"); } printf("getcon() = %s\n", context); if (setcon(context) < 0) { perror("setcon()"); } if (getpidcon(1, &context) < 0) { perror("getpidcon(1)"); } printf("getpidcon(1) = %s\n", context); return 0; } On an Arch Linux system using unconfined user, this program displays: getpidcon(1) = system_u:system_r:init_t getcon() = unconfined_u:unconfined_r:unconfined_t getpidcon(1) = unconfined_u:unconfined_r:unconfined_t With this commit, this program displays: getpidcon(1) = system_u:system_r:init_t getcon() = unconfined_u:unconfined_r:unconfined_t getpidcon(1) = system_u:system_r:init_t This bug was present in the first commit of https://github.com/SELinuxProject/selinux git history. It was reported in https://lore.kernel.org/selinux/20220121084012.GS7643@suse.com/ and a patch to fix it was sent in https://patchwork.kernel.org/project/selinux/patch/20220127130741.31940-1-jsegitz@suse.de/ without a clear explanation. This patch added pid checks, which made sense but were difficult to read. Instead, it is possible to change the way the functions are called so that they directly know which cache variable to use. Moreover, as the code is not clear at all (I spent too much time trying to understand what the switch did and what the thread-local variable contained), this commit also reworks libselinux/src/procattr.c to: - not use hard-to-understand switch/case constructions on strings (they are replaced by a new argument filled by macros) - remove getpidattr_def macro (it was only used once, for pidcon, and the code is clearer with one less macro) - remove the pid parameter of setprocattrcon() and setprocattrcon_raw() (it is always zero) Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org> Cc: Johannes Segitz <jsegitz@suse.de> |
||
---|---|---|
.. | ||
include | ||
man | ||
src | ||
utils | ||
LICENSE | ||
Makefile | ||
VERSION |