Commit c1fc80ca by Alexander Popov

Declare variables closer to their usage

parent 271e6bf0
...@@ -64,15 +64,6 @@ from .__about__ import __version__ ...@@ -64,15 +64,6 @@ from .__about__ import __version__
# pylint: disable=line-too-long,bad-whitespace,too-many-branches # pylint: disable=line-too-long,bad-whitespace,too-many-branches
# pylint: disable=too-many-statements,global-statement # pylint: disable=too-many-statements,global-statement
# Report modes:
# * verbose mode for
# - reporting about unknown kernel options in the config
# - verbose printing of ComplexOptCheck items
# * json mode for printing the results in JSON format
report_modes = ['verbose', 'json']
supported_archs = ['X86_64', 'X86_32', 'ARM64', 'ARM']
class OptCheck: class OptCheck:
def __init__(self, reason, decision, name, expected): def __init__(self, reason, decision, name, expected):
...@@ -228,14 +219,14 @@ class AND(ComplexOptCheck): ...@@ -228,14 +219,14 @@ class AND(ComplexOptCheck):
sys.exit('[!] ERROR: invalid AND check') sys.exit('[!] ERROR: invalid AND check')
def detect_arch(fname): def detect_arch(fname, archs):
with open(fname, 'r') as f: with open(fname, 'r') as f:
arch_pattern = re.compile("CONFIG_[a-zA-Z0-9_]*=y") arch_pattern = re.compile("CONFIG_[a-zA-Z0-9_]*=y")
arch = None arch = None
for line in f.readlines(): for line in f.readlines():
if arch_pattern.match(line): if arch_pattern.match(line):
option, _ = line[7:].split('=', 1) option, _ = line[7:].split('=', 1)
if option in supported_archs: if option in archs:
if not arch: if not arch:
arch = option arch = option
else: else:
...@@ -606,12 +597,13 @@ def parse_config_file(parsed_options, fname): ...@@ -606,12 +597,13 @@ def parse_config_file(parsed_options, fname):
def main(): def main():
mode = None # Report modes:
arch = None # * verbose mode for
kernel_version = None # - reporting about unknown kernel options in the config
config_checklist = [] # - verbose printing of ComplexOptCheck items
parsed_options = OrderedDict() # * json mode for printing the results in JSON format
report_modes = ['verbose', 'json']
supported_archs = ['X86_64', 'X86_32', 'ARM64', 'ARM']
parser = ArgumentParser(prog='kconfig-hardened-check', parser = ArgumentParser(prog='kconfig-hardened-check',
description='Checks the hardening options in the Linux kernel config') description='Checks the hardening options in the Linux kernel config')
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__) parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
...@@ -623,16 +615,19 @@ def main(): ...@@ -623,16 +615,19 @@ def main():
help='choose the report mode') help='choose the report mode')
args = parser.parse_args() args = parser.parse_args()
mode = None
if args.mode: if args.mode:
mode = args.mode mode = args.mode
if mode != 'json': if mode != 'json':
print("[+] Special report mode: {}".format(mode)) print("[+] Special report mode: {}".format(mode))
config_checklist = []
if args.config: if args.config:
if mode != 'json': if mode != 'json':
print('[+] Config file to check: {}'.format(args.config)) print('[+] Config file to check: {}'.format(args.config))
arch, msg = detect_arch(args.config) arch, msg = detect_arch(args.config, supported_archs)
if not arch: if not arch:
sys.exit('[!] ERROR: {}'.format(msg)) sys.exit('[!] ERROR: {}'.format(msg))
if mode != 'json': if mode != 'json':
...@@ -645,6 +640,7 @@ def main(): ...@@ -645,6 +640,7 @@ def main():
print('[+] Detected kernel version: {}.{}'.format(kernel_version[0], kernel_version[1])) print('[+] Detected kernel version: {}.{}'.format(kernel_version[0], kernel_version[1]))
construct_checklist(config_checklist, arch) construct_checklist(config_checklist, arch)
parsed_options = OrderedDict()
parse_config_file(parsed_options, args.config) parse_config_file(parsed_options, args.config)
perform_checks(config_checklist, parsed_options, kernel_version) perform_checks(config_checklist, parsed_options, kernel_version)
......
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