^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) # SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) config ARCH_HAS_UBSAN_SANITIZE_ALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) menuconfig UBSAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) bool "Undefined behaviour sanity checker"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) This option enables the Undefined Behaviour sanity checker.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) Compile-time instrumentation is used to detect various undefined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) behaviours at runtime. For more details, see:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) Documentation/dev-tools/ubsan.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) if UBSAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) config UBSAN_TRAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) bool "On Sanitizer warnings, abort the running kernel code"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) depends on !COMPILE_TEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) depends on $(cc-option, -fsanitize-undefined-trap-on-error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) Building kernels with Sanitizer features enabled tends to grow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) the kernel size by around 5%, due to adding all the debugging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) text on failure paths. To avoid this, Sanitizer instrumentation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) can just issue a trap. This reduces the kernel size overhead but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) turns all warnings (including potentially harmless conditions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) into full exceptions that abort the running kernel code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) (regardless of context, locks held, etc), which may destabilize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) the system. For some system builders this is an acceptable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) trade-off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) config UBSAN_KCOV_BROKEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) depends on CC_IS_CLANG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=bounds -fsanitize-coverage=trace-pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) Some versions of clang support either UBSAN or KCOV but not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) combination of the two.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) in newer releases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) config CC_HAS_UBSAN_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) def_bool $(cc-option,-fsanitize=bounds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) config CC_HAS_UBSAN_ARRAY_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) def_bool $(cc-option,-fsanitize=array-bounds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) config UBSAN_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) bool "Perform array index bounds checking"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) default UBSAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) depends on !UBSAN_KCOV_BROKEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) depends on CC_HAS_UBSAN_ARRAY_BOUNDS || CC_HAS_UBSAN_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) This option enables detection of directly indexed out of bounds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) array accesses, where the array size is known at compile time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) Note that this does not protect array overflows via bad calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) to the {str,mem}*cpy() family of functions (that is addressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) by CONFIG_FORTIFY_SOURCE).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) config UBSAN_ONLY_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) def_bool CC_HAS_UBSAN_BOUNDS && !CC_HAS_UBSAN_ARRAY_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) depends on UBSAN_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) This is a weird case: Clang's -fsanitize=bounds includes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) -fsanitize=local-bounds, but it's trapping-only, so for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) Clang, we must use -fsanitize=array-bounds when we want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) traditional array bounds checking enabled. For GCC, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) want -fsanitize=bounds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) config UBSAN_ARRAY_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) def_bool CC_HAS_UBSAN_ARRAY_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) depends on UBSAN_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) config UBSAN_LOCAL_BOUNDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) bool "Perform array local bounds checking"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) depends on UBSAN_TRAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) depends on !UBSAN_KCOV_BROKEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) depends on $(cc-option,-fsanitize=local-bounds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) This option enables -fsanitize=local-bounds which traps when an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) exception/error is detected. Therefore, it may only be enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) with CONFIG_UBSAN_TRAP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) Enabling this option detects errors due to accesses through a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pointer that is derived from an object of a statically-known size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) where an added offset (which may not be known statically) is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) out-of-bounds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) config UBSAN_SHIFT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) bool "Perform checking for bit-shift overflows"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) default UBSAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) depends on $(cc-option,-fsanitize=shift)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) This option enables -fsanitize=shift which checks for bit-shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) operations that overflow to the left or go switch to negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) for signed types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) config UBSAN_DIV_ZERO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) bool "Perform checking for integer divide-by-zero"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) depends on $(cc-option,-fsanitize=integer-divide-by-zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) This option enables -fsanitize=integer-divide-by-zero which checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) for integer division by zero. This is effectively redundant with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) kernel's existing exception handling, though it can provide greater
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) debugging information under CONFIG_UBSAN_REPORT_FULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) config UBSAN_UNREACHABLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) bool "Perform checking for unreachable code"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) # objtool already handles unreachable checking and gets angry about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) # seeing UBSan instrumentation located in unreachable places.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) depends on !STACK_VALIDATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) depends on $(cc-option,-fsanitize=unreachable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) This option enables -fsanitize=unreachable which checks for control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) flow reaching an expected-to-be-unreachable position.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) config UBSAN_OBJECT_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) bool "Perform checking for accesses beyond the end of objects"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) default UBSAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) # gcc hugely expands stack usage with -fsanitize=object-size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) # https://lore.kernel.org/lkml/CAHk-=wjPasyJrDuwDnpHJS2TuQfExwe=px-SzLeN8GFMAQJPmQ@mail.gmail.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) depends on !CC_IS_GCC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) depends on $(cc-option,-fsanitize=object-size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) This option enables -fsanitize=object-size which checks for accesses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) beyond the end of objects where the optimizer can determine both the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) object being operated on and its size, usually seen with bad downcasts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) or access to struct members from NULL pointers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) config UBSAN_BOOL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) bool "Perform checking for non-boolean values used as boolean"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) default UBSAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) depends on $(cc-option,-fsanitize=bool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) This option enables -fsanitize=bool which checks for boolean values being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) loaded that are neither 0 nor 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) config UBSAN_ENUM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) bool "Perform checking for out of bounds enum values"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) default UBSAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) depends on $(cc-option,-fsanitize=enum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) This option enables -fsanitize=enum which checks for values being loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) into an enum that are outside the range of given values for the given enum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) config UBSAN_ALIGNMENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) bool "Perform checking for misaligned pointer usage"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) default !HAVE_EFFICIENT_UNALIGNED_ACCESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) depends on !UBSAN_TRAP && !COMPILE_TEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) depends on $(cc-option,-fsanitize=alignment)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) This option enables the check of unaligned memory accesses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) Enabling this option on architectures that support unaligned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) accesses may produce a lot of false positives.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) config UBSAN_SANITIZE_ALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) bool "Enable instrumentation for the entire kernel"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) depends on ARCH_HAS_UBSAN_SANITIZE_ALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) default y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) This option activates instrumentation for the entire kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) If you don't enable this option, you have to explicitly specify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) UBSAN_SANITIZE := y for the files/directories you want to check for UB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) Enabling this option will get kernel image size increased
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) significantly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) config TEST_UBSAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) tristate "Module for testing for undefined behavior detection"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) depends on m
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) This is a test module for UBSAN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) It triggers various undefined behavior, and detect it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) endif # if UBSAN