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 <[email protected]> --- 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
