rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: alloc: fix dangling pointer in VecExt<T>::reserve()
@ 2024-04-29 19:24 Danilo Krummrich
  2024-04-29 19:52 ` Boqun Feng
  2024-04-30  8:25 ` Alice Ryhl
  0 siblings, 2 replies; 15+ messages in thread
From: Danilo Krummrich @ 2024-04-29 19:24 UTC (permalink / raw)
  To: ojeda, alex.gaynor, wedsonaf, boqun.feng, gary, bjorn3_gh,
	benno.lossin, a.hindborg, aliceryhl
  Cc: rust-for-linux, Danilo Krummrich

Currently, a Vec<T>'s ptr value, after calling Vec<T>::new(), is
initialized to Unique::dangling(). Hence, in VecExt<T>::reserve(), we're
passing a dangling pointer (instead of NULL) to krealloc() whenever a
new Vec<T> is created through VecExt<T> extension functions.

This only works since it happens that Unique::dangling()'s value (0x1)
falls within the range between 0x0 and ZERO_SIZE_PTR (0x10) and
krealloc() hence treats it the same as a NULL pointer however.

This isn't a case we should rely on, especially since other kernel
allocators are not as tolerant. Instead, pass a real NULL pointer to
krealloc_aligned() if Vec<T>'s capacity is zero.

Fixes: 5ab560ce12ed ("rust: alloc: update `VecExt` to take allocation flags")
Signed-off-by: Danilo Krummrich <dakr@redhat.com>
---
 rust/kernel/alloc/vec_ext.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/rust/kernel/alloc/vec_ext.rs b/rust/kernel/alloc/vec_ext.rs
index 6a916fcf8bf1..ffcf8a19f715 100644
--- a/rust/kernel/alloc/vec_ext.rs
+++ b/rust/kernel/alloc/vec_ext.rs
@@ -4,6 +4,7 @@
 
 use super::{AllocError, Flags};
 use alloc::vec::Vec;
+use core::ptr;
 use core::result::Result;
 
 /// Extensions to [`Vec`].
@@ -137,6 +138,15 @@ fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError>
 
         let (ptr, len, cap) = destructure(self);
 
+        // We need to make sure that ptr is either NULL or comes from a previous call to
+        // `krealloc_aligned`. A `Vec<T>`'s `ptr` value is not guaranteed to be NULL and might be
+        // dangling after being created with `Vec::new`. Instead, we can rely on `Vec<T>'s capacity
+        // to be zero if no memory has been allocated yet.
+        let ptr = match cap {
+            0 => ptr::null_mut(),
+            _ => ptr,
+        };
+
         // SAFETY: `ptr` is valid because it's either NULL or comes from a previous call to
         // `krealloc_aligned`. We also verified that the type is not a ZST.
         let new_ptr = unsafe { super::allocator::krealloc_aligned(ptr.cast(), layout, flags) };

base-commit: 2c1092853f163762ef0aabc551a630ef233e1be3
-- 
2.44.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2024-04-30 22:45 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-29 19:24 [PATCH] rust: alloc: fix dangling pointer in VecExt<T>::reserve() Danilo Krummrich
2024-04-29 19:52 ` Boqun Feng
2024-04-29 21:01   ` Danilo Krummrich
2024-04-29 22:01     ` Boqun Feng
2024-04-30 16:42       ` Danilo Krummrich
2024-04-30 18:33         ` Boqun Feng
2024-04-30 20:46           ` Danilo Krummrich
2024-04-30 20:59             ` Boqun Feng
2024-04-30 21:08               ` Boqun Feng
2024-04-30 22:19                 ` Danilo Krummrich
2024-04-30 22:41                   ` Boqun Feng
2024-04-30 22:06             ` Boqun Feng
2024-04-30 22:44     ` Miguel Ojeda
2024-04-30  8:25 ` Alice Ryhl
2024-04-30 12:07   ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).