cda3df86a50a1bd8ccae98d34c95b95b1d12d410
[kconfig-hardened-check.git] / kconfig_hardened_check / checks.py
1 #!/usr/bin/python3
2
3 """
4 This tool helps me to check Linux kernel options against
5 my security hardening preferences for X86_64, ARM64, X86_32, and ARM.
6 Let the computers do their job!
7
8 Author: Alexander Popov <alex.popov@linux.com>
9
10 This module contains knowledge for checks.
11 """
12
13 # N.B. Hardening sysctls:
14 #    kernel.kptr_restrict=2 (or 1?)
15 #    kernel.dmesg_restrict=1 (also see the kconfig option)
16 #    kernel.perf_event_paranoid=2 (or 3 with a custom patch, see https://lwn.net/Articles/696216/)
17 #    kernel.kexec_load_disabled=1
18 #    kernel.yama.ptrace_scope=3
19 #    user.max_user_namespaces=0
20 #    what about bpf_jit_enable?
21 #    kernel.unprivileged_bpf_disabled=1
22 #    net.core.bpf_jit_harden=2
23 #    vm.unprivileged_userfaultfd=0
24 #        (at first, it disabled unprivileged userfaultfd,
25 #         and since v5.11 it enables unprivileged userfaultfd for user-mode only)
26 #    vm.mmap_min_addr has a good value
27 #    dev.tty.ldisc_autoload=0
28 #    fs.protected_symlinks=1
29 #    fs.protected_hardlinks=1
30 #    fs.protected_fifos=2
31 #    fs.protected_regular=2
32 #    fs.suid_dumpable=0
33 #    kernel.modules_disabled=1
34 #    kernel.randomize_va_space=2
35 #    nosmt sysfs control file
36 #    dev.tty.legacy_tiocsti=0
37 #    vm.mmap_rnd_bits=max (?)
38 #    kernel.sysrq=0
39 #    abi.vsyscall32 (any value except 2)
40 #
41 # Think of these boot params:
42 #    module.sig_enforce=1
43 #    lockdown=confidentiality
44 #    mce=0
45 #    nosmt=force
46 #    intel_iommu=on
47 #    amd_iommu=on
48 #    efi=disable_early_pci_dma
49
50 # pylint: disable=missing-function-docstring,line-too-long,invalid-name
51 # pylint: disable=too-many-branches,too-many-statements
52
53 from .engine import KconfigCheck, CmdlineCheck, VersionCheck, OR, AND
54
55
56 def add_kconfig_checks(l, arch):
57     # Calling the KconfigCheck class constructor:
58     #     KconfigCheck(reason, decision, name, expected)
59     #
60     # [!] Don't add CmdlineChecks in add_kconfig_checks() to avoid wrong results
61     #     when the tool doesn't check the cmdline.
62
63     efi_not_set = KconfigCheck('-', '-', 'EFI', 'is not set')
64     cc_is_gcc = KconfigCheck('-', '-', 'CC_IS_GCC', 'y') # exists since v4.18
65     cc_is_clang = KconfigCheck('-', '-', 'CC_IS_CLANG', 'y') # exists since v4.18
66
67     modules_not_set = KconfigCheck('cut_attack_surface', 'kspp', 'MODULES', 'is not set')
68     devmem_not_set = KconfigCheck('cut_attack_surface', 'kspp', 'DEVMEM', 'is not set') # refers to LOCKDOWN
69     bpf_syscall_not_set = KconfigCheck('cut_attack_surface', 'lockdown', 'BPF_SYSCALL', 'is not set') # refers to LOCKDOWN
70
71     # 'self_protection', 'defconfig'
72     l += [KconfigCheck('self_protection', 'defconfig', 'BUG', 'y')]
73     l += [KconfigCheck('self_protection', 'defconfig', 'SLUB_DEBUG', 'y')]
74     l += [KconfigCheck('self_protection', 'defconfig', 'THREAD_INFO_IN_TASK', 'y')]
75     gcc_plugins_support_is_set = KconfigCheck('self_protection', 'defconfig', 'GCC_PLUGINS', 'y')
76     l += [gcc_plugins_support_is_set]
77     iommu_support_is_set = KconfigCheck('self_protection', 'defconfig', 'IOMMU_SUPPORT', 'y')
78     l += [iommu_support_is_set] # is needed for mitigating DMA attacks
79     l += [OR(KconfigCheck('self_protection', 'defconfig', 'STACKPROTECTOR', 'y'),
80              KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR', 'y'),
81              KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_REGULAR', 'y'),
82              KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_AUTO', 'y'),
83              KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_STRONG', 'y'))]
84     l += [OR(KconfigCheck('self_protection', 'defconfig', 'STACKPROTECTOR_STRONG', 'y'),
85              KconfigCheck('self_protection', 'defconfig', 'CC_STACKPROTECTOR_STRONG', 'y'))]
86     l += [OR(KconfigCheck('self_protection', 'defconfig', 'STRICT_KERNEL_RWX', 'y'),
87              KconfigCheck('self_protection', 'defconfig', 'DEBUG_RODATA', 'y'))] # before v4.11
88     l += [OR(KconfigCheck('self_protection', 'defconfig', 'STRICT_MODULE_RWX', 'y'),
89              KconfigCheck('self_protection', 'defconfig', 'DEBUG_SET_MODULE_RONX', 'y'),
90              modules_not_set)] # DEBUG_SET_MODULE_RONX was before v4.11
91     l += [OR(KconfigCheck('self_protection', 'defconfig', 'REFCOUNT_FULL', 'y'),
92              VersionCheck((5, 5)))] # REFCOUNT_FULL is enabled by default since v5.5
93     if arch in ('X86_64', 'ARM64', 'X86_32'):
94         l += [KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_BASE', 'y')]
95     if arch in ('X86_64', 'ARM64', 'ARM'):
96         l += [KconfigCheck('self_protection', 'defconfig', 'VMAP_STACK', 'y')]
97     if arch in ('X86_64', 'X86_32'):
98         l += [KconfigCheck('self_protection', 'defconfig', 'DEBUG_WX', 'y')]
99         l += [KconfigCheck('self_protection', 'defconfig', 'WERROR', 'y')]
100         l += [KconfigCheck('self_protection', 'defconfig', 'X86_MCE', 'y')]
101         l += [KconfigCheck('self_protection', 'defconfig', 'X86_MCE_INTEL', 'y')]
102         l += [KconfigCheck('self_protection', 'defconfig', 'X86_MCE_AMD', 'y')]
103         l += [KconfigCheck('self_protection', 'defconfig', 'MICROCODE', 'y')] # is needed for mitigating CPU bugs
104         l += [KconfigCheck('self_protection', 'defconfig', 'RETPOLINE', 'y')]
105         l += [KconfigCheck('self_protection', 'defconfig', 'SYN_COOKIES', 'y')] # another reason?
106         l += [OR(KconfigCheck('self_protection', 'defconfig', 'X86_SMAP', 'y'),
107                  VersionCheck((5, 19)))] # X86_SMAP is enabled by default since v5.19
108         l += [OR(KconfigCheck('self_protection', 'defconfig', 'X86_UMIP', 'y'),
109                  KconfigCheck('self_protection', 'defconfig', 'X86_INTEL_UMIP', 'y'))]
110     if arch in ('ARM64', 'ARM'):
111         l += [KconfigCheck('self_protection', 'defconfig', 'IOMMU_DEFAULT_DMA_STRICT', 'y')]
112         l += [KconfigCheck('self_protection', 'defconfig', 'IOMMU_DEFAULT_PASSTHROUGH', 'is not set')] # true if IOMMU_DEFAULT_DMA_STRICT is set
113         l += [KconfigCheck('self_protection', 'defconfig', 'STACKPROTECTOR_PER_TASK', 'y')]
114     if arch == 'X86_64':
115         l += [KconfigCheck('self_protection', 'defconfig', 'PAGE_TABLE_ISOLATION', 'y')]
116         l += [KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_MEMORY', 'y')]
117         l += [AND(KconfigCheck('self_protection', 'defconfig', 'INTEL_IOMMU', 'y'),
118                   iommu_support_is_set)]
119         l += [AND(KconfigCheck('self_protection', 'defconfig', 'AMD_IOMMU', 'y'),
120                   iommu_support_is_set)]
121     if arch == 'ARM64':
122         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_PAN', 'y')]
123         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_EPAN', 'y')]
124         l += [KconfigCheck('self_protection', 'defconfig', 'UNMAP_KERNEL_AT_EL0', 'y')]
125         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_E0PD', 'y')]
126         l += [KconfigCheck('self_protection', 'defconfig', 'RODATA_FULL_DEFAULT_ENABLED', 'y')]
127         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_PTR_AUTH_KERNEL', 'y')]
128         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_BTI_KERNEL', 'y')]
129         l += [KconfigCheck('self_protection', 'defconfig', 'MITIGATE_SPECTRE_BRANCH_HISTORY', 'y')]
130         l += [KconfigCheck('self_protection', 'defconfig', 'ARM64_MTE', 'y')]
131         l += [KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_MODULE_REGION_FULL', 'y')]
132         l += [OR(KconfigCheck('self_protection', 'defconfig', 'HARDEN_EL2_VECTORS', 'y'),
133                  AND(KconfigCheck('self_protection', 'defconfig', 'RANDOMIZE_BASE', 'y'),
134                      VersionCheck((5, 9))))] # HARDEN_EL2_VECTORS was included in RANDOMIZE_BASE in v5.9
135         l += [OR(KconfigCheck('self_protection', 'defconfig', 'HARDEN_BRANCH_PREDICTOR', 'y'),
136                  VersionCheck((5, 10)))] # HARDEN_BRANCH_PREDICTOR is enabled by default since v5.10
137     if arch == 'ARM':
138         l += [KconfigCheck('self_protection', 'defconfig', 'CPU_SW_DOMAIN_PAN', 'y')]
139         l += [KconfigCheck('self_protection', 'defconfig', 'HARDEN_BRANCH_PREDICTOR', 'y')]
140         l += [KconfigCheck('self_protection', 'defconfig', 'HARDEN_BRANCH_HISTORY', 'y')]
141         l += [KconfigCheck('self_protection', 'defconfig', 'DEBUG_ALIGN_RODATA', 'y')]
142
143     # 'self_protection', 'kspp'
144     l += [KconfigCheck('self_protection', 'kspp', 'BUG_ON_DATA_CORRUPTION', 'y')]
145     l += [KconfigCheck('self_protection', 'kspp', 'SCHED_STACK_END_CHECK', 'y')]
146     l += [KconfigCheck('self_protection', 'kspp', 'SLAB_FREELIST_HARDENED', 'y')]
147     l += [KconfigCheck('self_protection', 'kspp', 'SLAB_FREELIST_RANDOM', 'y')]
148     l += [KconfigCheck('self_protection', 'kspp', 'SHUFFLE_PAGE_ALLOCATOR', 'y')]
149     l += [KconfigCheck('self_protection', 'kspp', 'FORTIFY_SOURCE', 'y')]
150     l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_LIST', 'y')]
151     l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_VIRTUAL', 'y')]
152     l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_SG', 'y')]
153     l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_CREDENTIALS', 'y')]
154     l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_NOTIFIERS', 'y')]
155     l += [KconfigCheck('self_protection', 'kspp', 'INIT_ON_ALLOC_DEFAULT_ON', 'y')]
156     l += [KconfigCheck('self_protection', 'kspp', 'KFENCE', 'y')]
157     l += [KconfigCheck('self_protection', 'kspp', 'ZERO_CALL_USED_REGS', 'y')]
158     l += [KconfigCheck('self_protection', 'kspp', 'HW_RANDOM_TPM', 'y')]
159     l += [KconfigCheck('self_protection', 'kspp', 'STATIC_USERMODEHELPER', 'y')] # needs userspace support
160     randstruct_is_set = OR(KconfigCheck('self_protection', 'kspp', 'RANDSTRUCT_FULL', 'y'),
161                            KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_RANDSTRUCT', 'y'))
162     l += [randstruct_is_set]
163     l += [AND(KconfigCheck('self_protection', 'kspp', 'RANDSTRUCT_PERFORMANCE', 'is not set'),
164               KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_RANDSTRUCT_PERFORMANCE', 'is not set'),
165               randstruct_is_set)]
166     hardened_usercopy_is_set = KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY', 'y')
167     l += [hardened_usercopy_is_set]
168     l += [AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY_FALLBACK', 'is not set'),
169               hardened_usercopy_is_set)] # usercopy whitelist violations should be prohibited
170     l += [AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY_PAGESPAN', 'is not set'),
171               hardened_usercopy_is_set)] # this debugging for HARDENED_USERCOPY is not needed for security
172     l += [AND(KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_LATENT_ENTROPY', 'y'),
173               gcc_plugins_support_is_set)]
174     l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG', 'y'),
175              modules_not_set)]
176     l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG_ALL', 'y'),
177              modules_not_set)]
178     l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG_SHA512', 'y'),
179              modules_not_set)]
180     l += [OR(KconfigCheck('self_protection', 'kspp', 'MODULE_SIG_FORCE', 'y'),
181              modules_not_set)] # refers to LOCKDOWN
182     l += [OR(KconfigCheck('self_protection', 'kspp', 'INIT_STACK_ALL_ZERO', 'y'),
183              KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_STRUCTLEAK_BYREF_ALL', 'y'))]
184     l += [OR(KconfigCheck('self_protection', 'kspp', 'INIT_ON_FREE_DEFAULT_ON', 'y'),
185              KconfigCheck('self_protection', 'kspp', 'PAGE_POISONING_ZERO', 'y'))]
186              # CONFIG_INIT_ON_FREE_DEFAULT_ON was added in v5.3.
187              # CONFIG_PAGE_POISONING_ZERO was removed in v5.11.
188              # Starting from v5.11 CONFIG_PAGE_POISONING unconditionally checks
189              # the 0xAA poison pattern on allocation.
190              # That brings higher performance penalty.
191     l += [OR(KconfigCheck('self_protection', 'kspp', 'EFI_DISABLE_PCI_DMA', 'y'),
192              efi_not_set)]
193     l += [OR(KconfigCheck('self_protection', 'kspp', 'RESET_ATTACK_MITIGATION', 'y'),
194              efi_not_set)] # needs userspace support (systemd)
195     ubsan_bounds_is_set = KconfigCheck('self_protection', 'kspp', 'UBSAN_BOUNDS', 'y')
196     l += [ubsan_bounds_is_set]
197     l += [OR(KconfigCheck('self_protection', 'kspp', 'UBSAN_LOCAL_BOUNDS', 'y'),
198              AND(ubsan_bounds_is_set,
199                  cc_is_gcc))]
200     l += [AND(KconfigCheck('self_protection', 'kspp', 'UBSAN_TRAP', 'y'),
201               ubsan_bounds_is_set,
202               KconfigCheck('self_protection', 'kspp', 'UBSAN_SHIFT', 'is not set'),
203               KconfigCheck('self_protection', 'kspp', 'UBSAN_DIV_ZERO', 'is not set'),
204               KconfigCheck('self_protection', 'kspp', 'UBSAN_UNREACHABLE', 'is not set'),
205               KconfigCheck('self_protection', 'kspp', 'UBSAN_BOOL', 'is not set'),
206               KconfigCheck('self_protection', 'kspp', 'UBSAN_ENUM', 'is not set'),
207               KconfigCheck('self_protection', 'kspp', 'UBSAN_ALIGNMENT', 'is not set'))] # only array index bounds checking with traps
208     if arch in ('X86_64', 'ARM64', 'X86_32'):
209         l += [AND(KconfigCheck('self_protection', 'kspp', 'UBSAN_SANITIZE_ALL', 'y'),
210                   ubsan_bounds_is_set)] # ARCH_HAS_UBSAN_SANITIZE_ALL is not enabled for ARM
211         stackleak_is_set = KconfigCheck('self_protection', 'kspp', 'GCC_PLUGIN_STACKLEAK', 'y')
212         l += [AND(stackleak_is_set, gcc_plugins_support_is_set)]
213         l += [AND(KconfigCheck('self_protection', 'kspp', 'STACKLEAK_METRICS', 'is not set'),
214                   stackleak_is_set,
215                   gcc_plugins_support_is_set)]
216         l += [AND(KconfigCheck('self_protection', 'kspp', 'STACKLEAK_RUNTIME_DISABLE', 'is not set'),
217                   stackleak_is_set,
218                   gcc_plugins_support_is_set)]
219         l += [KconfigCheck('self_protection', 'kspp', 'RANDOMIZE_KSTACK_OFFSET_DEFAULT', 'y')]
220     if arch in ('X86_64', 'ARM64'):
221         cfi_clang_is_set = KconfigCheck('self_protection', 'kspp', 'CFI_CLANG', 'y')
222         l += [cfi_clang_is_set]
223         l += [AND(KconfigCheck('self_protection', 'kspp', 'CFI_PERMISSIVE', 'is not set'),
224                   cfi_clang_is_set)]
225     if arch in ('X86_64', 'X86_32'):
226         l += [KconfigCheck('self_protection', 'kspp', 'SCHED_CORE', 'y')]
227         l += [KconfigCheck('self_protection', 'kspp', 'DEFAULT_MMAP_MIN_ADDR', '65536')]
228         l += [KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_DMA_STRICT', 'y')]
229         l += [KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_PASSTHROUGH', 'is not set')] # true if IOMMU_DEFAULT_DMA_STRICT is set
230         l += [AND(KconfigCheck('self_protection', 'kspp', 'INTEL_IOMMU_DEFAULT_ON', 'y'),
231                   iommu_support_is_set)]
232     if arch in ('ARM64', 'ARM'):
233         l += [KconfigCheck('self_protection', 'kspp', 'DEBUG_WX', 'y')]
234         l += [KconfigCheck('self_protection', 'kspp', 'WERROR', 'y')]
235         l += [KconfigCheck('self_protection', 'kspp', 'DEFAULT_MMAP_MIN_ADDR', '32768')]
236         l += [KconfigCheck('self_protection', 'kspp', 'SYN_COOKIES', 'y')] # another reason?
237     if arch == 'X86_64':
238         l += [KconfigCheck('self_protection', 'kspp', 'SLS', 'y')] # vs CVE-2021-26341 in Straight-Line-Speculation
239         l += [AND(KconfigCheck('self_protection', 'kspp', 'INTEL_IOMMU_SVM', 'y'),
240                   iommu_support_is_set)]
241         l += [AND(KconfigCheck('self_protection', 'kspp', 'AMD_IOMMU_V2', 'y'),
242                   iommu_support_is_set)]
243     if arch == 'ARM64':
244         l += [KconfigCheck('self_protection', 'kspp', 'ARM64_SW_TTBR0_PAN', 'y')]
245         l += [KconfigCheck('self_protection', 'kspp', 'SHADOW_CALL_STACK', 'y')]
246         l += [KconfigCheck('self_protection', 'kspp', 'KASAN_HW_TAGS', 'y')] # see also: kasan=on, kasan.stacktrace=off, kasan.fault=panic
247     if arch == 'X86_32':
248         l += [KconfigCheck('self_protection', 'kspp', 'PAGE_TABLE_ISOLATION', 'y')]
249         l += [KconfigCheck('self_protection', 'kspp', 'HIGHMEM64G', 'y')]
250         l += [KconfigCheck('self_protection', 'kspp', 'X86_PAE', 'y')]
251         l += [AND(KconfigCheck('self_protection', 'kspp', 'INTEL_IOMMU', 'y'),
252                   iommu_support_is_set)]
253
254     # 'self_protection', 'clipos'
255     l += [KconfigCheck('self_protection', 'clipos', 'SLAB_MERGE_DEFAULT', 'is not set')]
256
257     # 'security_policy'
258     if arch in ('X86_64', 'ARM64', 'X86_32'):
259         l += [KconfigCheck('security_policy', 'defconfig', 'SECURITY', 'y')]
260     if arch == 'ARM':
261         l += [KconfigCheck('security_policy', 'kspp', 'SECURITY', 'y')]
262     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_YAMA', 'y')]
263     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_LANDLOCK', 'y')]
264     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_SELINUX_DISABLE', 'is not set')]
265     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_SELINUX_BOOTPARAM', 'is not set')]
266     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_SELINUX_DEVELOP', 'is not set')]
267     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_LOCKDOWN_LSM', 'y')]
268     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_LOCKDOWN_LSM_EARLY', 'y')]
269     l += [KconfigCheck('security_policy', 'kspp', 'LOCK_DOWN_KERNEL_FORCE_CONFIDENTIALITY', 'y')]
270     l += [KconfigCheck('security_policy', 'kspp', 'SECURITY_WRITABLE_HOOKS', 'is not set')] # refers to SECURITY_SELINUX_DISABLE
271     l += [OR(KconfigCheck('security_policy', 'my', 'SECURITY_SELINUX', 'y'),
272              KconfigCheck('security_policy', 'my', 'SECURITY_APPARMOR', 'y'),
273              KconfigCheck('security_policy', 'my', 'SECURITY_SMACK', 'y'),
274              KconfigCheck('security_policy', 'my', '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 += [KconfigCheck('cut_attack_surface', 'defconfig', 'X86_INTEL_TSX_MODE_OFF', 'y')] # tsx=off
286
287     # 'cut_attack_surface', 'kspp'
288     l += [KconfigCheck('cut_attack_surface', 'kspp', 'SECURITY_DMESG_RESTRICT', 'y')]
289     l += [KconfigCheck('cut_attack_surface', 'kspp', 'ACPI_CUSTOM_METHOD', 'is not set')] # refers to LOCKDOWN
290     l += [KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT_BRK', 'is not set')]
291     l += [KconfigCheck('cut_attack_surface', 'kspp', 'DEVKMEM', 'is not set')] # refers to LOCKDOWN
292     l += [KconfigCheck('cut_attack_surface', 'kspp', 'BINFMT_MISC', 'is not set')]
293     l += [KconfigCheck('cut_attack_surface', 'kspp', 'INET_DIAG', 'is not set')]
294     l += [KconfigCheck('cut_attack_surface', 'kspp', 'KEXEC', 'is not set')] # refers to LOCKDOWN
295     l += [KconfigCheck('cut_attack_surface', 'kspp', 'PROC_KCORE', 'is not set')] # refers to LOCKDOWN
296     l += [KconfigCheck('cut_attack_surface', 'kspp', 'LEGACY_PTYS', 'is not set')]
297     l += [KconfigCheck('cut_attack_surface', 'kspp', 'HIBERNATION', 'is not set')] # refers to LOCKDOWN
298     l += [KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT', 'is not set')]
299     l += [KconfigCheck('cut_attack_surface', 'kspp', 'IA32_EMULATION', 'is not set')]
300     l += [KconfigCheck('cut_attack_surface', 'kspp', 'X86_X32', 'is not set')]
301     l += [KconfigCheck('cut_attack_surface', 'kspp', 'X86_X32_ABI', 'is not set')]
302     l += [KconfigCheck('cut_attack_surface', 'kspp', 'MODIFY_LDT_SYSCALL', 'is not set')]
303     l += [KconfigCheck('cut_attack_surface', 'kspp', 'OABI_COMPAT', 'is not set')]
304     l += [KconfigCheck('cut_attack_surface', 'kspp', 'X86_MSR', 'is not set')] # refers to LOCKDOWN
305     l += [modules_not_set]
306     l += [devmem_not_set]
307     l += [OR(KconfigCheck('cut_attack_surface', 'kspp', 'IO_STRICT_DEVMEM', 'y'),
308              devmem_not_set)] # refers to LOCKDOWN
309     l += [AND(KconfigCheck('cut_attack_surface', 'kspp', 'LDISC_AUTOLOAD', 'is not set'),
310               KconfigCheck('cut_attack_surface', 'kspp', 'LDISC_AUTOLOAD', 'is present'))]
311     if arch in ('X86_64', 'X86_32'):
312         l += [KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT_VDSO', 'is not set')]
313               # CONFIG_COMPAT_VDSO disabled ASLR of vDSO only on X86_64 and X86_32;
314               # on ARM64 this option has different meaning
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', 'COREDUMP', 'is not set')] # cut userspace attack surface
381 #   l += [KconfigCheck('cut_attack_surface', 'clipos', 'IKCONFIG', 'is not set')] # no, IKCONFIG is needed for this check :)
382     if arch == 'X86_64':
383         l += [OR(KconfigCheck('cut_attack_surface', 'clipos', 'X86_VSYSCALL_EMULATION', 'is not set'),
384                  KconfigCheck('cut_attack_surface', 'kspp', 'LEGACY_VSYSCALL_NONE', 'y'))]
385                  # disabling X86_VSYSCALL_EMULATION turns vsyscall off completely,
386                  # and LEGACY_VSYSCALL_NONE can be changed at boot time via the cmdline parameter
387
388     # 'cut_attack_surface', 'lockdown'
389     l += [KconfigCheck('cut_attack_surface', 'lockdown', 'EFI_TEST', 'is not set')] # refers to LOCKDOWN
390     l += [KconfigCheck('cut_attack_surface', 'lockdown', 'MMIOTRACE_TEST', 'is not set')] # refers to LOCKDOWN
391     l += [KconfigCheck('cut_attack_surface', 'lockdown', 'KPROBES', 'is not set')] # refers to LOCKDOWN
392     l += [bpf_syscall_not_set] # refers to LOCKDOWN
393
394     # 'cut_attack_surface', 'my'
395     l += [KconfigCheck('cut_attack_surface', 'my', 'LEGACY_TIOCSTI', 'is not set')]
396     l += [KconfigCheck('cut_attack_surface', 'my', 'MMIOTRACE', 'is not set')] # refers to LOCKDOWN (permissive)
397     l += [KconfigCheck('cut_attack_surface', 'my', 'LIVEPATCH', 'is not set')]
398     l += [KconfigCheck('cut_attack_surface', 'my', 'IP_DCCP', 'is not set')]
399     l += [KconfigCheck('cut_attack_surface', 'my', 'IP_SCTP', 'is not set')]
400     l += [KconfigCheck('cut_attack_surface', 'my', 'FTRACE', 'is not set')] # refers to LOCKDOWN
401     l += [KconfigCheck('cut_attack_surface', 'my', 'VIDEO_VIVID', 'is not set')]
402     l += [KconfigCheck('cut_attack_surface', 'my', 'INPUT_EVBUG', 'is not set')] # Can be used as a keylogger
403     l += [KconfigCheck('cut_attack_surface', 'my', 'KGDB', 'is not set')]
404     l += [KconfigCheck('cut_attack_surface', 'my', 'AIO', 'is not set')]
405     l += [KconfigCheck('cut_attack_surface', 'my', 'CORESIGHT', 'is not set')]
406     l += [OR(KconfigCheck('cut_attack_surface', 'my', 'TRIM_UNUSED_KSYMS', 'y'),
407              modules_not_set)]
408
409     # 'harden_userspace'
410     if arch == 'ARM64':
411         l += [KconfigCheck('harden_userspace', 'defconfig', 'ARM64_PTR_AUTH', 'y')]
412         l += [KconfigCheck('harden_userspace', 'defconfig', 'ARM64_BTI', 'y')]
413     if arch in ('ARM', 'X86_32'):
414         l += [KconfigCheck('harden_userspace', 'defconfig', 'VMSPLIT_3G', 'y')]
415     l += [KconfigCheck('harden_userspace', 'my', 'ARCH_MMAP_RND_BITS', 'MAX')] # 'MAX' value is refined using ARCH_MMAP_RND_BITS_MAX
416
417
418 def add_cmdline_checks(l, arch):
419     # Calling the CmdlineCheck class constructor:
420     #     CmdlineCheck(reason, decision, name, expected)
421     #
422     # [!] Don't add CmdlineChecks in add_kconfig_checks() to avoid wrong results
423     #     when the tool doesn't check the cmdline.
424     #
425     # [!] Make sure that values of the options in CmdlineChecks need normalization.
426     #     For more info see normalize_cmdline_options().
427     #
428     # A common pattern for checking the 'param_x' cmdline parameter
429     # that __overrides__ the 'PARAM_X_DEFAULT' kconfig option:
430     #   l += [OR(CmdlineCheck(reason, decision, 'param_x', '1'),
431     #            AND(KconfigCheck(reason, decision, 'PARAM_X_DEFAULT_ON', 'y'),
432     #                CmdlineCheck(reason, decision, 'param_x, 'is not set')))]
433     #
434     # Here we don't check the kconfig options or minimal kernel version
435     # required for the cmdline parameters. That would make the checks
436     # very complex and not give a 100% guarantee anyway.
437
438     # 'self_protection', 'defconfig'
439     l += [CmdlineCheck('self_protection', 'defconfig', 'nosmep', 'is not set')]
440     l += [CmdlineCheck('self_protection', 'defconfig', 'nosmap', 'is not set')]
441     l += [CmdlineCheck('self_protection', 'defconfig', 'nokaslr', 'is not set')]
442     l += [CmdlineCheck('self_protection', 'defconfig', 'nopti', 'is not set')]
443     l += [CmdlineCheck('self_protection', 'defconfig', 'nospectre_v1', 'is not set')]
444     l += [CmdlineCheck('self_protection', 'defconfig', 'nospectre_v2', 'is not set')]
445     l += [CmdlineCheck('self_protection', 'defconfig', 'nospectre_bhb', 'is not set')]
446     l += [CmdlineCheck('self_protection', 'defconfig', 'nospec_store_bypass_disable', 'is not set')]
447     l += [CmdlineCheck('self_protection', 'defconfig', 'arm64.nobti', 'is not set')]
448     l += [CmdlineCheck('self_protection', 'defconfig', 'arm64.nopauth', 'is not set')]
449     l += [CmdlineCheck('self_protection', 'defconfig', 'arm64.nomte', 'is not set')]
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', 'kpti', 'is not off'),
478              AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
479                  CmdlineCheck('self_protection', 'defconfig', 'kpti', 'is not set')))]
480     if arch == 'ARM64':
481         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'ssbd', 'kernel'),
482                  CmdlineCheck('self_protection', 'my', 'ssbd', 'force-on'),
483                  AND(CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt'),
484                      CmdlineCheck('self_protection', 'defconfig', 'ssbd', 'is not set')))]
485         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'rodata', 'full'),
486                  AND(KconfigCheck('self_protection', 'defconfig', 'RODATA_FULL_DEFAULT_ENABLED', 'y'),
487                      CmdlineCheck('self_protection', 'defconfig', 'rodata', 'is not set')))]
488     else:
489         l += [OR(CmdlineCheck('self_protection', 'defconfig', 'rodata', '1'),
490                  CmdlineCheck('self_protection', 'defconfig', 'rodata', 'is not set'))]
491
492     # 'self_protection', 'kspp'
493     l += [CmdlineCheck('self_protection', 'kspp', 'nosmt', 'is present')]
494     l += [CmdlineCheck('self_protection', 'kspp', 'mitigations', 'auto,nosmt')] # 'nosmt' by kspp + 'auto' by defconfig
495     l += [CmdlineCheck('self_protection', 'kspp', 'slab_merge', 'is not set')] # consequence of 'slab_nomerge' by kspp
496     l += [CmdlineCheck('self_protection', 'kspp', 'slub_merge', 'is not set')] # consequence of 'slab_nomerge' by kspp
497     l += [OR(CmdlineCheck('self_protection', 'kspp', 'slab_nomerge', 'is present'),
498              AND(KconfigCheck('self_protection', 'clipos', 'SLAB_MERGE_DEFAULT', 'is not set'),
499                  CmdlineCheck('self_protection', 'kspp', 'slab_merge', 'is not set'),
500                  CmdlineCheck('self_protection', 'kspp', 'slub_merge', 'is not set')))]
501     l += [OR(CmdlineCheck('self_protection', 'kspp', 'init_on_alloc', '1'),
502              AND(KconfigCheck('self_protection', 'kspp', 'INIT_ON_ALLOC_DEFAULT_ON', 'y'),
503                  CmdlineCheck('self_protection', 'kspp', 'init_on_alloc', 'is not set')))]
504     l += [OR(CmdlineCheck('self_protection', 'kspp', 'init_on_free', '1'),
505              AND(KconfigCheck('self_protection', 'kspp', 'INIT_ON_FREE_DEFAULT_ON', 'y'),
506                  CmdlineCheck('self_protection', 'kspp', 'init_on_free', 'is not set')),
507              AND(CmdlineCheck('self_protection', 'kspp', 'page_poison', '1'),
508                  KconfigCheck('self_protection', 'kspp', 'PAGE_POISONING_ZERO', 'y'),
509                  CmdlineCheck('self_protection', 'kspp', 'slub_debug', 'P')))]
510     l += [OR(CmdlineCheck('self_protection', 'kspp', 'iommu.strict', '1'),
511              AND(KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_DMA_STRICT', 'y'),
512                  CmdlineCheck('self_protection', 'kspp', 'iommu.strict', 'is not set')))]
513     l += [OR(CmdlineCheck('self_protection', 'kspp', 'iommu.passthrough', '0'),
514              AND(KconfigCheck('self_protection', 'kspp', 'IOMMU_DEFAULT_PASSTHROUGH', 'is not set'),
515                  CmdlineCheck('self_protection', 'kspp', 'iommu.passthrough', 'is not set')))]
516     # The cmdline checks compatible with the kconfig recommendations of the KSPP project...
517     l += [OR(CmdlineCheck('self_protection', 'kspp', 'hardened_usercopy', '1'),
518              AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY', 'y'),
519                  CmdlineCheck('self_protection', 'kspp', 'hardened_usercopy', 'is not set')))]
520     l += [OR(CmdlineCheck('self_protection', 'kspp', 'slab_common.usercopy_fallback', '0'),
521              AND(KconfigCheck('self_protection', 'kspp', 'HARDENED_USERCOPY_FALLBACK', 'is not set'),
522                  CmdlineCheck('self_protection', 'kspp', 'slab_common.usercopy_fallback', 'is not set')))]
523     # ... the end
524     if arch in ('X86_64', 'ARM64', 'X86_32'):
525         l += [OR(CmdlineCheck('self_protection', 'kspp', 'randomize_kstack_offset', '1'),
526                  AND(KconfigCheck('self_protection', 'kspp', 'RANDOMIZE_KSTACK_OFFSET_DEFAULT', 'y'),
527                      CmdlineCheck('self_protection', 'kspp', 'randomize_kstack_offset', 'is not set')))]
528     if arch in ('X86_64', 'X86_32'):
529         l += [AND(CmdlineCheck('self_protection', 'kspp', 'pti', 'on'),
530                   CmdlineCheck('self_protection', 'defconfig', 'nopti', 'is not set'))]
531
532     # 'self_protection', 'clipos'
533     l += [CmdlineCheck('self_protection', 'clipos', 'page_alloc.shuffle', '1')]
534     if arch in ('X86_64', 'X86_32'):
535         l += [CmdlineCheck('self_protection', 'clipos', 'iommu', 'force')]
536
537     # 'cut_attack_surface', 'defconfig'
538     if arch in ('X86_64', 'X86_32'):
539         l += [OR(CmdlineCheck('cut_attack_surface', 'defconfig', 'tsx', 'off'),
540                  AND(KconfigCheck('cut_attack_surface', 'defconfig', 'X86_INTEL_TSX_MODE_OFF', 'y'),
541                      CmdlineCheck('cut_attack_surface', 'defconfig', 'tsx', 'is not set')))]
542
543     # 'cut_attack_surface', 'kspp'
544     if arch == 'X86_64':
545         l += [OR(CmdlineCheck('cut_attack_surface', 'kspp', 'vsyscall', 'none'),
546                  KconfigCheck('cut_attack_surface', 'clipos', 'X86_VSYSCALL_EMULATION', 'is not set'),
547                  AND(KconfigCheck('cut_attack_surface', 'kspp', 'LEGACY_VSYSCALL_NONE', 'y'),
548                      CmdlineCheck('cut_attack_surface', 'kspp', 'vsyscall', 'is not set')))]
549         l += [OR(CmdlineCheck('cut_attack_surface', 'my', 'vdso32', '1'),
550                  CmdlineCheck('cut_attack_surface', 'my', 'vdso32', '0'),
551                  AND(KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT_VDSO', 'is not set'),
552                      CmdlineCheck('cut_attack_surface', 'my', 'vdso32', 'is not set')))] # the vdso32 parameter must not be 2
553     if arch == 'X86_32':
554         l += [OR(CmdlineCheck('cut_attack_surface', 'my', 'vdso32', '1'),
555                  CmdlineCheck('cut_attack_surface', 'my', 'vdso', '1'),
556                  CmdlineCheck('cut_attack_surface', 'my', 'vdso32', '0'),
557                  CmdlineCheck('cut_attack_surface', 'my', 'vdso', '0'),
558                  AND(KconfigCheck('cut_attack_surface', 'kspp', 'COMPAT_VDSO', 'is not set'),
559                      CmdlineCheck('cut_attack_surface', 'my', 'vdso32', 'is not set'),
560                      CmdlineCheck('cut_attack_surface', 'my', 'vdso', 'is not set')))] # the vdso and vdso32 parameters must not be 2
561
562     # 'cut_attack_surface', 'grsec'
563     # The cmdline checks compatible with the kconfig options disabled by grsecurity...
564     l += [OR(CmdlineCheck('cut_attack_surface', 'grsec', 'debugfs', 'off'),
565              KconfigCheck('cut_attack_surface', 'grsec', 'DEBUG_FS', 'is not set'))] # ... the end
566
567     # 'cut_attack_surface', 'my'
568     l += [CmdlineCheck('cut_attack_surface', 'my', 'sysrq_always_enabled', 'is not set')]
569
570     # 'harden_userspace'
571     l += [CmdlineCheck('harden_userspace', 'defconfig', 'norandmaps', 'is not set')]
572
573
574 no_kstrtobool_options = [
575     'debugfs', # See debugfs_kernel() in fs/debugfs/inode.c
576     'mitigations', # See mitigations_parse_cmdline() in kernel/cpu.c
577     'pti', # See pti_check_boottime_disable() in arch/x86/mm/pti.c
578     'spectre_v2', # See spectre_v2_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
579     'spectre_v2_user', # See spectre_v2_parse_user_cmdline() in arch/x86/kernel/cpu/bugs.c
580     'spec_store_bypass_disable', # See ssb_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
581     'l1tf', # See l1tf_cmdline() in arch/x86/kernel/cpu/bugs.c
582     'mds', # See mds_cmdline() in arch/x86/kernel/cpu/bugs.c
583     'tsx_async_abort', # See tsx_async_abort_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
584     'srbds', # See srbds_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
585     'mmio_stale_data', # See mmio_stale_data_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
586     'retbleed', # See retbleed_parse_cmdline() in arch/x86/kernel/cpu/bugs.c
587     'tsx' # See tsx_init() in arch/x86/kernel/cpu/tsx.c
588 ]
589
590
591 def normalize_cmdline_options(option, value):
592     # Don't normalize the cmdline option values if
593     # the Linux kernel doesn't use kstrtobool() for them
594     if option in no_kstrtobool_options:
595         return value
596
597     # Implement a limited part of the kstrtobool() logic
598     if value in ('1', 'on', 'On', 'ON', 'y', 'Y', 'yes', 'Yes', 'YES'):
599         return '1'
600     if value in ('0', 'off', 'Off', 'OFF', 'n', 'N', 'no', 'No', 'NO'):
601         return '0'
602
603     # Preserve unique values
604     return value