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
16 def __init__(self, reason, decision, name, expected):
17 assert(name and name == name.strip() and len(name.split()) == 1), \
18 f'invalid name "{name}" for {self.__class__.__name__}'
21 assert(decision and decision == decision.strip() and len(decision.split()) == 1), \
22 f'invalid decision "{decision}" for "{name}" check'
23 self.decision = decision
25 assert(reason and reason == reason.strip() and len(reason.split()) == 1), \
26 f'invalid reason "{reason}" for "{name}" check'
29 assert(expected and expected == expected.strip()), \
30 f'invalid expected value "{expected}" for "{name}" check (1)'
31 val_len = len(expected.split())
33 assert(expected in ('is not set', 'is not off')), \
34 f'invalid expected value "{expected}" for "{name}" check (2)'
36 assert(expected == 'is present'), \
37 f'invalid expected value "{expected}" for "{name}" check (3)'
39 assert(val_len == 1), \
40 f'invalid expected value "{expected}" for "{name}" check (4)'
41 self.expected = expected
47 # handle the 'is present' check
48 if self.expected == 'is present':
49 if self.state is None:
50 self.result = 'FAIL: is not present'
52 self.result = 'OK: is present'
55 # handle the 'is not off' option check
56 if self.expected == 'is not off':
57 if self.state == 'off':
58 self.result = 'FAIL: is off'
59 elif self.state == '0':
60 self.result = 'FAIL: is off, "0"'
61 elif self.state is None:
62 self.result = 'FAIL: is off, not found'
64 self.result = f'OK: is not off, "{self.state}"'
67 # handle the option value check
68 if self.expected == self.state:
70 elif self.state is None:
71 if self.expected == 'is not set':
72 self.result = 'OK: is not found'
74 self.result = 'FAIL: is not found'
76 self.result = f'FAIL: "{self.state}"'
78 def table_print(self, _mode, with_results):
79 print(f'{self.name:<40}|{self.type:^7}|{self.expected:^12}|{self.decision:^10}|{self.reason:^18}', end='')
81 print(f'| {self.result}', end='')
83 def json_dump(self, with_results):
84 dump = [self.name, self.type, self.expected, self.decision, self.reason]
86 dump.append(self.result)
90 class KconfigCheck(OptCheck):
91 def __init__(self, *args, **kwargs):
92 super().__init__(*args, **kwargs)
93 self.name = 'CONFIG_' + self.name
100 class CmdlineCheck(OptCheck):
106 class SysctlCheck(OptCheck):
113 def __init__(self, ver_expected):
114 assert(ver_expected and isinstance(ver_expected, tuple) and len(ver_expected) == 2), \
115 f'invalid version "{ver_expected}" for VersionCheck'
116 self.ver_expected = ver_expected
125 if self.ver[0] > self.ver_expected[0]:
126 self.result = f'OK: version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
128 if self.ver[0] < self.ver_expected[0]:
129 self.result = f'FAIL: version < {self.ver_expected[0]}.{self.ver_expected[1]}'
131 if self.ver[1] >= self.ver_expected[1]:
132 self.result = f'OK: version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
134 self.result = f'FAIL: version < {self.ver_expected[0]}.{self.ver_expected[1]}'
136 def table_print(self, _mode, with_results):
137 ver_req = f'kernel version >= {self.ver_expected[0]}.{self.ver_expected[1]}'
138 print(f'{ver_req:<91}', end='')
140 print(f'| {self.result}', end='')
143 class ComplexOptCheck:
144 def __init__(self, *opts):
147 f'empty {self.__class__.__name__} check'
148 assert(len(self.opts) != 1), \
149 f'useless {self.__class__.__name__} check: {opts}'
150 assert(isinstance(opts[0], (KconfigCheck, CmdlineCheck, SysctlCheck))), \
151 f'invalid {self.__class__.__name__} check: {opts}'
160 return self.opts[0].name
164 return self.opts[0].expected
166 def table_print(self, mode, with_results):
167 if mode == 'verbose':
168 print(f' {"<<< " + self.__class__.__name__ + " >>>":87}', end='')
170 print(f'| {self.result}', end='')
173 o.table_print(mode, with_results)
176 o.table_print(mode, False)
178 print(f'| {self.result}', end='')
180 def json_dump(self, with_results):
181 dump = self.opts[0].json_dump(False)
183 dump.append(self.result)
187 class OR(ComplexOptCheck):
188 # self.opts[0] is the option that this OR-check is about.
190 # OR(<X_is_hardened>, <X_is_disabled>)
191 # OR(<X_is_hardened>, <old_X_is_hardened>)
193 for i, opt in enumerate(self.opts):
195 if opt.result.startswith('OK'):
196 self.result = opt.result
197 # Add more info for additional checks:
199 if opt.result == 'OK':
200 self.result = f'OK: {opt.name} is "{opt.expected}"'
201 elif opt.result == 'OK: is not found':
202 self.result = f'OK: {opt.name} is not found'
203 elif opt.result == 'OK: is present':
204 self.result = f'OK: {opt.name} is present'
205 elif opt.result.startswith('OK: is not off'):
206 self.result = f'OK: {opt.name} is not off'
208 # VersionCheck provides enough info
209 assert(opt.result.startswith('OK: version')), \
210 f'unexpected OK description "{opt.result}"'
212 self.result = self.opts[0].result
215 class AND(ComplexOptCheck):
216 # self.opts[0] is the option that this AND-check is about.
218 # AND(<suboption>, <main_option>)
219 # Suboption is not checked if checking of the main_option is failed.
220 # AND(<X_is_disabled>, <old_X_is_disabled>)
222 for i, opt in reversed(list(enumerate(self.opts))):
225 self.result = opt.result
227 if not opt.result.startswith('OK'):
228 # This FAIL is caused by additional checks,
229 # and not by the main option that this AND-check is about.
230 # Describe the reason of the FAIL.
231 if opt.result.startswith('FAIL: \"') or opt.result == 'FAIL: is not found':
232 self.result = f'FAIL: {opt.name} is not "{opt.expected}"'
233 elif opt.result == 'FAIL: is not present':
234 self.result = f'FAIL: {opt.name} is not present'
235 elif opt.result in ('FAIL: is off', 'FAIL: is off, "0"'):
236 self.result = f'FAIL: {opt.name} is off'
237 elif opt.result == 'FAIL: is off, not found':
238 self.result = f'FAIL: {opt.name} is off, not found'
240 # VersionCheck provides enough info
241 self.result = opt.result
242 assert(opt.result.startswith('FAIL: version')), \
243 f'unexpected FAIL description "{opt.result}"'
247 SIMPLE_OPTION_TYPES = ('kconfig', 'cmdline', 'sysctl', 'version')
250 def populate_simple_opt_with_data(opt, data, data_type):
251 assert(opt.type != 'complex'), \
252 f'unexpected ComplexOptCheck "{opt.name}"'
253 assert(opt.type in SIMPLE_OPTION_TYPES), \
254 f'invalid opt type "{opt.type}"'
255 assert(data_type in SIMPLE_OPTION_TYPES), \
256 f'invalid data type "{data_type}"'
260 if data_type != opt.type:
263 if data_type in ('kconfig', 'cmdline', 'sysctl'):
264 opt.state = data.get(opt.name, None)
266 assert(data_type == 'version'), \
267 f'unexpected data type "{data_type}"'
271 def populate_opt_with_data(opt, data, data_type):
272 if opt.type == 'complex':
274 if o.type == 'complex':
275 # Recursion for nested ComplexOptCheck objects
276 populate_opt_with_data(o, data, data_type)
278 populate_simple_opt_with_data(o, data, data_type)
280 assert(opt.type != 'version'), \
281 'a simple check with a single VersionCheck is useless'
282 populate_simple_opt_with_data(opt, data, data_type)
285 def populate_with_data(checklist, data, data_type):
286 for opt in checklist:
287 populate_opt_with_data(opt, data, data_type)
290 def override_expected_value(checklist, name, new_val):
291 for opt in checklist:
293 assert(opt.type in ('kconfig', 'cmdline', 'sysctl')), \
294 f'overriding an expected value for "{opt.type}" checks is not supported yet'
295 opt.expected = new_val
298 def perform_checks(checklist):
299 for opt in checklist: