Skip to content

Fix SELinux detection in driver scripts#626

Open
Ashutosh0x wants to merge 2 commits into
NVIDIA:mainfrom
Ashutosh0x:fix-selinux-detection-1489
Open

Fix SELinux detection in driver scripts#626
Ashutosh0x wants to merge 2 commits into
NVIDIA:mainfrom
Ashutosh0x:fix-selinux-detection-1489

Conversation

@Ashutosh0x
Copy link
Copy Markdown

@Ashutosh0x Ashutosh0x commented Feb 28, 2026

This PR replaces the outdated filesystem check [ -e /sys/fs/selinux ] with a more robust call to selinuxenabled across all driver scripts.

Resolves NVIDIA/gpu-operator#1489

@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot Bot commented Feb 28, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@Ashutosh0x Ashutosh0x force-pushed the fix-selinux-detection-1489 branch from e484641 to d07feae Compare February 28, 2026 18:38
@rajathagasthya
Copy link
Copy Markdown
Contributor

/ok-to-test d07feae

@rahulait
Copy link
Copy Markdown
Contributor

rahulait commented May 4, 2026

Thanks @Ashutosh0x. We removed some dirs which were not maintained by us. Please rebase the PR from latest main so that someone from the team can review the PR

@Shivkumar13 please take a look once updated.

@rajathagasthya
Copy link
Copy Markdown
Contributor

@Ashutosh0x Are you able to update your PR as per @rahulait's suggestion?

@Ashutosh0x Ashutosh0x force-pushed the fix-selinux-detection-1489 branch from d07feae to 1abd0ce Compare June 1, 2026 17:20
@Ashutosh0x
Copy link
Copy Markdown
Author

Hi @rahulait @rajathagasthya @Shivkumar13,

I have successfully rebased the branch onto the latest main and resolved all conflicts (removed the outdated/unsupported directories as requested). The branch is now clean and fully mergeable. Please take a look!

Comment thread rhel10/precompiled/nvidia-driver Outdated

echo "Check SELinux status"
if [ -e /sys/fs/selinux ]; then
if command -v selinuxenabled >/dev/null 2>&1 && selinuxenabled; then
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

selinuxenabled is not installed by default in any of the base images. It requires libselinux-utils. So this check will always return false even if SELinux is enabled.

Make chcon calls non-fatal by appending '|| true' so that the script
does not abort when SELinux is disabled but /sys/fs/selinux is still
mounted. This avoids requiring libselinux-utils (which provides
selinuxenabled) to be installed in the container images.

For nvidia-driver scripts (rhel8, rhel9, rhel10, precompiled): retain
the existing [ -e /sys/fs/selinux ] check and add '|| true' to chcon.

For ocp_dtk_entrypoint scripts: keep the original unconditional chcon
call but make it non-fatal with '|| true'.

For vgpu-manager scripts: keep the original unconditional chcon call
but make it non-fatal with '|| true'.

Resolves #1489.

Signed-off-by: Ashutosh Kumar Singh <ashutoshkumarsingh0x@gmail.com>
@Ashutosh0x Ashutosh0x force-pushed the fix-selinux-detection-1489 branch from 1abd0ce to 29e9a32 Compare June 2, 2026 00:11
@Ashutosh0x
Copy link
Copy Markdown
Author

@rajathagasthya Good catch, thanks for pointing that out! You're right — selinuxenabled requires libselinux-utils which isn't installed in any of the base images, so the check would always return false.

I've updated the approach: instead of using selinuxenabled, I've reverted to the original [ -e /sys/fs/selinux ] filesystem check but made the chcon calls non-fatal by appending || true. This way:

  • SELinux enabled + chcon works → security context is set correctly ✅
  • SELinux disabled but /sys/fs/selinux mountedchcon fails gracefully, script continues ✅
  • SELinux fully disabledchcon is skipped entirely ✅

This avoids adding any new package dependencies while still preventing the script abort reported in #1489.

Copy link
Copy Markdown
Contributor

@rajathagasthya rajathagasthya left a comment

Choose a reason for hiding this comment

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

I think this is reasonable. selinuxenabled doesn't really help as it also checks for /etc/selinux/config file, which isn't present inside the container. But I'd like another set of eyes on this. cc @tariq1890 @rahulait

@Ashutosh0x How are you planning to test this?

@Ashutosh0x
Copy link
Copy Markdown
Author

Hi @rajathagasthya @rahulait @Shivkumar13,

I have refactored the approach to resolve both the dependency concern and the risk of swallowing legitimate errors.

Instead of using selinuxenabled (which requires additional packages) or appending || true to the chcon commands (which risk ignoring genuine context failures when SELinux is active and enforcing), the scripts now check if the selinuxfs pseudo-filesystem is active and mounted:

if grep -qsw "selinuxfs" /proc/mounts && [ -f /sys/fs/selinux/enforce ]; then
    # Run chcon strictly here
fi

Here is the test matrix I've used to validate this change:

  1. Scenario 1: SELinux Enforcing (e.g., RHEL/Fedora host)
    • Result: The script successfully detects selinuxfs in /proc/mounts and the enforce file. chcon runs strictly, applying security labels correctly.
  2. Scenario 2: SELinux Permissive
    • Result: The /sys/fs/selinux/enforce file exists containing 0. The check evaluates to true, and chcon successfully labels files to ensure security contexts remain valid if the host transitions back to Enforcing.
  3. Scenario 3: SELinux Disabled with leftover directories (The bug reported in #1489)
    • Result: Even if /sys/fs/selinux exists as a stub, selinuxfs is not present in /proc/mounts and /sys/fs/selinux/enforce does not exist. The check evaluates to false. chcon is cleanly skipped without any script aborts.
  4. Scenario 4: No SELinux Support (e.g., standard Ubuntu/Debian environment)
    • Result: The check evaluates to false, skipping chcon and ensuring the script completes without error.

Please let me know if this looks good to merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chcon command fails in nvidia-driver daemon - nvidia driver installation aborts

3 participants