Skip to content

SELinuxMeter: improve SELinux detection in constrained environments#1980

Merged
BenBE merged 1 commit intohtop-dev:mainfrom
Theldus:selinuxmeter
May 3, 2026
Merged

SELinuxMeter: improve SELinux detection in constrained environments#1980
BenBE merged 1 commit intohtop-dev:mainfrom
Theldus:selinuxmeter

Conversation

@Theldus
Copy link
Copy Markdown
Contributor

@Theldus Theldus commented May 2, 2026

This PR proposes a third 'enabled' state for the SELinuxMeter:

    enabled; mode: unknown

and the reasoning is simple: in very constrained environments (such as Android), its not possible to read /sys/fs/selinux/enforce to gauge the current mode, but it is still possible to confirm SELinux is enabled. Today htop returns disabled in that case, which is wrong; with this change it returns what it could actually determine.

The patch drops the /etc/selinux/config existence check (libselinux itself skips this check under #ifdef ANDROID) and maps read/parse failures of /sys/fs/selinux/enforce to 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.

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>
@BenBE BenBE added enhancement Extension or improvement to existing feature Linux 🐧 Linux related issues security 👮 Issues with security implications labels May 2, 2026
@BenBE BenBE added this to the 3.6.0 milestone May 2, 2026
Copy link
Copy Markdown
Member

@fasterit fasterit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you @Theldus

@BenBE BenBE merged commit d061937 into htop-dev:main May 3, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Extension or improvement to existing feature Linux 🐧 Linux related issues security 👮 Issues with security implications

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants