Add some lightweight typing
[kconfig-hardened-check.git] / kernel_hardening_checker / __init__.py
1 #!/usr/bin/env python3
2
3 """
4 This tool is for checking the security hardening options of the Linux kernel.
5
6 Author: Alexander Popov <alex.popov@linux.com>
7
8 This module performs input/output.
9 """
10
11 # pylint: disable=missing-function-docstring,line-too-long,invalid-name,too-many-branches,too-many-statements
12
13 import gzip
14 import sys
15 from argparse import ArgumentParser
16 from collections import OrderedDict
17 from typing import List, Tuple
18 import re
19 import json
20 from .__about__ import __version__
21 from .checks import add_kconfig_checks, add_cmdline_checks, normalize_cmdline_options, add_sysctl_checks
22 from .engine import populate_with_data, perform_checks, override_expected_value
23
24
25 def _open(file: str, *args, **kwargs):
26     open_method = open
27     if file.endswith('.gz'):
28         open_method = gzip.open
29
30     return open_method(file, *args, **kwargs)
31
32
33 def detect_arch(fname: str, archs: List[str]) -> Tuple:
34     with _open(fname, 'rt', encoding='utf-8') as f:
35         arch_pattern = re.compile(r"CONFIG_[a-zA-Z0-9_]+=y$")
36         arch = None
37         for line in f.readlines():
38             if arch_pattern.match(line):
39                 option, _ = line[7:].split('=', 1)
40                 if option in archs:
41                     if arch is None:
42                         arch = option
43                     else:
44                         return None, 'detected more than one microarchitecture'
45         if arch is None:
46             return None, 'failed to detect microarchitecture'
47         return arch, 'OK'
48
49
50 def detect_kernel_version(fname: str) -> Tuple:
51     with _open(fname, 'rt', encoding='utf-8') as f:
52         ver_pattern = re.compile(r"^# Linux/.+ Kernel Configuration$|^Linux version .+")
53         for line in f.readlines():
54             if ver_pattern.match(line):
55                 line = line.strip()
56                 parts = line.split()
57                 ver_str = parts[2].split('-', 1)[0]
58                 ver_numbers = ver_str.split('.')
59                 if len(ver_numbers) >= 3:
60                     if all(map(lambda x: x.isdigit(), ver_numbers)):
61                         return tuple(map(int, ver_numbers)), None
62                 msg = f'failed to parse the version "{parts[2]}"'
63                 return None, msg
64         return None, 'no kernel version detected'
65
66
67 def detect_compiler(fname: str):
68     gcc_version = None
69     clang_version = None
70     with _open(fname, 'rt', encoding='utf-8') as f:
71         for line in f.readlines():
72             if line.startswith('CONFIG_GCC_VERSION='):
73                 gcc_version = line[19:-1]
74             if line.startswith('CONFIG_CLANG_VERSION='):
75                 clang_version = line[21:-1]
76     if gcc_version is None or clang_version is None:
77         return None, 'no CONFIG_GCC_VERSION or CONFIG_CLANG_VERSION'
78     if gcc_version == '0' and clang_version != '0':
79         return f'CLANG {clang_version}', 'OK'
80     if gcc_version != '0' and clang_version == '0':
81         return f'GCC {gcc_version}', 'OK'
82     sys.exit(f'[!] ERROR: invalid GCC_VERSION and CLANG_VERSION: {gcc_version} {clang_version}')
83
84
85 def print_unknown_options(checklist, parsed_options, opt_type):
86     known_options = []
87
88     for o1 in checklist:
89         if o1.opt_type != 'complex':
90             known_options.append(o1.name)
91             continue
92         for o2 in o1.opts:
93             if o2.opt_type != 'complex':
94                 if hasattr(o2, 'name'):
95                     known_options.append(o2.name)
96                 continue
97             for o3 in o2.opts:
98                 assert(o3.opt_type != 'complex'), \
99                        f'unexpected ComplexOptCheck inside {o2.name}'
100                 if hasattr(o3, 'name'):
101                     known_options.append(o3.name)
102
103     for option, value in parsed_options.items():
104         if option not in known_options:
105             print(f'[?] No check for {opt_type} option {option} ({value})')
106
107
108 def print_checklist(mode: str, checklist, with_results: bool):
109     if mode == 'json':
110         output = []
111         for opt in checklist:
112             output.append(opt.json_dump(with_results))
113         print(json.dumps(output))
114         return
115
116     # table header
117     sep_line_len = 91
118     if with_results:
119         sep_line_len += 30
120     print('=' * sep_line_len)
121     print(f'{"option_name":^40}|{"type":^7}|{"desired_val":^12}|{"decision":^10}|{"reason":^18}', end='')
122     if with_results:
123         print('| check_result', end='')
124     print()
125     print('=' * sep_line_len)
126
127     # table contents
128     for opt in checklist:
129         if with_results:
130             if mode == 'show_ok':
131                 if not opt.result.startswith('OK'):
132                     continue
133             if mode == 'show_fail':
134                 if not opt.result.startswith('FAIL'):
135                     continue
136         opt.table_print(mode, with_results)
137         print()
138         if mode == 'verbose':
139             print('-' * sep_line_len)
140     print()
141
142     # final score
143     if with_results:
144         fail_count = len(list(filter(lambda opt: opt.result.startswith('FAIL'), checklist)))
145         fail_suppressed = ''
146         ok_count = len(list(filter(lambda opt: opt.result.startswith('OK'), checklist)))
147         ok_suppressed = ''
148         if mode == 'show_ok':
149             fail_suppressed = ' (suppressed in output)'
150         if mode == 'show_fail':
151             ok_suppressed = ' (suppressed in output)'
152         print(f'[+] Config check is finished: \'OK\' - {ok_count}{ok_suppressed} / \'FAIL\' - {fail_count}{fail_suppressed}')
153
154
155 def parse_kconfig_file(_mode, parsed_options, fname: str):
156     with _open(fname, 'rt', encoding='utf-8') as f:
157         opt_is_on = re.compile(r"CONFIG_[a-zA-Z0-9_]+=.+$")
158         opt_is_off = re.compile(r"# CONFIG_[a-zA-Z0-9_]+ is not set$")
159
160         for line in f.readlines():
161             line = line.strip()
162             option = None
163             value = None
164
165             if opt_is_on.match(line):
166                 option, value = line.split('=', 1)
167                 if value == 'is not set':
168                     sys.exit(f'[!] ERROR: bad enabled Kconfig option "{line}"')
169             elif opt_is_off.match(line):
170                 option, value = line[2:].split(' ', 1)
171                 assert(value == 'is not set'), \
172                        f'unexpected value of disabled Kconfig option "{line}"'
173             elif line != '' and not line.startswith('#'):
174                 sys.exit(f'[!] ERROR: unexpected line in Kconfig file: "{line}"')
175
176             if option in parsed_options:
177                 sys.exit(f'[!] ERROR: Kconfig option "{line}" is found multiple times')
178
179             if option:
180                 parsed_options[option] = value
181
182
183 def parse_cmdline_file(mode, parsed_options, fname):
184     with open(fname, 'r', encoding='utf-8') as f:
185         line = f.readline()
186         opts = line.split()
187
188         line = f.readline()
189         if line:
190             sys.exit(f'[!] ERROR: more than one line in "{fname}"')
191
192         for opt in opts:
193             if '=' in opt:
194                 name, value = opt.split('=', 1)
195             else:
196                 name = opt
197                 value = '' # '' is not None
198             if name in parsed_options and mode != 'json':
199                 print(f'[!] WARNING: cmdline option "{name}" is found multiple times')
200             value = normalize_cmdline_options(name, value)
201             parsed_options[name] = value
202
203
204 def parse_sysctl_file(mode, parsed_options, fname):
205     with open(fname, 'r', encoding='utf-8') as f:
206         sysctl_pattern = re.compile(r"[a-zA-Z0-9/\._-]+ =.*$")
207         for line in f.readlines():
208             line = line.strip()
209             if not sysctl_pattern.match(line):
210                 sys.exit(f'[!] ERROR: unexpected line in sysctl file: "{line}"')
211             option, value = line.split('=', 1)
212             option = option.strip()
213             value = value.strip()
214             # sysctl options may be found multiple times, let's save the last value:
215             parsed_options[option] = value
216
217     # let's check the presence of some ancient sysctl option
218     # to ensure that we are parsing the output of `sudo sysctl -a > file`
219     if 'kernel.printk' not in parsed_options:
220         sys.exit(f'[!] ERROR: {fname} doesn\'t look like a sysctl output file, please try `sudo sysctl -a > {fname}`')
221
222     # let's check the presence of a sysctl option available for root
223     if 'net.core.bpf_jit_harden' not in parsed_options and mode != 'json':
224         print(f'[!] WARNING: sysctl option "net.core.bpf_jit_harden" available for root is not found in {fname}, please try `sudo sysctl -a > {fname}`')
225
226
227 def main():
228     # Report modes:
229     #   * verbose mode for
230     #     - reporting about unknown kernel options in the Kconfig
231     #     - verbose printing of ComplexOptCheck items
232     #   * json mode for printing the results in JSON format
233     report_modes = ['verbose', 'json', 'show_ok', 'show_fail']
234     supported_archs = ['X86_64', 'X86_32', 'ARM64', 'ARM']
235     parser = ArgumentParser(prog='kernel-hardening-checker',
236                             description='A tool for checking the security hardening options of the Linux kernel')
237     parser.add_argument('--version', action='version', version=f'%(prog)s {__version__}')
238     parser.add_argument('-m', '--mode', choices=report_modes,
239                         help='choose the report mode')
240     parser.add_argument('-c', '--config',
241                         help='check the security hardening options in the kernel Kconfig file (also supports *.gz files)')
242     parser.add_argument('-l', '--cmdline',
243                         help='check the security hardening options in the kernel cmdline file (contents of /proc/cmdline)')
244     parser.add_argument('-s', '--sysctl',
245                         help='check the security hardening options in the sysctl output file (`sudo sysctl -a > file`)')
246     parser.add_argument('-v', '--kernel-version',
247                         help='extract the version from the kernel version file (contents of /proc/version)')
248     parser.add_argument('-p', '--print', choices=supported_archs,
249                         help='print the security hardening recommendations for the selected microarchitecture')
250     parser.add_argument('-g', '--generate', choices=supported_archs,
251                         help='generate a Kconfig fragment with the security hardening options for the selected microarchitecture')
252     args = parser.parse_args()
253
254     mode = None
255     if args.mode:
256         mode = args.mode
257         if mode != 'json':
258             print(f'[+] Special report mode: {mode}')
259
260     config_checklist = []
261
262     if args.config:
263         if args.print:
264             sys.exit('[!] ERROR: --config and --print can\'t be used together')
265         if args.generate:
266             sys.exit('[!] ERROR: --config and --generate can\'t be used together')
267
268         if mode != 'json':
269             print(f'[+] Kconfig file to check: {args.config}')
270             if args.cmdline:
271                 print(f'[+] Kernel cmdline file to check: {args.cmdline}')
272             if args.sysctl:
273                 print(f'[+] Sysctl output file to check: {args.sysctl}')
274
275         arch, msg = detect_arch(args.config, supported_archs)
276         if arch is None:
277             sys.exit(f'[!] ERROR: {msg}')
278         if mode != 'json':
279             print(f'[+] Detected microarchitecture: {arch}')
280
281         if args.kernel_version:
282             kernel_version, msg = detect_kernel_version(args.kernel_version)
283         else:
284             kernel_version, msg = detect_kernel_version(args.config)
285         if kernel_version is None:
286             if args.kernel_version is None:
287                 print('[!] Hint: provide the kernel version file through --kernel-version option')
288             sys.exit(f'[!] ERROR: {msg}')
289         if mode != 'json':
290             print(f'[+] Detected kernel version: {kernel_version}')
291
292         compiler, msg = detect_compiler(args.config)
293         if mode != 'json':
294             if compiler:
295                 print(f'[+] Detected compiler: {compiler}')
296             else:
297                 print(f'[-] Can\'t detect the compiler: {msg}')
298
299         # add relevant Kconfig checks to the checklist
300         add_kconfig_checks(config_checklist, arch)
301
302         if args.cmdline:
303             # add relevant cmdline checks to the checklist
304             add_cmdline_checks(config_checklist, arch)
305
306         if args.sysctl:
307             # add relevant sysctl checks to the checklist
308             add_sysctl_checks(config_checklist, arch)
309
310         # populate the checklist with the parsed Kconfig data
311         parsed_kconfig_options = OrderedDict()
312         parse_kconfig_file(mode, parsed_kconfig_options, args.config)
313         populate_with_data(config_checklist, parsed_kconfig_options, 'kconfig')
314
315         # populate the checklist with the kernel version data
316         populate_with_data(config_checklist, kernel_version, 'version')
317
318         if args.cmdline:
319             # populate the checklist with the parsed cmdline data
320             parsed_cmdline_options = OrderedDict()
321             parse_cmdline_file(mode, parsed_cmdline_options, args.cmdline)
322             populate_with_data(config_checklist, parsed_cmdline_options, 'cmdline')
323
324         if args.sysctl:
325             # populate the checklist with the parsed sysctl data
326             parsed_sysctl_options = OrderedDict()
327             parse_sysctl_file(mode, parsed_sysctl_options, args.sysctl)
328             populate_with_data(config_checklist, parsed_sysctl_options, 'sysctl')
329
330         # hackish refinement of the CONFIG_ARCH_MMAP_RND_BITS check
331         mmap_rnd_bits_max = parsed_kconfig_options.get('CONFIG_ARCH_MMAP_RND_BITS_MAX', None)
332         if mmap_rnd_bits_max:
333             override_expected_value(config_checklist, 'CONFIG_ARCH_MMAP_RND_BITS', mmap_rnd_bits_max)
334         else:
335             # remove the CONFIG_ARCH_MMAP_RND_BITS check to avoid false results
336             if mode != 'json':
337                 print('[-] Can\'t check CONFIG_ARCH_MMAP_RND_BITS without CONFIG_ARCH_MMAP_RND_BITS_MAX')
338             config_checklist[:] = [o for o in config_checklist if o.name != 'CONFIG_ARCH_MMAP_RND_BITS']
339
340         # now everything is ready, perform the checks
341         perform_checks(config_checklist)
342
343         if mode == 'verbose':
344             # print the parsed options without the checks (for debugging)
345             print_unknown_options(config_checklist, parsed_kconfig_options, 'kconfig')
346             if args.cmdline:
347                 print_unknown_options(config_checklist, parsed_cmdline_options, 'cmdline')
348             if args.sysctl:
349                 print_unknown_options(config_checklist, parsed_sysctl_options, 'sysctl')
350
351         # finally print the results
352         print_checklist(mode, config_checklist, True)
353         sys.exit(0)
354     elif args.cmdline:
355         sys.exit('[!] ERROR: checking cmdline depends on checking Kconfig')
356     elif args.sysctl:
357         # separate sysctl checking (without kconfig)
358         assert(args.config is None and args.cmdline is None), 'unexpected args'
359         if args.print:
360             sys.exit('[!] ERROR: --sysctl and --print can\'t be used together')
361         if args.generate:
362             sys.exit('[!] ERROR: --sysctl and --generate can\'t be used together')
363
364         if mode != 'json':
365             print(f'[+] Sysctl output file to check: {args.sysctl}')
366
367         # add relevant sysctl checks to the checklist
368         add_sysctl_checks(config_checklist, None)
369
370         # populate the checklist with the parsed sysctl data
371         parsed_sysctl_options = OrderedDict()
372         parse_sysctl_file(mode, parsed_sysctl_options, args.sysctl)
373         populate_with_data(config_checklist, parsed_sysctl_options, 'sysctl')
374
375         # now everything is ready, perform the checks
376         perform_checks(config_checklist)
377
378         if mode == 'verbose':
379             # print the parsed options without the checks (for debugging)
380             print_unknown_options(config_checklist, parsed_sysctl_options, 'sysctl')
381
382         # finally print the results
383         print_checklist(mode, config_checklist, True)
384         sys.exit(0)
385
386     if args.print:
387         assert(args.config is None and args.cmdline is None and args.sysctl is None), 'unexpected args'
388         if args.generate:
389             sys.exit('[!] ERROR: --print and --generate can\'t be used together')
390         if mode and mode not in ('verbose', 'json'):
391             sys.exit(f'[!] ERROR: wrong mode "{mode}" for --print')
392         arch = args.print
393         add_kconfig_checks(config_checklist, arch)
394         add_cmdline_checks(config_checklist, arch)
395         add_sysctl_checks(config_checklist, arch)
396         if mode != 'json':
397             print(f'[+] Printing kernel security hardening options for {arch}...')
398         print_checklist(mode, config_checklist, False)
399         sys.exit(0)
400
401     if args.generate:
402         assert(args.config is None and args.cmdline is None and args.sysctl is None and args.print is None), 'unexpected args'
403         if mode:
404             sys.exit(f'[!] ERROR: wrong mode "{mode}" for --generate')
405         arch = args.generate
406         add_kconfig_checks(config_checklist, arch)
407         print(f'CONFIG_{arch}=y') # the Kconfig fragment should describe the microarchitecture
408         for opt in config_checklist:
409             if opt.name == 'CONFIG_ARCH_MMAP_RND_BITS':
410                 continue # don't add CONFIG_ARCH_MMAP_RND_BITS because its value needs refinement
411             if opt.expected == 'is not off':
412                 continue # don't add Kconfig options without explicitly recommended values
413             if opt.expected == 'is not set':
414                 print(f'# {opt.name} is not set')
415             else:
416                 print(f'{opt.name}={opt.expected}')
417         sys.exit(0)
418
419     parser.print_help()
420     sys.exit(0)