mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust: cpufreq: mark Policy::data() as unsafe
@ 2026-11-27 12:12 Bilal Hashim
  0 siblings, 0 replies; only message in thread
From: Bilal Hashim @ 2026-11-27 12:12 UTC (permalink / raw)
  To: Viresh Kumar, Rafael J . Wysocki, Miguel Ojeda
  Cc: linux-pm, rust-for-linux, linux-kernel, Bilal Hashim

Policy::data<T: ForeignOwnable>(&mut self) is currently a safe public
method that casts the untyped C pointer `policy->driver_data` via
`T::borrow(...)` for any caller-chosen type `T`.

Because `Policy` is not parameterized by `Driver::PData` and performs no
runtime type verification, any safe Rust caller with `&mut Policy` can
invoke `policy.data::<WrongType>()` and reinterpret `driver_data` as an
arbitrary `ForeignOwnable` type, causing type confusion and undefined
behavior in safe Rust.

Mark Policy::data() as `unsafe` and document the safety precondition that
`T` must match the exact `ForeignOwnable` type previously stored in the
policy's `driver_data`.

Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
--- a/rust/kernel/cpufreq.rs
+++ b/rust/kernel/cpufreq.rs
@@ -648,9 +648,15 @@ impl Policy {
     /// Returns the [`Policy`]'s private data.
-    pub fn data<T: ForeignOwnable>(&mut self) -> Option<<T>::Borrowed<'_>> {
+    ///
+    /// # Safety
+    ///
+    /// The caller must ensure that `T` is the exact [`ForeignOwnable`] type previously stored in
+    /// this [`Policy`]'s `driver_data`.
+    pub unsafe fn data<T: ForeignOwnable>(&mut self) -> Option<<T>::Borrowed<'_>> {
         if self.as_ref().driver_data.is_null() {
             None
         } else {
-            // SAFETY: The data is earlier set from [`set_data`].
+            // SAFETY: The caller guarantees that `T` matches the type previously stored via
+            // [`set_data`].
             Some(unsafe { T::borrow(self.as_ref().driver_data.cast()) })
         }
     }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-27 12:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 12:12 [PATCH] rust: cpufreq: mark Policy::data() as unsafe Bilal Hashim

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®