Skip to content

Commit 89f4192

Browse files
committed
ci: cover disabled SELinux and SMACK paths
Build and lint both security-context features in the existing SELinux job, then restart its Fedora VM with SELinux disabled and run binary integration tests for the disabled paths. Share the kernel checks in uutests and cover mkdir and mkfifo alongside the other utilities. Skip mkfifo's SELinux creation context when SELinux is disabled. Correct the Cargo feature comments. Closes #13332.
1 parent f286c98 commit 89f4192

13 files changed

Lines changed: 186 additions & 9 deletions

File tree

‎.github/workflows/CICD.yml‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ name: CICD
44
# spell-checker:ignore (env/flags) Awarnings Ccodegen Coverflow Cpanic Dwarnings RUSTDOCFLAGS RUSTFLAGS Zpanic CARGOFLAGS CLEVEL nodocs
55
# spell-checker:ignore (jargon) SHAs deps dequote softprops subshell toolchain fuzzers dedupe devel profdata profraw Cprofile
66
# spell-checker:ignore (people) Peltoche rivy Anson dawidd
7-
# 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 sccache setfacl utmpdump xargs zstd
8-
# 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 codspeed wasip libexecinfo
7+
# 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 sccache setfacl utmpdump xargs zstd
8+
# 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
99

1010
env:
1111
PROJECT_NAME: coreutils
@@ -975,17 +975,27 @@ jobs:
975975
- run: rsync -v -a -e ssh . lima-default:~/work/
976976
- name: Setup Rust and other build deps in VM
977977
run: |
978-
lima sudo dnf install --nodocs cargo clippy gcc g++ git rust libselinux-devel clang-devel attr -y
978+
lima sudo dnf install --nodocs cargo clippy gcc g++ git grubby rust libselinux-devel clang-devel attr -y
979979
- name: Verify SELinux Status
980980
run: |
981981
lima getenforce
982982
lima ls -laZ /etc/selinux
983983
- name: Build and Test with SELinux
984984
run: |
985985
lima ls
986-
lima bash -c "cd work && cargo test --features 'feat_selinux' --no-default-features"
987-
- name: Lint with SELinux
988-
run: lima bash -c "cd work && cargo clippy --all-targets --features 'feat_selinux' --no-default-features -- -D warnings"
986+
lima bash -c "cd work && cargo test --features 'feat_selinux,feat_smack' --no-default-features"
987+
- name: Lint with SELinux and SMACK support
988+
run: lima bash -c "cd work && cargo clippy --all-targets --features 'feat_selinux,feat_smack' --no-default-features -- -D warnings"
989+
- name: Restart Fedora VM with SELinux disabled
990+
run: |
991+
lima sudo grubby --update-kernel=ALL --args=selinux=0
992+
limactl stop default
993+
limactl start default
994+
- name: Verify SELinux and SMACK are disabled
995+
run: |
996+
lima bash -c 'test "$(getenforce)" = Disabled && test ! -e /sys/fs/smackfs'
997+
- name: Test disabled security context handling
998+
run: lima bash -c "cd work && cargo test --features 'feat_selinux,feat_smack' --no-default-features --test tests security_contexts_disabled"
989999

9901000
test_safe_traversal:
9911001
name: Safe Traversal Security Check

‎Cargo.toml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ feat_systemd_logind = [
465465
# "feat_selinux" == enable support for SELinux Security Context (by using `--features feat_selinux`)
466466
# NOTE:
467467
# * The selinux(-sys) crate requires `libselinux` headers and shared library to be accessible in the C toolchain at compile time.
468-
# * Running a uutils compiled with `feat_selinux` requires an SELinux enabled Kernel at run time.
468+
# * These utilities also run with SELinux disabled; setting security contexts requires an enabled kernel.
469469
feat_selinux = [
470470
"cp/selinux",
471471
"feat_require_selinux",
@@ -477,16 +477,18 @@ feat_selinux = [
477477
"mknod/selinux",
478478
"selinux",
479479
"stat/selinux",
480+
"uutests/selinux",
480481
]
481482
# "feat_smack" == enable support for SMACK Security Context (by using `--features feat_smack`)
482483
# NOTE:
483-
# * Running a uutils compiled with `feat_smack` requires a SMACK enabled Kernel at run time.
484+
# * These utilities also run with SMACK disabled; setting security contexts requires an enabled kernel.
484485
feat_smack = [
485486
"id/smack",
486487
"ls/smack",
487488
"mkdir/smack",
488489
"mkfifo/smack",
489490
"mknod/smack",
491+
"uutests/smack",
490492
]
491493
##
492494
## feature sets

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6161
let _selinux_guard = {
6262
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
6363
let context = matches.get_one::<String>(options::CONTEXT);
64-
if set_security_context || context.is_some() {
64+
if (set_security_context || context.is_some()) && uucore::selinux::is_selinux_enabled()
65+
{
6566
let mode = uucore::libc::S_IFIFO | mode as uucore::libc::mode_t;
6667
match uucore::selinux::FsCreateContext::new(
6768
std::path::Path::new(&f),

‎tests/by-util/test_cp.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10298,3 +10298,28 @@ fn test_progressbar_inexistent_source() {
1029810298
.fails_with_code(1)
1029910299
.stderr_contains("cp: cannot stat 'inexistent1': No such file or directory");
1030010300
}
10301+
10302+
#[test]
10303+
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
10304+
fn test_security_contexts_disabled() {
10305+
if !uutests::util::security_contexts_disabled() {
10306+
return;
10307+
}
10308+
let scene = TestScenario::new(util_name!());
10309+
scene.fixtures.write("source", "contents");
10310+
for (option, warning) in [
10311+
("-Z", ""),
10312+
(
10313+
"--context=some_context",
10314+
"cp: warning: ignoring --context; it requires an SELinux-enabled kernel\n",
10315+
),
10316+
] {
10317+
scene
10318+
.ucmd()
10319+
.args(&[option, "source", "dest"])
10320+
.succeeds()
10321+
.no_stdout()
10322+
.stderr_is(warning);
10323+
assert_eq!(scene.fixtures.read("dest"), "contents");
10324+
}
10325+
}

‎tests/by-util/test_id.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,15 @@ fn test_id_digital_username() {
581581

582582
new_ucmd!().arg("200").succeeds();
583583
}
584+
585+
#[test]
586+
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
587+
fn test_security_contexts_disabled() {
588+
if !uutests::util::security_contexts_disabled() {
589+
return;
590+
}
591+
new_ucmd!()
592+
.arg("-Z")
593+
.fails_with_code(1)
594+
.stderr_only("id: --context (-Z) works only on an SELinux/SMACK-enabled kernel\n");
595+
}

‎tests/by-util/test_install.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3382,3 +3382,28 @@ fn test_install_will_not_overwrite_just_created() {
33823382

33833383
assert_eq!(at.read("c/f"), "a");
33843384
}
3385+
3386+
#[test]
3387+
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
3388+
fn test_security_contexts_disabled() {
3389+
if !uutests::util::security_contexts_disabled() {
3390+
return;
3391+
}
3392+
let scene = TestScenario::new(util_name!());
3393+
scene.fixtures.write("source", "contents");
3394+
for (option, warning) in [
3395+
("-Z", ""),
3396+
(
3397+
"--context=some_context",
3398+
"install: warning: ignoring --context; it requires an SELinux-enabled kernel\n",
3399+
),
3400+
] {
3401+
scene
3402+
.ucmd()
3403+
.args(&[option, "source", "dest"])
3404+
.succeeds()
3405+
.no_stdout()
3406+
.stderr_is(warning);
3407+
assert_eq!(scene.fixtures.read("dest"), "contents");
3408+
}
3409+
}

‎tests/by-util/test_ls.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8507,3 +8507,18 @@ fn test_ls_dereference_sibling_alias_not_already_listed() {
85078507
.stdout_contains("link:\nfile")
85088508
.no_stderr();
85098509
}
8510+
8511+
#[test]
8512+
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
8513+
fn test_security_contexts_disabled() {
8514+
if !uutests::util::security_contexts_disabled() {
8515+
return;
8516+
}
8517+
let scene = TestScenario::new(util_name!());
8518+
scene.fixtures.touch("file");
8519+
scene
8520+
.ucmd()
8521+
.args(&["-Z", "file"])
8522+
.succeeds()
8523+
.stdout_only("? file\n");
8524+
}

‎tests/by-util/test_mkdir.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ fn test_mkdir_mkdir() {
7676
new_ucmd!().arg("test_dir").succeeds();
7777
}
7878

79+
#[test]
80+
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
81+
fn test_security_contexts_disabled() {
82+
if !uutests::util::security_contexts_disabled() {
83+
return;
84+
}
85+
let scene = TestScenario::new(util_name!());
86+
for (option, dir) in [("-Z", "default"), ("--context=some_context", "explicit")] {
87+
scene
88+
.ucmd()
89+
.args(&[option, "--", dir])
90+
.succeeds()
91+
.no_output();
92+
assert!(scene.fixtures.dir_exists(dir));
93+
}
94+
}
95+
7996
#[cfg(feature = "test_risky_names")]
8097
#[test]
8198
fn test_mkdir_non_unicode() {

‎tests/by-util/test_mkfifo.rs‎

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

29+
#[test]
30+
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
31+
fn test_security_contexts_disabled() {
32+
if !uutests::util::security_contexts_disabled() {
33+
return;
34+
}
35+
let scene = TestScenario::new(util_name!());
36+
for (option, fifo) in [
37+
("-Z", "default"),
38+
("--context", "default_long"),
39+
("--context=some_context", "explicit"),
40+
] {
41+
scene
42+
.ucmd()
43+
.args(&[option, "--", fifo])
44+
.succeeds()
45+
.no_output();
46+
assert!(scene.fixtures.is_fifo(fifo));
47+
}
48+
}
49+
2950
#[test]
3051
fn test_create_one_fifo_with_invalid_mode() {
3152
new_ucmd!()

‎tests/by-util/test_mknod.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,30 @@ mod diagnostics {
335335
assert!(!stderr.contains(":1:"), "{stderr}");
336336
}
337337
}
338+
339+
#[test]
340+
#[cfg(all(target_os = "linux", feature = "feat_selinux", feature = "feat_smack"))]
341+
fn test_security_contexts_disabled() {
342+
use std::os::unix::fs::FileTypeExt;
343+
344+
if !uutests::util::security_contexts_disabled() {
345+
return;
346+
}
347+
let scene = TestScenario::new(util_name!());
348+
for (option, file, warning) in [
349+
("-Z", "default", ""),
350+
(
351+
"--context=some_context",
352+
"explicit",
353+
"mknod: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n",
354+
),
355+
] {
356+
scene
357+
.ucmd()
358+
.args(&[option, file, "p"])
359+
.succeeds()
360+
.no_stdout()
361+
.stderr_is(warning);
362+
assert!(scene.fixtures.metadata(file).file_type().is_fifo());
363+
}
364+
}

0 commit comments

Comments
 (0)