* [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 2026-12-01 17:01 ` Danilo Krummrich 0 siblings, 2 replies; 3+ 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 related [flat|nested] 3+ 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 2026-12-01 17:01 ` Danilo Krummrich 1 sibling, 0 replies; 3+ 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] 3+ 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 @ 2026-12-01 17:01 ` Danilo Krummrich 1 sibling, 0 replies; 3+ messages in thread From: Danilo Krummrich @ 2026-12-01 17:01 UTC (permalink / raw) To: Peder Bergan Cc: Danilo Krummrich, Alice Ryhl, 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 On Mon, 16 Nov 2026 13:53:32 +0100, Peder Bergan wrote: > [PATCH] rust: alloc: check the length precondition in Vec::from_raw_parts Applied, thanks! Branch: alloc-next Tree: git@github.com:Rust-for-Linux/linux.git [1/1] rust: alloc: check the length precondition in Vec::from_raw_parts commit: dff36b77ad00 The patch will appear in the next linux-next integration (typically within 24 hours on weekdays). The patch is queued up for the upcoming merge window for the next major kernel release. ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-01 17:01 UTC | newest]
Thread overview: 3+ 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
2026-12-01 17:01 ` Danilo Krummrich
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox