4 This tool helps me to check Linux kernel options against
5 my security hardening preferences for X86_64, ARM64, X86_32, and ARM.
6 Let the computers do their job!
8 Author: Alexander Popov <alex.popov@linux.com>
10 This module is the engine of checks.
13 # pylint: disable=missing-class-docstring,missing-function-docstring
14 # pylint: disable=line-too-long,invalid-name,too-many-branches
18 def __init__(self, reason, decision, name, expected):
19 assert(name and name == name.strip() and len(name.split()) == 1), \
20 f'invalid name "{name}" for {self.__class__.__name__}'
23 assert(decision and decision == decision.strip() and len(decision.split()) == 1), \
24 f'invalid decision "{decision}" for "{name}" check'
25 self.decision = decision
27 assert(reason and reason == reason.strip() and len(reason.split()) == 1), \
28 f'invalid reason "{reason}" for "{name}" check'
31 assert(expected and expected == expected.strip()), \
32 f'invalid expected value "{expected}" for "{name}" check (1)'
33 val_len = len(expected.split())
35 assert(expected in ('is not set', 'is not off')), \
36 f'invalid expected value "{expected}" for "{name}" check (2)'
38 assert(expected == 'is present'), \
39 f'invalid expected value "{expected}" for "{name}" check (3)'
41 assert(val_len == 1), \
42 f'invalid expected value "{expected}" for "{name}" check (4)'
43 self.expected = expected
53 # handle the 'is present' check
54 if self.expected == 'is present':
55 if self.state is None:
56 self.result = 'FAIL: is not present'
58 self.result = 'OK: is present'
61 # handle the 'is not off' option check
62 if self.expected == 'is not off':
63 if self.state == 'off':
64 self.result = 'FAIL: is off'
65 elif self.state == '0':
66 self.result = 'FAIL: is off, "0"'
67 elif self.state is None:
68 self.result = 'FAIL: is off, not found'
70 self.result = f'OK: is not off, "{self.state}"'
73 # handle the option value check
74 if self.expected == self.state:
76 elif self.state is None:
77 if self.expected == 'is not set':
78 self.result = 'OK: is not found'
80 self.result = 'FAIL: is not found'
82 self.result = f'FAIL: "{self.state}"'
84 def table_print(self, _mode, with_results):
85 print(f'{self.name:<40}|{self.type:^7}|{self.expected:^12}|{self.decision:^10}|{self.reason:^18}', end='')
87 print(f'| {self.result}', end='')
89 def json_dump(self, with_results):
90 dump = [self.name, self.type, self.expected, self.decision, self.reason]
92 dump.append(self.result)
96 class KconfigCheck(OptCheck):
97 def __init__(self, *args, **kwargs):
98 super().__init__(*args, **kwargs)
99 self.name = 'CONFIG_' + self.name
106 class CmdlineCheck(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))), \
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', 'version', 'cmdline')
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'):
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 in ('kconfig', 'cmdline')), \
281 f'bad type "{opt.type}" for a simple check'
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 perform_checks(checklist):
291 for opt in checklist: