Skip to content

Commit f4b1617

Browse files
authored
Merge pull request #7909 from sylvestre/use-selinux-func
selinux: use the uucore::selinux::is_selinux_enabled() function
2 parents 7cd6876 + 545fab9 commit f4b1617

File tree

6 files changed

+23
-14
lines changed

6 files changed

+23
-14
lines changed

src/uu/id/src/id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
138138
selinux_supported: {
139139
#[cfg(feature = "selinux")]
140140
{
141-
selinux::kernel_support() != selinux::KernelSupport::Unsupported
141+
uucore::selinux::is_selinux_enabled()
142142
}
143143
#[cfg(not(feature = "selinux"))]
144144
{

src/uu/ls/src/ls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ impl Config {
11571157
selinux_supported: {
11581158
#[cfg(feature = "selinux")]
11591159
{
1160-
selinux::kernel_support() != selinux::KernelSupport::Unsupported
1160+
uucore::selinux::is_selinux_enabled()
11611161
}
11621162
#[cfg(not(feature = "selinux"))]
11631163
{

src/uu/runcon/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ path = "src/runcon.rs"
1919

2020
[dependencies]
2121
clap = { workspace = true }
22-
uucore = { workspace = true, features = ["entries", "fs", "perms"] }
22+
uucore = { workspace = true, features = ["entries", "fs", "perms", "selinux"] }
2323
selinux = { workspace = true }
2424
thiserror = { workspace = true }
2525
libc = { workspace = true }

src/uu/runcon/src/runcon.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn set_next_exec_context(context: &OpaqueSecurityContext) -> Result<()> {
271271
}
272272

273273
fn get_plain_context(context: &OsStr) -> Result<OpaqueSecurityContext> {
274-
if selinux::kernel_support() == selinux::KernelSupport::Unsupported {
274+
if !uucore::selinux::is_selinux_enabled() {
275275
return Err(Error::SELinuxNotEnabled);
276276
}
277277

@@ -342,7 +342,7 @@ fn get_custom_context(
342342
use OpaqueSecurityContext as OSC;
343343
type SetNewValueProc = fn(&OSC, &CStr) -> selinux::errors::Result<()>;
344344

345-
if selinux::kernel_support() == selinux::KernelSupport::Unsupported {
345+
if !uucore::selinux::is_selinux_enabled() {
346346
return Err(Error::SELinuxNotEnabled);
347347
}
348348

src/uucore/src/lib/features/selinux.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6+
//! Set of functions to manage SELinux security contexts
7+
68
use std::error::Error;
79
use std::path::Path;
810

@@ -284,7 +286,10 @@ mod tests {
284286
fn test_invalid_context_string_error() {
285287
let tmpfile = NamedTempFile::new().expect("Failed to create tempfile");
286288
let path = tmpfile.path();
287-
289+
if !is_selinux_enabled() {
290+
println!("test skipped: Kernel has no support for SElinux context");
291+
return;
292+
}
288293
// Pass a context string containing a null byte to trigger CString::new error
289294
let invalid_context = String::from("invalid\0context");
290295
let result = set_selinux_security_context(path, Some(&invalid_context));
@@ -322,7 +327,10 @@ mod tests {
322327
fn test_get_selinux_security_context() {
323328
let tmpfile = NamedTempFile::new().expect("Failed to create tempfile");
324329
let path = tmpfile.path();
325-
330+
if !is_selinux_enabled() {
331+
println!("test skipped: Kernel has no support for SElinux context");
332+
return;
333+
}
326334
std::fs::write(path, b"test content").expect("Failed to write to tempfile");
327335

328336
let result = get_selinux_security_context(path);
@@ -387,7 +395,10 @@ mod tests {
387395
#[test]
388396
fn test_get_selinux_context_nonexistent_file() {
389397
let path = Path::new("/nonexistent/file/that/does/not/exist");
390-
398+
if !is_selinux_enabled() {
399+
println!("test skipped: Kernel has no support for SElinux context");
400+
return;
401+
}
391402
let result = get_selinux_security_context(path);
392403

393404
assert!(result.is_err());

tests/by-util/test_id.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ fn test_id_zero() {
376376
#[test]
377377
#[cfg(feature = "feat_selinux")]
378378
fn test_id_context() {
379-
use selinux::{self, KernelSupport};
380-
if selinux::kernel_support() == KernelSupport::Unsupported {
379+
if !uucore::selinux::is_selinux_enabled() {
381380
println!("test skipped: Kernel has no support for SElinux context");
382381
return;
383382
}
@@ -450,12 +449,11 @@ fn test_id_no_specified_user_posixly() {
450449
feature = "feat_selinux"
451450
))]
452451
{
453-
use selinux::{self, KernelSupport};
454-
if selinux::kernel_support() == KernelSupport::Unsupported {
455-
println!("test skipped: Kernel has no support for SElinux context");
456-
} else {
452+
if uucore::selinux::is_selinux_enabled() {
457453
let result = ts.ucmd().succeeds();
458454
assert!(result.stdout_str().contains("context="));
455+
} else {
456+
println!("test skipped: Kernel has no support for SElinux context");
459457
}
460458
}
461459
}

0 commit comments

Comments
 (0)