X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=kernel_hardening_checker%2Fengine.py;h=9275fd26a91f75da2355050a198415caabcb6e6e;hb=5a977616f26cdaa334a71bc56265e117b9036a05;hp=0bf0ad560f09ccd0950e14f12dd4f625d31a391c;hpb=616d9f017fb5c87f466b6766e15a497308770b02;p=kconfig-hardened-check.git diff --git a/kernel_hardening_checker/engine.py b/kernel_hardening_checker/engine.py index 0bf0ad5..9275fd2 100644 --- a/kernel_hardening_checker/engine.py +++ b/kernel_hardening_checker/engine.py @@ -11,9 +11,12 @@ This module is the engine of checks. # pylint: disable=missing-class-docstring,missing-function-docstring # pylint: disable=line-too-long,invalid-name,too-many-branches -from typing import Dict, Tuple import sys +from typing import Optional, OrderedDict, Dict, List, Tuple +StrOrNone = Optional[str] +TupleOrNone = Optional[Tuple] + GREEN_COLOR = '\x1b[32m' RED_COLOR = '\x1b[31m' COLOR_END = '\x1b[0m' @@ -115,6 +118,7 @@ class OptCheck: "reason": self.reason, } if with_results: + assert self.result, f'unexpected empty result in {self.name}' dump["check_result"] = self.result dump["check_result_bool"] = self.result.startswith('OK') return dump @@ -230,6 +234,7 @@ class ComplexOptCheck: dump = self.opts[0].json_dump(False) if with_results: # Add the 'check_result' and 'check_result_bool' keys to the dictionary + assert self.result, f'unexpected empty result in {self.name}' dump["check_result"] = self.result dump["check_result_bool"] = self.result.startswith('OK') return dump @@ -348,3 +353,26 @@ def override_expected_value(checklist, name, new_val): def perform_checks(checklist): for opt in checklist: opt.check() + + +def print_unknown_options(checklist: List, parsed_options: OrderedDict[str, str], opt_type: str) -> None: + known_options = [] + + for o1 in checklist: + if o1.opt_type != 'complex': + known_options.append(o1.name) + continue + for o2 in o1.opts: + if o2.opt_type != 'complex': + if hasattr(o2, 'name'): + known_options.append(o2.name) + continue + for o3 in o2.opts: + assert(o3.opt_type != 'complex'), \ + f'unexpected ComplexOptCheck inside {o2.name}' + if hasattr(o3, 'name'): + known_options.append(o3.name) + + for option, value in parsed_options.items(): + if option not in known_options: + print(f'[?] No check for {opt_type} option {option} ({value})')