Skip to content
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ feat_acl = ["cp/feat_acl"]
# * 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.
feat_selinux = [
"cp/selinux",
"cp/feat_selinux",
"feat_require_selinux",
"id/selinux",
"install/selinux",
"ls/selinux",
"mkdir/selinux",
"mkfifo/selinux",
"mknod/selinux",
"selinux",
"stat/selinux",
"id/feat_selinux",
"install/feat_selinux",
"ls/feat_selinux",
"mkdir/feat_selinux",
"mkfifo/feat_selinux",
"mknod/feat_selinux",
"stat/feat_selinux",
"uutests/feat_selinux",
]
##
## feature sets
Expand Down Expand Up @@ -532,6 +532,7 @@ num-prime.workspace = true
pretty_assertions = "1.4.0"
rand.workspace = true
regex.workspace = true
selinux = { workspace = true }
sha1 = { workspace = true, features = ["std"] }
tempfile.workspace = true
time = { workspace = true, features = ["local-offset"] }
Expand Down
4 changes: 2 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ TEST_NO_FAIL_FAST :=--no-fail-fast
TEST_SPEC_FEATURE := test_unimplemented
else ifeq ($(SELINUX_ENABLED),1)
TEST_NO_FAIL_FAST :=
TEST_SPEC_FEATURE := selinux
BUILD_SPEC_FEATURE := selinux
TEST_SPEC_FEATURE := feat_selinux
BUILD_SPEC_FEATURE := feat_selinux
endif

define TEST_BUSYBOX
Expand Down
6 changes: 3 additions & 3 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ impl Options {
}
}

#[cfg(not(feature = "selinux"))]
#[cfg(not(feature = "feat_selinux"))]
if let Preserve::Yes { required } = attributes.context {
let selinux_disabled_error = CpError::Error(translate!("cp-error-selinux-not-enabled"));
if required {
Expand Down Expand Up @@ -1713,7 +1713,7 @@ pub(crate) fn copy_attributes(
Ok(())
})?;

#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
handle_preserve(&attributes.context, || -> CopyResult<()> {
// Get the source context and apply it to the destination
if let Ok(context) = selinux::SecurityContext::of_path(source, false, false) {
Expand Down Expand Up @@ -2469,7 +2469,7 @@ fn copy_file(
copy_attributes(source, dest, &options.attributes)?;
}

#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
if options.set_selinux_context && uucore::selinux::is_selinux_enabled() {
// Set the given selinux permissions on the copied file.
if let Err(e) =
Expand Down
18 changes: 12 additions & 6 deletions src/uu/id/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ macro_rules! cstr2cow {
}

fn get_context_help_text() -> String {
#[cfg(not(feature = "selinux"))]
#[cfg(not(feature = "feat_selinux"))]
return translate!("id-context-help-disabled");
#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
return translate!("id-context-help-enabled");
}

Expand Down Expand Up @@ -139,11 +139,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
cflag: matches.get_flag(options::OPT_CONTEXT),

selinux_supported: {
#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
{
uucore::selinux::is_selinux_enabled()
}
#[cfg(not(feature = "selinux"))]
#[cfg(not(feature = "feat_selinux"))]
{
false
}
Expand Down Expand Up @@ -183,7 +183,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if state.cflag {
return if state.selinux_supported {
// print SElinux context and exit
#[cfg(all(any(target_os = "linux", target_os = "android"), feature = "selinux"))]
#[cfg(all(
any(target_os = "linux", target_os = "android"),
feature = "feat_selinux"
))]
if let Ok(context) = selinux::SecurityContext::current(false) {
let bytes = context.as_bytes();
print!("{}{line_ending}", String::from_utf8_lossy(bytes));
Expand Down Expand Up @@ -644,7 +647,10 @@ fn id_print(state: &State, groups: &[u32]) {
.join(",")
);

#[cfg(all(any(target_os = "linux", target_os = "android"), feature = "selinux"))]
#[cfg(all(
any(target_os = "linux", target_os = "android"),
feature = "feat_selinux"
))]
if state.selinux_supported
&& !state.user_specified
&& std::env::var_os("POSIXLY_CORRECT").is_none()
Expand Down
2 changes: 1 addition & 1 deletion src/uu/install/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ uucore = { workspace = true, default-features = true, features = [
fluent = { workspace = true }

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

[[bin]]
name = "install"
Expand Down
12 changes: 6 additions & 6 deletions src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use uucore::fs::dir_strip_dot_for_creation;
use uucore::mode::get_umask;
use uucore::perms::{Verbosity, VerbosityLevel, wrap_chown};
use uucore::process::{getegid, geteuid};
#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
use uucore::selinux::{contexts_differ, set_selinux_security_context};
use uucore::translate;
use uucore::{format_usage, show, show_error, show_if_err};
Expand Down Expand Up @@ -113,7 +113,7 @@ enum InstallError {
#[error("{}", translate!("install-error-extra-operand", "operand" => .0.quote(), "usage" => .1.clone()))]
ExtraOperand(String, String),

#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
#[error("{}", .0)]
SelinuxContextFailed(String),
}
Expand Down Expand Up @@ -482,7 +482,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(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
show_if_err!(set_selinux_context(path, b));
}
// If the exit code was set, or show! has been called at least once
Expand Down Expand Up @@ -956,7 +956,7 @@ fn copy(from: &Path, to: &Path, b: &Behavior) -> UResult<()> {
preserve_timestamps(from, to)?;
}

#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
if b.preserve_context {
uucore::selinux::preserve_security_context(from, to)
.map_err(|e| InstallError::SelinuxContextFailed(e.to_string()))?;
Expand Down Expand Up @@ -1071,7 +1071,7 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> bool {
return true;
}

#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
if b.preserve_context && contexts_differ(from, to) {
return true;
}
Expand Down Expand Up @@ -1102,7 +1102,7 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> bool {
false
}

#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
fn set_selinux_context(path: &Path, behavior: &Behavior) -> UResult<()> {
if !behavior.preserve_context && behavior.context.is_some() {
// Use the provided context set by -Z/--context
Expand Down
8 changes: 4 additions & 4 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,11 +1083,11 @@ impl Config {
time_format_older,
context,
selinux_supported: {
#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
{
uucore::selinux::is_selinux_enabled()
}
#[cfg(not(feature = "selinux"))]
#[cfg(not(feature = "feat_selinux"))]
{
false
}
Expand Down Expand Up @@ -3278,7 +3278,7 @@ fn get_security_context(config: &Config, p_buf: &Path, must_dereference: bool) -
}
}
if config.selinux_supported {
#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
{
match selinux::SecurityContext::of_path(p_buf, must_dereference.to_owned(), false) {
Err(_r) => {
Expand All @@ -3302,7 +3302,7 @@ fn get_security_context(config: &Config, p_buf: &Path, must_dereference: bool) -
}
}
}
#[cfg(not(feature = "selinux"))]
#[cfg(not(feature = "feat_selinux"))]
{
substitute_string
}
Expand Down
2 changes: 1 addition & 1 deletion src/uu/mkdir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ uucore = { workspace = true, features = ["fs", "mode", "fsxattr"] }
fluent = { workspace = true }

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

[[bin]]
name = "mkdir"
Expand Down
2 changes: 1 addition & 1 deletion src/uu/mkdir/src/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ fn create_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<()> {
chmod(path, new_mode)?;

// Apply SELinux context if requested
#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
if config.set_selinux_context && uucore::selinux::is_selinux_enabled() {
if let Err(e) = uucore::selinux::set_selinux_security_context(path, config.context)
{
Expand Down
2 changes: 1 addition & 1 deletion src/uu/mkfifo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ uucore = { workspace = true, features = ["fs", "mode"] }
fluent = { workspace = true }

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

[[bin]]
name = "mkfifo"
Expand Down
2 changes: 1 addition & 1 deletion src/uu/mkfifo/src/mkfifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}

// Apply SELinux context if requested
#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
{
// Extract the SELinux related flags and options
let set_selinux_context = matches.get_flag(options::SELINUX);
Expand Down
2 changes: 1 addition & 1 deletion src/uu/mknod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ uucore = { workspace = true, features = ["mode"] }
fluent = { workspace = true }

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

[[bin]]
name = "mknod"
Expand Down
2 changes: 1 addition & 1 deletion src/uu/mknod/src/mknod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn mknod(file_name: &str, config: Config) -> i32 {
}

// Apply SELinux context if requested
#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
if config.set_selinux_context {
if let Err(e) = uucore::selinux::set_selinux_security_context(
std::path::Path::new(file_name),
Expand Down
2 changes: 1 addition & 1 deletion src/uu/mv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ windows-sys = { workspace = true, features = [
libc = { workspace = true }

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

[[bin]]
name = "mv"
Expand Down
4 changes: 2 additions & 2 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use uucore::fs::{
};
#[cfg(all(unix, not(any(target_os = "macos", target_os = "redox"))))]
use uucore::fsxattr;
#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
use uucore::selinux::set_selinux_security_context;
use uucore::translate;
use uucore::update_control;
Expand Down Expand Up @@ -767,7 +767,7 @@ fn rename(
rename_with_fallback(from, to, multi_progress, None, None)?;
}

#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
if let Some(ref context) = opts.context {
set_selinux_security_context(to, Some(context))
.map_err(|e| io::Error::other(e.to_string()))?;
Expand Down
2 changes: 1 addition & 1 deletion src/uu/stat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ thiserror = { workspace = true }
fluent = { workspace = true }

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

[[bin]]
name = "stat"
Expand Down
4 changes: 2 additions & 2 deletions src/uu/stat/src/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ impl Stater {
'B' => OutputType::Unsigned(512),
// SELinux security context string
'C' => {
#[cfg(feature = "selinux")]
#[cfg(feature = "feat_selinux")]
{
if uucore::selinux::is_selinux_enabled() {
match uucore::selinux::get_selinux_security_context(
Expand All @@ -969,7 +969,7 @@ impl Stater {
OutputType::Str(translate!("stat-selinux-unsupported-system"))
}
}
#[cfg(not(feature = "selinux"))]
#[cfg(not(feature = "feat_selinux"))]
{
OutputType::Str(translate!("stat-selinux-unsupported-os"))
}
Expand Down
12 changes: 6 additions & 6 deletions tests/by-util/test_chcon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::ffi::CString;
use std::path::Path;
use std::{io, iter, str};

use selinux::{SecurityContext, errors};
use uutests::at_and_ucmd;
use uutests::new_ucmd;

Expand Down Expand Up @@ -590,9 +591,9 @@ fn valid_reference_multi() {
);
}

fn get_file_context(path: impl AsRef<Path>) -> Result<Option<String>, selinux::errors::Error> {
fn get_file_context(path: impl AsRef<Path>) -> Result<Option<String>, errors::Error> {
let path = path.as_ref();
match selinux::SecurityContext::of_path(path, false, false) {
match SecurityContext::of_path(path, false, false) {
Err(r) => {
println!("get_file_context failed: '{}': {r}.", path.display());
Err(r)
Expand All @@ -619,15 +620,14 @@ fn get_file_context(path: impl AsRef<Path>) -> Result<Option<String>, selinux::e
}
}

fn set_file_context(path: impl AsRef<Path>, context: &str) -> Result<(), selinux::errors::Error> {
let c_context = CString::new(context.as_bytes()).map_err(|_r| selinux::errors::Error::IO {
fn set_file_context(path: impl AsRef<Path>, context: &str) -> Result<(), errors::Error> {
let c_context = CString::new(context.as_bytes()).map_err(|_r| errors::Error::IO {
source: io::Error::from(io::ErrorKind::InvalidInput),
operation: "CString::new",
})?;

let path = path.as_ref();
let r =
selinux::SecurityContext::from_c_str(&c_context, false).set_for_path(path, false, false);
let r = SecurityContext::from_c_str(&c_context, false).set_for_path(path, false, false);
if let Err(r) = &r {
println!(
"set_file_context failed: '{context}' => '{}': {r}.",
Expand Down
4 changes: 4 additions & 0 deletions tests/uutests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ uucore = { workspace = true, features = [
] }

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
selinux = { workspace = true, optional = true }

[target.'cfg(unix)'.dependencies]
nix = { workspace = true, features = ["process", "signal", "user", "term"] }
rlimit = "0.10.1"
xattr = { workspace = true }

[features]
feat_selinux = ["selinux"]
2 changes: 1 addition & 1 deletion util/build-gnu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ cd "${path_UUTILS}" && echo "[ pwd:'${PWD}' ]"
if [ "$(uname)" == "Linux" ]; then
# Only attempt to enable SELinux features on Linux
export SELINUX_ENABLED=1
CARGO_FEATURE_FLAGS="${CARGO_FEATURE_FLAGS} selinux"
CARGO_FEATURE_FLAGS="${CARGO_FEATURE_FLAGS} feat_selinux"
fi

# Trim leading whitespace from feature flags
Expand Down
Loading