Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ anstream = "1.0"
anyhow = "1.0.82"
camino = "1.1.6"
canon-json = "0.2.1"
cap-std-ext = "5.0.0"
cap-std-ext = "5.1.1"
cfg-if = "1.0"
chrono = { version = "0.4.38", default-features = false }
clap = "4.5.4"
Expand Down Expand Up @@ -121,3 +121,4 @@ todo = "deny"
# to trigger, and among the least valuable to fix.
needless_borrow = "allow"
needless_borrows_for_generic_args = "allow"

71 changes: 48 additions & 23 deletions crates/lib/src/lsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,21 @@ pub(crate) fn relabel_recurse(
relabel_recurse_inner(root, &mut path, as_path.as_mut(), policy)
}

/// A wrapper for creating a directory, also optionally setting a SELinux label.
/// The provided `skip` parameter is a device/inode that we will ignore (and not traverse).
/// Recursively ensure all files under a directory have SELinux labels.
/// Uses the `walk` API with `noxdev` and `skip_mountpoints` to avoid crossing
/// mount point boundaries
/// (e.g. into sysfs, procfs, etc.).
/// The provided `skip` parameter is a device/inode pair that we will ignore
/// (and not traverse into).
pub(crate) fn ensure_dir_labeled_recurse(
root: &Dir,
path: &mut Utf8PathBuf,
policy: &ostree::SePolicy,
skip: Option<(libc::dev_t, libc::ino64_t)>,
) -> Result<()> {
use cap_std_ext::dirext::WalkConfiguration;
use std::ops::ControlFlow;

// Juggle the cap-std requirement for relative paths vs the libselinux
// requirement for absolute paths by special casing the empty string "" as "."
// just for the initial directory enumeration.
Expand All @@ -406,6 +413,7 @@ pub(crate) fn ensure_dir_labeled_recurse(

let mut n = 0u64;

// Label the starting directory itself; the walk API only visits children.
let metadata = root.symlink_metadata(path_for_read)?;
match ensure_labeled(root, path, &metadata, policy)? {
SELinuxLabelState::Unlabeled => {
Expand All @@ -414,35 +422,52 @@ pub(crate) fn ensure_dir_labeled_recurse(
SELinuxLabelState::Unsupported => return Ok(()),
SELinuxLabelState::Labeled => {}
}

for ent in root.read_dir(path_for_read)? {
let ent = ent?;
let metadata = ent.metadata()?;
if let Some((skip_dev, skip_ino)) = skip.as_ref().copied() {
if (metadata.dev(), metadata.ino()) == (skip_dev, skip_ino) {
tracing::debug!("Skipping dev={skip_dev} inode={skip_ino}");
continue;
let config = WalkConfiguration::default()
.noxdev()
.skip_mountpoints()
Comment on lines +426 to +427
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obviously fine but don't we only need the skip_mountpoints? Conceptually it's a more restrictive noxdev I believe

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's true, it doesn't hurt anything like that but I also don't see how you could hit the noxdev case because you would hit the mountpoint case first.

.path_base(path_for_read.as_std_path());

root.open_dir(path_for_read)?
.walk::<_, anyhow::Error>(&config, |component| {
let metadata = component.entry.metadata()?;

// Check if this entry should be skipped
if let Some((skip_dev, skip_ino)) = skip {
if (metadata.dev(), metadata.ino()) == (skip_dev, skip_ino) {
tracing::debug!("Skipping dev={skip_dev} inode={skip_ino}");
// For directories, Break skips traversal into the directory
// but continues with the next sibling. For non-directories,
// Break would skip all remaining siblings, so use Continue
// to skip only this entry.
if component.file_type.is_dir() {
return Ok(ControlFlow::Break(()));
} else {
return Ok(ControlFlow::Continue(()));
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is just wrong let me know but that's how I understand it to work from the docs.

}
}
}
let name = ent.file_name();
let name = name
.to_str()
.ok_or_else(|| anyhow::anyhow!("Invalid non-UTF-8 filename: {name:?}"))?;
path.push(name);

if metadata.is_dir() {
ensure_dir_labeled_recurse(root, path, policy, skip)?;
} else {
let path = Utf8Path::from_path(component.path)
.ok_or_else(|| anyhow::anyhow!("Invalid non-UTF-8 path: {:?}", component.path))?;

match ensure_labeled(root, path, &metadata, policy)? {
SELinuxLabelState::Unlabeled => {
n += 1;
}
SELinuxLabelState::Unsupported => break,
// We check for Unsupported on the starting directory above,
// and the walk uses noxdev + skip_mountpoints to stay on
// the same filesystem, so hitting Unsupported here is
// unexpected.
SELinuxLabelState::Unsupported => {
anyhow::bail!(
"Unexpected SELinuxLabelState::Unsupported during walk at {path}"
);
}
SELinuxLabelState::Labeled => {}
}
}
path.pop();
}

Ok(ControlFlow::Continue(()))
})?;

if n > 0 {
tracing::debug!("Relabeled {n} objects in {path}");
Expand Down