Skip to content

Commit 6b8a5a1

Browse files
xtqqczzecakebaker
authored andcommitted
fix: refine feature checks for SELinux and SMACK support
1 parent 1bbd10e commit 6b8a5a1

5 files changed

Lines changed: 31 additions & 13 deletions

File tree

src/uu/mkdir/src/mkdir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<(
331331
}
332332

333333
// Apply SELinux context if requested
334-
#[cfg(feature = "selinux")]
334+
#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))]
335335
if config.set_security_context && uucore::selinux::is_selinux_enabled() {
336336
if let Err(e) = uucore::selinux::set_selinux_security_context(path, config.context)
337337
{
@@ -341,7 +341,7 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<(
341341
}
342342

343343
// Apply SMACK context if requested
344-
#[cfg(feature = "smack")]
344+
#[cfg(all(feature = "smack", target_os = "linux"))]
345345
if config.set_security_context {
346346
uucore::smack::set_smack_label_and_cleanup(path, config.context, |p| {
347347
std::fs::remove_dir(p)

src/uu/mkfifo/src/mkfifo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8383
}
8484

8585
// Apply SMACK context if requested
86-
#[cfg(feature = "smack")]
86+
#[cfg(all(feature = "smack", target_os = "linux"))]
8787
{
8888
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
8989
let context = matches.get_one::<String>(options::CONTEXT);

src/uu/mknod/src/mknod.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,17 @@ struct Config {
5757
dev: u64,
5858

5959
/// Set security context (SELinux/SMACK).
60-
#[cfg(any(feature = "selinux", feature = "smack"))]
60+
#[cfg(any(
61+
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
62+
all(feature = "smack", target_os = "linux"),
63+
))]
6164
set_security_context: bool,
6265

6366
/// Specific security context (SELinux/SMACK).
64-
#[cfg(any(feature = "selinux", feature = "smack"))]
67+
#[cfg(any(
68+
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
69+
all(feature = "smack", target_os = "linux"),
70+
))]
6571
context: Option<String>,
6672
}
6773

@@ -96,7 +102,7 @@ fn mknod(file_name: &str, config: Config) -> i32 {
96102
}
97103

98104
// Apply SELinux context if requested
99-
#[cfg(feature = "selinux")]
105+
#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))]
100106
if config.set_security_context {
101107
use std::io::Write as _;
102108

@@ -112,7 +118,7 @@ fn mknod(file_name: &str, config: Config) -> i32 {
112118
}
113119

114120
// Apply SMACK context if requested
115-
#[cfg(feature = "smack")]
121+
#[cfg(all(feature = "smack", target_os = "linux"))]
116122
if config.set_security_context {
117123
use std::io::Write as _;
118124

@@ -150,9 +156,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
150156
.expect("Missing argument 'NAME'");
151157

152158
// Extract the security context related flags and options
153-
#[cfg(any(feature = "selinux", feature = "smack"))]
159+
#[cfg(any(
160+
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
161+
all(feature = "smack", target_os = "linux"),
162+
))]
154163
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
155-
#[cfg(any(feature = "selinux", feature = "smack"))]
164+
#[cfg(any(
165+
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
166+
all(feature = "smack", target_os = "linux"),
167+
))]
156168
let context = matches.get_one::<String>(options::CONTEXT).cloned();
157169

158170
let dev = match (
@@ -181,9 +193,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
181193
file_type: file_type.clone(),
182194
use_umask,
183195
dev,
184-
#[cfg(any(feature = "selinux", feature = "smack"))]
196+
#[cfg(any(
197+
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
198+
all(feature = "smack", target_os = "linux"),
199+
))]
185200
set_security_context: set_security_context || context.is_some(),
186-
#[cfg(any(feature = "selinux", feature = "smack"))]
201+
#[cfg(any(
202+
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
203+
all(feature = "smack", target_os = "linux"),
204+
))]
187205
context,
188206
};
189207

src/uucore/src/lib/features.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub mod hardware;
8585
pub mod selinux;
8686
#[cfg(all(unix, not(target_os = "fuchsia"), feature = "signals"))]
8787
pub mod signals;
88-
#[cfg(all(target_os = "linux", feature = "smack"))]
88+
#[cfg(all(feature = "smack", target_os = "linux"))]
8989
pub mod smack;
9090
#[cfg(feature = "feat_systemd_logind")]
9191
pub mod systemd_logind;

src/uucore/src/lib/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub use crate::features::fsxattr;
125125
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
126126
pub use crate::features::selinux;
127127

128-
#[cfg(all(target_os = "linux", feature = "smack"))]
128+
#[cfg(all(feature = "smack", target_os = "linux"))]
129129
pub use crate::features::smack;
130130

131131
//## core functions

0 commit comments

Comments
 (0)