Skip to contentSkip to conversation

firmware_loader: annotate doctests as `no_run`

PATCH In review Danilo Krummrich 3 participants 5 messages Jul 8, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 firmware_loader: annotate doctests as `no_run`

The doctests of Firmware are compile-time only tests, since they require a proper Device and a valid path to a (firmware) blob in order to do something sane on runtime - we can't satisfy both of those requirements.

Hence, configure the example as no_run.

Unfortunately, the kernel's Rust build system can't consider the no_run attribute yet. Hence, for the meantime, wrap the example code into a new function and never actually call it.

Fixes de6582833db0 ("rust: add firmware abstractions")

---
v2: (no changes)

---
 rust/kernel/firmware.rs | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
rust/kernel/firmware.rs +6 -2
index 386c8fb44785..106a928a535e 100644
@@ -26,14 +26,18 @@
 ///
 /// # Examples
 ///
-/// ```
+/// ```no_run
 /// # use kernel::{c_str, device::Device, firmware::Firmware};
 ///
+/// # fn no_run() -> Result<(), Error> {
 /// # // SAFETY: *NOT* safe, just for the example to get an `ARef<Device>` instance
 /// # let dev = unsafe { Device::from_raw(core::ptr::null_mut()) };
 ///
-/// let fw = Firmware::request(c_str!("path/to/firmware.bin"), &dev).unwrap();
+/// let fw = Firmware::request(c_str!("path/to/firmware.bin"), &dev)?;
 /// let blob = fw.data();
+///
+/// # Ok(())
+/// # }
 /// ```
 pub struct Firmware(NonNull<bindings::firmware>);
 

base-commit: 997197b58bf6e22b8c6ef88a168d8292fa9acec9

Signature · 1 line
2.45.2
#1 lore raw

PATCH v2 2/2 firmware_loader: fix soundness issue in `request_internal`

request_internal must be called with one of the following function
pointers: request_firmware(), firmware_request_nowarn(), firmware_request_platform() or request_firmware_direct().

The previous FwFunc alias did not guarantee this, which is unsound.

In order to fix this up, implement FwFunc as new type with a corresponding type invariant.

---
v2:
- provide method for each wrapped FwFunc (Christian)

---
 rust/kernel/firmware.rs | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
rust/kernel/firmware.rs +20 -7
index 106a928a535e..2ba03af9f036 100644
@@ -7,10 +7,23 @@
 use crate::{bindings, device::Device, error::Error, error::Result, str::CStr};
 use core::ptr::NonNull;
 
-// One of the following: `bindings::request_firmware`, `bindings::firmware_request_nowarn`,
-// `firmware_request_platform`, `bindings::request_firmware_direct`
-type FwFunc =
-    unsafe extern "C" fn(*mut *const bindings::firmware, *const i8, *mut bindings::device) -> i32;
+/// # Invariants
+///
+/// One of the following: `bindings::request_firmware`, `bindings::firmware_request_nowarn`,
+/// `bindings::firmware_request_platform`, `bindings::request_firmware_direct`.
+struct FwFunc(
+    unsafe extern "C" fn(*mut *const bindings::firmware, *const i8, *mut bindings::device) -> i32,
+);
+
+impl FwFunc {
+    fn request() -> Self {
+        Self(bindings::request_firmware)
+    }
+
+    fn request_nowarn() -> Self {
+        Self(bindings::firmware_request_nowarn)
+    }
+}
 
 /// Abstraction around a C `struct firmware`.
 ///
@@ -48,7 +61,7 @@ fn request_internal(name: &CStr, dev: &Device, func: FwFunc) -> Result<Self> {
 
         // SAFETY: `pfw` is a valid pointer to a NULL initialized `bindings::firmware` pointer.
         // `name` and `dev` are valid as by their type invariants.
-        let ret = unsafe { func(pfw as _, name.as_char_ptr(), dev.as_raw()) };
+        let ret = unsafe { func.0(pfw as _, name.as_char_ptr(), dev.as_raw()) };
         if ret != 0 {
             return Err(Error::from_errno(ret));
         }
@@ -60,13 +73,13 @@ fn request_internal(name: &CStr, dev: &Device, func: FwFunc) -> Result<Self> {
 
     /// Send a firmware request and wait for it. See also `bindings::request_firmware`.
     pub fn request(name: &CStr, dev: &Device) -> Result<Self> {
-        Self::request_internal(name, dev, bindings::request_firmware)
+        Self::request_internal(name, dev, FwFunc::request())
     }
 
     /// Send a request for an optional firmware module. See also
     /// `bindings::firmware_request_nowarn`.
     pub fn request_nowarn(name: &CStr, dev: &Device) -> Result<Self> {
-        Self::request_internal(name, dev, bindings::firmware_request_nowarn)
+        Self::request_internal(name, dev, FwFunc::request_nowarn())
     }
 
     fn as_raw(&self) -> *mut bindings::firmware {
Signature · 1 line
2.45.2
maintainer
#2 lore raw

Re: [PATCH v2 2/2] firmware_loader: fix soundness issue in `request_internal`

Quoted 4 lines · Danilo Krummrich

request_internal must be called with one of the following function
pointers: request_firmware(), firmware_request_nowarn(), firmware_request_platform() or request_firmware_direct().

Quoted 8 lines

The previous FwFunc alias did not guarantee this, which is unsound.

In order to fix this up, implement FwFunc as new type with a corresponding type invariant.

While you're at it, can you go ahead and extend out selftest coverage for the firmware_loader so we can test Rust too? Could these issues have been caught with a selftest? If not why not?

Luis

#3 lore raw

Re: [PATCH v2 2/2] firmware_loader: fix soundness issue in `request_internal`

Quoted 7 lines · Luis Chamberlain
Quoted 6 lines · Danilo Krummrich

request_internal must be called with one of the following function
pointers: request_firmware(), firmware_request_nowarn(), firmware_request_platform() or request_firmware_direct().

The previous FwFunc alias did not guarantee this, which is unsound.

Quoted 8 lines
Quoted 6 lines

In order to fix this up, implement FwFunc as new type with a corresponding type invariant.

While you're at it, can you go ahead and extend out selftest coverage

I think that'd be good and I thought about it. However, I think it makes more sense once we got a few more abstractions in place, such that we can come up with a Rust test module analogue to lib/test_firmware.c. What do you think?

Quoted 2 lines

for the firmware_loader so we can test Rust too? Could these issues have been caught with a selftest? If not why not?

This patch isn't actually fixing a real bug. Which is also why I didn't put a "Fixes" tag.

It's more that without the FwFunc type indirection and the corresponding invariant the safety of request_internal isn't guranteed formally.

- Danilo

Quoted 3 lines

Luis

Reviewed-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] firmware_loader: fix soundness issue in `request_internal`

In reply to Danilo Krummrich

Greetings

Quoted 36 lines · Danilo Krummrich

request_internal must be called with one of the following function
pointers: request_firmware(), firmware_request_nowarn(), firmware_request_platform() or request_firmware_direct().

The previous FwFunc alias did not guarantee this, which is unsound.

In order to fix this up, implement FwFunc as new type with a corresponding type invariant.

---
v2:
- provide method for each wrapped FwFunc (Christian)> ---

 rust/kernel/firmware.rs | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
rust/kernel/firmware.rs +6 -4
index 106a928a535e..2ba03af9f036 100644
@@ -7,10 +7,23 @@
 use crate::{bindings, device::Device, error::Error, error::Result, str::CStr};
 use core::ptr::NonNull;
 
-// One of the following: `bindings::request_firmware`, `bindings::firmware_request_nowarn`,
-// `firmware_request_platform`, `bindings::request_firmware_direct`
-type FwFunc =
-    unsafe extern "C" fn(*mut *const bindings::firmware, *const i8, *mut bindings::device) -> i32;
+/// # Invariants
+///
+/// One of the following: `bindings::request_firmware`, `bindings::firmware_request_nowarn`,
+/// `bindings::firmware_request_platform`, `bindings::request_firmware_direct`.
+struct FwFunc(
+    unsafe extern "C" fn(*mut *const bindings::firmware, *const i8, *mut bindings::device) -> i32,

rust/kernel/firmware.rs line 25

Quoted 10 lines
rust/kernel/firmware.rs +10 -0
+);
+
+impl FwFunc {
+    fn request() -> Self {
+        Self(bindings::request_firmware)
+    }
+
+    fn request_nowarn() -> Self {
+        Self(bindings::firmware_request_nowarn)
+    }

I'm not sure if we should have a comment here that describes how the invariant is fulfilled.

In this case its not too bad since the Invariants are described just a few lines above, so:

Cheers,
Christian