fix(selinux): add missing os checks

This commit is contained in:
xtqqczze
2026-05-21 09:13:41 +02:00
committed by Daniel Hofstetter
parent a20214d07d
commit 24e3d0d39e
4 changed files with 33 additions and 18 deletions
+3 -3
View File
@@ -26,7 +26,7 @@ use uucore::translate;
use uucore::uio_error;
use walkdir::{DirEntry, WalkDir};
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
use crate::set_selinux_context;
use crate::{
CopyMode, CopyResult, CpError, Options, aligned_ancestors, context_for, copy_attributes,
@@ -563,7 +563,7 @@ pub(crate) fn copy_directory(
options.set_selinux_context,
)?;
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if options.set_selinux_context {
set_selinux_context(&dir.dest, options.context.as_ref())?;
}
@@ -583,7 +583,7 @@ pub(crate) fn copy_directory(
options.set_selinux_context,
)?;
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if options.set_selinux_context {
set_selinux_context(y, options.context.as_ref())?;
}
+5 -5
View File
@@ -896,11 +896,11 @@ impl Attributes {
mode: Preserve::Yes { required: true },
timestamps: Preserve::Yes { required: true },
context: {
#[cfg(feature = "feat_selinux")]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
{
Preserve::Yes { required: false }
}
#[cfg(not(feature = "feat_selinux"))]
#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))]
{
Preserve::No { explicit: false }
}
@@ -1141,7 +1141,7 @@ impl Options {
}
}
#[cfg(not(feature = "selinux"))]
#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))]
if let Preserve::Yes { required } = attributes.context {
let selinux_disabled_error = CpError::Error(translate!("cp-error-selinux-not-enabled"));
if required {
@@ -1692,7 +1692,7 @@ fn handle_preserve<F: Fn() -> CopyResult<()>>(p: Preserve, f: F) -> CopyResult<(
Ok(())
}
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
pub(crate) fn set_selinux_context(path: &Path, context: Option<&String>) -> CopyResult<()> {
if !uucore::selinux::is_selinux_enabled() {
return Ok(());
@@ -2670,7 +2670,7 @@ fn copy_file(
fs::File::create(dest).map(|f| f.set_len(0)).ok();
})?;
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if options.set_selinux_context {
set_selinux_context(dest, options.context.as_ref())?;
}
+13 -7
View File
@@ -489,7 +489,7 @@ fn directory(paths: &[OsString], b: &Behavior) -> UResult<()> {
}
// Set SELinux context for all created directories if needed
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if should_set_selinux_context(b) {
let context = get_context_for_selinux(b);
set_selinux_context_for_directories_install(path_to_create.as_path(), context);
@@ -514,7 +514,7 @@ fn directory(paths: &[OsString], b: &Behavior) -> UResult<()> {
show_if_err!(chown_optional_user_group(path, b));
// Set SELinux context for directory if needed
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if b.default_context {
show_if_err!(set_selinux_default_context(path));
} else if b.context.is_some() {
@@ -683,7 +683,10 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
}
// Set SELinux context for all created directories if needed
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(
feature = "selinux",
any(target_os = "linux", target_os = "android")
))]
if should_set_selinux_context(b) {
let context = get_context_for_selinux(b);
set_selinux_context_for_directories_install(to_create, context);
@@ -717,7 +720,10 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
}
// Set SELinux context for all created directories if needed
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(
feature = "selinux",
any(target_os = "linux", target_os = "android")
))]
if should_set_selinux_context(b) {
let context = get_context_for_selinux(b);
set_selinux_context_for_directories_install(to_create, context);
@@ -1090,7 +1096,7 @@ fn finalize_installed_file(
preserve_timestamps(from, to)?;
}
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if b.privileged {
if b.preserve_context {
uucore::selinux::preserve_security_context(from, to)
@@ -1154,7 +1160,7 @@ fn get_context_for_selinux(b: &Behavior) -> Option<&String> {
}
}
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
fn should_set_selinux_context(b: &Behavior) -> bool {
b.privileged && (b.context.is_some() || b.default_context)
}
@@ -1250,7 +1256,7 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> bool {
}
if b.privileged {
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if b.preserve_context && contexts_differ(from, to) {
return true;
}
+12 -3
View File
@@ -1830,7 +1830,10 @@ fn test_cp_preserve_xattr() {
}
#[test]
#[cfg(all(target_os = "linux", not(feature = "feat_selinux")))]
#[cfg(all(
not(feature = "feat_selinux"),
any(target_os = "linux", target_os = "android")
))]
fn test_cp_preserve_all_context_fails_on_non_selinux() {
new_ucmd!()
.arg(TEST_COPY_FROM_FOLDER_FILE)
@@ -6802,7 +6805,10 @@ fn test_cp_selinux() {
}
#[test]
#[cfg(feature = "feat_selinux")]
#[cfg(all(
feature = "feat_selinux",
any(target_os = "linux", target_os = "android")
))]
fn test_cp_selinux_invalid() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
@@ -7871,7 +7877,10 @@ fn test_cp_a_preserves_context() {
}
#[test]
#[cfg(feature = "feat_selinux")]
#[cfg(all(
feature = "feat_selinux",
any(target_os = "linux", target_os = "android")
))]
fn test_cp_preserve_context_with_z_fails() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("src");