From ca9e5044a1678a3ff33f13582bd6498632113c3e Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 12 Aug 2026 04:26:44 +0000 Subject: Prevent rootfs copy from following symlinks --- src/rootfs.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/rootfs.rs b/src/rootfs.rs index 9246aa8..ea7d87b 100644 --- a/src/rootfs.rs +++ b/src/rootfs.rs @@ -173,8 +173,7 @@ fn copy_entry( if let Some(parent) = output.parent() { fs::create_dir_all(parent)?; } - fs::copy(source, &output) - .with_context(|| format!("copy {} to {}", source.display(), output.display()))?; + replace_with_file(source, &output)?; fs::set_permissions(&output, metadata.permissions())?; manifest.record(destination); } else { @@ -213,6 +212,19 @@ fn enable_service(rootfs: &Path, service: &ServiceName, manifest: &mut MutationM Ok(()) } +fn replace_with_file(source: &Path, output: &Path) -> Result<()> { + match fs::symlink_metadata(output) { + Ok(metadata) if metadata.file_type().is_dir() => { + bail!("cannot replace directory with file: {}", output.display()); + } + Ok(_) => fs::remove_file(output).with_context(|| format!("replace {}", output.display()))?, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => return Err(error).with_context(|| format!("inspect {}", output.display())), + } + fs::copy(source, output).with_context(|| format!("copy {} to {}", source.display(), output.display()))?; + Ok(()) +} + fn replace_with_symlink(target: &Path, output: &Path) -> Result<()> { match fs::symlink_metadata(output) { Ok(metadata) if metadata.file_type().is_dir() => { -- cgit v1.2.3