Kconfig.kasan 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. # This config refers to the generic KASAN mode.
  3. config HAVE_ARCH_KASAN
  4. bool
  5. config HAVE_ARCH_KASAN_SW_TAGS
  6. bool
  7. config HAVE_ARCH_KASAN_HW_TAGS
  8. bool
  9. config HAVE_ARCH_KASAN_VMALLOC
  10. bool
  11. config ARCH_DISABLE_KASAN_INLINE
  12. bool
  13. help
  14. Disables both inline and stack instrumentation. Selected by
  15. architectures that do not support these instrumentation types.
  16. config ARCH_NEEDS_DEFER_KASAN
  17. bool
  18. config ARCH_DEFER_KASAN
  19. def_bool y
  20. depends on KASAN && ARCH_NEEDS_DEFER_KASAN
  21. help
  22. Architectures should select this if they need to defer KASAN
  23. initialization until shadow memory is properly set up. This
  24. enables runtime control via static keys. Otherwise, KASAN uses
  25. compile-time constants for better performance.
  26. config CC_HAS_KASAN_GENERIC
  27. def_bool $(cc-option, -fsanitize=kernel-address)
  28. config CC_HAS_KASAN_SW_TAGS
  29. def_bool $(cc-option, -fsanitize=kernel-hwaddress)
  30. # This option is only required for software KASAN modes.
  31. # Old GCC versions do not have proper support for no_sanitize_address.
  32. # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89124 for details.
  33. config CC_HAS_WORKING_NOSANITIZE_ADDRESS
  34. def_bool !CC_IS_GCC || GCC_VERSION >= 80300
  35. menuconfig KASAN
  36. bool "KASAN: dynamic memory safety error detector"
  37. depends on (((HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC) || \
  38. (HAVE_ARCH_KASAN_SW_TAGS && CC_HAS_KASAN_SW_TAGS)) && \
  39. CC_HAS_WORKING_NOSANITIZE_ADDRESS) || \
  40. HAVE_ARCH_KASAN_HW_TAGS
  41. depends on SYSFS && !SLUB_TINY
  42. select STACKDEPOT_ALWAYS_INIT
  43. help
  44. Enables KASAN (Kernel Address Sanitizer) - a dynamic memory safety
  45. error detector designed to find out-of-bounds and use-after-free bugs.
  46. See Documentation/dev-tools/kasan.rst for details.
  47. For better error reports, also enable CONFIG_STACKTRACE.
  48. if KASAN
  49. config CC_HAS_KASAN_MEMINTRINSIC_PREFIX
  50. def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=kernel-address -mllvm -asan-kernel-mem-intrinsic-prefix=1)) || \
  51. (CC_IS_GCC && $(cc-option,-fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1))
  52. # Don't define it if we don't need it: compilation of the test uses
  53. # this variable to decide how the compiler should treat builtins.
  54. depends on !KASAN_HW_TAGS
  55. help
  56. The compiler is able to prefix memintrinsics with __asan or __hwasan.
  57. choice
  58. prompt "KASAN mode"
  59. default KASAN_GENERIC
  60. help
  61. KASAN has three modes:
  62. 1. Generic KASAN (supported by many architectures, enabled with
  63. CONFIG_KASAN_GENERIC, similar to userspace ASan),
  64. 2. Software Tag-Based KASAN (arm64 only, based on software memory
  65. tagging, enabled with CONFIG_KASAN_SW_TAGS, similar to userspace
  66. HWASan), and
  67. 3. Hardware Tag-Based KASAN (arm64 only, based on hardware memory
  68. tagging, enabled with CONFIG_KASAN_HW_TAGS).
  69. See Documentation/dev-tools/kasan.rst for details about each mode.
  70. config KASAN_GENERIC
  71. bool "Generic KASAN"
  72. depends on HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC
  73. depends on CC_HAS_WORKING_NOSANITIZE_ADDRESS
  74. select SLUB_DEBUG
  75. select CONSTRUCTORS
  76. help
  77. Enables Generic KASAN.
  78. Requires GCC 8.3.0+ or Clang.
  79. Consumes about 1/8th of available memory at kernel start and adds an
  80. overhead of ~50% for dynamic allocations.
  81. The performance slowdown is ~x3.
  82. config KASAN_SW_TAGS
  83. bool "Software Tag-Based KASAN"
  84. depends on HAVE_ARCH_KASAN_SW_TAGS && CC_HAS_KASAN_SW_TAGS
  85. depends on CC_HAS_WORKING_NOSANITIZE_ADDRESS
  86. select SLUB_DEBUG
  87. select CONSTRUCTORS
  88. help
  89. Enables Software Tag-Based KASAN.
  90. Requires GCC 11+ or Clang.
  91. Supported only on arm64 CPUs and relies on Top Byte Ignore.
  92. Consumes about 1/16th of available memory at kernel start and
  93. add an overhead of ~20% for dynamic allocations.
  94. May potentially introduce problems related to pointer casting and
  95. comparison, as it embeds a tag into the top byte of each pointer.
  96. config KASAN_HW_TAGS
  97. bool "Hardware Tag-Based KASAN"
  98. depends on HAVE_ARCH_KASAN_HW_TAGS
  99. help
  100. Enables Hardware Tag-Based KASAN.
  101. Requires GCC 10+ or Clang 12+.
  102. Supported only on arm64 CPUs starting from ARMv8.5 and relies on
  103. Memory Tagging Extension and Top Byte Ignore.
  104. Consumes about 1/32nd of available memory.
  105. May potentially introduce problems related to pointer casting and
  106. comparison, as it embeds a tag into the top byte of each pointer.
  107. endchoice
  108. choice
  109. prompt "Instrumentation type"
  110. depends on KASAN_GENERIC || KASAN_SW_TAGS
  111. default KASAN_INLINE if !ARCH_DISABLE_KASAN_INLINE
  112. config KASAN_OUTLINE
  113. bool "Outline instrumentation"
  114. help
  115. Makes the compiler insert function calls that check whether the memory
  116. is accessible before each memory access. Slower than KASAN_INLINE, but
  117. does not bloat the size of the kernel's .text section so much.
  118. config KASAN_INLINE
  119. bool "Inline instrumentation"
  120. depends on !ARCH_DISABLE_KASAN_INLINE
  121. help
  122. Makes the compiler directly insert memory accessibility checks before
  123. each memory access. Faster than KASAN_OUTLINE (gives ~x2 boost for
  124. some workloads), but makes the kernel's .text size much bigger.
  125. endchoice
  126. config KASAN_STACK
  127. bool "Stack instrumentation (unsafe)" if CC_IS_CLANG && !COMPILE_TEST
  128. depends on KASAN_GENERIC || KASAN_SW_TAGS
  129. depends on !ARCH_DISABLE_KASAN_INLINE
  130. default y if CC_IS_GCC
  131. help
  132. Disables stack instrumentation and thus KASAN's ability to detect
  133. out-of-bounds bugs in stack variables.
  134. With Clang, stack instrumentation has a problem that causes excessive
  135. stack usage, see https://llvm.org/pr38809. Thus,
  136. with Clang, this option is deemed unsafe.
  137. This option is always disabled when compile-testing with Clang to
  138. avoid cluttering the log with stack overflow warnings.
  139. With GCC, enabling stack instrumentation is assumed to be safe.
  140. If the architecture disables inline instrumentation via
  141. ARCH_DISABLE_KASAN_INLINE, stack instrumentation gets disabled
  142. as well, as it adds inline-style instrumentation that is run
  143. unconditionally.
  144. config KASAN_VMALLOC
  145. bool "Check accesses to vmalloc allocations"
  146. depends on HAVE_ARCH_KASAN_VMALLOC
  147. help
  148. Makes KASAN check the validity of accesses to vmalloc allocations.
  149. With software KASAN modes, all types vmalloc allocations are
  150. checked. Enabling this option leads to higher memory usage.
  151. With Hardware Tag-Based KASAN, only non-executable VM_ALLOC mappings
  152. are checked. There is no additional memory usage.
  153. config KASAN_KUNIT_TEST
  154. tristate "KUnit-compatible tests of KASAN bug detection capabilities" if !KUNIT_ALL_TESTS
  155. depends on KASAN && KUNIT && TRACEPOINTS
  156. default KUNIT_ALL_TESTS
  157. help
  158. A KUnit-based KASAN test suite. Triggers different kinds of
  159. out-of-bounds and use-after-free accesses. Useful for testing whether
  160. KASAN can detect certain bug types.
  161. For more information on KUnit and unit tests in general, please refer
  162. to the KUnit documentation in Documentation/dev-tools/kunit/.
  163. config KASAN_EXTRA_INFO
  164. bool "Record and report more information"
  165. depends on KASAN
  166. help
  167. Record and report more information to help us find the cause of the
  168. bug and to help us correlate the error with other system events.
  169. Currently, the CPU number and timestamp are additionally
  170. recorded for each heap block at allocation and free time, and
  171. 8 bytes will be added to each metadata structure that records
  172. allocation or free information.
  173. In Generic KASAN, each kmalloc-8 and kmalloc-16 object will add
  174. 16 bytes of additional memory consumption, and each kmalloc-32
  175. object will add 8 bytes of additional memory consumption, not
  176. affecting other larger objects.
  177. In SW_TAGS KASAN and HW_TAGS KASAN, depending on the stack_ring_size
  178. boot parameter, it will add 8 * stack_ring_size bytes of additional
  179. memory consumption.
  180. endif # KASAN