fix(core): refuse symlink escapes in the target root
Reject a symlinked path component while writing beneath TargetRoot instead of following it, so a planted symlink cannot redirect an installer write to the host. Cover escaping directories and leaves.
This commit is contained in:
parent
9b5c1155b0
commit
0a6725b049
1 changed files with 63 additions and 0 deletions
|
|
@ -68,7 +68,34 @@ impl TargetRoot {
|
|||
self.resolve(&format!("home/{relative}"))
|
||||
}
|
||||
|
||||
/// Refuse to traverse a symlink while writing beneath the target root.
|
||||
///
|
||||
/// A freshly installed target is created by the installer, but a stage3
|
||||
/// archive (or a later, less careful writer) could leave a symlink whose
|
||||
/// target lies outside the root. Following it would write to the host. The
|
||||
/// write path therefore rejects symlinked components instead of resolving
|
||||
/// them.
|
||||
fn reject_symlink_components(&self, relative: &str) -> Result<()> {
|
||||
let rel = Path::new(relative.trim_start_matches('/'));
|
||||
let mut current = self.root.clone();
|
||||
for component in rel.components() {
|
||||
if let Component::Normal(part) = component {
|
||||
current.push(part);
|
||||
if let Ok(meta) = std::fs::symlink_metadata(¤t) {
|
||||
if meta.file_type().is_symlink() {
|
||||
return Err(CoreError::Validation(format!(
|
||||
"refusing to write through a symlink inside the target: {}",
|
||||
current.display()
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn ensure_dir(&self, relative: &str) -> Result<PathBuf> {
|
||||
self.reject_symlink_components(relative)?;
|
||||
let path = self.resolve(relative)?;
|
||||
std::fs::create_dir_all(&path).map_err(|e| CoreError::io(path.clone(), e))?;
|
||||
Ok(path)
|
||||
|
|
@ -84,6 +111,7 @@ impl TargetRoot {
|
|||
}
|
||||
|
||||
pub fn write(&self, relative: &str, contents: impl AsRef<[u8]>) -> Result<PathBuf> {
|
||||
self.reject_symlink_components(relative)?;
|
||||
let path = self.resolve(relative)?;
|
||||
if let Some(parent) = path.parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| CoreError::io(parent.to_path_buf(), e))?;
|
||||
|
|
@ -140,4 +168,39 @@ mod tests {
|
|||
assert!(p.starts_with(dir.path()));
|
||||
assert_eq!(t.read_to_string("etc/fstab").unwrap(), "hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_to_write_through_an_escaping_symlink() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let outside = tempfile::tempdir().unwrap();
|
||||
let t = TargetRoot::new(dir.path());
|
||||
std::os::unix::fs::symlink(outside.path(), dir.path().join("evil")).unwrap();
|
||||
let err = t.write("evil/pwned", "owned").unwrap_err();
|
||||
assert!(
|
||||
matches!(err, CoreError::Validation(_)),
|
||||
"expected a validation refusal, got {err:?}"
|
||||
);
|
||||
assert!(
|
||||
!outside.path().join("pwned").exists(),
|
||||
"the write escaped the target root"
|
||||
);
|
||||
// ensure_dir must refuse too.
|
||||
assert!(t.ensure_dir("evil/subdir").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_symlinked_leaf() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let outside = tempfile::tempdir().unwrap();
|
||||
std::fs::write(outside.path().join("target"), "original").unwrap();
|
||||
let t = TargetRoot::new(dir.path());
|
||||
std::fs::create_dir_all(dir.path().join("etc")).unwrap();
|
||||
std::os::unix::fs::symlink(outside.path().join("target"), dir.path().join("etc/passwd"))
|
||||
.unwrap();
|
||||
assert!(t.write("etc/passwd", "overwritten").is_err());
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(outside.path().join("target")).unwrap(),
|
||||
"original"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue