diff options
| author | Hermes Agent <hermes@localhost> | 2026-08-12 05:12:40 +0000 |
|---|---|---|
| committer | Hermes Agent <hermes@localhost> | 2026-08-12 05:12:40 +0000 |
| commit | 3a47e14cbe6e15de0758c8bd4233a8d84ff80aa8 (patch) | |
| tree | 3822eca08cfd2201e3c532187054bce75109f1c2 /src/archive.rs | |
| parent | 251509d734a635f8e6448cef0925d881d4f5f184 (diff) | |
Reject archive outputs inside rootfs
Diffstat (limited to 'src/archive.rs')
| -rw-r--r-- | src/archive.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/archive.rs b/src/archive.rs index 9a899bf..ad424c4 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -26,6 +26,7 @@ impl NativeTarWriter { } let output = output.as_ref(); + reject_output_inside_rootfs(rootfs, output)?; let temporary = temporary_output_path(output)?; let result = (|| { let file = OpenOptions::new() @@ -53,6 +54,24 @@ impl NativeTarWriter { } } +fn reject_output_inside_rootfs(rootfs: &Path, output: &Path) -> Result<()> { + let rootfs = rootfs + .canonicalize() + .with_context(|| format!("canonicalize rootfs {}", rootfs.display()))?; + let output_parent = output.parent().unwrap_or_else(|| Path::new(".")); + let output_parent = output_parent + .canonicalize() + .with_context(|| format!("canonicalize archive output parent {}", output_parent.display()))?; + if output_parent.starts_with(&rootfs) { + bail!( + "archive output must not be inside rootfs: {} is below {}", + output.display(), + rootfs.display() + ); + } + Ok(()) +} + fn temporary_output_path(output: &Path) -> Result<PathBuf> { let name = output .file_name() |