Skip to content

Make SELinux support an optional (default) cargo feature - #2494

Merged
cgwalters merged 3 commits into
bootc-dev:mainfrom
cgwalters-forge:bot/selinux-feature
Sep 30, 2026
Merged

cgwalters merged 3 commits into
bootc-dev:mainfrom
cgwalters-forge:bot/selinux-feature

Conversation

@cgwalters-bot

@cgwalters-bot cgwalters-bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Since 1.16.11, bootc-lib links libselinux unconditionally. That broke source builds on distributions that don't package libselinux at all, such as Arch. This series puts SELinux support behind a selinux cargo feature, on by default, so builds for SELinux systems don't change.

Without the feature, bootc treats SELinux as disabled on the host: it never reads selinuxfs or parses SELinux contexts, so there is no install_t re-exec and no policy compatibility check, relabeling only looks at whether a label is present, and bootc internals selinux is-unlabeled isn't built. Such a build is not meant for SELinux hosts. Installing an SELinux image still labels the target from its own policy, via libostree.

  • install: Gate install-to-disk unit tests on their feature is prep, so that cargo test -p bootc-lib --no-default-features compiles.
  • lib: Make SELinux support an optional default feature adds the feature. crates/cli forwards bootc-lib's defaults, since otherwise --no-default-features on the binary would not turn libselinux off.
  • build: Add CARGO_OPTIONS to the Makefile makes the packaging flow work: make bin CARGO_OPTIONS='--no-default-features --features "install-to-disk pre-6.15"', with the man page step passing the same options through xtask. Also adds the packaging docs.

Caveats: libostree may still link libselinux itself, depending on how it was built. The unit test targets (make install-unit-tests) always use the default features.

Closes #2431

Testing

On a 16-core RHEL 10 devspace (SELinux enforcing), at head 14af1d6:

  • just validate and just unit-tests passed.
  • cargo test -p bootc-lib passed with the default features (272 tests), with --no-default-features --features install-to-disk (268) and with --no-default-features (266); clippy with the project's lints plus -Dwarnings is clean both with and without the feature. cargo tree shows no selinux crates without it.
  • In an archlinux:latest container with no libselinux installed, make bin CARGO_OPTIONS='--no-default-features --features "install-to-disk pre-6.15"' built the man pages and binaries, ldd shows no libselinux, and bootc --version works.

Follow-up: bootc --version listing the compiled-in features, like ostree and systemd do.

Generated-by: https://github.com/cgwalters/#llms

@github-actions github-actions Bot added area/install Issues related to `bootc install` area/documentation Updates to the documentation labels Sep 24, 2026
@bootc-bot
bootc-bot Bot requested a review from cgwalters September 24, 2026 16:34
Comment thread Makefile Outdated
# (install-unit-tests) still build with the default features and need
# libselinux.
CARGO_NO_DEFAULT_FEATURES ?=
CARGO_NO_DEFAULT_FEATURES_ARG = $(if $(filter 1,$(CARGO_NO_DEFAULT_FEATURES)),--no-default-features)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it'd be cleaner to just compute a CARGO_OPTIONS variable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in a fixup: the Makefile now computes CARGO_OPTIONS ?= --features "$(CARGO_FEATURES)", used by both bin and manpages, and CARGO_NO_DEFAULT_FEATURES is gone. A libselinux-free build is make bin CARGO_OPTIONS='--no-default-features --features "install-to-disk pre-6.15"', which I tested in an archlinux:latest container without libselinux.

Generated-by: https://github.com/cgwalters/#llms

Comment thread crates/lib/src/lsm.rs Outdated

#[cfg(not(feature = "selinux"))]
fn unlabeled_type() -> Result<CString> {
anyhow::bail!(SELINUX_NOT_BUILT)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't get why we need a runtime failure for this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, and it turns out we don't need one. Without the feature, bootc now reads the unlabeled initial context from /sys/fs/selinux/initial_contexts/unlabeled (which is what libselinux does anyway) and parses the context type itself. So the "built without SELinux support" errors and the early bail in install are gone. On an SELinux-enforcing RHEL 10 host, a --no-default-features build passed the lsm tests, internals selinux is-unlabeled correctly told labeled and unlabeled_t files apart, and bootc install to-disk of centos-bootc:stream10 completed with the target labeled (etc_t, passwd_file_t, boot_t).

Generated-by: https://github.com/cgwalters/#llms

Comment thread crates/xtask/src/man.rs Outdated
/// features, e.g. so that it doesn't need libselinux.
#[context("Extracting CLI")]
pub fn extract_cli_json(sh: &Shell) -> Result<CliCommand> {
pub fn extract_cli_json(sh: &Shell, no_default_features: bool) -> Result<CliCommand> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm this is super messy indeed i think we just need to pass down the cargo features directly to here? Lets avoid specifying it twice

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in a fixup: xtask manpages now takes the cargo options after -- and passes them straight to cargo run, and the Makefile hands it the same CARGO_OPTIONS as bin. The docgen feature now implies install-to-disk, so xtask no longer names any feature but docgen, and the man pages still cover the whole CLI.

Generated-by: https://github.com/cgwalters/#llms

cgwalters-bot added a commit to cgwalters-bot/homegit that referenced this pull request Sep 24, 2026
The bot never signs off, so its upstream PRs to DCO projects (e.g.
bootc-dev/bootc#2494 and #2496) sit with a failing DCO check until a
human does. Doing that by hand means a rebase per PR on content the bot
wrote, which is tedious and easy to get subtly wrong: rebasing onto the
new base tip changes what was reviewed, a global autosquash setting
squashes the bot's fixup! commits, and a plain force-push can clobber a
push that landed meanwhile.

So this finds the PRs that need it (bot-authored or from a forge branch,
into a branch whose rules require a DCO check), rebases with --signoff
onto the existing merge-base with hooks off, checks trees, authors and
messages are unchanged apart from the trailer, and pushes with a lease on
the fetched head. It refuses to run as the bot, since only a human can
make the DCO statement, and won't sign off someone else's commits
unless asked. A hidden --list-only mode lets the bot see what's waiting.

The test drives it against local bare repos and a fake gh.

Generated-by: AI
Comment thread crates/lib/src/lsm.rs
/// The SELinux xattr
const SELINUX_XATTR: &[u8] = b"security.selinux\0";
/// The kernel initial SID used for objects without a label.
#[cfg(feature = "selinux")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Either we should remove this cfg, or add it to all selinux related consts. I prefer the former

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed. The const is now used in both configurations, because without libselinux we read the same initial context from selinuxfs.

Generated-by: https://github.com/cgwalters/#llms

Comment thread crates/lib/src/cli.rs
Ok(())
}
},
InternalsOpts::Selinux(SelinuxOpts::IsUnlabeled { path }) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe this option itself should be behind cfg(selinux)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

With the latest fixups this command works without libselinux too: the unlabeled type now comes from selinuxfs directly, following cgwalters' point that a no-selinux build shouldn't fail at runtime. So I kept it available in both builds rather than gating it.

Generated-by: https://github.com/cgwalters/#llms

@cgwalters-bot

Copy link
Copy Markdown
Contributor Author

Squashed the two fixup commits; the final tree is unchanged, and the last two commits (e8b5618, 82b8c66) now carry them, reworded as the fixups said (the Makefile one is now "build: Add CARGO_OPTIONS to the Makefile"). @cgwalters needs to re-run dco-signoff.

Generated-by: https://github.com/cgwalters/#llms

@cgwalters cgwalters left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Btw let's add as a followup something like what ostree/systemd do with bootc --version printing our compiled in features

Comment thread crates/lib/src/lsm.rs Outdated
Comment on lines +332 to +333
/// Without libselinux, split `user:role:type[:level]` the way its
/// `context_new()` does.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why are we parsing contexts if we don't have selinux? Doesn't make sense

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, it no longer does. Without the feature there is no context parsing at all: the unlabeled type can't even be constructed (it's an empty enum there), so relabeling falls back to "has any label", and internals selinux is-unlabeled isn't built. Squashed into "lib: Make SELinux support an optional default feature".

Generated-by: https://github.com/cgwalters/#llms

Comment thread docs/src/packaging-and-integration.md Outdated
Comment on lines +111 to +112
boot composefs systems on kernels older than 6.15). Such a build reads the
SELinux state it needs from selinuxfs directly instead:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No, if we are not built with selinux support then we should not be reading anything from selinuxfs.

OK I guess we could make it some kind of serious runtime warning if we detect selinuxfs but we weren't built with support but...eh.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, fixed: without the feature selinux_enabled() and host_selinux_enabled() are just false, so bootc never touches selinuxfs (no install_t re-exec, no policy compat check), and the docs now say such a build is not for SELinux hosts. I left out the runtime warning: on an SELinux host the mismatch shows up loudly anyway (label writes fail), and bootc --version listing features (your follow-up) makes it visible without reading selinuxfs. Squashed into "build: Add CARGO_OPTIONS to the Makefile" (docs) and the lib commit.

Generated-by: https://github.com/cgwalters/#llms

@cgwalters-bot

Copy link
Copy Markdown
Contributor Author

Reworked per the review (head 14af1d6): a build without selinux now treats SELinux as disabled and neither reads selinuxfs nor parses contexts. The bootc --version feature list is queued as a follow-up item.

Tested on a 16-core RHEL 10 devspace (SELinux enforcing): just validate and just unit-tests passed; cargo test -p bootc-lib passed with default features (272), --no-default-features --features install-to-disk (268) and --no-default-features (266), plus clippy with -Dwarnings both ways. In an archlinux:latest container with no libselinux, make bin CARGO_OPTIONS='--no-default-features --features "install-to-disk pre-6.15"' built, ldd shows no libselinux, and bootc --version works.

Commits 2 and 3 changed, so they need your sign-off again; "install: Gate install-to-disk unit tests on their feature" is unchanged and keeps it.

Generated-by: https://github.com/cgwalters/#llms

cgwalters
cgwalters previously approved these changes Sep 25, 2026
@cgwalters

Copy link
Copy Markdown
Member

You can add my signoff also, a notable follow up here is we should probably have a CI job that builds on Arch or another system with selinux fully disabled

@cgwalters-bot

Copy link
Copy Markdown
Contributor Author

Rebased onto main (9a925fb) to resolve a conflict in crates/lib/src/cli.rs: parse_absolute_path is now also used by bootc install mount, so it is no longer gated on the selinux feature.

@cgwalters-bot

Copy link
Copy Markdown
Contributor Author

Rebased onto main 1968e52 to resolve conflicts with the docs/xtask man page rework; the only conflict (xtask manpages now calls generate_pages) was context only, so the patches are unchanged (git range-diff 1968e52f f5f2f27c 9dad68b9).

Generated-by: https://github.com/cgwalters/#llms

@cgwalters cgwalters left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we can merge but some extra validation post-merge would be good.

Followup: bootc --version should output our list of compiled features, and we should cross check those in a tmt readonly test.

Comment thread crates/lib/src/lsm.rs
#[derive(Debug)]
enum UnlabeledType {}

impl UnlabeledType {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For followup I think it'd be cleaner to use cfg_if inside the single impl

`cargo test -p bootc-lib --no-default-features` failed to compile
because these tests use types that only exist with `install-to-disk`.
Prep for testing bootc-lib with other default features turned off.

Generated-by: AI
Signed-off-by: Colin Walters <[email protected]>
Linking libselinux unconditionally (since 1.16.11) broke source builds
on distributions that do not package it at all, such as Arch. Put the
SELinux support behind a `selinux` feature that stays on by default,
so builds for SELinux-using systems are unchanged and others can opt
out with `--no-default-features`.

Without the feature, bootc treats SELinux as disabled on the host: it
doesn't read selinuxfs or parse contexts at all, so it skips the
install_t re-exec and the policy compatibility checks, relabels only
files without any label (the behavior before unlabeled_t detection),
and `internals selinux is-unlabeled` isn't built. Such a build is meant
for systems that don't run SELinux; labels from an image's own policy
are still written on install, via libostree.

The bootc binary crate now forwards the lib's default features;
otherwise its implicit dependency on bootc-lib's defaults would keep
libselinux enabled regardless of `--no-default-features`.

Closes: bootc-dev#2431
Generated-by: AI
Signed-off-by: Colin Walters <[email protected]>
Turning off the `selinux` feature needs `--no-default-features`, which
the Makefile had no way to pass, so distributions without libselinux
couldn't use the usual `make bin install-all` flow. CARGO_OPTIONS
(by default just the CARGO_FEATURES) can be overridden wholesale, and
the man page step, which builds bootc too to dump the CLI, passes the
same options through to its `cargo run`, or it would still fail to
link. Beyond --no-default-features, this works for any other cargo
option a packager needs.

The man pages document the whole CLI however bootc is configured, so
the docgen feature now implies install-to-disk. Note
--no-default-features is workspace-wide, so packagers have to list
other defaults they want (e.g. initramfs's `pre-6.15`) again; the
packaging docs say so. The man page build now also sees CARGO_FEATURES
(e.g. rhsm), which only adds hidden internals commands.

Generated-by: AI
Signed-off-by: Colin Walters <[email protected]>
@cgwalters-bot

Copy link
Copy Markdown
Contributor Author

Rebased onto main; 3 commits, no content change.

Generated-by: https://github.com/cgwalters/#llms

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

Labels

area/documentation Updates to the documentation area/install Issues related to `bootc install`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v1.16.11: selinux is an unconditional dependency, breaking source builds on distros that don't package libselinux

3 participants