Refuse local copies whose destination is the source
Copying a capture into its own directory resolved the destination to the source's own path: a growing cap.pcap.partial was truncated to zero when the receiver opened its in-flight file, and a finished capture was silently replaced by its (possibly filtered) copy. Compare the source and its final name against both destination paths by inode, or by canonical parent + name for files that don't exist yet, and bail before anything is opened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f21b6c53e8
commit
6a80aaf461
1 file changed
+106
-1
+106
-1
@@ -7,7 +7,8 @@ use crate::protocol::{MessageSink, MessageSource, WireSink, WireSource};
|
||||
use crate::ssh::{build_server_command, spawn_remote_server};
|
||||
use crate::tracelog::Logger;
|
||||
use anyhow::{bail, Result};
|
||||
use std::path::PathBuf;
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{mpsc, Arc};
|
||||
use std::thread;
|
||||
|
||||
@@ -63,6 +64,10 @@ pub fn run(cli: ClientCli) -> Result<()> {
|
||||
|
||||
match (&source_target, &dest_target) {
|
||||
(Target::Local(src), Target::Local(_)) => {
|
||||
refuse_overlap(
|
||||
[Path::new(src), Path::new(&final_source_path)],
|
||||
[Path::new(&dest.temp_path), Path::new(&dest.final_path)],
|
||||
)?;
|
||||
let send_opts = SendOptions {
|
||||
source_path: PathBuf::from(src),
|
||||
is_partial,
|
||||
@@ -178,3 +183,103 @@ pub fn run(cli: ClientCli) -> Result<()> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Refuses a local copy whose destination lands on the source. The receiver
|
||||
/// truncates its in-flight file on open and renames over its final one, so
|
||||
/// either collision destroys the capture being read. Copying a capture into
|
||||
/// its own directory is enough to hit it: `dir/cap.pcap.partial` to `dir/`
|
||||
/// resolves to `dir/cap.pcap`, written via `dir/cap.pcap.partial`.
|
||||
///
|
||||
/// `sources` is the source as given plus its post-rename final path, since a
|
||||
/// growing source moves from one to the other mid-transfer.
|
||||
fn refuse_overlap(sources: [&Path; 2], dests: [&Path; 2]) -> Result<()> {
|
||||
for source in sources {
|
||||
for dest in dests {
|
||||
if same_location(source, dest) {
|
||||
bail!(
|
||||
"destination {} is the source {}: copying would overwrite the capture \
|
||||
being read. Copy it to a different directory or name.",
|
||||
dest.display(),
|
||||
source.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Whether two paths name the same file, whether or not it exists yet: the
|
||||
/// same inode when both exist (catching hard links and symlinks), otherwise
|
||||
/// the same canonical parent directory and file name.
|
||||
fn same_location(a: &Path, b: &Path) -> bool {
|
||||
if let (Ok(ma), Ok(mb)) = (std::fs::metadata(a), std::fs::metadata(b)) {
|
||||
return ma.dev() == mb.dev() && ma.ino() == mb.ino();
|
||||
}
|
||||
match (resolve(a), resolve(b)) {
|
||||
(Some(ra), Some(rb)) => ra == rb,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// The canonical form of `path`, or for a file that doesn't exist yet, its
|
||||
/// canonical parent joined with its name. `None` when even the parent
|
||||
/// doesn't exist, in which case nothing can collide with it.
|
||||
fn resolve(path: &Path) -> Option<PathBuf> {
|
||||
if let Ok(canonical) = std::fs::canonicalize(path) {
|
||||
return Some(canonical);
|
||||
}
|
||||
let parent = match path.parent() {
|
||||
Some(p) if !p.as_os_str().is_empty() => p,
|
||||
_ => Path::new("."),
|
||||
};
|
||||
Some(std::fs::canonicalize(parent).ok()?.join(path.file_name()?))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn copying_a_growing_capture_into_its_own_directory_is_refused() {
|
||||
let dir = tempdir().unwrap();
|
||||
let partial = dir.path().join("cap.pcap.partial");
|
||||
std::fs::write(&partial, b"live").unwrap();
|
||||
let final_src = dir.path().join("cap.pcap");
|
||||
// What `resolve_dest_paths` makes of DEST = the source's directory.
|
||||
let err = refuse_overlap([&partial, &final_src], [&partial, &final_src]).unwrap_err();
|
||||
assert!(err.to_string().contains("overwrite the capture"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symlinked_or_relative_spelling_of_the_source_is_still_caught() {
|
||||
let dir = tempdir().unwrap();
|
||||
let real = dir.path().join("real");
|
||||
std::fs::create_dir(&real).unwrap();
|
||||
let src = real.join("cap.pcap");
|
||||
std::fs::write(&src, b"done").unwrap();
|
||||
let link = dir.path().join("link");
|
||||
std::os::unix::fs::symlink(&real, &link).unwrap();
|
||||
let via_link = link.join("cap.pcap");
|
||||
let via_dotdot = real.join("..").join("real").join("cap.pcap.partial");
|
||||
assert!(refuse_overlap([&src, &src], [&via_dotdot, &via_link]).is_err());
|
||||
// Neither exists yet: compared by canonical parent + name.
|
||||
let not_yet = real.join("other.pcap");
|
||||
assert!(refuse_overlap([¬_yet, ¬_yet], [&link.join("other.pcap"), &via_dotdot]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_different_destination_is_allowed() {
|
||||
let dir = tempdir().unwrap();
|
||||
let src = dir.path().join("cap.pcap.partial");
|
||||
std::fs::write(&src, b"live").unwrap();
|
||||
let out = dir.path().join("out");
|
||||
std::fs::create_dir(&out).unwrap();
|
||||
let final_src = dir.path().join("cap.pcap");
|
||||
let dests = [out.join("cap.pcap.partial"), out.join("cap.pcap")];
|
||||
assert!(refuse_overlap([&src, &final_src], [&dests[0], &dests[1]]).is_ok());
|
||||
// A destination under a directory that doesn't exist can't collide.
|
||||
let missing = dir.path().join("nope").join("cap.pcap");
|
||||
assert!(refuse_overlap([&src, &final_src], [&missing, &missing]).is_ok());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user