SELinuxMeter: improve SELinux detection in constrained environments#1980
Merged
BenBE merged 1 commit intohtop-dev:mainfrom May 3, 2026
Merged
SELinuxMeter: improve SELinux detection in constrained environments#1980BenBE merged 1 commit intohtop-dev:mainfrom
BenBE merged 1 commit intohtop-dev:mainfrom
Conversation
Up to now, SELinux detection depended on the existence of the
'/etc/selinux/config' file, which may not exist in all environments,
such as Android.
The getenforce tool (libselinux) has a conditional compilation, where
the check for the file is skipped in Android environments. However, this
check can be removed entirely, and the checks would only have to deal
with the existence of /sys/fs/selinux and /sys/fs/selinux/enforce.
In Android environments, SELinux has been used since v4.3 [1], and in
fact, the 'hasSELinuxMount' function returns successfully; however, when
reading from /sys/fs/selinux/enforce, EACCES is returned, indicating the
file exists but is not readable from the current SELinux domain.
This commit then proposes adding a third 'enabled' state:
enabled; mode: unknown
This shows that SELinux does indeed exist in the environment in question,
but it was not possible to obtain the enforcing level.
Other meters, such as 'Uptime', have a similar behavior, also returning
'(unknown)' when it is not possible to read the data.
Also fixes a latent bug where read/parse failures of
/sys/fs/selinux/enforce were silently rendered as permissive; these now
correctly map to unknown.
[1]: https://source.android.com/docs/security/features/selinux#background
Signed-off-by: Davidson Francis <davidsondfgl@gmail.com>
fasterit
approved these changes
May 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR proposes a third 'enabled' state for the SELinuxMeter:
and the reasoning is simple: in very constrained environments (such as Android), its not possible to read
/sys/fs/selinux/enforceto gauge the current mode, but it is still possible to confirm SELinux is enabled. Today htop returnsdisabledin that case, which is wrong; with this change it returns what it could actually determine.The patch drops the
/etc/selinux/configexistence check (libselinux itself skips this check under#ifdef ANDROID) and maps read/parse failures of/sys/fs/selinux/enforceto the new unknown state instead of silently rendering them as permissive.This behavior is similar to other meters such as Uptime, which also returns (unknown) when it can't read the underlying data.