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):
19 if input.startswith('OK'):
21 elif input.startswith('FAIL:'):
24 assert(False), f'unexpected result "{input}"'
25 return f'{color}{input}{COLOR_END}'
29 def __init__(self, reason, decision, name, expected):
30 assert(name and name == name.strip() and len(name.split()) == 1), \
31 f'invalid name "{name}" for {self.__class__.__name__}'
34 assert(decision and decision == decision.strip() and len(decision.split()) == 1), \
35 f'invalid decision "{decision}" for "{name}" check'
36 self.decision = decision
38 assert(reason and reason == reason.strip() and len(reason.split()) == 1), \
39 f'invalid reason "{reason}" for "{name}" check'
42 assert(expected and expected == expected.strip()), \
43 f'invalid expected value "{expected}" for "{name}" check (1)'
44 val_len = len(expected.split())
46 assert(expected in ('is not set', 'is not off')), \
47 f'invalid expected value "{expected}" for "{name}" check (2)'
49 assert(expected == 'is present'), \
50 f'invalid expected value "{expected}" for "{name}" check (3)'
52 assert(val_len == 1), \
53 f'invalid expected value "{expected}" for "{name}" check (4)'
54 self.expected = expected
60 # handle the 'is present' check
61 if self.expected == 'is present':
62 if self.state is None:
63 self.result = 'FAIL: is not present'
65 self.result = 'OK: is present'
68 # handle the 'is not off' option check
69 if self.expected == 'is not off':
70 if self.state == 'off':
71 self.result = 'FAIL: is off'
72 elif self.state == '0':
73 self.result = 'FAIL: is off, "0"'
74 elif self.state is None:
75 self.result = 'FAIL: is off, not found'
77 self.result = f'OK: is not off, "{self.state}"'
80 # handle the option value check
81 if self.expected == self.state:
83 elif self.state is None:
84 if self.expected == 'is not set':
85 self.result = 'OK: is not found'
87 self.result = 'FAIL: is not found'
89 self.result = f'FAIL: "{self.state}"'
91 def table_print(self, _mode, with_results):
92 print(f'{self.name:<40}|{self.type:^7}|{self.expected:^12}|{self.decision:^10}|{self.reason:^18}', end='')
94 print(f'| {colorize_result(self.result)}', end='')
96 def json_dump(self, with_results):
97 dump = [self.name, self.type, self.expected, self.decision, self.reason]
99 dump.append(self.result)
103 class KconfigCheck(OptCheck):
104 def __init__(self, *args, **kwargs):
105 super().__init__(*args, **kwargs)
106 self.name = 'CONFIG_' + self.name
113 class CmdlineCheck(OptCheck):
119 class SysctlCheck(OptCheck):
126 def __init__(self, ver_expected):
127 assert(ver_expected and isinstance(ver_expected, tuple) and len(ver_expected) == 2), \
128 f'invalid version "{ver_expected}" for VersionCheck'
129 self.ver_expected = ver_expected
138 if self.ver[0] > self.ver_expected[0]:
139 self.result = f'OK: version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
141 if self.ver[0] < self.ver_expected[0]:
142 self.result = f'FAIL: version < {self.ver_expected[0]}.{self.ver_expected[1]}'
144 if self.ver[1] >= self.ver_expected[1]:
145 self.result = f'OK: version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
147 self.result = f'FAIL: version < {self.ver_expected[0]}.{self.ver_expected[1]}'
149 def table_print(self, _mode, with_results):
150 ver_req = f'kernel version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
151 print(f'{ver_req:<91}', end='')
153 print(f'| {colorize_result(self.result)}', end='')
156 class ComplexOptCheck:
157 def __init__(self, *opts):
160 f'empty {self.__class__.__name__} check'
161 assert(len(self.opts) != 1), \
162 f'useless {self.__class__.__name__} check: {opts}'
163 assert(isinstance(opts[0], (KconfigCheck, CmdlineCheck, SysctlCheck))), \
164 f'invalid {self.__class__.__name__} check: {opts}'
173 return self.opts[0].name
177 return self.opts[0].expected
179 def table_print(self, mode, with_results):
180 if mode == 'verbose':
181 print(f' {"<<< " + self.__class__.__name__ + " >>>":87}', end='')
183 print(f'| {colorize_result(self.result)}', end='')
186 o.table_print(mode, with_results)
189 o.table_print(mode, False)
191 print(f'| {colorize_result(self.result)}', end='')
193 def json_dump(self, with_results):
194 dump = self.opts[0].json_dump(False)
196 dump.append(self.result)
200 class OR(ComplexOptCheck):
201 # self.opts[0] is the option that this OR-check is about.
203 # OR(<X_is_hardened>, <X_is_disabled>)
204 # OR(<X_is_hardened>, <old_X_is_hardened>)
206 for i, opt in enumerate(self.opts):
208 if opt.result.startswith('OK'):
209 self.result = opt.result
210 # Add more info for additional checks:
212 if opt.result == 'OK':
213 self.result = f'OK: {opt.name} is "{opt.expected}"'
214 elif opt.result == 'OK: is not found':
215 self.result = f'OK: {opt.name} is not found'
216 elif opt.result == 'OK: is present':
217 self.result = f'OK: {opt.name} is present'
218 elif opt.result.startswith('OK: is not off'):
219 self.result = f'OK: {opt.name} is not off'
221 # VersionCheck provides enough info
222 assert(opt.result.startswith('OK: version')), \
223 f'unexpected OK description "{opt.result}"'
225 self.result = self.opts[0].result
228 class AND(ComplexOptCheck):
229 # self.opts[0] is the option that this AND-check is about.
231 # AND(<suboption>, <main_option>)
232 # Suboption is not checked if checking of the main_option is failed.
233 # AND(<X_is_disabled>, <old_X_is_disabled>)
235 for i, opt in reversed(list(enumerate(self.opts))):
238 self.result = opt.result
240 if not opt.result.startswith('OK'):
241 # This FAIL is caused by additional checks,
242 # and not by the main option that this AND-check is about.
243 # Describe the reason of the FAIL.
244 if opt.result.startswith('FAIL: \"') or opt.result == 'FAIL: is not found':
245 self.result = f'FAIL: {opt.name} is not "{opt.expected}"'
246 elif opt.result == 'FAIL: is not present':
247 self.result = f'FAIL: {opt.name} is not present'
248 elif opt.result in ('FAIL: is off', 'FAIL: is off, "0"'):
249 self.result = f'FAIL: {opt.name} is off'
250 elif opt.result == 'FAIL: is off, not found':
251 self.result = f'FAIL: {opt.name} is off, not found'
253 # VersionCheck provides enough info
254 self.result = opt.result
255 assert(opt.result.startswith('FAIL: version')), \
256 f'unexpected FAIL description "{opt.result}"'
260 SIMPLE_OPTION_TYPES = ('kconfig', 'cmdline', 'sysctl', 'version')
263 def populate_simple_opt_with_data(opt, data, data_type):
264 assert(opt.type != 'complex'), \
265 f'unexpected ComplexOptCheck "{opt.name}"'
266 assert(opt.type in SIMPLE_OPTION_TYPES), \
267 f'invalid opt type "{opt.type}"'
268 assert(data_type in SIMPLE_OPTION_TYPES), \
269 f'invalid data type "{data_type}"'
273 if data_type != opt.type:
276 if data_type in ('kconfig', 'cmdline', 'sysctl'):
277 opt.state = data.get(opt.name, None)
279 assert(data_type == 'version'), \
280 f'unexpected data type "{data_type}"'
284 def populate_opt_with_data(opt, data, data_type):
285 assert(opt.type != 'version'), 'a single VersionCheck is useless'
286 if opt.type != 'complex':
287 populate_simple_opt_with_data(opt, data, data_type)
290 if o.type != 'complex':
291 populate_simple_opt_with_data(o, data, data_type)
293 # Recursion for nested ComplexOptCheck objects
294 populate_opt_with_data(o, data, data_type)
297 def populate_with_data(checklist, data, data_type):
298 for opt in checklist:
299 populate_opt_with_data(opt, data, data_type)
302 def override_expected_value(checklist, name, new_val):
303 for opt in checklist:
305 assert(opt.type in ('kconfig', 'cmdline', 'sysctl')), \
306 f'overriding an expected value for "{opt.type}" checks is not supported yet'
307 opt.expected = new_val
310 def perform_checks(checklist):
311 for opt in checklist: