162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_cimenu "Kernel hardening options"
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciconfig GCC_PLUGIN_STRUCTLEAK
562306a36Sopenharmony_ci	bool
662306a36Sopenharmony_ci	help
762306a36Sopenharmony_ci	  While the kernel is built with warnings enabled for any missed
862306a36Sopenharmony_ci	  stack variable initializations, this warning is silenced for
962306a36Sopenharmony_ci	  anything passed by reference to another function, under the
1062306a36Sopenharmony_ci	  occasionally misguided assumption that the function will do
1162306a36Sopenharmony_ci	  the initialization. As this regularly leads to exploitable
1262306a36Sopenharmony_ci	  flaws, this plugin is available to identify and zero-initialize
1362306a36Sopenharmony_ci	  such variables, depending on the chosen level of coverage.
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci	  This plugin was originally ported from grsecurity/PaX. More
1662306a36Sopenharmony_ci	  information at:
1762306a36Sopenharmony_ci	   * https://grsecurity.net/
1862306a36Sopenharmony_ci	   * https://pax.grsecurity.net/
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cimenu "Memory initialization"
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ciconfig CC_HAS_AUTO_VAR_INIT_PATTERN
2362306a36Sopenharmony_ci	def_bool $(cc-option,-ftrivial-auto-var-init=pattern)
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciconfig CC_HAS_AUTO_VAR_INIT_ZERO_BARE
2662306a36Sopenharmony_ci	def_bool $(cc-option,-ftrivial-auto-var-init=zero)
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciconfig CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER
2962306a36Sopenharmony_ci	# Clang 16 and later warn about using the -enable flag, but it
3062306a36Sopenharmony_ci	# is required before then.
3162306a36Sopenharmony_ci	def_bool $(cc-option,-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang)
3262306a36Sopenharmony_ci	depends on !CC_HAS_AUTO_VAR_INIT_ZERO_BARE
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ciconfig CC_HAS_AUTO_VAR_INIT_ZERO
3562306a36Sopenharmony_ci	def_bool CC_HAS_AUTO_VAR_INIT_ZERO_BARE || CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cichoice
3862306a36Sopenharmony_ci	prompt "Initialize kernel stack variables at function entry"
3962306a36Sopenharmony_ci	default GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if COMPILE_TEST && GCC_PLUGINS
4062306a36Sopenharmony_ci	default INIT_STACK_ALL_PATTERN if COMPILE_TEST && CC_HAS_AUTO_VAR_INIT_PATTERN
4162306a36Sopenharmony_ci	default INIT_STACK_ALL_ZERO if CC_HAS_AUTO_VAR_INIT_ZERO
4262306a36Sopenharmony_ci	default INIT_STACK_NONE
4362306a36Sopenharmony_ci	help
4462306a36Sopenharmony_ci	  This option enables initialization of stack variables at
4562306a36Sopenharmony_ci	  function entry time. This has the possibility to have the
4662306a36Sopenharmony_ci	  greatest coverage (since all functions can have their
4762306a36Sopenharmony_ci	  variables initialized), but the performance impact depends
4862306a36Sopenharmony_ci	  on the function calling complexity of a given workload's
4962306a36Sopenharmony_ci	  syscalls.
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	  This chooses the level of coverage over classes of potentially
5262306a36Sopenharmony_ci	  uninitialized variables. The selected class of variable will be
5362306a36Sopenharmony_ci	  initialized before use in a function.
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	config INIT_STACK_NONE
5662306a36Sopenharmony_ci		bool "no automatic stack variable initialization (weakest)"
5762306a36Sopenharmony_ci		help
5862306a36Sopenharmony_ci		  Disable automatic stack variable initialization.
5962306a36Sopenharmony_ci		  This leaves the kernel vulnerable to the standard
6062306a36Sopenharmony_ci		  classes of uninitialized stack variable exploits
6162306a36Sopenharmony_ci		  and information exposures.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	config GCC_PLUGIN_STRUCTLEAK_USER
6462306a36Sopenharmony_ci		bool "zero-init structs marked for userspace (weak)"
6562306a36Sopenharmony_ci		# Plugin can be removed once the kernel only supports GCC 12+
6662306a36Sopenharmony_ci		depends on GCC_PLUGINS && !CC_HAS_AUTO_VAR_INIT_ZERO
6762306a36Sopenharmony_ci		select GCC_PLUGIN_STRUCTLEAK
6862306a36Sopenharmony_ci		help
6962306a36Sopenharmony_ci		  Zero-initialize any structures on the stack containing
7062306a36Sopenharmony_ci		  a __user attribute. This can prevent some classes of
7162306a36Sopenharmony_ci		  uninitialized stack variable exploits and information
7262306a36Sopenharmony_ci		  exposures, like CVE-2013-2141:
7362306a36Sopenharmony_ci		  https://git.kernel.org/linus/b9e146d8eb3b9eca
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	config GCC_PLUGIN_STRUCTLEAK_BYREF
7662306a36Sopenharmony_ci		bool "zero-init structs passed by reference (strong)"
7762306a36Sopenharmony_ci		# Plugin can be removed once the kernel only supports GCC 12+
7862306a36Sopenharmony_ci		depends on GCC_PLUGINS && !CC_HAS_AUTO_VAR_INIT_ZERO
7962306a36Sopenharmony_ci		depends on !(KASAN && KASAN_STACK)
8062306a36Sopenharmony_ci		select GCC_PLUGIN_STRUCTLEAK
8162306a36Sopenharmony_ci		help
8262306a36Sopenharmony_ci		  Zero-initialize any structures on the stack that may
8362306a36Sopenharmony_ci		  be passed by reference and had not already been
8462306a36Sopenharmony_ci		  explicitly initialized. This can prevent most classes
8562306a36Sopenharmony_ci		  of uninitialized stack variable exploits and information
8662306a36Sopenharmony_ci		  exposures, like CVE-2017-1000410:
8762306a36Sopenharmony_ci		  https://git.kernel.org/linus/06e7e776ca4d3654
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci		  As a side-effect, this keeps a lot of variables on the
9062306a36Sopenharmony_ci		  stack that can otherwise be optimized out, so combining
9162306a36Sopenharmony_ci		  this with CONFIG_KASAN_STACK can lead to a stack overflow
9262306a36Sopenharmony_ci		  and is disallowed.
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	config GCC_PLUGIN_STRUCTLEAK_BYREF_ALL
9562306a36Sopenharmony_ci		bool "zero-init everything passed by reference (very strong)"
9662306a36Sopenharmony_ci		# Plugin can be removed once the kernel only supports GCC 12+
9762306a36Sopenharmony_ci		depends on GCC_PLUGINS && !CC_HAS_AUTO_VAR_INIT_ZERO
9862306a36Sopenharmony_ci		depends on !(KASAN && KASAN_STACK)
9962306a36Sopenharmony_ci		select GCC_PLUGIN_STRUCTLEAK
10062306a36Sopenharmony_ci		help
10162306a36Sopenharmony_ci		  Zero-initialize any stack variables that may be passed
10262306a36Sopenharmony_ci		  by reference and had not already been explicitly
10362306a36Sopenharmony_ci		  initialized. This is intended to eliminate all classes
10462306a36Sopenharmony_ci		  of uninitialized stack variable exploits and information
10562306a36Sopenharmony_ci		  exposures.
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci		  As a side-effect, this keeps a lot of variables on the
10862306a36Sopenharmony_ci		  stack that can otherwise be optimized out, so combining
10962306a36Sopenharmony_ci		  this with CONFIG_KASAN_STACK can lead to a stack overflow
11062306a36Sopenharmony_ci		  and is disallowed.
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	config INIT_STACK_ALL_PATTERN
11362306a36Sopenharmony_ci		bool "pattern-init everything (strongest)"
11462306a36Sopenharmony_ci		depends on CC_HAS_AUTO_VAR_INIT_PATTERN
11562306a36Sopenharmony_ci		depends on !KMSAN
11662306a36Sopenharmony_ci		help
11762306a36Sopenharmony_ci		  Initializes everything on the stack (including padding)
11862306a36Sopenharmony_ci		  with a specific debug value. This is intended to eliminate
11962306a36Sopenharmony_ci		  all classes of uninitialized stack variable exploits and
12062306a36Sopenharmony_ci		  information exposures, even variables that were warned about
12162306a36Sopenharmony_ci		  having been left uninitialized.
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci		  Pattern initialization is known to provoke many existing bugs
12462306a36Sopenharmony_ci		  related to uninitialized locals, e.g. pointers receive
12562306a36Sopenharmony_ci		  non-NULL values, buffer sizes and indices are very big. The
12662306a36Sopenharmony_ci		  pattern is situation-specific; Clang on 64-bit uses 0xAA
12762306a36Sopenharmony_ci		  repeating for all types and padding except float and double
12862306a36Sopenharmony_ci		  which use 0xFF repeating (-NaN). Clang on 32-bit uses 0xFF
12962306a36Sopenharmony_ci		  repeating for all types and padding.
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	config INIT_STACK_ALL_ZERO
13262306a36Sopenharmony_ci		bool "zero-init everything (strongest and safest)"
13362306a36Sopenharmony_ci		depends on CC_HAS_AUTO_VAR_INIT_ZERO
13462306a36Sopenharmony_ci		depends on !KMSAN
13562306a36Sopenharmony_ci		help
13662306a36Sopenharmony_ci		  Initializes everything on the stack (including padding)
13762306a36Sopenharmony_ci		  with a zero value. This is intended to eliminate all
13862306a36Sopenharmony_ci		  classes of uninitialized stack variable exploits and
13962306a36Sopenharmony_ci		  information exposures, even variables that were warned
14062306a36Sopenharmony_ci		  about having been left uninitialized.
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci		  Zero initialization provides safe defaults for strings
14362306a36Sopenharmony_ci		  (immediately NUL-terminated), pointers (NULL), indices
14462306a36Sopenharmony_ci		  (index 0), and sizes (0 length), so it is therefore more
14562306a36Sopenharmony_ci		  suitable as a production security mitigation than pattern
14662306a36Sopenharmony_ci		  initialization.
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ciendchoice
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ciconfig GCC_PLUGIN_STRUCTLEAK_VERBOSE
15162306a36Sopenharmony_ci	bool "Report forcefully initialized variables"
15262306a36Sopenharmony_ci	depends on GCC_PLUGIN_STRUCTLEAK
15362306a36Sopenharmony_ci	depends on !COMPILE_TEST	# too noisy
15462306a36Sopenharmony_ci	help
15562306a36Sopenharmony_ci	  This option will cause a warning to be printed each time the
15662306a36Sopenharmony_ci	  structleak plugin finds a variable it thinks needs to be
15762306a36Sopenharmony_ci	  initialized. Since not all existing initializers are detected
15862306a36Sopenharmony_ci	  by the plugin, this can produce false positive warnings.
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciconfig GCC_PLUGIN_STACKLEAK
16162306a36Sopenharmony_ci	bool "Poison kernel stack before returning from syscalls"
16262306a36Sopenharmony_ci	depends on GCC_PLUGINS
16362306a36Sopenharmony_ci	depends on HAVE_ARCH_STACKLEAK
16462306a36Sopenharmony_ci	help
16562306a36Sopenharmony_ci	  This option makes the kernel erase the kernel stack before
16662306a36Sopenharmony_ci	  returning from system calls. This has the effect of leaving
16762306a36Sopenharmony_ci	  the stack initialized to the poison value, which both reduces
16862306a36Sopenharmony_ci	  the lifetime of any sensitive stack contents and reduces
16962306a36Sopenharmony_ci	  potential for uninitialized stack variable exploits or information
17062306a36Sopenharmony_ci	  exposures (it does not cover functions reaching the same stack
17162306a36Sopenharmony_ci	  depth as prior functions during the same syscall). This blocks
17262306a36Sopenharmony_ci	  most uninitialized stack variable attacks, with the performance
17362306a36Sopenharmony_ci	  impact being driven by the depth of the stack usage, rather than
17462306a36Sopenharmony_ci	  the function calling complexity.
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	  The performance impact on a single CPU system kernel compilation
17762306a36Sopenharmony_ci	  sees a 1% slowdown, other systems and workloads may vary and you
17862306a36Sopenharmony_ci	  are advised to test this feature on your expected workload before
17962306a36Sopenharmony_ci	  deploying it.
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	  This plugin was ported from grsecurity/PaX. More information at:
18262306a36Sopenharmony_ci	   * https://grsecurity.net/
18362306a36Sopenharmony_ci	   * https://pax.grsecurity.net/
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ciconfig GCC_PLUGIN_STACKLEAK_VERBOSE
18662306a36Sopenharmony_ci	bool "Report stack depth analysis instrumentation" if EXPERT
18762306a36Sopenharmony_ci	depends on GCC_PLUGIN_STACKLEAK
18862306a36Sopenharmony_ci	depends on !COMPILE_TEST	# too noisy
18962306a36Sopenharmony_ci	help
19062306a36Sopenharmony_ci	  This option will cause a warning to be printed each time the
19162306a36Sopenharmony_ci	  stackleak plugin finds a function it thinks needs to be
19262306a36Sopenharmony_ci	  instrumented. This is useful for comparing coverage between
19362306a36Sopenharmony_ci	  builds.
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ciconfig STACKLEAK_TRACK_MIN_SIZE
19662306a36Sopenharmony_ci	int "Minimum stack frame size of functions tracked by STACKLEAK"
19762306a36Sopenharmony_ci	default 100
19862306a36Sopenharmony_ci	range 0 4096
19962306a36Sopenharmony_ci	depends on GCC_PLUGIN_STACKLEAK
20062306a36Sopenharmony_ci	help
20162306a36Sopenharmony_ci	  The STACKLEAK gcc plugin instruments the kernel code for tracking
20262306a36Sopenharmony_ci	  the lowest border of the kernel stack (and for some other purposes).
20362306a36Sopenharmony_ci	  It inserts the stackleak_track_stack() call for the functions with
20462306a36Sopenharmony_ci	  a stack frame size greater than or equal to this parameter.
20562306a36Sopenharmony_ci	  If unsure, leave the default value 100.
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ciconfig STACKLEAK_METRICS
20862306a36Sopenharmony_ci	bool "Show STACKLEAK metrics in the /proc file system"
20962306a36Sopenharmony_ci	depends on GCC_PLUGIN_STACKLEAK
21062306a36Sopenharmony_ci	depends on PROC_FS
21162306a36Sopenharmony_ci	help
21262306a36Sopenharmony_ci	  If this is set, STACKLEAK metrics for every task are available in
21362306a36Sopenharmony_ci	  the /proc file system. In particular, /proc/<pid>/stack_depth
21462306a36Sopenharmony_ci	  shows the maximum kernel stack consumption for the current and
21562306a36Sopenharmony_ci	  previous syscalls. Although this information is not precise, it
21662306a36Sopenharmony_ci	  can be useful for estimating the STACKLEAK performance impact for
21762306a36Sopenharmony_ci	  your workloads.
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ciconfig STACKLEAK_RUNTIME_DISABLE
22062306a36Sopenharmony_ci	bool "Allow runtime disabling of kernel stack erasing"
22162306a36Sopenharmony_ci	depends on GCC_PLUGIN_STACKLEAK
22262306a36Sopenharmony_ci	help
22362306a36Sopenharmony_ci	  This option provides 'stack_erasing' sysctl, which can be used in
22462306a36Sopenharmony_ci	  runtime to control kernel stack erasing for kernels built with
22562306a36Sopenharmony_ci	  CONFIG_GCC_PLUGIN_STACKLEAK.
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ciconfig INIT_ON_ALLOC_DEFAULT_ON
22862306a36Sopenharmony_ci	bool "Enable heap memory zeroing on allocation by default"
22962306a36Sopenharmony_ci	depends on !KMSAN
23062306a36Sopenharmony_ci	help
23162306a36Sopenharmony_ci	  This has the effect of setting "init_on_alloc=1" on the kernel
23262306a36Sopenharmony_ci	  command line. This can be disabled with "init_on_alloc=0".
23362306a36Sopenharmony_ci	  When "init_on_alloc" is enabled, all page allocator and slab
23462306a36Sopenharmony_ci	  allocator memory will be zeroed when allocated, eliminating
23562306a36Sopenharmony_ci	  many kinds of "uninitialized heap memory" flaws, especially
23662306a36Sopenharmony_ci	  heap content exposures. The performance impact varies by
23762306a36Sopenharmony_ci	  workload, but most cases see <1% impact. Some synthetic
23862306a36Sopenharmony_ci	  workloads have measured as high as 7%.
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ciconfig INIT_ON_FREE_DEFAULT_ON
24162306a36Sopenharmony_ci	bool "Enable heap memory zeroing on free by default"
24262306a36Sopenharmony_ci	depends on !KMSAN
24362306a36Sopenharmony_ci	help
24462306a36Sopenharmony_ci	  This has the effect of setting "init_on_free=1" on the kernel
24562306a36Sopenharmony_ci	  command line. This can be disabled with "init_on_free=0".
24662306a36Sopenharmony_ci	  Similar to "init_on_alloc", when "init_on_free" is enabled,
24762306a36Sopenharmony_ci	  all page allocator and slab allocator memory will be zeroed
24862306a36Sopenharmony_ci	  when freed, eliminating many kinds of "uninitialized heap memory"
24962306a36Sopenharmony_ci	  flaws, especially heap content exposures. The primary difference
25062306a36Sopenharmony_ci	  with "init_on_free" is that data lifetime in memory is reduced,
25162306a36Sopenharmony_ci	  as anything freed is wiped immediately, making live forensics or
25262306a36Sopenharmony_ci	  cold boot memory attacks unable to recover freed memory contents.
25362306a36Sopenharmony_ci	  The performance impact varies by workload, but is more expensive
25462306a36Sopenharmony_ci	  than "init_on_alloc" due to the negative cache effects of
25562306a36Sopenharmony_ci	  touching "cold" memory areas. Most cases see 3-5% impact. Some
25662306a36Sopenharmony_ci	  synthetic workloads have measured as high as 8%.
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ciconfig CC_HAS_ZERO_CALL_USED_REGS
25962306a36Sopenharmony_ci	def_bool $(cc-option,-fzero-call-used-regs=used-gpr)
26062306a36Sopenharmony_ci	# https://github.com/ClangBuiltLinux/linux/issues/1766
26162306a36Sopenharmony_ci	# https://github.com/llvm/llvm-project/issues/59242
26262306a36Sopenharmony_ci	depends on !CC_IS_CLANG || CLANG_VERSION > 150006
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ciconfig ZERO_CALL_USED_REGS
26562306a36Sopenharmony_ci	bool "Enable register zeroing on function exit"
26662306a36Sopenharmony_ci	depends on CC_HAS_ZERO_CALL_USED_REGS
26762306a36Sopenharmony_ci	help
26862306a36Sopenharmony_ci	  At the end of functions, always zero any caller-used register
26962306a36Sopenharmony_ci	  contents. This helps ensure that temporary values are not
27062306a36Sopenharmony_ci	  leaked beyond the function boundary. This means that register
27162306a36Sopenharmony_ci	  contents are less likely to be available for side channels
27262306a36Sopenharmony_ci	  and information exposures. Additionally, this helps reduce the
27362306a36Sopenharmony_ci	  number of useful ROP gadgets by about 20% (and removes compiler
27462306a36Sopenharmony_ci	  generated "write-what-where" gadgets) in the resulting kernel
27562306a36Sopenharmony_ci	  image. This has a less than 1% performance impact on most
27662306a36Sopenharmony_ci	  workloads. Image size growth depends on architecture, and should
27762306a36Sopenharmony_ci	  be evaluated for suitability. For example, x86_64 grows by less
27862306a36Sopenharmony_ci	  than 1%, and arm64 grows by about 5%.
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ciendmenu
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_cimenu "Hardening of kernel data structures"
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ciconfig LIST_HARDENED
28562306a36Sopenharmony_ci	bool "Check integrity of linked list manipulation"
28662306a36Sopenharmony_ci	help
28762306a36Sopenharmony_ci	  Minimal integrity checking in the linked-list manipulation routines
28862306a36Sopenharmony_ci	  to catch memory corruptions that are not guaranteed to result in an
28962306a36Sopenharmony_ci	  immediate access fault.
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	  If unsure, say N.
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ciconfig BUG_ON_DATA_CORRUPTION
29462306a36Sopenharmony_ci	bool "Trigger a BUG when data corruption is detected"
29562306a36Sopenharmony_ci	select LIST_HARDENED
29662306a36Sopenharmony_ci	help
29762306a36Sopenharmony_ci	  Select this option if the kernel should BUG when it encounters
29862306a36Sopenharmony_ci	  data corruption in kernel memory structures when they get checked
29962306a36Sopenharmony_ci	  for validity.
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	  If unsure, say N.
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ciendmenu
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ciconfig CC_HAS_RANDSTRUCT
30662306a36Sopenharmony_ci	def_bool $(cc-option,-frandomize-layout-seed-file=/dev/null)
30762306a36Sopenharmony_ci	# Randstruct was first added in Clang 15, but it isn't safe to use until
30862306a36Sopenharmony_ci	# Clang 16 due to https://github.com/llvm/llvm-project/issues/60349
30962306a36Sopenharmony_ci	depends on !CC_IS_CLANG || CLANG_VERSION >= 160000
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_cichoice
31262306a36Sopenharmony_ci	prompt "Randomize layout of sensitive kernel structures"
31362306a36Sopenharmony_ci	default RANDSTRUCT_FULL if COMPILE_TEST && (GCC_PLUGINS || CC_HAS_RANDSTRUCT)
31462306a36Sopenharmony_ci	default RANDSTRUCT_NONE
31562306a36Sopenharmony_ci	help
31662306a36Sopenharmony_ci	  If you enable this, the layouts of structures that are entirely
31762306a36Sopenharmony_ci	  function pointers (and have not been manually annotated with
31862306a36Sopenharmony_ci	  __no_randomize_layout), or structures that have been explicitly
31962306a36Sopenharmony_ci	  marked with __randomize_layout, will be randomized at compile-time.
32062306a36Sopenharmony_ci	  This can introduce the requirement of an additional information
32162306a36Sopenharmony_ci	  exposure vulnerability for exploits targeting these structure
32262306a36Sopenharmony_ci	  types.
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	  Enabling this feature will introduce some performance impact,
32562306a36Sopenharmony_ci	  slightly increase memory usage, and prevent the use of forensic
32662306a36Sopenharmony_ci	  tools like Volatility against the system (unless the kernel
32762306a36Sopenharmony_ci	  source tree isn't cleaned after kernel installation).
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	  The seed used for compilation is in scripts/basic/randomize.seed.
33062306a36Sopenharmony_ci	  It remains after a "make clean" to allow for external modules to
33162306a36Sopenharmony_ci	  be compiled with the existing seed and will be removed by a
33262306a36Sopenharmony_ci	  "make mrproper" or "make distclean". This file should not be made
33362306a36Sopenharmony_ci	  public, or the structure layout can be determined.
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci	config RANDSTRUCT_NONE
33662306a36Sopenharmony_ci		bool "Disable structure layout randomization"
33762306a36Sopenharmony_ci		help
33862306a36Sopenharmony_ci		  Build normally: no structure layout randomization.
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci	config RANDSTRUCT_FULL
34162306a36Sopenharmony_ci		bool "Fully randomize structure layout"
34262306a36Sopenharmony_ci		depends on CC_HAS_RANDSTRUCT || GCC_PLUGINS
34362306a36Sopenharmony_ci		select MODVERSIONS if MODULES
34462306a36Sopenharmony_ci		help
34562306a36Sopenharmony_ci		  Fully randomize the member layout of sensitive
34662306a36Sopenharmony_ci		  structures as much as possible, which may have both a
34762306a36Sopenharmony_ci		  memory size and performance impact.
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci		  One difference between the Clang and GCC plugin
35062306a36Sopenharmony_ci		  implementations is the handling of bitfields. The GCC
35162306a36Sopenharmony_ci		  plugin treats them as fully separate variables,
35262306a36Sopenharmony_ci		  introducing sometimes significant padding. Clang tries
35362306a36Sopenharmony_ci		  to keep adjacent bitfields together, but with their bit
35462306a36Sopenharmony_ci		  ordering randomized.
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	config RANDSTRUCT_PERFORMANCE
35762306a36Sopenharmony_ci		bool "Limit randomization of structure layout to cache-lines"
35862306a36Sopenharmony_ci		depends on GCC_PLUGINS
35962306a36Sopenharmony_ci		select MODVERSIONS if MODULES
36062306a36Sopenharmony_ci		help
36162306a36Sopenharmony_ci		  Randomization of sensitive kernel structures will make a
36262306a36Sopenharmony_ci		  best effort at restricting randomization to cacheline-sized
36362306a36Sopenharmony_ci		  groups of members. It will further not randomize bitfields
36462306a36Sopenharmony_ci		  in structures. This reduces the performance hit of RANDSTRUCT
36562306a36Sopenharmony_ci		  at the cost of weakened randomization.
36662306a36Sopenharmony_ciendchoice
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ciconfig RANDSTRUCT
36962306a36Sopenharmony_ci	def_bool !RANDSTRUCT_NONE
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ciconfig GCC_PLUGIN_RANDSTRUCT
37262306a36Sopenharmony_ci	def_bool GCC_PLUGINS && RANDSTRUCT
37362306a36Sopenharmony_ci	help
37462306a36Sopenharmony_ci	  Use GCC plugin to randomize structure layout.
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci	  This plugin was ported from grsecurity/PaX. More
37762306a36Sopenharmony_ci	  information at:
37862306a36Sopenharmony_ci	   * https://grsecurity.net/
37962306a36Sopenharmony_ci	   * https://pax.grsecurity.net/
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ciendmenu
382