Skip to content

Commit 4d458fd

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

4 files changed

Lines changed: 50 additions & 4 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ mkfifo-error-invalid-mode = invalid mode: { $error }
1111
mkfifo-error-cannot-create-fifo = cannot create fifo { $path }: { $error }
1212
mkfifo-error-cannot-set-permissions = cannot set permissions on { $path }: { $error }
1313
mkfifo-error-non-file-permission = mode must specify only file permission bits
14+
15+
# Warning messages
16+
mkfifo-warning-context-ignored = ignoring --context; it requires an SELinux/SMACK-enabled kernel

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ mkfifo-error-invalid-mode = mode invalide : { $error }
1111
mkfifo-error-cannot-create-fifo = impossible de créer le fifo { $path } : { $error }
1212
mkfifo-error-cannot-set-permissions = impossible de définir les permissions sur { $path } : { $error }
1313
mkfifo-error-non-file-permission = le mode ne doit spécifier que des bits de permission de fichier
14+
15+
# Messages d'avertissement
16+
mkfifo-warning-context-ignored = --context ignoré ; nécessite un noyau avec SELinux/SMACK activé

‎src/uu/mkfifo/src/mkfifo.rs‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use uucore::display::Quotable;
1010
use uucore::error::{ExitCode, UResult, USimpleError, strip_errno};
1111
use uucore::translate;
1212

13-
use uucore::{format_usage, show};
13+
use uucore::{format_usage, show, show_warning};
1414

1515
mod options {
1616
pub static MODE: &str = "mode";
@@ -54,13 +54,26 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5454
.cloned()
5555
.collect();
5656

57+
let context = matches.get_one::<String>(options::CONTEXT);
58+
#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))]
59+
let selinux_enabled = uucore::selinux::is_selinux_enabled();
60+
#[cfg(not(all(feature = "selinux", any(target_os = "android", target_os = "linux"))))]
61+
let selinux_enabled = false;
62+
#[cfg(all(feature = "smack", target_os = "linux"))]
63+
let smack_enabled = uucore::smack::is_smack_enabled();
64+
#[cfg(not(all(feature = "smack", target_os = "linux")))]
65+
let smack_enabled = false;
66+
67+
if context.is_some() && !selinux_enabled && !smack_enabled {
68+
show_warning!("{}", translate!("mkfifo-warning-context-ignored"));
69+
}
70+
5771
for f in fifos {
5872
// Label the FIFO at creation, as GNU does; relabelling after leaves a window.
5973
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
6074
let _selinux_guard = {
6175
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
62-
let context = matches.get_one::<String>(options::CONTEXT);
63-
if set_security_context || context.is_some() {
76+
if (set_security_context || context.is_some()) && selinux_enabled {
6477
let mode = uucore::libc::S_IFIFO | mode as uucore::libc::mode_t;
6578
match uucore::selinux::FsCreateContext::new(
6679
std::path::Path::new(&f),
@@ -97,7 +110,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
97110
#[cfg(all(feature = "smack", target_os = "linux"))]
98111
{
99112
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
100-
let context = matches.get_one::<String>(options::CONTEXT);
101113
if set_security_context || context.is_some() {
102114
uucore::smack::set_smack_label_and_cleanup(&f, context, |p| {
103115
std::fs::remove_file(p)

‎tests/by-util/test_mkfifo.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,34 @@ fn test_create_one_fifo() {
2626
new_ucmd!().arg("abc").succeeds();
2727
}
2828

29+
#[rstest::rstest]
30+
#[case("-Z", "")]
31+
#[case("--context", "")]
32+
#[case(
33+
"--context=some_context",
34+
"mkfifo: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n"
35+
)]
36+
#[case(
37+
"--context=",
38+
"mkfifo: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n"
39+
)]
40+
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
41+
fn test_security_contexts_disabled(#[case] option: &str, #[case] warning: &str) {
42+
if !uutests::util::security_contexts_disabled() {
43+
return;
44+
}
45+
let scene = TestScenario::new(util_name!());
46+
scene
47+
.ucmd()
48+
.args(&[option, "--", "one", "two"])
49+
.succeeds()
50+
.no_stdout()
51+
.stderr_is(warning);
52+
for name in ["one", "two"] {
53+
assert!(scene.fixtures.is_fifo(name));
54+
}
55+
}
56+
2957
#[test]
3058
fn test_create_one_fifo_with_invalid_mode() {
3159
new_ucmd!()

0 commit comments

Comments
 (0)