From 560b276f7e9938475af921c8ebd4cd05910dbf31 Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Fri, 6 Dec 2024 20:45:36 +0100 Subject: [PATCH] rust/pvimg: Fix possible 'range start index out of range for slice' error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix possible 'range start index 16 out of range for slice of length 0' error by adding a check of the slice data length. Fixes: f4cf4ae6ebb1 ("rust: Add a new tool called 'pvimg'") Reviewed-by: Steffen Eiden Signed-off-by: Marc Hartmayer Signed-off-by: Jan Höppner --- rust/pvimg/src/pv_utils/se_hdr/brb.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rust/pvimg/src/pv_utils/se_hdr/brb.rs b/rust/pvimg/src/pv_utils/se_hdr/brb.rs index f7ae1bc9..ac3a2e6e 100644 --- a/rust/pvimg/src/pv_utils/se_hdr/brb.rs +++ b/rust/pvimg/src/pv_utils/se_hdr/brb.rs @@ -259,6 +259,10 @@ impl SeHdr { return Err(Error::InvalidSeHdr); } + if sehs <= common_size { + return Err(Error::InvalidSeHdr); + } + data.resize(sehs, 0); reader.read_exact(&mut data[common_size..])?; Self::try_from_data(&data) @@ -366,3 +370,22 @@ impl AeadCipherTrait for SeHdrPlain { self.data.aead_tag_size() } } + +#[cfg(test)] +mod tests { + use std::io::Cursor; + + use super::SeHdr; + use crate::error::Error; + + #[test] + fn test_sehdr_try_from_io() { + // Invalid SeHdr as `sehs` is set to 0 + assert!(matches!( + SeHdr::try_from_io(Cursor::new([ + 73, 66, 77, 83, 101, 99, 69, 120, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 8 + ])), + Err(Error::InvalidSeHdr) + )); + } +}