| 1 |
#[cfg(test)] |
| 2 |
mod tests; |
| 3 |
|
| 4 |
use core::ptr; |
| 5 |
use std::io; |
| 6 |
use std::os::raw::{c_char, c_int, c_uint, c_void}; |
| 7 |
use std::path::Path; |
| 8 |
|
| 9 |
use crate::errors::{Error, Result}; |
| 10 |
use crate::utils::{ |
| 11 |
CAllocatedBlock, get_static_path, os_str_to_c_string, ret_val_to_result, |
| 12 |
ret_val_to_result_with_path, |
| 13 |
}; |
| 14 |
|
| 15 |
/// Load a new SELinux policy. |
| 16 |
/// |
| 17 |
/// See: `security_load_policy()`. |
| 18 |
#[doc(alias = "security_load_policy")] |
| 19 |
pub fn load(policy_bytes: &[u8]) -> Result<()> { |
| 20 |
// security_load_policy() declares "data" as a constant pointer starting from libselinux |
| 21 |
// version 3.5. |
| 22 |
// Previous supported versions have the same security_load_policy() implementation, but declare |
| 23 |
// "data" as a mutable pointer, even though it is never modified. |
| 24 |
#[allow(clippy::as_conversions)] |
| 25 |
let data = policy_bytes.as_ptr() as *mut c_void; |
| 26 |
let r = unsafe { selinux_sys::security_load_policy(data.cast(), policy_bytes.len()) }; |
| 27 |
ret_val_to_result("security_load_policy()", r) |
| 28 |
} |
| 29 |
|
| 30 |
/// Make a policy image and load it. |
| 31 |
/// |
| 32 |
/// See: `selinux_mkload_policy()`. |
| 33 |
#[doc(alias = "selinux_mkload_policy")] |
| 34 |
pub fn make_and_load() -> Result<()> { |
| 35 |
let r = unsafe { selinux_sys::selinux_mkload_policy(0) }; |
| 36 |
ret_val_to_result("selinux_mkload_policy()", r) |
| 37 |
} |
| 38 |
|
| 39 |
/// Perform the initial policy load. |
| 40 |
/// |
| 41 |
/// See: `selinux_init_load_policy()`. |
| 42 |
#[doc(alias = "selinux_init_load_policy")] |
| 43 |
pub fn load_initial() -> Result<c_int> { |
| 44 |
let mut enforce: c_int = 0; |
| 45 |
if unsafe { selinux_sys::selinux_init_load_policy(&raw mut enforce) } == -1_i32 { |
| 46 |
Err(Error::last_io_error("selinux_init_load_policy()")) |
| 47 |
} else { |
| 48 |
Ok(enforce) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/// Get the type of SELinux policy running on the system. |
| 53 |
/// |
| 54 |
/// See: `selinux_getpolicytype()`. |
| 55 |
#[doc(alias = "selinux_getpolicytype")] |
| 56 |
pub fn policy_type() -> Result<CAllocatedBlock<c_char>> { |
| 57 |
let mut name_ptr: *mut c_char = ptr::null_mut(); |
| 58 |
if unsafe { selinux_sys::selinux_getpolicytype(&raw mut name_ptr) } == -1_i32 { |
| 59 |
Err(Error::last_io_error("selinux_getpolicytype()")) |
| 60 |
} else { |
| 61 |
CAllocatedBlock::new(name_ptr).ok_or_else(|| { |
| 62 |
Error::from_io("selinux_getpolicytype()", io::ErrorKind::InvalidData.into()) |
| 63 |
}) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// Get the version of the SELinux policy. |
| 68 |
/// |
| 69 |
/// See: `security_policyvers()`. |
| 70 |
#[doc(alias = "security_policyvers")] |
| 71 |
pub fn version_number() -> Result<c_uint> { |
| 72 |
let r: c_int = unsafe { selinux_sys::security_policyvers() }; |
| 73 |
if r == -1_i32 { |
| 74 |
Err(Error::last_io_error("security_policyvers()")) |
| 75 |
} else { |
| 76 |
#[allow(clippy::cast_sign_loss, clippy::as_conversions)] |
| 77 |
Ok(r as c_uint) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/// Return the path of the SELinux policy files for this machine. |
| 82 |
/// |
| 83 |
/// See: `selinux_policy_root()`. |
| 84 |
#[doc(alias = "selinux_policy_root")] |
| 85 |
pub fn root_path() -> Result<&'static Path> { |
| 86 |
get_static_path(selinux_sys::selinux_policy_root, "selinux_policy_root()") |
| 87 |
} |
| 88 |
|
| 89 |
/// Set an alternate SELinux root path for the SELinux policy files for this machine. |
| 90 |
/// |
| 91 |
/// See: `selinux_set_policy_root()`. |
| 92 |
#[doc(alias = "selinux_set_policy_root")] |
| 93 |
pub fn set_root_path(path: impl AsRef<Path>) -> Result<()> { |
| 94 |
let c_path = os_str_to_c_string(path.as_ref().as_os_str())?; |
| 95 |
let r = unsafe { selinux_sys::selinux_set_policy_root(c_path.as_ptr()) }; |
| 96 |
ret_val_to_result_with_path("selinux_set_policy_root()", r, path.as_ref()) |
| 97 |
} |
| 98 |
|
| 99 |
/// Return the currently loaded policy file from the kernel. |
| 100 |
/// |
| 101 |
/// See: `selinux_current_policy_path()`. |
| 102 |
#[doc(alias = "selinux_current_policy_path")] |
| 103 |
pub fn current_policy_path() -> Result<&'static Path> { |
| 104 |
let proc_name = "selinux_current_policy_path()"; |
| 105 |
get_static_path(selinux_sys::selinux_current_policy_path, proc_name) |
| 106 |
} |
| 107 |
|
| 108 |
/// Return the binary policy file loaded into kernel. |
| 109 |
/// |
| 110 |
/// See: `selinux_binary_policy_path()`. |
| 111 |
#[doc(alias = "selinux_binary_policy_path")] |
| 112 |
pub fn binary_policy_path() -> Result<&'static Path> { |
| 113 |
let proc_name = "selinux_binary_policy_path()"; |
| 114 |
get_static_path(selinux_sys::selinux_binary_policy_path, proc_name) |
| 115 |
} |