rust: cpufreq: reject NULL from cpufreq_cpu_get()
Subsystems (3)
PATCH v3 1/1 rust: cpufreq: reject NULL from cpufreq_cpu_get()
cpufreq_cpu_get() returns either a referenced policy or NULL. PolicyCpu::from_cpu() passed its return value to from_err_ptr(), which rejects ERR_PTR values but accepts NULL.
If the lookup fails, Policy::from_raw_mut() therefore constructs a mutable reference from NULL. Dropping the resulting PolicyCpu then passes the invalid pointer to cpufreq_cpu_put(), causing an oops in kobject_put().
Reject NULL with NonNull before constructing the Policy reference. Return ENODEV instead.
A KUnit negative-control run reproduced the oops with the original conversion. The same test passed with this change. The reproducer is available on request.
---
Changes in v3:
- Add Cc: [email protected].
Changes in v2:
- Format modified imports using the kernel's vertical import style.
v2: https://lore.kernel.org/r/[email protected]
rust/kernel/cpufreq.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
rust/kernel/cpufreq.rs +14 -4
@@ -14,7 +14,13 @@ cpumask, device::{Bound, Device}, devres, - error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR}, + error::{ + code::*, + from_result, + to_result, + Result, + VTABLE_DEFAULT_ERROR, // + }, ffi::{c_char, c_ulong}, prelude::*, types::ForeignOwnable,
@@ -29,7 +35,10 @@ marker::PhantomData, ops::{Deref, DerefMut}, pin::Pin, - ptr, + ptr::{ + self, +NonNull ,// + }, };use macros::vtable;
@@ -687,12 +696,13 @@ fn clear_data<T: ForeignOwnable>(&mut self) -> Option<T> {impl <'a>PolicyCpu <'a> {fn from_cpu (cpu:CpuId ) ->Result <Self > {// SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU. -let ptr =from_err_ptr (unsafe { bindings::cpufreq_cpu_get (u32::from (cpu)) })?; +let ptr = +NonNull ::new (unsafe { bindings::cpufreq_cpu_get (u32::from (cpu)) }).ok_or (ENODEV )?;Ok (Self (// SAFETY: The `ptr` is guaranteed to be valid and remains valid for the lifetime of // the returned reference. -unsafe {Policy ::from_raw_mut (ptr) }, +unsafe {Policy ::from_raw_mut (ptr.as_ptr ()) }, )) } }
Signature · 1 line
2.55.0
Quoted 11 lines · On Sat, 29 Aug 2026 19:10:13 +0300 Mehmet Koseoglu…
cpufreq_cpu_get()returns either a referenced policy or NULL.PolicyCpu::from_cpu()passed its return value tofrom_err_ptr(), which rejectsERR_PTRvalues but accepts NULL.If the lookup fails,
Policy::from_raw_mut()therefore constructs a mutable reference from NULL. Dropping the resulting PolicyCpu then passes the invalid pointer tocpufreq_cpu_put(), causing an oops inkobject_put().Reject NULL with NonNull before constructing the Policy reference. Return ENODEV instead.
Quoted 8 lines
A KUnit negative-control run reproduced the oops with the original conversion. The same test passed with this change. The reproducer is available on request.
Fixes 6ebdd7c93177 ("rust: cpufreq: Extend abstractions for policy and driver ops")Assisted-by LLMSigned-off-by Mehmet Koseoglu <[email protected]>
Quoted 61 lines
---
Changes in v3:
- Add Cc: [email protected].Changes in v2:
- Format modified imports using the kernel's vertical import style.v2: https://lore.kernel.org/r/[email protected]
rust/kernel/cpufreq.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)rust/kernel/cpufreq.rs +14
-4@@ -14,7 +14,13 @@ cpumask, device::{Bound, Device}, devres, - error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR}, + error::{ + code::*, + from_result, + to_result, + Result, + VTABLE_DEFAULT_ERROR, // + }, ffi::{c_char, c_ulong}, prelude::*, types::ForeignOwnable,@@ -29,7 +35,10 @@ marker::PhantomData, ops::{Deref, DerefMut}, pin::Pin, - ptr, + ptr::{ + self, +NonNull ,// + }, };use macros::vtable;@@ -687,12 +696,13 @@ fn clear_data<T: ForeignOwnable>(&mut self) -> Option<T> {impl <'a>PolicyCpu <'a> {fn from_cpu (cpu:CpuId ) ->Result <Self > {// SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU. -let ptr =from_err_ptr (unsafe { bindings::cpufreq_cpu_get (u32::from (cpu)) })?; +let ptr = +NonNull ::new (unsafe { bindings::cpufreq_cpu_get (u32::from (cpu)) }).ok_or (ENODEV )?;Ok (Self (// SAFETY: The `ptr` is guaranteed to be valid and remains valid for the lifetime of // the returned reference. -unsafe {Policy ::from_raw_mut (ptr) }, +unsafe {Policy ::from_raw_mut (ptr.as_ptr ()) }, )) } }Signature · 1 line
2.55.0
Quoted 3 lines · Mehmet Koseoglu
cpufreq_cpu_get()returns either a referenced policy or NULL.PolicyCpu::from_cpu()passed its return value tofrom_err_ptr(), which rejectsERR_PTRvalues but accepts NULL.
[...]
rust/kernel/cpufreq.rs fn clear_data<T: ForeignOwnable>(&mut self) -> Option<T> { · line 705
Quoted 5 lines
Ok(Self( // SAFETY: The `ptr` is guaranteed to be valid and remains valid for the lifetime of // the returned reference.rust/kernel/cpufreq.rs +1
-1- unsafe { Policy::from_raw_mut(ptr) }, + unsafe { Policy::from_raw_mut(ptr.as_ptr()) },
Just a quick nit here, maybe rephrase this SAFETY comment, e.g.:
// SAFETY: `ptr` is non-NULL and `cpufreq_cpu_get()` took a reference on it, so it is
// valid for writing and remains valid for the lifetime of the returned reference.Either way, looks good to me.
FWIW, I hit this too and have a reproducer [1].
[1] https://github.com/Rust-for-Linux/linux/issues/1258
Quoted 6 lines · spidermana
Either way, looks good to me.
FWIW, I hit this too and have a reproducer [1].
[1] https://github.com/Rust-for-Linux/linux/issues/1258Reviewed-by Elowen Xu <[email protected]>
Thanks for taking the time to send the tag! :)
The From: doesn't seem to match the email of the tag, is that intended?
Cheers,
Miguel
spidermana
Reviewed-byQuoted 1 line · Miguel Ojeda
The From: doesn't seem to match the email of the tag, is that intended?
Oops, no, thanks for noticing! Please disregard the tag in my previous reply. the correct one is:
Viresh Kumar
maintainerQuoted 7 lines · spidermana
Quoted 1 line · Miguel Ojeda
The From: doesn't seem to match the email of the tag, is that intended?
Oops, no, thanks for noticing! Please disregard the tag in my previous reply. the correct one is:
Reviewed-by spidermana <[email protected]>
Please resend the patch with all the fixes / tags / improvements. Thanks.
Signature · 1 line
viresh
Quoted 10 lines · Viresh Kumar
Quoted 7 lines · spidermana
Quoted 1 line · Miguel Ojeda
The From: doesn't seem to match the email of the tag, is that intended?
Oops, no, thanks for noticing! Please disregard the tag in my previous reply. the correct one is:
Reviewed-by spidermana <[email protected]>Please resend the patch with all the fixes / tags / improvements. Thanks.
Hi Viresh,
I see that v3 was already applied as 0ba1ee052b3f. Would you prefer a follow-up patch updating the SAFETY comment, or a v4 of the original patch with the comment update and collected review tags?
Thanks,
Mehmet
Viresh Kumar
maintainerQuoted 3 lines · Mehmet Koseoglu
I see that v3 was already applied as
0ba1ee052b3f. Would you prefer a follow-up patch updating the SAFETY comment, or a v4 of the original patch with the comment update and collected review tags?
Oops, I thought I haven't applied it yet. I did that couple of week ago before I went on vacation :(
You can resend the patch and I will apply the new one and drop the old one.
Signature · 1 line
viresh
PATCH v4 1/1 rust: cpufreq: reject NULL from cpufreq_cpu_get()
In reply to Mehmet Koseoglucpufreq_cpu_get() returns either a referenced policy or NULL. PolicyCpu::from_cpu() passed its return value to from_err_ptr(), which rejects ERR_PTR values but accepts NULL.
If the lookup fails, Policy::from_raw_mut() therefore constructs a mutable reference from NULL. Dropping the resulting PolicyCpu then passes the invalid pointer to cpufreq_cpu_put(), causing an oops in kobject_put().
Reject NULL with NonNull before constructing the Policy reference. Return ENODEV instead.
A KUnit negative-control run reproduced the oops with the original conversion. The same test passed with this change. The reproducer is available on request.
---
No worries at all, here is a v4 update with the comment update and review tags.
Changes in v4:
- Clarify that the policy pointer is non-NULL and cpufreq_cpu_get() holds a reference for the lifetime of the returned reference.
- Add Reviewed-by tags from Onur Özkan and spidermana (using the corrected tag from spidermana's follow-up).
- Resend at Viresh's request to replace the previously applied v3.
Changes in v3:
- Add Cc: [email protected].
Changes in v2:
- Format modified imports using the kernel's vertical import style.
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
rust/kernel/cpufreq.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
rust/kernel/cpufreq.rs +16 -6
@@ -14,7 +14,13 @@ cpumask, device::{Bound, Device}, devres, - error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR}, + error::{ + code::*, + from_result, + to_result, + Result, + VTABLE_DEFAULT_ERROR, // + }, ffi::{c_char, c_ulong}, prelude::*, types::ForeignOwnable,
@@ -29,7 +35,10 @@ marker::PhantomData, ops::{Deref, DerefMut}, pin::Pin, - ptr, + ptr::{ + self, +NonNull ,// + }, };use macros::vtable;
@@ -687,12 +696,13 @@ fn clear_data<T: ForeignOwnable>(&mut self) -> Option<T> {impl <'a>PolicyCpu <'a> {fn from_cpu (cpu:CpuId ) ->Result <Self > {// SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU. -let ptr =from_err_ptr (unsafe { bindings::cpufreq_cpu_get (u32::from (cpu)) })?; +let ptr = +NonNull ::new (unsafe { bindings::cpufreq_cpu_get (u32::from (cpu)) }).ok_or (ENODEV )?;Ok (Self ( -// SAFETY: The `ptr` is guaranteed to be valid and remains valid for the lifetime of -// the returned reference. +// SAFETY: `ptr` is non-NULL and `cpufreq_cpu_get()` took a reference on it, so it is +// valid for writing and remains valid for the lifetime of the returned reference. -unsafe {Policy ::from_raw_mut (ptr) }, +unsafe {Policy ::from_raw_mut (ptr.as_ptr ()) }, )) } }
Signature · 1 line
2.55.0
Quoted 15 lines · Mehmet Koseoglu
cpufreq_cpu_get()returns either a referenced policy or NULL.PolicyCpu::from_cpu()passed its return value tofrom_err_ptr(), which rejectsERR_PTRvalues but accepts NULL.If the lookup fails,
Policy::from_raw_mut()therefore constructs a mutable reference from NULL. Dropping the resulting PolicyCpu then passes the invalid pointer tocpufreq_cpu_put(), causing an oops inkobject_put().Reject NULL with NonNull before constructing the Policy reference. Return ENODEV instead.
A KUnit negative-control run reproduced the oops with the original conversion. The same test passed with this change. The reproducer is available on request.
Quoted 6 lines
Fixes 6ebdd7c93177 ("rust: cpufreq: Extend abstractions for policy and driver ops")Assisted-by LLMSigned-off-by Mehmet Koseoglu <[email protected]>Reviewed-by Onur Özkan <[email protected]>Reviewed-by spidermana <[email protected]>
Applied. Thanks.
Signature · 1 line
viresh