Skip to content

Commit 7842a61

Browse files
committed
install: Change to consume SELinux guard (and Arc<State>)
This avoids a dead code warning on newer rustc. Also, it's just better because if we fail to re-invoke `setenforce 1` this should be a fatal error probably. Signed-off-by: Colin Walters <[email protected]>
1 parent 201c439 commit 7842a61

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

lib/src/install.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,20 @@ pub(crate) async fn install_to_disk(mut opts: InstallToDiskOpts) -> Result<()> {
12421242
loopback_dev.close()?;
12431243
}
12441244

1245+
// At this point, all other threads should be gone.
1246+
if let Some(state) = Arc::into_inner(state) {
1247+
// If we had invoked `setenforce 0`, then let's re-enable it.
1248+
match state.selinux_state {
1249+
SELinuxFinalState::Enabled(Some(guard)) => {
1250+
guard.consume()?;
1251+
}
1252+
_ => {}
1253+
}
1254+
} else {
1255+
// This shouldn't happen...but we will make it not fatal right now
1256+
tracing::warn!("Failed to consume state Arc");
1257+
}
1258+
12451259
installation_complete();
12461260

12471261
Ok(())

lib/src/lsm.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,29 @@ pub(crate) fn selinux_ensure_install() -> Result<bool> {
9999
/// gain the `mac_admin` permission (install_t).
100100
#[cfg(feature = "install")]
101101
#[must_use]
102-
pub(crate) struct SetEnforceGuard;
102+
pub(crate) struct SetEnforceGuard(Option<()>);
103+
104+
#[cfg(feature = "install")]
105+
impl SetEnforceGuard {
106+
pub(crate) fn new() -> Self {
107+
SetEnforceGuard(Some(()))
108+
}
109+
110+
pub(crate) fn consume(mut self) -> Result<()> {
111+
// SAFETY: The option cannot have been consumed until now
112+
self.0.take().unwrap();
113+
// This returns errors
114+
selinux_set_permissive(false)
115+
}
116+
}
103117

104118
#[cfg(feature = "install")]
105119
impl Drop for SetEnforceGuard {
106120
fn drop(&mut self) {
107-
let _ = selinux_set_permissive(false);
121+
// A best-effort attempt to re-enable enforcement on drop (installation failure)
122+
if let Some(()) = self.0.take() {
123+
let _ = selinux_set_permissive(false);
124+
}
108125
}
109126
}
110127

@@ -121,7 +138,7 @@ pub(crate) fn selinux_ensure_install_or_setenforce() -> Result<Option<SetEnforce
121138
let g = if std::env::var_os("BOOTC_SETENFORCE0_FALLBACK").is_some() {
122139
tracing::warn!("Failed to enter install_t; temporarily setting permissive mode");
123140
selinux_set_permissive(true)?;
124-
Some(SetEnforceGuard)
141+
Some(SetEnforceGuard::new())
125142
} else {
126143
let current = get_current_security_context()?;
127144
anyhow::bail!("Failed to enter install_t (running as {current}) - use BOOTC_SETENFORCE0_FALLBACK=1 to override");

0 commit comments

Comments
 (0)