ksud: load profile sepolicy rules when boot

This commit is contained in:
weishu
2023-07-01 16:50:10 +08:00
parent 2bb73a2a92
commit a9c33f6940
2 changed files with 30 additions and 2 deletions

View File

@@ -158,6 +158,10 @@ pub fn on_post_data_fs() -> Result<()> {
warn!("load sepolicy.rule failed"); warn!("load sepolicy.rule failed");
} }
if let Err(e) = crate::profile::apply_sepolies() {
warn!("apply root profile sepolicy failed: {}", e);
}
// exec modules post-fs-data scripts // exec modules post-fs-data scripts
// TODO: Add timeout // TODO: Add timeout
if let Err(e) = crate::module::exec_post_fs_data() { if let Err(e) = crate::module::exec_post_fs_data() {

View File

@@ -1,6 +1,6 @@
use crate::defs; use crate::{defs, sepolicy};
use crate::utils::ensure_dir_exists; use crate::utils::ensure_dir_exists;
use anyhow::Result; use anyhow::{Context, Result};
use std::path::Path; use std::path::Path;
pub fn set_sepolicy(pkg: String, policy: String) -> Result<()> { pub fn set_sepolicy(pkg: String, policy: String) -> Result<()> {
@@ -44,3 +44,27 @@ pub fn list_templates() -> Result<()> {
} }
Ok(()) Ok(())
} }
pub fn apply_sepolies() -> Result<()> {
let path = Path::new(defs::PROFILE_SELINUX_DIR);
if !path.exists() {
log::info!("profile sepolicy dir not exists.");
return Ok(());
}
let sepolicies =
std::fs::read_dir(path).with_context(|| "profile sepolicy dir open failed.".to_string())?;
for sepolicy in sepolicies {
let Ok(sepolicy) = sepolicy else {
log::info!("profile sepolicy dir read failed.");
continue;
};
let sepolicy = sepolicy.path();
if sepolicy::apply_file(&sepolicy).is_ok() {
log::info!("profile sepolicy applied: {:?}", sepolicy);
} else {
log::info!("profile sepolicy apply failed: {:?}", sepolicy);
}
}
Ok(())
}