Update the README (arch support)
[kconfig-hardened-check.git] / kconfig-hardened-check.py
index c7b156f47cc000d11d0184de7338388d08d7ab45..f22437ac3a4c1660db3f26a6bc5e7b025984e83a 100755 (executable)
@@ -35,6 +35,9 @@ from collections import OrderedDict
 import re
 
 debug_mode = False  # set it to True to print the unknown options from the config
+
+supported_archs = [ 'X86_64' ]
+
 checklist = []
 
 
@@ -108,14 +111,33 @@ class OR:
         return False, self.result
 
 
-def construct_checklist():
+def detect_arch(fname):
+    with open(fname, 'r') as f:
+        arch_pattern = re.compile("CONFIG_[a-zA-Z0-9_]*=y")
+        arch = None
+        msg = None
+        print('[+] Trying to detect architecture in "{}"...'.format(fname))
+        for line in f.readlines():
+            if arch_pattern.match(line):
+                option, value = line[7:].split('=', 1)
+                if option in supported_archs:
+                    if not arch:
+                        arch = option
+                    else:
+                        return None, 'more than one supported architecture is detected'
+        if not arch:
+            return None, 'failed to detect architecture'
+        else:
+            return arch, 'OK'
+
+
+def construct_checklist(arch):
     modules_not_set = OptCheck('MODULES',                'is not set', 'kspp', 'cut_attack_surface')
     devmem_not_set = OptCheck('DEVMEM',                  'is not set', 'kspp', 'cut_attack_surface') # refers to LOCK_DOWN_KERNEL
 
     checklist.append(OptCheck('BUG',                         'y', 'defconfig', 'self_protection'))
     checklist.append(OptCheck('PAGE_TABLE_ISOLATION',        'y', 'defconfig', 'self_protection'))
     checklist.append(OptCheck('RETPOLINE',                   'y', 'defconfig', 'self_protection'))
-    checklist.append(OptCheck('X86_64',                      'y', 'defconfig', 'self_protection'))
     checklist.append(OptCheck('X86_SMAP',                    'y', 'defconfig', 'self_protection'))
     checklist.append(OptCheck('X86_INTEL_UMIP',              'y', 'defconfig', 'self_protection'))
     checklist.append(OR(OptCheck('STRICT_KERNEL_RWX',        'y', 'defconfig', 'self_protection'), \
@@ -127,7 +149,6 @@ def construct_checklist():
     checklist.append(OptCheck('VMAP_STACK',                  'y', 'defconfig', 'self_protection'))
     checklist.append(OptCheck('THREAD_INFO_IN_TASK',         'y', 'defconfig', 'self_protection'))
     checklist.append(OptCheck('SLUB_DEBUG',                  'y', 'defconfig', 'self_protection'))
-    checklist.append(OptCheck('LOCK_DOWN_KERNEL',            'y', 'ubuntu18', 'self_protection')) # remember about LOCK_DOWN_MANDATORY
     checklist.append(OR(OptCheck('STRICT_MODULE_RWX',        'y', 'defconfig', 'self_protection'), \
                         OptCheck('DEBUG_SET_MODULE_RONX',    'y', 'defconfig', 'self_protection'), \
                         modules_not_set)) # DEBUG_SET_MODULE_RONX was before v4.11
@@ -162,6 +183,7 @@ def construct_checklist():
     checklist.append(OptCheck('DEFAULT_MMAP_MIN_ADDR',            '65536', 'kspp', 'self_protection'))
 
     checklist.append(OptCheck('GCC_PLUGIN_STACKLEAK',             'y', 'my', 'self_protection'))
+    checklist.append(OptCheck('LOCK_DOWN_KERNEL',                 'y', 'my', 'self_protection')) # remember about LOCK_DOWN_MANDATORY
     checklist.append(OptCheck('SLUB_DEBUG_ON',                    'y', 'my', 'self_protection'))
     checklist.append(OptCheck('SECURITY_DMESG_RESTRICT',          'y', 'my', 'self_protection'))
     checklist.append(OptCheck('STATIC_USERMODEHELPER',            'y', 'my', 'self_protection')) # breaks systemd?
@@ -186,12 +208,6 @@ def construct_checklist():
     checklist.append(OptCheck('COMPAT_BRK',           'is not set', 'kspp', 'cut_attack_surface'))
     checklist.append(OptCheck('DEVKMEM',              'is not set', 'kspp', 'cut_attack_surface'))
     checklist.append(OptCheck('COMPAT_VDSO',          'is not set', 'kspp', 'cut_attack_surface'))
-    checklist.append(OptCheck('X86_PTDUMP',           'is not set', 'ubuntu18', 'cut_attack_surface'))
-    checklist.append(OptCheck('ZSMALLOC_STAT',        'is not set', 'ubuntu18', 'cut_attack_surface'))
-    checklist.append(OptCheck('PAGE_OWNER',           'is not set', 'ubuntu18', 'cut_attack_surface'))
-    checklist.append(OptCheck('DEBUG_KMEMLEAK',       'is not set', 'ubuntu18', 'cut_attack_surface'))
-    checklist.append(OptCheck('BINFMT_AOUT',          'is not set', 'ubuntu18', 'cut_attack_surface'))
-    checklist.append(OptCheck('MMIOTRACE_TEST',       'is not set', 'ubuntu18', 'cut_attack_surface')) # refers to LOCK_DOWN_KERNEL
     checklist.append(OptCheck('BINFMT_MISC',          'is not set', 'kspp', 'cut_attack_surface'))
     checklist.append(OptCheck('INET_DIAG',            'is not set', 'kspp', 'cut_attack_surface'))
     checklist.append(OptCheck('KEXEC',                'is not set', 'kspp', 'cut_attack_surface')) # refers to LOCK_DOWN_KERNEL
@@ -202,6 +218,11 @@ def construct_checklist():
     checklist.append(OptCheck('MODIFY_LDT_SYSCALL',   'is not set', 'kspp', 'cut_attack_surface'))
     checklist.append(OptCheck('HIBERNATION',          'is not set', 'kspp', 'cut_attack_surface')) # refers to LOCK_DOWN_KERNEL
 
+    checklist.append(OptCheck('X86_PTDUMP',              'is not set', 'grsecurity', 'cut_attack_surface'))
+    checklist.append(OptCheck('ZSMALLOC_STAT',           'is not set', 'grsecurity', 'cut_attack_surface'))
+    checklist.append(OptCheck('PAGE_OWNER',              'is not set', 'grsecurity', 'cut_attack_surface'))
+    checklist.append(OptCheck('DEBUG_KMEMLEAK',          'is not set', 'grsecurity', 'cut_attack_surface'))
+    checklist.append(OptCheck('BINFMT_AOUT',             'is not set', 'grsecurity', 'cut_attack_surface'))
     checklist.append(OptCheck('KPROBES',                 'is not set', 'grsecurity', 'cut_attack_surface')) # refers to LOCK_DOWN_KERNEL
     checklist.append(OptCheck('UPROBES',                 'is not set', 'grsecurity', 'cut_attack_surface'))
     checklist.append(OptCheck('GENERIC_TRACER',          'is not set', 'grsecurity', 'cut_attack_surface'))
@@ -220,6 +241,7 @@ def construct_checklist():
     checklist.append(OptCheck('ACPI_APEI_EINJ',       'is not set', 'lockdown', 'cut_attack_surface')) # refers to LOCK_DOWN_KERNEL
     checklist.append(OptCheck('PROFILING',            'is not set', 'lockdown', 'cut_attack_surface')) # refers to LOCK_DOWN_KERNEL
     checklist.append(OptCheck('BPF_SYSCALL',          'is not set', 'lockdown', 'cut_attack_surface')) # refers to LOCK_DOWN_KERNEL
+    checklist.append(OptCheck('MMIOTRACE_TEST',       'is not set', 'lockdown', 'cut_attack_surface')) # refers to LOCK_DOWN_KERNEL
 
     checklist.append(OptCheck('MMIOTRACE',            'is not set', 'my', 'cut_attack_surface')) # refers to LOCK_DOWN_KERNEL (permissive)
     checklist.append(OptCheck('KEXEC_FILE',           'is not set', 'my', 'cut_attack_surface')) # refers to LOCK_DOWN_KERNEL (permissive)
@@ -235,8 +257,8 @@ def construct_checklist():
 #   checklist.append(OptCheck('LKDTM',    'm', 'my', 'feature_test'))
 
 
-def print_checklist():
-    print('[+] Printing kernel hardening preferences...')
+def print_checklist(arch):
+    print('[+] Printing kernel hardening preferences for {}...'.format(arch))
     print('  {:<39}|{:^13}|{:^10}|{:^20}'.format(
         'option name', 'desired val', 'decision', 'reason'))
     print('  ' + '=' * 86)
@@ -308,21 +330,25 @@ def check_config_file(fname):
 
 if __name__ == '__main__':
     parser = ArgumentParser(description='Checks the hardening options in the Linux kernel config')
-    parser.add_argument('-p', '--print', action='store_true', help='print hardening preferences')
-    parser.add_argument('-c', '--config', help='check the config_file against these preferences')
-    parser.add_argument('--debug', action='store_true', help='enable internal debug mode')
+    parser.add_argument('-p', '--print', choices=supported_archs,
+                        help='print hardening preferences for selected architecture')
+    parser.add_argument('-c', '--config',
+                        help='check the config_file against these preferences')
+    parser.add_argument('--debug', action='store_true',
+                        help='enable internal debug mode')
     args = parser.parse_args()
 
-    construct_checklist()
-
-    if args.print:
-        print_checklist()
-        sys.exit(0)
-
     if args.debug:
         debug_mode = True
 
     if args.config:
+        arch, msg = detect_arch(args.config)
+        if not arch:
+            sys.exit('[!] ERROR: {}'.format(msg))
+        else:
+            print('[+] Detected architecture: {}'.format(arch))
+
+        construct_checklist(arch)
         check_config_file(args.config)
         error_count = len(list(filter(lambda opt: opt.result.startswith('FAIL'), checklist)))
         if error_count == 0:
@@ -331,4 +357,10 @@ if __name__ == '__main__':
         else:
             sys.exit('[-] config check is NOT PASSED: {} errors'.format(error_count))
 
+    if args.print:
+        arch = args.print
+        construct_checklist(arch)
+        print_checklist(arch)
+        sys.exit(0)
+
     parser.print_help()