Kconfig.ubsan 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. config ARCH_HAS_UBSAN_SANITIZE_ALL
  3. bool
  4. menuconfig UBSAN
  5. bool "Undefined behaviour sanity checker"
  6. help
  7. This option enables the Undefined Behaviour sanity checker.
  8. Compile-time instrumentation is used to detect various undefined
  9. behaviours at runtime. For more details, see:
  10. Documentation/dev-tools/ubsan.rst
  11. if UBSAN
  12. config UBSAN_TRAP
  13. bool "On Sanitizer warnings, abort the running kernel code"
  14. depends on !COMPILE_TEST
  15. depends on $(cc-option, -fsanitize-undefined-trap-on-error)
  16. help
  17. Building kernels with Sanitizer features enabled tends to grow
  18. the kernel size by around 5%, due to adding all the debugging
  19. text on failure paths. To avoid this, Sanitizer instrumentation
  20. can just issue a trap. This reduces the kernel size overhead but
  21. turns all warnings (including potentially harmless conditions)
  22. into full exceptions that abort the running kernel code
  23. (regardless of context, locks held, etc), which may destabilize
  24. the system. For some system builders this is an acceptable
  25. trade-off.
  26. config CC_HAS_UBSAN_BOUNDS
  27. def_bool $(cc-option,-fsanitize=bounds)
  28. config CC_HAS_UBSAN_ARRAY_BOUNDS
  29. def_bool $(cc-option,-fsanitize=array-bounds)
  30. config UBSAN_BOUNDS
  31. bool "Perform array index bounds checking"
  32. default UBSAN
  33. depends on CC_HAS_UBSAN_ARRAY_BOUNDS || CC_HAS_UBSAN_BOUNDS
  34. help
  35. This option enables detection of directly indexed out of bounds
  36. array accesses, where the array size is known at compile time.
  37. Note that this does not protect array overflows via bad calls
  38. to the {str,mem}*cpy() family of functions (that is addressed
  39. by CONFIG_FORTIFY_SOURCE).
  40. config UBSAN_ONLY_BOUNDS
  41. def_bool CC_HAS_UBSAN_BOUNDS && !CC_HAS_UBSAN_ARRAY_BOUNDS
  42. depends on UBSAN_BOUNDS
  43. help
  44. This is a weird case: Clang's -fsanitize=bounds includes
  45. -fsanitize=local-bounds, but it's trapping-only, so for
  46. Clang, we must use -fsanitize=array-bounds when we want
  47. traditional array bounds checking enabled. For GCC, we
  48. want -fsanitize=bounds.
  49. config UBSAN_ARRAY_BOUNDS
  50. def_bool CC_HAS_UBSAN_ARRAY_BOUNDS
  51. depends on UBSAN_BOUNDS
  52. config UBSAN_LOCAL_BOUNDS
  53. bool "Perform array local bounds checking"
  54. depends on UBSAN_TRAP
  55. depends on $(cc-option,-fsanitize=local-bounds)
  56. help
  57. This option enables -fsanitize=local-bounds which traps when an
  58. exception/error is detected. Therefore, it may only be enabled
  59. with CONFIG_UBSAN_TRAP.
  60. Enabling this option detects errors due to accesses through a
  61. pointer that is derived from an object of a statically-known size,
  62. where an added offset (which may not be known statically) is
  63. out-of-bounds.
  64. config UBSAN_SHIFT
  65. bool "Perform checking for bit-shift overflows"
  66. default UBSAN
  67. depends on $(cc-option,-fsanitize=shift)
  68. help
  69. This option enables -fsanitize=shift which checks for bit-shift
  70. operations that overflow to the left or go switch to negative
  71. for signed types.
  72. config UBSAN_DIV_ZERO
  73. bool "Perform checking for integer divide-by-zero"
  74. depends on $(cc-option,-fsanitize=integer-divide-by-zero)
  75. # https://github.com/ClangBuiltLinux/linux/issues/1657
  76. # https://github.com/llvm/llvm-project/issues/56289
  77. depends on !CC_IS_CLANG
  78. help
  79. This option enables -fsanitize=integer-divide-by-zero which checks
  80. for integer division by zero. This is effectively redundant with the
  81. kernel's existing exception handling, though it can provide greater
  82. debugging information under CONFIG_UBSAN_REPORT_FULL.
  83. config UBSAN_UNREACHABLE
  84. bool "Perform checking for unreachable code"
  85. # objtool already handles unreachable checking and gets angry about
  86. # seeing UBSan instrumentation located in unreachable places.
  87. depends on !(OBJTOOL && (STACK_VALIDATION || UNWINDER_ORC || HAVE_UACCESS_VALIDATION))
  88. depends on $(cc-option,-fsanitize=unreachable)
  89. help
  90. This option enables -fsanitize=unreachable which checks for control
  91. flow reaching an expected-to-be-unreachable position.
  92. config UBSAN_BOOL
  93. bool "Perform checking for non-boolean values used as boolean"
  94. default UBSAN
  95. depends on $(cc-option,-fsanitize=bool)
  96. help
  97. This option enables -fsanitize=bool which checks for boolean values being
  98. loaded that are neither 0 nor 1.
  99. config UBSAN_ENUM
  100. bool "Perform checking for out of bounds enum values"
  101. default UBSAN
  102. depends on $(cc-option,-fsanitize=enum)
  103. help
  104. This option enables -fsanitize=enum which checks for values being loaded
  105. into an enum that are outside the range of given values for the given enum.
  106. config UBSAN_ALIGNMENT
  107. bool "Perform checking for misaligned pointer usage"
  108. default !HAVE_EFFICIENT_UNALIGNED_ACCESS
  109. depends on !UBSAN_TRAP && !COMPILE_TEST
  110. depends on $(cc-option,-fsanitize=alignment)
  111. help
  112. This option enables the check of unaligned memory accesses.
  113. Enabling this option on architectures that support unaligned
  114. accesses may produce a lot of false positives.
  115. config UBSAN_SANITIZE_ALL
  116. bool "Enable instrumentation for the entire kernel"
  117. depends on ARCH_HAS_UBSAN_SANITIZE_ALL
  118. default y
  119. help
  120. This option activates instrumentation for the entire kernel.
  121. If you don't enable this option, you have to explicitly specify
  122. UBSAN_SANITIZE := y for the files/directories you want to check for UB.
  123. Enabling this option will get kernel image size increased
  124. significantly.
  125. config TEST_UBSAN
  126. tristate "Module for testing for undefined behavior detection"
  127. depends on m
  128. help
  129. This is a test module for UBSAN.
  130. It triggers various undefined behavior, and detect it.
  131. endif # if UBSAN