mirror of
git://git.yoctoproject.org/linux-yocto.git
synced 2025-10-22 15:03:53 +02:00

commit 51337a9a3a404fde0f5337662ffc7699793dfeb5 upstream. GCC doesn't support "hwasan-kernel-mem-intrinsic-prefix", only "asan-kernel-mem-intrinsic-prefix"[0], while LLVM supports both. This is already taken into account when checking "CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX", but not in the KASAN Makefile adding those parameters when "CONFIG_KASAN_SW_TAGS" is enabled. Replace the version check with "CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX", which already validates that mem-intrinsic prefix parameter can be used, and choose the correct name depending on compiler. GCC 13 and above trigger "CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX" which prevents `mem{cpy,move,set}()` being redefined in "mm/kasan/shadow.c" since commit36be5cba99
("kasan: treat meminstrinsic as builtins in uninstrumented files"), as we expect the compiler to prefix those calls with `__(hw)asan_` instead. But as the option passed to GCC has been incorrect, the compiler has not been emitting those prefixes, effectively never calling the instrumented versions of `mem{cpy,move,set}()` with "CONFIG_KASAN_SW_TAGS" enabled. If "CONFIG_FORTIFY_SOURCES" is enabled, this issue would be mitigated as it redefines `mem{cpy,move,set}()` and properly aliases the `__underlying_mem*()` that will be called to the instrumented versions. Link: https://lkml.kernel.org/r/20250821120735.156244-1-ada.coupriediaz@arm.com Link: https://gcc.gnu.org/onlinedocs/gcc-13.4.0/gcc/Optimize-Options.html [0] Signed-off-by: Ada Couprie Diaz <ada.coupriediaz@arm.com> Fixes:36be5cba99
("kasan: treat meminstrinsic as builtins in uninstrumented files") Reviewed-by: Yeoreum Yun <yeoreum.yun@arm.com> Cc: Alexander Potapenko <glider@google.com> Cc: Andrey Konovalov <andreyknvl@gmail.com> Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com> Cc: Dmitriy Vyukov <dvyukov@google.com> Cc: Marco Elver <elver@google.com> Cc: Marc Rutland <mark.rutland@arm.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Nathan Chancellor <nathan@kernel.org> Cc: Vincenzo Frascino <vincenzo.frascino@arm.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
109 lines
3.8 KiB
Makefile
109 lines
3.8 KiB
Makefile
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
|
|
# Safe for compiler to generate meminstrinsic calls in uninstrumented files.
|
|
CFLAGS_KASAN_NOSANITIZE :=
|
|
else
|
|
# Don't let compiler generate memintrinsic calls in uninstrumented files
|
|
# because they are instrumented.
|
|
CFLAGS_KASAN_NOSANITIZE := -fno-builtin
|
|
endif
|
|
|
|
KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
|
|
|
|
cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
|
|
rustc-param = $(call rustc-option, -Cllvm-args=-$(1),)
|
|
|
|
check-args = $(foreach arg,$(2),$(call $(1),$(arg)))
|
|
|
|
kasan_params :=
|
|
|
|
ifdef CONFIG_KASAN_STACK
|
|
stack_enable := 1
|
|
else
|
|
stack_enable := 0
|
|
endif
|
|
|
|
ifdef CONFIG_KASAN_GENERIC
|
|
|
|
ifdef CONFIG_KASAN_INLINE
|
|
# When the number of memory accesses in a function is less than this
|
|
# call threshold number, the compiler will use inline instrumentation.
|
|
# 10000 is chosen offhand as a sufficiently large number to make all
|
|
# kernel functions to be instrumented inline.
|
|
call_threshold := 10000
|
|
else
|
|
call_threshold := 0
|
|
endif
|
|
|
|
# First, enable -fsanitize=kernel-address together with providing the shadow
|
|
# mapping offset, as for GCC, -fasan-shadow-offset fails without -fsanitize
|
|
# (GCC accepts the shadow mapping offset via -fasan-shadow-offset instead of
|
|
# a --param like the other KASAN parameters).
|
|
# Instead of ifdef-checking the compiler, rely on cc-option.
|
|
CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
|
|
-fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
|
|
$(call cc-option, -fsanitize=kernel-address \
|
|
-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
|
|
|
|
# The minimum supported `rustc` version has a minimum supported LLVM
|
|
# version late enough that we can assume support for -asan-mapping-offset.
|
|
RUSTFLAGS_KASAN := -Zsanitizer=kernel-address \
|
|
-Zsanitizer-recover=kernel-address \
|
|
-Cllvm-args=-asan-mapping-offset=$(KASAN_SHADOW_OFFSET)
|
|
|
|
# Now, add other parameters enabled similarly in GCC, Clang, and rustc.
|
|
# As some of them are not supported by older compilers, these will be filtered
|
|
# through `cc-param` or `rust-param` as applicable.
|
|
kasan_params += asan-instrumentation-with-call-threshold=$(call_threshold) \
|
|
asan-stack=$(stack_enable) \
|
|
asan-instrument-allocas=1 \
|
|
asan-globals=1
|
|
|
|
# Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
|
|
# instead. With compilers that don't support this option, compiler-inserted
|
|
# memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
|
|
kasan_params += asan-kernel-mem-intrinsic-prefix=1
|
|
|
|
endif # CONFIG_KASAN_GENERIC
|
|
|
|
ifdef CONFIG_KASAN_SW_TAGS
|
|
|
|
CFLAGS_KASAN := -fsanitize=kernel-hwaddress
|
|
|
|
# This sets flags that will enable SW_TAGS KASAN once enabled in Rust. These
|
|
# will not work today, and is guarded against in dependencies for CONFIG_RUST.
|
|
RUSTFLAGS_KASAN := -Zsanitizer=kernel-hwaddress \
|
|
-Zsanitizer-recover=kernel-hwaddress
|
|
|
|
ifdef CONFIG_KASAN_INLINE
|
|
kasan_params += hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET)
|
|
else
|
|
kasan_params += hwasan-instrument-with-calls=1
|
|
endif
|
|
|
|
kasan_params += hwasan-instrument-stack=$(stack_enable) \
|
|
hwasan-use-short-granules=0 \
|
|
hwasan-inline-all-checks=0
|
|
|
|
# Instrument memcpy/memset/memmove calls by using instrumented __(hw)asan_mem*().
|
|
ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
|
|
ifdef CONFIG_CC_IS_GCC
|
|
kasan_params += asan-kernel-mem-intrinsic-prefix=1
|
|
else
|
|
kasan_params += hwasan-kernel-mem-intrinsic-prefix=1
|
|
endif
|
|
endif # CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
|
|
|
|
endif # CONFIG_KASAN_SW_TAGS
|
|
|
|
# Add all as-supported KASAN LLVM parameters requested by the configuration.
|
|
CFLAGS_KASAN += $(call check-args, cc-param, $(kasan_params))
|
|
|
|
ifdef CONFIG_RUST
|
|
# Avoid calling `rustc-param` unless Rust is enabled.
|
|
RUSTFLAGS_KASAN += $(call check-args, rustc-param, $(kasan_params))
|
|
endif # CONFIG_RUST
|
|
|
|
export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE RUSTFLAGS_KASAN
|