mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peder Bergan via B4 Relay <devnull+peder.bergan.gmail.com@kernel.org>
To: Danilo Krummrich <dakr@kernel.org>, Alice Ryhl <aliceryhl@google.com>
Cc: "Lorenzo Stoakes" <ljs@kernel.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Uladzislau Rezki" <urezki@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] rust: alloc: check the length precondition in Vec::from_raw_parts
Date: Mon, 16 Nov 2026 13:53:32 +0100	[thread overview]
Message-ID: <20261116-vec-raw-parts-precondition-v1-1-8f633f66b4c1@gmail.com> (raw)

From: Peder Bergan <peder.bergan@gmail.com>

Vec::from_raw_parts() requires length to be no greater than capacity for
nonzero-sized elements. Check this precondition with
unsafe_precondition_assert!() before constructing the vector so debug
builds diagnose invalid raw parts at this boundary.

Keep the check in the nonzero-sized branch because zero-sized vectors
ignore the capacity argument. Add a KUnit test for reconstructing a
nonempty zero-sized vector with a zero capacity argument.

Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1232
Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
Based on alloc-next.

My validation used the x86_64 rust_kvec KUnit suite under QEMU with
CLIPPY=1 and debug assertions enabled and disabled. All six tests passed
in both configurations. A temporary invalid-length probe triggered the
expected precondition diagnostic. The baseline has an unrelated unused
function warning in io/resource.rs, also present after this change.

OpenAI Codex assisted with coding and testing.
---
 rust/kernel/alloc/kvec.rs | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index cd0c57a9a..b8f4c1692 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -23,7 +23,8 @@ use crate::{
     page::{
         AsPageIter,
         PAGE_SIZE, //
-    }, //
+    },
+    unsafe_precondition_assert, //
 };
 
 use core::{
@@ -685,6 +686,8 @@ where
         let layout = if Self::is_zst() {
             ArrayLayout::empty()
         } else {
+            unsafe_precondition_assert!(length <= capacity);
+
             // SAFETY: By the safety requirements of this function, `capacity * size_of::<T>()` is
             // smaller than `isize::MAX`.
             unsafe { ArrayLayout::new_unchecked(capacity) }
@@ -1623,6 +1626,18 @@ mod tests {
     use super::*;
     use crate::prelude::*;
 
+    #[test]
+    fn test_kvec_from_raw_parts_zst() {
+        let ptr = NonNull::<()>::dangling().as_ptr();
+
+        // SAFETY: For a zero-sized type, `ptr` only needs to be dangling and well aligned.
+        // The capacity argument is ignored.
+        let v = unsafe { KVec::from_raw_parts(ptr, 3, 0) };
+
+        assert_eq!(v.len(), 3);
+        assert_eq!(v.capacity(), usize::MAX);
+    }
+
     #[test]
     fn test_kvec_retain() {
         /// Verify correctness for one specific function.

---
base-commit: a2a82cbb73342d605078cb85763472f40156af1f
change-id: 20261116-vec-raw-parts-precondition-b540ab5ccceb

Best regards,
--  
Peder Bergan <peder.bergan@gmail.com>



             reply	other threads:[~2026-11-16 12:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <Cr09eVvg5FecTvFRQ6ONjPHaWg--YJB_fcboFARKLEbwSllbd6wep9zKRdKcsOhp244ll6uaoT7qGtNK7FJgbw==@protonmail.internalid>
2026-11-16 12:53 ` Peder Bergan via B4 Relay [this message]
2026-11-16 13:07   ` Andreas Hindborg

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=20261116-vec-raw-parts-precondition-v1-1-8f633f66b4c1@gmail.com \
    --to=devnull+peder.bergan.gmail.com@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=peder.bergan@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=urezki@gmail.com \
    --cc=vbabka@kernel.org \
    --cc=work@onurozkan.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®