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 CD7B01FF0E4 for ; Tue, 11 Aug 2026 17:03:16 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 9C61E21570; Tue, 11 Aug 2026 17:03:16 +0200 (CEST) From: Robert Obkircher To: pbs-devel@lists.proxmox.com Subject: [PATCH v1 proxmox 3/6] io: avoid potential null dereference and memory leak on error path Date: Tue, 11 Aug 2026 17:02:02 +0200 Message-ID: <20260811150237.527116-4-r.obkircher@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260811150237.527116-1-r.obkircher@proxmox.com> References: <20260811150237.527116-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: 1786460576012 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.825 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_MED -2.3 Sender listed at https://www.dnswl.org/, medium 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: LHB56QHK645U4LXTMHT5ITKLFUXHUWNV X-Message-ID-Hash: LHB56QHK645U4LXTMHT5ITKLFUXHUWNV 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: Note that using uninitialized memory is still highly unsafe, because reading from it can cause miscompilations. The remaining uses should be cleaned up eventually, possibly when a better API like read_buf is stabilized. Link: https://godbolt.org/z/3xTnnYxnj Signed-off-by: Robert Obkircher --- proxmox-io/src/read.rs | 12 ++++++------ proxmox-io/src/vec/mod.rs | 22 ++++++++++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/proxmox-io/src/read.rs b/proxmox-io/src/read.rs index 67a608dc..0a73eea8 100644 --- a/proxmox-io/src/read.rs +++ b/proxmox-io/src/read.rs @@ -285,15 +285,15 @@ impl ReadExt for R { } unsafe fn read_host_value_boxed(&mut self) -> io::Result> { - // FIXME: Change this once #![feature(new_uninit)] lands for Box! - unsafe { - let ptr = std::alloc::alloc(std::alloc::Layout::new::()) as *mut T; + let mut result = Box::::new_uninit(); + // If this reads uninitialized memory from result or lies about + // having written to it that would be undefined behavior. self.read_exact(std::slice::from_raw_parts_mut( - ptr as *mut u8, - mem::size_of::(), + result.as_mut_ptr().cast::(), + size_of::(), ))?; - Ok(Box::from_raw(ptr)) + Ok(result.assume_init()) } } diff --git a/proxmox-io/src/vec/mod.rs b/proxmox-io/src/vec/mod.rs index 7fe106e8..e4cc9892 100644 --- a/proxmox-io/src/vec/mod.rs +++ b/proxmox-io/src/vec/mod.rs @@ -47,13 +47,27 @@ pub use byte_vec::ByteVecExt; /// /// # Safety /// -/// It's generally not unsafe to use this method, but the contents are uninitialized, and since -/// this does not return a `MaybeUninit` type to track the initialization state, this is simply -/// marked as unsafe for good measure. +/// It is unsafe to use this method because reading uninitialized memory is +/// undefined behavior and allows the compiler to do anything. It can delete +/// code-paths leading to such reads and optimize (x == x + 1) to true. +/// +/// The following example prints "hello 0 0" with opt-level=3: +/// ```rust +/// unsafe { +/// let ptr = std::alloc::alloc(std::alloc::Layout::array::(42).unwrap()); +/// let data = Box::from_raw(std::ptr::slice_from_raw_parts_mut(ptr, 42)); +/// let x = data[0]; +/// println!("hello {} {}", x, x + 1); // prints hello 0 0 +/// } +/// ``` #[inline] pub unsafe fn uninitialized(len: usize) -> Vec { unsafe { - let data = std::alloc::alloc(std::alloc::Layout::array::(len).unwrap()); + let layout = std::alloc::Layout::array::(len).unwrap(); + let data = std::alloc::alloc(layout); + if data.is_null() { + std::alloc::handle_alloc_error(layout); + } Vec::from_raw_parts(data, len, len) } } -- 2.47.3