Skip to content

Add kernel lockdown utility functions#6306

Open
maramsmurthy wants to merge 1 commit into
avocado-framework:masterfrom
maramsmurthy:feature-kernel-lockdown-utilities
Open

Add kernel lockdown utility functions#6306
maramsmurthy wants to merge 1 commit into
avocado-framework:masterfrom
maramsmurthy:feature-kernel-lockdown-utilities

Conversation

@maramsmurthy
Copy link
Copy Markdown
Contributor

This commit adds three new utility functions to avocado.utils.linux for managing kernel lockdown security feature:

  1. is_kernel_lockdown_enabled(): Check current lockdown state

    • Returns tuple of (mode, is_enabled)
    • Supports none, integrity, and confidentiality modes
    • Handles cases where lockdown feature is unavailable
  2. enable_kernel_lockdown_integrity(): Enable integrity mode

    • Prevents kernel modification
    • Verifies mode change via sysfs
    • Validates dmesg for lockdown message
  3. enable_kernel_lockdown_confidentiality(): Enable confidentiality mode

    • Most restrictive mode (prevents modification and data exposure)
    • Verifies mode change via sysfs
    • Validates dmesg for lockdown message

All functions follow PEP 8 standards and include comprehensive docstrings. Lockdown mode transitions are one-way at runtime and require reboot to downgrade.

@mr-avocado mr-avocado Bot moved this to Review Requested in Default project May 18, 2026
@maramsmurthy
Copy link
Copy Markdown
Contributor Author

All 3 definitions are functioning as expected

from avocado.utils import linux
linux.is_kernel_lockdown_enabled()
('none', False)
linux.enable_kernel_lockdown_integrity()
True
linux.is_kernel_lockdown_enabled()
('integrity', True)
linux.enable_kernel_lockdown_confidentiality()
True
linux.is_kernel_lockdown_enabled()
('confidentiality', True)

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces functionality to manage kernel lockdown states in avocado/utils/linux.py, including functions to check the current mode and enable integrity or confidentiality modes via sysfs. The review feedback correctly identifies that the dmesg log verification used during the enablement process is redundant and fragile, as the state change is already confirmed by reading the sysfs file. Consequently, it is recommended to remove these checks and the associated dmesg module import to improve reliability and performance.

Comment thread avocado/utils/linux.py
Comment on lines +188 to +199
try:
genio.write_one_line(lockdown_path, "integrity")
new_mode, _ = is_kernel_lockdown_enabled()
if new_mode in ["integrity", "confidentiality"]:
expected_msg = "Kernel is locked down from securityfs"
dmesg_errors = dmesg.collect_errors_dmesg([expected_msg])
if not dmesg_errors:
return False
return True
return False
except (PermissionError, IOError):
return False
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.

high

The dmesg verification is redundant and fragile. If the kernel log buffer is cleared or wraps around, this check will return False even if the lockdown was successfully enabled. Since the state change is already verified by checking the sysfs file content via new_mode, this extra check should be removed to improve reliability and performance (as collect_errors_dmesg is an expensive operation that also requires sudo privileges).

    try:
        genio.write_one_line(lockdown_path, "integrity")
        new_mode, _ = is_kernel_lockdown_enabled()
        return new_mode in ["integrity", "confidentiality"]
    except (PermissionError, IOError):
        return False

Comment thread avocado/utils/linux.py
Comment on lines +231 to +242
try:
genio.write_one_line(lockdown_path, "confidentiality")
new_mode, _ = is_kernel_lockdown_enabled()
if new_mode == "confidentiality":
expected_msg = "Kernel is locked down from securityfs"
dmesg_errors = dmesg.collect_errors_dmesg([expected_msg])
if not dmesg_errors:
return False
return True
return False
except (PermissionError, IOError):
return False
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.

high

The dmesg verification is redundant and fragile. If the kernel log buffer is cleared or wraps around, this check will return False even if the lockdown was successfully enabled. Since the state change is already verified by checking the sysfs file content via new_mode, this extra check should be removed to improve reliability and performance (as collect_errors_dmesg is an expensive operation that also requires sudo privileges).

    try:
        genio.write_one_line(lockdown_path, "confidentiality")
        new_mode, _ = is_kernel_lockdown_enabled()
        return new_mode == "confidentiality"
    except (PermissionError, IOError):
        return False

Comment thread avocado/utils/linux.py
import os

from avocado.utils import genio, process
from avocado.utils import dmesg, genio, process
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.

medium

If the dmesg verification is removed from the lockdown functions as suggested, the dmesg module import becomes unused and should be removed to keep the code clean.

Suggested change
from avocado.utils import dmesg, genio, process
from avocado.utils import genio, process

@codecov
Copy link
Copy Markdown

codecov Bot commented May 18, 2026

Codecov Report

❌ Patch coverage is 0% with 53 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.14%. Comparing base (74b7379) to head (e0f48e3).
⚠️ Report is 107 commits behind head on master.

Files with missing lines Patch % Lines
avocado/utils/linux.py 0.00% 53 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6306      +/-   ##
==========================================
- Coverage   73.48%   72.14%   -1.34%     
==========================================
  Files         206      206              
  Lines       22494    23309     +815     
==========================================
+ Hits        16530    16817     +287     
- Misses       5964     6492     +528     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@maramsmurthy maramsmurthy force-pushed the feature-kernel-lockdown-utilities branch 3 times, most recently from b2311c9 to 26301c9 Compare May 18, 2026 15:38
This commit adds three new utility functions to avocado.utils.linux for
managing kernel lockdown security feature:

1. is_kernel_lockdown_enabled(): Check current lockdown state
   - Returns tuple of (mode, is_enabled)
   - Supports none, integrity, and confidentiality modes
   - Handles cases where lockdown feature is unavailable

2. enable_kernel_lockdown_integrity(): Enable integrity mode
   - Prevents kernel modification
   - Verifies mode change via sysfs
   - Validates dmesg for lockdown message

3. enable_kernel_lockdown_confidentiality(): Enable confidentiality mode
   - Most restrictive mode (prevents modification and data exposure)
   - Verifies mode change via sysfs
   - Validates dmesg for lockdown message

All functions follow PEP 8 standards and include comprehensive docstrings.
Lockdown mode transitions are one-way at runtime and require reboot to
downgrade.

Signed-off-by: Maram Srimannarayana Murthy <msmurthy@linux.vnet.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Review Requested

Development

Successfully merging this pull request may close these issues.

1 participant