@@ -55,6 +55,8 @@ type BIOSDirectoryTableEntry struct {
5555 DestinationAddress uint64
5656}
5757
58+ const BIOSDirectoryTableEntrySize = 16
59+
5860// BIOSDirectoryTableHeader represents a BIOS Directory Table Header
5961// Table 11 from (1)
6062type BIOSDirectoryTableHeader struct {
@@ -125,7 +127,7 @@ func FindBIOSDirectoryTable(image []byte) (*BIOSDirectoryTable, bytes2.Range, er
125127 break
126128 }
127129
128- table , bytesRead , err := ParseBIOSDirectoryTable (bytes . NewBuffer ( image [idx :]) )
130+ table , bytesRead , err := ParseBIOSDirectoryTable (image [idx :])
129131 if err != nil {
130132 shift := uint64 (idx + len (cookieBytes ))
131133 image = image [shift :]
@@ -134,13 +136,15 @@ func FindBIOSDirectoryTable(image []byte) (*BIOSDirectoryTable, bytes2.Range, er
134136 }
135137 return table , bytes2.Range {Offset : offset + uint64 (idx ), Length : bytesRead }, err
136138 }
137- return nil , bytes2.Range {}, fmt .Errorf ("EmbeddedFirmwareStructure is not found" )
139+ return nil , bytes2.Range {}, fmt .Errorf ("BIOSDirectoryTable is not found" )
138140}
139141
140142// ParseBIOSDirectoryTable converts input bytes into BIOSDirectoryTable
141- func ParseBIOSDirectoryTable (r io. Reader ) (* BIOSDirectoryTable , uint64 , error ) {
143+ func ParseBIOSDirectoryTable (data [] byte ) (* BIOSDirectoryTable , uint64 , error ) {
142144 var table BIOSDirectoryTable
143145 var totalLength uint64
146+
147+ r := bytes .NewBuffer (data )
144148 if err := readAndCountSize (r , binary .LittleEndian , & table .BIOSCookie , & totalLength ); err != nil {
145149 return nil , 0 , err
146150 }
@@ -151,13 +155,19 @@ func ParseBIOSDirectoryTable(r io.Reader) (*BIOSDirectoryTable, uint64, error) {
151155 if err := readAndCountSize (r , binary .LittleEndian , & table .Checksum , & totalLength ); err != nil {
152156 return nil , 0 , err
153157 }
158+
154159 if err := readAndCountSize (r , binary .LittleEndian , & table .TotalEntries , & totalLength ); err != nil {
155160 return nil , 0 , err
156161 }
157162 if err := readAndCountSize (r , binary .LittleEndian , & table .Reserved , & totalLength ); err != nil {
158163 return nil , 0 , err
159164 }
160165
166+ sizeRequired := uint64 (table .TotalEntries ) * BIOSDirectoryTableEntrySize
167+ if uint64 (r .Len ()) < sizeRequired {
168+ return nil , 0 , fmt .Errorf ("not enough data, required: %d, actual: %d" , sizeRequired + totalLength , len (data ))
169+ }
170+
161171 table .Entries = make ([]BIOSDirectoryTableEntry , 0 , table .TotalEntries )
162172 for idx := uint32 (0 ); idx < table .TotalEntries ; idx ++ {
163173 entry , length , err := ParseBIOSDirectoryTableEntry (r )
0 commit comments