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
64 def set_state(self, data):
65 assert(data is None or isinstance(data, str)), \
66 f'invalid state "{data}" for "{self.name}" check'
70 # handle the 'is present' check
71 if self.expected == 'is present':
72 if self.state is None:
73 self.result = 'FAIL: is not present'
75 self.result = 'OK: is present'
78 # handle the 'is not off' option check
79 if self.expected == 'is not off':
80 if self.state == 'off':
81 self.result = 'FAIL: is off'
82 elif self.state == '0':
83 self.result = 'FAIL: is off, "0"'
84 elif self.state is None:
85 self.result = 'FAIL: is off, not found'
87 self.result = f'OK: is not off, "{self.state}"'
90 # handle the option value check
91 if self.expected == self.state:
93 elif self.state is None:
94 if self.expected == 'is not set':
95 self.result = 'OK: is not found'
97 self.result = 'FAIL: is not found'
99 self.result = f'FAIL: "{self.state}"'
101 def table_print(self, _mode, with_results):
102 print(f'{self.name:<40}|{self.type:^7}|{self.expected:^12}|{self.decision:^10}|{self.reason:^18}', end='')
104 print(f'| {colorize_result(self.result)}', end='')
106 def json_dump(self, with_results):
108 "option_name": self.name,
110 "desired_val": self.expected,
111 "decision": self.decision,
112 "reason": self.reason,
115 dump["check_result_text"] = self.result
116 dump["check_result"] = self.result.startswith('OK')
120 class KconfigCheck(OptCheck):
121 def __init__(self, *args, **kwargs):
122 super().__init__(*args, **kwargs)
123 self.name = f'CONFIG_{self.name}'
130 class CmdlineCheck(OptCheck):
136 class SysctlCheck(OptCheck):
143 def __init__(self, ver_expected):
144 assert(ver_expected and isinstance(ver_expected, tuple) and len(ver_expected) == 3), \
145 f'invalid expected version "{ver_expected}" for VersionCheck (1)'
146 assert(all(map(lambda x: isinstance(x, int), ver_expected))), \
147 f'invalid expected version "{ver_expected}" for VersionCheck (2)'
148 self.ver_expected = ver_expected
156 def set_state(self, data):
157 assert(data and isinstance(data, tuple) and len(data) >= 3), \
158 f'invalid version "{data}" for VersionCheck'
162 if self.ver[0] > self.ver_expected[0]:
163 self.result = f'OK: version >= {self.ver_expected}'
165 if self.ver[0] < self.ver_expected[0]:
166 self.result = f'FAIL: version < {self.ver_expected}'
168 # self.ver[0] and self.ver_expected[0] are equal
169 if self.ver[1] > self.ver_expected[1]:
170 self.result = f'OK: version >= {self.ver_expected}'
172 if self.ver[1] < self.ver_expected[1]:
173 self.result = f'FAIL: version < {self.ver_expected}'
175 # self.ver[1] and self.ver_expected[1] are equal too
176 if self.ver[2] >= self.ver_expected[2]:
177 self.result = f'OK: version >= {self.ver_expected}'
179 self.result = f'FAIL: version < {self.ver_expected}'
181 def table_print(self, _mode, with_results):
182 ver_req = f'kernel version >= {self.ver_expected}'
183 print(f'{ver_req:<91}', end='')
185 print(f'| {colorize_result(self.result)}', end='')
188 class ComplexOptCheck:
189 def __init__(self, *opts):
192 f'empty {self.__class__.__name__} check'
193 assert(len(self.opts) != 1), \
194 f'useless {self.__class__.__name__} check: {opts}'
195 assert(isinstance(opts[0], (KconfigCheck, CmdlineCheck, SysctlCheck))), \
196 f'invalid {self.__class__.__name__} check: {opts}'
205 return self.opts[0].name
209 return self.opts[0].expected
211 def table_print(self, mode, with_results):
212 if mode == 'verbose':
213 class_name = f'<<< {self.__class__.__name__} >>>'
214 print(f' {class_name:87}', end='')
216 print(f'| {colorize_result(self.result)}', end='')
219 o.table_print(mode, with_results)
222 o.table_print(mode, False)
224 print(f'| {colorize_result(self.result)}', end='')
226 def json_dump(self, with_results):
227 dump = self.opts[0].json_dump(False)
229 # Add 'check_result_text' and 'check_result' keys to the dictionary
230 dump["check_result_text"] = self.result
231 dump["check_result"] = self.result.startswith('OK')
235 class OR(ComplexOptCheck):
236 # self.opts[0] is the option that this OR-check is about.
238 # OR(<X_is_hardened>, <X_is_disabled>)
239 # OR(<X_is_hardened>, <old_X_is_hardened>)
241 for i, opt in enumerate(self.opts):
243 if opt.result.startswith('OK'):
244 self.result = opt.result
245 # Add more info for additional checks:
247 if opt.result == 'OK':
248 self.result = f'OK: {opt.name} is "{opt.expected}"'
249 elif opt.result == 'OK: is not found':
250 self.result = f'OK: {opt.name} is not found'
251 elif opt.result == 'OK: is present':
252 self.result = f'OK: {opt.name} is present'
253 elif opt.result.startswith('OK: is not off'):
254 self.result = f'OK: {opt.name} is not off'
256 # VersionCheck provides enough info
257 assert(opt.result.startswith('OK: version')), \
258 f'unexpected OK description "{opt.result}"'
260 self.result = self.opts[0].result
263 class AND(ComplexOptCheck):
264 # self.opts[0] is the option that this AND-check is about.
266 # AND(<suboption>, <main_option>)
267 # Suboption is not checked if checking of the main_option is failed.
268 # AND(<X_is_disabled>, <old_X_is_disabled>)
270 for i, opt in reversed(list(enumerate(self.opts))):
273 self.result = opt.result
275 if not opt.result.startswith('OK'):
276 # This FAIL is caused by additional checks,
277 # and not by the main option that this AND-check is about.
278 # Describe the reason of the FAIL.
279 if opt.result.startswith('FAIL: \"') or opt.result == 'FAIL: is not found':
280 self.result = f'FAIL: {opt.name} is not "{opt.expected}"'
281 elif opt.result == 'FAIL: is not present':
282 self.result = f'FAIL: {opt.name} is not present'
283 elif opt.result in ('FAIL: is off', 'FAIL: is off, "0"'):
284 self.result = f'FAIL: {opt.name} is off'
285 elif opt.result == 'FAIL: is off, not found':
286 self.result = f'FAIL: {opt.name} is off, not found'
288 # VersionCheck provides enough info
289 self.result = opt.result
290 assert(opt.result.startswith('FAIL: version')), \
291 f'unexpected FAIL description "{opt.result}"'
295 SIMPLE_OPTION_TYPES = ('kconfig', 'cmdline', 'sysctl', 'version')
298 def populate_simple_opt_with_data(opt, data, data_type):
299 assert(opt.type != 'complex'), \
300 f'unexpected ComplexOptCheck "{opt.name}"'
301 assert(opt.type in SIMPLE_OPTION_TYPES), \
302 f'invalid opt type "{opt.type}"'
303 assert(data_type in SIMPLE_OPTION_TYPES), \
304 f'invalid data type "{data_type}"'
308 if data_type != opt.type:
311 if data_type in ('kconfig', 'cmdline', 'sysctl'):
312 opt.set_state(data.get(opt.name, None))
314 assert(data_type == 'version'), \
315 f'unexpected data type "{data_type}"'
319 def populate_opt_with_data(opt, data, data_type):
320 assert(opt.type != 'version'), 'a single VersionCheck is useless'
321 if opt.type != 'complex':
322 populate_simple_opt_with_data(opt, data, data_type)
325 if o.type != 'complex':
326 populate_simple_opt_with_data(o, data, data_type)
328 # Recursion for nested ComplexOptCheck objects
329 populate_opt_with_data(o, data, data_type)
332 def populate_with_data(checklist, data, data_type):
333 for opt in checklist:
334 populate_opt_with_data(opt, data, data_type)
337 def override_expected_value(checklist, name, new_val):
338 for opt in checklist:
340 assert(opt.type in ('kconfig', 'cmdline', 'sysctl')), \
341 f'overriding an expected value for "{opt.type}" checks is not supported yet'
342 opt.expected = new_val
345 def perform_checks(checklist):
346 for opt in checklist: