Merge remote-tracking branch 'origin/pylint'
[kconfig-hardened-check.git] / kernel_hardening_checker / checks.py
1 #!/usr/bin/env python3
2
3 """
4 This tool is for checking the security hardening options of the Linux kernel.
5
6 Author: Alexander Popov <alex.popov@linux.com>
7
8 This module contains knowledge for checks.
9 """
10
11 # pylint: disable=missing-function-docstring,line-too-long
12 # pylint: disable=too-many-branches,too-many-statements,too-many-locals
13
14 from typing import List
15 from .engine import StrOrNone, ChecklistObjType, KconfigCheck, CmdlineCheck, SysctlCheck, VersionCheck, OR, AND
16
17
18 def add_kconfig_checks(l: List[ChecklistObjType], arch: str) -> None:
19     assert(arch), 'empty arch'
20
21     # Calling the KconfigCheck class constructor:
22     #     KconfigCheck(reason, decision, name, expected)
23     #
24     # [!] Don't add CmdlineChecks in add_kconfig_checks() to avoid wrong results
25     #     when the tool doesn't check the cmdline.
26
27     efi_not_set = KconfigCheck('-', '-', 'EFI', 'is not set')
28     cc_is_gcc = KconfigCheck('-', '-', 'CC_IS_GCC', 'y') # exists since v4.18
29     cc_is_clang = KconfigCheck('-', '-', 'CC_IS_CLANG', 'y') # exists since v4.18
30     if arch in ('X86_64', 'X86_32'):
31         cpu_sup_amd_not_set = KconfigCheck('-', '-', 'CPU_SUP_AMD', 'is not set')
32         cpu_sup_intel_not_set = KconfigCheck('-', '-', 'CPU_SUP_INTEL', 'is not set')
33
34     modules_not_set = KconfigCheck('cut_attack_surface', 'kspp', 'MODULES', 'is not set') # radical, but may be useful in some cases
35     devmem_not_set = KconfigCheck('cut_attack_surface', 'kspp', 'DEVMEM', 'is not set') # refers to LOCKDOWN
36     bpf_syscall_not_set = KconfigCheck('cut_attack_surface', 'lockdown', 'BPF_SYSCALL', 'is not set') # refers to LOCKDOWN
37
38     # 'self_protection', 'defconfig'
39     l += [KconfigCheck('self_protection', 'defconfig', 'BUG', 'y')]
40     l += [KconfigCheck('self_protection', 'defconfig', 'SLUB_DEBUG', 'y')]
41     l += [KconfigCheck('self_protection', 'defconfig', 'THREAD_INFO_IN_TASK', 'y')]
42     iommu_support_is_set = KconfigCheck('self_protection', 'defconfig', 'IOMMU_SUPPORT', 'y')
43     l += [iommu_support_is_set] # is needed for mitigating DMA attacks
44     l += [OR(KconfigCheck('self_protection', 'defconfig', 'STACKPROTECTOR', 'y'),
45              KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR', 'y'),
46              KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_REGULAR', 'y'),
47              KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_AUTO', 'y'),
48              KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_STRONG', 'y'))]
49     l += [OR(KconfigCheck('self_protection', 'defconfig', 'STACKPROTECTOR_STRONG', 'y'),
50              KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_STRONG', 'y'))]
51     l += [OR(KconfigCheck('self_protection', 'defconfig', 'STRICT_KERNEL_RWX', 'y'),
52              KconfigCheck('self_protection', 'defconfig', 'DEBUG_RODATA', 'y'))] # before v4.11
53     l += [OR(KconfigCheck('self_protection', 'defconfig', 'STRICT_MODULE_RWX', 'y'),
54              KconfigCheck('self_protection', 'defconfig', 'DEBUG_SET_MODULE_RONX', 'y'),
55              modules_not_set)] # DEBUG_SET_MODULE_RONX was before v4.11
56     l += [OR(KconfigCheck('self_protection', 'defconfig', 'REFCOUNT_FULL', 'y'),
57              VersionCheck((5, 4, 208)))]
58              # REFCOUNT_FULL is enabled by default since v5.5,
59              # and this is backported to v5.4.208
60     l += [OR(KconfigCheck('self_protection', 'defconfig', 'INIT_STACK_ALL_ZERO', 'y'),
61              KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_STRUCTLEAK_BYREF_ALL', 'y'))]
62     if arch in ('X86_64', 'ARM64', 'X86_32'):
63         l += [KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_BASE', 'y')]
64     vmap_stack_is_set = KconfigCheck('self_protection', 'defconfig', 'VMAP_STACK', 'y')
65     if arch in ('X86_64', 'ARM64', 'ARM'):
66         l += [vmap_stack_is_set]
67     if arch in ('X86_64', 'X86_32'):
68         l += [KconfigCheck('self_protection', 'defconfig', 'SPECULATION_MITIGATIONS', 'y')]
69         l += [KconfigCheck('self_protection', 'defconfig', 'DEBUG_WX', 'y')]
70         l += [KconfigCheck('self_protection', 'defconfig', 'WERROR', 'y')]
71         l += [KconfigCheck('self_protection', 'defconfig', 'X86_MCE', 'y')]
72         l += [KconfigCheck('self_protection', 'defconfig', 'RETPOLINE', 'y')]
73         l += [KconfigCheck('self_protection', 'defconfig', 'SYN_COOKIES', 'y')] # another reason?
74         microcode_is_set = KconfigCheck('self_protection', 'defconfig', 'MICROCODE', 'y')
75         l += [microcode_is_set] # is needed for mitigating CPU bugs
76         l += [OR(KconfigCheck('self_protection', 'defconfig', 'MICROCODE_INTEL', 'y'),
77                  cpu_sup_intel_not_set,
78                  AND(microcode_is_set,
79                      VersionCheck((6, 6, 0))))] # MICROCODE_INTEL was included in MICROCODE since v6.6
80         l += [OR(KconfigCheck('self_protection', 'defconfig', 'MICROCODE_AMD', 'y'),
81                  cpu_sup_amd_not_set,
82                  AND(microcode_is_set,
83                      VersionCheck((6, 6, 0))))] # MICROCODE_AMD was included in MICROCODE since v6.6
84         l += [OR(KconfigCheck('self_protection', 'defconfig', 'X86_SMAP', 'y'),
85                  VersionCheck((5, 19, 0)))] # X86_SMAP is enabled by default since v5.19
86         l += [OR(KconfigCheck('self_protection', 'defconfig', 'X86_UMIP', 'y'),
87                  KconfigCheck('self_protection', 'defconfig', 'X86_INTEL_UMIP', 'y'))]
88         l += [OR(KconfigCheck('self_protection', 'defconfig', 'X86_MCE_INTEL', 'y'),
89                  cpu_sup_intel_not_set)]
90         l += [OR(KconfigCheck('self_protection', 'defconfig', 'X86_MCE_AMD', 'y'),
91                  cpu_sup_amd_not_set)]
92     if arch in ('ARM64', 'ARM'):
93         l += [KconfigCheck('self_protection', 'defconfig', 'HW_RANDOM_TPM', 'y')]
94         l += [KconfigCheck('self_protection', 'defconfig', 'IOMMU_DEFAULT_DMA_STRICT', 'y')]
95         l += [KconfigCheck('self_protection', 'defconfig', 'IOMMU_DEFAULT_PASSTHROUGH', 'is not set')] # true if IOMMU_DEFAULT_DMA_STRICT is set
96         l += [KconfigCheck('self_protection', 'defconfig', 'STACKPROTECTOR_PER_TASK', 'y')]
97     if arch == 'X86_64':
98         l += [KconfigCheck('self_protection', 'defconfig', 'PAGE_TABLE_ISOLATION', 'y')]
99         l += [KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_MEMORY', 'y')]
100         l += [KconfigCheck('self_protection', 'defconfig', 'X86_KERNEL_IBT', 'y')]
101         l += [OR(KconfigCheck('self_protection', 'defconfig', 'CPU_SRSO', 'y'),
102                  cpu_sup_amd_not_set)]
103         l += [AND(KconfigCheck('self_protection', 'defconfig', 'INTEL_IOMMU', 'y'),
104                   iommu_support_is_set)]
105         l += [AND(KconfigCheck('self_protection', 'defconfig', 'AMD_IOMMU', 'y'),
106                   iommu_support_is_set)]
107     if arch == 'ARM64':
108         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_PAN', 'y')]
109         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_EPAN', 'y')]
110         l += [KconfigCheck('self_protection', 'defconfig', 'UNMAP_KERNEL_AT_EL0', 'y')]
111         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_E0PD', 'y')]
112         l += [KconfigCheck('self_protection', 'defconfig', 'RODATA_FULL_DEFAULT_ENABLED', 'y')]
113         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_PTR_AUTH_KERNEL', 'y')]
114         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_BTI_KERNEL', 'y')]
115         l += [KconfigCheck('self_protection', 'defconfig', 'MITIGATE_SPECTRE_BRANCH_HISTORY', 'y')]
116         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_MTE', 'y')]
117         l += [KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_MODULE_REGION_FULL', 'y')]
118         l += [OR(KconfigCheck('self_protection', 'defconfig', 'HARDEN_EL2_VECTORS', 'y'),
119                  AND(KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_BASE', 'y'),
120                      VersionCheck((5, 9, 0))))] # HARDEN_EL2_VECTORS was included in RANDOMIZE_BASE in v5.9
121         l += [OR(KconfigCheck('self_protection', 'defconfig', 'HARDEN_BRANCH_PREDICTOR', 'y'),
122                  VersionCheck((5, 10, 0)))] # HARDEN_BRANCH_PREDICTOR is enabled by default since v5.10
123     if arch == 'ARM':
124         l += [KconfigCheck('self_protection', 'defconfig', 'CPU_SW_DOMAIN_PAN', 'y')]
125         l += [KconfigCheck('self_protection', 'defconfig', 'HARDEN_BRANCH_PREDICTOR', 'y')]
126         l += [KconfigCheck('self_protection', 'defconfig', 'HARDEN_BRANCH_HISTORY', 'y')]
127         l += [KconfigCheck('self_protection', 'defconfig', 'DEBUG_ALIGN_RODATA', 'y')]
128
129     # 'self_protection', 'kspp'
130     l += [KconfigCheck('self_protection', 'kspp', 'BUG_ON_DATA_CORRUPTION', 'y')]
131     l += [KconfigCheck('self_protection', 'kspp', 'SLAB_FREELIST_HARDENED', 'y')]
132     l += [KconfigCheck('self_protection', 'kspp', 'SLAB_FREELIST_RANDOM', 'y')]
133     l += [KconfigCheck('self_protection', 'kspp', 'SHUFFLE_PAGE_ALLOCATOR', 'y')]
134     l += [KconfigCheck('self_protection', 'kspp', 'FORTIFY_SOURCE', 'y')]
135     l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_LIST', 'y')]
136     l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_VIRTUAL', 'y')]
137     l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_SG', 'y')]
138     l += [KconfigCheck('self_protection', 'kspp', 'INIT_ON_ALLOC_DEFAULT_ON', 'y')]
139     l += [KconfigCheck('self_protection', 'kspp', 'STATIC_USERMODEHELPER', 'y')] # needs userspace support
140     l += [KconfigCheck('self_protection', 'kspp', 'SCHED_CORE', 'y')]
141     l += [KconfigCheck('self_protection', 'kspp', 'SECURITY_LOCKDOWN_LSM', 'y')]
142     l += [KconfigCheck('self_protection', 'kspp', 'SECURITY_LOCKDOWN_LSM_EARLY', 'y')]
143     l += [KconfigCheck('self_protection', 'kspp', 'LOCK_DOWN_KERNEL_FORCE_CONFIDENTIALITY', 'y')]
144     cfi_clang_is_set = KconfigCheck('self_protection', 'kspp', 'CFI_CLANG', 'y')
145     cfi_clang_permissive_not_set = KconfigCheck('self_protection', 'kspp', 'CFI_PERMISSIVE', 'is not set')
146     l += [OR(KconfigCheck('self_protection', 'kspp', 'DEBUG_CREDENTIALS', 'y'),
147              VersionCheck((6, 6, 8)))] # DEBUG_CREDENTIALS was dropped in v6.6.8
148     l += [OR(KconfigCheck('self_protection', 'kspp', 'DEBUG_NOTIFIERS', 'y'),
149              AND(cfi_clang_is_set,
150                  cfi_clang_permissive_not_set,
151                  cc_is_clang))]
152     l += [OR(KconfigCheck('self_protection', 'kspp', 'SCHED_STACK_END_CHECK', 'y'),
153              vmap_stack_is_set)]
154     kfence_is_set = KconfigCheck('self_protection', 'kspp', 'KFENCE', 'y')
155     l += [kfence_is_set]
156     l += [AND(KconfigCheck('self_protection', 'a13xp0p0v', 'KFENCE_SAMPLE_INTERVAL', 'is not off'),
157               kfence_is_set)]
158     randstruct_is_set = OR(KconfigCheck('self_protection', 'kspp', 'RANDSTRUCT_FULL', 'y'),
159                            KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_RANDSTRUCT', 'y'))
160     l += [randstruct_is_set]
161     l += [AND(KconfigCheck('self_protection', 'kspp', 'RANDSTRUCT_PERFORMANCE', 'is not set'),
162               KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_RANDSTRUCT_PERFORMANCE', 'is not set'),
163               randstruct_is_set)]
164     hardened_usercopy_is_set = KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY', 'y')
165     l += [hardened_usercopy_is_set]
166     l += [AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY_FALLBACK', 'is not set'),
167               hardened_usercopy_is_set)] # usercopy whitelist violations should be prohibited
168     l += [AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY_PAGESPAN', 'is not set'),
169               hardened_usercopy_is_set)] # this debugging for HARDENED_USERCOPY is not needed for security
170     l += [AND(KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_LATENT_ENTROPY', 'y'),
171               cc_is_gcc)]
172     l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG', 'y'),
173              modules_not_set)]
174     l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG_ALL', 'y'),
175              modules_not_set)]
176     l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG_SHA512', 'y'),
177              KconfigCheck('self_protection', 'a13xp0p0v', 'MODULE_SIG_SHA3_512', 'y'),
178              modules_not_set)]
179     l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG_FORCE', 'y'),
180              modules_not_set)] # refers to LOCKDOWN
181     l += [OR(KconfigCheck('self_protection', 'kspp', 'INIT_ON_FREE_DEFAULT_ON', 'y'),
182              KconfigCheck('self_protection', 'kspp', 'PAGE_POISONING_ZERO', 'y'))]
183              # CONFIG_INIT_ON_FREE_DEFAULT_ON was added in v5.3.
184              # CONFIG_PAGE_POISONING_ZERO was removed in v5.11.
185              # Starting from v5.11 CONFIG_PAGE_POISONING unconditionally checks
186              # the 0xAA poison pattern on allocation.
187              # That brings higher performance penalty.
188     l += [OR(KconfigCheck('self_protection', 'kspp', 'EFI_DISABLE_PCI_DMA', 'y'),
189              efi_not_set)]
190     l += [OR(KconfigCheck('self_protection', 'kspp', 'RESET_ATTACK_MITIGATION', 'y'),
191              efi_not_set)] # needs userspace support (systemd)
192     ubsan_bounds_is_set = KconfigCheck('self_protection', 'kspp', 'UBSAN_BOUNDS', 'y')
193     l += [ubsan_bounds_is_set]
194     l += [OR(KconfigCheck('self_protection', 'kspp', 'UBSAN_LOCAL_BOUNDS', 'y'),
195              AND(ubsan_bounds_is_set,
196                  cc_is_gcc))]
197     l += [AND(KconfigCheck('self_protection', 'kspp', 'UBSAN_TRAP', 'y'),
198               ubsan_bounds_is_set,
199               KconfigCheck('self_protection', 'kspp', 'UBSAN_SHIFT', 'is not set'),
200               KconfigCheck('self_protection', 'kspp', 'UBSAN_DIV_ZERO', 'is not set'),
201               KconfigCheck('self_protection', 'kspp', 'UBSAN_UNREACHABLE', 'is not set'),
202               KconfigCheck('self_protection', 'kspp', 'UBSAN_BOOL', 'is not set'),
203               KconfigCheck('self_protection', 'kspp', 'UBSAN_ENUM', 'is not set'),
204               KconfigCheck('self_protection', 'kspp', 'UBSAN_ALIGNMENT', 'is not set'))] # only array index bounds checking with traps
205     l += [AND(KconfigCheck('self_protection', 'kspp', 'UBSAN_SANITIZE_ALL', 'y'),
206               ubsan_bounds_is_set)]
207     if arch in ('X86_64', 'ARM64', 'X86_32'):
208         stackleak_is_set = KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_STACKLEAK', 'y')
209         l += [AND(stackleak_is_set,
210                   cc_is_gcc)]
211         l += [AND(KconfigCheck('self_protection', 'kspp', 'STACKLEAK_METRICS', 'is not set'),
212                   stackleak_is_set,
213                   cc_is_gcc)]
214         l += [AND(KconfigCheck('self_protection', 'kspp', 'STACKLEAK_RUNTIME_DISABLE', 'is not set'),
215                   stackleak_is_set,
216                   cc_is_gcc)]
217         l += [KconfigCheck('self_protection', 'kspp', 'RANDOMIZE_KSTACK_OFFSET_DEFAULT', 'y')]
218     if arch in ('X86_64', 'ARM64'):
219         l += [AND(cfi_clang_is_set,
220                   cc_is_clang)]
221         l += [AND(cfi_clang_permissive_not_set,
222                   cfi_clang_is_set,
223                   cc_is_clang)]
224     if arch in ('X86_64', 'X86_32'):
225         l += [KconfigCheck('self_protection', 'kspp', 'HW_RANDOM_TPM', 'y')]
226         l += [KconfigCheck('self_protection', 'kspp', 'DEFAULT_MMAP_MIN_ADDR', '65536')]
227         l += [KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_DMA_STRICT', 'y')]
228         l += [KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_PASSTHROUGH', 'is not set')] # true if IOMMU_DEFAULT_DMA_STRICT is set
229         l += [AND(KconfigCheck('self_protection', 'kspp', 'INTEL_IOMMU_DEFAULT_ON', 'y'),
230                   iommu_support_is_set)]
231     if arch in ('ARM64', 'ARM'):
232         l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_WX', 'y')]
233         l += [KconfigCheck('self_protection', 'kspp', 'WERROR', 'y')]
234         l += [KconfigCheck('self_protection', 'kspp', 'DEFAULT_MMAP_MIN_ADDR', '32768')]
235         l += [KconfigCheck('self_protection', 'kspp', 'SYN_COOKIES', 'y')] # another reason?
236     if arch == 'X86_64':
237         l += [KconfigCheck('self_protection', 'kspp', 'SLS', 'y')] # vs CVE-2021-26341 in Straight-Line-Speculation
238         l += [AND(KconfigCheck('self_protection', 'kspp', 'INTEL_IOMMU_SVM', 'y'),
239                   iommu_support_is_set)]
240         l += [AND(KconfigCheck('self_protection', 'kspp', 'AMD_IOMMU_V2', 'y'),
241                   iommu_support_is_set)]
242     if arch == 'ARM64':
243         l += [KconfigCheck('self_protection', 'kspp', 'ARM64_SW_TTBR0_PAN', 'y')]
244         l += [KconfigCheck('self_protection', 'kspp', 'SHADOW_CALL_STACK', 'y')]
245         l += [KconfigCheck('self_protection', 'kspp', 'KASAN_HW_TAGS', 'y')] # see also: kasan=on, kasan.stacktrace=off, kasan.fault=panic
246     if arch == 'X86_32':
247         l += [KconfigCheck('self_protection', 'kspp', 'PAGE_TABLE_ISOLATION', 'y')]
248         l += [KconfigCheck('self_protection', 'kspp', 'HIGHMEM64G', 'y')]
249         l += [KconfigCheck('self_protection', 'kspp', 'X86_PAE', 'y')]
250         l += [AND(KconfigCheck('self_protection', 'kspp', 'INTEL_IOMMU', 'y'),
251                   iommu_support_is_set)]
252
253     # 'self_protection', 'clipos'
254     l += [KconfigCheck('self_protection', 'clipos', 'SLAB_MERGE_DEFAULT', 'is not set')]
255
256     # 'self_protection', 'a13xp0p0v'
257     l += [KconfigCheck('self_protection', 'a13xp0p0v', 'LIST_HARDENED', 'y')]
258     l += [KconfigCheck('self_protection', 'a13xp0p0v', 'RANDOM_KMALLOC_CACHES', 'y')]
259
260     # 'security_policy'
261     if arch in ('X86_64', 'ARM64', 'X86_32'):
262         l += [KconfigCheck('security_policy', 'defconfig', 'SECURITY', 'y')]
263     if arch == 'ARM':
264         l += [KconfigCheck('security_policy', 'kspp', 'SECURITY', 'y')]
265     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_YAMA', 'y')]
266     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_LANDLOCK', 'y')]
267     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_SELINUX_DISABLE', 'is not set')]
268     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_SELINUX_BOOTPARAM', 'is not set')]
269     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_SELINUX_DEVELOP', 'is not set')]
270     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_WRITABLE_HOOKS', 'is not set')] # refers to SECURITY_SELINUX_DISABLE
271     l += [KconfigCheck('security_policy', 'a13xp0p0v', 'SECURITY_SELINUX_DEBUG', 'is not set')]
272     l += [OR(KconfigCheck('security_policy', 'a13xp0p0v', 'SECURITY_SELINUX', 'y'),
273              KconfigCheck('security_policy', 'a13xp0p0v', 'SECURITY_APPARMOR', 'y'),
274              KconfigCheck('security_policy', 'a13xp0p0v', 'SECURITY_SMACK', 'y'),
275              KconfigCheck('security_policy', 'a13xp0p0v', 'SECURITY_TOMOYO', 'y'))] # one of major LSMs implementing MAC
276
277     # 'cut_attack_surface', 'defconfig'
278     l += [KconfigCheck('cut_attack_surface', 'defconfig', 'SECCOMP', 'y')]
279     l += [KconfigCheck('cut_attack_surface', 'defconfig', 'SECCOMP_FILTER', 'y')]
280     l += [OR(KconfigCheck('cut_attack_surface', 'defconfig', 'BPF_UNPRIV_DEFAULT_OFF', 'y'),
281              bpf_syscall_not_set)] # see unprivileged_bpf_disabled
282     if arch in ('X86_64', 'ARM64', 'X86_32'):
283         l += [OR(KconfigCheck('cut_attack_surface', 'defconfig', 'STRICT_DEVMEM', 'y'),
284                  devmem_not_set)] # refers to LOCKDOWN
285     if arch in ('X86_64', 'X86_32'):
286         l += [OR(KconfigCheck('cut_attack_surface', 'defconfig', 'X86_INTEL_TSX_MODE_OFF', 'y'),
287                  cpu_sup_intel_not_set)] # tsx=off
288
289     # 'cut_attack_surface', 'kspp'
290     l += [KconfigCheck('cut_attack_surface', 'kspp', 'SECURITY_DMESG_RESTRICT', 'y')]
291     l += [KconfigCheck('cut_attack_surface', 'kspp', 'ACPI_CUSTOM_METHOD', 'is not set')] # refers to LOCKDOWN
292     l += [KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT_BRK', 'is not set')]
293     l += [KconfigCheck('cut_attack_surface', 'kspp', 'DEVKMEM', 'is not set')] # refers to LOCKDOWN
294     l += [KconfigCheck('cut_attack_surface', 'kspp', 'BINFMT_MISC', 'is not set')]
295     l += [KconfigCheck('cut_attack_surface', 'kspp', 'INET_DIAG', 'is not set')]
296     l += [KconfigCheck('cut_attack_surface', 'kspp', 'KEXEC', 'is not set')] # refers to LOCKDOWN
297     l += [KconfigCheck('cut_attack_surface', 'kspp', 'PROC_KCORE', 'is not set')] # refers to LOCKDOWN
298     l += [KconfigCheck('cut_attack_surface', 'kspp', 'LEGACY_PTYS', 'is not set')]
299     l += [KconfigCheck('cut_attack_surface', 'kspp', 'HIBERNATION', 'is not set')] # refers to LOCKDOWN
300     l += [KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT', 'is not set')]
301     l += [KconfigCheck('cut_attack_surface', 'kspp', 'IA32_EMULATION', 'is not set')]
302     l += [KconfigCheck('cut_attack_surface', 'kspp', 'X86_X32', 'is not set')]
303     l += [KconfigCheck('cut_attack_surface', 'kspp', 'X86_X32_ABI', 'is not set')]
304     l += [KconfigCheck('cut_attack_surface', 'kspp', 'MODIFY_LDT_SYSCALL', 'is not set')]
305     l += [KconfigCheck('cut_attack_surface', 'kspp', 'OABI_COMPAT', 'is not set')]
306     l += [KconfigCheck('cut_attack_surface', 'kspp', 'X86_MSR', 'is not set')] # refers to LOCKDOWN
307     l += [KconfigCheck('cut_attack_surface', 'kspp', 'LEGACY_TIOCSTI', 'is not set')]
308     l += [modules_not_set]
309     l += [devmem_not_set]
310     l += [OR(KconfigCheck('cut_attack_surface', 'kspp', 'IO_STRICT_DEVMEM', 'y'),
311              devmem_not_set)] # refers to LOCKDOWN
312     l += [AND(KconfigCheck('cut_attack_surface', 'kspp', 'LDISC_AUTOLOAD', 'is not set'),
313               KconfigCheck('cut_attack_surface', 'kspp', 'LDISC_AUTOLOAD', 'is present'))]
314     if arch in ('X86_64', 'X86_32'):
315         l += [KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT_VDSO', 'is not set')]
316               # CONFIG_COMPAT_VDSO disabled ASLR of vDSO only on X86_64 and X86_32;
317               # on ARM64 this option has different meaning
318     if arch == 'X86_64':
319         l += [OR(KconfigCheck('cut_attack_surface', 'kspp', 'X86_VSYSCALL_EMULATION', 'is not set'),
320                  KconfigCheck('cut_attack_surface', 'kspp', 'LEGACY_VSYSCALL_NONE', 'y'))]
321                  # disabling X86_VSYSCALL_EMULATION turns vsyscall off completely,
322                  # and LEGACY_VSYSCALL_NONE can be changed at boot time via the cmdline parameter
323     if arch == 'ARM':
324         l += [OR(KconfigCheck('cut_attack_surface', 'kspp', 'STRICT_DEVMEM', 'y'),
325                  devmem_not_set)] # refers to LOCKDOWN
326
327     # 'cut_attack_surface', 'grsec'
328     l += [KconfigCheck('cut_attack_surface', 'grsec', 'ZSMALLOC_STAT', 'is not set')]
329     l += [KconfigCheck('cut_attack_surface', 'grsec', 'PAGE_OWNER', 'is not set')]
330     l += [KconfigCheck('cut_attack_surface', 'grsec', 'DEBUG_KMEMLEAK', 'is not set')]
331     l += [KconfigCheck('cut_attack_surface', 'grsec', 'BINFMT_AOUT', 'is not set')]
332     l += [KconfigCheck('cut_attack_surface', 'grsec', 'KPROBE_EVENTS', 'is not set')]
333     l += [KconfigCheck('cut_attack_surface', 'grsec', 'UPROBE_EVENTS', 'is not set')]
334     l += [KconfigCheck('cut_attack_surface', 'grsec', 'GENERIC_TRACER', 'is not set')] # refers to LOCKDOWN
335     l += [KconfigCheck('cut_attack_surface', 'grsec', 'FUNCTION_TRACER', 'is not set')]
336     l += [KconfigCheck('cut_attack_surface', 'grsec', 'STACK_TRACER', 'is not set')]
337     l += [KconfigCheck('cut_attack_surface', 'grsec', 'HIST_TRIGGERS', 'is not set')]
338     l += [KconfigCheck('cut_attack_surface', 'grsec', 'BLK_DEV_IO_TRACE', 'is not set')]
339     l += [KconfigCheck('cut_attack_surface', 'grsec', 'PROC_VMCORE', 'is not set')]
340     l += [KconfigCheck('cut_attack_surface', 'grsec', 'PROC_PAGE_MONITOR', 'is not set')]
341     l += [KconfigCheck('cut_attack_surface', 'grsec', 'USELIB', 'is not set')]
342     l += [KconfigCheck('cut_attack_surface', 'grsec', 'CHECKPOINT_RESTORE', 'is not set')]
343     l += [KconfigCheck('cut_attack_surface', 'grsec', 'USERFAULTFD', 'is not set')]
344     l += [KconfigCheck('cut_attack_surface', 'grsec', 'HWPOISON_INJECT', 'is not set')]
345     l += [KconfigCheck('cut_attack_surface', 'grsec', 'MEM_SOFT_DIRTY', 'is not set')]
346     l += [KconfigCheck('cut_attack_surface', 'grsec', 'DEVPORT', 'is not set')] # refers to LOCKDOWN
347     l += [KconfigCheck('cut_attack_surface', 'grsec', 'DEBUG_FS', 'is not set')] # refers to LOCKDOWN
348     l += [KconfigCheck('cut_attack_surface', 'grsec', 'NOTIFIER_ERROR_INJECTION', 'is not set')]
349     l += [KconfigCheck('cut_attack_surface', 'grsec', 'FAIL_FUTEX', 'is not set')]
350     l += [KconfigCheck('cut_attack_surface', 'grsec', 'PUNIT_ATOM_DEBUG', 'is not set')]
351     l += [KconfigCheck('cut_attack_surface', 'grsec', 'ACPI_CONFIGFS', 'is not set')]
352     l += [KconfigCheck('cut_attack_surface', 'grsec', 'EDAC_DEBUG', 'is not set')]
353     l += [KconfigCheck('cut_attack_surface', 'grsec', 'DRM_I915_DEBUG', 'is not set')]
354     l += [KconfigCheck('cut_attack_surface', 'grsec', 'BCACHE_CLOSURES_DEBUG', 'is not set')]
355     l += [KconfigCheck('cut_attack_surface', 'grsec', 'DVB_C8SECTPFE', 'is not set')]
356     l += [KconfigCheck('cut_attack_surface', 'grsec', 'MTD_SLRAM', 'is not set')]
357     l += [KconfigCheck('cut_attack_surface', 'grsec', 'MTD_PHRAM', 'is not set')]
358     l += [KconfigCheck('cut_attack_surface', 'grsec', 'IO_URING', 'is not set')]
359     l += [KconfigCheck('cut_attack_surface', 'grsec', 'KCMP', 'is not set')]
360     l += [KconfigCheck('cut_attack_surface', 'grsec', 'RSEQ', 'is not set')]
361     l += [KconfigCheck('cut_attack_surface', 'grsec', 'LATENCYTOP', 'is not set')]
362     l += [KconfigCheck('cut_attack_surface', 'grsec', 'KCOV', 'is not set')]
363     l += [KconfigCheck('cut_attack_surface', 'grsec', 'PROVIDE_OHCI1394_DMA_INIT', 'is not set')]
364     l += [KconfigCheck('cut_attack_surface', 'grsec', 'SUNRPC_DEBUG', 'is not set')]
365     l += [AND(KconfigCheck('cut_attack_surface', 'grsec', 'PTDUMP_DEBUGFS', 'is not set'),
366               KconfigCheck('cut_attack_surface', 'grsec', 'X86_PTDUMP', 'is not set'))]
367
368     # 'cut_attack_surface', 'maintainer'
369     l += [KconfigCheck('cut_attack_surface', 'maintainer', 'DRM_LEGACY', 'is not set')] # recommended by Daniel Vetter in /issues/38
370     l += [KconfigCheck('cut_attack_surface', 'maintainer', 'FB', 'is not set')] # recommended by Daniel Vetter in /issues/38
371     l += [KconfigCheck('cut_attack_surface', 'maintainer', 'VT', 'is not set')] # recommended by Daniel Vetter in /issues/38
372     l += [KconfigCheck('cut_attack_surface', 'maintainer', 'BLK_DEV_FD', 'is not set')] # recommended by Denis Efremov in /pull/54
373     l += [KconfigCheck('cut_attack_surface', 'maintainer', 'BLK_DEV_FD_RAWCMD', 'is not set')] # recommended by Denis Efremov in /pull/62
374     l += [KconfigCheck('cut_attack_surface', 'maintainer', 'NOUVEAU_LEGACY_CTX_SUPPORT', 'is not set')]
375                                             # recommended by Dave Airlie in kernel commit b30a43ac7132cdda
376
377     # 'cut_attack_surface', 'clipos'
378     l += [KconfigCheck('cut_attack_surface', 'clipos', 'STAGING', 'is not set')]
379     l += [KconfigCheck('cut_attack_surface', 'clipos', 'KSM', 'is not set')] # to prevent FLUSH+RELOAD attack
380     l += [KconfigCheck('cut_attack_surface', 'clipos', 'KALLSYMS', 'is not set')]
381     l += [KconfigCheck('cut_attack_surface', 'clipos', 'MAGIC_SYSRQ', 'is not set')]
382     l += [KconfigCheck('cut_attack_surface', 'clipos', 'KEXEC_FILE', 'is not set')] # refers to LOCKDOWN (permissive)
383     l += [KconfigCheck('cut_attack_surface', 'clipos', 'USER_NS', 'is not set')] # user.max_user_namespaces=0
384     l += [KconfigCheck('cut_attack_surface', 'clipos', 'X86_CPUID', 'is not set')]
385     l += [KconfigCheck('cut_attack_surface', 'clipos', 'X86_IOPL_IOPERM', 'is not set')] # refers to LOCKDOWN
386     l += [KconfigCheck('cut_attack_surface', 'clipos', 'ACPI_TABLE_UPGRADE', 'is not set')] # refers to LOCKDOWN
387     l += [KconfigCheck('cut_attack_surface', 'clipos', 'EFI_CUSTOM_SSDT_OVERLAYS', 'is not set')]
388     l += [KconfigCheck('cut_attack_surface', 'clipos', 'AIO', 'is not set')]
389 #   l += [KconfigCheck('cut_attack_surface', 'clipos', 'IKCONFIG', 'is not set')] # no, IKCONFIG is needed for this check :)
390
391     # 'cut_attack_surface', 'lockdown'
392     l += [KconfigCheck('cut_attack_surface', 'lockdown', 'EFI_TEST', 'is not set')] # refers to LOCKDOWN
393     l += [KconfigCheck('cut_attack_surface', 'lockdown', 'MMIOTRACE_TEST', 'is not set')] # refers to LOCKDOWN
394     l += [KconfigCheck('cut_attack_surface', 'lockdown', 'KPROBES', 'is not set')] # refers to LOCKDOWN
395     l += [bpf_syscall_not_set] # refers to LOCKDOWN
396
397     # 'cut_attack_surface', 'a13xp0p0v'
398     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'MMIOTRACE', 'is not set')] # refers to LOCKDOWN (permissive)
399     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'LIVEPATCH', 'is not set')]
400     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'IP_DCCP', 'is not set')]
401     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'IP_SCTP', 'is not set')]
402     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'FTRACE', 'is not set')] # refers to LOCKDOWN
403     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'VIDEO_VIVID', 'is not set')]
404     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'INPUT_EVBUG', 'is not set')] # Can be used as a keylogger
405     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'KGDB', 'is not set')]
406     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'CORESIGHT', 'is not set')]
407     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'XFS_SUPPORT_V4', 'is not set')]
408     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'MODULE_FORCE_LOAD', 'is not set')]
409     l += [KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'BLK_DEV_WRITE_MOUNTED', 'is not set')]
410     l += [OR(KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'TRIM_UNUSED_KSYMS', 'y'),
411              modules_not_set)]
412
413
414     # 'harden_userspace'
415     if arch == 'ARM64':
416         l += [KconfigCheck('harden_userspace', 'defconfig', 'ARM64_PTR_AUTH', 'y')]
417         l += [KconfigCheck('harden_userspace', 'defconfig', 'ARM64_BTI', 'y')]
418     if arch in ('ARM', 'X86_32'):
419         l += [KconfigCheck('harden_userspace', 'defconfig', 'VMSPLIT_3G', 'y')]
420     l += [KconfigCheck('harden_userspace', 'clipos', 'COREDUMP', 'is not set')]
421     l += [KconfigCheck('harden_userspace', 'a13xp0p0v', 'ARCH_MMAP_RND_BITS', 'MAX')] # 'MAX' value is refined using ARCH_MMAP_RND_BITS_MAX
422     if arch == 'X86_64':
423         l += [KconfigCheck('harden_userspace', 'a13xp0p0v', 'X86_USER_SHADOW_STACK', 'y')]
424
425
426 def add_cmdline_checks(l: List[ChecklistObjType], arch: str) -> None:
427     assert(arch), 'empty arch'
428
429     # Calling the CmdlineCheck class constructor:
430     #     CmdlineCheck(reason, decision, name, expected)
431     #
432     # [!] Don't add CmdlineChecks in add_kconfig_checks() to avoid wrong results
433     #     when the tool doesn't check the cmdline.
434     #
435     # [!] Make sure that values of the options in CmdlineChecks need normalization.
436     #     For more info see normalize_cmdline_options().
437     #
438     # A common pattern for checking the 'param_x' cmdline parameter
439     # that __overrides__ the 'PARAM_X_DEFAULT' kconfig option:
440     #   l += [OR(CmdlineCheck(reason, decision, 'param_x', '1'),
441     #            AND(KconfigCheck(reason, decision, 'PARAM_X_DEFAULT_ON', 'y'),
442     #                CmdlineCheck(reason, decision, 'param_x, 'is not set')))]
443     #
444     # Here we don't check the kconfig options or minimal kernel version
445     # required for the cmdline parameters. That would make the checks
446     # very complex and not give a 100% guarantee anyway.
447
448     # 'self_protection', 'defconfig'
449     l += [CmdlineCheck('self_protection', 'defconfig', 'nosmep', 'is not set')]
450     l += [CmdlineCheck('self_protection', 'defconfig', 'nosmap', 'is not set')]
451     l += [CmdlineCheck('self_protection', 'defconfig', 'nokaslr', 'is not set')]
452     l += [CmdlineCheck('self_protection', 'defconfig', 'nopti', 'is not set')]
453     l += [CmdlineCheck('self_protection', 'defconfig', 'nospectre_v1', 'is not set')]
454     l += [CmdlineCheck('self_protection', 'defconfig', 'nospectre_v2', 'is not set')]
455     l += [CmdlineCheck('self_protection', 'defconfig', 'nospectre_bhb', 'is not set')]
456     l += [CmdlineCheck('self_protection', 'defconfig', 'nospec_store_bypass_disable', 'is not set')]
457     l += [CmdlineCheck('self_protection', 'defconfig', 'dis_ucode_ldr', 'is not set')]
458     l += [CmdlineCheck('self_protection', 'defconfig', 'arm64.nobti', 'is not set')]
459     l += [CmdlineCheck('self_protection', 'defconfig', 'arm64.nopauth', 'is not set')]
460     l += [CmdlineCheck('self_protection', 'defconfig', 'arm64.nomte', 'is not set')]
461     if arch in ('X86_64', 'X86_32'):
462         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'spectre_v2', 'is not off'),
463                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
464                      CmdlineCheck('self_protection', 'defconfig', 'spectre_v2', 'is not set')))]
465         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'spectre_v2_user', 'is not off'),
466                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
467                      CmdlineCheck('self_protection', 'defconfig', 'spectre_v2_user', 'is not set')))]
468         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'spec_store_bypass_disable', 'is not off'),
469                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
470                      CmdlineCheck('self_protection', 'defconfig', 'spec_store_bypass_disable', 'is not set')))]
471         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'l1tf', 'is not off'),
472                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
473                      CmdlineCheck('self_protection', 'defconfig', 'l1tf', 'is not set')))]
474         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'mds', 'is not off'),
475                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
476                      CmdlineCheck('self_protection', 'defconfig', 'mds', 'is not set')))]
477         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'tsx_async_abort', 'is not off'),
478                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
479                      CmdlineCheck('self_protection', 'defconfig', 'tsx_async_abort', 'is not set')))]
480         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'srbds', 'is not off'),
481                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
482                      CmdlineCheck('self_protection', 'defconfig', 'srbds', 'is not set')))]
483         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'mmio_stale_data', 'is not off'),
484                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
485                      CmdlineCheck('self_protection', 'defconfig', 'mmio_stale_data', 'is not set')))]
486         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'retbleed', 'is not off'),
487                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
488                      CmdlineCheck('self_protection', 'defconfig', 'retbleed', 'is not set')))]
489         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'spec_rstack_overflow', 'is not off'),
490                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
491                      CmdlineCheck('self_protection', 'defconfig', 'spec_rstack_overflow', 'is not set')))]
492         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'gather_data_sampling', 'is not off'),
493                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
494                      CmdlineCheck('self_protection', 'defconfig', 'gather_data_sampling', 'is not set')))]
495     if arch == 'ARM64':
496         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'kpti', 'is not off'),
497                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
498                      CmdlineCheck('self_protection', 'defconfig', 'kpti', 'is not set')))]
499         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'ssbd', 'kernel'),
500                  CmdlineCheck('self_protection', 'a13xp0p0v', 'ssbd', 'force-on'),
501                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
502                      CmdlineCheck('self_protection', 'defconfig', 'ssbd', 'is not set')))]
503         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'rodata', 'full'),
504                  AND(KconfigCheck('self_protection', 'defconfig', 'RODATA_FULL_DEFAULT_ENABLED', 'y'),
505                      CmdlineCheck('self_protection', 'defconfig', 'rodata', 'is not set')))]
506     else:
507         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'rodata', 'on'),
508                  CmdlineCheck('self_protection', 'defconfig', 'rodata', 'is not set'))]
509
510     # 'self_protection', 'kspp'
511     l += [CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt')]
512     l += [CmdlineCheck('self_protection', 'kspp', 'slab_merge', 'is not set')] # consequence of 'slab_nomerge' by kspp
513     l += [CmdlineCheck('self_protection', 'kspp', 'slub_merge', 'is not set')] # consequence of 'slab_nomerge' by kspp
514     l += [CmdlineCheck('self_protection', 'kspp', 'page_alloc.shuffle', '1')]
515     l += [OR(CmdlineCheck('self_protection', 'kspp', 'slab_nomerge', 'is present'),
516              AND(KconfigCheck('self_protection', 'clipos', 'SLAB_MERGE_DEFAULT', 'is not set'),
517                  CmdlineCheck('self_protection', 'kspp', 'slab_merge', 'is not set'),
518                  CmdlineCheck('self_protection', 'kspp', 'slub_merge', 'is not set')))]
519     l += [OR(CmdlineCheck('self_protection', 'kspp', 'init_on_alloc', '1'),
520              AND(KconfigCheck('self_protection', 'kspp', 'INIT_ON_ALLOC_DEFAULT_ON', 'y'),
521                  CmdlineCheck('self_protection', 'kspp', 'init_on_alloc', 'is not set')))]
522     l += [OR(CmdlineCheck('self_protection', 'kspp', 'init_on_free', '1'),
523              AND(KconfigCheck('self_protection', 'kspp', 'INIT_ON_FREE_DEFAULT_ON', 'y'),
524                  CmdlineCheck('self_protection', 'kspp', 'init_on_free', 'is not set')),
525              AND(CmdlineCheck('self_protection', 'kspp', 'page_poison', '1'),
526                  KconfigCheck('self_protection', 'kspp', 'PAGE_POISONING_ZERO', 'y'),
527                  CmdlineCheck('self_protection', 'kspp', 'slub_debug', 'P')))]
528     l += [OR(CmdlineCheck('self_protection', 'kspp', 'hardened_usercopy', '1'),
529              AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY', 'y'),
530                  CmdlineCheck('self_protection', 'kspp', 'hardened_usercopy', 'is not set')))]
531     l += [AND(CmdlineCheck('self_protection', 'kspp', 'slab_common.usercopy_fallback', 'is not set'),
532               KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY_FALLBACK', 'is not set'))]
533               # Consequence of the HARDENED_USERCOPY_FALLBACK check by kspp.
534               # Don't require slab_common.usercopy_fallback=0,
535               # since HARDENED_USERCOPY_FALLBACK was removed in Linux v5.16.
536     if arch in ('X86_64', 'ARM64', 'X86_32'):
537         l += [OR(CmdlineCheck('self_protection', 'kspp', 'iommu.strict', '1'),
538                  AND(KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_DMA_STRICT', 'y'),
539                      CmdlineCheck('self_protection', 'kspp', 'iommu.strict', 'is not set')))]
540         l += [OR(CmdlineCheck('self_protection', 'kspp', 'iommu.passthrough', '0'),
541                  AND(KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_PASSTHROUGH', 'is not set'),
542                      CmdlineCheck('self_protection', 'kspp', 'iommu.passthrough', 'is not set')))]
543         l += [OR(CmdlineCheck('self_protection', 'kspp', 'randomize_kstack_offset', '1'),
544                  AND(KconfigCheck('self_protection', 'kspp', 'RANDOMIZE_KSTACK_OFFSET_DEFAULT', 'y'),
545                      CmdlineCheck('self_protection', 'kspp', 'randomize_kstack_offset', 'is not set')))]
546     if arch in ('X86_64', 'X86_32'):
547         l += [AND(CmdlineCheck('self_protection', 'kspp', 'pti', 'on'),
548                   CmdlineCheck('self_protection', 'defconfig', 'nopti', 'is not set'))]
549
550     # 'self_protection', 'clipos'
551     if arch in ('X86_64', 'X86_32'):
552         l += [CmdlineCheck('self_protection', 'clipos', 'iommu', 'force')]
553
554     # 'self_protection', 'a13xp0p0v'
555     l += [OR(CmdlineCheck('self_protection', 'a13xp0p0v', 'kfence.sample_interval', 'is not off'),
556              AND(KconfigCheck('self_protection', 'a13xp0p0v', 'KFENCE_SAMPLE_INTERVAL', 'is not off'),
557                  CmdlineCheck('self_protection', 'a13xp0p0v', 'kfence.sample_interval', 'is not set')))]
558
559     # 'cut_attack_surface', 'defconfig'
560     if arch in ('X86_64', 'X86_32'):
561         tsx_not_set = CmdlineCheck('cut_attack_surface', 'defconfig', 'tsx', 'is not set')
562         l += [OR(CmdlineCheck('cut_attack_surface', 'defconfig', 'tsx', 'off'),
563                  AND(KconfigCheck('cut_attack_surface', 'defconfig', 'X86_INTEL_TSX_MODE_OFF', 'y'),
564                      tsx_not_set),
565                  AND(KconfigCheck('-', '-', 'CPU_SUP_INTEL', 'is not set'),
566                      tsx_not_set))]
567
568     # 'cut_attack_surface', 'kspp'
569     l += [CmdlineCheck('cut_attack_surface', 'kspp', 'nosmt', 'is present')] # slow (high performance penalty)
570     if arch == 'X86_64':
571         l += [OR(CmdlineCheck('cut_attack_surface', 'kspp', 'vsyscall', 'none'),
572                  KconfigCheck('cut_attack_surface', 'kspp', 'X86_VSYSCALL_EMULATION', 'is not set'),
573                  AND(KconfigCheck('cut_attack_surface', 'kspp', 'LEGACY_VSYSCALL_NONE', 'y'),
574                      CmdlineCheck('cut_attack_surface', 'kspp', 'vsyscall', 'is not set')))]
575         l += [OR(CmdlineCheck('cut_attack_surface', 'kspp', 'vdso32', '0'),
576                  CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'vdso32', '1'),
577                  AND(KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT_VDSO', 'is not set'),
578                      CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'vdso32', 'is not set')))] # the vdso32 parameter must not be 2
579     if arch == 'X86_32':
580         l += [OR(CmdlineCheck('cut_attack_surface', 'kspp', 'vdso32', '0'),
581                  CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'vdso', '0'),
582                  CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'vdso32', '1'),
583                  CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'vdso', '1'),
584                  AND(KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT_VDSO', 'is not set'),
585                      CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'vdso32', 'is not set'),
586                      CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'vdso', 'is not set')))] # the vdso and vdso32 parameters must not be 2
587
588     # 'cut_attack_surface', 'grsec'
589     # The cmdline checks compatible with the kconfig options disabled by grsecurity...
590     l += [OR(CmdlineCheck('cut_attack_surface', 'grsec', 'debugfs', 'off'),
591              KconfigCheck('cut_attack_surface', 'grsec', 'DEBUG_FS', 'is not set'))] # ... the end
592
593     # 'cut_attack_surface', 'a13xp0p0v'
594     l += [CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'sysrq_always_enabled', 'is not set')]
595     l += [OR(CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'bdev_allow_write_mounted', '0'),
596              AND(KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'BLK_DEV_WRITE_MOUNTED', 'is not set'),
597                  CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'bdev_allow_write_mounted', 'is not set')))]
598     if arch == 'X86_64':
599         l += [OR(CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'ia32_emulation', '0'),
600                  KconfigCheck('cut_attack_surface', 'kspp', 'IA32_EMULATION', 'is not set'),
601                  AND(KconfigCheck('cut_attack_surface', 'a13xp0p0v', 'IA32_EMULATION_DEFAULT_DISABLED', 'y'),
602                      CmdlineCheck('cut_attack_surface', 'a13xp0p0v', 'ia32_emulation', 'is not set')))]
603
604     # 'harden_userspace'
605     l += [CmdlineCheck('harden_userspace', 'defconfig', 'norandmaps', 'is not set')]
606
607
608 no_kstrtobool_options = [
609     'debugfs', # See debugfs_kernel() in fs/debugfs/inode.c
610     'mitigations', # See mitigations_parse_cmdline() in kernel/cpu.c
611     'pti', # See pti_check_boottime_disable() in arch/x86/mm/pti.c
612     'spectre_v2', # See spectre_v2_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
613     'spectre_v2_user', # See spectre_v2_parse_user_cmdline() in arch/x86/kernel/cpu/bugs.c
614     'spec_store_bypass_disable', # See ssb_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
615     'l1tf', # See l1tf_cmdline() in arch/x86/kernel/cpu/bugs.c
616     'mds', # See mds_cmdline() in arch/x86/kernel/cpu/bugs.c
617     'tsx_async_abort', # See tsx_async_abort_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
618     'srbds', # See srbds_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
619     'mmio_stale_data', # See mmio_stale_data_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
620     'retbleed', # See retbleed_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
621     'rodata', # See set_debug_rodata() in init/main.c
622     'ssbd', # See parse_spectre_v4_param() in arch/arm64/kernel/proton-pack.c
623     'spec_rstack_overflow', # See srso_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
624     'gather_data_sampling', # See gds_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
625     'slub_debug', # See setup_slub_debug() in mm/slub.c
626     'iommu', # See iommu_setup() in arch/x86/kernel/pci-dma.c
627     'vsyscall', # See vsyscall_setup() in arch/x86/entry/vsyscall/vsyscall_64.c
628     'vdso32', # See vdso32_setup() in arch/x86/entry/vdso/vdso32-setup.c
629     'vdso', # See vdso32_setup() in arch/x86/entry/vdso/vdso32-setup.c
630     'tsx' # See tsx_init() in arch/x86/kernel/cpu/tsx.c
631 ]
632
633
634 def normalize_cmdline_options(option: str, value: str) -> str:
635     # Don't normalize the cmdline option values if
636     # the Linux kernel doesn't use kstrtobool() for them
637     if option in no_kstrtobool_options:
638         return value
639
640     # Implement a limited part of the kstrtobool() logic
641     if value.lower() in ('1', 'on', 'y', 'yes', 't', 'true'):
642         return '1'
643     if value.lower() in ('0', 'off', 'n', 'no', 'f', 'false'):
644         return '0'
645
646     # Preserve unique values
647     return value
648
649
650 # Ideas of security hardening sysctls:
651 #    what about bpf_jit_enable?
652 #    vm.mmap_min_addr has a good value
653 #    nosmt sysfs control file
654 #    vm.mmap_rnd_bits=max (?)
655 #    kernel.sysrq=0
656 #    abi.vsyscall32 (any value except 2)
657 #    kernel.oops_limit (think about a proper value)
658 #    kernel.warn_limit (think about a proper value)
659 #    net.ipv4.tcp_syncookies=1 (?)
660
661 def add_sysctl_checks(l: List[ChecklistObjType], _arch: StrOrNone) -> None:
662 # This function may be called with arch=None
663
664 # Calling the SysctlCheck class constructor:
665 #   SysctlCheck(reason, decision, name, expected)
666
667     # Use an omnipresent kconfig symbol to see if we have a kconfig file for checking
668     have_kconfig = KconfigCheck('-', '-', 'LOCALVERSION', 'is present')
669
670     l += [OR(SysctlCheck('self_protection', 'kspp', 'net.core.bpf_jit_harden', '2'),
671              AND(KconfigCheck('-', '-', 'BPF_JIT', 'is not set'),
672                  have_kconfig))]
673
674     l += [SysctlCheck('cut_attack_surface', 'kspp', 'kernel.dmesg_restrict', '1')]
675     l += [SysctlCheck('cut_attack_surface', 'kspp', 'kernel.perf_event_paranoid', '3')] # with a custom patch, see https://lwn.net/Articles/696216/
676     l += [SysctlCheck('cut_attack_surface', 'kspp', 'user.max_user_namespaces', '0')] # may break the upower daemon in Ubuntu
677     l += [SysctlCheck('cut_attack_surface', 'kspp', 'dev.tty.ldisc_autoload', '0')]
678     l += [SysctlCheck('cut_attack_surface', 'kspp', 'kernel.kptr_restrict', '2')]
679     l += [SysctlCheck('cut_attack_surface', 'kspp', 'dev.tty.legacy_tiocsti', '0')]
680     l += [OR(SysctlCheck('cut_attack_surface', 'kspp', 'kernel.kexec_load_disabled', '1'),
681              AND(KconfigCheck('-', '-', 'KEXEC_CORE', 'is not set'),
682                  have_kconfig))]
683     l += [OR(SysctlCheck('cut_attack_surface', 'kspp', 'kernel.unprivileged_bpf_disabled', '1'),
684              AND(KconfigCheck('cut_attack_surface', 'lockdown', 'BPF_SYSCALL', 'is not set'),
685                  have_kconfig))]
686     l += [OR(SysctlCheck('cut_attack_surface', 'kspp', 'vm.unprivileged_userfaultfd', '0'),
687              AND(KconfigCheck('cut_attack_surface', 'grsec', 'USERFAULTFD', 'is not set'),
688                  have_kconfig))]
689           # At first, it disabled unprivileged userfaultfd,
690           # and since v5.11 it enables unprivileged userfaultfd for user-mode only.
691
692     l += [OR(SysctlCheck('cut_attack_surface', 'clipos', 'kernel.modules_disabled', '1'),
693              AND(KconfigCheck('cut_attack_surface', 'kspp', 'MODULES', 'is not set'),
694                  have_kconfig))] # radical, but may be useful in some cases
695
696     l += [SysctlCheck('harden_userspace', 'kspp', 'fs.protected_symlinks', '1')]
697     l += [SysctlCheck('harden_userspace', 'kspp', 'fs.protected_hardlinks', '1')]
698     l += [SysctlCheck('harden_userspace', 'kspp', 'fs.protected_fifos', '2')]
699     l += [SysctlCheck('harden_userspace', 'kspp', 'fs.protected_regular', '2')]
700     l += [SysctlCheck('harden_userspace', 'kspp', 'fs.suid_dumpable', '0')]
701     l += [SysctlCheck('harden_userspace', 'kspp', 'kernel.randomize_va_space', '2')]
702     l += [SysctlCheck('harden_userspace', 'kspp', 'kernel.yama.ptrace_scope', '3')]