Skip to content

Commit 40325ae

Browse files
committed
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.
1 parent 4d458fd commit 40325ae

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎src/uu/mkdir/locales/en-US.ftl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ mkdir-error-failed-to-create-tree = failed to create whole tree
1616
mkdir-error-cannot-set-permissions = cannot set permissions { $path }
1717
mkdir-error-cannot-create-directory = cannot create directory { $path }
1818
19+
# Warning messages
20+
mkdir-warning-context-ignored = ignoring --context; it requires an SELinux/SMACK-enabled kernel
21+
1922
# Verbose output
2023
mkdir-verbose-created-directory = { $util_name }: created directory { $path }

‎src/uu/mkdir/locales/fr-FR.ftl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ mkdir-error-failed-to-create-tree = échec de la création de l'arborescence com
1616
mkdir-error-cannot-set-permissions = impossible de définir les permissions { $path }
1717
mkdir-error-cannot-create-directory = impossible de créer le répertoire { $path }
1818
19+
# Messages d'avertissement
20+
mkdir-warning-context-ignored = --context ignoré ; nécessite un noyau avec SELinux/SMACK activé
21+
1922
# Sortie détaillée
2023
mkdir-verbose-created-directory = { $util_name } : répertoire créé { $path }

‎src/uu/mkdir/src/mkdir.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use uucore::error::{FromIo, UResult, USimpleError};
1818
use uucore::mode;
1919
use uucore::quoting_style::locale_aware_c_single_escape;
2020
use uucore::{display::Quotable, fs::dir_strip_dot_for_creation};
21-
use uucore::{format_usage, show_if_err};
21+
use uucore::{format_usage, show_if_err, show_warning};
2222
use uucore::{translate, translate_text};
2323

2424
static DEFAULT_PERM: u32 = 0o777;
@@ -105,6 +105,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
105105
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
106106
let context = matches.get_one::<String>(options::CONTEXT);
107107

108+
#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))]
109+
let selinux_enabled = uucore::selinux::is_selinux_enabled();
110+
#[cfg(not(all(feature = "selinux", any(target_os = "android", target_os = "linux"))))]
111+
let selinux_enabled = false;
112+
#[cfg(all(feature = "smack", target_os = "linux"))]
113+
let smack_enabled = uucore::smack::is_smack_enabled();
114+
#[cfg(not(all(feature = "smack", target_os = "linux")))]
115+
let smack_enabled = false;
116+
117+
if context.is_some() && !selinux_enabled && !smack_enabled {
118+
show_warning!("{}", translate!("mkdir-warning-context-ignored"));
119+
}
120+
108121
let config = Config {
109122
recursive,
110123
mode,

‎tests/by-util/test_mkdir.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,33 @@ fn test_mkdir_mkdir() {
7373
new_ucmd!().arg("test_dir").succeeds();
7474
}
7575

76+
#[rstest::rstest]
77+
#[case("-Z", "")]
78+
#[case(
79+
"--context=some_context",
80+
"mkdir: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n"
81+
)]
82+
#[case(
83+
"--context=",
84+
"mkdir: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n"
85+
)]
86+
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
87+
fn test_security_contexts_disabled(#[case] option: &str, #[case] warning: &str) {
88+
if !uutests::util::security_contexts_disabled() {
89+
return;
90+
}
91+
let scene = TestScenario::new(util_name!());
92+
scene
93+
.ucmd()
94+
.args(&[option, "--", "one", "two"])
95+
.succeeds()
96+
.no_stdout()
97+
.stderr_is(warning);
98+
for name in ["one", "two"] {
99+
assert!(scene.fixtures.dir_exists(name));
100+
}
101+
}
102+
76103
#[cfg(feature = "test_risky_names")]
77104
#[test]
78105
fn test_mkdir_non_unicode() {

0 commit comments

Comments
 (0)