rust/pvimg: Fix possible 'range start index out of range for slice' error

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: f4cf4ae6eb ("rust: Add a new tool called 'pvimg'")
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Marc Hartmayer
2024-12-06 20:45:36 +01:00
committed by Jan Höppner
parent a19c43f613
commit 560b276f7e

View File

@@ -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)
));
}
}