Kconfig.hardening 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. menu "Kernel hardening options"
  3. config GCC_PLUGIN_STRUCTLEAK
  4. bool
  5. help
  6. While the kernel is built with warnings enabled for any missed
  7. stack variable initializations, this warning is silenced for
  8. anything passed by reference to another function, under the
  9. occasionally misguided assumption that the function will do
  10. the initialization. As this regularly leads to exploitable
  11. flaws, this plugin is available to identify and zero-initialize
  12. such variables, depending on the chosen level of coverage.
  13. This plugin was originally ported from grsecurity/PaX. More
  14. information at:
  15. * https://grsecurity.net/
  16. * https://pax.grsecurity.net/
  17. menu "Memory initialization"
  18. config CC_HAS_AUTO_VAR_INIT_PATTERN
  19. def_bool $(cc-option,-ftrivial-auto-var-init=pattern)
  20. config CC_HAS_AUTO_VAR_INIT_ZERO_BARE
  21. def_bool $(cc-option,-ftrivial-auto-var-init=zero)
  22. config CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER
  23. # Clang 16 and later warn about using the -enable flag, but it
  24. # is required before then.
  25. def_bool $(cc-option,-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang)
  26. depends on !CC_HAS_AUTO_VAR_INIT_ZERO_BARE
  27. config CC_HAS_AUTO_VAR_INIT_ZERO
  28. def_bool CC_HAS_AUTO_VAR_INIT_ZERO_BARE || CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER
  29. choice
  30. prompt "Initialize kernel stack variables at function entry"
  31. default GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if COMPILE_TEST && GCC_PLUGINS
  32. default INIT_STACK_ALL_PATTERN if COMPILE_TEST && CC_HAS_AUTO_VAR_INIT_PATTERN
  33. default INIT_STACK_ALL_ZERO if CC_HAS_AUTO_VAR_INIT_ZERO
  34. default INIT_STACK_NONE
  35. help
  36. This option enables initialization of stack variables at
  37. function entry time. This has the possibility to have the
  38. greatest coverage (since all functions can have their
  39. variables initialized), but the performance impact depends
  40. on the function calling complexity of a given workload's
  41. syscalls.
  42. This chooses the level of coverage over classes of potentially
  43. uninitialized variables. The selected class of variable will be
  44. initialized before use in a function.
  45. config INIT_STACK_NONE
  46. bool "no automatic stack variable initialization (weakest)"
  47. help
  48. Disable automatic stack variable initialization.
  49. This leaves the kernel vulnerable to the standard
  50. classes of uninitialized stack variable exploits
  51. and information exposures.
  52. config GCC_PLUGIN_STRUCTLEAK_USER
  53. bool "zero-init structs marked for userspace (weak)"
  54. # Plugin can be removed once the kernel only supports GCC 12+
  55. depends on GCC_PLUGINS && !CC_HAS_AUTO_VAR_INIT_ZERO
  56. select GCC_PLUGIN_STRUCTLEAK
  57. help
  58. Zero-initialize any structures on the stack containing
  59. a __user attribute. This can prevent some classes of
  60. uninitialized stack variable exploits and information
  61. exposures, like CVE-2013-2141:
  62. https://git.kernel.org/linus/b9e146d8eb3b9eca
  63. config GCC_PLUGIN_STRUCTLEAK_BYREF
  64. bool "zero-init structs passed by reference (strong)"
  65. # Plugin can be removed once the kernel only supports GCC 12+
  66. depends on GCC_PLUGINS && !CC_HAS_AUTO_VAR_INIT_ZERO
  67. depends on !(KASAN && KASAN_STACK)
  68. select GCC_PLUGIN_STRUCTLEAK
  69. help
  70. Zero-initialize any structures on the stack that may
  71. be passed by reference and had not already been
  72. explicitly initialized. This can prevent most classes
  73. of uninitialized stack variable exploits and information
  74. exposures, like CVE-2017-1000410:
  75. https://git.kernel.org/linus/06e7e776ca4d3654
  76. As a side-effect, this keeps a lot of variables on the
  77. stack that can otherwise be optimized out, so combining
  78. this with CONFIG_KASAN_STACK can lead to a stack overflow
  79. and is disallowed.
  80. config GCC_PLUGIN_STRUCTLEAK_BYREF_ALL
  81. bool "zero-init everything passed by reference (very strong)"
  82. # Plugin can be removed once the kernel only supports GCC 12+
  83. depends on GCC_PLUGINS && !CC_HAS_AUTO_VAR_INIT_ZERO
  84. depends on !(KASAN && KASAN_STACK)
  85. select GCC_PLUGIN_STRUCTLEAK
  86. help
  87. Zero-initialize any stack variables that may be passed
  88. by reference and had not already been explicitly
  89. initialized. This is intended to eliminate all classes
  90. of uninitialized stack variable exploits and information
  91. exposures.
  92. As a side-effect, this keeps a lot of variables on the
  93. stack that can otherwise be optimized out, so combining
  94. this with CONFIG_KASAN_STACK can lead to a stack overflow
  95. and is disallowed.
  96. config INIT_STACK_ALL_PATTERN
  97. bool "pattern-init everything (strongest)"
  98. depends on CC_HAS_AUTO_VAR_INIT_PATTERN
  99. depends on !KMSAN
  100. help
  101. Initializes everything on the stack (including padding)
  102. with a specific debug value. This is intended to eliminate
  103. all classes of uninitialized stack variable exploits and
  104. information exposures, even variables that were warned about
  105. having been left uninitialized.
  106. Pattern initialization is known to provoke many existing bugs
  107. related to uninitialized locals, e.g. pointers receive
  108. non-NULL values, buffer sizes and indices are very big. The
  109. pattern is situation-specific; Clang on 64-bit uses 0xAA
  110. repeating for all types and padding except float and double
  111. which use 0xFF repeating (-NaN). Clang on 32-bit uses 0xFF
  112. repeating for all types and padding.
  113. config INIT_STACK_ALL_ZERO
  114. bool "zero-init everything (strongest and safest)"
  115. depends on CC_HAS_AUTO_VAR_INIT_ZERO
  116. depends on !KMSAN
  117. help
  118. Initializes everything on the stack (including padding)
  119. with a zero value. This is intended to eliminate all
  120. classes of uninitialized stack variable exploits and
  121. information exposures, even variables that were warned
  122. about having been left uninitialized.
  123. Zero initialization provides safe defaults for strings
  124. (immediately NUL-terminated), pointers (NULL), indices
  125. (index 0), and sizes (0 length), so it is therefore more
  126. suitable as a production security mitigation than pattern
  127. initialization.
  128. endchoice
  129. config GCC_PLUGIN_STRUCTLEAK_VERBOSE
  130. bool "Report forcefully initialized variables"
  131. depends on GCC_PLUGIN_STRUCTLEAK
  132. depends on !COMPILE_TEST # too noisy
  133. help
  134. This option will cause a warning to be printed each time the
  135. structleak plugin finds a variable it thinks needs to be
  136. initialized. Since not all existing initializers are detected
  137. by the plugin, this can produce false positive warnings.
  138. config GCC_PLUGIN_STACKLEAK
  139. bool "Poison kernel stack before returning from syscalls"
  140. depends on GCC_PLUGINS
  141. depends on HAVE_ARCH_STACKLEAK
  142. help
  143. This option makes the kernel erase the kernel stack before
  144. returning from system calls. This has the effect of leaving
  145. the stack initialized to the poison value, which both reduces
  146. the lifetime of any sensitive stack contents and reduces
  147. potential for uninitialized stack variable exploits or information
  148. exposures (it does not cover functions reaching the same stack
  149. depth as prior functions during the same syscall). This blocks
  150. most uninitialized stack variable attacks, with the performance
  151. impact being driven by the depth of the stack usage, rather than
  152. the function calling complexity.
  153. The performance impact on a single CPU system kernel compilation
  154. sees a 1% slowdown, other systems and workloads may vary and you
  155. are advised to test this feature on your expected workload before
  156. deploying it.
  157. This plugin was ported from grsecurity/PaX. More information at:
  158. * https://grsecurity.net/
  159. * https://pax.grsecurity.net/
  160. config GCC_PLUGIN_STACKLEAK_VERBOSE
  161. bool "Report stack depth analysis instrumentation" if EXPERT
  162. depends on GCC_PLUGIN_STACKLEAK
  163. depends on !COMPILE_TEST # too noisy
  164. help
  165. This option will cause a warning to be printed each time the
  166. stackleak plugin finds a function it thinks needs to be
  167. instrumented. This is useful for comparing coverage between
  168. builds.
  169. config STACKLEAK_TRACK_MIN_SIZE
  170. int "Minimum stack frame size of functions tracked by STACKLEAK"
  171. default 100
  172. range 0 4096
  173. depends on GCC_PLUGIN_STACKLEAK
  174. help
  175. The STACKLEAK gcc plugin instruments the kernel code for tracking
  176. the lowest border of the kernel stack (and for some other purposes).
  177. It inserts the stackleak_track_stack() call for the functions with
  178. a stack frame size greater than or equal to this parameter.
  179. If unsure, leave the default value 100.
  180. config STACKLEAK_METRICS
  181. bool "Show STACKLEAK metrics in the /proc file system"
  182. depends on GCC_PLUGIN_STACKLEAK
  183. depends on PROC_FS
  184. help
  185. If this is set, STACKLEAK metrics for every task are available in
  186. the /proc file system. In particular, /proc/<pid>/stack_depth
  187. shows the maximum kernel stack consumption for the current and
  188. previous syscalls. Although this information is not precise, it
  189. can be useful for estimating the STACKLEAK performance impact for
  190. your workloads.
  191. config STACKLEAK_RUNTIME_DISABLE
  192. bool "Allow runtime disabling of kernel stack erasing"
  193. depends on GCC_PLUGIN_STACKLEAK
  194. help
  195. This option provides 'stack_erasing' sysctl, which can be used in
  196. runtime to control kernel stack erasing for kernels built with
  197. CONFIG_GCC_PLUGIN_STACKLEAK.
  198. config INIT_ON_ALLOC_DEFAULT_ON
  199. bool "Enable heap memory zeroing on allocation by default"
  200. depends on !KMSAN
  201. help
  202. This has the effect of setting "init_on_alloc=1" on the kernel
  203. command line. This can be disabled with "init_on_alloc=0".
  204. When "init_on_alloc" is enabled, all page allocator and slab
  205. allocator memory will be zeroed when allocated, eliminating
  206. many kinds of "uninitialized heap memory" flaws, especially
  207. heap content exposures. The performance impact varies by
  208. workload, but most cases see <1% impact. Some synthetic
  209. workloads have measured as high as 7%.
  210. config INIT_ON_FREE_DEFAULT_ON
  211. bool "Enable heap memory zeroing on free by default"
  212. depends on !KMSAN
  213. help
  214. This has the effect of setting "init_on_free=1" on the kernel
  215. command line. This can be disabled with "init_on_free=0".
  216. Similar to "init_on_alloc", when "init_on_free" is enabled,
  217. all page allocator and slab allocator memory will be zeroed
  218. when freed, eliminating many kinds of "uninitialized heap memory"
  219. flaws, especially heap content exposures. The primary difference
  220. with "init_on_free" is that data lifetime in memory is reduced,
  221. as anything freed is wiped immediately, making live forensics or
  222. cold boot memory attacks unable to recover freed memory contents.
  223. The performance impact varies by workload, but is more expensive
  224. than "init_on_alloc" due to the negative cache effects of
  225. touching "cold" memory areas. Most cases see 3-5% impact. Some
  226. synthetic workloads have measured as high as 8%.
  227. config CC_HAS_ZERO_CALL_USED_REGS
  228. def_bool $(cc-option,-fzero-call-used-regs=used-gpr)
  229. # https://github.com/ClangBuiltLinux/linux/issues/1766
  230. # https://github.com/llvm/llvm-project/issues/59242
  231. depends on !CC_IS_CLANG || CLANG_VERSION > 150006
  232. config ZERO_CALL_USED_REGS
  233. bool "Enable register zeroing on function exit"
  234. depends on CC_HAS_ZERO_CALL_USED_REGS
  235. help
  236. At the end of functions, always zero any caller-used register
  237. contents. This helps ensure that temporary values are not
  238. leaked beyond the function boundary. This means that register
  239. contents are less likely to be available for side channels
  240. and information exposures. Additionally, this helps reduce the
  241. number of useful ROP gadgets by about 20% (and removes compiler
  242. generated "write-what-where" gadgets) in the resulting kernel
  243. image. This has a less than 1% performance impact on most
  244. workloads. Image size growth depends on architecture, and should
  245. be evaluated for suitability. For example, x86_64 grows by less
  246. than 1%, and arm64 grows by about 5%.
  247. endmenu
  248. config CC_HAS_RANDSTRUCT
  249. def_bool $(cc-option,-frandomize-layout-seed-file=/dev/null)
  250. # Randstruct was first added in Clang 15, but it isn't safe to use until
  251. # Clang 16 due to https://github.com/llvm/llvm-project/issues/60349
  252. depends on !CC_IS_CLANG || CLANG_VERSION >= 160000
  253. choice
  254. prompt "Randomize layout of sensitive kernel structures"
  255. default RANDSTRUCT_FULL if COMPILE_TEST && (GCC_PLUGINS || CC_HAS_RANDSTRUCT)
  256. default RANDSTRUCT_NONE
  257. help
  258. If you enable this, the layouts of structures that are entirely
  259. function pointers (and have not been manually annotated with
  260. __no_randomize_layout), or structures that have been explicitly
  261. marked with __randomize_layout, will be randomized at compile-time.
  262. This can introduce the requirement of an additional information
  263. exposure vulnerability for exploits targeting these structure
  264. types.
  265. Enabling this feature will introduce some performance impact,
  266. slightly increase memory usage, and prevent the use of forensic
  267. tools like Volatility against the system (unless the kernel
  268. source tree isn't cleaned after kernel installation).
  269. The seed used for compilation is in scripts/basic/randomize.seed.
  270. It remains after a "make clean" to allow for external modules to
  271. be compiled with the existing seed and will be removed by a
  272. "make mrproper" or "make distclean". This file should not be made
  273. public, or the structure layout can be determined.
  274. config RANDSTRUCT_NONE
  275. bool "Disable structure layout randomization"
  276. help
  277. Build normally: no structure layout randomization.
  278. config RANDSTRUCT_FULL
  279. bool "Fully randomize structure layout"
  280. depends on CC_HAS_RANDSTRUCT || GCC_PLUGINS
  281. select MODVERSIONS if MODULES
  282. help
  283. Fully randomize the member layout of sensitive
  284. structures as much as possible, which may have both a
  285. memory size and performance impact.
  286. One difference between the Clang and GCC plugin
  287. implementations is the handling of bitfields. The GCC
  288. plugin treats them as fully separate variables,
  289. introducing sometimes significant padding. Clang tries
  290. to keep adjacent bitfields together, but with their bit
  291. ordering randomized.
  292. config RANDSTRUCT_PERFORMANCE
  293. bool "Limit randomization of structure layout to cache-lines"
  294. depends on GCC_PLUGINS
  295. select MODVERSIONS if MODULES
  296. help
  297. Randomization of sensitive kernel structures will make a
  298. best effort at restricting randomization to cacheline-sized
  299. groups of members. It will further not randomize bitfields
  300. in structures. This reduces the performance hit of RANDSTRUCT
  301. at the cost of weakened randomization.
  302. endchoice
  303. config RANDSTRUCT
  304. def_bool !RANDSTRUCT_NONE
  305. config GCC_PLUGIN_RANDSTRUCT
  306. def_bool GCC_PLUGINS && RANDSTRUCT
  307. help
  308. Use GCC plugin to randomize structure layout.
  309. This plugin was ported from grsecurity/PaX. More
  310. information at:
  311. * https://grsecurity.net/
  312. * https://pax.grsecurity.net/
  313. endmenu