18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_cimenu "Kernel hardening options"
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ciconfig GCC_PLUGIN_STRUCTLEAK
58c2ecf20Sopenharmony_ci	bool
68c2ecf20Sopenharmony_ci	help
78c2ecf20Sopenharmony_ci	  While the kernel is built with warnings enabled for any missed
88c2ecf20Sopenharmony_ci	  stack variable initializations, this warning is silenced for
98c2ecf20Sopenharmony_ci	  anything passed by reference to another function, under the
108c2ecf20Sopenharmony_ci	  occasionally misguided assumption that the function will do
118c2ecf20Sopenharmony_ci	  the initialization. As this regularly leads to exploitable
128c2ecf20Sopenharmony_ci	  flaws, this plugin is available to identify and zero-initialize
138c2ecf20Sopenharmony_ci	  such variables, depending on the chosen level of coverage.
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	  This plugin was originally ported from grsecurity/PaX. More
168c2ecf20Sopenharmony_ci	  information at:
178c2ecf20Sopenharmony_ci	   * https://grsecurity.net/
188c2ecf20Sopenharmony_ci	   * https://pax.grsecurity.net/
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cimenu "Memory initialization"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciconfig CC_HAS_AUTO_VAR_INIT_PATTERN
238c2ecf20Sopenharmony_ci	def_bool $(cc-option,-ftrivial-auto-var-init=pattern)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciconfig CC_HAS_AUTO_VAR_INIT_ZERO_BARE
268c2ecf20Sopenharmony_ci	def_bool $(cc-option,-ftrivial-auto-var-init=zero)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciconfig CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER
298c2ecf20Sopenharmony_ci	# Clang 16 and later warn about using the -enable flag, but it
308c2ecf20Sopenharmony_ci	# is required before then.
318c2ecf20Sopenharmony_ci	def_bool $(cc-option,-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang)
328c2ecf20Sopenharmony_ci	depends on !CC_HAS_AUTO_VAR_INIT_ZERO_BARE
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciconfig CC_HAS_AUTO_VAR_INIT_ZERO
358c2ecf20Sopenharmony_ci	def_bool CC_HAS_AUTO_VAR_INIT_ZERO_BARE || CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cichoice
388c2ecf20Sopenharmony_ci	prompt "Initialize kernel stack variables at function entry"
398c2ecf20Sopenharmony_ci	default GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if COMPILE_TEST && GCC_PLUGINS
408c2ecf20Sopenharmony_ci	default INIT_STACK_ALL_PATTERN if COMPILE_TEST && CC_HAS_AUTO_VAR_INIT_PATTERN
418c2ecf20Sopenharmony_ci	default INIT_STACK_ALL_ZERO if CC_HAS_AUTO_VAR_INIT_ZERO
428c2ecf20Sopenharmony_ci	default INIT_STACK_NONE
438c2ecf20Sopenharmony_ci	help
448c2ecf20Sopenharmony_ci	  This option enables initialization of stack variables at
458c2ecf20Sopenharmony_ci	  function entry time. This has the possibility to have the
468c2ecf20Sopenharmony_ci	  greatest coverage (since all functions can have their
478c2ecf20Sopenharmony_ci	  variables initialized), but the performance impact depends
488c2ecf20Sopenharmony_ci	  on the function calling complexity of a given workload's
498c2ecf20Sopenharmony_ci	  syscalls.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	  This chooses the level of coverage over classes of potentially
528c2ecf20Sopenharmony_ci	  uninitialized variables. The selected class of variable will be
538c2ecf20Sopenharmony_ci	  initialized before use in a function.
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	config INIT_STACK_NONE
568c2ecf20Sopenharmony_ci		bool "no automatic stack variable initialization (weakest)"
578c2ecf20Sopenharmony_ci		help
588c2ecf20Sopenharmony_ci		  Disable automatic stack variable initialization.
598c2ecf20Sopenharmony_ci		  This leaves the kernel vulnerable to the standard
608c2ecf20Sopenharmony_ci		  classes of uninitialized stack variable exploits
618c2ecf20Sopenharmony_ci		  and information exposures.
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	config GCC_PLUGIN_STRUCTLEAK_USER
648c2ecf20Sopenharmony_ci		bool "zero-init structs marked for userspace (weak)"
658c2ecf20Sopenharmony_ci		depends on GCC_PLUGINS
668c2ecf20Sopenharmony_ci		select GCC_PLUGIN_STRUCTLEAK
678c2ecf20Sopenharmony_ci		help
688c2ecf20Sopenharmony_ci		  Zero-initialize any structures on the stack containing
698c2ecf20Sopenharmony_ci		  a __user attribute. This can prevent some classes of
708c2ecf20Sopenharmony_ci		  uninitialized stack variable exploits and information
718c2ecf20Sopenharmony_ci		  exposures, like CVE-2013-2141:
728c2ecf20Sopenharmony_ci		  https://git.kernel.org/linus/b9e146d8eb3b9eca
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	config GCC_PLUGIN_STRUCTLEAK_BYREF
758c2ecf20Sopenharmony_ci		bool "zero-init structs passed by reference (strong)"
768c2ecf20Sopenharmony_ci		depends on GCC_PLUGINS
778c2ecf20Sopenharmony_ci		depends on !(KASAN && KASAN_STACK=1)
788c2ecf20Sopenharmony_ci		select GCC_PLUGIN_STRUCTLEAK
798c2ecf20Sopenharmony_ci		help
808c2ecf20Sopenharmony_ci		  Zero-initialize any structures on the stack that may
818c2ecf20Sopenharmony_ci		  be passed by reference and had not already been
828c2ecf20Sopenharmony_ci		  explicitly initialized. This can prevent most classes
838c2ecf20Sopenharmony_ci		  of uninitialized stack variable exploits and information
848c2ecf20Sopenharmony_ci		  exposures, like CVE-2017-1000410:
858c2ecf20Sopenharmony_ci		  https://git.kernel.org/linus/06e7e776ca4d3654
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci		  As a side-effect, this keeps a lot of variables on the
888c2ecf20Sopenharmony_ci		  stack that can otherwise be optimized out, so combining
898c2ecf20Sopenharmony_ci		  this with CONFIG_KASAN_STACK can lead to a stack overflow
908c2ecf20Sopenharmony_ci		  and is disallowed.
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	config GCC_PLUGIN_STRUCTLEAK_BYREF_ALL
938c2ecf20Sopenharmony_ci		bool "zero-init everything passed by reference (very strong)"
948c2ecf20Sopenharmony_ci		depends on GCC_PLUGINS
958c2ecf20Sopenharmony_ci		depends on !(KASAN && KASAN_STACK=1)
968c2ecf20Sopenharmony_ci		select GCC_PLUGIN_STRUCTLEAK
978c2ecf20Sopenharmony_ci		help
988c2ecf20Sopenharmony_ci		  Zero-initialize any stack variables that may be passed
998c2ecf20Sopenharmony_ci		  by reference and had not already been explicitly
1008c2ecf20Sopenharmony_ci		  initialized. This is intended to eliminate all classes
1018c2ecf20Sopenharmony_ci		  of uninitialized stack variable exploits and information
1028c2ecf20Sopenharmony_ci		  exposures.
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		  As a side-effect, this keeps a lot of variables on the
1058c2ecf20Sopenharmony_ci		  stack that can otherwise be optimized out, so combining
1068c2ecf20Sopenharmony_ci		  this with CONFIG_KASAN_STACK can lead to a stack overflow
1078c2ecf20Sopenharmony_ci		  and is disallowed.
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	config INIT_STACK_ALL_PATTERN
1108c2ecf20Sopenharmony_ci		bool "pattern-init everything (strongest)"
1118c2ecf20Sopenharmony_ci		depends on CC_HAS_AUTO_VAR_INIT_PATTERN
1128c2ecf20Sopenharmony_ci		help
1138c2ecf20Sopenharmony_ci		  Initializes everything on the stack (including padding)
1148c2ecf20Sopenharmony_ci		  with a specific debug value. This is intended to eliminate
1158c2ecf20Sopenharmony_ci		  all classes of uninitialized stack variable exploits and
1168c2ecf20Sopenharmony_ci		  information exposures, even variables that were warned about
1178c2ecf20Sopenharmony_ci		  having been left uninitialized.
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci		  Pattern initialization is known to provoke many existing bugs
1208c2ecf20Sopenharmony_ci		  related to uninitialized locals, e.g. pointers receive
1218c2ecf20Sopenharmony_ci		  non-NULL values, buffer sizes and indices are very big. The
1228c2ecf20Sopenharmony_ci		  pattern is situation-specific; Clang on 64-bit uses 0xAA
1238c2ecf20Sopenharmony_ci		  repeating for all types and padding except float and double
1248c2ecf20Sopenharmony_ci		  which use 0xFF repeating (-NaN). Clang on 32-bit uses 0xFF
1258c2ecf20Sopenharmony_ci		  repeating for all types and padding.
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	config INIT_STACK_ALL_ZERO
1288c2ecf20Sopenharmony_ci		bool "zero-init everything (strongest and safest)"
1298c2ecf20Sopenharmony_ci		depends on CC_HAS_AUTO_VAR_INIT_ZERO
1308c2ecf20Sopenharmony_ci		help
1318c2ecf20Sopenharmony_ci		  Initializes everything on the stack (including padding)
1328c2ecf20Sopenharmony_ci		  with a zero value. This is intended to eliminate all
1338c2ecf20Sopenharmony_ci		  classes of uninitialized stack variable exploits and
1348c2ecf20Sopenharmony_ci		  information exposures, even variables that were warned
1358c2ecf20Sopenharmony_ci		  about having been left uninitialized.
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci		  Zero initialization provides safe defaults for strings
1388c2ecf20Sopenharmony_ci		  (immediately NUL-terminated), pointers (NULL), indices
1398c2ecf20Sopenharmony_ci		  (index 0), and sizes (0 length), so it is therefore more
1408c2ecf20Sopenharmony_ci		  suitable as a production security mitigation than pattern
1418c2ecf20Sopenharmony_ci		  initialization.
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciendchoice
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ciconfig GCC_PLUGIN_STRUCTLEAK_VERBOSE
1468c2ecf20Sopenharmony_ci	bool "Report forcefully initialized variables"
1478c2ecf20Sopenharmony_ci	depends on GCC_PLUGIN_STRUCTLEAK
1488c2ecf20Sopenharmony_ci	depends on !COMPILE_TEST	# too noisy
1498c2ecf20Sopenharmony_ci	help
1508c2ecf20Sopenharmony_ci	  This option will cause a warning to be printed each time the
1518c2ecf20Sopenharmony_ci	  structleak plugin finds a variable it thinks needs to be
1528c2ecf20Sopenharmony_ci	  initialized. Since not all existing initializers are detected
1538c2ecf20Sopenharmony_ci	  by the plugin, this can produce false positive warnings.
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ciconfig GCC_PLUGIN_STACKLEAK
1568c2ecf20Sopenharmony_ci	bool "Poison kernel stack before returning from syscalls"
1578c2ecf20Sopenharmony_ci	depends on GCC_PLUGINS
1588c2ecf20Sopenharmony_ci	depends on HAVE_ARCH_STACKLEAK
1598c2ecf20Sopenharmony_ci	help
1608c2ecf20Sopenharmony_ci	  This option makes the kernel erase the kernel stack before
1618c2ecf20Sopenharmony_ci	  returning from system calls. This has the effect of leaving
1628c2ecf20Sopenharmony_ci	  the stack initialized to the poison value, which both reduces
1638c2ecf20Sopenharmony_ci	  the lifetime of any sensitive stack contents and reduces
1648c2ecf20Sopenharmony_ci	  potential for uninitialized stack variable exploits or information
1658c2ecf20Sopenharmony_ci	  exposures (it does not cover functions reaching the same stack
1668c2ecf20Sopenharmony_ci	  depth as prior functions during the same syscall). This blocks
1678c2ecf20Sopenharmony_ci	  most uninitialized stack variable attacks, with the performance
1688c2ecf20Sopenharmony_ci	  impact being driven by the depth of the stack usage, rather than
1698c2ecf20Sopenharmony_ci	  the function calling complexity.
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	  The performance impact on a single CPU system kernel compilation
1728c2ecf20Sopenharmony_ci	  sees a 1% slowdown, other systems and workloads may vary and you
1738c2ecf20Sopenharmony_ci	  are advised to test this feature on your expected workload before
1748c2ecf20Sopenharmony_ci	  deploying it.
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	  This plugin was ported from grsecurity/PaX. More information at:
1778c2ecf20Sopenharmony_ci	   * https://grsecurity.net/
1788c2ecf20Sopenharmony_ci	   * https://pax.grsecurity.net/
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciconfig STACKLEAK_TRACK_MIN_SIZE
1818c2ecf20Sopenharmony_ci	int "Minimum stack frame size of functions tracked by STACKLEAK"
1828c2ecf20Sopenharmony_ci	default 100
1838c2ecf20Sopenharmony_ci	range 0 4096
1848c2ecf20Sopenharmony_ci	depends on GCC_PLUGIN_STACKLEAK
1858c2ecf20Sopenharmony_ci	help
1868c2ecf20Sopenharmony_ci	  The STACKLEAK gcc plugin instruments the kernel code for tracking
1878c2ecf20Sopenharmony_ci	  the lowest border of the kernel stack (and for some other purposes).
1888c2ecf20Sopenharmony_ci	  It inserts the stackleak_track_stack() call for the functions with
1898c2ecf20Sopenharmony_ci	  a stack frame size greater than or equal to this parameter.
1908c2ecf20Sopenharmony_ci	  If unsure, leave the default value 100.
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciconfig STACKLEAK_METRICS
1938c2ecf20Sopenharmony_ci	bool "Show STACKLEAK metrics in the /proc file system"
1948c2ecf20Sopenharmony_ci	depends on GCC_PLUGIN_STACKLEAK
1958c2ecf20Sopenharmony_ci	depends on PROC_FS
1968c2ecf20Sopenharmony_ci	help
1978c2ecf20Sopenharmony_ci	  If this is set, STACKLEAK metrics for every task are available in
1988c2ecf20Sopenharmony_ci	  the /proc file system. In particular, /proc/<pid>/stack_depth
1998c2ecf20Sopenharmony_ci	  shows the maximum kernel stack consumption for the current and
2008c2ecf20Sopenharmony_ci	  previous syscalls. Although this information is not precise, it
2018c2ecf20Sopenharmony_ci	  can be useful for estimating the STACKLEAK performance impact for
2028c2ecf20Sopenharmony_ci	  your workloads.
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ciconfig STACKLEAK_RUNTIME_DISABLE
2058c2ecf20Sopenharmony_ci	bool "Allow runtime disabling of kernel stack erasing"
2068c2ecf20Sopenharmony_ci	depends on GCC_PLUGIN_STACKLEAK
2078c2ecf20Sopenharmony_ci	help
2088c2ecf20Sopenharmony_ci	  This option provides 'stack_erasing' sysctl, which can be used in
2098c2ecf20Sopenharmony_ci	  runtime to control kernel stack erasing for kernels built with
2108c2ecf20Sopenharmony_ci	  CONFIG_GCC_PLUGIN_STACKLEAK.
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ciconfig INIT_ON_ALLOC_DEFAULT_ON
2138c2ecf20Sopenharmony_ci	bool "Enable heap memory zeroing on allocation by default"
2148c2ecf20Sopenharmony_ci	help
2158c2ecf20Sopenharmony_ci	  This has the effect of setting "init_on_alloc=1" on the kernel
2168c2ecf20Sopenharmony_ci	  command line. This can be disabled with "init_on_alloc=0".
2178c2ecf20Sopenharmony_ci	  When "init_on_alloc" is enabled, all page allocator and slab
2188c2ecf20Sopenharmony_ci	  allocator memory will be zeroed when allocated, eliminating
2198c2ecf20Sopenharmony_ci	  many kinds of "uninitialized heap memory" flaws, especially
2208c2ecf20Sopenharmony_ci	  heap content exposures. The performance impact varies by
2218c2ecf20Sopenharmony_ci	  workload, but most cases see <1% impact. Some synthetic
2228c2ecf20Sopenharmony_ci	  workloads have measured as high as 7%.
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ciconfig INIT_ON_FREE_DEFAULT_ON
2258c2ecf20Sopenharmony_ci	bool "Enable heap memory zeroing on free by default"
2268c2ecf20Sopenharmony_ci	help
2278c2ecf20Sopenharmony_ci	  This has the effect of setting "init_on_free=1" on the kernel
2288c2ecf20Sopenharmony_ci	  command line. This can be disabled with "init_on_free=0".
2298c2ecf20Sopenharmony_ci	  Similar to "init_on_alloc", when "init_on_free" is enabled,
2308c2ecf20Sopenharmony_ci	  all page allocator and slab allocator memory will be zeroed
2318c2ecf20Sopenharmony_ci	  when freed, eliminating many kinds of "uninitialized heap memory"
2328c2ecf20Sopenharmony_ci	  flaws, especially heap content exposures. The primary difference
2338c2ecf20Sopenharmony_ci	  with "init_on_free" is that data lifetime in memory is reduced,
2348c2ecf20Sopenharmony_ci	  as anything freed is wiped immediately, making live forensics or
2358c2ecf20Sopenharmony_ci	  cold boot memory attacks unable to recover freed memory contents.
2368c2ecf20Sopenharmony_ci	  The performance impact varies by workload, but is more expensive
2378c2ecf20Sopenharmony_ci	  than "init_on_alloc" due to the negative cache effects of
2388c2ecf20Sopenharmony_ci	  touching "cold" memory areas. Most cases see 3-5% impact. Some
2398c2ecf20Sopenharmony_ci	  synthetic workloads have measured as high as 8%.
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ciendmenu
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ciendmenu
244