Kconfig.kasan 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 CC_HAS_KASAN_GENERIC
  17. def_bool $(cc-option, -fsanitize=kernel-address)
  18. config CC_HAS_KASAN_SW_TAGS
  19. def_bool $(cc-option, -fsanitize=kernel-hwaddress)
  20. # This option is only required for software KASAN modes.
  21. # Old GCC versions do not have proper support for no_sanitize_address.
  22. # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89124 for details.
  23. config CC_HAS_WORKING_NOSANITIZE_ADDRESS
  24. def_bool !CC_IS_GCC || GCC_VERSION >= 80300
  25. menuconfig KASAN
  26. bool "KASAN: dynamic memory safety error detector"
  27. depends on (((HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC) || \
  28. (HAVE_ARCH_KASAN_SW_TAGS && CC_HAS_KASAN_SW_TAGS)) && \
  29. CC_HAS_WORKING_NOSANITIZE_ADDRESS) || \
  30. HAVE_ARCH_KASAN_HW_TAGS
  31. depends on (SLUB && SYSFS) || (SLAB && !DEBUG_SLAB)
  32. select STACKDEPOT_ALWAYS_INIT
  33. help
  34. Enables KASAN (Kernel Address Sanitizer) - a dynamic memory safety
  35. error detector designed to find out-of-bounds and use-after-free bugs.
  36. See Documentation/dev-tools/kasan.rst for details.
  37. For better error reports, also enable CONFIG_STACKTRACE.
  38. if KASAN
  39. choice
  40. prompt "KASAN mode"
  41. default KASAN_GENERIC
  42. help
  43. KASAN has three modes:
  44. 1. Generic KASAN (supported by many architectures, enabled with
  45. CONFIG_KASAN_GENERIC, similar to userspace ASan),
  46. 2. Software Tag-Based KASAN (arm64 only, based on software memory
  47. tagging, enabled with CONFIG_KASAN_SW_TAGS, similar to userspace
  48. HWASan), and
  49. 3. Hardware Tag-Based KASAN (arm64 only, based on hardware memory
  50. tagging, enabled with CONFIG_KASAN_HW_TAGS).
  51. See Documentation/dev-tools/kasan.rst for details about each mode.
  52. config KASAN_GENERIC
  53. bool "Generic KASAN"
  54. depends on HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC
  55. depends on CC_HAS_WORKING_NOSANITIZE_ADDRESS
  56. select SLUB_DEBUG if SLUB
  57. select CONSTRUCTORS
  58. help
  59. Enables Generic KASAN.
  60. Requires GCC 8.3.0+ or Clang.
  61. Consumes about 1/8th of available memory at kernel start and adds an
  62. overhead of ~50% for dynamic allocations.
  63. The performance slowdown is ~x3.
  64. (Incompatible with CONFIG_DEBUG_SLAB: the kernel does not boot.)
  65. config KASAN_SW_TAGS
  66. bool "Software Tag-Based KASAN"
  67. depends on HAVE_ARCH_KASAN_SW_TAGS && CC_HAS_KASAN_SW_TAGS
  68. depends on CC_HAS_WORKING_NOSANITIZE_ADDRESS
  69. select SLUB_DEBUG if SLUB
  70. select CONSTRUCTORS
  71. help
  72. Enables Software Tag-Based KASAN.
  73. Requires GCC 11+ or Clang.
  74. Supported only on arm64 CPUs and relies on Top Byte Ignore.
  75. Consumes about 1/16th of available memory at kernel start and
  76. add an overhead of ~20% for dynamic allocations.
  77. May potentially introduce problems related to pointer casting and
  78. comparison, as it embeds a tag into the top byte of each pointer.
  79. (Incompatible with CONFIG_DEBUG_SLAB: the kernel does not boot.)
  80. config KASAN_HW_TAGS
  81. bool "Hardware Tag-Based KASAN"
  82. depends on HAVE_ARCH_KASAN_HW_TAGS
  83. depends on SLUB
  84. help
  85. Enables Hardware Tag-Based KASAN.
  86. Requires GCC 10+ or Clang 12+.
  87. Supported only on arm64 CPUs starting from ARMv8.5 and relies on
  88. Memory Tagging Extension and Top Byte Ignore.
  89. Consumes about 1/32nd of available memory.
  90. May potentially introduce problems related to pointer casting and
  91. comparison, as it embeds a tag into the top byte of each pointer.
  92. endchoice
  93. choice
  94. prompt "Instrumentation type"
  95. depends on KASAN_GENERIC || KASAN_SW_TAGS
  96. default KASAN_OUTLINE
  97. config KASAN_OUTLINE
  98. bool "Outline instrumentation"
  99. help
  100. Makes the compiler insert function calls that check whether the memory
  101. is accessible before each memory access. Slower than KASAN_INLINE, but
  102. does not bloat the size of the kernel's .text section so much.
  103. config KASAN_INLINE
  104. bool "Inline instrumentation"
  105. depends on !ARCH_DISABLE_KASAN_INLINE
  106. help
  107. Makes the compiler directly insert memory accessibility checks before
  108. each memory access. Faster than KASAN_OUTLINE (gives ~x2 boost for
  109. some workloads), but makes the kernel's .text size much bigger.
  110. endchoice
  111. config KASAN_STACK
  112. bool "Stack instrumentation (unsafe)" if CC_IS_CLANG && !COMPILE_TEST
  113. depends on KASAN_GENERIC || KASAN_SW_TAGS
  114. depends on !ARCH_DISABLE_KASAN_INLINE
  115. default y if CC_IS_GCC
  116. help
  117. Disables stack instrumentation and thus KASAN's ability to detect
  118. out-of-bounds bugs in stack variables.
  119. With Clang, stack instrumentation has a problem that causes excessive
  120. stack usage, see https://bugs.llvm.org/show_bug.cgi?id=38809. Thus,
  121. with Clang, this option is deemed unsafe.
  122. This option is always disabled when compile-testing with Clang to
  123. avoid cluttering the log with stack overflow warnings.
  124. With GCC, enabling stack instrumentation is assumed to be safe.
  125. If the architecture disables inline instrumentation via
  126. ARCH_DISABLE_KASAN_INLINE, stack instrumentation gets disabled
  127. as well, as it adds inline-style instrumentation that is run
  128. unconditionally.
  129. config KASAN_VMALLOC
  130. bool "Check accesses to vmalloc allocations"
  131. depends on HAVE_ARCH_KASAN_VMALLOC
  132. help
  133. Makes KASAN check the validity of accesses to vmalloc allocations.
  134. With software KASAN modes, all types vmalloc allocations are
  135. checked. Enabling this option leads to higher memory usage.
  136. With Hardware Tag-Based KASAN, only non-executable VM_ALLOC mappings
  137. are checked. There is no additional memory usage.
  138. config KASAN_KUNIT_TEST
  139. tristate "KUnit-compatible tests of KASAN bug detection capabilities" if !KUNIT_ALL_TESTS
  140. depends on KASAN && KUNIT
  141. default KUNIT_ALL_TESTS
  142. help
  143. A KUnit-based KASAN test suite. Triggers different kinds of
  144. out-of-bounds and use-after-free accesses. Useful for testing whether
  145. KASAN can detect certain bug types.
  146. For more information on KUnit and unit tests in general, please refer
  147. to the KUnit documentation in Documentation/dev-tools/kunit/.
  148. config KASAN_MODULE_TEST
  149. tristate "KUnit-incompatible tests of KASAN bug detection capabilities"
  150. depends on m && KASAN && !KASAN_HW_TAGS
  151. help
  152. A part of the KASAN test suite that is not integrated with KUnit.
  153. Incompatible with Hardware Tag-Based KASAN.
  154. endif # KASAN