Skip to content

Commit c53f7bf

Browse files
committed
Dedup sepolicy handling
For historical reasons the ostree sepolicy API can exist as a no-op even if it didn't find a policy, one has to query `.csum()` or `.name()` to verify it's present. In our code just map that case to None. Followup to 99d30df to ensure we consistently handle this case. Signed-off-by: Colin Walters <[email protected]>
1 parent 5c1cb06 commit c53f7bf

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

lib/src/install.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,16 +385,14 @@ pub(crate) struct State {
385385
impl State {
386386
#[context("Loading SELinux policy")]
387387
pub(crate) fn load_policy(&self) -> Result<Option<ostree::SePolicy>> {
388-
use std::os::fd::AsRawFd;
389388
if !self.selinux_state.enabled() {
390389
return Ok(None);
391390
}
392391
// We always use the physical container root to bootstrap policy
393-
let r = ostree::SePolicy::new_at(self.container_root.as_raw_fd(), gio::Cancellable::NONE)?;
394-
let csum = r
395-
.csum()
392+
let r = lsm::new_sepolicy_at(&self.container_root)?
396393
.ok_or_else(|| anyhow::anyhow!("SELinux enabled, but no policy found in root"))?;
397-
tracing::debug!("Loaded SELinux policy: {csum}");
394+
// SAFETY: Policy must have a checksum here
395+
tracing::debug!("Loaded SELinux policy: {}", r.csum().unwrap());
398396
Ok(Some(r))
399397
}
400398

lib/src/install/completion.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use fn_error_context::context;
1313
use ostree_ext::{gio, ostree};
1414
use rustix::fs::Mode;
1515
use rustix::fs::OFlags;
16-
use std::os::fd::AsRawFd;
1716

1817
use crate::utils::deployment_fd;
1918

@@ -294,13 +293,12 @@ pub(crate) async fn impl_completion(
294293
if !bound_images.is_empty() {
295294
// load the selinux policy from the target ostree deployment
296295
let deployment_fd = deployment_fd(sysroot, deployment)?;
297-
let sepolicy =
298-
&ostree::SePolicy::new_at(deployment_fd.as_raw_fd(), gio::Cancellable::NONE)?;
296+
let sepolicy = crate::lsm::new_sepolicy_at(deployment_fd)?;
299297

300298
// When we're run through ostree, we only lazily initialize the podman storage to avoid
301299
// having a hard dependency on it.
302300
let imgstorage =
303-
&crate::imgstorage::Storage::create(&sysroot_dir, &rundir, Some(sepolicy))?;
301+
&crate::imgstorage::Storage::create(&sysroot_dir, &rundir, sepolicy.as_ref())?;
304302
crate::boundimage::pull_images_impl(imgstorage, bound_images)
305303
.await
306304
.context("pulling bound images")?;

lib/src/lsm.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ pub(crate) fn selinux_ensure_install_or_setenforce() -> Result<Option<SetEnforce
169169
Ok(g)
170170
}
171171

172+
/// A thin wrapper for loading a SELinux policy that maps "policy nonexistent" to None.
173+
pub(crate) fn new_sepolicy_at(fd: impl AsFd) -> Result<Option<ostree::SePolicy>> {
174+
let fd = fd.as_fd();
175+
let cancellable = gio::Cancellable::NONE;
176+
let sepolicy = ostree::SePolicy::new_at(fd.as_raw_fd(), cancellable)?;
177+
let r = if sepolicy.csum().is_none() {
178+
None
179+
} else {
180+
Some(sepolicy)
181+
};
182+
Ok(r)
183+
}
184+
172185
#[context("Setting SELinux permissive mode")]
173186
#[allow(dead_code)]
174187
pub(crate) fn selinux_set_permissive(permissive: bool) -> Result<()> {

lib/src/store/mod.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use cap_std_ext::cap_std::fs::Dir;
88
use cap_std_ext::dirext::CapStdExtDirExt;
99
use clap::ValueEnum;
1010
use fn_error_context::context;
11-
use std::os::fd::AsRawFd;
1211

1312
use ostree_ext::container::OstreeImageReference;
1413
use ostree_ext::keyfileext::KeyFileExt;
14+
use ostree_ext::ostree;
1515
use ostree_ext::sysroot::SysrootLock;
16-
use ostree_ext::{gio, ostree};
1716

17+
use crate::lsm;
1818
use crate::spec::ImageStatus;
1919
use crate::utils::deployment_fd;
2020

@@ -94,25 +94,20 @@ impl Storage {
9494
// this should only happen during cleanup of a broken install
9595
tracing::trace!("falling back to container root's selinux policy");
9696
let container_root = Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
97-
&ostree::SePolicy::new_at(container_root.as_raw_fd(), gio::Cancellable::NONE)?
97+
lsm::new_sepolicy_at(&container_root)?
9898
} else {
9999
// load the sepolicy from the booted ostree deployment so the imgstorage can be
100100
// properly labeled with /var/lib/container/storage labels
101101
tracing::trace!("loading sepolicy from booted ostree deployment");
102102
let dep = self.sysroot.booted_deployment().unwrap();
103103
let dep_fs = deployment_fd(&self.sysroot, &dep)?;
104-
&ostree::SePolicy::new_at(dep_fs.as_raw_fd(), gio::Cancellable::NONE)?
105-
};
106-
107-
let sepolicy = if sepolicy.csum().is_none() {
108-
None
109-
} else {
110-
Some(sepolicy)
104+
lsm::new_sepolicy_at(&dep_fs)?
111105
};
112106

113107
tracing::trace!("sepolicy in get_ensure_imgstore: {sepolicy:?}");
114108

115-
let imgstore = crate::imgstorage::Storage::create(&sysroot_dir, &self.run, sepolicy)?;
109+
let imgstore =
110+
crate::imgstorage::Storage::create(&sysroot_dir, &self.run, sepolicy.as_ref())?;
116111
Ok(self.imgstore.get_or_init(|| imgstore))
117112
}
118113

0 commit comments

Comments
 (0)