all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Robert Obkircher <r.obkircher@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH v1 proxmox 1/6] uuid: avoid potential null dereference and memory leaks
Date: Tue, 11 Aug 2026 17:02:00 +0200	[thread overview]
Message-ID: <20260811150237.527116-2-r.obkircher@proxmox.com> (raw)
In-Reply-To: <20260811150237.527116-1-r.obkircher@proxmox.com>

Avoid null pointer dereferences on allocation failures and do not leak
memory on the error paths.

I'm also not fully convinced that writing to uninitialized memory via
assingment instead of ptr::write was guaranteed to be safe, but Miri
doesn't complain about it. See the link for some additional context.

Link: https://github.com/rust-lang/unsafe-code-guidelines/issues/346
Signed-off-by: Robert Obkircher <r.obkircher@proxmox.com>
---
 proxmox-uuid/src/lib.rs | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/proxmox-uuid/src/lib.rs b/proxmox-uuid/src/lib.rs
index f2658ec6..59750a3a 100644
--- a/proxmox-uuid/src/lib.rs
+++ b/proxmox-uuid/src/lib.rs
@@ -61,10 +61,9 @@ pub struct Uuid(Box<[u8; 16]>);
 impl Uuid {
     /// Generate a uuid with `uuid_generate(3)`.
     pub fn generate() -> Self {
-        use std::alloc::{Layout, alloc};
-        let uuid = unsafe { alloc(Layout::new::<[u8; 16]>()) as *mut [u8; 16] };
-        unsafe { uuid_generate(uuid) };
-        Self(unsafe { Box::from_raw(uuid) })
+        let mut uuid = Box::new_uninit();
+        unsafe { uuid_generate(uuid.as_mut_ptr()) };
+        Self(unsafe { uuid.assume_init() })
     }
 
     /// Get a reference to the internal 16 byte array.
@@ -92,12 +91,10 @@ impl Uuid {
     /// assert_eq!(uuid1, uuid2);
     /// ```
     pub fn parse_str(src: &str) -> Result<Self, UuidError> {
-        use std::alloc::{Layout, alloc};
-        let uuid: *mut [u8; 16] = unsafe { alloc(Layout::new::<[u8; 16]>()) as *mut [u8; 16] };
+        let mut uuid = [0; 16];
         if src.len() == 36 {
             // Unfortunately the manpage of `uuid_parse(3)` states that it technically requires a
             // terminating null byte at the end, which we don't have, so do this manually:
-            let uuid: &mut [u8] = unsafe { &mut (&mut *uuid)[..] };
             let src = src.as_bytes();
             if src[8] != b'-' || src[13] != b'-' || src[18] != b'-' || src[23] != b'-' {
                 return Err(UuidError);
@@ -118,7 +115,6 @@ impl Uuid {
                 uuid[i] = (hex_digit(src[2 * i + 4])? << 4) | hex_digit(src[2 * i + 5])?;
             }
         } else if src.len() == 32 {
-            let uuid: &mut [u8] = unsafe { &mut (&mut *uuid)[..] };
             let src = src.as_bytes();
             for i in 0..16 {
                 uuid[i] = (hex_digit(src[2 * i])? << 4) | hex_digit(src[2 * i + 1])?;
@@ -126,7 +122,7 @@ impl Uuid {
         } else {
             return Err(UuidError);
         }
-        Ok(Self(unsafe { Box::from_raw(uuid) }))
+        Ok(Self(Box::new(uuid)))
     }
 }
 
-- 
2.47.3





  reply	other threads:[~2026-08-11 15:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-11 15:01 [PATCH v1 proxmox 0/6] uninitialized memory allocations fixes Robert Obkircher
2026-08-11 15:02 ` Robert Obkircher [this message]
2026-08-11 15:02 ` [PATCH v1 proxmox 2/6] io: request zeroed memory instead of manually clearing it Robert Obkircher
2026-08-11 15:02 ` [PATCH v1 proxmox 3/6] io: avoid potential null dereference and memory leak on error path Robert Obkircher
2026-08-11 15:02 ` [PATCH v1 proxmox 4/6] io: remove boxed::uninitialized because it is unsound Robert Obkircher
2026-08-11 15:02 ` [PATCH v1 proxmox 5/6] io: remove unused append_to_vec functions Robert Obkircher
2026-08-11 15:02 ` [PATCH v1 proxmox 6/6] io: remove unused ByteVecExt trait with grow_ and resize_uninitialized Robert Obkircher

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260811150237.527116-2-r.obkircher@proxmox.com \
    --to=r.obkircher@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal