[PATCH v3 1/2] rust: lockdep: Remove support for dynamically allocated LockClassKeys

Mitchell Levy posted 2 patches 1 year ago
There is a newer version of this series
[PATCH v3 1/2] rust: lockdep: Remove support for dynamically allocated LockClassKeys
Posted by Mitchell Levy 1 year ago
Currently, dynamically allocated LockCLassKeys can be used from the Rust
side without having them registered. This is a soundness issue, so
remove them.

Suggested-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/rust-for-linux/20240815074519.2684107-3-nmi@metaspace.dk/
Cc: stable@vger.kernel.org
Signed-off-by: Mitchell Levy <levymitchell0@gmail.com>
---
 rust/kernel/sync.rs | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs
index 1eab7ebf25fd..cb92f2c323e5 100644
--- a/rust/kernel/sync.rs
+++ b/rust/kernel/sync.rs
@@ -29,28 +29,20 @@
 unsafe impl Sync for LockClassKey {}
 
 impl LockClassKey {
-    /// Creates a new lock class key.
-    pub const fn new() -> Self {
-        Self(Opaque::uninit())
-    }
-
     pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
         self.0.get()
     }
 }
 
-impl Default for LockClassKey {
-    fn default() -> Self {
-        Self::new()
-    }
-}
-
 /// Defines a new static lock class and returns a pointer to it.
 #[doc(hidden)]
 #[macro_export]
 macro_rules! static_lock_class {
     () => {{
-        static CLASS: $crate::sync::LockClassKey = $crate::sync::LockClassKey::new();
+        static CLASS: $crate::sync::LockClassKey =
+            // SAFETY: lockdep expects uninitialized memory when it's handed a statically allocated
+            // lock_class_key
+            unsafe { ::core::mem::MaybeUninit::uninit().assume_init() };
         &CLASS
     }};
 }

-- 
2.34.1
Re: [PATCH v3 1/2] rust: lockdep: Remove support for dynamically allocated LockClassKeys
Posted by Benno Lossin 1 year ago
On 05.02.25 20:59, Mitchell Levy wrote:
> Currently, dynamically allocated LockCLassKeys can be used from the Rust
> side without having them registered. This is a soundness issue, so
> remove them.
> 
> Suggested-by: Alice Ryhl <aliceryhl@google.com>
> Link: https://lore.kernel.org/rust-for-linux/20240815074519.2684107-3-nmi@metaspace.dk/
> Cc: stable@vger.kernel.org
> Signed-off-by: Mitchell Levy <levymitchell0@gmail.com>
> ---
>  rust/kernel/sync.rs | 16 ++++------------
>  1 file changed, 4 insertions(+), 12 deletions(-)

Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Re: [PATCH v3 1/2] rust: lockdep: Remove support for dynamically allocated LockClassKeys
Posted by Miguel Ojeda 1 year ago
On Wed, Feb 5, 2025 at 8:59 PM Mitchell Levy <levymitchell0@gmail.com> wrote:
>
> Currently, dynamically allocated LockCLassKeys can be used from the Rust
> side without having them registered. This is a soundness issue, so
> remove them.
>
> Suggested-by: Alice Ryhl <aliceryhl@google.com>
> Link: https://lore.kernel.org/rust-for-linux/20240815074519.2684107-3-nmi@metaspace.dk/
> Cc: stable@vger.kernel.org
> Signed-off-by: Mitchell Levy <levymitchell0@gmail.com>

I imagine we should have:

    Fixes: 6ea5aa08857a ("rust: sync: introduce `LockClassKey`")

Is that right?

Thanks!

Cheers,
Miguel
Re: [PATCH v3 1/2] rust: lockdep: Remove support for dynamically allocated LockClassKeys
Posted by Mitchell Levy 1 year ago
On Fri, Feb 07, 2025 at 12:27:58AM +0100, Miguel Ojeda wrote:
> On Wed, Feb 5, 2025 at 8:59 PM Mitchell Levy <levymitchell0@gmail.com> wrote:
> >
> > Currently, dynamically allocated LockCLassKeys can be used from the Rust
> > side without having them registered. This is a soundness issue, so
> > remove them.
> >
> > Suggested-by: Alice Ryhl <aliceryhl@google.com>
> > Link: https://lore.kernel.org/rust-for-linux/20240815074519.2684107-3-nmi@metaspace.dk/
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Mitchell Levy <levymitchell0@gmail.com>
> 
> I imagine we should have:
> 
>     Fixes: 6ea5aa08857a ("rust: sync: introduce `LockClassKey`")
> 
> Is that right?

That's correct. Thank you for catching this! I will include this on a
resend.

Mitchell

> Thanks!
> 
> Cheers,
> Miguel