From 7a5861f32f085437899400336db3117643223fbd Mon Sep 17 00:00:00 2001 From: ChristianVisintin Date: Mon, 8 Mar 2021 15:27:53 +0100 Subject: [PATCH] prevent infinite loops while performing `stat` on symbolic links pointing to themselves --- src/filetransfer/scp_transfer.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/filetransfer/scp_transfer.rs b/src/filetransfer/scp_transfer.rs index 3367036..6452fbd 100644 --- a/src/filetransfer/scp_transfer.rs +++ b/src/filetransfer/scp_transfer.rs @@ -151,12 +151,19 @@ impl ScpFileTransfer { if let Some(symlink_path) = symlink_path.as_ref() { is_dir = symlink_path.is_dir(); } - // Get symlink + // Get symlink; PATH mustn't be equal to filename let symlink: Option> = match symlink_path { None => None, - Some(p) => match self.stat(p.as_path()) { - Ok(e) => Some(Box::new(e)), - Err(_) => None, // Ignore errors + Some(p) => match p.file_name().unwrap_or(&std::ffi::OsStr::new("")) + == file_name.as_str() + { + // If name is equal, don't stat path; otherwise it would get stuck + true => None, + false => match self.stat(p.as_path()) { + // If path match filename + Ok(e) => Some(Box::new(e)), + Err(_) => None, // Ignore errors + }, }, }; // Check if file_name is '.' or '..'