Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH v2] rust: cpufreq: mark Policy::data() as unsafe
@ 2026-11-28 14:02 Bilal Hashim
  2026-11-30  3:40 ` Zhongqiu Han
  0 siblings, 1 reply; 2+ messages in thread
From: Bilal Hashim @ 2026-11-28 14:02 UTC (permalink / raw)
  To: Viresh Kumar, Rafael J . Wysocki
  Cc: Miguel Ojeda, Alice Ryhl, 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`, document the safety precondition that
`T` must match the exact `ForeignOwnable` type previously stored in the
policy's `driver_data`, and update the in-tree caller in
drivers/cpufreq/rcpufreq_dt.rs.

Fixes: 6ebdd7c93177 ("rust: cpufreq: Extend abstractions for policy and driver ops")
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
Changes in v2:
- Update caller in drivers/cpufreq/rcpufreq_dt.rs to wrap
  policy.data::<Self::PData>() in an unsafe block with a SAFETY comment.
- Add Fixes: tag.
---
--- a/drivers/cpufreq/rcpufreq_dt.rs
+++ b/drivers/cpufreq/rcpufreq_dt.rs
@@ -178,7 +178,9 @@ impl cpufreq::Driver for CPUFreqDTDriver {
     }
 
     fn target_index(policy: &mut cpufreq::Policy, index: cpufreq::TableIndex) -> Result {
-        let Some(data) = policy.data::<Self::PData>() else {
+        // SAFETY: `policy`'s private data was initialized with `Self::PData` in `Self::init()`.
+        let Some(data) = (unsafe { policy.data::<Self::PData>() }) else {
             return Err(ENOENT);
         };
 
--- 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] 2+ messages in thread

* Re: [PATCH v2] rust: cpufreq: mark Policy::data() as unsafe
  2026-11-28 14:02 [PATCH v2] rust: cpufreq: mark Policy::data() as unsafe Bilal Hashim
@ 2026-11-30  3:40 ` Zhongqiu Han
  0 siblings, 0 replies; 2+ messages in thread
From: Zhongqiu Han @ 2026-11-30  3:40 UTC (permalink / raw)
  To: Bilal Hashim, Viresh Kumar, Rafael J . Wysocki
  Cc: Miguel Ojeda, Alice Ryhl, linux-pm, rust-for-linux, linux-kernel,
	Zhongqiu Han

On 11/28/2026 10:02 PM, Bilal Hashim wrote:
> 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`, document the safety precondition that
> `T` must match the exact `ForeignOwnable` type previously stored in the
> policy's `driver_data`, and update the in-tree caller in
> drivers/cpufreq/rcpufreq_dt.rs.
> 
> Fixes: 6ebdd7c93177 ("rust: cpufreq: Extend abstractions for policy and driver ops")
> Signed-off-by: Bilal Hashim <AeeSec@126.com>
> ---
> Changes in v2:
> - Update caller in drivers/cpufreq/rcpufreq_dt.rs to wrap
>    policy.data::<Self::PData>() in an unsafe block with a SAFETY comment.
> - Add Fixes: tag.
> ---
> --- a/drivers/cpufreq/rcpufreq_dt.rs
> +++ b/drivers/cpufreq/rcpufreq_dt.rs
> @@ -178,7 +178,9 @@ impl cpufreq::Driver for CPUFreqDTDriver {
>       }

The patch does not apply:

$ git apply 0001-...patch
error: corrupt patch at line 43

The first hunk header is wrong:

@@ -178,7 +178,9 @@ impl cpufreq::Driver for CPUFreqDTDriver {

This hunk removes one line and adds two, so the new side has 7 - 1 + 2 =
8 lines, not 9.


>   
>       fn target_index(policy: &mut cpufreq::Policy, index: cpufreq::TableIndex) -> Result {
> -        let Some(data) = policy.data::<Self::PData>() else {
> +        // SAFETY: `policy`'s private data was initialized with `Self::PData` in `Self::init()`.
> +        let Some(data) = (unsafe { policy.data::<Self::PData>() }) else {
>               return Err(ENOENT);
>           };
>   
> --- 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()) })
>           }
>       }
> 
> 


-- 
Thx and BRs,
Zhongqiu Han

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30  3:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 14:02 [PATCH v2] rust: cpufreq: mark Policy::data() as unsafe Bilal Hashim
2026-11-30  3:40 ` Zhongqiu Han

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox