Parse all numbers of the kernel version
authorAlexander Popov <alex.popov@linux.com>
Sat, 9 Mar 2024 17:27:08 +0000 (20:27 +0300)
committerAlexander Popov <alex.popov@linux.com>
Sat, 9 Mar 2024 18:15:50 +0000 (21:15 +0300)
Refers to #88, #89, #97

kernel_hardening_checker/__init__.py

index 4855a59445bca6e120e8347aa0a1dba1994e1705..48410057d6fbf86bc65277e812d1cedb0607061f 100644 (file)
@@ -55,10 +55,11 @@ def detect_kernel_version(fname):
                 parts = line.split()
                 ver_str = parts[2].split('-', 1)[0]
                 ver_numbers = ver_str.split('.')
-                if len(ver_numbers) < 3 or not ver_numbers[0].isdigit() or not ver_numbers[1].isdigit():
-                    msg = f'failed to parse the version "{ver_str}"'
-                    return None, msg
-                return (int(ver_numbers[0]), int(ver_numbers[1])), None
+                if len(ver_numbers) >= 3:
+                    if all(map(lambda x: x.isdigit(), ver_numbers)):
+                        return tuple(map(int, ver_numbers)), None
+                msg = f'failed to parse the version "{parts[2]}"'
+                return None, msg
         return None, 'no kernel version detected'
 
 
@@ -285,7 +286,7 @@ def main():
                 print('[!] Hint: provide the kernel version file through --kernel-version option')
             sys.exit(f'[!] ERROR: {msg}')
         if mode != 'json':
-            print(f'[+] Detected kernel version: {kernel_version[0]}.{kernel_version[1]}')
+            print(f'[+] Detected kernel version: {kernel_version}')
 
         compiler, msg = detect_compiler(args.config)
         if mode != 'json':