|
| 1 | +// This file is part of the uutils uucore package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +use std::path::Path; |
| 7 | + |
| 8 | +use selinux::SecurityContext; |
| 9 | + |
| 10 | +/// Sets the SELinux security context for the given filesystem path. |
| 11 | +/// |
| 12 | +/// If a specific context is provided, it attempts to set this context explicitly. |
| 13 | +/// Otherwise, it applies the default SELinux context for the provided path. |
| 14 | +/// |
| 15 | +/// # Arguments |
| 16 | +/// |
| 17 | +/// * `path` - Filesystem path on which to set the SELinux context. |
| 18 | +/// * `context` - Optional SELinux context string to explicitly set. |
| 19 | +/// |
| 20 | +/// # Errors |
| 21 | +/// |
| 22 | +/// Returns an error if: |
| 23 | +/// - SELinux is not enabled on the system. |
| 24 | +/// - The provided context is invalid or cannot be applied. |
| 25 | +/// - The default SELinux context cannot be set. |
| 26 | +pub fn set_selinux_security_context(path: &Path, context: Option<&String>) -> Result<(), String> { |
| 27 | + // Check if SELinux is enabled on the system |
| 28 | + if selinux::kernel_support() == selinux::KernelSupport::Unsupported { |
| 29 | + return Err("SELinux is not enabled on this system".into()); |
| 30 | + } |
| 31 | + |
| 32 | + if let Some(ctx_str) = context { |
| 33 | + // Create a CString from the provided context string |
| 34 | + let c_context = std::ffi::CString::new(ctx_str.as_str()) |
| 35 | + .map_err(|_| "Invalid context string (contains null bytes)".to_string())?; |
| 36 | + |
| 37 | + // Convert the CString into an SELinux security context |
| 38 | + let security_context = selinux::OpaqueSecurityContext::from_c_str(&c_context) |
| 39 | + .map_err(|e| format!("Failed to create security context: {}", e))?; |
| 40 | + |
| 41 | + // Set the provided security context on the specified path |
| 42 | + SecurityContext::from_c_str( |
| 43 | + &security_context.to_c_string().map_err(|e| e.to_string())?, |
| 44 | + false, |
| 45 | + ) |
| 46 | + .set_for_path(path, false, false) |
| 47 | + .map_err(|e| format!("Failed to set context: {}", e)) |
| 48 | + } else { |
| 49 | + // If no context provided, set the default SELinux context for the path |
| 50 | + SecurityContext::set_default_for_path(path) |
| 51 | + .map_err(|e| format!("Failed to set default context: {}", e)) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use super::*; |
| 58 | + use tempfile::NamedTempFile; |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_selinux_context_setting() { |
| 62 | + let tmpfile = NamedTempFile::new().expect("Failed to create tempfile"); |
| 63 | + let path = tmpfile.path(); |
| 64 | + |
| 65 | + let result = set_selinux_security_context(path, None); |
| 66 | + |
| 67 | + if result.is_ok() { |
| 68 | + // SELinux enabled and successfully set default context |
| 69 | + assert!(true, "Successfully set SELinux context"); |
| 70 | + } else { |
| 71 | + let err = result.unwrap_err(); |
| 72 | + let valid_errors = [ |
| 73 | + "SELinux is not enabled on this system", |
| 74 | + &format!( |
| 75 | + "Failed to set default context: selinux_lsetfilecon_default() failed on path '{}'", |
| 76 | + path.display() |
| 77 | + ), |
| 78 | + ]; |
| 79 | + |
| 80 | + assert!( |
| 81 | + valid_errors.contains(&err.as_str()), |
| 82 | + "Unexpected error message: {}", |
| 83 | + err |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_invalid_context_string_error() { |
| 90 | + let tmpfile = NamedTempFile::new().expect("Failed to create tempfile"); |
| 91 | + let path = tmpfile.path(); |
| 92 | + |
| 93 | + // Pass a context string containing a null byte to trigger CString::new error |
| 94 | + let invalid_context = String::from("invalid\0context"); |
| 95 | + let result = set_selinux_security_context(path, Some(&invalid_context)); |
| 96 | + |
| 97 | + assert!(result.is_err()); |
| 98 | + assert_eq!( |
| 99 | + result.unwrap_err(), |
| 100 | + "Invalid context string (contains null bytes)" |
| 101 | + ); |
| 102 | + } |
| 103 | +} |
0 commit comments