Skip to content

Commit ba676e1

Browse files
committed
Fix potential integer underflow in variable-length table calculations
A table with an invalid `length` field can in theory cause out-of-bounds accesses upon iteration of the table's entries. This will apply to all variable-sized tables, so should be kept in mind for future additions.
1 parent 86f4561 commit ba676e1

3 files changed

Lines changed: 3 additions & 3 deletions

File tree

src/sdt/madt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl Madt {
5656
let ptr = unsafe { Pin::into_inner_unchecked(self) as *const Madt as *const u8 };
5757
MadtEntryIter {
5858
pointer: unsafe { ptr.add(mem::size_of::<Madt>()) },
59-
remaining_length: self.header.length - mem::size_of::<Madt>() as u32,
59+
remaining_length: self.header.length.saturating_sub(mem::size_of::<Madt>() as u32),
6060
_phantom: PhantomData,
6161
}
6262
}

src/sdt/mcfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Mcfg {
2424
/// Returns a slice containing each of the entries in the MCFG table. Where possible, `PlatformInfo.interrupt_model` should
2525
/// be enumerated instead.
2626
pub fn entries(&self) -> &[McfgEntry] {
27-
let length = self.header.length as usize - mem::size_of::<Mcfg>();
27+
let length = (self.header.length as usize).saturating_sub(mem::size_of::<Mcfg>());
2828

2929
// Intentionally round down in case length isn't an exact multiple of McfgEntry size - this
3030
// has been observed on real hardware (see rust-osdev/acpi#58)

src/sdt/srat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Srat {
3535
let ptr = unsafe { Pin::into_inner_unchecked(self) as *const Srat as *const u8 };
3636
SratEntryIter {
3737
pointer: unsafe { ptr.add(mem::size_of::<Srat>()) },
38-
remaining_length: self.header.length - mem::size_of::<Srat>() as u32,
38+
remaining_length: self.header.length.saturating_sub(mem::size_of::<Srat>() as u32),
3939
_phantom: PhantomData,
4040
}
4141
}

0 commit comments

Comments
 (0)