mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RESEND 0/3] drm/tyr: KUnit tests and fixes for the firmware parser
@ 2026-11-27 18:01 Daniel Almeida
  2026-11-27 18:01 ` [PATCH RESEND 1/3] drm/tyr: add KUnit tests " Daniel Almeida
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Daniel Almeida @ 2026-11-27 18:01 UTC (permalink / raw)
  To: dakr, simona, airlied, aliceryhl
  Cc: Sami Tolvanen, dri-devel, linux-kernel, rust-for-linux, Daniel Almeida

Tyr's firmware parser has no tests at all. Assuming well-formed files in
linux-firmware, it means that the error paths in the parser are not
tested. This is a bad combination for code that directly consumes
userspace data.

Patch 1 adds a KUnit suite for it. The suite builds a well-formed image
from named fields and checks that malformed images are rejected. This
suite obviously can't cover every corner case, but at least it can be
progressively expanded.

The parser also accepted two kinds of malformed images, so the gap is
closed by patches 2 and 3. Patch 2 rejects sections whose VA ranges
overlap, and patch 3 rejects section data that is larger than its VA
range. Each adds the test that fails without it. Sami Tolvanen had
already fixed both of these issues downstream.

This is based on drm-rust-next. Tested with KUnit on arm64 under QEMU,
and with IGT on an RK3588 board.

---
Daniel Almeida (3):
      drm/tyr: add KUnit tests for the firmware parser
      drm/tyr: reject firmware sections that overlap
      drm/tyr: reject firmware sections with oversized data

 drivers/gpu/drm/tyr/fw/parser.rs | 379 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 379 insertions(+)
---
base-commit: abfb640b2374e26de8ff81d488c39d8254db538e
change-id: 20261125-tyr-fw-parser-073e351dc7b6

Best regards,
--
Daniel Almeida <daniel.almeida@collabora.com>
-- 
Daniel Almeida <daniel.almeida@collabora.com>


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

* [PATCH RESEND 1/3] drm/tyr: add KUnit tests for the firmware parser
  2026-11-27 18:01 [PATCH RESEND 0/3] drm/tyr: KUnit tests and fixes for the firmware parser Daniel Almeida
@ 2026-11-27 18:01 ` Daniel Almeida
  2026-11-27 18:01 ` [PATCH RESEND 2/3] drm/tyr: reject firmware sections that overlap Daniel Almeida
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Daniel Almeida @ 2026-11-27 18:01 UTC (permalink / raw)
  To: dakr, simona, airlied, aliceryhl
  Cc: Sami Tolvanen, dri-devel, linux-kernel, rust-for-linux, Daniel Almeida

The firmware parser checks the header and the entries of the firmware
file before Tyr maps the sections into the MCU VM. The files in
linux-firmware pass all of these checks and no test feeds the parser a
malformed file, so nothing exercises its error paths.

The parser does not touch the hardware, which lets KUnit run it on
images built in memory. Add the tyr_fw_parser suite. It builds an image
from named fields, checks that a well-formed image parses into its
sections, and checks that malformed images are rejected.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 drivers/gpu/drm/tyr/fw/parser.rs | 298 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 298 insertions(+)

diff --git a/drivers/gpu/drm/tyr/fw/parser.rs b/drivers/gpu/drm/tyr/fw/parser.rs
index c4d0ad1d7899..de5881f1bf47 100644
--- a/drivers/gpu/drm/tyr/fw/parser.rs
+++ b/drivers/gpu/drm/tyr/fw/parser.rs
@@ -586,3 +586,301 @@ fn try_from(value: u8) -> Result<Self, Self::Error> {
         }
     }
 }
+
+#[kunit_tests(tyr_fw_parser)]
+mod tests {
+    use super::*;
+
+    use kernel::faux;
+
+    /// Appends little-endian values to a firmware image.
+    struct Writer(KVec<u8>);
+
+    impl Writer {
+        fn write_u8(&mut self, value: u8) -> Result {
+            self.0.push(value, GFP_KERNEL)?;
+            Ok(())
+        }
+
+        fn write_u16(&mut self, value: u16) -> Result {
+            self.0.extend_from_slice(&value.to_le_bytes(), GFP_KERNEL)?;
+            Ok(())
+        }
+
+        fn write_u32(&mut self, value: u32) -> Result {
+            self.0.extend_from_slice(&value.to_le_bytes(), GFP_KERNEL)?;
+            Ok(())
+        }
+    }
+
+    /// A firmware image with two entries.
+    ///
+    /// It has a field for each value that a test changes. [`Image::to_bytes()`] writes the other
+    /// values as zero.
+    struct Image {
+        magic: u32,
+        major: u8,
+        /// The padding after the version hash.
+        padding: u32,
+        /// Size of the firmware header and the entries, in bytes.
+        header_size: u32,
+        entries: [Entry; 2],
+        /// Size of the section data after the entries, in bytes.
+        data_size: u32,
+    }
+
+    /// An entry of an [`Image`].
+    struct Entry {
+        entry_type: u8,
+        /// Size of the entry, in bytes.
+        size: u8,
+        flags: u32,
+        va: Range<u32>,
+        data: Range<u32>,
+    }
+
+    impl Image {
+        /// Returns a well-formed image of 72 bytes.
+        ///
+        /// The first section has 4 bytes of data. The second section is at the start of the shared
+        /// region.
+        fn new() -> Self {
+            Self {
+                magic: FirmwareHeader::FW_BINARY_MAGIC,
+                major: 0,
+                padding: 0,
+                // A firmware header of 20 bytes and two entries of 24 bytes.
+                header_size: 68,
+                entries: [
+                    Entry {
+                        // An interface entry.
+                        entry_type: 0,
+                        // An entry header of 4 bytes and a section header of 20 bytes.
+                        size: 24,
+                        flags: SectionFlags::READ_MASK,
+                        va: 0x0..0x1000,
+                        data: 68..72,
+                    },
+                    Entry {
+                        entry_type: 0,
+                        size: 24,
+                        flags: SectionFlags::READ_MASK | SectionFlags::SHARED_MASK,
+                        va: CSF_MCU_SHARED_REGION_START..CSF_MCU_SHARED_REGION_START + 0x1000,
+                        data: 72..72,
+                    },
+                ],
+                data_size: 4,
+            }
+        }
+
+        /// Writes the headers in the order in which the parser reads them, followed by the data.
+        fn to_bytes(&self) -> Result<KVec<u8>> {
+            let minor = 0;
+            let padding1 = 0;
+            let version_hash = 0;
+
+            let mut w = Writer(KVec::new());
+
+            w.write_u32(self.magic)?;
+            w.write_u8(minor)?;
+            w.write_u8(self.major)?;
+            w.write_u16(padding1)?;
+            w.write_u32(version_hash)?;
+            w.write_u32(self.padding)?;
+            w.write_u32(self.header_size)?;
+
+            for entry in &self.entries {
+                // The entry header has the type in bits 7:0 and the size in bits 15:8.
+                w.write_u32(u32::from(entry.entry_type) | (u32::from(entry.size) << 8))?;
+
+                w.write_u32(entry.flags)?;
+                w.write_u32(entry.va.start)?;
+                w.write_u32(entry.va.end)?;
+                w.write_u32(entry.data.start)?;
+                w.write_u32(entry.data.end)?;
+            }
+
+            for _ in 0..self.data_size {
+                w.write_u8(0)?;
+            }
+
+            Ok(w.0)
+        }
+
+        /// Writes the image and parses it.
+        fn parse(&self) -> Result<KVec<ParsedSection>> {
+            parse_bytes(&self.to_bytes()?)
+        }
+    }
+
+    /// Parses a firmware image.
+    fn parse_bytes(image: &[u8]) -> Result<KVec<ParsedSection>> {
+        let reg = faux::Registration::new(c"tyr-fw-parser-kunit", None)?;
+
+        FwParser::new(reg.as_ref().as_ref(), image).parse()
+    }
+
+    /// The well-formed image parses into one section for each entry.
+    #[test]
+    fn well_formed_image() -> Result {
+        let image = Image::new();
+        let sections = image.parse()?;
+
+        assert_eq!(sections.len(), image.entries.len());
+        for (section, entry) in sections.iter().zip(&image.entries) {
+            assert_eq!(section.va, entry.va);
+            assert_eq!(section.data_range, entry.data);
+        }
+
+        Ok(())
+    }
+
+    /// A file that ends inside the firmware header is rejected.
+    #[test]
+    fn truncated_firmware_header() -> Result {
+        let image = Image::new().to_bytes()?;
+
+        // The first 16 of the 20 bytes of the firmware header.
+        assert_eq!(parse_bytes(&image[..16]).err(), Some(EINVAL));
+
+        Ok(())
+    }
+
+    /// A file with the wrong magic is rejected.
+    #[test]
+    fn wrong_magic() {
+        let mut image = Image::new();
+        image.magic = 0;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// A major version other than 0 is rejected.
+    #[test]
+    fn unsupported_major_version() {
+        let mut image = Image::new();
+        image.major = 1;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// Padding after the version hash that is not zero is rejected.
+    #[test]
+    fn nonzero_padding() {
+        let mut image = Image::new();
+        image.padding = 1;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// Headers that are larger than the file are rejected.
+    #[test]
+    fn headers_larger_than_file() {
+        let mut image = Image::new();
+        // The file is 72 bytes long.
+        image.header_size = 76;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// An image with no loadable section is rejected.
+    #[test]
+    fn no_loadable_section() {
+        let mut image = Image::new();
+        // The headers end with the firmware header.
+        image.header_size = 20;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// Headers that end inside an entry header are rejected.
+    #[test]
+    fn truncated_entry_header() {
+        let mut image = Image::new();
+        // The entries end at byte 68. A third entry header would end at byte 72.
+        image.header_size = 70;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// An entry that is smaller than its own header is rejected.
+    #[test]
+    fn entry_smaller_than_its_header() {
+        let mut image = Image::new();
+        image.entries[0].size = 0;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// An entry with a size that is not a multiple of 4 is rejected.
+    #[test]
+    fn unaligned_entry_size() {
+        let mut image = Image::new();
+        // The second entry is 25 bytes long, and the headers end where it ends.
+        image.entries[1].size = 25;
+        image.header_size = 69;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// An entry that ends past the headers is rejected.
+    #[test]
+    fn entry_larger_than_headers() {
+        let mut image = Image::new();
+        // The first entry starts at byte 20. It now ends at byte 72, 4 bytes past the headers.
+        image.entries[0].size = 52;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// An entry of an unknown type that is not optional is rejected.
+    #[test]
+    fn unknown_entry_type() {
+        let mut image = Image::new();
+        image.entries[0].entry_type = 0xff;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// Section flags that the driver does not know are rejected.
+    #[test]
+    fn unknown_section_flags() {
+        let mut image = Image::new();
+        // Bit 6 is not a section flag.
+        image.entries[0].flags = 1 << 6;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// A VA range that ends before it starts is rejected.
+    #[test]
+    fn section_va_end_before_start() {
+        let mut image = Image::new();
+        // The VA range ends at 0x1000.
+        image.entries[0].va.start = 0x2000;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// Section data that ends before it starts is rejected.
+    #[test]
+    fn section_data_end_before_start() {
+        let mut image = Image::new();
+        // The data starts at byte 68.
+        image.entries[0].data.end = 64;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// Section data that ends past the file is rejected.
+    #[test]
+    fn section_data_past_end_of_file() {
+        let mut image = Image::new();
+        // The file is 72 bytes long.
+        image.entries[0].data.end = 76;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// A VA range that is not page aligned is rejected.
+    #[test]
+    fn section_va_not_page_aligned() {
+        let mut image = Image::new();
+        image.entries[0].va.start = 0x800;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// A section at the start of the shared region that is not shared is rejected.
+    #[test]
+    fn shared_region_not_shared() {
+        let mut image = Image::new();
+        image.entries[1].flags = SectionFlags::READ_MASK;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+}

-- 
2.56.0


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

* [PATCH RESEND 2/3] drm/tyr: reject firmware sections that overlap
  2026-11-27 18:01 [PATCH RESEND 0/3] drm/tyr: KUnit tests and fixes for the firmware parser Daniel Almeida
  2026-11-27 18:01 ` [PATCH RESEND 1/3] drm/tyr: add KUnit tests " Daniel Almeida
@ 2026-11-27 18:01 ` Daniel Almeida
  2026-11-27 18:01 ` [PATCH RESEND 3/3] drm/tyr: reject firmware sections with oversized data Daniel Almeida
  2026-11-27 19:40 ` [PATCH RESEND 0/3] drm/tyr: KUnit tests and fixes for the firmware parser Daniel Almeida
  3 siblings, 0 replies; 5+ messages in thread
From: Daniel Almeida @ 2026-11-27 18:01 UTC (permalink / raw)
  To: dakr, simona, airlied, aliceryhl
  Cc: Sami Tolvanen, dri-devel, linux-kernel, rust-for-linux, Daniel Almeida

Firmware::new() maps each loadable section at the VA range given in its
section header. When two ranges overlap, GPUVM unmaps or splits the part
of the earlier mapping that the later one covers. The MCU then sees the
later section's pages there, and nothing reports the overlap.

Reject an image whose loadable sections overlap in FwParser::parse(),
and add a test for it.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 drivers/gpu/drm/tyr/fw/parser.rs | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/drivers/gpu/drm/tyr/fw/parser.rs b/drivers/gpu/drm/tyr/fw/parser.rs
index de5881f1bf47..133b63c5cff1 100644
--- a/drivers/gpu/drm/tyr/fw/parser.rs
+++ b/drivers/gpu/drm/tyr/fw/parser.rs
@@ -188,6 +188,18 @@ pub(super) fn parse(&mut self) -> Result<KVec<ParsedSection>> {
             let entry_section = self.parse_entry(header_end)?;
 
             if let Some(inner) = entry_section.inner {
+                if parsed_sections.iter().any(|other: &ParsedSection| {
+                    inner.va.start < other.va.end && other.va.start < inner.va.end
+                }) {
+                    dev_err!(
+                        self.cursor.dev,
+                        "Firmware virtual address range {:#x}..{:#x} overlaps another section",
+                        inner.va.start,
+                        inner.va.end
+                    );
+                    return Err(EINVAL);
+                }
+
                 parsed_sections.push(inner, GFP_KERNEL)?;
             }
         }
@@ -883,4 +895,29 @@ fn shared_region_not_shared() {
         image.entries[1].flags = SectionFlags::READ_MASK;
         assert_eq!(image.parse().err(), Some(EINVAL));
     }
+
+    /// Loadable sections with overlapping VA ranges are rejected.
+    #[test]
+    fn overlapping_sections() {
+        let mut image = Image::new();
+        // The second section is the page at `second`.
+        let second = image.entries[1].va.start;
+
+        // The first section ends where the second one starts.
+        image.entries[0].va = second - 0x1000..second;
+        assert!(image.parse().is_ok());
+
+        // The first section starts where the second one ends.
+        image.entries[0].va = second + 0x1000..second + 0x2000;
+        assert!(image.parse().is_ok());
+
+        // The first section covers the page below the second one and the second one.
+        image.entries[0].va = second - 0x1000..second + 0x1000;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+
+        // The second section covers three pages, and the first one is the page in the middle.
+        image.entries[0].va = second + 0x1000..second + 0x2000;
+        image.entries[1].va = second..second + 0x3000;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
 }

-- 
2.56.0


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

* [PATCH RESEND 3/3] drm/tyr: reject firmware sections with oversized data
  2026-11-27 18:01 [PATCH RESEND 0/3] drm/tyr: KUnit tests and fixes for the firmware parser Daniel Almeida
  2026-11-27 18:01 ` [PATCH RESEND 1/3] drm/tyr: add KUnit tests " Daniel Almeida
  2026-11-27 18:01 ` [PATCH RESEND 2/3] drm/tyr: reject firmware sections that overlap Daniel Almeida
@ 2026-11-27 18:01 ` Daniel Almeida
  2026-11-27 19:40 ` [PATCH RESEND 0/3] drm/tyr: KUnit tests and fixes for the firmware parser Daniel Almeida
  3 siblings, 0 replies; 5+ messages in thread
From: Daniel Almeida @ 2026-11-27 18:01 UTC (permalink / raw)
  To: dakr, simona, airlied, aliceryhl
  Cc: Sami Tolvanen, dri-devel, linux-kernel, rust-for-linux, Daniel Almeida

Firmware::new() creates a BO of the size of a section's VA range and
copies the section's data into it. Data larger than the range is only
caught at the copy, after the BO is created and mapped, and the data of
a section with an empty VA range is dropped without a message.

Reject both in FwParser::parse_section_entry(), as panthor does, and add
a test for each case.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 drivers/gpu/drm/tyr/fw/parser.rs | 44 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/drivers/gpu/drm/tyr/fw/parser.rs b/drivers/gpu/drm/tyr/fw/parser.rs
index 133b63c5cff1..43e7599f6622 100644
--- a/drivers/gpu/drm/tyr/fw/parser.rs
+++ b/drivers/gpu/drm/tyr/fw/parser.rs
@@ -374,6 +374,14 @@ fn parse_section_entry(
             return Err(EINVAL);
         }
 
+        if section_hdr.data.len() > section_hdr.va.len() {
+            dev_err!(
+                entry_cursor.dev,
+                "Firmware corrupted, section data exceeds section size"
+            );
+            return Err(EINVAL);
+        }
+
         if section_hdr.va.is_empty() {
             return Ok(None);
         }
@@ -859,6 +867,8 @@ fn section_va_end_before_start() {
         let mut image = Image::new();
         // The VA range ends at 0x1000.
         image.entries[0].va.start = 0x2000;
+        // Without data, the VA range is the section's only defect.
+        image.entries[0].data.end = 68;
         assert_eq!(image.parse().err(), Some(EINVAL));
     }
 
@@ -920,4 +930,38 @@ fn overlapping_sections() {
         image.entries[1].va = second..second + 0x3000;
         assert_eq!(image.parse().err(), Some(EINVAL));
     }
+
+    /// Section data that is larger than the VA range of the section is rejected.
+    #[test]
+    fn section_data_larger_than_va_range() {
+        let mut image = Image::new();
+        // The first section spans one page and its data starts at byte 68. The file holds one page
+        // and one byte of data.
+        image.data_size = 0x1001;
+
+        // One page of data fits.
+        image.entries[0].data.end = 68 + 0x1000;
+        assert!(image.parse().is_ok());
+
+        // One more byte does not.
+        image.entries[0].data.end = 68 + 0x1001;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+    }
+
+    /// Section data in a section with an empty VA range is rejected.
+    #[test]
+    fn section_data_in_empty_va_range() -> Result {
+        let mut image = Image::new();
+        image.entries[0].va.end = 0x0;
+
+        // Without data, the section is skipped.
+        image.entries[0].data.end = 68;
+        assert_eq!(image.parse()?.len(), 1);
+
+        // With 4 bytes of data, it is rejected.
+        image.entries[0].data.end = 72;
+        assert_eq!(image.parse().err(), Some(EINVAL));
+
+        Ok(())
+    }
 }

-- 
2.56.0


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

* Re: [PATCH RESEND 0/3] drm/tyr: KUnit tests and fixes for the firmware parser
  2026-11-27 18:01 [PATCH RESEND 0/3] drm/tyr: KUnit tests and fixes for the firmware parser Daniel Almeida
                   ` (2 preceding siblings ...)
  2026-11-27 18:01 ` [PATCH RESEND 3/3] drm/tyr: reject firmware sections with oversized data Daniel Almeida
@ 2026-11-27 19:40 ` Daniel Almeida
  3 siblings, 0 replies; 5+ messages in thread
From: Daniel Almeida @ 2026-11-27 19:40 UTC (permalink / raw)
  To: dakr, simona, airlied, aliceryhl
  Cc: Sami Tolvanen, dri-devel, linux-kernel, rust-for-linux

Sami,

Can I add your co-developed-by + sob for patches 2-3?
They are basically your fixes + tests

—Daniel

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

end of thread, other threads:[~2026-11-27 19:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 18:01 [PATCH RESEND 0/3] drm/tyr: KUnit tests and fixes for the firmware parser Daniel Almeida
2026-11-27 18:01 ` [PATCH RESEND 1/3] drm/tyr: add KUnit tests " Daniel Almeida
2026-11-27 18:01 ` [PATCH RESEND 2/3] drm/tyr: reject firmware sections that overlap Daniel Almeida
2026-11-27 18:01 ` [PATCH RESEND 3/3] drm/tyr: reject firmware sections with oversized data Daniel Almeida
2026-11-27 19:40 ` [PATCH RESEND 0/3] drm/tyr: KUnit tests and fixes for the firmware parser Daniel Almeida

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®