1use anyhow::Result;
4use fn_error_context::context;
5use std::path::Path;
6
7const SELINUX_MNT: &str = "/sys/fs/selinux";
9const INSTALL_T: &str = "install_t";
11
12pub fn is_selinux_enabled() -> bool {
14 Path::new(SELINUX_MNT).join("access").exists()
15}
16
17#[context("Verifying self is install_t SELinux domain")]
19pub fn verify_install_domain() -> Result<()> {
20 if !is_selinux_enabled() {
22 return Ok(());
23 }
24
25 if !rustix::process::getuid().is_root() {
28 return Ok(());
29 }
30
31 let self_domain = std::fs::read_to_string("/proc/self/attr/current")?;
32 let is_install_t = self_domain.split(':').any(|x| x == INSTALL_T);
33 if !is_install_t {
34 anyhow::bail!(
35 "Detected SELinux enabled system, but the executing binary is not labeled install_exec_t"
36 );
37 }
38 Ok(())
39}