Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
work around crash in case of a corrupt Linux kernel image
Browse files Browse the repository at this point in the history
  • Loading branch information
armijnhemel committed Dec 6, 2016
1 parent 71673d7 commit ab25b36
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/bat/identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def extractC(filepath, tags, scanenv, filesize, stringcutoff, linuxkernel, black
## Kernel symbols recorded in the image could lead to false positives,
## so they first have to be found and be blacklisted.
kernelfile = open(filepath, 'r')
validkernelfile = True
## TODO: this is inefficient
kerneldata = kernelfile.read()
kernelfile.close()
Expand Down Expand Up @@ -234,32 +235,37 @@ def extractC(filepath, tags, scanenv, filesize, stringcutoff, linuxkernel, black
## or a NULL character.
offset = jiffy_pos + len('loops_per_jiffy')
lastnull = offset + 1
lenkerneldata = len(kerneldata)
while True:
if offset == lenkerneldata:
validkernelfile = False
break
if not kerneldata[offset] in string.printable:
if not kerneldata[offset] == chr(0x00):
break
else:
lastnull = offset
offset += 1

if extractor.check_null(kerneldata, jiffy_pos, 'loops_per_jiffy'):
## loops_per_jiffy is not the first symbol in the list
## so work backwards
offset = jiffy_pos
firstnull = jiffy_pos - 1
if validkernelfile:
if extractor.check_null(kerneldata, jiffy_pos, 'loops_per_jiffy'):
## loops_per_jiffy is not the first symbol in the list
## so work backwards
offset = jiffy_pos
firstnull = jiffy_pos - 1

while True:
if not kerneldata[offset] in string.printable:
if not kerneldata[offset] == chr(0x00):
break
else:
firstnull = offset
offset -= 1
else:
firstnull = jiffy_pos
kernelsymdata = kerneldata[firstnull:lastnull]
kernelsymbols = filter(lambda x: x != '', kernelsymdata.split('\x00'))
blacklist.append((firstnull,lastnull))
while True:
if not kerneldata[offset] in string.printable:
if not kerneldata[offset] == chr(0x00):
break
else:
firstnull = offset
offset -= 1
else:
firstnull = jiffy_pos
kernelsymdata = kerneldata[firstnull:lastnull]
kernelsymbols = filter(lambda x: x != '', kernelsymdata.split('\x00'))
blacklist.append((firstnull,lastnull))

## If part of the file is blacklisted the blacklisted byte ranges
## should be ignored. Examples are firmwares, where there is a
Expand Down

0 comments on commit ab25b36

Please sign in to comment.