-
Notifications
You must be signed in to change notification settings - Fork 177
lsm: Use walk API with noxdev for recursive SELinux relabeling #2045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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 => { | ||
|
|
@@ -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() | ||
| .path_base(path_for_read.as_std_path()); | ||
|
|
||
| root.open_dir(path_for_read)? | ||
cgwalters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .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(())); | ||
| } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
cgwalters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| 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}"); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.