From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 689501FF16B for ; Fri, 21 Nov 2025 15:33:08 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 74F1A24239; Fri, 21 Nov 2025 15:33:15 +0100 (CET) From: Robert Obkircher To: pbs-devel@lists.proxmox.com Date: Fri, 21 Nov 2025 15:30:03 +0100 Message-ID: <20251121143255.136721-1-r.obkircher@proxmox.com> X-Mailer: git-send-email 2.47.3 MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1763735559894 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.079 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH v1 proxmox-backup] datastore: fix safety issue when writing FixedIndexHeader X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Backup Server development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pbs-devel-bounces@lists.proxmox.com Sender: "pbs-devel" In the old version the u8 buffer was not guaraneed to be sufficiently aligned to be cast to an 8-byte aligned FixedIndexHeader reference. The pointer was also obtained via Vec::as_ptr, whose result must not be used for mutations according to its documentation. In practice, it is unlikely that these issues caused any problems, because big allocations are usually aligned and because the actual implementation of as_ptr is similar to as_mut_ptr. This change adopts the approach used by the DynamicIndexWriter. It continues to guarantee that the 4096 bytes are not stack allocated. Signed-off-by: Robert Obkircher --- pbs-datastore/src/dynamic_index.rs | 2 ++ pbs-datastore/src/fixed_index.rs | 30 ++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/pbs-datastore/src/dynamic_index.rs b/pbs-datastore/src/dynamic_index.rs index ad49cdf3..37954b56 100644 --- a/pbs-datastore/src/dynamic_index.rs +++ b/pbs-datastore/src/dynamic_index.rs @@ -48,6 +48,8 @@ impl DynamicIndexHeader { pub fn as_bytes(&self) -> &[u8] { unsafe { + // SAFETY: there is no (uninitialized) padding, because the total + // size of all members is equal to the statically asserted size. std::slice::from_raw_parts( self as *const Self as *const u8, std::mem::size_of::(), diff --git a/pbs-datastore/src/fixed_index.rs b/pbs-datastore/src/fixed_index.rs index 6c3be2d4..90fd29fd 100644 --- a/pbs-datastore/src/fixed_index.rs +++ b/pbs-datastore/src/fixed_index.rs @@ -29,6 +29,26 @@ pub struct FixedIndexHeader { } proxmox_lang::static_assert_size!(FixedIndexHeader, 4096); +impl FixedIndexHeader { + /// Convenience method to allocate a zero-initialized header struct. + pub fn zeroed() -> Box { + unsafe { + Box::from_raw(std::alloc::alloc_zeroed(std::alloc::Layout::new::()) as *mut Self) + } + } + + pub fn as_bytes(&self) -> &[u8] { + unsafe { + // SAFETY: there is no (uninitialized) padding, because the total + // size of all members is equal to the statically asserted size. + std::slice::from_raw_parts( + self as *const Self as *const u8, + std::mem::size_of::(), + ) + } + } +} + // split image into fixed size chunks pub struct FixedIndexReader { @@ -237,7 +257,6 @@ impl Drop for FixedIndexWriter { } impl FixedIndexWriter { - #[allow(clippy::cast_ptr_alignment)] // Requires obtaining a shared chunk store lock beforehand pub fn create( store: Arc, @@ -267,18 +286,13 @@ impl FixedIndexWriter { let uuid = Uuid::generate(); - let buffer = vec![0u8; header_size]; - let header = unsafe { &mut *(buffer.as_ptr() as *mut FixedIndexHeader) }; - + let mut header = FixedIndexHeader::zeroed(); header.magic = file_formats::FIXED_SIZED_CHUNK_INDEX_1_0; header.ctime = i64::to_le(ctime); header.size = u64::to_le(size as u64); header.chunk_size = u64::to_le(chunk_size as u64); header.uuid = *uuid.as_bytes(); - - header.index_csum = [0u8; 32]; - - file.write_all(&buffer)?; + file.write_all(header.as_bytes())?; let index_length = size.div_ceil(chunk_size); let index_size = index_length * 32; -- 2.47.3 _______________________________________________ pbs-devel mailing list pbs-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel