@@ -841,7 +841,12 @@ impl CoffHeader {
841
841
+ symbol:: SymbolTable :: size ( self . number_of_symbol_table as usize ) ;
842
842
843
843
let length_field_size = core:: mem:: size_of :: < u32 > ( ) ;
844
- let length = bytes. pread_with :: < u32 > ( offset, scroll:: LE ) ? as usize - length_field_size;
844
+ let length = bytes
845
+ . pread_with :: < u32 > ( offset, scroll:: LE ) ?
846
+ . checked_sub ( length_field_size as u32 )
847
+ . ok_or ( error:: Error :: Malformed ( format ! (
848
+ "COFF length field size ({length_field_size:#x}) is larger than the parsed length value"
849
+ ) ) ) ? as usize ;
845
850
846
851
// The offset needs to be advanced in order to read the strings.
847
852
offset += length_field_size;
@@ -1374,7 +1379,10 @@ pub fn machine_to_str(machine: u16) -> &'static str {
1374
1379
mod tests {
1375
1380
use crate :: {
1376
1381
error,
1377
- pe:: header:: { DosStub , TeHeader } ,
1382
+ pe:: {
1383
+ header:: { DosStub , TeHeader } ,
1384
+ Coff ,
1385
+ } ,
1378
1386
} ;
1379
1387
1380
1388
use super :: {
@@ -1596,6 +1604,14 @@ mod tests {
1596
1604
0x00 ,
1597
1605
] ;
1598
1606
1607
+ /// An invalid small COFF object file
1608
+ ///
1609
+ /// https://github.com/m4b/goblin/issues/450
1610
+ const INVALID_COFF_OBJECT : [ u8 ; 20 ] = [
1611
+ 0x4C , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1612
+ 0x00 , 0x0F , 0x00 , 0xFF , 0x80 ,
1613
+ ] ;
1614
+
1599
1615
/// Malformed very small TE with valid TE magic.
1600
1616
///
1601
1617
/// https://github.com/m4b/goblin/issues/450
@@ -1748,6 +1764,20 @@ mod tests {
1748
1764
}
1749
1765
}
1750
1766
1767
+ #[ test]
1768
+ fn parse_invalid_small_coff ( ) {
1769
+ let header = Coff :: parse ( & INVALID_COFF_OBJECT ) ;
1770
+ assert_eq ! ( header. is_err( ) , true ) ;
1771
+ if let Err ( error:: Error :: Malformed ( msg) ) = header {
1772
+ assert_eq ! (
1773
+ msg,
1774
+ "COFF length field size (0x4) is larger than the parsed length value"
1775
+ ) ;
1776
+ } else {
1777
+ panic ! ( "Expected a Malformed error but got {:?}" , header) ;
1778
+ }
1779
+ }
1780
+
1751
1781
fn parse_with_omitted_dos_stub ( ) {
1752
1782
let header = Header :: parse ( & HEADER_WITH_OMITTED_DOS_STUB ) . unwrap ( ) ;
1753
1783
0 commit comments