1*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only 2*4882a593Smuzhiyunmenu "Kernel hardening options" 3*4882a593Smuzhiyun 4*4882a593Smuzhiyunconfig GCC_PLUGIN_STRUCTLEAK 5*4882a593Smuzhiyun bool 6*4882a593Smuzhiyun help 7*4882a593Smuzhiyun While the kernel is built with warnings enabled for any missed 8*4882a593Smuzhiyun stack variable initializations, this warning is silenced for 9*4882a593Smuzhiyun anything passed by reference to another function, under the 10*4882a593Smuzhiyun occasionally misguided assumption that the function will do 11*4882a593Smuzhiyun the initialization. As this regularly leads to exploitable 12*4882a593Smuzhiyun flaws, this plugin is available to identify and zero-initialize 13*4882a593Smuzhiyun such variables, depending on the chosen level of coverage. 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun This plugin was originally ported from grsecurity/PaX. More 16*4882a593Smuzhiyun information at: 17*4882a593Smuzhiyun * https://grsecurity.net/ 18*4882a593Smuzhiyun * https://pax.grsecurity.net/ 19*4882a593Smuzhiyun 20*4882a593Smuzhiyunmenu "Memory initialization" 21*4882a593Smuzhiyun 22*4882a593Smuzhiyunconfig CC_HAS_AUTO_VAR_INIT_PATTERN 23*4882a593Smuzhiyun def_bool $(cc-option,-ftrivial-auto-var-init=pattern) 24*4882a593Smuzhiyun 25*4882a593Smuzhiyunconfig CC_HAS_AUTO_VAR_INIT_ZERO_BARE 26*4882a593Smuzhiyun def_bool $(cc-option,-ftrivial-auto-var-init=zero) 27*4882a593Smuzhiyun 28*4882a593Smuzhiyunconfig CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER 29*4882a593Smuzhiyun # Clang 16 and later warn about using the -enable flag, but it 30*4882a593Smuzhiyun # is required before then. 31*4882a593Smuzhiyun def_bool $(cc-option,-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang) 32*4882a593Smuzhiyun depends on !CC_HAS_AUTO_VAR_INIT_ZERO_BARE 33*4882a593Smuzhiyun 34*4882a593Smuzhiyunconfig CC_HAS_AUTO_VAR_INIT_ZERO 35*4882a593Smuzhiyun def_bool CC_HAS_AUTO_VAR_INIT_ZERO_BARE || CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER 36*4882a593Smuzhiyun 37*4882a593Smuzhiyunchoice 38*4882a593Smuzhiyun prompt "Initialize kernel stack variables at function entry" 39*4882a593Smuzhiyun default GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if COMPILE_TEST && GCC_PLUGINS 40*4882a593Smuzhiyun default INIT_STACK_ALL_PATTERN if COMPILE_TEST && CC_HAS_AUTO_VAR_INIT_PATTERN 41*4882a593Smuzhiyun default INIT_STACK_ALL_ZERO if CC_HAS_AUTO_VAR_INIT_ZERO 42*4882a593Smuzhiyun default INIT_STACK_NONE 43*4882a593Smuzhiyun help 44*4882a593Smuzhiyun This option enables initialization of stack variables at 45*4882a593Smuzhiyun function entry time. This has the possibility to have the 46*4882a593Smuzhiyun greatest coverage (since all functions can have their 47*4882a593Smuzhiyun variables initialized), but the performance impact depends 48*4882a593Smuzhiyun on the function calling complexity of a given workload's 49*4882a593Smuzhiyun syscalls. 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun This chooses the level of coverage over classes of potentially 52*4882a593Smuzhiyun uninitialized variables. The selected class of variable will be 53*4882a593Smuzhiyun initialized before use in a function. 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun config INIT_STACK_NONE 56*4882a593Smuzhiyun bool "no automatic stack variable initialization (weakest)" 57*4882a593Smuzhiyun help 58*4882a593Smuzhiyun Disable automatic stack variable initialization. 59*4882a593Smuzhiyun This leaves the kernel vulnerable to the standard 60*4882a593Smuzhiyun classes of uninitialized stack variable exploits 61*4882a593Smuzhiyun and information exposures. 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun config GCC_PLUGIN_STRUCTLEAK_USER 64*4882a593Smuzhiyun bool "zero-init structs marked for userspace (weak)" 65*4882a593Smuzhiyun depends on GCC_PLUGINS 66*4882a593Smuzhiyun select GCC_PLUGIN_STRUCTLEAK 67*4882a593Smuzhiyun help 68*4882a593Smuzhiyun Zero-initialize any structures on the stack containing 69*4882a593Smuzhiyun a __user attribute. This can prevent some classes of 70*4882a593Smuzhiyun uninitialized stack variable exploits and information 71*4882a593Smuzhiyun exposures, like CVE-2013-2141: 72*4882a593Smuzhiyun https://git.kernel.org/linus/b9e146d8eb3b9eca 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun config GCC_PLUGIN_STRUCTLEAK_BYREF 75*4882a593Smuzhiyun bool "zero-init structs passed by reference (strong)" 76*4882a593Smuzhiyun depends on GCC_PLUGINS 77*4882a593Smuzhiyun depends on !(KASAN && KASAN_STACK) 78*4882a593Smuzhiyun select GCC_PLUGIN_STRUCTLEAK 79*4882a593Smuzhiyun help 80*4882a593Smuzhiyun Zero-initialize any structures on the stack that may 81*4882a593Smuzhiyun be passed by reference and had not already been 82*4882a593Smuzhiyun explicitly initialized. This can prevent most classes 83*4882a593Smuzhiyun of uninitialized stack variable exploits and information 84*4882a593Smuzhiyun exposures, like CVE-2017-1000410: 85*4882a593Smuzhiyun https://git.kernel.org/linus/06e7e776ca4d3654 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun As a side-effect, this keeps a lot of variables on the 88*4882a593Smuzhiyun stack that can otherwise be optimized out, so combining 89*4882a593Smuzhiyun this with CONFIG_KASAN_STACK can lead to a stack overflow 90*4882a593Smuzhiyun and is disallowed. 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun config GCC_PLUGIN_STRUCTLEAK_BYREF_ALL 93*4882a593Smuzhiyun bool "zero-init everything passed by reference (very strong)" 94*4882a593Smuzhiyun depends on GCC_PLUGINS 95*4882a593Smuzhiyun depends on !(KASAN && KASAN_STACK) 96*4882a593Smuzhiyun select GCC_PLUGIN_STRUCTLEAK 97*4882a593Smuzhiyun help 98*4882a593Smuzhiyun Zero-initialize any stack variables that may be passed 99*4882a593Smuzhiyun by reference and had not already been explicitly 100*4882a593Smuzhiyun initialized. This is intended to eliminate all classes 101*4882a593Smuzhiyun of uninitialized stack variable exploits and information 102*4882a593Smuzhiyun exposures. 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun As a side-effect, this keeps a lot of variables on the 105*4882a593Smuzhiyun stack that can otherwise be optimized out, so combining 106*4882a593Smuzhiyun this with CONFIG_KASAN_STACK can lead to a stack overflow 107*4882a593Smuzhiyun and is disallowed. 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun config INIT_STACK_ALL_PATTERN 110*4882a593Smuzhiyun bool "pattern-init everything (strongest)" 111*4882a593Smuzhiyun depends on CC_HAS_AUTO_VAR_INIT_PATTERN 112*4882a593Smuzhiyun help 113*4882a593Smuzhiyun Initializes everything on the stack (including padding) 114*4882a593Smuzhiyun with a specific debug value. This is intended to eliminate 115*4882a593Smuzhiyun all classes of uninitialized stack variable exploits and 116*4882a593Smuzhiyun information exposures, even variables that were warned about 117*4882a593Smuzhiyun having been left uninitialized. 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun Pattern initialization is known to provoke many existing bugs 120*4882a593Smuzhiyun related to uninitialized locals, e.g. pointers receive 121*4882a593Smuzhiyun non-NULL values, buffer sizes and indices are very big. The 122*4882a593Smuzhiyun pattern is situation-specific; Clang on 64-bit uses 0xAA 123*4882a593Smuzhiyun repeating for all types and padding except float and double 124*4882a593Smuzhiyun which use 0xFF repeating (-NaN). Clang on 32-bit uses 0xFF 125*4882a593Smuzhiyun repeating for all types and padding. 126*4882a593Smuzhiyun 127*4882a593Smuzhiyun config INIT_STACK_ALL_ZERO 128*4882a593Smuzhiyun bool "zero-init everything (strongest and safest)" 129*4882a593Smuzhiyun depends on CC_HAS_AUTO_VAR_INIT_ZERO 130*4882a593Smuzhiyun help 131*4882a593Smuzhiyun Initializes everything on the stack (including padding) 132*4882a593Smuzhiyun with a zero value. This is intended to eliminate all 133*4882a593Smuzhiyun classes of uninitialized stack variable exploits and 134*4882a593Smuzhiyun information exposures, even variables that were warned 135*4882a593Smuzhiyun about having been left uninitialized. 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun Zero initialization provides safe defaults for strings 138*4882a593Smuzhiyun (immediately NUL-terminated), pointers (NULL), indices 139*4882a593Smuzhiyun (index 0), and sizes (0 length), so it is therefore more 140*4882a593Smuzhiyun suitable as a production security mitigation than pattern 141*4882a593Smuzhiyun initialization. 142*4882a593Smuzhiyun 143*4882a593Smuzhiyunendchoice 144*4882a593Smuzhiyun 145*4882a593Smuzhiyunconfig GCC_PLUGIN_STRUCTLEAK_VERBOSE 146*4882a593Smuzhiyun bool "Report forcefully initialized variables" 147*4882a593Smuzhiyun depends on GCC_PLUGIN_STRUCTLEAK 148*4882a593Smuzhiyun depends on !COMPILE_TEST # too noisy 149*4882a593Smuzhiyun help 150*4882a593Smuzhiyun This option will cause a warning to be printed each time the 151*4882a593Smuzhiyun structleak plugin finds a variable it thinks needs to be 152*4882a593Smuzhiyun initialized. Since not all existing initializers are detected 153*4882a593Smuzhiyun by the plugin, this can produce false positive warnings. 154*4882a593Smuzhiyun 155*4882a593Smuzhiyunconfig GCC_PLUGIN_STACKLEAK 156*4882a593Smuzhiyun bool "Poison kernel stack before returning from syscalls" 157*4882a593Smuzhiyun depends on GCC_PLUGINS 158*4882a593Smuzhiyun depends on HAVE_ARCH_STACKLEAK 159*4882a593Smuzhiyun help 160*4882a593Smuzhiyun This option makes the kernel erase the kernel stack before 161*4882a593Smuzhiyun returning from system calls. This has the effect of leaving 162*4882a593Smuzhiyun the stack initialized to the poison value, which both reduces 163*4882a593Smuzhiyun the lifetime of any sensitive stack contents and reduces 164*4882a593Smuzhiyun potential for uninitialized stack variable exploits or information 165*4882a593Smuzhiyun exposures (it does not cover functions reaching the same stack 166*4882a593Smuzhiyun depth as prior functions during the same syscall). This blocks 167*4882a593Smuzhiyun most uninitialized stack variable attacks, with the performance 168*4882a593Smuzhiyun impact being driven by the depth of the stack usage, rather than 169*4882a593Smuzhiyun the function calling complexity. 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun The performance impact on a single CPU system kernel compilation 172*4882a593Smuzhiyun sees a 1% slowdown, other systems and workloads may vary and you 173*4882a593Smuzhiyun are advised to test this feature on your expected workload before 174*4882a593Smuzhiyun deploying it. 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun This plugin was ported from grsecurity/PaX. More information at: 177*4882a593Smuzhiyun * https://grsecurity.net/ 178*4882a593Smuzhiyun * https://pax.grsecurity.net/ 179*4882a593Smuzhiyun 180*4882a593Smuzhiyunconfig STACKLEAK_TRACK_MIN_SIZE 181*4882a593Smuzhiyun int "Minimum stack frame size of functions tracked by STACKLEAK" 182*4882a593Smuzhiyun default 100 183*4882a593Smuzhiyun range 0 4096 184*4882a593Smuzhiyun depends on GCC_PLUGIN_STACKLEAK 185*4882a593Smuzhiyun help 186*4882a593Smuzhiyun The STACKLEAK gcc plugin instruments the kernel code for tracking 187*4882a593Smuzhiyun the lowest border of the kernel stack (and for some other purposes). 188*4882a593Smuzhiyun It inserts the stackleak_track_stack() call for the functions with 189*4882a593Smuzhiyun a stack frame size greater than or equal to this parameter. 190*4882a593Smuzhiyun If unsure, leave the default value 100. 191*4882a593Smuzhiyun 192*4882a593Smuzhiyunconfig STACKLEAK_METRICS 193*4882a593Smuzhiyun bool "Show STACKLEAK metrics in the /proc file system" 194*4882a593Smuzhiyun depends on GCC_PLUGIN_STACKLEAK 195*4882a593Smuzhiyun depends on PROC_FS 196*4882a593Smuzhiyun help 197*4882a593Smuzhiyun If this is set, STACKLEAK metrics for every task are available in 198*4882a593Smuzhiyun the /proc file system. In particular, /proc/<pid>/stack_depth 199*4882a593Smuzhiyun shows the maximum kernel stack consumption for the current and 200*4882a593Smuzhiyun previous syscalls. Although this information is not precise, it 201*4882a593Smuzhiyun can be useful for estimating the STACKLEAK performance impact for 202*4882a593Smuzhiyun your workloads. 203*4882a593Smuzhiyun 204*4882a593Smuzhiyunconfig STACKLEAK_RUNTIME_DISABLE 205*4882a593Smuzhiyun bool "Allow runtime disabling of kernel stack erasing" 206*4882a593Smuzhiyun depends on GCC_PLUGIN_STACKLEAK 207*4882a593Smuzhiyun help 208*4882a593Smuzhiyun This option provides 'stack_erasing' sysctl, which can be used in 209*4882a593Smuzhiyun runtime to control kernel stack erasing for kernels built with 210*4882a593Smuzhiyun CONFIG_GCC_PLUGIN_STACKLEAK. 211*4882a593Smuzhiyun 212*4882a593Smuzhiyunconfig INIT_ON_ALLOC_DEFAULT_ON 213*4882a593Smuzhiyun bool "Enable heap memory zeroing on allocation by default" 214*4882a593Smuzhiyun help 215*4882a593Smuzhiyun This has the effect of setting "init_on_alloc=1" on the kernel 216*4882a593Smuzhiyun command line. This can be disabled with "init_on_alloc=0". 217*4882a593Smuzhiyun When "init_on_alloc" is enabled, all page allocator and slab 218*4882a593Smuzhiyun allocator memory will be zeroed when allocated, eliminating 219*4882a593Smuzhiyun many kinds of "uninitialized heap memory" flaws, especially 220*4882a593Smuzhiyun heap content exposures. The performance impact varies by 221*4882a593Smuzhiyun workload, but most cases see <1% impact. Some synthetic 222*4882a593Smuzhiyun workloads have measured as high as 7%. 223*4882a593Smuzhiyun 224*4882a593Smuzhiyunconfig INIT_ON_FREE_DEFAULT_ON 225*4882a593Smuzhiyun bool "Enable heap memory zeroing on free by default" 226*4882a593Smuzhiyun help 227*4882a593Smuzhiyun This has the effect of setting "init_on_free=1" on the kernel 228*4882a593Smuzhiyun command line. This can be disabled with "init_on_free=0". 229*4882a593Smuzhiyun Similar to "init_on_alloc", when "init_on_free" is enabled, 230*4882a593Smuzhiyun all page allocator and slab allocator memory will be zeroed 231*4882a593Smuzhiyun when freed, eliminating many kinds of "uninitialized heap memory" 232*4882a593Smuzhiyun flaws, especially heap content exposures. The primary difference 233*4882a593Smuzhiyun with "init_on_free" is that data lifetime in memory is reduced, 234*4882a593Smuzhiyun as anything freed is wiped immediately, making live forensics or 235*4882a593Smuzhiyun cold boot memory attacks unable to recover freed memory contents. 236*4882a593Smuzhiyun The performance impact varies by workload, but is more expensive 237*4882a593Smuzhiyun than "init_on_alloc" due to the negative cache effects of 238*4882a593Smuzhiyun touching "cold" memory areas. Most cases see 3-5% impact. Some 239*4882a593Smuzhiyun synthetic workloads have measured as high as 8%. 240*4882a593Smuzhiyun 241*4882a593Smuzhiyunendmenu 242*4882a593Smuzhiyun 243*4882a593Smuzhiyunendmenu 244