Skip to content

Commit c5fba4b

Browse files
authored
Merge pull request #7831 from sylvestre/selinux-mkfifo
mkfifo: implement selinux support
2 parents 18db15e + 4a94a4e commit c5fba4b

File tree

8 files changed

+100
-13
lines changed

8 files changed

+100
-13
lines changed

.vscode/cspell.dictionaries/jargon.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ flamegraph
4747
fsxattr
4848
fullblock
4949
getfacl
50+
getfattr
5051
getopt
5152
gibi
5253
gibibytes

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ feat_selinux = [
5151
"id/selinux",
5252
"ls/selinux",
5353
"mkdir/selinux",
54+
"mkfifo/selinux",
5455
"mknod/selinux",
5556
"stat/selinux",
5657
"selinux",

src/uu/mkfifo/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ clap = { workspace = true }
2222
libc = { workspace = true }
2323
uucore = { workspace = true, features = ["fs", "mode"] }
2424

25+
[features]
26+
selinux = ["uucore/selinux"]
27+
2528
[[bin]]
2629
name = "mkfifo"
2730
path = "src/main.rs"

src/uu/mkfifo/src/mkfifo.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use clap::{Arg, ArgAction, Command};
6+
use clap::{Arg, ArgAction, Command, value_parser};
77
use libc::mkfifo;
88
use std::ffi::CString;
99
use std::fs;
@@ -17,7 +17,7 @@ static ABOUT: &str = help_about!("mkfifo.md");
1717

1818
mod options {
1919
pub static MODE: &str = "mode";
20-
pub static SE_LINUX_SECURITY_CONTEXT: &str = "Z";
20+
pub static SELINUX: &str = "Z";
2121
pub static CONTEXT: &str = "context";
2222
pub static FIFO: &str = "fifo";
2323
}
@@ -26,13 +26,6 @@ mod options {
2626
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2727
let matches = uu_app().try_get_matches_from(args)?;
2828

29-
if matches.contains_id(options::CONTEXT) {
30-
return Err(USimpleError::new(1, "--context is not implemented"));
31-
}
32-
if matches.get_flag(options::SE_LINUX_SECURITY_CONTEXT) {
33-
return Err(USimpleError::new(1, "-Z is not implemented"));
34-
}
35-
3629
let mode = match matches.get_one::<String>(options::MODE) {
3730
// if mode is passed, ignore umask
3831
Some(m) => match usize::from_str_radix(m, 8) {
@@ -67,6 +60,27 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6760
format!("cannot set permissions on {}: {e}", f.quote()),
6861
));
6962
}
63+
64+
// Apply SELinux context if requested
65+
#[cfg(feature = "selinux")]
66+
{
67+
// Extract the SELinux related flags and options
68+
let set_selinux_context = matches.get_flag(options::SELINUX);
69+
let context = matches.get_one::<String>(options::CONTEXT);
70+
71+
if set_selinux_context || context.is_some() {
72+
use std::path::Path;
73+
if let Err(e) =
74+
uucore::selinux::set_selinux_security_context(Path::new(&f), context)
75+
{
76+
let _ = fs::remove_file(f);
77+
return Err(USimpleError::new(
78+
1,
79+
format!("failed to set SELinux security context: {e}"),
80+
));
81+
}
82+
}
83+
}
7084
}
7185

7286
Ok(())
@@ -86,7 +100,7 @@ pub fn uu_app() -> Command {
86100
.value_name("MODE"),
87101
)
88102
.arg(
89-
Arg::new(options::SE_LINUX_SECURITY_CONTEXT)
103+
Arg::new(options::SELINUX)
90104
.short('Z')
91105
.help("set the SELinux security context to default type")
92106
.action(ArgAction::SetTrue),
@@ -95,6 +109,9 @@ pub fn uu_app() -> Command {
95109
Arg::new(options::CONTEXT)
96110
.long(options::CONTEXT)
97111
.value_name("CTX")
112+
.value_parser(value_parser!(String))
113+
.num_args(0..=1)
114+
.require_equals(true)
98115
.help(
99116
"like -Z, or if CTX is specified then set the SELinux \
100117
or SMACK security context to CTX",

tests/by-util/test_cp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55
// spell-checker:ignore (flags) reflink (fs) tmpfs (linux) rlimit Rlim NOFILE clob btrfs neve ROOTDIR USERDIR procfs outfile uufs xattrs
6-
// spell-checker:ignore bdfl hlsl IRWXO IRWXG getfattr
6+
// spell-checker:ignore bdfl hlsl IRWXO IRWXG
77
use uutests::at_and_ucmd;
88
use uutests::new_ucmd;
99
use uutests::path_concat;

tests/by-util/test_mkdir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore bindgen getfattr testtest
6+
// spell-checker:ignore bindgen testtest
77

88
#![allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
99

tests/by-util/test_mkfifo.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
//
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
5+
6+
// spell-checker:ignore nconfined
7+
58
use uutests::new_ucmd;
69
use uutests::util::TestScenario;
710
use uutests::util_name;
@@ -101,3 +104,65 @@ fn test_create_fifo_with_umask() {
101104
test_fifo_creation(0o022, "prw-r--r--"); // spell-checker:disable-line
102105
test_fifo_creation(0o777, "p---------"); // spell-checker:disable-line
103106
}
107+
108+
#[test]
109+
#[cfg(feature = "feat_selinux")]
110+
fn test_mkfifo_selinux() {
111+
use std::process::Command;
112+
let ts = TestScenario::new(util_name!());
113+
let at = &ts.fixtures;
114+
let dest = "test_file";
115+
let args = [
116+
"-Z",
117+
"--context",
118+
"--context=unconfined_u:object_r:user_tmp_t:s0",
119+
];
120+
for arg in args {
121+
ts.ucmd().arg(arg).arg(dest).succeeds();
122+
assert!(at.is_fifo("test_file"));
123+
124+
let getfattr_output = Command::new("getfattr")
125+
.arg(at.plus_as_string(dest))
126+
.arg("-n")
127+
.arg("security.selinux")
128+
.output()
129+
.expect("Failed to run `getfattr` on the destination file");
130+
println!("{:?}", getfattr_output);
131+
assert!(
132+
getfattr_output.status.success(),
133+
"getfattr did not run successfully: {}",
134+
String::from_utf8_lossy(&getfattr_output.stderr)
135+
);
136+
137+
let stdout = String::from_utf8_lossy(&getfattr_output.stdout);
138+
assert!(
139+
stdout.contains("unconfined_u"),
140+
"Expected 'foo' not found in getfattr output:\n{stdout}"
141+
);
142+
at.remove(&at.plus_as_string(dest));
143+
}
144+
}
145+
146+
#[test]
147+
#[cfg(feature = "feat_selinux")]
148+
fn test_mkfifo_selinux_invalid() {
149+
let scene = TestScenario::new(util_name!());
150+
let at = &scene.fixtures;
151+
let dest = "orig";
152+
153+
let args = [
154+
"--context=a",
155+
"--context=unconfined_u:object_r:user_tmp_t:s0:a",
156+
"--context=nconfined_u:object_r:user_tmp_t:s0",
157+
];
158+
for arg in args {
159+
new_ucmd!()
160+
.arg(arg)
161+
.arg(dest)
162+
.fails()
163+
.stderr_contains("Failed to");
164+
if at.file_exists(dest) {
165+
at.remove(dest);
166+
}
167+
}
168+
}

tests/by-util/test_mknod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore getfattr nconfined
6+
// spell-checker:ignore nconfined
77

88
use uutests::new_ucmd;
99
use uutests::util::TestScenario;

0 commit comments

Comments
 (0)