format: bound Storage reads that hostile size fields could stretch
On a backend without the file in memory, a structure read whose length comes from untrusted header fields was clamped only by the end of the file, so a crafted size made one read (and copy) of up to the rest of the file. Each such read now covers what the parser actually uses: - local heap names: read in growing pieces (64 bytes first, then 4x) up to the end of the data segment, instead of the rest of the segment per name (quadratic for a big symbol-table group); - fractal heap indirect blocks: the doubling-table geometry locates the entry covering the object, and the first read ends at that entry; only if it is unallocated does the walk read the rest of the block (it visits every entry then). One walk implementation serves both; - paged fixed/extensible array data blocks over 1 MiB: the prefix and page bitmap, then each page in use on its own (smaller blocks are still one read); - blocks under one checksum (non-paged array data blocks, extensible array index and super blocks): the bounds check that comes first (the checksum's; the page bitmap's for a super block) is made against the file length before reading (Window::check_extent), so a block claimed past the end of the file costs no read. With the checksum feature off the parser has no such first check and the old read stands. Other windows were already bounded (the superblock and object header prefixes, the fractal heap header by a u16, SOHM tables by u8/u16 counts) or are exact reads checked against the file length first. In memory nothing changes: the pieces are borrowed slices. Tests: CountingStorage over a crafted heap (16 MiB file, width and rows 0xFFFF: under 1 KiB read, 16.7 MB before), a heap segment claiming 64 MiB (one 64-byte read per short name), long names at every piece boundary, a fixed array block claimed past the end of a 16 MiB file (under 64 bytes read), and in the equivalence harness an h5py file with a 2.4 MB fixed array block and a >1 MiB extensible array block, whole and cut at 97 points: every chunk index agrees with the slice read and the largest takes 205 KB (2.4 MB and 1.2 MB when read whole). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
@@ -235,6 +235,12 @@ fn short_read() -> FormatError {
|
||||
)
|
||||
}
|
||||
|
||||
/// Largest paged data block (fixed or extensible array) read in one piece.
|
||||
/// A bigger one is read as its prefix and then page by page, only the pages
|
||||
/// in use, so a block whose size fields claim more than the file holds
|
||||
/// costs no more than the pages it really has.
|
||||
pub(crate) const PAGED_BLOCK_ONE_READ_MAX: usize = 1 << 20;
|
||||
|
||||
/// A window of the file: up to `max` bytes read at `base`, fewer only at
|
||||
/// the end of the file. Its [`Window::ensure`] reports a bounds failure
|
||||
/// exactly as the whole-file check `ensure_len(file_data, base + rel, n)`
|
||||
@@ -272,6 +278,28 @@ impl<'a> Window<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`Window::ensure`] for a window at `base` that has not been read:
|
||||
/// whether `[rel, rel + needed)` lies in the file, with the same error.
|
||||
/// Lets a parser whose first step is to check a structure's whole extent
|
||||
/// (a checksum at its end) fail before reading a structure that a
|
||||
/// hostile size field has stretched past the end of the file.
|
||||
pub fn check_extent<S: Storage + ?Sized>(
|
||||
file: &S,
|
||||
base: u64,
|
||||
rel: usize,
|
||||
needed: usize,
|
||||
) -> Result<(), FormatError> {
|
||||
let base = usize::try_from(base).unwrap_or(usize::MAX);
|
||||
let file_len = len_usize(file);
|
||||
match base.checked_add(rel).and_then(|p| p.checked_add(needed)) {
|
||||
Some(end) if end <= file_len => Ok(()),
|
||||
_ => Err(FormatError::UnexpectedEof {
|
||||
expected: base.saturating_add(rel).saturating_add(needed),
|
||||
available: file_len,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Check that `[rel, rel + needed)` (relative to `base`) is in the file.
|
||||
#[inline]
|
||||
pub fn ensure(&self, rel: usize, needed: usize) -> Result<(), FormatError> {
|
||||
|
||||
Reference in New Issue
Block a user