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