4 This tool is for checking the security hardening options of the Linux kernel.
6 Author: Alexander Popov <alex.popov@linux.com>
8 This module is the engine of checks.
11 # pylint: disable=missing-class-docstring,missing-function-docstring
12 # pylint: disable=line-too-long,invalid-name,too-many-branches
14 GREEN_COLOR = '\x1b[32m'
15 RED_COLOR = '\x1b[31m'
18 def colorize_result(input_text):
19 if input_text is None:
21 if input_text.startswith('OK'):
24 assert(input_text.startswith('FAIL:')), f'unexpected result "{input_text}"'
26 return f'{color}{input_text}{COLOR_END}'
30 def __init__(self, reason, decision, name, expected):
31 assert(name and name == name.strip() and len(name.split()) == 1), \
32 f'invalid name "{name}" for {self.__class__.__name__}'
35 assert(decision and decision == decision.strip() and len(decision.split()) == 1), \
36 f'invalid decision "{decision}" for "{name}" check'
37 self.decision = decision
39 assert(reason and reason == reason.strip() and len(reason.split()) == 1), \
40 f'invalid reason "{reason}" for "{name}" check'
43 assert(expected and expected == expected.strip()), \
44 f'invalid expected value "{expected}" for "{name}" check (1)'
45 val_len = len(expected.split())
47 assert(expected in ('is not set', 'is not off')), \
48 f'invalid expected value "{expected}" for "{name}" check (2)'
50 assert(expected == 'is present'), \
51 f'invalid expected value "{expected}" for "{name}" check (3)'
53 assert(val_len == 1), \
54 f'invalid expected value "{expected}" for "{name}" check (4)'
55 self.expected = expected
61 # handle the 'is present' check
62 if self.expected == 'is present':
63 if self.state is None:
64 self.result = 'FAIL: is not present'
66 self.result = 'OK: is present'
69 # handle the 'is not off' option check
70 if self.expected == 'is not off':
71 if self.state == 'off':
72 self.result = 'FAIL: is off'
73 elif self.state == '0':
74 self.result = 'FAIL: is off, "0"'
75 elif self.state is None:
76 self.result = 'FAIL: is off, not found'
78 self.result = f'OK: is not off, "{self.state}"'
81 # handle the option value check
82 if self.expected == self.state:
84 elif self.state is None:
85 if self.expected == 'is not set':
86 self.result = 'OK: is not found'
88 self.result = 'FAIL: is not found'
90 self.result = f'FAIL: "{self.state}"'
92 def table_print(self, _mode, with_results):
93 print(f'{self.name:<40}|{self.type:^7}|{self.expected:^12}|{self.decision:^10}|{self.reason:^18}', end='')
95 print(f'| {colorize_result(self.result)}', end='')
97 def json_dump(self, with_results):
98 dump = [self.name, self.type, self.expected, self.decision, self.reason]
100 dump.append(self.result)
104 class KconfigCheck(OptCheck):
105 def __init__(self, *args, **kwargs):
106 super().__init__(*args, **kwargs)
107 self.name = 'CONFIG_' + self.name
114 class CmdlineCheck(OptCheck):
120 class SysctlCheck(OptCheck):
127 def __init__(self, ver_expected):
128 assert(ver_expected and isinstance(ver_expected, tuple) and len(ver_expected) == 2), \
129 f'invalid version "{ver_expected}" for VersionCheck'
130 self.ver_expected = ver_expected
139 if self.ver[0] > self.ver_expected[0]:
140 self.result = f'OK: version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
142 if self.ver[0] < self.ver_expected[0]:
143 self.result = f'FAIL: version < {self.ver_expected[0]}.{self.ver_expected[1]}'
145 if self.ver[1] >= self.ver_expected[1]:
146 self.result = f'OK: version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
148 self.result = f'FAIL: version < {self.ver_expected[0]}.{self.ver_expected[1]}'
150 def table_print(self, _mode, with_results):
151 ver_req = f'kernel version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
152 print(f'{ver_req:<91}', end='')
154 print(f'| {colorize_result(self.result)}', end='')
157 class ComplexOptCheck:
158 def __init__(self, *opts):
161 f'empty {self.__class__.__name__} check'
162 assert(len(self.opts) != 1), \
163 f'useless {self.__class__.__name__} check: {opts}'
164 assert(isinstance(opts[0], (KconfigCheck, CmdlineCheck, SysctlCheck))), \
165 f'invalid {self.__class__.__name__} check: {opts}'
174 return self.opts[0].name
178 return self.opts[0].expected
180 def table_print(self, mode, with_results):
181 if mode == 'verbose':
182 print(f' {"<<< " + self.__class__.__name__ + " >>>":87}', end='')
184 print(f'| {colorize_result(self.result)}', end='')
187 o.table_print(mode, with_results)
190 o.table_print(mode, False)
192 print(f'| {colorize_result(self.result)}', end='')
194 def json_dump(self, with_results):
195 dump = self.opts[0].json_dump(False)
197 dump.append(self.result)
201 class OR(ComplexOptCheck):
202 # self.opts[0] is the option that this OR-check is about.
204 # OR(<X_is_hardened>, <X_is_disabled>)
205 # OR(<X_is_hardened>, <old_X_is_hardened>)
207 for i, opt in enumerate(self.opts):
209 if opt.result.startswith('OK'):
210 self.result = opt.result
211 # Add more info for additional checks:
213 if opt.result == 'OK':
214 self.result = f'OK: {opt.name} is "{opt.expected}"'
215 elif opt.result == 'OK: is not found':
216 self.result = f'OK: {opt.name} is not found'
217 elif opt.result == 'OK: is present':
218 self.result = f'OK: {opt.name} is present'
219 elif opt.result.startswith('OK: is not off'):
220 self.result = f'OK: {opt.name} is not off'
222 # VersionCheck provides enough info
223 assert(opt.result.startswith('OK: version')), \
224 f'unexpected OK description "{opt.result}"'
226 self.result = self.opts[0].result
229 class AND(ComplexOptCheck):
230 # self.opts[0] is the option that this AND-check is about.
232 # AND(<suboption>, <main_option>)
233 # Suboption is not checked if checking of the main_option is failed.
234 # AND(<X_is_disabled>, <old_X_is_disabled>)
236 for i, opt in reversed(list(enumerate(self.opts))):
239 self.result = opt.result
241 if not opt.result.startswith('OK'):
242 # This FAIL is caused by additional checks,
243 # and not by the main option that this AND-check is about.
244 # Describe the reason of the FAIL.
245 if opt.result.startswith('FAIL: \"') or opt.result == 'FAIL: is not found':
246 self.result = f'FAIL: {opt.name} is not "{opt.expected}"'
247 elif opt.result == 'FAIL: is not present':
248 self.result = f'FAIL: {opt.name} is not present'
249 elif opt.result in ('FAIL: is off', 'FAIL: is off, "0"'):
250 self.result = f'FAIL: {opt.name} is off'
251 elif opt.result == 'FAIL: is off, not found':
252 self.result = f'FAIL: {opt.name} is off, not found'
254 # VersionCheck provides enough info
255 self.result = opt.result
256 assert(opt.result.startswith('FAIL: version')), \
257 f'unexpected FAIL description "{opt.result}"'
261 SIMPLE_OPTION_TYPES = ('kconfig', 'cmdline', 'sysctl', 'version')
264 def populate_simple_opt_with_data(opt, data, data_type):
265 assert(opt.type != 'complex'), \
266 f'unexpected ComplexOptCheck "{opt.name}"'
267 assert(opt.type in SIMPLE_OPTION_TYPES), \
268 f'invalid opt type "{opt.type}"'
269 assert(data_type in SIMPLE_OPTION_TYPES), \
270 f'invalid data type "{data_type}"'
274 if data_type != opt.type:
277 if data_type in ('kconfig', 'cmdline', 'sysctl'):
278 opt.state = data.get(opt.name, None)
280 assert(data_type == 'version'), \
281 f'unexpected data type "{data_type}"'
285 def populate_opt_with_data(opt, data, data_type):
286 assert(opt.type != 'version'), 'a single VersionCheck is useless'
287 if opt.type != 'complex':
288 populate_simple_opt_with_data(opt, data, data_type)
291 if o.type != 'complex':
292 populate_simple_opt_with_data(o, data, data_type)
294 # Recursion for nested ComplexOptCheck objects
295 populate_opt_with_data(o, data, data_type)
298 def populate_with_data(checklist, data, data_type):
299 for opt in checklist:
300 populate_opt_with_data(opt, data, data_type)
303 def override_expected_value(checklist, name, new_val):
304 for opt in checklist:
306 assert(opt.type in ('kconfig', 'cmdline', 'sysctl')), \
307 f'overriding an expected value for "{opt.type}" checks is not supported yet'
308 opt.expected = new_val
311 def perform_checks(checklist):
312 for opt in checklist: