Makefile.kasan 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # SPDX-License-Identifier: GPL-2.0
  2. ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
  3. # Safe for compiler to generate meminstrinsic calls in uninstrumented files.
  4. CFLAGS_KASAN_NOSANITIZE :=
  5. else
  6. # Don't let compiler generate memintrinsic calls in uninstrumented files
  7. # because they are instrumented.
  8. CFLAGS_KASAN_NOSANITIZE := -fno-builtin
  9. endif
  10. KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
  11. cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
  12. rustc-param = $(call rustc-option, -Cllvm-args=-$(1),)
  13. check-args = $(foreach arg,$(2),$(call $(1),$(arg)))
  14. kasan_params :=
  15. ifdef CONFIG_KASAN_STACK
  16. stack_enable := 1
  17. else
  18. stack_enable := 0
  19. endif
  20. ifdef CONFIG_KASAN_GENERIC
  21. ifdef CONFIG_KASAN_INLINE
  22. # When the number of memory accesses in a function is less than this
  23. # call threshold number, the compiler will use inline instrumentation.
  24. # 10000 is chosen offhand as a sufficiently large number to make all
  25. # kernel functions to be instrumented inline.
  26. call_threshold := 10000
  27. else
  28. call_threshold := 0
  29. endif
  30. # First, enable -fsanitize=kernel-address together with providing the shadow
  31. # mapping offset, as for GCC, -fasan-shadow-offset fails without -fsanitize
  32. # (GCC accepts the shadow mapping offset via -fasan-shadow-offset instead of
  33. # a --param like the other KASAN parameters).
  34. # Instead of ifdef-checking the compiler, rely on cc-option.
  35. CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
  36. -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
  37. $(call cc-option, -fsanitize=kernel-address \
  38. -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
  39. # The minimum supported `rustc` version has a minimum supported LLVM
  40. # version late enough that we can assume support for -asan-mapping-offset.
  41. RUSTFLAGS_KASAN := -Zsanitizer=kernel-address \
  42. -Zsanitizer-recover=kernel-address \
  43. -Cllvm-args=-asan-mapping-offset=$(KASAN_SHADOW_OFFSET)
  44. # Now, add other parameters enabled similarly in GCC, Clang, and rustc.
  45. # As some of them are not supported by older compilers, these will be filtered
  46. # through `cc-param` or `rust-param` as applicable.
  47. kasan_params += asan-instrumentation-with-call-threshold=$(call_threshold) \
  48. asan-stack=$(stack_enable) \
  49. asan-instrument-allocas=1 \
  50. asan-globals=1
  51. # Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
  52. # instead. With compilers that don't support this option, compiler-inserted
  53. # memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
  54. kasan_params += asan-kernel-mem-intrinsic-prefix=1
  55. endif # CONFIG_KASAN_GENERIC
  56. ifdef CONFIG_KASAN_SW_TAGS
  57. CFLAGS_KASAN := -fsanitize=kernel-hwaddress
  58. # This sets flags that will enable SW_TAGS KASAN once enabled in Rust. These
  59. # will not work today, and is guarded against in dependencies for CONFIG_RUST.
  60. RUSTFLAGS_KASAN := -Zsanitizer=kernel-hwaddress \
  61. -Zsanitizer-recover=kernel-hwaddress
  62. ifdef CONFIG_KASAN_INLINE
  63. kasan_params += hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET)
  64. else
  65. kasan_params += hwasan-instrument-with-calls=1
  66. endif
  67. kasan_params += hwasan-instrument-stack=$(stack_enable) \
  68. hwasan-use-short-granules=0 \
  69. hwasan-inline-all-checks=0
  70. # Instrument memcpy/memset/memmove calls by using instrumented __(hw)asan_mem*().
  71. ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
  72. ifdef CONFIG_CC_IS_GCC
  73. kasan_params += asan-kernel-mem-intrinsic-prefix=1
  74. else
  75. kasan_params += hwasan-kernel-mem-intrinsic-prefix=1
  76. endif
  77. endif # CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
  78. endif # CONFIG_KASAN_SW_TAGS
  79. # Add all as-supported KASAN LLVM parameters requested by the configuration.
  80. CFLAGS_KASAN += $(call check-args, cc-param, $(kasan_params))
  81. ifdef CONFIG_RUST
  82. # Avoid calling `rustc-param` unless Rust is enabled.
  83. RUSTFLAGS_KASAN += $(call check-args, rustc-param, $(kasan_params))
  84. endif # CONFIG_RUST
  85. export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE RUSTFLAGS_KASAN