Skip to content

Commit 8060cc6

Browse files
committed
lsm: Deduplicate and comment a bit of the install_t code
Just a followup to previous changes. Signed-off-by: Colin Walters <[email protected]>
1 parent 0910876 commit 8060cc6

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

lib/src/lsm.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,31 @@ const SELINUXFS: &str = "/sys/fs/selinux";
2020
/// The SELinux xattr
2121
#[cfg(feature = "install")]
2222
const SELINUX_XATTR: &[u8] = b"security.selinux\0";
23+
const SELF_CURRENT: &str = "/proc/self/attr/current";
2324

2425
#[context("Querying selinux availability")]
2526
pub(crate) fn selinux_enabled() -> Result<bool> {
2627
let filesystems = std::fs::read_to_string("/proc/filesystems")?;
2728
Ok(filesystems.contains("selinuxfs\n"))
2829
}
2930

31+
/// Get the current process SELinux security context
32+
fn get_current_security_context() -> Result<String> {
33+
std::fs::read_to_string(SELF_CURRENT).with_context(|| format!("Reading {SELF_CURRENT}"))
34+
}
35+
36+
/// Determine if a security context is the "install_t" type which can
37+
/// write arbitrary labels.
38+
fn context_is_install_t(context: &str) -> bool {
39+
// TODO: we shouldn't actually hardcode this...it's just ugly though
40+
// to figure out whether we really can gain CAP_MAC_ADMIN.
41+
context.contains(":install_t:")
42+
}
43+
3044
#[context("Ensuring selinux install_t type")]
3145
pub(crate) fn selinux_ensure_install() -> Result<()> {
3246
let guardenv = "_bootc_selinuxfs_mounted";
33-
let current = std::fs::read_to_string("/proc/self/attr/current")
34-
.context("Reading /proc/self/attr/current")?;
47+
let current = get_current_security_context()?;
3548
tracing::debug!("Current security context is {current}");
3649
if let Some(p) = std::env::var_os(guardenv) {
3750
let p = Path::new(&p);
@@ -85,13 +98,13 @@ impl Drop for SetEnforceGuard {
8598
#[cfg(feature = "install")]
8699
pub(crate) fn selinux_ensure_install_or_setenforce() -> Result<Option<SetEnforceGuard>> {
87100
// If the process already has install_t, exit early
88-
if self_has_install_t()? {
101+
let current = get_current_security_context()?;
102+
if context_is_install_t(&current) {
89103
return Ok(None);
90104
}
105+
// Note that this will re-exec the entire process
91106
selinux_ensure_install()?;
92-
let current = std::fs::read_to_string("/proc/self/attr/current")
93-
.context("Reading /proc/self/attr/current")?;
94-
let g = if !current.contains("install_t") {
107+
let g = if !context_is_install_t(&current) {
95108
tracing::warn!("Failed to enter install_t; temporarily setting permissive mode");
96109
selinux_set_permissive(true)?;
97110
Some(SetEnforceGuard)
@@ -174,10 +187,3 @@ pub(crate) fn xattrs_have_selinux(xattrs: &ostree::glib::Variant) -> bool {
174187
}
175188
false
176189
}
177-
178-
fn self_has_install_t() -> Result<bool> {
179-
let current = std::fs::read_to_string("/proc/self/attr/current")
180-
.context("Reading /proc/self/attr/current")?;
181-
182-
Ok(current.contains("install_t"))
183-
}

0 commit comments

Comments
 (0)