Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: CICD
# spell-checker:ignore (env/flags) Awarnings Ccodegen Coverflow Cpanic Dwarnings RUSTDOCFLAGS RUSTFLAGS Zpanic CARGOFLAGS CLEVEL nodocs
# spell-checker:ignore (jargon) SHAs deps dequote softprops subshell toolchain fuzzers dedupe devel profdata profraw Cprofile
# spell-checker:ignore (people) Peltoche rivy Anson dawidd
# spell-checker:ignore (shell/tools) binutils choco clippy dmake esac fakeroot fdesc fdescfs gmake grcov halium lcov libclang libcrypto libfuse libssl limactl nextest nocross pacman popd printf pushd redoxer rsync rustc rustfmt rustup shopt setfacl utmpdump xargs zstd
# spell-checker:ignore (misc) aarch alnum armhf bindir busytest coreutils defconfig DESTDIR gecos gnueabihf issuecomment maint manpages msys multisize noconfirm nofeatures nullglob onexitbegin onexitend pell runtest tempfile testsuite toybox userns uutils libsystemd codspeed wasip libexecinfo
# spell-checker:ignore (shell/tools) binutils choco clippy dmake esac fakeroot fdesc fdescfs gmake grcov grubby halium lcov libclang libcrypto libfuse libssl limactl nextest nocross pacman popd printf pushd redoxer rsync rustc rustfmt rustup shopt setfacl utmpdump xargs zstd
# spell-checker:ignore (misc) aarch alnum armhf bindir busytest coreutils defconfig DESTDIR gecos getenforce gnueabihf issuecomment maint manpages msys multisize noconfirm nofeatures nullglob onexitbegin onexitend pell runtest tempfile testsuite toybox userns uutils libsystemd smackfs codspeed wasip libexecinfo

env:
PROJECT_NAME: coreutils
Expand Down Expand Up @@ -954,15 +954,25 @@ jobs:
- run: rsync -v -a -e ssh . lima-default:~/work/
- name: Setup Rust and other build deps in VM
run: |
lima sudo dnf install --nodocs cargo gcc g++ git rust libselinux-devel clang-devel attr -y
lima sudo dnf install --nodocs cargo gcc g++ git grubby rust libselinux-devel clang-devel attr -y
- name: Verify SELinux Status
run: |
lima getenforce
lima ls -laZ /etc/selinux
- name: Build and Test with SELinux
run: |
lima ls
lima bash -c "cd work && cargo test --features 'feat_selinux' --no-default-features"
lima bash -c "cd work && cargo test --features 'feat_selinux,feat_smack' --no-default-features"
- name: Restart Fedora VM with SELinux disabled
run: |
lima sudo grubby --update-kernel=ALL --args=selinux=0
limactl stop default
limactl start default
- name: Verify SELinux and SMACK are disabled
run: |
lima bash -c 'test "$(getenforce)" = Disabled && test ! -e /sys/fs/smackfs'
- name: Test disabled security context handling
run: lima bash -c "cd work && cargo test --features 'feat_selinux,feat_smack' --no-default-features --test tests security_contexts_disabled"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why no test for mkdir and mkfifo? the issue was found with mkdir's SMACK branch
please add test_security_contexts_disabled in test_mkdir.rs and test_mkfifo.rs too


test_safe_traversal:
name: Safe Traversal Security Check
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ feat_systemd_logind = [
# "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.
# * These utilities also run with SELinux disabled; setting security contexts requires an enabled kernel.
feat_selinux = [
"cp/selinux",
"feat_require_selinux",
Expand All @@ -488,16 +488,18 @@ feat_selinux = [
"mknod/selinux",
"selinux",
"stat/selinux",
"uutests/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.
# * These utilities also run with SMACK disabled; setting security contexts requires an enabled kernel.
feat_smack = [
"id/smack",
"ls/smack",
"mkdir/smack",
"mkfifo/smack",
"mknod/smack",
"uutests/smack",
]
##
## feature sets
Expand Down
3 changes: 3 additions & 0 deletions src/uu/mkdir/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ mkdir-error-failed-to-create-tree = failed to create whole tree
mkdir-error-cannot-set-permissions = cannot set permissions { $path }
mkdir-error-cannot-create-directory = cannot create directory { $path }
# Warning messages
mkdir-warning-context-ignored = ignoring --context; it requires an SELinux/SMACK-enabled kernel
# Verbose output
mkdir-verbose-created-directory = { $util_name }: created directory { $path }
3 changes: 3 additions & 0 deletions src/uu/mkdir/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ mkdir-error-failed-to-create-tree = échec de la création de l'arborescence com
mkdir-error-cannot-set-permissions = impossible de définir les permissions { $path }
mkdir-error-cannot-create-directory = impossible de créer le répertoire { $path }
# Messages d'avertissement
mkdir-warning-context-ignored = --context ignoré ; nécessite un noyau avec SELinux/SMACK activé
# Sortie détaillée
mkdir-verbose-created-directory = { $util_name } : répertoire créé { $path }
15 changes: 14 additions & 1 deletion src/uu/mkdir/src/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use uucore::error::{FromIo, UResult, USimpleError};
use uucore::mode;
use uucore::quoting_style::locale_aware_c_single_escape;
use uucore::{display::Quotable, fs::dir_strip_dot_for_creation};
use uucore::{format_usage, show_if_err};
use uucore::{format_usage, show_if_err, show_warning};
use uucore::{translate, translate_text};

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

#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))]
let selinux_enabled = uucore::selinux::is_selinux_enabled();
#[cfg(not(all(feature = "selinux", any(target_os = "android", target_os = "linux"))))]
let selinux_enabled = false;
#[cfg(all(feature = "smack", target_os = "linux"))]
let smack_enabled = uucore::smack::is_smack_enabled();
#[cfg(not(all(feature = "smack", target_os = "linux")))]
let smack_enabled = false;

if context.is_some() && !selinux_enabled && !smack_enabled {
show_warning!("{}", translate!("mkdir-warning-context-ignored"));
}

let config = Config {
recursive,
mode,
Expand Down
3 changes: 3 additions & 0 deletions src/uu/mkfifo/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ mkfifo-error-invalid-mode = invalid mode: { $error }
mkfifo-error-cannot-create-fifo = cannot create fifo { $path }: { $error }
mkfifo-error-cannot-set-permissions = cannot set permissions on { $path }: { $error }
mkfifo-error-non-file-permission = mode must specify only file permission bits

# Warning messages
mkfifo-warning-context-ignored = ignoring --context; it requires an SELinux/SMACK-enabled kernel
3 changes: 3 additions & 0 deletions src/uu/mkfifo/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ mkfifo-error-invalid-mode = mode invalide : { $error }
mkfifo-error-cannot-create-fifo = impossible de créer le fifo { $path } : { $error }
mkfifo-error-cannot-set-permissions = impossible de définir les permissions sur { $path } : { $error }
mkfifo-error-non-file-permission = le mode ne doit spécifier que des bits de permission de fichier

# Messages d'avertissement
mkfifo-warning-context-ignored = --context ignoré ; nécessite un noyau avec SELinux/SMACK activé
20 changes: 16 additions & 4 deletions src/uu/mkfifo/src/mkfifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use uucore::display::Quotable;
use uucore::error::{ExitCode, UResult, USimpleError, strip_errno};
use uucore::translate;

use uucore::{format_usage, show};
use uucore::{format_usage, show, show_warning};

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

let context = matches.get_one::<String>(options::CONTEXT);
#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))]
let selinux_enabled = uucore::selinux::is_selinux_enabled();
#[cfg(not(all(feature = "selinux", any(target_os = "android", target_os = "linux"))))]
let selinux_enabled = false;
#[cfg(all(feature = "smack", target_os = "linux"))]
let smack_enabled = uucore::smack::is_smack_enabled();
#[cfg(not(all(feature = "smack", target_os = "linux")))]
let smack_enabled = false;

if context.is_some() && !selinux_enabled && !smack_enabled {
show_warning!("{}", translate!("mkfifo-warning-context-ignored"));
}

for f in fifos {
// Label the FIFO at creation, as GNU does; relabelling after leaves a window.
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
let _selinux_guard = {
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
let context = matches.get_one::<String>(options::CONTEXT);
if set_security_context || context.is_some() {
if (set_security_context || context.is_some()) && selinux_enabled {
let mode = uucore::libc::S_IFIFO | mode as uucore::libc::mode_t;
match uucore::selinux::FsCreateContext::new(
std::path::Path::new(&f),
Expand Down Expand Up @@ -97,7 +110,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
#[cfg(all(feature = "smack", target_os = "linux"))]
{
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
let context = matches.get_one::<String>(options::CONTEXT);
if set_security_context || context.is_some() {
uucore::smack::set_smack_label_and_cleanup(&f, context, |p| {
std::fs::remove_file(p)
Expand Down
25 changes: 25 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9828,3 +9828,28 @@ fn test_cp_source_in_unsearchable_dir() {
at.set_mode("d", 0o755);
assert!(!at.file_exists("dest"));
}

#[test]
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same guard copied in 6 files, could be a small helper in uutests, no?

fn test_security_contexts_disabled() {
if !uutests::util::security_contexts_disabled() {
return;
}
let scene = TestScenario::new(util_name!());
scene.fixtures.write("source", "contents");
for (option, warning) in [
("-Z", ""),
(
"--context=some_context",
"cp: warning: ignoring --context; it requires an SELinux-enabled kernel\n",
),
] {
scene
.ucmd()
.args(&[option, "source", "dest"])
.succeeds()
.no_stdout()
.stderr_is(warning);
assert_eq!(scene.fixtures.read("dest"), "contents");
}
}
12 changes: 12 additions & 0 deletions tests/by-util/test_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,15 @@ fn test_id_digital_username() {

new_ucmd!().arg("200").succeeds();
}

#[test]
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
fn test_security_contexts_disabled() {
if !uutests::util::security_contexts_disabled() {
return;
}
new_ucmd!()
.arg("-Z")
.fails_with_code(1)
.stderr_only("id: --context (-Z) works only on an SELinux/SMACK-enabled kernel\n");
}
25 changes: 25 additions & 0 deletions tests/by-util/test_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3428,3 +3428,28 @@ fn test_install_strip_program_sees_caller_umask() {
"strip program saw umask {reported}"
);
}

#[test]
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
fn test_security_contexts_disabled() {
if !uutests::util::security_contexts_disabled() {
return;
}
let scene = TestScenario::new(util_name!());
scene.fixtures.write("source", "contents");
for (option, warning) in [
("-Z", ""),
(
"--context=some_context",
"install: warning: ignoring --context; it requires an SELinux-enabled kernel\n",
),
] {
scene
.ucmd()
.args(&[option, "source", "dest"])
.succeeds()
.no_stdout()
.stderr_is(warning);
assert_eq!(scene.fixtures.read("dest"), "contents");
}
}
15 changes: 15 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8892,3 +8892,18 @@ fn test_dereferenced_context_reports_error_once(
.fails_with_code(if command_line { 2 } else { 1 })
.stderr_is("ls: cannot access 'dir/broken': No such file or directory\n");
}

#[test]
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
fn test_security_contexts_disabled() {
if !uutests::util::security_contexts_disabled() {
return;
}
let scene = TestScenario::new(util_name!());
scene.fixtures.touch("file");
scene
.ucmd()
.args(&["-Z", "file"])
.succeeds()
.stdout_only("? file\n");
}
27 changes: 27 additions & 0 deletions tests/by-util/test_mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@ fn test_mkdir_mkdir() {
new_ucmd!().arg("test_dir").succeeds();
}

#[rstest::rstest]
#[case("-Z", "")]
#[case(
"--context=some_context",
"mkdir: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n"
)]
#[case(
"--context=",
"mkdir: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n"
)]
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
fn test_security_contexts_disabled(#[case] option: &str, #[case] warning: &str) {
if !uutests::util::security_contexts_disabled() {
return;
}
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.args(&[option, "--", "one", "two"])
.succeeds()
.no_stdout()
.stderr_is(warning);
for name in ["one", "two"] {
assert!(scene.fixtures.dir_exists(name));
}
}

#[cfg(feature = "test_risky_names")]
#[test]
fn test_mkdir_non_unicode() {
Expand Down
28 changes: 28 additions & 0 deletions tests/by-util/test_mkfifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ fn test_create_one_fifo() {
new_ucmd!().arg("abc").succeeds();
}

#[rstest::rstest]
#[case("-Z", "")]
#[case("--context", "")]
#[case(
"--context=some_context",
"mkfifo: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n"
)]
#[case(
"--context=",
"mkfifo: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n"
)]
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
fn test_security_contexts_disabled(#[case] option: &str, #[case] warning: &str) {
if !uutests::util::security_contexts_disabled() {
return;
}
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.args(&[option, "--", "one", "two"])
.succeeds()
.no_stdout()
.stderr_is(warning);
for name in ["one", "two"] {
assert!(scene.fixtures.is_fifo(name));
}
}

#[test]
fn test_create_one_fifo_with_invalid_mode() {
new_ucmd!()
Expand Down
27 changes: 27 additions & 0 deletions tests/by-util/test_mknod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,30 @@ mod diagnostics {
assert!(!stderr.contains(":1:"), "{stderr}");
}
}

#[test]
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
fn test_security_contexts_disabled() {
use std::os::unix::fs::FileTypeExt;

if !uutests::util::security_contexts_disabled() {
return;
}
let scene = TestScenario::new(util_name!());
for (option, file, warning) in [
("-Z", "default", ""),
(
"--context=some_context",
"explicit",
"mknod: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n",
),
] {
scene
.ucmd()
.args(&[option, file, "p"])
.succeeds()
.no_stdout()
.stderr_is(warning);
assert!(scene.fixtures.metadata(file).file_type().is_fifo());
}
}
12 changes: 12 additions & 0 deletions tests/by-util/test_runcon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,15 @@ fn get_sestatus_context(output: &[u8]) -> &str {
.and_then(|bytes| std::str::from_utf8(bytes).ok())
.expect("Output of sestatus is unexpected")
}

#[test]
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
fn test_security_contexts_disabled() {
if !uutests::util::security_contexts_disabled() {
return;
}
new_ucmd!()
.args(&["some_context", "true"])
.fails_with_code(1)
.stderr_only("runcon: runcon may be used only on a SELinux kernel\n");
}
4 changes: 4 additions & 0 deletions tests/uutests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ all-features = true
[lib]
path = "src/lib/lib.rs"

[features]
selinux = ["uucore/selinux"]
smack = ["uucore/smack"]

[dependencies]
ctor = { workspace = true }
libc = { workspace = true }
Expand Down
Loading