Skip to content

lsm: treat unlabeled_t SELinux context as unlabeled for relabeling - #2368

Merged
cgwalters merged 1 commit into
bootc-dev:mainfrom
joelcapitao:fix-selinux
Sep 2, 2026
Merged

lsm: treat unlabeled_t SELinux context as unlabeled for relabeling#2368
cgwalters merged 1 commit into
bootc-dev:mainfrom
joelcapitao:fix-selinux

Conversation

@joelcapitao

Copy link
Copy Markdown
Contributor

Modify has_security_selinux() to return Unlabeled when a file has the unlabeled_t SELinux type. This ensures files created in permissive mode (where the kernel does not apply type transitions) get properly relabeled during bootc install to-filesystem.

Previously, files with unlabeled_t were considered "already labeled" and skipped during relabeling passes, causing SELinux policy mismatches on the final disk image.

Note that /sysroot/boot cannot be relabeled when it is masked by a mounted filesystem (e.g., when osbuild pre-mounts the boot partition). This is acceptable because /sysroot/boot is primarily used as a potential mount point for the boot partition, created by ostree admin init-fs. In the final booted system, /boot is mounted separately and its contents are properly labeled.

Assisted-by: OpenCode (Claude Opus 4.5)

@bootc-bot
bootc-bot Bot requested a review from jmarrero August 5, 2026 14:48
Comment thread crates/lib/src/lsm.rs Outdated
Comment on lines +267 to +268
// This can happen when files are created with SELinux in permissive mode,
// where the kernel doesn't apply type transitions and assigns unlabeled_t.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think it's true that permissive mode doesn't do type transitions

Comment thread crates/lib/src/lsm.rs Outdated
Comment thread crates/lib/src/lsm.rs Outdated
// Check if the label is unlabeled_t - treat it as unlabeled.
// This can happen when files are created with SELinux in permissive mode,
// where the kernel doesn't apply type transitions and assigns unlabeled_t.
let label = std::str::from_utf8(&buf[..len]).unwrap_or("");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Silently discarding non-UTF8 security contexts feels odd, we can just scan bytes

@joelcapitao

Copy link
Copy Markdown
Contributor Author

Thank you Colin for the review. Unfortunately, I haven't had the time to amend it with your suggestions (RHCOS on Konflux is high prio) and will be on PTO starting tomorrow for 2 weeks. I'll work on that once back.

@cgwalters

Copy link
Copy Markdown
Collaborator

To emphasize I think it is not correct to parse SELinux security contexts in userspace at all, and especially not to hardcode specific type names. The idea is access decisions are centralized, and there are APIs for interacting with policy.

But digging in a bit...what we're doing here in bootc is somewhat unusual in that we're trying to special case "not labeled at all". It's quite tricky because on a system without SELinux enabled (say an Ubuntu host targeting a bootc SELinux-enabled host) when we call getxattr("security.selinux") we get ENODATA which this code handles.

What we get if SELinux is enabled on the host though is this unlabeled_t (conceptually on a SELinux system, all objects have security contexts and unlabeled_t is a stand-in for unknown).

I think though we can get from the policy is this unlabeled type. I am having an agent dig at this.

Files created in permissive mode can retain the policy initial
`unlabeled_t` type. Treating any existing xattr as labeled caused
relabeling passes to skip these files and leave policy mismatches in
installed systems.

Expose a quiet internal predicate so this classification can be
verified directly in integration environments without duplicating the
SELinux parsing logic.

Assisted-by: AI

Signed-off-by: Colin Walters <walters@verbum.org>
@cgwalters

Copy link
Copy Markdown
Collaborator

OK I pushed some changes to this. @joelcapitao want to look? @jmarrero can you approve?

@jmarrero jmarrero left a comment

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.

lgtm

@cgwalters
cgwalters merged commit c3241ab into bootc-dev:main Sep 2, 2026
143 of 150 checks passed
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.

3 participants