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'):
23 elif input_text.startswith('FAIL:'):
26 assert(False), f'unexpected result "{input_text}"'
27 return f'{color}{input_text}{COLOR_END}'
31 def __init__(self, reason, decision, name, expected):
32 assert(name and name == name.strip() and len(name.split()) == 1), \
33 f'invalid name "{name}" for {self.__class__.__name__}'
36 assert(decision and decision == decision.strip() and len(decision.split()) == 1), \
37 f'invalid decision "{decision}" for "{name}" check'
38 self.decision = decision
40 assert(reason and reason == reason.strip() and len(reason.split()) == 1), \
41 f'invalid reason "{reason}" for "{name}" check'
44 assert(expected and expected == expected.strip()), \
45 f'invalid expected value "{expected}" for "{name}" check (1)'
46 val_len = len(expected.split())
48 assert(expected in ('is not set', 'is not off')), \
49 f'invalid expected value "{expected}" for "{name}" check (2)'
51 assert(expected == 'is present'), \
52 f'invalid expected value "{expected}" for "{name}" check (3)'
54 assert(val_len == 1), \
55 f'invalid expected value "{expected}" for "{name}" check (4)'
56 self.expected = expected
62 # handle the 'is present' check
63 if self.expected == 'is present':
64 if self.state is None:
65 self.result = 'FAIL: is not present'
67 self.result = 'OK: is present'
70 # handle the 'is not off' option check
71 if self.expected == 'is not off':
72 if self.state == 'off':
73 self.result = 'FAIL: is off'
74 elif self.state == '0':
75 self.result = 'FAIL: is off, "0"'
76 elif self.state is None:
77 self.result = 'FAIL: is off, not found'
79 self.result = f'OK: is not off, "{self.state}"'
82 # handle the option value check
83 if self.expected == self.state:
85 elif self.state is None:
86 if self.expected == 'is not set':
87 self.result = 'OK: is not found'
89 self.result = 'FAIL: is not found'
91 self.result = f'FAIL: "{self.state}"'
93 def table_print(self, _mode, with_results):
94 print(f'{self.name:<40}|{self.type:^7}|{self.expected:^12}|{self.decision:^10}|{self.reason:^18}', end='')
96 print(f'| {colorize_result(self.result)}', end='')
98 def json_dump(self, with_results):
99 dump = [self.name, self.type, self.expected, self.decision, self.reason]
101 dump.append(self.result)
105 class KconfigCheck(OptCheck):
106 def __init__(self, *args, **kwargs):
107 super().__init__(*args, **kwargs)
108 self.name = 'CONFIG_' + self.name
115 class CmdlineCheck(OptCheck):
121 class SysctlCheck(OptCheck):
128 def __init__(self, ver_expected):
129 assert(ver_expected and isinstance(ver_expected, tuple) and len(ver_expected) == 2), \
130 f'invalid version "{ver_expected}" for VersionCheck'
131 self.ver_expected = ver_expected
140 if self.ver[0] > self.ver_expected[0]:
141 self.result = f'OK: version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
143 if self.ver[0] < self.ver_expected[0]:
144 self.result = f'FAIL: version < {self.ver_expected[0]}.{self.ver_expected[1]}'
146 if self.ver[1] >= self.ver_expected[1]:
147 self.result = f'OK: version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
149 self.result = f'FAIL: version < {self.ver_expected[0]}.{self.ver_expected[1]}'
151 def table_print(self, _mode, with_results):
152 ver_req = f'kernel version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
153 print(f'{ver_req:<91}', end='')
155 print(f'| {colorize_result(self.result)}', end='')
158 class ComplexOptCheck:
159 def __init__(self, *opts):
162 f'empty {self.__class__.__name__} check'
163 assert(len(self.opts) != 1), \
164 f'useless {self.__class__.__name__} check: {opts}'
165 assert(isinstance(opts[0], (KconfigCheck, CmdlineCheck, SysctlCheck))), \
166 f'invalid {self.__class__.__name__} check: {opts}'
175 return self.opts[0].name
179 return self.opts[0].expected
181 def table_print(self, mode, with_results):
182 if mode == 'verbose':
183 print(f' {"<<< " + self.__class__.__name__ + " >>>":87}', end='')
185 print(f'| {colorize_result(self.result)}', end='')
188 o.table_print(mode, with_results)
191 o.table_print(mode, False)
193 print(f'| {colorize_result(self.result)}', end='')
195 def json_dump(self, with_results):
196 dump = self.opts[0].json_dump(False)
198 dump.append(self.result)
202 class OR(ComplexOptCheck):
203 # self.opts[0] is the option that this OR-check is about.
205 # OR(<X_is_hardened>, <X_is_disabled>)
206 # OR(<X_is_hardened>, <old_X_is_hardened>)
208 for i, opt in enumerate(self.opts):
210 if opt.result.startswith('OK'):
211 self.result = opt.result
212 # Add more info for additional checks:
214 if opt.result == 'OK':
215 self.result = f'OK: {opt.name} is "{opt.expected}"'
216 elif opt.result == 'OK: is not found':
217 self.result = f'OK: {opt.name} is not found'
218 elif opt.result == 'OK: is present':
219 self.result = f'OK: {opt.name} is present'
220 elif opt.result.startswith('OK: is not off'):
221 self.result = f'OK: {opt.name} is not off'
223 # VersionCheck provides enough info
224 assert(opt.result.startswith('OK: version')), \
225 f'unexpected OK description "{opt.result}"'
227 self.result = self.opts[0].result
230 class AND(ComplexOptCheck):
231 # self.opts[0] is the option that this AND-check is about.
233 # AND(<suboption>, <main_option>)
234 # Suboption is not checked if checking of the main_option is failed.
235 # AND(<X_is_disabled>, <old_X_is_disabled>)
237 for i, opt in reversed(list(enumerate(self.opts))):
240 self.result = opt.result
242 if not opt.result.startswith('OK'):
243 # This FAIL is caused by additional checks,
244 # and not by the main option that this AND-check is about.
245 # Describe the reason of the FAIL.
246 if opt.result.startswith('FAIL: \"') or opt.result == 'FAIL: is not found':
247 self.result = f'FAIL: {opt.name} is not "{opt.expected}"'
248 elif opt.result == 'FAIL: is not present':
249 self.result = f'FAIL: {opt.name} is not present'
250 elif opt.result in ('FAIL: is off', 'FAIL: is off, "0"'):
251 self.result = f'FAIL: {opt.name} is off'
252 elif opt.result == 'FAIL: is off, not found':
253 self.result = f'FAIL: {opt.name} is off, not found'
255 # VersionCheck provides enough info
256 self.result = opt.result
257 assert(opt.result.startswith('FAIL: version')), \
258 f'unexpected FAIL description "{opt.result}"'
262 SIMPLE_OPTION_TYPES = ('kconfig', 'cmdline', 'sysctl', 'version')
265 def populate_simple_opt_with_data(opt, data, data_type):
266 assert(opt.type != 'complex'), \
267 f'unexpected ComplexOptCheck "{opt.name}"'
268 assert(opt.type in SIMPLE_OPTION_TYPES), \
269 f'invalid opt type "{opt.type}"'
270 assert(data_type in SIMPLE_OPTION_TYPES), \
271 f'invalid data type "{data_type}"'
275 if data_type != opt.type:
278 if data_type in ('kconfig', 'cmdline', 'sysctl'):
279 opt.state = data.get(opt.name, None)
281 assert(data_type == 'version'), \
282 f'unexpected data type "{data_type}"'
286 def populate_opt_with_data(opt, data, data_type):
287 assert(opt.type != 'version'), 'a single VersionCheck is useless'
288 if opt.type != 'complex':
289 populate_simple_opt_with_data(opt, data, data_type)
292 if o.type != 'complex':
293 populate_simple_opt_with_data(o, data, data_type)
295 # Recursion for nested ComplexOptCheck objects
296 populate_opt_with_data(o, data, data_type)
299 def populate_with_data(checklist, data, data_type):
300 for opt in checklist:
301 populate_opt_with_data(opt, data, data_type)
304 def override_expected_value(checklist, name, new_val):
305 for opt in checklist:
307 assert(opt.type in ('kconfig', 'cmdline', 'sysctl')), \
308 f'overriding an expected value for "{opt.type}" checks is not supported yet'
309 opt.expected = new_val
312 def perform_checks(checklist):
313 for opt in checklist: