From 3783d6e8ae05f49c5af38f54a2ee0f92509ed31f Mon Sep 17 00:00:00 2001 From: Chaganti-Reddy Date: Wed, 8 Jul 2026 14:21:42 +0530 Subject: [PATCH 1/3] mkdir: warn when --context is used without SELinux/SMACK. Fixes #12985 --- src/uu/mkdir/locales/en-US.ftl | 3 +++ src/uu/mkdir/locales/fr-FR.ftl | 3 +++ src/uu/mkdir/src/mkdir.rs | 17 ++++++++++++----- tests/by-util/test_mkdir.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/uu/mkdir/locales/en-US.ftl b/src/uu/mkdir/locales/en-US.ftl index 44fd5f5adf3..4ac212ff670 100644 --- a/src/uu/mkdir/locales/en-US.ftl +++ b/src/uu/mkdir/locales/en-US.ftl @@ -15,5 +15,8 @@ mkdir-error-file-exists = { $path }: File exists mkdir-error-failed-to-create-tree = failed to create whole tree mkdir-error-cannot-set-permissions = cannot set permissions { $path } +# Warning messages +mkdir-warning-context-not-selinux = 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 d92bed96520..f8b557cc797 100644 --- a/src/uu/mkdir/locales/fr-FR.ftl +++ b/src/uu/mkdir/locales/fr-FR.ftl @@ -15,5 +15,8 @@ mkdir-error-file-exists = { $path } : Le fichier existe mkdir-error-failed-to-create-tree = échec de la création de l'arborescence complète mkdir-error-cannot-set-permissions = impossible de définir les permissions { $path } +# Messages d'avertissement +mkdir-warning-context-not-selinux = --context est ignoré ; cela 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 ff46ad59556..947d9baace7 100644 --- a/src/uu/mkdir/src/mkdir.rs +++ b/src/uu/mkdir/src/mkdir.rs @@ -16,6 +16,8 @@ use uucore::translate; #[cfg(not(windows))] use uucore::mode; +#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))] +use uucore::show_warning; use uucore::{display::Quotable, fs::dir_strip_dot_for_creation}; use uucore::{format_usage, show_if_err}; @@ -321,11 +323,16 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<( // Apply SELinux context if requested #[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))] - if config.set_security_context && uucore::selinux::is_selinux_enabled() { - if let Err(e) = uucore::selinux::set_selinux_security_context(path, config.context) - { - let _ = std::fs::remove_dir(path); - return Err(USimpleError::new(1, e.to_string())); + if config.set_security_context { + if uucore::selinux::is_selinux_enabled() { + if let Err(e) = + uucore::selinux::set_selinux_security_context(path, config.context) + { + let _ = std::fs::remove_dir(path); + return Err(USimpleError::new(1, e.to_string())); + } + } else { + show_warning!("{}", translate!("mkdir-warning-context-not-selinux")); } } diff --git a/tests/by-util/test_mkdir.rs b/tests/by-util/test_mkdir.rs index 5df3c778122..534073b0f21 100644 --- a/tests/by-util/test_mkdir.rs +++ b/tests/by-util/test_mkdir.rs @@ -564,6 +564,33 @@ fn test_selinux() { } } +#[test] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] +fn test_selinux_context_warns_when_kernel_not_enabled() { + // --context should still create the directory on a kernel without + // SELinux/SMACK, just with a warning that it had no effect (matches GNU). + // On a kernel that does have it enabled, that's test_selinux's job above. + if uucore::selinux::is_selinux_enabled() { + return; + } + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + let dest = "test_dir_context_warning"; + + new_ucmd!() + .arg("--context=unconfined_u:object_r:user_tmp_t:s0") + .arg(at.plus_as_string(dest)) + .succeeds() + .no_stdout() + .stderr_contains("ignoring --context"); + + assert!(at.dir_exists(dest)); +} + #[test] #[cfg(all( feature = "feat_selinux", From d0b0c170b0d4962a025d69120be1e26f7692b54c Mon Sep 17 00:00:00 2001 From: Chaganti-Reddy Date: Thu, 9 Jul 2026 19:34:35 +0530 Subject: [PATCH 2/3] fix stale comment: feat_selinux doesn't require an SELinux kernel to run --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8f2bede1353..8440f3fc8df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,7 +82,8 @@ feat_acl = ["cp/feat_acl"] # "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. +# * A uutils compiled with `feat_selinux` runs fine on a kernel without SELinux enabled; SELinux-specific +# behavior is just skipped at runtime (see `uucore::selinux::is_selinux_enabled`). feat_selinux = [ "cp/selinux", "feat_require_selinux", From 03108d520adf70bfa0356343f602f66fc393760d Mon Sep 17 00:00:00 2001 From: Chaganti-Reddy Date: Fri, 10 Jul 2026 10:17:15 +0530 Subject: [PATCH 3/3] mkdir: warn instead of silently ignoring --context when SMACK isn't enabled --- Cargo.toml | 2 +- src/uu/mkdir/src/mkdir.rs | 15 +++++++++++---- tests/by-util/test_mkdir.rs | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8440f3fc8df..e76811762c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -98,7 +98,7 @@ feat_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. +# * A uutils compiled with `feat_smack` runs fine on a kernel without SMACK enabled; SMACK-specific behavior is just skipped at runtime (see `uucore::smack::is_smack_enabled`). feat_smack = [ "id/smack", "ls/smack", diff --git a/src/uu/mkdir/src/mkdir.rs b/src/uu/mkdir/src/mkdir.rs index 947d9baace7..7bd7024da7f 100644 --- a/src/uu/mkdir/src/mkdir.rs +++ b/src/uu/mkdir/src/mkdir.rs @@ -16,7 +16,10 @@ use uucore::translate; #[cfg(not(windows))] use uucore::mode; -#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))] +#[cfg(any( + all(feature = "selinux", any(target_os = "android", target_os = "linux")), + all(feature = "smack", target_os = "linux") +))] use uucore::show_warning; use uucore::{display::Quotable, fs::dir_strip_dot_for_creation}; use uucore::{format_usage, show_if_err}; @@ -339,9 +342,13 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<( // Apply SMACK context if requested #[cfg(all(feature = "smack", target_os = "linux"))] if config.set_security_context { - uucore::smack::set_smack_label_and_cleanup(path, config.context, |p| { - std::fs::remove_dir(p) - })?; + if uucore::smack::is_smack_enabled() { + uucore::smack::set_smack_label_and_cleanup(path, config.context, |p| { + std::fs::remove_dir(p) + })?; + } else { + show_warning!("{}", translate!("mkdir-warning-context-not-selinux")); + } } Ok(()) } diff --git a/tests/by-util/test_mkdir.rs b/tests/by-util/test_mkdir.rs index 534073b0f21..cbf7cd7f67a 100644 --- a/tests/by-util/test_mkdir.rs +++ b/tests/by-util/test_mkdir.rs @@ -591,6 +591,27 @@ fn test_selinux_context_warns_when_kernel_not_enabled() { assert!(at.dir_exists(dest)); } +#[test] +#[cfg(all(feature = "feat_smack", target_os = "linux"))] +fn test_smack_context_warns_when_kernel_not_enabled() { + if uucore::smack::is_smack_enabled() { + return; + } + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + let dest = "test_dir_smack_context_warning"; + + new_ucmd!() + .arg("--context=some-label") + .arg(at.plus_as_string(dest)) + .succeeds() + .no_stdout() + .stderr_contains("ignoring --context"); + + assert!(at.dir_exists(dest)); +} + #[test] #[cfg(all( feature = "feat_selinux",