-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(sandbox): skip read-only mounts during recursive chown of /sandbox #2341
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
base: main
Are you sure you want to change the base?
Changes from all commits
90fae13
4cd9059
3d079d5
888f91a
0869591
2250cb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1168,10 +1168,12 @@ fn rewrite_group_at(path: &Path, gid: &str) -> Result<()> { | |
| /// Symlinks are skipped (not followed) to prevent privilege escalation via | ||
| /// malicious container images. The TOCTOU window is not exploitable because | ||
| /// no untrusted process is running yet. | ||
| /// | ||
| /// `EROFS` errors from `chown` are logged at debug level and the walk still | ||
| /// recurses into children (a read-only directory may contain writable submounts | ||
| /// or siblings that must be owned by the sandbox user). | ||
| #[cfg(unix)] | ||
| fn chown_sandbox_home(root: &Path, uid: Option<Uid>, gid: Option<Gid>) -> Result<()> { | ||
| use nix::unistd::chown; | ||
|
|
||
| let meta = std::fs::symlink_metadata(root).into_diagnostic()?; | ||
| if meta.file_type().is_symlink() { | ||
| return Err(miette::miette!( | ||
|
|
@@ -1180,22 +1182,38 @@ fn chown_sandbox_home(root: &Path, uid: Option<Uid>, gid: Option<Gid>) -> Result | |
| )); | ||
| } | ||
|
|
||
| chown(root, uid, gid).into_diagnostic()?; | ||
| chown_recursive(root, uid, gid, &nix::unistd::chown) | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| fn chown_recursive( | ||
| path: &Path, | ||
| uid: Option<Uid>, | ||
| gid: Option<Gid>, | ||
| do_chown: &impl Fn(&Path, Option<Uid>, Option<Gid>) -> nix::Result<()>, | ||
| ) -> Result<()> { | ||
| let meta = std::fs::symlink_metadata(path).into_diagnostic()?; | ||
|
|
||
| if meta.file_type().is_symlink() { | ||
| debug!(path = %path.display(), "Skipping symlink during sandbox home chown"); | ||
| return Ok(()); | ||
| } | ||
|
|
||
| if let Err(e) = do_chown(path, uid, gid) { | ||
|
Collaborator
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.
High: CWE-367/CWE-59: |
||
| if e == nix::errno::Errno::EROFS { | ||
|
varshaprasad96 marked this conversation as resolved.
|
||
| debug!(path = %path.display(), "Skipping read-only path during sandbox home chown"); | ||
| } else { | ||
| return Err(e).into_diagnostic(); | ||
| } | ||
| } | ||
|
|
||
| if meta.is_dir() | ||
| && let Ok(entries) = std::fs::read_dir(root) | ||
| && let Ok(entries) = std::fs::read_dir(path) | ||
| { | ||
| for entry in entries { | ||
| let entry = entry.into_diagnostic()?; | ||
| let path = entry.path(); | ||
| if path | ||
| .symlink_metadata() | ||
| .is_ok_and(|m| m.file_type().is_symlink()) | ||
| { | ||
| debug!(path = %path.display(), "Skipping symlink during sandbox home chown"); | ||
| continue; | ||
| } | ||
| chown_sandbox_home(&path, uid, gid)?; | ||
| let child = entry.path(); | ||
| chown_recursive(&child, uid, gid, do_chown)?; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2115,6 +2133,69 @@ mod tests { | |
| .expect("should skip symlink children without error"); | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| #[test] | ||
| fn chown_recursive_skips_erofs_and_continues() { | ||
| use std::sync::{Arc, Mutex}; | ||
|
|
||
| let dir = tempfile::tempdir().unwrap(); | ||
| let root = dir.path().join("sandbox"); | ||
| std::fs::create_dir(&root).unwrap(); | ||
|
|
||
| let readonly_dir = root.join("ro-mount"); | ||
| std::fs::create_dir(&readonly_dir).unwrap(); | ||
| std::fs::write(readonly_dir.join("writable-child.txt"), "data").unwrap(); | ||
|
|
||
| std::fs::write(root.join("writable-sibling.txt"), "data").unwrap(); | ||
|
|
||
| let uid = Some(nix::unistd::geteuid()); | ||
| let gid = Some(nix::unistd::getegid()); | ||
|
|
||
| let chowned: Arc<Mutex<Vec<PathBuf>>> = Arc::new(Mutex::new(Vec::new())); | ||
| let chowned_ref = Arc::clone(&chowned); | ||
|
|
||
| let readonly_dir_clone = readonly_dir.clone(); | ||
| let fake_chown = | ||
| move |path: &Path, _uid: Option<Uid>, _gid: Option<Gid>| -> nix::Result<()> { | ||
| if path == readonly_dir_clone { | ||
| return Err(nix::errno::Errno::EROFS); | ||
| } | ||
| chowned_ref.lock().unwrap().push(path.to_path_buf()); | ||
| Ok(()) | ||
| }; | ||
|
|
||
| chown_recursive(&root, uid, gid, &fake_chown).expect("EROFS should be handled gracefully"); | ||
|
|
||
| let chowned = chowned.lock().unwrap(); | ||
| assert!(chowned.contains(&root), "root should have been chowned"); | ||
| assert!( | ||
| chowned.contains(&readonly_dir.join("writable-child.txt")), | ||
| "writable child under EROFS directory should still be chowned" | ||
| ); | ||
| assert!( | ||
| chowned.contains(&root.join("writable-sibling.txt")), | ||
| "writable sibling should still be chowned" | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| #[test] | ||
| fn chown_recursive_propagates_non_erofs_errors() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let root = dir.path().join("sandbox"); | ||
| std::fs::create_dir(&root).unwrap(); | ||
|
|
||
| let uid = Some(nix::unistd::geteuid()); | ||
| let gid = Some(nix::unistd::getegid()); | ||
|
|
||
| let fake_chown = |_path: &Path, _uid: Option<Uid>, _gid: Option<Gid>| -> nix::Result<()> { | ||
| Err(nix::errno::Errno::EPERM) | ||
| }; | ||
|
|
||
| let result = chown_recursive(&root, uid, gid, &fake_chown); | ||
| assert!(result.is_err(), "non-EROFS errors should propagate"); | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| #[test] | ||
| fn rewrite_passwd_modifies_existing_sandbox_entry() { | ||
|
|
||
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.
Can we move the symlink check that is currently performed on
childin the loop below to here?Uh oh!
There was an error while loading. Please reload this page.
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.
Done in 4cd9059. Moved the symlink check into the top of
chown_recursive(reusing thesymlink_metadatacall already there), which also eliminated the redundant per-childsymlink_metadatain the loop