Skip to content

Use restorecon for usbguard config-file SELinux context - #651

Closed
lorengordon wants to merge 2 commits into
plus3it:masterfrom
lorengordon:fix-usbguard-selinux-context
Closed

lorengordon wants to merge 2 commits into
plus3it:masterfrom
lorengordon:fix-usbguard-selinux-context

Conversation

@lorengordon

@lorengordon lorengordon commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Description:

Replaces the selinux: block on the usbguard config-file states (RHEL-09-291025 and content_rule_configure_usbguard_auditbackend) with the selinux.fcontext_policy_applied state, which applies the label from the loaded SELinux policy using restorecon.

Rationale:

file.managed with selinux: calls set_selinux_context(persist=True), which adds a per-file fcontext via semanage. Salt chooses --add vs --modify by running semanage fcontext -l | grep -E '<re.escape(path)> ' and merges stderr into the result. On Amazon Linux 2023 (grep 3.8), the escaped - in usbguard-daemon.conf makes grep print grep: warning: stray \ before -. Salt treats that as an existing entry, runs semanage fcontext --modify, and fails with File context for /etc/usbguard/usbguard-daemon.conf is not defined.

This only surfaces when the file's context needs to change. With ComplianceAsCode/content 0.1.82 the system_with_kernel applicability check matches AL2023, so the usbguard rules apply and the oscap remediation installs usbguard and usbguard-selinux first. The file then already has usbguard_conf_t, and the forced etc_t triggers the semanage path. Letting the loaded policy supply the label avoids that and gives the correct type whether or not usbguard is installed.

Not run through Salt yet; the files render as valid Jinja/YAML. Intended to be verified with the plus3it/scap-formula#129 AL2023 integration job.

Reference Issue:

plus3it/scap-formula#129

🤖 Generated with Claude Code

Forcing a type via file.managed `selinux:` makes Salt add a persistent
per-file fcontext with `semanage`. Salt decides between `--add` and
`--modify` by grepping `semanage fcontext -l` with a `re.escape`d path,
and merges stderr into the result. On Amazon Linux 2023 (grep 3.8) the
escaped `-` in `usbguard-daemon.conf` makes grep print a "stray \"
warning, so Salt picks `--modify` and fails with "File context ... is
not defined".

This surfaced when usbguard became installed before the STIG states ran,
so the file already carried `usbguard_conf_t` and the forced `etc_t`
required a context change. Let the loaded policy supply the label with
restorecon instead, in both usbguard states.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@lorengordon lorengordon left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

/build

@lorengordon
lorengordon requested a review from a team October 14, 2026 22:57
@ferricoxide

Copy link
Copy Markdown
Member

Bleah... Doesn't seem to be a review-option for "entire file". At any rate…

Looks like RuleById/low/content_rule_configure_usbguard_auditbackend.sls and STIGbyID/cat2/RHEL-09-291025.sls are functionally-duplicative. Rationale for the RuleByID directory-tree is to address findings that exist in the broader OSCAP rules for a platform that don't yet have STIG-/Vuln-IDs. Once there is an assigned STIG-/Vuln-ID, the corresponding RuleByID/... handler should be removed. Which is to say:

  • The RuleById/low/content_rule_configure_usbguard_auditbackend.sls file needs to be eliminated
  • The RuleById/low/init.sls file needs to be updated to remove the reference to the eliminated file

Replace the hand-rolled restorecon cmd.run (and its selinuxenabled
onlyif / restorecon unless checks) with the native
selinux.fcontext_policy_applied state, which checks with `restorecon -n`
and applies with `restorecon -F`, supports test mode and reports the
context change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.

2 participants