* [PATCH] rust: alloc: check the length precondition in Vec::from_raw_parts
@ 2026-11-16 12:53 ` Peder Bergan via B4 Relay
2026-11-16 13:07 ` Andreas Hindborg
0 siblings, 1 reply; 2+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-16 12:53 UTC (permalink / raw)
To: Danilo Krummrich, Alice Ryhl
Cc: Lorenzo Stoakes, Vlastimil Babka, Liam R. Howlett,
Uladzislau Rezki, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel
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>
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] rust: alloc: check the length precondition in Vec::from_raw_parts
2026-11-16 12:53 ` [PATCH] rust: alloc: check the length precondition in Vec::from_raw_parts Peder Bergan via B4 Relay
@ 2026-11-16 13:07 ` Andreas Hindborg
0 siblings, 0 replies; 2+ messages in thread
From: Andreas Hindborg @ 2026-11-16 13:07 UTC (permalink / raw)
To: peder.bergan, Danilo Krummrich, Alice Ryhl
Cc: Lorenzo Stoakes, Vlastimil Babka, Liam R. Howlett,
Uladzislau Rezki, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Trevor Gross, Daniel Almeida,
Tamir Duberstein, Alexandre Courbot, Onur Özkan,
rust-for-linux, linux-kernel
"Peder Bergan via B4 Relay" <devnull+peder.bergan.gmail.com@kernel.org>
writes:
> 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>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Best regards,
Andreas Hindborg
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-16 13:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <Cr09eVvg5FecTvFRQ6ONjPHaWg--YJB_fcboFARKLEbwSllbd6wep9zKRdKcsOhp244ll6uaoT7qGtNK7FJgbw==@protonmail.internalid>
2026-11-16 12:53 ` [PATCH] rust: alloc: check the length precondition in Vec::from_raw_parts Peder Bergan via B4 Relay
2026-11-16 13:07 ` Andreas Hindborg
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®