fix(format): dataspaces and contiguous storage as libhdf5 reads them
- A simple dataspace of rank 0 holds one element in libhdf5 (the product
of no dimensions; h5py reads it as shape ()). num_elements() said 0, so
cve-2020-18494's /dset1 failed with DataSizeMismatch { expected: 0 }.
- A contiguous dataset whose storage is larger than its elements reads:
libhdf5 reads the elements from the start of the storage and ignores the
rest (H5D__contig_check checks only that they fit in the file). We
required the sizes to be equal, so the scalar /Dset1 of cve-2024-32623
and cve-2025-2309 (240 bytes of storage for one int) failed. Storage too
small for the elements is still an error. data_read::contiguous_read_len
is the rule, used by every contiguous read path.
- Dataspace::parse refuses what H5O__sdspace_decode refuses: more than 32
dimensions, a rank on a scalar or null dataspace, a dimension larger
than its maximum (new FormatError::InvalidDataspace).
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
@@ -7,6 +7,9 @@ use alloc::vec::Vec;
|
||||
|
||||
use crate::error::FormatError;
|
||||
|
||||
/// Most dimensions a dataspace can have (`H5S_MAX_RANK`).
|
||||
pub const MAX_RANK: u8 = 32;
|
||||
|
||||
/// Type of dataspace.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum DataspaceType {
|
||||
@@ -67,6 +70,12 @@ impl Dataspace {
|
||||
let version = data[0];
|
||||
let rank = data[1];
|
||||
let flags = data[2];
|
||||
// H5O__sdspace_decode's checks.
|
||||
if rank > MAX_RANK {
|
||||
return Err(FormatError::InvalidDataspace(
|
||||
"simple dataspace dimensionality is too large",
|
||||
));
|
||||
}
|
||||
|
||||
let (space_type, header_size) = match version {
|
||||
1 => {
|
||||
@@ -88,6 +97,11 @@ impl Dataspace {
|
||||
2 => DataspaceType::Null,
|
||||
_ => return Err(FormatError::InvalidDataspaceType(type_byte)),
|
||||
};
|
||||
if st != DataspaceType::Simple && rank > 0 {
|
||||
return Err(FormatError::InvalidDataspace(
|
||||
"invalid rank for scalar or NULL dataspace",
|
||||
));
|
||||
}
|
||||
(st, 4usize)
|
||||
}
|
||||
_ => return Err(FormatError::InvalidDataspaceVersion(version)),
|
||||
@@ -107,8 +121,13 @@ impl Dataspace {
|
||||
// Read max dimensions if flags bit 0 is set
|
||||
let max_dimensions = if flags & 0x01 != 0 {
|
||||
let mut max_dims = Vec::with_capacity(rank as usize);
|
||||
for _ in 0..rank {
|
||||
for i in 0..rank as usize {
|
||||
let val = read_length(data, pos, length_size)?;
|
||||
if dimensions[i] > val {
|
||||
return Err(FormatError::InvalidDataspace(
|
||||
"dataspace dimension size is greater than its maximum size",
|
||||
));
|
||||
}
|
||||
max_dims.push(val);
|
||||
pos += ls;
|
||||
}
|
||||
@@ -176,7 +195,6 @@ impl Dataspace {
|
||||
match self.space_type {
|
||||
DataspaceType::Null => Ok(0),
|
||||
DataspaceType::Scalar => Ok(1),
|
||||
DataspaceType::Simple if self.dimensions.is_empty() => Ok(0),
|
||||
DataspaceType::Simple => self
|
||||
.dimensions
|
||||
.iter()
|
||||
@@ -195,18 +213,14 @@ impl Dataspace {
|
||||
match self.space_type {
|
||||
DataspaceType::Null => 0,
|
||||
DataspaceType::Scalar => 1,
|
||||
DataspaceType::Simple => {
|
||||
if self.dimensions.is_empty() {
|
||||
0
|
||||
} else {
|
||||
// Saturate rather than wrap: a wrapped product could
|
||||
// under-size a buffer. Size-critical callers use
|
||||
// `checked_num_elements`.
|
||||
self.dimensions
|
||||
.iter()
|
||||
.fold(1u64, |acc, &d| acc.saturating_mul(d))
|
||||
}
|
||||
}
|
||||
// A simple dataspace of rank 0 holds one element, as in libhdf5
|
||||
// (the product of no dimensions). Saturate rather than wrap: a
|
||||
// wrapped product could under-size a buffer. Size-critical
|
||||
// callers use `checked_num_elements`.
|
||||
DataspaceType::Simple => self
|
||||
.dimensions
|
||||
.iter()
|
||||
.fold(1u64, |acc, &d| acc.saturating_mul(d)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -352,4 +366,44 @@ mod tests {
|
||||
let ds = Dataspace::parse(&data, 8).unwrap();
|
||||
assert_eq!(ds.max_dimensions, Some(vec![10]));
|
||||
}
|
||||
|
||||
/// A simple dataspace of rank 0 (cve-2020-18494's `/dset1`) holds one
|
||||
/// element in libhdf5, which h5py reads as shape `()`. It was 0.
|
||||
#[test]
|
||||
fn simple_rank_zero_holds_one_element() {
|
||||
let data = build_v2_dataspace(0, 0, 1, &[], None);
|
||||
let ds = Dataspace::parse(&data, 8).unwrap();
|
||||
assert_eq!(ds.space_type, DataspaceType::Simple);
|
||||
assert_eq!(ds.num_elements(), 1);
|
||||
assert_eq!(ds.checked_num_elements().unwrap(), 1);
|
||||
}
|
||||
|
||||
/// `H5O__sdspace_decode`'s checks.
|
||||
#[test]
|
||||
fn refuses_what_libhdf5_refuses() {
|
||||
let too_many = build_v2_dataspace(33, 0, 1, &[1; 33], None);
|
||||
assert!(matches!(
|
||||
Dataspace::parse(&too_many, 8),
|
||||
Err(FormatError::InvalidDataspace(_))
|
||||
));
|
||||
let scalar_with_rank = build_v2_dataspace(1, 0, 0, &[4], None);
|
||||
assert!(matches!(
|
||||
Dataspace::parse(&scalar_with_rank, 8),
|
||||
Err(FormatError::InvalidDataspace(_))
|
||||
));
|
||||
let null_with_rank = build_v2_dataspace(1, 0, 2, &[4], None);
|
||||
assert!(matches!(
|
||||
Dataspace::parse(&null_with_rank, 8),
|
||||
Err(FormatError::InvalidDataspace(_))
|
||||
));
|
||||
let over_max = build_v1_dataspace(2, 0x01, &[5, 20], Some(&[10, 10]));
|
||||
assert!(matches!(
|
||||
Dataspace::parse(&over_max, 8),
|
||||
Err(FormatError::InvalidDataspace(_))
|
||||
));
|
||||
// 32 dimensions, and a size equal to the maximum or unlimited, are fine.
|
||||
assert!(Dataspace::parse(&build_v2_dataspace(32, 0, 1, &[1; 32], None), 8).is_ok());
|
||||
let at_max = build_v1_dataspace(2, 0x01, &[10, 20], Some(&[10, u64::MAX]));
|
||||
assert!(Dataspace::parse(&at_max, 8).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user