Skip to content

Commit c69fb4a

Browse files
committed
mknod: reject unsupported file types
1 parent 80e7434 commit c69fb4a

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/uu/mknod/Cargo.toml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ readme.workspace = true
1414
[lib]
1515
name = "uu_mknod"
1616
path = "src/mknod.rs"
17-
test = false
1817
doctest = false
1918

2019
[dependencies]
@@ -23,6 +22,10 @@ fluent = { workspace = true }
2322
rustix = { workspace = true, features = ["fs"] }
2423
uucore = { workspace = true, features = ["fs", "mode"] }
2524

25+
[dev-dependencies]
26+
rstest = { workspace = true }
27+
tempfile = { workspace = true }
28+
2629
[features]
2730
diagnostics = ["uucore/diagnostics"]
2831
selinux = ["uucore/selinux"]

‎src/uu/mknod/src/mknod.rs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ struct Config {
5151
}
5252

5353
fn mknod(file_name: &OsStr, config: Config) -> i32 {
54+
if !matches!(
55+
config.file_type,
56+
FileType::BlockDevice | FileType::CharacterDevice | FileType::Fifo
57+
) {
58+
let _ = writeln!(
59+
io::stderr(),
60+
"{}: {}",
61+
uucore::execution_phrase(),
62+
io::Error::from(rustix::io::Errno::INVAL)
63+
);
64+
return 1;
65+
}
66+
5467
#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))]
5568
let selinux_enabled = uucore::selinux::is_selinux_enabled();
5669
#[cfg(not(all(feature = "selinux", any(target_os = "android", target_os = "linux"))))]
@@ -278,3 +291,36 @@ fn parse_type(tpe: &str) -> Result<FileType, String> {
278291
_ => Err(translate!("mknod-error-invalid-device-type", "type" => tpe.quote())),
279292
})
280293
}
294+
295+
#[cfg(test)]
296+
mod tests {
297+
use super::{Config, FileType, Mode, mknod};
298+
use rstest::rstest;
299+
use tempfile::tempdir;
300+
301+
#[rstest]
302+
#[case::regular_file(FileType::RegularFile)]
303+
#[case::directory(FileType::Directory)]
304+
#[case::symlink(FileType::Symlink)]
305+
#[case::socket(FileType::Socket)]
306+
#[case::unknown(FileType::Unknown)]
307+
fn test_mknod_rejects_unsupported_file_type(#[case] file_type: FileType) {
308+
let dir = tempdir().unwrap();
309+
let path = dir.path().join("node");
310+
let config = Config {
311+
mode: Mode::RWXU,
312+
file_type,
313+
use_umask: true,
314+
dev: 0,
315+
#[cfg(any(
316+
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
317+
all(feature = "smack", target_os = "linux"),
318+
))]
319+
set_security_context: false,
320+
context: None,
321+
};
322+
323+
assert_eq!(mknod(path.as_os_str(), config), 1);
324+
assert!(path.symlink_metadata().is_err());
325+
}
326+
}

0 commit comments

Comments
 (0)