Commit 15981bcf authored by Jérémy Bobbio's avatar Jérémy Bobbio

Prevent CBFS comparator from scanning big files needlessly

To recognize CBFS images, we fall back on looking for the magic number
by scanning the entire file. This is expensive! In order to speed up
diffoscope, we assume that any file bigger than 24 MiB will not be a CBFS
image. That's a reasonable assumption given that, as of today, the biggest
image produced by coreboot is 16 MiB.

We also now try to identify CBFS images last. This means other formats have a
chance to be recognized before the file gets fully scanned.

Thanks Mike Hommey for the detailed bug report and suggestions.

Closes: #807997
parent 7e717af5
......@@ -129,7 +129,6 @@ FILE_CLASSES = (
DebDataTarFile,
TextFile,
Bzip2File,
CbfsFile,
CpioFile,
DebFile,
DexFile,
......@@ -155,6 +154,7 @@ FILE_CLASSES = (
XzFile,
ZipFile,
ImageFile,
CbfsFile,
)
......
......@@ -82,6 +82,8 @@ CBFS_HEADER_VERSION1 = 0x31313131
CBFS_HEADER_VERSION2 = 0x31313132
CBFS_HEADER_SIZE = 8 * 4 # 8 * uint32_t
# On 2015-12-15, the largest image produced by coreboot is 16 MiB
CBFS_MAXIMUM_FILE_SIZE = 24 * 2 ** 20 # 24 MiB
def is_header_valid(buf, size, offset=0):
magic, version, romsize, bootblocksize, align, cbfs_offset, architecture, pad = struct.unpack_from('!IIIIIIII', buf, offset)
......@@ -97,7 +99,7 @@ class CbfsFile(File):
@staticmethod
def recognizes(file):
size = os.stat(file.path).st_size
if size < CBFS_HEADER_SIZE:
if size < CBFS_HEADER_SIZE or size > CBFS_MAXIMUM_FILE_SIZE:
return False
with open(file.path, 'rb') as f:
# pick at the latest byte as it should contain the relative offset of the header
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment