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