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