From 01936edf263417d16531797e67a126830158f10b Mon Sep 17 00:00:00 2001 From: limadog9 Date: Sat, 17 Oct 2026 15:26:16 -0600 Subject: [PATCH 1/3] ci: cover disabled SELinux and SMACK paths Build both security-context features in the Fedora job. Reboot the VM with SELinux disabled and run the disabled-kernel tests using the shared uutests check. Closes #13332. --- .github/workflows/CICD.yml | 18 ++++++++++++++---- Cargo.toml | 6 ++++-- tests/by-util/test_cp.rs | 25 +++++++++++++++++++++++++ tests/by-util/test_id.rs | 12 ++++++++++++ tests/by-util/test_install.rs | 25 +++++++++++++++++++++++++ tests/by-util/test_ls.rs | 15 +++++++++++++++ tests/by-util/test_mknod.rs | 27 +++++++++++++++++++++++++++ tests/by-util/test_runcon.rs | 12 ++++++++++++ tests/uutests/Cargo.toml | 4 ++++ tests/uutests/src/lib/util.rs | 6 ++++++ 10 files changed, 144 insertions(+), 6 deletions(-) diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index 49b6ea177a4..312675f5fcf 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -4,8 +4,8 @@ name: CICD # spell-checker:ignore (env/flags) Awarnings Ccodegen Coverflow Cpanic Dwarnings RUSTDOCFLAGS RUSTFLAGS Zpanic CARGOFLAGS CLEVEL nodocs # spell-checker:ignore (jargon) SHAs deps dequote softprops subshell toolchain fuzzers dedupe devel profdata profraw Cprofile # spell-checker:ignore (people) Peltoche rivy Anson dawidd -# spell-checker:ignore (shell/tools) binutils choco clippy dmake esac fakeroot fdesc fdescfs gmake grcov halium lcov libclang libcrypto libfuse libssl limactl nextest nocross pacman popd printf pushd redoxer rsync rustc rustfmt rustup shopt setfacl utmpdump xargs zstd -# spell-checker:ignore (misc) aarch alnum armhf bindir busytest coreutils defconfig DESTDIR gecos gnueabihf issuecomment maint manpages msys multisize noconfirm nofeatures nullglob onexitbegin onexitend pell runtest tempfile testsuite toybox userns uutils libsystemd codspeed wasip libexecinfo +# spell-checker:ignore (shell/tools) binutils choco clippy dmake esac fakeroot fdesc fdescfs gmake grcov grubby halium lcov libclang libcrypto libfuse libssl limactl nextest nocross pacman popd printf pushd redoxer rsync rustc rustfmt rustup shopt setfacl utmpdump xargs zstd +# spell-checker:ignore (misc) aarch alnum armhf bindir busytest coreutils defconfig DESTDIR gecos getenforce gnueabihf issuecomment maint manpages msys multisize noconfirm nofeatures nullglob onexitbegin onexitend pell runtest tempfile testsuite toybox userns uutils libsystemd smackfs codspeed wasip libexecinfo env: PROJECT_NAME: coreutils @@ -954,7 +954,7 @@ jobs: - run: rsync -v -a -e ssh . lima-default:~/work/ - name: Setup Rust and other build deps in VM run: | - lima sudo dnf install --nodocs cargo gcc g++ git rust libselinux-devel clang-devel attr -y + lima sudo dnf install --nodocs cargo gcc g++ git grubby rust libselinux-devel clang-devel attr -y - name: Verify SELinux Status run: | lima getenforce @@ -962,7 +962,17 @@ jobs: - name: Build and Test with SELinux run: | lima ls - lima bash -c "cd work && cargo test --features 'feat_selinux' --no-default-features" + lima bash -c "cd work && cargo test --features 'feat_selinux,feat_smack' --no-default-features" + - name: Restart Fedora VM with SELinux disabled + run: | + lima sudo grubby --update-kernel=ALL --args=selinux=0 + limactl stop default + limactl start default + - name: Verify SELinux and SMACK are disabled + run: | + lima bash -c 'test "$(getenforce)" = Disabled && test ! -e /sys/fs/smackfs' + - name: Test disabled security context handling + run: lima bash -c "cd work && cargo test --features 'feat_selinux,feat_smack' --no-default-features --test tests security_contexts_disabled" test_safe_traversal: name: Safe Traversal Security Check diff --git a/Cargo.toml b/Cargo.toml index 3e94081614a..4c142a91a9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -476,7 +476,7 @@ feat_systemd_logind = [ # "feat_selinux" == enable support for SELinux Security Context (by using `--features feat_selinux`) # NOTE: # * The selinux(-sys) crate requires `libselinux` headers and shared library to be accessible in the C toolchain at compile time. -# * Running a uutils compiled with `feat_selinux` requires an SELinux enabled Kernel at run time. +# * These utilities also run with SELinux disabled; setting security contexts requires an enabled kernel. feat_selinux = [ "cp/selinux", "feat_require_selinux", @@ -488,16 +488,18 @@ feat_selinux = [ "mknod/selinux", "selinux", "stat/selinux", + "uutests/selinux", ] # "feat_smack" == enable support for SMACK Security Context (by using `--features feat_smack`) # NOTE: -# * Running a uutils compiled with `feat_smack` requires a SMACK enabled Kernel at run time. +# * These utilities also run with SMACK disabled; setting security contexts requires an enabled kernel. feat_smack = [ "id/smack", "ls/smack", "mkdir/smack", "mkfifo/smack", "mknod/smack", + "uutests/smack", ] ## ## feature sets diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 06a9178ccf2..d511cdce2b3 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -9828,3 +9828,28 @@ fn test_cp_source_in_unsearchable_dir() { at.set_mode("d", 0o755); assert!(!at.file_exists("dest")); } + +#[test] +#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))] +fn test_security_contexts_disabled() { + if !uutests::util::security_contexts_disabled() { + return; + } + let scene = TestScenario::new(util_name!()); + scene.fixtures.write("source", "contents"); + for (option, warning) in [ + ("-Z", ""), + ( + "--context=some_context", + "cp: warning: ignoring --context; it requires an SELinux-enabled kernel\n", + ), + ] { + scene + .ucmd() + .args(&[option, "source", "dest"]) + .succeeds() + .no_stdout() + .stderr_is(warning); + assert_eq!(scene.fixtures.read("dest"), "contents"); + } +} diff --git a/tests/by-util/test_id.rs b/tests/by-util/test_id.rs index 30e4c1ac9a0..b008ddbf309 100644 --- a/tests/by-util/test_id.rs +++ b/tests/by-util/test_id.rs @@ -581,3 +581,15 @@ fn test_id_digital_username() { new_ucmd!().arg("200").succeeds(); } + +#[test] +#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))] +fn test_security_contexts_disabled() { + if !uutests::util::security_contexts_disabled() { + return; + } + new_ucmd!() + .arg("-Z") + .fails_with_code(1) + .stderr_only("id: --context (-Z) works only on an SELinux/SMACK-enabled kernel\n"); +} diff --git a/tests/by-util/test_install.rs b/tests/by-util/test_install.rs index 00dcea0afbe..fc2865925c3 100644 --- a/tests/by-util/test_install.rs +++ b/tests/by-util/test_install.rs @@ -3428,3 +3428,28 @@ fn test_install_strip_program_sees_caller_umask() { "strip program saw umask {reported}" ); } + +#[test] +#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))] +fn test_security_contexts_disabled() { + if !uutests::util::security_contexts_disabled() { + return; + } + let scene = TestScenario::new(util_name!()); + scene.fixtures.write("source", "contents"); + for (option, warning) in [ + ("-Z", ""), + ( + "--context=some_context", + "install: warning: ignoring --context; it requires an SELinux-enabled kernel\n", + ), + ] { + scene + .ucmd() + .args(&[option, "source", "dest"]) + .succeeds() + .no_stdout() + .stderr_is(warning); + assert_eq!(scene.fixtures.read("dest"), "contents"); + } +} diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 02e6fd7c824..0a4a9ad302c 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -8892,3 +8892,18 @@ fn test_dereferenced_context_reports_error_once( .fails_with_code(if command_line { 2 } else { 1 }) .stderr_is("ls: cannot access 'dir/broken': No such file or directory\n"); } + +#[test] +#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))] +fn test_security_contexts_disabled() { + if !uutests::util::security_contexts_disabled() { + return; + } + let scene = TestScenario::new(util_name!()); + scene.fixtures.touch("file"); + scene + .ucmd() + .args(&["-Z", "file"]) + .succeeds() + .stdout_only("? file\n"); +} diff --git a/tests/by-util/test_mknod.rs b/tests/by-util/test_mknod.rs index 3692ffe8135..b9098471901 100644 --- a/tests/by-util/test_mknod.rs +++ b/tests/by-util/test_mknod.rs @@ -406,3 +406,30 @@ mod diagnostics { assert!(!stderr.contains(":1:"), "{stderr}"); } } + +#[test] +#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))] +fn test_security_contexts_disabled() { + use std::os::unix::fs::FileTypeExt; + + if !uutests::util::security_contexts_disabled() { + return; + } + let scene = TestScenario::new(util_name!()); + for (option, file, warning) in [ + ("-Z", "default", ""), + ( + "--context=some_context", + "explicit", + "mknod: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n", + ), + ] { + scene + .ucmd() + .args(&[option, file, "p"]) + .succeeds() + .no_stdout() + .stderr_is(warning); + assert!(scene.fixtures.metadata(file).file_type().is_fifo()); + } +} diff --git a/tests/by-util/test_runcon.rs b/tests/by-util/test_runcon.rs index 0814cb55a54..9e0fb8cdb0d 100644 --- a/tests/by-util/test_runcon.rs +++ b/tests/by-util/test_runcon.rs @@ -180,3 +180,15 @@ fn get_sestatus_context(output: &[u8]) -> &str { .and_then(|bytes| std::str::from_utf8(bytes).ok()) .expect("Output of sestatus is unexpected") } + +#[test] +#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))] +fn test_security_contexts_disabled() { + if !uutests::util::security_contexts_disabled() { + return; + } + new_ucmd!() + .args(&["some_context", "true"]) + .fails_with_code(1) + .stderr_only("runcon: runcon may be used only on a SELinux kernel\n"); +} diff --git a/tests/uutests/Cargo.toml b/tests/uutests/Cargo.toml index 56e2de7991a..820b11990a3 100644 --- a/tests/uutests/Cargo.toml +++ b/tests/uutests/Cargo.toml @@ -18,6 +18,10 @@ all-features = true [lib] path = "src/lib/lib.rs" +[features] +selinux = ["uucore/selinux"] +smack = ["uucore/smack"] + [dependencies] ctor = { workspace = true } libc = { workspace = true } diff --git a/tests/uutests/src/lib/util.rs b/tests/uutests/src/lib/util.rs index d73ae9c7364..9f3686cff49 100644 --- a/tests/uutests/src/lib/util.rs +++ b/tests/uutests/src/lib/util.rs @@ -178,6 +178,12 @@ pub fn is_ci() -> bool { env::var("CI").is_ok_and(|s| s.eq_ignore_ascii_case("true")) } +/// Check whether both SELinux and SMACK are disabled on the running kernel. +#[cfg(all(target_os = "linux", feature = "selinux", feature = "smack"))] +pub fn security_contexts_disabled() -> bool { + !uucore::selinux::is_selinux_enabled() && !uucore::smack::is_smack_enabled() +} + /// Check if a locale is available on the system by verifying that /// `locale charmap` returns `"UTF-8"` when `LC_ALL` is set to the given locale. #[cfg(unix)] From 502ccaf3cdf9d44e410c4cde8054363c63d8bf72 Mon Sep 17 00:00:00 2001 From: limadog9 Date: Tue, 3 Nov 2026 10:58:27 +0000 Subject: [PATCH 2/3] mkfifo: warn when an explicit security context is ignored Skip SELinux creation contexts when the kernel facility is disabled. Warn once for an explicit context when neither SELinux nor SMACK is enabled, while keeping default context requests silent. Cover empty contexts and multiple operands in the disabled-kernel tests. --- src/uu/mkfifo/locales/en-US.ftl | 3 +++ src/uu/mkfifo/locales/fr-FR.ftl | 3 +++ src/uu/mkfifo/src/mkfifo.rs | 20 ++++++++++++++++---- tests/by-util/test_mkfifo.rs | 28 ++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/uu/mkfifo/locales/en-US.ftl b/src/uu/mkfifo/locales/en-US.ftl index f66a6b0459a..63d0a170d10 100644 --- a/src/uu/mkfifo/locales/en-US.ftl +++ b/src/uu/mkfifo/locales/en-US.ftl @@ -11,3 +11,6 @@ mkfifo-error-invalid-mode = invalid mode: { $error } mkfifo-error-cannot-create-fifo = cannot create fifo { $path }: { $error } mkfifo-error-cannot-set-permissions = cannot set permissions on { $path }: { $error } mkfifo-error-non-file-permission = mode must specify only file permission bits + +# Warning messages +mkfifo-warning-context-ignored = ignoring --context; it requires an SELinux/SMACK-enabled kernel diff --git a/src/uu/mkfifo/locales/fr-FR.ftl b/src/uu/mkfifo/locales/fr-FR.ftl index 038d8284927..fce24de7025 100644 --- a/src/uu/mkfifo/locales/fr-FR.ftl +++ b/src/uu/mkfifo/locales/fr-FR.ftl @@ -11,3 +11,6 @@ mkfifo-error-invalid-mode = mode invalide : { $error } mkfifo-error-cannot-create-fifo = impossible de créer le fifo { $path } : { $error } mkfifo-error-cannot-set-permissions = impossible de définir les permissions sur { $path } : { $error } mkfifo-error-non-file-permission = le mode ne doit spécifier que des bits de permission de fichier + +# Messages d'avertissement +mkfifo-warning-context-ignored = --context ignoré ; nécessite un noyau avec SELinux/SMACK activé diff --git a/src/uu/mkfifo/src/mkfifo.rs b/src/uu/mkfifo/src/mkfifo.rs index 12edae0a22b..378c947c28f 100644 --- a/src/uu/mkfifo/src/mkfifo.rs +++ b/src/uu/mkfifo/src/mkfifo.rs @@ -10,7 +10,7 @@ use uucore::display::Quotable; use uucore::error::{ExitCode, UResult, USimpleError, strip_errno}; use uucore::translate; -use uucore::{format_usage, show}; +use uucore::{format_usage, show, show_warning}; mod options { pub static MODE: &str = "mode"; @@ -54,13 +54,26 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .cloned() .collect(); + let context = matches.get_one::(options::CONTEXT); + #[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))] + let selinux_enabled = uucore::selinux::is_selinux_enabled(); + #[cfg(not(all(feature = "selinux", any(target_os = "android", target_os = "linux"))))] + let selinux_enabled = false; + #[cfg(all(feature = "smack", target_os = "linux"))] + let smack_enabled = uucore::smack::is_smack_enabled(); + #[cfg(not(all(feature = "smack", target_os = "linux")))] + let smack_enabled = false; + + if context.is_some() && !selinux_enabled && !smack_enabled { + show_warning!("{}", translate!("mkfifo-warning-context-ignored")); + } + for f in fifos { // Label the FIFO at creation, as GNU does; relabelling after leaves a window. #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] let _selinux_guard = { let set_security_context = matches.get_flag(options::SECURITY_CONTEXT); - let context = matches.get_one::(options::CONTEXT); - if set_security_context || context.is_some() { + if (set_security_context || context.is_some()) && selinux_enabled { let mode = uucore::libc::S_IFIFO | mode as uucore::libc::mode_t; match uucore::selinux::FsCreateContext::new( std::path::Path::new(&f), @@ -97,7 +110,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { #[cfg(all(feature = "smack", target_os = "linux"))] { let set_security_context = matches.get_flag(options::SECURITY_CONTEXT); - let context = matches.get_one::(options::CONTEXT); if set_security_context || context.is_some() { uucore::smack::set_smack_label_and_cleanup(&f, context, |p| { std::fs::remove_file(p) diff --git a/tests/by-util/test_mkfifo.rs b/tests/by-util/test_mkfifo.rs index 13d4247a98f..36bb09de35a 100644 --- a/tests/by-util/test_mkfifo.rs +++ b/tests/by-util/test_mkfifo.rs @@ -26,6 +26,34 @@ fn test_create_one_fifo() { new_ucmd!().arg("abc").succeeds(); } +#[rstest::rstest] +#[case("-Z", "")] +#[case("--context", "")] +#[case( + "--context=some_context", + "mkfifo: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n" +)] +#[case( + "--context=", + "mkfifo: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n" +)] +#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))] +fn test_security_contexts_disabled(#[case] option: &str, #[case] warning: &str) { + if !uutests::util::security_contexts_disabled() { + return; + } + let scene = TestScenario::new(util_name!()); + scene + .ucmd() + .args(&[option, "--", "one", "two"]) + .succeeds() + .no_stdout() + .stderr_is(warning); + for name in ["one", "two"] { + assert!(scene.fixtures.is_fifo(name)); + } +} + #[test] fn test_create_one_fifo_with_invalid_mode() { new_ucmd!() From ea17c3b2d59133efc99ed16720b76a84fd7de953 Mon Sep 17 00:00:00 2001 From: limadog9 Date: Tue, 3 Nov 2026 10:58:27 +0000 Subject: [PATCH 3/3] mkdir: warn when an explicit security context is ignored Match GNU by warning once for an explicit context when neither SELinux nor SMACK is enabled. Keep -Z silent and verify that both operands are created, including when the explicit context is empty. --- src/uu/mkdir/locales/en-US.ftl | 3 +++ src/uu/mkdir/locales/fr-FR.ftl | 3 +++ src/uu/mkdir/src/mkdir.rs | 15 ++++++++++++++- tests/by-util/test_mkdir.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/uu/mkdir/locales/en-US.ftl b/src/uu/mkdir/locales/en-US.ftl index 70684f0586c..fd8dc2696d6 100644 --- a/src/uu/mkdir/locales/en-US.ftl +++ b/src/uu/mkdir/locales/en-US.ftl @@ -16,5 +16,8 @@ mkdir-error-failed-to-create-tree = failed to create whole tree mkdir-error-cannot-set-permissions = cannot set permissions { $path } mkdir-error-cannot-create-directory = cannot create directory { $path } +# Warning messages +mkdir-warning-context-ignored = ignoring --context; it requires an SELinux/SMACK-enabled kernel + # Verbose output mkdir-verbose-created-directory = { $util_name }: created directory { $path } diff --git a/src/uu/mkdir/locales/fr-FR.ftl b/src/uu/mkdir/locales/fr-FR.ftl index 55c3fc1fe4b..8d3b2230549 100644 --- a/src/uu/mkdir/locales/fr-FR.ftl +++ b/src/uu/mkdir/locales/fr-FR.ftl @@ -16,5 +16,8 @@ mkdir-error-failed-to-create-tree = échec de la création de l'arborescence com mkdir-error-cannot-set-permissions = impossible de définir les permissions { $path } mkdir-error-cannot-create-directory = impossible de créer le répertoire { $path } +# Messages d'avertissement +mkdir-warning-context-ignored = --context ignoré ; nécessite un noyau avec SELinux/SMACK activé + # Sortie détaillée mkdir-verbose-created-directory = { $util_name } : répertoire créé { $path } diff --git a/src/uu/mkdir/src/mkdir.rs b/src/uu/mkdir/src/mkdir.rs index 09989d0a77e..f0e9dfaf0a3 100644 --- a/src/uu/mkdir/src/mkdir.rs +++ b/src/uu/mkdir/src/mkdir.rs @@ -18,7 +18,7 @@ use uucore::error::{FromIo, UResult, USimpleError}; use uucore::mode; use uucore::quoting_style::locale_aware_c_single_escape; use uucore::{display::Quotable, fs::dir_strip_dot_for_creation}; -use uucore::{format_usage, show_if_err}; +use uucore::{format_usage, show_if_err, show_warning}; use uucore::{translate, translate_text}; static DEFAULT_PERM: u32 = 0o777; @@ -105,6 +105,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let set_security_context = matches.get_flag(options::SECURITY_CONTEXT); let context = matches.get_one::(options::CONTEXT); + #[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))] + let selinux_enabled = uucore::selinux::is_selinux_enabled(); + #[cfg(not(all(feature = "selinux", any(target_os = "android", target_os = "linux"))))] + let selinux_enabled = false; + #[cfg(all(feature = "smack", target_os = "linux"))] + let smack_enabled = uucore::smack::is_smack_enabled(); + #[cfg(not(all(feature = "smack", target_os = "linux")))] + let smack_enabled = false; + + if context.is_some() && !selinux_enabled && !smack_enabled { + show_warning!("{}", translate!("mkdir-warning-context-ignored")); + } + let config = Config { recursive, mode, diff --git a/tests/by-util/test_mkdir.rs b/tests/by-util/test_mkdir.rs index 491b34857c7..075ae5f0edb 100644 --- a/tests/by-util/test_mkdir.rs +++ b/tests/by-util/test_mkdir.rs @@ -73,6 +73,33 @@ fn test_mkdir_mkdir() { new_ucmd!().arg("test_dir").succeeds(); } +#[rstest::rstest] +#[case("-Z", "")] +#[case( + "--context=some_context", + "mkdir: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n" +)] +#[case( + "--context=", + "mkdir: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n" +)] +#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))] +fn test_security_contexts_disabled(#[case] option: &str, #[case] warning: &str) { + if !uutests::util::security_contexts_disabled() { + return; + } + let scene = TestScenario::new(util_name!()); + scene + .ucmd() + .args(&[option, "--", "one", "two"]) + .succeeds() + .no_stdout() + .stderr_is(warning); + for name in ["one", "two"] { + assert!(scene.fixtures.dir_exists(name)); + } +} + #[cfg(feature = "test_risky_names")] #[test] fn test_mkdir_non_unicode() {