From 7eacdc765b1d48606087f7432527c4be5fab37de Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Mon, 8 Jun 2020 15:02:52 +0200 Subject: [PATCH] pxar: split assert_relative_path the check for a single component is only required in the dir stack atm Signed-off-by: Wolfgang Bumiller --- src/pxar/create.rs | 4 ++-- src/pxar/dir_stack.rs | 4 ++-- src/pxar/tools.rs | 11 +++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/pxar/create.rs b/src/pxar/create.rs index d803189a..7fbcae40 100644 --- a/src/pxar/create.rs +++ b/src/pxar/create.rs @@ -21,7 +21,7 @@ use proxmox::tools::fd::RawFdNum; use crate::pxar::catalog::BackupCatalogWriter; use crate::pxar::flags; -use crate::pxar::tools::assert_relative_path; +use crate::pxar::tools::assert_single_path_component; use crate::tools::{acl, fs, xattr, Fd}; fn detect_fs_type(fd: RawFd) -> Result { @@ -230,7 +230,7 @@ impl<'a, 'b> Archiver<'a, 'b> { } let os_file_name = OsStr::from_bytes(file_name_bytes); - assert_relative_path(os_file_name)?; + assert_single_path_component(os_file_name)?; let full_path = self.path.join(os_file_name); let stat = match nix::sys::stat::fstatat( diff --git a/src/pxar/dir_stack.rs b/src/pxar/dir_stack.rs index d1e10f6c..3dba7de5 100644 --- a/src/pxar/dir_stack.rs +++ b/src/pxar/dir_stack.rs @@ -10,7 +10,7 @@ use nix::sys::stat::{mkdirat, Mode}; use proxmox::sys::error::SysError; use pxar::Metadata; -use crate::pxar::tools::{assert_relative_path, perms_from_metadata}; +use crate::pxar::tools::{assert_single_path_component, perms_from_metadata}; pub struct PxarDir { file_name: OsString, @@ -95,7 +95,7 @@ impl PxarDirStack { } pub fn push(&mut self, file_name: OsString, metadata: Metadata) -> Result<(), Error> { - assert_relative_path(&file_name)?; + assert_single_path_component(&file_name)?; self.path.push(&file_name); self.dirs.push(PxarDir::new(file_name, metadata)); Ok(()) diff --git a/src/pxar/tools.rs b/src/pxar/tools.rs index 371ef766..ec5c13b2 100644 --- a/src/pxar/tools.rs +++ b/src/pxar/tools.rs @@ -24,11 +24,22 @@ pub fn assert_relative_path + ?Sized>(path: &S) -> Result<(), Er assert_relative_path_do(Path::new(path)) } +/// Make sure path is a single component and not '.' or '..'. +pub fn assert_single_path_component + ?Sized>(path: &S) -> Result<(), Error> { + assert_single_path_component_do(Path::new(path)) +} + fn assert_relative_path_do(path: &Path) -> Result<(), Error> { if !path.is_relative() { bail!("bad absolute file name in archive: {:?}", path); } + Ok(()) +} + +fn assert_single_path_component_do(path: &Path) -> Result<(), Error> { + assert_relative_path_do(path)?; + let mut components = path.components(); match components.next() { Some(std::path::Component::Normal(_)) => (),