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 # Please don't cry if my Python code looks like C.
13 # N.B Hardening command line parameters:
14 # iommu=force (does it help against DMA attacks?)
16 # Mitigations of CPU vulnerabilities:
18 # mitigations=auto,nosmt (nosmt is slow)
20 # spec_store_bypass_disable=on
22 # l1d_flush=on (a part of the l1tf option)
30 # sysrq_always_enabled
35 # Hardware tag-based KASAN with arm64 Memory Tagging Extension (MTE):
37 # kasan.stacktrace=off
40 # N.B. Hardening sysctls:
41 # kernel.kptr_restrict=2 (or 1?)
42 # kernel.dmesg_restrict=1 (also see the kconfig option)
43 # kernel.perf_event_paranoid=3
44 # kernel.kexec_load_disabled=1
45 # kernel.yama.ptrace_scope=3
46 # user.max_user_namespaces=0
47 # what about bpf_jit_enable?
48 # kernel.unprivileged_bpf_disabled=1
49 # net.core.bpf_jit_harden=2
50 # vm.unprivileged_userfaultfd=0
51 # (at first, it disabled unprivileged userfaultfd,
52 # and since v5.11 it enables unprivileged userfaultfd for user-mode only)
53 # vm.mmap_min_addr has a good value
54 # dev.tty.ldisc_autoload=0
55 # fs.protected_symlinks=1
56 # fs.protected_hardlinks=1
57 # fs.protected_fifos=2
58 # fs.protected_regular=2
60 # kernel.modules_disabled=1
61 # kernel.randomize_va_space = 2
64 # pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
65 # pylint: disable=line-too-long,invalid-name,too-many-branches,too-many-statements
69 from argparse import ArgumentParser
70 from collections import OrderedDict
73 from .__about__ import __version__
75 SIMPLE_OPTION_TYPES = ('kconfig', 'version', 'cmdline')
78 # Constructor without the 'expected' parameter is for option presence checks (any value is OK)
79 def __init__(self, reason, decision, name, expected=None):
80 assert(name and name == name.strip() and len(name.split()) == 1), \
81 'invalid name "{}" for {}'.format(name, self.__class__.__name__)
84 assert(decision and decision == decision.strip() and len(decision.split()) == 1), \
85 'invalid decision "{}" for "{}" check'.format(decision, name)
86 self.decision = decision
88 assert(reason and reason == reason.strip() and len(reason.split()) == 1), \
89 'invalid reason "{}" for "{}" check'.format(reason, name)
93 assert(expected == expected.strip()), \
94 'invalid expected value "{}" for "{}" check (1)'.format(expected, name)
95 val_len = len(expected.split())
97 assert(expected == 'is not set'), \
98 'invalid expected value "{}" for "{}" check (2)'.format(expected, name)
100 assert(val_len == 1), \
101 'invalid expected value "{}" for "{}" check (3)'.format(expected, name)
102 self.expected = expected
112 # handle the option presence check
113 if self.expected is None:
114 if self.state is None:
115 self.result = 'FAIL: is not present'
117 self.result = 'OK: is present'
120 # handle the option value check
121 if self.expected == self.state:
123 elif self.state is None:
124 if self.expected == 'is not set':
125 self.result = 'OK: is not found'
127 self.result = 'FAIL: is not found'
129 self.result = 'FAIL: "' + self.state + '"'
131 def table_print(self, _mode, with_results):
132 if self.expected is None:
135 expected = self.expected
136 print('{:<40}|{:^7}|{:^12}|{:^10}|{:^18}'.format(self.name, self.type, expected, self.decision, self.reason), end='')
138 print('| {}'.format(self.result), end='')
140 def json_dump(self, with_results):
141 dump = [self.name, self.type, self.expected, self.decision, self.reason]
143 dump.append(self.result)
147 class KconfigCheck(OptCheck):
148 def __init__(self, *args, **kwargs):
149 super().__init__(*args, **kwargs)
150 self.name = 'CONFIG_' + self.name
157 class CmdlineCheck(OptCheck):
164 def __init__(self, ver_expected):
165 assert(ver_expected and isinstance(ver_expected, tuple) and len(ver_expected) == 2), \
166 'invalid version "{}" for VersionCheck'.format(ver_expected)
167 self.ver_expected = ver_expected
176 if self.ver[0] > self.ver_expected[0]:
177 self.result = 'OK: version >= ' + str(self.ver_expected[0]) + '.' + str(self.ver_expected[1])
179 if self.ver[0] < self.ver_expected[0]:
180 self.result = 'FAIL: version < ' + str(self.ver_expected[0]) + '.' + str(self.ver_expected[1])
182 if self.ver[1] >= self.ver_expected[1]:
183 self.result = 'OK: version >= ' + str(self.ver_expected[0]) + '.' + str(self.ver_expected[1])
185 self.result = 'FAIL: version < ' + str(self.ver_expected[0]) + '.' + str(self.ver_expected[1])
187 def table_print(self, _mode, with_results):
188 ver_req = 'kernel version >= ' + str(self.ver_expected[0]) + '.' + str(self.ver_expected[1])
189 print('{:<91}'.format(ver_req), end='')
191 print('| {}'.format(self.result), end='')
194 class ComplexOptCheck:
195 def __init__(self, *opts):
198 'empty {} check'.format(self.__class__.__name__)
199 assert(len(self.opts) != 1), \
200 'useless {} check: {}'.format(self.__class__.__name__, opts)
201 assert(isinstance(opts[0], (KconfigCheck, CmdlineCheck))), \
202 'invalid {} check: {}'.format(self.__class__.__name__, opts)
211 return self.opts[0].name
215 return self.opts[0].expected
217 def table_print(self, mode, with_results):
218 if mode == 'verbose':
219 print(' {:87}'.format('<<< ' + self.__class__.__name__ + ' >>>'), end='')
221 print('| {}'.format(self.result), end='')
224 o.table_print(mode, with_results)
227 o.table_print(mode, False)
229 print('| {}'.format(self.result), end='')
231 def json_dump(self, with_results):
232 dump = self.opts[0].json_dump(False)
234 dump.append(self.result)
238 class OR(ComplexOptCheck):
239 # self.opts[0] is the option that this OR-check is about.
241 # OR(<X_is_hardened>, <X_is_disabled>)
242 # OR(<X_is_hardened>, <old_X_is_hardened>)
244 for i, opt in enumerate(self.opts):
246 if opt.result.startswith('OK'):
247 self.result = opt.result
248 # Add more info for additional checks:
250 if opt.result == 'OK':
251 self.result = 'OK: {} is "{}"'.format(opt.name, opt.expected)
252 elif opt.result == 'OK: is not found':
253 self.result = 'OK: {} is not found'.format(opt.name)
254 elif opt.result == 'OK: is present':
255 self.result = 'OK: {} is present'.format(opt.name)
257 # VersionCheck provides enough info
258 assert(opt.result.startswith('OK: version')), \
259 'unexpected OK description "{}"'.format(opt.result)
261 self.result = self.opts[0].result
264 class AND(ComplexOptCheck):
265 # self.opts[0] is the option that this AND-check is about.
267 # AND(<suboption>, <main_option>)
268 # Suboption is not checked if checking of the main_option is failed.
269 # AND(<X_is_disabled>, <old_X_is_disabled>)
271 for i, opt in reversed(list(enumerate(self.opts))):
274 self.result = opt.result
276 if not opt.result.startswith('OK'):
277 # This FAIL is caused by additional checks,
278 # and not by the main option that this AND-check is about.
279 # Describe the reason of the FAIL.
280 if opt.result.startswith('FAIL: \"') or opt.result == 'FAIL: is not found':
281 self.result = 'FAIL: {} is not "{}"'.format(opt.name, opt.expected)
282 elif opt.result == 'FAIL: is not present':
283 self.result = 'FAIL: {} is not present'.format(opt.name)
285 # VersionCheck provides enough info
286 self.result = opt.result
287 assert(opt.result.startswith('FAIL: version')), \
288 'unexpected FAIL description "{}"'.format(opt.result)
292 def detect_arch(fname, archs):
293 with open(fname, 'r') as f:
294 arch_pattern = re.compile("CONFIG_[a-zA-Z0-9_]*=y")
296 for line in f.readlines():
297 if arch_pattern.match(line):
298 option, _ = line[7:].split('=', 1)
303 return None, 'more than one supported architecture is detected'
305 return None, 'failed to detect architecture'
309 def detect_kernel_version(fname):
310 with open(fname, 'r') as f:
311 ver_pattern = re.compile("# Linux/.* Kernel Configuration")
312 for line in f.readlines():
313 if ver_pattern.match(line):
317 ver_numbers = ver_str.split('.')
318 if len(ver_numbers) < 3 or not ver_numbers[0].isdigit() or not ver_numbers[1].isdigit():
319 msg = 'failed to parse the version "' + ver_str + '"'
321 return (int(ver_numbers[0]), int(ver_numbers[1])), None
322 return None, 'no kernel version detected'
325 def detect_compiler(fname):
328 with open(fname, 'r') as f:
329 gcc_version_pattern = re.compile("CONFIG_GCC_VERSION=[0-9]*")
330 clang_version_pattern = re.compile("CONFIG_CLANG_VERSION=[0-9]*")
331 for line in f.readlines():
332 if gcc_version_pattern.match(line):
333 gcc_version = line[19:-1]
334 if clang_version_pattern.match(line):
335 clang_version = line[21:-1]
336 if not gcc_version or not clang_version:
337 return None, 'no CONFIG_GCC_VERSION or CONFIG_CLANG_VERSION'
338 if gcc_version == '0' and clang_version != '0':
339 return 'CLANG ' + clang_version, 'OK'
340 if gcc_version != '0' and clang_version == '0':
341 return 'GCC ' + gcc_version, 'OK'
342 sys.exit('[!] ERROR: invalid GCC_VERSION and CLANG_VERSION: {} {}'.format(gcc_version, clang_version))
345 def add_kconfig_checks(l, arch):
346 # Calling the KconfigCheck class constructor:
347 # KconfigCheck(reason, decision, name, expected)
349 # [!] Don't add CmdlineChecks in add_kconfig_checks() to avoid wrong results
350 # when the tool doesn't check the cmdline.
352 efi_not_set = KconfigCheck('-', '-', 'EFI', 'is not set')
353 cc_is_gcc = KconfigCheck('-', '-', 'CC_IS_GCC', 'y') # exists since v4.18
354 cc_is_clang = KconfigCheck('-', '-', 'CC_IS_CLANG', 'y') # exists since v4.18
356 modules_not_set = KconfigCheck('cut_attack_surface', 'kspp', 'MODULES', 'is not set')
357 devmem_not_set = KconfigCheck('cut_attack_surface', 'kspp', 'DEVMEM', 'is not set') # refers to LOCKDOWN
358 bpf_syscall_not_set = KconfigCheck('cut_attack_surface', 'lockdown', 'BPF_SYSCALL', 'is not set') # refers to LOCKDOWN
360 # 'self_protection', 'defconfig'
361 l += [KconfigCheck('self_protection', 'defconfig', 'BUG', 'y')]
362 l += [KconfigCheck('self_protection', 'defconfig', 'SLUB_DEBUG', 'y')]
363 gcc_plugins_support_is_set = KconfigCheck('self_protection', 'defconfig', 'GCC_PLUGINS', 'y')
364 l += [gcc_plugins_support_is_set]
365 l += [OR(KconfigCheck('self_protection', 'defconfig', 'STACKPROTECTOR', 'y'),
366 KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR', 'y'),
367 KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_REGULAR', 'y'),
368 KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_AUTO', 'y'),
369 KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_STRONG', 'y'))]
370 l += [OR(KconfigCheck('self_protection', 'defconfig', 'STACKPROTECTOR_STRONG', 'y'),
371 KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_STRONG', 'y'))]
372 l += [OR(KconfigCheck('self_protection', 'defconfig', 'STRICT_KERNEL_RWX', 'y'),
373 KconfigCheck('self_protection', 'defconfig', 'DEBUG_RODATA', 'y'))] # before v4.11
374 l += [OR(KconfigCheck('self_protection', 'defconfig', 'STRICT_MODULE_RWX', 'y'),
375 KconfigCheck('self_protection', 'defconfig', 'DEBUG_SET_MODULE_RONX', 'y'),
376 modules_not_set)] # DEBUG_SET_MODULE_RONX was before v4.11
377 l += [OR(KconfigCheck('self_protection', 'defconfig', 'REFCOUNT_FULL', 'y'),
378 VersionCheck((5, 5)))] # REFCOUNT_FULL is enabled by default since v5.5
379 l += [KconfigCheck('self_protection', 'defconfig', 'THREAD_INFO_IN_TASK', 'y')]
380 iommu_support_is_set = KconfigCheck('self_protection', 'defconfig', 'IOMMU_SUPPORT', 'y')
381 l += [iommu_support_is_set] # is needed for mitigating DMA attacks
382 if arch in ('X86_64', 'ARM64', 'X86_32'):
383 l += [KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_BASE', 'y')]
384 if arch in ('X86_64', 'ARM64'):
385 l += [KconfigCheck('self_protection', 'defconfig', 'VMAP_STACK', 'y')]
386 if arch in ('X86_64', 'X86_32'):
387 l += [KconfigCheck('self_protection', 'defconfig', 'X86_MCE', 'y')]
388 l += [KconfigCheck('self_protection', 'defconfig', 'X86_MCE_INTEL', 'y')]
389 l += [KconfigCheck('self_protection', 'defconfig', 'X86_MCE_AMD', 'y')]
390 l += [KconfigCheck('self_protection', 'defconfig', 'MICROCODE', 'y')] # is needed for mitigating CPU bugs
391 l += [KconfigCheck('self_protection', 'defconfig', 'RETPOLINE', 'y')]
392 l += [OR(KconfigCheck('self_protection', 'defconfig', 'X86_SMAP', 'y'),
393 VersionCheck((5, 19)))] # X86_SMAP is enabled by default since v5.19
394 l += [KconfigCheck('self_protection', 'defconfig', 'SYN_COOKIES', 'y')] # another reason?
395 l += [OR(KconfigCheck('self_protection', 'defconfig', 'X86_UMIP', 'y'),
396 KconfigCheck('self_protection', 'defconfig', 'X86_INTEL_UMIP', 'y'))]
397 if arch in ('ARM64', 'ARM'):
398 l += [KconfigCheck('self_protection', 'defconfig', 'STACKPROTECTOR_PER_TASK', 'y')]
400 l += [KconfigCheck('self_protection', 'defconfig', 'PAGE_TABLE_ISOLATION', 'y')]
401 l += [KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_MEMORY', 'y')]
402 l += [AND(KconfigCheck('self_protection', 'defconfig', 'INTEL_IOMMU', 'y'),
403 iommu_support_is_set)]
404 l += [AND(KconfigCheck('self_protection', 'defconfig', 'AMD_IOMMU', 'y'),
405 iommu_support_is_set)]
407 l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_PAN', 'y')]
408 l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_EPAN', 'y')]
409 l += [KconfigCheck('self_protection', 'defconfig', 'UNMAP_KERNEL_AT_EL0', 'y')]
410 l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_E0PD', 'y')]
411 l += [OR(KconfigCheck('self_protection', 'defconfig', 'HARDEN_EL2_VECTORS', 'y'),
412 AND(KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_BASE', 'y'),
413 VersionCheck((5, 9))))] # HARDEN_EL2_VECTORS was included in RANDOMIZE_BASE in v5.9
414 l += [KconfigCheck('self_protection', 'defconfig', 'RODATA_FULL_DEFAULT_ENABLED', 'y')]
415 l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_PTR_AUTH_KERNEL', 'y')]
416 l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_BTI_KERNEL', 'y')]
417 l += [OR(KconfigCheck('self_protection', 'defconfig', 'HARDEN_BRANCH_PREDICTOR', 'y'),
418 VersionCheck((5, 10)))] # HARDEN_BRANCH_PREDICTOR is enabled by default since v5.10
419 l += [KconfigCheck('self_protection', 'defconfig', 'MITIGATE_SPECTRE_BRANCH_HISTORY', 'y')]
420 l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_MTE', 'y')]
421 l += [KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_MODULE_REGION_FULL', 'y')]
423 l += [KconfigCheck('self_protection', 'defconfig', 'CPU_SW_DOMAIN_PAN', 'y')]
424 l += [KconfigCheck('self_protection', 'defconfig', 'HARDEN_BRANCH_PREDICTOR', 'y')]
425 l += [KconfigCheck('self_protection', 'defconfig', 'HARDEN_BRANCH_HISTORY', 'y')]
427 # 'self_protection', 'kspp'
428 l += [KconfigCheck('self_protection', 'kspp', 'BUG_ON_DATA_CORRUPTION', 'y')]
429 l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_WX', 'y')]
430 l += [KconfigCheck('self_protection', 'kspp', 'SCHED_STACK_END_CHECK', 'y')]
431 l += [KconfigCheck('self_protection', 'kspp', 'SLAB_FREELIST_HARDENED', 'y')]
432 l += [KconfigCheck('self_protection', 'kspp', 'SLAB_FREELIST_RANDOM', 'y')]
433 l += [KconfigCheck('self_protection', 'kspp', 'SHUFFLE_PAGE_ALLOCATOR', 'y')]
434 l += [KconfigCheck('self_protection', 'kspp', 'FORTIFY_SOURCE', 'y')]
435 l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_LIST', 'y')]
436 l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_VIRTUAL', 'y')]
437 l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_SG', 'y')]
438 l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_CREDENTIALS', 'y')]
439 l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_NOTIFIERS', 'y')]
440 l += [KconfigCheck('self_protection', 'kspp', 'INIT_ON_ALLOC_DEFAULT_ON', 'y')]
441 l += [AND(KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_LATENT_ENTROPY', 'y'),
442 gcc_plugins_support_is_set)]
443 l += [KconfigCheck('self_protection', 'kspp', 'KFENCE', 'y')]
444 l += [KconfigCheck('self_protection', 'kspp', 'WERROR', 'y')]
445 l += [KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_DMA_STRICT', 'y')]
446 l += [KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_PASSTHROUGH', 'is not set')] # true if IOMMU_DEFAULT_DMA_STRICT is set
447 l += [KconfigCheck('self_protection', 'kspp', 'ZERO_CALL_USED_REGS', 'y')]
448 l += [KconfigCheck('self_protection', 'kspp', 'HW_RANDOM_TPM', 'y')]
449 l += [KconfigCheck('self_protection', 'kspp', 'STATIC_USERMODEHELPER', 'y')] # needs userspace support
450 l += [KconfigCheck('self_protection', 'kspp', 'SCHED_CORE', 'y')]
451 randstruct_is_set = OR(KconfigCheck('self_protection', 'kspp', 'RANDSTRUCT_FULL', 'y'),
452 KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_RANDSTRUCT', 'y'))
453 l += [randstruct_is_set]
454 l += [AND(KconfigCheck('self_protection', 'kspp', 'RANDSTRUCT_PERFORMANCE', 'is not set'),
455 KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_RANDSTRUCT_PERFORMANCE', 'is not set'),
457 hardened_usercopy_is_set = KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY', 'y')
458 l += [hardened_usercopy_is_set]
459 l += [AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY_FALLBACK', 'is not set'),
460 hardened_usercopy_is_set)]
461 l += [AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY_PAGESPAN', 'is not set'),
462 hardened_usercopy_is_set)]
463 l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG', 'y'),
465 l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG_ALL', 'y'),
467 l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG_SHA512', 'y'),
469 l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG_FORCE', 'y'),
470 modules_not_set)] # refers to LOCKDOWN
471 l += [OR(KconfigCheck('self_protection', 'kspp', 'INIT_STACK_ALL_ZERO', 'y'),
472 KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_STRUCTLEAK_BYREF_ALL', 'y'))]
473 l += [OR(KconfigCheck('self_protection', 'kspp', 'INIT_ON_FREE_DEFAULT_ON', 'y'),
474 KconfigCheck('self_protection', 'kspp', 'PAGE_POISONING_ZERO', 'y'))]
475 # CONFIG_INIT_ON_FREE_DEFAULT_ON was added in v5.3.
476 # CONFIG_PAGE_POISONING_ZERO was removed in v5.11.
477 # Starting from v5.11 CONFIG_PAGE_POISONING unconditionally checks
478 # the 0xAA poison pattern on allocation.
479 # That brings higher performance penalty.
480 l += [OR(KconfigCheck('self_protection', 'kspp', 'EFI_DISABLE_PCI_DMA', 'y'),
482 l += [OR(KconfigCheck('self_protection', 'kspp', 'RESET_ATTACK_MITIGATION', 'y'),
483 efi_not_set)] # needs userspace support (systemd)
484 ubsan_bounds_is_set = KconfigCheck('self_protection', 'kspp', 'UBSAN_BOUNDS', 'y')
485 l += [ubsan_bounds_is_set]
486 l += [OR(KconfigCheck('self_protection', 'kspp', 'UBSAN_LOCAL_BOUNDS', 'y'),
487 AND(ubsan_bounds_is_set,
489 l += [AND(KconfigCheck('self_protection', 'kspp', 'UBSAN_TRAP', 'y'),
491 KconfigCheck('self_protection', 'kspp', 'UBSAN_SHIFT', 'is not set'),
492 KconfigCheck('self_protection', 'kspp', 'UBSAN_DIV_ZERO', 'is not set'),
493 KconfigCheck('self_protection', 'kspp', 'UBSAN_UNREACHABLE', 'is not set'),
494 KconfigCheck('self_protection', 'kspp', 'UBSAN_BOOL', 'is not set'),
495 KconfigCheck('self_protection', 'kspp', 'UBSAN_ENUM', 'is not set'),
496 KconfigCheck('self_protection', 'kspp', 'UBSAN_ALIGNMENT', 'is not set'))] # only array index bounds checking with traps
497 if arch in ('X86_64', 'ARM64', 'X86_32'):
498 l += [AND(KconfigCheck('self_protection', 'kspp', 'UBSAN_SANITIZE_ALL', 'y'),
499 ubsan_bounds_is_set)] # ARCH_HAS_UBSAN_SANITIZE_ALL is not enabled for ARM
500 stackleak_is_set = KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_STACKLEAK', 'y')
501 l += [AND(stackleak_is_set, gcc_plugins_support_is_set)]
502 l += [AND(KconfigCheck('self_protection', 'kspp', 'STACKLEAK_METRICS', 'is not set'),
504 gcc_plugins_support_is_set)]
505 l += [AND(KconfigCheck('self_protection', 'kspp', 'STACKLEAK_RUNTIME_DISABLE', 'is not set'),
507 gcc_plugins_support_is_set)]
508 l += [KconfigCheck('self_protection', 'kspp', 'RANDOMIZE_KSTACK_OFFSET_DEFAULT', 'y')]
509 if arch in ('X86_64', 'ARM64'):
510 cfi_clang_is_set = KconfigCheck('self_protection', 'kspp', 'CFI_CLANG', 'y')
511 l += [cfi_clang_is_set]
512 l += [AND(KconfigCheck('self_protection', 'kspp', 'CFI_PERMISSIVE', 'is not set'),
514 if arch in ('X86_64', 'X86_32'):
515 l += [KconfigCheck('self_protection', 'kspp', 'DEFAULT_MMAP_MIN_ADDR', '65536')]
516 l += [AND(KconfigCheck('self_protection', 'kspp', 'INTEL_IOMMU_DEFAULT_ON', 'y'),
517 iommu_support_is_set)]
518 if arch in ('ARM64', 'ARM'):
519 l += [KconfigCheck('self_protection', 'kspp', 'DEFAULT_MMAP_MIN_ADDR', '32768')]
520 l += [KconfigCheck('self_protection', 'kspp', 'SYN_COOKIES', 'y')] # another reason?
522 l += [KconfigCheck('self_protection', 'kspp', 'SLS', 'y')] # vs CVE-2021-26341 in Straight-Line-Speculation
523 l += [AND(KconfigCheck('self_protection', 'kspp', 'INTEL_IOMMU_SVM', 'y'),
524 iommu_support_is_set)]
525 l += [AND(KconfigCheck('self_protection', 'kspp', 'AMD_IOMMU_V2', 'y'),
526 iommu_support_is_set)]
528 l += [KconfigCheck('self_protection', 'kspp', 'ARM64_SW_TTBR0_PAN', 'y')]
529 l += [KconfigCheck('self_protection', 'kspp', 'SHADOW_CALL_STACK', 'y')]
530 l += [KconfigCheck('self_protection', 'kspp', 'KASAN_HW_TAGS', 'y')]
532 l += [KconfigCheck('self_protection', 'kspp', 'PAGE_TABLE_ISOLATION', 'y')]
533 l += [KconfigCheck('self_protection', 'kspp', 'HIGHMEM64G', 'y')]
534 l += [KconfigCheck('self_protection', 'kspp', 'X86_PAE', 'y')]
535 l += [AND(KconfigCheck('self_protection', 'kspp', 'INTEL_IOMMU', 'y'),
536 iommu_support_is_set)]
538 # 'self_protection', 'clipos'
539 l += [KconfigCheck('self_protection', 'clipos', 'SLAB_MERGE_DEFAULT', 'is not set')]
542 if arch in ('X86_64', 'ARM64', 'X86_32'):
543 l += [KconfigCheck('security_policy', 'defconfig', 'SECURITY', 'y')] # and choose your favourite LSM
545 l += [KconfigCheck('security_policy', 'kspp', 'SECURITY', 'y')] # and choose your favourite LSM
546 l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_YAMA', 'y')]
547 l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_LANDLOCK', 'y')]
548 l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_SELINUX_DISABLE', 'is not set')]
549 l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_SELINUX_BOOTPARAM', 'is not set')]
550 l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_SELINUX_DEVELOP', 'is not set')]
551 l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_LOCKDOWN_LSM', 'y')]
552 l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_LOCKDOWN_LSM_EARLY', 'y')]
553 l += [KconfigCheck('security_policy', 'kspp', 'LOCK_DOWN_KERNEL_FORCE_CONFIDENTIALITY', 'y')]
554 l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_WRITABLE_HOOKS', 'is not set')] # refers to SECURITY_SELINUX_DISABLE
556 # 'cut_attack_surface', 'defconfig'
557 l += [OR(KconfigCheck('cut_attack_surface', 'defconfig', 'BPF_UNPRIV_DEFAULT_OFF', 'y'),
558 bpf_syscall_not_set)] # see unprivileged_bpf_disabled
559 l += [KconfigCheck('cut_attack_surface', 'defconfig', 'SECCOMP', 'y')]
560 l += [KconfigCheck('cut_attack_surface', 'defconfig', 'SECCOMP_FILTER', 'y')]
561 if arch in ('X86_64', 'ARM64', 'X86_32'):
562 l += [OR(KconfigCheck('cut_attack_surface', 'defconfig', 'STRICT_DEVMEM', 'y'),
563 devmem_not_set)] # refers to LOCKDOWN
565 # 'cut_attack_surface', 'kspp'
566 l += [KconfigCheck('cut_attack_surface', 'kspp', 'SECURITY_DMESG_RESTRICT', 'y')]
567 l += [KconfigCheck('cut_attack_surface', 'kspp', 'ACPI_CUSTOM_METHOD', 'is not set')] # refers to LOCKDOWN
568 l += [KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT_BRK', 'is not set')]
569 l += [KconfigCheck('cut_attack_surface', 'kspp', 'DEVKMEM', 'is not set')] # refers to LOCKDOWN
570 l += [KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT_VDSO', 'is not set')]
571 l += [KconfigCheck('cut_attack_surface', 'kspp', 'BINFMT_MISC', 'is not set')]
572 l += [KconfigCheck('cut_attack_surface', 'kspp', 'INET_DIAG', 'is not set')]
573 l += [KconfigCheck('cut_attack_surface', 'kspp', 'KEXEC', 'is not set')] # refers to LOCKDOWN
574 l += [KconfigCheck('cut_attack_surface', 'kspp', 'PROC_KCORE', 'is not set')] # refers to LOCKDOWN
575 l += [KconfigCheck('cut_attack_surface', 'kspp', 'LEGACY_PTYS', 'is not set')]
576 l += [KconfigCheck('cut_attack_surface', 'kspp', 'HIBERNATION', 'is not set')] # refers to LOCKDOWN
577 l += [KconfigCheck('cut_attack_surface', 'kspp', 'IA32_EMULATION', 'is not set')]
578 l += [KconfigCheck('cut_attack_surface', 'kspp', 'X86_X32', 'is not set')]
579 l += [KconfigCheck('cut_attack_surface', 'kspp', 'MODIFY_LDT_SYSCALL', 'is not set')]
580 l += [KconfigCheck('cut_attack_surface', 'kspp', 'OABI_COMPAT', 'is not set')]
581 l += [KconfigCheck('cut_attack_surface', 'kspp', 'X86_MSR', 'is not set')] # refers to LOCKDOWN
582 l += [modules_not_set]
583 l += [devmem_not_set]
584 l += [OR(KconfigCheck('cut_attack_surface', 'kspp', 'IO_STRICT_DEVMEM', 'y'),
585 devmem_not_set)] # refers to LOCKDOWN
586 l += [AND(KconfigCheck('cut_attack_surface', 'kspp', 'LDISC_AUTOLOAD', 'is not set'),
587 KconfigCheck('cut_attack_surface', 'kspp', 'LDISC_AUTOLOAD'))] # option presence check
589 l += [OR(KconfigCheck('cut_attack_surface', 'kspp', 'STRICT_DEVMEM', 'y'),
590 devmem_not_set)] # refers to LOCKDOWN
592 l += [KconfigCheck('cut_attack_surface', 'kspp', 'LEGACY_VSYSCALL_NONE', 'y')] # 'vsyscall=none'
594 # 'cut_attack_surface', 'grsec'
595 l += [KconfigCheck('cut_attack_surface', 'grsec', 'ZSMALLOC_STAT', 'is not set')]
596 l += [KconfigCheck('cut_attack_surface', 'grsec', 'PAGE_OWNER', 'is not set')]
597 l += [KconfigCheck('cut_attack_surface', 'grsec', 'DEBUG_KMEMLEAK', 'is not set')]
598 l += [KconfigCheck('cut_attack_surface', 'grsec', 'BINFMT_AOUT', 'is not set')]
599 l += [KconfigCheck('cut_attack_surface', 'grsec', 'KPROBE_EVENTS', 'is not set')]
600 l += [KconfigCheck('cut_attack_surface', 'grsec', 'UPROBE_EVENTS', 'is not set')]
601 l += [KconfigCheck('cut_attack_surface', 'grsec', 'GENERIC_TRACER', 'is not set')] # refers to LOCKDOWN
602 l += [KconfigCheck('cut_attack_surface', 'grsec', 'FUNCTION_TRACER', 'is not set')]
603 l += [KconfigCheck('cut_attack_surface', 'grsec', 'STACK_TRACER', 'is not set')]
604 l += [KconfigCheck('cut_attack_surface', 'grsec', 'HIST_TRIGGERS', 'is not set')]
605 l += [KconfigCheck('cut_attack_surface', 'grsec', 'BLK_DEV_IO_TRACE', 'is not set')]
606 l += [KconfigCheck('cut_attack_surface', 'grsec', 'PROC_VMCORE', 'is not set')]
607 l += [KconfigCheck('cut_attack_surface', 'grsec', 'PROC_PAGE_MONITOR', 'is not set')]
608 l += [KconfigCheck('cut_attack_surface', 'grsec', 'USELIB', 'is not set')]
609 l += [KconfigCheck('cut_attack_surface', 'grsec', 'CHECKPOINT_RESTORE', 'is not set')]
610 l += [KconfigCheck('cut_attack_surface', 'grsec', 'USERFAULTFD', 'is not set')]
611 l += [KconfigCheck('cut_attack_surface', 'grsec', 'HWPOISON_INJECT', 'is not set')]
612 l += [KconfigCheck('cut_attack_surface', 'grsec', 'MEM_SOFT_DIRTY', 'is not set')]
613 l += [KconfigCheck('cut_attack_surface', 'grsec', 'DEVPORT', 'is not set')] # refers to LOCKDOWN
614 l += [KconfigCheck('cut_attack_surface', 'grsec', 'DEBUG_FS', 'is not set')] # refers to LOCKDOWN
615 l += [KconfigCheck('cut_attack_surface', 'grsec', 'NOTIFIER_ERROR_INJECTION', 'is not set')]
616 l += [KconfigCheck('cut_attack_surface', 'grsec', 'FAIL_FUTEX', 'is not set')]
617 l += [KconfigCheck('cut_attack_surface', 'grsec', 'PUNIT_ATOM_DEBUG', 'is not set')]
618 l += [KconfigCheck('cut_attack_surface', 'grsec', 'ACPI_CONFIGFS', 'is not set')]
619 l += [KconfigCheck('cut_attack_surface', 'grsec', 'EDAC_DEBUG', 'is not set')]
620 l += [KconfigCheck('cut_attack_surface', 'grsec', 'DRM_I915_DEBUG', 'is not set')]
621 l += [KconfigCheck('cut_attack_surface', 'grsec', 'BCACHE_CLOSURES_DEBUG', 'is not set')]
622 l += [KconfigCheck('cut_attack_surface', 'grsec', 'DVB_C8SECTPFE', 'is not set')]
623 l += [KconfigCheck('cut_attack_surface', 'grsec', 'MTD_SLRAM', 'is not set')]
624 l += [KconfigCheck('cut_attack_surface', 'grsec', 'MTD_PHRAM', 'is not set')]
625 l += [KconfigCheck('cut_attack_surface', 'grsec', 'IO_URING', 'is not set')]
626 l += [KconfigCheck('cut_attack_surface', 'grsec', 'KCMP', 'is not set')]
627 l += [KconfigCheck('cut_attack_surface', 'grsec', 'RSEQ', 'is not set')]
628 l += [KconfigCheck('cut_attack_surface', 'grsec', 'LATENCYTOP', 'is not set')]
629 l += [KconfigCheck('cut_attack_surface', 'grsec', 'KCOV', 'is not set')]
630 l += [KconfigCheck('cut_attack_surface', 'grsec', 'PROVIDE_OHCI1394_DMA_INIT', 'is not set')]
631 l += [KconfigCheck('cut_attack_surface', 'grsec', 'SUNRPC_DEBUG', 'is not set')]
632 l += [AND(KconfigCheck('cut_attack_surface', 'grsec', 'PTDUMP_DEBUGFS', 'is not set'),
633 KconfigCheck('cut_attack_surface', 'grsec', 'X86_PTDUMP', 'is not set'))]
635 # 'cut_attack_surface', 'maintainer'
636 l += [KconfigCheck('cut_attack_surface', 'maintainer', 'DRM_LEGACY', 'is not set')] # recommended by Daniel Vetter in /issues/38
637 l += [KconfigCheck('cut_attack_surface', 'maintainer', 'FB', 'is not set')] # recommended by Daniel Vetter in /issues/38
638 l += [KconfigCheck('cut_attack_surface', 'maintainer', 'VT', 'is not set')] # recommended by Daniel Vetter in /issues/38
639 l += [KconfigCheck('cut_attack_surface', 'maintainer', 'BLK_DEV_FD', 'is not set')] # recommended by Denis Efremov in /pull/54
640 l += [KconfigCheck('cut_attack_surface', 'maintainer', 'BLK_DEV_FD_RAWCMD', 'is not set')] # recommended by Denis Efremov in /pull/62
642 # 'cut_attack_surface', 'grapheneos'
643 l += [KconfigCheck('cut_attack_surface', 'grapheneos', 'AIO', 'is not set')]
645 # 'cut_attack_surface', 'clipos'
646 l += [KconfigCheck('cut_attack_surface', 'clipos', 'STAGING', 'is not set')]
647 l += [KconfigCheck('cut_attack_surface', 'clipos', 'KSM', 'is not set')] # to prevent FLUSH+RELOAD attack
648 # l += [KconfigCheck('cut_attack_surface', 'clipos', 'IKCONFIG', 'is not set')] # no, IKCONFIG is needed for this check :)
649 l += [KconfigCheck('cut_attack_surface', 'clipos', 'KALLSYMS', 'is not set')]
650 l += [KconfigCheck('cut_attack_surface', 'clipos', 'X86_VSYSCALL_EMULATION', 'is not set')]
651 l += [KconfigCheck('cut_attack_surface', 'clipos', 'MAGIC_SYSRQ', 'is not set')]
652 l += [KconfigCheck('cut_attack_surface', 'clipos', 'KEXEC_FILE', 'is not set')] # refers to LOCKDOWN (permissive)
653 l += [KconfigCheck('cut_attack_surface', 'clipos', 'USER_NS', 'is not set')] # user.max_user_namespaces=0
654 l += [KconfigCheck('cut_attack_surface', 'clipos', 'X86_CPUID', 'is not set')]
655 l += [KconfigCheck('cut_attack_surface', 'clipos', 'X86_IOPL_IOPERM', 'is not set')] # refers to LOCKDOWN
656 l += [KconfigCheck('cut_attack_surface', 'clipos', 'ACPI_TABLE_UPGRADE', 'is not set')] # refers to LOCKDOWN
657 l += [KconfigCheck('cut_attack_surface', 'clipos', 'EFI_CUSTOM_SSDT_OVERLAYS', 'is not set')]
658 l += [KconfigCheck('cut_attack_surface', 'clipos', 'COREDUMP', 'is not set')] # cut userspace attack surface
659 if arch in ('X86_64', 'X86_32'):
660 l += [KconfigCheck('cut_attack_surface', 'clipos', 'X86_INTEL_TSX_MODE_OFF', 'y')] # tsx=off
662 # 'cut_attack_surface', 'lockdown'
663 l += [bpf_syscall_not_set] # refers to LOCKDOWN
664 l += [KconfigCheck('cut_attack_surface', 'lockdown', 'EFI_TEST', 'is not set')] # refers to LOCKDOWN
665 l += [KconfigCheck('cut_attack_surface', 'lockdown', 'MMIOTRACE_TEST', 'is not set')] # refers to LOCKDOWN
666 l += [KconfigCheck('cut_attack_surface', 'lockdown', 'KPROBES', 'is not set')] # refers to LOCKDOWN
668 # 'cut_attack_surface', 'my'
669 l += [OR(KconfigCheck('cut_attack_surface', 'my', 'TRIM_UNUSED_KSYMS', 'y'),
671 l += [KconfigCheck('cut_attack_surface', 'my', 'MMIOTRACE', 'is not set')] # refers to LOCKDOWN (permissive)
672 l += [KconfigCheck('cut_attack_surface', 'my', 'LIVEPATCH', 'is not set')]
673 l += [KconfigCheck('cut_attack_surface', 'my', 'IP_DCCP', 'is not set')]
674 l += [KconfigCheck('cut_attack_surface', 'my', 'IP_SCTP', 'is not set')]
675 l += [KconfigCheck('cut_attack_surface', 'my', 'FTRACE', 'is not set')] # refers to LOCKDOWN
676 l += [KconfigCheck('cut_attack_surface', 'my', 'VIDEO_VIVID', 'is not set')]
677 l += [KconfigCheck('cut_attack_surface', 'my', 'INPUT_EVBUG', 'is not set')] # Can be used as a keylogger
678 l += [KconfigCheck('cut_attack_surface', 'my', 'KGDB', 'is not set')]
681 if arch in ('X86_64', 'ARM64', 'X86_32'):
682 l += [KconfigCheck('harden_userspace', 'defconfig', 'INTEGRITY', 'y')]
684 l += [KconfigCheck('harden_userspace', 'my', 'INTEGRITY', 'y')]
686 l += [KconfigCheck('harden_userspace', 'defconfig', 'ARM64_PTR_AUTH', 'y')]
687 l += [KconfigCheck('harden_userspace', 'defconfig', 'ARM64_BTI', 'y')]
688 if arch in ('ARM', 'X86_32'):
689 l += [KconfigCheck('harden_userspace', 'defconfig', 'VMSPLIT_3G', 'y')]
690 if arch in ('X86_64', 'ARM64'):
691 l += [KconfigCheck('harden_userspace', 'clipos', 'ARCH_MMAP_RND_BITS', '32')]
692 if arch in ('X86_32', 'ARM'):
693 l += [KconfigCheck('harden_userspace', 'my', 'ARCH_MMAP_RND_BITS', '16')]
696 def add_cmdline_checks(l, arch):
697 # Calling the CmdlineCheck class constructor:
698 # CmdlineCheck(reason, decision, name, expected)
700 # [!] Don't add CmdlineChecks in add_kconfig_checks() to avoid wrong results
701 # when the tool doesn't check the cmdline.
703 # [!] Make sure that values of the options in CmdlineChecks need normalization.
704 # For more info see normalize_cmdline_options().
706 # A common pattern for checking the 'param_x' cmdline parameter
707 # that __overrides__ the 'PARAM_X_DEFAULT' kconfig option:
708 # l += [OR(CmdlineCheck(reason, decision, 'param_x', '1'),
709 # AND(KconfigCheck(reason, decision, 'PARAM_X_DEFAULT_ON', 'y'),
710 # CmdlineCheck(reason, decision, 'param_x, 'is not set')))]
712 # Here we don't check the kconfig options or minimal kernel version
713 # required for the cmdline parameters. That would make the checks
714 # very complex and not give a 100% guarantee anyway.
716 # 'self_protection', 'defconfig'
717 l += [CmdlineCheck('self_protection', 'defconfig', 'nosmep', 'is not set')]
718 l += [CmdlineCheck('self_protection', 'defconfig', 'nosmap', 'is not set')]
719 l += [CmdlineCheck('self_protection', 'defconfig', 'nokaslr', 'is not set')]
720 l += [CmdlineCheck('self_protection', 'defconfig', 'nopti', 'is not set')]
721 l += [CmdlineCheck('self_protection', 'defconfig', 'nospectre_v1', 'is not set')]
722 l += [CmdlineCheck('self_protection', 'defconfig', 'nospectre_v2', 'is not set')]
724 l += [OR(CmdlineCheck('self_protection', 'defconfig', 'rodata', 'full'),
725 AND(KconfigCheck('self_protection', 'defconfig', 'RODATA_FULL_DEFAULT_ENABLED', 'y'),
726 CmdlineCheck('self_protection', 'defconfig', 'rodata', 'is not set')))]
728 l += [OR(CmdlineCheck('self_protection', 'defconfig', 'rodata', '1'),
729 CmdlineCheck('self_protection', 'defconfig', 'rodata', 'is not set'))]
731 # 'self_protection', 'kspp'
732 l += [OR(CmdlineCheck('self_protection', 'kspp', 'init_on_alloc', '1'),
733 AND(KconfigCheck('self_protection', 'kspp', 'INIT_ON_ALLOC_DEFAULT_ON', 'y'),
734 CmdlineCheck('self_protection', 'kspp', 'init_on_alloc', 'is not set')))]
735 l += [OR(CmdlineCheck('self_protection', 'kspp', 'init_on_free', '1'),
736 AND(KconfigCheck('self_protection', 'kspp', 'INIT_ON_FREE_DEFAULT_ON', 'y'),
737 CmdlineCheck('self_protection', 'kspp', 'init_on_free', 'is not set')),
738 AND(CmdlineCheck('self_protection', 'kspp', 'page_poison', '1'),
739 KconfigCheck('self_protection', 'kspp', 'PAGE_POISONING_ZERO', 'y'),
740 CmdlineCheck('self_protection', 'kspp', 'slub_debug', 'P')))]
741 l += [OR(CmdlineCheck('self_protection', 'kspp', 'slab_nomerge'),
742 AND(KconfigCheck('self_protection', 'clipos', 'SLAB_MERGE_DEFAULT', 'is not set'),
743 CmdlineCheck('self_protection', 'kspp', 'slab_merge', 'is not set')))] # option presence check
744 l += [OR(CmdlineCheck('self_protection', 'kspp', 'iommu.strict', '1'),
745 AND(KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_DMA_STRICT', 'y'),
746 CmdlineCheck('self_protection', 'kspp', 'iommu.strict', 'is not set')))]
747 l += [OR(CmdlineCheck('self_protection', 'kspp', 'iommu.passthrough', '0'),
748 AND(KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_PASSTHROUGH', 'is not set'),
749 CmdlineCheck('self_protection', 'kspp', 'iommu.passthrough', 'is not set')))]
750 # The cmdline checks compatible with the kconfig recommendations of the KSPP project...
751 l += [OR(CmdlineCheck('self_protection', 'kspp', 'hardened_usercopy', '1'),
752 AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY', 'y'),
753 CmdlineCheck('self_protection', 'kspp', 'hardened_usercopy', 'is not set')))]
754 l += [OR(CmdlineCheck('self_protection', 'kspp', 'slab_common.usercopy_fallback', '0'),
755 AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY_FALLBACK', 'is not set'),
756 CmdlineCheck('self_protection', 'kspp', 'slab_common.usercopy_fallback', 'is not set')))]
758 if arch in ('X86_64', 'ARM64', 'X86_32'):
759 l += [OR(CmdlineCheck('self_protection', 'kspp', 'randomize_kstack_offset', '1'),
760 AND(KconfigCheck('self_protection', 'kspp', 'RANDOMIZE_KSTACK_OFFSET_DEFAULT', 'y'),
761 CmdlineCheck('self_protection', 'kspp', 'randomize_kstack_offset', 'is not set')))]
762 if arch in ('X86_64', 'X86_32'):
763 l += [AND(CmdlineCheck('self_protection', 'kspp', 'pti', 'on'),
764 CmdlineCheck('self_protection', 'defconfig', 'nopti', 'is not set'))]
766 # 'self_protection', 'clipos'
767 l += [CmdlineCheck('self_protection', 'clipos', 'page_alloc.shuffle', '1')]
768 if arch in ('X86_64', 'X86_32'):
769 l += [AND(CmdlineCheck('self_protection', 'clipos', 'spectre_v2', 'on'),
770 CmdlineCheck('self_protection', 'defconfig', 'nospectre_v2', 'is not set'))]
772 # 'cut_attack_surface', 'kspp'
774 l += [OR(CmdlineCheck('cut_attack_surface', 'kspp', 'vsyscall', 'none'),
775 AND(KconfigCheck('cut_attack_surface', 'kspp', 'LEGACY_VSYSCALL_NONE', 'y'),
776 CmdlineCheck('cut_attack_surface', 'kspp', 'vsyscall', 'is not set')))]
778 # 'cut_attack_surface', 'grsec'
779 # The cmdline checks compatible with the kconfig options disabled by grsecurity...
780 l += [OR(CmdlineCheck('cut_attack_surface', 'grsec', 'debugfs', 'off'),
781 KconfigCheck('cut_attack_surface', 'grsec', 'DEBUG_FS', 'is not set'))] # ... the end
784 def print_unknown_options(checklist, parsed_options):
788 if o1.type != 'complex':
789 known_options.append(o1.name)
792 if o2.type != 'complex':
793 if hasattr(o2, 'name'):
794 known_options.append(o2.name)
797 assert(o3.type != 'complex'), \
798 'unexpected ComplexOptCheck inside {}'.format(o2.name)
799 if hasattr(o3, 'name'):
800 known_options.append(o3.name)
802 for option, value in parsed_options.items():
803 if option not in known_options:
804 print('[?] No check for option {} ({})'.format(option, value))
807 def print_checklist(mode, checklist, with_results):
811 output.append(o.json_dump(with_results))
812 print(json.dumps(output))
819 print('=' * sep_line_len)
820 print('{:^40}|{:^7}|{:^12}|{:^10}|{:^18}'.format('option name', 'type', 'desired val', 'decision', 'reason'), end='')
822 print('| {}'.format('check result'), end='')
824 print('=' * sep_line_len)
827 for opt in checklist:
829 if mode == 'show_ok':
830 if not opt.result.startswith('OK'):
832 if mode == 'show_fail':
833 if not opt.result.startswith('FAIL'):
835 opt.table_print(mode, with_results)
837 if mode == 'verbose':
838 print('-' * sep_line_len)
843 fail_count = len(list(filter(lambda opt: opt.result.startswith('FAIL'), checklist)))
845 ok_count = len(list(filter(lambda opt: opt.result.startswith('OK'), checklist)))
847 if mode == 'show_ok':
848 fail_suppressed = ' (suppressed in output)'
849 if mode == 'show_fail':
850 ok_suppressed = ' (suppressed in output)'
852 print('[+] Config check is finished: \'OK\' - {}{} / \'FAIL\' - {}{}'.format(ok_count, ok_suppressed, fail_count, fail_suppressed))
855 def populate_simple_opt_with_data(opt, data, data_type):
856 assert(opt.type != 'complex'), \
857 'unexpected ComplexOptCheck "{}"'.format(opt.name)
858 assert(opt.type in SIMPLE_OPTION_TYPES), \
859 'invalid opt type "{}"'.format(opt.type)
860 assert(data_type in SIMPLE_OPTION_TYPES), \
861 'invalid data type "{}"'.format(data_type)
863 if data_type != opt.type:
866 if data_type in ('kconfig', 'cmdline'):
867 opt.state = data.get(opt.name, None)
869 assert(data_type == 'version'), \
870 'unexpected data type "{}"'.format(data_type)
874 def populate_opt_with_data(opt, data, data_type):
875 if opt.type == 'complex':
877 if o.type == 'complex':
878 # Recursion for nested ComplexOptCheck objects
879 populate_opt_with_data(o, data, data_type)
881 populate_simple_opt_with_data(o, data, data_type)
883 assert(opt.type in ('kconfig', 'cmdline')), \
884 'bad type "{}" for a simple check'.format(opt.type)
885 populate_simple_opt_with_data(opt, data, data_type)
888 def populate_with_data(checklist, data, data_type):
889 for opt in checklist:
890 populate_opt_with_data(opt, data, data_type)
893 def perform_checks(checklist):
894 for opt in checklist:
898 def parse_kconfig_file(parsed_options, fname):
899 with open(fname, 'r') as f:
900 opt_is_on = re.compile("CONFIG_[a-zA-Z0-9_]*=[a-zA-Z0-9_\"]*")
901 opt_is_off = re.compile("# CONFIG_[a-zA-Z0-9_]* is not set")
903 for line in f.readlines():
908 if opt_is_on.match(line):
909 option, value = line.split('=', 1)
910 if value == 'is not set':
911 sys.exit('[!] ERROR: bad enabled kconfig option "{}"'.format(line))
912 elif opt_is_off.match(line):
913 option, value = line[2:].split(' ', 1)
914 if value != 'is not set':
915 sys.exit('[!] ERROR: bad disabled kconfig option "{}"'.format(line))
917 if option in parsed_options:
918 sys.exit('[!] ERROR: kconfig option "{}" exists multiple times'.format(line))
921 parsed_options[option] = value
924 def normalize_cmdline_options(option, value):
925 # Don't normalize the cmdline option values if
926 # the Linux kernel doesn't use kstrtobool() for them
928 # See pti_check_boottime_disable() in linux/arch/x86/mm/pti.c
930 if option == 'spectre_v2':
931 # See spectre_v2_parse_cmdline() in linux/arch/x86/kernel/cpu/bugs.c
933 if option == 'debugfs':
934 # See debugfs_kernel() in fs/debugfs/inode.c
937 # Implement a limited part of the kstrtobool() logic
938 if value in ('1', 'on', 'On', 'ON', 'y', 'Y', 'yes', 'Yes', 'YES'):
940 if value in ('0', 'off', 'Off', 'OFF', 'n', 'N', 'no', 'No', 'NO'):
943 # Preserve unique values
947 def parse_cmdline_file(parsed_options, fname):
948 with open(fname, 'r') as f:
954 sys.exit('[!] ERROR: more than one line in "{}"'.format(fname))
958 name, value = opt.split('=', 1)
961 value = '' # '' is not None
962 value = normalize_cmdline_options(name, value)
963 parsed_options[name] = value
969 # - reporting about unknown kernel options in the kconfig
970 # - verbose printing of ComplexOptCheck items
971 # * json mode for printing the results in JSON format
972 report_modes = ['verbose', 'json', 'show_ok', 'show_fail']
973 supported_archs = ['X86_64', 'X86_32', 'ARM64', 'ARM']
974 parser = ArgumentParser(prog='kconfig-hardened-check',
975 description='A tool for checking the security hardening options of the Linux kernel')
976 parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
977 parser.add_argument('-p', '--print', choices=supported_archs,
978 help='print security hardening preferences for the selected architecture')
979 parser.add_argument('-c', '--config',
980 help='check the kernel kconfig file against these preferences')
981 parser.add_argument('-l', '--cmdline',
982 help='check the kernel cmdline file against these preferences')
983 parser.add_argument('-m', '--mode', choices=report_modes,
984 help='choose the report mode')
985 args = parser.parse_args()
991 print('[+] Special report mode: {}'.format(mode))
993 config_checklist = []
997 sys.exit('[!] ERROR: --config and --print can\'t be used together')
1000 print('[+] Kconfig file to check: {}'.format(args.config))
1002 print('[+] Kernel cmdline file to check: {}'.format(args.cmdline))
1004 arch, msg = detect_arch(args.config, supported_archs)
1006 sys.exit('[!] ERROR: {}'.format(msg))
1008 print('[+] Detected architecture: {}'.format(arch))
1010 kernel_version, msg = detect_kernel_version(args.config)
1011 if not kernel_version:
1012 sys.exit('[!] ERROR: {}'.format(msg))
1014 print('[+] Detected kernel version: {}.{}'.format(kernel_version[0], kernel_version[1]))
1016 compiler, msg = detect_compiler(args.config)
1019 print('[+] Detected compiler: {}'.format(compiler))
1021 print('[-] Can\'t detect the compiler: {}'.format(msg))
1023 # add relevant kconfig checks to the checklist
1024 add_kconfig_checks(config_checklist, arch)
1027 # add relevant cmdline checks to the checklist
1028 add_cmdline_checks(config_checklist, arch)
1030 # populate the checklist with the parsed kconfig data
1031 parsed_kconfig_options = OrderedDict()
1032 parse_kconfig_file(parsed_kconfig_options, args.config)
1033 populate_with_data(config_checklist, parsed_kconfig_options, 'kconfig')
1034 populate_with_data(config_checklist, kernel_version, 'version')
1037 # populate the checklist with the parsed kconfig data
1038 parsed_cmdline_options = OrderedDict()
1039 parse_cmdline_file(parsed_cmdline_options, args.cmdline)
1040 populate_with_data(config_checklist, parsed_cmdline_options, 'cmdline')
1042 # now everything is ready for performing the checks
1043 perform_checks(config_checklist)
1045 # finally print the results
1046 if mode == 'verbose':
1047 print_unknown_options(config_checklist, parsed_kconfig_options)
1048 print_checklist(mode, config_checklist, True)
1052 sys.exit('[!] ERROR: checking cmdline doesn\'t work without checking kconfig')
1055 if mode in ('show_ok', 'show_fail'):
1056 sys.exit('[!] ERROR: wrong mode "{}" for --print'.format(mode))
1058 add_kconfig_checks(config_checklist, arch)
1059 add_cmdline_checks(config_checklist, arch)
1061 print('[+] Printing kernel security hardening preferences for {}...'.format(arch))
1062 print_checklist(mode, config_checklist, False)