Skip to content

Commit 24e3d0d

Browse files
xtqqczzecakebaker
authored andcommitted
fix(selinux): add missing os checks
1 parent a20214d commit 24e3d0d

4 files changed

Lines changed: 33 additions & 18 deletions

File tree

src/uu/cp/src/copydir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use uucore::translate;
2626
use uucore::uio_error;
2727
use walkdir::{DirEntry, WalkDir};
2828

29-
#[cfg(all(feature = "selinux", target_os = "linux"))]
29+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
3030
use crate::set_selinux_context;
3131
use crate::{
3232
CopyMode, CopyResult, CpError, Options, aligned_ancestors, context_for, copy_attributes,
@@ -563,7 +563,7 @@ pub(crate) fn copy_directory(
563563
options.set_selinux_context,
564564
)?;
565565

566-
#[cfg(all(feature = "selinux", target_os = "linux"))]
566+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
567567
if options.set_selinux_context {
568568
set_selinux_context(&dir.dest, options.context.as_ref())?;
569569
}
@@ -583,7 +583,7 @@ pub(crate) fn copy_directory(
583583
options.set_selinux_context,
584584
)?;
585585

586-
#[cfg(all(feature = "selinux", target_os = "linux"))]
586+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
587587
if options.set_selinux_context {
588588
set_selinux_context(y, options.context.as_ref())?;
589589
}

src/uu/cp/src/cp.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,11 @@ impl Attributes {
896896
mode: Preserve::Yes { required: true },
897897
timestamps: Preserve::Yes { required: true },
898898
context: {
899-
#[cfg(feature = "feat_selinux")]
899+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
900900
{
901901
Preserve::Yes { required: false }
902902
}
903-
#[cfg(not(feature = "feat_selinux"))]
903+
#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))]
904904
{
905905
Preserve::No { explicit: false }
906906
}
@@ -1141,7 +1141,7 @@ impl Options {
11411141
}
11421142
}
11431143

1144-
#[cfg(not(feature = "selinux"))]
1144+
#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))]
11451145
if let Preserve::Yes { required } = attributes.context {
11461146
let selinux_disabled_error = CpError::Error(translate!("cp-error-selinux-not-enabled"));
11471147
if required {
@@ -1692,7 +1692,7 @@ fn handle_preserve<F: Fn() -> CopyResult<()>>(p: Preserve, f: F) -> CopyResult<(
16921692
Ok(())
16931693
}
16941694

1695-
#[cfg(all(feature = "selinux", target_os = "linux"))]
1695+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
16961696
pub(crate) fn set_selinux_context(path: &Path, context: Option<&String>) -> CopyResult<()> {
16971697
if !uucore::selinux::is_selinux_enabled() {
16981698
return Ok(());
@@ -2670,7 +2670,7 @@ fn copy_file(
26702670
fs::File::create(dest).map(|f| f.set_len(0)).ok();
26712671
})?;
26722672

2673-
#[cfg(all(feature = "selinux", target_os = "linux"))]
2673+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
26742674
if options.set_selinux_context {
26752675
set_selinux_context(dest, options.context.as_ref())?;
26762676
}

src/uu/install/src/install.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ fn directory(paths: &[OsString], b: &Behavior) -> UResult<()> {
489489
}
490490

491491
// Set SELinux context for all created directories if needed
492-
#[cfg(all(feature = "selinux", target_os = "linux"))]
492+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
493493
if should_set_selinux_context(b) {
494494
let context = get_context_for_selinux(b);
495495
set_selinux_context_for_directories_install(path_to_create.as_path(), context);
@@ -514,7 +514,7 @@ fn directory(paths: &[OsString], b: &Behavior) -> UResult<()> {
514514
show_if_err!(chown_optional_user_group(path, b));
515515

516516
// Set SELinux context for directory if needed
517-
#[cfg(all(feature = "selinux", target_os = "linux"))]
517+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
518518
if b.default_context {
519519
show_if_err!(set_selinux_default_context(path));
520520
} else if b.context.is_some() {
@@ -683,7 +683,10 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
683683
}
684684

685685
// Set SELinux context for all created directories if needed
686-
#[cfg(all(feature = "selinux", target_os = "linux"))]
686+
#[cfg(all(
687+
feature = "selinux",
688+
any(target_os = "linux", target_os = "android")
689+
))]
687690
if should_set_selinux_context(b) {
688691
let context = get_context_for_selinux(b);
689692
set_selinux_context_for_directories_install(to_create, context);
@@ -717,7 +720,10 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
717720
}
718721

719722
// Set SELinux context for all created directories if needed
720-
#[cfg(all(feature = "selinux", target_os = "linux"))]
723+
#[cfg(all(
724+
feature = "selinux",
725+
any(target_os = "linux", target_os = "android")
726+
))]
721727
if should_set_selinux_context(b) {
722728
let context = get_context_for_selinux(b);
723729
set_selinux_context_for_directories_install(to_create, context);
@@ -1090,7 +1096,7 @@ fn finalize_installed_file(
10901096
preserve_timestamps(from, to)?;
10911097
}
10921098

1093-
#[cfg(all(feature = "selinux", target_os = "linux"))]
1099+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
10941100
if b.privileged {
10951101
if b.preserve_context {
10961102
uucore::selinux::preserve_security_context(from, to)
@@ -1154,7 +1160,7 @@ fn get_context_for_selinux(b: &Behavior) -> Option<&String> {
11541160
}
11551161
}
11561162

1157-
#[cfg(all(feature = "selinux", target_os = "linux"))]
1163+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
11581164
fn should_set_selinux_context(b: &Behavior) -> bool {
11591165
b.privileged && (b.context.is_some() || b.default_context)
11601166
}
@@ -1250,7 +1256,7 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> bool {
12501256
}
12511257

12521258
if b.privileged {
1253-
#[cfg(all(feature = "selinux", target_os = "linux"))]
1259+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
12541260
if b.preserve_context && contexts_differ(from, to) {
12551261
return true;
12561262
}

tests/by-util/test_cp.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,10 @@ fn test_cp_preserve_xattr() {
18301830
}
18311831

18321832
#[test]
1833-
#[cfg(all(target_os = "linux", not(feature = "feat_selinux")))]
1833+
#[cfg(all(
1834+
not(feature = "feat_selinux"),
1835+
any(target_os = "linux", target_os = "android")
1836+
))]
18341837
fn test_cp_preserve_all_context_fails_on_non_selinux() {
18351838
new_ucmd!()
18361839
.arg(TEST_COPY_FROM_FOLDER_FILE)
@@ -6802,7 +6805,10 @@ fn test_cp_selinux() {
68026805
}
68036806

68046807
#[test]
6805-
#[cfg(feature = "feat_selinux")]
6808+
#[cfg(all(
6809+
feature = "feat_selinux",
6810+
any(target_os = "linux", target_os = "android")
6811+
))]
68066812
fn test_cp_selinux_invalid() {
68076813
let scene = TestScenario::new(util_name!());
68086814
let at = &scene.fixtures;
@@ -7871,7 +7877,10 @@ fn test_cp_a_preserves_context() {
78717877
}
78727878

78737879
#[test]
7874-
#[cfg(feature = "feat_selinux")]
7880+
#[cfg(all(
7881+
feature = "feat_selinux",
7882+
any(target_os = "linux", target_os = "android")
7883+
))]
78757884
fn test_cp_preserve_context_with_z_fails() {
78767885
let (at, mut ucmd) = at_and_ucmd!();
78777886
at.touch("src");

0 commit comments

Comments
 (0)