From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id BF9D01FF0ED for ; Fri, 31 Jul 2026 16:25:21 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 8367921568; Fri, 31 Jul 2026 16:25:20 +0200 (CEST) From: Robert Obkircher To: pbs-devel@lists.proxmox.com Subject: [PATCH proxmox-backup 6/9] datastore: check that index files are regular files Date: Fri, 31 Jul 2026 16:21:19 +0200 Message-ID: <20260731142430.289893-7-r.obkircher@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260731142430.289893-1-r.obkircher@proxmox.com> References: <20260731142430.289893-1-r.obkircher@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1785507905468 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.142 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_LOW -0.7 Sender listed at https://www.dnswl.org/, low trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: JBZ3DF3WF4LF6BSTFXODICWM6IYQH2ZB X-Message-ID-Hash: JBZ3DF3WF4LF6BSTFXODICWM6IYQH2ZB X-MailFrom: r.obkircher@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Backup Server development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Return an explicit error message when trying to mmap something other than a regular file. Previously, this was caught implicitly by the non-zero size check or by a failing seek. Signed-off-by: Robert Obkircher --- pbs-datastore/src/dynamic_index.rs | 16 ++++++++-------- pbs-datastore/src/fixed_index.rs | 15 ++++++++------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pbs-datastore/src/dynamic_index.rs b/pbs-datastore/src/dynamic_index.rs index 7ee8bdcab..dd0b86f3f 100644 --- a/pbs-datastore/src/dynamic_index.rs +++ b/pbs-datastore/src/dynamic_index.rs @@ -8,6 +8,7 @@ use std::sync::{Arc, Mutex}; use std::task::Context; use anyhow::{Error, bail, format_err}; +use nix::sys::stat::{SFlag, fstat}; use proxmox_io::ReadExt; use proxmox_sys::mmap::Mmap; @@ -96,17 +97,16 @@ impl DynamicIndexReader { } pub fn new(mut file: std::fs::File) -> Result { + let stat = fstat(file.as_raw_fd()).map_err(|e| format_err!("fstat failed - {e}"))?; + if (stat.st_mode & SFlag::S_IFMT.bits()) != SFlag::S_IFREG.bits() { + bail!("not a regular file"); + } + // FIXME: This is NOT OUR job! Check the callers of this method and remove this! file.seek(SeekFrom::Start(0))?; let header_size = std::mem::size_of::(); - let rawfd = file.as_raw_fd(); - let stat = match nix::sys::stat::fstat(rawfd) { - Ok(stat) => stat, - Err(err) => bail!("fstat failed - {}", err), - }; - let size = stat.st_size as usize; if size < header_size { @@ -611,11 +611,11 @@ mod tests { check_error_contains( DynamicIndexReader::open(Path::new("/dev/stdin")), - "Illegal seek", + "not a regular file", ); check_error_contains( DynamicIndexReader::open(Path::new("/dev/zero")), - "index too small (0)", + "not a regular file", ); let path = dir.path().join("file"); diff --git a/pbs-datastore/src/fixed_index.rs b/pbs-datastore/src/fixed_index.rs index 0145f93d3..1d3d0170a 100644 --- a/pbs-datastore/src/fixed_index.rs +++ b/pbs-datastore/src/fixed_index.rs @@ -5,6 +5,7 @@ use std::path::{Path, PathBuf}; use std::ptr::NonNull; use anyhow::{Context, Error, bail, format_err}; +use nix::sys::stat::{SFlag, fstat}; use proxmox_io::ReadExt; use proxmox_uuid::Uuid; @@ -60,15 +61,15 @@ impl FixedIndexReader { } pub fn new(mut file: std::fs::File) -> Result { + let stat = fstat(file.as_raw_fd()).map_err(|e| format_err!("fstat failed - {e}"))?; + if (stat.st_mode & SFlag::S_IFMT.bits()) != SFlag::S_IFREG.bits() { + bail!("not a regular file"); + } + file.seek(SeekFrom::Start(0))?; let header_size = std::mem::size_of::(); - let stat = match nix::sys::stat::fstat(file.as_raw_fd()) { - Ok(stat) => stat, - Err(err) => bail!("fstat failed - {}", err), - }; - let size = stat.st_size as usize; if size < header_size { @@ -608,11 +609,11 @@ mod tests { check_error_contains( FixedIndexReader::open(Path::new("/dev/stdin")), - "Illegal seek", + "not a regular file", ); check_error_contains( FixedIndexReader::open(Path::new("/dev/zero")), - "index too small (0)", + "not a regular file", ); let path = dir.path().join("file"); -- 2.47.3