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