|
12 | 12 | KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
|
13 | 13 |
|
14 | 14 | cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
|
| 15 | +rustc-param = $(call rustc-option, -Cllvm-args=-$(1),) |
| 16 | + |
| 17 | +check-args = $(foreach arg,$(2),$(call $(1),$(arg))) |
| 18 | + |
| 19 | +kasan_params := |
15 | 20 |
|
16 | 21 | ifdef CONFIG_KASAN_STACK
|
17 | 22 | stack_enable := 1
|
@@ -41,39 +46,59 @@ CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
|
41 | 46 | $(call cc-option, -fsanitize=kernel-address \
|
42 | 47 | -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
|
43 | 48 |
|
44 |
| -# Now, add other parameters enabled similarly in both GCC and Clang. |
45 |
| -# As some of them are not supported by older compilers, use cc-param. |
46 |
| -CFLAGS_KASAN += $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \ |
47 |
| - $(call cc-param,asan-stack=$(stack_enable)) \ |
48 |
| - $(call cc-param,asan-instrument-allocas=1) \ |
49 |
| - $(call cc-param,asan-globals=1) |
| 49 | +# The minimum supported `rustc` version has a minimum supported LLVM |
| 50 | +# version late enough that we can assume support for -asan-mapping-offset. |
| 51 | +RUSTFLAGS_KASAN := -Zsanitizer=kernel-address \ |
| 52 | + -Zsanitizer-recover=kernel-address \ |
| 53 | + -Cllvm-args=-asan-mapping-offset=$(KASAN_SHADOW_OFFSET) |
| 54 | + |
| 55 | +# Now, add other parameters enabled similarly in GCC, Clang, and rustc. |
| 56 | +# As some of them are not supported by older compilers, these will be filtered |
| 57 | +# through `cc-param` or `rust-param` as applicable. |
| 58 | +kasan_params += asan-instrumentation-with-call-threshold=$(call_threshold) \ |
| 59 | + asan-stack=$(stack_enable) \ |
| 60 | + asan-instrument-allocas=1 \ |
| 61 | + asan-globals=1 |
50 | 62 |
|
51 | 63 | # Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
|
52 | 64 | # instead. With compilers that don't support this option, compiler-inserted
|
53 | 65 | # memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
|
54 |
| -CFLAGS_KASAN += $(call cc-param,asan-kernel-mem-intrinsic-prefix=1) |
| 66 | +kasan_params += asan-kernel-mem-intrinsic-prefix=1 |
55 | 67 |
|
56 | 68 | endif # CONFIG_KASAN_GENERIC
|
57 | 69 |
|
58 | 70 | ifdef CONFIG_KASAN_SW_TAGS
|
59 | 71 |
|
| 72 | +CFLAGS_KASAN := -fsanitize=kernel-hwaddress |
| 73 | + |
| 74 | +# This sets flags that will enable SW_TAGS KASAN once enabled in Rust. These |
| 75 | +# will not work today, and is guarded against in dependencies for CONFIG_RUST. |
| 76 | +RUSTFLAGS_KASAN := -Zsanitizer=kernel-hwaddress \ |
| 77 | + -Zsanitizer-recover=kernel-hwaddress |
| 78 | + |
60 | 79 | ifdef CONFIG_KASAN_INLINE
|
61 |
| - instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET)) |
| 80 | + kasan_params += hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET) |
62 | 81 | else
|
63 |
| - instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1) |
| 82 | + kasan_params += hwasan-instrument-with-calls=1 |
64 | 83 | endif
|
65 | 84 |
|
66 |
| -CFLAGS_KASAN := -fsanitize=kernel-hwaddress \ |
67 |
| - $(call cc-param,hwasan-instrument-stack=$(stack_enable)) \ |
68 |
| - $(call cc-param,hwasan-use-short-granules=0) \ |
69 |
| - $(call cc-param,hwasan-inline-all-checks=0) \ |
70 |
| - $(instrumentation_flags) |
| 85 | +kasan_params += hwasan-instrument-stack=$(stack_enable) \ |
| 86 | + hwasan-use-short-granules=0 \ |
| 87 | + hwasan-inline-all-checks=0 |
71 | 88 |
|
72 | 89 | # Instrument memcpy/memset/memmove calls by using instrumented __hwasan_mem*().
|
73 | 90 | ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y)
|
74 |
| - CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1) |
| 91 | + kasan_params += hwasan-kernel-mem-intrinsic-prefix=1 |
75 | 92 | endif
|
76 | 93 |
|
77 | 94 | endif # CONFIG_KASAN_SW_TAGS
|
78 | 95 |
|
79 |
| -export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE |
| 96 | +# Add all as-supported KASAN LLVM parameters requested by the configuration. |
| 97 | +CFLAGS_KASAN += $(call check-args, cc-param, $(kasan_params)) |
| 98 | + |
| 99 | +ifdef CONFIG_RUST |
| 100 | + # Avoid calling `rustc-param` unless Rust is enabled. |
| 101 | + RUSTFLAGS_KASAN += $(call check-args, rustc-param, $(kasan_params)) |
| 102 | +endif # CONFIG_RUST |
| 103 | + |
| 104 | +export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE RUSTFLAGS_KASAN |
0 commit comments